1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * spectrum management
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
6*4882a593Smuzhiyun * Copyright 2002-2005, Instant802 Networks, Inc.
7*4882a593Smuzhiyun * Copyright 2005-2006, Devicescape Software, Inc.
8*4882a593Smuzhiyun * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
9*4882a593Smuzhiyun * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
10*4882a593Smuzhiyun * Copyright 2007-2008, Intel Corporation
11*4882a593Smuzhiyun * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
12*4882a593Smuzhiyun * Copyright (C) 2018, 2020 Intel Corporation
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <linux/ieee80211.h>
16*4882a593Smuzhiyun #include <net/cfg80211.h>
17*4882a593Smuzhiyun #include <net/mac80211.h>
18*4882a593Smuzhiyun #include "ieee80211_i.h"
19*4882a593Smuzhiyun #include "sta_info.h"
20*4882a593Smuzhiyun #include "wme.h"
21*4882a593Smuzhiyun
ieee80211_parse_ch_switch_ie(struct ieee80211_sub_if_data * sdata,struct ieee802_11_elems * elems,enum nl80211_band current_band,u32 vht_cap_info,u32 sta_flags,u8 * bssid,struct ieee80211_csa_ie * csa_ie)22*4882a593Smuzhiyun int ieee80211_parse_ch_switch_ie(struct ieee80211_sub_if_data *sdata,
23*4882a593Smuzhiyun struct ieee802_11_elems *elems,
24*4882a593Smuzhiyun enum nl80211_band current_band,
25*4882a593Smuzhiyun u32 vht_cap_info,
26*4882a593Smuzhiyun u32 sta_flags, u8 *bssid,
27*4882a593Smuzhiyun struct ieee80211_csa_ie *csa_ie)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun enum nl80211_band new_band = current_band;
30*4882a593Smuzhiyun int new_freq;
31*4882a593Smuzhiyun u8 new_chan_no;
32*4882a593Smuzhiyun struct ieee80211_channel *new_chan;
33*4882a593Smuzhiyun struct cfg80211_chan_def new_vht_chandef = {};
34*4882a593Smuzhiyun const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
35*4882a593Smuzhiyun const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
36*4882a593Smuzhiyun int secondary_channel_offset = -1;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun memset(csa_ie, 0, sizeof(*csa_ie));
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun sec_chan_offs = elems->sec_chan_offs;
41*4882a593Smuzhiyun wide_bw_chansw_ie = elems->wide_bw_chansw_ie;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun if (sta_flags & (IEEE80211_STA_DISABLE_HT |
44*4882a593Smuzhiyun IEEE80211_STA_DISABLE_40MHZ)) {
45*4882a593Smuzhiyun sec_chan_offs = NULL;
46*4882a593Smuzhiyun wide_bw_chansw_ie = NULL;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun if (sta_flags & IEEE80211_STA_DISABLE_VHT)
50*4882a593Smuzhiyun wide_bw_chansw_ie = NULL;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun if (elems->ext_chansw_ie) {
53*4882a593Smuzhiyun if (!ieee80211_operating_class_to_band(
54*4882a593Smuzhiyun elems->ext_chansw_ie->new_operating_class,
55*4882a593Smuzhiyun &new_band)) {
56*4882a593Smuzhiyun sdata_info(sdata,
57*4882a593Smuzhiyun "cannot understand ECSA IE operating class, %d, ignoring\n",
58*4882a593Smuzhiyun elems->ext_chansw_ie->new_operating_class);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun new_chan_no = elems->ext_chansw_ie->new_ch_num;
61*4882a593Smuzhiyun csa_ie->count = elems->ext_chansw_ie->count;
62*4882a593Smuzhiyun csa_ie->mode = elems->ext_chansw_ie->mode;
63*4882a593Smuzhiyun } else if (elems->ch_switch_ie) {
64*4882a593Smuzhiyun new_chan_no = elems->ch_switch_ie->new_ch_num;
65*4882a593Smuzhiyun csa_ie->count = elems->ch_switch_ie->count;
66*4882a593Smuzhiyun csa_ie->mode = elems->ch_switch_ie->mode;
67*4882a593Smuzhiyun } else {
68*4882a593Smuzhiyun /* nothing here we understand */
69*4882a593Smuzhiyun return 1;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* Mesh Channel Switch Parameters Element */
73*4882a593Smuzhiyun if (elems->mesh_chansw_params_ie) {
74*4882a593Smuzhiyun csa_ie->ttl = elems->mesh_chansw_params_ie->mesh_ttl;
75*4882a593Smuzhiyun csa_ie->mode = elems->mesh_chansw_params_ie->mesh_flags;
76*4882a593Smuzhiyun csa_ie->pre_value = le16_to_cpu(
77*4882a593Smuzhiyun elems->mesh_chansw_params_ie->mesh_pre_value);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun if (elems->mesh_chansw_params_ie->mesh_flags &
80*4882a593Smuzhiyun WLAN_EID_CHAN_SWITCH_PARAM_REASON)
81*4882a593Smuzhiyun csa_ie->reason_code = le16_to_cpu(
82*4882a593Smuzhiyun elems->mesh_chansw_params_ie->mesh_reason);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
86*4882a593Smuzhiyun new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
87*4882a593Smuzhiyun if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
88*4882a593Smuzhiyun sdata_info(sdata,
89*4882a593Smuzhiyun "BSS %pM switches to unsupported channel (%d MHz), disconnecting\n",
90*4882a593Smuzhiyun bssid, new_freq);
91*4882a593Smuzhiyun return -EINVAL;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun if (sec_chan_offs) {
95*4882a593Smuzhiyun secondary_channel_offset = sec_chan_offs->sec_chan_offs;
96*4882a593Smuzhiyun } else if (!(sta_flags & IEEE80211_STA_DISABLE_HT)) {
97*4882a593Smuzhiyun /* If the secondary channel offset IE is not present,
98*4882a593Smuzhiyun * we can't know what's the post-CSA offset, so the
99*4882a593Smuzhiyun * best we can do is use 20MHz.
100*4882a593Smuzhiyun */
101*4882a593Smuzhiyun secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun switch (secondary_channel_offset) {
105*4882a593Smuzhiyun default:
106*4882a593Smuzhiyun /* secondary_channel_offset was present but is invalid */
107*4882a593Smuzhiyun case IEEE80211_HT_PARAM_CHA_SEC_NONE:
108*4882a593Smuzhiyun cfg80211_chandef_create(&csa_ie->chandef, new_chan,
109*4882a593Smuzhiyun NL80211_CHAN_HT20);
110*4882a593Smuzhiyun break;
111*4882a593Smuzhiyun case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
112*4882a593Smuzhiyun cfg80211_chandef_create(&csa_ie->chandef, new_chan,
113*4882a593Smuzhiyun NL80211_CHAN_HT40PLUS);
114*4882a593Smuzhiyun break;
115*4882a593Smuzhiyun case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
116*4882a593Smuzhiyun cfg80211_chandef_create(&csa_ie->chandef, new_chan,
117*4882a593Smuzhiyun NL80211_CHAN_HT40MINUS);
118*4882a593Smuzhiyun break;
119*4882a593Smuzhiyun case -1:
120*4882a593Smuzhiyun cfg80211_chandef_create(&csa_ie->chandef, new_chan,
121*4882a593Smuzhiyun NL80211_CHAN_NO_HT);
122*4882a593Smuzhiyun /* keep width for 5/10 MHz channels */
123*4882a593Smuzhiyun switch (sdata->vif.bss_conf.chandef.width) {
124*4882a593Smuzhiyun case NL80211_CHAN_WIDTH_5:
125*4882a593Smuzhiyun case NL80211_CHAN_WIDTH_10:
126*4882a593Smuzhiyun csa_ie->chandef.width =
127*4882a593Smuzhiyun sdata->vif.bss_conf.chandef.width;
128*4882a593Smuzhiyun break;
129*4882a593Smuzhiyun default:
130*4882a593Smuzhiyun break;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun break;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun if (wide_bw_chansw_ie) {
136*4882a593Smuzhiyun u8 new_seg1 = wide_bw_chansw_ie->new_center_freq_seg1;
137*4882a593Smuzhiyun struct ieee80211_vht_operation vht_oper = {
138*4882a593Smuzhiyun .chan_width =
139*4882a593Smuzhiyun wide_bw_chansw_ie->new_channel_width,
140*4882a593Smuzhiyun .center_freq_seg0_idx =
141*4882a593Smuzhiyun wide_bw_chansw_ie->new_center_freq_seg0,
142*4882a593Smuzhiyun .center_freq_seg1_idx = new_seg1,
143*4882a593Smuzhiyun /* .basic_mcs_set doesn't matter */
144*4882a593Smuzhiyun };
145*4882a593Smuzhiyun struct ieee80211_ht_operation ht_oper = {
146*4882a593Smuzhiyun .operation_mode =
147*4882a593Smuzhiyun cpu_to_le16(new_seg1 <<
148*4882a593Smuzhiyun IEEE80211_HT_OP_MODE_CCFS2_SHIFT),
149*4882a593Smuzhiyun };
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /* default, for the case of IEEE80211_VHT_CHANWIDTH_USE_HT,
152*4882a593Smuzhiyun * to the previously parsed chandef
153*4882a593Smuzhiyun */
154*4882a593Smuzhiyun new_vht_chandef = csa_ie->chandef;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* ignore if parsing fails */
157*4882a593Smuzhiyun if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
158*4882a593Smuzhiyun vht_cap_info,
159*4882a593Smuzhiyun &vht_oper, &ht_oper,
160*4882a593Smuzhiyun &new_vht_chandef))
161*4882a593Smuzhiyun new_vht_chandef.chan = NULL;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (sta_flags & IEEE80211_STA_DISABLE_80P80MHZ &&
164*4882a593Smuzhiyun new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
165*4882a593Smuzhiyun ieee80211_chandef_downgrade(&new_vht_chandef);
166*4882a593Smuzhiyun if (sta_flags & IEEE80211_STA_DISABLE_160MHZ &&
167*4882a593Smuzhiyun new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
168*4882a593Smuzhiyun ieee80211_chandef_downgrade(&new_vht_chandef);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /* if VHT data is there validate & use it */
172*4882a593Smuzhiyun if (new_vht_chandef.chan) {
173*4882a593Smuzhiyun if (!cfg80211_chandef_compatible(&new_vht_chandef,
174*4882a593Smuzhiyun &csa_ie->chandef)) {
175*4882a593Smuzhiyun sdata_info(sdata,
176*4882a593Smuzhiyun "BSS %pM: CSA has inconsistent channel data, disconnecting\n",
177*4882a593Smuzhiyun bssid);
178*4882a593Smuzhiyun return -EINVAL;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun csa_ie->chandef = new_vht_chandef;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (elems->max_channel_switch_time)
184*4882a593Smuzhiyun csa_ie->max_switch_time =
185*4882a593Smuzhiyun (elems->max_channel_switch_time[0] << 0) |
186*4882a593Smuzhiyun (elems->max_channel_switch_time[1] << 8) |
187*4882a593Smuzhiyun (elems->max_channel_switch_time[2] << 16);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun return 0;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data * sdata,struct ieee80211_msrment_ie * request_ie,const u8 * da,const u8 * bssid,u8 dialog_token)192*4882a593Smuzhiyun static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
193*4882a593Smuzhiyun struct ieee80211_msrment_ie *request_ie,
194*4882a593Smuzhiyun const u8 *da, const u8 *bssid,
195*4882a593Smuzhiyun u8 dialog_token)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun struct ieee80211_local *local = sdata->local;
198*4882a593Smuzhiyun struct sk_buff *skb;
199*4882a593Smuzhiyun struct ieee80211_mgmt *msr_report;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
202*4882a593Smuzhiyun sizeof(struct ieee80211_msrment_ie));
203*4882a593Smuzhiyun if (!skb)
204*4882a593Smuzhiyun return;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun skb_reserve(skb, local->hw.extra_tx_headroom);
207*4882a593Smuzhiyun msr_report = skb_put_zero(skb, 24);
208*4882a593Smuzhiyun memcpy(msr_report->da, da, ETH_ALEN);
209*4882a593Smuzhiyun memcpy(msr_report->sa, sdata->vif.addr, ETH_ALEN);
210*4882a593Smuzhiyun memcpy(msr_report->bssid, bssid, ETH_ALEN);
211*4882a593Smuzhiyun msr_report->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
212*4882a593Smuzhiyun IEEE80211_STYPE_ACTION);
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
215*4882a593Smuzhiyun msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
216*4882a593Smuzhiyun msr_report->u.action.u.measurement.action_code =
217*4882a593Smuzhiyun WLAN_ACTION_SPCT_MSR_RPRT;
218*4882a593Smuzhiyun msr_report->u.action.u.measurement.dialog_token = dialog_token;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
221*4882a593Smuzhiyun msr_report->u.action.u.measurement.length =
222*4882a593Smuzhiyun sizeof(struct ieee80211_msrment_ie);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun memset(&msr_report->u.action.u.measurement.msr_elem, 0,
225*4882a593Smuzhiyun sizeof(struct ieee80211_msrment_ie));
226*4882a593Smuzhiyun msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
227*4882a593Smuzhiyun msr_report->u.action.u.measurement.msr_elem.mode |=
228*4882a593Smuzhiyun IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
229*4882a593Smuzhiyun msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun ieee80211_tx_skb(sdata, skb);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
ieee80211_process_measurement_req(struct ieee80211_sub_if_data * sdata,struct ieee80211_mgmt * mgmt,size_t len)234*4882a593Smuzhiyun void ieee80211_process_measurement_req(struct ieee80211_sub_if_data *sdata,
235*4882a593Smuzhiyun struct ieee80211_mgmt *mgmt,
236*4882a593Smuzhiyun size_t len)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun /*
239*4882a593Smuzhiyun * Ignoring measurement request is spec violation.
240*4882a593Smuzhiyun * Mandatory measurements must be reported optional
241*4882a593Smuzhiyun * measurements might be refused or reported incapable
242*4882a593Smuzhiyun * For now just refuse
243*4882a593Smuzhiyun * TODO: Answer basic measurement as unmeasured
244*4882a593Smuzhiyun */
245*4882a593Smuzhiyun ieee80211_send_refuse_measurement_request(sdata,
246*4882a593Smuzhiyun &mgmt->u.action.u.measurement.msr_elem,
247*4882a593Smuzhiyun mgmt->sa, mgmt->bssid,
248*4882a593Smuzhiyun mgmt->u.action.u.measurement.dialog_token);
249*4882a593Smuzhiyun }
250