132176cfdSRui Paulo /*-
232176cfdSRui Paulo * Copyright (c) 2005-2008 Sam Leffler, Errno Consulting
332176cfdSRui Paulo * All rights reserved.
432176cfdSRui Paulo *
532176cfdSRui Paulo * Redistribution and use in source and binary forms, with or without
632176cfdSRui Paulo * modification, are permitted provided that the following conditions
732176cfdSRui Paulo * are met:
832176cfdSRui Paulo * 1. Redistributions of source code must retain the above copyright
932176cfdSRui Paulo * notice, this list of conditions and the following disclaimer.
1032176cfdSRui Paulo * 2. Redistributions in binary form must reproduce the above copyright
1132176cfdSRui Paulo * notice, this list of conditions and the following disclaimer in the
1232176cfdSRui Paulo * documentation and/or other materials provided with the distribution.
1332176cfdSRui Paulo *
1432176cfdSRui Paulo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1532176cfdSRui Paulo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1632176cfdSRui Paulo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1732176cfdSRui Paulo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1832176cfdSRui Paulo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1932176cfdSRui Paulo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2032176cfdSRui Paulo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2132176cfdSRui Paulo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2232176cfdSRui Paulo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2332176cfdSRui Paulo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2432176cfdSRui Paulo */
2532176cfdSRui Paulo
26085ff963SMatthew Dillon #include <sys/cdefs.h>
27085ff963SMatthew Dillon __FBSDID("$FreeBSD$");
28085ff963SMatthew Dillon
2932176cfdSRui Paulo /*
3032176cfdSRui Paulo * IEEE 802.11 regdomain support.
3132176cfdSRui Paulo */
3232176cfdSRui Paulo #include "opt_wlan.h"
3332176cfdSRui Paulo
3432176cfdSRui Paulo #include <sys/param.h>
3532176cfdSRui Paulo #include <sys/systm.h>
3632176cfdSRui Paulo #include <sys/kernel.h>
37085ff963SMatthew Dillon #include <sys/malloc.h>
3832176cfdSRui Paulo #include <sys/socket.h>
3932176cfdSRui Paulo
4032176cfdSRui Paulo #include <net/if.h>
41085ff963SMatthew Dillon #include <net/if_var.h>
4232176cfdSRui Paulo #include <net/if_media.h>
43085ff963SMatthew Dillon #include <net/ethernet.h>
4432176cfdSRui Paulo
4532176cfdSRui Paulo #include <netproto/802_11/ieee80211_var.h>
4632176cfdSRui Paulo #include <netproto/802_11/ieee80211_regdomain.h>
4732176cfdSRui Paulo
4832176cfdSRui Paulo static void
null_getradiocaps(struct ieee80211com * ic,int maxchan,int * n,struct ieee80211_channel * c)4932176cfdSRui Paulo null_getradiocaps(struct ieee80211com *ic, int maxchan,
5032176cfdSRui Paulo int *n, struct ieee80211_channel *c)
5132176cfdSRui Paulo {
5232176cfdSRui Paulo /* just feed back the current channel list */
5332176cfdSRui Paulo if (maxchan > ic->ic_nchans)
5432176cfdSRui Paulo maxchan = ic->ic_nchans;
5532176cfdSRui Paulo memcpy(c, ic->ic_channels, maxchan*sizeof(struct ieee80211_channel));
5632176cfdSRui Paulo *n = maxchan;
5732176cfdSRui Paulo }
5832176cfdSRui Paulo
5932176cfdSRui Paulo static int
null_setregdomain(struct ieee80211com * ic,struct ieee80211_regdomain * rd,int nchans,struct ieee80211_channel chans[])6032176cfdSRui Paulo null_setregdomain(struct ieee80211com *ic,
6132176cfdSRui Paulo struct ieee80211_regdomain *rd,
6232176cfdSRui Paulo int nchans, struct ieee80211_channel chans[])
6332176cfdSRui Paulo {
6432176cfdSRui Paulo return 0; /* accept anything */
6532176cfdSRui Paulo }
6632176cfdSRui Paulo
6732176cfdSRui Paulo void
ieee80211_regdomain_attach(struct ieee80211com * ic)6832176cfdSRui Paulo ieee80211_regdomain_attach(struct ieee80211com *ic)
6932176cfdSRui Paulo {
7032176cfdSRui Paulo if (ic->ic_regdomain.regdomain == 0 &&
7132176cfdSRui Paulo ic->ic_regdomain.country == CTRY_DEFAULT) {
7232176cfdSRui Paulo ic->ic_regdomain.country = CTRY_UNITED_STATES; /* XXX */
7332176cfdSRui Paulo ic->ic_regdomain.location = ' '; /* both */
7432176cfdSRui Paulo ic->ic_regdomain.isocc[0] = 'U'; /* XXX */
7532176cfdSRui Paulo ic->ic_regdomain.isocc[1] = 'S'; /* XXX */
7632176cfdSRui Paulo /* NB: driver calls ieee80211_init_channels or similar */
7732176cfdSRui Paulo }
7832176cfdSRui Paulo ic->ic_getradiocaps = null_getradiocaps;
7932176cfdSRui Paulo ic->ic_setregdomain = null_setregdomain;
8032176cfdSRui Paulo }
8132176cfdSRui Paulo
8232176cfdSRui Paulo void
ieee80211_regdomain_detach(struct ieee80211com * ic)8332176cfdSRui Paulo ieee80211_regdomain_detach(struct ieee80211com *ic)
8432176cfdSRui Paulo {
8532176cfdSRui Paulo if (ic->ic_countryie != NULL) {
86*4f655ef5SMatthew Dillon IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
8732176cfdSRui Paulo ic->ic_countryie = NULL;
8832176cfdSRui Paulo }
8932176cfdSRui Paulo }
9032176cfdSRui Paulo
9132176cfdSRui Paulo void
ieee80211_regdomain_vattach(struct ieee80211vap * vap)9232176cfdSRui Paulo ieee80211_regdomain_vattach(struct ieee80211vap *vap)
9332176cfdSRui Paulo {
9432176cfdSRui Paulo }
9532176cfdSRui Paulo
9632176cfdSRui Paulo void
ieee80211_regdomain_vdetach(struct ieee80211vap * vap)9732176cfdSRui Paulo ieee80211_regdomain_vdetach(struct ieee80211vap *vap)
9832176cfdSRui Paulo {
9932176cfdSRui Paulo }
10032176cfdSRui Paulo
101*4f655ef5SMatthew Dillon static const uint8_t def_chan_2ghz[] =
102*4f655ef5SMatthew Dillon { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
103*4f655ef5SMatthew Dillon static const uint8_t def_chan_5ghz_band1[] =
104*4f655ef5SMatthew Dillon { 36, 40, 44, 48, 52, 56, 60, 64 };
105*4f655ef5SMatthew Dillon static const uint8_t def_chan_5ghz_band2[] =
106*4f655ef5SMatthew Dillon { 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140 };
107*4f655ef5SMatthew Dillon static const uint8_t def_chan_5ghz_band3[] =
108*4f655ef5SMatthew Dillon { 149, 153, 157, 161 };
10932176cfdSRui Paulo
11032176cfdSRui Paulo /*
11132176cfdSRui Paulo * Setup the channel list for the specified regulatory domain,
11232176cfdSRui Paulo * country code, and operating modes. This interface is used
11332176cfdSRui Paulo * when a driver does not obtain the channel list from another
11432176cfdSRui Paulo * source (such as firmware).
11532176cfdSRui Paulo */
11632176cfdSRui Paulo int
ieee80211_init_channels(struct ieee80211com * ic,const struct ieee80211_regdomain * rd,const uint8_t bands[])11732176cfdSRui Paulo ieee80211_init_channels(struct ieee80211com *ic,
11832176cfdSRui Paulo const struct ieee80211_regdomain *rd, const uint8_t bands[])
11932176cfdSRui Paulo {
120*4f655ef5SMatthew Dillon struct ieee80211_channel *chans = ic->ic_channels;
121*4f655ef5SMatthew Dillon int *nchans = &ic->ic_nchans;
122*4f655ef5SMatthew Dillon int ht40;
12332176cfdSRui Paulo
12432176cfdSRui Paulo /* XXX just do something for now */
125*4f655ef5SMatthew Dillon ht40 = !!(ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40);
126*4f655ef5SMatthew Dillon *nchans = 0;
12732176cfdSRui Paulo if (isset(bands, IEEE80211_MODE_11B) ||
128085ff963SMatthew Dillon isset(bands, IEEE80211_MODE_11G) ||
129085ff963SMatthew Dillon isset(bands, IEEE80211_MODE_11NG)) {
130*4f655ef5SMatthew Dillon int nchan = nitems(def_chan_2ghz);
131*4f655ef5SMatthew Dillon if (!(rd != NULL && rd->ecm))
132*4f655ef5SMatthew Dillon nchan -= 3;
133*4f655ef5SMatthew Dillon
134*4f655ef5SMatthew Dillon ieee80211_add_channel_list_2ghz(chans, IEEE80211_CHAN_MAX,
135*4f655ef5SMatthew Dillon nchans, def_chan_2ghz, nchan, bands, ht40);
136085ff963SMatthew Dillon }
137085ff963SMatthew Dillon if (isset(bands, IEEE80211_MODE_11A) ||
138085ff963SMatthew Dillon isset(bands, IEEE80211_MODE_11NA)) {
139*4f655ef5SMatthew Dillon ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
140*4f655ef5SMatthew Dillon nchans, def_chan_5ghz_band1, nitems(def_chan_5ghz_band1),
141*4f655ef5SMatthew Dillon bands, ht40);
142*4f655ef5SMatthew Dillon ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
143*4f655ef5SMatthew Dillon nchans, def_chan_5ghz_band2, nitems(def_chan_5ghz_band2),
144*4f655ef5SMatthew Dillon bands, ht40);
145*4f655ef5SMatthew Dillon ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
146*4f655ef5SMatthew Dillon nchans, def_chan_5ghz_band3, nitems(def_chan_5ghz_band3),
147*4f655ef5SMatthew Dillon bands, ht40);
14832176cfdSRui Paulo }
14932176cfdSRui Paulo if (rd != NULL)
15032176cfdSRui Paulo ic->ic_regdomain = *rd;
15132176cfdSRui Paulo
15232176cfdSRui Paulo return 0;
15332176cfdSRui Paulo }
15432176cfdSRui Paulo
15532176cfdSRui Paulo static __inline int
chancompar(const void * a,const void * b)15632176cfdSRui Paulo chancompar(const void *a, const void *b)
15732176cfdSRui Paulo {
15832176cfdSRui Paulo const struct ieee80211_channel *ca = a;
15932176cfdSRui Paulo const struct ieee80211_channel *cb = b;
16032176cfdSRui Paulo
16132176cfdSRui Paulo return (ca->ic_freq == cb->ic_freq) ?
16232176cfdSRui Paulo (ca->ic_flags & IEEE80211_CHAN_ALL) -
16332176cfdSRui Paulo (cb->ic_flags & IEEE80211_CHAN_ALL) :
16432176cfdSRui Paulo ca->ic_freq - cb->ic_freq;
16532176cfdSRui Paulo }
16632176cfdSRui Paulo
16732176cfdSRui Paulo /*
16832176cfdSRui Paulo * Insertion sort.
16932176cfdSRui Paulo */
17032176cfdSRui Paulo #define swap(_a, _b, _size) { \
17132176cfdSRui Paulo uint8_t *s = _b; \
17232176cfdSRui Paulo int i = _size; \
17332176cfdSRui Paulo do { \
17432176cfdSRui Paulo uint8_t tmp = *_a; \
17532176cfdSRui Paulo *_a++ = *s; \
17632176cfdSRui Paulo *s++ = tmp; \
17732176cfdSRui Paulo } while (--i); \
17832176cfdSRui Paulo _a -= _size; \
17932176cfdSRui Paulo }
18032176cfdSRui Paulo
18132176cfdSRui Paulo static void
sort_channels(void * a,size_t n,size_t size)18232176cfdSRui Paulo sort_channels(void *a, size_t n, size_t size)
18332176cfdSRui Paulo {
18432176cfdSRui Paulo uint8_t *aa = a;
18532176cfdSRui Paulo uint8_t *ai, *t;
18632176cfdSRui Paulo
18732176cfdSRui Paulo KASSERT(n > 0, ("no channels"));
18832176cfdSRui Paulo for (ai = aa+size; --n >= 1; ai += size)
18932176cfdSRui Paulo for (t = ai; t > aa; t -= size) {
19032176cfdSRui Paulo uint8_t *u = t - size;
19132176cfdSRui Paulo if (chancompar(u, t) <= 0)
19232176cfdSRui Paulo break;
19332176cfdSRui Paulo swap(u, t, size);
19432176cfdSRui Paulo }
19532176cfdSRui Paulo }
19632176cfdSRui Paulo #undef swap
19732176cfdSRui Paulo
19832176cfdSRui Paulo /*
19932176cfdSRui Paulo * Order channels w/ the same frequency so that
20032176cfdSRui Paulo * b < g < htg and a < hta. This is used to optimize
20132176cfdSRui Paulo * channel table lookups and some user applications
20232176cfdSRui Paulo * may also depend on it (though they should not).
20332176cfdSRui Paulo */
20432176cfdSRui Paulo void
ieee80211_sort_channels(struct ieee80211_channel chans[],int nchans)20532176cfdSRui Paulo ieee80211_sort_channels(struct ieee80211_channel chans[], int nchans)
20632176cfdSRui Paulo {
20732176cfdSRui Paulo if (nchans > 0)
20832176cfdSRui Paulo sort_channels(chans, nchans, sizeof(struct ieee80211_channel));
20932176cfdSRui Paulo }
21032176cfdSRui Paulo
21132176cfdSRui Paulo /*
21232176cfdSRui Paulo * Allocate and construct a Country Information IE.
21332176cfdSRui Paulo */
21432176cfdSRui Paulo struct ieee80211_appie *
ieee80211_alloc_countryie(struct ieee80211com * ic)21532176cfdSRui Paulo ieee80211_alloc_countryie(struct ieee80211com *ic)
21632176cfdSRui Paulo {
21732176cfdSRui Paulo #define CHAN_UNINTERESTING \
21832176cfdSRui Paulo (IEEE80211_CHAN_TURBO | IEEE80211_CHAN_STURBO | \
21932176cfdSRui Paulo IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)
22032176cfdSRui Paulo /* XXX what about auto? */
22132176cfdSRui Paulo /* flag set of channels to be excluded (band added below) */
22232176cfdSRui Paulo static const int skipflags[IEEE80211_MODE_MAX] = {
22332176cfdSRui Paulo [IEEE80211_MODE_AUTO] = CHAN_UNINTERESTING,
22432176cfdSRui Paulo [IEEE80211_MODE_11A] = CHAN_UNINTERESTING,
22532176cfdSRui Paulo [IEEE80211_MODE_11B] = CHAN_UNINTERESTING,
22632176cfdSRui Paulo [IEEE80211_MODE_11G] = CHAN_UNINTERESTING,
22732176cfdSRui Paulo [IEEE80211_MODE_FH] = CHAN_UNINTERESTING
22832176cfdSRui Paulo | IEEE80211_CHAN_OFDM
22932176cfdSRui Paulo | IEEE80211_CHAN_CCK
23032176cfdSRui Paulo | IEEE80211_CHAN_DYN,
23132176cfdSRui Paulo [IEEE80211_MODE_TURBO_A] = CHAN_UNINTERESTING,
23232176cfdSRui Paulo [IEEE80211_MODE_TURBO_G] = CHAN_UNINTERESTING,
23332176cfdSRui Paulo [IEEE80211_MODE_STURBO_A] = CHAN_UNINTERESTING,
23432176cfdSRui Paulo [IEEE80211_MODE_HALF] = IEEE80211_CHAN_TURBO
23532176cfdSRui Paulo | IEEE80211_CHAN_STURBO,
23632176cfdSRui Paulo [IEEE80211_MODE_QUARTER] = IEEE80211_CHAN_TURBO
23732176cfdSRui Paulo | IEEE80211_CHAN_STURBO,
23832176cfdSRui Paulo [IEEE80211_MODE_11NA] = CHAN_UNINTERESTING,
23932176cfdSRui Paulo [IEEE80211_MODE_11NG] = CHAN_UNINTERESTING,
24032176cfdSRui Paulo };
24132176cfdSRui Paulo const struct ieee80211_regdomain *rd = &ic->ic_regdomain;
24232176cfdSRui Paulo uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm;
24332176cfdSRui Paulo struct ieee80211_appie *aie;
24432176cfdSRui Paulo struct ieee80211_country_ie *ie;
24532176cfdSRui Paulo int i, skip, nruns;
24632176cfdSRui Paulo
247*4f655ef5SMatthew Dillon #if defined(__DragonFly__)
24832176cfdSRui Paulo aie = kmalloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
249fcaa651dSRui Paulo M_INTWAIT | M_ZERO);
250*4f655ef5SMatthew Dillon #else
251*4f655ef5SMatthew Dillon aie = IEEE80211_MALLOC(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
252*4f655ef5SMatthew Dillon IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
253*4f655ef5SMatthew Dillon #endif
254085ff963SMatthew Dillon if (aie == NULL) {
2554f898719SImre Vadász ic_printf(ic, "%s: unable to allocate memory for country ie\n",
2564f898719SImre Vadász __func__);
257085ff963SMatthew Dillon /* XXX stat */
258085ff963SMatthew Dillon return NULL;
259085ff963SMatthew Dillon }
26032176cfdSRui Paulo ie = (struct ieee80211_country_ie *) aie->ie_data;
26132176cfdSRui Paulo ie->ie = IEEE80211_ELEMID_COUNTRY;
26232176cfdSRui Paulo if (rd->isocc[0] == '\0') {
2634f898719SImre Vadász ic_printf(ic, "no ISO country string for cc %d; using blanks\n",
2644f898719SImre Vadász rd->country);
26532176cfdSRui Paulo ie->cc[0] = ie->cc[1] = ' ';
26632176cfdSRui Paulo } else {
26732176cfdSRui Paulo ie->cc[0] = rd->isocc[0];
26832176cfdSRui Paulo ie->cc[1] = rd->isocc[1];
26932176cfdSRui Paulo }
27032176cfdSRui Paulo /*
27132176cfdSRui Paulo * Indoor/Outdoor portion of country string:
27232176cfdSRui Paulo * 'I' indoor only
27332176cfdSRui Paulo * 'O' outdoor only
274*4f655ef5SMatthew Dillon * ' ' all environments
27532176cfdSRui Paulo */
27632176cfdSRui Paulo ie->cc[2] = (rd->location == 'I' ? 'I' :
27732176cfdSRui Paulo rd->location == 'O' ? 'O' : ' ');
27832176cfdSRui Paulo /*
27932176cfdSRui Paulo * Run-length encoded channel+max tx power info.
28032176cfdSRui Paulo */
28132176cfdSRui Paulo frm = (uint8_t *)&ie->band[0];
28232176cfdSRui Paulo nextchan = 0; /* NB: impossible channel # */
28332176cfdSRui Paulo nruns = 0;
28432176cfdSRui Paulo memset(chans, 0, sizeof(chans));
28532176cfdSRui Paulo skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)];
28632176cfdSRui Paulo if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
28732176cfdSRui Paulo skip |= IEEE80211_CHAN_2GHZ;
28832176cfdSRui Paulo else if (IEEE80211_IS_CHAN_2GHZ(ic->ic_bsschan))
28932176cfdSRui Paulo skip |= IEEE80211_CHAN_5GHZ;
29032176cfdSRui Paulo for (i = 0; i < ic->ic_nchans; i++) {
29132176cfdSRui Paulo const struct ieee80211_channel *c = &ic->ic_channels[i];
29232176cfdSRui Paulo
29332176cfdSRui Paulo if (isset(chans, c->ic_ieee)) /* suppress dup's */
29432176cfdSRui Paulo continue;
29532176cfdSRui Paulo if (c->ic_flags & skip) /* skip band, etc. */
29632176cfdSRui Paulo continue;
29732176cfdSRui Paulo setbit(chans, c->ic_ieee);
29832176cfdSRui Paulo if (c->ic_ieee != nextchan ||
29932176cfdSRui Paulo c->ic_maxregpower != frm[-1]) { /* new run */
30032176cfdSRui Paulo if (nruns == IEEE80211_COUNTRY_MAX_BANDS) {
3014f898719SImre Vadász ic_printf(ic, "%s: country ie too big, "
30232176cfdSRui Paulo "runs > max %d, truncating\n",
30332176cfdSRui Paulo __func__, IEEE80211_COUNTRY_MAX_BANDS);
30432176cfdSRui Paulo /* XXX stat? fail? */
30532176cfdSRui Paulo break;
30632176cfdSRui Paulo }
30732176cfdSRui Paulo frm[0] = c->ic_ieee; /* starting channel # */
30832176cfdSRui Paulo frm[1] = 1; /* # channels in run */
30932176cfdSRui Paulo frm[2] = c->ic_maxregpower; /* tx power cap */
31032176cfdSRui Paulo frm += 3;
31132176cfdSRui Paulo nextchan = c->ic_ieee + 1; /* overflow? */
31232176cfdSRui Paulo nruns++;
31332176cfdSRui Paulo } else { /* extend run */
31432176cfdSRui Paulo frm[-2]++;
31532176cfdSRui Paulo nextchan++;
31632176cfdSRui Paulo }
31732176cfdSRui Paulo }
31832176cfdSRui Paulo ie->len = frm - ie->cc;
31932176cfdSRui Paulo if (ie->len & 1) { /* Zero pad to multiple of 2 */
32032176cfdSRui Paulo ie->len++;
32132176cfdSRui Paulo *frm++ = 0;
32232176cfdSRui Paulo }
32332176cfdSRui Paulo aie->ie_len = frm - aie->ie_data;
32432176cfdSRui Paulo
32532176cfdSRui Paulo return aie;
32632176cfdSRui Paulo #undef CHAN_UNINTERESTING
32732176cfdSRui Paulo }
32832176cfdSRui Paulo
32932176cfdSRui Paulo static int
allvapsdown(struct ieee80211com * ic)33032176cfdSRui Paulo allvapsdown(struct ieee80211com *ic)
33132176cfdSRui Paulo {
33232176cfdSRui Paulo struct ieee80211vap *vap;
33332176cfdSRui Paulo
334085ff963SMatthew Dillon IEEE80211_LOCK_ASSERT(ic);
33532176cfdSRui Paulo TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
33632176cfdSRui Paulo if (vap->iv_state != IEEE80211_S_INIT)
33732176cfdSRui Paulo return 0;
33832176cfdSRui Paulo return 1;
33932176cfdSRui Paulo }
34032176cfdSRui Paulo
34132176cfdSRui Paulo int
ieee80211_setregdomain(struct ieee80211vap * vap,struct ieee80211_regdomain_req * reg)34232176cfdSRui Paulo ieee80211_setregdomain(struct ieee80211vap *vap,
34332176cfdSRui Paulo struct ieee80211_regdomain_req *reg)
34432176cfdSRui Paulo {
34532176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic;
34632176cfdSRui Paulo struct ieee80211_channel *c;
34732176cfdSRui Paulo int desfreq = 0, desflags = 0; /* XXX silence gcc complaint */
34832176cfdSRui Paulo int error, i;
34932176cfdSRui Paulo
35032176cfdSRui Paulo if (reg->rd.location != 'I' && reg->rd.location != 'O' &&
35132176cfdSRui Paulo reg->rd.location != ' ') {
35232176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
35332176cfdSRui Paulo "%s: invalid location 0x%x\n", __func__, reg->rd.location);
35432176cfdSRui Paulo return EINVAL;
35532176cfdSRui Paulo }
35632176cfdSRui Paulo if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0') {
35732176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
35832176cfdSRui Paulo "%s: invalid iso cc 0x%x:0x%x\n", __func__,
35932176cfdSRui Paulo reg->rd.isocc[0], reg->rd.isocc[1]);
36032176cfdSRui Paulo return EINVAL;
36132176cfdSRui Paulo }
36232176cfdSRui Paulo if (reg->chaninfo.ic_nchans > IEEE80211_CHAN_MAX) {
36332176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
36432176cfdSRui Paulo "%s: too many channels %u, max %u\n", __func__,
36532176cfdSRui Paulo reg->chaninfo.ic_nchans, IEEE80211_CHAN_MAX);
36632176cfdSRui Paulo return EINVAL;
36732176cfdSRui Paulo }
36832176cfdSRui Paulo /*
36932176cfdSRui Paulo * Calculate freq<->IEEE mapping and default max tx power
37032176cfdSRui Paulo * for channels not setup. The driver can override these
37132176cfdSRui Paulo * setting to reflect device properties/requirements.
37232176cfdSRui Paulo */
37332176cfdSRui Paulo for (i = 0; i < reg->chaninfo.ic_nchans; i++) {
37432176cfdSRui Paulo c = ®->chaninfo.ic_chans[i];
37532176cfdSRui Paulo if (c->ic_freq == 0 || c->ic_flags == 0) {
37632176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
37732176cfdSRui Paulo "%s: invalid channel spec at [%u]\n", __func__, i);
37832176cfdSRui Paulo return EINVAL;
37932176cfdSRui Paulo }
38032176cfdSRui Paulo if (c->ic_maxregpower == 0) {
38132176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
38232176cfdSRui Paulo "%s: invalid channel spec, zero maxregpower, "
38332176cfdSRui Paulo "freq %u flags 0x%x\n", __func__,
38432176cfdSRui Paulo c->ic_freq, c->ic_flags);
38532176cfdSRui Paulo return EINVAL;
38632176cfdSRui Paulo }
38732176cfdSRui Paulo if (c->ic_ieee == 0)
38832176cfdSRui Paulo c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
38932176cfdSRui Paulo if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
39032176cfdSRui Paulo c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
39132176cfdSRui Paulo (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
39232176cfdSRui Paulo c->ic_flags);
39332176cfdSRui Paulo if (c->ic_maxpower == 0)
39432176cfdSRui Paulo c->ic_maxpower = 2*c->ic_maxregpower;
39532176cfdSRui Paulo }
396085ff963SMatthew Dillon IEEE80211_LOCK(ic);
39732176cfdSRui Paulo /* XXX bandaid; a running vap will likely crash */
39832176cfdSRui Paulo if (!allvapsdown(ic)) {
399085ff963SMatthew Dillon IEEE80211_UNLOCK(ic);
40032176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
40132176cfdSRui Paulo "%s: reject: vaps are running\n", __func__);
40232176cfdSRui Paulo return EBUSY;
40332176cfdSRui Paulo }
40432176cfdSRui Paulo error = ic->ic_setregdomain(ic, ®->rd,
40532176cfdSRui Paulo reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans);
40632176cfdSRui Paulo if (error != 0) {
407085ff963SMatthew Dillon IEEE80211_UNLOCK(ic);
40832176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
40932176cfdSRui Paulo "%s: driver rejected request, error %u\n", __func__, error);
41032176cfdSRui Paulo return error;
41132176cfdSRui Paulo }
41232176cfdSRui Paulo /*
41332176cfdSRui Paulo * Commit: copy in new channel table and reset media state.
41432176cfdSRui Paulo * On return the state machines will be clocked so all vaps
41532176cfdSRui Paulo * will reset their state.
41632176cfdSRui Paulo *
41732176cfdSRui Paulo * XXX ic_bsschan is marked undefined, must have vap's in
41832176cfdSRui Paulo * INIT state or we blow up forcing stations off
41932176cfdSRui Paulo */
42032176cfdSRui Paulo /*
42132176cfdSRui Paulo * Save any desired channel for restore below. Note this
42232176cfdSRui Paulo * needs to be done for all vaps but for now we only do
42332176cfdSRui Paulo * the one where the ioctl is issued.
42432176cfdSRui Paulo */
42532176cfdSRui Paulo if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
42632176cfdSRui Paulo desfreq = vap->iv_des_chan->ic_freq;
42732176cfdSRui Paulo desflags = vap->iv_des_chan->ic_flags;
42832176cfdSRui Paulo }
42932176cfdSRui Paulo /* regdomain parameters */
43032176cfdSRui Paulo memcpy(&ic->ic_regdomain, ®->rd, sizeof(reg->rd));
43132176cfdSRui Paulo /* channel table */
43232176cfdSRui Paulo memcpy(ic->ic_channels, reg->chaninfo.ic_chans,
43332176cfdSRui Paulo reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel));
43432176cfdSRui Paulo ic->ic_nchans = reg->chaninfo.ic_nchans;
43532176cfdSRui Paulo memset(&ic->ic_channels[ic->ic_nchans], 0,
43632176cfdSRui Paulo (IEEE80211_CHAN_MAX - ic->ic_nchans) *
43732176cfdSRui Paulo sizeof(struct ieee80211_channel));
438*4f655ef5SMatthew Dillon ieee80211_chan_init(ic);
43932176cfdSRui Paulo
44032176cfdSRui Paulo /*
44132176cfdSRui Paulo * Invalidate channel-related state.
44232176cfdSRui Paulo */
44332176cfdSRui Paulo if (ic->ic_countryie != NULL) {
444*4f655ef5SMatthew Dillon IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
44532176cfdSRui Paulo ic->ic_countryie = NULL;
44632176cfdSRui Paulo }
44732176cfdSRui Paulo ieee80211_scan_flush(vap);
44832176cfdSRui Paulo ieee80211_dfs_reset(ic);
44932176cfdSRui Paulo if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
45032176cfdSRui Paulo c = ieee80211_find_channel(ic, desfreq, desflags);
45132176cfdSRui Paulo /* NB: may be NULL if not present in new channel list */
45232176cfdSRui Paulo vap->iv_des_chan = (c != NULL) ? c : IEEE80211_CHAN_ANYC;
45332176cfdSRui Paulo }
454085ff963SMatthew Dillon IEEE80211_UNLOCK(ic);
45532176cfdSRui Paulo
45632176cfdSRui Paulo return 0;
45732176cfdSRui Paulo }
458