1 /*	$OpenBSD: ieee80211_regdomain.c,v 1.7 2006/11/26 19:46:28 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2004, 2005 Reyk Floeter <reyk@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * Basic regulation domain extensions for the IEEE 802.11 stack
21  */
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/mbuf.h>
26 #include <sys/kernel.h>
27 #include <sys/socket.h>
28 #include <sys/sockio.h>
29 #include <sys/endian.h>
30 #include <sys/errno.h>
31 
32 #include <net/if.h>
33 #include <net/if_dl.h>
34 #include <net/if_media.h>
35 #include <net/if_arp.h>
36 #include <net/if_llc.h>
37 
38 #ifdef INET
39 #include <netinet/in.h>
40 #include <netinet/if_ether.h>
41 #endif
42 
43 #include <net80211/ieee80211_var.h>
44 #include <net80211/ieee80211_regdomain.h>
45 
46 int	 ieee80211_regdomain_compare_cn(const void *, const void *);
47 int	 ieee80211_regdomain_compare_rn(const void *, const void *);
48 
49 static const struct ieee80211_regdomainname
50 ieee80211_r_names[] = IEEE80211_REGDOMAIN_NAMES;
51 
52 static const struct ieee80211_regdomainmap
53 ieee80211_r_map[] = IEEE80211_REGDOMAIN_MAP;
54 
55 static const struct ieee80211_countryname
56 ieee80211_r_ctry[] = IEEE80211_REGDOMAIN_COUNTRY_NAMES;
57 
58 #ifndef bsearch
59 const void *bsearch(const void *, const void *, size_t, size_t,
60     int (*)(const void *, const void *));
61 
62 const void *
63 bsearch(const void *key, const void *base0, size_t nmemb, size_t size,
64     int (*compar)(const void *, const void *))
65 {
66 	const char *base = base0;
67 	int lim, cmp;
68 	const void *p;
69 
70 	for (lim = nmemb; lim != 0; lim >>= 1) {
71 		p = base + (lim >> 1) * size;
72 		cmp = (*compar)(key, p);
73 		if (cmp == 0)
74 			return ((const void *)p);
75 		if (cmp > 0) {  /* key > p: move right */
76 			base = (const char *)p + size;
77 			lim--;
78 		} /* else move left */
79 	}
80 	return (NULL);
81 }
82 #endif
83 
84 int
85 ieee80211_regdomain_compare_cn(const void *a, const void *b)
86 {
87 	return (strcmp(((const struct ieee80211_countryname*)a)->cn_name,
88 	    ((const struct ieee80211_countryname*)b)->cn_name));
89 }
90 
91 int
92 ieee80211_regdomain_compare_rn(const void *a, const void *b)
93 {
94 	return (strcmp(((const struct ieee80211_regdomainname*)a)->rn_name,
95 	    ((const struct ieee80211_regdomainname*)b)->rn_name));
96 }
97 
98 u_int16_t
99 ieee80211_name2countrycode(const char *name)
100 {
101 	const struct ieee80211_countryname key = { CTRY_DEFAULT, name }, *value;
102 
103 	if ((value = bsearch(&key, &ieee80211_r_ctry,
104 	    sizeof(ieee80211_r_ctry) / sizeof(ieee80211_r_ctry[0]),
105 	    sizeof(struct ieee80211_countryname),
106 	    ieee80211_regdomain_compare_cn)) != NULL)
107 		return (value->cn_code);
108 
109 	return (CTRY_DEFAULT);
110 }
111 
112 u_int32_t
113 ieee80211_name2regdomain(const char *name)
114 {
115 	const struct ieee80211_regdomainname *value;
116 	struct ieee80211_regdomainname key;
117 
118 	key.rn_domain = DMN_DEFAULT;
119 	key.rn_name = name;
120 
121 	if ((value = bsearch(&key, &ieee80211_r_names,
122 	    sizeof(ieee80211_r_names) / sizeof(ieee80211_r_names[0]),
123 	    sizeof(struct ieee80211_regdomainname),
124 	    ieee80211_regdomain_compare_rn)) != NULL)
125 		return ((u_int32_t)value->rn_domain);
126 
127 	return ((u_int32_t)DMN_DEFAULT);
128 }
129 
130 const char *
131 ieee80211_countrycode2name(u_int16_t code)
132 {
133 	int i;
134 
135 	/* Linear search over the table */
136 	for (i = 0; i < (sizeof(ieee80211_r_ctry) /
137 	    sizeof(ieee80211_r_ctry[0])); i++)
138 		if (ieee80211_r_ctry[i].cn_code == code)
139 			return (ieee80211_r_ctry[i].cn_name);
140 
141 	return (NULL);
142 }
143 
144 const char *
145 ieee80211_regdomain2name(u_int32_t regdomain)
146 {
147 	int i;
148 
149 	/* Linear search over the table */
150 	for (i = 0; i < (sizeof(ieee80211_r_names) /
151 		sizeof(ieee80211_r_names[0])); i++)
152 		if (ieee80211_r_names[i].rn_domain == regdomain)
153 			return (ieee80211_r_names[i].rn_name);
154 
155 	return (ieee80211_r_names[0].rn_name);
156 }
157 
158 u_int32_t
159 ieee80211_regdomain2flag(u_int16_t regdomain, u_int16_t mhz)
160 {
161 	int i;
162 
163 	for (i = 0; i < (sizeof(ieee80211_r_map) /
164 		sizeof(ieee80211_r_map[0])); i++) {
165 		if (ieee80211_r_map[i].rm_domain == regdomain) {
166 			if (mhz >= 2000 && mhz <= 3000)
167 				return ((u_int32_t)
168 				    ieee80211_r_map[i].rm_domain_2ghz);
169 			if (mhz >= IEEE80211_CHANNELS_5GHZ_MIN &&
170 			    mhz <= IEEE80211_CHANNELS_5GHZ_MAX)
171 				return ((u_int32_t)
172 				    ieee80211_r_map[i].rm_domain_5ghz);
173 		}
174 	}
175 
176 	return ((u_int32_t)DMN_DEBUG);
177 }
178 
179 u_int32_t
180 ieee80211_countrycode2regdomain(u_int16_t code)
181 {
182 	int i;
183 
184 	for (i = 0;
185 	     i < (sizeof(ieee80211_r_ctry) / sizeof(ieee80211_r_ctry[0])); i++)
186 		if (ieee80211_r_ctry[i].cn_code == code)
187 			return (ieee80211_r_ctry[i].cn_domain);
188 
189 	return ((u_int32_t)DMN_DEFAULT);
190 }
191