Line data Source code
1 : /* $OpenBSD: ieee80211_ioctl.c,v 1.67 2018/09/10 11:07:43 phessler Exp $ */
2 : /* $NetBSD: ieee80211_ioctl.c,v 1.15 2004/05/06 02:58:16 dyoung Exp $ */
3 :
4 : /*-
5 : * Copyright (c) 2001 Atsushi Onoe
6 : * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
7 : * All rights reserved.
8 : *
9 : * Redistribution and use in source and binary forms, with or without
10 : * modification, are permitted provided that the following conditions
11 : * are met:
12 : * 1. Redistributions of source code must retain the above copyright
13 : * notice, this list of conditions and the following disclaimer.
14 : * 2. Redistributions in binary form must reproduce the above copyright
15 : * notice, this list of conditions and the following disclaimer in the
16 : * documentation and/or other materials provided with the distribution.
17 : * 3. The name of the author may not be used to endorse or promote products
18 : * derived from this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 : * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 : */
31 :
32 : /*
33 : * IEEE 802.11 ioctl support
34 : */
35 :
36 : #include <sys/param.h>
37 : #include <sys/kernel.h>
38 : #include <sys/socket.h>
39 : #include <sys/sockio.h>
40 : #include <sys/systm.h>
41 : #include <sys/endian.h>
42 : #include <sys/tree.h>
43 :
44 : #include <net/if.h>
45 : #include <net/if_media.h>
46 :
47 : #include <netinet/in.h>
48 : #include <netinet/if_ether.h>
49 :
50 : #include <net80211/ieee80211_var.h>
51 : #include <net80211/ieee80211_crypto.h>
52 : #include <net80211/ieee80211_ioctl.h>
53 :
54 : void ieee80211_node2req(struct ieee80211com *,
55 : const struct ieee80211_node *, struct ieee80211_nodereq *);
56 : void ieee80211_req2node(struct ieee80211com *,
57 : const struct ieee80211_nodereq *, struct ieee80211_node *);
58 :
59 : void
60 0 : ieee80211_node2req(struct ieee80211com *ic, const struct ieee80211_node *ni,
61 : struct ieee80211_nodereq *nr)
62 : {
63 : uint8_t rssi;
64 :
65 0 : memset(nr, 0, sizeof(*nr));
66 :
67 0 : strlcpy(nr->nr_ifname, ic->ic_if.if_xname, sizeof(nr->nr_ifname));
68 :
69 : /* Node address and name information */
70 0 : IEEE80211_ADDR_COPY(nr->nr_macaddr, ni->ni_macaddr);
71 0 : IEEE80211_ADDR_COPY(nr->nr_bssid, ni->ni_bssid);
72 0 : nr->nr_nwid_len = ni->ni_esslen;
73 0 : bcopy(ni->ni_essid, nr->nr_nwid, IEEE80211_NWID_LEN);
74 :
75 : /* Channel and rates */
76 0 : nr->nr_channel = ieee80211_chan2ieee(ic, ni->ni_chan);
77 0 : nr->nr_chan_flags = ni->ni_chan->ic_flags;
78 0 : if (ic->ic_curmode != IEEE80211_MODE_11N)
79 0 : nr->nr_chan_flags &= ~IEEE80211_CHAN_HT;
80 0 : nr->nr_nrates = ni->ni_rates.rs_nrates;
81 0 : bcopy(ni->ni_rates.rs_rates, nr->nr_rates, IEEE80211_RATE_MAXSIZE);
82 :
83 : /* Node status information */
84 0 : rssi = (*ic->ic_node_getrssi)(ic, ni);
85 0 : if (ic->ic_max_rssi) {
86 : /* Driver reports RSSI relative to ic_max_rssi. */
87 0 : nr->nr_rssi = rssi;
88 0 : } else {
89 : /*
90 : * Driver reports RSSI value in dBm.
91 : * Convert from unsigned to signed.
92 : * Some drivers report a negative value, some don't.
93 : * Reasonable range is -20dBm to -80dBm.
94 : */
95 0 : nr->nr_rssi = (rssi < 128) ? -rssi : rssi;
96 : }
97 0 : nr->nr_max_rssi = ic->ic_max_rssi;
98 0 : bcopy(ni->ni_tstamp, nr->nr_tstamp, sizeof(nr->nr_tstamp));
99 0 : nr->nr_intval = ni->ni_intval;
100 0 : nr->nr_capinfo = ni->ni_capinfo;
101 0 : nr->nr_erp = ni->ni_erp;
102 0 : nr->nr_pwrsave = ni->ni_pwrsave;
103 0 : nr->nr_associd = ni->ni_associd;
104 0 : nr->nr_txseq = ni->ni_txseq;
105 0 : nr->nr_rxseq = ni->ni_rxseq;
106 0 : nr->nr_fails = ni->ni_fails;
107 0 : nr->nr_inact = ni->ni_inact;
108 0 : nr->nr_txrate = ni->ni_txrate;
109 0 : nr->nr_state = ni->ni_state;
110 :
111 : /* RSN */
112 0 : nr->nr_rsnciphers = ni->ni_rsnciphers;
113 0 : nr->nr_rsnakms = 0;
114 0 : nr->nr_rsnprotos = 0;
115 0 : if (ni->ni_supported_rsnprotos & IEEE80211_PROTO_RSN)
116 0 : nr->nr_rsnprotos |= IEEE80211_WPA_PROTO_WPA2;
117 0 : if (ni->ni_supported_rsnprotos & IEEE80211_PROTO_WPA)
118 0 : nr->nr_rsnprotos |= IEEE80211_WPA_PROTO_WPA1;
119 0 : if (ni->ni_supported_rsnakms & IEEE80211_AKM_8021X)
120 0 : nr->nr_rsnakms |= IEEE80211_WPA_AKM_8021X;
121 0 : if (ni->ni_supported_rsnakms & IEEE80211_AKM_PSK)
122 0 : nr->nr_rsnakms |= IEEE80211_WPA_AKM_PSK;
123 0 : if (ni->ni_supported_rsnakms & IEEE80211_AKM_SHA256_8021X)
124 0 : nr->nr_rsnakms |= IEEE80211_WPA_AKM_SHA256_8021X;
125 0 : if (ni->ni_supported_rsnakms & IEEE80211_AKM_SHA256_PSK)
126 0 : nr->nr_rsnakms |= IEEE80211_WPA_AKM_SHA256_PSK;
127 :
128 : /* Node flags */
129 0 : nr->nr_flags = 0;
130 0 : if (bcmp(nr->nr_macaddr, nr->nr_bssid, IEEE80211_ADDR_LEN) == 0)
131 0 : nr->nr_flags |= IEEE80211_NODEREQ_AP;
132 0 : if (ni == ic->ic_bss)
133 0 : nr->nr_flags |= IEEE80211_NODEREQ_AP_BSS;
134 :
135 : /* HT */
136 0 : nr->nr_htcaps = ni->ni_htcaps;
137 0 : memcpy(nr->nr_rxmcs, ni->ni_rxmcs, sizeof(nr->nr_rxmcs));
138 0 : nr->nr_max_rxrate = ni->ni_max_rxrate;
139 0 : nr->nr_tx_mcs_set = ni->ni_tx_mcs_set;
140 0 : nr->nr_txmcs = ni->ni_txmcs;
141 0 : if (ni->ni_flags & IEEE80211_NODE_HT)
142 0 : nr->nr_flags |= IEEE80211_NODEREQ_HT;
143 0 : }
144 :
145 : void
146 0 : ieee80211_req2node(struct ieee80211com *ic, const struct ieee80211_nodereq *nr,
147 : struct ieee80211_node *ni)
148 : {
149 : /* Node address and name information */
150 0 : IEEE80211_ADDR_COPY(ni->ni_macaddr, nr->nr_macaddr);
151 0 : IEEE80211_ADDR_COPY(ni->ni_bssid, nr->nr_bssid);
152 0 : ni->ni_esslen = nr->nr_nwid_len;
153 0 : bcopy(nr->nr_nwid, ni->ni_essid, IEEE80211_NWID_LEN);
154 :
155 : /* Rates */
156 0 : ni->ni_rates.rs_nrates = nr->nr_nrates;
157 0 : bcopy(nr->nr_rates, ni->ni_rates.rs_rates, IEEE80211_RATE_MAXSIZE);
158 :
159 : /* Node information */
160 0 : ni->ni_intval = nr->nr_intval;
161 0 : ni->ni_capinfo = nr->nr_capinfo;
162 0 : ni->ni_erp = nr->nr_erp;
163 0 : ni->ni_pwrsave = nr->nr_pwrsave;
164 0 : ni->ni_associd = nr->nr_associd;
165 0 : ni->ni_txseq = nr->nr_txseq;
166 0 : ni->ni_rxseq = nr->nr_rxseq;
167 0 : ni->ni_fails = nr->nr_fails;
168 0 : ni->ni_inact = nr->nr_inact;
169 0 : ni->ni_txrate = nr->nr_txrate;
170 0 : ni->ni_state = nr->nr_state;
171 0 : }
172 :
173 : void
174 0 : ieee80211_disable_wep(struct ieee80211com *ic)
175 : {
176 : struct ieee80211_key *k;
177 : int i;
178 :
179 0 : for (i = 0; i < IEEE80211_WEP_NKID; i++) {
180 0 : k = &ic->ic_nw_keys[i];
181 0 : if (k->k_cipher != IEEE80211_CIPHER_NONE)
182 0 : (*ic->ic_delete_key)(ic, NULL, k);
183 0 : explicit_bzero(k, sizeof(*k));
184 : }
185 0 : ic->ic_flags &= ~IEEE80211_F_WEPON;
186 0 : }
187 :
188 : void
189 0 : ieee80211_disable_rsn(struct ieee80211com *ic)
190 : {
191 0 : ic->ic_flags &= ~(IEEE80211_F_PSK | IEEE80211_F_RSNON);
192 0 : explicit_bzero(ic->ic_psk, sizeof(ic->ic_psk));
193 0 : ic->ic_rsnprotos = 0;
194 0 : ic->ic_rsnakms = 0;
195 0 : ic->ic_rsngroupcipher = 0;
196 0 : ic->ic_rsnciphers = 0;
197 0 : }
198 :
199 : /* Keep in sync with ieee80211_node.c:ieee80211_ess_setnwkeys() */
200 : static int
201 0 : ieee80211_ioctl_setnwkeys(struct ieee80211com *ic,
202 : const struct ieee80211_nwkey *nwkey)
203 : {
204 : struct ieee80211_key *k;
205 : int error, i;
206 :
207 0 : if (!(ic->ic_caps & IEEE80211_C_WEP))
208 0 : return ENODEV;
209 :
210 0 : if (nwkey->i_wepon == IEEE80211_NWKEY_OPEN) {
211 0 : if (!(ic->ic_flags & IEEE80211_F_WEPON))
212 0 : return 0;
213 0 : ic->ic_flags &= ~IEEE80211_F_WEPON;
214 0 : return ENETRESET;
215 : }
216 0 : if (nwkey->i_defkid < 1 || nwkey->i_defkid > IEEE80211_WEP_NKID)
217 0 : return EINVAL;
218 :
219 0 : for (i = 0; i < IEEE80211_WEP_NKID; i++) {
220 0 : if (nwkey->i_key[i].i_keylen == 0 ||
221 0 : nwkey->i_key[i].i_keydat == NULL)
222 : continue; /* entry not set */
223 0 : if (nwkey->i_key[i].i_keylen > IEEE80211_KEYBUF_SIZE)
224 0 : return EINVAL;
225 :
226 : /* map wep key to ieee80211_key */
227 0 : k = &ic->ic_nw_keys[i];
228 0 : if (k->k_cipher != IEEE80211_CIPHER_NONE)
229 0 : (*ic->ic_delete_key)(ic, NULL, k);
230 0 : memset(k, 0, sizeof(*k));
231 0 : if (nwkey->i_key[i].i_keylen <= 5)
232 0 : k->k_cipher = IEEE80211_CIPHER_WEP40;
233 : else
234 0 : k->k_cipher = IEEE80211_CIPHER_WEP104;
235 0 : k->k_len = ieee80211_cipher_keylen(k->k_cipher);
236 0 : k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX;
237 0 : error = copyin(nwkey->i_key[i].i_keydat, k->k_key, k->k_len);
238 0 : if (error != 0)
239 0 : return error;
240 0 : if ((error = (*ic->ic_set_key)(ic, NULL, k)) != 0)
241 0 : return error;
242 : }
243 :
244 0 : ic->ic_def_txkey = nwkey->i_defkid - 1;
245 0 : ic->ic_flags |= IEEE80211_F_WEPON;
246 0 : if (ic->ic_flags & IEEE80211_F_RSNON)
247 0 : ieee80211_disable_rsn(ic);
248 :
249 0 : return ENETRESET;
250 0 : }
251 :
252 : static int
253 0 : ieee80211_ioctl_getnwkeys(struct ieee80211com *ic,
254 : struct ieee80211_nwkey *nwkey)
255 : {
256 : int i;
257 :
258 0 : if (ic->ic_flags & IEEE80211_F_WEPON)
259 0 : nwkey->i_wepon = IEEE80211_NWKEY_WEP;
260 : else
261 0 : nwkey->i_wepon = IEEE80211_NWKEY_OPEN;
262 :
263 0 : nwkey->i_defkid = ic->ic_wep_txkey + 1;
264 :
265 0 : for (i = 0; i < IEEE80211_WEP_NKID; i++) {
266 0 : if (nwkey->i_key[i].i_keydat == NULL)
267 : continue;
268 : /* do not show any keys to userland */
269 0 : return EPERM;
270 : }
271 0 : return 0;
272 0 : }
273 :
274 : /* Keep in sync with ieee80211_node.c:ieee80211_ess_setwpaparms() */
275 : static int
276 0 : ieee80211_ioctl_setwpaparms(struct ieee80211com *ic,
277 : const struct ieee80211_wpaparams *wpa)
278 : {
279 0 : if (!(ic->ic_caps & IEEE80211_C_RSN))
280 0 : return ENODEV;
281 :
282 0 : if (!wpa->i_enabled) {
283 0 : if (!(ic->ic_flags & IEEE80211_F_RSNON))
284 0 : return 0;
285 0 : ic->ic_flags &= ~IEEE80211_F_RSNON;
286 0 : ic->ic_rsnprotos = 0;
287 0 : ic->ic_rsnakms = 0;
288 0 : ic->ic_rsngroupcipher = 0;
289 0 : ic->ic_rsnciphers = 0;
290 0 : return ENETRESET;
291 : }
292 :
293 0 : ic->ic_rsnprotos = 0;
294 0 : if (wpa->i_protos & IEEE80211_WPA_PROTO_WPA1)
295 0 : ic->ic_rsnprotos |= IEEE80211_PROTO_WPA;
296 0 : if (wpa->i_protos & IEEE80211_WPA_PROTO_WPA2)
297 0 : ic->ic_rsnprotos |= IEEE80211_PROTO_RSN;
298 0 : if (ic->ic_rsnprotos == 0) /* set to default (RSN) */
299 0 : ic->ic_rsnprotos = IEEE80211_PROTO_RSN;
300 :
301 0 : ic->ic_rsnakms = 0;
302 0 : if (wpa->i_akms & IEEE80211_WPA_AKM_PSK)
303 0 : ic->ic_rsnakms |= IEEE80211_AKM_PSK;
304 0 : if (wpa->i_akms & IEEE80211_WPA_AKM_SHA256_PSK)
305 0 : ic->ic_rsnakms |= IEEE80211_AKM_SHA256_PSK;
306 0 : if (wpa->i_akms & IEEE80211_WPA_AKM_8021X)
307 0 : ic->ic_rsnakms |= IEEE80211_AKM_8021X;
308 0 : if (wpa->i_akms & IEEE80211_WPA_AKM_SHA256_8021X)
309 0 : ic->ic_rsnakms |= IEEE80211_AKM_SHA256_8021X;
310 0 : if (ic->ic_rsnakms == 0) /* set to default (PSK) */
311 0 : ic->ic_rsnakms = IEEE80211_AKM_PSK;
312 :
313 0 : if (wpa->i_groupcipher == IEEE80211_WPA_CIPHER_WEP40)
314 0 : ic->ic_rsngroupcipher = IEEE80211_CIPHER_WEP40;
315 0 : else if (wpa->i_groupcipher == IEEE80211_WPA_CIPHER_TKIP)
316 0 : ic->ic_rsngroupcipher = IEEE80211_CIPHER_TKIP;
317 0 : else if (wpa->i_groupcipher == IEEE80211_WPA_CIPHER_CCMP)
318 0 : ic->ic_rsngroupcipher = IEEE80211_CIPHER_CCMP;
319 0 : else if (wpa->i_groupcipher == IEEE80211_WPA_CIPHER_WEP104)
320 0 : ic->ic_rsngroupcipher = IEEE80211_CIPHER_WEP104;
321 : else { /* set to default */
322 0 : if (ic->ic_rsnprotos & IEEE80211_PROTO_WPA)
323 0 : ic->ic_rsngroupcipher = IEEE80211_CIPHER_TKIP;
324 : else
325 0 : ic->ic_rsngroupcipher = IEEE80211_CIPHER_CCMP;
326 : }
327 :
328 0 : ic->ic_rsnciphers = 0;
329 0 : if (wpa->i_ciphers & IEEE80211_WPA_CIPHER_TKIP)
330 0 : ic->ic_rsnciphers |= IEEE80211_CIPHER_TKIP;
331 0 : if (wpa->i_ciphers & IEEE80211_WPA_CIPHER_CCMP)
332 0 : ic->ic_rsnciphers |= IEEE80211_CIPHER_CCMP;
333 0 : if (wpa->i_ciphers & IEEE80211_WPA_CIPHER_USEGROUP)
334 0 : ic->ic_rsnciphers = IEEE80211_CIPHER_USEGROUP;
335 0 : if (ic->ic_rsnciphers == 0) { /* set to default (CCMP, TKIP if WPA1) */
336 0 : ic->ic_rsnciphers = IEEE80211_CIPHER_CCMP;
337 0 : if (ic->ic_rsnprotos & IEEE80211_PROTO_WPA)
338 0 : ic->ic_rsnciphers |= IEEE80211_CIPHER_TKIP;
339 : }
340 :
341 0 : ic->ic_flags |= IEEE80211_F_RSNON;
342 :
343 0 : return ENETRESET;
344 0 : }
345 :
346 : static int
347 0 : ieee80211_ioctl_getwpaparms(struct ieee80211com *ic,
348 : struct ieee80211_wpaparams *wpa)
349 : {
350 0 : wpa->i_enabled = (ic->ic_flags & IEEE80211_F_RSNON) ? 1 : 0;
351 :
352 0 : wpa->i_protos = 0;
353 0 : if (ic->ic_rsnprotos & IEEE80211_PROTO_WPA)
354 0 : wpa->i_protos |= IEEE80211_WPA_PROTO_WPA1;
355 0 : if (ic->ic_rsnprotos & IEEE80211_PROTO_RSN)
356 0 : wpa->i_protos |= IEEE80211_WPA_PROTO_WPA2;
357 :
358 0 : wpa->i_akms = 0;
359 0 : if (ic->ic_rsnakms & IEEE80211_AKM_PSK)
360 0 : wpa->i_akms |= IEEE80211_WPA_AKM_PSK;
361 0 : if (ic->ic_rsnakms & IEEE80211_AKM_SHA256_PSK)
362 0 : wpa->i_akms |= IEEE80211_WPA_AKM_SHA256_PSK;
363 0 : if (ic->ic_rsnakms & IEEE80211_AKM_8021X)
364 0 : wpa->i_akms |= IEEE80211_WPA_AKM_8021X;
365 0 : if (ic->ic_rsnakms & IEEE80211_AKM_SHA256_8021X)
366 0 : wpa->i_akms |= IEEE80211_WPA_AKM_SHA256_8021X;
367 :
368 0 : if (ic->ic_rsngroupcipher == IEEE80211_CIPHER_WEP40)
369 0 : wpa->i_groupcipher = IEEE80211_WPA_CIPHER_WEP40;
370 0 : else if (ic->ic_rsngroupcipher == IEEE80211_CIPHER_TKIP)
371 0 : wpa->i_groupcipher = IEEE80211_WPA_CIPHER_TKIP;
372 0 : else if (ic->ic_rsngroupcipher == IEEE80211_CIPHER_CCMP)
373 0 : wpa->i_groupcipher = IEEE80211_WPA_CIPHER_CCMP;
374 0 : else if (ic->ic_rsngroupcipher == IEEE80211_CIPHER_WEP104)
375 0 : wpa->i_groupcipher = IEEE80211_WPA_CIPHER_WEP104;
376 : else
377 0 : wpa->i_groupcipher = IEEE80211_WPA_CIPHER_NONE;
378 :
379 0 : wpa->i_ciphers = 0;
380 0 : if (ic->ic_rsnciphers & IEEE80211_CIPHER_TKIP)
381 0 : wpa->i_ciphers |= IEEE80211_WPA_CIPHER_TKIP;
382 0 : if (ic->ic_rsnciphers & IEEE80211_CIPHER_CCMP)
383 0 : wpa->i_ciphers |= IEEE80211_WPA_CIPHER_CCMP;
384 0 : if (ic->ic_rsnciphers & IEEE80211_CIPHER_USEGROUP)
385 0 : wpa->i_ciphers = IEEE80211_WPA_CIPHER_USEGROUP;
386 :
387 0 : return 0;
388 : }
389 :
390 : int
391 0 : ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
392 : {
393 0 : struct ieee80211com *ic = (void *)ifp;
394 0 : struct ifreq *ifr = (struct ifreq *)data;
395 : int i, error = 0;
396 : size_t len;
397 0 : struct ieee80211_nwid nwid;
398 0 : struct ieee80211_join join;
399 : struct ieee80211_joinreq_all *ja;
400 : struct ieee80211_ess *ess;
401 : struct ieee80211_wpapsk *psk;
402 : struct ieee80211_keyavail *ka;
403 : struct ieee80211_keyrun *kr;
404 : struct ieee80211_power *power;
405 : struct ieee80211_bssid *bssid;
406 : struct ieee80211chanreq *chanreq;
407 : struct ieee80211_channel *chan;
408 : struct ieee80211_txpower *txpower;
409 : static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
410 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
411 : };
412 0 : struct ieee80211_nodereq *nr, nrbuf;
413 : struct ieee80211_nodereq_all *na;
414 : struct ieee80211_node *ni;
415 : u_int32_t flags;
416 :
417 0 : switch (cmd) {
418 : case SIOCSIFMEDIA:
419 : case SIOCGIFMEDIA:
420 0 : error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
421 0 : break;
422 : case SIOCS80211NWID:
423 0 : if ((error = suser(curproc)) != 0)
424 : break;
425 0 : if ((error = copyin(ifr->ifr_data, &nwid, sizeof(nwid))) != 0)
426 : break;
427 0 : if (nwid.i_len > IEEE80211_NWID_LEN) {
428 : error = EINVAL;
429 0 : break;
430 : }
431 0 : memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
432 0 : ic->ic_des_esslen = nwid.i_len;
433 0 : memcpy(ic->ic_des_essid, nwid.i_nwid, nwid.i_len);
434 0 : if (ic->ic_des_esslen > 0) {
435 : /* 'nwid' disables auto-join magic */
436 0 : ic->ic_flags &= ~IEEE80211_F_AUTO_JOIN;
437 0 : } else {
438 : /* '-nwid' re-enables auto-join */
439 0 : ic->ic_flags |= IEEE80211_F_AUTO_JOIN;
440 : }
441 : /* disable WPA/WEP */
442 0 : ieee80211_disable_rsn(ic);
443 0 : ieee80211_disable_wep(ic);
444 : error = ENETRESET;
445 0 : break;
446 : case SIOCG80211NWID:
447 0 : memset(&nwid, 0, sizeof(nwid));
448 0 : switch (ic->ic_state) {
449 : case IEEE80211_S_INIT:
450 : case IEEE80211_S_SCAN:
451 0 : nwid.i_len = ic->ic_des_esslen;
452 0 : memcpy(nwid.i_nwid, ic->ic_des_essid, nwid.i_len);
453 0 : break;
454 : default:
455 0 : nwid.i_len = ic->ic_bss->ni_esslen;
456 0 : memcpy(nwid.i_nwid, ic->ic_bss->ni_essid, nwid.i_len);
457 0 : break;
458 : }
459 0 : error = copyout(&nwid, ifr->ifr_data, sizeof(nwid));
460 0 : break;
461 : case SIOCS80211JOIN:
462 0 : if ((error = suser(curproc)) != 0)
463 : break;
464 0 : if ((error = copyin(ifr->ifr_data, &join, sizeof(join))) != 0)
465 : break;
466 0 : if (join.i_len > IEEE80211_NWID_LEN) {
467 : error = EINVAL;
468 0 : break;
469 : }
470 0 : if (join.i_flags & IEEE80211_JOIN_DEL)
471 0 : ieee80211_del_ess(ic, join.i_nwid, join.i_len ? 0 : 1);
472 :
473 : /* save nwid for auto-join */
474 0 : if (!(join.i_flags & IEEE80211_JOIN_DEL)) {
475 0 : if (ieee80211_add_ess(ic, &join) == 0)
476 0 : ic->ic_flags |= IEEE80211_F_AUTO_JOIN;
477 : }
478 : break;
479 : case SIOCG80211JOIN:
480 0 : memset(&join, 0, sizeof(join));
481 : error = ENOENT;
482 0 : if (ic->ic_bss == NULL)
483 : break;
484 0 : TAILQ_FOREACH(ess, &ic->ic_ess, ess_next) {
485 0 : if (memcmp(ess->essid, ic->ic_bss->ni_essid,
486 0 : IEEE80211_NWID_LEN) == 0) {
487 0 : join.i_len = ic->ic_bss->ni_esslen;
488 0 : memcpy(join.i_nwid, ic->ic_bss->ni_essid,
489 : join.i_len);
490 0 : if (ic->ic_flags & IEEE80211_F_AUTO_JOIN)
491 0 : join.i_flags = IEEE80211_JOIN_FOUND;
492 0 : error = copyout(&join, ifr->ifr_data,
493 : sizeof(join));
494 0 : break;
495 : }
496 : }
497 : break;
498 : case SIOCG80211JOINALL:
499 0 : ja = (struct ieee80211_joinreq_all *)data;
500 0 : ja->ja_nodes = len = 0;
501 0 : TAILQ_FOREACH(ess, &ic->ic_ess, ess_next) {
502 0 : if (len + sizeof(ja->ja_node[0]) >= ja->ja_size) {
503 : error = E2BIG;
504 0 : break;
505 : }
506 0 : memset(&join, 0, sizeof(join));
507 0 : join.i_len = ess->esslen;
508 0 : memcpy(&join.i_nwid, ess->essid, join.i_len);
509 0 : error = copyout(&join, &ja->ja_node[ja->ja_nodes],
510 : sizeof(ja->ja_node[0]));
511 0 : if (error)
512 : break;
513 : len += sizeof(join);
514 0 : ja->ja_nodes++;
515 : }
516 : break;
517 : case SIOCS80211NWKEY:
518 0 : if ((error = suser(curproc)) != 0)
519 : break;
520 0 : error = ieee80211_ioctl_setnwkeys(ic, (void *)data);
521 0 : break;
522 : case SIOCG80211NWKEY:
523 0 : error = ieee80211_ioctl_getnwkeys(ic, (void *)data);
524 0 : break;
525 : case SIOCS80211WPAPARMS:
526 0 : if ((error = suser(curproc)) != 0)
527 : break;
528 0 : error = ieee80211_ioctl_setwpaparms(ic, (void *)data);
529 0 : break;
530 : case SIOCG80211WPAPARMS:
531 0 : error = ieee80211_ioctl_getwpaparms(ic, (void *)data);
532 0 : break;
533 : case SIOCS80211WPAPSK:
534 0 : if ((error = suser(curproc)) != 0)
535 : break;
536 0 : psk = (struct ieee80211_wpapsk *)data;
537 0 : if (psk->i_enabled) {
538 0 : ic->ic_flags |= IEEE80211_F_PSK;
539 0 : memcpy(ic->ic_psk, psk->i_psk, sizeof(ic->ic_psk));
540 0 : if (ic->ic_flags & IEEE80211_F_WEPON)
541 0 : ieee80211_disable_wep(ic);
542 : } else {
543 0 : ic->ic_flags &= ~IEEE80211_F_PSK;
544 0 : memset(ic->ic_psk, 0, sizeof(ic->ic_psk));
545 : }
546 : error = ENETRESET;
547 0 : break;
548 : case SIOCG80211WPAPSK:
549 0 : psk = (struct ieee80211_wpapsk *)data;
550 0 : if (ic->ic_flags & IEEE80211_F_PSK) {
551 : /* do not show any keys to userland */
552 0 : psk->i_enabled = 2;
553 0 : memset(psk->i_psk, 0, sizeof(psk->i_psk));
554 0 : break; /* return ok but w/o key */
555 : } else
556 0 : psk->i_enabled = 0;
557 0 : break;
558 : case SIOCS80211KEYAVAIL:
559 0 : if ((error = suser(curproc)) != 0)
560 : break;
561 0 : ka = (struct ieee80211_keyavail *)data;
562 0 : (void)ieee80211_pmksa_add(ic, IEEE80211_AKM_8021X,
563 0 : ka->i_macaddr, ka->i_key, ka->i_lifetime);
564 0 : break;
565 : case SIOCS80211KEYRUN:
566 0 : if ((error = suser(curproc)) != 0)
567 : break;
568 0 : kr = (struct ieee80211_keyrun *)data;
569 0 : error = ieee80211_keyrun(ic, kr->i_macaddr);
570 0 : if (error == 0 && (ic->ic_flags & IEEE80211_F_WEPON))
571 0 : ieee80211_disable_wep(ic);
572 : break;
573 : case SIOCS80211POWER:
574 0 : if ((error = suser(curproc)) != 0)
575 : break;
576 0 : power = (struct ieee80211_power *)data;
577 0 : ic->ic_lintval = power->i_maxsleep;
578 0 : if (power->i_enabled != 0) {
579 0 : if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
580 0 : error = EINVAL;
581 0 : else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
582 0 : ic->ic_flags |= IEEE80211_F_PMGTON;
583 : error = ENETRESET;
584 0 : }
585 : } else {
586 0 : if (ic->ic_flags & IEEE80211_F_PMGTON) {
587 0 : ic->ic_flags &= ~IEEE80211_F_PMGTON;
588 : error = ENETRESET;
589 0 : }
590 : }
591 : break;
592 : case SIOCG80211POWER:
593 0 : power = (struct ieee80211_power *)data;
594 0 : power->i_enabled = (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0;
595 0 : power->i_maxsleep = ic->ic_lintval;
596 0 : break;
597 : case SIOCS80211BSSID:
598 0 : if ((error = suser(curproc)) != 0)
599 : break;
600 0 : bssid = (struct ieee80211_bssid *)data;
601 0 : if (IEEE80211_ADDR_EQ(bssid->i_bssid, empty_macaddr))
602 0 : ic->ic_flags &= ~IEEE80211_F_DESBSSID;
603 : else {
604 0 : ic->ic_flags |= IEEE80211_F_DESBSSID;
605 0 : IEEE80211_ADDR_COPY(ic->ic_des_bssid, bssid->i_bssid);
606 : }
607 : #ifndef IEEE80211_STA_ONLY
608 0 : if (ic->ic_opmode == IEEE80211_M_HOSTAP)
609 : break;
610 : #endif
611 0 : switch (ic->ic_state) {
612 : case IEEE80211_S_INIT:
613 : case IEEE80211_S_SCAN:
614 : error = ENETRESET;
615 0 : break;
616 : default:
617 0 : if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
618 0 : !IEEE80211_ADDR_EQ(ic->ic_des_bssid,
619 : ic->ic_bss->ni_bssid))
620 0 : error = ENETRESET;
621 : break;
622 : }
623 : break;
624 : case SIOCG80211BSSID:
625 0 : bssid = (struct ieee80211_bssid *)data;
626 0 : switch (ic->ic_state) {
627 : case IEEE80211_S_INIT:
628 : case IEEE80211_S_SCAN:
629 : #ifndef IEEE80211_STA_ONLY
630 0 : if (ic->ic_opmode == IEEE80211_M_HOSTAP)
631 0 : IEEE80211_ADDR_COPY(bssid->i_bssid,
632 : ic->ic_myaddr);
633 : else
634 : #endif
635 0 : if (ic->ic_flags & IEEE80211_F_DESBSSID)
636 0 : IEEE80211_ADDR_COPY(bssid->i_bssid,
637 : ic->ic_des_bssid);
638 : else
639 0 : memset(bssid->i_bssid, 0, IEEE80211_ADDR_LEN);
640 : break;
641 : default:
642 0 : IEEE80211_ADDR_COPY(bssid->i_bssid,
643 : ic->ic_bss->ni_bssid);
644 0 : break;
645 : }
646 : break;
647 : case SIOCS80211CHANNEL:
648 0 : if ((error = suser(curproc)) != 0)
649 : break;
650 0 : chanreq = (struct ieee80211chanreq *)data;
651 0 : if (chanreq->i_channel == IEEE80211_CHAN_ANY)
652 0 : ic->ic_des_chan = IEEE80211_CHAN_ANYC;
653 0 : else if (chanreq->i_channel > IEEE80211_CHAN_MAX ||
654 0 : isclr(ic->ic_chan_active, chanreq->i_channel)) {
655 : error = EINVAL;
656 0 : break;
657 : } else
658 0 : ic->ic_ibss_chan = ic->ic_des_chan =
659 0 : &ic->ic_channels[chanreq->i_channel];
660 0 : switch (ic->ic_state) {
661 : case IEEE80211_S_INIT:
662 : case IEEE80211_S_SCAN:
663 : error = ENETRESET;
664 0 : break;
665 : default:
666 0 : if (ic->ic_opmode == IEEE80211_M_STA) {
667 0 : if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
668 0 : ic->ic_bss->ni_chan != ic->ic_des_chan)
669 0 : error = ENETRESET;
670 : } else {
671 0 : if (ic->ic_bss->ni_chan != ic->ic_ibss_chan)
672 0 : error = ENETRESET;
673 : }
674 : break;
675 : }
676 : break;
677 : case SIOCG80211CHANNEL:
678 0 : chanreq = (struct ieee80211chanreq *)data;
679 0 : switch (ic->ic_state) {
680 : case IEEE80211_S_INIT:
681 : case IEEE80211_S_SCAN:
682 0 : if (ic->ic_opmode == IEEE80211_M_STA)
683 0 : chan = ic->ic_des_chan;
684 : else
685 0 : chan = ic->ic_ibss_chan;
686 : break;
687 : default:
688 0 : chan = ic->ic_bss->ni_chan;
689 0 : break;
690 : }
691 0 : chanreq->i_channel = ieee80211_chan2ieee(ic, chan);
692 0 : break;
693 : case SIOCG80211ALLCHANS:
694 0 : error = copyout(ic->ic_channels,
695 0 : ((struct ieee80211_chanreq_all *)data)->i_chans,
696 : sizeof(ic->ic_channels));
697 0 : break;
698 : #if 0
699 : case SIOCG80211ZSTATS:
700 : #endif
701 : case SIOCG80211STATS:
702 : ifr = (struct ifreq *)data;
703 0 : error = copyout(&ic->ic_stats, ifr->ifr_data,
704 : sizeof(ic->ic_stats));
705 : #if 0
706 : if (cmd == SIOCG80211ZSTATS)
707 : memset(&ic->ic_stats, 0, sizeof(ic->ic_stats));
708 : #endif
709 0 : break;
710 : case SIOCS80211TXPOWER:
711 0 : if ((error = suser(curproc)) != 0)
712 : break;
713 0 : txpower = (struct ieee80211_txpower *)data;
714 0 : if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0) {
715 : error = EINVAL;
716 0 : break;
717 : }
718 0 : if (!(IEEE80211_TXPOWER_MIN <= txpower->i_val &&
719 0 : txpower->i_val <= IEEE80211_TXPOWER_MAX)) {
720 : error = EINVAL;
721 0 : break;
722 : }
723 0 : ic->ic_txpower = txpower->i_val;
724 : error = ENETRESET;
725 0 : break;
726 : case SIOCG80211TXPOWER:
727 0 : txpower = (struct ieee80211_txpower *)data;
728 0 : if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
729 0 : error = EINVAL;
730 : else
731 0 : txpower->i_val = ic->ic_txpower;
732 : break;
733 : case SIOCSIFMTU:
734 : ifr = (struct ifreq *)data;
735 0 : if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
736 0 : ifr->ifr_mtu <= IEEE80211_MTU_MAX))
737 0 : error = EINVAL;
738 : else
739 0 : ifp->if_mtu = ifr->ifr_mtu;
740 : break;
741 : case SIOCS80211SCAN:
742 : /* Disabled. SIOCG80211ALLNODES is enough. */
743 : break;
744 : case SIOCG80211NODE:
745 0 : nr = (struct ieee80211_nodereq *)data;
746 0 : ni = ieee80211_find_node(ic, nr->nr_macaddr);
747 0 : if (ni == NULL) {
748 : error = ENOENT;
749 0 : break;
750 : }
751 0 : ieee80211_node2req(ic, ni, nr);
752 0 : break;
753 : case SIOCS80211NODE:
754 0 : if ((error = suser(curproc)) != 0)
755 : break;
756 : #ifndef IEEE80211_STA_ONLY
757 0 : if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
758 : error = EINVAL;
759 0 : break;
760 : }
761 : #endif
762 0 : nr = (struct ieee80211_nodereq *)data;
763 :
764 0 : ni = ieee80211_find_node(ic, nr->nr_macaddr);
765 0 : if (ni == NULL)
766 0 : ni = ieee80211_alloc_node(ic, nr->nr_macaddr);
767 0 : if (ni == NULL) {
768 : error = ENOENT;
769 0 : break;
770 : }
771 :
772 0 : if (nr->nr_flags & IEEE80211_NODEREQ_COPY)
773 0 : ieee80211_req2node(ic, nr, ni);
774 : break;
775 : #ifndef IEEE80211_STA_ONLY
776 : case SIOCS80211DELNODE:
777 0 : if ((error = suser(curproc)) != 0)
778 : break;
779 0 : nr = (struct ieee80211_nodereq *)data;
780 0 : ni = ieee80211_find_node(ic, nr->nr_macaddr);
781 0 : if (ni == NULL)
782 0 : error = ENOENT;
783 0 : else if (ni == ic->ic_bss)
784 0 : error = EPERM;
785 : else {
786 0 : if (ni->ni_state == IEEE80211_STA_COLLECT)
787 : break;
788 :
789 : /* Disassociate station. */
790 0 : if (ni->ni_state == IEEE80211_STA_ASSOC)
791 0 : IEEE80211_SEND_MGMT(ic, ni,
792 : IEEE80211_FC0_SUBTYPE_DISASSOC,
793 : IEEE80211_REASON_ASSOC_LEAVE);
794 :
795 : /* Deauth station. */
796 0 : if (ni->ni_state >= IEEE80211_STA_AUTH)
797 0 : IEEE80211_SEND_MGMT(ic, ni,
798 : IEEE80211_FC0_SUBTYPE_DEAUTH,
799 : IEEE80211_REASON_AUTH_LEAVE);
800 :
801 0 : ieee80211_node_leave(ic, ni);
802 : }
803 : break;
804 : #endif
805 : case SIOCG80211ALLNODES:
806 0 : if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
807 : (IFF_UP | IFF_RUNNING)) {
808 : error = ENETDOWN;
809 0 : break;
810 : }
811 :
812 0 : na = (struct ieee80211_nodereq_all *)data;
813 0 : na->na_nodes = i = 0;
814 0 : ni = RBT_MIN(ieee80211_tree, &ic->ic_tree);
815 0 : while (ni && na->na_size >=
816 0 : i + sizeof(struct ieee80211_nodereq)) {
817 0 : ieee80211_node2req(ic, ni, &nrbuf);
818 0 : error = copyout(&nrbuf, (caddr_t)na->na_node + i,
819 : sizeof(struct ieee80211_nodereq));
820 0 : if (error)
821 : break;
822 0 : i += sizeof(struct ieee80211_nodereq);
823 0 : na->na_nodes++;
824 0 : ni = RBT_NEXT(ieee80211_tree, ni);
825 : }
826 : break;
827 : case SIOCG80211FLAGS:
828 0 : flags = ic->ic_flags;
829 : #ifndef IEEE80211_STA_ONLY
830 0 : if (ic->ic_opmode != IEEE80211_M_HOSTAP)
831 : #endif
832 0 : flags &= ~IEEE80211_F_HOSTAPMASK;
833 0 : ifr->ifr_flags = flags >> IEEE80211_F_USERSHIFT;
834 0 : break;
835 : case SIOCS80211FLAGS:
836 0 : if ((error = suser(curproc)) != 0)
837 : break;
838 0 : flags = (u_int32_t)ifr->ifr_flags << IEEE80211_F_USERSHIFT;
839 : if (
840 : #ifndef IEEE80211_STA_ONLY
841 0 : ic->ic_opmode != IEEE80211_M_HOSTAP &&
842 : #endif
843 0 : (flags & IEEE80211_F_HOSTAPMASK)) {
844 : error = EINVAL;
845 0 : break;
846 : }
847 0 : ic->ic_flags = (ic->ic_flags & ~IEEE80211_F_USERMASK) | flags;
848 : error = ENETRESET;
849 0 : break;
850 : case SIOCADDMULTI:
851 : case SIOCDELMULTI:
852 0 : error = (cmd == SIOCADDMULTI) ?
853 0 : ether_addmulti(ifr, &ic->ic_ac) :
854 0 : ether_delmulti(ifr, &ic->ic_ac);
855 0 : if (error == ENETRESET)
856 : error = 0;
857 0 : break;
858 : default:
859 0 : error = ether_ioctl(ifp, &ic->ic_ac, cmd, data);
860 0 : }
861 :
862 0 : return error;
863 0 : }
|