blob: 448c034184e275619732d23c01a01f83aad44ba7 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300283
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700286
Johannes Berg6c739412011-11-03 09:27:01 +0100287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300301 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300302 [NL80211_ATTR_STA_FLAGS2] = {
303 .len = sizeof(struct nl80211_sta_flag_update),
304 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300305 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300306 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
307 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200308 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
309 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
310 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200311 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100312 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100313 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
314 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100315 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
316 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200317 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200318 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
319 .len = IEEE80211_MAX_DATA_LEN },
320 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200321 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200322 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300323 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200324 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300325 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
326 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200327 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900328 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
329 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100330 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100331 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100332 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200333 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700334 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300335 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200336 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200337 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300338 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300339 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
340 .len = IEEE80211_MAX_DATA_LEN },
341 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530343 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300344 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530345 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300346 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
349 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100351 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200352 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
353 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700354 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800355 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
356 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
357 .len = NL80211_HT_CAPABILITY_LEN
358 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100359 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530360 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530361 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200362 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700363 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300364 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000365 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700366 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100367 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
368 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530369 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
370 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200371 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
372 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100373 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100374 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
375 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
376 .len = NL80211_VHT_CAPABILITY_LEN,
377 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200378 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
379 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
380 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg55682962007-09-20 13:09:35 -0400381};
382
Johannes Berge31b8212010-10-05 19:39:30 +0200383/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000384static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200385 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200386 [NL80211_KEY_IDX] = { .type = NLA_U8 },
387 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200388 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200389 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
390 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200391 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100392 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
393};
394
395/* policy for the key default flags */
396static const struct nla_policy
397nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
398 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
399 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200400};
401
Johannes Bergff1b6e62011-05-04 15:37:28 +0200402/* policy for WoWLAN attributes */
403static const struct nla_policy
404nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
405 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
406 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
407 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
408 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200409 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
410 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
411 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
412 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100413 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
414};
415
416static const struct nla_policy
417nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
418 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
419 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
420 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
421 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
422 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
423 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
424 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
425 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
426 },
427 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
428 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
429 },
430 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
431 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
432 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200433};
434
Johannes Berge5497d72011-07-05 16:35:40 +0200435/* policy for GTK rekey offload attributes */
436static const struct nla_policy
437nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
438 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
439 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
440 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
441};
442
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300443static const struct nla_policy
444nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200445 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300446 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700447 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300448};
449
Johannes Berg97990a02013-04-19 01:02:55 +0200450static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
451 struct netlink_callback *cb,
452 struct cfg80211_registered_device **rdev,
453 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100454{
Johannes Berg67748892010-10-04 21:14:06 +0200455 int err;
456
Johannes Berg67748892010-10-04 21:14:06 +0200457 rtnl_lock();
Johannes Berg97990a02013-04-19 01:02:55 +0200458 mutex_lock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200459
Johannes Berg97990a02013-04-19 01:02:55 +0200460 if (!cb->args[0]) {
461 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
462 nl80211_fam.attrbuf, nl80211_fam.maxattr,
463 nl80211_policy);
464 if (err)
465 goto out_unlock;
466
467 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
468 nl80211_fam.attrbuf);
469 if (IS_ERR(*wdev)) {
470 err = PTR_ERR(*wdev);
471 goto out_unlock;
472 }
473 *rdev = wiphy_to_dev((*wdev)->wiphy);
Johannes Berg289813a2013-07-30 22:34:28 +0200474 /* 0 is the first index - add 1 to parse only once */
475 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200476 cb->args[1] = (*wdev)->identifier;
477 } else {
Johannes Berg289813a2013-07-30 22:34:28 +0200478 /* subtract the 1 again here */
479 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200480 struct wireless_dev *tmp;
481
482 if (!wiphy) {
483 err = -ENODEV;
484 goto out_unlock;
485 }
486 *rdev = wiphy_to_dev(wiphy);
487 *wdev = NULL;
488
489 mutex_lock(&(*rdev)->devlist_mtx);
490 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
491 if (tmp->identifier == cb->args[1]) {
492 *wdev = tmp;
493 break;
494 }
495 }
496 mutex_unlock(&(*rdev)->devlist_mtx);
497
498 if (!*wdev) {
499 err = -ENODEV;
500 goto out_unlock;
501 }
Johannes Berg67748892010-10-04 21:14:06 +0200502 }
503
Johannes Berg97990a02013-04-19 01:02:55 +0200504 cfg80211_lock_rdev(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200505
Johannes Berg97990a02013-04-19 01:02:55 +0200506 mutex_unlock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200507 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200508 out_unlock:
509 mutex_unlock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200510 rtnl_unlock();
511 return err;
512}
513
Johannes Berg97990a02013-04-19 01:02:55 +0200514static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200515{
516 cfg80211_unlock_rdev(rdev);
517 rtnl_unlock();
518}
519
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100520/* IE validation */
521static bool is_valid_ie_attr(const struct nlattr *attr)
522{
523 const u8 *pos;
524 int len;
525
526 if (!attr)
527 return true;
528
529 pos = nla_data(attr);
530 len = nla_len(attr);
531
532 while (len) {
533 u8 elemlen;
534
535 if (len < 2)
536 return false;
537 len -= 2;
538
539 elemlen = pos[1];
540 if (elemlen > len)
541 return false;
542
543 len -= elemlen;
544 pos += 2 + elemlen;
545 }
546
547 return true;
548}
549
Johannes Berg55682962007-09-20 13:09:35 -0400550/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000551static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400552 int flags, u8 cmd)
553{
554 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000555 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400556}
557
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400558static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100559 struct ieee80211_channel *chan,
560 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400561{
David S. Miller9360ffd2012-03-29 04:41:26 -0400562 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
563 chan->center_freq))
564 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400565
David S. Miller9360ffd2012-03-29 04:41:26 -0400566 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
567 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
568 goto nla_put_failure;
569 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
570 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
571 goto nla_put_failure;
572 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
573 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
574 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100575 if (chan->flags & IEEE80211_CHAN_RADAR) {
576 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
577 goto nla_put_failure;
578 if (large) {
579 u32 time;
580
581 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
582
583 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
584 chan->dfs_state))
585 goto nla_put_failure;
586 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
587 time))
588 goto nla_put_failure;
589 }
590 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400591
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100592 if (large) {
593 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
594 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
595 goto nla_put_failure;
596 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
597 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
598 goto nla_put_failure;
599 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
600 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
601 goto nla_put_failure;
602 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
603 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
604 goto nla_put_failure;
605 }
606
David S. Miller9360ffd2012-03-29 04:41:26 -0400607 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
608 DBM_TO_MBM(chan->max_power)))
609 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400610
611 return 0;
612
613 nla_put_failure:
614 return -ENOBUFS;
615}
616
Johannes Berg55682962007-09-20 13:09:35 -0400617/* netlink command implementations */
618
Johannes Bergb9454e82009-07-08 13:29:08 +0200619struct key_parse {
620 struct key_params p;
621 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200622 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200623 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100624 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200625};
626
627static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
628{
629 struct nlattr *tb[NL80211_KEY_MAX + 1];
630 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
631 nl80211_key_policy);
632 if (err)
633 return err;
634
635 k->def = !!tb[NL80211_KEY_DEFAULT];
636 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
637
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100638 if (k->def) {
639 k->def_uni = true;
640 k->def_multi = true;
641 }
642 if (k->defmgmt)
643 k->def_multi = true;
644
Johannes Bergb9454e82009-07-08 13:29:08 +0200645 if (tb[NL80211_KEY_IDX])
646 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
647
648 if (tb[NL80211_KEY_DATA]) {
649 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
650 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
651 }
652
653 if (tb[NL80211_KEY_SEQ]) {
654 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
655 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
656 }
657
658 if (tb[NL80211_KEY_CIPHER])
659 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
660
Johannes Berge31b8212010-10-05 19:39:30 +0200661 if (tb[NL80211_KEY_TYPE]) {
662 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
663 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
664 return -EINVAL;
665 }
666
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100667 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
668 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100669 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
670 tb[NL80211_KEY_DEFAULT_TYPES],
671 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100672 if (err)
673 return err;
674
675 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
676 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
677 }
678
Johannes Bergb9454e82009-07-08 13:29:08 +0200679 return 0;
680}
681
682static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
683{
684 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
685 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
686 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
687 }
688
689 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
690 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
691 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
692 }
693
694 if (info->attrs[NL80211_ATTR_KEY_IDX])
695 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
696
697 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
698 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
699
700 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
701 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
702
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100703 if (k->def) {
704 k->def_uni = true;
705 k->def_multi = true;
706 }
707 if (k->defmgmt)
708 k->def_multi = true;
709
Johannes Berge31b8212010-10-05 19:39:30 +0200710 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
711 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
712 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
713 return -EINVAL;
714 }
715
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100716 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
717 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
718 int err = nla_parse_nested(
719 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
720 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
721 nl80211_key_default_policy);
722 if (err)
723 return err;
724
725 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
726 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
727 }
728
Johannes Bergb9454e82009-07-08 13:29:08 +0200729 return 0;
730}
731
732static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
733{
734 int err;
735
736 memset(k, 0, sizeof(*k));
737 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200738 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200739
740 if (info->attrs[NL80211_ATTR_KEY])
741 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
742 else
743 err = nl80211_parse_key_old(info, k);
744
745 if (err)
746 return err;
747
748 if (k->def && k->defmgmt)
749 return -EINVAL;
750
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100751 if (k->defmgmt) {
752 if (k->def_uni || !k->def_multi)
753 return -EINVAL;
754 }
755
Johannes Bergb9454e82009-07-08 13:29:08 +0200756 if (k->idx != -1) {
757 if (k->defmgmt) {
758 if (k->idx < 4 || k->idx > 5)
759 return -EINVAL;
760 } else if (k->def) {
761 if (k->idx < 0 || k->idx > 3)
762 return -EINVAL;
763 } else {
764 if (k->idx < 0 || k->idx > 5)
765 return -EINVAL;
766 }
767 }
768
769 return 0;
770}
771
Johannes Bergfffd0932009-07-08 14:22:54 +0200772static struct cfg80211_cached_keys *
773nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530774 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200775{
776 struct key_parse parse;
777 struct nlattr *key;
778 struct cfg80211_cached_keys *result;
779 int rem, err, def = 0;
780
781 result = kzalloc(sizeof(*result), GFP_KERNEL);
782 if (!result)
783 return ERR_PTR(-ENOMEM);
784
785 result->def = -1;
786 result->defmgmt = -1;
787
788 nla_for_each_nested(key, keys, rem) {
789 memset(&parse, 0, sizeof(parse));
790 parse.idx = -1;
791
792 err = nl80211_parse_key_new(key, &parse);
793 if (err)
794 goto error;
795 err = -EINVAL;
796 if (!parse.p.key)
797 goto error;
798 if (parse.idx < 0 || parse.idx > 4)
799 goto error;
800 if (parse.def) {
801 if (def)
802 goto error;
803 def = 1;
804 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100805 if (!parse.def_uni || !parse.def_multi)
806 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200807 } else if (parse.defmgmt)
808 goto error;
809 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200810 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200811 if (err)
812 goto error;
813 result->params[parse.idx].cipher = parse.p.cipher;
814 result->params[parse.idx].key_len = parse.p.key_len;
815 result->params[parse.idx].key = result->data[parse.idx];
816 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530817
818 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
819 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
820 if (no_ht)
821 *no_ht = true;
822 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200823 }
824
825 return result;
826 error:
827 kfree(result);
828 return ERR_PTR(err);
829}
830
831static int nl80211_key_allowed(struct wireless_dev *wdev)
832{
833 ASSERT_WDEV_LOCK(wdev);
834
Johannes Bergfffd0932009-07-08 14:22:54 +0200835 switch (wdev->iftype) {
836 case NL80211_IFTYPE_AP:
837 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200838 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700839 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200840 break;
841 case NL80211_IFTYPE_ADHOC:
842 if (!wdev->current_bss)
843 return -ENOLINK;
844 break;
845 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200846 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200847 if (wdev->sme_state != CFG80211_SME_CONNECTED)
848 return -ENOLINK;
849 break;
850 default:
851 return -EINVAL;
852 }
853
854 return 0;
855}
856
Johannes Berg7527a782011-05-13 10:58:57 +0200857static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
858{
859 struct nlattr *nl_modes = nla_nest_start(msg, attr);
860 int i;
861
862 if (!nl_modes)
863 goto nla_put_failure;
864
865 i = 0;
866 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400867 if ((ifmodes & 1) && nla_put_flag(msg, i))
868 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200869 ifmodes >>= 1;
870 i++;
871 }
872
873 nla_nest_end(msg, nl_modes);
874 return 0;
875
876nla_put_failure:
877 return -ENOBUFS;
878}
879
880static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100881 struct sk_buff *msg,
882 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200883{
884 struct nlattr *nl_combis;
885 int i, j;
886
887 nl_combis = nla_nest_start(msg,
888 NL80211_ATTR_INTERFACE_COMBINATIONS);
889 if (!nl_combis)
890 goto nla_put_failure;
891
892 for (i = 0; i < wiphy->n_iface_combinations; i++) {
893 const struct ieee80211_iface_combination *c;
894 struct nlattr *nl_combi, *nl_limits;
895
896 c = &wiphy->iface_combinations[i];
897
898 nl_combi = nla_nest_start(msg, i + 1);
899 if (!nl_combi)
900 goto nla_put_failure;
901
902 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
903 if (!nl_limits)
904 goto nla_put_failure;
905
906 for (j = 0; j < c->n_limits; j++) {
907 struct nlattr *nl_limit;
908
909 nl_limit = nla_nest_start(msg, j + 1);
910 if (!nl_limit)
911 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400912 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
913 c->limits[j].max))
914 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200915 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
916 c->limits[j].types))
917 goto nla_put_failure;
918 nla_nest_end(msg, nl_limit);
919 }
920
921 nla_nest_end(msg, nl_limits);
922
David S. Miller9360ffd2012-03-29 04:41:26 -0400923 if (c->beacon_int_infra_match &&
924 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
925 goto nla_put_failure;
926 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
927 c->num_different_channels) ||
928 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
929 c->max_interfaces))
930 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100931 if (large &&
932 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
933 c->radar_detect_widths))
934 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200935
936 nla_nest_end(msg, nl_combi);
937 }
938
939 nla_nest_end(msg, nl_combis);
940
941 return 0;
942nla_put_failure:
943 return -ENOBUFS;
944}
945
Johannes Berg3713b4e2013-02-14 16:19:38 +0100946#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100947static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
948 struct sk_buff *msg)
949{
950 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
951 struct nlattr *nl_tcp;
952
953 if (!tcp)
954 return 0;
955
956 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
957 if (!nl_tcp)
958 return -ENOBUFS;
959
960 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
961 tcp->data_payload_max))
962 return -ENOBUFS;
963
964 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
965 tcp->data_payload_max))
966 return -ENOBUFS;
967
968 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
969 return -ENOBUFS;
970
971 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
972 sizeof(*tcp->tok), tcp->tok))
973 return -ENOBUFS;
974
975 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
976 tcp->data_interval_max))
977 return -ENOBUFS;
978
979 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
980 tcp->wake_payload_max))
981 return -ENOBUFS;
982
983 nla_nest_end(msg, nl_tcp);
984 return 0;
985}
986
Johannes Berg3713b4e2013-02-14 16:19:38 +0100987static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +0100988 struct cfg80211_registered_device *dev,
989 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100990{
991 struct nlattr *nl_wowlan;
992
993 if (!dev->wiphy.wowlan.flags && !dev->wiphy.wowlan.n_patterns)
994 return 0;
995
996 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
997 if (!nl_wowlan)
998 return -ENOBUFS;
999
1000 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1001 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1002 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1003 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1004 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1005 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1006 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1007 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1008 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1009 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1010 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1011 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1012 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1013 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1014 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1015 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1016 return -ENOBUFS;
1017
1018 if (dev->wiphy.wowlan.n_patterns) {
1019 struct nl80211_wowlan_pattern_support pat = {
1020 .max_patterns = dev->wiphy.wowlan.n_patterns,
1021 .min_pattern_len = dev->wiphy.wowlan.pattern_min_len,
1022 .max_pattern_len = dev->wiphy.wowlan.pattern_max_len,
1023 .max_pkt_offset = dev->wiphy.wowlan.max_pkt_offset,
1024 };
1025
1026 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1027 sizeof(pat), &pat))
1028 return -ENOBUFS;
1029 }
1030
Johannes Bergb56cf722013-02-20 01:02:38 +01001031 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1032 return -ENOBUFS;
1033
Johannes Berg3713b4e2013-02-14 16:19:38 +01001034 nla_nest_end(msg, nl_wowlan);
1035
1036 return 0;
1037}
1038#endif
1039
1040static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1041 struct ieee80211_supported_band *sband)
1042{
1043 struct nlattr *nl_rates, *nl_rate;
1044 struct ieee80211_rate *rate;
1045 int i;
1046
1047 /* add HT info */
1048 if (sband->ht_cap.ht_supported &&
1049 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1050 sizeof(sband->ht_cap.mcs),
1051 &sband->ht_cap.mcs) ||
1052 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1053 sband->ht_cap.cap) ||
1054 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1055 sband->ht_cap.ampdu_factor) ||
1056 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1057 sband->ht_cap.ampdu_density)))
1058 return -ENOBUFS;
1059
1060 /* add VHT info */
1061 if (sband->vht_cap.vht_supported &&
1062 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1063 sizeof(sband->vht_cap.vht_mcs),
1064 &sband->vht_cap.vht_mcs) ||
1065 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1066 sband->vht_cap.cap)))
1067 return -ENOBUFS;
1068
1069 /* add bitrates */
1070 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1071 if (!nl_rates)
1072 return -ENOBUFS;
1073
1074 for (i = 0; i < sband->n_bitrates; i++) {
1075 nl_rate = nla_nest_start(msg, i);
1076 if (!nl_rate)
1077 return -ENOBUFS;
1078
1079 rate = &sband->bitrates[i];
1080 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1081 rate->bitrate))
1082 return -ENOBUFS;
1083 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1084 nla_put_flag(msg,
1085 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1086 return -ENOBUFS;
1087
1088 nla_nest_end(msg, nl_rate);
1089 }
1090
1091 nla_nest_end(msg, nl_rates);
1092
1093 return 0;
1094}
1095
1096static int
1097nl80211_send_mgmt_stypes(struct sk_buff *msg,
1098 const struct ieee80211_txrx_stypes *mgmt_stypes)
1099{
1100 u16 stypes;
1101 struct nlattr *nl_ftypes, *nl_ifs;
1102 enum nl80211_iftype ift;
1103 int i;
1104
1105 if (!mgmt_stypes)
1106 return 0;
1107
1108 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1109 if (!nl_ifs)
1110 return -ENOBUFS;
1111
1112 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1113 nl_ftypes = nla_nest_start(msg, ift);
1114 if (!nl_ftypes)
1115 return -ENOBUFS;
1116 i = 0;
1117 stypes = mgmt_stypes[ift].tx;
1118 while (stypes) {
1119 if ((stypes & 1) &&
1120 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1121 (i << 4) | IEEE80211_FTYPE_MGMT))
1122 return -ENOBUFS;
1123 stypes >>= 1;
1124 i++;
1125 }
1126 nla_nest_end(msg, nl_ftypes);
1127 }
1128
1129 nla_nest_end(msg, nl_ifs);
1130
1131 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1132 if (!nl_ifs)
1133 return -ENOBUFS;
1134
1135 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1136 nl_ftypes = nla_nest_start(msg, ift);
1137 if (!nl_ftypes)
1138 return -ENOBUFS;
1139 i = 0;
1140 stypes = mgmt_stypes[ift].rx;
1141 while (stypes) {
1142 if ((stypes & 1) &&
1143 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1144 (i << 4) | IEEE80211_FTYPE_MGMT))
1145 return -ENOBUFS;
1146 stypes >>= 1;
1147 i++;
1148 }
1149 nla_nest_end(msg, nl_ftypes);
1150 }
1151 nla_nest_end(msg, nl_ifs);
1152
1153 return 0;
1154}
1155
1156static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1157 struct sk_buff *msg, u32 portid, u32 seq,
1158 int flags, bool split, long *split_start,
1159 long *band_start, long *chan_start)
Johannes Berg55682962007-09-20 13:09:35 -04001160{
1161 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001162 struct nlattr *nl_bands, *nl_band;
1163 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001164 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001165 enum ieee80211_band band;
1166 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001167 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001168 const struct ieee80211_txrx_stypes *mgmt_stypes =
1169 dev->wiphy.mgmt_stypes;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001170 long start = 0, start_chan = 0, start_band = 0;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001171 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001172
Eric W. Biederman15e47302012-09-07 20:12:54 +00001173 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001174 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001175 return -ENOBUFS;
1176
1177 /* allow always using the variables */
1178 if (!split) {
1179 split_start = &start;
1180 band_start = &start_band;
1181 chan_start = &start_chan;
1182 }
Johannes Berg55682962007-09-20 13:09:35 -04001183
David S. Miller9360ffd2012-03-29 04:41:26 -04001184 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001185 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1186 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001187 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001188 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001189 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001190
Johannes Berg3713b4e2013-02-14 16:19:38 +01001191 switch (*split_start) {
1192 case 0:
1193 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1194 dev->wiphy.retry_short) ||
1195 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1196 dev->wiphy.retry_long) ||
1197 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1198 dev->wiphy.frag_threshold) ||
1199 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1200 dev->wiphy.rts_threshold) ||
1201 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1202 dev->wiphy.coverage_class) ||
1203 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1204 dev->wiphy.max_scan_ssids) ||
1205 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1206 dev->wiphy.max_sched_scan_ssids) ||
1207 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1208 dev->wiphy.max_scan_ie_len) ||
1209 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1210 dev->wiphy.max_sched_scan_ie_len) ||
1211 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1212 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001213 goto nla_put_failure;
1214
Johannes Berg3713b4e2013-02-14 16:19:38 +01001215 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1216 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1217 goto nla_put_failure;
1218 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1219 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1220 goto nla_put_failure;
1221 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1222 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1223 goto nla_put_failure;
1224 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1225 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1226 goto nla_put_failure;
1227 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1228 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1229 goto nla_put_failure;
1230 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1231 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001232 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001233
Johannes Berg3713b4e2013-02-14 16:19:38 +01001234 (*split_start)++;
1235 if (split)
1236 break;
1237 case 1:
1238 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1239 sizeof(u32) * dev->wiphy.n_cipher_suites,
1240 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001241 goto nla_put_failure;
1242
Johannes Berg3713b4e2013-02-14 16:19:38 +01001243 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1244 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001245 goto nla_put_failure;
1246
Johannes Berg3713b4e2013-02-14 16:19:38 +01001247 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1248 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1249 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001250
Johannes Berg3713b4e2013-02-14 16:19:38 +01001251 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1252 dev->wiphy.available_antennas_tx) ||
1253 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1254 dev->wiphy.available_antennas_rx))
1255 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001256
Johannes Berg3713b4e2013-02-14 16:19:38 +01001257 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1258 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1259 dev->wiphy.probe_resp_offload))
1260 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001261
Johannes Berg3713b4e2013-02-14 16:19:38 +01001262 if ((dev->wiphy.available_antennas_tx ||
1263 dev->wiphy.available_antennas_rx) &&
1264 dev->ops->get_antenna) {
1265 u32 tx_ant = 0, rx_ant = 0;
1266 int res;
1267 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1268 if (!res) {
1269 if (nla_put_u32(msg,
1270 NL80211_ATTR_WIPHY_ANTENNA_TX,
1271 tx_ant) ||
1272 nla_put_u32(msg,
1273 NL80211_ATTR_WIPHY_ANTENNA_RX,
1274 rx_ant))
1275 goto nla_put_failure;
1276 }
Johannes Bergee688b002008-01-24 19:38:39 +01001277 }
1278
Johannes Berg3713b4e2013-02-14 16:19:38 +01001279 (*split_start)++;
1280 if (split)
1281 break;
1282 case 2:
1283 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1284 dev->wiphy.interface_modes))
1285 goto nla_put_failure;
1286 (*split_start)++;
1287 if (split)
1288 break;
1289 case 3:
1290 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1291 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001292 goto nla_put_failure;
1293
Johannes Berg3713b4e2013-02-14 16:19:38 +01001294 for (band = *band_start; band < IEEE80211_NUM_BANDS; band++) {
1295 struct ieee80211_supported_band *sband;
1296
1297 sband = dev->wiphy.bands[band];
1298
1299 if (!sband)
1300 continue;
1301
1302 nl_band = nla_nest_start(msg, band);
1303 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001304 goto nla_put_failure;
1305
Johannes Berg3713b4e2013-02-14 16:19:38 +01001306 switch (*chan_start) {
1307 case 0:
1308 if (nl80211_send_band_rateinfo(msg, sband))
1309 goto nla_put_failure;
1310 (*chan_start)++;
1311 if (split)
1312 break;
1313 default:
1314 /* add frequencies */
1315 nl_freqs = nla_nest_start(
1316 msg, NL80211_BAND_ATTR_FREQS);
1317 if (!nl_freqs)
1318 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001319
Johannes Berg3713b4e2013-02-14 16:19:38 +01001320 for (i = *chan_start - 1;
1321 i < sband->n_channels;
1322 i++) {
1323 nl_freq = nla_nest_start(msg, i);
1324 if (!nl_freq)
1325 goto nla_put_failure;
1326
1327 chan = &sband->channels[i];
1328
Johannes Bergcdc89b92013-02-18 23:54:36 +01001329 if (nl80211_msg_put_channel(msg, chan,
1330 split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001331 goto nla_put_failure;
1332
1333 nla_nest_end(msg, nl_freq);
1334 if (split)
1335 break;
1336 }
1337 if (i < sband->n_channels)
1338 *chan_start = i + 2;
1339 else
1340 *chan_start = 0;
1341 nla_nest_end(msg, nl_freqs);
1342 }
1343
1344 nla_nest_end(msg, nl_band);
1345
1346 if (split) {
1347 /* start again here */
1348 if (*chan_start)
1349 band--;
1350 break;
1351 }
Johannes Bergee688b002008-01-24 19:38:39 +01001352 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001353 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001354
Johannes Berg3713b4e2013-02-14 16:19:38 +01001355 if (band < IEEE80211_NUM_BANDS)
1356 *band_start = band + 1;
1357 else
1358 *band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001359
Johannes Berg3713b4e2013-02-14 16:19:38 +01001360 /* if bands & channels are done, continue outside */
1361 if (*band_start == 0 && *chan_start == 0)
1362 (*split_start)++;
1363 if (split)
1364 break;
1365 case 4:
1366 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1367 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001368 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001369
1370 i = 0;
1371#define CMD(op, n) \
1372 do { \
1373 if (dev->ops->op) { \
1374 i++; \
1375 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1376 goto nla_put_failure; \
1377 } \
1378 } while (0)
1379
1380 CMD(add_virtual_intf, NEW_INTERFACE);
1381 CMD(change_virtual_intf, SET_INTERFACE);
1382 CMD(add_key, NEW_KEY);
1383 CMD(start_ap, START_AP);
1384 CMD(add_station, NEW_STATION);
1385 CMD(add_mpath, NEW_MPATH);
1386 CMD(update_mesh_config, SET_MESH_CONFIG);
1387 CMD(change_bss, SET_BSS);
1388 CMD(auth, AUTHENTICATE);
1389 CMD(assoc, ASSOCIATE);
1390 CMD(deauth, DEAUTHENTICATE);
1391 CMD(disassoc, DISASSOCIATE);
1392 CMD(join_ibss, JOIN_IBSS);
1393 CMD(join_mesh, JOIN_MESH);
1394 CMD(set_pmksa, SET_PMKSA);
1395 CMD(del_pmksa, DEL_PMKSA);
1396 CMD(flush_pmksa, FLUSH_PMKSA);
1397 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1398 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1399 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1400 CMD(mgmt_tx, FRAME);
1401 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1402 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1403 i++;
1404 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1405 goto nla_put_failure;
1406 }
1407 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1408 dev->ops->join_mesh) {
1409 i++;
1410 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1411 goto nla_put_failure;
1412 }
1413 CMD(set_wds_peer, SET_WDS_PEER);
1414 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1415 CMD(tdls_mgmt, TDLS_MGMT);
1416 CMD(tdls_oper, TDLS_OPER);
1417 }
1418 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1419 CMD(sched_scan_start, START_SCHED_SCAN);
1420 CMD(probe_client, PROBE_CLIENT);
1421 CMD(set_noack_map, SET_NOACK_MAP);
1422 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1423 i++;
1424 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1425 goto nla_put_failure;
1426 }
1427 CMD(start_p2p_device, START_P2P_DEVICE);
1428 CMD(set_mcast_rate, SET_MCAST_RATE);
Arend van Spriel5de17982013-04-18 15:49:00 +02001429 if (split) {
1430 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1431 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1432 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001433
Kalle Valo4745fc02011-11-17 19:06:10 +02001434#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001435 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001436#endif
1437
Johannes Berg8fdc6212009-03-14 09:34:01 +01001438#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001439
Johannes Berg3713b4e2013-02-14 16:19:38 +01001440 if (dev->ops->connect || dev->ops->auth) {
1441 i++;
1442 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001443 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001444 }
1445
Johannes Berg3713b4e2013-02-14 16:19:38 +01001446 if (dev->ops->disconnect || dev->ops->deauth) {
1447 i++;
1448 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1449 goto nla_put_failure;
1450 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001451
Johannes Berg3713b4e2013-02-14 16:19:38 +01001452 nla_nest_end(msg, nl_cmds);
1453 (*split_start)++;
1454 if (split)
1455 break;
1456 case 5:
1457 if (dev->ops->remain_on_channel &&
1458 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1459 nla_put_u32(msg,
1460 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1461 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001462 goto nla_put_failure;
1463
Johannes Berg3713b4e2013-02-14 16:19:38 +01001464 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1465 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1466 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001467
Johannes Berg3713b4e2013-02-14 16:19:38 +01001468 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1469 goto nla_put_failure;
1470 (*split_start)++;
1471 if (split)
1472 break;
1473 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001474#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001475 if (nl80211_send_wowlan(msg, dev, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001476 goto nla_put_failure;
1477 (*split_start)++;
1478 if (split)
1479 break;
1480#else
1481 (*split_start)++;
1482#endif
1483 case 7:
1484 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1485 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001486 goto nla_put_failure;
1487
Johannes Bergcdc89b92013-02-18 23:54:36 +01001488 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001489 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001490
Johannes Berg3713b4e2013-02-14 16:19:38 +01001491 (*split_start)++;
1492 if (split)
1493 break;
1494 case 8:
1495 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1496 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1497 dev->wiphy.ap_sme_capa))
1498 goto nla_put_failure;
1499
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001500 features = dev->wiphy.features;
1501 /*
1502 * We can only add the per-channel limit information if the
1503 * dump is split, otherwise it makes it too big. Therefore
1504 * only advertise it in that case.
1505 */
1506 if (split)
1507 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1508 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001509 goto nla_put_failure;
1510
1511 if (dev->wiphy.ht_capa_mod_mask &&
1512 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1513 sizeof(*dev->wiphy.ht_capa_mod_mask),
1514 dev->wiphy.ht_capa_mod_mask))
1515 goto nla_put_failure;
1516
1517 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1518 dev->wiphy.max_acl_mac_addrs &&
1519 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1520 dev->wiphy.max_acl_mac_addrs))
1521 goto nla_put_failure;
1522
1523 /*
1524 * Any information below this point is only available to
1525 * applications that can deal with it being split. This
1526 * helps ensure that newly added capabilities don't break
1527 * older tools by overrunning their buffers.
1528 *
1529 * We still increment split_start so that in the split
1530 * case we'll continue with more data in the next round,
1531 * but break unconditionally so unsplit data stops here.
1532 */
1533 (*split_start)++;
1534 break;
1535 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001536 if (dev->wiphy.extended_capabilities &&
1537 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1538 dev->wiphy.extended_capabilities_len,
1539 dev->wiphy.extended_capabilities) ||
1540 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1541 dev->wiphy.extended_capabilities_len,
1542 dev->wiphy.extended_capabilities_mask)))
1543 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001544
Johannes Bergee2aca32013-02-21 17:36:01 +01001545 if (dev->wiphy.vht_capa_mod_mask &&
1546 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1547 sizeof(*dev->wiphy.vht_capa_mod_mask),
1548 dev->wiphy.vht_capa_mod_mask))
1549 goto nla_put_failure;
1550
Johannes Berg3713b4e2013-02-14 16:19:38 +01001551 /* done */
1552 *split_start = 0;
1553 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001554 }
Johannes Berg55682962007-09-20 13:09:35 -04001555 return genlmsg_end(msg, hdr);
1556
1557 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001558 genlmsg_cancel(msg, hdr);
1559 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001560}
1561
1562static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1563{
Johannes Berg645e77d2013-03-01 14:03:49 +01001564 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001565 int start = cb->args[0];
1566 struct cfg80211_registered_device *dev;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001567 s64 filter_wiphy = -1;
1568 bool split = false;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001569 struct nlattr **tb;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001570 int res;
Johannes Berg55682962007-09-20 13:09:35 -04001571
Johannes Berg3a5a4232013-06-19 10:09:57 +02001572 /* will be zeroed in nlmsg_parse() */
1573 tb = kmalloc(sizeof(*tb) * (NL80211_ATTR_MAX + 1), GFP_KERNEL);
1574 if (!tb)
1575 return -ENOMEM;
1576
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001577 mutex_lock(&cfg80211_mutex);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001578 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
Johannes Berg3a5a4232013-06-19 10:09:57 +02001579 tb, NL80211_ATTR_MAX, nl80211_policy);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001580 if (res == 0) {
1581 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1582 if (tb[NL80211_ATTR_WIPHY])
1583 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1584 if (tb[NL80211_ATTR_WDEV])
1585 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1586 if (tb[NL80211_ATTR_IFINDEX]) {
1587 struct net_device *netdev;
1588 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1589
1590 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1591 if (!netdev) {
1592 mutex_unlock(&cfg80211_mutex);
Johannes Berg3a5a4232013-06-19 10:09:57 +02001593 kfree(tb);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001594 return -ENODEV;
1595 }
1596 if (netdev->ieee80211_ptr) {
1597 dev = wiphy_to_dev(
1598 netdev->ieee80211_ptr->wiphy);
1599 filter_wiphy = dev->wiphy_idx;
1600 }
1601 dev_put(netdev);
1602 }
1603 }
Johannes Berg3a5a4232013-06-19 10:09:57 +02001604 kfree(tb);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001605
Johannes Berg79c97e92009-07-07 03:56:12 +02001606 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001607 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1608 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001609 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001610 continue;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001611 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1612 continue;
1613 /* attempt to fit multiple wiphy data chunks into the skb */
1614 do {
1615 ret = nl80211_send_wiphy(dev, skb,
1616 NETLINK_CB(cb->skb).portid,
1617 cb->nlh->nlmsg_seq,
1618 NLM_F_MULTI,
1619 split, &cb->args[1],
1620 &cb->args[2],
1621 &cb->args[3]);
1622 if (ret < 0) {
1623 /*
1624 * If sending the wiphy data didn't fit (ENOBUFS
1625 * or EMSGSIZE returned), this SKB is still
1626 * empty (so it's not too big because another
1627 * wiphy dataset is already in the skb) and
1628 * we've not tried to adjust the dump allocation
1629 * yet ... then adjust the alloc size to be
1630 * bigger, and return 1 but with the empty skb.
1631 * This results in an empty message being RX'ed
1632 * in userspace, but that is ignored.
1633 *
1634 * We can then retry with the larger buffer.
1635 */
1636 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1637 !skb->len &&
1638 cb->min_dump_alloc < 4096) {
1639 cb->min_dump_alloc = 4096;
1640 mutex_unlock(&cfg80211_mutex);
1641 return 1;
1642 }
1643 idx--;
1644 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001645 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001646 } while (cb->args[1] > 0);
1647 break;
Johannes Berg55682962007-09-20 13:09:35 -04001648 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001649 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001650
1651 cb->args[0] = idx;
1652
1653 return skb->len;
1654}
1655
1656static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1657{
1658 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001659 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001660
Johannes Berg645e77d2013-03-01 14:03:49 +01001661 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001662 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001663 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001664
Johannes Berg3713b4e2013-02-14 16:19:38 +01001665 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1666 false, NULL, NULL, NULL) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001667 nlmsg_free(msg);
1668 return -ENOBUFS;
1669 }
Johannes Berg55682962007-09-20 13:09:35 -04001670
Johannes Berg134e6372009-07-10 09:51:34 +00001671 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001672}
1673
Jouni Malinen31888482008-10-30 16:59:24 +02001674static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1675 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1676 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1677 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1678 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1679 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1680};
1681
1682static int parse_txq_params(struct nlattr *tb[],
1683 struct ieee80211_txq_params *txq_params)
1684{
Johannes Berga3304b02012-03-28 11:04:24 +02001685 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001686 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1687 !tb[NL80211_TXQ_ATTR_AIFS])
1688 return -EINVAL;
1689
Johannes Berga3304b02012-03-28 11:04:24 +02001690 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001691 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1692 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1693 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1694 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1695
Johannes Berga3304b02012-03-28 11:04:24 +02001696 if (txq_params->ac >= NL80211_NUM_ACS)
1697 return -EINVAL;
1698
Jouni Malinen31888482008-10-30 16:59:24 +02001699 return 0;
1700}
1701
Johannes Bergf444de02010-05-05 15:25:02 +02001702static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1703{
1704 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001705 * You can only set the channel explicitly for WDS interfaces,
1706 * all others have their channel managed via their respective
1707 * "establish a connection" command (connect, join, ...)
1708 *
1709 * For AP/GO and mesh mode, the channel can be set with the
1710 * channel userspace API, but is only stored and passed to the
1711 * low-level driver when the AP starts or the mesh is joined.
1712 * This is for backward compatibility, userspace can also give
1713 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001714 *
1715 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001716 * whatever else is going on, so they have their own special
1717 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001718 */
1719 return !wdev ||
1720 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001721 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001722 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1723 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001724}
1725
Johannes Berg683b6d32012-11-08 21:25:48 +01001726static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1727 struct genl_info *info,
1728 struct cfg80211_chan_def *chandef)
1729{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301730 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001731
1732 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1733 return -EINVAL;
1734
1735 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1736
1737 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001738 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1739 chandef->center_freq1 = control_freq;
1740 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001741
1742 /* Primary channel not allowed */
1743 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1744 return -EINVAL;
1745
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001746 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1747 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001748
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001749 chantype = nla_get_u32(
1750 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1751
1752 switch (chantype) {
1753 case NL80211_CHAN_NO_HT:
1754 case NL80211_CHAN_HT20:
1755 case NL80211_CHAN_HT40PLUS:
1756 case NL80211_CHAN_HT40MINUS:
1757 cfg80211_chandef_create(chandef, chandef->chan,
1758 chantype);
1759 break;
1760 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001761 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001762 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001763 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1764 chandef->width =
1765 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1766 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1767 chandef->center_freq1 =
1768 nla_get_u32(
1769 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1770 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1771 chandef->center_freq2 =
1772 nla_get_u32(
1773 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1774 }
1775
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001776 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001777 return -EINVAL;
1778
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001779 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1780 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001781 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001782
Johannes Berg683b6d32012-11-08 21:25:48 +01001783 return 0;
1784}
1785
Johannes Bergf444de02010-05-05 15:25:02 +02001786static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1787 struct wireless_dev *wdev,
1788 struct genl_info *info)
1789{
Johannes Berg683b6d32012-11-08 21:25:48 +01001790 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001791 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001792 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1793
1794 if (wdev)
1795 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001796
Johannes Bergf444de02010-05-05 15:25:02 +02001797 if (!nl80211_can_set_dev_channel(wdev))
1798 return -EOPNOTSUPP;
1799
Johannes Berg683b6d32012-11-08 21:25:48 +01001800 result = nl80211_parse_chandef(rdev, info, &chandef);
1801 if (result)
1802 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001803
1804 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001805 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001806 case NL80211_IFTYPE_AP:
1807 case NL80211_IFTYPE_P2P_GO:
1808 if (wdev->beacon_interval) {
1809 result = -EBUSY;
1810 break;
1811 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001812 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001813 result = -EINVAL;
1814 break;
1815 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001816 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001817 result = 0;
1818 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001819 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001820 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001821 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001822 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001823 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001824 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001825 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001826 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001827 }
1828 mutex_unlock(&rdev->devlist_mtx);
1829
1830 return result;
1831}
1832
1833static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1834{
Johannes Berg4c476992010-10-04 21:36:35 +02001835 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1836 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001837
Johannes Berg4c476992010-10-04 21:36:35 +02001838 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001839}
1840
Bill Jordane8347eb2010-10-01 13:54:28 -04001841static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1842{
Johannes Berg43b19952010-10-07 13:10:30 +02001843 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1844 struct net_device *dev = info->user_ptr[1];
1845 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001846 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001847
1848 if (!info->attrs[NL80211_ATTR_MAC])
1849 return -EINVAL;
1850
Johannes Berg43b19952010-10-07 13:10:30 +02001851 if (netif_running(dev))
1852 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001853
Johannes Berg43b19952010-10-07 13:10:30 +02001854 if (!rdev->ops->set_wds_peer)
1855 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001856
Johannes Berg43b19952010-10-07 13:10:30 +02001857 if (wdev->iftype != NL80211_IFTYPE_WDS)
1858 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001859
1860 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001861 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001862}
1863
1864
Johannes Berg55682962007-09-20 13:09:35 -04001865static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1866{
1867 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001868 struct net_device *netdev = NULL;
1869 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001870 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001871 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001872 u32 changed;
1873 u8 retry_short = 0, retry_long = 0;
1874 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001875 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001876
Johannes Bergf444de02010-05-05 15:25:02 +02001877 /*
1878 * Try to find the wiphy and netdev. Normally this
1879 * function shouldn't need the netdev, but this is
1880 * done for backward compatibility -- previously
1881 * setting the channel was done per wiphy, but now
1882 * it is per netdev. Previous userland like hostapd
1883 * also passed a netdev to set_wiphy, so that it is
1884 * possible to let that go to the right netdev!
1885 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001886 mutex_lock(&cfg80211_mutex);
1887
Johannes Bergf444de02010-05-05 15:25:02 +02001888 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1889 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1890
1891 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1892 if (netdev && netdev->ieee80211_ptr) {
1893 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1894 mutex_lock(&rdev->mtx);
1895 } else
1896 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001897 }
1898
Johannes Bergf444de02010-05-05 15:25:02 +02001899 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001900 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1901 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001902 if (IS_ERR(rdev)) {
1903 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001904 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001905 }
1906 wdev = NULL;
1907 netdev = NULL;
1908 result = 0;
1909
1910 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001911 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001912 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001913
1914 /*
1915 * end workaround code, by now the rdev is available
1916 * and locked, and wdev may or may not be NULL.
1917 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001918
1919 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001920 result = cfg80211_dev_rename(
1921 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001922
1923 mutex_unlock(&cfg80211_mutex);
1924
1925 if (result)
1926 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001927
Jouni Malinen31888482008-10-30 16:59:24 +02001928 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1929 struct ieee80211_txq_params txq_params;
1930 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1931
1932 if (!rdev->ops->set_txq_params) {
1933 result = -EOPNOTSUPP;
1934 goto bad_res;
1935 }
1936
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001937 if (!netdev) {
1938 result = -EINVAL;
1939 goto bad_res;
1940 }
1941
Johannes Berg133a3ff2011-11-03 14:50:13 +01001942 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1943 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1944 result = -EINVAL;
1945 goto bad_res;
1946 }
1947
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001948 if (!netif_running(netdev)) {
1949 result = -ENETDOWN;
1950 goto bad_res;
1951 }
1952
Jouni Malinen31888482008-10-30 16:59:24 +02001953 nla_for_each_nested(nl_txq_params,
1954 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1955 rem_txq_params) {
1956 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1957 nla_data(nl_txq_params),
1958 nla_len(nl_txq_params),
1959 txq_params_policy);
1960 result = parse_txq_params(tb, &txq_params);
1961 if (result)
1962 goto bad_res;
1963
Hila Gonene35e4d22012-06-27 17:19:42 +03001964 result = rdev_set_txq_params(rdev, netdev,
1965 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001966 if (result)
1967 goto bad_res;
1968 }
1969 }
1970
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001971 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001972 result = __nl80211_set_channel(rdev,
1973 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1974 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001975 if (result)
1976 goto bad_res;
1977 }
1978
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001979 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001980 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001981 enum nl80211_tx_power_setting type;
1982 int idx, mbm = 0;
1983
Johannes Bergc8442112012-10-24 10:17:18 +02001984 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1985 txp_wdev = NULL;
1986
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001987 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001988 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001989 goto bad_res;
1990 }
1991
1992 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1993 type = nla_get_u32(info->attrs[idx]);
1994
1995 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1996 (type != NL80211_TX_POWER_AUTOMATIC)) {
1997 result = -EINVAL;
1998 goto bad_res;
1999 }
2000
2001 if (type != NL80211_TX_POWER_AUTOMATIC) {
2002 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2003 mbm = nla_get_u32(info->attrs[idx]);
2004 }
2005
Johannes Bergc8442112012-10-24 10:17:18 +02002006 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002007 if (result)
2008 goto bad_res;
2009 }
2010
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002011 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2012 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2013 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002014 if ((!rdev->wiphy.available_antennas_tx &&
2015 !rdev->wiphy.available_antennas_rx) ||
2016 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002017 result = -EOPNOTSUPP;
2018 goto bad_res;
2019 }
2020
2021 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2022 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2023
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002024 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002025 * available antenna masks, except for the "all" mask */
2026 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2027 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002028 result = -EINVAL;
2029 goto bad_res;
2030 }
2031
Bruno Randolf7f531e02010-12-16 11:30:22 +09002032 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2033 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002034
Hila Gonene35e4d22012-06-27 17:19:42 +03002035 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002036 if (result)
2037 goto bad_res;
2038 }
2039
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002040 changed = 0;
2041
2042 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2043 retry_short = nla_get_u8(
2044 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2045 if (retry_short == 0) {
2046 result = -EINVAL;
2047 goto bad_res;
2048 }
2049 changed |= WIPHY_PARAM_RETRY_SHORT;
2050 }
2051
2052 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2053 retry_long = nla_get_u8(
2054 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2055 if (retry_long == 0) {
2056 result = -EINVAL;
2057 goto bad_res;
2058 }
2059 changed |= WIPHY_PARAM_RETRY_LONG;
2060 }
2061
2062 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2063 frag_threshold = nla_get_u32(
2064 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2065 if (frag_threshold < 256) {
2066 result = -EINVAL;
2067 goto bad_res;
2068 }
2069 if (frag_threshold != (u32) -1) {
2070 /*
2071 * Fragments (apart from the last one) are required to
2072 * have even length. Make the fragmentation code
2073 * simpler by stripping LSB should someone try to use
2074 * odd threshold value.
2075 */
2076 frag_threshold &= ~0x1;
2077 }
2078 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2079 }
2080
2081 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2082 rts_threshold = nla_get_u32(
2083 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2084 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2085 }
2086
Lukáš Turek81077e82009-12-21 22:50:47 +01002087 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2088 coverage_class = nla_get_u8(
2089 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2090 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2091 }
2092
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002093 if (changed) {
2094 u8 old_retry_short, old_retry_long;
2095 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002096 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002097
2098 if (!rdev->ops->set_wiphy_params) {
2099 result = -EOPNOTSUPP;
2100 goto bad_res;
2101 }
2102
2103 old_retry_short = rdev->wiphy.retry_short;
2104 old_retry_long = rdev->wiphy.retry_long;
2105 old_frag_threshold = rdev->wiphy.frag_threshold;
2106 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002107 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002108
2109 if (changed & WIPHY_PARAM_RETRY_SHORT)
2110 rdev->wiphy.retry_short = retry_short;
2111 if (changed & WIPHY_PARAM_RETRY_LONG)
2112 rdev->wiphy.retry_long = retry_long;
2113 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2114 rdev->wiphy.frag_threshold = frag_threshold;
2115 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2116 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002117 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2118 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002119
Hila Gonene35e4d22012-06-27 17:19:42 +03002120 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002121 if (result) {
2122 rdev->wiphy.retry_short = old_retry_short;
2123 rdev->wiphy.retry_long = old_retry_long;
2124 rdev->wiphy.frag_threshold = old_frag_threshold;
2125 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002126 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002127 }
2128 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002129
Johannes Berg306d6112008-12-08 12:39:04 +01002130 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002131 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02002132 if (netdev)
2133 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002134 return result;
2135}
2136
Johannes Berg71bbc992012-06-15 15:30:18 +02002137static inline u64 wdev_id(struct wireless_dev *wdev)
2138{
2139 return (u64)wdev->identifier |
2140 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2141}
Johannes Berg55682962007-09-20 13:09:35 -04002142
Johannes Berg683b6d32012-11-08 21:25:48 +01002143static int nl80211_send_chandef(struct sk_buff *msg,
2144 struct cfg80211_chan_def *chandef)
2145{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002146 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002147
Johannes Berg683b6d32012-11-08 21:25:48 +01002148 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2149 chandef->chan->center_freq))
2150 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002151 switch (chandef->width) {
2152 case NL80211_CHAN_WIDTH_20_NOHT:
2153 case NL80211_CHAN_WIDTH_20:
2154 case NL80211_CHAN_WIDTH_40:
2155 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2156 cfg80211_get_chandef_type(chandef)))
2157 return -ENOBUFS;
2158 break;
2159 default:
2160 break;
2161 }
2162 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2163 return -ENOBUFS;
2164 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2165 return -ENOBUFS;
2166 if (chandef->center_freq2 &&
2167 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002168 return -ENOBUFS;
2169 return 0;
2170}
2171
Eric W. Biederman15e47302012-09-07 20:12:54 +00002172static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002173 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002174 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002175{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002176 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002177 void *hdr;
2178
Eric W. Biederman15e47302012-09-07 20:12:54 +00002179 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002180 if (!hdr)
2181 return -1;
2182
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002183 if (dev &&
2184 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002185 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002186 goto nla_put_failure;
2187
2188 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2189 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002190 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002191 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002192 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2193 rdev->devlist_generation ^
2194 (cfg80211_rdev_list_generation << 2)))
2195 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002196
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002197 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002198 int ret;
2199 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002200
Johannes Berg683b6d32012-11-08 21:25:48 +01002201 ret = rdev_get_channel(rdev, wdev, &chandef);
2202 if (ret == 0) {
2203 if (nl80211_send_chandef(msg, &chandef))
2204 goto nla_put_failure;
2205 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002206 }
2207
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002208 if (wdev->ssid_len) {
2209 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2210 goto nla_put_failure;
2211 }
2212
Johannes Berg55682962007-09-20 13:09:35 -04002213 return genlmsg_end(msg, hdr);
2214
2215 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002216 genlmsg_cancel(msg, hdr);
2217 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002218}
2219
2220static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2221{
2222 int wp_idx = 0;
2223 int if_idx = 0;
2224 int wp_start = cb->args[0];
2225 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002226 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002227 struct wireless_dev *wdev;
2228
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002229 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02002230 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2231 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002232 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002233 if (wp_idx < wp_start) {
2234 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002235 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002236 }
Johannes Berg55682962007-09-20 13:09:35 -04002237 if_idx = 0;
2238
Johannes Bergf5ea9122009-08-07 16:17:38 +02002239 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02002240 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002241 if (if_idx < if_start) {
2242 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002243 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002244 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002245 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002246 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002247 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002248 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002249 goto out;
2250 }
2251 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002252 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002253 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002254
2255 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002256 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002257 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002258 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002259
2260 cb->args[0] = wp_idx;
2261 cb->args[1] = if_idx;
2262
2263 return skb->len;
2264}
2265
2266static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2267{
2268 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002269 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002270 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002271
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002272 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002273 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002274 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002275
Eric W. Biederman15e47302012-09-07 20:12:54 +00002276 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002277 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002278 nlmsg_free(msg);
2279 return -ENOBUFS;
2280 }
Johannes Berg55682962007-09-20 13:09:35 -04002281
Johannes Berg134e6372009-07-10 09:51:34 +00002282 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002283}
2284
Michael Wu66f7ac52008-01-31 19:48:22 +01002285static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2286 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2287 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2288 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2289 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2290 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2291};
2292
2293static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2294{
2295 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2296 int flag;
2297
2298 *mntrflags = 0;
2299
2300 if (!nla)
2301 return -EINVAL;
2302
2303 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2304 nla, mntr_flags_policy))
2305 return -EINVAL;
2306
2307 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2308 if (flags[flag])
2309 *mntrflags |= (1<<flag);
2310
2311 return 0;
2312}
2313
Johannes Berg9bc383d2009-11-19 11:55:19 +01002314static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002315 struct net_device *netdev, u8 use_4addr,
2316 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002317{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002318 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002319 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002320 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002321 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002322 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002323
2324 switch (iftype) {
2325 case NL80211_IFTYPE_AP_VLAN:
2326 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2327 return 0;
2328 break;
2329 case NL80211_IFTYPE_STATION:
2330 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2331 return 0;
2332 break;
2333 default:
2334 break;
2335 }
2336
2337 return -EOPNOTSUPP;
2338}
2339
Johannes Berg55682962007-09-20 13:09:35 -04002340static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2341{
Johannes Berg4c476992010-10-04 21:36:35 +02002342 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002343 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002344 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002345 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002346 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002347 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002348 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002349
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002350 memset(&params, 0, sizeof(params));
2351
Johannes Berg04a773a2009-04-19 21:24:32 +02002352 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002353
Johannes Berg723b0382008-09-16 20:22:09 +02002354 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002355 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002356 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002357 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002358 if (ntype > NL80211_IFTYPE_MAX)
2359 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002360 }
2361
Johannes Berg92ffe052008-09-16 20:39:36 +02002362 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002363 struct wireless_dev *wdev = dev->ieee80211_ptr;
2364
Johannes Berg4c476992010-10-04 21:36:35 +02002365 if (ntype != NL80211_IFTYPE_MESH_POINT)
2366 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002367 if (netif_running(dev))
2368 return -EBUSY;
2369
2370 wdev_lock(wdev);
2371 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2372 IEEE80211_MAX_MESH_ID_LEN);
2373 wdev->mesh_id_up_len =
2374 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2375 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2376 wdev->mesh_id_up_len);
2377 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002378 }
2379
Felix Fietkau8b787642009-11-10 18:53:10 +01002380 if (info->attrs[NL80211_ATTR_4ADDR]) {
2381 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2382 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002383 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002384 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002385 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002386 } else {
2387 params.use_4addr = -1;
2388 }
2389
Johannes Berg92ffe052008-09-16 20:39:36 +02002390 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002391 if (ntype != NL80211_IFTYPE_MONITOR)
2392 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002393 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2394 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002395 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002396 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002397
2398 flags = &_flags;
2399 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002400 }
Johannes Berg3b858752009-03-12 09:55:09 +01002401
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002402 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002403 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002404 else
2405 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002406
Johannes Berg9bc383d2009-11-19 11:55:19 +01002407 if (!err && params.use_4addr != -1)
2408 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2409
Johannes Berg55682962007-09-20 13:09:35 -04002410 return err;
2411}
2412
2413static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2414{
Johannes Berg4c476992010-10-04 21:36:35 +02002415 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002416 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002417 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002418 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002419 int err;
2420 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002421 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002422
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002423 memset(&params, 0, sizeof(params));
2424
Johannes Berg55682962007-09-20 13:09:35 -04002425 if (!info->attrs[NL80211_ATTR_IFNAME])
2426 return -EINVAL;
2427
2428 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2429 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2430 if (type > NL80211_IFTYPE_MAX)
2431 return -EINVAL;
2432 }
2433
Johannes Berg79c97e92009-07-07 03:56:12 +02002434 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002435 !(rdev->wiphy.interface_modes & (1 << type)))
2436 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002437
Arend van Spriel1c18f142013-01-08 10:17:27 +01002438 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2439 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2440 ETH_ALEN);
2441 if (!is_valid_ether_addr(params.macaddr))
2442 return -EADDRNOTAVAIL;
2443 }
2444
Johannes Berg9bc383d2009-11-19 11:55:19 +01002445 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002446 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002447 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002448 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002449 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002450 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002451
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002452 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2453 if (!msg)
2454 return -ENOMEM;
2455
Michael Wu66f7ac52008-01-31 19:48:22 +01002456 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2457 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2458 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002459 wdev = rdev_add_virtual_intf(rdev,
2460 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2461 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002462 if (IS_ERR(wdev)) {
2463 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002464 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002465 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002466
Johannes Berg98104fde2012-06-16 00:19:54 +02002467 switch (type) {
2468 case NL80211_IFTYPE_MESH_POINT:
2469 if (!info->attrs[NL80211_ATTR_MESH_ID])
2470 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002471 wdev_lock(wdev);
2472 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2473 IEEE80211_MAX_MESH_ID_LEN);
2474 wdev->mesh_id_up_len =
2475 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2476 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2477 wdev->mesh_id_up_len);
2478 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002479 break;
2480 case NL80211_IFTYPE_P2P_DEVICE:
2481 /*
2482 * P2P Device doesn't have a netdev, so doesn't go
2483 * through the netdev notifier and must be added here
2484 */
2485 mutex_init(&wdev->mtx);
2486 INIT_LIST_HEAD(&wdev->event_list);
2487 spin_lock_init(&wdev->event_lock);
2488 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2489 spin_lock_init(&wdev->mgmt_registrations_lock);
2490
2491 mutex_lock(&rdev->devlist_mtx);
2492 wdev->identifier = ++rdev->wdev_id;
2493 list_add_rcu(&wdev->list, &rdev->wdev_list);
2494 rdev->devlist_generation++;
2495 mutex_unlock(&rdev->devlist_mtx);
2496 break;
2497 default:
2498 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002499 }
2500
Eric W. Biederman15e47302012-09-07 20:12:54 +00002501 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002502 rdev, wdev) < 0) {
2503 nlmsg_free(msg);
2504 return -ENOBUFS;
2505 }
2506
2507 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002508}
2509
2510static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2511{
Johannes Berg4c476992010-10-04 21:36:35 +02002512 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002513 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002514
Johannes Berg4c476992010-10-04 21:36:35 +02002515 if (!rdev->ops->del_virtual_intf)
2516 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002517
Johannes Berg84efbb82012-06-16 00:00:26 +02002518 /*
2519 * If we remove a wireless device without a netdev then clear
2520 * user_ptr[1] so that nl80211_post_doit won't dereference it
2521 * to check if it needs to do dev_put(). Otherwise it crashes
2522 * since the wdev has been freed, unlike with a netdev where
2523 * we need the dev_put() for the netdev to really be freed.
2524 */
2525 if (!wdev->netdev)
2526 info->user_ptr[1] = NULL;
2527
Hila Gonene35e4d22012-06-27 17:19:42 +03002528 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002529}
2530
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002531static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2532{
2533 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2534 struct net_device *dev = info->user_ptr[1];
2535 u16 noack_map;
2536
2537 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2538 return -EINVAL;
2539
2540 if (!rdev->ops->set_noack_map)
2541 return -EOPNOTSUPP;
2542
2543 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2544
Hila Gonene35e4d22012-06-27 17:19:42 +03002545 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002546}
2547
Johannes Berg41ade002007-12-19 02:03:29 +01002548struct get_key_cookie {
2549 struct sk_buff *msg;
2550 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002551 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002552};
2553
2554static void get_key_callback(void *c, struct key_params *params)
2555{
Johannes Bergb9454e82009-07-08 13:29:08 +02002556 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002557 struct get_key_cookie *cookie = c;
2558
David S. Miller9360ffd2012-03-29 04:41:26 -04002559 if ((params->key &&
2560 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2561 params->key_len, params->key)) ||
2562 (params->seq &&
2563 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2564 params->seq_len, params->seq)) ||
2565 (params->cipher &&
2566 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2567 params->cipher)))
2568 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002569
Johannes Bergb9454e82009-07-08 13:29:08 +02002570 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2571 if (!key)
2572 goto nla_put_failure;
2573
David S. Miller9360ffd2012-03-29 04:41:26 -04002574 if ((params->key &&
2575 nla_put(cookie->msg, NL80211_KEY_DATA,
2576 params->key_len, params->key)) ||
2577 (params->seq &&
2578 nla_put(cookie->msg, NL80211_KEY_SEQ,
2579 params->seq_len, params->seq)) ||
2580 (params->cipher &&
2581 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2582 params->cipher)))
2583 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002584
David S. Miller9360ffd2012-03-29 04:41:26 -04002585 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2586 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002587
2588 nla_nest_end(cookie->msg, key);
2589
Johannes Berg41ade002007-12-19 02:03:29 +01002590 return;
2591 nla_put_failure:
2592 cookie->error = 1;
2593}
2594
2595static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2596{
Johannes Berg4c476992010-10-04 21:36:35 +02002597 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002598 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002599 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002600 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002601 const u8 *mac_addr = NULL;
2602 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002603 struct get_key_cookie cookie = {
2604 .error = 0,
2605 };
2606 void *hdr;
2607 struct sk_buff *msg;
2608
2609 if (info->attrs[NL80211_ATTR_KEY_IDX])
2610 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2611
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002612 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002613 return -EINVAL;
2614
2615 if (info->attrs[NL80211_ATTR_MAC])
2616 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2617
Johannes Berge31b8212010-10-05 19:39:30 +02002618 pairwise = !!mac_addr;
2619 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2620 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2621 if (kt >= NUM_NL80211_KEYTYPES)
2622 return -EINVAL;
2623 if (kt != NL80211_KEYTYPE_GROUP &&
2624 kt != NL80211_KEYTYPE_PAIRWISE)
2625 return -EINVAL;
2626 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2627 }
2628
Johannes Berg4c476992010-10-04 21:36:35 +02002629 if (!rdev->ops->get_key)
2630 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002631
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002632 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002633 if (!msg)
2634 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002635
Eric W. Biederman15e47302012-09-07 20:12:54 +00002636 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002637 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002638 if (IS_ERR(hdr))
2639 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002640
2641 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002642 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002643
David S. Miller9360ffd2012-03-29 04:41:26 -04002644 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2645 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2646 goto nla_put_failure;
2647 if (mac_addr &&
2648 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2649 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002650
Johannes Berge31b8212010-10-05 19:39:30 +02002651 if (pairwise && mac_addr &&
2652 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2653 return -ENOENT;
2654
Hila Gonene35e4d22012-06-27 17:19:42 +03002655 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2656 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002657
2658 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002659 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002660
2661 if (cookie.error)
2662 goto nla_put_failure;
2663
2664 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002665 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002666
2667 nla_put_failure:
2668 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002669 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002670 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002671 return err;
2672}
2673
2674static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2675{
Johannes Berg4c476992010-10-04 21:36:35 +02002676 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002677 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002678 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002679 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002680
Johannes Bergb9454e82009-07-08 13:29:08 +02002681 err = nl80211_parse_key(info, &key);
2682 if (err)
2683 return err;
2684
2685 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002686 return -EINVAL;
2687
Johannes Bergb9454e82009-07-08 13:29:08 +02002688 /* only support setting default key */
2689 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002690 return -EINVAL;
2691
Johannes Bergfffd0932009-07-08 14:22:54 +02002692 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002693
2694 if (key.def) {
2695 if (!rdev->ops->set_default_key) {
2696 err = -EOPNOTSUPP;
2697 goto out;
2698 }
2699
2700 err = nl80211_key_allowed(dev->ieee80211_ptr);
2701 if (err)
2702 goto out;
2703
Hila Gonene35e4d22012-06-27 17:19:42 +03002704 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002705 key.def_uni, key.def_multi);
2706
2707 if (err)
2708 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002709
Johannes Berg3d23e342009-09-29 23:27:28 +02002710#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002711 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002712#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002713 } else {
2714 if (key.def_uni || !key.def_multi) {
2715 err = -EINVAL;
2716 goto out;
2717 }
2718
2719 if (!rdev->ops->set_default_mgmt_key) {
2720 err = -EOPNOTSUPP;
2721 goto out;
2722 }
2723
2724 err = nl80211_key_allowed(dev->ieee80211_ptr);
2725 if (err)
2726 goto out;
2727
Hila Gonene35e4d22012-06-27 17:19:42 +03002728 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002729 if (err)
2730 goto out;
2731
2732#ifdef CONFIG_CFG80211_WEXT
2733 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2734#endif
2735 }
2736
2737 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002738 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002739
Johannes Berg41ade002007-12-19 02:03:29 +01002740 return err;
2741}
2742
2743static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2744{
Johannes Berg4c476992010-10-04 21:36:35 +02002745 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002746 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002747 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002748 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002749 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002750
Johannes Bergb9454e82009-07-08 13:29:08 +02002751 err = nl80211_parse_key(info, &key);
2752 if (err)
2753 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002754
Johannes Bergb9454e82009-07-08 13:29:08 +02002755 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002756 return -EINVAL;
2757
Johannes Berg41ade002007-12-19 02:03:29 +01002758 if (info->attrs[NL80211_ATTR_MAC])
2759 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2760
Johannes Berge31b8212010-10-05 19:39:30 +02002761 if (key.type == -1) {
2762 if (mac_addr)
2763 key.type = NL80211_KEYTYPE_PAIRWISE;
2764 else
2765 key.type = NL80211_KEYTYPE_GROUP;
2766 }
2767
2768 /* for now */
2769 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2770 key.type != NL80211_KEYTYPE_GROUP)
2771 return -EINVAL;
2772
Johannes Berg4c476992010-10-04 21:36:35 +02002773 if (!rdev->ops->add_key)
2774 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002775
Johannes Berge31b8212010-10-05 19:39:30 +02002776 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2777 key.type == NL80211_KEYTYPE_PAIRWISE,
2778 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002779 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002780
2781 wdev_lock(dev->ieee80211_ptr);
2782 err = nl80211_key_allowed(dev->ieee80211_ptr);
2783 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002784 err = rdev_add_key(rdev, dev, key.idx,
2785 key.type == NL80211_KEYTYPE_PAIRWISE,
2786 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002787 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002788
Johannes Berg41ade002007-12-19 02:03:29 +01002789 return err;
2790}
2791
2792static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2793{
Johannes Berg4c476992010-10-04 21:36:35 +02002794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002795 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002796 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002797 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002798 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002799
Johannes Bergb9454e82009-07-08 13:29:08 +02002800 err = nl80211_parse_key(info, &key);
2801 if (err)
2802 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002803
2804 if (info->attrs[NL80211_ATTR_MAC])
2805 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2806
Johannes Berge31b8212010-10-05 19:39:30 +02002807 if (key.type == -1) {
2808 if (mac_addr)
2809 key.type = NL80211_KEYTYPE_PAIRWISE;
2810 else
2811 key.type = NL80211_KEYTYPE_GROUP;
2812 }
2813
2814 /* for now */
2815 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2816 key.type != NL80211_KEYTYPE_GROUP)
2817 return -EINVAL;
2818
Johannes Berg4c476992010-10-04 21:36:35 +02002819 if (!rdev->ops->del_key)
2820 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002821
Johannes Bergfffd0932009-07-08 14:22:54 +02002822 wdev_lock(dev->ieee80211_ptr);
2823 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002824
2825 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2826 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2827 err = -ENOENT;
2828
Johannes Bergfffd0932009-07-08 14:22:54 +02002829 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002830 err = rdev_del_key(rdev, dev, key.idx,
2831 key.type == NL80211_KEYTYPE_PAIRWISE,
2832 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002833
Johannes Berg3d23e342009-09-29 23:27:28 +02002834#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002835 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002836 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002837 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002838 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002839 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2840 }
2841#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002842 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002843
Johannes Berg41ade002007-12-19 02:03:29 +01002844 return err;
2845}
2846
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302847/* This function returns an error or the number of nested attributes */
2848static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2849{
2850 struct nlattr *attr;
2851 int n_entries = 0, tmp;
2852
2853 nla_for_each_nested(attr, nl_attr, tmp) {
2854 if (nla_len(attr) != ETH_ALEN)
2855 return -EINVAL;
2856
2857 n_entries++;
2858 }
2859
2860 return n_entries;
2861}
2862
2863/*
2864 * This function parses ACL information and allocates memory for ACL data.
2865 * On successful return, the calling function is responsible to free the
2866 * ACL buffer returned by this function.
2867 */
2868static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2869 struct genl_info *info)
2870{
2871 enum nl80211_acl_policy acl_policy;
2872 struct nlattr *attr;
2873 struct cfg80211_acl_data *acl;
2874 int i = 0, n_entries, tmp;
2875
2876 if (!wiphy->max_acl_mac_addrs)
2877 return ERR_PTR(-EOPNOTSUPP);
2878
2879 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2880 return ERR_PTR(-EINVAL);
2881
2882 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2883 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2884 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2885 return ERR_PTR(-EINVAL);
2886
2887 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2888 return ERR_PTR(-EINVAL);
2889
2890 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2891 if (n_entries < 0)
2892 return ERR_PTR(n_entries);
2893
2894 if (n_entries > wiphy->max_acl_mac_addrs)
2895 return ERR_PTR(-ENOTSUPP);
2896
2897 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2898 GFP_KERNEL);
2899 if (!acl)
2900 return ERR_PTR(-ENOMEM);
2901
2902 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2903 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2904 i++;
2905 }
2906
2907 acl->n_acl_entries = n_entries;
2908 acl->acl_policy = acl_policy;
2909
2910 return acl;
2911}
2912
2913static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2914{
2915 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2916 struct net_device *dev = info->user_ptr[1];
2917 struct cfg80211_acl_data *acl;
2918 int err;
2919
2920 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2921 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2922 return -EOPNOTSUPP;
2923
2924 if (!dev->ieee80211_ptr->beacon_interval)
2925 return -EINVAL;
2926
2927 acl = parse_acl_data(&rdev->wiphy, info);
2928 if (IS_ERR(acl))
2929 return PTR_ERR(acl);
2930
2931 err = rdev_set_mac_acl(rdev, dev, acl);
2932
2933 kfree(acl);
2934
2935 return err;
2936}
2937
Johannes Berg88600202012-02-13 15:17:18 +01002938static int nl80211_parse_beacon(struct genl_info *info,
2939 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002940{
Johannes Berg88600202012-02-13 15:17:18 +01002941 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002942
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002943 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2944 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2945 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2946 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002947 return -EINVAL;
2948
Johannes Berg88600202012-02-13 15:17:18 +01002949 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002950
Johannes Berged1b6cc2007-12-19 02:03:32 +01002951 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002952 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2953 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2954 if (!bcn->head_len)
2955 return -EINVAL;
2956 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002957 }
2958
2959 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002960 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2961 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002962 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002963 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002964 }
2965
Johannes Berg4c476992010-10-04 21:36:35 +02002966 if (!haveinfo)
2967 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002968
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002969 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002970 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2971 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002972 }
2973
2974 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002975 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002976 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002977 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002978 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2979 }
2980
2981 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002982 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002983 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002984 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002985 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2986 }
2987
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002988 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002989 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002990 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002991 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002992 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2993 }
2994
Johannes Berg88600202012-02-13 15:17:18 +01002995 return 0;
2996}
2997
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002998static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2999 struct cfg80211_ap_settings *params)
3000{
3001 struct wireless_dev *wdev;
3002 bool ret = false;
3003
3004 mutex_lock(&rdev->devlist_mtx);
3005
Johannes Berg89a54e42012-06-15 14:33:17 +02003006 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003007 if (wdev->iftype != NL80211_IFTYPE_AP &&
3008 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3009 continue;
3010
Johannes Berg683b6d32012-11-08 21:25:48 +01003011 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003012 continue;
3013
Johannes Berg683b6d32012-11-08 21:25:48 +01003014 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003015 ret = true;
3016 break;
3017 }
3018
3019 mutex_unlock(&rdev->devlist_mtx);
3020
3021 return ret;
3022}
3023
Jouni Malinene39e5b52012-09-30 19:29:39 +03003024static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3025 enum nl80211_auth_type auth_type,
3026 enum nl80211_commands cmd)
3027{
3028 if (auth_type > NL80211_AUTHTYPE_MAX)
3029 return false;
3030
3031 switch (cmd) {
3032 case NL80211_CMD_AUTHENTICATE:
3033 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3034 auth_type == NL80211_AUTHTYPE_SAE)
3035 return false;
3036 return true;
3037 case NL80211_CMD_CONNECT:
3038 case NL80211_CMD_START_AP:
3039 /* SAE not supported yet */
3040 if (auth_type == NL80211_AUTHTYPE_SAE)
3041 return false;
3042 return true;
3043 default:
3044 return false;
3045 }
3046}
3047
Johannes Berg88600202012-02-13 15:17:18 +01003048static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3049{
3050 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3051 struct net_device *dev = info->user_ptr[1];
3052 struct wireless_dev *wdev = dev->ieee80211_ptr;
3053 struct cfg80211_ap_settings params;
3054 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003055 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003056
3057 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3058 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3059 return -EOPNOTSUPP;
3060
3061 if (!rdev->ops->start_ap)
3062 return -EOPNOTSUPP;
3063
3064 if (wdev->beacon_interval)
3065 return -EALREADY;
3066
3067 memset(&params, 0, sizeof(params));
3068
3069 /* these are required for START_AP */
3070 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3071 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3072 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3073 return -EINVAL;
3074
3075 err = nl80211_parse_beacon(info, &params.beacon);
3076 if (err)
3077 return err;
3078
3079 params.beacon_interval =
3080 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3081 params.dtim_period =
3082 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3083
3084 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3085 if (err)
3086 return err;
3087
3088 /*
3089 * In theory, some of these attributes should be required here
3090 * but since they were not used when the command was originally
3091 * added, keep them optional for old user space programs to let
3092 * them continue to work with drivers that do not need the
3093 * additional information -- drivers must check!
3094 */
3095 if (info->attrs[NL80211_ATTR_SSID]) {
3096 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3097 params.ssid_len =
3098 nla_len(info->attrs[NL80211_ATTR_SSID]);
3099 if (params.ssid_len == 0 ||
3100 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3101 return -EINVAL;
3102 }
3103
3104 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3105 params.hidden_ssid = nla_get_u32(
3106 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3107 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3108 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3109 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3110 return -EINVAL;
3111 }
3112
3113 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3114
3115 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3116 params.auth_type = nla_get_u32(
3117 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003118 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3119 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003120 return -EINVAL;
3121 } else
3122 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3123
3124 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3125 NL80211_MAX_NR_CIPHER_SUITES);
3126 if (err)
3127 return err;
3128
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303129 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3130 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3131 return -EOPNOTSUPP;
3132 params.inactivity_timeout = nla_get_u16(
3133 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3134 }
3135
Johannes Berg53cabad2012-11-14 15:17:28 +01003136 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3137 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3138 return -EINVAL;
3139 params.p2p_ctwindow =
3140 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3141 if (params.p2p_ctwindow > 127)
3142 return -EINVAL;
3143 if (params.p2p_ctwindow != 0 &&
3144 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3145 return -EINVAL;
3146 }
3147
3148 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3149 u8 tmp;
3150
3151 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3152 return -EINVAL;
3153 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3154 if (tmp > 1)
3155 return -EINVAL;
3156 params.p2p_opp_ps = tmp;
3157 if (params.p2p_opp_ps != 0 &&
3158 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3159 return -EINVAL;
3160 }
3161
Johannes Bergaa430da2012-05-16 23:50:18 +02003162 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003163 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3164 if (err)
3165 return err;
3166 } else if (wdev->preset_chandef.chan) {
3167 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003168 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003169 return -EINVAL;
3170
Johannes Berg683b6d32012-11-08 21:25:48 +01003171 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003172 return -EINVAL;
3173
Simon Wunderlich04f39042013-02-08 18:16:19 +01003174 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3175 if (err < 0)
3176 return err;
3177 if (err) {
3178 radar_detect_width = BIT(params.chandef.width);
3179 params.radar_required = true;
3180 }
3181
Michal Kaziore4e32452012-06-29 12:47:08 +02003182 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003183 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3184 params.chandef.chan,
3185 CHAN_MODE_SHARED,
3186 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003187 mutex_unlock(&rdev->devlist_mtx);
3188
3189 if (err)
3190 return err;
3191
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303192 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3193 params.acl = parse_acl_data(&rdev->wiphy, info);
3194 if (IS_ERR(params.acl))
3195 return PTR_ERR(params.acl);
3196 }
3197
Hila Gonene35e4d22012-06-27 17:19:42 +03003198 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003199 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003200 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003201 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003202 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003203 wdev->ssid_len = params.ssid_len;
3204 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003205 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303206
3207 kfree(params.acl);
3208
Johannes Berg56d18932011-05-09 18:41:15 +02003209 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003210}
3211
Johannes Berg88600202012-02-13 15:17:18 +01003212static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3213{
3214 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3215 struct net_device *dev = info->user_ptr[1];
3216 struct wireless_dev *wdev = dev->ieee80211_ptr;
3217 struct cfg80211_beacon_data params;
3218 int err;
3219
3220 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3221 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3222 return -EOPNOTSUPP;
3223
3224 if (!rdev->ops->change_beacon)
3225 return -EOPNOTSUPP;
3226
3227 if (!wdev->beacon_interval)
3228 return -EINVAL;
3229
3230 err = nl80211_parse_beacon(info, &params);
3231 if (err)
3232 return err;
3233
Hila Gonene35e4d22012-06-27 17:19:42 +03003234 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003235}
3236
3237static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003238{
Johannes Berg4c476992010-10-04 21:36:35 +02003239 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3240 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003241
Michal Kazior60771782012-06-29 12:46:56 +02003242 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003243}
3244
Johannes Berg5727ef12007-12-19 02:03:34 +01003245static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3246 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3247 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3248 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003249 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003250 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003251 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003252};
3253
Johannes Bergeccb8e82009-05-11 21:57:56 +03003254static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003255 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003256 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003257{
3258 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003259 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003260 int flag;
3261
Johannes Bergeccb8e82009-05-11 21:57:56 +03003262 /*
3263 * Try parsing the new attribute first so userspace
3264 * can specify both for older kernels.
3265 */
3266 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3267 if (nla) {
3268 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003269
Johannes Bergeccb8e82009-05-11 21:57:56 +03003270 sta_flags = nla_data(nla);
3271 params->sta_flags_mask = sta_flags->mask;
3272 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003273 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003274 if ((params->sta_flags_mask |
3275 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3276 return -EINVAL;
3277 return 0;
3278 }
3279
3280 /* if present, parse the old attribute */
3281
3282 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003283 if (!nla)
3284 return 0;
3285
3286 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3287 nla, sta_flags_policy))
3288 return -EINVAL;
3289
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003290 /*
3291 * Only allow certain flags for interface types so that
3292 * other attributes are silently ignored. Remember that
3293 * this is backward compatibility code with old userspace
3294 * and shouldn't be hit in other cases anyway.
3295 */
3296 switch (iftype) {
3297 case NL80211_IFTYPE_AP:
3298 case NL80211_IFTYPE_AP_VLAN:
3299 case NL80211_IFTYPE_P2P_GO:
3300 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3301 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3302 BIT(NL80211_STA_FLAG_WME) |
3303 BIT(NL80211_STA_FLAG_MFP);
3304 break;
3305 case NL80211_IFTYPE_P2P_CLIENT:
3306 case NL80211_IFTYPE_STATION:
3307 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3308 BIT(NL80211_STA_FLAG_TDLS_PEER);
3309 break;
3310 case NL80211_IFTYPE_MESH_POINT:
3311 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3312 BIT(NL80211_STA_FLAG_MFP) |
3313 BIT(NL80211_STA_FLAG_AUTHORIZED);
3314 default:
3315 return -EINVAL;
3316 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003317
Johannes Berg3383b5a2012-05-10 20:14:43 +02003318 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3319 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003320 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003321
Johannes Berg3383b5a2012-05-10 20:14:43 +02003322 /* no longer support new API additions in old API */
3323 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3324 return -EINVAL;
3325 }
3326 }
3327
Johannes Berg5727ef12007-12-19 02:03:34 +01003328 return 0;
3329}
3330
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003331static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3332 int attr)
3333{
3334 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003335 u32 bitrate;
3336 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003337
3338 rate = nla_nest_start(msg, attr);
3339 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003340 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003341
3342 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3343 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003344 /* report 16-bit bitrate only if we can */
3345 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003346 if (bitrate > 0 &&
3347 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3348 return false;
3349 if (bitrate_compat > 0 &&
3350 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3351 return false;
3352
3353 if (info->flags & RATE_INFO_FLAGS_MCS) {
3354 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3355 return false;
3356 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3357 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3358 return false;
3359 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3360 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3361 return false;
3362 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3363 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3364 return false;
3365 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3366 return false;
3367 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3368 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3369 return false;
3370 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3371 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3372 return false;
3373 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3374 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3375 return false;
3376 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3377 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3378 return false;
3379 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3380 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3381 return false;
3382 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003383
3384 nla_nest_end(msg, rate);
3385 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003386}
3387
Eric W. Biederman15e47302012-09-07 20:12:54 +00003388static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003389 int flags,
3390 struct cfg80211_registered_device *rdev,
3391 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003392 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003393{
3394 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003395 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003396
Eric W. Biederman15e47302012-09-07 20:12:54 +00003397 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003398 if (!hdr)
3399 return -1;
3400
David S. Miller9360ffd2012-03-29 04:41:26 -04003401 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3402 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3403 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3404 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003405
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003406 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3407 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003408 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003409 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3410 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3411 sinfo->connected_time))
3412 goto nla_put_failure;
3413 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3414 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3415 sinfo->inactive_time))
3416 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003417 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3418 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003419 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003420 (u32)sinfo->rx_bytes))
3421 goto nla_put_failure;
3422 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003423 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003424 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3425 (u32)sinfo->tx_bytes))
3426 goto nla_put_failure;
3427 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3428 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003429 sinfo->rx_bytes))
3430 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003431 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3432 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003433 sinfo->tx_bytes))
3434 goto nla_put_failure;
3435 if ((sinfo->filled & STATION_INFO_LLID) &&
3436 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3437 goto nla_put_failure;
3438 if ((sinfo->filled & STATION_INFO_PLID) &&
3439 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3440 goto nla_put_failure;
3441 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3442 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3443 sinfo->plink_state))
3444 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003445 switch (rdev->wiphy.signal_type) {
3446 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003447 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3448 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3449 sinfo->signal))
3450 goto nla_put_failure;
3451 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3452 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3453 sinfo->signal_avg))
3454 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003455 break;
3456 default:
3457 break;
3458 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003459 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003460 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3461 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003462 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003463 }
3464 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3465 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3466 NL80211_STA_INFO_RX_BITRATE))
3467 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003468 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003469 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3470 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3471 sinfo->rx_packets))
3472 goto nla_put_failure;
3473 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3474 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3475 sinfo->tx_packets))
3476 goto nla_put_failure;
3477 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3478 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3479 sinfo->tx_retries))
3480 goto nla_put_failure;
3481 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3482 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3483 sinfo->tx_failed))
3484 goto nla_put_failure;
3485 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3486 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3487 sinfo->beacon_loss_count))
3488 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003489 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3490 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3491 sinfo->local_pm))
3492 goto nla_put_failure;
3493 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3494 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3495 sinfo->peer_pm))
3496 goto nla_put_failure;
3497 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3498 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3499 sinfo->nonpeer_pm))
3500 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003501 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3502 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3503 if (!bss_param)
3504 goto nla_put_failure;
3505
David S. Miller9360ffd2012-03-29 04:41:26 -04003506 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3507 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3508 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3509 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3510 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3511 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3512 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3513 sinfo->bss_param.dtim_period) ||
3514 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3515 sinfo->bss_param.beacon_interval))
3516 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003517
3518 nla_nest_end(msg, bss_param);
3519 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003520 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3521 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3522 sizeof(struct nl80211_sta_flag_update),
3523 &sinfo->sta_flags))
3524 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003525 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3526 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3527 sinfo->t_offset))
3528 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003529 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003530
David S. Miller9360ffd2012-03-29 04:41:26 -04003531 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3532 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3533 sinfo->assoc_req_ies))
3534 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003535
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003536 return genlmsg_end(msg, hdr);
3537
3538 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003539 genlmsg_cancel(msg, hdr);
3540 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003541}
3542
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003543static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003544 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003545{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003546 struct station_info sinfo;
3547 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003548 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003549 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003550 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003551 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003552
Johannes Berg97990a02013-04-19 01:02:55 +02003553 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003554 if (err)
3555 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003556
Johannes Berg97990a02013-04-19 01:02:55 +02003557 if (!wdev->netdev) {
3558 err = -EINVAL;
3559 goto out_err;
3560 }
3561
Johannes Bergbba95fe2008-07-29 13:22:51 +02003562 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003563 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003564 goto out_err;
3565 }
3566
Johannes Bergbba95fe2008-07-29 13:22:51 +02003567 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003568 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003569 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003570 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003571 if (err == -ENOENT)
3572 break;
3573 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003574 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003575
3576 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003577 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003578 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003579 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003580 &sinfo) < 0)
3581 goto out;
3582
3583 sta_idx++;
3584 }
3585
3586
3587 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003588 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003589 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003590 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003591 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003592
3593 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003594}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003595
Johannes Berg5727ef12007-12-19 02:03:34 +01003596static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3597{
Johannes Berg4c476992010-10-04 21:36:35 +02003598 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3599 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003600 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003601 struct sk_buff *msg;
3602 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003603 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003604
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003605 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003606
3607 if (!info->attrs[NL80211_ATTR_MAC])
3608 return -EINVAL;
3609
3610 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3611
Johannes Berg4c476992010-10-04 21:36:35 +02003612 if (!rdev->ops->get_station)
3613 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003614
Hila Gonene35e4d22012-06-27 17:19:42 +03003615 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003616 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003617 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003618
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003619 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003620 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003621 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003622
Eric W. Biederman15e47302012-09-07 20:12:54 +00003623 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003624 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003625 nlmsg_free(msg);
3626 return -ENOBUFS;
3627 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003628
Johannes Berg4c476992010-10-04 21:36:35 +02003629 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003630}
3631
Johannes Berg77ee7c82013-02-15 00:48:33 +01003632int cfg80211_check_station_change(struct wiphy *wiphy,
3633 struct station_parameters *params,
3634 enum cfg80211_station_type statype)
3635{
3636 if (params->listen_interval != -1)
3637 return -EINVAL;
3638 if (params->aid)
3639 return -EINVAL;
3640
3641 /* When you run into this, adjust the code below for the new flag */
3642 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3643
3644 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003645 case CFG80211_STA_MESH_PEER_KERNEL:
3646 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003647 /*
3648 * No ignoring the TDLS flag here -- the userspace mesh
3649 * code doesn't have the bug of including TDLS in the
3650 * mask everywhere.
3651 */
3652 if (params->sta_flags_mask &
3653 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3654 BIT(NL80211_STA_FLAG_MFP) |
3655 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3656 return -EINVAL;
3657 break;
3658 case CFG80211_STA_TDLS_PEER_SETUP:
3659 case CFG80211_STA_TDLS_PEER_ACTIVE:
3660 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3661 return -EINVAL;
3662 /* ignore since it can't change */
3663 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3664 break;
3665 default:
3666 /* disallow mesh-specific things */
3667 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3668 return -EINVAL;
3669 if (params->local_pm)
3670 return -EINVAL;
3671 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3672 return -EINVAL;
3673 }
3674
3675 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3676 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3677 /* TDLS can't be set, ... */
3678 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3679 return -EINVAL;
3680 /*
3681 * ... but don't bother the driver with it. This works around
3682 * a hostapd/wpa_supplicant issue -- it always includes the
3683 * TLDS_PEER flag in the mask even for AP mode.
3684 */
3685 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3686 }
3687
3688 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3689 /* reject other things that can't change */
3690 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3691 return -EINVAL;
3692 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3693 return -EINVAL;
3694 if (params->supported_rates)
3695 return -EINVAL;
3696 if (params->ext_capab || params->ht_capa || params->vht_capa)
3697 return -EINVAL;
3698 }
3699
3700 if (statype != CFG80211_STA_AP_CLIENT) {
3701 if (params->vlan)
3702 return -EINVAL;
3703 }
3704
3705 switch (statype) {
3706 case CFG80211_STA_AP_MLME_CLIENT:
3707 /* Use this only for authorizing/unauthorizing a station */
3708 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3709 return -EOPNOTSUPP;
3710 break;
3711 case CFG80211_STA_AP_CLIENT:
3712 /* accept only the listed bits */
3713 if (params->sta_flags_mask &
3714 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3715 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3716 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3717 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3718 BIT(NL80211_STA_FLAG_WME) |
3719 BIT(NL80211_STA_FLAG_MFP)))
3720 return -EINVAL;
3721
3722 /* but authenticated/associated only if driver handles it */
3723 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3724 params->sta_flags_mask &
3725 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3726 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3727 return -EINVAL;
3728 break;
3729 case CFG80211_STA_IBSS:
3730 case CFG80211_STA_AP_STA:
3731 /* reject any changes other than AUTHORIZED */
3732 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3733 return -EINVAL;
3734 break;
3735 case CFG80211_STA_TDLS_PEER_SETUP:
3736 /* reject any changes other than AUTHORIZED or WME */
3737 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3738 BIT(NL80211_STA_FLAG_WME)))
3739 return -EINVAL;
3740 /* force (at least) rates when authorizing */
3741 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3742 !params->supported_rates)
3743 return -EINVAL;
3744 break;
3745 case CFG80211_STA_TDLS_PEER_ACTIVE:
3746 /* reject any changes */
3747 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003748 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003749 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3750 return -EINVAL;
3751 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003752 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003753 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3754 return -EINVAL;
3755 break;
3756 }
3757
3758 return 0;
3759}
3760EXPORT_SYMBOL(cfg80211_check_station_change);
3761
Johannes Berg5727ef12007-12-19 02:03:34 +01003762/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003763 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003764 */
Johannes Berg80b99892011-11-18 16:23:01 +01003765static struct net_device *get_vlan(struct genl_info *info,
3766 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003767{
Johannes Berg463d0182009-07-14 00:33:35 +02003768 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003769 struct net_device *v;
3770 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003771
Johannes Berg80b99892011-11-18 16:23:01 +01003772 if (!vlanattr)
3773 return NULL;
3774
3775 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3776 if (!v)
3777 return ERR_PTR(-ENODEV);
3778
3779 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3780 ret = -EINVAL;
3781 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003782 }
Johannes Berg80b99892011-11-18 16:23:01 +01003783
Johannes Berg77ee7c82013-02-15 00:48:33 +01003784 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3785 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3786 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3787 ret = -EINVAL;
3788 goto error;
3789 }
3790
Johannes Berg80b99892011-11-18 16:23:01 +01003791 if (!netif_running(v)) {
3792 ret = -ENETDOWN;
3793 goto error;
3794 }
3795
3796 return v;
3797 error:
3798 dev_put(v);
3799 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003800}
3801
Jouni Malinendf881292013-02-14 21:10:54 +02003802static struct nla_policy
3803nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3804 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3805 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3806};
3807
Johannes Bergff276692013-02-15 00:09:01 +01003808static int nl80211_parse_sta_wme(struct genl_info *info,
3809 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003810{
Jouni Malinendf881292013-02-14 21:10:54 +02003811 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3812 struct nlattr *nla;
3813 int err;
3814
Jouni Malinendf881292013-02-14 21:10:54 +02003815 /* parse WME attributes if present */
3816 if (!info->attrs[NL80211_ATTR_STA_WME])
3817 return 0;
3818
3819 nla = info->attrs[NL80211_ATTR_STA_WME];
3820 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3821 nl80211_sta_wme_policy);
3822 if (err)
3823 return err;
3824
3825 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3826 params->uapsd_queues = nla_get_u8(
3827 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3828 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3829 return -EINVAL;
3830
3831 if (tb[NL80211_STA_WME_MAX_SP])
3832 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3833
3834 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3835 return -EINVAL;
3836
3837 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3838
3839 return 0;
3840}
3841
Johannes Bergff276692013-02-15 00:09:01 +01003842static int nl80211_set_station_tdls(struct genl_info *info,
3843 struct station_parameters *params)
3844{
3845 /* Dummy STA entry gets updated once the peer capabilities are known */
3846 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3847 params->ht_capa =
3848 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3849 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3850 params->vht_capa =
3851 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3852
3853 return nl80211_parse_sta_wme(info, params);
3854}
3855
Johannes Berg5727ef12007-12-19 02:03:34 +01003856static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3857{
Johannes Berg4c476992010-10-04 21:36:35 +02003858 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003859 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003860 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003861 u8 *mac_addr;
3862 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003863
3864 memset(&params, 0, sizeof(params));
3865
3866 params.listen_interval = -1;
3867
Johannes Berg77ee7c82013-02-15 00:48:33 +01003868 if (!rdev->ops->change_station)
3869 return -EOPNOTSUPP;
3870
Johannes Berg5727ef12007-12-19 02:03:34 +01003871 if (info->attrs[NL80211_ATTR_STA_AID])
3872 return -EINVAL;
3873
3874 if (!info->attrs[NL80211_ATTR_MAC])
3875 return -EINVAL;
3876
3877 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3878
3879 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3880 params.supported_rates =
3881 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3882 params.supported_rates_len =
3883 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3884 }
3885
Jouni Malinen9d62a982013-02-14 21:10:13 +02003886 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3887 params.capability =
3888 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3889 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3890 }
3891
3892 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3893 params.ext_capab =
3894 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3895 params.ext_capab_len =
3896 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3897 }
3898
Jouni Malinendf881292013-02-14 21:10:54 +02003899 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003900 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003901
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003902 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003903 return -EINVAL;
3904
Johannes Bergf8bacc22013-02-14 23:27:01 +01003905 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003906 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003907 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3908 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3909 return -EINVAL;
3910 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003911
Johannes Bergf8bacc22013-02-14 23:27:01 +01003912 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003913 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003914 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3915 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3916 return -EINVAL;
3917 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3918 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003919
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003920 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3921 enum nl80211_mesh_power_mode pm = nla_get_u32(
3922 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3923
3924 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3925 pm > NL80211_MESH_POWER_MAX)
3926 return -EINVAL;
3927
3928 params.local_pm = pm;
3929 }
3930
Johannes Berg77ee7c82013-02-15 00:48:33 +01003931 /* Include parameters for TDLS peer (will check later) */
3932 err = nl80211_set_station_tdls(info, &params);
3933 if (err)
3934 return err;
3935
3936 params.vlan = get_vlan(info, rdev);
3937 if (IS_ERR(params.vlan))
3938 return PTR_ERR(params.vlan);
3939
Johannes Berga97f4422009-06-18 17:23:43 +02003940 switch (dev->ieee80211_ptr->iftype) {
3941 case NL80211_IFTYPE_AP:
3942 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003943 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003944 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003945 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01003946 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02003947 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02003948 break;
3949 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003950 err = -EOPNOTSUPP;
3951 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02003952 }
3953
Johannes Berg77ee7c82013-02-15 00:48:33 +01003954 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03003955 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003956
Johannes Berg77ee7c82013-02-15 00:48:33 +01003957 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01003958 if (params.vlan)
3959 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003960
Johannes Berg5727ef12007-12-19 02:03:34 +01003961 return err;
3962}
3963
3964static int nl80211_new_station(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];
Johannes Berg5727ef12007-12-19 02:03:34 +01003967 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003968 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003969 struct station_parameters params;
3970 u8 *mac_addr = NULL;
3971
3972 memset(&params, 0, sizeof(params));
3973
Johannes Berg984c3112013-02-14 23:43:25 +01003974 if (!rdev->ops->add_station)
3975 return -EOPNOTSUPP;
3976
Johannes Berg5727ef12007-12-19 02:03:34 +01003977 if (!info->attrs[NL80211_ATTR_MAC])
3978 return -EINVAL;
3979
Johannes Berg5727ef12007-12-19 02:03:34 +01003980 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3981 return -EINVAL;
3982
3983 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3984 return -EINVAL;
3985
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003986 if (!info->attrs[NL80211_ATTR_STA_AID])
3987 return -EINVAL;
3988
Johannes Berg5727ef12007-12-19 02:03:34 +01003989 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3990 params.supported_rates =
3991 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3992 params.supported_rates_len =
3993 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3994 params.listen_interval =
3995 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003996
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003997 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3998 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3999 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004000
Jouni Malinen9d62a982013-02-14 21:10:13 +02004001 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4002 params.capability =
4003 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4004 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4005 }
4006
4007 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4008 params.ext_capab =
4009 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4010 params.ext_capab_len =
4011 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4012 }
4013
Jouni Malinen36aedc92008-08-25 11:58:58 +03004014 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4015 params.ht_capa =
4016 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004017
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004018 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4019 params.vht_capa =
4020 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4021
Johannes Bergf8bacc22013-02-14 23:27:01 +01004022 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004023 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004024 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4025 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4026 return -EINVAL;
4027 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004028
Johannes Bergff276692013-02-15 00:09:01 +01004029 err = nl80211_parse_sta_wme(info, &params);
4030 if (err)
4031 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004032
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004033 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004034 return -EINVAL;
4035
Johannes Berg77ee7c82013-02-15 00:48:33 +01004036 /* When you run into this, adjust the code below for the new flag */
4037 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4038
Johannes Bergbdd90d52011-12-14 12:20:27 +01004039 switch (dev->ieee80211_ptr->iftype) {
4040 case NL80211_IFTYPE_AP:
4041 case NL80211_IFTYPE_AP_VLAN:
4042 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004043 /* ignore WME attributes if iface/sta is not capable */
4044 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4045 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4046 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004047
Johannes Bergbdd90d52011-12-14 12:20:27 +01004048 /* TDLS peers cannot be added */
4049 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004050 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004051 /* but don't bother the driver with it */
4052 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004053
Johannes Bergd582cff2012-10-26 17:53:44 +02004054 /* allow authenticated/associated only if driver handles it */
4055 if (!(rdev->wiphy.features &
4056 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4057 params.sta_flags_mask &
4058 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4059 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4060 return -EINVAL;
4061
Johannes Bergbdd90d52011-12-14 12:20:27 +01004062 /* must be last in here for error handling */
4063 params.vlan = get_vlan(info, rdev);
4064 if (IS_ERR(params.vlan))
4065 return PTR_ERR(params.vlan);
4066 break;
4067 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004068 /* ignore uAPSD data */
4069 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4070
Johannes Bergd582cff2012-10-26 17:53:44 +02004071 /* associated is disallowed */
4072 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4073 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004074 /* TDLS peers cannot be added */
4075 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004076 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004077 break;
4078 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004079 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004080 /* ignore uAPSD data */
4081 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4082
Johannes Berg77ee7c82013-02-15 00:48:33 +01004083 /* these are disallowed */
4084 if (params.sta_flags_mask &
4085 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4086 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004087 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004088 /* Only TDLS peers can be added */
4089 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4090 return -EINVAL;
4091 /* Can only add if TDLS ... */
4092 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4093 return -EOPNOTSUPP;
4094 /* ... with external setup is supported */
4095 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4096 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004097 /*
4098 * Older wpa_supplicant versions always mark the TDLS peer
4099 * as authorized, but it shouldn't yet be.
4100 */
4101 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004102 break;
4103 default:
4104 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004105 }
4106
Johannes Bergbdd90d52011-12-14 12:20:27 +01004107 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004108
Hila Gonene35e4d22012-06-27 17:19:42 +03004109 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004110
Johannes Berg5727ef12007-12-19 02:03:34 +01004111 if (params.vlan)
4112 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004113 return err;
4114}
4115
4116static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4117{
Johannes Berg4c476992010-10-04 21:36:35 +02004118 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4119 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004120 u8 *mac_addr = NULL;
4121
4122 if (info->attrs[NL80211_ATTR_MAC])
4123 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4124
Johannes Berge80cf852009-05-11 14:43:13 +02004125 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004126 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004127 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004128 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4129 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004130
Johannes Berg4c476992010-10-04 21:36:35 +02004131 if (!rdev->ops->del_station)
4132 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004133
Hila Gonene35e4d22012-06-27 17:19:42 +03004134 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004135}
4136
Eric W. Biederman15e47302012-09-07 20:12:54 +00004137static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004138 int flags, struct net_device *dev,
4139 u8 *dst, u8 *next_hop,
4140 struct mpath_info *pinfo)
4141{
4142 void *hdr;
4143 struct nlattr *pinfoattr;
4144
Eric W. Biederman15e47302012-09-07 20:12:54 +00004145 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004146 if (!hdr)
4147 return -1;
4148
David S. Miller9360ffd2012-03-29 04:41:26 -04004149 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4150 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4151 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4152 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4153 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004154
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004155 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4156 if (!pinfoattr)
4157 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004158 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4159 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4160 pinfo->frame_qlen))
4161 goto nla_put_failure;
4162 if (((pinfo->filled & MPATH_INFO_SN) &&
4163 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4164 ((pinfo->filled & MPATH_INFO_METRIC) &&
4165 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4166 pinfo->metric)) ||
4167 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4168 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4169 pinfo->exptime)) ||
4170 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4171 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4172 pinfo->flags)) ||
4173 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4174 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4175 pinfo->discovery_timeout)) ||
4176 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4177 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4178 pinfo->discovery_retries)))
4179 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004180
4181 nla_nest_end(msg, pinfoattr);
4182
4183 return genlmsg_end(msg, hdr);
4184
4185 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004186 genlmsg_cancel(msg, hdr);
4187 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004188}
4189
4190static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004191 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004192{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004193 struct mpath_info pinfo;
4194 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004195 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004196 u8 dst[ETH_ALEN];
4197 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004198 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004199 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004200
Johannes Berg97990a02013-04-19 01:02:55 +02004201 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004202 if (err)
4203 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004204
4205 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004206 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004207 goto out_err;
4208 }
4209
Johannes Berg97990a02013-04-19 01:02:55 +02004210 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004211 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004212 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004213 }
4214
Johannes Bergbba95fe2008-07-29 13:22:51 +02004215 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004216 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4217 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004218 if (err == -ENOENT)
4219 break;
4220 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004221 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004222
Eric W. Biederman15e47302012-09-07 20:12:54 +00004223 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004224 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004225 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004226 &pinfo) < 0)
4227 goto out;
4228
4229 path_idx++;
4230 }
4231
4232
4233 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004234 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004235 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004236 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004237 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004238 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004239}
4240
4241static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4242{
Johannes Berg4c476992010-10-04 21:36:35 +02004243 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004244 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004245 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004246 struct mpath_info pinfo;
4247 struct sk_buff *msg;
4248 u8 *dst = NULL;
4249 u8 next_hop[ETH_ALEN];
4250
4251 memset(&pinfo, 0, sizeof(pinfo));
4252
4253 if (!info->attrs[NL80211_ATTR_MAC])
4254 return -EINVAL;
4255
4256 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4257
Johannes Berg4c476992010-10-04 21:36:35 +02004258 if (!rdev->ops->get_mpath)
4259 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004260
Johannes Berg4c476992010-10-04 21:36:35 +02004261 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4262 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004263
Hila Gonene35e4d22012-06-27 17:19:42 +03004264 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004265 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004266 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004267
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004268 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004269 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004270 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004271
Eric W. Biederman15e47302012-09-07 20:12:54 +00004272 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004273 dev, dst, next_hop, &pinfo) < 0) {
4274 nlmsg_free(msg);
4275 return -ENOBUFS;
4276 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004277
Johannes Berg4c476992010-10-04 21:36:35 +02004278 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004279}
4280
4281static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4282{
Johannes Berg4c476992010-10-04 21:36:35 +02004283 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4284 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004285 u8 *dst = NULL;
4286 u8 *next_hop = NULL;
4287
4288 if (!info->attrs[NL80211_ATTR_MAC])
4289 return -EINVAL;
4290
4291 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4292 return -EINVAL;
4293
4294 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4295 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4296
Johannes Berg4c476992010-10-04 21:36:35 +02004297 if (!rdev->ops->change_mpath)
4298 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004299
Johannes Berg4c476992010-10-04 21:36:35 +02004300 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4301 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004302
Hila Gonene35e4d22012-06-27 17:19:42 +03004303 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004304}
Johannes Berg4c476992010-10-04 21:36:35 +02004305
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004306static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4307{
Johannes Berg4c476992010-10-04 21:36:35 +02004308 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4309 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004310 u8 *dst = NULL;
4311 u8 *next_hop = NULL;
4312
4313 if (!info->attrs[NL80211_ATTR_MAC])
4314 return -EINVAL;
4315
4316 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4317 return -EINVAL;
4318
4319 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4320 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4321
Johannes Berg4c476992010-10-04 21:36:35 +02004322 if (!rdev->ops->add_mpath)
4323 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004324
Johannes Berg4c476992010-10-04 21:36:35 +02004325 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4326 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004327
Hila Gonene35e4d22012-06-27 17:19:42 +03004328 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004329}
4330
4331static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4332{
Johannes Berg4c476992010-10-04 21:36:35 +02004333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4334 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004335 u8 *dst = NULL;
4336
4337 if (info->attrs[NL80211_ATTR_MAC])
4338 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4339
Johannes Berg4c476992010-10-04 21:36:35 +02004340 if (!rdev->ops->del_mpath)
4341 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004342
Hila Gonene35e4d22012-06-27 17:19:42 +03004343 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004344}
4345
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004346static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4347{
Johannes Berg4c476992010-10-04 21:36:35 +02004348 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4349 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004350 struct bss_parameters params;
4351
4352 memset(&params, 0, sizeof(params));
4353 /* default to not changing parameters */
4354 params.use_cts_prot = -1;
4355 params.use_short_preamble = -1;
4356 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004357 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004358 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004359 params.p2p_ctwindow = -1;
4360 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004361
4362 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4363 params.use_cts_prot =
4364 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4365 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4366 params.use_short_preamble =
4367 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4368 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4369 params.use_short_slot_time =
4370 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004371 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4372 params.basic_rates =
4373 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4374 params.basic_rates_len =
4375 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4376 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004377 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4378 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004379 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4380 params.ht_opmode =
4381 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004382
Johannes Berg53cabad2012-11-14 15:17:28 +01004383 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4384 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4385 return -EINVAL;
4386 params.p2p_ctwindow =
4387 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4388 if (params.p2p_ctwindow < 0)
4389 return -EINVAL;
4390 if (params.p2p_ctwindow != 0 &&
4391 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4392 return -EINVAL;
4393 }
4394
4395 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4396 u8 tmp;
4397
4398 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4399 return -EINVAL;
4400 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4401 if (tmp > 1)
4402 return -EINVAL;
4403 params.p2p_opp_ps = tmp;
4404 if (params.p2p_opp_ps &&
4405 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4406 return -EINVAL;
4407 }
4408
Johannes Berg4c476992010-10-04 21:36:35 +02004409 if (!rdev->ops->change_bss)
4410 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004411
Johannes Berg074ac8d2010-09-16 14:58:22 +02004412 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004413 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4414 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004415
Hila Gonene35e4d22012-06-27 17:19:42 +03004416 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004417}
4418
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004419static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004420 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4421 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4422 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4423 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4424 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4425 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4426};
4427
4428static int parse_reg_rule(struct nlattr *tb[],
4429 struct ieee80211_reg_rule *reg_rule)
4430{
4431 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4432 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4433
4434 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4435 return -EINVAL;
4436 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4437 return -EINVAL;
4438 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4439 return -EINVAL;
4440 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4441 return -EINVAL;
4442 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4443 return -EINVAL;
4444
4445 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4446
4447 freq_range->start_freq_khz =
4448 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4449 freq_range->end_freq_khz =
4450 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4451 freq_range->max_bandwidth_khz =
4452 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4453
4454 power_rule->max_eirp =
4455 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4456
4457 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4458 power_rule->max_antenna_gain =
4459 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4460
4461 return 0;
4462}
4463
4464static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4465{
4466 int r;
4467 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004468 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004469
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004470 /*
4471 * You should only get this when cfg80211 hasn't yet initialized
4472 * completely when built-in to the kernel right between the time
4473 * window between nl80211_init() and regulatory_init(), if that is
4474 * even possible.
4475 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004476 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004477 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004478
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004479 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4480 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004481
4482 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4483
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004484 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4485 user_reg_hint_type =
4486 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4487 else
4488 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4489
4490 switch (user_reg_hint_type) {
4491 case NL80211_USER_REG_HINT_USER:
4492 case NL80211_USER_REG_HINT_CELL_BASE:
4493 break;
4494 default:
4495 return -EINVAL;
4496 }
4497
4498 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004499
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004500 return r;
4501}
4502
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004503static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004504 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004505{
Johannes Berg4c476992010-10-04 21:36:35 +02004506 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004507 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004508 struct wireless_dev *wdev = dev->ieee80211_ptr;
4509 struct mesh_config cur_params;
4510 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004511 void *hdr;
4512 struct nlattr *pinfoattr;
4513 struct sk_buff *msg;
4514
Johannes Berg29cbe682010-12-03 09:20:44 +01004515 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4516 return -EOPNOTSUPP;
4517
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004518 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004519 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004520
Johannes Berg29cbe682010-12-03 09:20:44 +01004521 wdev_lock(wdev);
4522 /* If not connected, get default parameters */
4523 if (!wdev->mesh_id_len)
4524 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4525 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004526 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004527 wdev_unlock(wdev);
4528
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004529 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004530 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004531
4532 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004533 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004534 if (!msg)
4535 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004536 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004537 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004538 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004539 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004540 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004541 if (!pinfoattr)
4542 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004543 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4544 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4545 cur_params.dot11MeshRetryTimeout) ||
4546 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4547 cur_params.dot11MeshConfirmTimeout) ||
4548 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4549 cur_params.dot11MeshHoldingTimeout) ||
4550 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4551 cur_params.dot11MeshMaxPeerLinks) ||
4552 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4553 cur_params.dot11MeshMaxRetries) ||
4554 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4555 cur_params.dot11MeshTTL) ||
4556 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4557 cur_params.element_ttl) ||
4558 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4559 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004560 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4561 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004562 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4563 cur_params.dot11MeshHWMPmaxPREQretries) ||
4564 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4565 cur_params.path_refresh_time) ||
4566 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4567 cur_params.min_discovery_timeout) ||
4568 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4569 cur_params.dot11MeshHWMPactivePathTimeout) ||
4570 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4571 cur_params.dot11MeshHWMPpreqMinInterval) ||
4572 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4573 cur_params.dot11MeshHWMPperrMinInterval) ||
4574 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4575 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4576 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4577 cur_params.dot11MeshHWMPRootMode) ||
4578 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4579 cur_params.dot11MeshHWMPRannInterval) ||
4580 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4581 cur_params.dot11MeshGateAnnouncementProtocol) ||
4582 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4583 cur_params.dot11MeshForwarding) ||
4584 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004585 cur_params.rssi_threshold) ||
4586 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004587 cur_params.ht_opmode) ||
4588 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4589 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4590 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004591 cur_params.dot11MeshHWMProotInterval) ||
4592 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004593 cur_params.dot11MeshHWMPconfirmationInterval) ||
4594 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4595 cur_params.power_mode) ||
4596 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4597 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004598 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004599 nla_nest_end(msg, pinfoattr);
4600 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004601 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004602
Johannes Berg3b858752009-03-12 09:55:09 +01004603 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004604 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004605 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004606 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004607 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004608}
4609
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004610static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004611 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4612 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4613 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4614 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4615 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4616 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004617 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004618 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004619 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004620 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4621 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4622 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4623 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4624 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004625 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004626 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004627 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004628 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004629 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004630 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004631 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4632 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004633 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4634 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004635 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004636 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4637 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004638};
4639
Javier Cardonac80d5452010-12-16 17:37:49 -08004640static const struct nla_policy
4641 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004642 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004643 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4644 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004645 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004646 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004647 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004648 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004649 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004650};
4651
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004652static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004653 struct mesh_config *cfg,
4654 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004655{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004656 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004657 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004658
Marco Porschea54fba2013-01-07 16:04:48 +01004659#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4660do { \
4661 if (tb[attr]) { \
4662 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4663 return -EINVAL; \
4664 cfg->param = fn(tb[attr]); \
4665 mask |= (1 << (attr - 1)); \
4666 } \
4667} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004668
4669
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004670 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004671 return -EINVAL;
4672 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004673 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004674 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004675 return -EINVAL;
4676
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004677 /* This makes sure that there aren't more than 32 mesh config
4678 * parameters (otherwise our bitfield scheme would not work.) */
4679 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4680
4681 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004682 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004683 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4684 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004685 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004686 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4687 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004688 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004689 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4690 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004691 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004692 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4693 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004694 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004695 mask, NL80211_MESHCONF_MAX_RETRIES,
4696 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004697 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004698 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004699 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004700 mask, NL80211_MESHCONF_ELEMENT_TTL,
4701 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004702 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004703 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4704 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004705 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4706 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004707 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4708 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004709 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004710 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4711 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004712 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004713 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4714 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004715 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004716 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4717 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004718 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4719 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004720 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4721 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004722 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004723 1, 65535, mask,
4724 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004725 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004726 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004727 1, 65535, mask,
4728 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004729 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004730 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004731 dot11MeshHWMPnetDiameterTraversalTime,
4732 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004733 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4734 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004735 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4736 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4737 nla_get_u8);
4738 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4739 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004740 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004741 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004742 dot11MeshGateAnnouncementProtocol, 0, 1,
4743 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004744 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004745 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004746 mask, NL80211_MESHCONF_FORWARDING,
4747 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004748 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004749 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4750 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004751 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004752 mask, NL80211_MESHCONF_HT_OPMODE,
4753 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004754 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004755 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004756 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4757 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004758 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004759 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4760 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004761 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004762 dot11MeshHWMPconfirmationInterval,
4763 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004764 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4765 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004766 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4767 NL80211_MESH_POWER_ACTIVE,
4768 NL80211_MESH_POWER_MAX,
4769 mask, NL80211_MESHCONF_POWER_MODE,
4770 nla_get_u32);
4771 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4772 0, 65535, mask,
4773 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004774 if (mask_out)
4775 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004776
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004777 return 0;
4778
4779#undef FILL_IN_MESH_PARAM_IF_SET
4780}
4781
Javier Cardonac80d5452010-12-16 17:37:49 -08004782static int nl80211_parse_mesh_setup(struct genl_info *info,
4783 struct mesh_setup *setup)
4784{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004786 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4787
4788 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4789 return -EINVAL;
4790 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4791 info->attrs[NL80211_ATTR_MESH_SETUP],
4792 nl80211_mesh_setup_params_policy))
4793 return -EINVAL;
4794
Javier Cardonad299a1f2012-03-31 11:31:33 -07004795 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4796 setup->sync_method =
4797 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4798 IEEE80211_SYNC_METHOD_VENDOR :
4799 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4800
Javier Cardonac80d5452010-12-16 17:37:49 -08004801 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4802 setup->path_sel_proto =
4803 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4804 IEEE80211_PATH_PROTOCOL_VENDOR :
4805 IEEE80211_PATH_PROTOCOL_HWMP;
4806
4807 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4808 setup->path_metric =
4809 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4810 IEEE80211_PATH_METRIC_VENDOR :
4811 IEEE80211_PATH_METRIC_AIRTIME;
4812
Javier Cardona581a8b02011-04-07 15:08:27 -07004813
4814 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004815 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004816 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004817 if (!is_valid_ie_attr(ieattr))
4818 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004819 setup->ie = nla_data(ieattr);
4820 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004821 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004822 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4823 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4824 return -EINVAL;
4825 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004826 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4827 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004828 if (setup->is_secure)
4829 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08004830
4831 return 0;
4832}
4833
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004834static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004835 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004836{
4837 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4838 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004839 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004840 struct mesh_config cfg;
4841 u32 mask;
4842 int err;
4843
Johannes Berg29cbe682010-12-03 09:20:44 +01004844 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4845 return -EOPNOTSUPP;
4846
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004847 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004848 return -EOPNOTSUPP;
4849
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004850 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004851 if (err)
4852 return err;
4853
Johannes Berg29cbe682010-12-03 09:20:44 +01004854 wdev_lock(wdev);
4855 if (!wdev->mesh_id_len)
4856 err = -ENOLINK;
4857
4858 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004859 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004860
4861 wdev_unlock(wdev);
4862
4863 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004864}
4865
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004866static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4867{
Johannes Berg458f4f92012-12-06 15:47:38 +01004868 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004869 struct sk_buff *msg;
4870 void *hdr = NULL;
4871 struct nlattr *nl_reg_rules;
4872 unsigned int i;
4873 int err = -EINVAL;
4874
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004875 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004876
4877 if (!cfg80211_regdomain)
4878 goto out;
4879
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004880 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004881 if (!msg) {
4882 err = -ENOBUFS;
4883 goto out;
4884 }
4885
Eric W. Biederman15e47302012-09-07 20:12:54 +00004886 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004887 NL80211_CMD_GET_REG);
4888 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004889 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004890
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004891 if (reg_last_request_cell_base() &&
4892 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4893 NL80211_USER_REG_HINT_CELL_BASE))
4894 goto nla_put_failure;
4895
Johannes Berg458f4f92012-12-06 15:47:38 +01004896 rcu_read_lock();
4897 regdom = rcu_dereference(cfg80211_regdomain);
4898
4899 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4900 (regdom->dfs_region &&
4901 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4902 goto nla_put_failure_rcu;
4903
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004904 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4905 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004906 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004907
Johannes Berg458f4f92012-12-06 15:47:38 +01004908 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004909 struct nlattr *nl_reg_rule;
4910 const struct ieee80211_reg_rule *reg_rule;
4911 const struct ieee80211_freq_range *freq_range;
4912 const struct ieee80211_power_rule *power_rule;
4913
Johannes Berg458f4f92012-12-06 15:47:38 +01004914 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004915 freq_range = &reg_rule->freq_range;
4916 power_rule = &reg_rule->power_rule;
4917
4918 nl_reg_rule = nla_nest_start(msg, i);
4919 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004920 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004921
David S. Miller9360ffd2012-03-29 04:41:26 -04004922 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4923 reg_rule->flags) ||
4924 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4925 freq_range->start_freq_khz) ||
4926 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4927 freq_range->end_freq_khz) ||
4928 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4929 freq_range->max_bandwidth_khz) ||
4930 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4931 power_rule->max_antenna_gain) ||
4932 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4933 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004934 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004935
4936 nla_nest_end(msg, nl_reg_rule);
4937 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004938 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004939
4940 nla_nest_end(msg, nl_reg_rules);
4941
4942 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004943 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004944 goto out;
4945
Johannes Berg458f4f92012-12-06 15:47:38 +01004946nla_put_failure_rcu:
4947 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004948nla_put_failure:
4949 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004950put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004951 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004952 err = -EMSGSIZE;
4953out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004954 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004955 return err;
4956}
4957
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004958static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4959{
4960 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4961 struct nlattr *nl_reg_rule;
4962 char *alpha2 = NULL;
4963 int rem_reg_rules = 0, r = 0;
4964 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004965 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004966 struct ieee80211_regdomain *rd = NULL;
4967
4968 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4969 return -EINVAL;
4970
4971 if (!info->attrs[NL80211_ATTR_REG_RULES])
4972 return -EINVAL;
4973
4974 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4975
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004976 if (info->attrs[NL80211_ATTR_DFS_REGION])
4977 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4978
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004979 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004980 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004981 num_rules++;
4982 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004983 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004984 }
4985
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004986 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004987 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004988
4989 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004990 if (!rd)
4991 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004992
4993 rd->n_reg_rules = num_rules;
4994 rd->alpha2[0] = alpha2[0];
4995 rd->alpha2[1] = alpha2[1];
4996
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004997 /*
4998 * Disable DFS master mode if the DFS region was
4999 * not supported or known on this kernel.
5000 */
5001 if (reg_supported_dfs_region(dfs_region))
5002 rd->dfs_region = dfs_region;
5003
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005004 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005005 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005006 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01005007 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5008 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005009 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5010 if (r)
5011 goto bad_reg;
5012
5013 rule_idx++;
5014
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005015 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5016 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005017 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005018 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005019 }
5020
Johannes Berg6913b492012-12-04 00:48:59 +01005021 mutex_lock(&cfg80211_mutex);
5022
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005023 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005024 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005025 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01005026 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005027
Johannes Bergd2372b32008-10-24 20:32:20 +02005028 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005029 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005030 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005031}
5032
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005033static int validate_scan_freqs(struct nlattr *freqs)
5034{
5035 struct nlattr *attr1, *attr2;
5036 int n_channels = 0, tmp1, tmp2;
5037
5038 nla_for_each_nested(attr1, freqs, tmp1) {
5039 n_channels++;
5040 /*
5041 * Some hardware has a limited channel list for
5042 * scanning, and it is pretty much nonsensical
5043 * to scan for a channel twice, so disallow that
5044 * and don't require drivers to check that the
5045 * channel list they get isn't longer than what
5046 * they can scan, as long as they can scan all
5047 * the channels they registered at once.
5048 */
5049 nla_for_each_nested(attr2, freqs, tmp2)
5050 if (attr1 != attr2 &&
5051 nla_get_u32(attr1) == nla_get_u32(attr2))
5052 return 0;
5053 }
5054
5055 return n_channels;
5056}
5057
Johannes Berg2a519312009-02-10 21:25:55 +01005058static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5059{
Johannes Berg4c476992010-10-04 21:36:35 +02005060 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005061 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005062 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005063 struct nlattr *attr;
5064 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005065 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005066 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005067
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005068 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5069 return -EINVAL;
5070
Johannes Berg79c97e92009-07-07 03:56:12 +02005071 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005072
Johannes Berg4c476992010-10-04 21:36:35 +02005073 if (!rdev->ops->scan)
5074 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005075
Johannes Bergf9f47522013-03-19 15:04:07 +01005076 mutex_lock(&rdev->sched_scan_mtx);
5077 if (rdev->scan_req) {
5078 err = -EBUSY;
5079 goto unlock;
5080 }
Johannes Berg2a519312009-02-10 21:25:55 +01005081
5082 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005083 n_channels = validate_scan_freqs(
5084 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005085 if (!n_channels) {
5086 err = -EINVAL;
5087 goto unlock;
5088 }
Johannes Berg2a519312009-02-10 21:25:55 +01005089 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005090 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005091 n_channels = 0;
5092
Johannes Berg2a519312009-02-10 21:25:55 +01005093 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5094 if (wiphy->bands[band])
5095 n_channels += wiphy->bands[band]->n_channels;
5096 }
5097
5098 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5099 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5100 n_ssids++;
5101
Johannes Bergf9f47522013-03-19 15:04:07 +01005102 if (n_ssids > wiphy->max_scan_ssids) {
5103 err = -EINVAL;
5104 goto unlock;
5105 }
Johannes Berg2a519312009-02-10 21:25:55 +01005106
Jouni Malinen70692ad2009-02-16 19:39:13 +02005107 if (info->attrs[NL80211_ATTR_IE])
5108 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5109 else
5110 ie_len = 0;
5111
Johannes Bergf9f47522013-03-19 15:04:07 +01005112 if (ie_len > wiphy->max_scan_ie_len) {
5113 err = -EINVAL;
5114 goto unlock;
5115 }
Johannes Berg18a83652009-03-31 12:12:05 +02005116
Johannes Berg2a519312009-02-10 21:25:55 +01005117 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005118 + sizeof(*request->ssids) * n_ssids
5119 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005120 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005121 if (!request) {
5122 err = -ENOMEM;
5123 goto unlock;
5124 }
Johannes Berg2a519312009-02-10 21:25:55 +01005125
Johannes Berg2a519312009-02-10 21:25:55 +01005126 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005127 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005128 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005129 if (ie_len) {
5130 if (request->ssids)
5131 request->ie = (void *)(request->ssids + n_ssids);
5132 else
5133 request->ie = (void *)(request->channels + n_channels);
5134 }
Johannes Berg2a519312009-02-10 21:25:55 +01005135
Johannes Berg584991d2009-11-02 13:32:03 +01005136 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005137 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5138 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005139 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005140 struct ieee80211_channel *chan;
5141
5142 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5143
5144 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005145 err = -EINVAL;
5146 goto out_free;
5147 }
Johannes Berg584991d2009-11-02 13:32:03 +01005148
5149 /* ignore disabled channels */
5150 if (chan->flags & IEEE80211_CHAN_DISABLED)
5151 continue;
5152
5153 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005154 i++;
5155 }
5156 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005157 enum ieee80211_band band;
5158
Johannes Berg2a519312009-02-10 21:25:55 +01005159 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005160 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5161 int j;
5162 if (!wiphy->bands[band])
5163 continue;
5164 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005165 struct ieee80211_channel *chan;
5166
5167 chan = &wiphy->bands[band]->channels[j];
5168
5169 if (chan->flags & IEEE80211_CHAN_DISABLED)
5170 continue;
5171
5172 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005173 i++;
5174 }
5175 }
5176 }
5177
Johannes Berg584991d2009-11-02 13:32:03 +01005178 if (!i) {
5179 err = -EINVAL;
5180 goto out_free;
5181 }
5182
5183 request->n_channels = i;
5184
Johannes Berg2a519312009-02-10 21:25:55 +01005185 i = 0;
5186 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5187 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005188 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005189 err = -EINVAL;
5190 goto out_free;
5191 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005192 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005193 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005194 i++;
5195 }
5196 }
5197
Jouni Malinen70692ad2009-02-16 19:39:13 +02005198 if (info->attrs[NL80211_ATTR_IE]) {
5199 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005200 memcpy((void *)request->ie,
5201 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005202 request->ie_len);
5203 }
5204
Johannes Berg34850ab2011-07-18 18:08:35 +02005205 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005206 if (wiphy->bands[i])
5207 request->rates[i] =
5208 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005209
5210 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5211 nla_for_each_nested(attr,
5212 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5213 tmp) {
5214 enum ieee80211_band band = nla_type(attr);
5215
Dan Carpenter84404622011-07-29 11:52:18 +03005216 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005217 err = -EINVAL;
5218 goto out_free;
5219 }
5220 err = ieee80211_get_ratemask(wiphy->bands[band],
5221 nla_data(attr),
5222 nla_len(attr),
5223 &request->rates[band]);
5224 if (err)
5225 goto out_free;
5226 }
5227 }
5228
Sam Leffler46856bb2012-10-11 21:03:32 -07005229 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005230 request->flags = nla_get_u32(
5231 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005232 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5233 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5234 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5235 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005236 err = -EOPNOTSUPP;
5237 goto out_free;
5238 }
5239 }
Sam Lefflered4737712012-10-11 21:03:31 -07005240
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305241 request->no_cck =
5242 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5243
Johannes Bergfd014282012-06-18 19:17:03 +02005244 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005245 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005246 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005247
Johannes Berg79c97e92009-07-07 03:56:12 +02005248 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005249 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005250
Johannes Berg463d0182009-07-14 00:33:35 +02005251 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005252 nl80211_send_scan_start(rdev, wdev);
5253 if (wdev->netdev)
5254 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005255 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005256 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005257 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005258 kfree(request);
5259 }
Johannes Berg3b858752009-03-12 09:55:09 +01005260
Johannes Bergf9f47522013-03-19 15:04:07 +01005261 unlock:
5262 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01005263 return err;
5264}
5265
Luciano Coelho807f8a82011-05-11 17:09:35 +03005266static int nl80211_start_sched_scan(struct sk_buff *skb,
5267 struct genl_info *info)
5268{
5269 struct cfg80211_sched_scan_request *request;
5270 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5271 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005272 struct nlattr *attr;
5273 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005274 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005275 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005276 enum ieee80211_band band;
5277 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005278 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005279
5280 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5281 !rdev->ops->sched_scan_start)
5282 return -EOPNOTSUPP;
5283
5284 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5285 return -EINVAL;
5286
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005287 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5288 return -EINVAL;
5289
5290 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5291 if (interval == 0)
5292 return -EINVAL;
5293
Luciano Coelho807f8a82011-05-11 17:09:35 +03005294 wiphy = &rdev->wiphy;
5295
5296 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5297 n_channels = validate_scan_freqs(
5298 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5299 if (!n_channels)
5300 return -EINVAL;
5301 } else {
5302 n_channels = 0;
5303
5304 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5305 if (wiphy->bands[band])
5306 n_channels += wiphy->bands[band]->n_channels;
5307 }
5308
5309 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5310 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5311 tmp)
5312 n_ssids++;
5313
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005314 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005315 return -EINVAL;
5316
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005317 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5318 nla_for_each_nested(attr,
5319 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5320 tmp)
5321 n_match_sets++;
5322
5323 if (n_match_sets > wiphy->max_match_sets)
5324 return -EINVAL;
5325
Luciano Coelho807f8a82011-05-11 17:09:35 +03005326 if (info->attrs[NL80211_ATTR_IE])
5327 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5328 else
5329 ie_len = 0;
5330
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005331 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005332 return -EINVAL;
5333
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005334 mutex_lock(&rdev->sched_scan_mtx);
5335
5336 if (rdev->sched_scan_req) {
5337 err = -EINPROGRESS;
5338 goto out;
5339 }
5340
Luciano Coelho807f8a82011-05-11 17:09:35 +03005341 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005342 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005343 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005344 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005345 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005346 if (!request) {
5347 err = -ENOMEM;
5348 goto out;
5349 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005350
5351 if (n_ssids)
5352 request->ssids = (void *)&request->channels[n_channels];
5353 request->n_ssids = n_ssids;
5354 if (ie_len) {
5355 if (request->ssids)
5356 request->ie = (void *)(request->ssids + n_ssids);
5357 else
5358 request->ie = (void *)(request->channels + n_channels);
5359 }
5360
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005361 if (n_match_sets) {
5362 if (request->ie)
5363 request->match_sets = (void *)(request->ie + ie_len);
5364 else if (request->ssids)
5365 request->match_sets =
5366 (void *)(request->ssids + n_ssids);
5367 else
5368 request->match_sets =
5369 (void *)(request->channels + n_channels);
5370 }
5371 request->n_match_sets = n_match_sets;
5372
Luciano Coelho807f8a82011-05-11 17:09:35 +03005373 i = 0;
5374 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5375 /* user specified, bail out if channel not found */
5376 nla_for_each_nested(attr,
5377 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5378 tmp) {
5379 struct ieee80211_channel *chan;
5380
5381 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5382
5383 if (!chan) {
5384 err = -EINVAL;
5385 goto out_free;
5386 }
5387
5388 /* ignore disabled channels */
5389 if (chan->flags & IEEE80211_CHAN_DISABLED)
5390 continue;
5391
5392 request->channels[i] = chan;
5393 i++;
5394 }
5395 } else {
5396 /* all channels */
5397 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5398 int j;
5399 if (!wiphy->bands[band])
5400 continue;
5401 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5402 struct ieee80211_channel *chan;
5403
5404 chan = &wiphy->bands[band]->channels[j];
5405
5406 if (chan->flags & IEEE80211_CHAN_DISABLED)
5407 continue;
5408
5409 request->channels[i] = chan;
5410 i++;
5411 }
5412 }
5413 }
5414
5415 if (!i) {
5416 err = -EINVAL;
5417 goto out_free;
5418 }
5419
5420 request->n_channels = i;
5421
5422 i = 0;
5423 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5424 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5425 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005426 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005427 err = -EINVAL;
5428 goto out_free;
5429 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005430 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005431 memcpy(request->ssids[i].ssid, nla_data(attr),
5432 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005433 i++;
5434 }
5435 }
5436
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005437 i = 0;
5438 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5439 nla_for_each_nested(attr,
5440 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5441 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005442 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005443
5444 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5445 nla_data(attr), nla_len(attr),
5446 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005447 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005448 if (ssid) {
5449 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5450 err = -EINVAL;
5451 goto out_free;
5452 }
5453 memcpy(request->match_sets[i].ssid.ssid,
5454 nla_data(ssid), nla_len(ssid));
5455 request->match_sets[i].ssid.ssid_len =
5456 nla_len(ssid);
5457 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005458 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5459 if (rssi)
5460 request->rssi_thold = nla_get_u32(rssi);
5461 else
5462 request->rssi_thold =
5463 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005464 i++;
5465 }
5466 }
5467
Luciano Coelho807f8a82011-05-11 17:09:35 +03005468 if (info->attrs[NL80211_ATTR_IE]) {
5469 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5470 memcpy((void *)request->ie,
5471 nla_data(info->attrs[NL80211_ATTR_IE]),
5472 request->ie_len);
5473 }
5474
Sam Leffler46856bb2012-10-11 21:03:32 -07005475 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005476 request->flags = nla_get_u32(
5477 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005478 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5479 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5480 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5481 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005482 err = -EOPNOTSUPP;
5483 goto out_free;
5484 }
5485 }
Sam Lefflered4737712012-10-11 21:03:31 -07005486
Luciano Coelho807f8a82011-05-11 17:09:35 +03005487 request->dev = dev;
5488 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005489 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005490 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005491
Hila Gonene35e4d22012-06-27 17:19:42 +03005492 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005493 if (!err) {
5494 rdev->sched_scan_req = request;
5495 nl80211_send_sched_scan(rdev, dev,
5496 NL80211_CMD_START_SCHED_SCAN);
5497 goto out;
5498 }
5499
5500out_free:
5501 kfree(request);
5502out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005503 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005504 return err;
5505}
5506
5507static int nl80211_stop_sched_scan(struct sk_buff *skb,
5508 struct genl_info *info)
5509{
5510 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005511 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005512
5513 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5514 !rdev->ops->sched_scan_stop)
5515 return -EOPNOTSUPP;
5516
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005517 mutex_lock(&rdev->sched_scan_mtx);
5518 err = __cfg80211_stop_sched_scan(rdev, false);
5519 mutex_unlock(&rdev->sched_scan_mtx);
5520
5521 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005522}
5523
Simon Wunderlich04f39042013-02-08 18:16:19 +01005524static int nl80211_start_radar_detection(struct sk_buff *skb,
5525 struct genl_info *info)
5526{
5527 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5528 struct net_device *dev = info->user_ptr[1];
5529 struct wireless_dev *wdev = dev->ieee80211_ptr;
5530 struct cfg80211_chan_def chandef;
5531 int err;
5532
5533 err = nl80211_parse_chandef(rdev, info, &chandef);
5534 if (err)
5535 return err;
5536
5537 if (wdev->cac_started)
5538 return -EBUSY;
5539
5540 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5541 if (err < 0)
5542 return err;
5543
5544 if (err == 0)
5545 return -EINVAL;
5546
5547 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5548 return -EINVAL;
5549
5550 if (!rdev->ops->start_radar_detection)
5551 return -EOPNOTSUPP;
5552
5553 mutex_lock(&rdev->devlist_mtx);
5554 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5555 chandef.chan, CHAN_MODE_SHARED,
5556 BIT(chandef.width));
5557 if (err)
5558 goto err_locked;
5559
5560 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5561 if (!err) {
5562 wdev->channel = chandef.chan;
5563 wdev->cac_started = true;
5564 wdev->cac_start_time = jiffies;
5565 }
5566err_locked:
5567 mutex_unlock(&rdev->devlist_mtx);
5568
5569 return err;
5570}
5571
Johannes Berg9720bb32011-06-21 09:45:33 +02005572static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5573 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005574 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005575 struct wireless_dev *wdev,
5576 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005577{
Johannes Berg48ab9052009-07-10 18:42:31 +02005578 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005579 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005580 void *hdr;
5581 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005582 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005583
5584 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005585
Eric W. Biederman15e47302012-09-07 20:12:54 +00005586 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005587 NL80211_CMD_NEW_SCAN_RESULTS);
5588 if (!hdr)
5589 return -1;
5590
Johannes Berg9720bb32011-06-21 09:45:33 +02005591 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5592
Johannes Berg97990a02013-04-19 01:02:55 +02005593 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5594 goto nla_put_failure;
5595 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005596 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5597 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005598 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5599 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005600
5601 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5602 if (!bss)
5603 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005604 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005605 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005606 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005607
5608 rcu_read_lock();
5609 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005610 if (ies) {
5611 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5612 goto fail_unlock_rcu;
5613 tsf = true;
5614 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5615 ies->len, ies->data))
5616 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005617 }
5618 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005619 if (ies) {
5620 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5621 goto fail_unlock_rcu;
5622 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5623 ies->len, ies->data))
5624 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005625 }
5626 rcu_read_unlock();
5627
David S. Miller9360ffd2012-03-29 04:41:26 -04005628 if (res->beacon_interval &&
5629 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5630 goto nla_put_failure;
5631 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5632 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5633 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5634 jiffies_to_msecs(jiffies - intbss->ts)))
5635 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005636
Johannes Berg77965c92009-02-18 18:45:06 +01005637 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005638 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005639 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5640 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005641 break;
5642 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005643 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5644 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005645 break;
5646 default:
5647 break;
5648 }
5649
Johannes Berg48ab9052009-07-10 18:42:31 +02005650 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005651 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005652 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005653 if (intbss == wdev->current_bss &&
5654 nla_put_u32(msg, NL80211_BSS_STATUS,
5655 NL80211_BSS_STATUS_ASSOCIATED))
5656 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005657 break;
5658 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005659 if (intbss == wdev->current_bss &&
5660 nla_put_u32(msg, NL80211_BSS_STATUS,
5661 NL80211_BSS_STATUS_IBSS_JOINED))
5662 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005663 break;
5664 default:
5665 break;
5666 }
5667
Johannes Berg2a519312009-02-10 21:25:55 +01005668 nla_nest_end(msg, bss);
5669
5670 return genlmsg_end(msg, hdr);
5671
Johannes Berg8cef2c92013-02-05 16:54:31 +01005672 fail_unlock_rcu:
5673 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005674 nla_put_failure:
5675 genlmsg_cancel(msg, hdr);
5676 return -EMSGSIZE;
5677}
5678
Johannes Berg97990a02013-04-19 01:02:55 +02005679static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005680{
Johannes Berg48ab9052009-07-10 18:42:31 +02005681 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005682 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005683 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005684 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005685 int err;
5686
Johannes Berg97990a02013-04-19 01:02:55 +02005687 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005688 if (err)
5689 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005690
Johannes Berg48ab9052009-07-10 18:42:31 +02005691 wdev_lock(wdev);
5692 spin_lock_bh(&rdev->bss_lock);
5693 cfg80211_bss_expire(rdev);
5694
Johannes Berg9720bb32011-06-21 09:45:33 +02005695 cb->seq = rdev->bss_generation;
5696
Johannes Berg48ab9052009-07-10 18:42:31 +02005697 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005698 if (++idx <= start)
5699 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005700 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005701 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005702 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005703 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005704 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005705 }
5706 }
5707
Johannes Berg48ab9052009-07-10 18:42:31 +02005708 spin_unlock_bh(&rdev->bss_lock);
5709 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005710
Johannes Berg97990a02013-04-19 01:02:55 +02005711 cb->args[2] = idx;
5712 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005713
Johannes Berg67748892010-10-04 21:14:06 +02005714 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005715}
5716
Eric W. Biederman15e47302012-09-07 20:12:54 +00005717static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005718 int flags, struct net_device *dev,
5719 struct survey_info *survey)
5720{
5721 void *hdr;
5722 struct nlattr *infoattr;
5723
Eric W. Biederman15e47302012-09-07 20:12:54 +00005724 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005725 NL80211_CMD_NEW_SURVEY_RESULTS);
5726 if (!hdr)
5727 return -ENOMEM;
5728
David S. Miller9360ffd2012-03-29 04:41:26 -04005729 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5730 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005731
5732 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5733 if (!infoattr)
5734 goto nla_put_failure;
5735
David S. Miller9360ffd2012-03-29 04:41:26 -04005736 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5737 survey->channel->center_freq))
5738 goto nla_put_failure;
5739
5740 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5741 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5742 goto nla_put_failure;
5743 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5744 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5745 goto nla_put_failure;
5746 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5747 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5748 survey->channel_time))
5749 goto nla_put_failure;
5750 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5751 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5752 survey->channel_time_busy))
5753 goto nla_put_failure;
5754 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5755 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5756 survey->channel_time_ext_busy))
5757 goto nla_put_failure;
5758 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5759 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5760 survey->channel_time_rx))
5761 goto nla_put_failure;
5762 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5763 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5764 survey->channel_time_tx))
5765 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005766
5767 nla_nest_end(msg, infoattr);
5768
5769 return genlmsg_end(msg, hdr);
5770
5771 nla_put_failure:
5772 genlmsg_cancel(msg, hdr);
5773 return -EMSGSIZE;
5774}
5775
5776static int nl80211_dump_survey(struct sk_buff *skb,
5777 struct netlink_callback *cb)
5778{
5779 struct survey_info survey;
5780 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02005781 struct wireless_dev *wdev;
5782 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01005783 int res;
5784
Johannes Berg97990a02013-04-19 01:02:55 +02005785 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005786 if (res)
5787 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005788
Johannes Berg97990a02013-04-19 01:02:55 +02005789 if (!wdev->netdev) {
5790 res = -EINVAL;
5791 goto out_err;
5792 }
5793
Holger Schurig61fa7132009-11-11 12:25:40 +01005794 if (!dev->ops->dump_survey) {
5795 res = -EOPNOTSUPP;
5796 goto out_err;
5797 }
5798
5799 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005800 struct ieee80211_channel *chan;
5801
Johannes Berg97990a02013-04-19 01:02:55 +02005802 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005803 if (res == -ENOENT)
5804 break;
5805 if (res)
5806 goto out_err;
5807
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005808 /* Survey without a channel doesn't make sense */
5809 if (!survey.channel) {
5810 res = -EINVAL;
5811 goto out;
5812 }
5813
5814 chan = ieee80211_get_channel(&dev->wiphy,
5815 survey.channel->center_freq);
5816 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5817 survey_idx++;
5818 continue;
5819 }
5820
Holger Schurig61fa7132009-11-11 12:25:40 +01005821 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005822 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005823 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02005824 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01005825 goto out;
5826 survey_idx++;
5827 }
5828
5829 out:
Johannes Berg97990a02013-04-19 01:02:55 +02005830 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01005831 res = skb->len;
5832 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02005833 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005834 return res;
5835}
5836
Samuel Ortizb23aa672009-07-01 21:26:54 +02005837static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5838{
5839 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5840 NL80211_WPA_VERSION_2));
5841}
5842
Jouni Malinen636a5d32009-03-19 13:39:22 +02005843static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5844{
Johannes Berg4c476992010-10-04 21:36:35 +02005845 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5846 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005847 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005848 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5849 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005850 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005851 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005852 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005853
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005854 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5855 return -EINVAL;
5856
5857 if (!info->attrs[NL80211_ATTR_MAC])
5858 return -EINVAL;
5859
Jouni Malinen17780922009-03-27 20:52:47 +02005860 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5861 return -EINVAL;
5862
Johannes Berg19957bb2009-07-02 17:20:43 +02005863 if (!info->attrs[NL80211_ATTR_SSID])
5864 return -EINVAL;
5865
5866 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5867 return -EINVAL;
5868
Johannes Bergfffd0932009-07-08 14:22:54 +02005869 err = nl80211_parse_key(info, &key);
5870 if (err)
5871 return err;
5872
5873 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005874 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5875 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005876 if (!key.p.key || !key.p.key_len)
5877 return -EINVAL;
5878 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5879 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5880 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5881 key.p.key_len != WLAN_KEY_LEN_WEP104))
5882 return -EINVAL;
5883 if (key.idx > 4)
5884 return -EINVAL;
5885 } else {
5886 key.p.key_len = 0;
5887 key.p.key = NULL;
5888 }
5889
Johannes Bergafea0b72010-08-10 09:46:42 +02005890 if (key.idx >= 0) {
5891 int i;
5892 bool ok = false;
5893 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5894 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5895 ok = true;
5896 break;
5897 }
5898 }
Johannes Berg4c476992010-10-04 21:36:35 +02005899 if (!ok)
5900 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005901 }
5902
Johannes Berg4c476992010-10-04 21:36:35 +02005903 if (!rdev->ops->auth)
5904 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005905
Johannes Berg074ac8d2010-09-16 14:58:22 +02005906 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005907 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5908 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005909
Johannes Berg19957bb2009-07-02 17:20:43 +02005910 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005911 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005912 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005913 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5914 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005915
Johannes Berg19957bb2009-07-02 17:20:43 +02005916 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5917 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5918
5919 if (info->attrs[NL80211_ATTR_IE]) {
5920 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5921 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5922 }
5923
5924 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005925 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005926 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005927
Jouni Malinene39e5b52012-09-30 19:29:39 +03005928 if (auth_type == NL80211_AUTHTYPE_SAE &&
5929 !info->attrs[NL80211_ATTR_SAE_DATA])
5930 return -EINVAL;
5931
5932 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5933 if (auth_type != NL80211_AUTHTYPE_SAE)
5934 return -EINVAL;
5935 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5936 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5937 /* need to include at least Auth Transaction and Status Code */
5938 if (sae_data_len < 4)
5939 return -EINVAL;
5940 }
5941
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005942 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5943
Johannes Berg95de8172012-01-20 13:55:25 +01005944 /*
5945 * Since we no longer track auth state, ignore
5946 * requests to only change local state.
5947 */
5948 if (local_state_change)
5949 return 0;
5950
Johannes Berg4c476992010-10-04 21:36:35 +02005951 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5952 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005953 key.p.key, key.p.key_len, key.idx,
5954 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005955}
5956
Johannes Bergc0692b82010-08-27 14:26:53 +03005957static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5958 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005959 struct cfg80211_crypto_settings *settings,
5960 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005961{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005962 memset(settings, 0, sizeof(*settings));
5963
Samuel Ortizb23aa672009-07-01 21:26:54 +02005964 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5965
Johannes Bergc0692b82010-08-27 14:26:53 +03005966 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5967 u16 proto;
5968 proto = nla_get_u16(
5969 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5970 settings->control_port_ethertype = cpu_to_be16(proto);
5971 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5972 proto != ETH_P_PAE)
5973 return -EINVAL;
5974 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5975 settings->control_port_no_encrypt = true;
5976 } else
5977 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5978
Samuel Ortizb23aa672009-07-01 21:26:54 +02005979 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5980 void *data;
5981 int len, i;
5982
5983 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5984 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5985 settings->n_ciphers_pairwise = len / sizeof(u32);
5986
5987 if (len % sizeof(u32))
5988 return -EINVAL;
5989
Johannes Berg3dc27d22009-07-02 21:36:37 +02005990 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005991 return -EINVAL;
5992
5993 memcpy(settings->ciphers_pairwise, data, len);
5994
5995 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005996 if (!cfg80211_supported_cipher_suite(
5997 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005998 settings->ciphers_pairwise[i]))
5999 return -EINVAL;
6000 }
6001
6002 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6003 settings->cipher_group =
6004 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006005 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6006 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006007 return -EINVAL;
6008 }
6009
6010 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6011 settings->wpa_versions =
6012 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6013 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6014 return -EINVAL;
6015 }
6016
6017 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6018 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006019 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006020
6021 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6022 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6023 settings->n_akm_suites = len / sizeof(u32);
6024
6025 if (len % sizeof(u32))
6026 return -EINVAL;
6027
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006028 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6029 return -EINVAL;
6030
Samuel Ortizb23aa672009-07-01 21:26:54 +02006031 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006032 }
6033
6034 return 0;
6035}
6036
Jouni Malinen636a5d32009-03-19 13:39:22 +02006037static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6038{
Johannes Berg4c476992010-10-04 21:36:35 +02006039 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6040 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006041 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006042 struct cfg80211_assoc_request req = {};
6043 const u8 *bssid, *ssid;
6044 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006045
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006046 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6047 return -EINVAL;
6048
6049 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006050 !info->attrs[NL80211_ATTR_SSID] ||
6051 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006052 return -EINVAL;
6053
Johannes Berg4c476992010-10-04 21:36:35 +02006054 if (!rdev->ops->assoc)
6055 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006056
Johannes Berg074ac8d2010-09-16 14:58:22 +02006057 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006058 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6059 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006060
Johannes Berg19957bb2009-07-02 17:20:43 +02006061 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006062
Johannes Berg19957bb2009-07-02 17:20:43 +02006063 chan = ieee80211_get_channel(&rdev->wiphy,
6064 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006065 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6066 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006067
Johannes Berg19957bb2009-07-02 17:20:43 +02006068 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6069 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006070
6071 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006072 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6073 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006074 }
6075
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006076 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006077 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006078 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006079 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006080 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006081 else if (mfp != NL80211_MFP_NO)
6082 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006083 }
6084
Johannes Berg3e5d7642009-07-07 14:37:26 +02006085 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006086 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006087
Ben Greear7e7c8922011-11-18 11:31:59 -08006088 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006089 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006090
6091 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006092 memcpy(&req.ht_capa_mask,
6093 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6094 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006095
6096 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006097 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006098 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006099 memcpy(&req.ht_capa,
6100 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6101 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006102 }
6103
Johannes Bergee2aca32013-02-21 17:36:01 +01006104 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006105 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006106
6107 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006108 memcpy(&req.vht_capa_mask,
6109 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6110 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006111
6112 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006113 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006114 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006115 memcpy(&req.vht_capa,
6116 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6117 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006118 }
6119
Johannes Bergf62fab72013-02-21 20:09:09 +01006120 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006121 if (!err)
Johannes Bergf62fab72013-02-21 20:09:09 +01006122 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6123 ssid, ssid_len, &req);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006124
Jouni Malinen636a5d32009-03-19 13:39:22 +02006125 return err;
6126}
6127
6128static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6129{
Johannes Berg4c476992010-10-04 21:36:35 +02006130 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6131 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006132 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006133 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006134 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006135 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006136
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006137 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6138 return -EINVAL;
6139
6140 if (!info->attrs[NL80211_ATTR_MAC])
6141 return -EINVAL;
6142
6143 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6144 return -EINVAL;
6145
Johannes Berg4c476992010-10-04 21:36:35 +02006146 if (!rdev->ops->deauth)
6147 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006148
Johannes Berg074ac8d2010-09-16 14:58:22 +02006149 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006150 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6151 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006152
Johannes Berg19957bb2009-07-02 17:20:43 +02006153 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006154
Johannes Berg19957bb2009-07-02 17:20:43 +02006155 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6156 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006157 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006158 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006159 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006160
6161 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006162 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6163 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006164 }
6165
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006166 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6167
Johannes Berg4c476992010-10-04 21:36:35 +02006168 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6169 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006170}
6171
6172static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6173{
Johannes Berg4c476992010-10-04 21:36:35 +02006174 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6175 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006176 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006177 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006178 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006179 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006180
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006181 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6182 return -EINVAL;
6183
6184 if (!info->attrs[NL80211_ATTR_MAC])
6185 return -EINVAL;
6186
6187 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6188 return -EINVAL;
6189
Johannes Berg4c476992010-10-04 21:36:35 +02006190 if (!rdev->ops->disassoc)
6191 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006192
Johannes Berg074ac8d2010-09-16 14:58:22 +02006193 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006194 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6195 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006196
Johannes Berg19957bb2009-07-02 17:20:43 +02006197 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006198
Johannes Berg19957bb2009-07-02 17:20:43 +02006199 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6200 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006201 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006202 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006203 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006204
6205 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006206 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6207 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006208 }
6209
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006210 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6211
Johannes Berg4c476992010-10-04 21:36:35 +02006212 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6213 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006214}
6215
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006216static bool
6217nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6218 int mcast_rate[IEEE80211_NUM_BANDS],
6219 int rateval)
6220{
6221 struct wiphy *wiphy = &rdev->wiphy;
6222 bool found = false;
6223 int band, i;
6224
6225 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6226 struct ieee80211_supported_band *sband;
6227
6228 sband = wiphy->bands[band];
6229 if (!sband)
6230 continue;
6231
6232 for (i = 0; i < sband->n_bitrates; i++) {
6233 if (sband->bitrates[i].bitrate == rateval) {
6234 mcast_rate[band] = i + 1;
6235 found = true;
6236 break;
6237 }
6238 }
6239 }
6240
6241 return found;
6242}
6243
Johannes Berg04a773a2009-04-19 21:24:32 +02006244static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6245{
Johannes Berg4c476992010-10-04 21:36:35 +02006246 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6247 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006248 struct cfg80211_ibss_params ibss;
6249 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006250 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006251 int err;
6252
Johannes Berg8e30bc52009-04-22 17:45:38 +02006253 memset(&ibss, 0, sizeof(ibss));
6254
Johannes Berg04a773a2009-04-19 21:24:32 +02006255 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6256 return -EINVAL;
6257
Johannes Berg683b6d32012-11-08 21:25:48 +01006258 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006259 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6260 return -EINVAL;
6261
Johannes Berg8e30bc52009-04-22 17:45:38 +02006262 ibss.beacon_interval = 100;
6263
6264 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6265 ibss.beacon_interval =
6266 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6267 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6268 return -EINVAL;
6269 }
6270
Johannes Berg4c476992010-10-04 21:36:35 +02006271 if (!rdev->ops->join_ibss)
6272 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006273
Johannes Berg4c476992010-10-04 21:36:35 +02006274 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6275 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006276
Johannes Berg79c97e92009-07-07 03:56:12 +02006277 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006278
Johannes Berg39193492011-09-16 13:45:25 +02006279 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006280 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006281
6282 if (!is_valid_ether_addr(ibss.bssid))
6283 return -EINVAL;
6284 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006285 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6286 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6287
6288 if (info->attrs[NL80211_ATTR_IE]) {
6289 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6290 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6291 }
6292
Johannes Berg683b6d32012-11-08 21:25:48 +01006293 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6294 if (err)
6295 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006296
Johannes Berg683b6d32012-11-08 21:25:48 +01006297 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006298 return -EINVAL;
6299
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006300 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6301 return -EINVAL;
6302 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6303 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01006304 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006305
Johannes Berg04a773a2009-04-19 21:24:32 +02006306 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006307 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006308
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006309 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6310 u8 *rates =
6311 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6312 int n_rates =
6313 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6314 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006315 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006316
Johannes Berg34850ab2011-07-18 18:08:35 +02006317 err = ieee80211_get_ratemask(sband, rates, n_rates,
6318 &ibss.basic_rates);
6319 if (err)
6320 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006321 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006322
6323 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6324 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6325 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6326 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006327
Johannes Berg4c476992010-10-04 21:36:35 +02006328 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306329 bool no_ht = false;
6330
Johannes Berg4c476992010-10-04 21:36:35 +02006331 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306332 info->attrs[NL80211_ATTR_KEYS],
6333 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006334 if (IS_ERR(connkeys))
6335 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306336
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006337 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6338 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306339 kfree(connkeys);
6340 return -EINVAL;
6341 }
Johannes Berg4c476992010-10-04 21:36:35 +02006342 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006343
Antonio Quartulli267335d2012-01-31 20:25:47 +01006344 ibss.control_port =
6345 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6346
Johannes Berg4c476992010-10-04 21:36:35 +02006347 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006348 if (err)
6349 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006350 return err;
6351}
6352
6353static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6354{
Johannes Berg4c476992010-10-04 21:36:35 +02006355 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6356 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006357
Johannes Berg4c476992010-10-04 21:36:35 +02006358 if (!rdev->ops->leave_ibss)
6359 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006360
Johannes Berg4c476992010-10-04 21:36:35 +02006361 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6362 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006363
Johannes Berg4c476992010-10-04 21:36:35 +02006364 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006365}
6366
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006367static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6368{
6369 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6370 struct net_device *dev = info->user_ptr[1];
6371 int mcast_rate[IEEE80211_NUM_BANDS];
6372 u32 nla_rate;
6373 int err;
6374
6375 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6376 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6377 return -EOPNOTSUPP;
6378
6379 if (!rdev->ops->set_mcast_rate)
6380 return -EOPNOTSUPP;
6381
6382 memset(mcast_rate, 0, sizeof(mcast_rate));
6383
6384 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6385 return -EINVAL;
6386
6387 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6388 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6389 return -EINVAL;
6390
6391 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6392
6393 return err;
6394}
6395
6396
Johannes Bergaff89a92009-07-01 21:26:51 +02006397#ifdef CONFIG_NL80211_TESTMODE
6398static struct genl_multicast_group nl80211_testmode_mcgrp = {
6399 .name = "testmode",
6400};
6401
6402static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6403{
Johannes Berg4c476992010-10-04 21:36:35 +02006404 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006405 int err;
6406
6407 if (!info->attrs[NL80211_ATTR_TESTDATA])
6408 return -EINVAL;
6409
Johannes Bergaff89a92009-07-01 21:26:51 +02006410 err = -EOPNOTSUPP;
6411 if (rdev->ops->testmode_cmd) {
6412 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006413 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006414 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6415 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6416 rdev->testmode_info = NULL;
6417 }
6418
Johannes Bergaff89a92009-07-01 21:26:51 +02006419 return err;
6420}
6421
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006422static int nl80211_testmode_dump(struct sk_buff *skb,
6423 struct netlink_callback *cb)
6424{
Johannes Berg00918d32011-12-13 17:22:05 +01006425 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006426 int err;
6427 long phy_idx;
6428 void *data = NULL;
6429 int data_len = 0;
6430
6431 if (cb->args[0]) {
6432 /*
6433 * 0 is a valid index, but not valid for args[0],
6434 * so we need to offset by 1.
6435 */
6436 phy_idx = cb->args[0] - 1;
6437 } else {
6438 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6439 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6440 nl80211_policy);
6441 if (err)
6442 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006443
Johannes Berg2bd7e352012-06-15 14:23:16 +02006444 mutex_lock(&cfg80211_mutex);
6445 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6446 nl80211_fam.attrbuf);
6447 if (IS_ERR(rdev)) {
6448 mutex_unlock(&cfg80211_mutex);
6449 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006450 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006451 phy_idx = rdev->wiphy_idx;
6452 rdev = NULL;
6453 mutex_unlock(&cfg80211_mutex);
6454
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006455 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6456 cb->args[1] =
6457 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6458 }
6459
6460 if (cb->args[1]) {
6461 data = nla_data((void *)cb->args[1]);
6462 data_len = nla_len((void *)cb->args[1]);
6463 }
6464
6465 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006466 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6467 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006468 mutex_unlock(&cfg80211_mutex);
6469 return -ENOENT;
6470 }
Johannes Berg00918d32011-12-13 17:22:05 +01006471 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006472 mutex_unlock(&cfg80211_mutex);
6473
Johannes Berg00918d32011-12-13 17:22:05 +01006474 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006475 err = -EOPNOTSUPP;
6476 goto out_err;
6477 }
6478
6479 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006480 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006481 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6482 NL80211_CMD_TESTMODE);
6483 struct nlattr *tmdata;
6484
David S. Miller9360ffd2012-03-29 04:41:26 -04006485 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006486 genlmsg_cancel(skb, hdr);
6487 break;
6488 }
6489
6490 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6491 if (!tmdata) {
6492 genlmsg_cancel(skb, hdr);
6493 break;
6494 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006495 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006496 nla_nest_end(skb, tmdata);
6497
6498 if (err == -ENOBUFS || err == -ENOENT) {
6499 genlmsg_cancel(skb, hdr);
6500 break;
6501 } else if (err) {
6502 genlmsg_cancel(skb, hdr);
6503 goto out_err;
6504 }
6505
6506 genlmsg_end(skb, hdr);
6507 }
6508
6509 err = skb->len;
6510 /* see above */
6511 cb->args[0] = phy_idx + 1;
6512 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006513 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006514 return err;
6515}
6516
Johannes Bergaff89a92009-07-01 21:26:51 +02006517static struct sk_buff *
6518__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006519 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006520{
6521 struct sk_buff *skb;
6522 void *hdr;
6523 struct nlattr *data;
6524
6525 skb = nlmsg_new(approxlen + 100, gfp);
6526 if (!skb)
6527 return NULL;
6528
Eric W. Biederman15e47302012-09-07 20:12:54 +00006529 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006530 if (!hdr) {
6531 kfree_skb(skb);
6532 return NULL;
6533 }
6534
David S. Miller9360ffd2012-03-29 04:41:26 -04006535 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6536 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006537 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6538
6539 ((void **)skb->cb)[0] = rdev;
6540 ((void **)skb->cb)[1] = hdr;
6541 ((void **)skb->cb)[2] = data;
6542
6543 return skb;
6544
6545 nla_put_failure:
6546 kfree_skb(skb);
6547 return NULL;
6548}
6549
6550struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6551 int approxlen)
6552{
6553 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6554
6555 if (WARN_ON(!rdev->testmode_info))
6556 return NULL;
6557
6558 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006559 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006560 rdev->testmode_info->snd_seq,
6561 GFP_KERNEL);
6562}
6563EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6564
6565int cfg80211_testmode_reply(struct sk_buff *skb)
6566{
6567 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6568 void *hdr = ((void **)skb->cb)[1];
6569 struct nlattr *data = ((void **)skb->cb)[2];
6570
6571 if (WARN_ON(!rdev->testmode_info)) {
6572 kfree_skb(skb);
6573 return -EINVAL;
6574 }
6575
6576 nla_nest_end(skb, data);
6577 genlmsg_end(skb, hdr);
6578 return genlmsg_reply(skb, rdev->testmode_info);
6579}
6580EXPORT_SYMBOL(cfg80211_testmode_reply);
6581
6582struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6583 int approxlen, gfp_t gfp)
6584{
6585 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6586
6587 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6588}
6589EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6590
6591void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6592{
Michal Kaziorc8942c62013-06-25 09:17:17 +02006593 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006594 void *hdr = ((void **)skb->cb)[1];
6595 struct nlattr *data = ((void **)skb->cb)[2];
6596
6597 nla_nest_end(skb, data);
6598 genlmsg_end(skb, hdr);
Michal Kaziorc8942c62013-06-25 09:17:17 +02006599 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), skb, 0,
6600 nl80211_testmode_mcgrp.id, gfp);
Johannes Bergaff89a92009-07-01 21:26:51 +02006601}
6602EXPORT_SYMBOL(cfg80211_testmode_event);
6603#endif
6604
Samuel Ortizb23aa672009-07-01 21:26:54 +02006605static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6606{
Johannes Berg4c476992010-10-04 21:36:35 +02006607 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6608 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006609 struct cfg80211_connect_params connect;
6610 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006611 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006612 int err;
6613
6614 memset(&connect, 0, sizeof(connect));
6615
6616 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6617 return -EINVAL;
6618
6619 if (!info->attrs[NL80211_ATTR_SSID] ||
6620 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6621 return -EINVAL;
6622
6623 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6624 connect.auth_type =
6625 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006626 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6627 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006628 return -EINVAL;
6629 } else
6630 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6631
6632 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6633
Johannes Bergc0692b82010-08-27 14:26:53 +03006634 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006635 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006636 if (err)
6637 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006638
Johannes Berg074ac8d2010-09-16 14:58:22 +02006639 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006640 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6641 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006642
Johannes Berg79c97e92009-07-07 03:56:12 +02006643 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006644
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306645 connect.bg_scan_period = -1;
6646 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6647 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6648 connect.bg_scan_period =
6649 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6650 }
6651
Samuel Ortizb23aa672009-07-01 21:26:54 +02006652 if (info->attrs[NL80211_ATTR_MAC])
6653 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6654 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6655 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6656
6657 if (info->attrs[NL80211_ATTR_IE]) {
6658 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6659 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6660 }
6661
Jouni Malinencee00a92013-01-15 17:15:57 +02006662 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6663 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6664 if (connect.mfp != NL80211_MFP_REQUIRED &&
6665 connect.mfp != NL80211_MFP_NO)
6666 return -EINVAL;
6667 } else {
6668 connect.mfp = NL80211_MFP_NO;
6669 }
6670
Samuel Ortizb23aa672009-07-01 21:26:54 +02006671 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6672 connect.channel =
6673 ieee80211_get_channel(wiphy,
6674 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6675 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006676 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6677 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006678 }
6679
Johannes Bergfffd0932009-07-08 14:22:54 +02006680 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6681 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306682 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006683 if (IS_ERR(connkeys))
6684 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006685 }
6686
Ben Greear7e7c8922011-11-18 11:31:59 -08006687 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6688 connect.flags |= ASSOC_REQ_DISABLE_HT;
6689
6690 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6691 memcpy(&connect.ht_capa_mask,
6692 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6693 sizeof(connect.ht_capa_mask));
6694
6695 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006696 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6697 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006698 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006699 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006700 memcpy(&connect.ht_capa,
6701 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6702 sizeof(connect.ht_capa));
6703 }
6704
Johannes Bergee2aca32013-02-21 17:36:01 +01006705 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6706 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6707
6708 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6709 memcpy(&connect.vht_capa_mask,
6710 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6711 sizeof(connect.vht_capa_mask));
6712
6713 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6714 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
6715 kfree(connkeys);
6716 return -EINVAL;
6717 }
6718 memcpy(&connect.vht_capa,
6719 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6720 sizeof(connect.vht_capa));
6721 }
6722
Johannes Bergfffd0932009-07-08 14:22:54 +02006723 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006724 if (err)
6725 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006726 return err;
6727}
6728
6729static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6730{
Johannes Berg4c476992010-10-04 21:36:35 +02006731 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6732 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006733 u16 reason;
6734
6735 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6736 reason = WLAN_REASON_DEAUTH_LEAVING;
6737 else
6738 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6739
6740 if (reason == 0)
6741 return -EINVAL;
6742
Johannes Berg074ac8d2010-09-16 14:58:22 +02006743 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006744 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6745 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006746
Johannes Berg4c476992010-10-04 21:36:35 +02006747 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006748}
6749
Johannes Berg463d0182009-07-14 00:33:35 +02006750static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6751{
Johannes Berg4c476992010-10-04 21:36:35 +02006752 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006753 struct net *net;
6754 int err;
6755 u32 pid;
6756
6757 if (!info->attrs[NL80211_ATTR_PID])
6758 return -EINVAL;
6759
6760 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6761
Johannes Berg463d0182009-07-14 00:33:35 +02006762 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006763 if (IS_ERR(net))
6764 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006765
6766 err = 0;
6767
6768 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006769 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6770 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006771
Johannes Berg463d0182009-07-14 00:33:35 +02006772 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006773 return err;
6774}
6775
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006776static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6777{
Johannes Berg4c476992010-10-04 21:36:35 +02006778 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006779 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6780 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006781 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006782 struct cfg80211_pmksa pmksa;
6783
6784 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6785
6786 if (!info->attrs[NL80211_ATTR_MAC])
6787 return -EINVAL;
6788
6789 if (!info->attrs[NL80211_ATTR_PMKID])
6790 return -EINVAL;
6791
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006792 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6793 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6794
Johannes Berg074ac8d2010-09-16 14:58:22 +02006795 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006796 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6797 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006798
6799 switch (info->genlhdr->cmd) {
6800 case NL80211_CMD_SET_PMKSA:
6801 rdev_ops = rdev->ops->set_pmksa;
6802 break;
6803 case NL80211_CMD_DEL_PMKSA:
6804 rdev_ops = rdev->ops->del_pmksa;
6805 break;
6806 default:
6807 WARN_ON(1);
6808 break;
6809 }
6810
Johannes Berg4c476992010-10-04 21:36:35 +02006811 if (!rdev_ops)
6812 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006813
Johannes Berg4c476992010-10-04 21:36:35 +02006814 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006815}
6816
6817static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6818{
Johannes Berg4c476992010-10-04 21:36:35 +02006819 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6820 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006821
Johannes Berg074ac8d2010-09-16 14:58:22 +02006822 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006823 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6824 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006825
Johannes Berg4c476992010-10-04 21:36:35 +02006826 if (!rdev->ops->flush_pmksa)
6827 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006828
Hila Gonene35e4d22012-06-27 17:19:42 +03006829 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006830}
6831
Arik Nemtsov109086c2011-09-28 14:12:50 +03006832static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6833{
6834 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6835 struct net_device *dev = info->user_ptr[1];
6836 u8 action_code, dialog_token;
6837 u16 status_code;
6838 u8 *peer;
6839
6840 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6841 !rdev->ops->tdls_mgmt)
6842 return -EOPNOTSUPP;
6843
6844 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6845 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6846 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6847 !info->attrs[NL80211_ATTR_IE] ||
6848 !info->attrs[NL80211_ATTR_MAC])
6849 return -EINVAL;
6850
6851 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6852 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6853 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6854 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6855
Hila Gonene35e4d22012-06-27 17:19:42 +03006856 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6857 dialog_token, status_code,
6858 nla_data(info->attrs[NL80211_ATTR_IE]),
6859 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006860}
6861
6862static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6863{
6864 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6865 struct net_device *dev = info->user_ptr[1];
6866 enum nl80211_tdls_operation operation;
6867 u8 *peer;
6868
6869 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6870 !rdev->ops->tdls_oper)
6871 return -EOPNOTSUPP;
6872
6873 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6874 !info->attrs[NL80211_ATTR_MAC])
6875 return -EINVAL;
6876
6877 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6878 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6879
Hila Gonene35e4d22012-06-27 17:19:42 +03006880 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006881}
6882
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006883static int nl80211_remain_on_channel(struct sk_buff *skb,
6884 struct genl_info *info)
6885{
Johannes Berg4c476992010-10-04 21:36:35 +02006886 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006887 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006888 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006889 struct sk_buff *msg;
6890 void *hdr;
6891 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006892 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006893 int err;
6894
6895 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6896 !info->attrs[NL80211_ATTR_DURATION])
6897 return -EINVAL;
6898
6899 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6900
Johannes Berg7c4ef712011-11-18 15:33:48 +01006901 if (!rdev->ops->remain_on_channel ||
6902 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006903 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006904
Johannes Bergebf348f2012-06-01 12:50:54 +02006905 /*
6906 * We should be on that channel for at least a minimum amount of
6907 * time (10ms) but no longer than the driver supports.
6908 */
6909 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6910 duration > rdev->wiphy.max_remain_on_channel_duration)
6911 return -EINVAL;
6912
Johannes Berg683b6d32012-11-08 21:25:48 +01006913 err = nl80211_parse_chandef(rdev, info, &chandef);
6914 if (err)
6915 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006916
6917 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006918 if (!msg)
6919 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006920
Eric W. Biederman15e47302012-09-07 20:12:54 +00006921 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006922 NL80211_CMD_REMAIN_ON_CHANNEL);
6923
6924 if (IS_ERR(hdr)) {
6925 err = PTR_ERR(hdr);
6926 goto free_msg;
6927 }
6928
Johannes Berg683b6d32012-11-08 21:25:48 +01006929 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6930 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006931
6932 if (err)
6933 goto free_msg;
6934
David S. Miller9360ffd2012-03-29 04:41:26 -04006935 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6936 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006937
6938 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006939
6940 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006941
6942 nla_put_failure:
6943 err = -ENOBUFS;
6944 free_msg:
6945 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006946 return err;
6947}
6948
6949static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6950 struct genl_info *info)
6951{
Johannes Berg4c476992010-10-04 21:36:35 +02006952 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006953 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006954 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006955
6956 if (!info->attrs[NL80211_ATTR_COOKIE])
6957 return -EINVAL;
6958
Johannes Berg4c476992010-10-04 21:36:35 +02006959 if (!rdev->ops->cancel_remain_on_channel)
6960 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006961
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006962 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6963
Hila Gonene35e4d22012-06-27 17:19:42 +03006964 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006965}
6966
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006967static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6968 u8 *rates, u8 rates_len)
6969{
6970 u8 i;
6971 u32 mask = 0;
6972
6973 for (i = 0; i < rates_len; i++) {
6974 int rate = (rates[i] & 0x7f) * 5;
6975 int ridx;
6976 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6977 struct ieee80211_rate *srate =
6978 &sband->bitrates[ridx];
6979 if (rate == srate->bitrate) {
6980 mask |= 1 << ridx;
6981 break;
6982 }
6983 }
6984 if (ridx == sband->n_bitrates)
6985 return 0; /* rate not found */
6986 }
6987
6988 return mask;
6989}
6990
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006991static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6992 u8 *rates, u8 rates_len,
6993 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6994{
6995 u8 i;
6996
6997 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6998
6999 for (i = 0; i < rates_len; i++) {
7000 int ridx, rbit;
7001
7002 ridx = rates[i] / 8;
7003 rbit = BIT(rates[i] % 8);
7004
7005 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007006 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007007 return false;
7008
7009 /* check availability */
7010 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7011 mcs[ridx] |= rbit;
7012 else
7013 return false;
7014 }
7015
7016 return true;
7017}
7018
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007019static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007020 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7021 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007022 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
7023 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007024};
7025
7026static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7027 struct genl_info *info)
7028{
7029 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007030 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007031 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007032 int rem, i;
7033 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007034 struct nlattr *tx_rates;
7035 struct ieee80211_supported_band *sband;
7036
7037 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7038 return -EINVAL;
7039
Johannes Berg4c476992010-10-04 21:36:35 +02007040 if (!rdev->ops->set_bitrate_mask)
7041 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007042
7043 memset(&mask, 0, sizeof(mask));
7044 /* Default to all rates enabled */
7045 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7046 sband = rdev->wiphy.bands[i];
7047 mask.control[i].legacy =
7048 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007049 if (sband)
7050 memcpy(mask.control[i].mcs,
7051 sband->ht_cap.mcs.rx_mask,
7052 sizeof(mask.control[i].mcs));
7053 else
7054 memset(mask.control[i].mcs, 0,
7055 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007056 }
7057
7058 /*
7059 * The nested attribute uses enum nl80211_band as the index. This maps
7060 * directly to the enum ieee80211_band values used in cfg80211.
7061 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007062 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007063 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7064 {
7065 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007066 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7067 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007068 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007069 if (sband == NULL)
7070 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007071 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7072 nla_len(tx_rates), nl80211_txattr_policy);
7073 if (tb[NL80211_TXRATE_LEGACY]) {
7074 mask.control[band].legacy = rateset_to_mask(
7075 sband,
7076 nla_data(tb[NL80211_TXRATE_LEGACY]),
7077 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307078 if ((mask.control[band].legacy == 0) &&
7079 nla_len(tb[NL80211_TXRATE_LEGACY]))
7080 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007081 }
7082 if (tb[NL80211_TXRATE_MCS]) {
7083 if (!ht_rateset_to_mask(
7084 sband,
7085 nla_data(tb[NL80211_TXRATE_MCS]),
7086 nla_len(tb[NL80211_TXRATE_MCS]),
7087 mask.control[band].mcs))
7088 return -EINVAL;
7089 }
7090
7091 if (mask.control[band].legacy == 0) {
7092 /* don't allow empty legacy rates if HT
7093 * is not even supported. */
7094 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7095 return -EINVAL;
7096
7097 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7098 if (mask.control[band].mcs[i])
7099 break;
7100
7101 /* legacy and mcs rates may not be both empty */
7102 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02007103 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007104 }
7105 }
7106
Hila Gonene35e4d22012-06-27 17:19:42 +03007107 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007108}
7109
Johannes Berg2e161f72010-08-12 15:38:38 +02007110static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007111{
Johannes Berg4c476992010-10-04 21:36:35 +02007112 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007113 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007114 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007115
7116 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7117 return -EINVAL;
7118
Johannes Berg2e161f72010-08-12 15:38:38 +02007119 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7120 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007121
Johannes Berg71bbc992012-06-15 15:30:18 +02007122 switch (wdev->iftype) {
7123 case NL80211_IFTYPE_STATION:
7124 case NL80211_IFTYPE_ADHOC:
7125 case NL80211_IFTYPE_P2P_CLIENT:
7126 case NL80211_IFTYPE_AP:
7127 case NL80211_IFTYPE_AP_VLAN:
7128 case NL80211_IFTYPE_MESH_POINT:
7129 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007130 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007131 break;
7132 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007133 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007134 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007135
7136 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007137 if (!rdev->ops->mgmt_tx)
7138 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007139
Eric W. Biederman15e47302012-09-07 20:12:54 +00007140 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007141 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7142 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007143}
7144
Johannes Berg2e161f72010-08-12 15:38:38 +02007145static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007146{
Johannes Berg4c476992010-10-04 21:36:35 +02007147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007148 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007149 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007150 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007151 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007152 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007153 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007154 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01007155 bool offchan, no_cck, dont_wait_for_ack;
7156
7157 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02007158
Johannes Berg683b6d32012-11-08 21:25:48 +01007159 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007160 return -EINVAL;
7161
Johannes Berg4c476992010-10-04 21:36:35 +02007162 if (!rdev->ops->mgmt_tx)
7163 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007164
Johannes Berg71bbc992012-06-15 15:30:18 +02007165 switch (wdev->iftype) {
7166 case NL80211_IFTYPE_STATION:
7167 case NL80211_IFTYPE_ADHOC:
7168 case NL80211_IFTYPE_P2P_CLIENT:
7169 case NL80211_IFTYPE_AP:
7170 case NL80211_IFTYPE_AP_VLAN:
7171 case NL80211_IFTYPE_MESH_POINT:
7172 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007173 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007174 break;
7175 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007176 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007177 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007178
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007179 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007180 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007181 return -EINVAL;
7182 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007183
7184 /*
7185 * We should wait on the channel for at least a minimum amount
7186 * of time (10ms) but no longer than the driver supports.
7187 */
7188 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7189 wait > rdev->wiphy.max_remain_on_channel_duration)
7190 return -EINVAL;
7191
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007192 }
7193
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007194 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7195
Johannes Berg7c4ef712011-11-18 15:33:48 +01007196 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7197 return -EINVAL;
7198
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307199 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7200
Johannes Berg683b6d32012-11-08 21:25:48 +01007201 err = nl80211_parse_chandef(rdev, info, &chandef);
7202 if (err)
7203 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02007204
Johannes Berge247bd902011-11-04 11:18:21 +01007205 if (!dont_wait_for_ack) {
7206 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7207 if (!msg)
7208 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007209
Eric W. Biederman15e47302012-09-07 20:12:54 +00007210 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007211 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007212
Johannes Berge247bd902011-11-04 11:18:21 +01007213 if (IS_ERR(hdr)) {
7214 err = PTR_ERR(hdr);
7215 goto free_msg;
7216 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007217 }
Johannes Berge247bd902011-11-04 11:18:21 +01007218
Johannes Berg683b6d32012-11-08 21:25:48 +01007219 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02007220 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7221 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01007222 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007223 if (err)
7224 goto free_msg;
7225
Johannes Berge247bd902011-11-04 11:18:21 +01007226 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007227 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7228 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007229
Johannes Berge247bd902011-11-04 11:18:21 +01007230 genlmsg_end(msg, hdr);
7231 return genlmsg_reply(msg, info);
7232 }
7233
7234 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007235
7236 nla_put_failure:
7237 err = -ENOBUFS;
7238 free_msg:
7239 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007240 return err;
7241}
7242
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007243static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7244{
7245 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007246 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007247 u64 cookie;
7248
7249 if (!info->attrs[NL80211_ATTR_COOKIE])
7250 return -EINVAL;
7251
7252 if (!rdev->ops->mgmt_tx_cancel_wait)
7253 return -EOPNOTSUPP;
7254
Johannes Berg71bbc992012-06-15 15:30:18 +02007255 switch (wdev->iftype) {
7256 case NL80211_IFTYPE_STATION:
7257 case NL80211_IFTYPE_ADHOC:
7258 case NL80211_IFTYPE_P2P_CLIENT:
7259 case NL80211_IFTYPE_AP:
7260 case NL80211_IFTYPE_AP_VLAN:
7261 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007262 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007263 break;
7264 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007265 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007266 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007267
7268 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7269
Hila Gonene35e4d22012-06-27 17:19:42 +03007270 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007271}
7272
Kalle Valoffb9eb32010-02-17 17:58:10 +02007273static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7274{
Johannes Berg4c476992010-10-04 21:36:35 +02007275 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007276 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007277 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007278 u8 ps_state;
7279 bool state;
7280 int err;
7281
Johannes Berg4c476992010-10-04 21:36:35 +02007282 if (!info->attrs[NL80211_ATTR_PS_STATE])
7283 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007284
7285 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7286
Johannes Berg4c476992010-10-04 21:36:35 +02007287 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7288 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007289
7290 wdev = dev->ieee80211_ptr;
7291
Johannes Berg4c476992010-10-04 21:36:35 +02007292 if (!rdev->ops->set_power_mgmt)
7293 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007294
7295 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7296
7297 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007298 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007299
Hila Gonene35e4d22012-06-27 17:19:42 +03007300 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007301 if (!err)
7302 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007303 return err;
7304}
7305
7306static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7307{
Johannes Berg4c476992010-10-04 21:36:35 +02007308 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007309 enum nl80211_ps_state ps_state;
7310 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007311 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007312 struct sk_buff *msg;
7313 void *hdr;
7314 int err;
7315
Kalle Valoffb9eb32010-02-17 17:58:10 +02007316 wdev = dev->ieee80211_ptr;
7317
Johannes Berg4c476992010-10-04 21:36:35 +02007318 if (!rdev->ops->set_power_mgmt)
7319 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007320
7321 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007322 if (!msg)
7323 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007324
Eric W. Biederman15e47302012-09-07 20:12:54 +00007325 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007326 NL80211_CMD_GET_POWER_SAVE);
7327 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007328 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007329 goto free_msg;
7330 }
7331
7332 if (wdev->ps)
7333 ps_state = NL80211_PS_ENABLED;
7334 else
7335 ps_state = NL80211_PS_DISABLED;
7336
David S. Miller9360ffd2012-03-29 04:41:26 -04007337 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7338 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007339
7340 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007341 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007342
Johannes Berg4c476992010-10-04 21:36:35 +02007343 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007344 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007345 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007346 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007347 return err;
7348}
7349
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007350static struct nla_policy
7351nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7352 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7353 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7354 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007355 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7356 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7357 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007358};
7359
Thomas Pedersen84f10702012-07-12 16:17:33 -07007360static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007361 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007362{
7363 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7364 struct wireless_dev *wdev;
7365 struct net_device *dev = info->user_ptr[1];
7366
Johannes Bergd9d8b012012-11-26 12:51:52 +01007367 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007368 return -EINVAL;
7369
7370 wdev = dev->ieee80211_ptr;
7371
7372 if (!rdev->ops->set_cqm_txe_config)
7373 return -EOPNOTSUPP;
7374
7375 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7376 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7377 return -EOPNOTSUPP;
7378
Hila Gonene35e4d22012-06-27 17:19:42 +03007379 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007380}
7381
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007382static int nl80211_set_cqm_rssi(struct genl_info *info,
7383 s32 threshold, u32 hysteresis)
7384{
Johannes Berg4c476992010-10-04 21:36:35 +02007385 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007386 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007387 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007388
7389 if (threshold > 0)
7390 return -EINVAL;
7391
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007392 wdev = dev->ieee80211_ptr;
7393
Johannes Berg4c476992010-10-04 21:36:35 +02007394 if (!rdev->ops->set_cqm_rssi_config)
7395 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007396
Johannes Berg074ac8d2010-09-16 14:58:22 +02007397 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007398 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7399 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007400
Hila Gonene35e4d22012-06-27 17:19:42 +03007401 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007402}
7403
7404static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7405{
7406 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7407 struct nlattr *cqm;
7408 int err;
7409
7410 cqm = info->attrs[NL80211_ATTR_CQM];
7411 if (!cqm) {
7412 err = -EINVAL;
7413 goto out;
7414 }
7415
7416 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7417 nl80211_attr_cqm_policy);
7418 if (err)
7419 goto out;
7420
7421 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7422 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7423 s32 threshold;
7424 u32 hysteresis;
7425 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7426 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7427 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007428 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7429 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7430 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7431 u32 rate, pkts, intvl;
7432 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7433 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7434 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7435 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007436 } else
7437 err = -EINVAL;
7438
7439out:
7440 return err;
7441}
7442
Johannes Berg29cbe682010-12-03 09:20:44 +01007443static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7444{
7445 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7446 struct net_device *dev = info->user_ptr[1];
7447 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007448 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007449 int err;
7450
7451 /* start with default */
7452 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007453 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007454
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007455 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007456 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007457 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007458 if (err)
7459 return err;
7460 }
7461
7462 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7463 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7464 return -EINVAL;
7465
Javier Cardonac80d5452010-12-16 17:37:49 -08007466 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7467 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7468
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007469 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7470 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7471 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7472 return -EINVAL;
7473
Marco Porsch9bdbf042013-01-07 16:04:51 +01007474 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7475 setup.beacon_interval =
7476 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7477 if (setup.beacon_interval < 10 ||
7478 setup.beacon_interval > 10000)
7479 return -EINVAL;
7480 }
7481
7482 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7483 setup.dtim_period =
7484 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7485 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7486 return -EINVAL;
7487 }
7488
Javier Cardonac80d5452010-12-16 17:37:49 -08007489 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7490 /* parse additional setup parameters if given */
7491 err = nl80211_parse_mesh_setup(info, &setup);
7492 if (err)
7493 return err;
7494 }
7495
Thomas Pedersend37bb182013-03-04 13:06:13 -08007496 if (setup.user_mpm)
7497 cfg.auto_open_plinks = false;
7498
Johannes Bergcc1d2802012-05-16 23:50:20 +02007499 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007500 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7501 if (err)
7502 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007503 } else {
7504 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007505 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007506 }
7507
Javier Cardonac80d5452010-12-16 17:37:49 -08007508 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007509}
7510
7511static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7512{
7513 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7514 struct net_device *dev = info->user_ptr[1];
7515
7516 return cfg80211_leave_mesh(rdev, dev);
7517}
7518
Johannes Bergdfb89c52012-06-27 09:23:48 +02007519#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007520static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7521 struct cfg80211_registered_device *rdev)
7522{
7523 struct nlattr *nl_pats, *nl_pat;
7524 int i, pat_len;
7525
7526 if (!rdev->wowlan->n_patterns)
7527 return 0;
7528
7529 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7530 if (!nl_pats)
7531 return -ENOBUFS;
7532
7533 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7534 nl_pat = nla_nest_start(msg, i + 1);
7535 if (!nl_pat)
7536 return -ENOBUFS;
7537 pat_len = rdev->wowlan->patterns[i].pattern_len;
7538 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7539 DIV_ROUND_UP(pat_len, 8),
7540 rdev->wowlan->patterns[i].mask) ||
7541 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7542 pat_len, rdev->wowlan->patterns[i].pattern) ||
7543 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7544 rdev->wowlan->patterns[i].pkt_offset))
7545 return -ENOBUFS;
7546 nla_nest_end(msg, nl_pat);
7547 }
7548 nla_nest_end(msg, nl_pats);
7549
7550 return 0;
7551}
7552
Johannes Berg2a0e0472013-01-23 22:57:40 +01007553static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7554 struct cfg80211_wowlan_tcp *tcp)
7555{
7556 struct nlattr *nl_tcp;
7557
7558 if (!tcp)
7559 return 0;
7560
7561 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7562 if (!nl_tcp)
7563 return -ENOBUFS;
7564
7565 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7566 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7567 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7568 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7569 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7570 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7571 tcp->payload_len, tcp->payload) ||
7572 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7573 tcp->data_interval) ||
7574 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7575 tcp->wake_len, tcp->wake_data) ||
7576 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7577 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7578 return -ENOBUFS;
7579
7580 if (tcp->payload_seq.len &&
7581 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7582 sizeof(tcp->payload_seq), &tcp->payload_seq))
7583 return -ENOBUFS;
7584
7585 if (tcp->payload_tok.len &&
7586 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7587 sizeof(tcp->payload_tok) + tcp->tokens_size,
7588 &tcp->payload_tok))
7589 return -ENOBUFS;
7590
Johannes Berge248ad32013-05-16 10:24:28 +02007591 nla_nest_end(msg, nl_tcp);
7592
Johannes Berg2a0e0472013-01-23 22:57:40 +01007593 return 0;
7594}
7595
Johannes Bergff1b6e62011-05-04 15:37:28 +02007596static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7597{
7598 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7599 struct sk_buff *msg;
7600 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007601 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007602
Johannes Berg2a0e0472013-01-23 22:57:40 +01007603 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7604 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007605 return -EOPNOTSUPP;
7606
Johannes Berg2a0e0472013-01-23 22:57:40 +01007607 if (rdev->wowlan && rdev->wowlan->tcp) {
7608 /* adjust size to have room for all the data */
7609 size += rdev->wowlan->tcp->tokens_size +
7610 rdev->wowlan->tcp->payload_len +
7611 rdev->wowlan->tcp->wake_len +
7612 rdev->wowlan->tcp->wake_len / 8;
7613 }
7614
7615 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007616 if (!msg)
7617 return -ENOMEM;
7618
Eric W. Biederman15e47302012-09-07 20:12:54 +00007619 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007620 NL80211_CMD_GET_WOWLAN);
7621 if (!hdr)
7622 goto nla_put_failure;
7623
7624 if (rdev->wowlan) {
7625 struct nlattr *nl_wowlan;
7626
7627 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7628 if (!nl_wowlan)
7629 goto nla_put_failure;
7630
David S. Miller9360ffd2012-03-29 04:41:26 -04007631 if ((rdev->wowlan->any &&
7632 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7633 (rdev->wowlan->disconnect &&
7634 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7635 (rdev->wowlan->magic_pkt &&
7636 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7637 (rdev->wowlan->gtk_rekey_failure &&
7638 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7639 (rdev->wowlan->eap_identity_req &&
7640 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7641 (rdev->wowlan->four_way_handshake &&
7642 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7643 (rdev->wowlan->rfkill_release &&
7644 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7645 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007646
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007647 if (nl80211_send_wowlan_patterns(msg, rdev))
7648 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007649
7650 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7651 goto nla_put_failure;
7652
Johannes Bergff1b6e62011-05-04 15:37:28 +02007653 nla_nest_end(msg, nl_wowlan);
7654 }
7655
7656 genlmsg_end(msg, hdr);
7657 return genlmsg_reply(msg, info);
7658
7659nla_put_failure:
7660 nlmsg_free(msg);
7661 return -ENOBUFS;
7662}
7663
Johannes Berg2a0e0472013-01-23 22:57:40 +01007664static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7665 struct nlattr *attr,
7666 struct cfg80211_wowlan *trig)
7667{
7668 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7669 struct cfg80211_wowlan_tcp *cfg;
7670 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7671 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7672 u32 size;
7673 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7674 int err, port;
7675
7676 if (!rdev->wiphy.wowlan.tcp)
7677 return -EINVAL;
7678
7679 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7680 nla_data(attr), nla_len(attr),
7681 nl80211_wowlan_tcp_policy);
7682 if (err)
7683 return err;
7684
7685 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7686 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7687 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7688 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7689 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7690 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7691 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7692 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7693 return -EINVAL;
7694
7695 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7696 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7697 return -EINVAL;
7698
7699 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg723d5682013-02-26 13:56:40 +01007700 rdev->wiphy.wowlan.tcp->data_interval_max ||
7701 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007702 return -EINVAL;
7703
7704 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7705 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7706 return -EINVAL;
7707
7708 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7709 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7710 return -EINVAL;
7711
7712 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7713 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7714
7715 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7716 tokens_size = tokln - sizeof(*tok);
7717
7718 if (!tok->len || tokens_size % tok->len)
7719 return -EINVAL;
7720 if (!rdev->wiphy.wowlan.tcp->tok)
7721 return -EINVAL;
7722 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7723 return -EINVAL;
7724 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7725 return -EINVAL;
7726 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7727 return -EINVAL;
7728 if (tok->offset + tok->len > data_size)
7729 return -EINVAL;
7730 }
7731
7732 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7733 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7734 if (!rdev->wiphy.wowlan.tcp->seq)
7735 return -EINVAL;
7736 if (seq->len == 0 || seq->len > 4)
7737 return -EINVAL;
7738 if (seq->len + seq->offset > data_size)
7739 return -EINVAL;
7740 }
7741
7742 size = sizeof(*cfg);
7743 size += data_size;
7744 size += wake_size + wake_mask_size;
7745 size += tokens_size;
7746
7747 cfg = kzalloc(size, GFP_KERNEL);
7748 if (!cfg)
7749 return -ENOMEM;
7750 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7751 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7752 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7753 ETH_ALEN);
7754 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7755 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7756 else
7757 port = 0;
7758#ifdef CONFIG_INET
7759 /* allocate a socket and port for it and use it */
7760 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7761 IPPROTO_TCP, &cfg->sock, 1);
7762 if (err) {
7763 kfree(cfg);
7764 return err;
7765 }
7766 if (inet_csk_get_port(cfg->sock->sk, port)) {
7767 sock_release(cfg->sock);
7768 kfree(cfg);
7769 return -EADDRINUSE;
7770 }
7771 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7772#else
7773 if (!port) {
7774 kfree(cfg);
7775 return -EINVAL;
7776 }
7777 cfg->src_port = port;
7778#endif
7779
7780 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7781 cfg->payload_len = data_size;
7782 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7783 memcpy((void *)cfg->payload,
7784 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7785 data_size);
7786 if (seq)
7787 cfg->payload_seq = *seq;
7788 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7789 cfg->wake_len = wake_size;
7790 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7791 memcpy((void *)cfg->wake_data,
7792 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7793 wake_size);
7794 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7795 data_size + wake_size;
7796 memcpy((void *)cfg->wake_mask,
7797 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7798 wake_mask_size);
7799 if (tok) {
7800 cfg->tokens_size = tokens_size;
7801 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7802 }
7803
7804 trig->tcp = cfg;
7805
7806 return 0;
7807}
7808
Johannes Bergff1b6e62011-05-04 15:37:28 +02007809static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7810{
7811 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7812 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007813 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007814 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007815 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7816 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007817 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007818
Johannes Berg2a0e0472013-01-23 22:57:40 +01007819 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7820 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007821 return -EOPNOTSUPP;
7822
Johannes Bergae33bd82012-07-12 16:25:02 +02007823 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7824 cfg80211_rdev_free_wowlan(rdev);
7825 rdev->wowlan = NULL;
7826 goto set_wakeup;
7827 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007828
7829 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7830 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7831 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7832 nl80211_wowlan_policy);
7833 if (err)
7834 return err;
7835
7836 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7837 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7838 return -EINVAL;
7839 new_triggers.any = true;
7840 }
7841
7842 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7843 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7844 return -EINVAL;
7845 new_triggers.disconnect = true;
7846 }
7847
7848 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7849 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7850 return -EINVAL;
7851 new_triggers.magic_pkt = true;
7852 }
7853
Johannes Berg77dbbb12011-07-13 10:48:55 +02007854 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7855 return -EINVAL;
7856
7857 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7858 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7859 return -EINVAL;
7860 new_triggers.gtk_rekey_failure = true;
7861 }
7862
7863 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7864 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7865 return -EINVAL;
7866 new_triggers.eap_identity_req = true;
7867 }
7868
7869 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7870 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7871 return -EINVAL;
7872 new_triggers.four_way_handshake = true;
7873 }
7874
7875 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7876 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7877 return -EINVAL;
7878 new_triggers.rfkill_release = true;
7879 }
7880
Johannes Bergff1b6e62011-05-04 15:37:28 +02007881 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7882 struct nlattr *pat;
7883 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007884 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007885 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7886
7887 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7888 rem)
7889 n_patterns++;
7890 if (n_patterns > wowlan->n_patterns)
7891 return -EINVAL;
7892
7893 new_triggers.patterns = kcalloc(n_patterns,
7894 sizeof(new_triggers.patterns[0]),
7895 GFP_KERNEL);
7896 if (!new_triggers.patterns)
7897 return -ENOMEM;
7898
7899 new_triggers.n_patterns = n_patterns;
7900 i = 0;
7901
7902 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7903 rem) {
7904 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7905 nla_data(pat), nla_len(pat), NULL);
7906 err = -EINVAL;
7907 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7908 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7909 goto error;
7910 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7911 mask_len = DIV_ROUND_UP(pat_len, 8);
7912 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7913 mask_len)
7914 goto error;
7915 if (pat_len > wowlan->pattern_max_len ||
7916 pat_len < wowlan->pattern_min_len)
7917 goto error;
7918
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007919 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7920 pkt_offset = 0;
7921 else
7922 pkt_offset = nla_get_u32(
7923 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7924 if (pkt_offset > wowlan->max_pkt_offset)
7925 goto error;
7926 new_triggers.patterns[i].pkt_offset = pkt_offset;
7927
Johannes Bergff1b6e62011-05-04 15:37:28 +02007928 new_triggers.patterns[i].mask =
7929 kmalloc(mask_len + pat_len, GFP_KERNEL);
7930 if (!new_triggers.patterns[i].mask) {
7931 err = -ENOMEM;
7932 goto error;
7933 }
7934 new_triggers.patterns[i].pattern =
7935 new_triggers.patterns[i].mask + mask_len;
7936 memcpy(new_triggers.patterns[i].mask,
7937 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7938 mask_len);
7939 new_triggers.patterns[i].pattern_len = pat_len;
7940 memcpy(new_triggers.patterns[i].pattern,
7941 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7942 pat_len);
7943 i++;
7944 }
7945 }
7946
Johannes Berg2a0e0472013-01-23 22:57:40 +01007947 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7948 err = nl80211_parse_wowlan_tcp(
7949 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7950 &new_triggers);
7951 if (err)
7952 goto error;
7953 }
7954
Johannes Bergae33bd82012-07-12 16:25:02 +02007955 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7956 if (!ntrig) {
7957 err = -ENOMEM;
7958 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007959 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007960 cfg80211_rdev_free_wowlan(rdev);
7961 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007962
Johannes Bergae33bd82012-07-12 16:25:02 +02007963 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007964 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007965 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007966
Johannes Bergff1b6e62011-05-04 15:37:28 +02007967 return 0;
7968 error:
7969 for (i = 0; i < new_triggers.n_patterns; i++)
7970 kfree(new_triggers.patterns[i].mask);
7971 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007972 if (new_triggers.tcp && new_triggers.tcp->sock)
7973 sock_release(new_triggers.tcp->sock);
7974 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007975 return err;
7976}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007977#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007978
Johannes Berge5497d72011-07-05 16:35:40 +02007979static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7980{
7981 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7982 struct net_device *dev = info->user_ptr[1];
7983 struct wireless_dev *wdev = dev->ieee80211_ptr;
7984 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7985 struct cfg80211_gtk_rekey_data rekey_data;
7986 int err;
7987
7988 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7989 return -EINVAL;
7990
7991 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7992 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7993 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7994 nl80211_rekey_policy);
7995 if (err)
7996 return err;
7997
7998 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7999 return -ERANGE;
8000 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8001 return -ERANGE;
8002 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8003 return -ERANGE;
8004
8005 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8006 NL80211_KEK_LEN);
8007 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8008 NL80211_KCK_LEN);
8009 memcpy(rekey_data.replay_ctr,
8010 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8011 NL80211_REPLAY_CTR_LEN);
8012
8013 wdev_lock(wdev);
8014 if (!wdev->current_bss) {
8015 err = -ENOTCONN;
8016 goto out;
8017 }
8018
8019 if (!rdev->ops->set_rekey_data) {
8020 err = -EOPNOTSUPP;
8021 goto out;
8022 }
8023
Hila Gonene35e4d22012-06-27 17:19:42 +03008024 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008025 out:
8026 wdev_unlock(wdev);
8027 return err;
8028}
8029
Johannes Berg28946da2011-11-04 11:18:12 +01008030static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8031 struct genl_info *info)
8032{
8033 struct net_device *dev = info->user_ptr[1];
8034 struct wireless_dev *wdev = dev->ieee80211_ptr;
8035
8036 if (wdev->iftype != NL80211_IFTYPE_AP &&
8037 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8038 return -EINVAL;
8039
Eric W. Biederman15e47302012-09-07 20:12:54 +00008040 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008041 return -EBUSY;
8042
Eric W. Biederman15e47302012-09-07 20:12:54 +00008043 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008044 return 0;
8045}
8046
Johannes Berg7f6cf312011-11-04 11:18:15 +01008047static int nl80211_probe_client(struct sk_buff *skb,
8048 struct genl_info *info)
8049{
8050 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8051 struct net_device *dev = info->user_ptr[1];
8052 struct wireless_dev *wdev = dev->ieee80211_ptr;
8053 struct sk_buff *msg;
8054 void *hdr;
8055 const u8 *addr;
8056 u64 cookie;
8057 int err;
8058
8059 if (wdev->iftype != NL80211_IFTYPE_AP &&
8060 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8061 return -EOPNOTSUPP;
8062
8063 if (!info->attrs[NL80211_ATTR_MAC])
8064 return -EINVAL;
8065
8066 if (!rdev->ops->probe_client)
8067 return -EOPNOTSUPP;
8068
8069 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8070 if (!msg)
8071 return -ENOMEM;
8072
Eric W. Biederman15e47302012-09-07 20:12:54 +00008073 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008074 NL80211_CMD_PROBE_CLIENT);
8075
8076 if (IS_ERR(hdr)) {
8077 err = PTR_ERR(hdr);
8078 goto free_msg;
8079 }
8080
8081 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8082
Hila Gonene35e4d22012-06-27 17:19:42 +03008083 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008084 if (err)
8085 goto free_msg;
8086
David S. Miller9360ffd2012-03-29 04:41:26 -04008087 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8088 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008089
8090 genlmsg_end(msg, hdr);
8091
8092 return genlmsg_reply(msg, info);
8093
8094 nla_put_failure:
8095 err = -ENOBUFS;
8096 free_msg:
8097 nlmsg_free(msg);
8098 return err;
8099}
8100
Johannes Berg5e760232011-11-04 11:18:17 +01008101static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8102{
8103 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008104 struct cfg80211_beacon_registration *reg, *nreg;
8105 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008106
8107 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8108 return -EOPNOTSUPP;
8109
Ben Greear37c73b52012-10-26 14:49:25 -07008110 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8111 if (!nreg)
8112 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008113
Ben Greear37c73b52012-10-26 14:49:25 -07008114 /* First, check if already registered. */
8115 spin_lock_bh(&rdev->beacon_registrations_lock);
8116 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8117 if (reg->nlportid == info->snd_portid) {
8118 rv = -EALREADY;
8119 goto out_err;
8120 }
8121 }
8122 /* Add it to the list */
8123 nreg->nlportid = info->snd_portid;
8124 list_add(&nreg->list, &rdev->beacon_registrations);
8125
8126 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008127
8128 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008129out_err:
8130 spin_unlock_bh(&rdev->beacon_registrations_lock);
8131 kfree(nreg);
8132 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008133}
8134
Johannes Berg98104fde2012-06-16 00:19:54 +02008135static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8136{
8137 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8138 struct wireless_dev *wdev = info->user_ptr[1];
8139 int err;
8140
8141 if (!rdev->ops->start_p2p_device)
8142 return -EOPNOTSUPP;
8143
8144 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8145 return -EOPNOTSUPP;
8146
8147 if (wdev->p2p_started)
8148 return 0;
8149
8150 mutex_lock(&rdev->devlist_mtx);
8151 err = cfg80211_can_add_interface(rdev, wdev->iftype);
8152 mutex_unlock(&rdev->devlist_mtx);
8153 if (err)
8154 return err;
8155
Johannes Bergeeb126e2012-10-23 15:16:50 +02008156 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008157 if (err)
8158 return err;
8159
8160 wdev->p2p_started = true;
8161 mutex_lock(&rdev->devlist_mtx);
8162 rdev->opencount++;
8163 mutex_unlock(&rdev->devlist_mtx);
8164
8165 return 0;
8166}
8167
8168static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8169{
8170 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8171 struct wireless_dev *wdev = info->user_ptr[1];
8172
8173 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8174 return -EOPNOTSUPP;
8175
8176 if (!rdev->ops->stop_p2p_device)
8177 return -EOPNOTSUPP;
8178
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008179 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +01008180 mutex_lock(&rdev->sched_scan_mtx);
8181 cfg80211_stop_p2p_device(rdev, wdev);
8182 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008183 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg98104fde2012-06-16 00:19:54 +02008184
8185 return 0;
8186}
8187
Johannes Berg3713b4e2013-02-14 16:19:38 +01008188static int nl80211_get_protocol_features(struct sk_buff *skb,
8189 struct genl_info *info)
8190{
8191 void *hdr;
8192 struct sk_buff *msg;
8193
8194 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8195 if (!msg)
8196 return -ENOMEM;
8197
8198 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8199 NL80211_CMD_GET_PROTOCOL_FEATURES);
8200 if (!hdr)
8201 goto nla_put_failure;
8202
8203 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8204 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8205 goto nla_put_failure;
8206
8207 genlmsg_end(msg, hdr);
8208 return genlmsg_reply(msg, info);
8209
8210 nla_put_failure:
8211 kfree_skb(msg);
8212 return -ENOBUFS;
8213}
8214
Jouni Malinen355199e2013-02-27 17:14:27 +02008215static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8216{
8217 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8218 struct cfg80211_update_ft_ies_params ft_params;
8219 struct net_device *dev = info->user_ptr[1];
8220
8221 if (!rdev->ops->update_ft_ies)
8222 return -EOPNOTSUPP;
8223
8224 if (!info->attrs[NL80211_ATTR_MDID] ||
8225 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8226 return -EINVAL;
8227
8228 memset(&ft_params, 0, sizeof(ft_params));
8229 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8230 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8231 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8232
8233 return rdev_update_ft_ies(rdev, dev, &ft_params);
8234}
8235
Arend van Spriel5de17982013-04-18 15:49:00 +02008236static int nl80211_crit_protocol_start(struct sk_buff *skb,
8237 struct genl_info *info)
8238{
8239 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8240 struct wireless_dev *wdev = info->user_ptr[1];
8241 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8242 u16 duration;
8243 int ret;
8244
8245 if (!rdev->ops->crit_proto_start)
8246 return -EOPNOTSUPP;
8247
8248 if (WARN_ON(!rdev->ops->crit_proto_stop))
8249 return -EINVAL;
8250
8251 if (rdev->crit_proto_nlportid)
8252 return -EBUSY;
8253
8254 /* determine protocol if provided */
8255 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8256 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8257
8258 if (proto >= NUM_NL80211_CRIT_PROTO)
8259 return -EINVAL;
8260
8261 /* timeout must be provided */
8262 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8263 return -EINVAL;
8264
8265 duration =
8266 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8267
8268 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8269 return -ERANGE;
8270
8271 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8272 if (!ret)
8273 rdev->crit_proto_nlportid = info->snd_portid;
8274
8275 return ret;
8276}
8277
8278static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8279 struct genl_info *info)
8280{
8281 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8282 struct wireless_dev *wdev = info->user_ptr[1];
8283
8284 if (!rdev->ops->crit_proto_stop)
8285 return -EOPNOTSUPP;
8286
8287 if (rdev->crit_proto_nlportid) {
8288 rdev->crit_proto_nlportid = 0;
8289 rdev_crit_proto_stop(rdev, wdev);
8290 }
8291 return 0;
8292}
8293
Johannes Berg4c476992010-10-04 21:36:35 +02008294#define NL80211_FLAG_NEED_WIPHY 0x01
8295#define NL80211_FLAG_NEED_NETDEV 0x02
8296#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008297#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8298#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8299 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008300#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008301/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008302#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8303 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008304
8305static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8306 struct genl_info *info)
8307{
8308 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008309 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008310 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008311 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8312
8313 if (rtnl)
8314 rtnl_lock();
8315
8316 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008317 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008318 if (IS_ERR(rdev)) {
8319 if (rtnl)
8320 rtnl_unlock();
8321 return PTR_ERR(rdev);
8322 }
8323 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008324 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8325 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008326 mutex_lock(&cfg80211_mutex);
8327 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8328 info->attrs);
8329 if (IS_ERR(wdev)) {
8330 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02008331 if (rtnl)
8332 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02008333 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02008334 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008335
Johannes Berg89a54e42012-06-15 14:33:17 +02008336 dev = wdev->netdev;
8337 rdev = wiphy_to_dev(wdev->wiphy);
8338
Johannes Berg1bf614e2012-06-15 15:23:36 +02008339 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8340 if (!dev) {
8341 mutex_unlock(&cfg80211_mutex);
8342 if (rtnl)
8343 rtnl_unlock();
8344 return -EINVAL;
8345 }
8346
8347 info->user_ptr[1] = dev;
8348 } else {
8349 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02008350 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008351
Johannes Berg1bf614e2012-06-15 15:23:36 +02008352 if (dev) {
8353 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8354 !netif_running(dev)) {
8355 mutex_unlock(&cfg80211_mutex);
8356 if (rtnl)
8357 rtnl_unlock();
8358 return -ENETDOWN;
8359 }
8360
8361 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008362 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8363 if (!wdev->p2p_started) {
8364 mutex_unlock(&cfg80211_mutex);
8365 if (rtnl)
8366 rtnl_unlock();
8367 return -ENETDOWN;
8368 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02008369 }
8370
Johannes Berg89a54e42012-06-15 14:33:17 +02008371 cfg80211_lock_rdev(rdev);
8372
8373 mutex_unlock(&cfg80211_mutex);
8374
Johannes Berg4c476992010-10-04 21:36:35 +02008375 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008376 }
8377
8378 return 0;
8379}
8380
8381static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8382 struct genl_info *info)
8383{
8384 if (info->user_ptr[0])
8385 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02008386 if (info->user_ptr[1]) {
8387 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8388 struct wireless_dev *wdev = info->user_ptr[1];
8389
8390 if (wdev->netdev)
8391 dev_put(wdev->netdev);
8392 } else {
8393 dev_put(info->user_ptr[1]);
8394 }
8395 }
Johannes Berg4c476992010-10-04 21:36:35 +02008396 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8397 rtnl_unlock();
8398}
8399
Johannes Berg55682962007-09-20 13:09:35 -04008400static struct genl_ops nl80211_ops[] = {
8401 {
8402 .cmd = NL80211_CMD_GET_WIPHY,
8403 .doit = nl80211_get_wiphy,
8404 .dumpit = nl80211_dump_wiphy,
8405 .policy = nl80211_policy,
8406 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008407 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04008408 },
8409 {
8410 .cmd = NL80211_CMD_SET_WIPHY,
8411 .doit = nl80211_set_wiphy,
8412 .policy = nl80211_policy,
8413 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008414 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008415 },
8416 {
8417 .cmd = NL80211_CMD_GET_INTERFACE,
8418 .doit = nl80211_get_interface,
8419 .dumpit = nl80211_dump_interface,
8420 .policy = nl80211_policy,
8421 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02008422 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04008423 },
8424 {
8425 .cmd = NL80211_CMD_SET_INTERFACE,
8426 .doit = nl80211_set_interface,
8427 .policy = nl80211_policy,
8428 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008429 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8430 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008431 },
8432 {
8433 .cmd = NL80211_CMD_NEW_INTERFACE,
8434 .doit = nl80211_new_interface,
8435 .policy = nl80211_policy,
8436 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008437 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8438 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008439 },
8440 {
8441 .cmd = NL80211_CMD_DEL_INTERFACE,
8442 .doit = nl80211_del_interface,
8443 .policy = nl80211_policy,
8444 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02008445 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008446 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008447 },
Johannes Berg41ade002007-12-19 02:03:29 +01008448 {
8449 .cmd = NL80211_CMD_GET_KEY,
8450 .doit = nl80211_get_key,
8451 .policy = nl80211_policy,
8452 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008453 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008454 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008455 },
8456 {
8457 .cmd = NL80211_CMD_SET_KEY,
8458 .doit = nl80211_set_key,
8459 .policy = nl80211_policy,
8460 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008461 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008462 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008463 },
8464 {
8465 .cmd = NL80211_CMD_NEW_KEY,
8466 .doit = nl80211_new_key,
8467 .policy = nl80211_policy,
8468 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008469 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008470 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008471 },
8472 {
8473 .cmd = NL80211_CMD_DEL_KEY,
8474 .doit = nl80211_del_key,
8475 .policy = nl80211_policy,
8476 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008477 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008478 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008479 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008480 {
8481 .cmd = NL80211_CMD_SET_BEACON,
8482 .policy = nl80211_policy,
8483 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008484 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008485 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008486 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008487 },
8488 {
Johannes Berg88600202012-02-13 15:17:18 +01008489 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008490 .policy = nl80211_policy,
8491 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008492 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008493 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008494 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008495 },
8496 {
Johannes Berg88600202012-02-13 15:17:18 +01008497 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008498 .policy = nl80211_policy,
8499 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008500 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008501 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008502 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008503 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008504 {
8505 .cmd = NL80211_CMD_GET_STATION,
8506 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008507 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008508 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008509 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8510 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008511 },
8512 {
8513 .cmd = NL80211_CMD_SET_STATION,
8514 .doit = nl80211_set_station,
8515 .policy = nl80211_policy,
8516 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008517 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008518 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008519 },
8520 {
8521 .cmd = NL80211_CMD_NEW_STATION,
8522 .doit = nl80211_new_station,
8523 .policy = nl80211_policy,
8524 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008525 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008526 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008527 },
8528 {
8529 .cmd = NL80211_CMD_DEL_STATION,
8530 .doit = nl80211_del_station,
8531 .policy = nl80211_policy,
8532 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008533 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008534 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008535 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008536 {
8537 .cmd = NL80211_CMD_GET_MPATH,
8538 .doit = nl80211_get_mpath,
8539 .dumpit = nl80211_dump_mpath,
8540 .policy = nl80211_policy,
8541 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008542 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008543 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008544 },
8545 {
8546 .cmd = NL80211_CMD_SET_MPATH,
8547 .doit = nl80211_set_mpath,
8548 .policy = nl80211_policy,
8549 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008551 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008552 },
8553 {
8554 .cmd = NL80211_CMD_NEW_MPATH,
8555 .doit = nl80211_new_mpath,
8556 .policy = nl80211_policy,
8557 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008558 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008559 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008560 },
8561 {
8562 .cmd = NL80211_CMD_DEL_MPATH,
8563 .doit = nl80211_del_mpath,
8564 .policy = nl80211_policy,
8565 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008566 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008567 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008568 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008569 {
8570 .cmd = NL80211_CMD_SET_BSS,
8571 .doit = nl80211_set_bss,
8572 .policy = nl80211_policy,
8573 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008574 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008575 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008576 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008577 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008578 .cmd = NL80211_CMD_GET_REG,
8579 .doit = nl80211_get_reg,
8580 .policy = nl80211_policy,
8581 /* can be retrieved by unprivileged users */
8582 },
8583 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008584 .cmd = NL80211_CMD_SET_REG,
8585 .doit = nl80211_set_reg,
8586 .policy = nl80211_policy,
8587 .flags = GENL_ADMIN_PERM,
8588 },
8589 {
8590 .cmd = NL80211_CMD_REQ_SET_REG,
8591 .doit = nl80211_req_set_reg,
8592 .policy = nl80211_policy,
8593 .flags = GENL_ADMIN_PERM,
8594 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008595 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008596 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8597 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008598 .policy = nl80211_policy,
8599 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008600 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008601 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008602 },
8603 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008604 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8605 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008606 .policy = nl80211_policy,
8607 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008608 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008609 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008610 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008611 {
Johannes Berg2a519312009-02-10 21:25:55 +01008612 .cmd = NL80211_CMD_TRIGGER_SCAN,
8613 .doit = nl80211_trigger_scan,
8614 .policy = nl80211_policy,
8615 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008616 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008617 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008618 },
8619 {
8620 .cmd = NL80211_CMD_GET_SCAN,
8621 .policy = nl80211_policy,
8622 .dumpit = nl80211_dump_scan,
8623 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008624 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008625 .cmd = NL80211_CMD_START_SCHED_SCAN,
8626 .doit = nl80211_start_sched_scan,
8627 .policy = nl80211_policy,
8628 .flags = GENL_ADMIN_PERM,
8629 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8630 NL80211_FLAG_NEED_RTNL,
8631 },
8632 {
8633 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8634 .doit = nl80211_stop_sched_scan,
8635 .policy = nl80211_policy,
8636 .flags = GENL_ADMIN_PERM,
8637 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8638 NL80211_FLAG_NEED_RTNL,
8639 },
8640 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008641 .cmd = NL80211_CMD_AUTHENTICATE,
8642 .doit = nl80211_authenticate,
8643 .policy = nl80211_policy,
8644 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008645 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008646 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008647 },
8648 {
8649 .cmd = NL80211_CMD_ASSOCIATE,
8650 .doit = nl80211_associate,
8651 .policy = nl80211_policy,
8652 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008653 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008654 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008655 },
8656 {
8657 .cmd = NL80211_CMD_DEAUTHENTICATE,
8658 .doit = nl80211_deauthenticate,
8659 .policy = nl80211_policy,
8660 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008661 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008662 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008663 },
8664 {
8665 .cmd = NL80211_CMD_DISASSOCIATE,
8666 .doit = nl80211_disassociate,
8667 .policy = nl80211_policy,
8668 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008669 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008670 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008671 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008672 {
8673 .cmd = NL80211_CMD_JOIN_IBSS,
8674 .doit = nl80211_join_ibss,
8675 .policy = nl80211_policy,
8676 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008677 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008678 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008679 },
8680 {
8681 .cmd = NL80211_CMD_LEAVE_IBSS,
8682 .doit = nl80211_leave_ibss,
8683 .policy = nl80211_policy,
8684 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008685 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008686 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008687 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008688#ifdef CONFIG_NL80211_TESTMODE
8689 {
8690 .cmd = NL80211_CMD_TESTMODE,
8691 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008692 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008693 .policy = nl80211_policy,
8694 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008695 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8696 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008697 },
8698#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008699 {
8700 .cmd = NL80211_CMD_CONNECT,
8701 .doit = nl80211_connect,
8702 .policy = nl80211_policy,
8703 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008704 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008705 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008706 },
8707 {
8708 .cmd = NL80211_CMD_DISCONNECT,
8709 .doit = nl80211_disconnect,
8710 .policy = nl80211_policy,
8711 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008712 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008713 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008714 },
Johannes Berg463d0182009-07-14 00:33:35 +02008715 {
8716 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8717 .doit = nl80211_wiphy_netns,
8718 .policy = nl80211_policy,
8719 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008720 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8721 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008722 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008723 {
8724 .cmd = NL80211_CMD_GET_SURVEY,
8725 .policy = nl80211_policy,
8726 .dumpit = nl80211_dump_survey,
8727 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008728 {
8729 .cmd = NL80211_CMD_SET_PMKSA,
8730 .doit = nl80211_setdel_pmksa,
8731 .policy = nl80211_policy,
8732 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008733 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008734 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008735 },
8736 {
8737 .cmd = NL80211_CMD_DEL_PMKSA,
8738 .doit = nl80211_setdel_pmksa,
8739 .policy = nl80211_policy,
8740 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008741 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008742 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008743 },
8744 {
8745 .cmd = NL80211_CMD_FLUSH_PMKSA,
8746 .doit = nl80211_flush_pmksa,
8747 .policy = nl80211_policy,
8748 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008749 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008750 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008751 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008752 {
8753 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8754 .doit = nl80211_remain_on_channel,
8755 .policy = nl80211_policy,
8756 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008757 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008758 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008759 },
8760 {
8761 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8762 .doit = nl80211_cancel_remain_on_channel,
8763 .policy = nl80211_policy,
8764 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008765 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008766 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008767 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008768 {
8769 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8770 .doit = nl80211_set_tx_bitrate_mask,
8771 .policy = nl80211_policy,
8772 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008773 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8774 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008775 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008776 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008777 .cmd = NL80211_CMD_REGISTER_FRAME,
8778 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008779 .policy = nl80211_policy,
8780 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008781 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008782 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008783 },
8784 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008785 .cmd = NL80211_CMD_FRAME,
8786 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008787 .policy = nl80211_policy,
8788 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008789 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008790 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008791 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008792 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008793 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8794 .doit = nl80211_tx_mgmt_cancel_wait,
8795 .policy = nl80211_policy,
8796 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008797 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008798 NL80211_FLAG_NEED_RTNL,
8799 },
8800 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008801 .cmd = NL80211_CMD_SET_POWER_SAVE,
8802 .doit = nl80211_set_power_save,
8803 .policy = nl80211_policy,
8804 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008805 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8806 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008807 },
8808 {
8809 .cmd = NL80211_CMD_GET_POWER_SAVE,
8810 .doit = nl80211_get_power_save,
8811 .policy = nl80211_policy,
8812 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008813 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8814 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008815 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008816 {
8817 .cmd = NL80211_CMD_SET_CQM,
8818 .doit = nl80211_set_cqm,
8819 .policy = nl80211_policy,
8820 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008821 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8822 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008823 },
Johannes Bergf444de02010-05-05 15:25:02 +02008824 {
8825 .cmd = NL80211_CMD_SET_CHANNEL,
8826 .doit = nl80211_set_channel,
8827 .policy = nl80211_policy,
8828 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008829 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8830 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008831 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008832 {
8833 .cmd = NL80211_CMD_SET_WDS_PEER,
8834 .doit = nl80211_set_wds_peer,
8835 .policy = nl80211_policy,
8836 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008837 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8838 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008839 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008840 {
8841 .cmd = NL80211_CMD_JOIN_MESH,
8842 .doit = nl80211_join_mesh,
8843 .policy = nl80211_policy,
8844 .flags = GENL_ADMIN_PERM,
8845 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8846 NL80211_FLAG_NEED_RTNL,
8847 },
8848 {
8849 .cmd = NL80211_CMD_LEAVE_MESH,
8850 .doit = nl80211_leave_mesh,
8851 .policy = nl80211_policy,
8852 .flags = GENL_ADMIN_PERM,
8853 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8854 NL80211_FLAG_NEED_RTNL,
8855 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008856#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008857 {
8858 .cmd = NL80211_CMD_GET_WOWLAN,
8859 .doit = nl80211_get_wowlan,
8860 .policy = nl80211_policy,
8861 /* can be retrieved by unprivileged users */
8862 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8863 NL80211_FLAG_NEED_RTNL,
8864 },
8865 {
8866 .cmd = NL80211_CMD_SET_WOWLAN,
8867 .doit = nl80211_set_wowlan,
8868 .policy = nl80211_policy,
8869 .flags = GENL_ADMIN_PERM,
8870 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8871 NL80211_FLAG_NEED_RTNL,
8872 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008873#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008874 {
8875 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8876 .doit = nl80211_set_rekey_data,
8877 .policy = nl80211_policy,
8878 .flags = GENL_ADMIN_PERM,
8879 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8880 NL80211_FLAG_NEED_RTNL,
8881 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008882 {
8883 .cmd = NL80211_CMD_TDLS_MGMT,
8884 .doit = nl80211_tdls_mgmt,
8885 .policy = nl80211_policy,
8886 .flags = GENL_ADMIN_PERM,
8887 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8888 NL80211_FLAG_NEED_RTNL,
8889 },
8890 {
8891 .cmd = NL80211_CMD_TDLS_OPER,
8892 .doit = nl80211_tdls_oper,
8893 .policy = nl80211_policy,
8894 .flags = GENL_ADMIN_PERM,
8895 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8896 NL80211_FLAG_NEED_RTNL,
8897 },
Johannes Berg28946da2011-11-04 11:18:12 +01008898 {
8899 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8900 .doit = nl80211_register_unexpected_frame,
8901 .policy = nl80211_policy,
8902 .flags = GENL_ADMIN_PERM,
8903 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8904 NL80211_FLAG_NEED_RTNL,
8905 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008906 {
8907 .cmd = NL80211_CMD_PROBE_CLIENT,
8908 .doit = nl80211_probe_client,
8909 .policy = nl80211_policy,
8910 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008911 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008912 NL80211_FLAG_NEED_RTNL,
8913 },
Johannes Berg5e760232011-11-04 11:18:17 +01008914 {
8915 .cmd = NL80211_CMD_REGISTER_BEACONS,
8916 .doit = nl80211_register_beacons,
8917 .policy = nl80211_policy,
8918 .flags = GENL_ADMIN_PERM,
8919 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8920 NL80211_FLAG_NEED_RTNL,
8921 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008922 {
8923 .cmd = NL80211_CMD_SET_NOACK_MAP,
8924 .doit = nl80211_set_noack_map,
8925 .policy = nl80211_policy,
8926 .flags = GENL_ADMIN_PERM,
8927 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8928 NL80211_FLAG_NEED_RTNL,
8929 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008930 {
8931 .cmd = NL80211_CMD_START_P2P_DEVICE,
8932 .doit = nl80211_start_p2p_device,
8933 .policy = nl80211_policy,
8934 .flags = GENL_ADMIN_PERM,
8935 .internal_flags = NL80211_FLAG_NEED_WDEV |
8936 NL80211_FLAG_NEED_RTNL,
8937 },
8938 {
8939 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8940 .doit = nl80211_stop_p2p_device,
8941 .policy = nl80211_policy,
8942 .flags = GENL_ADMIN_PERM,
8943 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8944 NL80211_FLAG_NEED_RTNL,
8945 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008946 {
8947 .cmd = NL80211_CMD_SET_MCAST_RATE,
8948 .doit = nl80211_set_mcast_rate,
8949 .policy = nl80211_policy,
8950 .flags = GENL_ADMIN_PERM,
8951 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8952 NL80211_FLAG_NEED_RTNL,
8953 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308954 {
8955 .cmd = NL80211_CMD_SET_MAC_ACL,
8956 .doit = nl80211_set_mac_acl,
8957 .policy = nl80211_policy,
8958 .flags = GENL_ADMIN_PERM,
8959 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8960 NL80211_FLAG_NEED_RTNL,
8961 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008962 {
8963 .cmd = NL80211_CMD_RADAR_DETECT,
8964 .doit = nl80211_start_radar_detection,
8965 .policy = nl80211_policy,
8966 .flags = GENL_ADMIN_PERM,
8967 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8968 NL80211_FLAG_NEED_RTNL,
8969 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01008970 {
8971 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
8972 .doit = nl80211_get_protocol_features,
8973 .policy = nl80211_policy,
8974 },
Jouni Malinen355199e2013-02-27 17:14:27 +02008975 {
8976 .cmd = NL80211_CMD_UPDATE_FT_IES,
8977 .doit = nl80211_update_ft_ies,
8978 .policy = nl80211_policy,
8979 .flags = GENL_ADMIN_PERM,
8980 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8981 NL80211_FLAG_NEED_RTNL,
8982 },
Arend van Spriel5de17982013-04-18 15:49:00 +02008983 {
8984 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
8985 .doit = nl80211_crit_protocol_start,
8986 .policy = nl80211_policy,
8987 .flags = GENL_ADMIN_PERM,
8988 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8989 NL80211_FLAG_NEED_RTNL,
8990 },
8991 {
8992 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
8993 .doit = nl80211_crit_protocol_stop,
8994 .policy = nl80211_policy,
8995 .flags = GENL_ADMIN_PERM,
8996 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8997 NL80211_FLAG_NEED_RTNL,
8998 }
Johannes Berg55682962007-09-20 13:09:35 -04008999};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009000
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009001static struct genl_multicast_group nl80211_mlme_mcgrp = {
9002 .name = "mlme",
9003};
Johannes Berg55682962007-09-20 13:09:35 -04009004
9005/* multicast groups */
9006static struct genl_multicast_group nl80211_config_mcgrp = {
9007 .name = "config",
9008};
Johannes Berg2a519312009-02-10 21:25:55 +01009009static struct genl_multicast_group nl80211_scan_mcgrp = {
9010 .name = "scan",
9011};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009012static struct genl_multicast_group nl80211_regulatory_mcgrp = {
9013 .name = "regulatory",
9014};
Johannes Berg55682962007-09-20 13:09:35 -04009015
9016/* notification functions */
9017
9018void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9019{
9020 struct sk_buff *msg;
9021
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009022 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009023 if (!msg)
9024 return;
9025
Johannes Berg3713b4e2013-02-14 16:19:38 +01009026 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
9027 false, NULL, NULL, NULL) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04009028 nlmsg_free(msg);
9029 return;
9030 }
9031
Johannes Berg463d0182009-07-14 00:33:35 +02009032 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9033 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009034}
9035
Johannes Berg362a4152009-05-24 16:43:15 +02009036static int nl80211_add_scan_req(struct sk_buff *msg,
9037 struct cfg80211_registered_device *rdev)
9038{
9039 struct cfg80211_scan_request *req = rdev->scan_req;
9040 struct nlattr *nest;
9041 int i;
9042
Johannes Bergf9f47522013-03-19 15:04:07 +01009043 lockdep_assert_held(&rdev->sched_scan_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +02009044
Johannes Berg362a4152009-05-24 16:43:15 +02009045 if (WARN_ON(!req))
9046 return 0;
9047
9048 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9049 if (!nest)
9050 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009051 for (i = 0; i < req->n_ssids; i++) {
9052 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9053 goto nla_put_failure;
9054 }
Johannes Berg362a4152009-05-24 16:43:15 +02009055 nla_nest_end(msg, nest);
9056
9057 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9058 if (!nest)
9059 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009060 for (i = 0; i < req->n_channels; i++) {
9061 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9062 goto nla_put_failure;
9063 }
Johannes Berg362a4152009-05-24 16:43:15 +02009064 nla_nest_end(msg, nest);
9065
David S. Miller9360ffd2012-03-29 04:41:26 -04009066 if (req->ie &&
9067 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9068 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02009069
Sam Lefflered4737712012-10-11 21:03:31 -07009070 if (req->flags)
9071 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
9072
Johannes Berg362a4152009-05-24 16:43:15 +02009073 return 0;
9074 nla_put_failure:
9075 return -ENOBUFS;
9076}
9077
Johannes Berga538e2d2009-06-16 19:56:42 +02009078static int nl80211_send_scan_msg(struct sk_buff *msg,
9079 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009080 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009081 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02009082 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01009083{
9084 void *hdr;
9085
Eric W. Biederman15e47302012-09-07 20:12:54 +00009086 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01009087 if (!hdr)
9088 return -1;
9089
David S. Miller9360ffd2012-03-29 04:41:26 -04009090 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009091 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9092 wdev->netdev->ifindex)) ||
9093 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009094 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009095
Johannes Berg362a4152009-05-24 16:43:15 +02009096 /* ignore errors and send incomplete event anyway */
9097 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009098
9099 return genlmsg_end(msg, hdr);
9100
9101 nla_put_failure:
9102 genlmsg_cancel(msg, hdr);
9103 return -EMSGSIZE;
9104}
9105
Luciano Coelho807f8a82011-05-11 17:09:35 +03009106static int
9107nl80211_send_sched_scan_msg(struct sk_buff *msg,
9108 struct cfg80211_registered_device *rdev,
9109 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009110 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009111{
9112 void *hdr;
9113
Eric W. Biederman15e47302012-09-07 20:12:54 +00009114 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009115 if (!hdr)
9116 return -1;
9117
David S. Miller9360ffd2012-03-29 04:41:26 -04009118 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9119 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9120 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009121
9122 return genlmsg_end(msg, hdr);
9123
9124 nla_put_failure:
9125 genlmsg_cancel(msg, hdr);
9126 return -EMSGSIZE;
9127}
9128
Johannes Berga538e2d2009-06-16 19:56:42 +02009129void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009130 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009131{
9132 struct sk_buff *msg;
9133
Thomas Graf58050fc2012-06-28 03:57:45 +00009134 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009135 if (!msg)
9136 return;
9137
Johannes Bergfd014282012-06-18 19:17:03 +02009138 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009139 NL80211_CMD_TRIGGER_SCAN) < 0) {
9140 nlmsg_free(msg);
9141 return;
9142 }
9143
Johannes Berg463d0182009-07-14 00:33:35 +02009144 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9145 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009146}
9147
Johannes Berg2a519312009-02-10 21:25:55 +01009148void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009149 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009150{
9151 struct sk_buff *msg;
9152
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009153 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009154 if (!msg)
9155 return;
9156
Johannes Bergfd014282012-06-18 19:17:03 +02009157 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009158 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009159 nlmsg_free(msg);
9160 return;
9161 }
9162
Johannes Berg463d0182009-07-14 00:33:35 +02009163 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9164 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009165}
9166
9167void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009168 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009169{
9170 struct sk_buff *msg;
9171
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009172 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009173 if (!msg)
9174 return;
9175
Johannes Bergfd014282012-06-18 19:17:03 +02009176 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009177 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009178 nlmsg_free(msg);
9179 return;
9180 }
9181
Johannes Berg463d0182009-07-14 00:33:35 +02009182 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9183 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009184}
9185
Luciano Coelho807f8a82011-05-11 17:09:35 +03009186void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9187 struct net_device *netdev)
9188{
9189 struct sk_buff *msg;
9190
9191 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9192 if (!msg)
9193 return;
9194
9195 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9196 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9197 nlmsg_free(msg);
9198 return;
9199 }
9200
9201 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9202 nl80211_scan_mcgrp.id, GFP_KERNEL);
9203}
9204
9205void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9206 struct net_device *netdev, u32 cmd)
9207{
9208 struct sk_buff *msg;
9209
Thomas Graf58050fc2012-06-28 03:57:45 +00009210 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009211 if (!msg)
9212 return;
9213
9214 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9215 nlmsg_free(msg);
9216 return;
9217 }
9218
9219 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9220 nl80211_scan_mcgrp.id, GFP_KERNEL);
9221}
9222
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009223/*
9224 * This can happen on global regulatory changes or device specific settings
9225 * based on custom world regulatory domains.
9226 */
9227void nl80211_send_reg_change_event(struct regulatory_request *request)
9228{
9229 struct sk_buff *msg;
9230 void *hdr;
9231
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009232 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009233 if (!msg)
9234 return;
9235
9236 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9237 if (!hdr) {
9238 nlmsg_free(msg);
9239 return;
9240 }
9241
9242 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04009243 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9244 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009245
David S. Miller9360ffd2012-03-29 04:41:26 -04009246 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9247 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9248 NL80211_REGDOM_TYPE_WORLD))
9249 goto nla_put_failure;
9250 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9251 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9252 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9253 goto nla_put_failure;
9254 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9255 request->intersect) {
9256 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9257 NL80211_REGDOM_TYPE_INTERSECTION))
9258 goto nla_put_failure;
9259 } else {
9260 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9261 NL80211_REGDOM_TYPE_COUNTRY) ||
9262 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9263 request->alpha2))
9264 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009265 }
9266
Johannes Bergf4173762012-12-03 18:23:37 +01009267 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04009268 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9269 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009270
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009271 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009272
Johannes Bergbc43b282009-07-25 10:54:13 +02009273 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009274 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009275 GFP_ATOMIC);
9276 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009277
9278 return;
9279
9280nla_put_failure:
9281 genlmsg_cancel(msg, hdr);
9282 nlmsg_free(msg);
9283}
9284
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009285static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9286 struct net_device *netdev,
9287 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009288 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009289{
9290 struct sk_buff *msg;
9291 void *hdr;
9292
Johannes Berge6d6e342009-07-01 21:26:47 +02009293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009294 if (!msg)
9295 return;
9296
9297 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9298 if (!hdr) {
9299 nlmsg_free(msg);
9300 return;
9301 }
9302
David S. Miller9360ffd2012-03-29 04:41:26 -04009303 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9304 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9305 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9306 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009307
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009308 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009309
Johannes Berg463d0182009-07-14 00:33:35 +02009310 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9311 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009312 return;
9313
9314 nla_put_failure:
9315 genlmsg_cancel(msg, hdr);
9316 nlmsg_free(msg);
9317}
9318
9319void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009320 struct net_device *netdev, const u8 *buf,
9321 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009322{
9323 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009324 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009325}
9326
9327void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9328 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009329 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009330{
Johannes Berge6d6e342009-07-01 21:26:47 +02009331 nl80211_send_mlme_event(rdev, netdev, buf, len,
9332 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009333}
9334
Jouni Malinen53b46b82009-03-27 20:53:56 +02009335void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009336 struct net_device *netdev, const u8 *buf,
9337 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009338{
9339 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009340 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009341}
9342
Jouni Malinen53b46b82009-03-27 20:53:56 +02009343void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9344 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009345 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009346{
9347 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009348 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009349}
9350
Johannes Berg947add32013-02-22 22:05:20 +01009351void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
9352 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +02009353{
Johannes Berg947add32013-02-22 22:05:20 +01009354 struct wireless_dev *wdev = dev->ieee80211_ptr;
9355 struct wiphy *wiphy = wdev->wiphy;
9356 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009357
Johannes Berg947add32013-02-22 22:05:20 +01009358 trace_cfg80211_send_unprot_deauth(dev);
9359 nl80211_send_mlme_event(rdev, dev, buf, len,
9360 NL80211_CMD_UNPROT_DEAUTHENTICATE, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009361}
Johannes Berg947add32013-02-22 22:05:20 +01009362EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
9363
9364void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
9365 size_t len)
9366{
9367 struct wireless_dev *wdev = dev->ieee80211_ptr;
9368 struct wiphy *wiphy = wdev->wiphy;
9369 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9370
9371 trace_cfg80211_send_unprot_disassoc(dev);
9372 nl80211_send_mlme_event(rdev, dev, buf, len,
9373 NL80211_CMD_UNPROT_DISASSOCIATE, GFP_ATOMIC);
9374}
9375EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009376
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04009377static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9378 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02009379 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009380{
9381 struct sk_buff *msg;
9382 void *hdr;
9383
Johannes Berge6d6e342009-07-01 21:26:47 +02009384 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009385 if (!msg)
9386 return;
9387
9388 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9389 if (!hdr) {
9390 nlmsg_free(msg);
9391 return;
9392 }
9393
David S. Miller9360ffd2012-03-29 04:41:26 -04009394 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9395 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9396 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9397 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9398 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03009399
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009400 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03009401
Johannes Berg463d0182009-07-14 00:33:35 +02009402 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9403 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009404 return;
9405
9406 nla_put_failure:
9407 genlmsg_cancel(msg, hdr);
9408 nlmsg_free(msg);
9409}
9410
9411void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009412 struct net_device *netdev, const u8 *addr,
9413 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009414{
9415 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02009416 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009417}
9418
9419void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009420 struct net_device *netdev, const u8 *addr,
9421 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009422{
Johannes Berge6d6e342009-07-01 21:26:47 +02009423 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9424 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009425}
9426
Samuel Ortizb23aa672009-07-01 21:26:54 +02009427void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9428 struct net_device *netdev, const u8 *bssid,
9429 const u8 *req_ie, size_t req_ie_len,
9430 const u8 *resp_ie, size_t resp_ie_len,
9431 u16 status, gfp_t gfp)
9432{
9433 struct sk_buff *msg;
9434 void *hdr;
9435
Thomas Graf58050fc2012-06-28 03:57:45 +00009436 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009437 if (!msg)
9438 return;
9439
9440 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9441 if (!hdr) {
9442 nlmsg_free(msg);
9443 return;
9444 }
9445
David S. Miller9360ffd2012-03-29 04:41:26 -04009446 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9447 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9448 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9449 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9450 (req_ie &&
9451 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9452 (resp_ie &&
9453 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9454 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009455
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009456 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009457
Johannes Berg463d0182009-07-14 00:33:35 +02009458 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9459 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009460 return;
9461
9462 nla_put_failure:
9463 genlmsg_cancel(msg, hdr);
9464 nlmsg_free(msg);
9465
9466}
9467
9468void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9469 struct net_device *netdev, const u8 *bssid,
9470 const u8 *req_ie, size_t req_ie_len,
9471 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9472{
9473 struct sk_buff *msg;
9474 void *hdr;
9475
Thomas Graf58050fc2012-06-28 03:57:45 +00009476 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009477 if (!msg)
9478 return;
9479
9480 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9481 if (!hdr) {
9482 nlmsg_free(msg);
9483 return;
9484 }
9485
David S. Miller9360ffd2012-03-29 04:41:26 -04009486 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9487 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9488 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9489 (req_ie &&
9490 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9491 (resp_ie &&
9492 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9493 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009494
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009495 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009496
Johannes Berg463d0182009-07-14 00:33:35 +02009497 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9498 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009499 return;
9500
9501 nla_put_failure:
9502 genlmsg_cancel(msg, hdr);
9503 nlmsg_free(msg);
9504
9505}
9506
9507void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9508 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02009509 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009510{
9511 struct sk_buff *msg;
9512 void *hdr;
9513
Thomas Graf58050fc2012-06-28 03:57:45 +00009514 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009515 if (!msg)
9516 return;
9517
9518 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9519 if (!hdr) {
9520 nlmsg_free(msg);
9521 return;
9522 }
9523
David S. Miller9360ffd2012-03-29 04:41:26 -04009524 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9525 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9526 (from_ap && reason &&
9527 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9528 (from_ap &&
9529 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9530 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9531 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009532
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009533 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009534
Johannes Berg463d0182009-07-14 00:33:35 +02009535 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9536 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009537 return;
9538
9539 nla_put_failure:
9540 genlmsg_cancel(msg, hdr);
9541 nlmsg_free(msg);
9542
9543}
9544
Johannes Berg04a773a2009-04-19 21:24:32 +02009545void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9546 struct net_device *netdev, const u8 *bssid,
9547 gfp_t gfp)
9548{
9549 struct sk_buff *msg;
9550 void *hdr;
9551
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009552 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009553 if (!msg)
9554 return;
9555
9556 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9557 if (!hdr) {
9558 nlmsg_free(msg);
9559 return;
9560 }
9561
David S. Miller9360ffd2012-03-29 04:41:26 -04009562 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9563 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9564 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9565 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009566
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009567 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009568
Johannes Berg463d0182009-07-14 00:33:35 +02009569 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9570 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009571 return;
9572
9573 nla_put_failure:
9574 genlmsg_cancel(msg, hdr);
9575 nlmsg_free(msg);
9576}
9577
Johannes Berg947add32013-02-22 22:05:20 +01009578void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
9579 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -07009580{
Johannes Berg947add32013-02-22 22:05:20 +01009581 struct wireless_dev *wdev = dev->ieee80211_ptr;
9582 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009583 struct sk_buff *msg;
9584 void *hdr;
9585
Johannes Berg947add32013-02-22 22:05:20 +01009586 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
9587 return;
9588
9589 trace_cfg80211_notify_new_peer_candidate(dev, addr);
9590
Javier Cardonac93b5e72011-04-07 15:08:34 -07009591 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9592 if (!msg)
9593 return;
9594
9595 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9596 if (!hdr) {
9597 nlmsg_free(msg);
9598 return;
9599 }
9600
David S. Miller9360ffd2012-03-29 04:41:26 -04009601 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +01009602 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9603 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009604 (ie_len && ie &&
9605 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9606 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009607
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009608 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009609
9610 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9611 nl80211_mlme_mcgrp.id, gfp);
9612 return;
9613
9614 nla_put_failure:
9615 genlmsg_cancel(msg, hdr);
9616 nlmsg_free(msg);
9617}
Johannes Berg947add32013-02-22 22:05:20 +01009618EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009619
Jouni Malinena3b8b052009-03-27 21:59:49 +02009620void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9621 struct net_device *netdev, const u8 *addr,
9622 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009623 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009624{
9625 struct sk_buff *msg;
9626 void *hdr;
9627
Johannes Berge6d6e342009-07-01 21:26:47 +02009628 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009629 if (!msg)
9630 return;
9631
9632 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9633 if (!hdr) {
9634 nlmsg_free(msg);
9635 return;
9636 }
9637
David S. Miller9360ffd2012-03-29 04:41:26 -04009638 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9639 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9640 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9641 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9642 (key_id != -1 &&
9643 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9644 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9645 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009646
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009647 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009648
Johannes Berg463d0182009-07-14 00:33:35 +02009649 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9650 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009651 return;
9652
9653 nla_put_failure:
9654 genlmsg_cancel(msg, hdr);
9655 nlmsg_free(msg);
9656}
9657
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009658void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9659 struct ieee80211_channel *channel_before,
9660 struct ieee80211_channel *channel_after)
9661{
9662 struct sk_buff *msg;
9663 void *hdr;
9664 struct nlattr *nl_freq;
9665
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009666 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009667 if (!msg)
9668 return;
9669
9670 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9671 if (!hdr) {
9672 nlmsg_free(msg);
9673 return;
9674 }
9675
9676 /*
9677 * Since we are applying the beacon hint to a wiphy we know its
9678 * wiphy_idx is valid
9679 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009680 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9681 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009682
9683 /* Before */
9684 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9685 if (!nl_freq)
9686 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009687 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009688 goto nla_put_failure;
9689 nla_nest_end(msg, nl_freq);
9690
9691 /* After */
9692 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9693 if (!nl_freq)
9694 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009695 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009696 goto nla_put_failure;
9697 nla_nest_end(msg, nl_freq);
9698
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009699 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009700
Johannes Berg463d0182009-07-14 00:33:35 +02009701 rcu_read_lock();
9702 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9703 GFP_ATOMIC);
9704 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009705
9706 return;
9707
9708nla_put_failure:
9709 genlmsg_cancel(msg, hdr);
9710 nlmsg_free(msg);
9711}
9712
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009713static void nl80211_send_remain_on_chan_event(
9714 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009715 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009716 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009717 unsigned int duration, gfp_t gfp)
9718{
9719 struct sk_buff *msg;
9720 void *hdr;
9721
9722 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9723 if (!msg)
9724 return;
9725
9726 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9727 if (!hdr) {
9728 nlmsg_free(msg);
9729 return;
9730 }
9731
David S. Miller9360ffd2012-03-29 04:41:26 -04009732 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009733 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9734 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009735 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009736 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009737 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9738 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009739 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9740 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009741
David S. Miller9360ffd2012-03-29 04:41:26 -04009742 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9743 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9744 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009745
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009746 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009747
9748 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9749 nl80211_mlme_mcgrp.id, gfp);
9750 return;
9751
9752 nla_put_failure:
9753 genlmsg_cancel(msg, hdr);
9754 nlmsg_free(msg);
9755}
9756
Johannes Berg947add32013-02-22 22:05:20 +01009757void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9758 struct ieee80211_channel *chan,
9759 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009760{
Johannes Berg947add32013-02-22 22:05:20 +01009761 struct wiphy *wiphy = wdev->wiphy;
9762 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9763
9764 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009765 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009766 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009767 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009768}
Johannes Berg947add32013-02-22 22:05:20 +01009769EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009770
Johannes Berg947add32013-02-22 22:05:20 +01009771void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9772 struct ieee80211_channel *chan,
9773 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009774{
Johannes Berg947add32013-02-22 22:05:20 +01009775 struct wiphy *wiphy = wdev->wiphy;
9776 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9777
9778 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009779 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009780 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009781}
Johannes Berg947add32013-02-22 22:05:20 +01009782EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009783
Johannes Berg947add32013-02-22 22:05:20 +01009784void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
9785 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +01009786{
Johannes Berg947add32013-02-22 22:05:20 +01009787 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9788 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +01009789 struct sk_buff *msg;
9790
Johannes Berg947add32013-02-22 22:05:20 +01009791 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
9792
Thomas Graf58050fc2012-06-28 03:57:45 +00009793 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009794 if (!msg)
9795 return;
9796
John W. Linville66266b32012-03-15 13:25:41 -04009797 if (nl80211_send_station(msg, 0, 0, 0,
9798 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009799 nlmsg_free(msg);
9800 return;
9801 }
9802
9803 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9804 nl80211_mlme_mcgrp.id, gfp);
9805}
Johannes Berg947add32013-02-22 22:05:20 +01009806EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +01009807
Johannes Berg947add32013-02-22 22:05:20 +01009808void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +02009809{
Johannes Berg947add32013-02-22 22:05:20 +01009810 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9811 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +02009812 struct sk_buff *msg;
9813 void *hdr;
9814
Johannes Berg947add32013-02-22 22:05:20 +01009815 trace_cfg80211_del_sta(dev, mac_addr);
9816
Thomas Graf58050fc2012-06-28 03:57:45 +00009817 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009818 if (!msg)
9819 return;
9820
9821 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9822 if (!hdr) {
9823 nlmsg_free(msg);
9824 return;
9825 }
9826
David S. Miller9360ffd2012-03-29 04:41:26 -04009827 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9828 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9829 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009830
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009831 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009832
9833 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9834 nl80211_mlme_mcgrp.id, gfp);
9835 return;
9836
9837 nla_put_failure:
9838 genlmsg_cancel(msg, hdr);
9839 nlmsg_free(msg);
9840}
Johannes Berg947add32013-02-22 22:05:20 +01009841EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +02009842
Johannes Berg947add32013-02-22 22:05:20 +01009843void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
9844 enum nl80211_connect_failed_reason reason,
9845 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309846{
Johannes Berg947add32013-02-22 22:05:20 +01009847 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9848 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309849 struct sk_buff *msg;
9850 void *hdr;
9851
9852 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9853 if (!msg)
9854 return;
9855
9856 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9857 if (!hdr) {
9858 nlmsg_free(msg);
9859 return;
9860 }
9861
9862 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9863 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9864 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9865 goto nla_put_failure;
9866
9867 genlmsg_end(msg, hdr);
9868
9869 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9870 nl80211_mlme_mcgrp.id, gfp);
9871 return;
9872
9873 nla_put_failure:
9874 genlmsg_cancel(msg, hdr);
9875 nlmsg_free(msg);
9876}
Johannes Berg947add32013-02-22 22:05:20 +01009877EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309878
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009879static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9880 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009881{
9882 struct wireless_dev *wdev = dev->ieee80211_ptr;
9883 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9884 struct sk_buff *msg;
9885 void *hdr;
9886 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009887 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009888
Eric W. Biederman15e47302012-09-07 20:12:54 +00009889 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009890 return false;
9891
9892 msg = nlmsg_new(100, gfp);
9893 if (!msg)
9894 return true;
9895
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009896 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009897 if (!hdr) {
9898 nlmsg_free(msg);
9899 return true;
9900 }
9901
David S. Miller9360ffd2012-03-29 04:41:26 -04009902 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9903 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9904 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9905 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009906
9907 err = genlmsg_end(msg, hdr);
9908 if (err < 0) {
9909 nlmsg_free(msg);
9910 return true;
9911 }
9912
Eric W. Biederman15e47302012-09-07 20:12:54 +00009913 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009914 return true;
9915
9916 nla_put_failure:
9917 genlmsg_cancel(msg, hdr);
9918 nlmsg_free(msg);
9919 return true;
9920}
9921
Johannes Berg947add32013-02-22 22:05:20 +01009922bool cfg80211_rx_spurious_frame(struct net_device *dev,
9923 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009924{
Johannes Berg947add32013-02-22 22:05:20 +01009925 struct wireless_dev *wdev = dev->ieee80211_ptr;
9926 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009927
Johannes Berg947add32013-02-22 22:05:20 +01009928 trace_cfg80211_rx_spurious_frame(dev, addr);
9929
9930 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9931 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
9932 trace_cfg80211_return_bool(false);
9933 return false;
9934 }
9935 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9936 addr, gfp);
9937 trace_cfg80211_return_bool(ret);
9938 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009939}
Johannes Berg947add32013-02-22 22:05:20 +01009940EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
9941
9942bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
9943 const u8 *addr, gfp_t gfp)
9944{
9945 struct wireless_dev *wdev = dev->ieee80211_ptr;
9946 bool ret;
9947
9948 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
9949
9950 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9951 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
9952 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
9953 trace_cfg80211_return_bool(false);
9954 return false;
9955 }
9956 ret = __nl80211_unexpected_frame(dev,
9957 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9958 addr, gfp);
9959 trace_cfg80211_return_bool(ret);
9960 return ret;
9961}
9962EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009963
Johannes Berg2e161f72010-08-12 15:38:38 +02009964int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009965 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009966 int freq, int sig_dbm,
9967 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009968{
Johannes Berg71bbc992012-06-15 15:30:18 +02009969 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009970 struct sk_buff *msg;
9971 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009972
9973 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9974 if (!msg)
9975 return -ENOMEM;
9976
Johannes Berg2e161f72010-08-12 15:38:38 +02009977 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009978 if (!hdr) {
9979 nlmsg_free(msg);
9980 return -ENOMEM;
9981 }
9982
David S. Miller9360ffd2012-03-29 04:41:26 -04009983 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009984 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9985 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +03009986 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009987 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9988 (sig_dbm &&
9989 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9990 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9991 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009992
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009993 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009994
Eric W. Biederman15e47302012-09-07 20:12:54 +00009995 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009996
9997 nla_put_failure:
9998 genlmsg_cancel(msg, hdr);
9999 nlmsg_free(msg);
10000 return -ENOBUFS;
10001}
10002
Johannes Berg947add32013-02-22 22:05:20 +010010003void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10004 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010005{
Johannes Berg947add32013-02-22 22:05:20 +010010006 struct wiphy *wiphy = wdev->wiphy;
10007 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010008 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010009 struct sk_buff *msg;
10010 void *hdr;
10011
Johannes Berg947add32013-02-22 22:05:20 +010010012 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10013
Jouni Malinen026331c2010-02-15 12:53:10 +020010014 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10015 if (!msg)
10016 return;
10017
Johannes Berg2e161f72010-08-12 15:38:38 +020010018 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010019 if (!hdr) {
10020 nlmsg_free(msg);
10021 return;
10022 }
10023
David S. Miller9360ffd2012-03-29 04:41:26 -040010024 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010025 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10026 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010027 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010028 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10029 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10030 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10031 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010032
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010033 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010034
Michal Kaziorc8942c62013-06-25 09:17:17 +020010035 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10036 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020010037 return;
10038
10039 nla_put_failure:
10040 genlmsg_cancel(msg, hdr);
10041 nlmsg_free(msg);
10042}
Johannes Berg947add32013-02-22 22:05:20 +010010043EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020010044
Johannes Berg947add32013-02-22 22:05:20 +010010045void cfg80211_cqm_rssi_notify(struct net_device *dev,
10046 enum nl80211_cqm_rssi_threshold_event rssi_event,
10047 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010048{
Johannes Berg947add32013-02-22 22:05:20 +010010049 struct wireless_dev *wdev = dev->ieee80211_ptr;
10050 struct wiphy *wiphy = wdev->wiphy;
10051 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010052 struct sk_buff *msg;
10053 struct nlattr *pinfoattr;
10054 void *hdr;
10055
Johannes Berg947add32013-02-22 22:05:20 +010010056 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10057
Thomas Graf58050fc2012-06-28 03:57:45 +000010058 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010059 if (!msg)
10060 return;
10061
10062 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10063 if (!hdr) {
10064 nlmsg_free(msg);
10065 return;
10066 }
10067
David S. Miller9360ffd2012-03-29 04:41:26 -040010068 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010069 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040010070 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010071
10072 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10073 if (!pinfoattr)
10074 goto nla_put_failure;
10075
David S. Miller9360ffd2012-03-29 04:41:26 -040010076 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10077 rssi_event))
10078 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010079
10080 nla_nest_end(msg, pinfoattr);
10081
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010082 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010083
10084 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10085 nl80211_mlme_mcgrp.id, gfp);
10086 return;
10087
10088 nla_put_failure:
10089 genlmsg_cancel(msg, hdr);
10090 nlmsg_free(msg);
10091}
Johannes Berg947add32013-02-22 22:05:20 +010010092EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010093
Johannes Berg947add32013-02-22 22:05:20 +010010094static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10095 struct net_device *netdev, const u8 *bssid,
10096 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010097{
10098 struct sk_buff *msg;
10099 struct nlattr *rekey_attr;
10100 void *hdr;
10101
Thomas Graf58050fc2012-06-28 03:57:45 +000010102 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010103 if (!msg)
10104 return;
10105
10106 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10107 if (!hdr) {
10108 nlmsg_free(msg);
10109 return;
10110 }
10111
David S. Miller9360ffd2012-03-29 04:41:26 -040010112 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10113 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10114 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10115 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010116
10117 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10118 if (!rekey_attr)
10119 goto nla_put_failure;
10120
David S. Miller9360ffd2012-03-29 04:41:26 -040010121 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10122 NL80211_REPLAY_CTR_LEN, replay_ctr))
10123 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010124
10125 nla_nest_end(msg, rekey_attr);
10126
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010127 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010128
10129 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10130 nl80211_mlme_mcgrp.id, gfp);
10131 return;
10132
10133 nla_put_failure:
10134 genlmsg_cancel(msg, hdr);
10135 nlmsg_free(msg);
10136}
10137
Johannes Berg947add32013-02-22 22:05:20 +010010138void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10139 const u8 *replay_ctr, gfp_t gfp)
10140{
10141 struct wireless_dev *wdev = dev->ieee80211_ptr;
10142 struct wiphy *wiphy = wdev->wiphy;
10143 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10144
10145 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10146 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10147}
10148EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10149
10150static void
10151nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10152 struct net_device *netdev, int index,
10153 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010154{
10155 struct sk_buff *msg;
10156 struct nlattr *attr;
10157 void *hdr;
10158
Thomas Graf58050fc2012-06-28 03:57:45 +000010159 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010160 if (!msg)
10161 return;
10162
10163 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10164 if (!hdr) {
10165 nlmsg_free(msg);
10166 return;
10167 }
10168
David S. Miller9360ffd2012-03-29 04:41:26 -040010169 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10170 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10171 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010172
10173 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10174 if (!attr)
10175 goto nla_put_failure;
10176
David S. Miller9360ffd2012-03-29 04:41:26 -040010177 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10178 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10179 (preauth &&
10180 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10181 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010182
10183 nla_nest_end(msg, attr);
10184
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010185 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010186
10187 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10188 nl80211_mlme_mcgrp.id, gfp);
10189 return;
10190
10191 nla_put_failure:
10192 genlmsg_cancel(msg, hdr);
10193 nlmsg_free(msg);
10194}
10195
Johannes Berg947add32013-02-22 22:05:20 +010010196void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10197 const u8 *bssid, bool preauth, gfp_t gfp)
10198{
10199 struct wireless_dev *wdev = dev->ieee80211_ptr;
10200 struct wiphy *wiphy = wdev->wiphy;
10201 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10202
10203 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10204 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10205}
10206EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10207
10208static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10209 struct net_device *netdev,
10210 struct cfg80211_chan_def *chandef,
10211 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070010212{
10213 struct sk_buff *msg;
10214 void *hdr;
10215
Thomas Graf58050fc2012-06-28 03:57:45 +000010216 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070010217 if (!msg)
10218 return;
10219
10220 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10221 if (!hdr) {
10222 nlmsg_free(msg);
10223 return;
10224 }
10225
Johannes Berg683b6d32012-11-08 21:25:48 +010010226 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10227 goto nla_put_failure;
10228
10229 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040010230 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070010231
10232 genlmsg_end(msg, hdr);
10233
10234 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10235 nl80211_mlme_mcgrp.id, gfp);
10236 return;
10237
10238 nla_put_failure:
10239 genlmsg_cancel(msg, hdr);
10240 nlmsg_free(msg);
10241}
10242
Johannes Berg947add32013-02-22 22:05:20 +010010243void cfg80211_ch_switch_notify(struct net_device *dev,
10244 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070010245{
Johannes Berg947add32013-02-22 22:05:20 +010010246 struct wireless_dev *wdev = dev->ieee80211_ptr;
10247 struct wiphy *wiphy = wdev->wiphy;
10248 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10249
10250 trace_cfg80211_ch_switch_notify(dev, chandef);
10251
10252 wdev_lock(wdev);
10253
10254 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10255 wdev->iftype != NL80211_IFTYPE_P2P_GO))
10256 goto out;
10257
10258 wdev->channel = chandef->chan;
10259 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
10260out:
10261 wdev_unlock(wdev);
10262 return;
10263}
10264EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10265
10266void cfg80211_cqm_txe_notify(struct net_device *dev,
10267 const u8 *peer, u32 num_packets,
10268 u32 rate, u32 intvl, gfp_t gfp)
10269{
10270 struct wireless_dev *wdev = dev->ieee80211_ptr;
10271 struct wiphy *wiphy = wdev->wiphy;
10272 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010273 struct sk_buff *msg;
10274 struct nlattr *pinfoattr;
10275 void *hdr;
10276
10277 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10278 if (!msg)
10279 return;
10280
10281 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10282 if (!hdr) {
10283 nlmsg_free(msg);
10284 return;
10285 }
10286
10287 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010288 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070010289 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10290 goto nla_put_failure;
10291
10292 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10293 if (!pinfoattr)
10294 goto nla_put_failure;
10295
10296 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10297 goto nla_put_failure;
10298
10299 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10300 goto nla_put_failure;
10301
10302 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10303 goto nla_put_failure;
10304
10305 nla_nest_end(msg, pinfoattr);
10306
10307 genlmsg_end(msg, hdr);
10308
10309 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10310 nl80211_mlme_mcgrp.id, gfp);
10311 return;
10312
10313 nla_put_failure:
10314 genlmsg_cancel(msg, hdr);
10315 nlmsg_free(msg);
10316}
Johannes Berg947add32013-02-22 22:05:20 +010010317EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010318
10319void
Simon Wunderlich04f39042013-02-08 18:16:19 +010010320nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10321 struct cfg80211_chan_def *chandef,
10322 enum nl80211_radar_event event,
10323 struct net_device *netdev, gfp_t gfp)
10324{
10325 struct sk_buff *msg;
10326 void *hdr;
10327
10328 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10329 if (!msg)
10330 return;
10331
10332 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
10333 if (!hdr) {
10334 nlmsg_free(msg);
10335 return;
10336 }
10337
10338 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10339 goto nla_put_failure;
10340
10341 /* NOP and radar events don't need a netdev parameter */
10342 if (netdev) {
10343 struct wireless_dev *wdev = netdev->ieee80211_ptr;
10344
10345 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10346 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10347 goto nla_put_failure;
10348 }
10349
10350 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
10351 goto nla_put_failure;
10352
10353 if (nl80211_send_chandef(msg, chandef))
10354 goto nla_put_failure;
10355
10356 if (genlmsg_end(msg, hdr) < 0) {
10357 nlmsg_free(msg);
10358 return;
10359 }
10360
10361 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10362 nl80211_mlme_mcgrp.id, gfp);
10363 return;
10364
10365 nla_put_failure:
10366 genlmsg_cancel(msg, hdr);
10367 nlmsg_free(msg);
10368}
10369
Johannes Berg947add32013-02-22 22:05:20 +010010370void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10371 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010010372{
Johannes Berg947add32013-02-22 22:05:20 +010010373 struct wireless_dev *wdev = dev->ieee80211_ptr;
10374 struct wiphy *wiphy = wdev->wiphy;
10375 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010376 struct sk_buff *msg;
10377 struct nlattr *pinfoattr;
10378 void *hdr;
10379
Johannes Berg947add32013-02-22 22:05:20 +010010380 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
10381
Thomas Graf58050fc2012-06-28 03:57:45 +000010382 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010383 if (!msg)
10384 return;
10385
10386 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10387 if (!hdr) {
10388 nlmsg_free(msg);
10389 return;
10390 }
10391
David S. Miller9360ffd2012-03-29 04:41:26 -040010392 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010393 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010394 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10395 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010396
10397 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10398 if (!pinfoattr)
10399 goto nla_put_failure;
10400
David S. Miller9360ffd2012-03-29 04:41:26 -040010401 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10402 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010403
10404 nla_nest_end(msg, pinfoattr);
10405
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010406 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010407
10408 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10409 nl80211_mlme_mcgrp.id, gfp);
10410 return;
10411
10412 nla_put_failure:
10413 genlmsg_cancel(msg, hdr);
10414 nlmsg_free(msg);
10415}
Johannes Berg947add32013-02-22 22:05:20 +010010416EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010417
Johannes Berg7f6cf312011-11-04 11:18:15 +010010418void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10419 u64 cookie, bool acked, gfp_t gfp)
10420{
10421 struct wireless_dev *wdev = dev->ieee80211_ptr;
10422 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10423 struct sk_buff *msg;
10424 void *hdr;
10425 int err;
10426
Beni Lev4ee3e062012-08-27 12:49:39 +030010427 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10428
Thomas Graf58050fc2012-06-28 03:57:45 +000010429 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030010430
Johannes Berg7f6cf312011-11-04 11:18:15 +010010431 if (!msg)
10432 return;
10433
10434 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10435 if (!hdr) {
10436 nlmsg_free(msg);
10437 return;
10438 }
10439
David S. Miller9360ffd2012-03-29 04:41:26 -040010440 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10441 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10442 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10443 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10444 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10445 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010446
10447 err = genlmsg_end(msg, hdr);
10448 if (err < 0) {
10449 nlmsg_free(msg);
10450 return;
10451 }
10452
10453 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10454 nl80211_mlme_mcgrp.id, gfp);
10455 return;
10456
10457 nla_put_failure:
10458 genlmsg_cancel(msg, hdr);
10459 nlmsg_free(msg);
10460}
10461EXPORT_SYMBOL(cfg80211_probe_status);
10462
Johannes Berg5e760232011-11-04 11:18:17 +010010463void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10464 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070010465 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010010466{
10467 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10468 struct sk_buff *msg;
10469 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070010470 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010010471
Beni Lev4ee3e062012-08-27 12:49:39 +030010472 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10473
Ben Greear37c73b52012-10-26 14:49:25 -070010474 spin_lock_bh(&rdev->beacon_registrations_lock);
10475 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10476 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10477 if (!msg) {
10478 spin_unlock_bh(&rdev->beacon_registrations_lock);
10479 return;
10480 }
Johannes Berg5e760232011-11-04 11:18:17 +010010481
Ben Greear37c73b52012-10-26 14:49:25 -070010482 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10483 if (!hdr)
10484 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010010485
Ben Greear37c73b52012-10-26 14:49:25 -070010486 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10487 (freq &&
10488 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10489 (sig_dbm &&
10490 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10491 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10492 goto nla_put_failure;
10493
10494 genlmsg_end(msg, hdr);
10495
10496 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010010497 }
Ben Greear37c73b52012-10-26 14:49:25 -070010498 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010499 return;
10500
10501 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070010502 spin_unlock_bh(&rdev->beacon_registrations_lock);
10503 if (hdr)
10504 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010010505 nlmsg_free(msg);
10506}
10507EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10508
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010509#ifdef CONFIG_PM
10510void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10511 struct cfg80211_wowlan_wakeup *wakeup,
10512 gfp_t gfp)
10513{
10514 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10515 struct sk_buff *msg;
10516 void *hdr;
10517 int err, size = 200;
10518
10519 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10520
10521 if (wakeup)
10522 size += wakeup->packet_present_len;
10523
10524 msg = nlmsg_new(size, gfp);
10525 if (!msg)
10526 return;
10527
10528 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10529 if (!hdr)
10530 goto free_msg;
10531
10532 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10533 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10534 goto free_msg;
10535
10536 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10537 wdev->netdev->ifindex))
10538 goto free_msg;
10539
10540 if (wakeup) {
10541 struct nlattr *reasons;
10542
10543 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10544
10545 if (wakeup->disconnect &&
10546 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10547 goto free_msg;
10548 if (wakeup->magic_pkt &&
10549 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10550 goto free_msg;
10551 if (wakeup->gtk_rekey_failure &&
10552 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10553 goto free_msg;
10554 if (wakeup->eap_identity_req &&
10555 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10556 goto free_msg;
10557 if (wakeup->four_way_handshake &&
10558 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10559 goto free_msg;
10560 if (wakeup->rfkill_release &&
10561 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10562 goto free_msg;
10563
10564 if (wakeup->pattern_idx >= 0 &&
10565 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10566 wakeup->pattern_idx))
10567 goto free_msg;
10568
Johannes Berg2a0e0472013-01-23 22:57:40 +010010569 if (wakeup->tcp_match)
10570 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10571
10572 if (wakeup->tcp_connlost)
10573 nla_put_flag(msg,
10574 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10575
10576 if (wakeup->tcp_nomoretokens)
10577 nla_put_flag(msg,
10578 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10579
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010580 if (wakeup->packet) {
10581 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10582 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10583
10584 if (!wakeup->packet_80211) {
10585 pkt_attr =
10586 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10587 len_attr =
10588 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10589 }
10590
10591 if (wakeup->packet_len &&
10592 nla_put_u32(msg, len_attr, wakeup->packet_len))
10593 goto free_msg;
10594
10595 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10596 wakeup->packet))
10597 goto free_msg;
10598 }
10599
10600 nla_nest_end(msg, reasons);
10601 }
10602
10603 err = genlmsg_end(msg, hdr);
10604 if (err < 0)
10605 goto free_msg;
10606
10607 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10608 nl80211_mlme_mcgrp.id, gfp);
10609 return;
10610
10611 free_msg:
10612 nlmsg_free(msg);
10613}
10614EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10615#endif
10616
Jouni Malinen3475b092012-11-16 22:49:57 +020010617void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10618 enum nl80211_tdls_operation oper,
10619 u16 reason_code, gfp_t gfp)
10620{
10621 struct wireless_dev *wdev = dev->ieee80211_ptr;
10622 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10623 struct sk_buff *msg;
10624 void *hdr;
10625 int err;
10626
10627 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10628 reason_code);
10629
10630 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10631 if (!msg)
10632 return;
10633
10634 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10635 if (!hdr) {
10636 nlmsg_free(msg);
10637 return;
10638 }
10639
10640 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10641 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10642 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10643 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10644 (reason_code > 0 &&
10645 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10646 goto nla_put_failure;
10647
10648 err = genlmsg_end(msg, hdr);
10649 if (err < 0) {
10650 nlmsg_free(msg);
10651 return;
10652 }
10653
10654 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10655 nl80211_mlme_mcgrp.id, gfp);
10656 return;
10657
10658 nla_put_failure:
10659 genlmsg_cancel(msg, hdr);
10660 nlmsg_free(msg);
10661}
10662EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10663
Jouni Malinen026331c2010-02-15 12:53:10 +020010664static int nl80211_netlink_notify(struct notifier_block * nb,
10665 unsigned long state,
10666 void *_notify)
10667{
10668 struct netlink_notify *notify = _notify;
10669 struct cfg80211_registered_device *rdev;
10670 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010671 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010672
10673 if (state != NETLINK_URELEASE)
10674 return NOTIFY_DONE;
10675
10676 rcu_read_lock();
10677
Johannes Berg5e760232011-11-04 11:18:17 +010010678 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010679 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010680 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010681
10682 spin_lock_bh(&rdev->beacon_registrations_lock);
10683 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10684 list) {
10685 if (reg->nlportid == notify->portid) {
10686 list_del(&reg->list);
10687 kfree(reg);
10688 break;
10689 }
10690 }
10691 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010692 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010693
10694 rcu_read_unlock();
10695
10696 return NOTIFY_DONE;
10697}
10698
10699static struct notifier_block nl80211_netlink_notifier = {
10700 .notifier_call = nl80211_netlink_notify,
10701};
10702
Jouni Malinen355199e2013-02-27 17:14:27 +020010703void cfg80211_ft_event(struct net_device *netdev,
10704 struct cfg80211_ft_event_params *ft_event)
10705{
10706 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
10707 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10708 struct sk_buff *msg;
10709 void *hdr;
10710 int err;
10711
10712 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
10713
10714 if (!ft_event->target_ap)
10715 return;
10716
10717 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10718 if (!msg)
10719 return;
10720
10721 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
10722 if (!hdr) {
10723 nlmsg_free(msg);
10724 return;
10725 }
10726
10727 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
10728 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
10729 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
10730 if (ft_event->ies)
10731 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
10732 if (ft_event->ric_ies)
10733 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
10734 ft_event->ric_ies);
10735
10736 err = genlmsg_end(msg, hdr);
10737 if (err < 0) {
10738 nlmsg_free(msg);
10739 return;
10740 }
10741
10742 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10743 nl80211_mlme_mcgrp.id, GFP_KERNEL);
10744}
10745EXPORT_SYMBOL(cfg80211_ft_event);
10746
Arend van Spriel5de17982013-04-18 15:49:00 +020010747void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
10748{
10749 struct cfg80211_registered_device *rdev;
10750 struct sk_buff *msg;
10751 void *hdr;
10752 u32 nlportid;
10753
10754 rdev = wiphy_to_dev(wdev->wiphy);
10755 if (!rdev->crit_proto_nlportid)
10756 return;
10757
10758 nlportid = rdev->crit_proto_nlportid;
10759 rdev->crit_proto_nlportid = 0;
10760
10761 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10762 if (!msg)
10763 return;
10764
10765 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
10766 if (!hdr)
10767 goto nla_put_failure;
10768
10769 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10770 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10771 goto nla_put_failure;
10772
10773 genlmsg_end(msg, hdr);
10774
10775 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
10776 return;
10777
10778 nla_put_failure:
10779 if (hdr)
10780 genlmsg_cancel(msg, hdr);
10781 nlmsg_free(msg);
10782
10783}
10784EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
10785
Johannes Berg55682962007-09-20 13:09:35 -040010786/* initialisation/exit functions */
10787
10788int nl80211_init(void)
10789{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010790 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010791
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010792 err = genl_register_family_with_ops(&nl80211_fam,
10793 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010794 if (err)
10795 return err;
10796
Johannes Berg55682962007-09-20 13:09:35 -040010797 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10798 if (err)
10799 goto err_out;
10800
Johannes Berg2a519312009-02-10 21:25:55 +010010801 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10802 if (err)
10803 goto err_out;
10804
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010805 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10806 if (err)
10807 goto err_out;
10808
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010809 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10810 if (err)
10811 goto err_out;
10812
Johannes Bergaff89a92009-07-01 21:26:51 +020010813#ifdef CONFIG_NL80211_TESTMODE
10814 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10815 if (err)
10816 goto err_out;
10817#endif
10818
Jouni Malinen026331c2010-02-15 12:53:10 +020010819 err = netlink_register_notifier(&nl80211_netlink_notifier);
10820 if (err)
10821 goto err_out;
10822
Johannes Berg55682962007-09-20 13:09:35 -040010823 return 0;
10824 err_out:
10825 genl_unregister_family(&nl80211_fam);
10826 return err;
10827}
10828
10829void nl80211_exit(void)
10830{
Jouni Malinen026331c2010-02-15 12:53:10 +020010831 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010832 genl_unregister_family(&nl80211_fam);
10833}