blob: d4fece3bb18a8215ce30fd55e748e7f59b5bcee1 [file] [log] [blame]
Jouni Malinen6039f6d2009-03-19 13:39:21 +02001/*
2 * cfg80211 MLME SAP interface
3 *
4 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
Felix Fietkauc6fb08a2012-03-18 22:58:04 +01009#include <linux/etherdevice.h>
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010#include <linux/netdevice.h>
11#include <linux/nl80211.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Johannes Berga9a11622009-07-27 12:01:53 +020013#include <linux/wireless.h>
Jouni Malinen6039f6d2009-03-19 13:39:21 +020014#include <net/cfg80211.h>
Johannes Berga9a11622009-07-27 12:01:53 +020015#include <net/iw_handler.h>
Jouni Malinen6039f6d2009-03-19 13:39:21 +020016#include "core.h"
17#include "nl80211.h"
18
Johannes Bergcb0b4be2009-07-07 03:56:07 +020019void cfg80211_send_rx_auth(struct net_device *dev, const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020020{
Johannes Berg19957bb2009-07-02 17:20:43 +020021 struct wireless_dev *wdev = dev->ieee80211_ptr;
22 struct wiphy *wiphy = wdev->wiphy;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020023 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +020024
Johannes Berg667503d2009-07-07 03:56:11 +020025 wdev_lock(wdev);
Johannes Bergcb0b4be2009-07-07 03:56:07 +020026
Johannes Berg95de8172012-01-20 13:55:25 +010027 nl80211_send_rx_auth(rdev, dev, buf, len, GFP_KERNEL);
28 cfg80211_sme_rx_auth(dev, buf, len);
Johannes Berg667503d2009-07-07 03:56:11 +020029
30 wdev_unlock(wdev);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020031}
32EXPORT_SYMBOL(cfg80211_send_rx_auth);
33
Johannes Berg95de8172012-01-20 13:55:25 +010034void cfg80211_send_rx_assoc(struct net_device *dev, struct cfg80211_bss *bss,
35 const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020036{
Johannes Berg6829c872009-07-02 09:13:27 +020037 u16 status_code;
38 struct wireless_dev *wdev = dev->ieee80211_ptr;
39 struct wiphy *wiphy = wdev->wiphy;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020040 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020041 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
42 u8 *ie = mgmt->u.assoc_resp.variable;
Johannes Berg95de8172012-01-20 13:55:25 +010043 int ieoffs = offsetof(struct ieee80211_mgmt, u.assoc_resp.variable);
Johannes Berg6829c872009-07-02 09:13:27 +020044
Johannes Berg667503d2009-07-07 03:56:11 +020045 wdev_lock(wdev);
Johannes Bergcb0b4be2009-07-07 03:56:07 +020046
Johannes Berg6829c872009-07-02 09:13:27 +020047 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
48
Johannes Bergf401a6f2009-08-07 14:51:05 +020049 /*
50 * This is a bit of a hack, we don't notify userspace of
51 * a (re-)association reply if we tried to send a reassoc
52 * and got a reject -- we only try again with an assoc
53 * frame instead of reassoc.
54 */
55 if (status_code != WLAN_STATUS_SUCCESS && wdev->conn &&
Johannes Berg95de8172012-01-20 13:55:25 +010056 cfg80211_sme_failed_reassoc(wdev)) {
57 cfg80211_put_bss(bss);
Johannes Bergf401a6f2009-08-07 14:51:05 +020058 goto out;
Johannes Berg95de8172012-01-20 13:55:25 +010059 }
Johannes Bergf401a6f2009-08-07 14:51:05 +020060
Johannes Bergcb0b4be2009-07-07 03:56:07 +020061 nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL);
Johannes Berg6829c872009-07-02 09:13:27 +020062
Johannes Berg95de8172012-01-20 13:55:25 +010063 if (status_code != WLAN_STATUS_SUCCESS && wdev->conn) {
Johannes Berg7d930bc2009-10-20 15:08:53 +090064 cfg80211_sme_failed_assoc(wdev);
Johannes Berg7d930bc2009-10-20 15:08:53 +090065 /*
66 * do not call connect_result() now because the
67 * sme will schedule work that does it later.
68 */
Johannes Berg95de8172012-01-20 13:55:25 +010069 cfg80211_put_bss(bss);
Johannes Berg7d930bc2009-10-20 15:08:53 +090070 goto out;
Johannes Bergdf7fc0f2009-07-29 11:23:49 +020071 }
72
Johannes Bergea416a72009-08-17 12:22:14 +020073 if (!wdev->conn && wdev->sme_state == CFG80211_SME_IDLE) {
74 /*
75 * This is for the userspace SME, the CONNECTING
76 * state will be changed to CONNECTED by
77 * __cfg80211_connect_result() below.
78 */
79 wdev->sme_state = CFG80211_SME_CONNECTING;
80 }
81
Johannes Berg95de8172012-01-20 13:55:25 +010082 /* this consumes the bss reference */
Johannes Bergdf7fc0f2009-07-29 11:23:49 +020083 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, ie, len - ieoffs,
84 status_code,
Johannes Berg95de8172012-01-20 13:55:25 +010085 status_code == WLAN_STATUS_SUCCESS, bss);
Johannes Bergf401a6f2009-08-07 14:51:05 +020086 out:
Johannes Berg667503d2009-07-07 03:56:11 +020087 wdev_unlock(wdev);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020088}
89EXPORT_SYMBOL(cfg80211_send_rx_assoc);
90
Holger Schurigce470612009-10-13 13:28:13 +020091void __cfg80211_send_deauth(struct net_device *dev,
Johannes Berg667503d2009-07-07 03:56:11 +020092 const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020093{
Johannes Berg6829c872009-07-02 09:13:27 +020094 struct wireless_dev *wdev = dev->ieee80211_ptr;
95 struct wiphy *wiphy = wdev->wiphy;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020096 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020097 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
Johannes Berg19957bb2009-07-02 17:20:43 +020098 const u8 *bssid = mgmt->bssid;
Johannes Berg95de8172012-01-20 13:55:25 +010099 bool was_current = false;
Johannes Berg6829c872009-07-02 09:13:27 +0200100
Johannes Berg667503d2009-07-07 03:56:11 +0200101 ASSERT_WDEV_LOCK(wdev);
Johannes Bergcb0b4be2009-07-07 03:56:07 +0200102
Johannes Berg19957bb2009-07-02 17:20:43 +0200103 if (wdev->current_bss &&
Joe Perchesac422d32012-05-08 18:56:55 +0000104 ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200105 cfg80211_unhold_bss(wdev->current_bss);
106 cfg80211_put_bss(&wdev->current_bss->pub);
107 wdev->current_bss = NULL;
Johannes Berg3f3b6a82010-08-05 10:20:27 +0200108 was_current = true;
Johannes Berg19957bb2009-07-02 17:20:43 +0200109 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200110
Johannes Berg5fba4af32009-12-02 12:43:42 +0100111 nl80211_send_deauth(rdev, dev, buf, len, GFP_KERNEL);
112
Johannes Berg3f3b6a82010-08-05 10:20:27 +0200113 if (wdev->sme_state == CFG80211_SME_CONNECTED && was_current) {
Johannes Berg6829c872009-07-02 09:13:27 +0200114 u16 reason_code;
115 bool from_ap;
116
117 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
118
Joe Perchesac422d32012-05-08 18:56:55 +0000119 from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
Johannes Berg667503d2009-07-07 03:56:11 +0200120 __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
Johannes Berg6829c872009-07-02 09:13:27 +0200121 } else if (wdev->sme_state == CFG80211_SME_CONNECTING) {
Johannes Berg667503d2009-07-07 03:56:11 +0200122 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
123 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200124 false, NULL);
Johannes Berg667503d2009-07-07 03:56:11 +0200125 }
126}
Holger Schurigce470612009-10-13 13:28:13 +0200127EXPORT_SYMBOL(__cfg80211_send_deauth);
Johannes Berg667503d2009-07-07 03:56:11 +0200128
Holger Schurigce470612009-10-13 13:28:13 +0200129void cfg80211_send_deauth(struct net_device *dev, const u8 *buf, size_t len)
Johannes Berg667503d2009-07-07 03:56:11 +0200130{
131 struct wireless_dev *wdev = dev->ieee80211_ptr;
132
Holger Schurigce470612009-10-13 13:28:13 +0200133 wdev_lock(wdev);
134 __cfg80211_send_deauth(dev, buf, len);
135 wdev_unlock(wdev);
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200136}
Jouni Malinen53b46b82009-03-27 20:53:56 +0200137EXPORT_SYMBOL(cfg80211_send_deauth);
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200138
Holger Schurigce470612009-10-13 13:28:13 +0200139void __cfg80211_send_disassoc(struct net_device *dev,
Johannes Berg667503d2009-07-07 03:56:11 +0200140 const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200141{
Johannes Berg6829c872009-07-02 09:13:27 +0200142 struct wireless_dev *wdev = dev->ieee80211_ptr;
143 struct wiphy *wiphy = wdev->wiphy;
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200144 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200145 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
Johannes Berg19957bb2009-07-02 17:20:43 +0200146 const u8 *bssid = mgmt->bssid;
Johannes Berg19957bb2009-07-02 17:20:43 +0200147 u16 reason_code;
148 bool from_ap;
Johannes Berg6829c872009-07-02 09:13:27 +0200149
Johannes Berg596a07c2009-07-11 00:17:32 +0200150 ASSERT_WDEV_LOCK(wdev);
Johannes Bergcb0b4be2009-07-07 03:56:07 +0200151
152 nl80211_send_disassoc(rdev, dev, buf, len, GFP_KERNEL);
Johannes Berg6829c872009-07-02 09:13:27 +0200153
Johannes Berg596a07c2009-07-11 00:17:32 +0200154 if (wdev->sme_state != CFG80211_SME_CONNECTED)
155 return;
Johannes Berg6829c872009-07-02 09:13:27 +0200156
Johannes Berg19957bb2009-07-02 17:20:43 +0200157 if (wdev->current_bss &&
Joe Perchesac422d32012-05-08 18:56:55 +0000158 ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
Johannes Berg95de8172012-01-20 13:55:25 +0100159 cfg80211_sme_disassoc(dev, wdev->current_bss);
160 cfg80211_unhold_bss(wdev->current_bss);
161 cfg80211_put_bss(&wdev->current_bss->pub);
162 wdev->current_bss = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200163 } else
164 WARN_ON(1);
Johannes Berg6829c872009-07-02 09:13:27 +0200165
Johannes Berg6829c872009-07-02 09:13:27 +0200166
Johannes Berg19957bb2009-07-02 17:20:43 +0200167 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
168
Joe Perchesac422d32012-05-08 18:56:55 +0000169 from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
Johannes Berg667503d2009-07-07 03:56:11 +0200170 __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
Johannes Berg667503d2009-07-07 03:56:11 +0200171}
Holger Schurigce470612009-10-13 13:28:13 +0200172EXPORT_SYMBOL(__cfg80211_send_disassoc);
Johannes Berg667503d2009-07-07 03:56:11 +0200173
Holger Schurigce470612009-10-13 13:28:13 +0200174void cfg80211_send_disassoc(struct net_device *dev, const u8 *buf, size_t len)
Johannes Berg667503d2009-07-07 03:56:11 +0200175{
176 struct wireless_dev *wdev = dev->ieee80211_ptr;
177
Holger Schurigce470612009-10-13 13:28:13 +0200178 wdev_lock(wdev);
179 __cfg80211_send_disassoc(dev, buf, len);
180 wdev_unlock(wdev);
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200181}
Jouni Malinen53b46b82009-03-27 20:53:56 +0200182EXPORT_SYMBOL(cfg80211_send_disassoc);
Jouni Malinena3b8b052009-03-27 21:59:49 +0200183
Jouni Malinencf4e5942010-12-16 00:52:40 +0200184void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
185 size_t len)
186{
187 struct wireless_dev *wdev = dev->ieee80211_ptr;
188 struct wiphy *wiphy = wdev->wiphy;
189 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
190
191 nl80211_send_unprot_deauth(rdev, dev, buf, len, GFP_ATOMIC);
192}
193EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
194
195void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
196 size_t len)
197{
198 struct wireless_dev *wdev = dev->ieee80211_ptr;
199 struct wiphy *wiphy = wdev->wiphy;
200 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
201
202 nl80211_send_unprot_disassoc(rdev, dev, buf, len, GFP_ATOMIC);
203}
204EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
205
Johannes Berga58ce432009-11-19 12:45:42 +0100206void cfg80211_send_auth_timeout(struct net_device *dev, const u8 *addr)
207{
208 struct wireless_dev *wdev = dev->ieee80211_ptr;
209 struct wiphy *wiphy = wdev->wiphy;
210 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
211
212 wdev_lock(wdev);
213
214 nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
215 if (wdev->sme_state == CFG80211_SME_CONNECTING)
216 __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
217 WLAN_STATUS_UNSPECIFIED_FAILURE,
218 false, NULL);
219
Johannes Berg667503d2009-07-07 03:56:11 +0200220 wdev_unlock(wdev);
Jouni Malinen1965c852009-04-22 21:38:25 +0300221}
222EXPORT_SYMBOL(cfg80211_send_auth_timeout);
223
Johannes Bergcb0b4be2009-07-07 03:56:07 +0200224void cfg80211_send_assoc_timeout(struct net_device *dev, const u8 *addr)
Jouni Malinen1965c852009-04-22 21:38:25 +0300225{
Johannes Berg6829c872009-07-02 09:13:27 +0200226 struct wireless_dev *wdev = dev->ieee80211_ptr;
227 struct wiphy *wiphy = wdev->wiphy;
Jouni Malinen1965c852009-04-22 21:38:25 +0300228 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200229
Johannes Berg667503d2009-07-07 03:56:11 +0200230 wdev_lock(wdev);
Johannes Bergcb0b4be2009-07-07 03:56:07 +0200231
232 nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL);
Johannes Berg6829c872009-07-02 09:13:27 +0200233 if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Berg667503d2009-07-07 03:56:11 +0200234 __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
235 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200236 false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200237
Johannes Berg667503d2009-07-07 03:56:11 +0200238 wdev_unlock(wdev);
Jouni Malinen1965c852009-04-22 21:38:25 +0300239}
240EXPORT_SYMBOL(cfg80211_send_assoc_timeout);
241
Jouni Malinena3b8b052009-03-27 21:59:49 +0200242void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
243 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +0200244 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +0200245{
246 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
247 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg3d23e342009-09-29 23:27:28 +0200248#ifdef CONFIG_CFG80211_WEXT
Johannes Bergf58d4ed2009-06-19 02:45:21 +0200249 union iwreq_data wrqu;
Johannes Berge6d6e342009-07-01 21:26:47 +0200250 char *buf = kmalloc(128, gfp);
Johannes Bergf58d4ed2009-06-19 02:45:21 +0200251
252 if (buf) {
253 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
254 "keyid=%d %scast addr=%pM)", key_id,
255 key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
256 addr);
257 memset(&wrqu, 0, sizeof(wrqu));
258 wrqu.data.length = strlen(buf);
259 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
260 kfree(buf);
261 }
262#endif
263
Johannes Berge6d6e342009-07-01 21:26:47 +0200264 nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +0200265}
266EXPORT_SYMBOL(cfg80211_michael_mic_failure);
Johannes Berg19957bb2009-07-02 17:20:43 +0200267
268/* some MLME handling for userspace SME */
Johannes Berg667503d2009-07-07 03:56:11 +0200269int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
270 struct net_device *dev,
271 struct ieee80211_channel *chan,
272 enum nl80211_auth_type auth_type,
273 const u8 *bssid,
274 const u8 *ssid, int ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200275 const u8 *ie, int ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +0100276 const u8 *key, int key_len, int key_idx)
Johannes Berg19957bb2009-07-02 17:20:43 +0200277{
278 struct wireless_dev *wdev = dev->ieee80211_ptr;
279 struct cfg80211_auth_request req;
Johannes Berg95de8172012-01-20 13:55:25 +0100280 int err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200281
Johannes Berg667503d2009-07-07 03:56:11 +0200282 ASSERT_WDEV_LOCK(wdev);
283
Johannes Bergfffd0932009-07-08 14:22:54 +0200284 if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
285 if (!key || !key_len || key_idx < 0 || key_idx > 4)
286 return -EINVAL;
287
Johannes Berg0a9b5e12009-07-02 18:26:18 +0200288 if (wdev->current_bss &&
Joe Perchesac422d32012-05-08 18:56:55 +0000289 ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
Johannes Berg0a9b5e12009-07-02 18:26:18 +0200290 return -EALREADY;
291
Johannes Berg19957bb2009-07-02 17:20:43 +0200292 memset(&req, 0, sizeof(req));
293
294 req.ie = ie;
295 req.ie_len = ie_len;
296 req.auth_type = auth_type;
297 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
298 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200299 req.key = key;
300 req.key_len = key_len;
301 req.key_idx = key_idx;
Johannes Berg19957bb2009-07-02 17:20:43 +0200302 if (!req.bss)
303 return -ENOENT;
304
Michal Kaziore4e32452012-06-29 12:47:08 +0200305 err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
306 CHAN_MODE_SHARED);
307 if (err)
308 goto out;
309
Johannes Berg19957bb2009-07-02 17:20:43 +0200310 err = rdev->ops->auth(&rdev->wiphy, dev, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200311
Michal Kaziore4e32452012-06-29 12:47:08 +0200312out:
Johannes Berg95de8172012-01-20 13:55:25 +0100313 cfg80211_put_bss(req.bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200314 return err;
315}
316
Johannes Berg667503d2009-07-07 03:56:11 +0200317int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
318 struct net_device *dev, struct ieee80211_channel *chan,
319 enum nl80211_auth_type auth_type, const u8 *bssid,
320 const u8 *ssid, int ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200321 const u8 *ie, int ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +0100322 const u8 *key, int key_len, int key_idx)
Johannes Berg667503d2009-07-07 03:56:11 +0200323{
324 int err;
325
Michal Kaziore4e32452012-06-29 12:47:08 +0200326 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200327 wdev_lock(dev->ieee80211_ptr);
328 err = __cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +0200329 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +0100330 key, key_len, key_idx);
Johannes Berg667503d2009-07-07 03:56:11 +0200331 wdev_unlock(dev->ieee80211_ptr);
Michal Kaziore4e32452012-06-29 12:47:08 +0200332 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200333
334 return err;
335}
336
Ben Greear7e7c8922011-11-18 11:31:59 -0800337/* Do a logical ht_capa &= ht_capa_mask. */
338void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
339 const struct ieee80211_ht_cap *ht_capa_mask)
340{
341 int i;
342 u8 *p1, *p2;
343 if (!ht_capa_mask) {
344 memset(ht_capa, 0, sizeof(*ht_capa));
345 return;
346 }
347
348 p1 = (u8*)(ht_capa);
349 p2 = (u8*)(ht_capa_mask);
350 for (i = 0; i<sizeof(*ht_capa); i++)
351 p1[i] &= p2[i];
352}
353
Johannes Berg667503d2009-07-07 03:56:11 +0200354int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
355 struct net_device *dev,
356 struct ieee80211_channel *chan,
357 const u8 *bssid, const u8 *prev_bssid,
358 const u8 *ssid, int ssid_len,
359 const u8 *ie, int ie_len, bool use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -0800360 struct cfg80211_crypto_settings *crypt,
361 u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
362 struct ieee80211_ht_cap *ht_capa_mask)
Johannes Berg19957bb2009-07-02 17:20:43 +0200363{
364 struct wireless_dev *wdev = dev->ieee80211_ptr;
365 struct cfg80211_assoc_request req;
Johannes Berg95de8172012-01-20 13:55:25 +0100366 int err;
Jouni Malinen24b6b152009-11-17 21:35:38 +0200367 bool was_connected = false;
Johannes Berg19957bb2009-07-02 17:20:43 +0200368
Johannes Berg667503d2009-07-07 03:56:11 +0200369 ASSERT_WDEV_LOCK(wdev);
370
Johannes Berg19957bb2009-07-02 17:20:43 +0200371 memset(&req, 0, sizeof(req));
372
Jouni Malinen24b6b152009-11-17 21:35:38 +0200373 if (wdev->current_bss && prev_bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000374 ether_addr_equal(wdev->current_bss->pub.bssid, prev_bssid)) {
Jouni Malinen24b6b152009-11-17 21:35:38 +0200375 /*
376 * Trying to reassociate: Allow this to proceed and let the old
377 * association to be dropped when the new one is completed.
378 */
379 if (wdev->sme_state == CFG80211_SME_CONNECTED) {
380 was_connected = true;
381 wdev->sme_state = CFG80211_SME_CONNECTING;
382 }
383 } else if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200384 return -EALREADY;
385
386 req.ie = ie;
387 req.ie_len = ie_len;
388 memcpy(&req.crypto, crypt, sizeof(req.crypto));
389 req.use_mfp = use_mfp;
Johannes Berg3e5d7642009-07-07 14:37:26 +0200390 req.prev_bssid = prev_bssid;
Ben Greear7e7c8922011-11-18 11:31:59 -0800391 req.flags = assoc_flags;
392 if (ht_capa)
393 memcpy(&req.ht_capa, ht_capa, sizeof(req.ht_capa));
394 if (ht_capa_mask)
395 memcpy(&req.ht_capa_mask, ht_capa_mask,
396 sizeof(req.ht_capa_mask));
397 cfg80211_oper_and_ht_capa(&req.ht_capa_mask,
398 rdev->wiphy.ht_capa_mod_mask);
399
Johannes Berg19957bb2009-07-02 17:20:43 +0200400 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
401 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
Jouni Malinen24b6b152009-11-17 21:35:38 +0200402 if (!req.bss) {
403 if (was_connected)
404 wdev->sme_state = CFG80211_SME_CONNECTED;
Johannes Berg19957bb2009-07-02 17:20:43 +0200405 return -ENOENT;
Jouni Malinen24b6b152009-11-17 21:35:38 +0200406 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200407
Michal Kaziore4e32452012-06-29 12:47:08 +0200408 err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
409 CHAN_MODE_SHARED);
410 if (err)
411 goto out;
412
Johannes Berg19957bb2009-07-02 17:20:43 +0200413 err = rdev->ops->assoc(&rdev->wiphy, dev, &req);
Johannes Berg95de8172012-01-20 13:55:25 +0100414
Michal Kaziore4e32452012-06-29 12:47:08 +0200415out:
Johannes Berg95de8172012-01-20 13:55:25 +0100416 if (err) {
417 if (was_connected)
418 wdev->sme_state = CFG80211_SME_CONNECTED;
419 cfg80211_put_bss(req.bss);
420 }
421
Johannes Berg19957bb2009-07-02 17:20:43 +0200422 return err;
423}
424
Johannes Berg667503d2009-07-07 03:56:11 +0200425int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
426 struct net_device *dev,
427 struct ieee80211_channel *chan,
428 const u8 *bssid, const u8 *prev_bssid,
429 const u8 *ssid, int ssid_len,
430 const u8 *ie, int ie_len, bool use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -0800431 struct cfg80211_crypto_settings *crypt,
432 u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
433 struct ieee80211_ht_cap *ht_capa_mask)
Johannes Berg667503d2009-07-07 03:56:11 +0200434{
435 struct wireless_dev *wdev = dev->ieee80211_ptr;
436 int err;
437
Michal Kaziore4e32452012-06-29 12:47:08 +0200438 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200439 wdev_lock(wdev);
440 err = __cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
Ben Greear7e7c8922011-11-18 11:31:59 -0800441 ssid, ssid_len, ie, ie_len, use_mfp, crypt,
442 assoc_flags, ht_capa, ht_capa_mask);
Johannes Berg667503d2009-07-07 03:56:11 +0200443 wdev_unlock(wdev);
Michal Kaziore4e32452012-06-29 12:47:08 +0200444 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200445
446 return err;
447}
448
449int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
450 struct net_device *dev, const u8 *bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300451 const u8 *ie, int ie_len, u16 reason,
452 bool local_state_change)
Johannes Berg19957bb2009-07-02 17:20:43 +0200453{
454 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg95de8172012-01-20 13:55:25 +0100455 struct cfg80211_deauth_request req = {
456 .bssid = bssid,
457 .reason_code = reason,
458 .ie = ie,
459 .ie_len = ie_len,
460 };
Johannes Berg19957bb2009-07-02 17:20:43 +0200461
Johannes Berg667503d2009-07-07 03:56:11 +0200462 ASSERT_WDEV_LOCK(wdev);
463
Johannes Berg95de8172012-01-20 13:55:25 +0100464 if (local_state_change) {
465 if (wdev->current_bss &&
Joe Perchesac422d32012-05-08 18:56:55 +0000466 ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
Johannes Berg95de8172012-01-20 13:55:25 +0100467 cfg80211_unhold_bss(wdev->current_bss);
468 cfg80211_put_bss(&wdev->current_bss->pub);
469 wdev->current_bss = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200470 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200471
Johannes Berg95de8172012-01-20 13:55:25 +0100472 return 0;
473 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200474
Johannes Berg63c9c5e2012-02-24 13:50:51 +0100475 return rdev->ops->deauth(&rdev->wiphy, dev, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200476}
477
Johannes Berg667503d2009-07-07 03:56:11 +0200478int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
479 struct net_device *dev, const u8 *bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300480 const u8 *ie, int ie_len, u16 reason,
481 bool local_state_change)
Johannes Berg667503d2009-07-07 03:56:11 +0200482{
483 struct wireless_dev *wdev = dev->ieee80211_ptr;
484 int err;
485
486 wdev_lock(wdev);
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300487 err = __cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason,
488 local_state_change);
Johannes Berg667503d2009-07-07 03:56:11 +0200489 wdev_unlock(wdev);
490
491 return err;
492}
493
494static int __cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
495 struct net_device *dev, const u8 *bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300496 const u8 *ie, int ie_len, u16 reason,
497 bool local_state_change)
Johannes Berg19957bb2009-07-02 17:20:43 +0200498{
499 struct wireless_dev *wdev = dev->ieee80211_ptr;
500 struct cfg80211_disassoc_request req;
501
Johannes Berg667503d2009-07-07 03:56:11 +0200502 ASSERT_WDEV_LOCK(wdev);
503
Johannes Bergf9d6b402009-07-27 10:22:28 +0200504 if (wdev->sme_state != CFG80211_SME_CONNECTED)
505 return -ENOTCONN;
506
507 if (WARN_ON(!wdev->current_bss))
508 return -ENOTCONN;
509
Johannes Berg19957bb2009-07-02 17:20:43 +0200510 memset(&req, 0, sizeof(req));
511 req.reason_code = reason;
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300512 req.local_state_change = local_state_change;
Johannes Berg19957bb2009-07-02 17:20:43 +0200513 req.ie = ie;
514 req.ie_len = ie_len;
Joe Perchesac422d32012-05-08 18:56:55 +0000515 if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
Johannes Berg19957bb2009-07-02 17:20:43 +0200516 req.bss = &wdev->current_bss->pub;
517 else
518 return -ENOTCONN;
519
Johannes Berg63c9c5e2012-02-24 13:50:51 +0100520 return rdev->ops->disassoc(&rdev->wiphy, dev, &req);
Johannes Berg667503d2009-07-07 03:56:11 +0200521}
522
523int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
524 struct net_device *dev, const u8 *bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300525 const u8 *ie, int ie_len, u16 reason,
526 bool local_state_change)
Johannes Berg667503d2009-07-07 03:56:11 +0200527{
528 struct wireless_dev *wdev = dev->ieee80211_ptr;
529 int err;
530
531 wdev_lock(wdev);
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300532 err = __cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason,
533 local_state_change);
Johannes Berg667503d2009-07-07 03:56:11 +0200534 wdev_unlock(wdev);
535
536 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200537}
538
539void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
540 struct net_device *dev)
541{
542 struct wireless_dev *wdev = dev->ieee80211_ptr;
543 struct cfg80211_deauth_request req;
Johannes Berg95de8172012-01-20 13:55:25 +0100544 u8 bssid[ETH_ALEN];
Johannes Berg19957bb2009-07-02 17:20:43 +0200545
Johannes Berg667503d2009-07-07 03:56:11 +0200546 ASSERT_WDEV_LOCK(wdev);
547
Johannes Berg19957bb2009-07-02 17:20:43 +0200548 if (!rdev->ops->deauth)
549 return;
550
551 memset(&req, 0, sizeof(req));
552 req.reason_code = WLAN_REASON_DEAUTH_LEAVING;
553 req.ie = NULL;
554 req.ie_len = 0;
555
Johannes Berg95de8172012-01-20 13:55:25 +0100556 if (!wdev->current_bss)
557 return;
Johannes Berg19957bb2009-07-02 17:20:43 +0200558
Johannes Berg95de8172012-01-20 13:55:25 +0100559 memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
560 req.bssid = bssid;
Johannes Berg63c9c5e2012-02-24 13:50:51 +0100561 rdev->ops->deauth(&rdev->wiphy, dev, &req);
Johannes Berg95de8172012-01-20 13:55:25 +0100562
563 if (wdev->current_bss) {
564 cfg80211_unhold_bss(wdev->current_bss);
565 cfg80211_put_bss(&wdev->current_bss->pub);
566 wdev->current_bss = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200567 }
568}
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100569
570void cfg80211_ready_on_channel(struct net_device *dev, u64 cookie,
571 struct ieee80211_channel *chan,
572 enum nl80211_channel_type channel_type,
573 unsigned int duration, gfp_t gfp)
574{
575 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
576 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
577
578 nl80211_send_remain_on_channel(rdev, dev, cookie, chan, channel_type,
579 duration, gfp);
580}
581EXPORT_SYMBOL(cfg80211_ready_on_channel);
582
583void cfg80211_remain_on_channel_expired(struct net_device *dev,
584 u64 cookie,
585 struct ieee80211_channel *chan,
586 enum nl80211_channel_type channel_type,
587 gfp_t gfp)
588{
589 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
590 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
591
592 nl80211_send_remain_on_channel_cancel(rdev, dev, cookie, chan,
593 channel_type, gfp);
594}
595EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Johannes Berg98b62182009-12-23 13:15:44 +0100596
597void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
598 struct station_info *sinfo, gfp_t gfp)
599{
600 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
601 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
602
603 nl80211_send_sta_event(rdev, dev, mac_addr, sinfo, gfp);
604}
605EXPORT_SYMBOL(cfg80211_new_sta);
Jouni Malinen026331c2010-02-15 12:53:10 +0200606
Jouni Malinenec15e682011-03-23 15:29:52 +0200607void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
608{
609 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
610 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
611
612 nl80211_send_sta_del_event(rdev, dev, mac_addr, gfp);
613}
614EXPORT_SYMBOL(cfg80211_del_sta);
615
Johannes Berg2e161f782010-08-12 15:38:38 +0200616struct cfg80211_mgmt_registration {
Jouni Malinen026331c2010-02-15 12:53:10 +0200617 struct list_head list;
618
619 u32 nlpid;
620
621 int match_len;
622
Johannes Berg2e161f782010-08-12 15:38:38 +0200623 __le16 frame_type;
624
Jouni Malinen026331c2010-02-15 12:53:10 +0200625 u8 match[];
626};
627
Johannes Berg2e161f782010-08-12 15:38:38 +0200628int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
629 u16 frame_type, const u8 *match_data,
630 int match_len)
Jouni Malinen026331c2010-02-15 12:53:10 +0200631{
Johannes Berg271733c2010-10-13 12:06:23 +0200632 struct wiphy *wiphy = wdev->wiphy;
633 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg2e161f782010-08-12 15:38:38 +0200634 struct cfg80211_mgmt_registration *reg, *nreg;
Jouni Malinen026331c2010-02-15 12:53:10 +0200635 int err = 0;
Johannes Berg2e161f782010-08-12 15:38:38 +0200636 u16 mgmt_type;
637
638 if (!wdev->wiphy->mgmt_stypes)
639 return -EOPNOTSUPP;
640
641 if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
642 return -EINVAL;
643
644 if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
645 return -EINVAL;
646
647 mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
648 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
649 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +0200650
651 nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
652 if (!nreg)
653 return -ENOMEM;
654
Johannes Berg2e161f782010-08-12 15:38:38 +0200655 spin_lock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200656
Johannes Berg2e161f782010-08-12 15:38:38 +0200657 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +0200658 int mlen = min(match_len, reg->match_len);
659
Johannes Berg2e161f782010-08-12 15:38:38 +0200660 if (frame_type != le16_to_cpu(reg->frame_type))
661 continue;
662
Jouni Malinen026331c2010-02-15 12:53:10 +0200663 if (memcmp(reg->match, match_data, mlen) == 0) {
664 err = -EALREADY;
665 break;
666 }
667 }
668
669 if (err) {
670 kfree(nreg);
671 goto out;
672 }
673
674 memcpy(nreg->match, match_data, match_len);
675 nreg->match_len = match_len;
676 nreg->nlpid = snd_pid;
Johannes Berg2e161f782010-08-12 15:38:38 +0200677 nreg->frame_type = cpu_to_le16(frame_type);
678 list_add(&nreg->list, &wdev->mgmt_registrations);
Jouni Malinen026331c2010-02-15 12:53:10 +0200679
Johannes Berg271733c2010-10-13 12:06:23 +0200680 if (rdev->ops->mgmt_frame_register)
681 rdev->ops->mgmt_frame_register(wiphy, wdev->netdev,
682 frame_type, true);
683
Jouni Malinen026331c2010-02-15 12:53:10 +0200684 out:
Johannes Berg2e161f782010-08-12 15:38:38 +0200685 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Johannes Berg271733c2010-10-13 12:06:23 +0200686
Jouni Malinen026331c2010-02-15 12:53:10 +0200687 return err;
688}
689
Johannes Berg2e161f782010-08-12 15:38:38 +0200690void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid)
Jouni Malinen026331c2010-02-15 12:53:10 +0200691{
Johannes Berg271733c2010-10-13 12:06:23 +0200692 struct wiphy *wiphy = wdev->wiphy;
693 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg2e161f782010-08-12 15:38:38 +0200694 struct cfg80211_mgmt_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +0200695
Johannes Berg2e161f782010-08-12 15:38:38 +0200696 spin_lock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200697
Johannes Berg2e161f782010-08-12 15:38:38 +0200698 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
Johannes Berg271733c2010-10-13 12:06:23 +0200699 if (reg->nlpid != nlpid)
700 continue;
701
702 if (rdev->ops->mgmt_frame_register) {
703 u16 frame_type = le16_to_cpu(reg->frame_type);
704
705 rdev->ops->mgmt_frame_register(wiphy, wdev->netdev,
706 frame_type, false);
Jouni Malinen026331c2010-02-15 12:53:10 +0200707 }
Johannes Berg271733c2010-10-13 12:06:23 +0200708
709 list_del(&reg->list);
710 kfree(reg);
Jouni Malinen026331c2010-02-15 12:53:10 +0200711 }
712
Johannes Berg2e161f782010-08-12 15:38:38 +0200713 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Johannes Berg28946da2011-11-04 11:18:12 +0100714
715 if (nlpid == wdev->ap_unexpected_nlpid)
716 wdev->ap_unexpected_nlpid = 0;
Jouni Malinen026331c2010-02-15 12:53:10 +0200717}
718
Johannes Berg2e161f782010-08-12 15:38:38 +0200719void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
Jouni Malinen026331c2010-02-15 12:53:10 +0200720{
Johannes Berg2e161f782010-08-12 15:38:38 +0200721 struct cfg80211_mgmt_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +0200722
Johannes Berg2e161f782010-08-12 15:38:38 +0200723 spin_lock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200724
Johannes Berg2e161f782010-08-12 15:38:38 +0200725 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +0200726 list_del(&reg->list);
727 kfree(reg);
728 }
729
Johannes Berg2e161f782010-08-12 15:38:38 +0200730 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200731}
732
Johannes Berg2e161f782010-08-12 15:38:38 +0200733int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
734 struct net_device *dev,
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100735 struct ieee80211_channel *chan, bool offchan,
Johannes Berg2e161f782010-08-12 15:38:38 +0200736 enum nl80211_channel_type channel_type,
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100737 bool channel_type_valid, unsigned int wait,
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530738 const u8 *buf, size_t len, bool no_cck,
Johannes Berge247bd902011-11-04 11:18:21 +0100739 bool dont_wait_for_ack, u64 *cookie)
Jouni Malinen026331c2010-02-15 12:53:10 +0200740{
741 struct wireless_dev *wdev = dev->ieee80211_ptr;
742 const struct ieee80211_mgmt *mgmt;
Johannes Berg2e161f782010-08-12 15:38:38 +0200743 u16 stype;
Jouni Malinen026331c2010-02-15 12:53:10 +0200744
Johannes Berg2e161f782010-08-12 15:38:38 +0200745 if (!wdev->wiphy->mgmt_stypes)
Jouni Malinen026331c2010-02-15 12:53:10 +0200746 return -EOPNOTSUPP;
Johannes Berg2e161f782010-08-12 15:38:38 +0200747
748 if (!rdev->ops->mgmt_tx)
749 return -EOPNOTSUPP;
750
Jouni Malinen026331c2010-02-15 12:53:10 +0200751 if (len < 24 + 1)
752 return -EINVAL;
753
754 mgmt = (const struct ieee80211_mgmt *) buf;
Johannes Berg2e161f782010-08-12 15:38:38 +0200755
756 if (!ieee80211_is_mgmt(mgmt->frame_control))
Jouni Malinen026331c2010-02-15 12:53:10 +0200757 return -EINVAL;
Johannes Berg2e161f782010-08-12 15:38:38 +0200758
759 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
760 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
761 return -EINVAL;
762
763 if (ieee80211_is_action(mgmt->frame_control) &&
764 mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
Johannes Berg663fcaf2010-09-30 21:06:09 +0200765 int err = 0;
766
Johannes Bergfe100ac2010-08-09 15:52:03 +0200767 wdev_lock(wdev);
768
Johannes Berg663fcaf2010-09-30 21:06:09 +0200769 switch (wdev->iftype) {
770 case NL80211_IFTYPE_ADHOC:
771 case NL80211_IFTYPE_STATION:
772 case NL80211_IFTYPE_P2P_CLIENT:
773 if (!wdev->current_bss) {
774 err = -ENOTCONN;
775 break;
776 }
Johannes Bergfe100ac2010-08-09 15:52:03 +0200777
Joe Perchesac422d32012-05-08 18:56:55 +0000778 if (!ether_addr_equal(wdev->current_bss->pub.bssid,
779 mgmt->bssid)) {
Johannes Berg663fcaf2010-09-30 21:06:09 +0200780 err = -ENOTCONN;
781 break;
782 }
783
784 /*
785 * check for IBSS DA must be done by driver as
786 * cfg80211 doesn't track the stations
787 */
788 if (wdev->iftype == NL80211_IFTYPE_ADHOC)
789 break;
790
791 /* for station, check that DA is the AP */
Joe Perchesac422d32012-05-08 18:56:55 +0000792 if (!ether_addr_equal(wdev->current_bss->pub.bssid,
793 mgmt->da)) {
Johannes Berg663fcaf2010-09-30 21:06:09 +0200794 err = -ENOTCONN;
795 break;
796 }
797 break;
798 case NL80211_IFTYPE_AP:
799 case NL80211_IFTYPE_P2P_GO:
800 case NL80211_IFTYPE_AP_VLAN:
Joe Perchesac422d32012-05-08 18:56:55 +0000801 if (!ether_addr_equal(mgmt->bssid, dev->dev_addr))
Johannes Berg663fcaf2010-09-30 21:06:09 +0200802 err = -EINVAL;
803 break;
Javier Cardona0778a6a2011-05-03 16:57:08 -0700804 case NL80211_IFTYPE_MESH_POINT:
Joe Perchesac422d32012-05-08 18:56:55 +0000805 if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
Javier Cardona0778a6a2011-05-03 16:57:08 -0700806 err = -EINVAL;
807 break;
808 }
809 /*
810 * check for mesh DA must be done by driver as
811 * cfg80211 doesn't track the stations
812 */
813 break;
Johannes Berg663fcaf2010-09-30 21:06:09 +0200814 default:
815 err = -EOPNOTSUPP;
816 break;
817 }
Johannes Bergfe100ac2010-08-09 15:52:03 +0200818 wdev_unlock(wdev);
Johannes Berg663fcaf2010-09-30 21:06:09 +0200819
820 if (err)
821 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +0200822 }
823
Joe Perchesac422d32012-05-08 18:56:55 +0000824 if (!ether_addr_equal(mgmt->sa, dev->dev_addr))
Jouni Malinen026331c2010-02-15 12:53:10 +0200825 return -EINVAL;
826
827 /* Transmit the Action frame as requested by user space */
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100828 return rdev->ops->mgmt_tx(&rdev->wiphy, dev, chan, offchan,
829 channel_type, channel_type_valid,
Johannes Berge247bd902011-11-04 11:18:21 +0100830 wait, buf, len, no_cck, dont_wait_for_ack,
831 cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +0200832}
833
Johannes Berg804483e2012-03-05 22:18:41 +0100834bool cfg80211_rx_mgmt(struct net_device *dev, int freq, int sig_mbm,
835 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +0200836{
837 struct wireless_dev *wdev = dev->ieee80211_ptr;
838 struct wiphy *wiphy = wdev->wiphy;
839 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg2e161f782010-08-12 15:38:38 +0200840 struct cfg80211_mgmt_registration *reg;
841 const struct ieee80211_txrx_stypes *stypes =
842 &wiphy->mgmt_stypes[wdev->iftype];
843 struct ieee80211_mgmt *mgmt = (void *)buf;
844 const u8 *data;
845 int data_len;
Jouni Malinen026331c2010-02-15 12:53:10 +0200846 bool result = false;
Johannes Berg2e161f782010-08-12 15:38:38 +0200847 __le16 ftype = mgmt->frame_control &
848 cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
849 u16 stype;
Jouni Malinen026331c2010-02-15 12:53:10 +0200850
Johannes Berg2e161f782010-08-12 15:38:38 +0200851 stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
Jouni Malinen026331c2010-02-15 12:53:10 +0200852
Johannes Berg2e161f782010-08-12 15:38:38 +0200853 if (!(stypes->rx & BIT(stype)))
854 return false;
Jouni Malinen026331c2010-02-15 12:53:10 +0200855
Johannes Berg2e161f782010-08-12 15:38:38 +0200856 data = buf + ieee80211_hdrlen(mgmt->frame_control);
857 data_len = len - ieee80211_hdrlen(mgmt->frame_control);
Jouni Malinen026331c2010-02-15 12:53:10 +0200858
Johannes Berg2e161f782010-08-12 15:38:38 +0200859 spin_lock_bh(&wdev->mgmt_registrations_lock);
860
861 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
862 if (reg->frame_type != ftype)
Jouni Malinen026331c2010-02-15 12:53:10 +0200863 continue;
864
Johannes Berg2e161f782010-08-12 15:38:38 +0200865 if (reg->match_len > data_len)
866 continue;
867
868 if (memcmp(reg->match, data, reg->match_len))
Jouni Malinen026331c2010-02-15 12:53:10 +0200869 continue;
870
871 /* found match! */
872
873 /* Indicate the received Action frame to user space */
Johannes Berg804483e2012-03-05 22:18:41 +0100874 if (nl80211_send_mgmt(rdev, dev, reg->nlpid,
875 freq, sig_mbm,
Johannes Berg2e161f782010-08-12 15:38:38 +0200876 buf, len, gfp))
Jouni Malinen026331c2010-02-15 12:53:10 +0200877 continue;
878
879 result = true;
880 break;
881 }
882
Johannes Berg2e161f782010-08-12 15:38:38 +0200883 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200884
885 return result;
886}
Johannes Berg2e161f782010-08-12 15:38:38 +0200887EXPORT_SYMBOL(cfg80211_rx_mgmt);
Jouni Malinen026331c2010-02-15 12:53:10 +0200888
Johannes Berg2e161f782010-08-12 15:38:38 +0200889void cfg80211_mgmt_tx_status(struct net_device *dev, u64 cookie,
890 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +0200891{
892 struct wireless_dev *wdev = dev->ieee80211_ptr;
893 struct wiphy *wiphy = wdev->wiphy;
894 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
895
896 /* Indicate TX status of the Action frame to user space */
Johannes Berg2e161f782010-08-12 15:38:38 +0200897 nl80211_send_mgmt_tx_status(rdev, dev, cookie, buf, len, ack, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +0200898}
Johannes Berg2e161f782010-08-12 15:38:38 +0200899EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200900
901void cfg80211_cqm_rssi_notify(struct net_device *dev,
902 enum nl80211_cqm_rssi_threshold_event rssi_event,
903 gfp_t gfp)
904{
905 struct wireless_dev *wdev = dev->ieee80211_ptr;
906 struct wiphy *wiphy = wdev->wiphy;
907 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
908
909 /* Indicate roaming trigger event to user space */
910 nl80211_send_cqm_rssi_notify(rdev, dev, rssi_event, gfp);
911}
912EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +0100913
914void cfg80211_cqm_pktloss_notify(struct net_device *dev,
915 const u8 *peer, u32 num_packets, gfp_t gfp)
916{
917 struct wireless_dev *wdev = dev->ieee80211_ptr;
918 struct wiphy *wiphy = wdev->wiphy;
919 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
920
921 /* Indicate roaming trigger event to user space */
922 nl80211_send_cqm_pktloss_notify(rdev, dev, peer, num_packets, gfp);
923}
924EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Berge5497d72011-07-05 16:35:40 +0200925
926void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
927 const u8 *replay_ctr, gfp_t gfp)
928{
929 struct wireless_dev *wdev = dev->ieee80211_ptr;
930 struct wiphy *wiphy = wdev->wiphy;
931 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
932
933 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
934}
935EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
Jouni Malinenc9df56b2011-09-16 18:56:23 +0300936
937void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
938 const u8 *bssid, bool preauth, gfp_t gfp)
939{
940 struct wireless_dev *wdev = dev->ieee80211_ptr;
941 struct wiphy *wiphy = wdev->wiphy;
942 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
943
944 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
945}
946EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
Johannes Berg28946da2011-11-04 11:18:12 +0100947
Thomas Pedersen53145262012-04-06 13:35:47 -0700948void cfg80211_ch_switch_notify(struct net_device *dev, int freq,
949 enum nl80211_channel_type type)
950{
951 struct wireless_dev *wdev = dev->ieee80211_ptr;
952 struct wiphy *wiphy = wdev->wiphy;
953 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
954 struct ieee80211_channel *chan;
955
956 wdev_lock(wdev);
957
958 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
959 wdev->iftype != NL80211_IFTYPE_P2P_GO))
960 goto out;
961
962 chan = rdev_freq_to_chan(rdev, freq, type);
963 if (WARN_ON(!chan))
964 goto out;
965
Michal Kaziorf4489eb2012-06-29 12:46:58 +0200966 wdev->channel = chan;
Thomas Pedersen53145262012-04-06 13:35:47 -0700967 nl80211_ch_switch_notify(rdev, dev, freq, type, GFP_KERNEL);
968out:
969 wdev_unlock(wdev);
970 return;
971}
972EXPORT_SYMBOL(cfg80211_ch_switch_notify);
973
Johannes Berg28946da2011-11-04 11:18:12 +0100974bool cfg80211_rx_spurious_frame(struct net_device *dev,
975 const u8 *addr, gfp_t gfp)
976{
977 struct wireless_dev *wdev = dev->ieee80211_ptr;
978
979 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
980 wdev->iftype != NL80211_IFTYPE_P2P_GO))
981 return false;
982
983 return nl80211_unexpected_frame(dev, addr, gfp);
984}
985EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +0100986
987bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
988 const u8 *addr, gfp_t gfp)
989{
990 struct wireless_dev *wdev = dev->ieee80211_ptr;
991
992 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
993 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
994 wdev->iftype != NL80211_IFTYPE_AP_VLAN))
995 return false;
996
997 return nl80211_unexpected_4addr_frame(dev, addr, gfp);
998}
999EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);