• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Operating classes
3  * Copyright(c) 2015 Intel Deutschland GmbH
4  * Contact Information:
5  * Intel Linux Wireless <ilw@linux.intel.com>
6  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
7  *
8  * This software may be distributed under the terms of the BSD license.
9  * See README for more details.
10  */
11 
12 #include "utils/includes.h"
13 
14 #include "utils/common.h"
15 #include "common/ieee802_11_common.h"
16 #include "wpa_supplicant_i.h"
17 #include "bss.h"
18 
19 
allow_channel(struct hostapd_hw_modes * mode,u8 op_class,u8 chan,unsigned int * flags)20 static enum chan_allowed allow_channel(struct hostapd_hw_modes *mode,
21 				       u8 op_class, u8 chan,
22 				       unsigned int *flags)
23 {
24 	int i;
25 	bool is_6ghz = op_class >= 131 && op_class <= 136;
26 
27 	for (i = 0; i < mode->num_channels; i++) {
28 		bool chan_is_6ghz;
29 
30 		chan_is_6ghz = mode->channels[i].freq >= 5935 &&
31 			mode->channels[i].freq <= 7115;
32 		if (is_6ghz == chan_is_6ghz && mode->channels[i].chan == chan)
33 			break;
34 	}
35 
36 	if (i == mode->num_channels ||
37 	    (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED))
38 		return NOT_ALLOWED;
39 
40 	if (flags)
41 		*flags = mode->channels[i].flag;
42 
43 	if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
44 		return NO_IR;
45 
46 	return ALLOWED;
47 }
48 
49 
get_center_80mhz(struct hostapd_hw_modes * mode,u8 channel,const u8 * center_channels,size_t num_chan)50 static int get_center_80mhz(struct hostapd_hw_modes *mode, u8 channel,
51 			    const u8 *center_channels, size_t num_chan)
52 {
53 	size_t i;
54 
55 	if (mode->mode != HOSTAPD_MODE_IEEE80211A)
56 		return 0;
57 
58 	for (i = 0; i < num_chan; i++) {
59 		/*
60 		 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
61 		 * so the center channel is 6 channels away from the start/end.
62 		 */
63 		if (channel >= center_channels[i] - 6 &&
64 		    channel <= center_channels[i] + 6)
65 			return center_channels[i];
66 	}
67 
68 	return 0;
69 }
70 
71 
verify_80mhz(struct hostapd_hw_modes * mode,u8 op_class,u8 channel)72 static enum chan_allowed verify_80mhz(struct hostapd_hw_modes *mode,
73 				      u8 op_class, u8 channel)
74 {
75 	u8 center_chan;
76 	unsigned int i;
77 	unsigned int no_ir = 0;
78 	const u8 *center_channels;
79 	size_t num_chan;
80 	const u8 center_channels_5ghz[] = { 42, 58, 106, 122, 138, 155, 171 };
81 	const u8 center_channels_6ghz[] = { 7, 23, 39, 55, 71, 87, 103, 119,
82 					    135, 151, 167, 183, 199, 215 };
83 
84 	if (is_6ghz_op_class(op_class)) {
85 		center_channels = center_channels_6ghz;
86 		num_chan = ARRAY_SIZE(center_channels_6ghz);
87 	} else {
88 		center_channels = center_channels_5ghz;
89 		num_chan = ARRAY_SIZE(center_channels_5ghz);
90 	}
91 
92 	center_chan = get_center_80mhz(mode, channel, center_channels,
93 				       num_chan);
94 	if (!center_chan)
95 		return NOT_ALLOWED;
96 
97 	/* check all the channels are available */
98 	for (i = 0; i < 4; i++) {
99 		unsigned int flags;
100 		u8 adj_chan = center_chan - 6 + i * 4;
101 
102 		if (allow_channel(mode, op_class, adj_chan, &flags) ==
103 		    NOT_ALLOWED)
104 			return NOT_ALLOWED;
105 
106 		if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL))
107 			return NOT_ALLOWED;
108 
109 		if (flags & HOSTAPD_CHAN_NO_IR)
110 			no_ir = 1;
111 	}
112 
113 	if (no_ir)
114 		return NO_IR;
115 
116 	return ALLOWED;
117 }
118 
119 
get_center_160mhz(struct hostapd_hw_modes * mode,u8 channel,const u8 * center_channels,size_t num_chan)120 static int get_center_160mhz(struct hostapd_hw_modes *mode, u8 channel,
121 			     const u8 *center_channels, size_t num_chan)
122 {
123 	unsigned int i;
124 
125 	if (mode->mode != HOSTAPD_MODE_IEEE80211A)
126 		return 0;
127 
128 	for (i = 0; i < num_chan; i++) {
129 		/*
130 		 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
131 		 * so the center channel is 14 channels away from the start/end.
132 		 */
133 		if (channel >= center_channels[i] - 14 &&
134 		    channel <= center_channels[i] + 14)
135 			return center_channels[i];
136 	}
137 
138 	return 0;
139 }
140 
141 
verify_160mhz(struct hostapd_hw_modes * mode,u8 op_class,u8 channel)142 static enum chan_allowed verify_160mhz(struct hostapd_hw_modes *mode,
143 				       u8 op_class, u8 channel)
144 {
145 	u8 center_chan;
146 	unsigned int i;
147 	unsigned int no_ir = 0;
148 	const u8 *center_channels;
149 	size_t num_chan;
150 	const u8 center_channels_5ghz[] = { 50, 114, 163 };
151 	const u8 center_channels_6ghz[] = { 15, 47, 79, 111, 143, 175, 207 };
152 
153 	if (is_6ghz_op_class(op_class)) {
154 		center_channels = center_channels_6ghz;
155 		num_chan = ARRAY_SIZE(center_channels_6ghz);
156 	} else {
157 		center_channels = center_channels_5ghz;
158 		num_chan = ARRAY_SIZE(center_channels_5ghz);
159 	}
160 
161 	center_chan = get_center_160mhz(mode, channel, center_channels,
162 					num_chan);
163 	if (!center_chan)
164 		return NOT_ALLOWED;
165 
166 	/* Check all the channels are available */
167 	for (i = 0; i < 8; i++) {
168 		unsigned int flags;
169 		u8 adj_chan = center_chan - 14 + i * 4;
170 
171 		if (allow_channel(mode, op_class, adj_chan, &flags) ==
172 		    NOT_ALLOWED)
173 			return NOT_ALLOWED;
174 
175 		if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL) ||
176 		    !(flags & HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL))
177 			return NOT_ALLOWED;
178 
179 		if (flags & HOSTAPD_CHAN_NO_IR)
180 			no_ir = 1;
181 	}
182 
183 	if (no_ir)
184 		return NO_IR;
185 
186 	return ALLOWED;
187 }
188 
189 
verify_channel(struct hostapd_hw_modes * mode,u8 op_class,u8 channel,u8 bw)190 enum chan_allowed verify_channel(struct hostapd_hw_modes *mode, u8 op_class,
191 				 u8 channel, u8 bw)
192 {
193 	unsigned int flag = 0;
194 	enum chan_allowed res, res2;
195 
196 	res2 = res = allow_channel(mode, op_class, channel, &flag);
197 	if (bw == BW40MINUS || (bw == BW40 && (((channel - 1) / 4) % 2))) {
198 		if (!(flag & HOSTAPD_CHAN_HT40MINUS))
199 			return NOT_ALLOWED;
200 		res2 = allow_channel(mode, op_class, channel - 4, NULL);
201 	} else if (bw == BW40PLUS) {
202 		if (!(flag & HOSTAPD_CHAN_HT40PLUS))
203 			return NOT_ALLOWED;
204 		res2 = allow_channel(mode, op_class, channel + 4, NULL);
205 	} else if (is_6ghz_op_class(op_class) && bw == BW40) {
206 		if (get_6ghz_sec_channel(channel) < 0)
207 			res2 = allow_channel(mode, op_class, channel - 4, NULL);
208 		else
209 			res2 = allow_channel(mode, op_class, channel + 4, NULL);
210 	} else if (bw == BW80) {
211 		/*
212 		 * channel is a center channel and as such, not necessarily a
213 		 * valid 20 MHz channels. Override earlier allow_channel()
214 		 * result and use only the 80 MHz specific version.
215 		 */
216 		res2 = res = verify_80mhz(mode, op_class, channel);
217 	} else if (bw == BW160) {
218 		/*
219 		 * channel is a center channel and as such, not necessarily a
220 		 * valid 20 MHz channels. Override earlier allow_channel()
221 		 * result and use only the 160 MHz specific version.
222 		 */
223 		res2 = res = verify_160mhz(mode, op_class, channel);
224 	} else if (bw == BW80P80) {
225 		/*
226 		 * channel is a center channel and as such, not necessarily a
227 		 * valid 20 MHz channels. Override earlier allow_channel()
228 		 * result and use only the 80 MHz specific version.
229 		 */
230 		res2 = res = verify_80mhz(mode, op_class, channel);
231 	}
232 
233 	if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
234 		return NOT_ALLOWED;
235 
236 	if (res == NO_IR || res2 == NO_IR)
237 		return NO_IR;
238 
239 	return ALLOWED;
240 }
241 
242 
wpas_op_class_supported(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const struct oper_class_map * op_class)243 static int wpas_op_class_supported(struct wpa_supplicant *wpa_s,
244 				   struct wpa_ssid *ssid,
245 				   const struct oper_class_map *op_class)
246 {
247 	int chan;
248 	size_t i;
249 	struct hostapd_hw_modes *mode;
250 	int found;
251 	int z;
252 	int freq2 = 0;
253 	int freq5 = 0;
254 
255 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op_class->mode,
256 			is_6ghz_op_class(op_class->op_class));
257 	if (!mode)
258 		return 0;
259 
260 	/* If we are configured to disable certain things, take that into
261 	 * account here. */
262 	if (ssid && ssid->freq_list && ssid->freq_list[0]) {
263 		for (z = 0; ; z++) {
264 			int f = ssid->freq_list[z];
265 
266 			if (f == 0)
267 				break; /* end of list */
268 			if (f > 4000 && f < 6000)
269 				freq5 = 1;
270 			else if (f > 2400 && f < 2500)
271 				freq2 = 1;
272 		}
273 	} else {
274 		/* No frequencies specified, can use anything hardware supports.
275 		 */
276 		freq2 = freq5 = 1;
277 	}
278 
279 	if (op_class->op_class >= 115 && op_class->op_class <= 130 && !freq5)
280 		return 0;
281 	if (op_class->op_class >= 81 && op_class->op_class <= 84 && !freq2)
282 		return 0;
283 
284 #ifdef CONFIG_HT_OVERRIDES
285 	if (ssid && ssid->disable_ht) {
286 		switch (op_class->op_class) {
287 		case 83:
288 		case 84:
289 		case 104:
290 		case 105:
291 		case 116:
292 		case 117:
293 		case 119:
294 		case 120:
295 		case 122:
296 		case 123:
297 		case 126:
298 		case 127:
299 		case 128:
300 		case 129:
301 		case 130:
302 			/* Disable >= 40 MHz channels if HT is disabled */
303 			return 0;
304 		}
305 	}
306 #endif /* CONFIG_HT_OVERRIDES */
307 
308 #ifdef CONFIG_VHT_OVERRIDES
309 	if (ssid && ssid->disable_vht) {
310 		if (op_class->op_class >= 128 && op_class->op_class <= 130) {
311 			/* Disable >= 80 MHz channels if VHT is disabled */
312 			return 0;
313 		}
314 	}
315 #endif /* CONFIG_VHT_OVERRIDES */
316 
317 	if (op_class->op_class == 128) {
318 		u8 channels[] = { 42, 58, 106, 122, 138, 155, 171 };
319 
320 		for (i = 0; i < ARRAY_SIZE(channels); i++) {
321 			if (verify_channel(mode, op_class->op_class,
322 					   channels[i], op_class->bw) !=
323 			    NOT_ALLOWED)
324 				return 1;
325 		}
326 
327 		return 0;
328 	}
329 
330 	if (op_class->op_class == 129) {
331 		/* Check if either 160 MHz channels is allowed */
332 		return verify_channel(mode, op_class->op_class, 50,
333 				      op_class->bw) != NOT_ALLOWED ||
334 			verify_channel(mode, op_class->op_class, 114,
335 				       op_class->bw) != NOT_ALLOWED ||
336 			verify_channel(mode, op_class->op_class, 163,
337 				       op_class->bw) != NOT_ALLOWED;
338 	}
339 
340 	if (op_class->op_class == 130) {
341 		/* Need at least two non-contiguous 80 MHz segments */
342 		found = 0;
343 
344 		if (verify_channel(mode, op_class->op_class, 42,
345 				   op_class->bw) != NOT_ALLOWED ||
346 		    verify_channel(mode, op_class->op_class, 58,
347 				   op_class->bw) != NOT_ALLOWED)
348 			found++;
349 		if (verify_channel(mode, op_class->op_class, 106,
350 				   op_class->bw) != NOT_ALLOWED ||
351 		    verify_channel(mode, op_class->op_class, 122,
352 				   op_class->bw) != NOT_ALLOWED ||
353 		    verify_channel(mode, op_class->op_class, 138,
354 				   op_class->bw) != NOT_ALLOWED ||
355 		    verify_channel(mode, op_class->op_class, 155,
356 				   op_class->bw) != NOT_ALLOWED ||
357 		    verify_channel(mode, op_class->op_class, 171,
358 				   op_class->bw) != NOT_ALLOWED)
359 			found++;
360 		if (verify_channel(mode, op_class->op_class, 106,
361 				   op_class->bw) != NOT_ALLOWED &&
362 		    verify_channel(mode, op_class->op_class, 138,
363 				   op_class->bw) != NOT_ALLOWED)
364 			found++;
365 		if (verify_channel(mode, op_class->op_class, 122,
366 				   op_class->bw) != NOT_ALLOWED &&
367 		    verify_channel(mode, op_class->op_class, 155,
368 				   op_class->bw) != NOT_ALLOWED)
369 			found++;
370 		if (verify_channel(mode, op_class->op_class, 138,
371 				   op_class->bw) != NOT_ALLOWED &&
372 		    verify_channel(mode, op_class->op_class, 171,
373 				   op_class->bw) != NOT_ALLOWED)
374 			found++;
375 
376 		if (found >= 2)
377 			return 1;
378 
379 		return 0;
380 	}
381 
382 	if (op_class->op_class == 135) {
383 		/* Need at least two 80 MHz segments which do not fall under the
384 		 * same 160 MHz segment to support 80+80 in 6 GHz.
385 		 */
386 		int first_seg = 0;
387 		int curr_seg = 0;
388 
389 		for (chan = op_class->min_chan; chan <= op_class->max_chan;
390 		     chan += op_class->inc) {
391 			curr_seg++;
392 			if (verify_channel(mode, op_class->op_class, chan,
393 					   op_class->bw) != NOT_ALLOWED) {
394 				if (!first_seg) {
395 					first_seg = curr_seg;
396 					continue;
397 				}
398 
399 				/* Supported if at least two non-consecutive 80
400 				 * MHz segments allowed.
401 				 */
402 				if ((curr_seg - first_seg) > 1)
403 					return 1;
404 
405 				/* Supported even if the 80 MHz segments are
406 				 * consecutive when they do not fall under the
407 				 * same 160 MHz segment.
408 				 */
409 				if ((first_seg % 2) == 0)
410 					return 1;
411 			}
412 		}
413 
414 		return 0;
415 	}
416 
417 	found = 0;
418 	for (chan = op_class->min_chan; chan <= op_class->max_chan;
419 	     chan += op_class->inc) {
420 		if (verify_channel(mode, op_class->op_class, chan,
421 				   op_class->bw) != NOT_ALLOWED) {
422 			found = 1;
423 			break;
424 		}
425 	}
426 
427 	return found;
428 }
429 
430 
wpas_sta_secondary_channel_offset(struct wpa_bss * bss,u8 * current,u8 * channel)431 static int wpas_sta_secondary_channel_offset(struct wpa_bss *bss, u8 *current,
432 					     u8 *channel)
433 {
434 
435 	const u8 *ies;
436 	u8 phy_type;
437 	size_t ies_len;
438 
439 	if (!bss)
440 		return -1;
441 	ies = wpa_bss_ie_ptr(bss);
442 	ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
443 	return wpas_get_op_chan_phy(bss->freq, ies, ies_len, current,
444 				    channel, &phy_type);
445 }
446 
447 
wpas_supp_op_class_ie(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_bss * bss,u8 * pos,size_t len)448 size_t wpas_supp_op_class_ie(struct wpa_supplicant *wpa_s,
449 			     struct wpa_ssid *ssid,
450 			     struct wpa_bss *bss, u8 *pos, size_t len)
451 {
452 	struct wpabuf *buf;
453 	u8 op, current, chan;
454 	u8 *ie_len;
455 	size_t res;
456 
457 	/*
458 	 * Determine the current operating class correct mode based on
459 	 * advertised BSS capabilities, if available. Fall back to a less
460 	 * accurate guess based on frequency if the needed IEs are not available
461 	 * or used.
462 	 */
463 	if (wpas_sta_secondary_channel_offset(bss, &current, &chan) < 0 &&
464 	    ieee80211_freq_to_channel_ext(bss->freq, 0,
465 					  CONF_OPER_CHWIDTH_USE_HT, &current,
466 					  &chan) == NUM_HOSTAPD_MODES)
467 		return 0;
468 
469 	/*
470 	 * Need 3 bytes for EID, length, and current operating class, plus
471 	 * 1 byte for every other supported operating class.
472 	 */
473 	buf = wpabuf_alloc(global_op_class_size + 3);
474 	if (!buf)
475 		return 0;
476 
477 	wpabuf_put_u8(buf, WLAN_EID_SUPPORTED_OPERATING_CLASSES);
478 	/* Will set the length later, putting a placeholder */
479 	ie_len = wpabuf_put(buf, 1);
480 	wpabuf_put_u8(buf, current);
481 
482 	for (op = 0; global_op_class[op].op_class; op++) {
483 		if (wpas_op_class_supported(wpa_s, ssid, &global_op_class[op]))
484 			wpabuf_put_u8(buf, global_op_class[op].op_class);
485 	}
486 
487 	*ie_len = wpabuf_len(buf) - 2;
488 	if (*ie_len < 2) {
489 		wpa_printf(MSG_DEBUG,
490 			   "No supported operating classes IE to add");
491 		res = 0;
492 	} else if (wpabuf_len(buf) > len) {
493 		wpa_printf(MSG_ERROR,
494 			   "Supported operating classes IE exceeds maximum buffer length");
495 		res = 0;
496 	} else {
497 		os_memcpy(pos, wpabuf_head(buf), wpabuf_len(buf));
498 		res = wpabuf_len(buf);
499 		wpa_hexdump_buf(MSG_DEBUG,
500 				"Added supported operating classes IE", buf);
501 	}
502 
503 	wpabuf_free(buf);
504 	return res;
505 }
506 
507 
wpas_supp_op_classes(struct wpa_supplicant * wpa_s)508 int * wpas_supp_op_classes(struct wpa_supplicant *wpa_s)
509 {
510 	int op;
511 	unsigned int pos, max_num = 0;
512 	int *classes;
513 
514 	for (op = 0; global_op_class[op].op_class; op++)
515 		max_num++;
516 	classes = os_zalloc((max_num + 1) * sizeof(int));
517 	if (!classes)
518 		return NULL;
519 
520 	for (op = 0, pos = 0; global_op_class[op].op_class; op++) {
521 		if (wpas_op_class_supported(wpa_s, NULL, &global_op_class[op]))
522 			classes[pos++] = global_op_class[op].op_class;
523 	}
524 
525 	return classes;
526 }
527