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