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


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

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

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

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

Cache object: 18e21a235ce1592f10ebafea2ff8a592


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