1ebb5671cSchristos /*
2ebb5671cSchristos * Operating classes
3ebb5671cSchristos * Copyright(c) 2015 Intel Deutschland GmbH
4ebb5671cSchristos * Contact Information:
5ebb5671cSchristos * Intel Linux Wireless <ilw@linux.intel.com>
6ebb5671cSchristos * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
7ebb5671cSchristos *
8ebb5671cSchristos * This software may be distributed under the terms of the BSD license.
9ebb5671cSchristos * See README for more details.
10ebb5671cSchristos */
11ebb5671cSchristos
12ebb5671cSchristos #include "utils/includes.h"
13ebb5671cSchristos
14ebb5671cSchristos #include "utils/common.h"
15ebb5671cSchristos #include "common/ieee802_11_common.h"
16ebb5671cSchristos #include "wpa_supplicant_i.h"
17ebb5671cSchristos
18ebb5671cSchristos
allow_channel(struct hostapd_hw_modes * mode,u8 chan,unsigned int * flags)19ebb5671cSchristos static enum chan_allowed allow_channel(struct hostapd_hw_modes *mode, u8 chan,
20ebb5671cSchristos unsigned int *flags)
21ebb5671cSchristos {
22ebb5671cSchristos int i;
23ebb5671cSchristos
24ebb5671cSchristos for (i = 0; i < mode->num_channels; i++) {
25ebb5671cSchristos if (mode->channels[i].chan == chan)
26ebb5671cSchristos break;
27ebb5671cSchristos }
28ebb5671cSchristos
29ebb5671cSchristos if (i == mode->num_channels ||
30ebb5671cSchristos (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED))
31ebb5671cSchristos return NOT_ALLOWED;
32ebb5671cSchristos
33ebb5671cSchristos if (flags)
34ebb5671cSchristos *flags = mode->channels[i].flag;
35ebb5671cSchristos
36ebb5671cSchristos if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
37ebb5671cSchristos return NO_IR;
38ebb5671cSchristos
39ebb5671cSchristos return ALLOWED;
40ebb5671cSchristos }
41ebb5671cSchristos
42ebb5671cSchristos
get_center_80mhz(struct hostapd_hw_modes * mode,u8 channel)43ebb5671cSchristos static int get_center_80mhz(struct hostapd_hw_modes *mode, u8 channel)
44ebb5671cSchristos {
45ebb5671cSchristos u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
46ebb5671cSchristos size_t i;
47ebb5671cSchristos
48ebb5671cSchristos if (mode->mode != HOSTAPD_MODE_IEEE80211A)
49ebb5671cSchristos return 0;
50ebb5671cSchristos
51ebb5671cSchristos for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
52ebb5671cSchristos /*
53ebb5671cSchristos * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
54ebb5671cSchristos * so the center channel is 6 channels away from the start/end.
55ebb5671cSchristos */
56ebb5671cSchristos if (channel >= center_channels[i] - 6 &&
57ebb5671cSchristos channel <= center_channels[i] + 6)
58ebb5671cSchristos return center_channels[i];
59ebb5671cSchristos }
60ebb5671cSchristos
61ebb5671cSchristos return 0;
62ebb5671cSchristos }
63ebb5671cSchristos
64ebb5671cSchristos
verify_80mhz(struct hostapd_hw_modes * mode,u8 channel)65ebb5671cSchristos static enum chan_allowed verify_80mhz(struct hostapd_hw_modes *mode, u8 channel)
66ebb5671cSchristos {
67ebb5671cSchristos u8 center_chan;
68ebb5671cSchristos unsigned int i;
69ebb5671cSchristos unsigned int no_ir = 0;
70ebb5671cSchristos
71ebb5671cSchristos center_chan = get_center_80mhz(mode, channel);
72ebb5671cSchristos if (!center_chan)
73ebb5671cSchristos return NOT_ALLOWED;
74ebb5671cSchristos
75ebb5671cSchristos /* check all the channels are available */
76ebb5671cSchristos for (i = 0; i < 4; i++) {
77ebb5671cSchristos unsigned int flags;
78ebb5671cSchristos u8 adj_chan = center_chan - 6 + i * 4;
79ebb5671cSchristos
80ebb5671cSchristos if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
81ebb5671cSchristos return NOT_ALLOWED;
82ebb5671cSchristos
83ebb5671cSchristos if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) ||
84ebb5671cSchristos (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) ||
85ebb5671cSchristos (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) ||
86ebb5671cSchristos (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)))
87ebb5671cSchristos return NOT_ALLOWED;
88ebb5671cSchristos
89ebb5671cSchristos if (flags & HOSTAPD_CHAN_NO_IR)
90ebb5671cSchristos no_ir = 1;
91ebb5671cSchristos }
92ebb5671cSchristos
93ebb5671cSchristos if (no_ir)
94ebb5671cSchristos return NO_IR;
95ebb5671cSchristos
96ebb5671cSchristos return ALLOWED;
97ebb5671cSchristos }
98ebb5671cSchristos
99ebb5671cSchristos
get_center_160mhz(struct hostapd_hw_modes * mode,u8 channel)100ebb5671cSchristos static int get_center_160mhz(struct hostapd_hw_modes *mode, u8 channel)
101ebb5671cSchristos {
102ebb5671cSchristos u8 center_channels[] = { 50, 114 };
103ebb5671cSchristos unsigned int i;
104ebb5671cSchristos
105ebb5671cSchristos if (mode->mode != HOSTAPD_MODE_IEEE80211A)
106ebb5671cSchristos return 0;
107ebb5671cSchristos
108ebb5671cSchristos for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
109ebb5671cSchristos /*
110ebb5671cSchristos * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
111ebb5671cSchristos * so the center channel is 14 channels away from the start/end.
112ebb5671cSchristos */
113ebb5671cSchristos if (channel >= center_channels[i] - 14 &&
114ebb5671cSchristos channel <= center_channels[i] + 14)
115ebb5671cSchristos return center_channels[i];
116ebb5671cSchristos }
117ebb5671cSchristos
118ebb5671cSchristos return 0;
119ebb5671cSchristos }
120ebb5671cSchristos
121ebb5671cSchristos
verify_160mhz(struct hostapd_hw_modes * mode,u8 channel)122ebb5671cSchristos static enum chan_allowed verify_160mhz(struct hostapd_hw_modes *mode,
123ebb5671cSchristos u8 channel)
124ebb5671cSchristos {
125ebb5671cSchristos u8 center_chan;
126ebb5671cSchristos unsigned int i;
127ebb5671cSchristos unsigned int no_ir = 0;
128ebb5671cSchristos
129ebb5671cSchristos center_chan = get_center_160mhz(mode, channel);
130ebb5671cSchristos if (!center_chan)
131ebb5671cSchristos return NOT_ALLOWED;
132ebb5671cSchristos
133ebb5671cSchristos /* Check all the channels are available */
134ebb5671cSchristos for (i = 0; i < 8; i++) {
135ebb5671cSchristos unsigned int flags;
136ebb5671cSchristos u8 adj_chan = center_chan - 14 + i * 4;
137ebb5671cSchristos
138ebb5671cSchristos if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
139ebb5671cSchristos return NOT_ALLOWED;
140ebb5671cSchristos
141ebb5671cSchristos if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150)) ||
142ebb5671cSchristos (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130)) ||
143ebb5671cSchristos (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110)) ||
144ebb5671cSchristos (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90)) ||
145ebb5671cSchristos (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70)) ||
146ebb5671cSchristos (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50)) ||
147ebb5671cSchristos (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30)) ||
148ebb5671cSchristos (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10)))
149ebb5671cSchristos return NOT_ALLOWED;
150ebb5671cSchristos
151ebb5671cSchristos if (flags & HOSTAPD_CHAN_NO_IR)
152ebb5671cSchristos no_ir = 1;
153ebb5671cSchristos }
154ebb5671cSchristos
155ebb5671cSchristos if (no_ir)
156ebb5671cSchristos return NO_IR;
157ebb5671cSchristos
158ebb5671cSchristos return ALLOWED;
159ebb5671cSchristos }
160ebb5671cSchristos
161ebb5671cSchristos
verify_channel(struct hostapd_hw_modes * mode,u8 channel,u8 bw)162ebb5671cSchristos enum chan_allowed verify_channel(struct hostapd_hw_modes *mode, u8 channel,
163ebb5671cSchristos u8 bw)
164ebb5671cSchristos {
165ebb5671cSchristos unsigned int flag = 0;
166ebb5671cSchristos enum chan_allowed res, res2;
167ebb5671cSchristos
168ebb5671cSchristos res2 = res = allow_channel(mode, channel, &flag);
169ebb5671cSchristos if (bw == BW40MINUS) {
170ebb5671cSchristos if (!(flag & HOSTAPD_CHAN_HT40MINUS))
171ebb5671cSchristos return NOT_ALLOWED;
172ebb5671cSchristos res2 = allow_channel(mode, channel - 4, NULL);
173ebb5671cSchristos } else if (bw == BW40PLUS) {
174ebb5671cSchristos if (!(flag & HOSTAPD_CHAN_HT40PLUS))
175ebb5671cSchristos return NOT_ALLOWED;
176ebb5671cSchristos res2 = allow_channel(mode, channel + 4, NULL);
177ebb5671cSchristos } else if (bw == BW80) {
178ebb5671cSchristos /*
179ebb5671cSchristos * channel is a center channel and as such, not necessarily a
180ebb5671cSchristos * valid 20 MHz channels. Override earlier allow_channel()
181ebb5671cSchristos * result and use only the 80 MHz specific version.
182ebb5671cSchristos */
183ebb5671cSchristos res2 = res = verify_80mhz(mode, channel);
184ebb5671cSchristos } else if (bw == BW160) {
185ebb5671cSchristos /*
186ebb5671cSchristos * channel is a center channel and as such, not necessarily a
187ebb5671cSchristos * valid 20 MHz channels. Override earlier allow_channel()
188ebb5671cSchristos * result and use only the 160 MHz specific version.
189ebb5671cSchristos */
190ebb5671cSchristos res2 = res = verify_160mhz(mode, channel);
191ebb5671cSchristos } else if (bw == BW80P80) {
192ebb5671cSchristos /*
193ebb5671cSchristos * channel is a center channel and as such, not necessarily a
194ebb5671cSchristos * valid 20 MHz channels. Override earlier allow_channel()
195ebb5671cSchristos * result and use only the 80 MHz specific version.
196ebb5671cSchristos */
197ebb5671cSchristos res2 = res = verify_80mhz(mode, channel);
198ebb5671cSchristos }
199ebb5671cSchristos
200ebb5671cSchristos if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
201ebb5671cSchristos return NOT_ALLOWED;
202ebb5671cSchristos
203ebb5671cSchristos if (res == NO_IR || res2 == NO_IR)
204ebb5671cSchristos return NO_IR;
205ebb5671cSchristos
206ebb5671cSchristos return ALLOWED;
207ebb5671cSchristos }
208ebb5671cSchristos
209ebb5671cSchristos
wpas_op_class_supported(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const struct oper_class_map * op_class)210ebb5671cSchristos static int wpas_op_class_supported(struct wpa_supplicant *wpa_s,
211*0dddab58Schristos struct wpa_ssid *ssid,
212ebb5671cSchristos const struct oper_class_map *op_class)
213ebb5671cSchristos {
214ebb5671cSchristos int chan;
215ebb5671cSchristos size_t i;
216ebb5671cSchristos struct hostapd_hw_modes *mode;
217ebb5671cSchristos int found;
218*0dddab58Schristos int z;
219*0dddab58Schristos int freq2 = 0;
220*0dddab58Schristos int freq5 = 0;
221ebb5671cSchristos
222ebb5671cSchristos mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op_class->mode);
223ebb5671cSchristos if (!mode)
224ebb5671cSchristos return 0;
225ebb5671cSchristos
226*0dddab58Schristos /* If we are configured to disable certain things, take that into
227*0dddab58Schristos * account here. */
228*0dddab58Schristos if (ssid->freq_list && ssid->freq_list[0]) {
229*0dddab58Schristos for (z = 0; ; z++) {
230*0dddab58Schristos int f = ssid->freq_list[z];
231*0dddab58Schristos
232*0dddab58Schristos if (f == 0)
233*0dddab58Schristos break; /* end of list */
234*0dddab58Schristos if (f > 4000 && f < 6000)
235*0dddab58Schristos freq5 = 1;
236*0dddab58Schristos else if (f > 2400 && f < 2500)
237*0dddab58Schristos freq2 = 1;
238*0dddab58Schristos }
239*0dddab58Schristos } else {
240*0dddab58Schristos /* No frequencies specified, can use anything hardware supports.
241*0dddab58Schristos */
242*0dddab58Schristos freq2 = freq5 = 1;
243*0dddab58Schristos }
244*0dddab58Schristos
245*0dddab58Schristos if (op_class->op_class >= 115 && op_class->op_class <= 130 && !freq5)
246*0dddab58Schristos return 0;
247*0dddab58Schristos if (op_class->op_class >= 81 && op_class->op_class <= 84 && !freq2)
248*0dddab58Schristos return 0;
249*0dddab58Schristos
250*0dddab58Schristos #ifdef CONFIG_HT_OVERRIDES
251*0dddab58Schristos if (ssid->disable_ht) {
252*0dddab58Schristos switch (op_class->op_class) {
253*0dddab58Schristos case 83:
254*0dddab58Schristos case 84:
255*0dddab58Schristos case 104:
256*0dddab58Schristos case 105:
257*0dddab58Schristos case 116:
258*0dddab58Schristos case 117:
259*0dddab58Schristos case 119:
260*0dddab58Schristos case 120:
261*0dddab58Schristos case 122:
262*0dddab58Schristos case 123:
263*0dddab58Schristos case 126:
264*0dddab58Schristos case 127:
265*0dddab58Schristos case 128:
266*0dddab58Schristos case 129:
267*0dddab58Schristos case 130:
268*0dddab58Schristos /* Disable >= 40 MHz channels if HT is disabled */
269*0dddab58Schristos return 0;
270*0dddab58Schristos }
271*0dddab58Schristos }
272*0dddab58Schristos #endif /* CONFIG_HT_OVERRIDES */
273*0dddab58Schristos
274*0dddab58Schristos #ifdef CONFIG_VHT_OVERRIDES
275*0dddab58Schristos if (ssid->disable_vht) {
276*0dddab58Schristos if (op_class->op_class >= 128 && op_class->op_class <= 130) {
277*0dddab58Schristos /* Disable >= 80 MHz channels if VHT is disabled */
278*0dddab58Schristos return 0;
279*0dddab58Schristos }
280*0dddab58Schristos }
281*0dddab58Schristos #endif /* CONFIG_VHT_OVERRIDES */
282*0dddab58Schristos
283ebb5671cSchristos if (op_class->op_class == 128) {
284ebb5671cSchristos u8 channels[] = { 42, 58, 106, 122, 138, 155 };
285ebb5671cSchristos
286ebb5671cSchristos for (i = 0; i < ARRAY_SIZE(channels); i++) {
287ebb5671cSchristos if (verify_channel(mode, channels[i], op_class->bw) !=
288ebb5671cSchristos NOT_ALLOWED)
289ebb5671cSchristos return 1;
290ebb5671cSchristos }
291ebb5671cSchristos
292ebb5671cSchristos return 0;
293ebb5671cSchristos }
294ebb5671cSchristos
295ebb5671cSchristos if (op_class->op_class == 129) {
296ebb5671cSchristos /* Check if either 160 MHz channels is allowed */
297ebb5671cSchristos return verify_channel(mode, 50, op_class->bw) != NOT_ALLOWED ||
298ebb5671cSchristos verify_channel(mode, 114, op_class->bw) != NOT_ALLOWED;
299ebb5671cSchristos }
300ebb5671cSchristos
301ebb5671cSchristos if (op_class->op_class == 130) {
302ebb5671cSchristos /* Need at least two non-contiguous 80 MHz segments */
303ebb5671cSchristos found = 0;
304ebb5671cSchristos
305ebb5671cSchristos if (verify_channel(mode, 42, op_class->bw) != NOT_ALLOWED ||
306ebb5671cSchristos verify_channel(mode, 58, op_class->bw) != NOT_ALLOWED)
307ebb5671cSchristos found++;
308ebb5671cSchristos if (verify_channel(mode, 106, op_class->bw) != NOT_ALLOWED ||
309ebb5671cSchristos verify_channel(mode, 122, op_class->bw) != NOT_ALLOWED ||
310ebb5671cSchristos verify_channel(mode, 138, op_class->bw) != NOT_ALLOWED)
311ebb5671cSchristos found++;
312ebb5671cSchristos if (verify_channel(mode, 106, op_class->bw) != NOT_ALLOWED &&
313ebb5671cSchristos verify_channel(mode, 138, op_class->bw) != NOT_ALLOWED)
314ebb5671cSchristos found++;
315ebb5671cSchristos if (verify_channel(mode, 155, op_class->bw) != NOT_ALLOWED)
316ebb5671cSchristos found++;
317ebb5671cSchristos
318ebb5671cSchristos if (found >= 2)
319ebb5671cSchristos return 1;
320ebb5671cSchristos
321ebb5671cSchristos return 0;
322ebb5671cSchristos }
323ebb5671cSchristos
324ebb5671cSchristos found = 0;
325ebb5671cSchristos for (chan = op_class->min_chan; chan <= op_class->max_chan;
326ebb5671cSchristos chan += op_class->inc) {
327ebb5671cSchristos if (verify_channel(mode, chan, op_class->bw) != NOT_ALLOWED) {
328ebb5671cSchristos found = 1;
329ebb5671cSchristos break;
330ebb5671cSchristos }
331ebb5671cSchristos }
332ebb5671cSchristos
333ebb5671cSchristos return found;
334ebb5671cSchristos }
335ebb5671cSchristos
336ebb5671cSchristos
wpas_supp_op_class_ie(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,int freq,u8 * pos,size_t len)337*0dddab58Schristos size_t wpas_supp_op_class_ie(struct wpa_supplicant *wpa_s,
338*0dddab58Schristos struct wpa_ssid *ssid,
339*0dddab58Schristos int freq, u8 *pos, size_t len)
340ebb5671cSchristos {
341ebb5671cSchristos struct wpabuf *buf;
342ebb5671cSchristos u8 op, current, chan;
343ebb5671cSchristos u8 *ie_len;
344ebb5671cSchristos size_t res;
345ebb5671cSchristos
346ebb5671cSchristos /*
347ebb5671cSchristos * Assume 20 MHz channel for now.
348ebb5671cSchristos * TODO: Use the secondary channel and VHT channel width that will be
349ebb5671cSchristos * used after association.
350ebb5671cSchristos */
351*0dddab58Schristos if (ieee80211_freq_to_channel_ext(freq, 0, CHANWIDTH_USE_HT,
352ebb5671cSchristos ¤t, &chan) == NUM_HOSTAPD_MODES)
353ebb5671cSchristos return 0;
354ebb5671cSchristos
355ebb5671cSchristos /*
356ebb5671cSchristos * Need 3 bytes for EID, length, and current operating class, plus
357ebb5671cSchristos * 1 byte for every other supported operating class.
358ebb5671cSchristos */
359ebb5671cSchristos buf = wpabuf_alloc(global_op_class_size + 3);
360ebb5671cSchristos if (!buf)
361ebb5671cSchristos return 0;
362ebb5671cSchristos
363ebb5671cSchristos wpabuf_put_u8(buf, WLAN_EID_SUPPORTED_OPERATING_CLASSES);
364ebb5671cSchristos /* Will set the length later, putting a placeholder */
365ebb5671cSchristos ie_len = wpabuf_put(buf, 1);
366ebb5671cSchristos wpabuf_put_u8(buf, current);
367ebb5671cSchristos
368ebb5671cSchristos for (op = 0; global_op_class[op].op_class; op++) {
369*0dddab58Schristos if (wpas_op_class_supported(wpa_s, ssid, &global_op_class[op]))
370ebb5671cSchristos wpabuf_put_u8(buf, global_op_class[op].op_class);
371ebb5671cSchristos }
372ebb5671cSchristos
373ebb5671cSchristos *ie_len = wpabuf_len(buf) - 2;
374ad6e66b0Sroy if (*ie_len < 2) {
375ad6e66b0Sroy wpa_printf(MSG_DEBUG,
376ad6e66b0Sroy "No supported operating classes IE to add");
377ad6e66b0Sroy res = 0;
378ad6e66b0Sroy } else if (wpabuf_len(buf) > len) {
379ebb5671cSchristos wpa_printf(MSG_ERROR,
380ad6e66b0Sroy "Supported operating classes IE exceed length");
381ebb5671cSchristos res = 0;
382ebb5671cSchristos } else {
383ebb5671cSchristos os_memcpy(pos, wpabuf_head(buf), wpabuf_len(buf));
384ebb5671cSchristos res = wpabuf_len(buf);
385ebb5671cSchristos wpa_hexdump_buf(MSG_DEBUG,
386ebb5671cSchristos "Added supported operating classes IE", buf);
387ebb5671cSchristos }
388ebb5671cSchristos
389ebb5671cSchristos wpabuf_free(buf);
390ebb5671cSchristos return res;
391ebb5671cSchristos }
392