1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2008 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 /*
30  * IEEE 802.11 regdomain support.
31  */
32 #include "opt_wlan.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/if_media.h>
43 #include <net/ethernet.h>
44 
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_regdomain.h>
47 
48 static void
49 null_getradiocaps(struct ieee80211com *ic, int maxchan,
50 	int *n, struct ieee80211_channel *c)
51 {
52 	/* just feed back the current channel list */
53 	if (maxchan > ic->ic_nchans)
54 		maxchan = ic->ic_nchans;
55 	memcpy(c, ic->ic_channels, maxchan*sizeof(struct ieee80211_channel));
56 	*n = maxchan;
57 }
58 
59 static int
60 null_setregdomain(struct ieee80211com *ic,
61 	struct ieee80211_regdomain *rd,
62 	int nchans, struct ieee80211_channel chans[])
63 {
64 	return 0;		/* accept anything */
65 }
66 
67 void
68 ieee80211_regdomain_attach(struct ieee80211com *ic)
69 {
70 	if (ic->ic_regdomain.regdomain == 0 &&
71 	    ic->ic_regdomain.country == CTRY_DEFAULT) {
72 		ic->ic_regdomain.location = ' ';		/* both */
73 		/* NB: driver calls ieee80211_init_channels or similar */
74 	}
75 	ic->ic_getradiocaps = null_getradiocaps;
76 	ic->ic_setregdomain = null_setregdomain;
77 }
78 
79 void
80 ieee80211_regdomain_detach(struct ieee80211com *ic)
81 {
82 	if (ic->ic_countryie != NULL) {
83 		IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
84 		ic->ic_countryie = NULL;
85 	}
86 }
87 
88 void
89 ieee80211_regdomain_vattach(struct ieee80211vap *vap)
90 {
91 }
92 
93 void
94 ieee80211_regdomain_vdetach(struct ieee80211vap *vap)
95 {
96 }
97 
98 static const uint8_t def_chan_2ghz[] =
99     { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
100 static const uint8_t def_chan_5ghz_band1[] =
101     { 36, 40, 44, 48, 52, 56, 60, 64 };
102 static const uint8_t def_chan_5ghz_band2[] =
103     { 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140 };
104 static const uint8_t def_chan_5ghz_band3[] =
105     { 149, 153, 157, 161 };
106 
107 /*
108  * Setup the channel list for the specified regulatory domain,
109  * country code, and operating modes.  This interface is used
110  * when a driver does not obtain the channel list from another
111  * source (such as firmware).
112  */
113 int
114 ieee80211_init_channels(struct ieee80211com *ic,
115 	const struct ieee80211_regdomain *rd, const uint8_t bands[])
116 {
117 	struct ieee80211_channel *chans = ic->ic_channels;
118 	int *nchans = &ic->ic_nchans;
119 	int cbw_flags;
120 
121 	/* XXX just do something for now */
122 	cbw_flags = (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) ?
123 	    NET80211_CBW_FLAG_HT40 : 0;
124 	*nchans = 0;
125 	if (isset(bands, IEEE80211_MODE_11B) ||
126 	    isset(bands, IEEE80211_MODE_11G) ||
127 	    isset(bands, IEEE80211_MODE_11NG)) {
128 		int nchan = nitems(def_chan_2ghz);
129 		if (!(rd != NULL && rd->ecm))
130 			nchan -= 3;
131 
132 		ieee80211_add_channel_list_2ghz(chans, IEEE80211_CHAN_MAX,
133 		    nchans, def_chan_2ghz, nchan, bands, cbw_flags);
134 	}
135 	/* XXX IEEE80211_MODE_VHT_2GHZ if we really want to. */
136 
137 	if (isset(bands, IEEE80211_MODE_11A) ||
138 	    isset(bands, IEEE80211_MODE_11NA)) {
139 		ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
140 		    nchans, def_chan_5ghz_band1, nitems(def_chan_5ghz_band1),
141 		    bands, cbw_flags);
142 		ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
143 		    nchans, def_chan_5ghz_band2, nitems(def_chan_5ghz_band2),
144 		    bands, cbw_flags);
145 		ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
146 		    nchans, def_chan_5ghz_band3, nitems(def_chan_5ghz_band3),
147 		    bands, cbw_flags);
148 	}
149 	if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
150 		cbw_flags |= NET80211_CBW_FLAG_HT40;  /* Make sure this is set; or assert?  */
151 		cbw_flags |= NET80211_CBW_FLAG_VHT80;
152 		if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(ic->ic_vht_cap.vht_cap_info))
153 			cbw_flags |= NET80211_CBW_FLAG_VHT160;
154 		if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
155 		    ic->ic_vht_cap.vht_cap_info))
156 			cbw_flags |= NET80211_CBW_FLAG_VHT80P80;
157 		ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
158 		    nchans, def_chan_5ghz_band1, nitems(def_chan_5ghz_band1),
159 		    bands, cbw_flags);
160 		ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
161 		    nchans, def_chan_5ghz_band2, nitems(def_chan_5ghz_band2),
162 		    bands, cbw_flags);
163 		ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
164 		    nchans, def_chan_5ghz_band3, nitems(def_chan_5ghz_band3),
165 		    bands, cbw_flags);
166 	}
167 	if (rd != NULL)
168 		ic->ic_regdomain = *rd;
169 
170 	return 0;
171 }
172 
173 static __inline int
174 chancompar(const void *a, const void *b)
175 {
176 	const struct ieee80211_channel *ca = a;
177 	const struct ieee80211_channel *cb = b;
178 
179 	return (ca->ic_freq == cb->ic_freq) ?
180 		(ca->ic_flags & IEEE80211_CHAN_ALL) -
181 		    (cb->ic_flags & IEEE80211_CHAN_ALL) :
182 		ca->ic_freq - cb->ic_freq;
183 }
184 
185 /*
186  * Insertion sort.
187  */
188 #define swap(_a, _b, _size) {			\
189 	uint8_t *s = _b;			\
190 	int i = _size;				\
191 	do {					\
192 		uint8_t tmp = *_a;		\
193 		*_a++ = *s;			\
194 		*s++ = tmp;			\
195 	} while (--i);				\
196 	_a -= _size;				\
197 }
198 
199 static void
200 sort_channels(void *a, size_t n, size_t size)
201 {
202 	uint8_t *aa = a;
203 	uint8_t *ai, *t;
204 
205 	KASSERT(n > 0, ("no channels"));
206 	for (ai = aa+size; --n >= 1; ai += size)
207 		for (t = ai; t > aa; t -= size) {
208 			uint8_t *u = t - size;
209 			if (chancompar(u, t) <= 0)
210 				break;
211 			swap(u, t, size);
212 		}
213 }
214 #undef swap
215 
216 /*
217  * Order channels w/ the same frequency so that
218  * b < g < htg and a < hta.  This is used to optimize
219  * channel table lookups and some user applications
220  * may also depend on it (though they should not).
221  */
222 void
223 ieee80211_sort_channels(struct ieee80211_channel chans[], int nchans)
224 {
225 	if (nchans > 0)
226 		sort_channels(chans, nchans, sizeof(struct ieee80211_channel));
227 }
228 
229 /*
230  * Allocate and construct a Country Information IE.
231  */
232 struct ieee80211_appie *
233 ieee80211_alloc_countryie(struct ieee80211com *ic)
234 {
235 #define	CHAN_UNINTERESTING \
236     (IEEE80211_CHAN_TURBO | IEEE80211_CHAN_STURBO | \
237      IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)
238 	/* XXX what about auto? */
239 	/* flag set of channels to be excluded (band added below) */
240 	static const int skipflags[IEEE80211_MODE_MAX] = {
241 	    [IEEE80211_MODE_AUTO]	= CHAN_UNINTERESTING,
242 	    [IEEE80211_MODE_11A]	= CHAN_UNINTERESTING,
243 	    [IEEE80211_MODE_11B]	= CHAN_UNINTERESTING,
244 	    [IEEE80211_MODE_11G]	= CHAN_UNINTERESTING,
245 	    [IEEE80211_MODE_FH]		= CHAN_UNINTERESTING
246 					| IEEE80211_CHAN_OFDM
247 					| IEEE80211_CHAN_CCK
248 					| IEEE80211_CHAN_DYN,
249 	    [IEEE80211_MODE_TURBO_A]	= CHAN_UNINTERESTING,
250 	    [IEEE80211_MODE_TURBO_G]	= CHAN_UNINTERESTING,
251 	    [IEEE80211_MODE_STURBO_A]	= CHAN_UNINTERESTING,
252 	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_TURBO
253 					| IEEE80211_CHAN_STURBO,
254 	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_TURBO
255 					| IEEE80211_CHAN_STURBO,
256 	    [IEEE80211_MODE_11NA]	= CHAN_UNINTERESTING,
257 	    [IEEE80211_MODE_11NG]	= CHAN_UNINTERESTING,
258 	};
259 	const struct ieee80211_regdomain *rd = &ic->ic_regdomain;
260 	uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm;
261 	struct ieee80211_appie *aie;
262 	struct ieee80211_country_ie *ie;
263 	int i, skip, nruns;
264 
265 	aie = IEEE80211_MALLOC(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
266 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
267 	if (aie == NULL) {
268 		ic_printf(ic, "%s: unable to allocate memory for country ie\n",
269 		    __func__);
270 		/* XXX stat */
271 		return NULL;
272 	}
273 	ie = (struct ieee80211_country_ie *) aie->ie_data;
274 	ie->ie = IEEE80211_ELEMID_COUNTRY;
275 	if (rd->isocc[0] == '\0') {
276 		ic_printf(ic, "no ISO country string for cc %d; using blanks\n",
277 		    rd->country);
278 		ie->cc[0] = ie->cc[1] = ' ';
279 	} else {
280 		ie->cc[0] = rd->isocc[0];
281 		ie->cc[1] = rd->isocc[1];
282 	}
283 	/*
284 	 * Indoor/Outdoor portion of country string:
285 	 *     'I' indoor only
286 	 *     'O' outdoor only
287 	 *     ' ' all environments
288 	 */
289 	ie->cc[2] = (rd->location == 'I' ? 'I' :
290 		     rd->location == 'O' ? 'O' : ' ');
291 	/*
292 	 * Run-length encoded channel+max tx power info.
293 	 */
294 	frm = (uint8_t *)&ie->band[0];
295 	nextchan = 0;			/* NB: impossible channel # */
296 	nruns = 0;
297 	memset(chans, 0, sizeof(chans));
298 	skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)];
299 	if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
300 		skip |= IEEE80211_CHAN_2GHZ;
301 	else if (IEEE80211_IS_CHAN_2GHZ(ic->ic_bsschan))
302 		skip |= IEEE80211_CHAN_5GHZ;
303 	for (i = 0; i < ic->ic_nchans; i++) {
304 		const struct ieee80211_channel *c = &ic->ic_channels[i];
305 
306 		if (isset(chans, c->ic_ieee))		/* suppress dup's */
307 			continue;
308 		if (c->ic_flags & skip)			/* skip band, etc. */
309 			continue;
310 		setbit(chans, c->ic_ieee);
311 		if (c->ic_ieee != nextchan ||
312 		    c->ic_maxregpower != frm[-1]) {	/* new run */
313 			if (nruns == IEEE80211_COUNTRY_MAX_BANDS) {
314 				ic_printf(ic, "%s: country ie too big, "
315 				    "runs > max %d, truncating\n",
316 				    __func__, IEEE80211_COUNTRY_MAX_BANDS);
317 				/* XXX stat? fail? */
318 				break;
319 			}
320 			frm[0] = c->ic_ieee;		/* starting channel # */
321 			frm[1] = 1;			/* # channels in run */
322 			frm[2] = c->ic_maxregpower;	/* tx power cap */
323 			frm += 3;
324 			nextchan = c->ic_ieee + 1;	/* overflow? */
325 			nruns++;
326 		} else {				/* extend run */
327 			frm[-2]++;
328 			nextchan++;
329 		}
330 	}
331 	ie->len = frm - ie->cc;
332 	if (ie->len & 1) {		/* Zero pad to multiple of 2 */
333 		ie->len++;
334 		*frm++ = 0;
335 	}
336 	aie->ie_len = frm - aie->ie_data;
337 
338 	return aie;
339 #undef CHAN_UNINTERESTING
340 }
341 
342 static int
343 allvapsdown(struct ieee80211com *ic)
344 {
345 	struct ieee80211vap *vap;
346 
347 	IEEE80211_LOCK_ASSERT(ic);
348 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
349 		if (vap->iv_state != IEEE80211_S_INIT)
350 			return 0;
351 	return 1;
352 }
353 
354 int
355 ieee80211_setregdomain(struct ieee80211vap *vap,
356     struct ieee80211_regdomain_req *reg)
357 {
358 	struct ieee80211com *ic = vap->iv_ic;
359 	struct ieee80211_channel *c;
360 	int desfreq = 0, desflags = 0;		/* XXX silence gcc complaint */
361 	int error, i;
362 
363 	if (reg->rd.location != 'I' && reg->rd.location != 'O' &&
364 	    reg->rd.location != ' ') {
365 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
366 		    "%s: invalid location 0x%x\n", __func__, reg->rd.location);
367 		return EINVAL;
368 	}
369 	if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0') {
370 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
371 		    "%s: invalid iso cc 0x%x:0x%x\n", __func__,
372 		    reg->rd.isocc[0], reg->rd.isocc[1]);
373 		return EINVAL;
374 	}
375 	if (reg->chaninfo.ic_nchans > IEEE80211_CHAN_MAX) {
376 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
377 		    "%s: too many channels %u, max %u\n", __func__,
378 		    reg->chaninfo.ic_nchans, IEEE80211_CHAN_MAX);
379 		return EINVAL;
380 	}
381 	/*
382 	 * Calculate freq<->IEEE mapping and default max tx power
383 	 * for channels not setup.  The driver can override these
384 	 * setting to reflect device properties/requirements.
385 	 */
386 	for (i = 0; i < reg->chaninfo.ic_nchans; i++) {
387 		c = &reg->chaninfo.ic_chans[i];
388 		if (c->ic_freq == 0 || c->ic_flags == 0) {
389 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
390 			    "%s: invalid channel spec at [%u]\n", __func__, i);
391 			return EINVAL;
392 		}
393 		if (c->ic_maxregpower == 0) {
394 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
395 			    "%s: invalid channel spec, zero maxregpower, "
396 			    "freq %u flags 0x%x\n", __func__,
397 			    c->ic_freq, c->ic_flags);
398 			return EINVAL;
399 		}
400 		if (c->ic_ieee == 0)
401 			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
402 		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
403 			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
404 			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
405 			    c->ic_flags);
406 		if (c->ic_maxpower == 0)
407 			c->ic_maxpower = 2*c->ic_maxregpower;
408 	}
409 	IEEE80211_LOCK(ic);
410 	/* XXX bandaid; a running vap will likely crash */
411 	if (!allvapsdown(ic)) {
412 		IEEE80211_UNLOCK(ic);
413 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
414 		    "%s: reject: vaps are running\n", __func__);
415 		return EBUSY;
416 	}
417 	error = ic->ic_setregdomain(ic, &reg->rd,
418 	    reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans);
419 	if (error != 0) {
420 		IEEE80211_UNLOCK(ic);
421 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
422 		    "%s: driver rejected request, error %u\n", __func__, error);
423 		return error;
424 	}
425 	/*
426 	 * Commit: copy in new channel table and reset media state.
427 	 * On return the state machines will be clocked so all vaps
428 	 * will reset their state.
429 	 *
430 	 * XXX ic_bsschan is marked undefined, must have vap's in
431 	 *     INIT state or we blow up forcing stations off
432 	 */
433 	/*
434 	 * Save any desired channel for restore below.  Note this
435 	 * needs to be done for all vaps but for now we only do
436 	 * the one where the ioctl is issued.
437 	 */
438 	if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
439 		desfreq = vap->iv_des_chan->ic_freq;
440 		desflags = vap->iv_des_chan->ic_flags;
441 	}
442 	/* regdomain parameters */
443 	memcpy(&ic->ic_regdomain, &reg->rd, sizeof(reg->rd));
444 	/* channel table */
445 	memcpy(ic->ic_channels, reg->chaninfo.ic_chans,
446 	    reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel));
447 	ic->ic_nchans = reg->chaninfo.ic_nchans;
448 	memset(&ic->ic_channels[ic->ic_nchans], 0,
449 	    (IEEE80211_CHAN_MAX - ic->ic_nchans) *
450 	       sizeof(struct ieee80211_channel));
451 	ieee80211_chan_init(ic);
452 
453 	/*
454 	 * Invalidate channel-related state.
455 	 */
456 	if (ic->ic_countryie != NULL) {
457 		IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
458 		ic->ic_countryie = NULL;
459 	}
460 	ieee80211_scan_flush(vap);
461 	ieee80211_dfs_reset(ic);
462 	if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
463 		c = ieee80211_find_channel(ic, desfreq, desflags);
464 		/* NB: may be NULL if not present in new channel list */
465 		vap->iv_des_chan = (c != NULL) ? c : IEEE80211_CHAN_ANYC;
466 	}
467 	IEEE80211_UNLOCK(ic);
468 
469 	return 0;
470 }
471