[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/net80211/ieee80211_regdomain.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  1 /*-
  2  * Copyright (c) 2005-2008 Sam Leffler, Errno Consulting
  3  * All rights reserved.
  4  *
  5  * Redistribution and use in source and binary forms, with or without
  6  * modification, are permitted provided that the following conditions
  7  * are met:
  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.
 13  *
 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.
 24  */
 25 
 26 #include <sys/cdefs.h>
 27 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_regdomain.c,v 1.5 2008/10/25 23:28:24 sam Exp $");
 28 
 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  
 38 #include <sys/socket.h>
 39 
 40 #include <net/if.h>
 41 #include <net/if_media.h>
 42 
 43 #include <net80211/ieee80211_var.h>
 44 #include <net80211/ieee80211_regdomain.h>
 45 
 46 static void
 47 null_getradiocaps(struct ieee80211com *ic, int *n, struct ieee80211_channel *c)
 48 {
 49         /* just feed back the current channel list */
 50         *n = ic->ic_nchans;
 51         memcpy(c, ic->ic_channels,
 52             ic->ic_nchans*sizeof(struct ieee80211_channel));
 53 }
 54 
 55 static int
 56 null_setregdomain(struct ieee80211com *ic,
 57         struct ieee80211_regdomain *rd,
 58         int nchans, struct ieee80211_channel chans[])
 59 {
 60         return 0;               /* accept anything */
 61 }
 62 
 63 void
 64 ieee80211_regdomain_attach(struct ieee80211com *ic)
 65 {
 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 */
 73         }
 74         ic->ic_getradiocaps = null_getradiocaps;
 75         ic->ic_setregdomain = null_setregdomain;
 76 }
 77 
 78 void
 79 ieee80211_regdomain_detach(struct ieee80211com *ic)
 80 {
 81         if (ic->ic_countryie != NULL) {
 82                 free(ic->ic_countryie, M_80211_NODE_IE);
 83                 ic->ic_countryie = NULL;
 84         }
 85 }
 86 
 87 void
 88 ieee80211_regdomain_vattach(struct ieee80211vap *vap)
 89 {
 90 }
 91 
 92 void
 93 ieee80211_regdomain_vdetach(struct ieee80211vap *vap)
 94 {
 95 }
 96 
 97 static void
 98 addchan(struct ieee80211com *ic, int ieee, int flags)
 99 {
100         struct ieee80211_channel *c;
101 
102         c = &ic->ic_channels[ic->ic_nchans++];
103         c->ic_freq = ieee80211_ieee2mhz(ieee, flags);
104         c->ic_ieee = ieee;
105         c->ic_flags = flags;
106         c->ic_extieee = 0;
107 }
108 
109 /*
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).
114  */
115 int
116 ieee80211_init_channels(struct ieee80211com *ic,
117         const struct ieee80211_regdomain *rd, const uint8_t bands[])
118 {
119         int i;
120 
121         /* XXX just do something for now */
122         ic->ic_nchans = 0;
123         if (isset(bands, IEEE80211_MODE_11B) ||
124             isset(bands, IEEE80211_MODE_11G)) {
125                 int maxchan = 11;
126                 if (rd != NULL && rd->ecm)
127                         maxchan = 14;
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);
133                 }
134         }
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);
142         }
143         if (rd != NULL)
144                 ic->ic_regdomain = *rd;
145 
146         return 0;
147 }
148 
149 static __inline int
150 chancompar(const void *a, const void *b)
151 {
152         const struct ieee80211_channel *ca = a;
153         const struct ieee80211_channel *cb = b;
154 
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;
159 }
160 
161 /*
162  * Insertion sort.
163  */
164 #define swap(_a, _b, _size) {                   \
165         uint8_t *s = _b;                        \
166         int i = _size;                          \
167         do {                                    \
168                 uint8_t tmp = *_a;              \
169                 *_a++ = *s;                     \
170                 *s++ = tmp;                     \
171         } while (--i);                          \
172         _a -= _size;                            \
173 }
174 
175 static void
176 sort_channels(void *a, size_t n, size_t size)
177 {
178         uint8_t *aa = a;
179         uint8_t *ai, *t;
180 
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)
186                                 break;
187                         swap(u, t, size);
188                 }
189 }
190 #undef swap
191 
192 /*
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).
197  */
198 void
199 ieee80211_sort_channels(struct ieee80211_channel chans[], int nchans)
200 {
201         if (nchans > 0)
202                 sort_channels(chans, nchans, sizeof(struct ieee80211_channel));
203 }
204 
205 /*
206  * Allocate and construct a Country Information IE.
207  */
208 struct ieee80211_appie *
209 ieee80211_alloc_countryie(struct ieee80211com *ic)
210 {
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 */
228         };
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;
233         int i, skip, nruns;
234 
235         aie = malloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
236             M_NOWAIT | M_ZERO);
237         if (aie == NULL) {
238                 if_printf(ic->ic_ifp,
239                     "%s: unable to allocate memory for country ie\n", __func__);
240                 /* XXX stat */
241                 return NULL;
242         }
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] = ' ';
249         } else {
250                 ie->cc[0] = rd->isocc[0];
251                 ie->cc[1] = rd->isocc[1];
252         }
253         /* 
254          * Indoor/Outdoor portion of country string:
255          *     'I' indoor only
256          *     'O' outdoor only
257          *     ' ' all enviroments
258          */
259         ie->cc[2] = (rd->location == 'I' ? 'I' :
260                      rd->location == 'O' ? 'O' : ' ');
261         /* 
262          * Run-length encoded channel+max tx power info.
263          */
264         frm = (uint8_t *)&ie->band[0];
265         nextchan = 0;                   /* NB: impossible channel # */
266         nruns = 0;
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];
271 
272                 if (isset(chans, c->ic_ieee))           /* suppress dup's */
273                         continue;
274                 if (c->ic_flags & skip)                 /* skip band, etc. */
275                         continue;
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? */
284                                 break;
285                         }
286                         frm[0] = c->ic_ieee;            /* starting channel # */
287                         frm[1] = 1;                     /* # channels in run */
288                         frm[2] = c->ic_maxregpower;     /* tx power cap */
289                         frm += 3;
290                         nextchan = c->ic_ieee + 1;      /* overflow? */
291                         nruns++;
292                 } else {                                /* extend run */
293                         frm[-2]++;
294                         nextchan++;
295                 }
296         }
297         ie->len = frm - ie->cc;
298         if (ie->len & 1) {              /* Zero pad to multiple of 2 */
299                 ie->len++;
300                 *frm++ = 0;
301         }
302         aie->ie_len = frm - aie->ie_data;
303 
304         return aie;
305 #undef CHAN_UNINTERESTING
306 }
307 
308 static int
309 allvapsdown(struct ieee80211com *ic)
310 {
311         struct ieee80211vap *vap;
312 
313         IEEE80211_LOCK_ASSERT(ic);
314         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
315                 if (vap->iv_state != IEEE80211_S_INIT)
316                         return 0;
317         return 1;
318 }
319 
320 int
321 ieee80211_setregdomain(struct ieee80211vap *vap,
322     struct ieee80211_regdomain_req *reg)
323 {
324         struct ieee80211com *ic = vap->iv_ic;
325         struct ieee80211_channel *c;
326         int desfreq = 0, desflags = 0;          /* XXX silence gcc complaint */
327         int error, i;
328 
329         if (reg->rd.location != 'I' && reg->rd.location != 'O' &&
330             reg->rd.location != ' ') {
331                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
332                     "%s: invalid location 0x%x\n", __func__, reg->rd.location);
333                 return EINVAL;
334         }
335         if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0') {
336                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
337                     "%s: invalid iso cc 0x%x:0x%x\n", __func__,
338                     reg->rd.isocc[0], reg->rd.isocc[1]);
339                 return EINVAL;
340         }
341         if (reg->chaninfo.ic_nchans >= IEEE80211_CHAN_MAX) {
342                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
343                     "%s: too many channels %u, max %u\n", __func__,
344                     reg->chaninfo.ic_nchans, IEEE80211_CHAN_MAX);
345                 return EINVAL;
346         }
347         /*
348          * Calculate freq<->IEEE mapping and default max tx power
349          * for channels not setup.  The driver can override these
350          * setting to reflect device properties/requirements.
351          */
352         for (i = 0; i < reg->chaninfo.ic_nchans; i++) {
353                 c = &reg->chaninfo.ic_chans[i];
354                 if (c->ic_freq == 0 || c->ic_flags == 0) {
355                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
356                             "%s: invalid channel spec at [%u]\n", __func__, i);
357                         return EINVAL;
358                 }
359                 if (c->ic_maxregpower == 0) {
360                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
361                             "%s: invalid channel spec, zero maxregpower, "
362                             "freq %u flags 0x%x\n", __func__,
363                             c->ic_freq, c->ic_flags);
364                         return EINVAL;
365                 }
366                 if (c->ic_ieee == 0)
367                         c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
368                 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
369                         c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
370                             (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
371                             c->ic_flags);
372                 if (c->ic_maxpower == 0)
373                         c->ic_maxpower = 2*c->ic_maxregpower;
374         }
375         IEEE80211_LOCK(ic);
376         error = ic->ic_setregdomain(ic, &reg->rd,
377             reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans);
378         if (error != 0) {
379                 IEEE80211_UNLOCK(ic);
380                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
381                     "%s: driver rejected request, error %u\n", __func__, error);
382                 return error;
383         }
384         /* XXX bandaid; a running vap will likely crash */
385         if (!allvapsdown(ic)) {
386                 IEEE80211_UNLOCK(ic);
387                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
388                     "%s: reject: vaps are running\n", __func__);
389                 return EBUSY;
390         }
391         /*
392          * Commit: copy in new channel table and reset media state.
393          * On return the state machines will be clocked so all vaps
394          * will reset their state.
395          *
396          * XXX ic_bsschan is marked undefined, must have vap's in
397          *     INIT state or we blow up forcing stations off
398          */
399         /*
400          * Save any desired channel for restore below.  Note this
401          * needs to be done for all vaps but for now we only do
402          * the one where the ioctl is issued.
403          */
404         if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
405                 desfreq = vap->iv_des_chan->ic_freq;
406                 desflags = vap->iv_des_chan->ic_flags;
407         }
408         /* regdomain parameters */
409         memcpy(&ic->ic_regdomain, &reg->rd, sizeof(reg->rd));
410         /* channel table */
411         memcpy(ic->ic_channels, reg->chaninfo.ic_chans,
412             reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel));
413         ic->ic_nchans = reg->chaninfo.ic_nchans;
414         memset(&ic->ic_channels[ic->ic_nchans], 0,
415             (IEEE80211_CHAN_MAX - ic->ic_nchans) *
416                sizeof(struct ieee80211_channel));
417         ieee80211_media_init(ic);
418 
419         /*
420          * Invalidate channel-related state.
421          */
422         if (ic->ic_countryie != NULL) {
423                 free(ic->ic_countryie, M_80211_NODE_IE);
424                 ic->ic_countryie = NULL;
425         }
426         ieee80211_scan_flush(vap);
427         ieee80211_dfs_reset(ic);
428         if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
429                 /* NB: may be NULL if not present in new channel list */
430                 vap->iv_des_chan = ieee80211_find_channel(ic, desfreq, desflags);
431         }
432         IEEE80211_UNLOCK(ic);
433 
434         return 0;
435 }
436 

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.