blob: cbb48e26a8715b8964333bf9b69c52010289bafb [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>
Johannes Berg33d87832015-06-23 17:47:05 +02005 * Copyright (c) 2015 Intel Deutschland GmbH
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
Felix Fietkauc6fb08a2012-03-18 22:58:04 +010010#include <linux/etherdevice.h>
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011#include <linux/netdevice.h>
12#include <linux/nl80211.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Johannes Berga9a11622009-07-27 12:01:53 +020014#include <linux/wireless.h>
Jouni Malinen6039f6d2009-03-19 13:39:21 +020015#include <net/cfg80211.h>
Johannes Berga9a11622009-07-27 12:01:53 +020016#include <net/iw_handler.h>
Jouni Malinen6039f6d2009-03-19 13:39:21 +020017#include "core.h"
18#include "nl80211.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030019#include "rdev-ops.h"
20
Jouni Malinen6039f6d2009-03-19 13:39:21 +020021
Johannes Berg6ff57cf2013-05-16 00:55:00 +020022void cfg80211_rx_assoc_resp(struct net_device *dev, struct cfg80211_bss *bss,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030023 const u8 *buf, size_t len, int uapsd_queues)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020024{
Johannes Berg6829c872009-07-02 09:13:27 +020025 struct wireless_dev *wdev = dev->ieee80211_ptr;
26 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080027 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020028 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
29 u8 *ie = mgmt->u.assoc_resp.variable;
Johannes Berg95de8172012-01-20 13:55:25 +010030 int ieoffs = offsetof(struct ieee80211_mgmt, u.assoc_resp.variable);
Johannes Bergceca7b72013-05-16 00:55:45 +020031 u16 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
Johannes Berg6829c872009-07-02 09:13:27 +020032
Beni Lev4ee3e062012-08-27 12:49:39 +030033 trace_cfg80211_send_rx_assoc(dev, bss);
Johannes Bergcb0b4be2009-07-07 03:56:07 +020034
Johannes Bergf401a6f2009-08-07 14:51:05 +020035 /*
36 * This is a bit of a hack, we don't notify userspace of
37 * a (re-)association reply if we tried to send a reassoc
38 * and got a reject -- we only try again with an assoc
39 * frame instead of reassoc.
40 */
Johannes Bergceca7b72013-05-16 00:55:45 +020041 if (cfg80211_sme_rx_assoc_resp(wdev, status_code)) {
Johannes Bergf1940c52013-06-19 13:21:15 +020042 cfg80211_unhold_bss(bss_from_pub(bss));
Johannes Berg5b112d32013-02-01 01:49:58 +010043 cfg80211_put_bss(wiphy, bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +020044 return;
Johannes Berg95de8172012-01-20 13:55:25 +010045 }
Johannes Bergf401a6f2009-08-07 14:51:05 +020046
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030047 nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL, uapsd_queues);
Johannes Bergceca7b72013-05-16 00:55:45 +020048 /* update current_bss etc., consumes the bss reference */
Johannes Bergdf7fc0f2009-07-29 11:23:49 +020049 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, ie, len - ieoffs,
50 status_code,
Johannes Berg95de8172012-01-20 13:55:25 +010051 status_code == WLAN_STATUS_SUCCESS, bss);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020052}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020053EXPORT_SYMBOL(cfg80211_rx_assoc_resp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020054
Johannes Bergceca7b72013-05-16 00:55:45 +020055static void cfg80211_process_auth(struct wireless_dev *wdev,
56 const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020057{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080058 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020059
Johannes Bergceca7b72013-05-16 00:55:45 +020060 nl80211_send_rx_auth(rdev, wdev->netdev, buf, len, GFP_KERNEL);
61 cfg80211_sme_rx_auth(wdev, buf, len);
Johannes Berg667503dd2009-07-07 03:56:11 +020062}
Jouni Malinen6039f6d2009-03-19 13:39:21 +020063
Johannes Bergceca7b72013-05-16 00:55:45 +020064static void cfg80211_process_deauth(struct wireless_dev *wdev,
65 const u8 *buf, size_t len)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020066{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080067 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020068 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
Johannes Berg19957bb2009-07-02 17:20:43 +020069 const u8 *bssid = mgmt->bssid;
Johannes Bergceca7b72013-05-16 00:55:45 +020070 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
71 bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr);
Johannes Berg6829c872009-07-02 09:13:27 +020072
Johannes Bergceca7b72013-05-16 00:55:45 +020073 nl80211_send_deauth(rdev, wdev->netdev, buf, len, GFP_KERNEL);
Johannes Berg6829c872009-07-02 09:13:27 +020074
Johannes Bergceca7b72013-05-16 00:55:45 +020075 if (!wdev->current_bss ||
76 !ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
Johannes Berg596a07c2009-07-11 00:17:32 +020077 return;
Johannes Berg6829c872009-07-02 09:13:27 +020078
Johannes Bergceca7b72013-05-16 00:55:45 +020079 __cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap);
80 cfg80211_sme_deauth(wdev);
81}
Johannes Berg6829c872009-07-02 09:13:27 +020082
Johannes Bergceca7b72013-05-16 00:55:45 +020083static void cfg80211_process_disassoc(struct wireless_dev *wdev,
84 const u8 *buf, size_t len)
85{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080086 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergceca7b72013-05-16 00:55:45 +020087 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
88 const u8 *bssid = mgmt->bssid;
89 u16 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
90 bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr);
Johannes Berg19957bb2009-07-02 17:20:43 +020091
Johannes Bergceca7b72013-05-16 00:55:45 +020092 nl80211_send_disassoc(rdev, wdev->netdev, buf, len, GFP_KERNEL);
93
94 if (WARN_ON(!wdev->current_bss ||
95 !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
96 return;
97
98 __cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap);
99 cfg80211_sme_disassoc(wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200100}
Jouni Malinena3b8b052009-03-27 21:59:49 +0200101
Johannes Berg6ff57cf2013-05-16 00:55:00 +0200102void cfg80211_rx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len)
103{
104 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg6ff57cf2013-05-16 00:55:00 +0200105 struct ieee80211_mgmt *mgmt = (void *)buf;
106
107 ASSERT_WDEV_LOCK(wdev);
108
109 trace_cfg80211_rx_mlme_mgmt(dev, buf, len);
110
111 if (WARN_ON(len < 2))
112 return;
113
Johannes Bergceca7b72013-05-16 00:55:45 +0200114 if (ieee80211_is_auth(mgmt->frame_control))
115 cfg80211_process_auth(wdev, buf, len);
116 else if (ieee80211_is_deauth(mgmt->frame_control))
117 cfg80211_process_deauth(wdev, buf, len);
118 else if (ieee80211_is_disassoc(mgmt->frame_control))
119 cfg80211_process_disassoc(wdev, buf, len);
Johannes Berg6ff57cf2013-05-16 00:55:00 +0200120}
121EXPORT_SYMBOL(cfg80211_rx_mlme_mgmt);
122
123void cfg80211_auth_timeout(struct net_device *dev, const u8 *addr)
Johannes Berga58ce432009-11-19 12:45:42 +0100124{
125 struct wireless_dev *wdev = dev->ieee80211_ptr;
126 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800127 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berga58ce432009-11-19 12:45:42 +0100128
Beni Lev4ee3e062012-08-27 12:49:39 +0300129 trace_cfg80211_send_auth_timeout(dev, addr);
Johannes Berga58ce432009-11-19 12:45:42 +0100130
131 nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200132 cfg80211_sme_auth_timeout(wdev);
Jouni Malinen1965c852009-04-22 21:38:25 +0300133}
Johannes Berg6ff57cf2013-05-16 00:55:00 +0200134EXPORT_SYMBOL(cfg80211_auth_timeout);
Jouni Malinen1965c852009-04-22 21:38:25 +0300135
Johannes Berg959867f2013-06-19 13:05:42 +0200136void cfg80211_assoc_timeout(struct net_device *dev, struct cfg80211_bss *bss)
Jouni Malinen1965c852009-04-22 21:38:25 +0300137{
Johannes Berg6829c872009-07-02 09:13:27 +0200138 struct wireless_dev *wdev = dev->ieee80211_ptr;
139 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800140 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200141
Johannes Berg959867f2013-06-19 13:05:42 +0200142 trace_cfg80211_send_assoc_timeout(dev, bss->bssid);
Johannes Bergcb0b4be2009-07-07 03:56:07 +0200143
Johannes Berg959867f2013-06-19 13:05:42 +0200144 nl80211_send_assoc_timeout(rdev, dev, bss->bssid, GFP_KERNEL);
Johannes Bergceca7b72013-05-16 00:55:45 +0200145 cfg80211_sme_assoc_timeout(wdev);
Johannes Berg959867f2013-06-19 13:05:42 +0200146
Johannes Bergf1940c52013-06-19 13:21:15 +0200147 cfg80211_unhold_bss(bss_from_pub(bss));
Johannes Berg959867f2013-06-19 13:05:42 +0200148 cfg80211_put_bss(wiphy, bss);
Jouni Malinen1965c852009-04-22 21:38:25 +0300149}
Johannes Berg6ff57cf2013-05-16 00:55:00 +0200150EXPORT_SYMBOL(cfg80211_assoc_timeout);
151
152void cfg80211_tx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len)
153{
154 struct wireless_dev *wdev = dev->ieee80211_ptr;
155 struct ieee80211_mgmt *mgmt = (void *)buf;
156
157 ASSERT_WDEV_LOCK(wdev);
158
159 trace_cfg80211_tx_mlme_mgmt(dev, buf, len);
160
161 if (WARN_ON(len < 2))
162 return;
163
164 if (ieee80211_is_deauth(mgmt->frame_control))
Johannes Bergceca7b72013-05-16 00:55:45 +0200165 cfg80211_process_deauth(wdev, buf, len);
Johannes Berg6ff57cf2013-05-16 00:55:00 +0200166 else
Johannes Bergceca7b72013-05-16 00:55:45 +0200167 cfg80211_process_disassoc(wdev, buf, len);
Johannes Berg6ff57cf2013-05-16 00:55:00 +0200168}
169EXPORT_SYMBOL(cfg80211_tx_mlme_mgmt);
Jouni Malinen1965c852009-04-22 21:38:25 +0300170
Jouni Malinena3b8b052009-03-27 21:59:49 +0200171void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
172 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +0200173 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +0200174{
175 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800176 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg3d23e342009-09-29 23:27:28 +0200177#ifdef CONFIG_CFG80211_WEXT
Johannes Bergf58d4ed2009-06-19 02:45:21 +0200178 union iwreq_data wrqu;
Johannes Berge6d6e342009-07-01 21:26:47 +0200179 char *buf = kmalloc(128, gfp);
Johannes Bergf58d4ed2009-06-19 02:45:21 +0200180
181 if (buf) {
182 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
183 "keyid=%d %scast addr=%pM)", key_id,
184 key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
185 addr);
186 memset(&wrqu, 0, sizeof(wrqu));
187 wrqu.data.length = strlen(buf);
188 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
189 kfree(buf);
190 }
191#endif
192
Beni Lev4ee3e062012-08-27 12:49:39 +0300193 trace_cfg80211_michael_mic_failure(dev, addr, key_type, key_id, tsc);
Johannes Berge6d6e342009-07-01 21:26:47 +0200194 nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +0200195}
196EXPORT_SYMBOL(cfg80211_michael_mic_failure);
Johannes Berg19957bb2009-07-02 17:20:43 +0200197
198/* some MLME handling for userspace SME */
Johannes Berg91bf9b22013-05-15 17:44:01 +0200199int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
200 struct net_device *dev,
201 struct ieee80211_channel *chan,
202 enum nl80211_auth_type auth_type,
203 const u8 *bssid,
204 const u8 *ssid, int ssid_len,
205 const u8 *ie, int ie_len,
206 const u8 *key, int key_len, int key_idx,
207 const u8 *sae_data, int sae_data_len)
Johannes Berg19957bb2009-07-02 17:20:43 +0200208{
209 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg7ade7032013-05-13 11:37:30 +0200210 struct cfg80211_auth_request req = {
211 .ie = ie,
212 .ie_len = ie_len,
213 .sae_data = sae_data,
214 .sae_data_len = sae_data_len,
215 .auth_type = auth_type,
216 .key = key,
217 .key_len = key_len,
218 .key_idx = key_idx,
219 };
Johannes Berg95de8172012-01-20 13:55:25 +0100220 int err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200221
Johannes Berg667503dd2009-07-07 03:56:11 +0200222 ASSERT_WDEV_LOCK(wdev);
223
Johannes Bergfffd0932009-07-08 14:22:54 +0200224 if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
Johannes Bergb6b55552016-09-13 16:25:58 +0200225 if (!key || !key_len || key_idx < 0 || key_idx > 3)
Johannes Bergfffd0932009-07-08 14:22:54 +0200226 return -EINVAL;
227
Johannes Berg0a9b5e12009-07-02 18:26:18 +0200228 if (wdev->current_bss &&
Joe Perchesac422d32012-05-08 18:56:55 +0000229 ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
Johannes Berg0a9b5e12009-07-02 18:26:18 +0200230 return -EALREADY;
231
Johannes Berg19957bb2009-07-02 17:20:43 +0200232 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200233 IEEE80211_BSS_TYPE_ESS,
234 IEEE80211_PRIVACY_ANY);
Johannes Berg19957bb2009-07-02 17:20:43 +0200235 if (!req.bss)
236 return -ENOENT;
237
Hila Gonene35e4d22012-06-27 17:19:42 +0300238 err = rdev_auth(rdev, dev, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200239
Johannes Berg5b112d32013-02-01 01:49:58 +0100240 cfg80211_put_bss(&rdev->wiphy, req.bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200241 return err;
242}
243
Ben Greear7e7c8922011-11-18 11:31:59 -0800244/* Do a logical ht_capa &= ht_capa_mask. */
245void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
246 const struct ieee80211_ht_cap *ht_capa_mask)
247{
248 int i;
249 u8 *p1, *p2;
250 if (!ht_capa_mask) {
251 memset(ht_capa, 0, sizeof(*ht_capa));
252 return;
253 }
254
255 p1 = (u8*)(ht_capa);
256 p2 = (u8*)(ht_capa_mask);
257 for (i = 0; i<sizeof(*ht_capa); i++)
258 p1[i] &= p2[i];
259}
260
Johannes Bergee2aca32013-02-21 17:36:01 +0100261/* Do a logical ht_capa &= ht_capa_mask. */
262void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
263 const struct ieee80211_vht_cap *vht_capa_mask)
264{
265 int i;
266 u8 *p1, *p2;
267 if (!vht_capa_mask) {
268 memset(vht_capa, 0, sizeof(*vht_capa));
269 return;
270 }
271
272 p1 = (u8*)(vht_capa);
273 p2 = (u8*)(vht_capa_mask);
274 for (i = 0; i < sizeof(*vht_capa); i++)
275 p1[i] &= p2[i];
276}
277
Johannes Berg91bf9b22013-05-15 17:44:01 +0200278int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
279 struct net_device *dev,
280 struct ieee80211_channel *chan,
281 const u8 *bssid,
282 const u8 *ssid, int ssid_len,
283 struct cfg80211_assoc_request *req)
Johannes Berg19957bb2009-07-02 17:20:43 +0200284{
285 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg95de8172012-01-20 13:55:25 +0100286 int err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200287
Johannes Berg667503dd2009-07-07 03:56:11 +0200288 ASSERT_WDEV_LOCK(wdev);
289
Johannes Bergceca7b72013-05-16 00:55:45 +0200290 if (wdev->current_bss &&
291 (!req->prev_bssid || !ether_addr_equal(wdev->current_bss->pub.bssid,
292 req->prev_bssid)))
Johannes Berg19957bb2009-07-02 17:20:43 +0200293 return -EALREADY;
294
Johannes Bergf62fab72013-02-21 20:09:09 +0100295 cfg80211_oper_and_ht_capa(&req->ht_capa_mask,
Ben Greear7e7c8922011-11-18 11:31:59 -0800296 rdev->wiphy.ht_capa_mod_mask);
Johannes Bergf62fab72013-02-21 20:09:09 +0100297 cfg80211_oper_and_vht_capa(&req->vht_capa_mask,
Johannes Bergee2aca32013-02-21 17:36:01 +0100298 rdev->wiphy.vht_capa_mod_mask);
Ben Greear7e7c8922011-11-18 11:31:59 -0800299
Johannes Bergf62fab72013-02-21 20:09:09 +0100300 req->bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200301 IEEE80211_BSS_TYPE_ESS,
302 IEEE80211_PRIVACY_ANY);
Johannes Bergceca7b72013-05-16 00:55:45 +0200303 if (!req->bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200304 return -ENOENT;
305
Johannes Bergf62fab72013-02-21 20:09:09 +0100306 err = rdev_assoc(rdev, dev, req);
Johannes Bergf1940c52013-06-19 13:21:15 +0200307 if (!err)
308 cfg80211_hold_bss(bss_from_pub(req->bss));
Luciano Coelho73de86a2014-02-13 11:31:59 +0200309 else
Johannes Bergf62fab72013-02-21 20:09:09 +0100310 cfg80211_put_bss(&rdev->wiphy, req->bss);
Johannes Berg95de8172012-01-20 13:55:25 +0100311
Johannes Berg19957bb2009-07-02 17:20:43 +0200312 return err;
313}
314
Johannes Berg91bf9b22013-05-15 17:44:01 +0200315int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
316 struct net_device *dev, const u8 *bssid,
317 const u8 *ie, int ie_len, u16 reason,
318 bool local_state_change)
Johannes Berg19957bb2009-07-02 17:20:43 +0200319{
320 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg95de8172012-01-20 13:55:25 +0100321 struct cfg80211_deauth_request req = {
322 .bssid = bssid,
323 .reason_code = reason,
324 .ie = ie,
325 .ie_len = ie_len,
Stanislaw Gruszka68632552012-10-15 14:52:41 +0200326 .local_state_change = local_state_change,
Johannes Berg95de8172012-01-20 13:55:25 +0100327 };
Johannes Berg19957bb2009-07-02 17:20:43 +0200328
Johannes Berg667503dd2009-07-07 03:56:11 +0200329 ASSERT_WDEV_LOCK(wdev);
330
Johannes Bergceca7b72013-05-16 00:55:45 +0200331 if (local_state_change &&
332 (!wdev->current_bss ||
333 !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
Johannes Berg95de8172012-01-20 13:55:25 +0100334 return 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200335
Hila Gonene35e4d22012-06-27 17:19:42 +0300336 return rdev_deauth(rdev, dev, &req);
Johannes Berg19957bb2009-07-02 17:20:43 +0200337}
338
Johannes Berg91bf9b22013-05-15 17:44:01 +0200339int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
340 struct net_device *dev, const u8 *bssid,
341 const u8 *ie, int ie_len, u16 reason,
342 bool local_state_change)
Johannes Berg19957bb2009-07-02 17:20:43 +0200343{
344 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg7ade7032013-05-13 11:37:30 +0200345 struct cfg80211_disassoc_request req = {
346 .reason_code = reason,
347 .local_state_change = local_state_change,
348 .ie = ie,
349 .ie_len = ie_len,
350 };
Johannes Bergceca7b72013-05-16 00:55:45 +0200351 int err;
Johannes Berg19957bb2009-07-02 17:20:43 +0200352
Johannes Berg667503dd2009-07-07 03:56:11 +0200353 ASSERT_WDEV_LOCK(wdev);
354
Johannes Bergceca7b72013-05-16 00:55:45 +0200355 if (!wdev->current_bss)
Johannes Bergf9d6b402009-07-27 10:22:28 +0200356 return -ENOTCONN;
357
Joe Perchesac422d32012-05-08 18:56:55 +0000358 if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
Johannes Berg19957bb2009-07-02 17:20:43 +0200359 req.bss = &wdev->current_bss->pub;
360 else
361 return -ENOTCONN;
362
Johannes Bergceca7b72013-05-16 00:55:45 +0200363 err = rdev_disassoc(rdev, dev, &req);
364 if (err)
365 return err;
366
367 /* driver should have reported the disassoc */
368 WARN_ON(wdev->current_bss);
369 return 0;
Johannes Berg667503dd2009-07-07 03:56:11 +0200370}
371
Johannes Berg19957bb2009-07-02 17:20:43 +0200372void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
373 struct net_device *dev)
374{
375 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg95de8172012-01-20 13:55:25 +0100376 u8 bssid[ETH_ALEN];
Johannes Berg19957bb2009-07-02 17:20:43 +0200377
Johannes Berg667503dd2009-07-07 03:56:11 +0200378 ASSERT_WDEV_LOCK(wdev);
379
Johannes Berg19957bb2009-07-02 17:20:43 +0200380 if (!rdev->ops->deauth)
381 return;
382
Johannes Berg95de8172012-01-20 13:55:25 +0100383 if (!wdev->current_bss)
384 return;
Johannes Berg19957bb2009-07-02 17:20:43 +0200385
Johannes Berg95de8172012-01-20 13:55:25 +0100386 memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergceca7b72013-05-16 00:55:45 +0200387 cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
388 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200389}
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100390
Johannes Berg2e161f72010-08-12 15:38:38 +0200391struct cfg80211_mgmt_registration {
Jouni Malinen026331c2010-02-15 12:53:10 +0200392 struct list_head list;
Johannes Berg33d87832015-06-23 17:47:05 +0200393 struct wireless_dev *wdev;
Jouni Malinen026331c2010-02-15 12:53:10 +0200394
Eric W. Biederman15e47302012-09-07 20:12:54 +0000395 u32 nlportid;
Jouni Malinen026331c2010-02-15 12:53:10 +0200396
397 int match_len;
398
Johannes Berg2e161f72010-08-12 15:38:38 +0200399 __le16 frame_type;
400
Jouni Malinen026331c2010-02-15 12:53:10 +0200401 u8 match[];
402};
403
Johannes Berg33d87832015-06-23 17:47:05 +0200404static void
405cfg80211_process_mlme_unregistrations(struct cfg80211_registered_device *rdev)
406{
407 struct cfg80211_mgmt_registration *reg;
408
409 ASSERT_RTNL();
410
411 spin_lock_bh(&rdev->mlme_unreg_lock);
412 while ((reg = list_first_entry_or_null(&rdev->mlme_unreg,
413 struct cfg80211_mgmt_registration,
414 list))) {
415 list_del(&reg->list);
416 spin_unlock_bh(&rdev->mlme_unreg_lock);
417
418 if (rdev->ops->mgmt_frame_register) {
419 u16 frame_type = le16_to_cpu(reg->frame_type);
420
421 rdev_mgmt_frame_register(rdev, reg->wdev,
422 frame_type, false);
423 }
424
425 kfree(reg);
426
427 spin_lock_bh(&rdev->mlme_unreg_lock);
428 }
429 spin_unlock_bh(&rdev->mlme_unreg_lock);
430}
431
432void cfg80211_mlme_unreg_wk(struct work_struct *wk)
433{
434 struct cfg80211_registered_device *rdev;
435
436 rdev = container_of(wk, struct cfg80211_registered_device,
437 mlme_unreg_wk);
438
439 rtnl_lock();
440 cfg80211_process_mlme_unregistrations(rdev);
441 rtnl_unlock();
442}
443
Eric W. Biederman15e47302012-09-07 20:12:54 +0000444int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
Johannes Berg2e161f72010-08-12 15:38:38 +0200445 u16 frame_type, const u8 *match_data,
446 int match_len)
Jouni Malinen026331c2010-02-15 12:53:10 +0200447{
Johannes Berg271733c2010-10-13 12:06:23 +0200448 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800449 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2e161f72010-08-12 15:38:38 +0200450 struct cfg80211_mgmt_registration *reg, *nreg;
Jouni Malinen026331c2010-02-15 12:53:10 +0200451 int err = 0;
Johannes Berg2e161f72010-08-12 15:38:38 +0200452 u16 mgmt_type;
453
454 if (!wdev->wiphy->mgmt_stypes)
455 return -EOPNOTSUPP;
456
457 if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
458 return -EINVAL;
459
460 if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
461 return -EINVAL;
462
463 mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
464 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
465 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +0200466
467 nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
468 if (!nreg)
469 return -ENOMEM;
470
Johannes Berg2e161f72010-08-12 15:38:38 +0200471 spin_lock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200472
Johannes Berg2e161f72010-08-12 15:38:38 +0200473 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
Jouni Malinen026331c2010-02-15 12:53:10 +0200474 int mlen = min(match_len, reg->match_len);
475
Johannes Berg2e161f72010-08-12 15:38:38 +0200476 if (frame_type != le16_to_cpu(reg->frame_type))
477 continue;
478
Jouni Malinen026331c2010-02-15 12:53:10 +0200479 if (memcmp(reg->match, match_data, mlen) == 0) {
480 err = -EALREADY;
481 break;
482 }
483 }
484
485 if (err) {
486 kfree(nreg);
487 goto out;
488 }
489
490 memcpy(nreg->match, match_data, match_len);
491 nreg->match_len = match_len;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000492 nreg->nlportid = snd_portid;
Johannes Berg2e161f72010-08-12 15:38:38 +0200493 nreg->frame_type = cpu_to_le16(frame_type);
Johannes Berg33d87832015-06-23 17:47:05 +0200494 nreg->wdev = wdev;
Johannes Berg2e161f72010-08-12 15:38:38 +0200495 list_add(&nreg->list, &wdev->mgmt_registrations);
Johannes Berg33d87832015-06-23 17:47:05 +0200496 spin_unlock_bh(&wdev->mgmt_registrations_lock);
497
498 /* process all unregistrations to avoid driver confusion */
499 cfg80211_process_mlme_unregistrations(rdev);
Jouni Malinen026331c2010-02-15 12:53:10 +0200500
Johannes Berg271733c2010-10-13 12:06:23 +0200501 if (rdev->ops->mgmt_frame_register)
Hila Gonene35e4d22012-06-27 17:19:42 +0300502 rdev_mgmt_frame_register(rdev, wdev, frame_type, true);
Johannes Berg271733c2010-10-13 12:06:23 +0200503
Johannes Berg33d87832015-06-23 17:47:05 +0200504 return 0;
505
Jouni Malinen026331c2010-02-15 12:53:10 +0200506 out:
Johannes Berg2e161f72010-08-12 15:38:38 +0200507 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Johannes Berg271733c2010-10-13 12:06:23 +0200508
Jouni Malinen026331c2010-02-15 12:53:10 +0200509 return err;
510}
511
Eric W. Biederman15e47302012-09-07 20:12:54 +0000512void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
Jouni Malinen026331c2010-02-15 12:53:10 +0200513{
Johannes Berg271733c2010-10-13 12:06:23 +0200514 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800515 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2e161f72010-08-12 15:38:38 +0200516 struct cfg80211_mgmt_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +0200517
Johannes Berg2e161f72010-08-12 15:38:38 +0200518 spin_lock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200519
Johannes Berg2e161f72010-08-12 15:38:38 +0200520 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000521 if (reg->nlportid != nlportid)
Johannes Berg271733c2010-10-13 12:06:23 +0200522 continue;
523
Johannes Berg271733c2010-10-13 12:06:23 +0200524 list_del(&reg->list);
Johannes Berg33d87832015-06-23 17:47:05 +0200525 spin_lock(&rdev->mlme_unreg_lock);
526 list_add_tail(&reg->list, &rdev->mlme_unreg);
527 spin_unlock(&rdev->mlme_unreg_lock);
528
529 schedule_work(&rdev->mlme_unreg_wk);
Jouni Malinen026331c2010-02-15 12:53:10 +0200530 }
531
Johannes Berg2e161f72010-08-12 15:38:38 +0200532 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Johannes Berg28946da2011-11-04 11:18:12 +0100533
Arend van Spriel5de17982013-04-18 15:49:00 +0200534 if (nlportid && rdev->crit_proto_nlportid == nlportid) {
535 rdev->crit_proto_nlportid = 0;
536 rdev_crit_proto_stop(rdev, wdev);
537 }
538
Eric W. Biederman15e47302012-09-07 20:12:54 +0000539 if (nlportid == wdev->ap_unexpected_nlportid)
540 wdev->ap_unexpected_nlportid = 0;
Jouni Malinen026331c2010-02-15 12:53:10 +0200541}
542
Johannes Berg2e161f72010-08-12 15:38:38 +0200543void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
Jouni Malinen026331c2010-02-15 12:53:10 +0200544{
Johannes Berg33d87832015-06-23 17:47:05 +0200545 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Jouni Malinen026331c2010-02-15 12:53:10 +0200546
Johannes Berg2e161f72010-08-12 15:38:38 +0200547 spin_lock_bh(&wdev->mgmt_registrations_lock);
Johannes Berg33d87832015-06-23 17:47:05 +0200548 spin_lock(&rdev->mlme_unreg_lock);
549 list_splice_tail_init(&wdev->mgmt_registrations, &rdev->mlme_unreg);
550 spin_unlock(&rdev->mlme_unreg_lock);
Johannes Berg2e161f72010-08-12 15:38:38 +0200551 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Johannes Berg33d87832015-06-23 17:47:05 +0200552
553 cfg80211_process_mlme_unregistrations(rdev);
Jouni Malinen026331c2010-02-15 12:53:10 +0200554}
555
Johannes Berg2e161f72010-08-12 15:38:38 +0200556int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +0200557 struct wireless_dev *wdev,
Andrei Otcheretianskib176e622013-11-18 19:06:49 +0200558 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
Jouni Malinen026331c2010-02-15 12:53:10 +0200559{
Jouni Malinen026331c2010-02-15 12:53:10 +0200560 const struct ieee80211_mgmt *mgmt;
Johannes Berg2e161f72010-08-12 15:38:38 +0200561 u16 stype;
Jouni Malinen026331c2010-02-15 12:53:10 +0200562
Johannes Berg2e161f72010-08-12 15:38:38 +0200563 if (!wdev->wiphy->mgmt_stypes)
Jouni Malinen026331c2010-02-15 12:53:10 +0200564 return -EOPNOTSUPP;
Johannes Berg2e161f72010-08-12 15:38:38 +0200565
566 if (!rdev->ops->mgmt_tx)
567 return -EOPNOTSUPP;
568
Andrei Otcheretianskib176e622013-11-18 19:06:49 +0200569 if (params->len < 24 + 1)
Jouni Malinen026331c2010-02-15 12:53:10 +0200570 return -EINVAL;
571
Andrei Otcheretianskib176e622013-11-18 19:06:49 +0200572 mgmt = (const struct ieee80211_mgmt *)params->buf;
Johannes Berg2e161f72010-08-12 15:38:38 +0200573
574 if (!ieee80211_is_mgmt(mgmt->frame_control))
Jouni Malinen026331c2010-02-15 12:53:10 +0200575 return -EINVAL;
Johannes Berg2e161f72010-08-12 15:38:38 +0200576
577 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
578 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
579 return -EINVAL;
580
581 if (ieee80211_is_action(mgmt->frame_control) &&
582 mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
Johannes Berg663fcaf2010-09-30 21:06:09 +0200583 int err = 0;
584
Johannes Bergfe100ac2010-08-09 15:52:03 +0200585 wdev_lock(wdev);
586
Johannes Berg663fcaf2010-09-30 21:06:09 +0200587 switch (wdev->iftype) {
588 case NL80211_IFTYPE_ADHOC:
589 case NL80211_IFTYPE_STATION:
590 case NL80211_IFTYPE_P2P_CLIENT:
591 if (!wdev->current_bss) {
592 err = -ENOTCONN;
593 break;
594 }
Johannes Bergfe100ac2010-08-09 15:52:03 +0200595
Joe Perchesac422d32012-05-08 18:56:55 +0000596 if (!ether_addr_equal(wdev->current_bss->pub.bssid,
597 mgmt->bssid)) {
Johannes Berg663fcaf2010-09-30 21:06:09 +0200598 err = -ENOTCONN;
599 break;
600 }
601
602 /*
603 * check for IBSS DA must be done by driver as
604 * cfg80211 doesn't track the stations
605 */
606 if (wdev->iftype == NL80211_IFTYPE_ADHOC)
607 break;
608
609 /* for station, check that DA is the AP */
Joe Perchesac422d32012-05-08 18:56:55 +0000610 if (!ether_addr_equal(wdev->current_bss->pub.bssid,
611 mgmt->da)) {
Johannes Berg663fcaf2010-09-30 21:06:09 +0200612 err = -ENOTCONN;
613 break;
614 }
615 break;
616 case NL80211_IFTYPE_AP:
617 case NL80211_IFTYPE_P2P_GO:
618 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg98104fde2012-06-16 00:19:54 +0200619 if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
Johannes Berg663fcaf2010-09-30 21:06:09 +0200620 err = -EINVAL;
621 break;
Javier Cardona0778a6a2011-05-03 16:57:08 -0700622 case NL80211_IFTYPE_MESH_POINT:
Joe Perchesac422d32012-05-08 18:56:55 +0000623 if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
Javier Cardona0778a6a2011-05-03 16:57:08 -0700624 err = -EINVAL;
625 break;
626 }
627 /*
628 * check for mesh DA must be done by driver as
629 * cfg80211 doesn't track the stations
630 */
631 break;
Johannes Berg98104fde2012-06-16 00:19:54 +0200632 case NL80211_IFTYPE_P2P_DEVICE:
633 /*
634 * fall through, P2P device only supports
635 * public action frames
636 */
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300637 case NL80211_IFTYPE_NAN:
Johannes Berg663fcaf2010-09-30 21:06:09 +0200638 default:
639 err = -EOPNOTSUPP;
640 break;
641 }
Johannes Bergfe100ac2010-08-09 15:52:03 +0200642 wdev_unlock(wdev);
Johannes Berg663fcaf2010-09-30 21:06:09 +0200643
644 if (err)
645 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +0200646 }
647
Johannes Berg98104fde2012-06-16 00:19:54 +0200648 if (!ether_addr_equal(mgmt->sa, wdev_address(wdev)))
Jouni Malinen026331c2010-02-15 12:53:10 +0200649 return -EINVAL;
650
651 /* Transmit the Action frame as requested by user space */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +0200652 return rdev_mgmt_tx(rdev, wdev, params, cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +0200653}
654
Johannes Berg71bbc992012-06-15 15:30:18 +0200655bool cfg80211_rx_mgmt(struct wireless_dev *wdev, int freq, int sig_mbm,
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -0700656 const u8 *buf, size_t len, u32 flags)
Jouni Malinen026331c2010-02-15 12:53:10 +0200657{
Jouni Malinen026331c2010-02-15 12:53:10 +0200658 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800659 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg2e161f72010-08-12 15:38:38 +0200660 struct cfg80211_mgmt_registration *reg;
661 const struct ieee80211_txrx_stypes *stypes =
662 &wiphy->mgmt_stypes[wdev->iftype];
663 struct ieee80211_mgmt *mgmt = (void *)buf;
664 const u8 *data;
665 int data_len;
Jouni Malinen026331c2010-02-15 12:53:10 +0200666 bool result = false;
Johannes Berg2e161f72010-08-12 15:38:38 +0200667 __le16 ftype = mgmt->frame_control &
668 cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
669 u16 stype;
Jouni Malinen026331c2010-02-15 12:53:10 +0200670
Beni Lev4ee3e062012-08-27 12:49:39 +0300671 trace_cfg80211_rx_mgmt(wdev, freq, sig_mbm);
Johannes Berg2e161f72010-08-12 15:38:38 +0200672 stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
Jouni Malinen026331c2010-02-15 12:53:10 +0200673
Beni Lev4ee3e062012-08-27 12:49:39 +0300674 if (!(stypes->rx & BIT(stype))) {
675 trace_cfg80211_return_bool(false);
Johannes Berg2e161f72010-08-12 15:38:38 +0200676 return false;
Beni Lev4ee3e062012-08-27 12:49:39 +0300677 }
Jouni Malinen026331c2010-02-15 12:53:10 +0200678
Johannes Berg2e161f72010-08-12 15:38:38 +0200679 data = buf + ieee80211_hdrlen(mgmt->frame_control);
680 data_len = len - ieee80211_hdrlen(mgmt->frame_control);
Jouni Malinen026331c2010-02-15 12:53:10 +0200681
Johannes Berg2e161f72010-08-12 15:38:38 +0200682 spin_lock_bh(&wdev->mgmt_registrations_lock);
683
684 list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
685 if (reg->frame_type != ftype)
Jouni Malinen026331c2010-02-15 12:53:10 +0200686 continue;
687
Johannes Berg2e161f72010-08-12 15:38:38 +0200688 if (reg->match_len > data_len)
689 continue;
690
691 if (memcmp(reg->match, data, reg->match_len))
Jouni Malinen026331c2010-02-15 12:53:10 +0200692 continue;
693
694 /* found match! */
695
696 /* Indicate the received Action frame to user space */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000697 if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +0100698 freq, sig_mbm,
Vladimir Kondratiev970fdfa2014-08-11 03:29:57 -0700699 buf, len, flags, GFP_ATOMIC))
Jouni Malinen026331c2010-02-15 12:53:10 +0200700 continue;
701
702 result = true;
703 break;
704 }
705
Johannes Berg2e161f72010-08-12 15:38:38 +0200706 spin_unlock_bh(&wdev->mgmt_registrations_lock);
Jouni Malinen026331c2010-02-15 12:53:10 +0200707
Beni Lev4ee3e062012-08-27 12:49:39 +0300708 trace_cfg80211_return_bool(result);
Jouni Malinen026331c2010-02-15 12:53:10 +0200709 return result;
710}
Johannes Berg2e161f72010-08-12 15:38:38 +0200711EXPORT_SYMBOL(cfg80211_rx_mgmt);
Jouni Malinen026331c2010-02-15 12:53:10 +0200712
Simon Wunderlich04f39042013-02-08 18:16:19 +0100713void cfg80211_dfs_channels_update_work(struct work_struct *work)
714{
Geliang Tanga85a7e22016-01-01 23:48:52 +0800715 struct delayed_work *delayed_work = to_delayed_work(work);
Simon Wunderlich04f39042013-02-08 18:16:19 +0100716 struct cfg80211_registered_device *rdev;
717 struct cfg80211_chan_def chandef;
718 struct ieee80211_supported_band *sband;
719 struct ieee80211_channel *c;
720 struct wiphy *wiphy;
721 bool check_again = false;
722 unsigned long timeout, next_time = 0;
723 int bandid, i;
724
Simon Wunderlich04f39042013-02-08 18:16:19 +0100725 rdev = container_of(delayed_work, struct cfg80211_registered_device,
726 dfs_update_channels_wk);
727 wiphy = &rdev->wiphy;
728
Johannes Berg5fe231e2013-05-08 21:45:15 +0200729 rtnl_lock();
Johannes Berg57fbcce2016-04-12 15:56:15 +0200730 for (bandid = 0; bandid < NUM_NL80211_BANDS; bandid++) {
Simon Wunderlich04f39042013-02-08 18:16:19 +0100731 sband = wiphy->bands[bandid];
732 if (!sband)
733 continue;
734
735 for (i = 0; i < sband->n_channels; i++) {
736 c = &sband->channels[i];
737
738 if (c->dfs_state != NL80211_DFS_UNAVAILABLE)
739 continue;
740
Michal Kaziorc532a582013-10-17 11:21:11 -0700741 timeout = c->dfs_state_entered + msecs_to_jiffies(
742 IEEE80211_DFS_MIN_NOP_TIME_MS);
Simon Wunderlich04f39042013-02-08 18:16:19 +0100743
744 if (time_after_eq(jiffies, timeout)) {
745 c->dfs_state = NL80211_DFS_USABLE;
Michal Kaziorbbe09bb2013-10-17 11:21:12 -0700746 c->dfs_state_entered = jiffies;
747
Simon Wunderlich04f39042013-02-08 18:16:19 +0100748 cfg80211_chandef_create(&chandef, c,
749 NL80211_CHAN_NO_HT);
750
751 nl80211_radar_notify(rdev, &chandef,
752 NL80211_RADAR_NOP_FINISHED,
753 NULL, GFP_ATOMIC);
754 continue;
755 }
756
757 if (!check_again)
758 next_time = timeout - jiffies;
759 else
760 next_time = min(next_time, timeout - jiffies);
761 check_again = true;
762 }
763 }
Johannes Berg5fe231e2013-05-08 21:45:15 +0200764 rtnl_unlock();
Simon Wunderlich04f39042013-02-08 18:16:19 +0100765
766 /* reschedule if there are other channels waiting to be cleared again */
767 if (check_again)
768 queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
769 next_time);
770}
771
772
773void cfg80211_radar_event(struct wiphy *wiphy,
774 struct cfg80211_chan_def *chandef,
775 gfp_t gfp)
776{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800777 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Simon Wunderlich04f39042013-02-08 18:16:19 +0100778 unsigned long timeout;
779
780 trace_cfg80211_radar_event(wiphy, chandef);
781
782 /* only set the chandef supplied channel to unavailable, in
783 * case the radar is detected on only one of multiple channels
784 * spanned by the chandef.
785 */
786 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE);
787
788 timeout = msecs_to_jiffies(IEEE80211_DFS_MIN_NOP_TIME_MS);
789 queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
790 timeout);
791
792 nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp);
793}
794EXPORT_SYMBOL(cfg80211_radar_event);
795
796void cfg80211_cac_event(struct net_device *netdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +0100797 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +0100798 enum nl80211_radar_event event, gfp_t gfp)
799{
800 struct wireless_dev *wdev = netdev->ieee80211_ptr;
801 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800802 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Simon Wunderlich04f39042013-02-08 18:16:19 +0100803 unsigned long timeout;
804
805 trace_cfg80211_cac_event(netdev, event);
806
807 if (WARN_ON(!wdev->cac_started))
808 return;
809
Michal Kazior9e0e2962014-01-29 14:22:27 +0100810 if (WARN_ON(!wdev->chandef.chan))
Simon Wunderlich04f39042013-02-08 18:16:19 +0100811 return;
812
Simon Wunderlich04f39042013-02-08 18:16:19 +0100813 switch (event) {
814 case NL80211_RADAR_CAC_FINISHED:
815 timeout = wdev->cac_start_time +
Janusz Dziedzic31559f32014-02-21 19:46:13 +0100816 msecs_to_jiffies(wdev->cac_time_ms);
Simon Wunderlich04f39042013-02-08 18:16:19 +0100817 WARN_ON(!time_after_eq(jiffies, timeout));
Janusz Dziedzicd2859df2013-11-06 13:55:51 +0100818 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE);
Simon Wunderlich04f39042013-02-08 18:16:19 +0100819 break;
820 case NL80211_RADAR_CAC_ABORTED:
821 break;
822 default:
823 WARN_ON(1);
824 return;
825 }
826 wdev->cac_started = false;
827
Janusz Dziedzicd2859df2013-11-06 13:55:51 +0100828 nl80211_radar_notify(rdev, chandef, event, netdev, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +0100829}
830EXPORT_SYMBOL(cfg80211_cac_event);