xref: /dragonfly/sys/netproto/802_11/wlan/ieee80211_proto.c (revision dd3f3f080b7c394141ceedf89c0c6a15e29f9203)
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
4  * Copyright (c) 2012 IEEE
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * IEEE 802.11 protocol support.
33  */
34 
35 #include "opt_inet.h"
36 #include "opt_wlan.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_media.h>
49 #include <net/ethernet.h>               /* XXX for ether_sprintf */
50 
51 #if defined(__DragonFly__)
52 #include <net/ifq_var.h>
53 #endif
54 
55 #include <netproto/802_11/ieee80211_var.h>
56 #include <netproto/802_11/ieee80211_adhoc.h>
57 #include <netproto/802_11/ieee80211_sta.h>
58 #include <netproto/802_11/ieee80211_hostap.h>
59 #include <netproto/802_11/ieee80211_wds.h>
60 #ifdef IEEE80211_SUPPORT_MESH
61 #include <netproto/802_11/ieee80211_mesh.h>
62 #endif
63 #include <netproto/802_11/ieee80211_monitor.h>
64 #include <netproto/802_11/ieee80211_input.h>
65 
66 /* XXX tunables */
67 #define   AGGRESSIVE_MODE_SWITCH_HYSTERESIS       3         /* pkts / 100ms */
68 #define   HIGH_PRI_SWITCH_THRESH                            10        /* pkts / 100ms */
69 
70 const char *mgt_subtype_name[] = {
71           "assoc_req",        "assoc_resp",       "reassoc_req",      "reassoc_resp",
72           "probe_req",        "probe_resp",       "timing_adv",       "reserved#7",
73           "beacon", "atim",             "disassoc",         "auth",
74           "deauth", "action", "action_noack",     "reserved#15"
75 };
76 const char *ctl_subtype_name[] = {
77           "reserved#0",       "reserved#1",       "reserved#2",       "reserved#3",
78           "reserved#4",       "reserved#5",       "reserved#6",       "control_wrap",
79           "bar",              "ba",               "ps_poll",          "rts",
80           "cts",              "ack",              "cf_end", "cf_end_ack"
81 };
82 const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
83           "IBSS",             /* IEEE80211_M_IBSS */
84           "STA",              /* IEEE80211_M_STA */
85           "WDS",              /* IEEE80211_M_WDS */
86           "AHDEMO", /* IEEE80211_M_AHDEMO */
87           "HOSTAP", /* IEEE80211_M_HOSTAP */
88           "MONITOR",          /* IEEE80211_M_MONITOR */
89           "MBSS"              /* IEEE80211_M_MBSS */
90 };
91 const char *ieee80211_state_name[IEEE80211_S_MAX] = {
92           "INIT",             /* IEEE80211_S_INIT */
93           "SCAN",             /* IEEE80211_S_SCAN */
94           "AUTH",             /* IEEE80211_S_AUTH */
95           "ASSOC",  /* IEEE80211_S_ASSOC */
96           "CAC",              /* IEEE80211_S_CAC */
97           "RUN",              /* IEEE80211_S_RUN */
98           "CSA",              /* IEEE80211_S_CSA */
99           "SLEEP",  /* IEEE80211_S_SLEEP */
100 };
101 const char *ieee80211_wme_acnames[] = {
102           "WME_AC_BE",
103           "WME_AC_BK",
104           "WME_AC_VI",
105           "WME_AC_VO",
106           "WME_UPSD",
107 };
108 
109 
110 /*
111  * Reason code descriptions were (mostly) obtained from
112  * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
113  */
114 const char *
ieee80211_reason_to_string(uint16_t reason)115 ieee80211_reason_to_string(uint16_t reason)
116 {
117           switch (reason) {
118           case IEEE80211_REASON_UNSPECIFIED:
119                     return ("unspecified");
120           case IEEE80211_REASON_AUTH_EXPIRE:
121                     return ("previous authentication is expired");
122           case IEEE80211_REASON_AUTH_LEAVE:
123                     return ("sending STA is leaving/has left IBSS or ESS");
124           case IEEE80211_REASON_ASSOC_EXPIRE:
125                     return ("disassociated due to inactivity");
126           case IEEE80211_REASON_ASSOC_TOOMANY:
127                     return ("too many associated STAs");
128           case IEEE80211_REASON_NOT_AUTHED:
129                     return ("class 2 frame received from nonauthenticated STA");
130           case IEEE80211_REASON_NOT_ASSOCED:
131                     return ("class 3 frame received from nonassociated STA");
132           case IEEE80211_REASON_ASSOC_LEAVE:
133                     return ("sending STA is leaving/has left BSS");
134           case IEEE80211_REASON_ASSOC_NOT_AUTHED:
135                     return ("STA requesting (re)association is not authenticated");
136           case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
137                     return ("information in the Power Capability element is "
138                               "unacceptable");
139           case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
140                     return ("information in the Supported Channels element is "
141                               "unacceptable");
142           case IEEE80211_REASON_IE_INVALID:
143                     return ("invalid element");
144           case IEEE80211_REASON_MIC_FAILURE:
145                     return ("MIC failure");
146           case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
147                     return ("4-Way handshake timeout");
148           case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
149                     return ("group key update timeout");
150           case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
151                     return ("element in 4-Way handshake different from "
152                               "(re)association request/probe response/beacon frame");
153           case IEEE80211_REASON_GROUP_CIPHER_INVALID:
154                     return ("invalid group cipher");
155           case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
156                     return ("invalid pairwise cipher");
157           case IEEE80211_REASON_AKMP_INVALID:
158                     return ("invalid AKMP");
159           case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
160                     return ("unsupported version in RSN IE");
161           case IEEE80211_REASON_INVALID_RSN_IE_CAP:
162                     return ("invalid capabilities in RSN IE");
163           case IEEE80211_REASON_802_1X_AUTH_FAILED:
164                     return ("IEEE 802.1X authentication failed");
165           case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
166                     return ("cipher suite rejected because of the security "
167                               "policy");
168           case IEEE80211_REASON_UNSPECIFIED_QOS:
169                     return ("unspecified (QoS-related)");
170           case IEEE80211_REASON_INSUFFICIENT_BW:
171                     return ("QoS AP lacks sufficient bandwidth for this QoS STA");
172           case IEEE80211_REASON_TOOMANY_FRAMES:
173                     return ("too many frames need to be acknowledged");
174           case IEEE80211_REASON_OUTSIDE_TXOP:
175                     return ("STA is transmitting outside the limits of its TXOPs");
176           case IEEE80211_REASON_LEAVING_QBSS:
177                     return ("requested from peer STA (the STA is "
178                               "resetting/leaving the BSS)");
179           case IEEE80211_REASON_BAD_MECHANISM:
180                     return ("requested from peer STA (it does not want to use "
181                               "the mechanism)");
182           case IEEE80211_REASON_SETUP_NEEDED:
183                     return ("requested from peer STA (setup is required for the "
184                               "used mechanism)");
185           case IEEE80211_REASON_TIMEOUT:
186                     return ("requested from peer STA (timeout)");
187           case IEEE80211_REASON_PEER_LINK_CANCELED:
188                     return ("SME cancels the mesh peering instance (not related "
189                               "to the maximum number of peer mesh STAs)");
190           case IEEE80211_REASON_MESH_MAX_PEERS:
191                     return ("maximum number of peer mesh STAs was reached");
192           case IEEE80211_REASON_MESH_CPVIOLATION:
193                     return ("the received information violates the Mesh "
194                               "Configuration policy configured in the mesh STA "
195                               "profile");
196           case IEEE80211_REASON_MESH_CLOSE_RCVD:
197                     return ("the mesh STA has received a Mesh Peering Close "
198                               "message requesting to close the mesh peering");
199           case IEEE80211_REASON_MESH_MAX_RETRIES:
200                     return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
201                               "Peering Open messages, without receiving a Mesh "
202                               "Peering Confirm message");
203           case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
204                     return ("the confirmTimer for the mesh peering instance times "
205                               "out");
206           case IEEE80211_REASON_MESH_INVALID_GTK:
207                     return ("the mesh STA fails to unwrap the GTK or the values "
208                               "in the wrapped contents do not match");
209           case IEEE80211_REASON_MESH_INCONS_PARAMS:
210                     return ("the mesh STA receives inconsistent information about "
211                               "the mesh parameters between Mesh Peering Management "
212                               "frames");
213           case IEEE80211_REASON_MESH_INVALID_SECURITY:
214                     return ("the mesh STA fails the authenticated mesh peering "
215                               "exchange because due to failure in selecting "
216                               "pairwise/group ciphersuite");
217           case IEEE80211_REASON_MESH_PERR_NO_PROXY:
218                     return ("the mesh STA does not have proxy information for "
219                               "this external destination");
220           case IEEE80211_REASON_MESH_PERR_NO_FI:
221                     return ("the mesh STA does not have forwarding information "
222                               "for this destination");
223           case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
224                     return ("the mesh STA determines that the link to the next "
225                               "hop of an active path in its forwarding information "
226                               "is no longer usable");
227           case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
228                     return ("the MAC address of the STA already exists in the "
229                               "mesh BSS");
230           case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
231                     return ("the mesh STA performs channel switch to meet "
232                               "regulatory requirements");
233           case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
234                     return ("the mesh STA performs channel switch with "
235                               "unspecified reason");
236           default:
237                     return ("reserved/unknown");
238           }
239 }
240 
241 static void beacon_miss(void *, int);
242 static void beacon_swmiss(void *, int);
243 static void parent_updown(void *, int);
244 static void update_mcast(void *, int);
245 static void update_promisc(void *, int);
246 static void update_channel(void *, int);
247 static void update_chw(void *, int);
248 static void update_wme(void *, int);
249 static void restart_vaps(void *, int);
250 static void ieee80211_newstate_cb(void *, int);
251 
252 static int
null_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)253 null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
254           const struct ieee80211_bpf_params *params)
255 {
256 
257           ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
258           m_freem(m);
259           return ENETDOWN;
260 }
261 
262 void
ieee80211_proto_attach(struct ieee80211com * ic)263 ieee80211_proto_attach(struct ieee80211com *ic)
264 {
265           uint8_t hdrlen;
266 
267           /* override the 802.3 setting */
268           hdrlen = ic->ic_headroom
269                     + sizeof(struct ieee80211_qosframe_addr4)
270                     + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
271                     + IEEE80211_WEP_EXTIVLEN;
272           /* XXX no way to recalculate on ifdetach */
273           if (ALIGN(hdrlen) > max_linkhdr) {
274                     /* XXX sanity check... */
275                     max_linkhdr = ALIGN(hdrlen);
276                     max_hdr = max_linkhdr + max_protohdr;
277                     max_datalen = MHLEN - max_hdr;
278           }
279           ic->ic_protmode = IEEE80211_PROT_CTSONLY;
280 
281           TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
282           TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
283           TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
284           TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
285           TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
286           TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
287           TASK_INIT(&ic->ic_wme_task, 0, update_wme, ic);
288           TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
289 
290           ic->ic_wme.wme_hipri_switch_hysteresis =
291                     AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
292 
293           /* initialize management frame handlers */
294           ic->ic_send_mgmt = ieee80211_send_mgmt;
295           ic->ic_raw_xmit = null_raw_xmit;
296 
297           ieee80211_adhoc_attach(ic);
298           ieee80211_sta_attach(ic);
299           ieee80211_wds_attach(ic);
300           ieee80211_hostap_attach(ic);
301 #ifdef IEEE80211_SUPPORT_MESH
302           ieee80211_mesh_attach(ic);
303 #endif
304           ieee80211_monitor_attach(ic);
305 }
306 
307 void
ieee80211_proto_detach(struct ieee80211com * ic)308 ieee80211_proto_detach(struct ieee80211com *ic)
309 {
310           ieee80211_monitor_detach(ic);
311 #ifdef IEEE80211_SUPPORT_MESH
312           ieee80211_mesh_detach(ic);
313 #endif
314           ieee80211_hostap_detach(ic);
315           ieee80211_wds_detach(ic);
316           ieee80211_adhoc_detach(ic);
317           ieee80211_sta_detach(ic);
318 }
319 
320 static void
null_update_beacon(struct ieee80211vap * vap,int item)321 null_update_beacon(struct ieee80211vap *vap, int item)
322 {
323 }
324 
325 void
ieee80211_proto_vattach(struct ieee80211vap * vap)326 ieee80211_proto_vattach(struct ieee80211vap *vap)
327 {
328           struct ieee80211com *ic = vap->iv_ic;
329           struct ifnet *ifp = vap->iv_ifp;
330           int i;
331 
332           /* override the 802.3 setting */
333           ifp->if_hdrlen = ic->ic_headroom
334                 + sizeof(struct ieee80211_qosframe_addr4)
335                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
336                 + IEEE80211_WEP_EXTIVLEN;
337 
338           vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
339           vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
340           vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
341           callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
342 #if defined(__DragonFly__)
343           callout_init_mp(&vap->iv_mgtsend);
344 #else
345           callout_init(&vap->iv_mgtsend, 1);
346 #endif
347           TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
348           TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
349           /*
350            * Install default tx rate handling: no fixed rate, lowest
351            * supported rate for mgmt and multicast frames.  Default
352            * max retry count.  These settings can be changed by the
353            * driver and/or user applications.
354            */
355           for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
356                     const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
357 
358                     vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
359 
360                     /*
361                      * Setting the management rate to MCS 0 assumes that the
362                      * BSS Basic rate set is empty and the BSS Basic MCS set
363                      * is not.
364                      *
365                      * Since we're not checking this, default to the lowest
366                      * defined rate for this mode.
367                      *
368                      * At least one 11n AP (DLINK DIR-825) is reported to drop
369                      * some MCS management traffic (eg BA response frames.)
370                      *
371                      * See also: 9.6.0 of the 802.11n-2009 specification.
372                      */
373 #ifdef    NOTYET
374                     if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
375                               vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
376                               vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
377                     } else {
378                               vap->iv_txparms[i].mgmtrate =
379                                   rs->rs_rates[0] & IEEE80211_RATE_VAL;
380                               vap->iv_txparms[i].mcastrate =
381                                   rs->rs_rates[0] & IEEE80211_RATE_VAL;
382                     }
383 #endif
384                     vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
385                     vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
386                     vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
387           }
388           vap->iv_roaming = IEEE80211_ROAMING_AUTO;
389 
390           vap->iv_update_beacon = null_update_beacon;
391           vap->iv_deliver_data = ieee80211_deliver_data;
392 
393           /* attach support for operating mode */
394           ic->ic_vattach[vap->iv_opmode](vap);
395 }
396 
397 void
ieee80211_proto_vdetach(struct ieee80211vap * vap)398 ieee80211_proto_vdetach(struct ieee80211vap *vap)
399 {
400 #define   FREEAPPIE(ie) do { \
401           if (ie != NULL) \
402                     IEEE80211_FREE(ie, M_80211_NODE_IE); \
403 } while (0)
404           /*
405            * Detach operating mode module.
406            */
407           if (vap->iv_opdetach != NULL)
408                     vap->iv_opdetach(vap);
409           /*
410            * This should not be needed as we detach when reseting
411            * the state but be conservative here since the
412            * authenticator may do things like spawn kernel threads.
413            */
414           if (vap->iv_auth->ia_detach != NULL)
415                     vap->iv_auth->ia_detach(vap);
416           /*
417            * Detach any ACL'ator.
418            */
419           if (vap->iv_acl != NULL)
420                     vap->iv_acl->iac_detach(vap);
421 
422           FREEAPPIE(vap->iv_appie_beacon);
423           FREEAPPIE(vap->iv_appie_probereq);
424           FREEAPPIE(vap->iv_appie_proberesp);
425           FREEAPPIE(vap->iv_appie_assocreq);
426           FREEAPPIE(vap->iv_appie_assocresp);
427           FREEAPPIE(vap->iv_appie_wpa);
428 #undef FREEAPPIE
429 }
430 
431 /*
432  * Simple-minded authenticator module support.
433  */
434 
435 #define   IEEE80211_AUTH_MAX  (IEEE80211_AUTH_WPA+1)
436 /* XXX well-known names */
437 static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
438           "wlan_internal",    /* IEEE80211_AUTH_NONE */
439           "wlan_internal",    /* IEEE80211_AUTH_OPEN */
440           "wlan_internal",    /* IEEE80211_AUTH_SHARED */
441           "wlan_xauth",                 /* IEEE80211_AUTH_8021X        */
442           "wlan_internal",    /* IEEE80211_AUTH_AUTO */
443           "wlan_xauth",                 /* IEEE80211_AUTH_WPA */
444 };
445 static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
446 
447 static const struct ieee80211_authenticator auth_internal = {
448           .ia_name            = "wlan_internal",
449           .ia_attach                    = NULL,
450           .ia_detach                    = NULL,
451           .ia_node_join                 = NULL,
452           .ia_node_leave                = NULL,
453 };
454 
455 /*
456  * Setup internal authenticators once; they are never unregistered.
457  */
458 static void
ieee80211_auth_setup(void)459 ieee80211_auth_setup(void)
460 {
461           ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
462           ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
463           ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
464 }
465 SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
466 
467 const struct ieee80211_authenticator *
ieee80211_authenticator_get(int auth)468 ieee80211_authenticator_get(int auth)
469 {
470           if (auth >= IEEE80211_AUTH_MAX)
471                     return NULL;
472           if (authenticators[auth] == NULL)
473                     ieee80211_load_module(auth_modnames[auth]);
474           return authenticators[auth];
475 }
476 
477 void
ieee80211_authenticator_register(int type,const struct ieee80211_authenticator * auth)478 ieee80211_authenticator_register(int type,
479           const struct ieee80211_authenticator *auth)
480 {
481           if (type >= IEEE80211_AUTH_MAX)
482                     return;
483           authenticators[type] = auth;
484 }
485 
486 void
ieee80211_authenticator_unregister(int type)487 ieee80211_authenticator_unregister(int type)
488 {
489 
490           if (type >= IEEE80211_AUTH_MAX)
491                     return;
492           authenticators[type] = NULL;
493 }
494 
495 /*
496  * Very simple-minded ACL module support.
497  */
498 /* XXX just one for now */
499 static    const struct ieee80211_aclator *acl = NULL;
500 
501 void
ieee80211_aclator_register(const struct ieee80211_aclator * iac)502 ieee80211_aclator_register(const struct ieee80211_aclator *iac)
503 {
504           kprintf("wlan: %s acl policy registered\n", iac->iac_name);
505           acl = iac;
506 }
507 
508 void
ieee80211_aclator_unregister(const struct ieee80211_aclator * iac)509 ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
510 {
511           if (acl == iac)
512                     acl = NULL;
513           kprintf("wlan: %s acl policy unregistered\n", iac->iac_name);
514 }
515 
516 const struct ieee80211_aclator *
ieee80211_aclator_get(const char * name)517 ieee80211_aclator_get(const char *name)
518 {
519           if (acl == NULL)
520                     ieee80211_load_module("wlan_acl");
521           return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
522 }
523 
524 void
ieee80211_print_essid(const uint8_t * essid,int len)525 ieee80211_print_essid(const uint8_t *essid, int len)
526 {
527           const uint8_t *p;
528           int i;
529 
530           if (len > IEEE80211_NWID_LEN)
531                     len = IEEE80211_NWID_LEN;
532           /* determine printable or not */
533           for (i = 0, p = essid; i < len; i++, p++) {
534                     if (*p < ' ' || *p > 0x7e)
535                               break;
536           }
537           if (i == len) {
538                     kprintf("\"");
539                     for (i = 0, p = essid; i < len; i++, p++)
540                               kprintf("%c", *p);
541                     kprintf("\"");
542           } else {
543                     kprintf("0x");
544                     for (i = 0, p = essid; i < len; i++, p++)
545                               kprintf("%02x", *p);
546           }
547 }
548 
549 void
ieee80211_dump_pkt(struct ieee80211com * ic,const uint8_t * buf,int len,int rate,int rssi)550 ieee80211_dump_pkt(struct ieee80211com *ic,
551           const uint8_t *buf, int len, int rate, int rssi)
552 {
553           const struct ieee80211_frame *wh;
554           int i;
555 
556           wh = (const struct ieee80211_frame *)buf;
557           switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
558           case IEEE80211_FC1_DIR_NODS:
559                     kprintf("NODS %s", ether_sprintf(wh->i_addr2));
560                     kprintf("->%s", ether_sprintf(wh->i_addr1));
561                     kprintf("(%s)", ether_sprintf(wh->i_addr3));
562                     break;
563           case IEEE80211_FC1_DIR_TODS:
564                     kprintf("TODS %s", ether_sprintf(wh->i_addr2));
565                     kprintf("->%s", ether_sprintf(wh->i_addr3));
566                     kprintf("(%s)", ether_sprintf(wh->i_addr1));
567                     break;
568           case IEEE80211_FC1_DIR_FROMDS:
569                     kprintf("FRDS %s", ether_sprintf(wh->i_addr3));
570                     kprintf("->%s", ether_sprintf(wh->i_addr1));
571                     kprintf("(%s)", ether_sprintf(wh->i_addr2));
572                     break;
573           case IEEE80211_FC1_DIR_DSTODS:
574                     kprintf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
575                     kprintf("->%s", ether_sprintf(wh->i_addr3));
576                     kprintf("(%s", ether_sprintf(wh->i_addr2));
577                     kprintf("->%s)", ether_sprintf(wh->i_addr1));
578                     break;
579           }
580           switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
581           case IEEE80211_FC0_TYPE_DATA:
582                     kprintf(" data");
583                     break;
584           case IEEE80211_FC0_TYPE_MGT:
585                     kprintf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
586                     break;
587           default:
588                     kprintf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
589                     break;
590           }
591           if (IEEE80211_QOS_HAS_SEQ(wh)) {
592                     const struct ieee80211_qosframe *qwh =
593                               (const struct ieee80211_qosframe *)buf;
594                     kprintf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
595                               qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
596           }
597           if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
598                     int off;
599 
600                     off = ieee80211_anyhdrspace(ic, wh);
601                     kprintf(" WEP [IV %.02x %.02x %.02x",
602                               buf[off+0], buf[off+1], buf[off+2]);
603                     if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
604                               kprintf(" %.02x %.02x %.02x",
605                                         buf[off+4], buf[off+5], buf[off+6]);
606                     kprintf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
607           }
608           if (rate >= 0)
609                     kprintf(" %dM", rate / 2);
610           if (rssi >= 0)
611                     kprintf(" +%d", rssi);
612           kprintf("\n");
613           if (len > 0) {
614                     for (i = 0; i < len; i++) {
615                               if ((i & 1) == 0)
616                                         kprintf(" ");
617                               kprintf("%02x", buf[i]);
618                     }
619                     kprintf("\n");
620           }
621 }
622 
623 static __inline int
findrix(const struct ieee80211_rateset * rs,int r)624 findrix(const struct ieee80211_rateset *rs, int r)
625 {
626           int i;
627 
628           for (i = 0; i < rs->rs_nrates; i++)
629                     if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
630                               return i;
631           return -1;
632 }
633 
634 int
ieee80211_fix_rate(struct ieee80211_node * ni,struct ieee80211_rateset * nrs,int flags)635 ieee80211_fix_rate(struct ieee80211_node *ni,
636           struct ieee80211_rateset *nrs, int flags)
637 {
638           struct ieee80211vap *vap = ni->ni_vap;
639           struct ieee80211com *ic = ni->ni_ic;
640           int i, j, rix, error;
641           int okrate, badrate, fixedrate, ucastrate;
642           const struct ieee80211_rateset *srs;
643           uint8_t r;
644 
645           error = 0;
646           okrate = badrate = 0;
647           ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
648           if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
649                     /*
650                      * Workaround awkwardness with fixed rate.  We are called
651                      * to check both the legacy rate set and the HT rate set
652                      * but we must apply any legacy fixed rate check only to the
653                      * legacy rate set and vice versa.  We cannot tell what type
654                      * of rate set we've been given (legacy or HT) but we can
655                      * distinguish the fixed rate type (MCS have 0x80 set).
656                      * So to deal with this the caller communicates whether to
657                      * check MCS or legacy rate using the flags and we use the
658                      * type of any fixed rate to avoid applying an MCS to a
659                      * legacy rate and vice versa.
660                      */
661                     if (ucastrate & 0x80) {
662                               if (flags & IEEE80211_F_DOFRATE)
663                                         flags &= ~IEEE80211_F_DOFRATE;
664                     } else if ((ucastrate & 0x80) == 0) {
665                               if (flags & IEEE80211_F_DOFMCS)
666                                         flags &= ~IEEE80211_F_DOFMCS;
667                     }
668                     /* NB: required to make MCS match below work */
669                     ucastrate &= IEEE80211_RATE_VAL;
670           }
671           fixedrate = IEEE80211_FIXED_RATE_NONE;
672           /*
673            * XXX we are called to process both MCS and legacy rates;
674            * we must use the appropriate basic rate set or chaos will
675            * ensue; for now callers that want MCS must supply
676            * IEEE80211_F_DOBRS; at some point we'll need to split this
677            * function so there are two variants, one for MCS and one
678            * for legacy rates.
679            */
680           if (flags & IEEE80211_F_DOBRS)
681                     srs = (const struct ieee80211_rateset *)
682                         ieee80211_get_suphtrates(ic, ni->ni_chan);
683           else
684                     srs = ieee80211_get_suprates(ic, ni->ni_chan);
685           for (i = 0; i < nrs->rs_nrates; ) {
686                     if (flags & IEEE80211_F_DOSORT) {
687                               /*
688                                * Sort rates.
689                                */
690                               for (j = i + 1; j < nrs->rs_nrates; j++) {
691                                         if (IEEE80211_RV(nrs->rs_rates[i]) >
692                                             IEEE80211_RV(nrs->rs_rates[j])) {
693                                                   r = nrs->rs_rates[i];
694                                                   nrs->rs_rates[i] = nrs->rs_rates[j];
695                                                   nrs->rs_rates[j] = r;
696                                         }
697                               }
698                     }
699                     r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
700                     badrate = r;
701                     /*
702                      * Check for fixed rate.
703                      */
704                     if (r == ucastrate)
705                               fixedrate = r;
706                     /*
707                      * Check against supported rates.
708                      */
709                     rix = findrix(srs, r);
710                     if (flags & IEEE80211_F_DONEGO) {
711                               if (rix < 0) {
712                                         /*
713                                          * A rate in the node's rate set is not
714                                          * supported.  If this is a basic rate and we
715                                          * are operating as a STA then this is an error.
716                                          * Otherwise we just discard/ignore the rate.
717                                          */
718                                         if ((flags & IEEE80211_F_JOIN) &&
719                                             (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
720                                                   error++;
721                               } else if ((flags & IEEE80211_F_JOIN) == 0) {
722                                         /*
723                                          * Overwrite with the supported rate
724                                          * value so any basic rate bit is set.
725                                          */
726                                         nrs->rs_rates[i] = srs->rs_rates[rix];
727                               }
728                     }
729                     if ((flags & IEEE80211_F_DODEL) && rix < 0) {
730                               /*
731                                * Delete unacceptable rates.
732                                */
733                               nrs->rs_nrates--;
734                               for (j = i; j < nrs->rs_nrates; j++)
735                                         nrs->rs_rates[j] = nrs->rs_rates[j + 1];
736                               nrs->rs_rates[j] = 0;
737                               continue;
738                     }
739                     if (rix >= 0)
740                               okrate = nrs->rs_rates[i];
741                     i++;
742           }
743           if (okrate == 0 || error != 0 ||
744               ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
745                fixedrate != ucastrate)) {
746                     IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
747                         "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
748                         "ucastrate %x\n", __func__, flags, okrate, error,
749                         fixedrate, ucastrate);
750                     return badrate | IEEE80211_RATE_BASIC;
751           } else
752                     return IEEE80211_RV(okrate);
753 }
754 
755 /*
756  * Reset 11g-related state.
757  */
758 void
ieee80211_reset_erp(struct ieee80211com * ic)759 ieee80211_reset_erp(struct ieee80211com *ic)
760 {
761           ic->ic_flags &= ~IEEE80211_F_USEPROT;
762           ic->ic_nonerpsta = 0;
763           ic->ic_longslotsta = 0;
764           /*
765            * Short slot time is enabled only when operating in 11g
766            * and not in an IBSS.  We must also honor whether or not
767            * the driver is capable of doing it.
768            */
769           ieee80211_set_shortslottime(ic,
770                     IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
771                     IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
772                     (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
773                     ic->ic_opmode == IEEE80211_M_HOSTAP &&
774                     (ic->ic_caps & IEEE80211_C_SHSLOT)));
775           /*
776            * Set short preamble and ERP barker-preamble flags.
777            */
778           if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
779               (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
780                     ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
781                     ic->ic_flags &= ~IEEE80211_F_USEBARKER;
782           } else {
783                     ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
784                     ic->ic_flags |= IEEE80211_F_USEBARKER;
785           }
786 }
787 
788 /*
789  * Set the short slot time state and notify the driver.
790  */
791 void
ieee80211_set_shortslottime(struct ieee80211com * ic,int onoff)792 ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
793 {
794           if (onoff)
795                     ic->ic_flags |= IEEE80211_F_SHSLOT;
796           else
797                     ic->ic_flags &= ~IEEE80211_F_SHSLOT;
798           /* notify driver */
799           if (ic->ic_updateslot != NULL)
800                     ic->ic_updateslot(ic);
801 }
802 
803 /*
804  * Check if the specified rate set supports ERP.
805  * NB: the rate set is assumed to be sorted.
806  */
807 int
ieee80211_iserp_rateset(const struct ieee80211_rateset * rs)808 ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
809 {
810           static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
811           int i, j;
812 
813           if (rs->rs_nrates < nitems(rates))
814                     return 0;
815           for (i = 0; i < nitems(rates); i++) {
816                     for (j = 0; j < rs->rs_nrates; j++) {
817                               int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
818                               if (rates[i] == r)
819                                         goto next;
820                               if (r > rates[i])
821                                         return 0;
822                     }
823                     return 0;
824           next:
825                     ;
826           }
827           return 1;
828 }
829 
830 /*
831  * Mark the basic rates for the rate table based on the
832  * operating mode.  For real 11g we mark all the 11b rates
833  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
834  * 11b rates.  There's also a pseudo 11a-mode used to mark only
835  * the basic OFDM rates.
836  */
837 static void
setbasicrates(struct ieee80211_rateset * rs,enum ieee80211_phymode mode,int add)838 setbasicrates(struct ieee80211_rateset *rs,
839     enum ieee80211_phymode mode, int add)
840 {
841           static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
842               [IEEE80211_MODE_11A]      = { 3, { 12, 24, 48 } },
843               [IEEE80211_MODE_11B]      = { 2, { 2, 4 } },
844                                                       /* NB: mixed b/g */
845               [IEEE80211_MODE_11G]      = { 4, { 2, 4, 11, 22 } },
846               [IEEE80211_MODE_TURBO_A]  = { 3, { 12, 24, 48 } },
847               [IEEE80211_MODE_TURBO_G]  = { 4, { 2, 4, 11, 22 } },
848               [IEEE80211_MODE_STURBO_A] = { 3, { 12, 24, 48 } },
849               [IEEE80211_MODE_HALF]     = { 3, { 6, 12, 24 } },
850               [IEEE80211_MODE_QUARTER]  = { 3, { 3, 6, 12 } },
851               [IEEE80211_MODE_11NA]     = { 3, { 12, 24, 48 } },
852                                                       /* NB: mixed b/g */
853               [IEEE80211_MODE_11NG]     = { 4, { 2, 4, 11, 22 } },
854           };
855           int i, j;
856 
857           for (i = 0; i < rs->rs_nrates; i++) {
858                     if (!add)
859                               rs->rs_rates[i] &= IEEE80211_RATE_VAL;
860                     for (j = 0; j < basic[mode].rs_nrates; j++)
861                               if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
862                                         rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
863                                         break;
864                               }
865           }
866 }
867 
868 /*
869  * Set the basic rates in a rate set.
870  */
871 void
ieee80211_setbasicrates(struct ieee80211_rateset * rs,enum ieee80211_phymode mode)872 ieee80211_setbasicrates(struct ieee80211_rateset *rs,
873     enum ieee80211_phymode mode)
874 {
875           setbasicrates(rs, mode, 0);
876 }
877 
878 /*
879  * Add basic rates to a rate set.
880  */
881 void
ieee80211_addbasicrates(struct ieee80211_rateset * rs,enum ieee80211_phymode mode)882 ieee80211_addbasicrates(struct ieee80211_rateset *rs,
883     enum ieee80211_phymode mode)
884 {
885           setbasicrates(rs, mode, 1);
886 }
887 
888 /*
889  * WME protocol support.
890  *
891  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
892  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
893  * Draft 2.0 Test Plan (Appendix D).
894  *
895  * Static/Dynamic Turbo mode settings come from Atheros.
896  */
897 typedef struct phyParamType {
898           uint8_t             aifsn;
899           uint8_t             logcwmin;
900           uint8_t             logcwmax;
901           uint16_t  txopLimit;
902           uint8_t   acm;
903 } paramType;
904 
905 static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
906           [IEEE80211_MODE_AUTO]         = { 3, 4,  6,  0, 0 },
907           [IEEE80211_MODE_11A]          = { 3, 4,  6,  0, 0 },
908           [IEEE80211_MODE_11B]          = { 3, 4,  6,  0, 0 },
909           [IEEE80211_MODE_11G]          = { 3, 4,  6,  0, 0 },
910           [IEEE80211_MODE_FH] = { 3, 4,  6,  0, 0 },
911           [IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
912           [IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
913           [IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
914           [IEEE80211_MODE_HALF]         = { 3, 4,  6,  0, 0 },
915           [IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
916           [IEEE80211_MODE_11NA]         = { 3, 4,  6,  0, 0 },
917           [IEEE80211_MODE_11NG]         = { 3, 4,  6,  0, 0 },
918 };
919 static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
920           [IEEE80211_MODE_AUTO]         = { 7, 4, 10,  0, 0 },
921           [IEEE80211_MODE_11A]          = { 7, 4, 10,  0, 0 },
922           [IEEE80211_MODE_11B]          = { 7, 4, 10,  0, 0 },
923           [IEEE80211_MODE_11G]          = { 7, 4, 10,  0, 0 },
924           [IEEE80211_MODE_FH] = { 7, 4, 10,  0, 0 },
925           [IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
926           [IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
927           [IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
928           [IEEE80211_MODE_HALF]         = { 7, 4, 10,  0, 0 },
929           [IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
930           [IEEE80211_MODE_11NA]         = { 7, 4, 10,  0, 0 },
931           [IEEE80211_MODE_11NG]         = { 7, 4, 10,  0, 0 },
932 };
933 static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
934           [IEEE80211_MODE_AUTO]         = { 1, 3, 4,  94, 0 },
935           [IEEE80211_MODE_11A]          = { 1, 3, 4,  94, 0 },
936           [IEEE80211_MODE_11B]          = { 1, 3, 4, 188, 0 },
937           [IEEE80211_MODE_11G]          = { 1, 3, 4,  94, 0 },
938           [IEEE80211_MODE_FH] = { 1, 3, 4, 188, 0 },
939           [IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
940           [IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
941           [IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
942           [IEEE80211_MODE_HALF]         = { 1, 3, 4,  94, 0 },
943           [IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
944           [IEEE80211_MODE_11NA]         = { 1, 3, 4,  94, 0 },
945           [IEEE80211_MODE_11NG]         = { 1, 3, 4,  94, 0 },
946 };
947 static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
948           [IEEE80211_MODE_AUTO]         = { 1, 2, 3,  47, 0 },
949           [IEEE80211_MODE_11A]          = { 1, 2, 3,  47, 0 },
950           [IEEE80211_MODE_11B]          = { 1, 2, 3, 102, 0 },
951           [IEEE80211_MODE_11G]          = { 1, 2, 3,  47, 0 },
952           [IEEE80211_MODE_FH] = { 1, 2, 3, 102, 0 },
953           [IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
954           [IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
955           [IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
956           [IEEE80211_MODE_HALF]         = { 1, 2, 3,  47, 0 },
957           [IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
958           [IEEE80211_MODE_11NA]         = { 1, 2, 3,  47, 0 },
959           [IEEE80211_MODE_11NG]         = { 1, 2, 3,  47, 0 },
960 };
961 
962 static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
963           [IEEE80211_MODE_AUTO]         = { 3, 4, 10,  0, 0 },
964           [IEEE80211_MODE_11A]          = { 3, 4, 10,  0, 0 },
965           [IEEE80211_MODE_11B]          = { 3, 4, 10,  0, 0 },
966           [IEEE80211_MODE_11G]          = { 3, 4, 10,  0, 0 },
967           [IEEE80211_MODE_FH] = { 3, 4, 10,  0, 0 },
968           [IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
969           [IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
970           [IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
971           [IEEE80211_MODE_HALF]         = { 3, 4, 10,  0, 0 },
972           [IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
973           [IEEE80211_MODE_11NA]         = { 3, 4, 10,  0, 0 },
974           [IEEE80211_MODE_11NG]         = { 3, 4, 10,  0, 0 },
975 };
976 static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
977           [IEEE80211_MODE_AUTO]         = { 2, 3, 4,  94, 0 },
978           [IEEE80211_MODE_11A]          = { 2, 3, 4,  94, 0 },
979           [IEEE80211_MODE_11B]          = { 2, 3, 4, 188, 0 },
980           [IEEE80211_MODE_11G]          = { 2, 3, 4,  94, 0 },
981           [IEEE80211_MODE_FH] = { 2, 3, 4, 188, 0 },
982           [IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
983           [IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
984           [IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
985           [IEEE80211_MODE_HALF]         = { 2, 3, 4,  94, 0 },
986           [IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
987           [IEEE80211_MODE_11NA]         = { 2, 3, 4,  94, 0 },
988           [IEEE80211_MODE_11NG]         = { 2, 3, 4,  94, 0 },
989 };
990 static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
991           [IEEE80211_MODE_AUTO]         = { 2, 2, 3,  47, 0 },
992           [IEEE80211_MODE_11A]          = { 2, 2, 3,  47, 0 },
993           [IEEE80211_MODE_11B]          = { 2, 2, 3, 102, 0 },
994           [IEEE80211_MODE_11G]          = { 2, 2, 3,  47, 0 },
995           [IEEE80211_MODE_FH] = { 2, 2, 3, 102, 0 },
996           [IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
997           [IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
998           [IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
999           [IEEE80211_MODE_HALF]         = { 2, 2, 3,  47, 0 },
1000           [IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
1001           [IEEE80211_MODE_11NA]         = { 2, 2, 3,  47, 0 },
1002           [IEEE80211_MODE_11NG]         = { 2, 2, 3,  47, 0 },
1003 };
1004 
1005 static void
_setifsparams(struct wmeParams * wmep,const paramType * phy)1006 _setifsparams(struct wmeParams *wmep, const paramType *phy)
1007 {
1008           wmep->wmep_aifsn = phy->aifsn;
1009           wmep->wmep_logcwmin = phy->logcwmin;
1010           wmep->wmep_logcwmax = phy->logcwmax;
1011           wmep->wmep_txopLimit = phy->txopLimit;
1012 }
1013 
1014 static void
setwmeparams(struct ieee80211vap * vap,const char * type,int ac,struct wmeParams * wmep,const paramType * phy)1015 setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
1016           struct wmeParams *wmep, const paramType *phy)
1017 {
1018           wmep->wmep_acm = phy->acm;
1019           _setifsparams(wmep, phy);
1020 
1021           IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1022               "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
1023               ieee80211_wme_acnames[ac], type,
1024               wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
1025               wmep->wmep_logcwmax, wmep->wmep_txopLimit);
1026 }
1027 
1028 static void
ieee80211_wme_initparams_locked(struct ieee80211vap * vap)1029 ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
1030 {
1031           struct ieee80211com *ic = vap->iv_ic;
1032           struct ieee80211_wme_state *wme = &ic->ic_wme;
1033           const paramType *pPhyParam, *pBssPhyParam;
1034           struct wmeParams *wmep;
1035           enum ieee80211_phymode mode;
1036           int i;
1037 
1038           IEEE80211_LOCK_ASSERT(ic);
1039 
1040           if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
1041                     return;
1042 
1043           /*
1044            * Clear the wme cap_info field so a qoscount from a previous
1045            * vap doesn't confuse later code which only parses the beacon
1046            * field and updates hardware when said field changes.
1047            * Otherwise the hardware is programmed with defaults, not what
1048            * the beacon actually announces.
1049            */
1050           wme->wme_wmeChanParams.cap_info = 0;
1051 
1052           /*
1053            * Select mode; we can be called early in which case we
1054            * always use auto mode.  We know we'll be called when
1055            * entering the RUN state with bsschan setup properly
1056            * so state will eventually get set correctly
1057            */
1058           if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1059                     mode = ieee80211_chan2mode(ic->ic_bsschan);
1060           else
1061                     mode = IEEE80211_MODE_AUTO;
1062           for (i = 0; i < WME_NUM_AC; i++) {
1063                     switch (i) {
1064                     case WME_AC_BK:
1065                               pPhyParam = &phyParamForAC_BK[mode];
1066                               pBssPhyParam = &phyParamForAC_BK[mode];
1067                               break;
1068                     case WME_AC_VI:
1069                               pPhyParam = &phyParamForAC_VI[mode];
1070                               pBssPhyParam = &bssPhyParamForAC_VI[mode];
1071                               break;
1072                     case WME_AC_VO:
1073                               pPhyParam = &phyParamForAC_VO[mode];
1074                               pBssPhyParam = &bssPhyParamForAC_VO[mode];
1075                               break;
1076                     case WME_AC_BE:
1077                     default:
1078                               pPhyParam = &phyParamForAC_BE[mode];
1079                               pBssPhyParam = &bssPhyParamForAC_BE[mode];
1080                               break;
1081                     }
1082                     wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1083                     if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1084                               setwmeparams(vap, "chan", i, wmep, pPhyParam);
1085                     } else {
1086                               setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
1087                     }
1088                     wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1089                     setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
1090           }
1091           /* NB: check ic_bss to avoid NULL deref on initial attach */
1092           if (vap->iv_bss != NULL) {
1093                     /*
1094                      * Calculate aggressive mode switching threshold based
1095                      * on beacon interval.  This doesn't need locking since
1096                      * we're only called before entering the RUN state at
1097                      * which point we start sending beacon frames.
1098                      */
1099                     wme->wme_hipri_switch_thresh =
1100                               (HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
1101                     wme->wme_flags &= ~WME_F_AGGRMODE;
1102                     ieee80211_wme_updateparams(vap);
1103           }
1104 }
1105 
1106 void
ieee80211_wme_initparams(struct ieee80211vap * vap)1107 ieee80211_wme_initparams(struct ieee80211vap *vap)
1108 {
1109           struct ieee80211com *ic = vap->iv_ic;
1110 
1111           IEEE80211_LOCK(ic);
1112           ieee80211_wme_initparams_locked(vap);
1113           IEEE80211_UNLOCK(ic);
1114 }
1115 
1116 /*
1117  * Update WME parameters for ourself and the BSS.
1118  */
1119 void
ieee80211_wme_updateparams_locked(struct ieee80211vap * vap)1120 ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
1121 {
1122           static const paramType aggrParam[IEEE80211_MODE_MAX] = {
1123               [IEEE80211_MODE_AUTO]     = { 2, 4, 10, 64, 0 },
1124               [IEEE80211_MODE_11A]      = { 2, 4, 10, 64, 0 },
1125               [IEEE80211_MODE_11B]      = { 2, 5, 10, 64, 0 },
1126               [IEEE80211_MODE_11G]      = { 2, 4, 10, 64, 0 },
1127               [IEEE80211_MODE_FH]                 = { 2, 5, 10, 64, 0 },
1128               [IEEE80211_MODE_TURBO_A]  = { 1, 3, 10, 64, 0 },
1129               [IEEE80211_MODE_TURBO_G]  = { 1, 3, 10, 64, 0 },
1130               [IEEE80211_MODE_STURBO_A] = { 1, 3, 10, 64, 0 },
1131               [IEEE80211_MODE_HALF]     = { 2, 4, 10, 64, 0 },
1132               [IEEE80211_MODE_QUARTER]  = { 2, 4, 10, 64, 0 },
1133               [IEEE80211_MODE_11NA]     = { 2, 4, 10, 64, 0 },        /* XXXcheck*/
1134               [IEEE80211_MODE_11NG]     = { 2, 4, 10, 64, 0 },        /* XXXcheck*/
1135           };
1136           struct ieee80211com *ic = vap->iv_ic;
1137           struct ieee80211_wme_state *wme = &ic->ic_wme;
1138           const struct wmeParams *wmep;
1139           struct wmeParams *chanp, *bssp;
1140           enum ieee80211_phymode mode;
1141           int i;
1142           int do_aggrmode = 0;
1143 
1144           /*
1145            * Set up the channel access parameters for the physical
1146            * device.  First populate the configured settings.
1147            */
1148           for (i = 0; i < WME_NUM_AC; i++) {
1149                     chanp = &wme->wme_chanParams.cap_wmeParams[i];
1150                     wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
1151                     chanp->wmep_aifsn = wmep->wmep_aifsn;
1152                     chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1153                     chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1154                     chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1155 
1156                     chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
1157                     wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
1158                     chanp->wmep_aifsn = wmep->wmep_aifsn;
1159                     chanp->wmep_logcwmin = wmep->wmep_logcwmin;
1160                     chanp->wmep_logcwmax = wmep->wmep_logcwmax;
1161                     chanp->wmep_txopLimit = wmep->wmep_txopLimit;
1162           }
1163 
1164           /*
1165            * Select mode; we can be called early in which case we
1166            * always use auto mode.  We know we'll be called when
1167            * entering the RUN state with bsschan setup properly
1168            * so state will eventually get set correctly
1169            */
1170           if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1171                     mode = ieee80211_chan2mode(ic->ic_bsschan);
1172           else
1173                     mode = IEEE80211_MODE_AUTO;
1174 
1175           /*
1176            * This implements aggressive mode as found in certain
1177            * vendors' AP's.  When there is significant high
1178            * priority (VI/VO) traffic in the BSS throttle back BE
1179            * traffic by using conservative parameters.  Otherwise
1180            * BE uses aggressive params to optimize performance of
1181            * legacy/non-QoS traffic.
1182            */
1183 
1184           /* Hostap? Only if aggressive mode is enabled */
1185         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1186                (wme->wme_flags & WME_F_AGGRMODE) != 0)
1187                     do_aggrmode = 1;
1188 
1189           /*
1190            * Station? Only if we're in a non-QoS BSS.
1191            */
1192           else if ((vap->iv_opmode == IEEE80211_M_STA &&
1193                (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
1194                     do_aggrmode = 1;
1195 
1196           /*
1197            * IBSS? Only if we we have WME enabled.
1198            */
1199           else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
1200               (vap->iv_flags & IEEE80211_F_WME))
1201                     do_aggrmode = 1;
1202 
1203           /*
1204            * If WME is disabled on this VAP, default to aggressive mode
1205            * regardless of the configuration.
1206            */
1207           if ((vap->iv_flags & IEEE80211_F_WME) == 0)
1208                     do_aggrmode = 1;
1209 
1210           /* XXX WDS? */
1211 
1212           /* XXX MBSS? */
1213 
1214           if (do_aggrmode) {
1215                     chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1216                     bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1217 
1218                     chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
1219                     chanp->wmep_logcwmin = bssp->wmep_logcwmin =
1220                         aggrParam[mode].logcwmin;
1221                     chanp->wmep_logcwmax = bssp->wmep_logcwmax =
1222                         aggrParam[mode].logcwmax;
1223                     chanp->wmep_txopLimit = bssp->wmep_txopLimit =
1224                         (vap->iv_flags & IEEE80211_F_BURST) ?
1225                               aggrParam[mode].txopLimit : 0;
1226                     IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1227                         "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
1228                         "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
1229                         chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
1230                         chanp->wmep_logcwmax, chanp->wmep_txopLimit);
1231           }
1232 
1233 
1234           /*
1235            * Change the contention window based on the number of associated
1236            * stations.  If the number of associated stations is 1 and
1237            * aggressive mode is enabled, lower the contention window even
1238            * further.
1239            */
1240           if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1241               ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
1242                     static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
1243                         [IEEE80211_MODE_AUTO]     = 3,
1244                         [IEEE80211_MODE_11A]      = 3,
1245                         [IEEE80211_MODE_11B]      = 4,
1246                         [IEEE80211_MODE_11G]      = 3,
1247                         [IEEE80211_MODE_FH]                 = 4,
1248                         [IEEE80211_MODE_TURBO_A]  = 3,
1249                         [IEEE80211_MODE_TURBO_G]  = 3,
1250                         [IEEE80211_MODE_STURBO_A] = 3,
1251                         [IEEE80211_MODE_HALF]     = 3,
1252                         [IEEE80211_MODE_QUARTER]  = 3,
1253                         [IEEE80211_MODE_11NA]     = 3,
1254                         [IEEE80211_MODE_11NG]     = 3,
1255                     };
1256                     chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
1257                     bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
1258 
1259                     chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
1260                     IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1261                         "update %s (chan+bss) logcwmin %u\n",
1262                         ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
1263           }
1264 
1265           /*
1266            * Arrange for the beacon update.
1267            *
1268            * XXX what about MBSS, WDS?
1269            */
1270           if (vap->iv_opmode == IEEE80211_M_HOSTAP
1271               || vap->iv_opmode == IEEE80211_M_IBSS) {
1272                     /*
1273                      * Arrange for a beacon update and bump the parameter
1274                      * set number so associated stations load the new values.
1275                      */
1276                     wme->wme_bssChanParams.cap_info =
1277                               (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
1278                     ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
1279           }
1280 
1281           /* schedule the deferred WME update */
1282           ieee80211_runtask(ic, &ic->ic_wme_task);
1283 
1284           IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
1285               "%s: WME params updated, cap_info 0x%x\n", __func__,
1286               vap->iv_opmode == IEEE80211_M_STA ?
1287                     wme->wme_wmeChanParams.cap_info :
1288                     wme->wme_bssChanParams.cap_info);
1289 }
1290 
1291 void
ieee80211_wme_updateparams(struct ieee80211vap * vap)1292 ieee80211_wme_updateparams(struct ieee80211vap *vap)
1293 {
1294           struct ieee80211com *ic = vap->iv_ic;
1295 
1296           if (ic->ic_caps & IEEE80211_C_WME) {
1297                     IEEE80211_LOCK(ic);
1298                     ieee80211_wme_updateparams_locked(vap);
1299                     IEEE80211_UNLOCK(ic);
1300           }
1301 }
1302 
1303 static void
parent_updown(void * arg,int npending)1304 parent_updown(void *arg, int npending)
1305 {
1306           struct ieee80211com *ic = arg;
1307 
1308           ic->ic_parent(ic);
1309 }
1310 
1311 static void
update_mcast(void * arg,int npending)1312 update_mcast(void *arg, int npending)
1313 {
1314           struct ieee80211com *ic = arg;
1315 
1316           ic->ic_update_mcast(ic);
1317 }
1318 
1319 static void
update_promisc(void * arg,int npending)1320 update_promisc(void *arg, int npending)
1321 {
1322           struct ieee80211com *ic = arg;
1323 
1324           ic->ic_update_promisc(ic);
1325 }
1326 
1327 static void
update_channel(void * arg,int npending)1328 update_channel(void *arg, int npending)
1329 {
1330           struct ieee80211com *ic = arg;
1331 
1332           ic->ic_set_channel(ic);
1333           ieee80211_radiotap_chan_change(ic);
1334 }
1335 
1336 static void
update_chw(void * arg,int npending)1337 update_chw(void *arg, int npending)
1338 {
1339           struct ieee80211com *ic = arg;
1340 
1341           /*
1342            * XXX should we defer the channel width _config_ update until now?
1343            */
1344           ic->ic_update_chw(ic);
1345 }
1346 
1347 static void
update_wme(void * arg,int npending)1348 update_wme(void *arg, int npending)
1349 {
1350           struct ieee80211com *ic = arg;
1351 
1352           /*
1353            * XXX should we defer the WME configuration update until now?
1354            */
1355           ic->ic_wme.wme_update(ic);
1356 }
1357 
1358 static void
restart_vaps(void * arg,int npending)1359 restart_vaps(void *arg, int npending)
1360 {
1361           struct ieee80211com *ic = arg;
1362 
1363           ieee80211_suspend_all(ic);
1364           ieee80211_resume_all(ic);
1365 }
1366 
1367 /*
1368  * Block until the parent is in a known state.  This is
1369  * used after any operations that dispatch a task (e.g.
1370  * to auto-configure the parent device up/down).
1371  */
1372 void
ieee80211_waitfor_parent(struct ieee80211com * ic)1373 ieee80211_waitfor_parent(struct ieee80211com *ic)
1374 {
1375           taskqueue_block(ic->ic_tq);
1376           ieee80211_draintask(ic, &ic->ic_parent_task);
1377           ieee80211_draintask(ic, &ic->ic_mcast_task);
1378           ieee80211_draintask(ic, &ic->ic_promisc_task);
1379           ieee80211_draintask(ic, &ic->ic_chan_task);
1380           ieee80211_draintask(ic, &ic->ic_bmiss_task);
1381           ieee80211_draintask(ic, &ic->ic_chw_task);
1382           ieee80211_draintask(ic, &ic->ic_wme_task);
1383           taskqueue_unblock(ic->ic_tq);
1384 }
1385 
1386 /*
1387  * Check to see whether the current channel needs reset.
1388  *
1389  * Some devices don't handle being given an invalid channel
1390  * in their operating mode very well (eg wpi(4) will throw a
1391  * firmware exception.)
1392  *
1393  * Return 0 if we're ok, 1 if the channel needs to be reset.
1394  *
1395  * See PR kern/202502.
1396  */
1397 static int
ieee80211_start_check_reset_chan(struct ieee80211vap * vap)1398 ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
1399 {
1400           struct ieee80211com *ic = vap->iv_ic;
1401 
1402           if ((vap->iv_opmode == IEEE80211_M_IBSS &&
1403                IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
1404               (vap->iv_opmode == IEEE80211_M_HOSTAP &&
1405                IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
1406                     return (1);
1407           return (0);
1408 }
1409 
1410 /*
1411  * Reset the curchan to a known good state.
1412  */
1413 static void
ieee80211_start_reset_chan(struct ieee80211vap * vap)1414 ieee80211_start_reset_chan(struct ieee80211vap *vap)
1415 {
1416           struct ieee80211com *ic = vap->iv_ic;
1417 
1418           ic->ic_curchan = &ic->ic_channels[0];
1419 }
1420 
1421 /*
1422  * Start a vap running.  If this is the first vap to be
1423  * set running on the underlying device then we
1424  * automatically bring the device up.
1425  */
1426 void
ieee80211_start_locked(struct ieee80211vap * vap)1427 ieee80211_start_locked(struct ieee80211vap *vap)
1428 {
1429           struct ifnet *ifp = vap->iv_ifp;
1430           struct ieee80211com *ic = vap->iv_ic;
1431 
1432           IEEE80211_LOCK_ASSERT(ic);
1433 
1434           IEEE80211_DPRINTF(vap,
1435                     IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1436                     "start running, %d vaps running\n", ic->ic_nrunning);
1437 
1438           if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1439                     /*
1440                      * Mark us running.  Note that it's ok to do this first;
1441                      * if we need to bring the parent device up we defer that
1442                      * to avoid dropping the com lock.  We expect the device
1443                      * to respond to being marked up by calling back into us
1444                      * through ieee80211_start_all at which point we'll come
1445                      * back in here and complete the work.
1446                      */
1447                     ifp->if_drv_flags |= IFF_DRV_RUNNING;
1448                     /*
1449                      * We are not running; if this we are the first vap
1450                      * to be brought up auto-up the parent if necessary.
1451                      */
1452                     if (ic->ic_nrunning++ == 0) {
1453 
1454                               /* reset the channel to a known good channel */
1455                               if (ieee80211_start_check_reset_chan(vap))
1456                                         ieee80211_start_reset_chan(vap);
1457 
1458                               IEEE80211_DPRINTF(vap,
1459                                   IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1460                                   "%s: up parent %s\n", __func__, ic->ic_name);
1461                               ieee80211_runtask(ic, &ic->ic_parent_task);
1462                               return;
1463                     }
1464           }
1465           /*
1466            * If the parent is up and running, then kick the
1467            * 802.11 state machine as appropriate.
1468            */
1469           if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
1470                     if (vap->iv_opmode == IEEE80211_M_STA) {
1471 #if 0
1472                               /* XXX bypasses scan too easily; disable for now */
1473                               /*
1474                                * Try to be intelligent about clocking the state
1475                                * machine.  If we're currently in RUN state then
1476                                * we should be able to apply any new state/parameters
1477                                * simply by re-associating.  Otherwise we need to
1478                                * re-scan to select an appropriate ap.
1479                                */
1480                               if (vap->iv_state >= IEEE80211_S_RUN)
1481                                         ieee80211_new_state_locked(vap,
1482                                             IEEE80211_S_ASSOC, 1);
1483                               else
1484 #endif
1485                                         ieee80211_new_state_locked(vap,
1486                                             IEEE80211_S_SCAN, 0);
1487                     } else {
1488                               /*
1489                                * For monitor+wds mode there's nothing to do but
1490                                * start running.  Otherwise if this is the first
1491                                * vap to be brought up, start a scan which may be
1492                                * preempted if the station is locked to a particular
1493                                * channel.
1494                                */
1495                               vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
1496                               if (vap->iv_opmode == IEEE80211_M_MONITOR ||
1497                                   vap->iv_opmode == IEEE80211_M_WDS)
1498                                         ieee80211_new_state_locked(vap,
1499                                             IEEE80211_S_RUN, -1);
1500                               else
1501                                         ieee80211_new_state_locked(vap,
1502                                             IEEE80211_S_SCAN, 0);
1503                     }
1504           }
1505 }
1506 
1507 /*
1508  * Start a single vap.
1509  */
1510 void
ieee80211_init(void * arg)1511 ieee80211_init(void *arg)
1512 {
1513           struct ieee80211vap *vap = arg;
1514 
1515           IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1516               "%s\n", __func__);
1517 
1518           IEEE80211_LOCK(vap->iv_ic);
1519           ieee80211_start_locked(vap);
1520           IEEE80211_UNLOCK(vap->iv_ic);
1521 }
1522 
1523 /*
1524  * Start all runnable vap's on a device.
1525  */
1526 void
ieee80211_start_all(struct ieee80211com * ic)1527 ieee80211_start_all(struct ieee80211com *ic)
1528 {
1529           struct ieee80211vap *vap;
1530 
1531           IEEE80211_LOCK(ic);
1532           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1533                     struct ifnet *ifp = vap->iv_ifp;
1534                     if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1535                               ieee80211_start_locked(vap);
1536           }
1537           IEEE80211_UNLOCK(ic);
1538 }
1539 
1540 /*
1541  * Stop a vap.  We force it down using the state machine
1542  * then mark it's ifnet not running.  If this is the last
1543  * vap running on the underlying device then we close it
1544  * too to insure it will be properly initialized when the
1545  * next vap is brought up.
1546  */
1547 void
ieee80211_stop_locked(struct ieee80211vap * vap)1548 ieee80211_stop_locked(struct ieee80211vap *vap)
1549 {
1550           struct ieee80211com *ic = vap->iv_ic;
1551           struct ifnet *ifp = vap->iv_ifp;
1552 
1553           IEEE80211_LOCK_ASSERT(ic);
1554 
1555           IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1556               "stop running, %d vaps running\n", ic->ic_nrunning);
1557 
1558           ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
1559           if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1560                     ifp->if_drv_flags &= ~IFF_DRV_RUNNING;  /* mark us stopped */
1561                     if (--ic->ic_nrunning == 0) {
1562                               IEEE80211_DPRINTF(vap,
1563                                   IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
1564                                   "down parent %s\n", ic->ic_name);
1565                               ieee80211_runtask(ic, &ic->ic_parent_task);
1566                     }
1567           }
1568 }
1569 
1570 void
ieee80211_stop(struct ieee80211vap * vap)1571 ieee80211_stop(struct ieee80211vap *vap)
1572 {
1573           struct ieee80211com *ic = vap->iv_ic;
1574 
1575           IEEE80211_LOCK(ic);
1576           ieee80211_stop_locked(vap);
1577           IEEE80211_UNLOCK(ic);
1578 }
1579 
1580 /*
1581  * Stop all vap's running on a device.
1582  */
1583 void
ieee80211_stop_all(struct ieee80211com * ic)1584 ieee80211_stop_all(struct ieee80211com *ic)
1585 {
1586           struct ieee80211vap *vap;
1587 
1588           IEEE80211_LOCK(ic);
1589           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1590                     struct ifnet *ifp = vap->iv_ifp;
1591                     if (IFNET_IS_UP_RUNNING(ifp)) /* NB: avoid recursion */
1592                               ieee80211_stop_locked(vap);
1593           }
1594           IEEE80211_UNLOCK(ic);
1595 
1596           ieee80211_waitfor_parent(ic);
1597 }
1598 
1599 /*
1600  * Stop all vap's running on a device and arrange
1601  * for those that were running to be resumed.
1602  */
1603 void
ieee80211_suspend_all(struct ieee80211com * ic)1604 ieee80211_suspend_all(struct ieee80211com *ic)
1605 {
1606           struct ieee80211vap *vap;
1607 
1608           IEEE80211_LOCK(ic);
1609           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1610                     struct ifnet *ifp = vap->iv_ifp;
1611                     if (IFNET_IS_UP_RUNNING(ifp)) {         /* NB: avoid recursion */
1612                               vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
1613                               ieee80211_stop_locked(vap);
1614                     }
1615           }
1616           IEEE80211_UNLOCK(ic);
1617 
1618           ieee80211_waitfor_parent(ic);
1619 }
1620 
1621 /*
1622  * Start all vap's marked for resume.
1623  */
1624 void
ieee80211_resume_all(struct ieee80211com * ic)1625 ieee80211_resume_all(struct ieee80211com *ic)
1626 {
1627           struct ieee80211vap *vap;
1628 
1629           IEEE80211_LOCK(ic);
1630           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1631                     struct ifnet *ifp = vap->iv_ifp;
1632                     if (!IFNET_IS_UP_RUNNING(ifp) &&
1633                         (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
1634                               vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
1635                               ieee80211_start_locked(vap);
1636                     }
1637           }
1638           IEEE80211_UNLOCK(ic);
1639 }
1640 
1641 /*
1642  * Restart all vap's running on a device.
1643  */
1644 void
ieee80211_restart_all(struct ieee80211com * ic)1645 ieee80211_restart_all(struct ieee80211com *ic)
1646 {
1647           /*
1648            * NB: do not use ieee80211_runtask here, we will
1649            * block & drain net80211 taskqueue.
1650            */
1651 #if defined(__DragonFly__)
1652           taskqueue_enqueue(taskqueue_thread[0], &ic->ic_restart_task);
1653 #else
1654           taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
1655 #endif
1656 }
1657 
1658 void
ieee80211_beacon_miss(struct ieee80211com * ic)1659 ieee80211_beacon_miss(struct ieee80211com *ic)
1660 {
1661           IEEE80211_LOCK(ic);
1662           if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1663                     /* Process in a taskq, the handler may reenter the driver */
1664                     ieee80211_runtask(ic, &ic->ic_bmiss_task);
1665           }
1666           IEEE80211_UNLOCK(ic);
1667 }
1668 
1669 static void
beacon_miss(void * arg,int npending)1670 beacon_miss(void *arg, int npending)
1671 {
1672           struct ieee80211com *ic = arg;
1673           struct ieee80211vap *vap;
1674 
1675           IEEE80211_LOCK(ic);
1676           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1677                     /*
1678                      * We only pass events through for sta vap's in RUN+ state;
1679                      * may be too restrictive but for now this saves all the
1680                      * handlers duplicating these checks.
1681                      */
1682                     if (vap->iv_opmode == IEEE80211_M_STA &&
1683                         vap->iv_state >= IEEE80211_S_RUN &&
1684                         vap->iv_bmiss != NULL)
1685                               vap->iv_bmiss(vap);
1686           }
1687           IEEE80211_UNLOCK(ic);
1688 }
1689 
1690 static void
beacon_swmiss(void * arg,int npending)1691 beacon_swmiss(void *arg, int npending)
1692 {
1693           struct ieee80211vap *vap = arg;
1694           struct ieee80211com *ic = vap->iv_ic;
1695 
1696           IEEE80211_LOCK(ic);
1697           if (vap->iv_state >= IEEE80211_S_RUN) {
1698                     /* XXX Call multiple times if npending > zero? */
1699                     vap->iv_bmiss(vap);
1700           }
1701           IEEE80211_UNLOCK(ic);
1702 }
1703 
1704 /*
1705  * Software beacon miss handling.  Check if any beacons
1706  * were received in the last period.  If not post a
1707  * beacon miss; otherwise reset the counter.
1708  */
1709 void
ieee80211_swbmiss(void * arg)1710 ieee80211_swbmiss(void *arg)
1711 {
1712           struct ieee80211vap *vap = arg;
1713           struct ieee80211com *ic = vap->iv_ic;
1714 
1715           IEEE80211_LOCK_ASSERT(ic);
1716 
1717           KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1718               ("wrong state %d", vap->iv_state));
1719 
1720           if (ic->ic_flags & IEEE80211_F_SCAN) {
1721                     /*
1722                      * If scanning just ignore and reset state.  If we get a
1723                      * bmiss after coming out of scan because we haven't had
1724                      * time to receive a beacon then we should probe the AP
1725                      * before posting a real bmiss (unless iv_bmiss_max has
1726                      * been artifiically lowered).  A cleaner solution might
1727                      * be to disable the timer on scan start/end but to handle
1728                      * case of multiple sta vap's we'd need to disable the
1729                      * timers of all affected vap's.
1730                      */
1731                     vap->iv_swbmiss_count = 0;
1732           } else if (vap->iv_swbmiss_count == 0) {
1733                     if (vap->iv_bmiss != NULL)
1734                               ieee80211_runtask(ic, &vap->iv_swbmiss_task);
1735           } else
1736                     vap->iv_swbmiss_count = 0;
1737           callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
1738                     ieee80211_swbmiss, vap);
1739 }
1740 
1741 /*
1742  * Start an 802.11h channel switch.  We record the parameters,
1743  * mark the operation pending, notify each vap through the
1744  * beacon update mechanism so it can update the beacon frame
1745  * contents, and then switch vap's to CSA state to block outbound
1746  * traffic.  Devices that handle CSA directly can use the state
1747  * switch to do the right thing so long as they call
1748  * ieee80211_csa_completeswitch when it's time to complete the
1749  * channel change.  Devices that depend on the net80211 layer can
1750  * use ieee80211_beacon_update to handle the countdown and the
1751  * channel switch.
1752  */
1753 void
ieee80211_csa_startswitch(struct ieee80211com * ic,struct ieee80211_channel * c,int mode,int count)1754 ieee80211_csa_startswitch(struct ieee80211com *ic,
1755           struct ieee80211_channel *c, int mode, int count)
1756 {
1757           struct ieee80211vap *vap;
1758 
1759           IEEE80211_LOCK_ASSERT(ic);
1760 
1761           ic->ic_csa_newchan = c;
1762           ic->ic_csa_mode = mode;
1763           ic->ic_csa_count = count;
1764           ic->ic_flags |= IEEE80211_F_CSAPENDING;
1765           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1766                     if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1767                         vap->iv_opmode == IEEE80211_M_IBSS ||
1768                         vap->iv_opmode == IEEE80211_M_MBSS)
1769                               ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
1770                     /* switch to CSA state to block outbound traffic */
1771                     if (vap->iv_state == IEEE80211_S_RUN)
1772                               ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
1773           }
1774           ieee80211_notify_csa(ic, c, mode, count);
1775 }
1776 
1777 /*
1778  * Complete the channel switch by transitioning all CSA VAPs to RUN.
1779  * This is called by both the completion and cancellation functions
1780  * so each VAP is placed back in the RUN state and can thus transmit.
1781  */
1782 static void
csa_completeswitch(struct ieee80211com * ic)1783 csa_completeswitch(struct ieee80211com *ic)
1784 {
1785           struct ieee80211vap *vap;
1786 
1787           ic->ic_csa_newchan = NULL;
1788           ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
1789 
1790           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1791                     if (vap->iv_state == IEEE80211_S_CSA)
1792                               ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1793 }
1794 
1795 /*
1796  * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
1797  * We clear state and move all vap's in CSA state to RUN state
1798  * so they can again transmit.
1799  *
1800  * Although this may not be completely correct, update the BSS channel
1801  * for each VAP to the newly configured channel. The setcurchan sets
1802  * the current operating channel for the interface (so the radio does
1803  * switch over) but the VAP BSS isn't updated, leading to incorrectly
1804  * reported information via ioctl.
1805  */
1806 void
ieee80211_csa_completeswitch(struct ieee80211com * ic)1807 ieee80211_csa_completeswitch(struct ieee80211com *ic)
1808 {
1809           struct ieee80211vap *vap;
1810 
1811           IEEE80211_LOCK_ASSERT(ic);
1812 
1813           KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
1814 
1815           ieee80211_setcurchan(ic, ic->ic_csa_newchan);
1816           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1817                     if (vap->iv_state == IEEE80211_S_CSA)
1818                               vap->iv_bss->ni_chan = ic->ic_curchan;
1819 
1820           csa_completeswitch(ic);
1821 }
1822 
1823 /*
1824  * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
1825  * We clear state and move all vap's in CSA state to RUN state
1826  * so they can again transmit.
1827  */
1828 void
ieee80211_csa_cancelswitch(struct ieee80211com * ic)1829 ieee80211_csa_cancelswitch(struct ieee80211com *ic)
1830 {
1831           IEEE80211_LOCK_ASSERT(ic);
1832 
1833           csa_completeswitch(ic);
1834 }
1835 
1836 /*
1837  * Complete a DFS CAC started by ieee80211_dfs_cac_start.
1838  * We clear state and move all vap's in CAC state to RUN state.
1839  */
1840 void
ieee80211_cac_completeswitch(struct ieee80211vap * vap0)1841 ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
1842 {
1843           struct ieee80211com *ic = vap0->iv_ic;
1844           struct ieee80211vap *vap;
1845 
1846           IEEE80211_LOCK(ic);
1847           /*
1848            * Complete CAC state change for lead vap first; then
1849            * clock all the other vap's waiting.
1850            */
1851           KASSERT(vap0->iv_state == IEEE80211_S_CAC,
1852               ("wrong state %d", vap0->iv_state));
1853           ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
1854 
1855           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1856                     if (vap->iv_state == IEEE80211_S_CAC)
1857                               ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
1858           IEEE80211_UNLOCK(ic);
1859 }
1860 
1861 /*
1862  * Force all vap's other than the specified vap to the INIT state
1863  * and mark them as waiting for a scan to complete.  These vaps
1864  * will be brought up when the scan completes and the scanning vap
1865  * reaches RUN state by wakeupwaiting.
1866  */
1867 static void
markwaiting(struct ieee80211vap * vap0)1868 markwaiting(struct ieee80211vap *vap0)
1869 {
1870           struct ieee80211com *ic = vap0->iv_ic;
1871           struct ieee80211vap *vap;
1872 
1873           IEEE80211_LOCK_ASSERT(ic);
1874 
1875           /*
1876            * A vap list entry can not disappear since we are running on the
1877            * taskqueue and a vap destroy will queue and drain another state
1878            * change task.
1879            */
1880           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1881                     if (vap == vap0)
1882                               continue;
1883                     if (vap->iv_state != IEEE80211_S_INIT) {
1884                               /* NB: iv_newstate may drop the lock */
1885                               vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
1886                               IEEE80211_LOCK_ASSERT(ic);
1887                               vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
1888                     }
1889           }
1890 }
1891 
1892 /*
1893  * Wakeup all vap's waiting for a scan to complete.  This is the
1894  * companion to markwaiting (above) and is used to coordinate
1895  * multiple vaps scanning.
1896  * This is called from the state taskqueue.
1897  */
1898 static void
wakeupwaiting(struct ieee80211vap * vap0)1899 wakeupwaiting(struct ieee80211vap *vap0)
1900 {
1901           struct ieee80211com *ic = vap0->iv_ic;
1902           struct ieee80211vap *vap;
1903 
1904           IEEE80211_LOCK_ASSERT(ic);
1905 
1906           /*
1907            * A vap list entry can not disappear since we are running on the
1908            * taskqueue and a vap destroy will queue and drain another state
1909            * change task.
1910            */
1911           TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1912                     if (vap == vap0)
1913                               continue;
1914                     if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
1915                               vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
1916                               /* NB: sta's cannot go INIT->RUN */
1917                               /* NB: iv_newstate may drop the lock */
1918                               vap->iv_newstate(vap,
1919                                   vap->iv_opmode == IEEE80211_M_STA ?
1920                                       IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
1921                               IEEE80211_LOCK_ASSERT(ic);
1922                     }
1923           }
1924 }
1925 
1926 /*
1927  * Handle post state change work common to all operating modes.
1928  */
1929 static void
ieee80211_newstate_cb(void * xvap,int npending)1930 ieee80211_newstate_cb(void *xvap, int npending)
1931 {
1932           struct ieee80211vap *vap = xvap;
1933           struct ieee80211com *ic = vap->iv_ic;
1934           enum ieee80211_state nstate, ostate;
1935           int arg, rc;
1936 
1937           IEEE80211_LOCK(ic);
1938           nstate = vap->iv_nstate;
1939           arg = vap->iv_nstate_arg;
1940 
1941           if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
1942                     /*
1943                      * We have been requested to drop back to the INIT before
1944                      * proceeding to the new state.
1945                      */
1946                     /* Deny any state changes while we are here. */
1947                     vap->iv_nstate = IEEE80211_S_INIT;
1948                     IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1949                         "%s: %s -> %s arg %d\n", __func__,
1950                         ieee80211_state_name[vap->iv_state],
1951                         ieee80211_state_name[vap->iv_nstate], arg);
1952                     vap->iv_newstate(vap, vap->iv_nstate, 0);
1953                     IEEE80211_LOCK_ASSERT(ic);
1954                     vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
1955                         IEEE80211_FEXT_STATEWAIT);
1956                     /* enqueue new state transition after cancel_scan() task */
1957                     ieee80211_new_state_locked(vap, nstate, arg);
1958                     goto done;
1959           }
1960 
1961           ostate = vap->iv_state;
1962           if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
1963                     /*
1964                      * SCAN was forced; e.g. on beacon miss.  Force other running
1965                      * vap's to INIT state and mark them as waiting for the scan to
1966                      * complete.  This insures they don't interfere with our
1967                      * scanning.  Since we are single threaded the vaps can not
1968                      * transition again while we are executing.
1969                      *
1970                      * XXX not always right, assumes ap follows sta
1971                      */
1972                     markwaiting(vap);
1973           }
1974           IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1975               "%s: %s -> %s arg %d\n", __func__,
1976               ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
1977 
1978           rc = vap->iv_newstate(vap, nstate, arg);
1979           IEEE80211_LOCK_ASSERT(ic);
1980           vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
1981           if (rc != 0) {
1982                     /* State transition failed */
1983                     KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
1984                     KASSERT(nstate != IEEE80211_S_INIT,
1985                         ("INIT state change failed"));
1986                     IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
1987                         "%s: %s returned error %d\n", __func__,
1988                         ieee80211_state_name[nstate], rc);
1989                     goto done;
1990           }
1991 
1992           /* No actual transition, skip post processing */
1993           if (ostate == nstate)
1994                     goto done;
1995 
1996           if (nstate == IEEE80211_S_RUN) {
1997                     /*
1998                      * OACTIVE may be set on the vap if the upper layer
1999                      * tried to transmit (e.g. IPv6 NDP) before we reach
2000                      * RUN state.  Clear it and restart xmit.
2001                      *
2002                      * Note this can also happen as a result of SLEEP->RUN
2003                      * (i.e. coming out of power save mode).
2004                      */
2005 #if defined(__DragonFly__)
2006                     struct ifaltq_subque *ifsq;
2007                     int wst;
2008 
2009                     ifsq = ifq_get_subq_default(&vap->iv_ifp->if_snd);
2010                     ifsq_clr_oactive(ifsq);
2011                     wst = wlan_serialize_push();
2012                     vap->iv_ifp->if_start(vap->iv_ifp, ifsq);
2013                     wlan_serialize_pop(wst);
2014 #else
2015                     vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2016 #endif
2017 
2018                     /*
2019                      * XXX TODO Kick-start a VAP queue - this should be a method!
2020                      */
2021 
2022                     /* bring up any vaps waiting on us */
2023                     wakeupwaiting(vap);
2024           } else if (nstate == IEEE80211_S_INIT) {
2025                     /*
2026                      * Flush the scan cache if we did the last scan (XXX?)
2027                      * and flush any frames on send queues from this vap.
2028                      * Note the mgt q is used only for legacy drivers and
2029                      * will go away shortly.
2030                      */
2031                     ieee80211_scan_flush(vap);
2032 
2033                     /*
2034                      * XXX TODO: ic/vap queue flush
2035                      */
2036           }
2037 done:
2038           IEEE80211_UNLOCK(ic);
2039 }
2040 
2041 /*
2042  * Public interface for initiating a state machine change.
2043  * This routine single-threads the request and coordinates
2044  * the scheduling of multiple vaps for the purpose of selecting
2045  * an operating channel.  Specifically the following scenarios
2046  * are handled:
2047  * o only one vap can be selecting a channel so on transition to
2048  *   SCAN state if another vap is already scanning then
2049  *   mark the caller for later processing and return without
2050  *   doing anything (XXX? expectations by caller of synchronous operation)
2051  * o only one vap can be doing CAC of a channel so on transition to
2052  *   CAC state if another vap is already scanning for radar then
2053  *   mark the caller for later processing and return without
2054  *   doing anything (XXX? expectations by caller of synchronous operation)
2055  * o if another vap is already running when a request is made
2056  *   to SCAN then an operating channel has been chosen; bypass
2057  *   the scan and just join the channel
2058  *
2059  * Note that the state change call is done through the iv_newstate
2060  * method pointer so any driver routine gets invoked.  The driver
2061  * will normally call back into operating mode-specific
2062  * ieee80211_newstate routines (below) unless it needs to completely
2063  * bypass the state machine (e.g. because the firmware has it's
2064  * own idea how things should work).  Bypassing the net80211 layer
2065  * is usually a mistake and indicates lack of proper integration
2066  * with the net80211 layer.
2067  */
2068 int
ieee80211_new_state_locked(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2069 ieee80211_new_state_locked(struct ieee80211vap *vap,
2070           enum ieee80211_state nstate, int arg)
2071 {
2072           struct ieee80211com *ic = vap->iv_ic;
2073           struct ieee80211vap *vp;
2074           enum ieee80211_state ostate;
2075           int nrunning, nscanning;
2076 
2077           IEEE80211_LOCK_ASSERT(ic);
2078 
2079           if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
2080                     if (vap->iv_nstate == IEEE80211_S_INIT ||
2081                         ((vap->iv_state == IEEE80211_S_INIT ||
2082                         (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
2083                         vap->iv_nstate == IEEE80211_S_SCAN &&
2084                         nstate > IEEE80211_S_SCAN)) {
2085                               /*
2086                                * XXX The vap is being stopped/started,
2087                                * do not allow any other state changes
2088                                * until this is completed.
2089                                */
2090                               IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2091                                   "%s: %s -> %s (%s) transition discarded\n",
2092                                   __func__,
2093                                   ieee80211_state_name[vap->iv_state],
2094                                   ieee80211_state_name[nstate],
2095                                   ieee80211_state_name[vap->iv_nstate]);
2096                               return -1;
2097                     } else if (vap->iv_state != vap->iv_nstate) {
2098 #if 0
2099                               /* Warn if the previous state hasn't completed. */
2100                               IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2101                                   "%s: pending %s -> %s transition lost\n", __func__,
2102                                   ieee80211_state_name[vap->iv_state],
2103                                   ieee80211_state_name[vap->iv_nstate]);
2104 #else
2105                               /* XXX temporarily enable to identify issues */
2106                               if_printf(vap->iv_ifp,
2107                                   "%s: pending %s -> %s transition lost\n",
2108                                   __func__, ieee80211_state_name[vap->iv_state],
2109                                   ieee80211_state_name[vap->iv_nstate]);
2110 #endif
2111                     }
2112           }
2113 
2114           nrunning = nscanning = 0;
2115           /* XXX can track this state instead of calculating */
2116           TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
2117                     if (vp != vap) {
2118                               if (vp->iv_state >= IEEE80211_S_RUN)
2119                                         nrunning++;
2120                               /* XXX doesn't handle bg scan */
2121                               /* NB: CAC+AUTH+ASSOC treated like SCAN */
2122                               else if (vp->iv_state > IEEE80211_S_INIT)
2123                                         nscanning++;
2124                     }
2125           }
2126           ostate = vap->iv_state;
2127           IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2128               "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
2129               ieee80211_state_name[ostate], ieee80211_state_name[nstate],
2130               nrunning, nscanning);
2131           switch (nstate) {
2132           case IEEE80211_S_SCAN:
2133                     if (ostate == IEEE80211_S_INIT) {
2134                               /*
2135                                * INIT -> SCAN happens on initial bringup.
2136                                */
2137                               KASSERT(!(nscanning && nrunning),
2138                                   ("%d scanning and %d running", nscanning, nrunning));
2139                               if (nscanning) {
2140                                         /*
2141                                          * Someone is scanning, defer our state
2142                                          * change until the work has completed.
2143                                          */
2144                                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2145                                             "%s: defer %s -> %s\n",
2146                                             __func__, ieee80211_state_name[ostate],
2147                                             ieee80211_state_name[nstate]);
2148                                         vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2149                                         return 0;
2150                               }
2151                               if (nrunning) {
2152                                         /*
2153                                          * Someone is operating; just join the channel
2154                                          * they have chosen.
2155                                          */
2156                                         /* XXX kill arg? */
2157                                         /* XXX check each opmode, adhoc? */
2158                                         if (vap->iv_opmode == IEEE80211_M_STA)
2159                                                   nstate = IEEE80211_S_SCAN;
2160                                         else
2161                                                   nstate = IEEE80211_S_RUN;
2162 #ifdef IEEE80211_DEBUG
2163                                         if (nstate != IEEE80211_S_SCAN) {
2164                                                   IEEE80211_DPRINTF(vap,
2165                                                       IEEE80211_MSG_STATE,
2166                                                       "%s: override, now %s -> %s\n",
2167                                                       __func__,
2168                                                       ieee80211_state_name[ostate],
2169                                                       ieee80211_state_name[nstate]);
2170                                         }
2171 #endif
2172                               }
2173                     }
2174                     break;
2175           case IEEE80211_S_RUN:
2176                     if (vap->iv_opmode == IEEE80211_M_WDS &&
2177                         (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
2178                         nscanning) {
2179                               /*
2180                                * Legacy WDS with someone else scanning; don't
2181                                * go online until that completes as we should
2182                                * follow the other vap to the channel they choose.
2183                                */
2184                               IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2185                                    "%s: defer %s -> %s (legacy WDS)\n", __func__,
2186                                    ieee80211_state_name[ostate],
2187                                    ieee80211_state_name[nstate]);
2188                               vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
2189                               return 0;
2190                     }
2191                     if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
2192                         IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2193                         (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
2194                         !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
2195                               /*
2196                                * This is a DFS channel, transition to CAC state
2197                                * instead of RUN.  This allows us to initiate
2198                                * Channel Availability Check (CAC) as specified
2199                                * by 11h/DFS.
2200                                */
2201                               nstate = IEEE80211_S_CAC;
2202                               IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
2203                                    "%s: override %s -> %s (DFS)\n", __func__,
2204                                    ieee80211_state_name[ostate],
2205                                    ieee80211_state_name[nstate]);
2206                     }
2207                     break;
2208           case IEEE80211_S_INIT:
2209                     /* cancel any scan in progress */
2210                     ieee80211_cancel_scan(vap);
2211                     if (ostate == IEEE80211_S_INIT ) {
2212                               /* XXX don't believe this */
2213                               /* INIT -> INIT. nothing to do */
2214                               vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
2215                     }
2216                     /* fall thru... */
2217           default:
2218                     break;
2219           }
2220           /* defer the state change to a thread */
2221           vap->iv_nstate = nstate;
2222           vap->iv_nstate_arg = arg;
2223           vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
2224           ieee80211_runtask(ic, &vap->iv_nstate_task);
2225           return EINPROGRESS;
2226 }
2227 
2228 int
ieee80211_new_state(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2229 ieee80211_new_state(struct ieee80211vap *vap,
2230           enum ieee80211_state nstate, int arg)
2231 {
2232           struct ieee80211com *ic = vap->iv_ic;
2233           int rc;
2234 
2235           IEEE80211_LOCK(ic);
2236           rc = ieee80211_new_state_locked(vap, nstate, arg);
2237           IEEE80211_UNLOCK(ic);
2238           return rc;
2239 }
2240