2 * Copyright (c) 2005-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * IEEE 802.11 regdomain support.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
41 #include <net/if_media.h>
43 #include <net80211/ieee80211_var.h>
44 #include <net80211/ieee80211_regdomain.h>
47 null_getradiocaps(struct ieee80211com *ic, int *n, struct ieee80211_channel *c)
49 /* just feed back the current channel list */
51 memcpy(c, ic->ic_channels,
52 ic->ic_nchans*sizeof(struct ieee80211_channel));
56 null_setregdomain(struct ieee80211com *ic,
57 struct ieee80211_regdomain *rd,
58 int nchans, struct ieee80211_channel chans[])
60 return 0; /* accept anything */
64 ieee80211_regdomain_attach(struct ieee80211com *ic)
66 if (ic->ic_regdomain.regdomain == 0 &&
67 ic->ic_regdomain.country == CTRY_DEFAULT) {
68 ic->ic_regdomain.country = CTRY_UNITED_STATES; /* XXX */
69 ic->ic_regdomain.location = ' '; /* both */
70 ic->ic_regdomain.isocc[0] = 'U'; /* XXX */
71 ic->ic_regdomain.isocc[1] = 'S'; /* XXX */
72 /* XXX? too late to setup default channel list */
74 ic->ic_getradiocaps = null_getradiocaps;
75 ic->ic_setregdomain = null_setregdomain;
79 ieee80211_regdomain_detach(struct ieee80211com *ic)
81 if (ic->ic_countryie != NULL) {
82 free(ic->ic_countryie, M_80211_NODE_IE);
83 ic->ic_countryie = NULL;
88 ieee80211_regdomain_vattach(struct ieee80211vap *vap)
93 ieee80211_regdomain_vdetach(struct ieee80211vap *vap)
98 addchan(struct ieee80211com *ic, int ieee, int flags)
100 struct ieee80211_channel *c;
102 c = &ic->ic_channels[ic->ic_nchans++];
103 c->ic_freq = ieee80211_ieee2mhz(ieee, flags);
110 * Setup the channel list for the specified regulatory domain,
111 * country code, and operating modes. This interface is used
112 * when a driver does not obtain the channel list from another
113 * source (such as firmware).
116 ieee80211_init_channels(struct ieee80211com *ic,
117 const struct ieee80211_regdomain *rd, const uint8_t bands[])
121 /* XXX just do something for now */
123 if (isset(bands, IEEE80211_MODE_11B) ||
124 isset(bands, IEEE80211_MODE_11G)) {
126 if (rd != NULL && rd->ecm)
128 for (i = 1; i <= maxchan; i++) {
129 if (isset(bands, IEEE80211_MODE_11B))
130 addchan(ic, i, IEEE80211_CHAN_B);
131 if (isset(bands, IEEE80211_MODE_11G))
132 addchan(ic, i, IEEE80211_CHAN_G);
135 if (isset(bands, IEEE80211_MODE_11A)) {
136 for (i = 36; i <= 64; i += 4)
137 addchan(ic, i, IEEE80211_CHAN_A);
138 for (i = 100; i <= 140; i += 4)
139 addchan(ic, i, IEEE80211_CHAN_A);
140 for (i = 149; i <= 161; i += 4)
141 addchan(ic, i, IEEE80211_CHAN_A);
144 ic->ic_regdomain = *rd;
150 chancompar(const void *a, const void *b)
152 const struct ieee80211_channel *ca = a;
153 const struct ieee80211_channel *cb = b;
155 return (ca->ic_freq == cb->ic_freq) ?
156 (ca->ic_flags & IEEE80211_CHAN_ALL) -
157 (cb->ic_flags & IEEE80211_CHAN_ALL) :
158 ca->ic_freq - cb->ic_freq;
164 #define swap(_a, _b, _size) { \
176 sort_channels(void *a, size_t n, size_t size)
181 KASSERT(n > 0, ("no channels"));
182 for (ai = aa+size; --n >= 1; ai += size)
183 for (t = ai; t > aa; t -= size) {
184 uint8_t *u = t - size;
185 if (chancompar(u, t) <= 0)
193 * Order channels w/ the same frequency so that
194 * b < g < htg and a < hta. This is used to optimize
195 * channel table lookups and some user applications
196 * may also depend on it (though they should not).
199 ieee80211_sort_channels(struct ieee80211_channel chans[], int nchans)
202 sort_channels(chans, nchans, sizeof(struct ieee80211_channel));
206 * Allocate and construct a Country Information IE.
208 struct ieee80211_appie *
209 ieee80211_alloc_countryie(struct ieee80211com *ic)
211 #define CHAN_UNINTERESTING \
212 (IEEE80211_CHAN_TURBO | IEEE80211_CHAN_STURBO | \
213 IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)
214 /* XXX what about auto? */
215 /* flag set of channels to be excluded */
216 static const int skipflags[IEEE80211_MODE_MAX] = {
217 CHAN_UNINTERESTING, /* MODE_AUTO */
218 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_11A */
219 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11B */
220 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11G */
221 CHAN_UNINTERESTING | IEEE80211_CHAN_OFDM | /* MODE_FH */
222 IEEE80211_CHAN_CCK | IEEE80211_CHAN_DYN,
223 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_TURBO_A */
224 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_TURBO_G */
225 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_STURBO_A */
226 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_11NA */
227 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11NG */
229 const struct ieee80211_regdomain *rd = &ic->ic_regdomain;
230 uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm;
231 struct ieee80211_appie *aie;
232 struct ieee80211_country_ie *ie;
235 aie = malloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
238 if_printf(ic->ic_ifp,
239 "%s: unable to allocate memory for country ie\n", __func__);
243 ie = (struct ieee80211_country_ie *) aie->ie_data;
244 ie->ie = IEEE80211_ELEMID_COUNTRY;
245 if (rd->isocc[0] == '\0') {
246 if_printf(ic->ic_ifp, "no ISO country string for cc %d; "
247 "using blanks\n", rd->country);
248 ie->cc[0] = ie->cc[1] = ' ';
250 ie->cc[0] = rd->isocc[0];
251 ie->cc[1] = rd->isocc[1];
254 * Indoor/Outdoor portion of country string:
257 * ' ' all enviroments
259 ie->cc[2] = (rd->location == 'I' ? 'I' :
260 rd->location == 'O' ? 'O' : ' ');
262 * Run-length encoded channel+max tx power info.
264 frm = (uint8_t *)&ie->band[0];
265 nextchan = 0; /* NB: impossible channel # */
267 memset(chans, 0, sizeof(chans));
268 skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)];
269 for (i = 0; i < ic->ic_nchans; i++) {
270 const struct ieee80211_channel *c = &ic->ic_channels[i];
272 if (isset(chans, c->ic_ieee)) /* suppress dup's */
274 if (c->ic_flags & skip) /* skip band, etc. */
276 setbit(chans, c->ic_ieee);
277 if (c->ic_ieee != nextchan ||
278 c->ic_maxregpower != frm[-1]) { /* new run */
279 if (nruns == IEEE80211_COUNTRY_MAX_BANDS) {
280 if_printf(ic->ic_ifp, "%s: country ie too big, "
281 "runs > max %d, truncating\n",
282 __func__, IEEE80211_COUNTRY_MAX_BANDS);
283 /* XXX stat? fail? */
286 frm[0] = c->ic_ieee; /* starting channel # */
287 frm[1] = 1; /* # channels in run */
288 frm[2] = c->ic_maxregpower; /* tx power cap */
290 nextchan = c->ic_ieee + 1; /* overflow? */
292 } else { /* extend run */
297 ie->len = frm - ie->cc;
298 if (ie->len & 1) { /* Zero pad to multiple of 2 */
302 aie->ie_len = frm - aie->ie_data;
305 #undef CHAN_UNINTERESTING
309 allvapsdown(struct ieee80211com *ic)
311 struct ieee80211vap *vap;
313 IEEE80211_LOCK_ASSERT(ic);
314 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
315 if (vap->iv_state != IEEE80211_S_INIT)
321 ieee80211_setregdomain(struct ieee80211vap *vap,
322 struct ieee80211_regdomain_req *reg)
324 struct ieee80211com *ic = vap->iv_ic;
325 struct ieee80211_channel *c;
326 int desfreq = 0, desflags = 0; /* XXX silence gcc complaint */
329 if (reg->rd.location != 'I' && reg->rd.location != 'O' &&
330 reg->rd.location != ' ')
332 if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0')
334 if (reg->chaninfo.ic_nchans >= IEEE80211_CHAN_MAX)
337 * Calculate freq<->IEEE mapping and default max tx power
338 * for channels not setup. The driver can override these
339 * setting to reflect device properties/requirements.
341 for (i = 0; i < reg->chaninfo.ic_nchans; i++) {
342 c = ®->chaninfo.ic_chans[i];
343 if (c->ic_freq == 0 || c->ic_flags == 0)
345 if (c->ic_maxregpower == 0)
348 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
349 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
350 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
351 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
353 if (c->ic_maxpower == 0)
354 c->ic_maxpower = 2*c->ic_maxregpower;
357 error = ic->ic_setregdomain(ic, ®->rd,
358 reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans);
360 IEEE80211_UNLOCK(ic);
363 /* XXX bandaid; a running vap will likely crash */
364 if (!allvapsdown(ic)) {
365 IEEE80211_UNLOCK(ic);
369 * Commit: copy in new channel table and reset media state.
370 * On return the state machines will be clocked so all vaps
371 * will reset their state.
373 * XXX ic_bsschan is marked undefined, must have vap's in
374 * INIT state or we blow up forcing stations off
377 * Save any desired channel for restore below. Note this
378 * needs to be done for all vaps but for now we only do
379 * the one where the ioctl is issued.
381 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
382 desfreq = vap->iv_des_chan->ic_freq;
383 desflags = vap->iv_des_chan->ic_flags;
385 /* regdomain parameters */
386 memcpy(&ic->ic_regdomain, ®->rd, sizeof(reg->rd));
388 memcpy(ic->ic_channels, reg->chaninfo.ic_chans,
389 reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel));
390 ic->ic_nchans = reg->chaninfo.ic_nchans;
391 memset(&ic->ic_channels[ic->ic_nchans], 0,
392 (IEEE80211_CHAN_MAX - ic->ic_nchans) *
393 sizeof(struct ieee80211_channel));
394 ieee80211_media_init(ic);
397 * Invalidate channel-related state.
399 if (ic->ic_countryie != NULL) {
400 free(ic->ic_countryie, M_80211_NODE_IE);
401 ic->ic_countryie = NULL;
403 ieee80211_scan_flush(vap);
404 ieee80211_dfs_reset(ic);
405 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
406 /* NB: may be NULL if not present in new channel list */
407 vap->iv_des_chan = ieee80211_find_channel(ic, desfreq, desflags);
409 IEEE80211_UNLOCK(ic);