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-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
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/8.1/sys/net80211/ieee80211_hostap.c 206271 2010-04-06 14:07:48Z rpaulo $");
   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 static void hostap_recv_pspoll(struct ieee80211_node *, struct mbuf *);
   77 
   78 void
   79 ieee80211_hostap_attach(struct ieee80211com *ic)
   80 {
   81         ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
   82 }
   83 
   84 void
   85 ieee80211_hostap_detach(struct ieee80211com *ic)
   86 {
   87 }
   88 
   89 static void
   90 hostap_vdetach(struct ieee80211vap *vap)
   91 {
   92 }
   93 
   94 static void
   95 hostap_vattach(struct ieee80211vap *vap)
   96 {
   97         vap->iv_newstate = hostap_newstate;
   98         vap->iv_input = hostap_input;
   99         vap->iv_recv_mgmt = hostap_recv_mgmt;
  100         vap->iv_recv_ctl = hostap_recv_ctl;
  101         vap->iv_opdetach = hostap_vdetach;
  102         vap->iv_deliver_data = hostap_deliver_data;
  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         m->m_flags &= ~(M_80211_RX | M_MCAST | M_BCAST);
  359 
  360         KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
  361             ("gack, opmode %d", vap->iv_opmode));
  362         /*
  363          * Do accounting.
  364          */
  365         ifp->if_ipackets++;
  366         IEEE80211_NODE_STAT(ni, rx_data);
  367         IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
  368         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
  369                 m->m_flags |= M_MCAST;          /* XXX M_BCAST? */
  370                 IEEE80211_NODE_STAT(ni, rx_mcast);
  371         } else
  372                 IEEE80211_NODE_STAT(ni, rx_ucast);
  373 
  374         /* perform as a bridge within the AP */
  375         if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
  376                 struct mbuf *mcopy = NULL;
  377 
  378                 if (m->m_flags & M_MCAST) {
  379                         mcopy = m_dup(m, M_DONTWAIT);
  380                         if (mcopy == NULL)
  381                                 ifp->if_oerrors++;
  382                         else
  383                                 mcopy->m_flags |= M_MCAST;
  384                 } else {
  385                         /*
  386                          * Check if the destination is associated with the
  387                          * same vap and authorized to receive traffic.
  388                          * Beware of traffic destined for the vap itself;
  389                          * sending it will not work; just let it be delivered
  390                          * normally.
  391                          */
  392                         struct ieee80211_node *sta = ieee80211_find_vap_node(
  393                              &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
  394                         if (sta != NULL) {
  395                                 if (ieee80211_node_is_authorized(sta)) {
  396                                         /*
  397                                          * Beware of sending to ourself; this
  398                                          * needs to happen via the normal
  399                                          * input path.
  400                                          */
  401                                         if (sta != vap->iv_bss) {
  402                                                 mcopy = m;
  403                                                 m = NULL;
  404                                         }
  405                                 } else {
  406                                         vap->iv_stats.is_rx_unauth++;
  407                                         IEEE80211_NODE_STAT(sta, rx_unauth);
  408                                 }
  409                                 ieee80211_free_node(sta);
  410                         }
  411                 }
  412                 if (mcopy != NULL) {
  413                         int len, err;
  414                         len = mcopy->m_pkthdr.len;
  415                         err = ifp->if_transmit(ifp, mcopy);
  416                         if (err) {
  417                                 /* NB: IFQ_HANDOFF reclaims mcopy */
  418                         } else {
  419                                 ifp->if_opackets++;
  420                         }
  421                 }
  422         }
  423         if (m != NULL) {
  424                 /*
  425                  * Mark frame as coming from vap's interface.
  426                  */
  427                 m->m_pkthdr.rcvif = ifp;
  428                 if (m->m_flags & M_MCAST) {
  429                         /*
  430                          * Spam DWDS vap's w/ multicast traffic.
  431                          */
  432                         /* XXX only if dwds in use? */
  433                         ieee80211_dwds_mcast(vap, m);
  434                 }
  435                 if (ni->ni_vlan != 0) {
  436                         /* attach vlan tag */
  437                         m->m_pkthdr.ether_vtag = ni->ni_vlan;
  438                         m->m_flags |= M_VLANTAG;
  439                 }
  440                 ifp->if_input(ifp, m);
  441         }
  442 }
  443 
  444 /*
  445  * Decide if a received management frame should be
  446  * printed when debugging is enabled.  This filters some
  447  * of the less interesting frames that come frequently
  448  * (e.g. beacons).
  449  */
  450 static __inline int
  451 doprint(struct ieee80211vap *vap, int subtype)
  452 {
  453         switch (subtype) {
  454         case IEEE80211_FC0_SUBTYPE_BEACON:
  455                 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
  456         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
  457                 return 0;
  458         }
  459         return 1;
  460 }
  461 
  462 /*
  463  * Process a received frame.  The node associated with the sender
  464  * should be supplied.  If nothing was found in the node table then
  465  * the caller is assumed to supply a reference to iv_bss instead.
  466  * The RSSI and a timestamp are also supplied.  The RSSI data is used
  467  * during AP scanning to select a AP to associate with; it can have
  468  * any units so long as values have consistent units and higher values
  469  * mean ``better signal''.  The receive timestamp is currently not used
  470  * by the 802.11 layer.
  471  */
  472 static int
  473 hostap_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
  474 {
  475 #define SEQ_LEQ(a,b)    ((int)((a)-(b)) <= 0)
  476 #define HAS_SEQ(type)   ((type & 0x4) == 0)
  477         struct ieee80211vap *vap = ni->ni_vap;
  478         struct ieee80211com *ic = ni->ni_ic;
  479         struct ifnet *ifp = vap->iv_ifp;
  480         struct ieee80211_frame *wh;
  481         struct ieee80211_key *key;
  482         struct ether_header *eh;
  483         int hdrspace, need_tap;
  484         uint8_t dir, type, subtype, qos;
  485         uint8_t *bssid;
  486         uint16_t rxseq;
  487 
  488         if (m->m_flags & M_AMPDU_MPDU) {
  489                 /*
  490                  * Fastpath for A-MPDU reorder q resubmission.  Frames
  491                  * w/ M_AMPDU_MPDU marked have already passed through
  492                  * here but were received out of order and been held on
  493                  * the reorder queue.  When resubmitted they are marked
  494                  * with the M_AMPDU_MPDU flag and we can bypass most of
  495                  * the normal processing.
  496                  */
  497                 wh = mtod(m, struct ieee80211_frame *);
  498                 type = IEEE80211_FC0_TYPE_DATA;
  499                 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
  500                 subtype = IEEE80211_FC0_SUBTYPE_QOS;
  501                 hdrspace = ieee80211_hdrspace(ic, wh);  /* XXX optimize? */
  502                 goto resubmit_ampdu;
  503         }
  504 
  505         KASSERT(ni != NULL, ("null node"));
  506         ni->ni_inact = ni->ni_inact_reload;
  507 
  508         need_tap = 1;                   /* mbuf need to be tapped. */
  509         type = -1;                      /* undefined */
  510 
  511         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
  512                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
  513                     ni->ni_macaddr, NULL,
  514                     "too short (1): len %u", m->m_pkthdr.len);
  515                 vap->iv_stats.is_rx_tooshort++;
  516                 goto out;
  517         }
  518         /*
  519          * Bit of a cheat here, we use a pointer for a 3-address
  520          * frame format but don't reference fields past outside
  521          * ieee80211_frame_min w/o first validating the data is
  522          * present.
  523          */
  524         wh = mtod(m, struct ieee80211_frame *);
  525 
  526         if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
  527             IEEE80211_FC0_VERSION_0) {
  528                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
  529                     ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
  530                     wh->i_fc[0], wh->i_fc[1]);
  531                 vap->iv_stats.is_rx_badversion++;
  532                 goto err;
  533         }
  534 
  535         dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
  536         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
  537         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
  538         if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
  539                 if (dir != IEEE80211_FC1_DIR_NODS)
  540                         bssid = wh->i_addr1;
  541                 else if (type == IEEE80211_FC0_TYPE_CTL)
  542                         bssid = wh->i_addr1;
  543                 else {
  544                         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
  545                                 IEEE80211_DISCARD_MAC(vap,
  546                                     IEEE80211_MSG_ANY, ni->ni_macaddr,
  547                                     NULL, "too short (2): len %u",
  548                                     m->m_pkthdr.len);
  549                                 vap->iv_stats.is_rx_tooshort++;
  550                                 goto out;
  551                         }
  552                         bssid = wh->i_addr3;
  553                 }
  554                 /*
  555                  * Validate the bssid.
  556                  */
  557                 if (!(type == IEEE80211_FC0_TYPE_MGT &&
  558                       subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
  559                     !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
  560                     !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
  561                         /* not interested in */
  562                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
  563                             bssid, NULL, "%s", "not to bss");
  564                         vap->iv_stats.is_rx_wrongbss++;
  565                         goto out;
  566                 }
  567 
  568                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
  569                 ni->ni_noise = nf;
  570                 if (HAS_SEQ(type)) {
  571                         uint8_t tid = ieee80211_gettid(wh);
  572                         if (IEEE80211_QOS_HAS_SEQ(wh) &&
  573                             TID_TO_WME_AC(tid) >= WME_AC_VI)
  574                                 ic->ic_wme.wme_hipri_traffic++;
  575                         rxseq = le16toh(*(uint16_t *)wh->i_seq);
  576                         if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
  577                             (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
  578                             SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
  579                                 /* duplicate, discard */
  580                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
  581                                     bssid, "duplicate",
  582                                     "seqno <%u,%u> fragno <%u,%u> tid %u",
  583                                     rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
  584                                     ni->ni_rxseqs[tid] >>
  585                                         IEEE80211_SEQ_SEQ_SHIFT,
  586                                     rxseq & IEEE80211_SEQ_FRAG_MASK,
  587                                     ni->ni_rxseqs[tid] &
  588                                         IEEE80211_SEQ_FRAG_MASK,
  589                                     tid);
  590                                 vap->iv_stats.is_rx_dup++;
  591                                 IEEE80211_NODE_STAT(ni, rx_dup);
  592                                 goto out;
  593                         }
  594                         ni->ni_rxseqs[tid] = rxseq;
  595                 }
  596         }
  597 
  598         switch (type) {
  599         case IEEE80211_FC0_TYPE_DATA:
  600                 hdrspace = ieee80211_hdrspace(ic, wh);
  601                 if (m->m_len < hdrspace &&
  602                     (m = m_pullup(m, hdrspace)) == NULL) {
  603                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
  604                             ni->ni_macaddr, NULL,
  605                             "data too short: expecting %u", hdrspace);
  606                         vap->iv_stats.is_rx_tooshort++;
  607                         goto out;               /* XXX */
  608                 }
  609                 if (!(dir == IEEE80211_FC1_DIR_TODS ||
  610                      (dir == IEEE80211_FC1_DIR_DSTODS &&
  611                       (vap->iv_flags & IEEE80211_F_DWDS)))) {
  612                         if (dir != IEEE80211_FC1_DIR_DSTODS) {
  613                                 IEEE80211_DISCARD(vap,
  614                                     IEEE80211_MSG_INPUT, wh, "data",
  615                                     "incorrect dir 0x%x", dir);
  616                         } else {
  617                                 IEEE80211_DISCARD(vap,
  618                                     IEEE80211_MSG_INPUT |
  619                                     IEEE80211_MSG_WDS, wh,
  620                                     "4-address data",
  621                                     "%s", "DWDS not enabled");
  622                         }
  623                         vap->iv_stats.is_rx_wrongdir++;
  624                         goto out;
  625                 }
  626                 /* check if source STA is associated */
  627                 if (ni == vap->iv_bss) {
  628                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  629                             wh, "data", "%s", "unknown src");
  630                         ieee80211_send_error(ni, wh->i_addr2,
  631                             IEEE80211_FC0_SUBTYPE_DEAUTH,
  632                             IEEE80211_REASON_NOT_AUTHED);
  633                         vap->iv_stats.is_rx_notassoc++;
  634                         goto err;
  635                 }
  636                 if (ni->ni_associd == 0) {
  637                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  638                             wh, "data", "%s", "unassoc src");
  639                         IEEE80211_SEND_MGMT(ni,
  640                             IEEE80211_FC0_SUBTYPE_DISASSOC,
  641                             IEEE80211_REASON_NOT_ASSOCED);
  642                         vap->iv_stats.is_rx_notassoc++;
  643                         goto err;
  644                 }
  645 
  646                 /*
  647                  * Check for power save state change.
  648                  * XXX out-of-order A-MPDU frames?
  649                  */
  650                 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
  651                     (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
  652                         ieee80211_node_pwrsave(ni,
  653                                 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
  654                 /*
  655                  * For 4-address packets handle WDS discovery
  656                  * notifications.  Once a WDS link is setup frames
  657                  * are just delivered to the WDS vap (see below).
  658                  */
  659                 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
  660                         if (!ieee80211_node_is_authorized(ni)) {
  661                                 IEEE80211_DISCARD(vap,
  662                                     IEEE80211_MSG_INPUT |
  663                                     IEEE80211_MSG_WDS, wh,
  664                                     "4-address data",
  665                                     "%s", "unauthorized port");
  666                                 vap->iv_stats.is_rx_unauth++;
  667                                 IEEE80211_NODE_STAT(ni, rx_unauth);
  668                                 goto err;
  669                         }
  670                         ieee80211_dwds_discover(ni, m);
  671                         return type;
  672                 }
  673 
  674                 /*
  675                  * Handle A-MPDU re-ordering.  If the frame is to be
  676                  * processed directly then ieee80211_ampdu_reorder
  677                  * will return 0; otherwise it has consumed the mbuf
  678                  * and we should do nothing more with it.
  679                  */
  680                 if ((m->m_flags & M_AMPDU) &&
  681                     ieee80211_ampdu_reorder(ni, m) != 0) {
  682                         m = NULL;
  683                         goto out;
  684                 }
  685         resubmit_ampdu:
  686 
  687                 /*
  688                  * Handle privacy requirements.  Note that we
  689                  * must not be preempted from here until after
  690                  * we (potentially) call ieee80211_crypto_demic;
  691                  * otherwise we may violate assumptions in the
  692                  * crypto cipher modules used to do delayed update
  693                  * of replay sequence numbers.
  694                  */
  695                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
  696                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
  697                                 /*
  698                                  * Discard encrypted frames when privacy is off.
  699                                  */
  700                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  701                                     wh, "WEP", "%s", "PRIVACY off");
  702                                 vap->iv_stats.is_rx_noprivacy++;
  703                                 IEEE80211_NODE_STAT(ni, rx_noprivacy);
  704                                 goto out;
  705                         }
  706                         key = ieee80211_crypto_decap(ni, m, hdrspace);
  707                         if (key == NULL) {
  708                                 /* NB: stats+msgs handled in crypto_decap */
  709                                 IEEE80211_NODE_STAT(ni, rx_wepfail);
  710                                 goto out;
  711                         }
  712                         wh = mtod(m, struct ieee80211_frame *);
  713                         wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
  714                 } else {
  715                         /* XXX M_WEP and IEEE80211_F_PRIVACY */
  716                         key = NULL;
  717                 }
  718 
  719                 /*
  720                  * Save QoS bits for use below--before we strip the header.
  721                  */
  722                 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
  723                         qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
  724                             ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
  725                             ((struct ieee80211_qosframe *)wh)->i_qos[0];
  726                 } else
  727                         qos = 0;
  728 
  729                 /*
  730                  * Next up, any fragmentation.
  731                  */
  732                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
  733                         m = ieee80211_defrag(ni, m, hdrspace);
  734                         if (m == NULL) {
  735                                 /* Fragment dropped or frame not complete yet */
  736                                 goto out;
  737                         }
  738                 }
  739                 wh = NULL;              /* no longer valid, catch any uses */
  740 
  741                 /*
  742                  * Next strip any MSDU crypto bits.
  743                  */
  744                 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
  745                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
  746                             ni->ni_macaddr, "data", "%s", "demic error");
  747                         vap->iv_stats.is_rx_demicfail++;
  748                         IEEE80211_NODE_STAT(ni, rx_demicfail);
  749                         goto out;
  750                 }
  751                 /* copy to listener after decrypt */
  752                 if (ieee80211_radiotap_active_vap(vap))
  753                         ieee80211_radiotap_rx(vap, m);
  754                 need_tap = 0;
  755                 /*
  756                  * Finally, strip the 802.11 header.
  757                  */
  758                 m = ieee80211_decap(vap, m, hdrspace);
  759                 if (m == NULL) {
  760                         /* XXX mask bit to check for both */
  761                         /* don't count Null data frames as errors */
  762                         if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
  763                             subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
  764                                 goto out;
  765                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
  766                             ni->ni_macaddr, "data", "%s", "decap error");
  767                         vap->iv_stats.is_rx_decap++;
  768                         IEEE80211_NODE_STAT(ni, rx_decap);
  769                         goto err;
  770                 }
  771                 eh = mtod(m, struct ether_header *);
  772                 if (!ieee80211_node_is_authorized(ni)) {
  773                         /*
  774                          * Deny any non-PAE frames received prior to
  775                          * authorization.  For open/shared-key
  776                          * authentication the port is mark authorized
  777                          * after authentication completes.  For 802.1x
  778                          * the port is not marked authorized by the
  779                          * authenticator until the handshake has completed.
  780                          */
  781                         if (eh->ether_type != htons(ETHERTYPE_PAE)) {
  782                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
  783                                     eh->ether_shost, "data",
  784                                     "unauthorized port: ether type 0x%x len %u",
  785                                     eh->ether_type, m->m_pkthdr.len);
  786                                 vap->iv_stats.is_rx_unauth++;
  787                                 IEEE80211_NODE_STAT(ni, rx_unauth);
  788                                 goto err;
  789                         }
  790                 } else {
  791                         /*
  792                          * When denying unencrypted frames, discard
  793                          * any non-PAE frames received without encryption.
  794                          */
  795                         if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
  796                             (key == NULL && (m->m_flags & M_WEP) == 0) &&
  797                             eh->ether_type != htons(ETHERTYPE_PAE)) {
  798                                 /*
  799                                  * Drop unencrypted frames.
  800                                  */
  801                                 vap->iv_stats.is_rx_unencrypted++;
  802                                 IEEE80211_NODE_STAT(ni, rx_unencrypted);
  803                                 goto out;
  804                         }
  805                 }
  806                 /* XXX require HT? */
  807                 if (qos & IEEE80211_QOS_AMSDU) {
  808                         m = ieee80211_decap_amsdu(ni, m);
  809                         if (m == NULL)
  810                                 return IEEE80211_FC0_TYPE_DATA;
  811                 } else {
  812 #ifdef IEEE80211_SUPPORT_SUPERG
  813                         m = ieee80211_decap_fastframe(vap, ni, m);
  814                         if (m == NULL)
  815                                 return IEEE80211_FC0_TYPE_DATA;
  816 #endif
  817                 }
  818                 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
  819                         ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
  820                 else
  821                         hostap_deliver_data(vap, ni, m);
  822                 return IEEE80211_FC0_TYPE_DATA;
  823 
  824         case IEEE80211_FC0_TYPE_MGT:
  825                 vap->iv_stats.is_rx_mgmt++;
  826                 IEEE80211_NODE_STAT(ni, rx_mgmt);
  827                 if (dir != IEEE80211_FC1_DIR_NODS) {
  828                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  829                             wh, "mgt", "incorrect dir 0x%x", dir);
  830                         vap->iv_stats.is_rx_wrongdir++;
  831                         goto err;
  832                 }
  833                 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
  834                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
  835                             ni->ni_macaddr, "mgt", "too short: len %u",
  836                             m->m_pkthdr.len);
  837                         vap->iv_stats.is_rx_tooshort++;
  838                         goto out;
  839                 }
  840                 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
  841                         /* ensure return frames are unicast */
  842                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
  843                             wh, NULL, "source is multicast: %s",
  844                             ether_sprintf(wh->i_addr2));
  845                         vap->iv_stats.is_rx_mgtdiscard++;       /* XXX stat */
  846                         goto out;
  847                 }
  848 #ifdef IEEE80211_DEBUG
  849                 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
  850                     ieee80211_msg_dumppkts(vap)) {
  851                         if_printf(ifp, "received %s from %s rssi %d\n",
  852                             ieee80211_mgt_subtype_name[subtype >>
  853                                 IEEE80211_FC0_SUBTYPE_SHIFT],
  854                             ether_sprintf(wh->i_addr2), rssi);
  855                 }
  856 #endif
  857                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
  858                         if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
  859                                 /*
  860                                  * Only shared key auth frames with a challenge
  861                                  * should be encrypted, discard all others.
  862                                  */
  863                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  864                                     wh, NULL,
  865                                     "%s", "WEP set but not permitted");
  866                                 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
  867                                 goto out;
  868                         }
  869                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
  870                                 /*
  871                                  * Discard encrypted frames when privacy is off.
  872                                  */
  873                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
  874                                     wh, NULL, "%s", "WEP set but PRIVACY off");
  875                                 vap->iv_stats.is_rx_noprivacy++;
  876                                 goto out;
  877                         }
  878                         hdrspace = ieee80211_hdrspace(ic, wh);
  879                         key = ieee80211_crypto_decap(ni, m, hdrspace);
  880                         if (key == NULL) {
  881                                 /* NB: stats+msgs handled in crypto_decap */
  882                                 goto out;
  883                         }
  884                         wh = mtod(m, struct ieee80211_frame *);
  885                         wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
  886                 }
  887                 /*
  888                  * Pass the packet to radiotap before calling iv_recv_mgmt().
  889                  * Otherwise iv_recv_mgmt() might pass another packet to
  890                  * radiotap, resulting in out of order packet captures.
  891                  */
  892                 if (ieee80211_radiotap_active_vap(vap))
  893                         ieee80211_radiotap_rx(vap, m);
  894                 need_tap = 0;
  895                 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
  896                 goto out;
  897 
  898         case IEEE80211_FC0_TYPE_CTL:
  899                 vap->iv_stats.is_rx_ctl++;
  900                 IEEE80211_NODE_STAT(ni, rx_ctrl);
  901                 vap->iv_recv_ctl(ni, m, subtype);
  902                 goto out;
  903         default:
  904                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
  905                     wh, "bad", "frame type 0x%x", type);
  906                 /* should not come here */
  907                 break;
  908         }
  909 err:
  910         ifp->if_ierrors++;
  911 out:
  912         if (m != NULL) {
  913                 if (need_tap && ieee80211_radiotap_active_vap(vap))
  914                         ieee80211_radiotap_rx(vap, m);
  915                 m_freem(m);
  916         }
  917         return type;
  918 #undef SEQ_LEQ
  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: iswapoui 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                  * prreq frame format
 1804                  *      [tlv] ssid
 1805                  *      [tlv] supported rates
 1806                  *      [tlv] extended supported rates
 1807                  */
 1808                 ssid = rates = xrates = NULL;
 1809                 sfrm = frm;
 1810                 while (efrm - frm > 1) {
 1811                         IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
 1812                         switch (*frm) {
 1813                         case IEEE80211_ELEMID_SSID:
 1814                                 ssid = frm;
 1815                                 break;
 1816                         case IEEE80211_ELEMID_RATES:
 1817                                 rates = frm;
 1818                                 break;
 1819                         case IEEE80211_ELEMID_XRATES:
 1820                                 xrates = frm;
 1821                                 break;
 1822                         }
 1823                         frm += frm[1] + 2;
 1824                 }
 1825                 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
 1826                 if (xrates != NULL)
 1827                         IEEE80211_VERIFY_ELEMENT(xrates,
 1828                                 IEEE80211_RATE_MAXSIZE - rates[1], return);
 1829                 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
 1830                 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
 1831                 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
 1832                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
 1833                             wh, NULL,
 1834                             "%s", "no ssid with ssid suppression enabled");
 1835                         vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
 1836                         return;
 1837                 }
 1838 
 1839                 /* XXX find a better class or define it's own */
 1840                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
 1841                     "%s", "recv probe req");
 1842                 /*
 1843                  * Some legacy 11b clients cannot hack a complete
 1844                  * probe response frame.  When the request includes
 1845                  * only a bare-bones rate set, communicate this to
 1846                  * the transmit side.
 1847                  */
 1848                 ieee80211_send_proberesp(vap, wh->i_addr2,
 1849                     is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
 1850                 break;
 1851 
 1852         case IEEE80211_FC0_SUBTYPE_AUTH: {
 1853                 uint16_t algo, seq, status;
 1854 
 1855                 if (vap->iv_state != IEEE80211_S_RUN) {
 1856                         vap->iv_stats.is_rx_mgtdiscard++;
 1857                         return;
 1858                 }
 1859                 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
 1860                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
 1861                             wh, NULL, "%s", "wrong bssid");
 1862                         vap->iv_stats.is_rx_wrongbss++; /*XXX unique stat?*/
 1863                         return;
 1864                 }
 1865                 /*
 1866                  * auth frame format
 1867                  *      [2] algorithm
 1868                  *      [2] sequence
 1869                  *      [2] status
 1870                  *      [tlv*] challenge
 1871                  */
 1872                 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
 1873                 algo   = le16toh(*(uint16_t *)frm);
 1874                 seq    = le16toh(*(uint16_t *)(frm + 2));
 1875                 status = le16toh(*(uint16_t *)(frm + 4));
 1876                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
 1877                     "recv auth frame with algorithm %d seq %d", algo, seq);
 1878                 /*
 1879                  * Consult the ACL policy module if setup.
 1880                  */
 1881                 if (vap->iv_acl != NULL &&
 1882                     !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
 1883                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
 1884                             wh, NULL, "%s", "disallowed by ACL");
 1885                         vap->iv_stats.is_rx_acl++;
 1886                         ieee80211_send_error(ni, wh->i_addr2,
 1887                             IEEE80211_FC0_SUBTYPE_AUTH,
 1888                             (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
 1889                         return;
 1890                 }
 1891                 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
 1892                         IEEE80211_DISCARD(vap,
 1893                             IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
 1894                             wh, NULL, "%s", "TKIP countermeasures enabled");
 1895                         vap->iv_stats.is_rx_auth_countermeasures++;
 1896                         ieee80211_send_error(ni, wh->i_addr2,
 1897                                 IEEE80211_FC0_SUBTYPE_AUTH,
 1898                                 IEEE80211_REASON_MIC_FAILURE);
 1899                         return;
 1900                 }
 1901                 if (algo == IEEE80211_AUTH_ALG_SHARED)
 1902                         hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
 1903                             seq, status);
 1904                 else if (algo == IEEE80211_AUTH_ALG_OPEN)
 1905                         hostap_auth_open(ni, wh, rssi, nf, seq, status);
 1906                 else if (algo == IEEE80211_AUTH_ALG_LEAP) {
 1907                         authalgreject(ni, wh, algo,
 1908                             seq+1, IEEE80211_STATUS_ALG);
 1909                         return;
 1910                 } else {
 1911                         /*
 1912                          * We assume that an unknown algorithm is the result
 1913                          * of a decryption failure on a shared key auth frame;
 1914                          * return a status code appropriate for that instead
 1915                          * of IEEE80211_STATUS_ALG.
 1916                          *
 1917                          * NB: a seq# of 4 is intentional; the decrypted
 1918                          *     frame likely has a bogus seq value.
 1919                          */
 1920                         authalgreject(ni, wh, algo,
 1921                             4, IEEE80211_STATUS_CHALLENGE);
 1922                         return;
 1923                 } 
 1924                 break;
 1925         }
 1926 
 1927         case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
 1928         case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
 1929                 uint16_t capinfo, lintval;
 1930                 struct ieee80211_rsnparms rsnparms;
 1931 
 1932                 if (vap->iv_state != IEEE80211_S_RUN) {
 1933                         vap->iv_stats.is_rx_mgtdiscard++;
 1934                         return;
 1935                 }
 1936                 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
 1937                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
 1938                             wh, NULL, "%s", "wrong bssid");
 1939                         vap->iv_stats.is_rx_assoc_bss++;
 1940                         return;
 1941                 }
 1942                 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
 1943                         reassoc = 1;
 1944                         resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
 1945                 } else {
 1946                         reassoc = 0;
 1947                         resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
 1948                 }
 1949                 if (ni == vap->iv_bss) {
 1950                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
 1951                             "deny %s request, sta not authenticated",
 1952                             reassoc ? "reassoc" : "assoc");
 1953                         ieee80211_send_error(ni, wh->i_addr2,
 1954                             IEEE80211_FC0_SUBTYPE_DEAUTH,
 1955                             IEEE80211_REASON_ASSOC_NOT_AUTHED);
 1956                         vap->iv_stats.is_rx_assoc_notauth++;
 1957                         return;
 1958                 }
 1959 
 1960                 /*
 1961                  * asreq frame format
 1962                  *      [2] capability information
 1963                  *      [2] listen interval
 1964                  *      [6*] current AP address (reassoc only)
 1965                  *      [tlv] ssid
 1966                  *      [tlv] supported rates
 1967                  *      [tlv] extended supported rates
 1968                  *      [tlv] WPA or RSN
 1969                  *      [tlv] HT capabilities
 1970                  *      [tlv] Atheros capabilities
 1971                  */
 1972                 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
 1973                 capinfo = le16toh(*(uint16_t *)frm);    frm += 2;
 1974                 lintval = le16toh(*(uint16_t *)frm);    frm += 2;
 1975                 if (reassoc)
 1976                         frm += 6;       /* ignore current AP info */
 1977                 ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
 1978                 sfrm = frm;
 1979                 while (efrm - frm > 1) {
 1980                         IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
 1981                         switch (*frm) {
 1982                         case IEEE80211_ELEMID_SSID:
 1983                                 ssid = frm;
 1984                                 break;
 1985                         case IEEE80211_ELEMID_RATES:
 1986                                 rates = frm;
 1987                                 break;
 1988                         case IEEE80211_ELEMID_XRATES:
 1989                                 xrates = frm;
 1990                                 break;
 1991                         case IEEE80211_ELEMID_RSN:
 1992                                 rsn = frm;
 1993                                 break;
 1994                         case IEEE80211_ELEMID_HTCAP:
 1995                                 htcap = frm;
 1996                                 break;
 1997                         case IEEE80211_ELEMID_VENDOR:
 1998                                 if (iswpaoui(frm))
 1999                                         wpa = frm;
 2000                                 else if (iswmeinfo(frm))
 2001                                         wme = frm;
 2002 #ifdef IEEE80211_SUPPORT_SUPERG
 2003                                 else if (isatherosoui(frm))
 2004                                         ath = frm;
 2005 #endif
 2006                                 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
 2007                                         if (ishtcapoui(frm) && htcap == NULL)
 2008                                                 htcap = frm;
 2009                                 }
 2010                                 break;
 2011                         }
 2012                         frm += frm[1] + 2;
 2013                 }
 2014                 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
 2015                 if (xrates != NULL)
 2016                         IEEE80211_VERIFY_ELEMENT(xrates,
 2017                                 IEEE80211_RATE_MAXSIZE - rates[1], return);
 2018                 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
 2019                 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
 2020                 if (htcap != NULL) {
 2021                         IEEE80211_VERIFY_LENGTH(htcap[1],
 2022                              htcap[0] == IEEE80211_ELEMID_VENDOR ?
 2023                                  4 + sizeof(struct ieee80211_ie_htcap)-2 :
 2024                                  sizeof(struct ieee80211_ie_htcap)-2,
 2025                              return);           /* XXX just NULL out? */
 2026                 }
 2027 
 2028                 if ((vap->iv_flags & IEEE80211_F_WPA) &&
 2029                     !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
 2030                         return;
 2031                 /* discard challenge after association */
 2032                 if (ni->ni_challenge != NULL) {
 2033                         free(ni->ni_challenge, M_80211_NODE);
 2034                         ni->ni_challenge = NULL;
 2035                 }
 2036                 /* NB: 802.11 spec says to ignore station's privacy bit */
 2037                 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
 2038                         capinfomismatch(ni, wh, reassoc, resp,
 2039                             "capability", capinfo);
 2040                         return;
 2041                 }
 2042                 /*
 2043                  * Disallow re-associate w/ invalid slot time setting.
 2044                  */
 2045                 if (ni->ni_associd != 0 &&
 2046                     IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
 2047                     ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
 2048                         capinfomismatch(ni, wh, reassoc, resp,
 2049                             "slot time", capinfo);
 2050                         return;
 2051                 }
 2052                 rate = ieee80211_setup_rates(ni, rates, xrates,
 2053                                 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
 2054                                 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
 2055                 if (rate & IEEE80211_RATE_BASIC) {
 2056                         ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
 2057                         vap->iv_stats.is_rx_assoc_norate++;
 2058                         return;
 2059                 }
 2060                 /*
 2061                  * If constrained to 11g-only stations reject an
 2062                  * 11b-only station.  We cheat a bit here by looking
 2063                  * at the max negotiated xmit rate and assuming anyone
 2064                  * with a best rate <24Mb/s is an 11b station.
 2065                  */
 2066                 if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
 2067                         ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
 2068                         vap->iv_stats.is_rx_assoc_norate++;
 2069                         return;
 2070                 }
 2071                 /*
 2072                  * Do HT rate set handling and setup HT node state.
 2073                  */
 2074                 ni->ni_chan = vap->iv_bss->ni_chan;
 2075                 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
 2076                         rate = ieee80211_setup_htrates(ni, htcap,
 2077                                 IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
 2078                                 IEEE80211_F_DOBRS);
 2079                         if (rate & IEEE80211_RATE_BASIC) {
 2080                                 ratesetmismatch(ni, wh, reassoc, resp,
 2081                                     "HT", rate);
 2082                                 vap->iv_stats.is_ht_assoc_norate++;
 2083                                 return;
 2084                         }
 2085                         ieee80211_ht_node_init(ni);
 2086                         ieee80211_ht_updatehtcap(ni, htcap);
 2087                 } else if (ni->ni_flags & IEEE80211_NODE_HT)
 2088                         ieee80211_ht_node_cleanup(ni);
 2089 #ifdef IEEE80211_SUPPORT_SUPERG
 2090                 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
 2091                         ieee80211_ff_node_cleanup(ni);
 2092 #endif
 2093                 /*
 2094                  * Allow AMPDU operation only with unencrypted traffic
 2095                  * or AES-CCM; the 11n spec only specifies these ciphers
 2096                  * so permitting any others is undefined and can lead
 2097                  * to interoperability problems.
 2098                  */
 2099                 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
 2100                     (((vap->iv_flags & IEEE80211_F_WPA) &&
 2101                       rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
 2102                      (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
 2103                         IEEE80211_NOTE(vap,
 2104                             IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
 2105                             "disallow HT use because WEP or TKIP requested, "
 2106                             "capinfo 0x%x ucastcipher %d", capinfo,
 2107                             rsnparms.rsn_ucastcipher);
 2108                         ieee80211_ht_node_cleanup(ni);
 2109                         vap->iv_stats.is_ht_assoc_downgrade++;
 2110                 }
 2111                 /*
 2112                  * If constrained to 11n-only stations reject legacy stations.
 2113                  */
 2114                 if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
 2115                     (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
 2116                         htcapmismatch(ni, wh, reassoc, resp);
 2117                         vap->iv_stats.is_ht_assoc_nohtcap++;
 2118                         return;
 2119                 }
 2120                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
 2121                 ni->ni_noise = nf;
 2122                 ni->ni_intval = lintval;
 2123                 ni->ni_capinfo = capinfo;
 2124                 ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
 2125                 ni->ni_fhindex = vap->iv_bss->ni_fhindex;
 2126                 /*
 2127                  * Store the IEs.
 2128                  * XXX maybe better to just expand
 2129                  */
 2130                 if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
 2131 #define setie(_ie, _off)        ieee80211_ies_setie(ni->ni_ies, _ie, _off)
 2132                         if (wpa != NULL)
 2133                                 setie(wpa_ie, wpa - sfrm);
 2134                         if (rsn != NULL)
 2135                                 setie(rsn_ie, rsn - sfrm);
 2136                         if (htcap != NULL)
 2137                                 setie(htcap_ie, htcap - sfrm);
 2138                         if (wme != NULL) {
 2139                                 setie(wme_ie, wme - sfrm);
 2140                                 /*
 2141                                  * Mark node as capable of QoS.
 2142                                  */
 2143                                 ni->ni_flags |= IEEE80211_NODE_QOS;
 2144                         } else
 2145                                 ni->ni_flags &= ~IEEE80211_NODE_QOS;
 2146 #ifdef IEEE80211_SUPPORT_SUPERG
 2147                         if (ath != NULL) {
 2148                                 setie(ath_ie, ath - sfrm);
 2149                                 /* 
 2150                                  * Parse ATH station parameters.
 2151                                  */
 2152                                 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
 2153                         } else
 2154 #endif
 2155                                 ni->ni_ath_flags = 0;
 2156 #undef setie
 2157                 } else {
 2158                         ni->ni_flags &= ~IEEE80211_NODE_QOS;
 2159                         ni->ni_ath_flags = 0;
 2160                 }
 2161                 ieee80211_node_join(ni, resp);
 2162                 ieee80211_deliver_l2uf(ni);
 2163                 break;
 2164         }
 2165 
 2166         case IEEE80211_FC0_SUBTYPE_DEAUTH:
 2167         case IEEE80211_FC0_SUBTYPE_DISASSOC: {
 2168                 uint16_t reason;
 2169 
 2170                 if (vap->iv_state != IEEE80211_S_RUN ||
 2171                     /* NB: can happen when in promiscuous mode */
 2172                     !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
 2173                         vap->iv_stats.is_rx_mgtdiscard++;
 2174                         break;
 2175                 }
 2176                 /*
 2177                  * deauth/disassoc frame format
 2178                  *      [2] reason
 2179                  */
 2180                 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
 2181                 reason = le16toh(*(uint16_t *)frm);
 2182                 if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
 2183                         vap->iv_stats.is_rx_deauth++;
 2184                         IEEE80211_NODE_STAT(ni, rx_deauth);
 2185                 } else {
 2186                         vap->iv_stats.is_rx_disassoc++;
 2187                         IEEE80211_NODE_STAT(ni, rx_disassoc);
 2188                 }
 2189                 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
 2190                     "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
 2191                         IEEE80211_FC0_SUBTYPE_SHIFT], reason);
 2192                 if (ni != vap->iv_bss)
 2193                         ieee80211_node_leave(ni);
 2194                 break;
 2195         }
 2196 
 2197         case IEEE80211_FC0_SUBTYPE_ACTION:
 2198                 if (vap->iv_state == IEEE80211_S_RUN) {
 2199                         if (ieee80211_parse_action(ni, m0) == 0)
 2200                                 ic->ic_recv_action(ni, wh, frm, efrm);
 2201                 } else
 2202                         vap->iv_stats.is_rx_mgtdiscard++;
 2203                 break;
 2204 
 2205         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
 2206         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
 2207         default:
 2208                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
 2209                      wh, "mgt", "subtype 0x%x not handled", subtype);
 2210                 vap->iv_stats.is_rx_badsubtype++;
 2211                 break;
 2212         }
 2213 }
 2214 
 2215 static void
 2216 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
 2217 {
 2218         switch (subtype) {
 2219         case IEEE80211_FC0_SUBTYPE_PS_POLL:
 2220                 hostap_recv_pspoll(ni, m);
 2221                 break;
 2222         case IEEE80211_FC0_SUBTYPE_BAR:
 2223                 ieee80211_recv_bar(ni, m);
 2224                 break;
 2225         }
 2226 }
 2227 
 2228 /*
 2229  * Process a received ps-poll frame.
 2230  */
 2231 static void
 2232 hostap_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
 2233 {
 2234         struct ieee80211vap *vap = ni->ni_vap;
 2235         struct ieee80211_frame_min *wh;
 2236         struct ifnet *ifp;
 2237         struct mbuf *m;
 2238         uint16_t aid;
 2239         int qlen;
 2240 
 2241         wh = mtod(m0, struct ieee80211_frame_min *);
 2242         if (ni->ni_associd == 0) {
 2243                 IEEE80211_DISCARD(vap,
 2244                     IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
 2245                     (struct ieee80211_frame *) wh, NULL,
 2246                     "%s", "unassociated station");
 2247                 vap->iv_stats.is_ps_unassoc++;
 2248                 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
 2249                         IEEE80211_REASON_NOT_ASSOCED);
 2250                 return;
 2251         }
 2252 
 2253         aid = le16toh(*(uint16_t *)wh->i_dur);
 2254         if (aid != ni->ni_associd) {
 2255                 IEEE80211_DISCARD(vap,
 2256                     IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
 2257                     (struct ieee80211_frame *) wh, NULL,
 2258                     "aid mismatch: sta aid 0x%x poll aid 0x%x",
 2259                     ni->ni_associd, aid);
 2260                 vap->iv_stats.is_ps_badaid++;
 2261                 /*
 2262                  * NB: We used to deauth the station but it turns out
 2263                  * the Blackberry Curve 8230 (and perhaps other devices) 
 2264                  * sometimes send the wrong AID when WME is negotiated.
 2265                  * Being more lenient here seems ok as we already check
 2266                  * the station is associated and we only return frames
 2267                  * queued for the station (i.e. we don't use the AID).
 2268                  */
 2269                 return;
 2270         }
 2271 
 2272         /* Okay, take the first queued packet and put it out... */
 2273         m = ieee80211_node_psq_dequeue(ni, &qlen);
 2274         if (m == NULL) {
 2275                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
 2276                     "%s", "recv ps-poll, but queue empty");
 2277                 ieee80211_send_nulldata(ieee80211_ref_node(ni));
 2278                 vap->iv_stats.is_ps_qempty++;   /* XXX node stat */
 2279                 if (vap->iv_set_tim != NULL)
 2280                         vap->iv_set_tim(ni, 0); /* just in case */
 2281                 return;
 2282         }
 2283         /* 
 2284          * If there are more packets, set the more packets bit
 2285          * in the packet dispatched to the station; otherwise
 2286          * turn off the TIM bit.
 2287          */
 2288         if (qlen != 0) {
 2289                 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
 2290                     "recv ps-poll, send packet, %u still queued", qlen);
 2291                 m->m_flags |= M_MORE_DATA;
 2292         } else {
 2293                 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
 2294                     "%s", "recv ps-poll, send packet, queue empty");
 2295                 if (vap->iv_set_tim != NULL)
 2296                         vap->iv_set_tim(ni, 0);
 2297         }
 2298         m->m_flags |= M_PWR_SAV;                /* bypass PS handling */
 2299 
 2300         if (m->m_flags & M_ENCAP)
 2301                 ifp = vap->iv_ic->ic_ifp;
 2302         else
 2303                 ifp = vap->iv_ifp;
 2304         IF_ENQUEUE(&ifp->if_snd, m);
 2305         if_start(ifp);
 2306 }

Cache object: d9490ac3c7f4f0a56ced9f50d9c156b6


[ 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.