Line data Source code
1 : /*
2 : * Common hostapd/wpa_supplicant HW features
3 : * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4 : * Copyright (c) 2015, Qualcomm Atheros, Inc.
5 : *
6 : * This software may be distributed under the terms of the BSD license.
7 : * See README for more details.
8 : */
9 :
10 : #include "includes.h"
11 :
12 : #include "common.h"
13 : #include "defs.h"
14 : #include "ieee802_11_defs.h"
15 : #include "ieee802_11_common.h"
16 : #include "hw_features_common.h"
17 :
18 :
19 1360 : struct hostapd_channel_data * hw_get_channel_chan(struct hostapd_hw_modes *mode,
20 : int chan, int *freq)
21 : {
22 : int i;
23 :
24 1360 : if (freq)
25 1360 : *freq = 0;
26 :
27 1360 : if (!mode)
28 0 : return NULL;
29 :
30 3204 : for (i = 0; i < mode->num_channels; i++) {
31 3204 : struct hostapd_channel_data *ch = &mode->channels[i];
32 3204 : if (ch->chan == chan) {
33 1360 : if (freq)
34 1360 : *freq = ch->freq;
35 1360 : return ch;
36 : }
37 : }
38 :
39 0 : return NULL;
40 : }
41 :
42 :
43 64 : struct hostapd_channel_data * hw_get_channel_freq(struct hostapd_hw_modes *mode,
44 : int freq, int *chan)
45 : {
46 : int i;
47 :
48 64 : if (chan)
49 64 : *chan = 0;
50 :
51 64 : if (!mode)
52 0 : return NULL;
53 :
54 497 : for (i = 0; i < mode->num_channels; i++) {
55 487 : struct hostapd_channel_data *ch = &mode->channels[i];
56 487 : if (ch->freq == freq) {
57 54 : if (chan)
58 54 : *chan = ch->chan;
59 54 : return ch;
60 : }
61 : }
62 :
63 10 : return NULL;
64 : }
65 :
66 :
67 1360 : int hw_get_freq(struct hostapd_hw_modes *mode, int chan)
68 : {
69 : int freq;
70 :
71 1360 : hw_get_channel_chan(mode, chan, &freq);
72 :
73 1360 : return freq;
74 : }
75 :
76 :
77 64 : int hw_get_chan(struct hostapd_hw_modes *mode, int freq)
78 : {
79 : int chan;
80 :
81 64 : hw_get_channel_freq(mode, freq, &chan);
82 :
83 64 : return chan;
84 : }
85 :
86 :
87 36 : int allowed_ht40_channel_pair(struct hostapd_hw_modes *mode, int pri_chan,
88 : int sec_chan)
89 : {
90 : int ok, j, first;
91 36 : int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
92 : 184, 192 };
93 : size_t k;
94 :
95 36 : if (pri_chan == sec_chan || !sec_chan)
96 0 : return 1; /* HT40 not used */
97 :
98 36 : wpa_printf(MSG_DEBUG,
99 : "HT40: control channel: %d secondary channel: %d",
100 : pri_chan, sec_chan);
101 :
102 : /* Verify that HT40 secondary channel is an allowed 20 MHz
103 : * channel */
104 36 : ok = 0;
105 141 : for (j = 0; j < mode->num_channels; j++) {
106 141 : struct hostapd_channel_data *chan = &mode->channels[j];
107 267 : if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
108 126 : chan->chan == sec_chan) {
109 36 : ok = 1;
110 36 : break;
111 : }
112 : }
113 36 : if (!ok) {
114 0 : wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
115 : sec_chan);
116 0 : return 0;
117 : }
118 :
119 : /*
120 : * Verify that HT40 primary,secondary channel pair is allowed per
121 : * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
122 : * 2.4 GHz rules allow all cases where the secondary channel fits into
123 : * the list of allowed channels (already checked above).
124 : */
125 36 : if (mode->mode != HOSTAPD_MODE_IEEE80211A)
126 14 : return 1;
127 :
128 22 : first = pri_chan < sec_chan ? pri_chan : sec_chan;
129 :
130 22 : ok = 0;
131 44 : for (k = 0; k < ARRAY_SIZE(allowed); k++) {
132 44 : if (first == allowed[k]) {
133 22 : ok = 1;
134 22 : break;
135 : }
136 : }
137 22 : if (!ok) {
138 0 : wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
139 : pri_chan, sec_chan);
140 0 : return 0;
141 : }
142 :
143 22 : return 1;
144 : }
145 :
146 :
147 13 : void get_pri_sec_chan(struct wpa_scan_res *bss, int *pri_chan, int *sec_chan)
148 : {
149 : struct ieee80211_ht_operation *oper;
150 : struct ieee802_11_elems elems;
151 :
152 13 : *pri_chan = *sec_chan = 0;
153 :
154 13 : ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
155 26 : if (elems.ht_operation &&
156 13 : elems.ht_operation_len >= sizeof(*oper)) {
157 13 : oper = (struct ieee80211_ht_operation *) elems.ht_operation;
158 13 : *pri_chan = oper->primary_chan;
159 13 : if (oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) {
160 8 : int sec = oper->ht_param &
161 : HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
162 8 : if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
163 6 : *sec_chan = *pri_chan + 4;
164 2 : else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
165 2 : *sec_chan = *pri_chan - 4;
166 : }
167 : }
168 13 : }
169 :
170 :
171 24 : int check_40mhz_5g(struct hostapd_hw_modes *mode,
172 : struct wpa_scan_results *scan_res, int pri_chan,
173 : int sec_chan)
174 : {
175 : int pri_freq, sec_freq, pri_bss, sec_bss;
176 : int bss_pri_chan, bss_sec_chan;
177 : size_t i;
178 : int match;
179 :
180 24 : if (!mode || !scan_res || !pri_chan || !sec_chan)
181 0 : return 0;
182 :
183 24 : if (pri_chan == sec_chan)
184 0 : return 0;
185 :
186 24 : pri_freq = hw_get_freq(mode, pri_chan);
187 24 : sec_freq = hw_get_freq(mode, sec_chan);
188 :
189 : /*
190 : * Switch PRI/SEC channels if Beacons were detected on selected SEC
191 : * channel, but not on selected PRI channel.
192 : */
193 24 : pri_bss = sec_bss = 0;
194 31 : for (i = 0; i < scan_res->num; i++) {
195 7 : struct wpa_scan_res *bss = scan_res->res[i];
196 7 : if (bss->freq == pri_freq)
197 3 : pri_bss++;
198 4 : else if (bss->freq == sec_freq)
199 2 : sec_bss++;
200 : }
201 24 : if (sec_bss && !pri_bss) {
202 1 : wpa_printf(MSG_INFO,
203 : "Switch own primary and secondary channel to get secondary channel with no Beacons from other BSSes");
204 1 : return 2;
205 : }
206 :
207 : /*
208 : * Match PRI/SEC channel with any existing HT40 BSS on the same
209 : * channels that we are about to use (if already mixed order in
210 : * existing BSSes, use own preference).
211 : */
212 23 : match = 0;
213 27 : for (i = 0; i < scan_res->num; i++) {
214 6 : struct wpa_scan_res *bss = scan_res->res[i];
215 6 : get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
216 9 : if (pri_chan == bss_pri_chan &&
217 3 : sec_chan == bss_sec_chan) {
218 2 : match = 1;
219 2 : break;
220 : }
221 : }
222 23 : if (!match) {
223 23 : for (i = 0; i < scan_res->num; i++) {
224 3 : struct wpa_scan_res *bss = scan_res->res[i];
225 3 : get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
226 4 : if (pri_chan == bss_sec_chan &&
227 1 : sec_chan == bss_pri_chan) {
228 6 : wpa_printf(MSG_INFO, "Switch own primary and "
229 : "secondary channel due to BSS "
230 : "overlap with " MACSTR,
231 6 : MAC2STR(bss->bssid));
232 1 : return 2;
233 : }
234 : }
235 : }
236 :
237 22 : return 1;
238 : }
239 :
240 :
241 5 : int check_20mhz_bss(struct wpa_scan_res *bss, int pri_freq, int start, int end)
242 : {
243 : struct ieee802_11_elems elems;
244 : struct ieee80211_ht_operation *oper;
245 :
246 5 : if (bss->freq < start || bss->freq > end || bss->freq == pri_freq)
247 2 : return 0;
248 :
249 3 : ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
250 3 : if (!elems.ht_capabilities) {
251 7 : wpa_printf(MSG_DEBUG, "Found overlapping legacy BSS: "
252 6 : MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
253 1 : return 1;
254 : }
255 :
256 4 : if (elems.ht_operation &&
257 2 : elems.ht_operation_len >= sizeof(*oper)) {
258 2 : oper = (struct ieee80211_ht_operation *) elems.ht_operation;
259 2 : if (oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)
260 2 : return 0;
261 :
262 0 : wpa_printf(MSG_DEBUG, "Found overlapping 20 MHz HT BSS: "
263 0 : MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
264 0 : return 1;
265 : }
266 0 : return 0;
267 : }
268 :
269 :
270 17 : int check_40mhz_2g4(struct hostapd_hw_modes *mode,
271 : struct wpa_scan_results *scan_res, int pri_chan,
272 : int sec_chan)
273 : {
274 : int pri_freq, sec_freq;
275 : int affected_start, affected_end;
276 : size_t i;
277 :
278 17 : if (!mode || !scan_res || !pri_chan || !sec_chan)
279 0 : return 0;
280 :
281 17 : if (pri_chan == sec_chan)
282 0 : return 0;
283 :
284 17 : pri_freq = hw_get_freq(mode, pri_chan);
285 17 : sec_freq = hw_get_freq(mode, sec_chan);
286 :
287 17 : affected_start = (pri_freq + sec_freq) / 2 - 25;
288 17 : affected_end = (pri_freq + sec_freq) / 2 + 25;
289 17 : wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
290 : affected_start, affected_end);
291 19 : for (i = 0; i < scan_res->num; i++) {
292 5 : struct wpa_scan_res *bss = scan_res->res[i];
293 5 : int pri = bss->freq;
294 5 : int sec = pri;
295 : struct ieee802_11_elems elems;
296 :
297 : /* Check for overlapping 20 MHz BSS */
298 5 : if (check_20mhz_bss(bss, pri_freq, affected_start,
299 : affected_end)) {
300 1 : wpa_printf(MSG_DEBUG,
301 : "Overlapping 20 MHz BSS is found");
302 4 : return 0;
303 : }
304 :
305 4 : get_pri_sec_chan(bss, &pri_chan, &sec_chan);
306 :
307 4 : if (sec_chan) {
308 4 : if (sec_chan < pri_chan)
309 2 : sec = pri - 20;
310 : else
311 2 : sec = pri + 20;
312 : }
313 :
314 4 : if ((pri < affected_start || pri > affected_end) &&
315 1 : (sec < affected_start || sec > affected_end))
316 1 : continue; /* not within affected channel range */
317 :
318 21 : wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
319 : " freq=%d pri=%d sec=%d",
320 18 : MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
321 :
322 3 : if (sec_chan) {
323 3 : if (pri_freq != pri || sec_freq != sec) {
324 14 : wpa_printf(MSG_DEBUG,
325 : "40 MHz pri/sec mismatch with BSS "
326 : MACSTR
327 : " <%d,%d> (chan=%d%c) vs. <%d,%d>",
328 12 : MAC2STR(bss->bssid),
329 : pri, sec, pri_chan,
330 : sec > pri ? '+' : '-',
331 : pri_freq, sec_freq);
332 2 : return 0;
333 : }
334 : }
335 :
336 1 : ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems,
337 : 0);
338 2 : if (elems.ht_capabilities &&
339 1 : elems.ht_capabilities_len >=
340 : sizeof(struct ieee80211_ht_capabilities)) {
341 1 : struct ieee80211_ht_capabilities *ht_cap =
342 : (struct ieee80211_ht_capabilities *)
343 : elems.ht_capabilities;
344 :
345 1 : if (le_to_host16(ht_cap->ht_capabilities_info) &
346 : HT_CAP_INFO_40MHZ_INTOLERANT) {
347 0 : wpa_printf(MSG_DEBUG,
348 : "40 MHz Intolerant is set on channel %d in BSS "
349 0 : MACSTR, pri, MAC2STR(bss->bssid));
350 0 : return 0;
351 : }
352 : }
353 : }
354 :
355 14 : return 1;
356 : }
|