1170530Ssam/*-
2178354Ssam * Copyright (c) 2005-2008 Sam Leffler, Errno Consulting
3170530Ssam * All rights reserved.
4170530Ssam *
5170530Ssam * Redistribution and use in source and binary forms, with or without
6170530Ssam * modification, are permitted provided that the following conditions
7170530Ssam * are met:
8170530Ssam * 1. Redistributions of source code must retain the above copyright
9170530Ssam *    notice, this list of conditions and the following disclaimer.
10170530Ssam * 2. Redistributions in binary form must reproduce the above copyright
11170530Ssam *    notice, this list of conditions and the following disclaimer in the
12170530Ssam *    documentation and/or other materials provided with the distribution.
13170530Ssam *
14170530Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15170530Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16170530Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17170530Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18170530Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19170530Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20170530Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21170530Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22170530Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23170530Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24170530Ssam */
25170530Ssam
26170530Ssam#include <sys/cdefs.h>
27170530Ssam__FBSDID("$FreeBSD$");
28170530Ssam
29170530Ssam/*
30170530Ssam * IEEE 802.11 regdomain support.
31170530Ssam */
32178354Ssam#include "opt_wlan.h"
33170530Ssam
34170530Ssam#include <sys/param.h>
35170530Ssam#include <sys/systm.h>
36170530Ssam#include <sys/kernel.h>
37170530Ssam
38170530Ssam#include <sys/socket.h>
39170530Ssam
40170530Ssam#include <net/if.h>
41170530Ssam#include <net/if_media.h>
42170530Ssam
43170530Ssam#include <net80211/ieee80211_var.h>
44170530Ssam#include <net80211/ieee80211_regdomain.h>
45170530Ssam
46178354Ssamstatic void
47187800Ssamnull_getradiocaps(struct ieee80211com *ic, int maxchan,
48187800Ssam	int *n, struct ieee80211_channel *c)
49178354Ssam{
50178354Ssam	/* just feed back the current channel list */
51187800Ssam	if (maxchan > ic->ic_nchans)
52187800Ssam		maxchan = ic->ic_nchans;
53187800Ssam	memcpy(c, ic->ic_channels, maxchan*sizeof(struct ieee80211_channel));
54187834Ssam	*n = maxchan;
55178354Ssam}
56178354Ssam
57178354Ssamstatic int
58178354Ssamnull_setregdomain(struct ieee80211com *ic,
59178354Ssam	struct ieee80211_regdomain *rd,
60178354Ssam	int nchans, struct ieee80211_channel chans[])
61178354Ssam{
62178354Ssam	return 0;		/* accept anything */
63178354Ssam}
64178354Ssam
65170530Ssamvoid
66170530Ssamieee80211_regdomain_attach(struct ieee80211com *ic)
67170530Ssam{
68178354Ssam	if (ic->ic_regdomain.regdomain == 0 &&
69178354Ssam	    ic->ic_regdomain.country == CTRY_DEFAULT) {
70178354Ssam		ic->ic_regdomain.country = CTRY_UNITED_STATES;	/* XXX */
71178354Ssam		ic->ic_regdomain.location = ' ';		/* both */
72178354Ssam		ic->ic_regdomain.isocc[0] = 'U';		/* XXX */
73178354Ssam		ic->ic_regdomain.isocc[1] = 'S';		/* XXX */
74187802Ssam		/* NB: driver calls ieee80211_init_channels or similar */
75178354Ssam	}
76178354Ssam	ic->ic_getradiocaps = null_getradiocaps;
77178354Ssam	ic->ic_setregdomain = null_setregdomain;
78170530Ssam}
79170530Ssam
80170530Ssamvoid
81170530Ssamieee80211_regdomain_detach(struct ieee80211com *ic)
82170530Ssam{
83178354Ssam	if (ic->ic_countryie != NULL) {
84178354Ssam		free(ic->ic_countryie, M_80211_NODE_IE);
85178354Ssam		ic->ic_countryie = NULL;
86178354Ssam	}
87170530Ssam}
88170530Ssam
89178354Ssamvoid
90178354Ssamieee80211_regdomain_vattach(struct ieee80211vap *vap)
91178354Ssam{
92178354Ssam}
93178354Ssam
94178354Ssamvoid
95178354Ssamieee80211_regdomain_vdetach(struct ieee80211vap *vap)
96178354Ssam{
97178354Ssam}
98178354Ssam
99170530Ssamstatic void
100170530Ssamaddchan(struct ieee80211com *ic, int ieee, int flags)
101170530Ssam{
102170530Ssam	struct ieee80211_channel *c;
103170530Ssam
104170530Ssam	c = &ic->ic_channels[ic->ic_nchans++];
105170530Ssam	c->ic_freq = ieee80211_ieee2mhz(ieee, flags);
106170530Ssam	c->ic_ieee = ieee;
107170530Ssam	c->ic_flags = flags;
108178354Ssam	c->ic_extieee = 0;
109170530Ssam}
110170530Ssam
111170530Ssam/*
112170530Ssam * Setup the channel list for the specified regulatory domain,
113170530Ssam * country code, and operating modes.  This interface is used
114170530Ssam * when a driver does not obtain the channel list from another
115170530Ssam * source (such as firmware).
116170530Ssam */
117178354Ssamint
118170530Ssamieee80211_init_channels(struct ieee80211com *ic,
119178354Ssam	const struct ieee80211_regdomain *rd, const uint8_t bands[])
120170530Ssam{
121170530Ssam	int i;
122170530Ssam
123170530Ssam	/* XXX just do something for now */
124170530Ssam	ic->ic_nchans = 0;
125178354Ssam	if (isset(bands, IEEE80211_MODE_11B) ||
126178354Ssam	    isset(bands, IEEE80211_MODE_11G)) {
127178354Ssam		int maxchan = 11;
128178354Ssam		if (rd != NULL && rd->ecm)
129178354Ssam			maxchan = 14;
130178354Ssam		for (i = 1; i <= maxchan; i++) {
131178354Ssam			if (isset(bands, IEEE80211_MODE_11B))
132170530Ssam				addchan(ic, i, IEEE80211_CHAN_B);
133178354Ssam			if (isset(bands, IEEE80211_MODE_11G))
134170530Ssam				addchan(ic, i, IEEE80211_CHAN_G);
135170530Ssam		}
136170530Ssam	}
137178354Ssam	if (isset(bands, IEEE80211_MODE_11A)) {
138170530Ssam		for (i = 36; i <= 64; i += 4)
139170530Ssam			addchan(ic, i, IEEE80211_CHAN_A);
140170530Ssam		for (i = 100; i <= 140; i += 4)
141170530Ssam			addchan(ic, i, IEEE80211_CHAN_A);
142170530Ssam		for (i = 149; i <= 161; i += 4)
143170530Ssam			addchan(ic, i, IEEE80211_CHAN_A);
144170530Ssam	}
145178354Ssam	if (rd != NULL)
146178354Ssam		ic->ic_regdomain = *rd;
147178354Ssam
148178354Ssam	return 0;
149170530Ssam}
150170530Ssam
151178354Ssamstatic __inline int
152178354Ssamchancompar(const void *a, const void *b)
153178354Ssam{
154178354Ssam	const struct ieee80211_channel *ca = a;
155178354Ssam	const struct ieee80211_channel *cb = b;
156178354Ssam
157178354Ssam	return (ca->ic_freq == cb->ic_freq) ?
158178354Ssam		(ca->ic_flags & IEEE80211_CHAN_ALL) -
159178354Ssam		    (cb->ic_flags & IEEE80211_CHAN_ALL) :
160178354Ssam		ca->ic_freq - cb->ic_freq;
161178354Ssam}
162178354Ssam
163170530Ssam/*
164178354Ssam * Insertion sort.
165170530Ssam */
166178354Ssam#define swap(_a, _b, _size) {			\
167178354Ssam	uint8_t *s = _b;			\
168178354Ssam	int i = _size;				\
169178354Ssam	do {					\
170178354Ssam		uint8_t tmp = *_a;		\
171178354Ssam		*_a++ = *s;			\
172178354Ssam		*s++ = tmp;			\
173178354Ssam	} while (--i);				\
174178354Ssam	_a -= _size;				\
175178354Ssam}
176178354Ssam
177178354Ssamstatic void
178178354Ssamsort_channels(void *a, size_t n, size_t size)
179170530Ssam{
180178354Ssam	uint8_t *aa = a;
181178354Ssam	uint8_t *ai, *t;
182178354Ssam
183178354Ssam	KASSERT(n > 0, ("no channels"));
184178354Ssam	for (ai = aa+size; --n >= 1; ai += size)
185178354Ssam		for (t = ai; t > aa; t -= size) {
186178354Ssam			uint8_t *u = t - size;
187178354Ssam			if (chancompar(u, t) <= 0)
188178354Ssam				break;
189178354Ssam			swap(u, t, size);
190178354Ssam		}
191178354Ssam}
192178354Ssam#undef swap
193178354Ssam
194178354Ssam/*
195178354Ssam * Order channels w/ the same frequency so that
196178354Ssam * b < g < htg and a < hta.  This is used to optimize
197178354Ssam * channel table lookups and some user applications
198178354Ssam * may also depend on it (though they should not).
199178354Ssam */
200178354Ssamvoid
201178354Ssamieee80211_sort_channels(struct ieee80211_channel chans[], int nchans)
202178354Ssam{
203178354Ssam	if (nchans > 0)
204178354Ssam		sort_channels(chans, nchans, sizeof(struct ieee80211_channel));
205178354Ssam}
206178354Ssam
207178354Ssam/*
208178354Ssam * Allocate and construct a Country Information IE.
209178354Ssam */
210178354Ssamstruct ieee80211_appie *
211178354Ssamieee80211_alloc_countryie(struct ieee80211com *ic)
212178354Ssam{
213170530Ssam#define	CHAN_UNINTERESTING \
214170530Ssam    (IEEE80211_CHAN_TURBO | IEEE80211_CHAN_STURBO | \
215170530Ssam     IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)
216170530Ssam	/* XXX what about auto? */
217188782Ssam	/* flag set of channels to be excluded (band added below) */
218170530Ssam	static const int skipflags[IEEE80211_MODE_MAX] = {
219188778Ssam	    [IEEE80211_MODE_AUTO]	= CHAN_UNINTERESTING,
220188778Ssam	    [IEEE80211_MODE_11A]	= CHAN_UNINTERESTING,
221188778Ssam	    [IEEE80211_MODE_11B]	= CHAN_UNINTERESTING,
222188778Ssam	    [IEEE80211_MODE_11G]	= CHAN_UNINTERESTING,
223188778Ssam	    [IEEE80211_MODE_FH]		= CHAN_UNINTERESTING
224188778Ssam					| IEEE80211_CHAN_OFDM
225188778Ssam					| IEEE80211_CHAN_CCK
226188778Ssam					| IEEE80211_CHAN_DYN,
227188778Ssam	    [IEEE80211_MODE_TURBO_A]	= CHAN_UNINTERESTING,
228188778Ssam	    [IEEE80211_MODE_TURBO_G]	= CHAN_UNINTERESTING,
229188778Ssam	    [IEEE80211_MODE_STURBO_A]	= CHAN_UNINTERESTING,
230188782Ssam	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_TURBO
231188782Ssam					| IEEE80211_CHAN_STURBO,
232188782Ssam	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_TURBO
233188782Ssam					| IEEE80211_CHAN_STURBO,
234188778Ssam	    [IEEE80211_MODE_11NA]	= CHAN_UNINTERESTING,
235188778Ssam	    [IEEE80211_MODE_11NG]	= CHAN_UNINTERESTING,
236170530Ssam	};
237178354Ssam	const struct ieee80211_regdomain *rd = &ic->ic_regdomain;
238178354Ssam	uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm;
239178354Ssam	struct ieee80211_appie *aie;
240178354Ssam	struct ieee80211_country_ie *ie;
241178354Ssam	int i, skip, nruns;
242170530Ssam
243178354Ssam	aie = malloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
244178354Ssam	    M_NOWAIT | M_ZERO);
245178354Ssam	if (aie == NULL) {
246178354Ssam		if_printf(ic->ic_ifp,
247178354Ssam		    "%s: unable to allocate memory for country ie\n", __func__);
248178354Ssam		/* XXX stat */
249178354Ssam		return NULL;
250178354Ssam	}
251178354Ssam	ie = (struct ieee80211_country_ie *) aie->ie_data;
252170530Ssam	ie->ie = IEEE80211_ELEMID_COUNTRY;
253178354Ssam	if (rd->isocc[0] == '\0') {
254178354Ssam		if_printf(ic->ic_ifp, "no ISO country string for cc %d; "
255178354Ssam			"using blanks\n", rd->country);
256178354Ssam		ie->cc[0] = ie->cc[1] = ' ';
257178354Ssam	} else {
258178354Ssam		ie->cc[0] = rd->isocc[0];
259178354Ssam		ie->cc[1] = rd->isocc[1];
260170530Ssam	}
261170530Ssam	/*
262178354Ssam	 * Indoor/Outdoor portion of country string:
263170530Ssam	 *     'I' indoor only
264170530Ssam	 *     'O' outdoor only
265170530Ssam	 *     ' ' all enviroments
266170530Ssam	 */
267178354Ssam	ie->cc[2] = (rd->location == 'I' ? 'I' :
268178354Ssam		     rd->location == 'O' ? 'O' : ' ');
269170530Ssam	/*
270170530Ssam	 * Run-length encoded channel+max tx power info.
271170530Ssam	 */
272170530Ssam	frm = (uint8_t *)&ie->band[0];
273170530Ssam	nextchan = 0;			/* NB: impossible channel # */
274178354Ssam	nruns = 0;
275170530Ssam	memset(chans, 0, sizeof(chans));
276178354Ssam	skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)];
277188778Ssam	if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
278188778Ssam		skip |= IEEE80211_CHAN_2GHZ;
279188778Ssam	else if (IEEE80211_IS_CHAN_2GHZ(ic->ic_bsschan))
280188778Ssam		skip |= IEEE80211_CHAN_5GHZ;
281170530Ssam	for (i = 0; i < ic->ic_nchans; i++) {
282170530Ssam		const struct ieee80211_channel *c = &ic->ic_channels[i];
283170530Ssam
284170530Ssam		if (isset(chans, c->ic_ieee))		/* suppress dup's */
285170530Ssam			continue;
286172204Ssam		if (c->ic_flags & skip)			/* skip band, etc. */
287170530Ssam			continue;
288170530Ssam		setbit(chans, c->ic_ieee);
289170530Ssam		if (c->ic_ieee != nextchan ||
290170530Ssam		    c->ic_maxregpower != frm[-1]) {	/* new run */
291178354Ssam			if (nruns == IEEE80211_COUNTRY_MAX_BANDS) {
292178354Ssam				if_printf(ic->ic_ifp, "%s: country ie too big, "
293178354Ssam				    "runs > max %d, truncating\n",
294178354Ssam				    __func__, IEEE80211_COUNTRY_MAX_BANDS);
295178354Ssam				/* XXX stat? fail? */
296178354Ssam				break;
297178354Ssam			}
298170530Ssam			frm[0] = c->ic_ieee;		/* starting channel # */
299170530Ssam			frm[1] = 1;			/* # channels in run */
300170530Ssam			frm[2] = c->ic_maxregpower;	/* tx power cap */
301170530Ssam			frm += 3;
302170530Ssam			nextchan = c->ic_ieee + 1;	/* overflow? */
303178354Ssam			nruns++;
304170530Ssam		} else {				/* extend run */
305170530Ssam			frm[-2]++;
306170530Ssam			nextchan++;
307170530Ssam		}
308170530Ssam	}
309170530Ssam	ie->len = frm - ie->cc;
310171985Ssephe	if (ie->len & 1) {		/* Zero pad to multiple of 2 */
311170530Ssam		ie->len++;
312171985Ssephe		*frm++ = 0;
313171985Ssephe	}
314178354Ssam	aie->ie_len = frm - aie->ie_data;
315178354Ssam
316178354Ssam	return aie;
317170530Ssam#undef CHAN_UNINTERESTING
318170530Ssam}
319170530Ssam
320178354Ssamstatic int
321178354Ssamallvapsdown(struct ieee80211com *ic)
322170530Ssam{
323178354Ssam	struct ieee80211vap *vap;
324170530Ssam
325178354Ssam	IEEE80211_LOCK_ASSERT(ic);
326178354Ssam	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
327178354Ssam		if (vap->iv_state != IEEE80211_S_INIT)
328178354Ssam			return 0;
329178354Ssam	return 1;
330170530Ssam}
331170530Ssam
332170530Ssamint
333178354Ssamieee80211_setregdomain(struct ieee80211vap *vap,
334178354Ssam    struct ieee80211_regdomain_req *reg)
335170530Ssam{
336178354Ssam	struct ieee80211com *ic = vap->iv_ic;
337178354Ssam	struct ieee80211_channel *c;
338178354Ssam	int desfreq = 0, desflags = 0;		/* XXX silence gcc complaint */
339178354Ssam	int error, i;
340170530Ssam
341178354Ssam	if (reg->rd.location != 'I' && reg->rd.location != 'O' &&
342184272Ssam	    reg->rd.location != ' ') {
343184272Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
344184272Ssam		    "%s: invalid location 0x%x\n", __func__, reg->rd.location);
345178354Ssam		return EINVAL;
346184272Ssam	}
347184272Ssam	if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0') {
348184272Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
349184272Ssam		    "%s: invalid iso cc 0x%x:0x%x\n", __func__,
350184272Ssam		    reg->rd.isocc[0], reg->rd.isocc[1]);
351178354Ssam		return EINVAL;
352184272Ssam	}
353186107Ssam	if (reg->chaninfo.ic_nchans > IEEE80211_CHAN_MAX) {
354184272Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
355184272Ssam		    "%s: too many channels %u, max %u\n", __func__,
356184272Ssam		    reg->chaninfo.ic_nchans, IEEE80211_CHAN_MAX);
357178354Ssam		return EINVAL;
358184272Ssam	}
359178354Ssam	/*
360178354Ssam	 * Calculate freq<->IEEE mapping and default max tx power
361178354Ssam	 * for channels not setup.  The driver can override these
362178354Ssam	 * setting to reflect device properties/requirements.
363178354Ssam	 */
364178354Ssam	for (i = 0; i < reg->chaninfo.ic_nchans; i++) {
365178354Ssam		c = &reg->chaninfo.ic_chans[i];
366184272Ssam		if (c->ic_freq == 0 || c->ic_flags == 0) {
367184272Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
368184272Ssam			    "%s: invalid channel spec at [%u]\n", __func__, i);
369178354Ssam			return EINVAL;
370184272Ssam		}
371184272Ssam		if (c->ic_maxregpower == 0) {
372184272Ssam			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
373184272Ssam			    "%s: invalid channel spec, zero maxregpower, "
374184272Ssam			    "freq %u flags 0x%x\n", __func__,
375184272Ssam			    c->ic_freq, c->ic_flags);
376178354Ssam			return EINVAL;
377184272Ssam		}
378178354Ssam		if (c->ic_ieee == 0)
379178354Ssam			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
380178354Ssam		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
381178354Ssam			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
382178354Ssam			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
383178354Ssam			    c->ic_flags);
384178354Ssam		if (c->ic_maxpower == 0)
385178354Ssam			c->ic_maxpower = 2*c->ic_maxregpower;
386170530Ssam	}
387178354Ssam	IEEE80211_LOCK(ic);
388187635Ssam	/* XXX bandaid; a running vap will likely crash */
389187635Ssam	if (!allvapsdown(ic)) {
390187635Ssam		IEEE80211_UNLOCK(ic);
391187635Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
392187635Ssam		    "%s: reject: vaps are running\n", __func__);
393187635Ssam		return EBUSY;
394187635Ssam	}
395178354Ssam	error = ic->ic_setregdomain(ic, &reg->rd,
396178354Ssam	    reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans);
397178354Ssam	if (error != 0) {
398178354Ssam		IEEE80211_UNLOCK(ic);
399184272Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
400184272Ssam		    "%s: driver rejected request, error %u\n", __func__, error);
401178354Ssam		return error;
402178354Ssam	}
403178354Ssam	/*
404178354Ssam	 * Commit: copy in new channel table and reset media state.
405178354Ssam	 * On return the state machines will be clocked so all vaps
406178354Ssam	 * will reset their state.
407178354Ssam	 *
408178354Ssam	 * XXX ic_bsschan is marked undefined, must have vap's in
409178354Ssam	 *     INIT state or we blow up forcing stations off
410178354Ssam	 */
411178354Ssam	/*
412178354Ssam	 * Save any desired channel for restore below.  Note this
413178354Ssam	 * needs to be done for all vaps but for now we only do
414178354Ssam	 * the one where the ioctl is issued.
415178354Ssam	 */
416178354Ssam	if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
417178354Ssam		desfreq = vap->iv_des_chan->ic_freq;
418178354Ssam		desflags = vap->iv_des_chan->ic_flags;
419178354Ssam	}
420178354Ssam	/* regdomain parameters */
421178354Ssam	memcpy(&ic->ic_regdomain, &reg->rd, sizeof(reg->rd));
422178354Ssam	/* channel table */
423178354Ssam	memcpy(ic->ic_channels, reg->chaninfo.ic_chans,
424178354Ssam	    reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel));
425178354Ssam	ic->ic_nchans = reg->chaninfo.ic_nchans;
426178354Ssam	memset(&ic->ic_channels[ic->ic_nchans], 0,
427178354Ssam	    (IEEE80211_CHAN_MAX - ic->ic_nchans) *
428178354Ssam	       sizeof(struct ieee80211_channel));
429178354Ssam	ieee80211_media_init(ic);
430178354Ssam
431178354Ssam	/*
432178354Ssam	 * Invalidate channel-related state.
433178354Ssam	 */
434178354Ssam	if (ic->ic_countryie != NULL) {
435178354Ssam		free(ic->ic_countryie, M_80211_NODE_IE);
436178354Ssam		ic->ic_countryie = NULL;
437178354Ssam	}
438178354Ssam	ieee80211_scan_flush(vap);
439178354Ssam	ieee80211_dfs_reset(ic);
440178354Ssam	if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
441188427Ssam		c = ieee80211_find_channel(ic, desfreq, desflags);
442178354Ssam		/* NB: may be NULL if not present in new channel list */
443188427Ssam		vap->iv_des_chan = (c != NULL) ? c : IEEE80211_CHAN_ANYC;
444178354Ssam	}
445178354Ssam	IEEE80211_UNLOCK(ic);
446178354Ssam
447178354Ssam	return 0;
448170530Ssam}
449