blob: c82adfee7697dc436da4b6d50dc08101c476b459 [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"
Hila Gonene35e4d22012-06-27 17:19:42 +030018#include "rdev-ops.h"
19
Jouni Malinen6039f6d2009-03-19 13:39:21 +020020
Johannes Bergcb0b4be2009-07-07 03:56:07 +020021void cfg80211_send_rx_auth(struct net_device *dev, const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020022{
Johannes Berg19957bb2009-07-02 17:20:43 +020023 struct wireless_dev *wdev = dev->ieee80211_ptr;
24 struct wiphy *wiphy = wdev->wiphy;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020025 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +020026
Beni Lev4ee3e062012-08-27 12:49:39 +030027 trace_cfg80211_send_rx_auth(dev);
Johannes Berg667503dd2009-07-07 03:56:11 +020028 wdev_lock(wdev);
Johannes Bergcb0b4be2009-07-07 03:56:07 +020029
Johannes Berg95de8172012-01-20 13:55:25 +010030 nl80211_send_rx_auth(rdev, dev, buf, len, GFP_KERNEL);
31 cfg80211_sme_rx_auth(dev, buf, len);
Johannes Berg667503dd2009-07-07 03:56:11 +020032
33 wdev_unlock(wdev);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020034}
35EXPORT_SYMBOL(cfg80211_send_rx_auth);
36
Johannes Berg95de8172012-01-20 13:55:25 +010037void cfg80211_send_rx_assoc(struct net_device *dev, struct cfg80211_bss *bss,
38 const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020039{
Johannes Berg6829c872009-07-02 09:13:27 +020040 u16 status_code;
41 struct wireless_dev *wdev = dev->ieee80211_ptr;
42 struct wiphy *wiphy = wdev->wiphy;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020043 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020044 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
45 u8 *ie = mgmt->u.assoc_resp.variable;
Johannes Berg95de8172012-01-20 13:55:25 +010046 int ieoffs = offsetof(struct ieee80211_mgmt, u.assoc_resp.variable);
Johannes Berg6829c872009-07-02 09:13:27 +020047
Beni Lev4ee3e062012-08-27 12:49:39 +030048 trace_cfg80211_send_rx_assoc(dev, bss);
Johannes Berg667503dd2009-07-07 03:56:11 +020049 wdev_lock(wdev);
Johannes Bergcb0b4be2009-07-07 03:56:07 +020050
Johannes Berg6829c872009-07-02 09:13:27 +020051 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
52
Johannes Bergf401a6f2009-08-07 14:51:05 +020053 /*
54 * This is a bit of a hack, we don't notify userspace of
55 * a (re-)association reply if we tried to send a reassoc
56 * and got a reject -- we only try again with an assoc
57 * frame instead of reassoc.
58 */
59 if (status_code != WLAN_STATUS_SUCCESS && wdev->conn &&
Johannes Berg95de8172012-01-20 13:55:25 +010060 cfg80211_sme_failed_reassoc(wdev)) {
Johannes Berg5b112d32013-02-01 01:49:58 +010061 cfg80211_put_bss(wiphy, bss);
Johannes Bergf401a6f2009-08-07 14:51:05 +020062 goto out;
Johannes Berg95de8172012-01-20 13:55:25 +010063 }
Johannes Bergf401a6f2009-08-07 14:51:05 +020064
Johannes Bergcb0b4be2009-07-07 03:56:07 +020065 nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL);
Johannes Berg6829c872009-07-02 09:13:27 +020066
Johannes Berg95de8172012-01-20 13:55:25 +010067 if (status_code != WLAN_STATUS_SUCCESS && wdev->conn) {
Johannes Berg7d930bc2009-10-20 15:08:53 +090068 cfg80211_sme_failed_assoc(wdev);
Johannes Berg7d930bc2009-10-20 15:08:53 +090069 /*
70 * do not call connect_result() now because the
71 * sme will schedule work that does it later.
72 */
Johannes Berg5b112d32013-02-01 01:49:58 +010073 cfg80211_put_bss(wiphy, bss);
Johannes Berg7d930bc2009-10-20 15:08:53 +090074 goto out;
Johannes Bergdf7fc0f2009-07-29 11:23:49 +020075 }
76
Johannes Bergea416a72009-08-17 12:22:14 +020077 if (!wdev->conn && wdev->sme_state == CFG80211_SME_IDLE) {
78 /*
79 * This is for the userspace SME, the CONNECTING
80 * state will be changed to CONNECTED by
81 * __cfg80211_connect_result() below.
82 */
83 wdev->sme_state = CFG80211_SME_CONNECTING;
84 }
85
Johannes Berg95de8172012-01-20 13:55:25 +010086 /* this consumes the bss reference */
Johannes Bergdf7fc0f2009-07-29 11:23:49 +020087 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, ie, len - ieoffs,
88 status_code,
Johannes Berg95de8172012-01-20 13:55:25 +010089 status_code == WLAN_STATUS_SUCCESS, bss);
Johannes Bergf401a6f2009-08-07 14:51:05 +020090 out:
Johannes Berg667503dd2009-07-07 03:56:11 +020091 wdev_unlock(wdev);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020092}
93EXPORT_SYMBOL(cfg80211_send_rx_assoc);
94
Holger Schurigce470612009-10-13 13:28:13 +020095void __cfg80211_send_deauth(struct net_device *dev,
Johannes Berg667503dd2009-07-07 03:56:11 +020096 const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020097{
Johannes Berg6829c872009-07-02 09:13:27 +020098 struct wireless_dev *wdev = dev->ieee80211_ptr;
99 struct wiphy *wiphy = wdev->wiphy;
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200100 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200101 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
Johannes Berg19957bb2009-07-02 17:20:43 +0200102 const u8 *bssid = mgmt->bssid;
Johannes Berg95de8172012-01-20 13:55:25 +0100103 bool was_current = false;
Johannes Berg6829c872009-07-02 09:13:27 +0200104
Beni Lev4ee3e062012-08-27 12:49:39 +0300105 trace___cfg80211_send_deauth(dev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200106 ASSERT_WDEV_LOCK(wdev);
Johannes Bergcb0b4be2009-07-07 03:56:07 +0200107
Johannes Berg19957bb2009-07-02 17:20:43 +0200108 if (wdev->current_bss &&
Joe Perchesac422d32012-05-08 18:56:55 +0000109 ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200110 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100111 cfg80211_put_bss(wiphy, &wdev->current_bss->pub);
Johannes Berg19957bb2009-07-02 17:20:43 +0200112 wdev->current_bss = NULL;
Johannes Berg3f3b6a82010-08-05 10:20:27 +0200113 was_current = true;
Johannes Berg19957bb2009-07-02 17:20:43 +0200114 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200115
Johannes Berg5fba4af2009-12-02 12:43:42 +0100116 nl80211_send_deauth(rdev, dev, buf, len, GFP_KERNEL);
117
Johannes Berg3f3b6a82010-08-05 10:20:27 +0200118 if (wdev->sme_state == CFG80211_SME_CONNECTED && was_current) {
Johannes Berg6829c872009-07-02 09:13:27 +0200119 u16 reason_code;
120 bool from_ap;
121
122 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
123
Joe Perchesac422d32012-05-08 18:56:55 +0000124 from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
Johannes Berg667503dd2009-07-07 03:56:11 +0200125 __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
Johannes Berg6829c872009-07-02 09:13:27 +0200126 } else if (wdev->sme_state == CFG80211_SME_CONNECTING) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200127 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
128 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200129 false, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200130 }
131}
Holger Schurigce470612009-10-13 13:28:13 +0200132EXPORT_SYMBOL(__cfg80211_send_deauth);
Johannes Berg667503dd2009-07-07 03:56:11 +0200133
Holger Schurigce470612009-10-13 13:28:13 +0200134void cfg80211_send_deauth(struct net_device *dev, const u8 *buf, size_t len)
Johannes Berg667503dd2009-07-07 03:56:11 +0200135{
136 struct wireless_dev *wdev = dev->ieee80211_ptr;
137
Holger Schurigce470612009-10-13 13:28:13 +0200138 wdev_lock(wdev);
139 __cfg80211_send_deauth(dev, buf, len);
140 wdev_unlock(wdev);
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200141}
Jouni Malinen53b46b82009-03-27 20:53:56 +0200142EXPORT_SYMBOL(cfg80211_send_deauth);
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200143
Holger Schurigce470612009-10-13 13:28:13 +0200144void __cfg80211_send_disassoc(struct net_device *dev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200145 const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200146{
Johannes Berg6829c872009-07-02 09:13:27 +0200147 struct wireless_dev *wdev = dev->ieee80211_ptr;
148 struct wiphy *wiphy = wdev->wiphy;
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200149 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200150 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
Johannes Berg19957bb2009-07-02 17:20:43 +0200151 const u8 *bssid = mgmt->bssid;
Johannes Berg19957bb2009-07-02 17:20:43 +0200152 u16 reason_code;
153 bool from_ap;
Johannes Berg6829c872009-07-02 09:13:27 +0200154
Beni Lev4ee3e062012-08-27 12:49:39 +0300155 trace___cfg80211_send_disassoc(dev);
Johannes Berg596a07c2009-07-11 00:17:32 +0200156 ASSERT_WDEV_LOCK(wdev);
Johannes Bergcb0b4be2009-07-07 03:56:07 +0200157
158 nl80211_send_disassoc(rdev, dev, buf, len, GFP_KERNEL);
Johannes Berg6829c872009-07-02 09:13:27 +0200159
Johannes Berg596a07c2009-07-11 00:17:32 +0200160 if (wdev->sme_state != CFG80211_SME_CONNECTED)
161 return;
Johannes Berg6829c872009-07-02 09:13:27 +0200162
Johannes Berg19957bb2009-07-02 17:20:43 +0200163 if (wdev->current_bss &&
Joe Perchesac422d32012-05-08 18:56:55 +0000164 ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
Johannes Berg95de8172012-01-20 13:55:25 +0100165 cfg80211_sme_disassoc(dev, wdev->current_bss);
166 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100167 cfg80211_put_bss(wiphy, &wdev->current_bss->pub);
Johannes Berg95de8172012-01-20 13:55:25 +0100168 wdev->current_bss = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200169 } else
170 WARN_ON(1);
Johannes Berg6829c872009-07-02 09:13:27 +0200171
Johannes Berg6829c872009-07-02 09:13:27 +0200172
Johannes Berg19957bb2009-07-02 17:20:43 +0200173 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
174
Joe Perchesac422d32012-05-08 18:56:55 +0000175 from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
Johannes Berg667503dd2009-07-07 03:56:11 +0200176 __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
Johannes Berg667503dd2009-07-07 03:56:11 +0200177}
Holger Schurigce470612009-10-13 13:28:13 +0200178EXPORT_SYMBOL(__cfg80211_send_disassoc);
Johannes Berg667503dd2009-07-07 03:56:11 +0200179
Holger Schurigce470612009-10-13 13:28:13 +0200180void cfg80211_send_disassoc(struct net_device *dev, const u8 *buf, size_t len)
Johannes Berg667503dd2009-07-07 03:56:11 +0200181{
182 struct wireless_dev *wdev = dev->ieee80211_ptr;
183
Holger Schurigce470612009-10-13 13:28:13 +0200184 wdev_lock(wdev);
185 __cfg80211_send_disassoc(dev, buf, len);
186 wdev_unlock(wdev);
Jouni Malinen6039f6d2009-03-19 13:39:21 +0200187}
Jouni Malinen53b46b82009-03-27 20:53:56 +0200188EXPORT_SYMBOL(cfg80211_send_disassoc);
Jouni Malinena3b8b052009-03-27 21:59:49 +0200189
Johannes Berga58ce432009-11-19 12:45:42 +0100190void cfg80211_send_auth_timeout(struct net_device *dev, const u8 *addr)
191{
192 struct wireless_dev *wdev = dev->ieee80211_ptr;
193 struct wiphy *wiphy = wdev->wiphy;
194 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
195
Beni Lev4ee3e062012-08-27 12:49:39 +0300196 trace_cfg80211_send_auth_timeout(dev, addr);
Johannes Berga58ce432009-11-19 12:45:42 +0100197 wdev_lock(wdev);
198
199 nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
200 if (wdev->sme_state == CFG80211_SME_CONNECTING)
201 __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
202 WLAN_STATUS_UNSPECIFIED_FAILURE,
203 false, NULL);
204
Johannes Berg667503dd2009-07-07 03:56:11 +0200205 wdev_unlock(wdev);
Jouni Malinen1965c852009-04-22 21:38:25 +0300206}
207EXPORT_SYMBOL(cfg80211_send_auth_timeout);
208
Johannes Bergcb0b4be2009-07-07 03:56:07 +0200209void cfg80211_send_assoc_timeout(struct net_device *dev, const u8 *addr)
Jouni Malinen1965c852009-04-22 21:38:25 +0300210{
Johannes Berg6829c872009-07-02 09:13:27 +0200211 struct wireless_dev *wdev = dev->ieee80211_ptr;
212 struct wiphy *wiphy = wdev->wiphy;
Jouni Malinen1965c852009-04-22 21:38:25 +0300213 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200214
Beni Lev4ee3e062012-08-27 12:49:39 +0300215 trace_cfg80211_send_assoc_timeout(dev, addr);
Johannes Berg667503dd2009-07-07 03:56:11 +0200216 wdev_lock(wdev);
Johannes Bergcb0b4be2009-07-07 03:56:07 +0200217
218 nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL);
Johannes Berg6829c872009-07-02 09:13:27 +0200219 if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Berg667503dd2009-07-07 03:56:11 +0200220 __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
221 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200222 false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200223
Johannes Berg667503dd2009-07-07 03:56:11 +0200224 wdev_unlock(wdev);
Jouni Malinen1965c852009-04-22 21:38:25 +0300225}
226EXPORT_SYMBOL(cfg80211_send_assoc_timeout);
227
Jouni Malinena3b8b052009-03-27 21:59:49 +0200228void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
229 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +0200230 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +0200231{
232 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
233 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg3d23e342009-09-29 23:27:28 +0200234#ifdef CONFIG_CFG80211_WEXT
Johannes Bergf58d4ed2009-06-19 02:45:21 +0200235 union iwreq_data wrqu;
Johannes Berge6d6e342009-07-01 21:26:47 +0200236 char *buf = kmalloc(128, gfp);
Johannes Bergf58d4ed2009-06-19 02:45:21 +0200237
238 if (buf) {
239 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
240 "keyid=%d %scast addr=%pM)", key_id,
241 key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
242 addr);
243 memset(&wrqu, 0, sizeof(wrqu));
244 wrqu.data.length = strlen(buf);
245 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
246 kfree(buf);
247 }
248#endif
249
Beni Lev4ee3e062012-08-27 12:49:39 +0300250 trace_cfg80211_michael_mic_failure(dev, addr, key_type, key_id, tsc);
Johannes Berge6d6e342009-07-01 21:26:47 +0200251 nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +0200252}
253EXPORT_SYMBOL(cfg80211_michael_mic_failure);
Johannes Berg19957bb2009-07-02 17:20:43 +0200254
255/* some MLME handling for userspace SME */
Johannes Berg667503dd2009-07-07 03:56:11 +0200256int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
257 struct net_device *dev,
258 struct ieee80211_channel *chan,
259 enum nl80211_auth_type auth_type,
260 const u8 *bssid,
261 const u8 *ssid, int ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200262 const u8 *ie, int ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +0300263 const u8 *key, int key_len, int key_idx,
264 const u8 *sae_data, int sae_data_len)
Johannes Berg19957bb2009-07-02 17:20:43 +0200265{
266 struct wireless_dev *wdev = dev->ieee80211_ptr;
267 struct cfg80211_auth_request req;
Johannes Berg95de8172012-01-20 13:55:25 +0100268 int err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200269
Johannes Berg667503dd2009-07-07 03:56:11 +0200270 ASSERT_WDEV_LOCK(wdev);
271
Johannes Bergfffd0932009-07-08 14:22:54 +0200272 if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
273 if (!key || !key_len || key_idx < 0 || key_idx > 4)
274 return -EINVAL;
275
Johannes Berg0a9b5e12009-07-02 18:26:18 +0200276 if (wdev->current_bss &&
Joe Perchesac422d32012-05-08 18:56:55 +0000277 ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
Johannes Berg0a9b5e12009-07-02 18:26:18 +0200278 return -EALREADY;
279
Johannes Berg19957bb2009-07-02 17:20:43 +0200280 memset(&req, 0, sizeof(req));
281
282 req.ie = ie;
283 req.ie_len = ie_len;
Jouni Malinene39e5b52012-09-30 19:29:39 +0300284 req.sae_data = sae_data;
285 req.sae_data_len = sae_data_len;
Johannes Berg19957bb2009-07-02 17:20:43 +0200286 req.auth_type = auth_type;
287 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
288 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200289 req.key = key;
290 req.key_len = key_len;
291 req.key_idx = key_idx;
Johannes Berg19957bb2009-07-02 17:20:43 +0200292 if (!req.bss)
293 return -ENOENT;
294
Michal Kaziore4e32452012-06-29 12:47:08 +0200295 err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
296 CHAN_MODE_SHARED);
297 if (err)
298 goto out;
299
Hila Gonene35e4d22012-06-27 17:19:42 +0300300 err = rdev_auth(rdev, dev, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200301
Michal Kaziore4e32452012-06-29 12:47:08 +0200302out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100303 cfg80211_put_bss(&rdev->wiphy, req.bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200304 return err;
305}
306
Johannes Berg667503dd2009-07-07 03:56:11 +0200307int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
308 struct net_device *dev, struct ieee80211_channel *chan,
309 enum nl80211_auth_type auth_type, const u8 *bssid,
310 const u8 *ssid, int ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200311 const u8 *ie, int ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +0300312 const u8 *key, int key_len, int key_idx,
313 const u8 *sae_data, int sae_data_len)
Johannes Berg667503dd2009-07-07 03:56:11 +0200314{
315 int err;
316
Michal Kaziore4e32452012-06-29 12:47:08 +0200317 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200318 wdev_lock(dev->ieee80211_ptr);
319 err = __cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +0200320 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +0300321 key, key_len, key_idx,
322 sae_data, sae_data_len);
Johannes Berg667503dd2009-07-07 03:56:11 +0200323 wdev_unlock(dev->ieee80211_ptr);
Michal Kaziore4e32452012-06-29 12:47:08 +0200324 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200325
326 return err;
327}
328
Ben Greear7e7c8922011-11-18 11:31:59 -0800329/* Do a logical ht_capa &= ht_capa_mask. */
330void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
331 const struct ieee80211_ht_cap *ht_capa_mask)
332{
333 int i;
334 u8 *p1, *p2;
335 if (!ht_capa_mask) {
336 memset(ht_capa, 0, sizeof(*ht_capa));
337 return;
338 }
339
340 p1 = (u8*)(ht_capa);
341 p2 = (u8*)(ht_capa_mask);
342 for (i = 0; i<sizeof(*ht_capa); i++)
343 p1[i] &= p2[i];
344}
345
Johannes Bergee2aca32013-02-21 17:36:01 +0100346/* Do a logical ht_capa &= ht_capa_mask. */
347void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
348 const struct ieee80211_vht_cap *vht_capa_mask)
349{
350 int i;
351 u8 *p1, *p2;
352 if (!vht_capa_mask) {
353 memset(vht_capa, 0, sizeof(*vht_capa));
354 return;
355 }
356
357 p1 = (u8*)(vht_capa);
358 p2 = (u8*)(vht_capa_mask);
359 for (i = 0; i < sizeof(*vht_capa); i++)
360 p1[i] &= p2[i];
361}
362
Johannes Berg667503dd2009-07-07 03:56:11 +0200363int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
364 struct net_device *dev,
365 struct ieee80211_channel *chan,
366 const u8 *bssid, const u8 *prev_bssid,
367 const u8 *ssid, int ssid_len,
368 const u8 *ie, int ie_len, bool use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -0800369 struct cfg80211_crypto_settings *crypt,
370 u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
Johannes Bergee2aca32013-02-21 17:36:01 +0100371 struct ieee80211_ht_cap *ht_capa_mask,
372 struct ieee80211_vht_cap *vht_capa,
373 struct ieee80211_vht_cap *vht_capa_mask)
Johannes Berg19957bb2009-07-02 17:20:43 +0200374{
375 struct wireless_dev *wdev = dev->ieee80211_ptr;
376 struct cfg80211_assoc_request req;
Johannes Berg95de8172012-01-20 13:55:25 +0100377 int err;
Jouni Malinen24b6b152009-11-17 21:35:38 +0200378 bool was_connected = false;
Johannes Berg19957bb2009-07-02 17:20:43 +0200379
Johannes Berg667503dd2009-07-07 03:56:11 +0200380 ASSERT_WDEV_LOCK(wdev);
381
Johannes Berg19957bb2009-07-02 17:20:43 +0200382 memset(&req, 0, sizeof(req));
383
Jouni Malinen24b6b152009-11-17 21:35:38 +0200384 if (wdev->current_bss && prev_bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000385 ether_addr_equal(wdev->current_bss->pub.bssid, prev_bssid)) {
Jouni Malinen24b6b152009-11-17 21:35:38 +0200386 /*
387 * Trying to reassociate: Allow this to proceed and let the old
388 * association to be dropped when the new one is completed.
389 */
390 if (wdev->sme_state == CFG80211_SME_CONNECTED) {
391 was_connected = true;
392 wdev->sme_state = CFG80211_SME_CONNECTING;
393 }
394 } else if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200395 return -EALREADY;
396
397 req.ie = ie;
398 req.ie_len = ie_len;
399 memcpy(&req.crypto, crypt, sizeof(req.crypto));
400 req.use_mfp = use_mfp;
Johannes Berg3e5d7642009-07-07 14:37:26 +0200401 req.prev_bssid = prev_bssid;
Ben Greear7e7c8922011-11-18 11:31:59 -0800402 req.flags = assoc_flags;
403 if (ht_capa)
404 memcpy(&req.ht_capa, ht_capa, sizeof(req.ht_capa));
405 if (ht_capa_mask)
406 memcpy(&req.ht_capa_mask, ht_capa_mask,
407 sizeof(req.ht_capa_mask));
408 cfg80211_oper_and_ht_capa(&req.ht_capa_mask,
409 rdev->wiphy.ht_capa_mod_mask);
Johannes Bergee2aca32013-02-21 17:36:01 +0100410 if (vht_capa)
411 memcpy(&req.vht_capa, vht_capa, sizeof(req.vht_capa));
412 if (vht_capa_mask)
413 memcpy(&req.vht_capa_mask, vht_capa_mask,
414 sizeof(req.vht_capa_mask));
415 cfg80211_oper_and_vht_capa(&req.vht_capa_mask,
416 rdev->wiphy.vht_capa_mod_mask);
Ben Greear7e7c8922011-11-18 11:31:59 -0800417
Johannes Berg19957bb2009-07-02 17:20:43 +0200418 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
419 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
Jouni Malinen24b6b152009-11-17 21:35:38 +0200420 if (!req.bss) {
421 if (was_connected)
422 wdev->sme_state = CFG80211_SME_CONNECTED;
Johannes Berg19957bb2009-07-02 17:20:43 +0200423 return -ENOENT;
Jouni Malinen24b6b152009-11-17 21:35:38 +0200424 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200425
Michal Kaziore4e32452012-06-29 12:47:08 +0200426 err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
427 CHAN_MODE_SHARED);
428 if (err)
429 goto out;
430
Hila Gonene35e4d22012-06-27 17:19:42 +0300431 err = rdev_assoc(rdev, dev, &req);
Johannes Berg95de8172012-01-20 13:55:25 +0100432
Michal Kaziore4e32452012-06-29 12:47:08 +0200433out:
Johannes Berg95de8172012-01-20 13:55:25 +0100434 if (err) {
435 if (was_connected)
436 wdev->sme_state = CFG80211_SME_CONNECTED;
Johannes Berg5b112d32013-02-01 01:49:58 +0100437 cfg80211_put_bss(&rdev->wiphy, req.bss);
Johannes Berg95de8172012-01-20 13:55:25 +0100438 }
439
Johannes Berg19957bb2009-07-02 17:20:43 +0200440 return err;
441}
442
Johannes Berg667503dd2009-07-07 03:56:11 +0200443int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
444 struct net_device *dev,
445 struct ieee80211_channel *chan,
446 const u8 *bssid, const u8 *prev_bssid,
447 const u8 *ssid, int ssid_len,
448 const u8 *ie, int ie_len, bool use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -0800449 struct cfg80211_crypto_settings *crypt,
450 u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
Johannes Bergee2aca32013-02-21 17:36:01 +0100451 struct ieee80211_ht_cap *ht_capa_mask,
452 struct ieee80211_vht_cap *vht_capa,
453 struct ieee80211_vht_cap *vht_capa_mask)
Johannes Berg667503dd2009-07-07 03:56:11 +0200454{
455 struct wireless_dev *wdev = dev->ieee80211_ptr;
456 int err;
457
Michal Kaziore4e32452012-06-29 12:47:08 +0200458 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200459 wdev_lock(wdev);
460 err = __cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
Ben Greear7e7c8922011-11-18 11:31:59 -0800461 ssid, ssid_len, ie, ie_len, use_mfp, crypt,
Johannes Bergee2aca32013-02-21 17:36:01 +0100462 assoc_flags, ht_capa, ht_capa_mask,
463 vht_capa, vht_capa_mask);
Johannes Berg667503dd2009-07-07 03:56:11 +0200464 wdev_unlock(wdev);
Michal Kaziore4e32452012-06-29 12:47:08 +0200465 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200466
467 return err;
468}
469
470int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
471 struct net_device *dev, const u8 *bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300472 const u8 *ie, int ie_len, u16 reason,
473 bool local_state_change)
Johannes Berg19957bb2009-07-02 17:20:43 +0200474{
475 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg95de8172012-01-20 13:55:25 +0100476 struct cfg80211_deauth_request req = {
477 .bssid = bssid,
478 .reason_code = reason,
479 .ie = ie,
480 .ie_len = ie_len,
Stanislaw Gruszka68632552012-10-15 14:52:41 +0200481 .local_state_change = local_state_change,
Johannes Berg95de8172012-01-20 13:55:25 +0100482 };
Johannes Berg19957bb2009-07-02 17:20:43 +0200483
Johannes Berg667503dd2009-07-07 03:56:11 +0200484 ASSERT_WDEV_LOCK(wdev);
485
Stanislaw Gruszka68632552012-10-15 14:52:41 +0200486 if (local_state_change && (!wdev->current_bss ||
487 !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
Johannes Berg95de8172012-01-20 13:55:25 +0100488 return 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200489
Hila Gonene35e4d22012-06-27 17:19:42 +0300490 return rdev_deauth(rdev, dev, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200491}
492
Johannes Berg667503dd2009-07-07 03:56:11 +0200493int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
494 struct net_device *dev, const u8 *bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300495 const u8 *ie, int ie_len, u16 reason,
496 bool local_state_change)
Johannes Berg667503dd2009-07-07 03:56:11 +0200497{
498 struct wireless_dev *wdev = dev->ieee80211_ptr;
499 int err;
500
501 wdev_lock(wdev);
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300502 err = __cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason,
503 local_state_change);
Johannes Berg667503dd2009-07-07 03:56:11 +0200504 wdev_unlock(wdev);
505
506 return err;
507}
508
509static int __cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
510 struct net_device *dev, const u8 *bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300511 const u8 *ie, int ie_len, u16 reason,
512 bool local_state_change)
Johannes Berg19957bb2009-07-02 17:20:43 +0200513{
514 struct wireless_dev *wdev = dev->ieee80211_ptr;
515 struct cfg80211_disassoc_request req;
516
Johannes Berg667503dd2009-07-07 03:56:11 +0200517 ASSERT_WDEV_LOCK(wdev);
518
Johannes Bergf9d6b402009-07-27 10:22:28 +0200519 if (wdev->sme_state != CFG80211_SME_CONNECTED)
520 return -ENOTCONN;
521
Johannes Berg8dcf0112013-01-26 22:16:08 +0100522 if (WARN(!wdev->current_bss, "sme_state=%d\n", wdev->sme_state))
Johannes Bergf9d6b402009-07-27 10:22:28 +0200523 return -ENOTCONN;
524
Johannes Berg19957bb2009-07-02 17:20:43 +0200525 memset(&req, 0, sizeof(req));
526 req.reason_code = reason;
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300527 req.local_state_change = local_state_change;
Johannes Berg19957bb2009-07-02 17:20:43 +0200528 req.ie = ie;
529 req.ie_len = ie_len;
Joe Perchesac422d32012-05-08 18:56:55 +0000530 if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
Johannes Berg19957bb2009-07-02 17:20:43 +0200531 req.bss = &wdev->current_bss->pub;
532 else
533 return -ENOTCONN;
534
Hila Gonene35e4d22012-06-27 17:19:42 +0300535 return rdev_disassoc(rdev, dev, &req);
Johannes Berg667503dd2009-07-07 03:56:11 +0200536}
537
538int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
539 struct net_device *dev, const u8 *bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300540 const u8 *ie, int ie_len, u16 reason,
541 bool local_state_change)
Johannes Berg667503dd2009-07-07 03:56:11 +0200542{
543 struct wireless_dev *wdev = dev->ieee80211_ptr;
544 int err;
545
546 wdev_lock(wdev);
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300547 err = __cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason,
548 local_state_change);
Johannes Berg667503dd2009-07-07 03:56:11 +0200549 wdev_unlock(wdev);
550
551 return err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200552}
553
554void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
555 struct net_device *dev)
556{
557 struct wireless_dev *wdev = dev->ieee80211_ptr;
558 struct cfg80211_deauth_request req;
Johannes Berg95de8172012-01-20 13:55:25 +0100559 u8 bssid[ETH_ALEN];
Johannes Berg19957bb2009-07-02 17:20:43 +0200560
Johannes Berg667503dd2009-07-07 03:56:11 +0200561 ASSERT_WDEV_LOCK(wdev);
562
Johannes Berg19957bb2009-07-02 17:20:43 +0200563 if (!rdev->ops->deauth)
564 return;
565
566 memset(&req, 0, sizeof(req));
567 req.reason_code = WLAN_REASON_DEAUTH_LEAVING;
568 req.ie = NULL;
569 req.ie_len = 0;
570
Johannes Berg95de8172012-01-20 13:55:25 +0100571 if (!wdev->current_bss)
572 return;
Johannes Berg19957bb2009-07-02 17:20:43 +0200573
Johannes Berg95de8172012-01-20 13:55:25 +0100574 memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
575 req.bssid = bssid;
Hila Gonene35e4d22012-06-27 17:19:42 +0300576 rdev_deauth(rdev, dev, &req);
Johannes Berg95de8172012-01-20 13:55:25 +0100577
578 if (wdev->current_bss) {
579 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100580 cfg80211_put_bss(&rdev->wiphy, &wdev->current_bss->pub);
Johannes Berg95de8172012-01-20 13:55:25 +0100581 wdev->current_bss = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200582 }
583}
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100584
Johannes Berg2e161f72010-08-12 15:38:38 +0200585struct cfg80211_mgmt_registration {
Jouni Malinen026331c2010-02-15 12:53:10 +0200586 struct list_head list;
587
Eric W. Biederman15e47302012-09-07 20:12:54 +0000588 u32 nlportid;
Jouni Malinen026331c2010-02-15 12:53:10 +0200589
590 int match_len;
591
Johannes Berg2e161f72010-08-12 15:38:38 +0200592 __le16 frame_type;
593
Jouni Malinen026331c2010-02-15 12:53:10 +0200594 u8 match[];
595};
596
Eric W. Biederman15e47302012-09-07 20:12:54 +0000597int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
Johannes Berg2e161f72010-08-12 15:38:38 +0200598 u16 frame_type, const u8 *match_data,
599 int match_len)
Jouni Malinen026331c2010-02-15 12:53:10 +0200600{
Johannes Berg271733c2010-10-13 12:06:23 +0200601 struct wiphy *wiphy = wdev->wiphy;
602 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg2e161f72010-08-12 15:38:38 +0200603 struct cfg80211_mgmt_registration *reg, *nreg;
Jouni Malinen026331c2010-02-15 12:53:10 +0200604 int err = 0;
Johannes Berg2e161f72010-08-12 15:38:38 +0200605 u16 mgmt_type;
606
607 if (!wdev->wiphy->mgmt_stypes)
608 return -EOPNOTSUPP;
609
610 if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
611 return -EINVAL;
612
613 if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
614 return -EINVAL;
615
616 mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
617 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
618 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +0200619
620 nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
621 if (!nreg)
622 return -ENOMEM;
623
Johannes Berg2e161f72010-08-12 15:38:38 +0200624 spin_lock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200625
Johannes Berg2e161f72010-08-12 15:38:38 +0200626 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +0200627 int mlen = min(match_len, reg->match_len);
628
Johannes Berg2e161f72010-08-12 15:38:38 +0200629 if (frame_type != le16_to_cpu(reg->frame_type))
630 continue;
631
Jouni Malinen026331c2010-02-15 12:53:10 +0200632 if (memcmp(reg->match, match_data, mlen) == 0) {
633 err = -EALREADY;
634 break;
635 }
636 }
637
638 if (err) {
639 kfree(nreg);
640 goto out;
641 }
642
643 memcpy(nreg->match, match_data, match_len);
644 nreg->match_len = match_len;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000645 nreg->nlportid = snd_portid;
Johannes Berg2e161f72010-08-12 15:38:38 +0200646 nreg->frame_type = cpu_to_le16(frame_type);
647 list_add(&nreg->list, &wdev->mgmt_registrations);
Jouni Malinen026331c2010-02-15 12:53:10 +0200648
Johannes Berg271733c2010-10-13 12:06:23 +0200649 if (rdev->ops->mgmt_frame_register)
Hila Gonene35e4d22012-06-27 17:19:42 +0300650 rdev_mgmt_frame_register(rdev, wdev, frame_type, true);
Johannes Berg271733c2010-10-13 12:06:23 +0200651
Jouni Malinen026331c2010-02-15 12:53:10 +0200652 out:
Johannes Berg2e161f72010-08-12 15:38:38 +0200653 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Johannes Berg271733c2010-10-13 12:06:23 +0200654
Jouni Malinen026331c2010-02-15 12:53:10 +0200655 return err;
656}
657
Eric W. Biederman15e47302012-09-07 20:12:54 +0000658void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
Jouni Malinen026331c2010-02-15 12:53:10 +0200659{
Johannes Berg271733c2010-10-13 12:06:23 +0200660 struct wiphy *wiphy = wdev->wiphy;
661 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg2e161f72010-08-12 15:38:38 +0200662 struct cfg80211_mgmt_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +0200663
Johannes Berg2e161f72010-08-12 15:38:38 +0200664 spin_lock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200665
Johannes Berg2e161f72010-08-12 15:38:38 +0200666 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000667 if (reg->nlportid != nlportid)
Johannes Berg271733c2010-10-13 12:06:23 +0200668 continue;
669
670 if (rdev->ops->mgmt_frame_register) {
671 u16 frame_type = le16_to_cpu(reg->frame_type);
672
Hila Gonene35e4d22012-06-27 17:19:42 +0300673 rdev_mgmt_frame_register(rdev, wdev,
674 frame_type, false);
Jouni Malinen026331c2010-02-15 12:53:10 +0200675 }
Johannes Berg271733c2010-10-13 12:06:23 +0200676
677 list_del(&reg->list);
678 kfree(reg);
Jouni Malinen026331c2010-02-15 12:53:10 +0200679 }
680
Johannes Berg2e161f72010-08-12 15:38:38 +0200681 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Johannes Berg28946da2011-11-04 11:18:12 +0100682
Eric W. Biederman15e47302012-09-07 20:12:54 +0000683 if (nlportid == wdev->ap_unexpected_nlportid)
684 wdev->ap_unexpected_nlportid = 0;
Jouni Malinen026331c2010-02-15 12:53:10 +0200685}
686
Johannes Berg2e161f72010-08-12 15:38:38 +0200687void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
Jouni Malinen026331c2010-02-15 12:53:10 +0200688{
Johannes Berg2e161f72010-08-12 15:38:38 +0200689 struct cfg80211_mgmt_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +0200690
Johannes Berg2e161f72010-08-12 15:38:38 +0200691 spin_lock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200692
Johannes Berg2e161f72010-08-12 15:38:38 +0200693 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +0200694 list_del(&reg->list);
695 kfree(reg);
696 }
697
Johannes Berg2e161f72010-08-12 15:38:38 +0200698 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200699}
700
Johannes Berg2e161f72010-08-12 15:38:38 +0200701int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +0200702 struct wireless_dev *wdev,
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100703 struct ieee80211_channel *chan, bool offchan,
Johannes Berg42d97a52012-11-08 18:31:02 +0100704 unsigned int wait, const u8 *buf, size_t len,
705 bool no_cck, bool dont_wait_for_ack, u64 *cookie)
Jouni Malinen026331c2010-02-15 12:53:10 +0200706{
Jouni Malinen026331c2010-02-15 12:53:10 +0200707 const struct ieee80211_mgmt *mgmt;
Johannes Berg2e161f72010-08-12 15:38:38 +0200708 u16 stype;
Jouni Malinen026331c2010-02-15 12:53:10 +0200709
Johannes Berg2e161f72010-08-12 15:38:38 +0200710 if (!wdev->wiphy->mgmt_stypes)
Jouni Malinen026331c2010-02-15 12:53:10 +0200711 return -EOPNOTSUPP;
Johannes Berg2e161f72010-08-12 15:38:38 +0200712
713 if (!rdev->ops->mgmt_tx)
714 return -EOPNOTSUPP;
715
Jouni Malinen026331c2010-02-15 12:53:10 +0200716 if (len < 24 + 1)
717 return -EINVAL;
718
719 mgmt = (const struct ieee80211_mgmt *) buf;
Johannes Berg2e161f72010-08-12 15:38:38 +0200720
721 if (!ieee80211_is_mgmt(mgmt->frame_control))
Jouni Malinen026331c2010-02-15 12:53:10 +0200722 return -EINVAL;
Johannes Berg2e161f72010-08-12 15:38:38 +0200723
724 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
725 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
726 return -EINVAL;
727
728 if (ieee80211_is_action(mgmt->frame_control) &&
729 mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
Johannes Berg663fcaf2010-09-30 21:06:09 +0200730 int err = 0;
731
Johannes Bergfe100ac2010-08-09 15:52:03 +0200732 wdev_lock(wdev);
733
Johannes Berg663fcaf2010-09-30 21:06:09 +0200734 switch (wdev->iftype) {
735 case NL80211_IFTYPE_ADHOC:
736 case NL80211_IFTYPE_STATION:
737 case NL80211_IFTYPE_P2P_CLIENT:
738 if (!wdev->current_bss) {
739 err = -ENOTCONN;
740 break;
741 }
Johannes Bergfe100ac2010-08-09 15:52:03 +0200742
Joe Perchesac422d32012-05-08 18:56:55 +0000743 if (!ether_addr_equal(wdev->current_bss->pub.bssid,
744 mgmt->bssid)) {
Johannes Berg663fcaf2010-09-30 21:06:09 +0200745 err = -ENOTCONN;
746 break;
747 }
748
749 /*
750 * check for IBSS DA must be done by driver as
751 * cfg80211 doesn't track the stations
752 */
753 if (wdev->iftype == NL80211_IFTYPE_ADHOC)
754 break;
755
756 /* for station, check that DA is the AP */
Joe Perchesac422d32012-05-08 18:56:55 +0000757 if (!ether_addr_equal(wdev->current_bss->pub.bssid,
758 mgmt->da)) {
Johannes Berg663fcaf2010-09-30 21:06:09 +0200759 err = -ENOTCONN;
760 break;
761 }
762 break;
763 case NL80211_IFTYPE_AP:
764 case NL80211_IFTYPE_P2P_GO:
765 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg98104fde2012-06-16 00:19:54 +0200766 if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
Johannes Berg663fcaf2010-09-30 21:06:09 +0200767 err = -EINVAL;
768 break;
Javier Cardona0778a6a2011-05-03 16:57:08 -0700769 case NL80211_IFTYPE_MESH_POINT:
Joe Perchesac422d32012-05-08 18:56:55 +0000770 if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
Javier Cardona0778a6a2011-05-03 16:57:08 -0700771 err = -EINVAL;
772 break;
773 }
774 /*
775 * check for mesh DA must be done by driver as
776 * cfg80211 doesn't track the stations
777 */
778 break;
Johannes Berg98104fde2012-06-16 00:19:54 +0200779 case NL80211_IFTYPE_P2P_DEVICE:
780 /*
781 * fall through, P2P device only supports
782 * public action frames
783 */
Johannes Berg663fcaf2010-09-30 21:06:09 +0200784 default:
785 err = -EOPNOTSUPP;
786 break;
787 }
Johannes Bergfe100ac2010-08-09 15:52:03 +0200788 wdev_unlock(wdev);
Johannes Berg663fcaf2010-09-30 21:06:09 +0200789
790 if (err)
791 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +0200792 }
793
Johannes Berg98104fde2012-06-16 00:19:54 +0200794 if (!ether_addr_equal(mgmt->sa, wdev_address(wdev)))
Jouni Malinen026331c2010-02-15 12:53:10 +0200795 return -EINVAL;
796
797 /* Transmit the Action frame as requested by user space */
Hila Gonene35e4d22012-06-27 17:19:42 +0300798 return rdev_mgmt_tx(rdev, wdev, chan, offchan,
Hila Gonene35e4d22012-06-27 17:19:42 +0300799 wait, buf, len, no_cck, dont_wait_for_ack,
800 cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +0200801}
802
Johannes Berg71bbc992012-06-15 15:30:18 +0200803bool cfg80211_rx_mgmt(struct wireless_dev *wdev, int freq, int sig_mbm,
Johannes Berg804483e2012-03-05 22:18:41 +0100804 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +0200805{
Jouni Malinen026331c2010-02-15 12:53:10 +0200806 struct wiphy *wiphy = wdev->wiphy;
807 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg2e161f72010-08-12 15:38:38 +0200808 struct cfg80211_mgmt_registration *reg;
809 const struct ieee80211_txrx_stypes *stypes =
810 &wiphy->mgmt_stypes[wdev->iftype];
811 struct ieee80211_mgmt *mgmt = (void *)buf;
812 const u8 *data;
813 int data_len;
Jouni Malinen026331c2010-02-15 12:53:10 +0200814 bool result = false;
Johannes Berg2e161f72010-08-12 15:38:38 +0200815 __le16 ftype = mgmt->frame_control &
816 cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
817 u16 stype;
Jouni Malinen026331c2010-02-15 12:53:10 +0200818
Beni Lev4ee3e062012-08-27 12:49:39 +0300819 trace_cfg80211_rx_mgmt(wdev, freq, sig_mbm);
Johannes Berg2e161f72010-08-12 15:38:38 +0200820 stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
Jouni Malinen026331c2010-02-15 12:53:10 +0200821
Beni Lev4ee3e062012-08-27 12:49:39 +0300822 if (!(stypes->rx & BIT(stype))) {
823 trace_cfg80211_return_bool(false);
Johannes Berg2e161f72010-08-12 15:38:38 +0200824 return false;
Beni Lev4ee3e062012-08-27 12:49:39 +0300825 }
Jouni Malinen026331c2010-02-15 12:53:10 +0200826
Johannes Berg2e161f72010-08-12 15:38:38 +0200827 data = buf + ieee80211_hdrlen(mgmt->frame_control);
828 data_len = len - ieee80211_hdrlen(mgmt->frame_control);
Jouni Malinen026331c2010-02-15 12:53:10 +0200829
Johannes Berg2e161f72010-08-12 15:38:38 +0200830 spin_lock_bh(&wdev->mgmt_registrations_lock);
831
832 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
833 if (reg->frame_type != ftype)
Jouni Malinen026331c2010-02-15 12:53:10 +0200834 continue;
835
Johannes Berg2e161f72010-08-12 15:38:38 +0200836 if (reg->match_len > data_len)
837 continue;
838
839 if (memcmp(reg->match, data, reg->match_len))
Jouni Malinen026331c2010-02-15 12:53:10 +0200840 continue;
841
842 /* found match! */
843
844 /* Indicate the received Action frame to user space */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000845 if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +0100846 freq, sig_mbm,
Johannes Berg2e161f72010-08-12 15:38:38 +0200847 buf, len, gfp))
Jouni Malinen026331c2010-02-15 12:53:10 +0200848 continue;
849
850 result = true;
851 break;
852 }
853
Johannes Berg2e161f72010-08-12 15:38:38 +0200854 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200855
Beni Lev4ee3e062012-08-27 12:49:39 +0300856 trace_cfg80211_return_bool(result);
Jouni Malinen026331c2010-02-15 12:53:10 +0200857 return result;
858}
Johannes Berg2e161f72010-08-12 15:38:38 +0200859EXPORT_SYMBOL(cfg80211_rx_mgmt);
Jouni Malinen026331c2010-02-15 12:53:10 +0200860
Simon Wunderlich04f39042013-02-08 18:16:19 +0100861void cfg80211_dfs_channels_update_work(struct work_struct *work)
862{
863 struct delayed_work *delayed_work;
864 struct cfg80211_registered_device *rdev;
865 struct cfg80211_chan_def chandef;
866 struct ieee80211_supported_band *sband;
867 struct ieee80211_channel *c;
868 struct wiphy *wiphy;
869 bool check_again = false;
870 unsigned long timeout, next_time = 0;
871 int bandid, i;
872
873 delayed_work = container_of(work, struct delayed_work, work);
874 rdev = container_of(delayed_work, struct cfg80211_registered_device,
875 dfs_update_channels_wk);
876 wiphy = &rdev->wiphy;
877
878 mutex_lock(&cfg80211_mutex);
879 for (bandid = 0; bandid < IEEE80211_NUM_BANDS; bandid++) {
880 sband = wiphy->bands[bandid];
881 if (!sband)
882 continue;
883
884 for (i = 0; i < sband->n_channels; i++) {
885 c = &sband->channels[i];
886
887 if (c->dfs_state != NL80211_DFS_UNAVAILABLE)
888 continue;
889
890 timeout = c->dfs_state_entered +
891 IEEE80211_DFS_MIN_NOP_TIME_MS;
892
893 if (time_after_eq(jiffies, timeout)) {
894 c->dfs_state = NL80211_DFS_USABLE;
895 cfg80211_chandef_create(&chandef, c,
896 NL80211_CHAN_NO_HT);
897
898 nl80211_radar_notify(rdev, &chandef,
899 NL80211_RADAR_NOP_FINISHED,
900 NULL, GFP_ATOMIC);
901 continue;
902 }
903
904 if (!check_again)
905 next_time = timeout - jiffies;
906 else
907 next_time = min(next_time, timeout - jiffies);
908 check_again = true;
909 }
910 }
911 mutex_unlock(&cfg80211_mutex);
912
913 /* reschedule if there are other channels waiting to be cleared again */
914 if (check_again)
915 queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
916 next_time);
917}
918
919
920void cfg80211_radar_event(struct wiphy *wiphy,
921 struct cfg80211_chan_def *chandef,
922 gfp_t gfp)
923{
924 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
925 unsigned long timeout;
926
927 trace_cfg80211_radar_event(wiphy, chandef);
928
929 /* only set the chandef supplied channel to unavailable, in
930 * case the radar is detected on only one of multiple channels
931 * spanned by the chandef.
932 */
933 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE);
934
935 timeout = msecs_to_jiffies(IEEE80211_DFS_MIN_NOP_TIME_MS);
936 queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
937 timeout);
938
939 nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp);
940}
941EXPORT_SYMBOL(cfg80211_radar_event);
942
943void cfg80211_cac_event(struct net_device *netdev,
944 enum nl80211_radar_event event, gfp_t gfp)
945{
946 struct wireless_dev *wdev = netdev->ieee80211_ptr;
947 struct wiphy *wiphy = wdev->wiphy;
948 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
949 struct cfg80211_chan_def chandef;
950 unsigned long timeout;
951
952 trace_cfg80211_cac_event(netdev, event);
953
954 if (WARN_ON(!wdev->cac_started))
955 return;
956
957 if (WARN_ON(!wdev->channel))
958 return;
959
960 cfg80211_chandef_create(&chandef, wdev->channel, NL80211_CHAN_NO_HT);
961
962 switch (event) {
963 case NL80211_RADAR_CAC_FINISHED:
964 timeout = wdev->cac_start_time +
965 msecs_to_jiffies(IEEE80211_DFS_MIN_CAC_TIME_MS);
966 WARN_ON(!time_after_eq(jiffies, timeout));
967 cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_AVAILABLE);
968 break;
969 case NL80211_RADAR_CAC_ABORTED:
970 break;
971 default:
972 WARN_ON(1);
973 return;
974 }
975 wdev->cac_started = false;
976
977 nl80211_radar_notify(rdev, &chandef, event, netdev, gfp);
978}
979EXPORT_SYMBOL(cfg80211_cac_event);