summaryrefslogtreecommitdiffstats
path: root/sys/net80211/ieee80211_regdomain.c
blob: 257884f60a667af0d8a2f0dd7c316f1fd51df623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*-
 * Copyright (c) 2005-2007 Sam Leffler, Errno Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * IEEE 802.11 regdomain support.
 */

#include <sys/param.h>
#include <sys/systm.h> 
#include <sys/kernel.h>
 
#include <sys/socket.h>

#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <net/ethernet.h>

#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_regdomain.h>

void
ieee80211_regdomain_attach(struct ieee80211com *ic)
{
	ic->ic_regdomain = 0;			/* XXX */
	ic->ic_countrycode = CTRY_UNITED_STATES;/* XXX */
	ic->ic_location = 1+2;			/* both */
}

void
ieee80211_regdomain_detach(struct ieee80211com *ic)
{
}

static void
addchan(struct ieee80211com *ic, int ieee, int flags)
{
	struct ieee80211_channel *c;

	c = &ic->ic_channels[ic->ic_nchans++];
	c->ic_freq = ieee80211_ieee2mhz(ieee, flags);
	c->ic_ieee = ieee;
	c->ic_flags = flags;
}

/*
 * Setup the channel list for the specified regulatory domain,
 * country code, and operating modes.  This interface is used
 * when a driver does not obtain the channel list from another
 * source (such as firmware).
 */
void
ieee80211_init_channels(struct ieee80211com *ic,
	int rd, enum ISOCountryCode cc, int bands, int outdoor, int ecm)
{
	int i;

	/* XXX just do something for now */
	ic->ic_nchans = 0;
	if (isset(&bands, IEEE80211_MODE_11B) ||
	    isset(&bands, IEEE80211_MODE_11G)) {
		for (i = 1; i <= (ecm ? 14 : 11); i++) {
			if (isset(&bands, IEEE80211_MODE_11B))
				addchan(ic, i, IEEE80211_CHAN_B);
			if (isset(&bands, IEEE80211_MODE_11G))
				addchan(ic, i, IEEE80211_CHAN_G);
		}
	}
	if (isset(&bands, IEEE80211_MODE_11A)) {
		for (i = 36; i <= 64; i += 4)
			addchan(ic, i, IEEE80211_CHAN_A);
		for (i = 100; i <= 140; i += 4)
			addchan(ic, i, IEEE80211_CHAN_A);
		for (i = 149; i <= 161; i += 4)
			addchan(ic, i, IEEE80211_CHAN_A);
	}
	ic->ic_regdomain = rd;
	ic->ic_countrycode = cc;
	ic->ic_location = outdoor;
}

/*
 * Add Country Information IE.
 */
uint8_t *
ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic,
	enum ISOCountryCode cc, int location)
{
#define	CHAN_UNINTERESTING \
    (IEEE80211_CHAN_TURBO | IEEE80211_CHAN_STURBO | \
     IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)
	/* XXX what about auto? */
	/* flag set of channels to be excluded */
	static const int skipflags[IEEE80211_MODE_MAX] = {
	    CHAN_UNINTERESTING,				/* MODE_AUTO */
	    CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ,	/* MODE_11A */
	    CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ,	/* MODE_11B */
	    CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ,	/* MODE_11G */
	    CHAN_UNINTERESTING | IEEE80211_CHAN_OFDM |	/* MODE_FH */
	        IEEE80211_CHAN_CCK | IEEE80211_CHAN_DYN,
	    CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ,	/* MODE_TURBO_A */
	    CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ,	/* MODE_TURBO_G */
	    CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ,	/* MODE_STURBO_A */
	    CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ,	/* MODE_11NA */
	    CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ,	/* MODE_11NG */
	};
	struct ieee80211_country_ie *ie = (struct ieee80211_country_ie *)frm;
	const char *iso_name;
	uint8_t nextchan, chans[IEEE80211_CHAN_BYTES];
	int i, skip;

	ie->ie = IEEE80211_ELEMID_COUNTRY;
	iso_name = ieee80211_cctoiso(cc);
	if (iso_name == NULL) {
		if_printf(ic->ic_ifp, "bad country code %d ignored\n", cc);
		iso_name = "  ";
	}
	ie->cc[0] = iso_name[0];
	ie->cc[1] = iso_name[1];
	/* 
	 * Indoor/Outdoor portion of country string.
	 * NB: this is not quite right, since we should have one of:
	 *     'I' indoor only
	 *     'O' outdoor only
	 *     ' ' all enviroments
	 */
	ie->cc[2] = ((location & 3) == 3 ? ' ' : location & 1 ? 'I' : 'O');

	/* 
	 * Run-length encoded channel+max tx power info.
	 */
	frm = (uint8_t *)&ie->band[0];
	nextchan = 0;			/* NB: impossible channel # */
	memset(chans, 0, sizeof(chans));
	skip = skipflags[ic->ic_curmode];
	for (i = 0; i < ic->ic_nchans; i++) {
		const struct ieee80211_channel *c = &ic->ic_channels[i];

		if (isset(chans, c->ic_ieee))		/* suppress dup's */
			continue;
		if ((c->ic_flags & skip) == 0)		/* skip band, etc. */
			continue;
		setbit(chans, c->ic_ieee);
		if (c->ic_ieee != nextchan ||
		    c->ic_maxregpower != frm[-1]) {	/* new run */
			/* XXX max of 83 runs */
			frm[0] = c->ic_ieee;		/* starting channel # */
			frm[1] = 1;			/* # channels in run */
			frm[2] = c->ic_maxregpower;	/* tx power cap */
			frm += 3;
			nextchan = c->ic_ieee + 1;	/* overflow? */
		} else {				/* extend run */
			frm[-2]++;
			nextchan++;
		}
	}
	ie->len = frm - ie->cc;
	if (ie->len & 1)		/* pad to multiple of 2 */
		ie->len++;
	return frm;
#undef CHAN_UNINTERESTING
}

/*
 * Country Code Table for code-to-string conversion.
 */
static const struct {
	enum ISOCountryCode iso_code;	   
	const char*	iso_name;
} country_strings[] = {
    { CTRY_DEBUG,	 	"DB" },		/* NB: nonstandard */
    { CTRY_DEFAULT,	 	"NA" },		/* NB: nonstandard */
    { CTRY_ALBANIA,		"AL" },
    { CTRY_ALGERIA,		"DZ" },
    { CTRY_ARGENTINA,		"AR" },
    { CTRY_ARMENIA,		"AM" },
    { CTRY_AUSTRALIA,		"AU" },
    { CTRY_AUSTRIA,		"AT" },
    { CTRY_AZERBAIJAN,		"AZ" },
    { CTRY_BAHRAIN,		"BH" },
    { CTRY_BELARUS,		"BY" },
    { CTRY_BELGIUM,		"BE" },
    { CTRY_BELIZE,		"BZ" },
    { CTRY_BOLIVIA,		"BO" },
    { CTRY_BRAZIL,		"BR" },
    { CTRY_BRUNEI_DARUSSALAM,	"BN" },
    { CTRY_BULGARIA,		"BG" },
    { CTRY_CANADA,		"CA" },
    { CTRY_CHILE,		"CL" },
    { CTRY_CHINA,		"CN" },
    { CTRY_COLOMBIA,		"CO" },
    { CTRY_COSTA_RICA,		"CR" },
    { CTRY_CROATIA,		"HR" },
    { CTRY_CYPRUS,		"CY" },
    { CTRY_CZECH,		"CZ" },
    { CTRY_DENMARK,		"DK" },
    { CTRY_DOMINICAN_REPUBLIC,	"DO" },
    { CTRY_ECUADOR,		"EC" },
    { CTRY_EGYPT,		"EG" },
    { CTRY_EL_SALVADOR,		"SV" },    
    { CTRY_ESTONIA,		"EE" },
    { CTRY_FINLAND,		"FI" },
    { CTRY_FRANCE,		"FR" },
    { CTRY_FRANCE2,		"F2" },
    { CTRY_GEORGIA,		"GE" },
    { CTRY_GERMANY,		"DE" },
    { CTRY_GREECE,		"GR" },
    { CTRY_GUATEMALA,		"GT" },
    { CTRY_HONDURAS,		"HN" },
    { CTRY_HONG_KONG,		"HK" },
    { CTRY_HUNGARY,		"HU" },
    { CTRY_ICELAND,		"IS" },
    { CTRY_INDIA,		"IN" },
    { CTRY_INDONESIA,		"ID" },
    { CTRY_IRAN,		"IR" },
    { CTRY_IRELAND,		"IE" },
    { CTRY_ISRAEL,		"IL" },
    { CTRY_ITALY,		"IT" },
    { CTRY_JAMAICA,		"JM" },
    { CTRY_JAPAN,		"JP" },
    { CTRY_JAPAN1,		"J1" },
    { CTRY_JAPAN2,		"J2" },    
    { CTRY_JAPAN3,		"J3" },
    { CTRY_JAPAN4,		"J4" },
    { CTRY_JAPAN5,		"J5" },    
    { CTRY_JORDAN,		"JO" },
    { CTRY_KAZAKHSTAN,		"KZ" },
    { CTRY_KOREA_NORTH,		"KP" },
    { CTRY_KOREA_ROC,		"KR" },
    { CTRY_KOREA_ROC2,		"K2" },
    { CTRY_KUWAIT,		"KW" },
    { CTRY_LATVIA,		"LV" },
    { CTRY_LEBANON,		"LB" },
    { CTRY_LIECHTENSTEIN,	"LI" },
    { CTRY_LITHUANIA,		"LT" },
    { CTRY_LUXEMBOURG,		"LU" },
    { CTRY_MACAU,		"MO" },
    { CTRY_MACEDONIA,		"MK" },
    { CTRY_MALAYSIA,		"MY" },
    { CTRY_MEXICO,		"MX" },
    { CTRY_MONACO,		"MC" },
    { CTRY_MOROCCO,		"MA" },
    { CTRY_NETHERLANDS,		"NL" },
    { CTRY_NEW_ZEALAND,		"NZ" },
    { CTRY_NORWAY,		"NO" },
    { CTRY_OMAN,		"OM" },
    { CTRY_PAKISTAN,		"PK" },
    { CTRY_PANAMA,		"PA" },
    { CTRY_PERU,		"PE" },
    { CTRY_PHILIPPINES,		"PH" },
    { CTRY_POLAND,		"PL" },
    { CTRY_PORTUGAL,		"PT" },
    { CTRY_PUERTO_RICO,		"PR" },
    { CTRY_QATAR,		"QA" },
    { CTRY_ROMANIA,		"RO" },
    { CTRY_RUSSIA,		"RU" },
    { CTRY_SAUDI_ARABIA,	"SA" },
    { CTRY_SINGAPORE,		"SG" },
    { CTRY_SLOVAKIA,		"SK" },
    { CTRY_SLOVENIA,		"SI" },
    { CTRY_SOUTH_AFRICA,	"ZA" },
    { CTRY_SPAIN,		"ES" },
    { CTRY_SWEDEN,		"SE" },
    { CTRY_SWITZERLAND,		"CH" },
    { CTRY_SYRIA,		"SY" },
    { CTRY_TAIWAN,		"TW" },
    { CTRY_THAILAND,		"TH" },
    { CTRY_TRINIDAD_Y_TOBAGO,	"TT" },
    { CTRY_TUNISIA,		"TN" },
    { CTRY_TURKEY,		"TR" },
    { CTRY_UKRAINE,		"UA" },
    { CTRY_UAE,			"AE" },
    { CTRY_UNITED_KINGDOM,	"GB" },
    { CTRY_UNITED_STATES,	"US" },
    { CTRY_URUGUAY,		"UY" },
    { CTRY_UZBEKISTAN,		"UZ" },    
    { CTRY_VENEZUELA,		"VE" },
    { CTRY_VIET_NAM,		"VN" },
    { CTRY_YEMEN,		"YE" },
    { CTRY_ZIMBABWE,		"ZW" }    
};

const char *
ieee80211_cctoiso(enum ISOCountryCode cc)
{
#define	N(a)	(sizeof(a) / sizeof(a[0]))
	int i;

	for (i = 0; i < N(country_strings); i++) {
		if (country_strings[i].iso_code == cc)
			return country_strings[i].iso_name;
	}
	return NULL;
#undef N
}

int
ieee80211_isotocc(const char iso[2])
{
#define	N(a)	(sizeof(a) / sizeof(a[0]))
	int i;

	for (i = 0; i < N(country_strings); i++) {
		if (country_strings[i].iso_name[0] == iso[0] &&
		    country_strings[i].iso_name[1] == iso[1])
			return country_strings[i].iso_code;
	}
	return -1;
#undef N
}
OpenPOWER on IntegriCloud