The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net80211/ieee80211_hostap.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   24  */
   25 
   26 #include <sys/cdefs.h>
   27 #ifdef __FreeBSD__
   28 __FBSDID("$FreeBSD: releng/10.4/sys/net80211/ieee80211_hostap.c 262007 2014-02-17 01:36:53Z kevlo $");
   29 #endif
   30 
   31 /*
   32  * IEEE 802.11 HOSTAP mode support.
   33  */
   34 #include "opt_inet.h"
   35 #include "opt_wlan.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h> 
   39 #include <sys/mbuf.h>   
   40 #include <sys/malloc.h>
   41 #include <sys/kernel.h>
   42 
   43 #include <sys/socket.h>
   44 #include <sys/sockio.h>
   45 #include <sys/endian.h>
   46 #include <sys/errno.h>
   47 #include <sys/proc.h>
   48 #include <sys/sysctl.h>
   49 
   50 #include <net/if.h>
   51 #include <net/if_media.h>
   52 #include <net/if_llc.h>
   53 #include <net/ethernet.h>
   54 
   55 #include <net/bpf.h>
   56 
   57 #include <net80211/ieee80211_var.h>
   58 #include <net80211/ieee80211_hostap.h>
   59 #include <net80211/ieee80211_input.h>
   60 #ifdef IEEE80211_SUPPORT_SUPERG
   61 #include <net80211/ieee80211_superg.h>
   62 #endif
   63 #include <net80211/ieee80211_wds.h>
   64 
   65 #define IEEE80211_RATE2MBS(r)   (((r) & IEEE80211_RATE_VAL) / 2)
   66 
   67 static  void hostap_vattach(struct ieee80211vap *);
   68 static  int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
   69 static  int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
   70             int rssi, int nf);
   71 static void hostap_deliver_data(struct ieee80211vap *,
   72             struct ieee80211_node *, struct mbuf *);
   73 static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
   74             int subtype, int rssi, int nf);
   75 static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
   76 
   77 void
   78 ieee80211_hostap_attach(struct ieee80211com *ic)
   79 {
   80         ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
   81 }
   82 
   83 void
   84 ieee80211_hostap_detach(struct ieee80211com *ic)
   85 {
   86 }
   87 
   88 static void
   89 hostap_vdetach(struct ieee80211vap *vap)
   90 {
   91 }
   92 
   93 static void
   94 hostap_vattach(struct ieee80211vap *vap)
   95 {
   96         vap->iv_newstate = hostap_newstate;
   97         vap->iv_input = hostap_input;
   98         vap->iv_recv_mgmt = hostap_recv_mgmt;
   99         vap->iv_recv_ctl = hostap_recv_ctl;
  100         vap->iv_opdetach = hostap_vdetach;
  101         vap->iv_deliver_data = hostap_deliver_data;
  102         vap->iv_recv_pspoll = ieee80211_recv_pspoll;
  103 }
  104 
  105 static void
  106 sta_disassoc(void *arg, struct ieee80211_node *ni)
  107 {
  108         struct ieee80211vap *vap = arg;
  109 
  110         if (ni->ni_vap == vap && ni->ni_associd != 0) {
  111                 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
  112                         IEEE80211_REASON_ASSOC_LEAVE);
  113                 ieee80211_node_leave(ni);
  114         }
  115 }
  116 
  117 static void
  118 sta_csa(void *arg, struct ieee80211_node *ni)
  119 {
  120         struct ieee80211vap *vap = arg;
  121 
  122         if (ni->ni_vap == vap && ni->ni_associd != 0)
  123                 if (ni->ni_inact > vap->iv_inact_init) {
  124                         ni->ni_inact = vap->iv_inact_init;
  125                         IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
  126                             "%s: inact %u", __func__, ni->ni_inact);
  127                 }
  128 }
  129 
  130 static void
  131 sta_drop(void *arg, struct ieee80211_node *ni)
  132 {
  133         struct ieee80211vap *vap = arg;
  134 
  135         if (ni->ni_vap == vap && ni->ni_associd != 0)
  136                 ieee80211_node_leave(ni);
  137 }
  138 
  139 /*
  140  * Does a channel change require associated stations to re-associate
  141  * so protocol state is correct.  This is used when doing CSA across
  142  * bands or similar (e.g. HT -> legacy).
  143  */
  144 static int
  145 isbandchange(struct ieee80211com *ic)
  146 {
  147         return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
  148             (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
  149              IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
  150 }
  151 
  152 /*
  153  * IEEE80211_M_HOSTAP vap state machine handler.
  154  */
  155 static int
  156 hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
  157 {
  158         struct ieee80211com *ic = vap->iv_ic;
  159         enum ieee80211_state ostate;
  160 
  161         IEEE80211_LOCK_ASSERT(ic);
  162 
  163         ostate = vap->iv_state;
  164         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
  165             __func__, ieee80211_state_name[ostate],
  166             ieee80211_state_name[nstate], arg);
  167         vap->iv_state = nstate;                 /* state transition */
  168         if (ostate != IEEE80211_S_SCAN)
  169                 ieee80211_cancel_scan(vap);     /* background scan */
  170         switch (nstate) {
  171         case IEEE80211_S_INIT:
  172                 switch (ostate) {
  173                 case IEEE80211_S_SCAN:
  174                         ieee80211_cancel_scan(vap);
  175                         break;
  176                 case IEEE80211_S_CAC:
  177                         ieee80211_dfs_cac_stop(vap);
  178                         break;
  179                 case IEEE80211_S_RUN:
  180                         ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
  181                         break;
  182                 default:
  183                         break;
  184                 }
  185                 if (ostate != IEEE80211_S_INIT) {
  186                         /* NB: optimize INIT -> INIT case */
  187                         ieee80211_reset_bss(vap);
  188                 }
  189                 if (vap->iv_auth->ia_detach != NULL)
  190                         vap->iv_auth->ia_detach(vap);
  191                 break;
  192         case IEEE80211_S_SCAN:
  193                 switch (ostate) {
  194                 case IEEE80211_S_CSA:
  195                 case IEEE80211_S_RUN:
  196                         ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
  197                         /*
  198                          * Clear overlapping BSS state; the beacon frame
  199                          * will be reconstructed on transition to the RUN
  200                          * state and the timeout routines check if the flag
  201                          * is set before doing anything so this is sufficient.
  202                          */
  203                         ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
  204                         ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
  205                         /* fall thru... */
  206                 case IEEE80211_S_CAC:
  207                         /*
  208                          * NB: We may get here because of a manual channel
  209                          *     change in which case we need to stop CAC
  210                          * XXX no need to stop if ostate RUN but it's ok
  211                          */
  212                         ieee80211_dfs_cac_stop(vap);
  213                         /* fall thru... */
  214                 case IEEE80211_S_INIT:
  215                         if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
  216                             !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
  217                                 /*
  218                                  * Already have a channel; bypass the
  219                                  * scan and startup immediately.  
  220                                  * ieee80211_create_ibss will call back to
  221                                  * move us to RUN state.
  222                                  */
  223                                 ieee80211_create_ibss(vap, vap->iv_des_chan);
  224                                 break;
  225                         }
  226                         /*
  227                          * Initiate a scan.  We can come here as a result
  228                          * of an IEEE80211_IOC_SCAN_REQ too in which case
  229                          * the vap will be marked with IEEE80211_FEXT_SCANREQ
  230                          * and the scan request parameters will be present
  231                          * in iv_scanreq.  Otherwise we do the default.
  232                          */
  233                         if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
  234                                 ieee80211_check_scan(vap,
  235                                     vap->iv_scanreq_flags,
  236                                     vap->iv_scanreq_duration,
  237                                     vap->iv_scanreq_mindwell,
  238                                     vap->iv_scanreq_maxdwell,
  239                                     vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
  240                                 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
  241                         } else
  242                                 ieee80211_check_scan_current(vap);
  243                         break;
  244                 case IEEE80211_S_SCAN:
  245                         /*
  246                          * A state change requires a reset; scan.
  247                          */
  248                         ieee80211_check_scan_current(vap);
  249                         break;
  250                 default:
  251                         break;
  252                 }
  253                 break;
  254         case IEEE80211_S_CAC:
  255                 /*
  256                  * Start CAC on a DFS channel.  We come here when starting
  257                  * a bss on a DFS channel (see ieee80211_create_ibss).
  258                  */
  259                 ieee80211_dfs_cac_start(vap);
  260                 break;
  261         case IEEE80211_S_RUN:
  262                 if (vap->iv_flags & IEEE80211_F_WPA) {
  263                         /* XXX validate prerequisites */
  264                 }
  265                 switch (ostate) {
  266                 case IEEE80211_S_INIT:
  267                         /*
  268                          * Already have a channel; bypass the
  269                          * scan and startup immediately.
  270                          * Note that ieee80211_create_ibss will call
  271                          * back to do a RUN->RUN state change.
  272                          */
  273                         ieee80211_create_ibss(vap,
  274                             ieee80211_ht_adjust_channel(ic,
  275                                 ic->ic_curchan, vap->iv_flags_ht));
  276                         /* NB: iv_bss is changed on return */
  277                         break;
  278                 case IEEE80211_S_CAC:
  279                         /*
  280                          * NB: This is the normal state change when CAC
  281                          * expires and no radar was detected; no need to
  282                          * clear the CAC timer as it's already expired.
  283                          */
  284                         /* fall thru... */
  285                 case IEEE80211_S_CSA:
  286                         /*
  287                          * Shorten inactivity timer of associated stations
  288                          * to weed out sta's that don't follow a CSA.
  289                          */
  290                         ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
  291                         /*
  292                          * Update bss node channel to reflect where
  293                          * we landed after CSA.
  294                          */
  295                         ieee80211_node_set_chan(vap->iv_bss,
  296                             ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
  297                                 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
  298                         /* XXX bypass debug msgs */
  299                         break;
  300                 case IEEE80211_S_SCAN:
  301                 case IEEE80211_S_RUN:
  302 #ifdef IEEE80211_DEBUG
  303                         if (ieee80211_msg_debug(vap)) {
  304                                 struct ieee80211_node *ni = vap->iv_bss;
  305                                 ieee80211_note(vap,
  306                                     "synchronized with %s ssid ",
  307                                     ether_sprintf(ni->ni_bssid));
  308                                 ieee80211_print_essid(ni->ni_essid,
  309                                     ni->ni_esslen);
  310                                 /* XXX MCS/HT */
  311                                 printf(" channel %d start %uMb\n",
  312                                     ieee80211_chan2ieee(ic, ic->ic_curchan),
  313                                     IEEE80211_RATE2MBS(ni->ni_txrate));
  314                         }
  315 #endif
  316                         break;
  317                 default:
  318                         break;
  319                 }
  320                 /*
  321                  * Start/stop the authenticator.  We delay until here
  322                  * to allow configuration to happen out of order.
  323                  */
  324                 if (vap->iv_auth->ia_attach != NULL) {
  325                         /* XXX check failure */
  326                         vap->iv_auth->ia_attach(vap);
  327                 } else if (vap->iv_auth->ia_detach != NULL) {
  328                         vap->iv_auth->ia_detach(vap);
  329                 }
  330                 ieee80211_node_authorize(vap->iv_bss);
  331                 break;
  332         case IEEE80211_S_CSA:
  333                 if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
  334                         /*
  335                          * On a ``band change'' silently drop associated
  336                          * stations as they must re-associate before they
  337                          * can pass traffic (as otherwise protocol state
  338                          * such as capabilities and the negotiated rate
  339                          * set may/will be wrong).
  340                          */
  341                         ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
  342                 }
  343                 break;
  344         default:
  345                 break;
  346         }
  347         return 0;
  348 }
  349 
  350 static void
  351 hostap_deliver_data(struct ieee80211vap *vap,
  352         struct ieee80211_node *ni, struct mbuf *m)
  353 {
  354         struct ether_header *eh = mtod(m, struct ether_header *);
  355         struct ifnet *ifp = vap->iv_ifp;
  356 
  357         /* clear driver/net80211 flags before passing up */
  358 #if __FreeBSD_version >= 1000046
  359         m->m_flags &= ~(M_MCAST | M_BCAST);
  360         m_clrprotoflags(m);
  361 #else
  362         m->m_flags &= ~(M_80211_RX | M_MCAST | M_BCAST);
  363 #endif
  364 
  365         KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
  366             ("gack, opmode %d", vap->iv_opmode));
  367         /*
  368          * Do accounting.
  369          */
  370         ifp->if_ipackets++;
  371         IEEE80211_NODE_STAT(ni, rx_data);
  372         IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
  373         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
  374                 m->m_flags |= M_MCAST;          /* XXX M_BCAST? */
  375                 IEEE80211_NODE_STAT(ni, rx_mcast);
  376         } else
  377                 IEEE80211_NODE_STAT(ni, rx_ucast);
  378 
  379         /* perform as a bridge within the AP */
  380         if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
  381                 struct mbuf *mcopy = NULL;
  382 
  383                 if (m->m_flags & M_MCAST) {
  384                         mcopy = m_dup(m, M_NOWAIT);
  385                         if (mcopy == NULL)
  386                                 ifp->if_oerrors++;
  387                         else
  388                                 mcopy->m_flags |= M_MCAST;
  389                 } else {
  390                         /*
  391                          * Check if the destination is associated with the
  392                          * same vap and authorized to receive traffic.
  393                          * Beware of traffic destined for the vap itself;
  394                          * sending it will not work; just let it be delivered
  395                          * normally.
  396                          */
  397                         struct ieee80211_node *sta = ieee80211_find_vap_node(
  398                              &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
  399                         if (sta != NULL) {
  400                                 if (ieee80211_node_is_authorized(sta)) {
  401                                         /*
  402                                          * Beware of sending to ourself; this
  403                                          * needs to happen via the normal
  404                                          * input path.
  405                                          */
  406                                         if (sta != vap->iv_bss) {
  407                                                 mcopy = m;
  408                                                 m = NULL;
  409                                         }
  410                                 } else {
  411                                         vap->iv_stats.is_rx_unauth++;
  412                                         IEEE80211_NODE_STAT(sta, rx_unauth);
  413                                 }
  414                                 ieee80211_free_node(sta);
  415                         }
  416                 }
  417                 if (mcopy != NULL) {
  418                         int len, err;
  419                         len = mcopy->m_pkthdr.len;
  420                         err = ieee80211_vap_xmitpkt(vap, mcopy);
  421                         if (err) {
  422                                 /* NB: IFQ_HANDOFF reclaims mcopy */
  423                         } else {
  424                                 ifp->if_opackets++;
  425                         }
  426                 }
  427         }
  428         if (m != NULL) {
  429                 /*
  430                  * Mark frame as coming from vap's interface.
  431                  */
  432                 m->m_pkthdr.rcvif = ifp;
  433                 if (m->m_flags & M_MCAST) {
  434                         /*
  435                          * Spam DWDS vap's w/ multicast traffic.
  436                          */
  437                         /* XXX only if dwds in use? */
  438                         ieee80211_dwds_mcast(vap, m);
  439                 }
  440                 if (ni->ni_vlan != 0) {
  441                         /* attach vlan tag */
  442                         m->m_pkthdr.ether_vtag = ni->ni_vlan;
  443                         m->m_flags |= M_VLANTAG;
  444                 }
  445                 ifp->if_input(ifp, m);
  446         }
  447 }
  448 
  449 /*
  450  * Decide if a received management frame should be
  451  * printed when debugging is enabled.  This filters some
  452  * of the less interesting frames that come frequently
  453  * (e.g. beacons).
  454  */
  455 static __inline int
  456 doprint(struct ieee80211vap *vap, int subtype)
  457 {
  458         switch (subtype) {
  459         case IEEE80211_FC0_SUBTYPE_BEACON:
  460                 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
  461         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
  462                 return 0;
  463         }
  464         return 1;
  465 }
  466 
  467 /*
  468  * Process a received frame.  The node associated with the sender
  469  * should be supplied.  If nothing was found in the node table then
  470  * the caller is assumed to supply a reference to iv_bss instead.
  471  * The RSSI and a timestamp are also supplied.  The RSSI data is used
  472  * during AP scanning to select a AP to associate with; it can have
  473  * any units so long as values have consistent units and higher values
  474  * mean ``better signal''.  The receive timestamp is currently not used
  475  * by the 802.11 layer.
  476  */
  477 static int
  478 hostap_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
  479 {
  480 #define HAS_SEQ(type)   ((type & 0x4) == 0)
  481         struct ieee80211vap *vap = ni->ni_vap;
  482         struct ieee80211com *ic = ni->ni_ic;
  483         struct ifnet *ifp = vap->iv_ifp;
  484         struct ieee80211_frame *wh;
  485         struct ieee80211_key *key;
  486         struct ether_header *eh;
  487         int hdrspace, need_tap = 1;     /* mbuf need to be tapped. */
  488         uint8_t dir, type, subtype, qos;
  489         uint8_t *bssid;
  490         uint16_t rxseq;
  491 
  492         if (m->m_flags & M_AMPDU_MPDU) {
  493                 /*
  494                  * Fastpath for A-MPDU reorder q resubmission.  Frames
  495                  * w/ M_AMPDU_MPDU marked have already passed through
  496                  * here but were received out of order and been held on
  497                  * the reorder queue.  When resubmitted they are marked
  498                  * with the M_AMPDU_MPDU flag and we can bypass most of
  499                  * the normal processing.
  500                  */
  501                 wh = mtod(m, struct ieee80211_frame *);
  502                 type = IEEE80211_FC0_TYPE_DATA;
  503                 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
  504                 subtype = IEEE80211_FC0_SUBTYPE_QOS;
  505                 hdrspace = ieee80211_hdrspace(ic, wh);  /* XXX optimize? */
  506                 goto resubmit_ampdu;
  507         }
  508 
  509         KASSERT(ni != NULL, ("null node"));
  510         ni->ni_inact = ni->ni_inact_reload;
  511 
  512         type = -1;                      /* undefined */
  513 
  514         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
  515                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
  516                     ni->ni_macaddr, NULL,
  517                     "too short (1): len %u", m->m_pkthdr.len);
  518                 vap->iv_stats.is_rx_tooshort++;
  519                 goto out;
  520         }
  521         /*
  522          * Bit of a cheat here, we use a pointer for a 3-address
  523          * frame format but don't reference fields past outside
  524          * ieee80211_frame_min w/o first validating the data is
  525          * present.
  526          */
  527         wh = mtod(m, struct ieee80211_frame *);
  528 
  529         if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
  530             IEEE80211_FC0_VERSION_0) {
  531                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
  532                     ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
  533                     wh->i_fc[0], wh->i_fc[1]);
  534                 vap->iv_stats.is_rx_badversion++;
  535                 goto err;
  536         }
  537 
  538         dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
  539         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
  540         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
  541         if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
  542                 if (dir != IEEE80211_FC1_DIR_NODS)
  543                         bssid = wh->i_addr1;
  544                 else if (type == IEEE80211_FC0_TYPE_CTL)
  545                         bssid = wh->i_addr1;
  546                 else {
  547                         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
  548                                 IEEE80211_DISCARD_MAC(vap,
  549                                     IEEE80211_MSG_ANY, ni->ni_macaddr,
  550                                     NULL, "too short (2): len %u",
  551                                     m->m_pkthdr.len);
  552                                 vap->iv_stats.is_rx_tooshort++;
  553                                 goto out;
  554                         }
  555                         bssid = wh->i_addr3;
  556                 }
  557                 /*
  558                  * Validate the bssid.
  559                  */
  560                 if (!(type == IEEE80211_FC0_TYPE_MGT &&
  561                       subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
  562                     !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
  563                     !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
  564                         /* not interested in */
  565                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
  566                             bssid, NULL, "%s", "not to bss");
  567                         vap->iv_stats.is_rx_wrongbss++;
  568                         goto out;
  569                 }
  570 
  571                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
  572                 ni->ni_noise = nf;
  573                 if (HAS_SEQ(type)) {
  574                         uint8_t tid = ieee80211_gettid(wh);
  575                         if (IEEE80211_QOS_HAS_SEQ(wh) &&
  576                             TID_TO_WME_AC(tid) >= WME_AC_VI)
  577                                 ic->ic_wme.wme_hipri_traffic++;
  578                         rxseq = le16toh(*(uint16_t *)wh->i_seq);
  579                         if (! ieee80211_check_rxseq(ni, wh)) {
  580                                 /* duplicate, discard */
  581                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
  582                                     bssid, "duplicate",
  583                                     "seqno <%u,%u> fragno <%u,%u> tid %u",
  584                                     rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
  585                                     ni->ni_rxseqs[tid] >>
  586                                         IEEE80211_SEQ_SEQ_SHIFT,
  587                                     rxseq & IEEE80211_SEQ_FRAG_MASK,
  588                                     ni->ni_rxseqs[tid] &
  589                                         IEEE80211_SEQ_FRAG_MASK,
  590                                     tid);
  591                                 vap->iv_stats.is_rx_dup++;
  592                                 IEEE80211_NODE_STAT(ni, rx_dup);
  593                                 goto out;
  594                         }
  595                         ni->ni_rxseqs[tid] = rxseq;
  596                 }
  597         }
  598 
  599         switch (type) {
  600         case IEEE80211_FC0_TYPE_DATA:
  601                 hdrspace = ieee80211_hdrspace(ic, wh);
  602                 if (m->m_len < hdrspace &&
  603                     (m = m_pullup(m, hdrspace)) == NULL) {
  604                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
  605                             ni->ni_macaddr, NULL,
  606                             "data too short: expecting %u", hdrspace);
  607                         vap->iv_stats.is_rx_tooshort++;
  608                         goto out;               /* XXX */
  609                 }
  610                 if (!(dir == IEEE80211_FC1_DIR_TODS ||
  611                      (dir == IEEE80211_FC1_DIR_DSTODS &&
  612                       (vap->iv_flags & IEEE80211_F_DWDS)))) {
  613                         if (dir != IEEE80211_FC1_DIR_DSTODS) {
  614                                 IEEE80211_DISCARD(vap,
  615                                     IEEE80211_MSG_INPUT, wh, "data",
  616                                     "incorrect dir 0x%x", dir);
  617                         } else {
  618                                 IEEE80211_DISCARD(vap,
  619                                     IEEE80211_MSG_INPUT |
  620                                     IEEE80211_MSG_WDS, wh,
  621                                     "4-address data",
  622                                     "%s", "DWDS not enabled");
  623                         }
  624                         vap->iv_stats.is_rx_wrongdir++;
  625                         goto out;
  626                 }
  627                 /* check if source STA is associated */
  628                 if (ni == vap->iv_bss) {
  629                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  630                             wh, "data", "%s", "unknown src");
  631                         ieee80211_send_error(ni, wh->i_addr2,
  632                             IEEE80211_FC0_SUBTYPE_DEAUTH,
  633                             IEEE80211_REASON_NOT_AUTHED);
  634                         vap->iv_stats.is_rx_notassoc++;
  635                         goto err;
  636                 }
  637                 if (ni->ni_associd == 0) {
  638                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  639                             wh, "data", "%s", "unassoc src");
  640                         IEEE80211_SEND_MGMT(ni,
  641                             IEEE80211_FC0_SUBTYPE_DISASSOC,
  642                             IEEE80211_REASON_NOT_ASSOCED);
  643                         vap->iv_stats.is_rx_notassoc++;
  644                         goto err;
  645                 }
  646 
  647                 /*
  648                  * Check for power save state change.
  649                  * XXX out-of-order A-MPDU frames?
  650                  */
  651                 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
  652                     (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
  653                         vap->iv_node_ps(ni,
  654                                 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
  655                 /*
  656                  * For 4-address packets handle WDS discovery
  657                  * notifications.  Once a WDS link is setup frames
  658                  * are just delivered to the WDS vap (see below).
  659                  */
  660                 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
  661                         if (!ieee80211_node_is_authorized(ni)) {
  662                                 IEEE80211_DISCARD(vap,
  663                                     IEEE80211_MSG_INPUT |
  664                                     IEEE80211_MSG_WDS, wh,
  665                                     "4-address data",
  666                                     "%s", "unauthorized port");
  667                                 vap->iv_stats.is_rx_unauth++;
  668                                 IEEE80211_NODE_STAT(ni, rx_unauth);
  669                                 goto err;
  670                         }
  671                         ieee80211_dwds_discover(ni, m);
  672                         return type;
  673                 }
  674 
  675                 /*
  676                  * Handle A-MPDU re-ordering.  If the frame is to be
  677                  * processed directly then ieee80211_ampdu_reorder
  678                  * will return 0; otherwise it has consumed the mbuf
  679                  * and we should do nothing more with it.
  680                  */
  681                 if ((m->m_flags & M_AMPDU) &&
  682                     ieee80211_ampdu_reorder(ni, m) != 0) {
  683                         m = NULL;
  684                         goto out;
  685                 }
  686         resubmit_ampdu:
  687 
  688                 /*
  689                  * Handle privacy requirements.  Note that we
  690                  * must not be preempted from here until after
  691                  * we (potentially) call ieee80211_crypto_demic;
  692                  * otherwise we may violate assumptions in the
  693                  * crypto cipher modules used to do delayed update
  694                  * of replay sequence numbers.
  695                  */
  696                 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
  697                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
  698                                 /*
  699                                  * Discard encrypted frames when privacy is off.
  700                                  */
  701                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  702                                     wh, "WEP", "%s", "PRIVACY off");
  703                                 vap->iv_stats.is_rx_noprivacy++;
  704                                 IEEE80211_NODE_STAT(ni, rx_noprivacy);
  705                                 goto out;
  706                         }
  707                         key = ieee80211_crypto_decap(ni, m, hdrspace);
  708                         if (key == NULL) {
  709                                 /* NB: stats+msgs handled in crypto_decap */
  710                                 IEEE80211_NODE_STAT(ni, rx_wepfail);
  711                                 goto out;
  712                         }
  713                         wh = mtod(m, struct ieee80211_frame *);
  714                         wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
  715                 } else {
  716                         /* XXX M_WEP and IEEE80211_F_PRIVACY */
  717                         key = NULL;
  718                 }
  719 
  720                 /*
  721                  * Save QoS bits for use below--before we strip the header.
  722                  */
  723                 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
  724                         qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
  725                             ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
  726                             ((struct ieee80211_qosframe *)wh)->i_qos[0];
  727                 } else
  728                         qos = 0;
  729 
  730                 /*
  731                  * Next up, any fragmentation.
  732                  */
  733                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
  734                         m = ieee80211_defrag(ni, m, hdrspace);
  735                         if (m == NULL) {
  736                                 /* Fragment dropped or frame not complete yet */
  737                                 goto out;
  738                         }
  739                 }
  740                 wh = NULL;              /* no longer valid, catch any uses */
  741 
  742                 /*
  743                  * Next strip any MSDU crypto bits.
  744                  */
  745                 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
  746                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
  747                             ni->ni_macaddr, "data", "%s", "demic error");
  748                         vap->iv_stats.is_rx_demicfail++;
  749                         IEEE80211_NODE_STAT(ni, rx_demicfail);
  750                         goto out;
  751                 }
  752                 /* copy to listener after decrypt */
  753                 if (ieee80211_radiotap_active_vap(vap))
  754                         ieee80211_radiotap_rx(vap, m);
  755                 need_tap = 0;
  756                 /*
  757                  * Finally, strip the 802.11 header.
  758                  */
  759                 m = ieee80211_decap(vap, m, hdrspace);
  760                 if (m == NULL) {
  761                         /* XXX mask bit to check for both */
  762                         /* don't count Null data frames as errors */
  763                         if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
  764                             subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
  765                                 goto out;
  766                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
  767                             ni->ni_macaddr, "data", "%s", "decap error");
  768                         vap->iv_stats.is_rx_decap++;
  769                         IEEE80211_NODE_STAT(ni, rx_decap);
  770                         goto err;
  771                 }
  772                 eh = mtod(m, struct ether_header *);
  773                 if (!ieee80211_node_is_authorized(ni)) {
  774                         /*
  775                          * Deny any non-PAE frames received prior to
  776                          * authorization.  For open/shared-key
  777                          * authentication the port is mark authorized
  778                          * after authentication completes.  For 802.1x
  779                          * the port is not marked authorized by the
  780                          * authenticator until the handshake has completed.
  781                          */
  782                         if (eh->ether_type != htons(ETHERTYPE_PAE)) {
  783                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
  784                                     eh->ether_shost, "data",
  785                                     "unauthorized port: ether type 0x%x len %u",
  786                                     eh->ether_type, m->m_pkthdr.len);
  787                                 vap->iv_stats.is_rx_unauth++;
  788                                 IEEE80211_NODE_STAT(ni, rx_unauth);
  789                                 goto err;
  790                         }
  791                 } else {
  792                         /*
  793                          * When denying unencrypted frames, discard
  794                          * any non-PAE frames received without encryption.
  795                          */
  796                         if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
  797                             (key == NULL && (m->m_flags & M_WEP) == 0) &&
  798                             eh->ether_type != htons(ETHERTYPE_PAE)) {
  799                                 /*
  800                                  * Drop unencrypted frames.
  801                                  */
  802                                 vap->iv_stats.is_rx_unencrypted++;
  803                                 IEEE80211_NODE_STAT(ni, rx_unencrypted);
  804                                 goto out;
  805                         }
  806                 }
  807                 /* XXX require HT? */
  808                 if (qos & IEEE80211_QOS_AMSDU) {
  809                         m = ieee80211_decap_amsdu(ni, m);
  810                         if (m == NULL)
  811                                 return IEEE80211_FC0_TYPE_DATA;
  812                 } else {
  813 #ifdef IEEE80211_SUPPORT_SUPERG
  814                         m = ieee80211_decap_fastframe(vap, ni, m);
  815                         if (m == NULL)
  816                                 return IEEE80211_FC0_TYPE_DATA;
  817 #endif
  818                 }
  819                 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
  820                         ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
  821                 else
  822                         hostap_deliver_data(vap, ni, m);
  823                 return IEEE80211_FC0_TYPE_DATA;
  824 
  825         case IEEE80211_FC0_TYPE_MGT:
  826                 vap->iv_stats.is_rx_mgmt++;
  827                 IEEE80211_NODE_STAT(ni, rx_mgmt);
  828                 if (dir != IEEE80211_FC1_DIR_NODS) {
  829                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  830                             wh, "mgt", "incorrect dir 0x%x", dir);
  831                         vap->iv_stats.is_rx_wrongdir++;
  832                         goto err;
  833                 }
  834                 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
  835                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
  836                             ni->ni_macaddr, "mgt", "too short: len %u",
  837                             m->m_pkthdr.len);
  838                         vap->iv_stats.is_rx_tooshort++;
  839                         goto out;
  840                 }
  841                 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
  842                         /* ensure return frames are unicast */
  843                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
  844                             wh, NULL, "source is multicast: %s",
  845                             ether_sprintf(wh->i_addr2));
  846                         vap->iv_stats.is_rx_mgtdiscard++;       /* XXX stat */
  847                         goto out;
  848                 }
  849 #ifdef IEEE80211_DEBUG
  850                 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
  851                     ieee80211_msg_dumppkts(vap)) {
  852                         if_printf(ifp, "received %s from %s rssi %d\n",
  853                             ieee80211_mgt_subtype_name[subtype >>
  854                                 IEEE80211_FC0_SUBTYPE_SHIFT],
  855                             ether_sprintf(wh->i_addr2), rssi);
  856                 }
  857 #endif
  858                 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
  859                         if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
  860                                 /*
  861                                  * Only shared key auth frames with a challenge
  862                                  * should be encrypted, discard all others.
  863                                  */
  864                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  865                                     wh, NULL,
  866                                     "%s", "WEP set but not permitted");
  867                                 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
  868                                 goto out;
  869                         }
  870                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
  871                                 /*
  872                                  * Discard encrypted frames when privacy is off.
  873                                  */
  874                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  875                                     wh, NULL, "%s", "WEP set but PRIVACY off");
  876                                 vap->iv_stats.is_rx_noprivacy++;
  877                                 goto out;
  878                         }
  879                         hdrspace = ieee80211_hdrspace(ic, wh);
  880                         key = ieee80211_crypto_decap(ni, m, hdrspace);
  881                         if (key == NULL) {
  882                                 /* NB: stats+msgs handled in crypto_decap */
  883                                 goto out;
  884                         }
  885                         wh = mtod(m, struct ieee80211_frame *);
  886                         wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
  887                 }
  888                 /*
  889                  * Pass the packet to radiotap before calling iv_recv_mgmt().
  890                  * Otherwise iv_recv_mgmt() might pass another packet to
  891                  * radiotap, resulting in out of order packet captures.
  892                  */
  893                 if (ieee80211_radiotap_active_vap(vap))
  894                         ieee80211_radiotap_rx(vap, m);
  895                 need_tap = 0;
  896                 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
  897                 goto out;
  898 
  899         case IEEE80211_FC0_TYPE_CTL:
  900                 vap->iv_stats.is_rx_ctl++;
  901                 IEEE80211_NODE_STAT(ni, rx_ctrl);
  902                 vap->iv_recv_ctl(ni, m, subtype);
  903                 goto out;
  904         default:
  905                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
  906                     wh, "bad", "frame type 0x%x", type);
  907                 /* should not come here */
  908                 break;
  909         }
  910 err:
  911         ifp->if_ierrors++;
  912 out:
  913         if (m != NULL) {
  914                 if (need_tap && ieee80211_radiotap_active_vap(vap))
  915                         ieee80211_radiotap_rx(vap, m);
  916                 m_freem(m);
  917         }
  918         return type;
  919 }
  920 
  921 static void
  922 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
  923     int rssi, int nf, uint16_t seq, uint16_t status)
  924 {
  925         struct ieee80211vap *vap = ni->ni_vap;
  926 
  927         KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
  928 
  929         if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
  930                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
  931                     ni->ni_macaddr, "open auth",
  932                     "bad sta auth mode %u", ni->ni_authmode);
  933                 vap->iv_stats.is_rx_bad_auth++; /* XXX */
  934                 /*
  935                  * Clear any challenge text that may be there if
  936                  * a previous shared key auth failed and then an
  937                  * open auth is attempted.
  938                  */
  939                 if (ni->ni_challenge != NULL) {
  940                         free(ni->ni_challenge, M_80211_NODE);
  941                         ni->ni_challenge = NULL;
  942                 }
  943                 /* XXX hack to workaround calling convention */
  944                 ieee80211_send_error(ni, wh->i_addr2, 
  945                     IEEE80211_FC0_SUBTYPE_AUTH,
  946                     (seq + 1) | (IEEE80211_STATUS_ALG<<16));
  947                 return;
  948         }
  949         if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
  950                 vap->iv_stats.is_rx_bad_auth++;
  951                 return;
  952         }
  953         /* always accept open authentication requests */
  954         if (ni == vap->iv_bss) {
  955                 ni = ieee80211_dup_bss(vap, wh->i_addr2);
  956                 if (ni == NULL)
  957                         return;
  958         } else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
  959                 (void) ieee80211_ref_node(ni);
  960         /*
  961          * Mark the node as referenced to reflect that it's
  962          * reference count has been bumped to insure it remains
  963          * after the transaction completes.
  964          */
  965         ni->ni_flags |= IEEE80211_NODE_AREF;
  966         /*
  967          * Mark the node as requiring a valid association id
  968          * before outbound traffic is permitted.
  969          */
  970         ni->ni_flags |= IEEE80211_NODE_ASSOCID;
  971 
  972         if (vap->iv_acl != NULL &&
  973             vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
  974                 /*
  975                  * When the ACL policy is set to RADIUS we defer the
  976                  * authorization to a user agent.  Dispatch an event,
  977                  * a subsequent MLME call will decide the fate of the
  978                  * station.  If the user agent is not present then the
  979                  * node will be reclaimed due to inactivity.
  980                  */
  981                 IEEE80211_NOTE_MAC(vap,
  982                     IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
  983                     "%s", "station authentication defered (radius acl)");
  984                 ieee80211_notify_node_auth(ni);
  985         } else {
  986                 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
  987                 IEEE80211_NOTE_MAC(vap,
  988                     IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
  989                     "%s", "station authenticated (open)");
  990                 /*
  991                  * When 802.1x is not in use mark the port
  992                  * authorized at this point so traffic can flow.
  993                  */
  994                 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
  995                         ieee80211_node_authorize(ni);
  996         }
  997 }
  998 
  999 static void
 1000 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
 1001     uint8_t *frm, uint8_t *efrm, int rssi, int nf,
 1002     uint16_t seq, uint16_t status)
 1003 {
 1004         struct ieee80211vap *vap = ni->ni_vap;
 1005         uint8_t *challenge;
 1006         int allocbs, estatus;
 1007 
 1008         KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
 1009 
 1010         /*
 1011          * NB: this can happen as we allow pre-shared key
 1012          * authentication to be enabled w/o wep being turned
 1013          * on so that configuration of these can be done
 1014          * in any order.  It may be better to enforce the
 1015          * ordering in which case this check would just be
 1016          * for sanity/consistency.
 1017          */
 1018         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
 1019                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
 1020                     ni->ni_macaddr, "shared key auth",
 1021                     "%s", " PRIVACY is disabled");
 1022                 estatus = IEEE80211_STATUS_ALG;
 1023                 goto bad;
 1024         }
 1025         /*
 1026          * Pre-shared key authentication is evil; accept
 1027          * it only if explicitly configured (it is supported
 1028          * mainly for compatibility with clients like Mac OS X).
 1029          */
 1030         if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
 1031             ni->ni_authmode != IEEE80211_AUTH_SHARED) {
 1032                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
 1033                     ni->ni_macaddr, "shared key auth",
 1034                     "bad sta auth mode %u", ni->ni_authmode);
 1035                 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
 1036                 estatus = IEEE80211_STATUS_ALG;
 1037                 goto bad;
 1038         }
 1039 
 1040         challenge = NULL;
 1041         if (frm + 1 < efrm) {
 1042                 if ((frm[1] + 2) > (efrm - frm)) {
 1043                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
 1044                             ni->ni_macaddr, "shared key auth",
 1045                             "ie %d/%d too long",
 1046                             frm[0], (frm[1] + 2) - (efrm - frm));
 1047                         vap->iv_stats.is_rx_bad_auth++;
 1048                         estatus = IEEE80211_STATUS_CHALLENGE;
 1049                         goto bad;
 1050                 }
 1051                 if (*frm == IEEE80211_ELEMID_CHALLENGE)
 1052                         challenge = frm;
 1053                 frm += frm[1] + 2;
 1054         }
 1055         switch (seq) {
 1056         case IEEE80211_AUTH_SHARED_CHALLENGE:
 1057         case IEEE80211_AUTH_SHARED_RESPONSE:
 1058                 if (challenge == NULL) {
 1059                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
 1060                             ni->ni_macaddr, "shared key auth",
 1061                             "%s", "no challenge");
 1062                         vap->iv_stats.is_rx_bad_auth++;
 1063                         estatus = IEEE80211_STATUS_CHALLENGE;
 1064                         goto bad;
 1065                 }
 1066                 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
 1067                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
 1068                             ni->ni_macaddr, "shared key auth",
 1069                             "bad challenge len %d", challenge[1]);
 1070                         vap->iv_stats.is_rx_bad_auth++;
 1071                         estatus = IEEE80211_STATUS_CHALLENGE;
 1072                         goto bad;
 1073                 }
 1074         default:
 1075                 break;
 1076         }
 1077         switch (seq) {
 1078         case IEEE80211_AUTH_SHARED_REQUEST:
 1079                 if (ni == vap->iv_bss) {
 1080                         ni = ieee80211_dup_bss(vap, wh->i_addr2);
 1081                         if (ni == NULL) {
 1082                                 /* NB: no way to return an error */
 1083                                 return;
 1084                         }
 1085                         allocbs = 1;
 1086                 } else {
 1087                         if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
 1088                                 (void) ieee80211_ref_node(ni);
 1089                         allocbs = 0;
 1090                 }
 1091                 /*
 1092                  * Mark the node as referenced to reflect that it's
 1093                  * reference count has been bumped to insure it remains
 1094                  * after the transaction completes.
 1095                  */
 1096                 ni->ni_flags |= IEEE80211_NODE_AREF;
 1097                 /*
 1098                  * Mark the node as requiring a valid associatio id
 1099                  * before outbound traffic is permitted.
 1100                  */
 1101                 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
 1102                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
 1103                 ni->ni_noise = nf;
 1104                 if (!ieee80211_alloc_challenge(ni)) {
 1105                         /* NB: don't return error so they rexmit */
 1106                         return;
 1107                 }
 1108                 get_random_bytes(ni->ni_challenge,
 1109                         IEEE80211_CHALLENGE_LEN);
 1110                 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
 1111                     ni, "shared key %sauth request", allocbs ? "" : "re");
 1112                 /*
 1113                  * When the ACL policy is set to RADIUS we defer the
 1114                  * authorization to a user agent.  Dispatch an event,
 1115                  * a subsequent MLME call will decide the fate of the
 1116                  * station.  If the user agent is not present then the
 1117                  * node will be reclaimed due to inactivity.
 1118                  */
 1119                 if (vap->iv_acl != NULL &&
 1120                     vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
 1121                         IEEE80211_NOTE_MAC(vap,
 1122                             IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
 1123                             ni->ni_macaddr,
 1124                             "%s", "station authentication defered (radius acl)");
 1125                         ieee80211_notify_node_auth(ni);
 1126                         return;
 1127                 }
 1128                 break;
 1129         case IEEE80211_AUTH_SHARED_RESPONSE:
 1130                 if (ni == vap->iv_bss) {
 1131                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
 1132                             ni->ni_macaddr, "shared key response",
 1133                             "%s", "unknown station");
 1134                         /* NB: don't send a response */
 1135                         return;
 1136                 }
 1137                 if (ni->ni_challenge == NULL) {
 1138                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
 1139                             ni->ni_macaddr, "shared key response",
 1140                             "%s", "no challenge recorded");
 1141                         vap->iv_stats.is_rx_bad_auth++;
 1142                         estatus = IEEE80211_STATUS_CHALLENGE;
 1143                         goto bad;
 1144                 }
 1145                 if (memcmp(ni->ni_challenge, &challenge[2],
 1146                            challenge[1]) != 0) {
 1147                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
 1148                             ni->ni_macaddr, "shared key response",
 1149                             "%s", "challenge mismatch");
 1150                         vap->iv_stats.is_rx_auth_fail++;
 1151                         estatus = IEEE80211_STATUS_CHALLENGE;
 1152                         goto bad;
 1153                 }
 1154                 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
 1155                     ni, "%s", "station authenticated (shared key)");
 1156                 ieee80211_node_authorize(ni);
 1157                 break;
 1158         default:
 1159                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
 1160                     ni->ni_macaddr, "shared key auth",
 1161                     "bad seq %d", seq);
 1162                 vap->iv_stats.is_rx_bad_auth++;
 1163                 estatus = IEEE80211_STATUS_SEQUENCE;
 1164                 goto bad;
 1165         }
 1166         IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
 1167         return;
 1168 bad:
 1169         /*
 1170          * Send an error response; but only when operating as an AP.
 1171          */
 1172         /* XXX hack to workaround calling convention */
 1173         ieee80211_send_error(ni, wh->i_addr2,
 1174             IEEE80211_FC0_SUBTYPE_AUTH,
 1175             (seq + 1) | (estatus<<16));
 1176 }
 1177 
 1178 /*
 1179  * Convert a WPA cipher selector OUI to an internal
 1180  * cipher algorithm.  Where appropriate we also
 1181  * record any key length.
 1182  */
 1183 static int
 1184 wpa_cipher(const uint8_t *sel, uint8_t *keylen)
 1185 {
 1186 #define WPA_SEL(x)      (((x)<<24)|WPA_OUI)
 1187         uint32_t w = LE_READ_4(sel);
 1188 
 1189         switch (w) {
 1190         case WPA_SEL(WPA_CSE_NULL):
 1191                 return IEEE80211_CIPHER_NONE;
 1192         case WPA_SEL(WPA_CSE_WEP40):
 1193                 if (keylen)
 1194                         *keylen = 40 / NBBY;
 1195                 return IEEE80211_CIPHER_WEP;
 1196         case WPA_SEL(WPA_CSE_WEP104):
 1197                 if (keylen)
 1198                         *keylen = 104 / NBBY;
 1199                 return IEEE80211_CIPHER_WEP;
 1200         case WPA_SEL(WPA_CSE_TKIP):
 1201                 return IEEE80211_CIPHER_TKIP;
 1202         case WPA_SEL(WPA_CSE_CCMP):
 1203                 return IEEE80211_CIPHER_AES_CCM;
 1204         }
 1205         return 32;              /* NB: so 1<< is discarded */
 1206 #undef WPA_SEL
 1207 }
 1208 
 1209 /*
 1210  * Convert a WPA key management/authentication algorithm
 1211  * to an internal code.
 1212  */
 1213 static int
 1214 wpa_keymgmt(const uint8_t *sel)
 1215 {
 1216 #define WPA_SEL(x)      (((x)<<24)|WPA_OUI)
 1217         uint32_t w = LE_READ_4(sel);
 1218 
 1219         switch (w) {
 1220         case WPA_SEL(WPA_ASE_8021X_UNSPEC):
 1221                 return WPA_ASE_8021X_UNSPEC;
 1222         case WPA_SEL(WPA_ASE_8021X_PSK):
 1223                 return WPA_ASE_8021X_PSK;
 1224         case WPA_SEL(WPA_ASE_NONE):
 1225                 return WPA_ASE_NONE;
 1226         }
 1227         return 0;               /* NB: so is discarded */
 1228 #undef WPA_SEL
 1229 }
 1230 
 1231 /*
 1232  * Parse a WPA information element to collect parameters.
 1233  * Note that we do not validate security parameters; that
 1234  * is handled by the authenticator; the parsing done here
 1235  * is just for internal use in making operational decisions.
 1236  */
 1237 static int
 1238 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
 1239         struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
 1240 {
 1241         uint8_t len = frm[1];
 1242         uint32_t w;
 1243         int n;
 1244 
 1245         /*
 1246          * Check the length once for fixed parts: OUI, type,
 1247          * version, mcast cipher, and 2 selector counts.
 1248          * Other, variable-length data, must be checked separately.
 1249          */
 1250         if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
 1251                 IEEE80211_DISCARD_IE(vap,
 1252                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
 1253                     wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
 1254                 return IEEE80211_REASON_IE_INVALID;
 1255         }
 1256         if (len < 14) {
 1257                 IEEE80211_DISCARD_IE(vap,
 1258                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
 1259                     wh, "WPA", "too short, len %u", len);
 1260                 return IEEE80211_REASON_IE_INVALID;
 1261         }
 1262         frm += 6, len -= 4;             /* NB: len is payload only */
 1263         /* NB: iswpaoui already validated the OUI and type */
 1264         w = LE_READ_2(frm);
 1265         if (w != WPA_VERSION) {
 1266                 IEEE80211_DISCARD_IE(vap,
 1267                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
 1268                     wh, "WPA", "bad version %u", w);
 1269                 return IEEE80211_REASON_IE_INVALID;
 1270         }
 1271         frm += 2, len -= 2;
 1272 
 1273         memset(rsn, 0, sizeof(*rsn));
 1274 
 1275         /* multicast/group cipher */
 1276         rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
 1277         frm += 4, len -= 4;
 1278 
 1279         /* unicast ciphers */
 1280         n = LE_READ_2(frm);
 1281         frm += 2, len -= 2;
 1282         if (len < n*4+2) {
 1283                 IEEE80211_DISCARD_IE(vap,
 1284                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
 1285                     wh, "WPA", "ucast cipher data too short; len %u, n %u",
 1286                     len, n);
 1287                 return IEEE80211_REASON_IE_INVALID;
 1288         }
 1289         w = 0;
 1290         for (; n > 0; n--) {
 1291                 w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
 1292                 frm += 4, len -= 4;
 1293         }
 1294         if (w & (1<<IEEE80211_CIPHER_TKIP))
 1295                 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
 1296         else
 1297                 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
 1298 
 1299         /* key management algorithms */
 1300         n = LE_READ_2(frm);
 1301         frm += 2, len -= 2;
 1302         if (len < n*4) {
 1303                 IEEE80211_DISCARD_IE(vap,
 1304                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
 1305                     wh, "WPA", "key mgmt alg data too short; len %u, n %u",
 1306                     len, n);
 1307                 return IEEE80211_REASON_IE_INVALID;
 1308         }
 1309         w = 0;
 1310         for (; n > 0; n--) {
 1311                 w |= wpa_keymgmt(frm);
 1312                 frm += 4, len -= 4;
 1313         }
 1314         if (w & WPA_ASE_8021X_UNSPEC)
 1315                 rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
 1316         else
 1317                 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
 1318 
 1319         if (len > 2)            /* optional capabilities */
 1320                 rsn->rsn_caps = LE_READ_2(frm);
 1321 
 1322         return 0;
 1323 }
 1324 
 1325 /*
 1326  * Convert an RSN cipher selector OUI to an internal
 1327  * cipher algorithm.  Where appropriate we also
 1328  * record any key length.
 1329  */
 1330 static int
 1331 rsn_cipher(const uint8_t *sel, uint8_t *keylen)
 1332 {
 1333 #define RSN_SEL(x)      (((x)<<24)|RSN_OUI)
 1334         uint32_t w = LE_READ_4(sel);
 1335 
 1336         switch (w) {
 1337         case RSN_SEL(RSN_CSE_NULL):
 1338                 return IEEE80211_CIPHER_NONE;
 1339         case RSN_SEL(RSN_CSE_WEP40):
 1340                 if (keylen)
 1341                         *keylen = 40 / NBBY;
 1342                 return IEEE80211_CIPHER_WEP;
 1343         case RSN_SEL(RSN_CSE_WEP104):
 1344                 if (keylen)
 1345                         *keylen = 104 / NBBY;
 1346                 return IEEE80211_CIPHER_WEP;
 1347         case RSN_SEL(RSN_CSE_TKIP):
 1348                 return IEEE80211_CIPHER_TKIP;
 1349         case RSN_SEL(RSN_CSE_CCMP):
 1350                 return IEEE80211_CIPHER_AES_CCM;
 1351         case RSN_SEL(RSN_CSE_WRAP):
 1352                 return IEEE80211_CIPHER_AES_OCB;
 1353         }
 1354         return 32;              /* NB: so 1<< is discarded */
 1355 #undef WPA_SEL
 1356 }
 1357 
 1358 /*
 1359  * Convert an RSN key management/authentication algorithm
 1360  * to an internal code.
 1361  */
 1362 static int
 1363 rsn_keymgmt(const uint8_t *sel)
 1364 {
 1365 #define RSN_SEL(x)      (((x)<<24)|RSN_OUI)
 1366         uint32_t w = LE_READ_4(sel);
 1367 
 1368         switch (w) {
 1369         case RSN_SEL(RSN_ASE_8021X_UNSPEC):
 1370                 return RSN_ASE_8021X_UNSPEC;
 1371         case RSN_SEL(RSN_ASE_8021X_PSK):
 1372                 return RSN_ASE_8021X_PSK;
 1373         case RSN_SEL(RSN_ASE_NONE):
 1374                 return RSN_ASE_NONE;
 1375         }
 1376         return 0;               /* NB: so is discarded */
 1377 #undef RSN_SEL
 1378 }
 1379 
 1380 /*
 1381  * Parse a WPA/RSN information element to collect parameters
 1382  * and validate the parameters against what has been
 1383  * configured for the system.
 1384  */
 1385 static int
 1386 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
 1387         struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
 1388 {
 1389         uint8_t len = frm[1];
 1390         uint32_t w;
 1391         int n;
 1392 
 1393         /*
 1394          * Check the length once for fixed parts: 
 1395          * version, mcast cipher, and 2 selector counts.
 1396          * Other, variable-length data, must be checked separately.
 1397          */
 1398         if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
 1399                 IEEE80211_DISCARD_IE(vap,
 1400                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
 1401                     wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
 1402                 return IEEE80211_REASON_IE_INVALID;
 1403         }
 1404         if (len < 10) {
 1405                 IEEE80211_DISCARD_IE(vap,
 1406                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
 1407                     wh, "RSN", "too short, len %u", len);
 1408                 return IEEE80211_REASON_IE_INVALID;
 1409         }
 1410         frm += 2;
 1411         w = LE_READ_2(frm);
 1412         if (w != RSN_VERSION) {
 1413                 IEEE80211_DISCARD_IE(vap,
 1414                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
 1415                     wh, "RSN", "bad version %u", w);
 1416                 return IEEE80211_REASON_IE_INVALID;
 1417         }
 1418         frm += 2, len -= 2;
 1419 
 1420         memset(rsn, 0, sizeof(*rsn));
 1421 
 1422         /* multicast/group cipher */
 1423         rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
 1424         frm += 4, len -= 4;
 1425 
 1426         /* unicast ciphers */
 1427         n = LE_READ_2(frm);
 1428         frm += 2, len -= 2;
 1429         if (len < n*4+2) {
 1430                 IEEE80211_DISCARD_IE(vap,
 1431                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
 1432                     wh, "RSN", "ucast cipher data too short; len %u, n %u",
 1433                     len, n);
 1434                 return IEEE80211_REASON_IE_INVALID;
 1435         }
 1436         w = 0;
 1437         for (; n > 0; n--) {
 1438                 w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
 1439                 frm += 4, len -= 4;
 1440         }
 1441         if (w & (1<<IEEE80211_CIPHER_TKIP))
 1442                 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
 1443         else
 1444                 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
 1445 
 1446         /* key management algorithms */
 1447         n = LE_READ_2(frm);
 1448         frm += 2, len -= 2;
 1449         if (len < n*4) {
 1450                 IEEE80211_DISCARD_IE(vap,
 1451                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
 1452                     wh, "RSN", "key mgmt alg data too short; len %u, n %u",
 1453                     len, n);
 1454                 return IEEE80211_REASON_IE_INVALID;
 1455         }
 1456         w = 0;
 1457         for (; n > 0; n--) {
 1458                 w |= rsn_keymgmt(frm);
 1459                 frm += 4, len -= 4;
 1460         }
 1461         if (w & RSN_ASE_8021X_UNSPEC)
 1462                 rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
 1463         else
 1464                 rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
 1465 
 1466         /* optional RSN capabilities */
 1467         if (len > 2)
 1468                 rsn->rsn_caps = LE_READ_2(frm);
 1469         /* XXXPMKID */
 1470 
 1471         return 0;
 1472 }
 1473 
 1474 /*
 1475  * WPA/802.11i assocation request processing.
 1476  */
 1477 static int
 1478 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
 1479         const struct ieee80211_frame *wh, const uint8_t *wpa,
 1480         const uint8_t *rsn, uint16_t capinfo)
 1481 {
 1482         struct ieee80211vap *vap = ni->ni_vap;
 1483         uint8_t reason;
 1484         int badwparsn;
 1485 
 1486         ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
 1487         if (wpa == NULL && rsn == NULL) {
 1488                 if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
 1489                         /*
 1490                          * W-Fi Protected Setup (WPS) permits
 1491                          * clients to associate and pass EAPOL frames
 1492                          * to establish initial credentials.
 1493                          */
 1494                         ni->ni_flags |= IEEE80211_NODE_WPS;
 1495                         return 1;
 1496                 }
 1497                 if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
 1498                     (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
 1499                         /* 
 1500                          * Transitional Security Network.  Permits clients
 1501                          * to associate and use WEP while WPA is configured.
 1502                          */
 1503                         ni->ni_flags |= IEEE80211_NODE_TSN;
 1504                         return 1;
 1505                 }
 1506                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
 1507                     wh, NULL, "%s", "no WPA/RSN IE in association request");
 1508                 vap->iv_stats.is_rx_assoc_badwpaie++;
 1509                 reason = IEEE80211_REASON_IE_INVALID;
 1510                 goto bad;
 1511         }
 1512         /* assert right association security credentials */
 1513         badwparsn = 0;                  /* NB: to silence compiler */
 1514         switch (vap->iv_flags & IEEE80211_F_WPA) {
 1515         case IEEE80211_F_WPA1:
 1516                 badwparsn = (wpa == NULL);
 1517                 break;
 1518         case IEEE80211_F_WPA2:
 1519                 badwparsn = (rsn == NULL);
 1520                 break;
 1521         case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
 1522                 badwparsn = (wpa == NULL && rsn == NULL);
 1523                 break;
 1524         }
 1525         if (badwparsn) {
 1526                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
 1527                     wh, NULL,
 1528                     "%s", "missing WPA/RSN IE in association request");
 1529                 vap->iv_stats.is_rx_assoc_badwpaie++;
 1530                 reason = IEEE80211_REASON_IE_INVALID;
 1531                 goto bad;
 1532         }
 1533         /*
 1534          * Parse WPA/RSN information element.
 1535          */
 1536         if (wpa != NULL)
 1537                 reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
 1538         else
 1539                 reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
 1540         if (reason != 0) {
 1541                 /* XXX distinguish WPA/RSN? */
 1542                 vap->iv_stats.is_rx_assoc_badwpaie++;
 1543                 goto bad;
 1544         }
 1545         IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
 1546             "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
 1547             wpa != NULL ? "WPA" : "RSN",
 1548             rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
 1549             rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
 1550             rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
 1551 
 1552         return 1;
 1553 bad:
 1554         ieee80211_node_deauth(ni, reason);
 1555         return 0;
 1556 }
 1557 
 1558 /* XXX find a better place for definition */
 1559 struct l2_update_frame {
 1560         struct ether_header eh;
 1561         uint8_t dsap;
 1562         uint8_t ssap;
 1563         uint8_t control;
 1564         uint8_t xid[3];
 1565 }  __packed;
 1566 
 1567 /*
 1568  * Deliver a TGf L2UF frame on behalf of a station.
 1569  * This primes any bridge when the station is roaming
 1570  * between ap's on the same wired network.
 1571  */
 1572 static void
 1573 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
 1574 {
 1575         struct ieee80211vap *vap = ni->ni_vap;
 1576         struct ifnet *ifp = vap->iv_ifp;
 1577         struct mbuf *m;
 1578         struct l2_update_frame *l2uf;
 1579         struct ether_header *eh;
 1580         
 1581         m = m_gethdr(M_NOWAIT, MT_DATA);
 1582         if (m == NULL) {
 1583                 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
 1584                     "%s", "no mbuf for l2uf frame");
 1585                 vap->iv_stats.is_rx_nobuf++;    /* XXX not right */
 1586                 return;
 1587         }
 1588         l2uf = mtod(m, struct l2_update_frame *);
 1589         eh = &l2uf->eh;
 1590         /* dst: Broadcast address */
 1591         IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
 1592         /* src: associated STA */
 1593         IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
 1594         eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
 1595         
 1596         l2uf->dsap = 0;
 1597         l2uf->ssap = 0;
 1598         l2uf->control = 0xf5;
 1599         l2uf->xid[0] = 0x81;
 1600         l2uf->xid[1] = 0x80;
 1601         l2uf->xid[2] = 0x00;
 1602         
 1603         m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
 1604         hostap_deliver_data(vap, ni, m);
 1605 }
 1606 
 1607 static void
 1608 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
 1609         int reassoc, int resp, const char *tag, int rate)
 1610 {
 1611         IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
 1612             "deny %s request, %s rate set mismatch, rate/MCS %d",
 1613             reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
 1614         IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
 1615         ieee80211_node_leave(ni);
 1616 }
 1617 
 1618 static void
 1619 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
 1620         int reassoc, int resp, const char *tag, int capinfo)
 1621 {
 1622         struct ieee80211vap *vap = ni->ni_vap;
 1623 
 1624         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
 1625             "deny %s request, %s mismatch 0x%x",
 1626             reassoc ? "reassoc" : "assoc", tag, capinfo);
 1627         IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
 1628         ieee80211_node_leave(ni);
 1629         vap->iv_stats.is_rx_assoc_capmismatch++;
 1630 }
 1631 
 1632 static void
 1633 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
 1634         int reassoc, int resp)
 1635 {
 1636         IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
 1637             "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
 1638         /* XXX no better code */
 1639         IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
 1640         ieee80211_node_leave(ni);
 1641 }
 1642 
 1643 static void
 1644 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
 1645         int algo, int seq, int status)
 1646 {
 1647         struct ieee80211vap *vap = ni->ni_vap;
 1648 
 1649         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
 1650             wh, NULL, "unsupported alg %d", algo);
 1651         vap->iv_stats.is_rx_auth_unsupported++;
 1652         ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
 1653             seq | (status << 16));
 1654 }
 1655 
 1656 static __inline int
 1657 ishtmixed(const uint8_t *ie)
 1658 {
 1659         const struct ieee80211_ie_htinfo *ht =
 1660             (const struct ieee80211_ie_htinfo *) ie;
 1661         return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
 1662             IEEE80211_HTINFO_OPMODE_MIXED;
 1663 }
 1664 
 1665 static int
 1666 is11bclient(const uint8_t *rates, const uint8_t *xrates)
 1667 {
 1668         static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
 1669         int i;
 1670 
 1671         /* NB: the 11b clients we care about will not have xrates */
 1672         if (xrates != NULL || rates == NULL)
 1673                 return 0;
 1674         for (i = 0; i < rates[1]; i++) {
 1675                 int r = rates[2+i] & IEEE80211_RATE_VAL;
 1676                 if (r > 2*11 || ((1<<r) & brates) == 0)
 1677                         return 0;
 1678         }
 1679         return 1;
 1680 }
 1681 
 1682 static void
 1683 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
 1684         int subtype, int rssi, int nf)
 1685 {
 1686         struct ieee80211vap *vap = ni->ni_vap;
 1687         struct ieee80211com *ic = ni->ni_ic;
 1688         struct ieee80211_frame *wh;
 1689         uint8_t *frm, *efrm, *sfrm;
 1690         uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
 1691         int reassoc, resp;
 1692         uint8_t rate;
 1693 
 1694         wh = mtod(m0, struct ieee80211_frame *);
 1695         frm = (uint8_t *)&wh[1];
 1696         efrm = mtod(m0, uint8_t *) + m0->m_len;
 1697         switch (subtype) {
 1698         case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
 1699         case IEEE80211_FC0_SUBTYPE_BEACON: {
 1700                 struct ieee80211_scanparams scan;
 1701                 /*
 1702                  * We process beacon/probe response frames when scanning;
 1703                  * otherwise we check beacon frames for overlapping non-ERP
 1704                  * BSS in 11g and/or overlapping legacy BSS when in HT.
 1705                  */ 
 1706                 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
 1707                     subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
 1708                         vap->iv_stats.is_rx_mgtdiscard++;
 1709                         return;
 1710                 }
 1711                 /* NB: accept off-channel frames */
 1712                 if (ieee80211_parse_beacon(ni, m0, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
 1713                         return;
 1714                 /*
 1715                  * Count frame now that we know it's to be processed.
 1716                  */
 1717                 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
 1718                         vap->iv_stats.is_rx_beacon++;           /* XXX remove */
 1719                         IEEE80211_NODE_STAT(ni, rx_beacons);
 1720                 } else
 1721                         IEEE80211_NODE_STAT(ni, rx_proberesp);
 1722                 /*
 1723                  * If scanning, just pass information to the scan module.
 1724                  */
 1725                 if (ic->ic_flags & IEEE80211_F_SCAN) {
 1726                         if (scan.status == 0 &&         /* NB: on channel */
 1727                             (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
 1728                                 /*
 1729                                  * Actively scanning a channel marked passive;
 1730                                  * send a probe request now that we know there
 1731                                  * is 802.11 traffic present.
 1732                                  *
 1733                                  * XXX check if the beacon we recv'd gives
 1734                                  * us what we need and suppress the probe req
 1735                                  */
 1736                                 ieee80211_probe_curchan(vap, 1);
 1737                                 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
 1738                         }
 1739                         ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
 1740                         return;
 1741                 }
 1742                 /*
 1743                  * Check beacon for overlapping bss w/ non ERP stations.
 1744                  * If we detect one and protection is configured but not
 1745                  * enabled, enable it and start a timer that'll bring us
 1746                  * out if we stop seeing the bss.
 1747                  */
 1748                 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
 1749                     scan.status == 0 &&                 /* NB: on-channel */
 1750                     ((scan.erp & 0x100) == 0 ||         /* NB: no ERP, 11b sta*/
 1751                      (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
 1752                         ic->ic_lastnonerp = ticks;
 1753                         ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
 1754                         if (ic->ic_protmode != IEEE80211_PROT_NONE &&
 1755                             (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
 1756                                 IEEE80211_NOTE_FRAME(vap,
 1757                                     IEEE80211_MSG_ASSOC, wh,
 1758                                     "non-ERP present on channel %d "
 1759                                     "(saw erp 0x%x from channel %d), "
 1760                                     "enable use of protection",
 1761                                     ic->ic_curchan->ic_ieee,
 1762                                     scan.erp, scan.chan);
 1763                                 ic->ic_flags |= IEEE80211_F_USEPROT;
 1764                                 ieee80211_notify_erp(ic);
 1765                         }
 1766                 }
 1767                 /* 
 1768                  * Check beacon for non-HT station on HT channel
 1769                  * and update HT BSS occupancy as appropriate.
 1770                  */
 1771                 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
 1772                         if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
 1773                                 /*
 1774                                  * Off control channel; only check frames
 1775                                  * that come in the extension channel when
 1776                                  * operating w/ HT40.
 1777                                  */
 1778                                 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
 1779                                         break;
 1780                                 if (scan.chan != ic->ic_curchan->ic_extieee)
 1781                                         break;
 1782                         }
 1783                         if (scan.htinfo == NULL) {
 1784                                 ieee80211_htprot_update(ic,
 1785                                     IEEE80211_HTINFO_OPMODE_PROTOPT |
 1786                                     IEEE80211_HTINFO_NONHT_PRESENT);
 1787                         } else if (ishtmixed(scan.htinfo)) {
 1788                                 /* XXX? take NONHT_PRESENT from beacon? */
 1789                                 ieee80211_htprot_update(ic,
 1790                                     IEEE80211_HTINFO_OPMODE_MIXED |
 1791                                     IEEE80211_HTINFO_NONHT_PRESENT);
 1792                         }
 1793                 }
 1794                 break;
 1795         }
 1796 
 1797         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
 1798                 if (vap->iv_state != IEEE80211_S_RUN) {
 1799                         vap->iv_stats.is_rx_mgtdiscard++;
 1800                         return;
 1801                 }
 1802                 /*
 1803                  * Consult the ACL policy module if setup.
 1804                  */
 1805                 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
 1806                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
 1807                             wh, NULL, "%s", "disallowed by ACL");
 1808                         vap->iv_stats.is_rx_acl++;
 1809                         return;
 1810                 }
 1811                 /*
 1812                  * prreq frame format
 1813                  *      [tlv] ssid
 1814                  *      [tlv] supported rates
 1815                  *      [tlv] extended supported rates
 1816                  */
 1817                 ssid = rates = xrates = NULL;
 1818                 sfrm = frm;
 1819                 while (efrm - frm > 1) {
 1820                         IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
 1821                         switch (*frm) {
 1822                         case IEEE80211_ELEMID_SSID:
 1823                                 ssid = frm;
 1824                                 break;
 1825                         case IEEE80211_ELEMID_RATES:
 1826                                 rates = frm;
 1827                                 break;
 1828                         case IEEE80211_ELEMID_XRATES:
 1829                                 xrates = frm;
 1830                                 break;
 1831                         }
 1832                         frm += frm[1] + 2;
 1833                 }
 1834                 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
 1835                 if (xrates != NULL)
 1836                         IEEE80211_VERIFY_ELEMENT(xrates,
 1837                                 IEEE80211_RATE_MAXSIZE - rates[1], return);
 1838                 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
 1839                 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
 1840                 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
 1841                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
 1842                             wh, NULL,
 1843                             "%s", "no ssid with ssid suppression enabled");
 1844                         vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
 1845                         return;
 1846                 }
 1847 
 1848                 /* XXX find a better class or define it's own */
 1849                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
 1850                     "%s", "recv probe req");
 1851                 /*
 1852                  * Some legacy 11b clients cannot hack a complete
 1853                  * probe response frame.  When the request includes
 1854                  * only a bare-bones rate set, communicate this to
 1855                  * the transmit side.
 1856                  */
 1857                 ieee80211_send_proberesp(vap, wh->i_addr2,
 1858                     is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
 1859                 break;
 1860 
 1861         case IEEE80211_FC0_SUBTYPE_AUTH: {
 1862                 uint16_t algo, seq, status;
 1863 
 1864                 if (vap->iv_state != IEEE80211_S_RUN) {
 1865                         vap->iv_stats.is_rx_mgtdiscard++;
 1866                         return;
 1867                 }
 1868                 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
 1869                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
 1870                             wh, NULL, "%s", "wrong bssid");
 1871                         vap->iv_stats.is_rx_wrongbss++; /*XXX unique stat?*/
 1872                         return;
 1873                 }
 1874                 /*
 1875                  * auth frame format
 1876                  *      [2] algorithm
 1877                  *      [2] sequence
 1878                  *      [2] status
 1879                  *      [tlv*] challenge
 1880                  */
 1881                 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
 1882                 algo   = le16toh(*(uint16_t *)frm);
 1883                 seq    = le16toh(*(uint16_t *)(frm + 2));
 1884                 status = le16toh(*(uint16_t *)(frm + 4));
 1885                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
 1886                     "recv auth frame with algorithm %d seq %d", algo, seq);
 1887                 /*
 1888                  * Consult the ACL policy module if setup.
 1889                  */
 1890                 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
 1891                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
 1892                             wh, NULL, "%s", "disallowed by ACL");
 1893                         vap->iv_stats.is_rx_acl++;
 1894                         ieee80211_send_error(ni, wh->i_addr2,
 1895                             IEEE80211_FC0_SUBTYPE_AUTH,
 1896                             (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
 1897                         return;
 1898                 }
 1899                 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
 1900                         IEEE80211_DISCARD(vap,
 1901                             IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
 1902                             wh, NULL, "%s", "TKIP countermeasures enabled");
 1903                         vap->iv_stats.is_rx_auth_countermeasures++;
 1904                         ieee80211_send_error(ni, wh->i_addr2,
 1905                                 IEEE80211_FC0_SUBTYPE_AUTH,
 1906                                 IEEE80211_REASON_MIC_FAILURE);
 1907                         return;
 1908                 }
 1909                 if (algo == IEEE80211_AUTH_ALG_SHARED)
 1910                         hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
 1911                             seq, status);
 1912                 else if (algo == IEEE80211_AUTH_ALG_OPEN)
 1913                         hostap_auth_open(ni, wh, rssi, nf, seq, status);
 1914                 else if (algo == IEEE80211_AUTH_ALG_LEAP) {
 1915                         authalgreject(ni, wh, algo,
 1916                             seq+1, IEEE80211_STATUS_ALG);
 1917                         return;
 1918                 } else {
 1919                         /*
 1920                          * We assume that an unknown algorithm is the result
 1921                          * of a decryption failure on a shared key auth frame;
 1922                          * return a status code appropriate for that instead
 1923                          * of IEEE80211_STATUS_ALG.
 1924                          *
 1925                          * NB: a seq# of 4 is intentional; the decrypted
 1926                          *     frame likely has a bogus seq value.
 1927                          */
 1928                         authalgreject(ni, wh, algo,
 1929                             4, IEEE80211_STATUS_CHALLENGE);
 1930                         return;
 1931                 } 
 1932                 break;
 1933         }
 1934 
 1935         case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
 1936         case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
 1937                 uint16_t capinfo, lintval;
 1938                 struct ieee80211_rsnparms rsnparms;
 1939 
 1940                 if (vap->iv_state != IEEE80211_S_RUN) {
 1941                         vap->iv_stats.is_rx_mgtdiscard++;
 1942                         return;
 1943                 }
 1944                 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
 1945                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
 1946                             wh, NULL, "%s", "wrong bssid");
 1947                         vap->iv_stats.is_rx_assoc_bss++;
 1948                         return;
 1949                 }
 1950                 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
 1951                         reassoc = 1;
 1952                         resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
 1953                 } else {
 1954                         reassoc = 0;
 1955                         resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
 1956                 }
 1957                 if (ni == vap->iv_bss) {
 1958                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
 1959                             "deny %s request, sta not authenticated",
 1960                             reassoc ? "reassoc" : "assoc");
 1961                         ieee80211_send_error(ni, wh->i_addr2,
 1962                             IEEE80211_FC0_SUBTYPE_DEAUTH,
 1963                             IEEE80211_REASON_ASSOC_NOT_AUTHED);
 1964                         vap->iv_stats.is_rx_assoc_notauth++;
 1965                         return;
 1966                 }
 1967 
 1968                 /*
 1969                  * asreq frame format
 1970                  *      [2] capability information
 1971                  *      [2] listen interval
 1972                  *      [6*] current AP address (reassoc only)
 1973                  *      [tlv] ssid
 1974                  *      [tlv] supported rates
 1975                  *      [tlv] extended supported rates
 1976                  *      [tlv] WPA or RSN
 1977                  *      [tlv] HT capabilities
 1978                  *      [tlv] Atheros capabilities
 1979                  */
 1980                 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
 1981                 capinfo = le16toh(*(uint16_t *)frm);    frm += 2;
 1982                 lintval = le16toh(*(uint16_t *)frm);    frm += 2;
 1983                 if (reassoc)
 1984                         frm += 6;       /* ignore current AP info */
 1985                 ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
 1986                 sfrm = frm;
 1987                 while (efrm - frm > 1) {
 1988                         IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
 1989                         switch (*frm) {
 1990                         case IEEE80211_ELEMID_SSID:
 1991                                 ssid = frm;
 1992                                 break;
 1993                         case IEEE80211_ELEMID_RATES:
 1994                                 rates = frm;
 1995                                 break;
 1996                         case IEEE80211_ELEMID_XRATES:
 1997                                 xrates = frm;
 1998                                 break;
 1999                         case IEEE80211_ELEMID_RSN:
 2000                                 rsn = frm;
 2001                                 break;
 2002                         case IEEE80211_ELEMID_HTCAP:
 2003                                 htcap = frm;
 2004                                 break;
 2005                         case IEEE80211_ELEMID_VENDOR:
 2006                                 if (iswpaoui(frm))
 2007                                         wpa = frm;
 2008                                 else if (iswmeinfo(frm))
 2009                                         wme = frm;
 2010 #ifdef IEEE80211_SUPPORT_SUPERG
 2011                                 else if (isatherosoui(frm))
 2012                                         ath = frm;
 2013 #endif
 2014                                 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
 2015                                         if (ishtcapoui(frm) && htcap == NULL)
 2016                                                 htcap = frm;
 2017                                 }
 2018                                 break;
 2019                         }
 2020                         frm += frm[1] + 2;
 2021                 }
 2022                 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
 2023                 if (xrates != NULL)
 2024                         IEEE80211_VERIFY_ELEMENT(xrates,
 2025                                 IEEE80211_RATE_MAXSIZE - rates[1], return);
 2026                 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
 2027                 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
 2028                 if (htcap != NULL) {
 2029                         IEEE80211_VERIFY_LENGTH(htcap[1],
 2030                              htcap[0] == IEEE80211_ELEMID_VENDOR ?
 2031                                  4 + sizeof(struct ieee80211_ie_htcap)-2 :
 2032                                  sizeof(struct ieee80211_ie_htcap)-2,
 2033                              return);           /* XXX just NULL out? */
 2034                 }
 2035 
 2036                 if ((vap->iv_flags & IEEE80211_F_WPA) &&
 2037                     !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
 2038                         return;
 2039                 /* discard challenge after association */
 2040                 if (ni->ni_challenge != NULL) {
 2041                         free(ni->ni_challenge, M_80211_NODE);
 2042                         ni->ni_challenge = NULL;
 2043                 }
 2044                 /* NB: 802.11 spec says to ignore station's privacy bit */
 2045                 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
 2046                         capinfomismatch(ni, wh, reassoc, resp,
 2047                             "capability", capinfo);
 2048                         return;
 2049                 }
 2050                 /*
 2051                  * Disallow re-associate w/ invalid slot time setting.
 2052                  */
 2053                 if (ni->ni_associd != 0 &&
 2054                     IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
 2055                     ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
 2056                         capinfomismatch(ni, wh, reassoc, resp,
 2057                             "slot time", capinfo);
 2058                         return;
 2059                 }
 2060                 rate = ieee80211_setup_rates(ni, rates, xrates,
 2061                                 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
 2062                                 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
 2063                 if (rate & IEEE80211_RATE_BASIC) {
 2064                         ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
 2065                         vap->iv_stats.is_rx_assoc_norate++;
 2066                         return;
 2067                 }
 2068                 /*
 2069                  * If constrained to 11g-only stations reject an
 2070                  * 11b-only station.  We cheat a bit here by looking
 2071                  * at the max negotiated xmit rate and assuming anyone
 2072                  * with a best rate <24Mb/s is an 11b station.
 2073                  */
 2074                 if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
 2075                         ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
 2076                         vap->iv_stats.is_rx_assoc_norate++;
 2077                         return;
 2078                 }
 2079                 /*
 2080                  * Do HT rate set handling and setup HT node state.
 2081                  */
 2082                 ni->ni_chan = vap->iv_bss->ni_chan;
 2083                 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
 2084                         rate = ieee80211_setup_htrates(ni, htcap,
 2085                                 IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
 2086                                 IEEE80211_F_DOBRS);
 2087                         if (rate & IEEE80211_RATE_BASIC) {
 2088                                 ratesetmismatch(ni, wh, reassoc, resp,
 2089                                     "HT", rate);
 2090                                 vap->iv_stats.is_ht_assoc_norate++;
 2091                                 return;
 2092                         }
 2093                         ieee80211_ht_node_init(ni);
 2094                         ieee80211_ht_updatehtcap(ni, htcap);
 2095                 } else if (ni->ni_flags & IEEE80211_NODE_HT)
 2096                         ieee80211_ht_node_cleanup(ni);
 2097 #ifdef IEEE80211_SUPPORT_SUPERG
 2098                 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
 2099                         ieee80211_ff_node_cleanup(ni);
 2100 #endif
 2101                 /*
 2102                  * Allow AMPDU operation only with unencrypted traffic
 2103                  * or AES-CCM; the 11n spec only specifies these ciphers
 2104                  * so permitting any others is undefined and can lead
 2105                  * to interoperability problems.
 2106                  */
 2107                 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
 2108                     (((vap->iv_flags & IEEE80211_F_WPA) &&
 2109                       rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
 2110                      (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
 2111                         IEEE80211_NOTE(vap,
 2112                             IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
 2113                             "disallow HT use because WEP or TKIP requested, "
 2114                             "capinfo 0x%x ucastcipher %d", capinfo,
 2115                             rsnparms.rsn_ucastcipher);
 2116                         ieee80211_ht_node_cleanup(ni);
 2117                         vap->iv_stats.is_ht_assoc_downgrade++;
 2118                 }
 2119                 /*
 2120                  * If constrained to 11n-only stations reject legacy stations.
 2121                  */
 2122                 if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
 2123                     (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
 2124                         htcapmismatch(ni, wh, reassoc, resp);
 2125                         vap->iv_stats.is_ht_assoc_nohtcap++;
 2126                         return;
 2127                 }
 2128                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
 2129                 ni->ni_noise = nf;
 2130                 ni->ni_intval = lintval;
 2131                 ni->ni_capinfo = capinfo;
 2132                 ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
 2133                 ni->ni_fhindex = vap->iv_bss->ni_fhindex;
 2134                 /*
 2135                  * Store the IEs.
 2136                  * XXX maybe better to just expand
 2137                  */
 2138                 if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
 2139 #define setie(_ie, _off)        ieee80211_ies_setie(ni->ni_ies, _ie, _off)
 2140                         if (wpa != NULL)
 2141                                 setie(wpa_ie, wpa - sfrm);
 2142                         if (rsn != NULL)
 2143                                 setie(rsn_ie, rsn - sfrm);
 2144                         if (htcap != NULL)
 2145                                 setie(htcap_ie, htcap - sfrm);
 2146                         if (wme != NULL) {
 2147                                 setie(wme_ie, wme - sfrm);
 2148                                 /*
 2149                                  * Mark node as capable of QoS.
 2150                                  */
 2151                                 ni->ni_flags |= IEEE80211_NODE_QOS;
 2152                         } else
 2153                                 ni->ni_flags &= ~IEEE80211_NODE_QOS;
 2154 #ifdef IEEE80211_SUPPORT_SUPERG
 2155                         if (ath != NULL) {
 2156                                 setie(ath_ie, ath - sfrm);
 2157                                 /* 
 2158                                  * Parse ATH station parameters.
 2159                                  */
 2160                                 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
 2161                         } else
 2162 #endif
 2163                                 ni->ni_ath_flags = 0;
 2164 #undef setie
 2165                 } else {
 2166                         ni->ni_flags &= ~IEEE80211_NODE_QOS;
 2167                         ni->ni_ath_flags = 0;
 2168                 }
 2169                 ieee80211_node_join(ni, resp);
 2170                 ieee80211_deliver_l2uf(ni);
 2171                 break;
 2172         }
 2173 
 2174         case IEEE80211_FC0_SUBTYPE_DEAUTH:
 2175         case IEEE80211_FC0_SUBTYPE_DISASSOC: {
 2176                 uint16_t reason;
 2177 
 2178                 if (vap->iv_state != IEEE80211_S_RUN ||
 2179                     /* NB: can happen when in promiscuous mode */
 2180                     !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
 2181                         vap->iv_stats.is_rx_mgtdiscard++;
 2182                         break;
 2183                 }
 2184                 /*
 2185                  * deauth/disassoc frame format
 2186                  *      [2] reason
 2187                  */
 2188                 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
 2189                 reason = le16toh(*(uint16_t *)frm);
 2190                 if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
 2191                         vap->iv_stats.is_rx_deauth++;
 2192                         IEEE80211_NODE_STAT(ni, rx_deauth);
 2193                 } else {
 2194                         vap->iv_stats.is_rx_disassoc++;
 2195                         IEEE80211_NODE_STAT(ni, rx_disassoc);
 2196                 }
 2197                 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
 2198                     "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
 2199                         IEEE80211_FC0_SUBTYPE_SHIFT], reason);
 2200                 if (ni != vap->iv_bss)
 2201                         ieee80211_node_leave(ni);
 2202                 break;
 2203         }
 2204 
 2205         case IEEE80211_FC0_SUBTYPE_ACTION:
 2206         case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
 2207                 if (ni == vap->iv_bss) {
 2208                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
 2209                             wh, NULL, "%s", "unknown node");
 2210                         vap->iv_stats.is_rx_mgtdiscard++;
 2211                 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
 2212                     !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
 2213                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
 2214                             wh, NULL, "%s", "not for us");
 2215                         vap->iv_stats.is_rx_mgtdiscard++;
 2216                 } else if (vap->iv_state != IEEE80211_S_RUN) {
 2217                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
 2218                             wh, NULL, "wrong state %s",
 2219                             ieee80211_state_name[vap->iv_state]);
 2220                         vap->iv_stats.is_rx_mgtdiscard++;
 2221                 } else {
 2222                         if (ieee80211_parse_action(ni, m0) == 0)
 2223                                 (void)ic->ic_recv_action(ni, wh, frm, efrm);
 2224                 }
 2225                 break;
 2226 
 2227         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
 2228         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
 2229         case IEEE80211_FC0_SUBTYPE_ATIM:
 2230                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
 2231                     wh, NULL, "%s", "not handled");
 2232                 vap->iv_stats.is_rx_mgtdiscard++;
 2233                 break;
 2234 
 2235         default:
 2236                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
 2237                     wh, "mgt", "subtype 0x%x not handled", subtype);
 2238                 vap->iv_stats.is_rx_badsubtype++;
 2239                 break;
 2240         }
 2241 }
 2242 
 2243 static void
 2244 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
 2245 {
 2246         switch (subtype) {
 2247         case IEEE80211_FC0_SUBTYPE_PS_POLL:
 2248                 ni->ni_vap->iv_recv_pspoll(ni, m);
 2249                 break;
 2250         case IEEE80211_FC0_SUBTYPE_BAR:
 2251                 ieee80211_recv_bar(ni, m);
 2252                 break;
 2253         }
 2254 }
 2255 
 2256 /*
 2257  * Process a received ps-poll frame.
 2258  */
 2259 void
 2260 ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
 2261 {
 2262         struct ieee80211vap *vap = ni->ni_vap;
 2263         struct ieee80211com *ic = vap->iv_ic;
 2264         struct ieee80211_frame_min *wh;
 2265         struct mbuf *m;
 2266         uint16_t aid;
 2267         int qlen;
 2268 
 2269         wh = mtod(m0, struct ieee80211_frame_min *);
 2270         if (ni->ni_associd == 0) {
 2271                 IEEE80211_DISCARD(vap,
 2272                     IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
 2273                     (struct ieee80211_frame *) wh, NULL,
 2274                     "%s", "unassociated station");
 2275                 vap->iv_stats.is_ps_unassoc++;
 2276                 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
 2277                         IEEE80211_REASON_NOT_ASSOCED);
 2278                 return;
 2279         }
 2280 
 2281         aid = le16toh(*(uint16_t *)wh->i_dur);
 2282         if (aid != ni->ni_associd) {
 2283                 IEEE80211_DISCARD(vap,
 2284                     IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
 2285                     (struct ieee80211_frame *) wh, NULL,
 2286                     "aid mismatch: sta aid 0x%x poll aid 0x%x",
 2287                     ni->ni_associd, aid);
 2288                 vap->iv_stats.is_ps_badaid++;
 2289                 /*
 2290                  * NB: We used to deauth the station but it turns out
 2291                  * the Blackberry Curve 8230 (and perhaps other devices) 
 2292                  * sometimes send the wrong AID when WME is negotiated.
 2293                  * Being more lenient here seems ok as we already check
 2294                  * the station is associated and we only return frames
 2295                  * queued for the station (i.e. we don't use the AID).
 2296                  */
 2297                 return;
 2298         }
 2299 
 2300         /* Okay, take the first queued packet and put it out... */
 2301         m = ieee80211_node_psq_dequeue(ni, &qlen);
 2302         if (m == NULL) {
 2303                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
 2304                     "%s", "recv ps-poll, but queue empty");
 2305                 ieee80211_send_nulldata(ieee80211_ref_node(ni));
 2306                 vap->iv_stats.is_ps_qempty++;   /* XXX node stat */
 2307                 if (vap->iv_set_tim != NULL)
 2308                         vap->iv_set_tim(ni, 0); /* just in case */
 2309                 return;
 2310         }
 2311         /* 
 2312          * If there are more packets, set the more packets bit
 2313          * in the packet dispatched to the station; otherwise
 2314          * turn off the TIM bit.
 2315          */
 2316         if (qlen != 0) {
 2317                 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
 2318                     "recv ps-poll, send packet, %u still queued", qlen);
 2319                 m->m_flags |= M_MORE_DATA;
 2320         } else {
 2321                 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
 2322                     "%s", "recv ps-poll, send packet, queue empty");
 2323                 if (vap->iv_set_tim != NULL)
 2324                         vap->iv_set_tim(ni, 0);
 2325         }
 2326         m->m_flags |= M_PWR_SAV;                /* bypass PS handling */
 2327 
 2328         /*
 2329          * Do the right thing; if it's an encap'ed frame then
 2330          * call ieee80211_parent_xmitpkt() (and free the ref) else
 2331          * call ieee80211_vap_xmitpkt().
 2332          */
 2333         if (m->m_flags & M_ENCAP) {
 2334                 if (ieee80211_parent_xmitpkt(ic, m) != 0)
 2335                         ieee80211_free_node(ni);
 2336         } else {
 2337                 (void) ieee80211_vap_xmitpkt(vap, m);
 2338         }
 2339 }

Cache object: 62c7e5e785df93e46db9cbb950d4ebed


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.