Home | History | Annotate | Line # | Download | only in net80211
ieee80211_output.c revision 1.63.2.6
      1  1.63.2.6    martin /*	$NetBSD: ieee80211_output.c,v 1.63.2.6 2020/04/13 08:05:16 martin Exp $	*/
      2  1.63.2.2      phil 
      3  1.63.2.1      phil /*-
      4  1.63.2.1      phil  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  1.63.2.1      phil  *
      6       1.1    dyoung  * Copyright (c) 2001 Atsushi Onoe
      7  1.63.2.1      phil  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
      8       1.1    dyoung  * All rights reserved.
      9       1.1    dyoung  *
     10       1.1    dyoung  * Redistribution and use in source and binary forms, with or without
     11       1.1    dyoung  * modification, are permitted provided that the following conditions
     12       1.1    dyoung  * are met:
     13       1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     14       1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     15       1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     17       1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     18       1.1    dyoung  *
     19       1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20       1.1    dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21       1.1    dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22       1.1    dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23       1.1    dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24       1.1    dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25       1.1    dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26       1.1    dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27       1.1    dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28       1.1    dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29       1.1    dyoung  */
     30       1.1    dyoung 
     31       1.1    dyoung #include <sys/cdefs.h>
     32  1.63.2.5  christos #ifdef __NetBSD__
     33  1.63.2.6    martin __KERNEL_RCSID(0, "$NetBSD: ieee80211_output.c,v 1.63.2.6 2020/04/13 08:05:16 martin Exp $");
     34  1.63.2.2      phil #endif
     35       1.1    dyoung 
     36  1.63.2.5  christos #ifdef _KERNEL_OPT
     37       1.1    dyoung #include "opt_inet.h"
     38  1.63.2.1      phil #include "opt_inet6.h"
     39  1.63.2.1      phil #include "opt_wlan.h"
     40  1.63.2.5  christos #endif
     41       1.1    dyoung 
     42       1.1    dyoung #include <sys/param.h>
     43      1.29    dyoung #include <sys/systm.h>
     44       1.1    dyoung #include <sys/kernel.h>
     45  1.63.2.1      phil #include <sys/malloc.h>
     46  1.63.2.1      phil #include <sys/mbuf.h>
     47       1.1    dyoung #include <sys/endian.h>
     48       1.1    dyoung 
     49  1.63.2.1      phil #include <sys/socket.h>
     50  1.63.2.1      phil 
     51  1.63.2.1      phil #include <net/bpf.h>
     52  1.63.2.2      phil #if __FreeBSD__
     53  1.63.2.1      phil #include <net/ethernet.h>
     54  1.63.2.2      phil #endif
     55       1.1    dyoung #include <net/if.h>
     56  1.63.2.2      phil #if __FreeBSD__
     57  1.63.2.1      phil #include <net/if_var.h>
     58  1.63.2.2      phil #endif
     59      1.29    dyoung #include <net/if_llc.h>
     60       1.1    dyoung #include <net/if_media.h>
     61  1.63.2.2      phil #if __FreeBSD__
     62  1.63.2.1      phil #include <net/if_vlan_var.h>
     63  1.63.2.2      phil #endif
     64  1.63.2.2      phil #ifdef __NetBSD__
     65  1.63.2.2      phil #include <net/if_ether.h>
     66  1.63.2.2      phil #include <net/route.h>
     67  1.63.2.2      phil #endif
     68       1.1    dyoung 
     69       1.5    dyoung #include <net80211/ieee80211_var.h>
     70  1.63.2.1      phil #include <net80211/ieee80211_regdomain.h>
     71  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
     72  1.63.2.1      phil #include <net80211/ieee80211_superg.h>
     73  1.63.2.1      phil #endif
     74  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_TDMA
     75  1.63.2.1      phil #include <net80211/ieee80211_tdma.h>
     76  1.63.2.1      phil #endif
     77  1.63.2.1      phil #include <net80211/ieee80211_wds.h>
     78  1.63.2.1      phil #include <net80211/ieee80211_mesh.h>
     79  1.63.2.1      phil #include <net80211/ieee80211_vht.h>
     80       1.1    dyoung 
     81  1.63.2.1      phil #if defined(INET) || defined(INET6)
     82  1.63.2.1      phil #include <netinet/in.h>
     83  1.63.2.1      phil #endif
     84       1.1    dyoung 
     85       1.1    dyoung #ifdef INET
     86  1.63.2.2      phil #if __FreeBSD__
     87  1.63.2.1      phil #include <netinet/if_ether.h>
     88  1.63.2.2      phil #endif
     89      1.29    dyoung #include <netinet/in_systm.h>
     90      1.29    dyoung #include <netinet/ip.h>
     91  1.63.2.1      phil #endif
     92  1.63.2.1      phil #ifdef INET6
     93  1.63.2.1      phil #include <netinet/ip6.h>
     94       1.5    dyoung #endif
     95       1.1    dyoung 
     96  1.63.2.2      phil #if __FreeBSD__
     97  1.63.2.1      phil #include <security/mac/mac_framework.h>
     98  1.63.2.2      phil #endif
     99  1.63.2.2      phil 
    100  1.63.2.2      phil #ifdef __NetBSD__
    101  1.63.2.2      phil #undef  KASSERT
    102  1.63.2.2      phil #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
    103  1.63.2.2      phil #endif
    104  1.63.2.1      phil 
    105  1.63.2.1      phil #define	ETHER_HEADER_COPY(dst, src) \
    106  1.63.2.1      phil 	memcpy(dst, src, sizeof(struct ether_header))
    107  1.63.2.1      phil 
    108  1.63.2.1      phil static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
    109      1.46    dyoung 	u_int hdrsize, u_int ciphdrsize, u_int mtu);
    110  1.63.2.1      phil static	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
    111      1.46    dyoung 
    112      1.15   mycroft #ifdef IEEE80211_DEBUG
    113      1.15   mycroft /*
    114      1.15   mycroft  * Decide if an outbound management frame should be
    115      1.15   mycroft  * printed when debugging is enabled.  This filters some
    116      1.15   mycroft  * of the less interesting frames that come frequently
    117      1.15   mycroft  * (e.g. beacons).
    118      1.15   mycroft  */
    119      1.15   mycroft static __inline int
    120  1.63.2.1      phil doprint(struct ieee80211vap *vap, int subtype)
    121      1.15   mycroft {
    122      1.15   mycroft 	switch (subtype) {
    123      1.15   mycroft 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    124  1.63.2.1      phil 		return (vap->iv_opmode == IEEE80211_M_IBSS);
    125      1.15   mycroft 	}
    126      1.15   mycroft 	return 1;
    127      1.15   mycroft }
    128      1.15   mycroft #endif
    129      1.15   mycroft 
    130       1.1    dyoung /*
    131  1.63.2.1      phil  * Transmit a frame to the given destination on the given VAP.
    132  1.63.2.1      phil  *
    133  1.63.2.1      phil  * It's up to the caller to figure out the details of who this
    134  1.63.2.1      phil  * is going to and resolving the node.
    135  1.63.2.1      phil  *
    136  1.63.2.1      phil  * This routine takes care of queuing it for power save,
    137  1.63.2.1      phil  * A-MPDU state stuff, fast-frames state stuff, encapsulation
    138  1.63.2.1      phil  * if required, then passing it up to the driver layer.
    139  1.63.2.1      phil  *
    140  1.63.2.1      phil  * This routine (for now) consumes the mbuf and frees the node
    141  1.63.2.1      phil  * reference; it ideally will return a TX status which reflects
    142  1.63.2.1      phil  * whether the mbuf was consumed or not, so the caller can
    143  1.63.2.1      phil  * free the mbuf (if appropriate) and the node reference (again,
    144  1.63.2.1      phil  * if appropriate.)
    145  1.63.2.1      phil  */
    146  1.63.2.1      phil int
    147  1.63.2.1      phil ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
    148  1.63.2.1      phil     struct ieee80211_node *ni)
    149  1.63.2.1      phil {
    150  1.63.2.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    151  1.63.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    152  1.63.2.1      phil 	int mcast;
    153  1.63.2.1      phil 
    154  1.63.2.1      phil 	if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
    155  1.63.2.1      phil 	    (m->m_flags & M_PWR_SAV) == 0) {
    156  1.63.2.1      phil 		/*
    157  1.63.2.1      phil 		 * Station in power save mode; pass the frame
    158  1.63.2.1      phil 		 * to the 802.11 layer and continue.  We'll get
    159  1.63.2.1      phil 		 * the frame back when the time is right.
    160  1.63.2.1      phil 		 * XXX lose WDS vap linkage?
    161  1.63.2.1      phil 		 */
    162  1.63.2.1      phil 		if (ieee80211_pwrsave(ni, m) != 0)
    163  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    164  1.63.2.1      phil 		ieee80211_free_node(ni);
    165  1.63.2.1      phil 
    166  1.63.2.1      phil 		/*
    167  1.63.2.1      phil 		 * We queued it fine, so tell the upper layer
    168  1.63.2.1      phil 		 * that we consumed it.
    169  1.63.2.1      phil 		 */
    170  1.63.2.1      phil 		return (0);
    171  1.63.2.1      phil 	}
    172  1.63.2.1      phil 	/* calculate priority so drivers can find the tx queue */
    173  1.63.2.1      phil 	if (ieee80211_classify(ni, m)) {
    174  1.63.2.1      phil 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
    175  1.63.2.1      phil 		    ni->ni_macaddr, NULL,
    176  1.63.2.1      phil 		    "%s", "classification failure");
    177  1.63.2.1      phil 		vap->iv_stats.is_tx_classify++;
    178  1.63.2.1      phil 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    179  1.63.2.1      phil 		m_freem(m);
    180  1.63.2.1      phil 		ieee80211_free_node(ni);
    181  1.63.2.1      phil 
    182  1.63.2.1      phil 		/* XXX better status? */
    183  1.63.2.1      phil 		return (0);
    184  1.63.2.1      phil 	}
    185  1.63.2.1      phil 	/*
    186  1.63.2.1      phil 	 * Stash the node pointer.  Note that we do this after
    187  1.63.2.1      phil 	 * any call to ieee80211_dwds_mcast because that code
    188  1.63.2.1      phil 	 * uses any existing value for rcvif to identify the
    189  1.63.2.1      phil 	 * interface it (might have been) received on.
    190  1.63.2.1      phil 	 */
    191  1.63.2.2      phil #if __FreeBSD__
    192  1.63.2.1      phil 	m->m_pkthdr.rcvif = (void *)ni;
    193  1.63.2.2      phil #elif __NetBSD__
    194  1.63.2.2      phil 	m_set_rcvif(m, (void *)ni);
    195  1.63.2.2      phil #endif
    196  1.63.2.1      phil 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
    197  1.63.2.1      phil 
    198  1.63.2.1      phil 	BPF_MTAP(ifp, m);		/* 802.3 tx */
    199  1.63.2.1      phil 
    200  1.63.2.1      phil 	/*
    201  1.63.2.1      phil 	 * Check if A-MPDU tx aggregation is setup or if we
    202  1.63.2.1      phil 	 * should try to enable it.  The sta must be associated
    203  1.63.2.1      phil 	 * with HT and A-MPDU enabled for use.  When the policy
    204  1.63.2.1      phil 	 * routine decides we should enable A-MPDU we issue an
    205  1.63.2.1      phil 	 * ADDBA request and wait for a reply.  The frame being
    206  1.63.2.1      phil 	 * encapsulated will go out w/o using A-MPDU, or possibly
    207  1.63.2.1      phil 	 * it might be collected by the driver and held/retransmit.
    208  1.63.2.1      phil 	 * The default ic_ampdu_enable routine handles staggering
    209  1.63.2.1      phil 	 * ADDBA requests in case the receiver NAK's us or we are
    210  1.63.2.1      phil 	 * otherwise unable to establish a BA stream.
    211  1.63.2.1      phil 	 *
    212  1.63.2.1      phil 	 * Don't treat group-addressed frames as candidates for aggregation;
    213  1.63.2.1      phil 	 * net80211 doesn't support 802.11aa-2012 and so group addressed
    214  1.63.2.1      phil 	 * frames will always have sequence numbers allocated from the NON_QOS
    215  1.63.2.1      phil 	 * TID.
    216  1.63.2.1      phil 	 */
    217  1.63.2.1      phil 	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
    218  1.63.2.1      phil 	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)) {
    219  1.63.2.1      phil 		if ((m->m_flags & M_EAPOL) == 0 && (! mcast)) {
    220  1.63.2.1      phil 			int tid = WME_AC_TO_TID(M_WME_GETAC(m));
    221  1.63.2.1      phil 			struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
    222  1.63.2.1      phil 
    223  1.63.2.1      phil 			ieee80211_txampdu_count_packet(tap);
    224  1.63.2.1      phil 			if (IEEE80211_AMPDU_RUNNING(tap)) {
    225  1.63.2.1      phil 				/*
    226  1.63.2.1      phil 				 * Operational, mark frame for aggregation.
    227  1.63.2.1      phil 				 *
    228  1.63.2.1      phil 				 * XXX do tx aggregation here
    229  1.63.2.1      phil 				 */
    230  1.63.2.1      phil 				m->m_flags |= M_AMPDU_MPDU;
    231  1.63.2.1      phil 			} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
    232  1.63.2.1      phil 			    ic->ic_ampdu_enable(ni, tap)) {
    233  1.63.2.1      phil 				/*
    234  1.63.2.1      phil 				 * Not negotiated yet, request service.
    235  1.63.2.1      phil 				 */
    236  1.63.2.1      phil 				ieee80211_ampdu_request(ni, tap);
    237  1.63.2.1      phil 				/* XXX hold frame for reply? */
    238  1.63.2.1      phil 			}
    239  1.63.2.1      phil 		}
    240  1.63.2.1      phil 	}
    241  1.63.2.1      phil 
    242  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
    243  1.63.2.1      phil 	/*
    244  1.63.2.1      phil 	 * Check for AMSDU/FF; queue for aggregation
    245  1.63.2.1      phil 	 *
    246  1.63.2.1      phil 	 * Note: we don't bother trying to do fast frames or
    247  1.63.2.1      phil 	 * A-MSDU encapsulation for 802.3 drivers.  Now, we
    248  1.63.2.1      phil 	 * likely could do it for FF (because it's a magic
    249  1.63.2.1      phil 	 * atheros tunnel LLC type) but I don't think we're going
    250  1.63.2.1      phil 	 * to really need to.  For A-MSDU we'd have to set the
    251  1.63.2.1      phil 	 * A-MSDU QoS bit in the wifi header, so we just plain
    252  1.63.2.1      phil 	 * can't do it.
    253  1.63.2.1      phil 	 *
    254  1.63.2.1      phil 	 * Strictly speaking, we could actually /do/ A-MSDU / FF
    255  1.63.2.1      phil 	 * with A-MPDU together which for certain circumstances
    256  1.63.2.1      phil 	 * is beneficial (eg A-MSDU of TCK ACKs.)  However,
    257  1.63.2.1      phil 	 * I'll ignore that for now so existing behaviour is maintained.
    258  1.63.2.1      phil 	 * Later on it would be good to make "amsdu + ampdu" configurable.
    259  1.63.2.1      phil 	 */
    260  1.63.2.1      phil 	else if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
    261  1.63.2.1      phil 		if ((! mcast) && ieee80211_amsdu_tx_ok(ni)) {
    262  1.63.2.1      phil 			m = ieee80211_amsdu_check(ni, m);
    263  1.63.2.1      phil 			if (m == NULL) {
    264  1.63.2.1      phil 				/* NB: any ni ref held on stageq */
    265  1.63.2.1      phil 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
    266  1.63.2.1      phil 				    "%s: amsdu_check queued frame\n",
    267  1.63.2.1      phil 				    __func__);
    268  1.63.2.1      phil 				return (0);
    269  1.63.2.1      phil 			}
    270  1.63.2.1      phil 		} else if ((! mcast) && IEEE80211_ATH_CAP(vap, ni,
    271  1.63.2.1      phil 		    IEEE80211_NODE_FF)) {
    272  1.63.2.1      phil 			m = ieee80211_ff_check(ni, m);
    273  1.63.2.1      phil 			if (m == NULL) {
    274  1.63.2.1      phil 				/* NB: any ni ref held on stageq */
    275  1.63.2.1      phil 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
    276  1.63.2.1      phil 				    "%s: ff_check queued frame\n",
    277  1.63.2.1      phil 				    __func__);
    278  1.63.2.1      phil 				return (0);
    279  1.63.2.1      phil 			}
    280  1.63.2.1      phil 		}
    281  1.63.2.1      phil 	}
    282  1.63.2.1      phil #endif /* IEEE80211_SUPPORT_SUPERG */
    283  1.63.2.1      phil 
    284  1.63.2.1      phil 	/*
    285  1.63.2.1      phil 	 * Grab the TX lock - serialise the TX process from this
    286  1.63.2.1      phil 	 * point (where TX state is being checked/modified)
    287  1.63.2.1      phil 	 * through to driver queue.
    288  1.63.2.1      phil 	 */
    289  1.63.2.1      phil 	IEEE80211_TX_LOCK(ic);
    290  1.63.2.1      phil 
    291  1.63.2.1      phil 	/*
    292  1.63.2.1      phil 	 * XXX make the encap and transmit code a separate function
    293  1.63.2.1      phil 	 * so things like the FF (and later A-MSDU) path can just call
    294  1.63.2.1      phil 	 * it for flushed frames.
    295  1.63.2.1      phil 	 */
    296  1.63.2.1      phil 	if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
    297  1.63.2.1      phil 		/*
    298  1.63.2.1      phil 		 * Encapsulate the packet in prep for transmission.
    299  1.63.2.1      phil 		 */
    300  1.63.2.1      phil 		m = ieee80211_encap(vap, ni, m);
    301  1.63.2.1      phil 		if (m == NULL) {
    302  1.63.2.1      phil 			/* NB: stat+msg handled in ieee80211_encap */
    303  1.63.2.1      phil 			IEEE80211_TX_UNLOCK(ic);
    304  1.63.2.1      phil 			ieee80211_free_node(ni);
    305  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    306  1.63.2.1      phil 			return (ENOBUFS);
    307  1.63.2.1      phil 		}
    308  1.63.2.1      phil 	}
    309  1.63.2.1      phil 	(void) ieee80211_parent_xmitpkt(ic, m);
    310  1.63.2.1      phil 
    311  1.63.2.1      phil 	/*
    312  1.63.2.1      phil 	 * Unlock at this point - no need to hold it across
    313  1.63.2.1      phil 	 * ieee80211_free_node() (ie, the comlock)
    314  1.63.2.1      phil 	 */
    315  1.63.2.1      phil 	IEEE80211_TX_UNLOCK(ic);
    316  1.63.2.1      phil 	ic->ic_lastdata = ticks;
    317  1.63.2.1      phil 
    318  1.63.2.1      phil 	return (0);
    319  1.63.2.1      phil }
    320  1.63.2.1      phil 
    321  1.63.2.1      phil 
    322  1.63.2.1      phil 
    323  1.63.2.1      phil /*
    324  1.63.2.1      phil  * Send the given mbuf through the given vap.
    325  1.63.2.1      phil  *
    326  1.63.2.1      phil  * This consumes the mbuf regardless of whether the transmit
    327  1.63.2.1      phil  * was successful or not.
    328  1.63.2.1      phil  *
    329  1.63.2.1      phil  * This does none of the initial checks that ieee80211_start()
    330  1.63.2.1      phil  * does (eg CAC timeout, interface wakeup) - the caller must
    331  1.63.2.1      phil  * do this first.
    332  1.63.2.1      phil  */
    333  1.63.2.1      phil static int
    334  1.63.2.1      phil ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
    335  1.63.2.1      phil {
    336  1.63.2.1      phil #define	IS_DWDS(vap) \
    337  1.63.2.1      phil 	(vap->iv_opmode == IEEE80211_M_WDS && \
    338  1.63.2.1      phil 	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
    339  1.63.2.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    340  1.63.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    341  1.63.2.1      phil 	struct ieee80211_node *ni;
    342  1.63.2.1      phil 	struct ether_header *eh;
    343  1.63.2.1      phil 
    344  1.63.2.1      phil 	/*
    345  1.63.2.1      phil 	 * Cancel any background scan.
    346  1.63.2.1      phil 	 */
    347  1.63.2.1      phil 	if (ic->ic_flags & IEEE80211_F_SCAN)
    348  1.63.2.1      phil 		ieee80211_cancel_anyscan(vap);
    349  1.63.2.1      phil 	/*
    350  1.63.2.1      phil 	 * Find the node for the destination so we can do
    351  1.63.2.1      phil 	 * things like power save and fast frames aggregation.
    352  1.63.2.1      phil 	 *
    353  1.63.2.1      phil 	 * NB: past this point various code assumes the first
    354  1.63.2.1      phil 	 *     mbuf has the 802.3 header present (and contiguous).
    355  1.63.2.1      phil 	 */
    356  1.63.2.1      phil 	ni = NULL;
    357  1.63.2.1      phil 	if (m->m_len < sizeof(struct ether_header) &&
    358  1.63.2.1      phil 	   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
    359  1.63.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
    360  1.63.2.1      phil 		    "discard frame, %s\n", "m_pullup failed");
    361  1.63.2.1      phil 		vap->iv_stats.is_tx_nobuf++;	/* XXX */
    362  1.63.2.1      phil 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    363  1.63.2.1      phil 		return (ENOBUFS);
    364  1.63.2.1      phil 	}
    365  1.63.2.1      phil 	eh = mtod(m, struct ether_header *);
    366  1.63.2.1      phil 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
    367  1.63.2.1      phil 		if (IS_DWDS(vap)) {
    368  1.63.2.1      phil 			/*
    369  1.63.2.1      phil 			 * Only unicast frames from the above go out
    370  1.63.2.1      phil 			 * DWDS vaps; multicast frames are handled by
    371  1.63.2.1      phil 			 * dispatching the frame as it comes through
    372  1.63.2.1      phil 			 * the AP vap (see below).
    373  1.63.2.1      phil 			 */
    374  1.63.2.1      phil 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
    375  1.63.2.1      phil 			    eh->ether_dhost, "mcast", "%s", "on DWDS");
    376  1.63.2.1      phil 			vap->iv_stats.is_dwds_mcast++;
    377  1.63.2.1      phil 			m_freem(m);
    378  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    379  1.63.2.1      phil 			/* XXX better status? */
    380  1.63.2.1      phil 			return (ENOBUFS);
    381  1.63.2.1      phil 		}
    382  1.63.2.1      phil 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
    383  1.63.2.1      phil 			/*
    384  1.63.2.1      phil 			 * Spam DWDS vap's w/ multicast traffic.
    385  1.63.2.1      phil 			 */
    386  1.63.2.1      phil 			/* XXX only if dwds in use? */
    387  1.63.2.1      phil 			ieee80211_dwds_mcast(vap, m);
    388  1.63.2.1      phil 		}
    389  1.63.2.1      phil 	}
    390  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
    391  1.63.2.1      phil 	if (vap->iv_opmode != IEEE80211_M_MBSS) {
    392  1.63.2.1      phil #endif
    393  1.63.2.1      phil 		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
    394  1.63.2.1      phil 		if (ni == NULL) {
    395  1.63.2.1      phil 			/* NB: ieee80211_find_txnode does stat+msg */
    396  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    397  1.63.2.1      phil 			m_freem(m);
    398  1.63.2.1      phil 			/* XXX better status? */
    399  1.63.2.1      phil 			return (ENOBUFS);
    400  1.63.2.1      phil 		}
    401  1.63.2.1      phil 		if (ni->ni_associd == 0 &&
    402  1.63.2.1      phil 		    (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
    403  1.63.2.1      phil 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
    404  1.63.2.1      phil 			    eh->ether_dhost, NULL,
    405  1.63.2.1      phil 			    "sta not associated (type 0x%04x)",
    406  1.63.2.1      phil 			    htons(eh->ether_type));
    407  1.63.2.1      phil 			vap->iv_stats.is_tx_notassoc++;
    408  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    409  1.63.2.1      phil 			m_freem(m);
    410  1.63.2.1      phil 			ieee80211_free_node(ni);
    411  1.63.2.1      phil 			/* XXX better status? */
    412  1.63.2.1      phil 			return (ENOBUFS);
    413  1.63.2.1      phil 		}
    414  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
    415  1.63.2.1      phil 	} else {
    416  1.63.2.1      phil 		if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
    417  1.63.2.1      phil 			/*
    418  1.63.2.1      phil 			 * Proxy station only if configured.
    419  1.63.2.1      phil 			 */
    420  1.63.2.1      phil 			if (!ieee80211_mesh_isproxyena(vap)) {
    421  1.63.2.1      phil 				IEEE80211_DISCARD_MAC(vap,
    422  1.63.2.1      phil 				    IEEE80211_MSG_OUTPUT |
    423  1.63.2.1      phil 				    IEEE80211_MSG_MESH,
    424  1.63.2.1      phil 				    eh->ether_dhost, NULL,
    425  1.63.2.1      phil 				    "%s", "proxy not enabled");
    426  1.63.2.1      phil 				vap->iv_stats.is_mesh_notproxy++;
    427  1.63.2.1      phil 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    428  1.63.2.1      phil 				m_freem(m);
    429  1.63.2.1      phil 				/* XXX better status? */
    430  1.63.2.1      phil 				return (ENOBUFS);
    431  1.63.2.1      phil 			}
    432  1.63.2.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
    433  1.63.2.1      phil 			    "forward frame from DS SA(%6D), DA(%6D)\n",
    434  1.63.2.1      phil 			    eh->ether_shost, ":",
    435  1.63.2.1      phil 			    eh->ether_dhost, ":");
    436  1.63.2.1      phil 			ieee80211_mesh_proxy_check(vap, eh->ether_shost);
    437  1.63.2.1      phil 		}
    438  1.63.2.1      phil 		ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
    439  1.63.2.1      phil 		if (ni == NULL) {
    440  1.63.2.1      phil 			/*
    441  1.63.2.1      phil 			 * NB: ieee80211_mesh_discover holds/disposes
    442  1.63.2.1      phil 			 * frame (e.g. queueing on path discovery).
    443  1.63.2.1      phil 			 */
    444  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    445  1.63.2.1      phil 			/* XXX better status? */
    446  1.63.2.1      phil 			return (ENOBUFS);
    447  1.63.2.1      phil 		}
    448  1.63.2.1      phil 	}
    449  1.63.2.1      phil #endif
    450  1.63.2.1      phil 
    451  1.63.2.1      phil 	/*
    452  1.63.2.1      phil 	 * We've resolved the sender, so attempt to transmit it.
    453  1.63.2.1      phil 	 */
    454  1.63.2.1      phil 
    455  1.63.2.1      phil 	if (vap->iv_state == IEEE80211_S_SLEEP) {
    456  1.63.2.1      phil 		/*
    457  1.63.2.1      phil 		 * In power save; queue frame and then  wakeup device
    458  1.63.2.1      phil 		 * for transmit.
    459  1.63.2.1      phil 		 */
    460  1.63.2.1      phil 		ic->ic_lastdata = ticks;
    461  1.63.2.1      phil 		if (ieee80211_pwrsave(ni, m) != 0)
    462  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    463  1.63.2.1      phil 		ieee80211_free_node(ni);
    464  1.63.2.1      phil 		ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
    465  1.63.2.1      phil 		return (0);
    466  1.63.2.1      phil 	}
    467  1.63.2.1      phil 
    468  1.63.2.1      phil 	if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
    469  1.63.2.1      phil 		return (ENOBUFS);
    470  1.63.2.1      phil 	return (0);
    471  1.63.2.1      phil #undef	IS_DWDS
    472  1.63.2.1      phil }
    473  1.63.2.1      phil 
    474  1.63.2.1      phil /*
    475  1.63.2.1      phil  * Start method for vap's.  All packets from the stack come
    476  1.63.2.1      phil  * through here.  We handle common processing of the packets
    477  1.63.2.1      phil  * before dispatching them to the underlying device.
    478  1.63.2.1      phil  *
    479  1.63.2.1      phil  * if_transmit() requires that the mbuf be consumed by this call
    480  1.63.2.1      phil  * regardless of the return condition.
    481  1.63.2.1      phil  */
    482  1.63.2.1      phil int
    483  1.63.2.1      phil ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
    484  1.63.2.1      phil {
    485  1.63.2.1      phil 	struct ieee80211vap *vap = ifp->if_softc;
    486  1.63.2.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    487  1.63.2.1      phil 
    488  1.63.2.1      phil 	/*
    489  1.63.2.1      phil 	 * No data frames go out unless we're running.
    490  1.63.2.1      phil 	 * Note in particular this covers CAC and CSA
    491  1.63.2.1      phil 	 * states (though maybe we should check muting
    492  1.63.2.1      phil 	 * for CSA).
    493  1.63.2.1      phil 	 */
    494  1.63.2.1      phil 	if (vap->iv_state != IEEE80211_S_RUN &&
    495  1.63.2.1      phil 	    vap->iv_state != IEEE80211_S_SLEEP) {
    496  1.63.2.1      phil 		IEEE80211_LOCK(ic);
    497  1.63.2.1      phil 		/* re-check under the com lock to avoid races */
    498  1.63.2.1      phil 		if (vap->iv_state != IEEE80211_S_RUN &&
    499  1.63.2.1      phil 		    vap->iv_state != IEEE80211_S_SLEEP) {
    500  1.63.2.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
    501  1.63.2.1      phil 			    "%s: ignore queue, in %s state\n",
    502  1.63.2.1      phil 			    __func__, ieee80211_state_name[vap->iv_state]);
    503  1.63.2.1      phil 			vap->iv_stats.is_tx_badstate++;
    504  1.63.2.1      phil 			IEEE80211_UNLOCK(ic);
    505  1.63.2.2      phil #if __FreeBSD__
    506  1.63.2.1      phil 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
    507  1.63.2.2      phil #elif __NetBSD__
    508  1.63.2.2      phil 			ifp->if_flags |= IFF_OACTIVE;
    509  1.63.2.2      phil #endif
    510  1.63.2.1      phil 			m_freem(m);
    511  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    512  1.63.2.1      phil 			return (ENETDOWN);
    513  1.63.2.1      phil 		}
    514  1.63.2.1      phil 		IEEE80211_UNLOCK(ic);
    515  1.63.2.1      phil 	}
    516  1.63.2.1      phil 
    517  1.63.2.1      phil 	/*
    518  1.63.2.1      phil 	 * Sanitize mbuf flags for net80211 use.  We cannot
    519  1.63.2.1      phil 	 * clear M_PWR_SAV or M_MORE_DATA because these may
    520  1.63.2.1      phil 	 * be set for frames that are re-submitted from the
    521  1.63.2.1      phil 	 * power save queue.
    522  1.63.2.1      phil 	 *
    523  1.63.2.1      phil 	 * NB: This must be done before ieee80211_classify as
    524  1.63.2.1      phil 	 *     it marks EAPOL in frames with M_EAPOL.
    525  1.63.2.1      phil 	 */
    526  1.63.2.1      phil 	m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
    527  1.63.2.1      phil 
    528  1.63.2.1      phil 	/*
    529  1.63.2.1      phil 	 * Bump to the packet transmission path.
    530  1.63.2.1      phil 	 * The mbuf will be consumed here.
    531  1.63.2.1      phil 	 */
    532  1.63.2.1      phil 	return (ieee80211_start_pkt(vap, m));
    533  1.63.2.1      phil }
    534  1.63.2.1      phil 
    535  1.63.2.1      phil void
    536  1.63.2.1      phil ieee80211_vap_qflush(struct ifnet *ifp)
    537  1.63.2.1      phil {
    538  1.63.2.1      phil 
    539  1.63.2.1      phil 	/* Empty for now */
    540  1.63.2.1      phil }
    541  1.63.2.1      phil 
    542  1.63.2.1      phil /*
    543  1.63.2.1      phil  * 802.11 raw output routine.
    544  1.63.2.1      phil  *
    545  1.63.2.1      phil  * XXX TODO: this (and other send routines) should correctly
    546  1.63.2.1      phil  * XXX keep the pwr mgmt bit set if it decides to call into the
    547  1.63.2.1      phil  * XXX driver to send a frame whilst the state is SLEEP.
    548  1.63.2.1      phil  *
    549  1.63.2.1      phil  * Otherwise the peer may decide that we're awake and flood us
    550  1.63.2.1      phil  * with traffic we are still too asleep to receive!
    551  1.63.2.1      phil  */
    552  1.63.2.1      phil int
    553  1.63.2.1      phil ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
    554  1.63.2.1      phil     struct mbuf *m, const struct ieee80211_bpf_params *params)
    555  1.63.2.1      phil {
    556  1.63.2.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    557  1.63.2.1      phil 	int error;
    558  1.63.2.1      phil 
    559  1.63.2.1      phil 	/*
    560  1.63.2.1      phil 	 * Set node - the caller has taken a reference, so ensure
    561  1.63.2.1      phil 	 * that the mbuf has the same node value that
    562  1.63.2.1      phil 	 * it would if it were going via the normal path.
    563  1.63.2.1      phil 	 */
    564  1.63.2.2      phil #if __FreeBSD__
    565  1.63.2.1      phil 	m->m_pkthdr.rcvif = (void *)ni;
    566  1.63.2.2      phil #elif __NetBSD__
    567  1.63.2.2      phil 	m_set_rcvif(m, (void*)ni);
    568  1.63.2.2      phil #endif
    569  1.63.2.1      phil 
    570  1.63.2.1      phil 	/*
    571  1.63.2.1      phil 	 * Attempt to add bpf transmit parameters.
    572  1.63.2.1      phil 	 *
    573  1.63.2.1      phil 	 * For now it's ok to fail; the raw_xmit api still takes
    574  1.63.2.1      phil 	 * them as an option.
    575  1.63.2.1      phil 	 *
    576  1.63.2.1      phil 	 * Later on when ic_raw_xmit() has params removed,
    577  1.63.2.1      phil 	 * they'll have to be added - so fail the transmit if
    578  1.63.2.1      phil 	 * they can't be.
    579  1.63.2.1      phil 	 */
    580  1.63.2.1      phil 	if (params)
    581  1.63.2.1      phil 		(void) ieee80211_add_xmit_params(m, params);
    582  1.63.2.1      phil 
    583  1.63.2.1      phil 	error = ic->ic_raw_xmit(ni, m, params);
    584  1.63.2.1      phil 	if (error) {
    585  1.63.2.1      phil 		if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
    586  1.63.2.1      phil 		ieee80211_free_node(ni);
    587  1.63.2.1      phil 	}
    588  1.63.2.1      phil 	return (error);
    589  1.63.2.1      phil }
    590  1.63.2.1      phil 
    591  1.63.2.1      phil static int
    592  1.63.2.1      phil ieee80211_validate_frame(struct mbuf *m,
    593  1.63.2.1      phil     const struct ieee80211_bpf_params *params)
    594  1.63.2.1      phil {
    595  1.63.2.1      phil 	struct ieee80211_frame *wh;
    596  1.63.2.1      phil 	int type;
    597  1.63.2.1      phil 
    598  1.63.2.1      phil 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
    599  1.63.2.1      phil 		return (EINVAL);
    600  1.63.2.1      phil 
    601  1.63.2.1      phil 	wh = mtod(m, struct ieee80211_frame *);
    602  1.63.2.1      phil 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
    603  1.63.2.1      phil 	    IEEE80211_FC0_VERSION_0)
    604  1.63.2.1      phil 		return (EINVAL);
    605  1.63.2.1      phil 
    606  1.63.2.1      phil 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
    607  1.63.2.1      phil 	if (type != IEEE80211_FC0_TYPE_DATA) {
    608  1.63.2.1      phil 		if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
    609  1.63.2.1      phil 		    IEEE80211_FC1_DIR_NODS)
    610  1.63.2.1      phil 			return (EINVAL);
    611  1.63.2.1      phil 
    612  1.63.2.1      phil 		if (type != IEEE80211_FC0_TYPE_MGT &&
    613  1.63.2.1      phil 		    (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) != 0)
    614  1.63.2.1      phil 			return (EINVAL);
    615  1.63.2.1      phil 
    616  1.63.2.1      phil 		/* XXX skip other field checks? */
    617  1.63.2.1      phil 	}
    618  1.63.2.1      phil 
    619  1.63.2.1      phil 	if ((params && (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0) ||
    620  1.63.2.1      phil 	    (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) != 0) {
    621  1.63.2.1      phil 		int subtype;
    622  1.63.2.1      phil 
    623  1.63.2.1      phil 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
    624  1.63.2.1      phil 
    625  1.63.2.1      phil 		/*
    626  1.63.2.1      phil 		 * See IEEE Std 802.11-2012,
    627  1.63.2.1      phil 		 * 8.2.4.1.9 'Protected Frame field'
    628  1.63.2.1      phil 		 */
    629  1.63.2.1      phil 		/* XXX no support for robust management frames yet. */
    630  1.63.2.1      phil 		if (!(type == IEEE80211_FC0_TYPE_DATA ||
    631  1.63.2.1      phil 		    (type == IEEE80211_FC0_TYPE_MGT &&
    632  1.63.2.1      phil 		     subtype == IEEE80211_FC0_SUBTYPE_AUTH)))
    633  1.63.2.1      phil 			return (EINVAL);
    634  1.63.2.1      phil 
    635  1.63.2.1      phil 		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
    636  1.63.2.1      phil 	}
    637  1.63.2.1      phil 
    638  1.63.2.1      phil 	if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh))
    639  1.63.2.1      phil 		return (EINVAL);
    640  1.63.2.1      phil 
    641  1.63.2.1      phil 	return (0);
    642  1.63.2.1      phil }
    643  1.63.2.1      phil 
    644  1.63.2.1      phil /*
    645  1.63.2.1      phil  * 802.11 output routine. This is (currently) used only to
    646  1.63.2.1      phil  * connect bpf write calls to the 802.11 layer for injecting
    647  1.63.2.1      phil  * raw 802.11 frames.
    648  1.63.2.1      phil  */
    649  1.63.2.3      phil #if __FreeBSD__
    650  1.63.2.1      phil int
    651  1.63.2.1      phil ieee80211_output(struct ifnet *ifp, struct mbuf *m,
    652  1.63.2.1      phil 	const struct sockaddr *dst, struct route *ro)
    653  1.63.2.3      phil #elif __NetBSD__
    654  1.63.2.3      phil int
    655  1.63.2.3      phil ieee80211_output(struct ifnet *ifp, struct mbuf *m,
    656  1.63.2.3      phil 	const struct sockaddr *dst, const struct rtentry *ro)
    657  1.63.2.3      phil #endif
    658  1.63.2.1      phil {
    659  1.63.2.1      phil #define senderr(e) do { error = (e); goto bad;} while (0)
    660  1.63.2.1      phil 	const struct ieee80211_bpf_params *params = NULL;
    661  1.63.2.1      phil 	struct ieee80211_node *ni = NULL;
    662  1.63.2.1      phil 	struct ieee80211vap *vap;
    663  1.63.2.1      phil 	struct ieee80211_frame *wh;
    664  1.63.2.1      phil 	struct ieee80211com *ic = NULL;
    665  1.63.2.1      phil 	int error;
    666  1.63.2.1      phil 	int ret;
    667  1.63.2.1      phil 
    668  1.63.2.2      phil #if __FreeBSD__
    669  1.63.2.1      phil 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
    670  1.63.2.2      phil #elif __NetBSD__
    671  1.63.2.2      phil 	if (ifp->if_flags & IFF_OACTIVE) {
    672  1.63.2.2      phil #endif
    673  1.63.2.1      phil 		/*
    674  1.63.2.1      phil 		 * Short-circuit requests if the vap is marked OACTIVE
    675  1.63.2.1      phil 		 * as this can happen because a packet came down through
    676  1.63.2.1      phil 		 * ieee80211_start before the vap entered RUN state in
    677  1.63.2.1      phil 		 * which case it's ok to just drop the frame.  This
    678  1.63.2.1      phil 		 * should not be necessary but callers of if_output don't
    679  1.63.2.1      phil 		 * check OACTIVE.
    680  1.63.2.1      phil 		 */
    681  1.63.2.1      phil 		senderr(ENETDOWN);
    682  1.63.2.1      phil 	}
    683  1.63.2.1      phil 	vap = ifp->if_softc;
    684  1.63.2.1      phil 	ic = vap->iv_ic;
    685  1.63.2.1      phil 	/*
    686  1.63.2.1      phil 	 * Hand to the 802.3 code if not tagged as
    687  1.63.2.1      phil 	 * a raw 802.11 frame.
    688  1.63.2.1      phil 	 */
    689  1.63.2.1      phil 	if (dst->sa_family != AF_IEEE80211)
    690  1.63.2.1      phil 		return vap->iv_output(ifp, m, dst, ro);
    691  1.63.2.1      phil #ifdef MAC
    692  1.63.2.1      phil 	error = mac_ifnet_check_transmit(ifp, m);
    693  1.63.2.1      phil 	if (error)
    694  1.63.2.1      phil 		senderr(error);
    695  1.63.2.1      phil #endif
    696  1.63.2.2      phil #if __FreeBSD__
    697  1.63.2.1      phil 	if (ifp->if_flags & IFF_MONITOR)
    698  1.63.2.1      phil 		senderr(ENETDOWN);
    699  1.63.2.2      phil #endif
    700  1.63.2.1      phil 	if (!IFNET_IS_UP_RUNNING(ifp))
    701  1.63.2.1      phil 		senderr(ENETDOWN);
    702  1.63.2.1      phil 	if (vap->iv_state == IEEE80211_S_CAC) {
    703  1.63.2.1      phil 		IEEE80211_DPRINTF(vap,
    704  1.63.2.1      phil 		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
    705  1.63.2.1      phil 		    "block %s frame in CAC state\n", "raw data");
    706  1.63.2.1      phil 		vap->iv_stats.is_tx_badstate++;
    707  1.63.2.1      phil 		senderr(EIO);		/* XXX */
    708  1.63.2.1      phil 	} else if (vap->iv_state == IEEE80211_S_SCAN)
    709  1.63.2.1      phil 		senderr(EIO);
    710  1.63.2.1      phil 	/* XXX bypass bridge, pfil, carp, etc. */
    711  1.63.2.1      phil 
    712  1.63.2.1      phil 	/*
    713  1.63.2.1      phil 	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
    714  1.63.2.1      phil 	 * present by setting the sa_len field of the sockaddr (yes,
    715  1.63.2.1      phil 	 * this is a hack).
    716  1.63.2.1      phil 	 * NB: we assume sa_data is suitably aligned to cast.
    717  1.63.2.1      phil 	 */
    718  1.63.2.1      phil 	if (dst->sa_len != 0)
    719  1.63.2.1      phil 		params = (const struct ieee80211_bpf_params *)dst->sa_data;
    720  1.63.2.1      phil 
    721  1.63.2.1      phil 	error = ieee80211_validate_frame(m, params);
    722  1.63.2.1      phil 	if (error != 0)
    723  1.63.2.1      phil 		senderr(error);
    724  1.63.2.1      phil 
    725  1.63.2.1      phil 	wh = mtod(m, struct ieee80211_frame *);
    726  1.63.2.1      phil 
    727  1.63.2.1      phil 	/* locate destination node */
    728  1.63.2.1      phil 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
    729  1.63.2.1      phil 	case IEEE80211_FC1_DIR_NODS:
    730  1.63.2.1      phil 	case IEEE80211_FC1_DIR_FROMDS:
    731  1.63.2.1      phil 		ni = ieee80211_find_txnode(vap, wh->i_addr1);
    732  1.63.2.1      phil 		break;
    733  1.63.2.1      phil 	case IEEE80211_FC1_DIR_TODS:
    734  1.63.2.1      phil 	case IEEE80211_FC1_DIR_DSTODS:
    735  1.63.2.1      phil 		ni = ieee80211_find_txnode(vap, wh->i_addr3);
    736  1.63.2.1      phil 		break;
    737  1.63.2.1      phil 	default:
    738  1.63.2.1      phil 		senderr(EDOOFUS);
    739  1.63.2.1      phil 	}
    740  1.63.2.1      phil 	if (ni == NULL) {
    741  1.63.2.1      phil 		/*
    742  1.63.2.1      phil 		 * Permit packets w/ bpf params through regardless
    743  1.63.2.1      phil 		 * (see below about sa_len).
    744  1.63.2.1      phil 		 */
    745  1.63.2.1      phil 		if (dst->sa_len == 0)
    746  1.63.2.1      phil 			senderr(EHOSTUNREACH);
    747  1.63.2.1      phil 		ni = ieee80211_ref_node(vap->iv_bss);
    748  1.63.2.1      phil 	}
    749  1.63.2.1      phil 
    750  1.63.2.1      phil 	/*
    751  1.63.2.1      phil 	 * Sanitize mbuf for net80211 flags leaked from above.
    752  1.63.2.1      phil 	 *
    753  1.63.2.1      phil 	 * NB: This must be done before ieee80211_classify as
    754  1.63.2.1      phil 	 *     it marks EAPOL in frames with M_EAPOL.
    755  1.63.2.1      phil 	 */
    756  1.63.2.1      phil 	m->m_flags &= ~M_80211_TX;
    757  1.63.2.1      phil 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
    758  1.63.2.1      phil 
    759  1.63.2.1      phil 	if (IEEE80211_IS_DATA(wh)) {
    760  1.63.2.1      phil 		/* calculate priority so drivers can find the tx queue */
    761  1.63.2.1      phil 		if (ieee80211_classify(ni, m))
    762  1.63.2.1      phil 			senderr(EIO);		/* XXX */
    763  1.63.2.1      phil 
    764  1.63.2.1      phil 		/* NB: ieee80211_encap does not include 802.11 header */
    765  1.63.2.1      phil 		IEEE80211_NODE_STAT_ADD(ni, tx_bytes,
    766  1.63.2.1      phil 		    m->m_pkthdr.len - ieee80211_hdrsize(wh));
    767  1.63.2.1      phil 	} else
    768  1.63.2.1      phil 		M_WME_SETAC(m, WME_AC_BE);
    769  1.63.2.1      phil 
    770  1.63.2.1      phil 	IEEE80211_NODE_STAT(ni, tx_data);
    771  1.63.2.1      phil 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
    772  1.63.2.1      phil 		IEEE80211_NODE_STAT(ni, tx_mcast);
    773  1.63.2.1      phil 		m->m_flags |= M_MCAST;
    774  1.63.2.1      phil 	} else
    775  1.63.2.1      phil 		IEEE80211_NODE_STAT(ni, tx_ucast);
    776  1.63.2.1      phil 
    777  1.63.2.1      phil 	IEEE80211_TX_LOCK(ic);
    778  1.63.2.1      phil 	ret = ieee80211_raw_output(vap, ni, m, params);
    779  1.63.2.1      phil 	IEEE80211_TX_UNLOCK(ic);
    780  1.63.2.1      phil 	return (ret);
    781  1.63.2.1      phil bad:
    782  1.63.2.1      phil 	if (m != NULL)
    783  1.63.2.1      phil 		m_freem(m);
    784  1.63.2.1      phil 	if (ni != NULL)
    785  1.63.2.1      phil 		ieee80211_free_node(ni);
    786  1.63.2.1      phil 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
    787  1.63.2.1      phil 	return error;
    788  1.63.2.1      phil #undef senderr
    789  1.63.2.1      phil }
    790  1.63.2.1      phil 
    791  1.63.2.1      phil /*
    792      1.39     skrll  * Set the direction field and address fields of an outgoing
    793  1.63.2.1      phil  * frame.  Note this should be called early on in constructing
    794  1.63.2.1      phil  * a frame as it sets i_fc[1]; other bits can then be or'd in.
    795      1.39     skrll  */
    796  1.63.2.1      phil void
    797  1.63.2.1      phil ieee80211_send_setup(
    798      1.39     skrll 	struct ieee80211_node *ni,
    799  1.63.2.1      phil 	struct mbuf *m,
    800  1.63.2.1      phil 	int type, int tid,
    801  1.63.2.1      phil 	const uint8_t sa[IEEE80211_ADDR_LEN],
    802  1.63.2.1      phil 	const uint8_t da[IEEE80211_ADDR_LEN],
    803  1.63.2.1      phil 	const uint8_t bssid[IEEE80211_ADDR_LEN])
    804      1.39     skrll {
    805      1.39     skrll #define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
    806  1.63.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    807  1.63.2.1      phil 	struct ieee80211_tx_ampdu *tap;
    808  1.63.2.1      phil 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
    809  1.63.2.1      phil 	ieee80211_seq seqno;
    810      1.39     skrll 
    811  1.63.2.1      phil 	IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
    812      1.60      maxv 
    813  1.63.2.1      phil 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
    814      1.39     skrll 	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
    815  1.63.2.1      phil 		switch (vap->iv_opmode) {
    816      1.39     skrll 		case IEEE80211_M_STA:
    817      1.39     skrll 			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
    818      1.39     skrll 			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
    819      1.39     skrll 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
    820      1.39     skrll 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
    821      1.39     skrll 			break;
    822      1.39     skrll 		case IEEE80211_M_IBSS:
    823      1.39     skrll 		case IEEE80211_M_AHDEMO:
    824      1.39     skrll 			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    825      1.39     skrll 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
    826      1.39     skrll 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
    827      1.39     skrll 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
    828      1.39     skrll 			break;
    829      1.39     skrll 		case IEEE80211_M_HOSTAP:
    830      1.39     skrll 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
    831      1.39     skrll 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
    832      1.39     skrll 			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
    833      1.39     skrll 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
    834      1.39     skrll 			break;
    835  1.63.2.1      phil 		case IEEE80211_M_WDS:
    836  1.63.2.1      phil 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
    837  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
    838  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
    839  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
    840  1.63.2.1      phil 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
    841  1.63.2.1      phil 			break;
    842  1.63.2.1      phil 		case IEEE80211_M_MBSS:
    843  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
    844  1.63.2.1      phil 			if (IEEE80211_IS_MULTICAST(da)) {
    845  1.63.2.1      phil 				wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
    846  1.63.2.1      phil 				/* XXX next hop */
    847  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr1, da);
    848  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr2,
    849  1.63.2.1      phil 				    vap->iv_myaddr);
    850  1.63.2.1      phil 			} else {
    851  1.63.2.1      phil 				wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
    852  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr1, da);
    853  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr2,
    854  1.63.2.1      phil 				    vap->iv_myaddr);
    855  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr3, da);
    856  1.63.2.1      phil 				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
    857  1.63.2.1      phil 			}
    858  1.63.2.1      phil #endif
    859  1.63.2.1      phil 			break;
    860      1.39     skrll 		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
    861      1.39     skrll 			break;
    862      1.39     skrll 		}
    863      1.39     skrll 	} else {
    864      1.39     skrll 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    865      1.39     skrll 		IEEE80211_ADDR_COPY(wh->i_addr1, da);
    866      1.39     skrll 		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
    867  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
    868  1.63.2.1      phil 		if (vap->iv_opmode == IEEE80211_M_MBSS)
    869  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
    870  1.63.2.1      phil 		else
    871  1.63.2.1      phil #endif
    872  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
    873  1.63.2.1      phil 	}
    874  1.63.2.1      phil 	*(uint16_t *)&wh->i_dur[0] = 0;
    875  1.63.2.1      phil 
    876  1.63.2.1      phil 	/*
    877  1.63.2.1      phil 	 * XXX TODO: this is what the TX lock is for.
    878  1.63.2.1      phil 	 * Here we're incrementing sequence numbers, and they
    879  1.63.2.1      phil 	 * need to be in lock-step with what the driver is doing
    880  1.63.2.1      phil 	 * both in TX ordering and crypto encap (IV increment.)
    881  1.63.2.1      phil 	 *
    882  1.63.2.1      phil 	 * If the driver does seqno itself, then we can skip
    883  1.63.2.1      phil 	 * assigning sequence numbers here, and we can avoid
    884  1.63.2.1      phil 	 * requiring the TX lock.
    885  1.63.2.1      phil 	 */
    886  1.63.2.1      phil 	tap = &ni->ni_tx_ampdu[tid];
    887  1.63.2.1      phil 	if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap)) {
    888  1.63.2.1      phil 		m->m_flags |= M_AMPDU_MPDU;
    889  1.63.2.1      phil 
    890  1.63.2.1      phil 		/* NB: zero out i_seq field (for s/w encryption etc) */
    891  1.63.2.1      phil 		*(uint16_t *)&wh->i_seq[0] = 0;
    892  1.63.2.1      phil 	} else {
    893  1.63.2.1      phil 		if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
    894  1.63.2.1      phil 				      type & IEEE80211_FC0_SUBTYPE_MASK))
    895  1.63.2.1      phil 			/*
    896  1.63.2.1      phil 			 * 802.11-2012 9.3.2.10 - QoS multicast frames
    897  1.63.2.1      phil 			 * come out of a different seqno space.
    898  1.63.2.1      phil 			 */
    899  1.63.2.1      phil 			if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
    900  1.63.2.1      phil 				seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
    901  1.63.2.1      phil 			} else {
    902  1.63.2.1      phil 				seqno = ni->ni_txseqs[tid]++;
    903  1.63.2.1      phil 			}
    904  1.63.2.1      phil 		else
    905  1.63.2.1      phil 			seqno = 0;
    906  1.63.2.1      phil 
    907  1.63.2.1      phil 		*(uint16_t *)&wh->i_seq[0] =
    908  1.63.2.1      phil 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
    909  1.63.2.1      phil 		M_SEQNO_SET(m, seqno);
    910      1.39     skrll 	}
    911      1.60      maxv 
    912  1.63.2.1      phil 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
    913  1.63.2.1      phil 		m->m_flags |= M_MCAST;
    914      1.39     skrll #undef WH4
    915      1.39     skrll }
    916      1.39     skrll 
    917      1.39     skrll /*
    918       1.1    dyoung  * Send a management frame to the specified node.  The node pointer
    919       1.1    dyoung  * must have a reference as the pointer will be passed to the driver
    920       1.1    dyoung  * and potentially held for a long time.  If the frame is successfully
    921       1.1    dyoung  * dispatched to the driver, then it is responsible for freeing the
    922  1.63.2.1      phil  * reference (and potentially free'ing up any associated storage);
    923  1.63.2.1      phil  * otherwise deal with reclaiming any reference (on error).
    924       1.1    dyoung  */
    925  1.63.2.1      phil int
    926  1.63.2.1      phil ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
    927  1.63.2.1      phil 	struct ieee80211_bpf_params *params)
    928       1.1    dyoung {
    929  1.63.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    930  1.63.2.1      phil 	struct ieee80211com *ic = ni->ni_ic;
    931       1.1    dyoung 	struct ieee80211_frame *wh;
    932  1.63.2.1      phil 	int ret;
    933       1.1    dyoung 
    934  1.63.2.1      phil 	KASSERT(ni != NULL, ("null node"));
    935       1.1    dyoung 
    936  1.63.2.1      phil 	if (vap->iv_state == IEEE80211_S_CAC) {
    937  1.63.2.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
    938  1.63.2.1      phil 		    ni, "block %s frame in CAC state",
    939  1.63.2.1      phil 			ieee80211_mgt_subtype_name(type));
    940  1.63.2.1      phil 		vap->iv_stats.is_tx_badstate++;
    941  1.63.2.1      phil 		ieee80211_free_node(ni);
    942  1.63.2.1      phil 		m_freem(m);
    943  1.63.2.1      phil 		return EIO;		/* XXX */
    944  1.63.2.1      phil 	}
    945  1.63.2.1      phil 
    946  1.63.2.1      phil 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
    947  1.63.2.1      phil 	if (m == NULL) {
    948  1.63.2.1      phil 		ieee80211_free_node(ni);
    949       1.1    dyoung 		return ENOMEM;
    950  1.63.2.1      phil 	}
    951       1.1    dyoung 
    952  1.63.2.1      phil 	IEEE80211_TX_LOCK(ic);
    953      1.60      maxv 
    954  1.63.2.1      phil 	wh = mtod(m, struct ieee80211_frame *);
    955  1.63.2.1      phil 	ieee80211_send_setup(ni, m,
    956  1.63.2.1      phil 	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
    957  1.63.2.1      phil 	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
    958  1.63.2.1      phil 	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
    959  1.63.2.1      phil 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
    960  1.63.2.1      phil 		    "encrypting frame (%s)", __func__);
    961  1.63.2.1      phil 		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
    962       1.7    dyoung 	}
    963  1.63.2.1      phil 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
    964  1.63.2.1      phil 
    965  1.63.2.1      phil 	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
    966  1.63.2.1      phil 	M_WME_SETAC(m, params->ibp_pri);
    967      1.60      maxv 
    968       1.1    dyoung #ifdef IEEE80211_DEBUG
    969      1.15   mycroft 	/* avoid printing too many frames */
    970  1.63.2.1      phil 	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
    971  1.63.2.1      phil 	    ieee80211_msg_dumppkts(vap)) {
    972      1.29    dyoung 		printf("[%s] send %s on channel %u\n",
    973      1.29    dyoung 		    ether_sprintf(wh->i_addr1),
    974  1.63.2.1      phil 		    ieee80211_mgt_subtype_name(type),
    975      1.39     skrll 		    ieee80211_chan2ieee(ic, ic->ic_curchan));
    976      1.15   mycroft 	}
    977       1.1    dyoung #endif
    978      1.29    dyoung 	IEEE80211_NODE_STAT(ni, tx_mgmt);
    979  1.63.2.1      phil 
    980  1.63.2.1      phil 	ret = ieee80211_raw_output(vap, ni, m, params);
    981  1.63.2.1      phil 	IEEE80211_TX_UNLOCK(ic);
    982  1.63.2.1      phil 	return (ret);
    983  1.63.2.1      phil }
    984  1.63.2.1      phil 
    985  1.63.2.1      phil static void
    986  1.63.2.1      phil ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
    987  1.63.2.1      phil     int status)
    988  1.63.2.1      phil {
    989  1.63.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    990  1.63.2.1      phil 
    991  1.63.2.1      phil 	wakeup(vap);
    992       1.1    dyoung }
    993       1.1    dyoung 
    994       1.1    dyoung /*
    995  1.63.2.1      phil  * Send a null data frame to the specified node.  If the station
    996  1.63.2.1      phil  * is setup for QoS then a QoS Null Data frame is constructed.
    997  1.63.2.1      phil  * If this is a WDS station then a 4-address frame is constructed.
    998      1.39     skrll  *
    999      1.39     skrll  * NB: the caller is assumed to have setup a node reference
   1000      1.39     skrll  *     for use; this is necessary to deal with a race condition
   1001  1.63.2.1      phil  *     when probing for inactive stations.  Like ieee80211_mgmt_output
   1002  1.63.2.1      phil  *     we must cleanup any node reference on error;  however we
   1003  1.63.2.1      phil  *     can safely just unref it as we know it will never be the
   1004  1.63.2.1      phil  *     last reference to the node.
   1005      1.29    dyoung  */
   1006      1.29    dyoung int
   1007      1.39     skrll ieee80211_send_nulldata(struct ieee80211_node *ni)
   1008      1.29    dyoung {
   1009  1.63.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   1010      1.39     skrll 	struct ieee80211com *ic = ni->ni_ic;
   1011      1.29    dyoung 	struct mbuf *m;
   1012      1.29    dyoung 	struct ieee80211_frame *wh;
   1013  1.63.2.1      phil 	int hdrlen;
   1014  1.63.2.1      phil 	uint8_t *frm;
   1015  1.63.2.1      phil 	int ret;
   1016  1.63.2.1      phil 
   1017  1.63.2.1      phil 	if (vap->iv_state == IEEE80211_S_CAC) {
   1018  1.63.2.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
   1019  1.63.2.1      phil 		    ni, "block %s frame in CAC state", "null data");
   1020  1.63.2.1      phil 		ieee80211_unref_node(&ni);
   1021  1.63.2.1      phil 		vap->iv_stats.is_tx_badstate++;
   1022  1.63.2.1      phil 		return EIO;		/* XXX */
   1023  1.63.2.1      phil 	}
   1024  1.63.2.1      phil 
   1025  1.63.2.1      phil 	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
   1026  1.63.2.1      phil 		hdrlen = sizeof(struct ieee80211_qosframe);
   1027  1.63.2.1      phil 	else
   1028  1.63.2.1      phil 		hdrlen = sizeof(struct ieee80211_frame);
   1029  1.63.2.1      phil 	/* NB: only WDS vap's get 4-address frames */
   1030  1.63.2.1      phil 	if (vap->iv_opmode == IEEE80211_M_WDS)
   1031  1.63.2.1      phil 		hdrlen += IEEE80211_ADDR_LEN;
   1032  1.63.2.1      phil 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
   1033  1.63.2.1      phil 		hdrlen = roundup(hdrlen, sizeof(uint32_t));
   1034      1.29    dyoung 
   1035  1.63.2.1      phil 	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
   1036      1.29    dyoung 	if (m == NULL) {
   1037  1.63.2.1      phil 		/* XXX debug msg */
   1038      1.39     skrll 		ieee80211_unref_node(&ni);
   1039  1.63.2.1      phil 		vap->iv_stats.is_tx_nobuf++;
   1040  1.63.2.1      phil 		return ENOMEM;
   1041  1.63.2.1      phil 	}
   1042  1.63.2.1      phil 	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
   1043  1.63.2.1      phil 	    ("leading space %zd", M_LEADINGSPACE(m)));
   1044  1.63.2.1      phil 	M_PREPEND(m, hdrlen, M_NOWAIT);
   1045  1.63.2.1      phil 	if (m == NULL) {
   1046  1.63.2.1      phil 		/* NB: cannot happen */
   1047  1.63.2.1      phil 		ieee80211_free_node(ni);
   1048      1.29    dyoung 		return ENOMEM;
   1049      1.29    dyoung 	}
   1050      1.29    dyoung 
   1051  1.63.2.1      phil 	IEEE80211_TX_LOCK(ic);
   1052      1.60      maxv 
   1053  1.63.2.1      phil 	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
   1054  1.63.2.1      phil 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
   1055  1.63.2.1      phil 		const int tid = WME_AC_TO_TID(WME_AC_BE);
   1056  1.63.2.1      phil 		uint8_t *qos;
   1057  1.63.2.1      phil 
   1058  1.63.2.1      phil 		ieee80211_send_setup(ni, m,
   1059  1.63.2.1      phil 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
   1060  1.63.2.1      phil 		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
   1061      1.60      maxv 
   1062  1.63.2.1      phil 		if (vap->iv_opmode == IEEE80211_M_WDS)
   1063  1.63.2.1      phil 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
   1064  1.63.2.1      phil 		else
   1065  1.63.2.1      phil 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
   1066  1.63.2.1      phil 		qos[0] = tid & IEEE80211_QOS_TID;
   1067  1.63.2.1      phil 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
   1068  1.63.2.1      phil 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
   1069  1.63.2.1      phil 		qos[1] = 0;
   1070  1.63.2.1      phil 	} else {
   1071  1.63.2.1      phil 		ieee80211_send_setup(ni, m,
   1072  1.63.2.1      phil 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
   1073  1.63.2.1      phil 		    IEEE80211_NONQOS_TID,
   1074  1.63.2.1      phil 		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
   1075  1.63.2.1      phil 	}
   1076  1.63.2.1      phil 	if (vap->iv_opmode != IEEE80211_M_WDS) {
   1077  1.63.2.1      phil 		/* NB: power management bit is never sent by an AP */
   1078  1.63.2.1      phil 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
   1079  1.63.2.1      phil 		    vap->iv_opmode != IEEE80211_M_HOSTAP)
   1080  1.63.2.1      phil 			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
   1081  1.63.2.1      phil 	}
   1082  1.63.2.1      phil 	if ((ic->ic_flags & IEEE80211_F_SCAN) &&
   1083  1.63.2.1      phil 	    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
   1084  1.63.2.1      phil 		ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
   1085  1.63.2.1      phil 		    NULL);
   1086      1.60      maxv 	}
   1087  1.63.2.1      phil 	m->m_len = m->m_pkthdr.len = hdrlen;
   1088  1.63.2.1      phil 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
   1089      1.60      maxv 
   1090  1.63.2.1      phil 	M_WME_SETAC(m, WME_AC_BE);
   1091      1.29    dyoung 
   1092      1.29    dyoung 	IEEE80211_NODE_STAT(ni, tx_data);
   1093      1.29    dyoung 
   1094  1.63.2.1      phil 	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
   1095  1.63.2.1      phil 	    "send %snull data frame on channel %u, pwr mgt %s",
   1096  1.63.2.1      phil 	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
   1097      1.39     skrll 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
   1098      1.39     skrll 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
   1099      1.39     skrll 
   1100  1.63.2.1      phil 	ret = ieee80211_raw_output(vap, ni, m, NULL);
   1101  1.63.2.1      phil 	IEEE80211_TX_UNLOCK(ic);
   1102  1.63.2.1      phil 	return (ret);
   1103      1.29    dyoung }
   1104      1.29    dyoung 
   1105      1.29    dyoung /*
   1106      1.29    dyoung  * Assign priority to a frame based on any vlan tag assigned
   1107      1.29    dyoung  * to the station and/or any Diffserv setting in an IP header.
   1108      1.29    dyoung  * Finally, if an ACM policy is setup (in station mode) it's
   1109      1.29    dyoung  * applied.
   1110      1.29    dyoung  */
   1111      1.29    dyoung int
   1112  1.63.2.1      phil ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
   1113      1.29    dyoung {
   1114  1.63.2.1      phil 	const struct ether_header *eh = NULL;
   1115  1.63.2.1      phil 	uint16_t ether_type;
   1116      1.29    dyoung 	int v_wme_ac, d_wme_ac, ac;
   1117      1.29    dyoung 
   1118  1.63.2.1      phil 	if (__predict_false(m->m_flags & M_ENCAP)) {
   1119  1.63.2.1      phil 		struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
   1120  1.63.2.1      phil 		struct llc *llc;
   1121  1.63.2.1      phil 		int hdrlen, subtype;
   1122  1.63.2.1      phil 
   1123  1.63.2.1      phil 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   1124  1.63.2.1      phil 		if (subtype & IEEE80211_FC0_SUBTYPE_NODATA) {
   1125  1.63.2.1      phil 			ac = WME_AC_BE;
   1126  1.63.2.1      phil 			goto done;
   1127  1.63.2.1      phil 		}
   1128  1.63.2.1      phil 
   1129  1.63.2.1      phil 		hdrlen = ieee80211_hdrsize(wh);
   1130  1.63.2.1      phil 		if (m->m_pkthdr.len < hdrlen + sizeof(*llc))
   1131  1.63.2.1      phil 			return 1;
   1132  1.63.2.1      phil 
   1133  1.63.2.1      phil 		llc = (struct llc *)mtodo(m, hdrlen);
   1134  1.63.2.1      phil 		if (llc->llc_dsap != LLC_SNAP_LSAP ||
   1135  1.63.2.1      phil 		    llc->llc_ssap != LLC_SNAP_LSAP ||
   1136  1.63.2.1      phil 		    llc->llc_control != LLC_UI ||
   1137  1.63.2.1      phil 		    llc->llc_snap.org_code[0] != 0 ||
   1138  1.63.2.1      phil 		    llc->llc_snap.org_code[1] != 0 ||
   1139  1.63.2.1      phil 		    llc->llc_snap.org_code[2] != 0)
   1140  1.63.2.1      phil 			return 1;
   1141  1.63.2.1      phil 
   1142  1.63.2.1      phil 		ether_type = llc->llc_snap.ether_type;
   1143  1.63.2.1      phil 	} else {
   1144  1.63.2.1      phil 		eh = mtod(m, struct ether_header *);
   1145  1.63.2.1      phil 		ether_type = eh->ether_type;
   1146  1.63.2.1      phil 	}
   1147  1.63.2.1      phil 
   1148  1.63.2.1      phil 	/*
   1149  1.63.2.1      phil 	 * Always promote PAE/EAPOL frames to high priority.
   1150  1.63.2.1      phil 	 */
   1151  1.63.2.1      phil 	if (ether_type == htons(ETHERTYPE_PAE)) {
   1152  1.63.2.1      phil 		/* NB: mark so others don't need to check header */
   1153  1.63.2.1      phil 		m->m_flags |= M_EAPOL;
   1154  1.63.2.1      phil 		ac = WME_AC_VO;
   1155  1.63.2.1      phil 		goto done;
   1156  1.63.2.1      phil 	}
   1157  1.63.2.1      phil 	/*
   1158  1.63.2.1      phil 	 * Non-qos traffic goes to BE.
   1159  1.63.2.1      phil 	 */
   1160      1.29    dyoung 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
   1161      1.29    dyoung 		ac = WME_AC_BE;
   1162      1.29    dyoung 		goto done;
   1163      1.29    dyoung 	}
   1164      1.29    dyoung 
   1165      1.29    dyoung 	/*
   1166      1.29    dyoung 	 * If node has a vlan tag then all traffic
   1167      1.29    dyoung 	 * to it must have a matching tag.
   1168      1.29    dyoung 	 */
   1169      1.29    dyoung 	v_wme_ac = 0;
   1170      1.29    dyoung 	if (ni->ni_vlan != 0) {
   1171  1.63.2.1      phil 		 if ((m->m_flags & M_VLANTAG) == 0) {
   1172      1.29    dyoung 			IEEE80211_NODE_STAT(ni, tx_novlantag);
   1173      1.29    dyoung 			return 1;
   1174      1.29    dyoung 		}
   1175  1.63.2.1      phil 		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
   1176      1.29    dyoung 		    EVL_VLANOFTAG(ni->ni_vlan)) {
   1177      1.29    dyoung 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
   1178      1.29    dyoung 			return 1;
   1179      1.29    dyoung 		}
   1180      1.29    dyoung 		/* map vlan priority to AC */
   1181  1.63.2.1      phil 		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
   1182      1.29    dyoung 	}
   1183      1.29    dyoung 
   1184  1.63.2.1      phil 	/* XXX m_copydata may be too slow for fast path */
   1185      1.29    dyoung #ifdef INET
   1186  1.63.2.1      phil 	if (eh && eh->ether_type == htons(ETHERTYPE_IP)) {
   1187  1.63.2.1      phil 		uint8_t tos;
   1188  1.63.2.1      phil 		/*
   1189  1.63.2.1      phil 		 * IP frame, map the DSCP bits from the TOS field.
   1190  1.63.2.1      phil 		 */
   1191  1.63.2.1      phil 		/* NB: ip header may not be in first mbuf */
   1192  1.63.2.1      phil 		m_copydata(m, sizeof(struct ether_header) +
   1193  1.63.2.1      phil 		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
   1194  1.63.2.1      phil 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
   1195  1.63.2.1      phil 		d_wme_ac = TID_TO_WME_AC(tos);
   1196      1.29    dyoung 	} else {
   1197      1.29    dyoung #endif /* INET */
   1198  1.63.2.1      phil #ifdef INET6
   1199  1.63.2.1      phil 	if (eh && eh->ether_type == htons(ETHERTYPE_IPV6)) {
   1200  1.63.2.1      phil 		uint32_t flow;
   1201  1.63.2.1      phil 		uint8_t tos;
   1202  1.63.2.1      phil 		/*
   1203  1.63.2.1      phil 		 * IPv6 frame, map the DSCP bits from the traffic class field.
   1204  1.63.2.1      phil 		 */
   1205  1.63.2.1      phil 		m_copydata(m, sizeof(struct ether_header) +
   1206  1.63.2.1      phil 		    offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
   1207  1.63.2.1      phil 		    (caddr_t) &flow);
   1208  1.63.2.1      phil 		tos = (uint8_t)(ntohl(flow) >> 20);
   1209  1.63.2.1      phil 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
   1210  1.63.2.1      phil 		d_wme_ac = TID_TO_WME_AC(tos);
   1211  1.63.2.1      phil 	} else {
   1212  1.63.2.1      phil #endif /* INET6 */
   1213      1.29    dyoung 		d_wme_ac = WME_AC_BE;
   1214  1.63.2.1      phil #ifdef INET6
   1215  1.63.2.1      phil 	}
   1216  1.63.2.1      phil #endif
   1217      1.29    dyoung #ifdef INET
   1218      1.29    dyoung 	}
   1219      1.29    dyoung #endif
   1220      1.29    dyoung 	/*
   1221      1.29    dyoung 	 * Use highest priority AC.
   1222      1.29    dyoung 	 */
   1223      1.29    dyoung 	if (v_wme_ac > d_wme_ac)
   1224      1.29    dyoung 		ac = v_wme_ac;
   1225      1.29    dyoung 	else
   1226      1.29    dyoung 		ac = d_wme_ac;
   1227      1.29    dyoung 
   1228      1.29    dyoung 	/*
   1229      1.29    dyoung 	 * Apply ACM policy.
   1230      1.29    dyoung 	 */
   1231  1.63.2.1      phil 	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
   1232      1.29    dyoung 		static const int acmap[4] = {
   1233      1.29    dyoung 			WME_AC_BK,	/* WME_AC_BE */
   1234      1.29    dyoung 			WME_AC_BK,	/* WME_AC_BK */
   1235      1.29    dyoung 			WME_AC_BE,	/* WME_AC_VI */
   1236      1.29    dyoung 			WME_AC_VI,	/* WME_AC_VO */
   1237      1.29    dyoung 		};
   1238  1.63.2.1      phil 		struct ieee80211com *ic = ni->ni_ic;
   1239  1.63.2.1      phil 
   1240      1.29    dyoung 		while (ac != WME_AC_BK &&
   1241      1.29    dyoung 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
   1242      1.29    dyoung 			ac = acmap[ac];
   1243      1.29    dyoung 	}
   1244      1.29    dyoung done:
   1245      1.29    dyoung 	M_WME_SETAC(m, ac);
   1246      1.29    dyoung 	return 0;
   1247      1.29    dyoung }
   1248      1.29    dyoung 
   1249      1.29    dyoung /*
   1250      1.29    dyoung  * Insure there is sufficient contiguous space to encapsulate the
   1251      1.29    dyoung  * 802.11 data frame.  If room isn't already there, arrange for it.
   1252      1.29    dyoung  * Drivers and cipher modules assume we have done the necessary work
   1253      1.29    dyoung  * and fail rudely if they don't find the space they need.
   1254      1.29    dyoung  */
   1255  1.63.2.1      phil struct mbuf *
   1256  1.63.2.1      phil ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
   1257      1.29    dyoung 	struct ieee80211_key *key, struct mbuf *m)
   1258      1.29    dyoung {
   1259      1.29    dyoung #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
   1260  1.63.2.1      phil 	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
   1261      1.29    dyoung 
   1262      1.29    dyoung 	if (key != NULL) {
   1263      1.29    dyoung 		/* XXX belongs in crypto code? */
   1264      1.29    dyoung 		needed_space += key->wk_cipher->ic_header;
   1265      1.29    dyoung 		/* XXX frags */
   1266  1.63.2.1      phil 		/*
   1267  1.63.2.1      phil 		 * When crypto is being done in the host we must insure
   1268  1.63.2.1      phil 		 * the data are writable for the cipher routines; clone
   1269  1.63.2.1      phil 		 * a writable mbuf chain.
   1270  1.63.2.1      phil 		 * XXX handle SWMIC specially
   1271  1.63.2.1      phil 		 */
   1272  1.63.2.1      phil 		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
   1273  1.63.2.1      phil 			m = m_unshare(m, M_NOWAIT);
   1274  1.63.2.1      phil 			if (m == NULL) {
   1275  1.63.2.1      phil 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
   1276  1.63.2.1      phil 				    "%s: cannot get writable mbuf\n", __func__);
   1277  1.63.2.1      phil 				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
   1278  1.63.2.1      phil 				return NULL;
   1279  1.63.2.1      phil 			}
   1280  1.63.2.1      phil 		}
   1281      1.29    dyoung 	}
   1282      1.29    dyoung 	/*
   1283      1.29    dyoung 	 * We know we are called just before stripping an Ethernet
   1284      1.29    dyoung 	 * header and prepending an LLC header.  This means we know
   1285      1.29    dyoung 	 * there will be
   1286      1.29    dyoung 	 *	sizeof(struct ether_header) - sizeof(struct llc)
   1287      1.29    dyoung 	 * bytes recovered to which we need additional space for the
   1288      1.29    dyoung 	 * 802.11 header and any crypto header.
   1289      1.29    dyoung 	 */
   1290      1.29    dyoung 	/* XXX check trailing space and copy instead? */
   1291      1.29    dyoung 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
   1292      1.29    dyoung 		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
   1293      1.29    dyoung 		if (n == NULL) {
   1294  1.63.2.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
   1295  1.63.2.1      phil 			    "%s: cannot expand storage\n", __func__);
   1296  1.63.2.1      phil 			vap->iv_stats.is_tx_nobuf++;
   1297      1.29    dyoung 			m_freem(m);
   1298      1.29    dyoung 			return NULL;
   1299      1.29    dyoung 		}
   1300  1.63.2.2      phil #if __FreeBSD__
   1301  1.63.2.1      phil 		KASSERT(needed_space <= MHLEN,
   1302  1.63.2.1      phil 		    ("not enough room, need %u got %d\n", needed_space, MHLEN));
   1303  1.63.2.2      phil #elif __NetBSD__
   1304  1.63.2.2      phil 		KASSERT(needed_space <= MHLEN,
   1305  1.63.2.6    martin 		    ("not enough room, need %u got %lu\n", needed_space, (u_long)MHLEN));
   1306  1.63.2.2      phil #endif
   1307  1.63.2.6    martin 
   1308      1.29    dyoung 		/*
   1309      1.29    dyoung 		 * Setup new mbuf to have leading space to prepend the
   1310      1.29    dyoung 		 * 802.11 header and any crypto header bits that are
   1311      1.29    dyoung 		 * required (the latter are added when the driver calls
   1312      1.29    dyoung 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
   1313      1.29    dyoung 		 */
   1314  1.63.2.1      phil 		/* NB: must be first 'cuz it clobbers m_data */
   1315  1.63.2.1      phil 		m_move_pkthdr(n, m);
   1316  1.63.2.1      phil 		n->m_len = 0;			/* NB: m_gethdr does not set */
   1317      1.29    dyoung 		n->m_data += needed_space;
   1318      1.29    dyoung 		/*
   1319      1.29    dyoung 		 * Pull up Ethernet header to create the expected layout.
   1320      1.29    dyoung 		 * We could use m_pullup but that's overkill (i.e. we don't
   1321      1.29    dyoung 		 * need the actual data) and it cannot fail so do it inline
   1322      1.29    dyoung 		 * for speed.
   1323      1.29    dyoung 		 */
   1324  1.63.2.1      phil 		/* NB: struct ether_header is known to be contiguous */
   1325      1.29    dyoung 		n->m_len += sizeof(struct ether_header);
   1326      1.29    dyoung 		m->m_len -= sizeof(struct ether_header);
   1327      1.29    dyoung 		m->m_data += sizeof(struct ether_header);
   1328      1.29    dyoung 		/*
   1329      1.29    dyoung 		 * Replace the head of the chain.
   1330      1.29    dyoung 		 */
   1331      1.29    dyoung 		n->m_next = m;
   1332      1.29    dyoung 		m = n;
   1333      1.34     skrll 	}
   1334      1.29    dyoung 	return m;
   1335      1.29    dyoung #undef TO_BE_RECLAIMED
   1336      1.29    dyoung }
   1337      1.29    dyoung 
   1338      1.29    dyoung /*
   1339      1.29    dyoung  * Return the transmit key to use in sending a unicast frame.
   1340      1.29    dyoung  * If a unicast key is set we use that.  When no unicast key is set
   1341      1.29    dyoung  * we fall back to the default transmit key.
   1342      1.29    dyoung  */
   1343      1.29    dyoung static __inline struct ieee80211_key *
   1344  1.63.2.1      phil ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
   1345  1.63.2.1      phil 	struct ieee80211_node *ni)
   1346      1.29    dyoung {
   1347  1.63.2.1      phil 	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
   1348  1.63.2.1      phil 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
   1349  1.63.2.1      phil 		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
   1350      1.29    dyoung 			return NULL;
   1351  1.63.2.1      phil 		return &vap->iv_nw_keys[vap->iv_def_txkey];
   1352      1.29    dyoung 	} else {
   1353      1.29    dyoung 		return &ni->ni_ucastkey;
   1354      1.29    dyoung 	}
   1355      1.29    dyoung }
   1356      1.29    dyoung 
   1357      1.29    dyoung /*
   1358      1.29    dyoung  * Return the transmit key to use in sending a multicast frame.
   1359      1.29    dyoung  * Multicast traffic always uses the group key which is installed as
   1360      1.29    dyoung  * the default tx key.
   1361      1.29    dyoung  */
   1362      1.29    dyoung static __inline struct ieee80211_key *
   1363  1.63.2.1      phil ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
   1364  1.63.2.1      phil 	struct ieee80211_node *ni)
   1365      1.29    dyoung {
   1366  1.63.2.1      phil 	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
   1367  1.63.2.1      phil 	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
   1368      1.29    dyoung 		return NULL;
   1369  1.63.2.1      phil 	return &vap->iv_nw_keys[vap->iv_def_txkey];
   1370      1.29    dyoung }
   1371      1.29    dyoung 
   1372      1.29    dyoung /*
   1373      1.29    dyoung  * Encapsulate an outbound data frame.  The mbuf chain is updated.
   1374      1.29    dyoung  * If an error is encountered NULL is returned.  The caller is required
   1375      1.29    dyoung  * to provide a node reference and pullup the ethernet header in the
   1376      1.29    dyoung  * first mbuf.
   1377  1.63.2.1      phil  *
   1378  1.63.2.1      phil  * NB: Packet is assumed to be processed by ieee80211_classify which
   1379  1.63.2.1      phil  *     marked EAPOL frames w/ M_EAPOL.
   1380       1.1    dyoung  */
   1381       1.1    dyoung struct mbuf *
   1382  1.63.2.1      phil ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
   1383  1.63.2.1      phil     struct mbuf *m)
   1384       1.1    dyoung {
   1385  1.63.2.1      phil #define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
   1386  1.63.2.1      phil #define MC01(mc)	((struct ieee80211_meshcntl_ae01 *)mc)
   1387  1.63.2.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   1388  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   1389  1.63.2.1      phil 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
   1390  1.63.2.1      phil 	struct ieee80211_meshcntl_ae10 *mc;
   1391  1.63.2.1      phil 	struct ieee80211_mesh_route *rt = NULL;
   1392  1.63.2.1      phil 	int dir = -1;
   1393  1.63.2.1      phil #endif
   1394       1.1    dyoung 	struct ether_header eh;
   1395       1.1    dyoung 	struct ieee80211_frame *wh;
   1396      1.29    dyoung 	struct ieee80211_key *key;
   1397       1.1    dyoung 	struct llc *llc;
   1398  1.63.2.1      phil 	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr, is_mcast;
   1399  1.63.2.1      phil 	ieee80211_seq seqno;
   1400  1.63.2.1      phil 	int meshhdrsize, meshae;
   1401  1.63.2.1      phil 	uint8_t *qos;
   1402  1.63.2.1      phil 	int is_amsdu = 0;
   1403  1.63.2.4      phil 
   1404  1.63.2.1      phil 	IEEE80211_TX_LOCK_ASSERT(ic);
   1405  1.63.2.1      phil 
   1406  1.63.2.1      phil 	is_mcast = !! (m->m_flags & (M_MCAST | M_BCAST));
   1407       1.1    dyoung 
   1408  1.63.2.1      phil 	/*
   1409  1.63.2.1      phil 	 * Copy existing Ethernet header to a safe place.  The
   1410  1.63.2.1      phil 	 * rest of the code assumes it's ok to strip it when
   1411  1.63.2.1      phil 	 * reorganizing state for the final encapsulation.
   1412  1.63.2.1      phil 	 */
   1413  1.63.2.1      phil 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
   1414  1.63.2.1      phil 	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
   1415       1.1    dyoung 
   1416      1.29    dyoung 	/*
   1417      1.29    dyoung 	 * Insure space for additional headers.  First identify
   1418      1.29    dyoung 	 * transmit key to use in calculating any buffer adjustments
   1419      1.29    dyoung 	 * required.  This is also used below to do privacy
   1420      1.29    dyoung 	 * encapsulation work.  Then calculate the 802.11 header
   1421      1.29    dyoung 	 * size and any padding required by the driver.
   1422      1.29    dyoung 	 *
   1423      1.29    dyoung 	 * Note key may be NULL if we fall back to the default
   1424      1.29    dyoung 	 * transmit key and that is not set.  In that case the
   1425      1.29    dyoung 	 * buffer may not be expanded as needed by the cipher
   1426      1.29    dyoung 	 * routines, but they will/should discard it.
   1427      1.29    dyoung 	 */
   1428  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
   1429  1.63.2.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA ||
   1430  1.63.2.1      phil 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
   1431  1.63.2.1      phil 		    (vap->iv_opmode == IEEE80211_M_WDS &&
   1432  1.63.2.1      phil 		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
   1433  1.63.2.1      phil 			key = ieee80211_crypto_getucastkey(vap, ni);
   1434  1.63.2.1      phil 		else
   1435  1.63.2.1      phil 			key = ieee80211_crypto_getmcastkey(vap, ni);
   1436  1.63.2.1      phil 		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
   1437  1.63.2.1      phil 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
   1438  1.63.2.1      phil 			    eh.ether_dhost,
   1439  1.63.2.1      phil 			    "no default transmit key (%s) deftxkey %u",
   1440  1.63.2.1      phil 			    __func__, vap->iv_def_txkey);
   1441  1.63.2.1      phil 			vap->iv_stats.is_tx_nodefkey++;
   1442  1.63.2.1      phil 			goto bad;
   1443      1.29    dyoung 		}
   1444  1.63.2.1      phil 	} else
   1445      1.29    dyoung 		key = NULL;
   1446      1.29    dyoung 	/*
   1447      1.29    dyoung 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
   1448      1.29    dyoung 	 * frames so suppress use.  This may be an issue if other
   1449      1.29    dyoung 	 * ap's require all data frames to be QoS-encapsulated
   1450      1.29    dyoung 	 * once negotiated in which case we'll need to make this
   1451      1.29    dyoung 	 * configurable.
   1452  1.63.2.1      phil 	 *
   1453  1.63.2.1      phil 	 * Don't send multicast QoS frames.
   1454  1.63.2.1      phil 	 * Technically multicast frames can be QoS if all stations in the
   1455  1.63.2.1      phil 	 * BSS are also QoS.
   1456  1.63.2.1      phil 	 *
   1457  1.63.2.1      phil 	 * NB: mesh data frames are QoS, including multicast frames.
   1458      1.29    dyoung 	 */
   1459  1.63.2.1      phil 	addqos =
   1460  1.63.2.1      phil 	    (((is_mcast == 0) && (ni->ni_flags &
   1461  1.63.2.1      phil 	     (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))) ||
   1462  1.63.2.1      phil 	    (vap->iv_opmode == IEEE80211_M_MBSS)) &&
   1463  1.63.2.1      phil 	    (m->m_flags & M_EAPOL) == 0;
   1464  1.63.2.1      phil 
   1465      1.29    dyoung 	if (addqos)
   1466      1.29    dyoung 		hdrsize = sizeof(struct ieee80211_qosframe);
   1467      1.29    dyoung 	else
   1468      1.29    dyoung 		hdrsize = sizeof(struct ieee80211_frame);
   1469  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   1470  1.63.2.1      phil 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
   1471  1.63.2.1      phil 		/*
   1472  1.63.2.1      phil 		 * Mesh data frames are encapsulated according to the
   1473  1.63.2.1      phil 		 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
   1474  1.63.2.1      phil 		 * o Group Addressed data (aka multicast) originating
   1475  1.63.2.1      phil 		 *   at the local sta are sent w/ 3-address format and
   1476  1.63.2.1      phil 		 *   address extension mode 00
   1477  1.63.2.1      phil 		 * o Individually Addressed data (aka unicast) originating
   1478  1.63.2.1      phil 		 *   at the local sta are sent w/ 4-address format and
   1479  1.63.2.1      phil 		 *   address extension mode 00
   1480  1.63.2.1      phil 		 * o Group Addressed data forwarded from a non-mesh sta are
   1481  1.63.2.1      phil 		 *   sent w/ 3-address format and address extension mode 01
   1482  1.63.2.1      phil 		 * o Individually Address data from another sta are sent
   1483  1.63.2.1      phil 		 *   w/ 4-address format and address extension mode 10
   1484  1.63.2.1      phil 		 */
   1485  1.63.2.1      phil 		is4addr = 0;		/* NB: don't use, disable */
   1486  1.63.2.1      phil 		if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
   1487  1.63.2.1      phil 			rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
   1488  1.63.2.1      phil 			KASSERT(rt != NULL, ("route is NULL"));
   1489  1.63.2.1      phil 			dir = IEEE80211_FC1_DIR_DSTODS;
   1490  1.63.2.1      phil 			hdrsize += IEEE80211_ADDR_LEN;
   1491  1.63.2.1      phil 			if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
   1492  1.63.2.1      phil 				if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
   1493  1.63.2.1      phil 				    vap->iv_myaddr)) {
   1494  1.63.2.1      phil 					IEEE80211_NOTE_MAC(vap,
   1495  1.63.2.1      phil 					    IEEE80211_MSG_MESH,
   1496  1.63.2.1      phil 					    eh.ether_dhost,
   1497  1.63.2.1      phil 					    "%s", "trying to send to ourself");
   1498  1.63.2.1      phil 					goto bad;
   1499  1.63.2.1      phil 				}
   1500  1.63.2.1      phil 				meshae = IEEE80211_MESH_AE_10;
   1501  1.63.2.1      phil 				meshhdrsize =
   1502  1.63.2.1      phil 				    sizeof(struct ieee80211_meshcntl_ae10);
   1503  1.63.2.1      phil 			} else {
   1504  1.63.2.1      phil 				meshae = IEEE80211_MESH_AE_00;
   1505  1.63.2.1      phil 				meshhdrsize =
   1506  1.63.2.1      phil 				    sizeof(struct ieee80211_meshcntl);
   1507  1.63.2.1      phil 			}
   1508  1.63.2.1      phil 		} else {
   1509  1.63.2.1      phil 			dir = IEEE80211_FC1_DIR_FROMDS;
   1510  1.63.2.1      phil 			if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
   1511  1.63.2.1      phil 				/* proxy group */
   1512  1.63.2.1      phil 				meshae = IEEE80211_MESH_AE_01;
   1513  1.63.2.1      phil 				meshhdrsize =
   1514  1.63.2.1      phil 				    sizeof(struct ieee80211_meshcntl_ae01);
   1515  1.63.2.1      phil 			} else {
   1516  1.63.2.1      phil 				/* group */
   1517  1.63.2.1      phil 				meshae = IEEE80211_MESH_AE_00;
   1518  1.63.2.1      phil 				meshhdrsize = sizeof(struct ieee80211_meshcntl);
   1519  1.63.2.1      phil 			}
   1520  1.63.2.1      phil 		}
   1521  1.63.2.1      phil 	} else {
   1522  1.63.2.1      phil #endif
   1523  1.63.2.1      phil 		/*
   1524  1.63.2.1      phil 		 * 4-address frames need to be generated for:
   1525  1.63.2.1      phil 		 * o packets sent through a WDS vap (IEEE80211_M_WDS)
   1526  1.63.2.1      phil 		 * o packets sent through a vap marked for relaying
   1527  1.63.2.1      phil 		 *   (e.g. a station operating with dynamic WDS)
   1528  1.63.2.1      phil 		 */
   1529  1.63.2.1      phil 		is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
   1530  1.63.2.1      phil 		    ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
   1531  1.63.2.1      phil 		     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
   1532  1.63.2.1      phil 		if (is4addr)
   1533  1.63.2.1      phil 			hdrsize += IEEE80211_ADDR_LEN;
   1534  1.63.2.1      phil 		meshhdrsize = meshae = 0;
   1535  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   1536  1.63.2.1      phil 	}
   1537  1.63.2.1      phil #endif
   1538  1.63.2.1      phil 	/*
   1539  1.63.2.1      phil 	 * Honor driver DATAPAD requirement.
   1540  1.63.2.1      phil 	 */
   1541      1.29    dyoung 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
   1542  1.63.2.1      phil 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
   1543  1.63.2.1      phil 	else
   1544  1.63.2.1      phil 		hdrspace = hdrsize;
   1545      1.60      maxv 
   1546  1.63.2.1      phil 	if (__predict_true((m->m_flags & M_FF) == 0)) {
   1547  1.63.2.1      phil 		/*
   1548  1.63.2.1      phil 		 * Normal frame.
   1549  1.63.2.1      phil 		 */
   1550  1.63.2.1      phil 		m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
   1551  1.63.2.1      phil 		if (m == NULL) {
   1552  1.63.2.1      phil 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
   1553  1.63.2.1      phil 			goto bad;
   1554  1.63.2.1      phil 		}
   1555  1.63.2.1      phil 		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
   1556  1.63.2.1      phil 		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
   1557  1.63.2.1      phil 		llc = mtod(m, struct llc *);
   1558  1.63.2.1      phil 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
   1559  1.63.2.1      phil 		llc->llc_control = LLC_UI;
   1560  1.63.2.1      phil 		llc->llc_snap.org_code[0] = 0;
   1561  1.63.2.1      phil 		llc->llc_snap.org_code[1] = 0;
   1562  1.63.2.1      phil 		llc->llc_snap.org_code[2] = 0;
   1563  1.63.2.1      phil 		llc->llc_snap.ether_type = eh.ether_type;
   1564  1.63.2.1      phil 	} else {
   1565  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   1566  1.63.2.1      phil 		/*
   1567  1.63.2.1      phil 		 * Aggregated frame.  Check if it's for AMSDU or FF.
   1568  1.63.2.1      phil 		 *
   1569  1.63.2.1      phil 		 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
   1570  1.63.2.1      phil 		 * anywhere for some reason.  But, since 11n requires
   1571  1.63.2.1      phil 		 * AMSDU RX, we can just assume "11n" == "AMSDU".
   1572  1.63.2.1      phil 		 */
   1573  1.63.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
   1574  1.63.2.1      phil 		if (ieee80211_amsdu_tx_ok(ni)) {
   1575  1.63.2.1      phil 			m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
   1576  1.63.2.1      phil 			is_amsdu = 1;
   1577  1.63.2.1      phil 		} else {
   1578  1.63.2.1      phil 			m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
   1579  1.63.2.1      phil 		}
   1580  1.63.2.1      phil 		if (m == NULL)
   1581  1.63.2.1      phil #endif
   1582  1.63.2.1      phil 			goto bad;
   1583      1.12    dyoung 	}
   1584      1.29    dyoung 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
   1585      1.29    dyoung 
   1586  1.63.2.1      phil 	M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
   1587      1.10    dyoung 	if (m == NULL) {
   1588  1.63.2.1      phil 		vap->iv_stats.is_tx_nobuf++;
   1589       1.1    dyoung 		goto bad;
   1590      1.10    dyoung 	}
   1591       1.1    dyoung 	wh = mtod(m, struct ieee80211_frame *);
   1592       1.1    dyoung 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
   1593  1.63.2.1      phil 	*(uint16_t *)wh->i_dur = 0;
   1594  1.63.2.1      phil 	qos = NULL;	/* NB: quiet compiler */
   1595  1.63.2.1      phil 	if (is4addr) {
   1596  1.63.2.1      phil 		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
   1597  1.63.2.1      phil 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
   1598  1.63.2.1      phil 		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
   1599  1.63.2.1      phil 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
   1600  1.63.2.1      phil 		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
   1601  1.63.2.1      phil 	} else switch (vap->iv_opmode) {
   1602       1.1    dyoung 	case IEEE80211_M_STA:
   1603       1.1    dyoung 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
   1604       1.1    dyoung 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
   1605       1.1    dyoung 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
   1606       1.1    dyoung 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
   1607       1.1    dyoung 		break;
   1608       1.1    dyoung 	case IEEE80211_M_IBSS:
   1609       1.1    dyoung 	case IEEE80211_M_AHDEMO:
   1610       1.1    dyoung 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   1611       1.1    dyoung 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
   1612       1.1    dyoung 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
   1613      1.29    dyoung 		/*
   1614  1.63.2.1      phil 		 * NB: always use the bssid from iv_bss as the
   1615      1.29    dyoung 		 *     neighbor's may be stale after an ibss merge
   1616      1.29    dyoung 		 */
   1617  1.63.2.1      phil 		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
   1618       1.1    dyoung 		break;
   1619       1.1    dyoung 	case IEEE80211_M_HOSTAP:
   1620       1.1    dyoung 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
   1621       1.1    dyoung 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
   1622       1.1    dyoung 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
   1623       1.1    dyoung 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
   1624       1.1    dyoung 		break;
   1625  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   1626  1.63.2.1      phil 	case IEEE80211_M_MBSS:
   1627  1.63.2.1      phil 		/* NB: offset by hdrspace to deal with DATAPAD */
   1628  1.63.2.1      phil 		mc = (struct ieee80211_meshcntl_ae10 *)
   1629  1.63.2.1      phil 		     (mtod(m, uint8_t *) + hdrspace);
   1630  1.63.2.1      phil 		wh->i_fc[1] = dir;
   1631  1.63.2.1      phil 		switch (meshae) {
   1632  1.63.2.1      phil 		case IEEE80211_MESH_AE_00:	/* no proxy */
   1633  1.63.2.1      phil 			mc->mc_flags = 0;
   1634  1.63.2.1      phil 			if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
   1635  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr1,
   1636  1.63.2.1      phil 				    ni->ni_macaddr);
   1637  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr2,
   1638  1.63.2.1      phil 				    vap->iv_myaddr);
   1639  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr3,
   1640  1.63.2.1      phil 				    eh.ether_dhost);
   1641  1.63.2.1      phil 				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
   1642  1.63.2.1      phil 				    eh.ether_shost);
   1643  1.63.2.1      phil 				qos =((struct ieee80211_qosframe_addr4 *)
   1644  1.63.2.1      phil 				    wh)->i_qos;
   1645  1.63.2.1      phil 			} else if (dir == IEEE80211_FC1_DIR_FROMDS) {
   1646  1.63.2.1      phil 				 /* mcast */
   1647  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr1,
   1648  1.63.2.1      phil 				    eh.ether_dhost);
   1649  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr2,
   1650  1.63.2.1      phil 				    vap->iv_myaddr);
   1651  1.63.2.1      phil 				IEEE80211_ADDR_COPY(wh->i_addr3,
   1652  1.63.2.1      phil 				    eh.ether_shost);
   1653  1.63.2.1      phil 				qos = ((struct ieee80211_qosframe *)
   1654  1.63.2.1      phil 				    wh)->i_qos;
   1655  1.63.2.1      phil 			}
   1656  1.63.2.1      phil 			break;
   1657  1.63.2.1      phil 		case IEEE80211_MESH_AE_01:	/* mcast, proxy */
   1658  1.63.2.1      phil 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
   1659  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
   1660  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
   1661  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
   1662  1.63.2.1      phil 			mc->mc_flags = 1;
   1663  1.63.2.1      phil 			IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
   1664  1.63.2.1      phil 			    eh.ether_shost);
   1665  1.63.2.1      phil 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
   1666  1.63.2.1      phil 			break;
   1667  1.63.2.1      phil 		case IEEE80211_MESH_AE_10:	/* ucast, proxy */
   1668  1.63.2.1      phil 			KASSERT(rt != NULL, ("route is NULL"));
   1669  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
   1670  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
   1671  1.63.2.1      phil 			IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
   1672  1.63.2.1      phil 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
   1673  1.63.2.1      phil 			mc->mc_flags = IEEE80211_MESH_AE_10;
   1674  1.63.2.1      phil 			IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
   1675  1.63.2.1      phil 			IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
   1676  1.63.2.1      phil 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
   1677  1.63.2.1      phil 			break;
   1678  1.63.2.1      phil 		default:
   1679  1.63.2.1      phil 			KASSERT(0, ("meshae %d", meshae));
   1680  1.63.2.1      phil 			break;
   1681  1.63.2.1      phil 		}
   1682  1.63.2.1      phil 		mc->mc_ttl = ms->ms_ttl;
   1683  1.63.2.1      phil 		ms->ms_seq++;
   1684  1.63.2.1      phil 		le32enc(mc->mc_seq, ms->ms_seq);
   1685  1.63.2.1      phil 		break;
   1686  1.63.2.1      phil #endif
   1687  1.63.2.1      phil 	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
   1688  1.63.2.1      phil 	default:
   1689       1.1    dyoung 		goto bad;
   1690       1.1    dyoung 	}
   1691      1.33    dyoung 	if (m->m_flags & M_MORE_DATA)
   1692      1.33    dyoung 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
   1693      1.29    dyoung 	if (addqos) {
   1694      1.29    dyoung 		int ac, tid;
   1695      1.29    dyoung 
   1696  1.63.2.1      phil 		if (is4addr) {
   1697  1.63.2.1      phil 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
   1698  1.63.2.1      phil 		/* NB: mesh case handled earlier */
   1699  1.63.2.1      phil 		} else if (vap->iv_opmode != IEEE80211_M_MBSS)
   1700  1.63.2.1      phil 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
   1701      1.29    dyoung 		ac = M_WME_GETAC(m);
   1702      1.29    dyoung 		/* map from access class/queue to 11e header priorty value */
   1703      1.29    dyoung 		tid = WME_AC_TO_TID(ac);
   1704  1.63.2.1      phil 		qos[0] = tid & IEEE80211_QOS_TID;
   1705      1.29    dyoung 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
   1706  1.63.2.1      phil 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
   1707  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   1708  1.63.2.1      phil 		if (vap->iv_opmode == IEEE80211_M_MBSS)
   1709  1.63.2.1      phil 			qos[1] = IEEE80211_QOS_MC;
   1710  1.63.2.1      phil 		else
   1711  1.63.2.1      phil #endif
   1712  1.63.2.1      phil 			qos[1] = 0;
   1713  1.63.2.1      phil 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
   1714  1.63.2.1      phil 
   1715  1.63.2.1      phil 		/*
   1716  1.63.2.1      phil 		 * If this is an A-MSDU then ensure we set the
   1717  1.63.2.1      phil 		 * relevant field.
   1718  1.63.2.1      phil 		 */
   1719  1.63.2.1      phil 		if (is_amsdu)
   1720  1.63.2.1      phil 			qos[0] |= IEEE80211_QOS_AMSDU;
   1721  1.63.2.1      phil 
   1722  1.63.2.1      phil 		/*
   1723  1.63.2.1      phil 		 * XXX TODO TX lock is needed for atomic updates of sequence
   1724  1.63.2.1      phil 		 * numbers.  If the driver does it, then don't do it here;
   1725  1.63.2.1      phil 		 * and we don't need the TX lock held.
   1726  1.63.2.1      phil 		 */
   1727  1.63.2.1      phil 		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
   1728  1.63.2.1      phil 			/*
   1729  1.63.2.1      phil 			 * 802.11-2012 9.3.2.10 -
   1730  1.63.2.1      phil 			 *
   1731  1.63.2.1      phil 			 * If this is a multicast frame then we need
   1732  1.63.2.1      phil 			 * to ensure that the sequence number comes from
   1733  1.63.2.1      phil 			 * a separate seqno space and not the TID space.
   1734  1.63.2.1      phil 			 *
   1735  1.63.2.1      phil 			 * Otherwise multicast frames may actually cause
   1736  1.63.2.1      phil 			 * holes in the TX blockack window space and
   1737  1.63.2.1      phil 			 * upset various things.
   1738  1.63.2.1      phil 			 */
   1739  1.63.2.1      phil 			if (IEEE80211_IS_MULTICAST(wh->i_addr1))
   1740  1.63.2.1      phil 				seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
   1741  1.63.2.1      phil 			else
   1742  1.63.2.1      phil 				seqno = ni->ni_txseqs[tid]++;
   1743  1.63.2.1      phil 
   1744  1.63.2.1      phil 			/*
   1745  1.63.2.1      phil 			 * NB: don't assign a sequence # to potential
   1746  1.63.2.1      phil 			 * aggregates; we expect this happens at the
   1747  1.63.2.1      phil 			 * point the frame comes off any aggregation q
   1748  1.63.2.1      phil 			 * as otherwise we may introduce holes in the
   1749  1.63.2.1      phil 			 * BA sequence space and/or make window accouting
   1750  1.63.2.1      phil 			 * more difficult.
   1751  1.63.2.1      phil 			 *
   1752  1.63.2.1      phil 			 * XXX may want to control this with a driver
   1753  1.63.2.1      phil 			 * capability; this may also change when we pull
   1754  1.63.2.1      phil 			 * aggregation up into net80211
   1755  1.63.2.1      phil 			 */
   1756  1.63.2.1      phil 			seqno = ni->ni_txseqs[tid]++;
   1757  1.63.2.1      phil 			*(uint16_t *)wh->i_seq =
   1758  1.63.2.1      phil 			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
   1759  1.63.2.1      phil 			M_SEQNO_SET(m, seqno);
   1760  1.63.2.1      phil 		} else {
   1761  1.63.2.1      phil 			/* NB: zero out i_seq field (for s/w encryption etc) */
   1762  1.63.2.1      phil 			*(uint16_t *)wh->i_seq = 0;
   1763  1.63.2.1      phil 		}
   1764      1.29    dyoung 	} else {
   1765  1.63.2.1      phil 		/*
   1766  1.63.2.1      phil 		 * XXX TODO TX lock is needed for atomic updates of sequence
   1767  1.63.2.1      phil 		 * numbers.  If the driver does it, then don't do it here;
   1768  1.63.2.1      phil 		 * and we don't need the TX lock held.
   1769  1.63.2.1      phil 		 */
   1770  1.63.2.1      phil 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
   1771  1.63.2.1      phil 		*(uint16_t *)wh->i_seq =
   1772  1.63.2.1      phil 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
   1773  1.63.2.1      phil 		M_SEQNO_SET(m, seqno);
   1774  1.63.2.1      phil 
   1775  1.63.2.1      phil 		/*
   1776  1.63.2.1      phil 		 * XXX TODO: we shouldn't allow EAPOL, etc that would
   1777  1.63.2.1      phil 		 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
   1778  1.63.2.1      phil 		 */
   1779  1.63.2.1      phil 		if (is_amsdu)
   1780  1.63.2.1      phil 			printf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
   1781  1.63.2.1      phil 			    __func__);
   1782      1.29    dyoung 	}
   1783      1.60      maxv 
   1784  1.63.2.1      phil 	/*
   1785  1.63.2.1      phil 	 * Check if xmit fragmentation is required.
   1786  1.63.2.1      phil 	 *
   1787  1.63.2.1      phil 	 * If the hardware does fragmentation offload, then don't bother
   1788  1.63.2.1      phil 	 * doing it here.
   1789  1.63.2.1      phil 	 */
   1790  1.63.2.1      phil 	if (IEEE80211_CONF_FRAG_OFFLOAD(ic))
   1791  1.63.2.1      phil 		txfrag = 0;
   1792  1.63.2.1      phil 	else
   1793  1.63.2.1      phil 		txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
   1794  1.63.2.1      phil 		    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
   1795  1.63.2.1      phil 		    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
   1796  1.63.2.1      phil 		    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
   1797      1.60      maxv 
   1798      1.29    dyoung 	if (key != NULL) {
   1799      1.29    dyoung 		/*
   1800      1.29    dyoung 		 * IEEE 802.1X: send EAPOL frames always in the clear.
   1801      1.29    dyoung 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
   1802      1.29    dyoung 		 */
   1803  1.63.2.1      phil 		if ((m->m_flags & M_EAPOL) == 0 ||
   1804  1.63.2.1      phil 		    ((vap->iv_flags & IEEE80211_F_WPA) &&
   1805  1.63.2.1      phil 		     (vap->iv_opmode == IEEE80211_M_STA ?
   1806  1.63.2.1      phil 		      !IEEE80211_KEY_UNDEFINED(key) :
   1807  1.63.2.1      phil 		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
   1808  1.63.2.1      phil 			wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
   1809  1.63.2.1      phil 			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
   1810  1.63.2.1      phil 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
   1811  1.63.2.1      phil 				    eh.ether_dhost,
   1812  1.63.2.1      phil 				    "%s", "enmic failed, discard frame");
   1813  1.63.2.1      phil 				vap->iv_stats.is_crypto_enmicfail++;
   1814      1.29    dyoung 				goto bad;
   1815      1.29    dyoung 			}
   1816      1.29    dyoung 		}
   1817      1.29    dyoung 	}
   1818  1.63.2.1      phil 	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
   1819  1.63.2.1      phil 	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
   1820      1.46    dyoung 		goto bad;
   1821      1.29    dyoung 
   1822  1.63.2.1      phil 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
   1823  1.63.2.1      phil 
   1824      1.29    dyoung 	IEEE80211_NODE_STAT(ni, tx_data);
   1825  1.63.2.1      phil 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   1826  1.63.2.1      phil 		IEEE80211_NODE_STAT(ni, tx_mcast);
   1827  1.63.2.1      phil 		m->m_flags |= M_MCAST;
   1828  1.63.2.1      phil 	} else
   1829  1.63.2.1      phil 		IEEE80211_NODE_STAT(ni, tx_ucast);
   1830      1.29    dyoung 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
   1831      1.29    dyoung 
   1832       1.1    dyoung 	return m;
   1833       1.1    dyoung bad:
   1834       1.1    dyoung 	if (m != NULL)
   1835       1.1    dyoung 		m_freem(m);
   1836       1.1    dyoung 	return NULL;
   1837  1.63.2.1      phil #undef WH4
   1838  1.63.2.1      phil #undef MC01
   1839       1.1    dyoung }
   1840       1.1    dyoung 
   1841  1.63.2.1      phil void
   1842  1.63.2.1      phil ieee80211_free_mbuf(struct mbuf *m)
   1843      1.18    dyoung {
   1844  1.63.2.1      phil 	struct mbuf *next;
   1845      1.18    dyoung 
   1846  1.63.2.1      phil 	if (m == NULL)
   1847  1.63.2.1      phil 		return;
   1848      1.19    dyoung 
   1849  1.63.2.1      phil 	do {
   1850  1.63.2.1      phil 		next = m->m_nextpkt;
   1851  1.63.2.1      phil 		m->m_nextpkt = NULL;
   1852  1.63.2.1      phil 		m_freem(m);
   1853  1.63.2.1      phil 	} while ((m = next) != NULL);
   1854      1.18    dyoung }
   1855      1.18    dyoung 
   1856      1.18    dyoung /*
   1857      1.46    dyoung  * Fragment the frame according to the specified mtu.
   1858      1.46    dyoung  * The size of the 802.11 header (w/o padding) is provided
   1859      1.46    dyoung  * so we don't need to recalculate it.  We create a new
   1860      1.46    dyoung  * mbuf for each fragment and chain it through m_nextpkt;
   1861      1.46    dyoung  * we might be able to optimize this by reusing the original
   1862      1.46    dyoung  * packet's mbufs but that is significantly more complicated.
   1863      1.46    dyoung  */
   1864      1.46    dyoung static int
   1865  1.63.2.1      phil ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
   1866      1.46    dyoung 	u_int hdrsize, u_int ciphdrsize, u_int mtu)
   1867      1.46    dyoung {
   1868  1.63.2.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1869      1.46    dyoung 	struct ieee80211_frame *wh, *whf;
   1870  1.63.2.1      phil 	struct mbuf *m, *prev;
   1871  1.63.2.1      phil 	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
   1872  1.63.2.1      phil 	u_int hdrspace;
   1873      1.46    dyoung 
   1874  1.63.2.1      phil 	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
   1875  1.63.2.1      phil 	KASSERT(m0->m_pkthdr.len > mtu,
   1876      1.46    dyoung 		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
   1877      1.46    dyoung 
   1878  1.63.2.1      phil 	/*
   1879  1.63.2.1      phil 	 * Honor driver DATAPAD requirement.
   1880  1.63.2.1      phil 	 */
   1881  1.63.2.1      phil 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
   1882  1.63.2.1      phil 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
   1883  1.63.2.1      phil 	else
   1884  1.63.2.1      phil 		hdrspace = hdrsize;
   1885  1.63.2.1      phil 
   1886      1.46    dyoung 	wh = mtod(m0, struct ieee80211_frame *);
   1887      1.46    dyoung 	/* NB: mark the first frag; it will be propagated below */
   1888      1.46    dyoung 	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
   1889  1.63.2.1      phil 	totalhdrsize = hdrspace + ciphdrsize;
   1890      1.46    dyoung 	fragno = 1;
   1891      1.46    dyoung 	off = mtu - ciphdrsize;
   1892      1.46    dyoung 	remainder = m0->m_pkthdr.len - off;
   1893      1.46    dyoung 	prev = m0;
   1894      1.46    dyoung 	do {
   1895  1.63.2.1      phil 		fragsize = MIN(totalhdrsize + remainder, mtu);
   1896  1.63.2.2      phil #if __FreeBSD__
   1897  1.63.2.1      phil 		m = m_get2(fragsize, M_NOWAIT, MT_DATA, M_PKTHDR);
   1898  1.63.2.2      phil #elif __NetBSD__
   1899  1.63.2.2      phil 		m = m_get(M_NOWAIT, MT_DATA);
   1900  1.63.2.2      phil #endif
   1901      1.46    dyoung 		if (m == NULL)
   1902      1.46    dyoung 			goto bad;
   1903      1.46    dyoung 		/* leave room to prepend any cipher header */
   1904      1.46    dyoung 		m_align(m, fragsize - ciphdrsize);
   1905      1.46    dyoung 
   1906      1.46    dyoung 		/*
   1907      1.46    dyoung 		 * Form the header in the fragment.  Note that since
   1908      1.46    dyoung 		 * we mark the first fragment with the MORE_FRAG bit
   1909      1.46    dyoung 		 * it automatically is propagated to each fragment; we
   1910      1.46    dyoung 		 * need only clear it on the last fragment (done below).
   1911  1.63.2.1      phil 		 * NB: frag 1+ dont have Mesh Control field present.
   1912      1.46    dyoung 		 */
   1913      1.46    dyoung 		whf = mtod(m, struct ieee80211_frame *);
   1914      1.46    dyoung 		memcpy(whf, wh, hdrsize);
   1915  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   1916  1.63.2.1      phil 		if (vap->iv_opmode == IEEE80211_M_MBSS) {
   1917  1.63.2.1      phil 			if (IEEE80211_IS_DSTODS(wh))
   1918  1.63.2.1      phil 				((struct ieee80211_qosframe_addr4 *)
   1919  1.63.2.1      phil 				    whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
   1920  1.63.2.1      phil 			else
   1921  1.63.2.1      phil 				((struct ieee80211_qosframe *)
   1922  1.63.2.1      phil 				    whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
   1923  1.63.2.1      phil 		}
   1924  1.63.2.1      phil #endif
   1925  1.63.2.1      phil 		*(uint16_t *)&whf->i_seq[0] |= htole16(
   1926      1.46    dyoung 			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
   1927      1.46    dyoung 				IEEE80211_SEQ_FRAG_SHIFT);
   1928      1.46    dyoung 		fragno++;
   1929      1.46    dyoung 
   1930      1.46    dyoung 		payload = fragsize - totalhdrsize;
   1931      1.46    dyoung 		/* NB: destination is known to be contiguous */
   1932  1.63.2.1      phil 
   1933  1.63.2.1      phil 		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
   1934  1.63.2.1      phil 		m->m_len = hdrspace + payload;
   1935  1.63.2.1      phil 		m->m_pkthdr.len = hdrspace + payload;
   1936      1.46    dyoung 		m->m_flags |= M_FRAG;
   1937      1.46    dyoung 
   1938      1.46    dyoung 		/* chain up the fragment */
   1939      1.46    dyoung 		prev->m_nextpkt = m;
   1940      1.46    dyoung 		prev = m;
   1941      1.46    dyoung 
   1942      1.46    dyoung 		/* deduct fragment just formed */
   1943      1.46    dyoung 		remainder -= payload;
   1944      1.46    dyoung 		off += payload;
   1945      1.46    dyoung 	} while (remainder != 0);
   1946      1.61      maxv 
   1947  1.63.2.1      phil 	/* set the last fragment */
   1948  1.63.2.1      phil 	m->m_flags |= M_LASTFRAG;
   1949      1.46    dyoung 	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
   1950      1.46    dyoung 
   1951      1.46    dyoung 	/* strip first mbuf now that everything has been copied */
   1952      1.46    dyoung 	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
   1953      1.46    dyoung 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
   1954      1.46    dyoung 
   1955  1.63.2.1      phil 	vap->iv_stats.is_tx_fragframes++;
   1956  1.63.2.1      phil 	vap->iv_stats.is_tx_frags += fragno-1;
   1957      1.46    dyoung 
   1958      1.46    dyoung 	return 1;
   1959      1.46    dyoung bad:
   1960      1.46    dyoung 	/* reclaim fragments but leave original frame for caller to free */
   1961  1.63.2.1      phil 	ieee80211_free_mbuf(m0->m_nextpkt);
   1962      1.46    dyoung 	m0->m_nextpkt = NULL;
   1963      1.46    dyoung 	return 0;
   1964      1.46    dyoung }
   1965      1.46    dyoung 
   1966      1.46    dyoung /*
   1967       1.1    dyoung  * Add a supported rates element id to a frame.
   1968       1.1    dyoung  */
   1969  1.63.2.1      phil uint8_t *
   1970  1.63.2.1      phil ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
   1971       1.1    dyoung {
   1972       1.1    dyoung 	int nrates;
   1973       1.1    dyoung 
   1974       1.1    dyoung 	*frm++ = IEEE80211_ELEMID_RATES;
   1975       1.1    dyoung 	nrates = rs->rs_nrates;
   1976       1.1    dyoung 	if (nrates > IEEE80211_RATE_SIZE)
   1977       1.1    dyoung 		nrates = IEEE80211_RATE_SIZE;
   1978       1.1    dyoung 	*frm++ = nrates;
   1979       1.1    dyoung 	memcpy(frm, rs->rs_rates, nrates);
   1980       1.1    dyoung 	return frm + nrates;
   1981       1.1    dyoung }
   1982       1.1    dyoung 
   1983       1.1    dyoung /*
   1984       1.1    dyoung  * Add an extended supported rates element id to a frame.
   1985       1.1    dyoung  */
   1986  1.63.2.1      phil uint8_t *
   1987  1.63.2.1      phil ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
   1988       1.1    dyoung {
   1989       1.1    dyoung 	/*
   1990       1.1    dyoung 	 * Add an extended supported rates element if operating in 11g mode.
   1991       1.1    dyoung 	 */
   1992       1.1    dyoung 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
   1993       1.1    dyoung 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
   1994       1.1    dyoung 		*frm++ = IEEE80211_ELEMID_XRATES;
   1995       1.1    dyoung 		*frm++ = nrates;
   1996       1.1    dyoung 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
   1997       1.1    dyoung 		frm += nrates;
   1998       1.1    dyoung 	}
   1999       1.1    dyoung 	return frm;
   2000       1.1    dyoung }
   2001       1.1    dyoung 
   2002      1.29    dyoung /*
   2003  1.63.2.1      phil  * Add an ssid element to a frame.
   2004       1.1    dyoung  */
   2005  1.63.2.1      phil uint8_t *
   2006  1.63.2.1      phil ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
   2007       1.1    dyoung {
   2008       1.1    dyoung 	*frm++ = IEEE80211_ELEMID_SSID;
   2009       1.1    dyoung 	*frm++ = len;
   2010       1.1    dyoung 	memcpy(frm, ssid, len);
   2011       1.1    dyoung 	return frm + len;
   2012       1.1    dyoung }
   2013       1.1    dyoung 
   2014      1.29    dyoung /*
   2015      1.29    dyoung  * Add an erp element to a frame.
   2016      1.29    dyoung  */
   2017  1.63.2.1      phil static uint8_t *
   2018  1.63.2.1      phil ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
   2019      1.29    dyoung {
   2020  1.63.2.1      phil 	uint8_t erp;
   2021      1.29    dyoung 
   2022      1.29    dyoung 	*frm++ = IEEE80211_ELEMID_ERP;
   2023      1.29    dyoung 	*frm++ = 1;
   2024      1.29    dyoung 	erp = 0;
   2025      1.29    dyoung 	if (ic->ic_nonerpsta != 0)
   2026      1.29    dyoung 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
   2027      1.29    dyoung 	if (ic->ic_flags & IEEE80211_F_USEPROT)
   2028      1.29    dyoung 		erp |= IEEE80211_ERP_USE_PROTECTION;
   2029      1.29    dyoung 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
   2030      1.29    dyoung 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
   2031      1.29    dyoung 	*frm++ = erp;
   2032      1.29    dyoung 	return frm;
   2033      1.29    dyoung }
   2034      1.29    dyoung 
   2035  1.63.2.1      phil /*
   2036  1.63.2.1      phil  * Add a CFParams element to a frame.
   2037  1.63.2.1      phil  */
   2038  1.63.2.1      phil static uint8_t *
   2039  1.63.2.1      phil ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
   2040      1.29    dyoung {
   2041  1.63.2.1      phil #define	ADDSHORT(frm, v) do {	\
   2042  1.63.2.1      phil 	le16enc(frm, v);	\
   2043  1.63.2.1      phil 	frm += 2;		\
   2044      1.29    dyoung } while (0)
   2045  1.63.2.1      phil 	*frm++ = IEEE80211_ELEMID_CFPARMS;
   2046  1.63.2.1      phil 	*frm++ = 6;
   2047  1.63.2.1      phil 	*frm++ = 0;		/* CFP count */
   2048  1.63.2.1      phil 	*frm++ = 2;		/* CFP period */
   2049  1.63.2.1      phil 	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
   2050  1.63.2.1      phil 	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
   2051      1.29    dyoung 	return frm;
   2052      1.29    dyoung #undef ADDSHORT
   2053      1.29    dyoung }
   2054      1.29    dyoung 
   2055  1.63.2.1      phil static __inline uint8_t *
   2056  1.63.2.1      phil add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
   2057      1.29    dyoung {
   2058  1.63.2.1      phil 	memcpy(frm, ie->ie_data, ie->ie_len);
   2059  1.63.2.1      phil 	return frm + ie->ie_len;
   2060  1.63.2.1      phil }
   2061  1.63.2.1      phil 
   2062  1.63.2.1      phil static __inline uint8_t *
   2063  1.63.2.1      phil add_ie(uint8_t *frm, const uint8_t *ie)
   2064  1.63.2.1      phil {
   2065  1.63.2.1      phil 	memcpy(frm, ie, 2 + ie[1]);
   2066  1.63.2.1      phil 	return frm + 2 + ie[1];
   2067  1.63.2.1      phil }
   2068  1.63.2.1      phil 
   2069  1.63.2.1      phil #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
   2070  1.63.2.1      phil /*
   2071  1.63.2.1      phil  * Add a WME information element to a frame.
   2072  1.63.2.1      phil  */
   2073  1.63.2.1      phil uint8_t *
   2074  1.63.2.1      phil ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
   2075  1.63.2.1      phil {
   2076  1.63.2.1      phil 	static const struct ieee80211_wme_info info = {
   2077  1.63.2.1      phil 		.wme_id		= IEEE80211_ELEMID_VENDOR,
   2078  1.63.2.1      phil 		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
   2079  1.63.2.1      phil 		.wme_oui	= { WME_OUI_BYTES },
   2080  1.63.2.1      phil 		.wme_type	= WME_OUI_TYPE,
   2081  1.63.2.1      phil 		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
   2082  1.63.2.1      phil 		.wme_version	= WME_VERSION,
   2083  1.63.2.1      phil 		.wme_info	= 0,
   2084  1.63.2.1      phil 	};
   2085  1.63.2.1      phil 	memcpy(frm, &info, sizeof(info));
   2086  1.63.2.1      phil 	return frm + sizeof(info);
   2087  1.63.2.1      phil }
   2088  1.63.2.1      phil 
   2089  1.63.2.1      phil /*
   2090  1.63.2.1      phil  * Add a WME parameters element to a frame.
   2091  1.63.2.1      phil  */
   2092  1.63.2.1      phil static uint8_t *
   2093  1.63.2.1      phil ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
   2094  1.63.2.1      phil {
   2095  1.63.2.1      phil #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
   2096  1.63.2.1      phil #define	ADDSHORT(frm, v) do {	\
   2097  1.63.2.1      phil 	le16enc(frm, v);	\
   2098  1.63.2.1      phil 	frm += 2;		\
   2099      1.29    dyoung } while (0)
   2100  1.63.2.1      phil 	/* NB: this works 'cuz a param has an info at the front */
   2101  1.63.2.1      phil 	static const struct ieee80211_wme_info param = {
   2102  1.63.2.1      phil 		.wme_id		= IEEE80211_ELEMID_VENDOR,
   2103  1.63.2.1      phil 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
   2104  1.63.2.1      phil 		.wme_oui	= { WME_OUI_BYTES },
   2105  1.63.2.1      phil 		.wme_type	= WME_OUI_TYPE,
   2106  1.63.2.1      phil 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
   2107  1.63.2.1      phil 		.wme_version	= WME_VERSION,
   2108      1.29    dyoung 	};
   2109  1.63.2.1      phil 	int i;
   2110      1.29    dyoung 
   2111  1.63.2.1      phil 	memcpy(frm, &param, sizeof(param));
   2112  1.63.2.1      phil 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
   2113  1.63.2.1      phil 	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
   2114  1.63.2.1      phil 	*frm++ = 0;					/* reserved field */
   2115  1.63.2.1      phil 	for (i = 0; i < WME_NUM_AC; i++) {
   2116  1.63.2.1      phil 		const struct wmeParams *ac =
   2117  1.63.2.1      phil 		       &wme->wme_bssChanParams.cap_wmeParams[i];
   2118  1.63.2.1      phil 		*frm++ = SM(i, WME_PARAM_ACI)
   2119  1.63.2.1      phil 		       | SM(ac->wmep_acm, WME_PARAM_ACM)
   2120  1.63.2.1      phil 		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
   2121  1.63.2.1      phil 		       ;
   2122  1.63.2.1      phil 		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
   2123  1.63.2.1      phil 		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
   2124  1.63.2.1      phil 		       ;
   2125  1.63.2.1      phil 		ADDSHORT(frm, ac->wmep_txopLimit);
   2126  1.63.2.1      phil 	}
   2127      1.29    dyoung 	return frm;
   2128  1.63.2.1      phil #undef SM
   2129      1.29    dyoung #undef ADDSHORT
   2130  1.63.2.1      phil }
   2131  1.63.2.1      phil #undef WME_OUI_BYTES
   2132  1.63.2.1      phil 
   2133  1.63.2.1      phil /*
   2134  1.63.2.1      phil  * Add an 11h Power Constraint element to a frame.
   2135  1.63.2.1      phil  */
   2136  1.63.2.1      phil static uint8_t *
   2137  1.63.2.1      phil ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
   2138  1.63.2.1      phil {
   2139  1.63.2.1      phil 	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
   2140  1.63.2.1      phil 	/* XXX per-vap tx power limit? */
   2141  1.63.2.1      phil 	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
   2142  1.63.2.1      phil 
   2143  1.63.2.1      phil 	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
   2144  1.63.2.1      phil 	frm[1] = 1;
   2145  1.63.2.1      phil 	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
   2146  1.63.2.1      phil 	return frm + 3;
   2147  1.63.2.1      phil }
   2148  1.63.2.1      phil 
   2149  1.63.2.1      phil /*
   2150  1.63.2.1      phil  * Add an 11h Power Capability element to a frame.
   2151  1.63.2.1      phil  */
   2152  1.63.2.1      phil static uint8_t *
   2153  1.63.2.1      phil ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
   2154  1.63.2.1      phil {
   2155  1.63.2.1      phil 	frm[0] = IEEE80211_ELEMID_PWRCAP;
   2156  1.63.2.1      phil 	frm[1] = 2;
   2157  1.63.2.1      phil 	frm[2] = c->ic_minpower;
   2158  1.63.2.1      phil 	frm[3] = c->ic_maxpower;
   2159  1.63.2.1      phil 	return frm + 4;
   2160  1.63.2.1      phil }
   2161  1.63.2.1      phil 
   2162  1.63.2.1      phil /*
   2163  1.63.2.1      phil  * Add an 11h Supported Channels element to a frame.
   2164  1.63.2.1      phil  */
   2165  1.63.2.1      phil static uint8_t *
   2166  1.63.2.1      phil ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
   2167  1.63.2.1      phil {
   2168  1.63.2.1      phil 	static const int ielen = 26;
   2169  1.63.2.1      phil 
   2170  1.63.2.1      phil 	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
   2171  1.63.2.1      phil 	frm[1] = ielen;
   2172  1.63.2.1      phil 	/* XXX not correct */
   2173  1.63.2.1      phil 	memcpy(frm+2, ic->ic_chan_avail, ielen);
   2174  1.63.2.1      phil 	return frm + 2 + ielen;
   2175  1.63.2.1      phil }
   2176  1.63.2.1      phil 
   2177  1.63.2.1      phil /*
   2178  1.63.2.1      phil  * Add an 11h Quiet time element to a frame.
   2179  1.63.2.1      phil  */
   2180  1.63.2.1      phil static uint8_t *
   2181  1.63.2.1      phil ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap, int update)
   2182  1.63.2.1      phil {
   2183  1.63.2.1      phil 	struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
   2184  1.63.2.1      phil 
   2185  1.63.2.1      phil 	quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
   2186  1.63.2.1      phil 	quiet->len = 6;
   2187  1.63.2.1      phil 
   2188  1.63.2.1      phil 	/*
   2189  1.63.2.1      phil 	 * Only update every beacon interval - otherwise probe responses
   2190  1.63.2.1      phil 	 * would update the quiet count value.
   2191  1.63.2.1      phil 	 */
   2192  1.63.2.1      phil 	if (update) {
   2193  1.63.2.1      phil 		if (vap->iv_quiet_count_value == 1)
   2194  1.63.2.1      phil 			vap->iv_quiet_count_value = vap->iv_quiet_count;
   2195  1.63.2.1      phil 		else if (vap->iv_quiet_count_value > 1)
   2196  1.63.2.1      phil 			vap->iv_quiet_count_value--;
   2197  1.63.2.1      phil 	}
   2198  1.63.2.1      phil 
   2199  1.63.2.1      phil 	if (vap->iv_quiet_count_value == 0) {
   2200  1.63.2.1      phil 		/* value 0 is reserved as per 802.11h standerd */
   2201  1.63.2.1      phil 		vap->iv_quiet_count_value = 1;
   2202  1.63.2.1      phil 	}
   2203  1.63.2.1      phil 
   2204  1.63.2.1      phil 	quiet->tbttcount = vap->iv_quiet_count_value;
   2205  1.63.2.1      phil 	quiet->period = vap->iv_quiet_period;
   2206  1.63.2.1      phil 	quiet->duration = htole16(vap->iv_quiet_duration);
   2207  1.63.2.1      phil 	quiet->offset = htole16(vap->iv_quiet_offset);
   2208  1.63.2.1      phil 	return frm + sizeof(*quiet);
   2209      1.29    dyoung }
   2210      1.29    dyoung 
   2211      1.29    dyoung /*
   2212  1.63.2.1      phil  * Add an 11h Channel Switch Announcement element to a frame.
   2213  1.63.2.1      phil  * Note that we use the per-vap CSA count to adjust the global
   2214  1.63.2.1      phil  * counter so we can use this routine to form probe response
   2215  1.63.2.1      phil  * frames and get the current count.
   2216      1.29    dyoung  */
   2217  1.63.2.1      phil static uint8_t *
   2218  1.63.2.1      phil ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
   2219      1.29    dyoung {
   2220  1.63.2.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2221  1.63.2.1      phil 	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
   2222      1.29    dyoung 
   2223  1.63.2.1      phil 	csa->csa_ie = IEEE80211_ELEMID_CSA;
   2224  1.63.2.1      phil 	csa->csa_len = 3;
   2225  1.63.2.1      phil 	csa->csa_mode = 1;		/* XXX force quiet on channel */
   2226  1.63.2.1      phil 	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
   2227  1.63.2.1      phil 	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
   2228  1.63.2.1      phil 	return frm + sizeof(*csa);
   2229      1.29    dyoung }
   2230      1.29    dyoung 
   2231      1.29    dyoung /*
   2232  1.63.2.1      phil  * Add an 11h country information element to a frame.
   2233      1.29    dyoung  */
   2234  1.63.2.1      phil static uint8_t *
   2235  1.63.2.1      phil ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
   2236       1.1    dyoung {
   2237  1.63.2.1      phil 
   2238  1.63.2.1      phil 	if (ic->ic_countryie == NULL ||
   2239  1.63.2.1      phil 	    ic->ic_countryie_chan != ic->ic_bsschan) {
   2240  1.63.2.1      phil 		/*
   2241  1.63.2.1      phil 		 * Handle lazy construction of ie.  This is done on
   2242  1.63.2.1      phil 		 * first use and after a channel change that requires
   2243  1.63.2.1      phil 		 * re-calculation.
   2244  1.63.2.1      phil 		 */
   2245  1.63.2.1      phil 		if (ic->ic_countryie != NULL)
   2246  1.63.2.1      phil 			IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
   2247  1.63.2.1      phil 		ic->ic_countryie = ieee80211_alloc_countryie(ic);
   2248  1.63.2.1      phil 		if (ic->ic_countryie == NULL)
   2249  1.63.2.1      phil 			return frm;
   2250  1.63.2.1      phil 		ic->ic_countryie_chan = ic->ic_bsschan;
   2251  1.63.2.1      phil 	}
   2252  1.63.2.1      phil 	return add_appie(frm, ic->ic_countryie);
   2253      1.29    dyoung }
   2254       1.1    dyoung 
   2255  1.63.2.1      phil uint8_t *
   2256  1.63.2.1      phil ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
   2257      1.29    dyoung {
   2258  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
   2259  1.63.2.1      phil 		return (add_ie(frm, vap->iv_wpa_ie));
   2260  1.63.2.1      phil 	else {
   2261  1.63.2.1      phil 		/* XXX else complain? */
   2262  1.63.2.1      phil 		return (frm);
   2263  1.63.2.1      phil 	}
   2264  1.63.2.1      phil }
   2265      1.29    dyoung 
   2266  1.63.2.1      phil uint8_t *
   2267  1.63.2.1      phil ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
   2268  1.63.2.1      phil {
   2269  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
   2270  1.63.2.1      phil 		return (add_ie(frm, vap->iv_rsn_ie));
   2271  1.63.2.1      phil 	else {
   2272  1.63.2.1      phil 		/* XXX else complain? */
   2273  1.63.2.1      phil 		return (frm);
   2274      1.29    dyoung 	}
   2275  1.63.2.1      phil }
   2276      1.61      maxv 
   2277  1.63.2.1      phil uint8_t *
   2278  1.63.2.1      phil ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
   2279  1.63.2.1      phil {
   2280  1.63.2.1      phil 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
   2281  1.63.2.1      phil 		*frm++ = IEEE80211_ELEMID_QOS;
   2282  1.63.2.1      phil 		*frm++ = 1;
   2283  1.63.2.1      phil 		*frm++ = 0;
   2284  1.63.2.1      phil 	}
   2285  1.63.2.1      phil 
   2286  1.63.2.1      phil 	return (frm);
   2287       1.1    dyoung }
   2288       1.1    dyoung 
   2289       1.1    dyoung /*
   2290      1.39     skrll  * Send a probe request frame with the specified ssid
   2291      1.39     skrll  * and any optional information element data.
   2292      1.39     skrll  */
   2293      1.39     skrll int
   2294      1.39     skrll ieee80211_send_probereq(struct ieee80211_node *ni,
   2295  1.63.2.1      phil 	const uint8_t sa[IEEE80211_ADDR_LEN],
   2296  1.63.2.1      phil 	const uint8_t da[IEEE80211_ADDR_LEN],
   2297  1.63.2.1      phil 	const uint8_t bssid[IEEE80211_ADDR_LEN],
   2298  1.63.2.1      phil 	const uint8_t *ssid, size_t ssidlen)
   2299      1.39     skrll {
   2300  1.63.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   2301      1.39     skrll 	struct ieee80211com *ic = ni->ni_ic;
   2302  1.63.2.1      phil 	struct ieee80211_node *bss;
   2303  1.63.2.1      phil 	const struct ieee80211_txparam *tp;
   2304  1.63.2.1      phil 	struct ieee80211_bpf_params params;
   2305  1.63.2.1      phil 	const struct ieee80211_rateset *rs;
   2306      1.39     skrll 	struct mbuf *m;
   2307  1.63.2.1      phil 	uint8_t *frm;
   2308  1.63.2.1      phil 	int ret;
   2309  1.63.2.1      phil 
   2310  1.63.2.1      phil 	bss = ieee80211_ref_node(vap->iv_bss);
   2311  1.63.2.1      phil 
   2312  1.63.2.1      phil 	if (vap->iv_state == IEEE80211_S_CAC) {
   2313  1.63.2.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
   2314  1.63.2.1      phil 		    "block %s frame in CAC state", "probe request");
   2315  1.63.2.1      phil 		vap->iv_stats.is_tx_badstate++;
   2316  1.63.2.1      phil 		ieee80211_free_node(bss);
   2317  1.63.2.1      phil 		return EIO;		/* XXX */
   2318  1.63.2.1      phil 	}
   2319      1.39     skrll 
   2320      1.39     skrll 	/*
   2321      1.39     skrll 	 * Hold a reference on the node so it doesn't go away until after
   2322      1.39     skrll 	 * the xmit is complete all the way in the driver.  On error we
   2323      1.39     skrll 	 * will remove our reference.
   2324      1.39     skrll 	 */
   2325  1.63.2.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
   2326      1.39     skrll 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
   2327      1.39     skrll 		__func__, __LINE__,
   2328      1.39     skrll 		ni, ether_sprintf(ni->ni_macaddr),
   2329      1.39     skrll 		ieee80211_node_refcnt(ni)+1);
   2330      1.39     skrll 	ieee80211_ref_node(ni);
   2331      1.39     skrll 
   2332      1.39     skrll 	/*
   2333      1.39     skrll 	 * prreq frame format
   2334      1.39     skrll 	 *	[tlv] ssid
   2335      1.39     skrll 	 *	[tlv] supported rates
   2336  1.63.2.1      phil 	 *	[tlv] RSN (optional)
   2337      1.39     skrll 	 *	[tlv] extended supported rates
   2338  1.63.2.1      phil 	 *	[tlv] HT cap (optional)
   2339  1.63.2.1      phil 	 *	[tlv] VHT cap (optional)
   2340  1.63.2.1      phil 	 *	[tlv] WPA (optional)
   2341      1.39     skrll 	 *	[tlv] user-specified ie's
   2342      1.39     skrll 	 */
   2343      1.39     skrll 	m = ieee80211_getmgtframe(&frm,
   2344  1.63.2.1      phil 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
   2345  1.63.2.1      phil 	       	 2 + IEEE80211_NWID_LEN
   2346      1.39     skrll 	       + 2 + IEEE80211_RATE_SIZE
   2347  1.63.2.1      phil 	       + sizeof(struct ieee80211_ie_htcap)
   2348  1.63.2.1      phil 	       + sizeof(struct ieee80211_ie_vhtcap)
   2349  1.63.2.1      phil 	       + sizeof(struct ieee80211_ie_htinfo)	/* XXX not needed? */
   2350  1.63.2.1      phil 	       + sizeof(struct ieee80211_ie_wpa)
   2351      1.39     skrll 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
   2352  1.63.2.1      phil 	       + sizeof(struct ieee80211_ie_wpa)
   2353  1.63.2.1      phil 	       + (vap->iv_appie_probereq != NULL ?
   2354  1.63.2.1      phil 		   vap->iv_appie_probereq->ie_len : 0)
   2355      1.39     skrll 	);
   2356      1.39     skrll 	if (m == NULL) {
   2357  1.63.2.1      phil 		vap->iv_stats.is_tx_nobuf++;
   2358      1.39     skrll 		ieee80211_free_node(ni);
   2359  1.63.2.1      phil 		ieee80211_free_node(bss);
   2360      1.39     skrll 		return ENOMEM;
   2361      1.39     skrll 	}
   2362      1.39     skrll 
   2363      1.39     skrll 	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
   2364  1.63.2.1      phil 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
   2365  1.63.2.1      phil 	frm = ieee80211_add_rates(frm, rs);
   2366  1.63.2.1      phil 	frm = ieee80211_add_rsn(frm, vap);
   2367  1.63.2.1      phil 	frm = ieee80211_add_xrates(frm, rs);
   2368  1.63.2.1      phil 
   2369  1.63.2.1      phil 	/*
   2370  1.63.2.1      phil 	 * Note: we can't use bss; we don't have one yet.
   2371  1.63.2.1      phil 	 *
   2372  1.63.2.1      phil 	 * So, we should announce our capabilities
   2373  1.63.2.1      phil 	 * in this channel mode (2g/5g), not the
   2374  1.63.2.1      phil 	 * channel details itself.
   2375  1.63.2.1      phil 	 */
   2376  1.63.2.1      phil 	if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
   2377  1.63.2.1      phil 	    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
   2378  1.63.2.1      phil 		struct ieee80211_channel *c;
   2379  1.63.2.1      phil 
   2380  1.63.2.1      phil 		/*
   2381  1.63.2.1      phil 		 * Get the HT channel that we should try upgrading to.
   2382  1.63.2.1      phil 		 * If we can do 40MHz then this'll upgrade it appropriately.
   2383  1.63.2.1      phil 		 */
   2384  1.63.2.1      phil 		c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
   2385  1.63.2.1      phil 		    vap->iv_flags_ht);
   2386  1.63.2.1      phil 		frm = ieee80211_add_htcap_ch(frm, vap, c);
   2387  1.63.2.1      phil 	}
   2388      1.39     skrll 
   2389  1.63.2.1      phil 	/*
   2390  1.63.2.1      phil 	 * XXX TODO: need to figure out what/how to update the
   2391  1.63.2.1      phil 	 * VHT channel.
   2392  1.63.2.1      phil 	 */
   2393  1.63.2.1      phil #if 0
   2394  1.63.2.1      phil 	(vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
   2395  1.63.2.1      phil 		struct ieee80211_channel *c;
   2396  1.63.2.1      phil 
   2397  1.63.2.1      phil 		c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
   2398  1.63.2.1      phil 		    vap->iv_flags_ht);
   2399  1.63.2.1      phil 		c = ieee80211_vht_adjust_channel(ic, c, vap->iv_flags_vht);
   2400  1.63.2.1      phil 		frm = ieee80211_add_vhtcap_ch(frm, vap, c);
   2401      1.39     skrll 	}
   2402  1.63.2.1      phil #endif
   2403      1.39     skrll 
   2404  1.63.2.1      phil 	frm = ieee80211_add_wpa(frm, vap);
   2405  1.63.2.1      phil 	if (vap->iv_appie_probereq != NULL)
   2406  1.63.2.1      phil 		frm = add_appie(frm, vap->iv_appie_probereq);
   2407  1.63.2.1      phil 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
   2408  1.63.2.1      phil 
   2409  1.63.2.1      phil 	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
   2410  1.63.2.1      phil 	    ("leading space %zd", M_LEADINGSPACE(m)));
   2411  1.63.2.1      phil 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
   2412      1.60      maxv 	if (m == NULL) {
   2413  1.63.2.1      phil 		/* NB: cannot happen */
   2414      1.60      maxv 		ieee80211_free_node(ni);
   2415  1.63.2.1      phil 		ieee80211_free_node(bss);
   2416      1.39     skrll 		return ENOMEM;
   2417      1.60      maxv 	}
   2418      1.39     skrll 
   2419  1.63.2.1      phil 	IEEE80211_TX_LOCK(ic);
   2420  1.63.2.1      phil 	ieee80211_send_setup(ni, m,
   2421  1.63.2.1      phil 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
   2422  1.63.2.1      phil 	     IEEE80211_NONQOS_TID, sa, da, bssid);
   2423      1.39     skrll 	/* XXX power management? */
   2424  1.63.2.1      phil 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
   2425  1.63.2.1      phil 
   2426  1.63.2.1      phil 	M_WME_SETAC(m, WME_AC_BE);
   2427      1.39     skrll 
   2428      1.39     skrll 	IEEE80211_NODE_STAT(ni, tx_probereq);
   2429      1.39     skrll 	IEEE80211_NODE_STAT(ni, tx_mgmt);
   2430      1.39     skrll 
   2431  1.63.2.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
   2432  1.63.2.1      phil 	    "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n",
   2433  1.63.2.1      phil 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
   2434  1.63.2.1      phil 	    ether_sprintf(bssid),
   2435  1.63.2.1      phil 	    sa, ":",
   2436  1.63.2.1      phil 	    da, ":",
   2437  1.63.2.1      phil 	    ssidlen, ssid);
   2438  1.63.2.1      phil 
   2439  1.63.2.1      phil 	memset(&params, 0, sizeof(params));
   2440  1.63.2.1      phil 	params.ibp_pri = M_WME_GETAC(m);
   2441  1.63.2.1      phil 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
   2442  1.63.2.1      phil 	params.ibp_rate0 = tp->mgmtrate;
   2443  1.63.2.1      phil 	if (IEEE80211_IS_MULTICAST(da)) {
   2444  1.63.2.1      phil 		params.ibp_flags |= IEEE80211_BPF_NOACK;
   2445  1.63.2.1      phil 		params.ibp_try0 = 1;
   2446  1.63.2.1      phil 	} else
   2447  1.63.2.1      phil 		params.ibp_try0 = tp->maxretry;
   2448  1.63.2.1      phil 	params.ibp_power = ni->ni_txpower;
   2449  1.63.2.1      phil 	ret = ieee80211_raw_output(vap, ni, m, &params);
   2450  1.63.2.1      phil 	IEEE80211_TX_UNLOCK(ic);
   2451  1.63.2.1      phil 	ieee80211_free_node(bss);
   2452  1.63.2.1      phil 	return (ret);
   2453  1.63.2.1      phil }
   2454  1.63.2.1      phil 
   2455  1.63.2.1      phil /*
   2456  1.63.2.1      phil  * Calculate capability information for mgt frames.
   2457  1.63.2.1      phil  */
   2458  1.63.2.1      phil uint16_t
   2459  1.63.2.1      phil ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
   2460  1.63.2.1      phil {
   2461  1.63.2.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2462  1.63.2.1      phil 	uint16_t capinfo;
   2463  1.63.2.1      phil 
   2464  1.63.2.1      phil 	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
   2465      1.39     skrll 
   2466  1.63.2.1      phil 	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
   2467  1.63.2.1      phil 		capinfo = IEEE80211_CAPINFO_ESS;
   2468  1.63.2.1      phil 	else if (vap->iv_opmode == IEEE80211_M_IBSS)
   2469  1.63.2.1      phil 		capinfo = IEEE80211_CAPINFO_IBSS;
   2470  1.63.2.1      phil 	else
   2471  1.63.2.1      phil 		capinfo = 0;
   2472  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
   2473  1.63.2.1      phil 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
   2474  1.63.2.1      phil 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
   2475  1.63.2.1      phil 	    IEEE80211_IS_CHAN_2GHZ(chan))
   2476  1.63.2.1      phil 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
   2477  1.63.2.1      phil 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   2478  1.63.2.1      phil 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
   2479  1.63.2.1      phil 	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
   2480  1.63.2.1      phil 		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
   2481  1.63.2.1      phil 	return capinfo;
   2482      1.39     skrll }
   2483      1.39     skrll 
   2484      1.39     skrll /*
   2485       1.1    dyoung  * Send a management frame.  The node is for the destination (or ic_bss
   2486       1.1    dyoung  * when in station mode).  Nodes other than ic_bss have their reference
   2487       1.1    dyoung  * count bumped to reflect our use for an indeterminant time.
   2488       1.1    dyoung  */
   2489       1.1    dyoung int
   2490  1.63.2.1      phil ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
   2491       1.1    dyoung {
   2492  1.63.2.1      phil #define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
   2493  1.63.2.1      phil #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
   2494  1.63.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   2495  1.63.2.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   2496  1.63.2.1      phil 	struct ieee80211_node *bss = vap->iv_bss;
   2497  1.63.2.1      phil 	struct ieee80211_bpf_params params;
   2498       1.1    dyoung 	struct mbuf *m;
   2499  1.63.2.1      phil 	uint8_t *frm;
   2500  1.63.2.1      phil 	uint16_t capinfo;
   2501  1.63.2.1      phil 	int has_challenge, is_shared_key, ret, status;
   2502       1.1    dyoung 
   2503  1.63.2.1      phil 	KASSERT(ni != NULL, ("null node"));
   2504       1.1    dyoung 
   2505       1.1    dyoung 	/*
   2506       1.1    dyoung 	 * Hold a reference on the node so it doesn't go away until after
   2507       1.1    dyoung 	 * the xmit is complete all the way in the driver.  On error we
   2508       1.1    dyoung 	 * will remove our reference.
   2509       1.1    dyoung 	 */
   2510  1.63.2.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
   2511      1.29    dyoung 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
   2512      1.29    dyoung 		__func__, __LINE__,
   2513      1.29    dyoung 		ni, ether_sprintf(ni->ni_macaddr),
   2514      1.29    dyoung 		ieee80211_node_refcnt(ni)+1);
   2515      1.17    dyoung 	ieee80211_ref_node(ni);
   2516      1.29    dyoung 
   2517  1.63.2.1      phil 	memset(&params, 0, sizeof(params));
   2518       1.1    dyoung 	switch (type) {
   2519       1.1    dyoung 
   2520  1.63.2.1      phil 	case IEEE80211_FC0_SUBTYPE_AUTH:
   2521      1.29    dyoung 		status = arg >> 16;
   2522      1.29    dyoung 		arg &= 0xffff;
   2523  1.63.2.1      phil 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
   2524  1.63.2.1      phil 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
   2525  1.63.2.1      phil 		    ni->ni_challenge != NULL);
   2526       1.7    dyoung 
   2527      1.29    dyoung 		/*
   2528      1.29    dyoung 		 * Deduce whether we're doing open authentication or
   2529      1.29    dyoung 		 * shared key authentication.  We do the latter if
   2530      1.29    dyoung 		 * we're in the middle of a shared key authentication
   2531      1.29    dyoung 		 * handshake or if we're initiating an authentication
   2532      1.29    dyoung 		 * request and configured to use shared key.
   2533      1.29    dyoung 		 */
   2534  1.63.2.1      phil 		is_shared_key = has_challenge ||
   2535  1.63.2.1      phil 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
   2536  1.63.2.1      phil 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
   2537  1.63.2.1      phil 		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
   2538      1.29    dyoung 
   2539  1.63.2.1      phil 		m = ieee80211_getmgtframe(&frm,
   2540  1.63.2.1      phil 			  ic->ic_headroom + sizeof(struct ieee80211_frame),
   2541  1.63.2.1      phil 			  3 * sizeof(uint16_t)
   2542  1.63.2.1      phil 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
   2543  1.63.2.1      phil 				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
   2544  1.63.2.1      phil 		);
   2545      1.29    dyoung 		if (m == NULL)
   2546      1.29    dyoung 			senderr(ENOMEM, is_tx_nobuf);
   2547       1.7    dyoung 
   2548  1.63.2.1      phil 		((uint16_t *)frm)[0] =
   2549  1.63.2.1      phil 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
   2550       1.7    dyoung 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
   2551  1.63.2.1      phil 		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
   2552  1.63.2.1      phil 		((uint16_t *)frm)[2] = htole16(status);/* status */
   2553       1.7    dyoung 
   2554  1.63.2.1      phil 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
   2555  1.63.2.1      phil 			((uint16_t *)frm)[3] =
   2556       1.7    dyoung 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
   2557       1.7    dyoung 			    IEEE80211_ELEMID_CHALLENGE);
   2558  1.63.2.1      phil 			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
   2559       1.7    dyoung 			    IEEE80211_CHALLENGE_LEN);
   2560  1.63.2.1      phil 			m->m_pkthdr.len = m->m_len =
   2561  1.63.2.1      phil 				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
   2562       1.7    dyoung 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
   2563  1.63.2.1      phil 				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
   2564  1.63.2.1      phil 				    "request encrypt frame (%s)", __func__);
   2565  1.63.2.1      phil 				/* mark frame for encryption */
   2566  1.63.2.1      phil 				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
   2567       1.7    dyoung 			}
   2568  1.63.2.1      phil 		} else
   2569  1.63.2.1      phil 			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
   2570      1.29    dyoung 
   2571      1.29    dyoung 		/* XXX not right for shared key */
   2572      1.29    dyoung 		if (status == IEEE80211_STATUS_SUCCESS)
   2573      1.29    dyoung 			IEEE80211_NODE_STAT(ni, tx_auth);
   2574      1.29    dyoung 		else
   2575      1.29    dyoung 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
   2576      1.29    dyoung 
   2577  1.63.2.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA)
   2578  1.63.2.1      phil 			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
   2579  1.63.2.1      phil 				(void *) vap->iv_state);
   2580       1.1    dyoung 		break;
   2581       1.1    dyoung 
   2582       1.1    dyoung 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
   2583  1.63.2.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
   2584  1.63.2.1      phil 		    "send station deauthenticate (reason: %d (%s))", arg,
   2585  1.63.2.1      phil 		    ieee80211_reason_to_string(arg));
   2586  1.63.2.1      phil 		m = ieee80211_getmgtframe(&frm,
   2587  1.63.2.1      phil 			ic->ic_headroom + sizeof(struct ieee80211_frame),
   2588  1.63.2.1      phil 			sizeof(uint16_t));
   2589       1.1    dyoung 		if (m == NULL)
   2590      1.29    dyoung 			senderr(ENOMEM, is_tx_nobuf);
   2591  1.63.2.1      phil 		*(uint16_t *)frm = htole16(arg);	/* reason */
   2592  1.63.2.1      phil 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
   2593      1.29    dyoung 
   2594      1.29    dyoung 		IEEE80211_NODE_STAT(ni, tx_deauth);
   2595      1.29    dyoung 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
   2596      1.29    dyoung 
   2597      1.39     skrll 		ieee80211_node_unauthorize(ni);		/* port closed */
   2598       1.1    dyoung 		break;
   2599       1.1    dyoung 
   2600       1.1    dyoung 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
   2601       1.1    dyoung 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
   2602       1.1    dyoung 		/*
   2603       1.1    dyoung 		 * asreq frame format
   2604       1.1    dyoung 		 *	[2] capability information
   2605       1.1    dyoung 		 *	[2] listen interval
   2606       1.1    dyoung 		 *	[6*] current AP address (reassoc only)
   2607       1.1    dyoung 		 *	[tlv] ssid
   2608       1.1    dyoung 		 *	[tlv] supported rates
   2609       1.1    dyoung 		 *	[tlv] extended supported rates
   2610  1.63.2.1      phil 		 *	[4] power capability (optional)
   2611  1.63.2.1      phil 		 *	[28] supported channels (optional)
   2612  1.63.2.1      phil 		 *	[tlv] HT capabilities
   2613  1.63.2.1      phil 		 *	[tlv] VHT capabilities
   2614  1.63.2.1      phil 		 *	[tlv] WME (optional)
   2615  1.63.2.1      phil 		 *	[tlv] Vendor OUI HT capabilities (optional)
   2616  1.63.2.1      phil 		 *	[tlv] Atheros capabilities (if negotiated)
   2617  1.63.2.1      phil 		 *	[tlv] AppIE's (optional)
   2618       1.1    dyoung 		 */
   2619      1.29    dyoung 		m = ieee80211_getmgtframe(&frm,
   2620  1.63.2.1      phil 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
   2621  1.63.2.1      phil 			 sizeof(uint16_t)
   2622  1.63.2.1      phil 		       + sizeof(uint16_t)
   2623       1.1    dyoung 		       + IEEE80211_ADDR_LEN
   2624      1.29    dyoung 		       + 2 + IEEE80211_NWID_LEN
   2625       1.1    dyoung 		       + 2 + IEEE80211_RATE_SIZE
   2626      1.29    dyoung 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
   2627  1.63.2.1      phil 		       + 4
   2628  1.63.2.1      phil 		       + 2 + 26
   2629      1.29    dyoung 		       + sizeof(struct ieee80211_wme_info)
   2630  1.63.2.1      phil 		       + sizeof(struct ieee80211_ie_htcap)
   2631  1.63.2.1      phil 		       + sizeof(struct ieee80211_ie_vhtcap)
   2632  1.63.2.1      phil 		       + 4 + sizeof(struct ieee80211_ie_htcap)
   2633  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   2634  1.63.2.1      phil 		       + sizeof(struct ieee80211_ath_ie)
   2635  1.63.2.1      phil #endif
   2636  1.63.2.1      phil 		       + (vap->iv_appie_wpa != NULL ?
   2637  1.63.2.1      phil 				vap->iv_appie_wpa->ie_len : 0)
   2638  1.63.2.1      phil 		       + (vap->iv_appie_assocreq != NULL ?
   2639  1.63.2.1      phil 				vap->iv_appie_assocreq->ie_len : 0)
   2640      1.29    dyoung 		);
   2641       1.1    dyoung 		if (m == NULL)
   2642      1.29    dyoung 			senderr(ENOMEM, is_tx_nobuf);
   2643       1.1    dyoung 
   2644  1.63.2.1      phil 		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
   2645  1.63.2.1      phil 		    ("wrong mode %u", vap->iv_opmode));
   2646  1.63.2.1      phil 		capinfo = IEEE80211_CAPINFO_ESS;
   2647  1.63.2.1      phil 		if (vap->iv_flags & IEEE80211_F_PRIVACY)
   2648       1.1    dyoung 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
   2649      1.10    dyoung 		/*
   2650      1.10    dyoung 		 * NB: Some 11a AP's reject the request when
   2651  1.63.2.1      phil 		 *     short preamble is set.
   2652      1.10    dyoung 		 */
   2653      1.10    dyoung 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
   2654      1.39     skrll 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
   2655       1.1    dyoung 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
   2656  1.63.2.1      phil 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
   2657      1.29    dyoung 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
   2658       1.1    dyoung 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
   2659  1.63.2.1      phil 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
   2660  1.63.2.1      phil 		    (vap->iv_flags & IEEE80211_F_DOTH))
   2661  1.63.2.1      phil 			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
   2662  1.63.2.1      phil 		*(uint16_t *)frm = htole16(capinfo);
   2663       1.1    dyoung 		frm += 2;
   2664       1.1    dyoung 
   2665  1.63.2.1      phil 		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
   2666  1.63.2.1      phil 		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
   2667  1.63.2.1      phil 						    bss->ni_intval));
   2668       1.1    dyoung 		frm += 2;
   2669       1.1    dyoung 
   2670       1.1    dyoung 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
   2671  1.63.2.1      phil 			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
   2672       1.1    dyoung 			frm += IEEE80211_ADDR_LEN;
   2673       1.1    dyoung 		}
   2674       1.1    dyoung 
   2675       1.1    dyoung 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
   2676       1.1    dyoung 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
   2677  1.63.2.1      phil 		frm = ieee80211_add_rsn(frm, vap);
   2678       1.1    dyoung 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
   2679  1.63.2.1      phil 		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
   2680  1.63.2.1      phil 			frm = ieee80211_add_powercapability(frm,
   2681  1.63.2.1      phil 			    ic->ic_curchan);
   2682  1.63.2.1      phil 			frm = ieee80211_add_supportedchannels(frm, ic);
   2683  1.63.2.1      phil 		}
   2684  1.63.2.1      phil 
   2685  1.63.2.1      phil 		/*
   2686  1.63.2.1      phil 		 * Check the channel - we may be using an 11n NIC with an
   2687  1.63.2.1      phil 		 * 11n capable station, but we're configured to be an 11b
   2688  1.63.2.1      phil 		 * channel.
   2689  1.63.2.1      phil 		 */
   2690  1.63.2.1      phil 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
   2691  1.63.2.1      phil 		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
   2692  1.63.2.1      phil 		    ni->ni_ies.htcap_ie != NULL &&
   2693  1.63.2.1      phil 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
   2694  1.63.2.1      phil 			frm = ieee80211_add_htcap(frm, ni);
   2695  1.63.2.1      phil 		}
   2696  1.63.2.1      phil 
   2697  1.63.2.1      phil 		if ((vap->iv_flags_vht & IEEE80211_FVHT_VHT) &&
   2698  1.63.2.1      phil 		    IEEE80211_IS_CHAN_VHT(ni->ni_chan) &&
   2699  1.63.2.1      phil 		    ni->ni_ies.vhtcap_ie != NULL &&
   2700  1.63.2.1      phil 		    ni->ni_ies.vhtcap_ie[0] == IEEE80211_ELEMID_VHT_CAP) {
   2701  1.63.2.1      phil 			frm = ieee80211_add_vhtcap(frm, ni);
   2702  1.63.2.1      phil 		}
   2703  1.63.2.1      phil 
   2704  1.63.2.1      phil 		frm = ieee80211_add_wpa(frm, vap);
   2705  1.63.2.1      phil 		if ((ic->ic_flags & IEEE80211_F_WME) &&
   2706  1.63.2.1      phil 		    ni->ni_ies.wme_ie != NULL)
   2707      1.29    dyoung 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
   2708  1.63.2.1      phil 
   2709  1.63.2.1      phil 		/*
   2710  1.63.2.1      phil 		 * Same deal - only send HT info if we're on an 11n
   2711  1.63.2.1      phil 		 * capable channel.
   2712  1.63.2.1      phil 		 */
   2713  1.63.2.1      phil 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
   2714  1.63.2.1      phil 		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
   2715  1.63.2.1      phil 		    ni->ni_ies.htcap_ie != NULL &&
   2716  1.63.2.1      phil 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
   2717  1.63.2.1      phil 			frm = ieee80211_add_htcap_vendor(frm, ni);
   2718      1.29    dyoung 		}
   2719  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   2720  1.63.2.1      phil 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
   2721  1.63.2.1      phil 			frm = ieee80211_add_ath(frm,
   2722  1.63.2.1      phil 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
   2723  1.63.2.1      phil 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
   2724  1.63.2.1      phil 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
   2725  1.63.2.1      phil 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
   2726  1.63.2.1      phil 		}
   2727  1.63.2.1      phil #endif /* IEEE80211_SUPPORT_SUPERG */
   2728  1.63.2.1      phil 		if (vap->iv_appie_assocreq != NULL)
   2729  1.63.2.1      phil 			frm = add_appie(frm, vap->iv_appie_assocreq);
   2730  1.63.2.1      phil 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
   2731       1.1    dyoung 
   2732  1.63.2.1      phil 		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
   2733  1.63.2.1      phil 			(void *) vap->iv_state);
   2734       1.1    dyoung 		break;
   2735       1.1    dyoung 
   2736       1.1    dyoung 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
   2737       1.1    dyoung 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
   2738       1.1    dyoung 		/*
   2739  1.63.2.1      phil 		 * asresp frame format
   2740       1.1    dyoung 		 *	[2] capability information
   2741       1.1    dyoung 		 *	[2] status
   2742       1.1    dyoung 		 *	[2] association ID
   2743       1.1    dyoung 		 *	[tlv] supported rates
   2744       1.1    dyoung 		 *	[tlv] extended supported rates
   2745  1.63.2.1      phil 		 *	[tlv] HT capabilities (standard, if STA enabled)
   2746  1.63.2.1      phil 		 *	[tlv] HT information (standard, if STA enabled)
   2747  1.63.2.1      phil 		 *	[tlv] VHT capabilities (standard, if STA enabled)
   2748  1.63.2.1      phil 		 *	[tlv] VHT information (standard, if STA enabled)
   2749  1.63.2.1      phil 		 *	[tlv] WME (if configured and STA enabled)
   2750  1.63.2.1      phil 		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
   2751  1.63.2.1      phil 		 *	[tlv] HT information (vendor OUI, if STA enabled)
   2752  1.63.2.1      phil 		 *	[tlv] Atheros capabilities (if STA enabled)
   2753  1.63.2.1      phil 		 *	[tlv] AppIE's (optional)
   2754       1.1    dyoung 		 */
   2755      1.29    dyoung 		m = ieee80211_getmgtframe(&frm,
   2756  1.63.2.1      phil 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
   2757  1.63.2.1      phil 			 sizeof(uint16_t)
   2758  1.63.2.1      phil 		       + sizeof(uint16_t)
   2759  1.63.2.1      phil 		       + sizeof(uint16_t)
   2760       1.1    dyoung 		       + 2 + IEEE80211_RATE_SIZE
   2761      1.29    dyoung 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
   2762  1.63.2.1      phil 		       + sizeof(struct ieee80211_ie_htcap) + 4
   2763  1.63.2.1      phil 		       + sizeof(struct ieee80211_ie_htinfo) + 4
   2764  1.63.2.1      phil 		       + sizeof(struct ieee80211_ie_vhtcap)
   2765  1.63.2.1      phil 		       + sizeof(struct ieee80211_ie_vht_operation)
   2766      1.29    dyoung 		       + sizeof(struct ieee80211_wme_param)
   2767  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   2768  1.63.2.1      phil 		       + sizeof(struct ieee80211_ath_ie)
   2769  1.63.2.1      phil #endif
   2770  1.63.2.1      phil 		       + (vap->iv_appie_assocresp != NULL ?
   2771  1.63.2.1      phil 				vap->iv_appie_assocresp->ie_len : 0)
   2772      1.29    dyoung 		);
   2773       1.1    dyoung 		if (m == NULL)
   2774      1.29    dyoung 			senderr(ENOMEM, is_tx_nobuf);
   2775       1.1    dyoung 
   2776  1.63.2.1      phil 		capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
   2777  1.63.2.1      phil 		*(uint16_t *)frm = htole16(capinfo);
   2778       1.1    dyoung 		frm += 2;
   2779       1.1    dyoung 
   2780  1.63.2.1      phil 		*(uint16_t *)frm = htole16(arg);	/* status */
   2781       1.1    dyoung 		frm += 2;
   2782       1.1    dyoung 
   2783      1.29    dyoung 		if (arg == IEEE80211_STATUS_SUCCESS) {
   2784  1.63.2.1      phil 			*(uint16_t *)frm = htole16(ni->ni_associd);
   2785      1.29    dyoung 			IEEE80211_NODE_STAT(ni, tx_assoc);
   2786  1.63.2.1      phil 		} else
   2787      1.29    dyoung 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
   2788       1.1    dyoung 		frm += 2;
   2789       1.1    dyoung 
   2790       1.1    dyoung 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
   2791       1.1    dyoung 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
   2792  1.63.2.1      phil 		/* NB: respond according to what we received */
   2793  1.63.2.1      phil 		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
   2794  1.63.2.1      phil 			frm = ieee80211_add_htcap(frm, ni);
   2795  1.63.2.1      phil 			frm = ieee80211_add_htinfo(frm, ni);
   2796  1.63.2.1      phil 		}
   2797  1.63.2.1      phil 		if ((vap->iv_flags & IEEE80211_F_WME) &&
   2798  1.63.2.1      phil 		    ni->ni_ies.wme_ie != NULL)
   2799      1.29    dyoung 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
   2800  1.63.2.1      phil 		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
   2801  1.63.2.1      phil 			frm = ieee80211_add_htcap_vendor(frm, ni);
   2802  1.63.2.1      phil 			frm = ieee80211_add_htinfo_vendor(frm, ni);
   2803  1.63.2.1      phil 		}
   2804  1.63.2.1      phil 		if (ni->ni_flags & IEEE80211_NODE_VHT) {
   2805  1.63.2.1      phil 			frm = ieee80211_add_vhtcap(frm, ni);
   2806  1.63.2.1      phil 			frm = ieee80211_add_vhtinfo(frm, ni);
   2807  1.63.2.1      phil 		}
   2808  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   2809  1.63.2.1      phil 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
   2810  1.63.2.1      phil 			frm = ieee80211_add_ath(frm,
   2811  1.63.2.1      phil 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
   2812  1.63.2.1      phil 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
   2813  1.63.2.1      phil 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
   2814  1.63.2.1      phil 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
   2815  1.63.2.1      phil #endif /* IEEE80211_SUPPORT_SUPERG */
   2816  1.63.2.1      phil 		if (vap->iv_appie_assocresp != NULL)
   2817  1.63.2.1      phil 			frm = add_appie(frm, vap->iv_appie_assocresp);
   2818  1.63.2.1      phil 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
   2819  1.63.2.1      phil 		break;
   2820  1.63.2.1      phil 
   2821  1.63.2.1      phil 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
   2822  1.63.2.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
   2823  1.63.2.1      phil 		    "send station disassociate (reason: %d (%s))", arg,
   2824  1.63.2.1      phil 		    ieee80211_reason_to_string(arg));
   2825  1.63.2.1      phil 		m = ieee80211_getmgtframe(&frm,
   2826  1.63.2.1      phil 			ic->ic_headroom + sizeof(struct ieee80211_frame),
   2827  1.63.2.1      phil 			sizeof(uint16_t));
   2828  1.63.2.1      phil 		if (m == NULL)
   2829  1.63.2.1      phil 			senderr(ENOMEM, is_tx_nobuf);
   2830  1.63.2.1      phil 		*(uint16_t *)frm = htole16(arg);	/* reason */
   2831  1.63.2.1      phil 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
   2832  1.63.2.1      phil 
   2833  1.63.2.1      phil 		IEEE80211_NODE_STAT(ni, tx_disassoc);
   2834  1.63.2.1      phil 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
   2835       1.1    dyoung 		break;
   2836       1.1    dyoung 
   2837  1.63.2.1      phil 	default:
   2838  1.63.2.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
   2839  1.63.2.1      phil 		    "invalid mgmt frame type %u", type);
   2840  1.63.2.1      phil 		senderr(EINVAL, is_tx_unknownmgt);
   2841  1.63.2.1      phil 		/* NOTREACHED */
   2842  1.63.2.1      phil 	}
   2843  1.63.2.1      phil 
   2844  1.63.2.1      phil 	/* NB: force non-ProbeResp frames to the highest queue */
   2845  1.63.2.1      phil 	params.ibp_pri = WME_AC_VO;
   2846  1.63.2.1      phil 	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
   2847  1.63.2.1      phil 	/* NB: we know all frames are unicast */
   2848  1.63.2.1      phil 	params.ibp_try0 = bss->ni_txparms->maxretry;
   2849  1.63.2.1      phil 	params.ibp_power = bss->ni_txpower;
   2850  1.63.2.1      phil 	return ieee80211_mgmt_output(ni, m, type, &params);
   2851  1.63.2.1      phil bad:
   2852  1.63.2.1      phil 	ieee80211_free_node(ni);
   2853  1.63.2.1      phil 	return ret;
   2854  1.63.2.1      phil #undef senderr
   2855  1.63.2.1      phil #undef HTFLAGS
   2856  1.63.2.1      phil }
   2857  1.63.2.1      phil 
   2858  1.63.2.1      phil /*
   2859  1.63.2.1      phil  * Return an mbuf with a probe response frame in it.
   2860  1.63.2.1      phil  * Space is left to prepend and 802.11 header at the
   2861  1.63.2.1      phil  * front but it's left to the caller to fill in.
   2862  1.63.2.1      phil  */
   2863  1.63.2.1      phil struct mbuf *
   2864  1.63.2.1      phil ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
   2865  1.63.2.1      phil {
   2866  1.63.2.1      phil 	struct ieee80211vap *vap = bss->ni_vap;
   2867  1.63.2.1      phil 	struct ieee80211com *ic = bss->ni_ic;
   2868  1.63.2.1      phil 	const struct ieee80211_rateset *rs;
   2869  1.63.2.1      phil 	struct mbuf *m;
   2870  1.63.2.1      phil 	uint16_t capinfo;
   2871  1.63.2.1      phil 	uint8_t *frm;
   2872  1.63.2.1      phil 
   2873  1.63.2.1      phil 	/*
   2874  1.63.2.1      phil 	 * probe response frame format
   2875  1.63.2.1      phil 	 *	[8] time stamp
   2876  1.63.2.1      phil 	 *	[2] beacon interval
   2877  1.63.2.1      phil 	 *	[2] cabability information
   2878  1.63.2.1      phil 	 *	[tlv] ssid
   2879  1.63.2.1      phil 	 *	[tlv] supported rates
   2880  1.63.2.1      phil 	 *	[tlv] parameter set (FH/DS)
   2881  1.63.2.1      phil 	 *	[tlv] parameter set (IBSS)
   2882  1.63.2.1      phil 	 *	[tlv] country (optional)
   2883  1.63.2.1      phil 	 *	[3] power control (optional)
   2884  1.63.2.1      phil 	 *	[5] channel switch announcement (CSA) (optional)
   2885  1.63.2.1      phil 	 *	[tlv] extended rate phy (ERP)
   2886  1.63.2.1      phil 	 *	[tlv] extended supported rates
   2887  1.63.2.1      phil 	 *	[tlv] RSN (optional)
   2888  1.63.2.1      phil 	 *	[tlv] HT capabilities
   2889  1.63.2.1      phil 	 *	[tlv] HT information
   2890  1.63.2.1      phil 	 *	[tlv] VHT capabilities
   2891  1.63.2.1      phil 	 *	[tlv] VHT information
   2892  1.63.2.1      phil 	 *	[tlv] WPA (optional)
   2893  1.63.2.1      phil 	 *	[tlv] WME (optional)
   2894  1.63.2.1      phil 	 *	[tlv] Vendor OUI HT capabilities (optional)
   2895  1.63.2.1      phil 	 *	[tlv] Vendor OUI HT information (optional)
   2896  1.63.2.1      phil 	 *	[tlv] Atheros capabilities
   2897  1.63.2.1      phil 	 *	[tlv] AppIE's (optional)
   2898  1.63.2.1      phil 	 *	[tlv] Mesh ID (MBSS)
   2899  1.63.2.1      phil 	 *	[tlv] Mesh Conf (MBSS)
   2900  1.63.2.1      phil 	 */
   2901  1.63.2.1      phil 	m = ieee80211_getmgtframe(&frm,
   2902  1.63.2.1      phil 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
   2903  1.63.2.1      phil 		 8
   2904  1.63.2.1      phil 	       + sizeof(uint16_t)
   2905  1.63.2.1      phil 	       + sizeof(uint16_t)
   2906  1.63.2.1      phil 	       + 2 + IEEE80211_NWID_LEN
   2907  1.63.2.1      phil 	       + 2 + IEEE80211_RATE_SIZE
   2908  1.63.2.1      phil 	       + 7	/* max(7,3) */
   2909  1.63.2.1      phil 	       + IEEE80211_COUNTRY_MAX_SIZE
   2910  1.63.2.1      phil 	       + 3
   2911  1.63.2.1      phil 	       + sizeof(struct ieee80211_csa_ie)
   2912  1.63.2.1      phil 	       + sizeof(struct ieee80211_quiet_ie)
   2913  1.63.2.1      phil 	       + 3
   2914  1.63.2.1      phil 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
   2915  1.63.2.1      phil 	       + sizeof(struct ieee80211_ie_wpa)
   2916  1.63.2.1      phil 	       + sizeof(struct ieee80211_ie_htcap)
   2917  1.63.2.1      phil 	       + sizeof(struct ieee80211_ie_htinfo)
   2918  1.63.2.1      phil 	       + sizeof(struct ieee80211_ie_wpa)
   2919  1.63.2.1      phil 	       + sizeof(struct ieee80211_wme_param)
   2920  1.63.2.1      phil 	       + 4 + sizeof(struct ieee80211_ie_htcap)
   2921  1.63.2.1      phil 	       + 4 + sizeof(struct ieee80211_ie_htinfo)
   2922  1.63.2.1      phil 	       +  sizeof(struct ieee80211_ie_vhtcap)
   2923  1.63.2.1      phil 	       +  sizeof(struct ieee80211_ie_vht_operation)
   2924  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   2925  1.63.2.1      phil 	       + sizeof(struct ieee80211_ath_ie)
   2926  1.63.2.1      phil #endif
   2927  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   2928  1.63.2.1      phil 	       + 2 + IEEE80211_MESHID_LEN
   2929  1.63.2.1      phil 	       + sizeof(struct ieee80211_meshconf_ie)
   2930  1.63.2.1      phil #endif
   2931  1.63.2.1      phil 	       + (vap->iv_appie_proberesp != NULL ?
   2932  1.63.2.1      phil 			vap->iv_appie_proberesp->ie_len : 0)
   2933  1.63.2.1      phil 	);
   2934  1.63.2.1      phil 	if (m == NULL) {
   2935  1.63.2.1      phil 		vap->iv_stats.is_tx_nobuf++;
   2936  1.63.2.1      phil 		return NULL;
   2937  1.63.2.1      phil 	}
   2938  1.63.2.1      phil 
   2939  1.63.2.1      phil 	memset(frm, 0, 8);	/* timestamp should be filled later */
   2940  1.63.2.1      phil 	frm += 8;
   2941  1.63.2.1      phil 	*(uint16_t *)frm = htole16(bss->ni_intval);
   2942  1.63.2.1      phil 	frm += 2;
   2943  1.63.2.1      phil 	capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
   2944  1.63.2.1      phil 	*(uint16_t *)frm = htole16(capinfo);
   2945  1.63.2.1      phil 	frm += 2;
   2946  1.63.2.1      phil 
   2947  1.63.2.1      phil 	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
   2948  1.63.2.1      phil 	rs = ieee80211_get_suprates(ic, bss->ni_chan);
   2949  1.63.2.1      phil 	frm = ieee80211_add_rates(frm, rs);
   2950  1.63.2.1      phil 
   2951  1.63.2.1      phil 	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
   2952  1.63.2.1      phil 		*frm++ = IEEE80211_ELEMID_FHPARMS;
   2953  1.63.2.1      phil 		*frm++ = 5;
   2954  1.63.2.1      phil 		*frm++ = bss->ni_fhdwell & 0x00ff;
   2955  1.63.2.1      phil 		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
   2956  1.63.2.1      phil 		*frm++ = IEEE80211_FH_CHANSET(
   2957  1.63.2.1      phil 		    ieee80211_chan2ieee(ic, bss->ni_chan));
   2958  1.63.2.1      phil 		*frm++ = IEEE80211_FH_CHANPAT(
   2959  1.63.2.1      phil 		    ieee80211_chan2ieee(ic, bss->ni_chan));
   2960  1.63.2.1      phil 		*frm++ = bss->ni_fhindex;
   2961  1.63.2.1      phil 	} else {
   2962  1.63.2.1      phil 		*frm++ = IEEE80211_ELEMID_DSPARMS;
   2963  1.63.2.1      phil 		*frm++ = 1;
   2964  1.63.2.1      phil 		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
   2965  1.63.2.1      phil 	}
   2966  1.63.2.1      phil 
   2967  1.63.2.1      phil 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
   2968  1.63.2.1      phil 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
   2969  1.63.2.1      phil 		*frm++ = 2;
   2970  1.63.2.1      phil 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
   2971  1.63.2.1      phil 	}
   2972  1.63.2.1      phil 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
   2973  1.63.2.1      phil 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
   2974  1.63.2.1      phil 		frm = ieee80211_add_countryie(frm, ic);
   2975  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_DOTH) {
   2976  1.63.2.1      phil 		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
   2977  1.63.2.1      phil 			frm = ieee80211_add_powerconstraint(frm, vap);
   2978  1.63.2.1      phil 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
   2979  1.63.2.1      phil 			frm = ieee80211_add_csa(frm, vap);
   2980  1.63.2.1      phil 	}
   2981  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_DOTH) {
   2982  1.63.2.1      phil 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
   2983  1.63.2.1      phil 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
   2984  1.63.2.1      phil 			if (vap->iv_quiet)
   2985  1.63.2.1      phil 				frm = ieee80211_add_quiet(frm, vap, 0);
   2986  1.63.2.1      phil 		}
   2987  1.63.2.1      phil 	}
   2988  1.63.2.1      phil 	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
   2989  1.63.2.1      phil 		frm = ieee80211_add_erp(frm, ic);
   2990  1.63.2.1      phil 	frm = ieee80211_add_xrates(frm, rs);
   2991  1.63.2.1      phil 	frm = ieee80211_add_rsn(frm, vap);
   2992  1.63.2.1      phil 	/*
   2993  1.63.2.1      phil 	 * NB: legacy 11b clients do not get certain ie's.
   2994  1.63.2.1      phil 	 *     The caller identifies such clients by passing
   2995  1.63.2.1      phil 	 *     a token in legacy to us.  Could expand this to be
   2996  1.63.2.1      phil 	 *     any legacy client for stuff like HT ie's.
   2997  1.63.2.1      phil 	 */
   2998  1.63.2.1      phil 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
   2999  1.63.2.1      phil 	    legacy != IEEE80211_SEND_LEGACY_11B) {
   3000  1.63.2.1      phil 		frm = ieee80211_add_htcap(frm, bss);
   3001  1.63.2.1      phil 		frm = ieee80211_add_htinfo(frm, bss);
   3002  1.63.2.1      phil 	}
   3003  1.63.2.1      phil 	if (IEEE80211_IS_CHAN_VHT(bss->ni_chan) &&
   3004  1.63.2.1      phil 	    legacy != IEEE80211_SEND_LEGACY_11B) {
   3005  1.63.2.1      phil 		frm = ieee80211_add_vhtcap(frm, bss);
   3006  1.63.2.1      phil 		frm = ieee80211_add_vhtinfo(frm, bss);
   3007  1.63.2.1      phil 	}
   3008  1.63.2.1      phil 	frm = ieee80211_add_wpa(frm, vap);
   3009  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_WME)
   3010  1.63.2.1      phil 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
   3011  1.63.2.1      phil 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
   3012  1.63.2.1      phil 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
   3013  1.63.2.1      phil 	    legacy != IEEE80211_SEND_LEGACY_11B) {
   3014  1.63.2.1      phil 		frm = ieee80211_add_htcap_vendor(frm, bss);
   3015  1.63.2.1      phil 		frm = ieee80211_add_htinfo_vendor(frm, bss);
   3016  1.63.2.1      phil 	}
   3017  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   3018  1.63.2.1      phil 	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
   3019  1.63.2.1      phil 	    legacy != IEEE80211_SEND_LEGACY_11B)
   3020  1.63.2.1      phil 		frm = ieee80211_add_athcaps(frm, bss);
   3021  1.63.2.1      phil #endif
   3022  1.63.2.1      phil 	if (vap->iv_appie_proberesp != NULL)
   3023  1.63.2.1      phil 		frm = add_appie(frm, vap->iv_appie_proberesp);
   3024  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   3025  1.63.2.1      phil 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
   3026  1.63.2.1      phil 		frm = ieee80211_add_meshid(frm, vap);
   3027  1.63.2.1      phil 		frm = ieee80211_add_meshconf(frm, vap);
   3028  1.63.2.1      phil 	}
   3029  1.63.2.1      phil #endif
   3030  1.63.2.1      phil 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
   3031  1.63.2.1      phil 
   3032  1.63.2.1      phil 	return m;
   3033  1.63.2.1      phil }
   3034  1.63.2.1      phil 
   3035  1.63.2.1      phil /*
   3036  1.63.2.1      phil  * Send a probe response frame to the specified mac address.
   3037  1.63.2.1      phil  * This does not go through the normal mgt frame api so we
   3038  1.63.2.1      phil  * can specify the destination address and re-use the bss node
   3039  1.63.2.1      phil  * for the sta reference.
   3040  1.63.2.1      phil  */
   3041  1.63.2.1      phil int
   3042  1.63.2.1      phil ieee80211_send_proberesp(struct ieee80211vap *vap,
   3043  1.63.2.1      phil 	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
   3044  1.63.2.1      phil {
   3045  1.63.2.1      phil 	struct ieee80211_node *bss = vap->iv_bss;
   3046  1.63.2.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   3047  1.63.2.1      phil 	struct mbuf *m;
   3048  1.63.2.1      phil 	int ret;
   3049  1.63.2.1      phil 
   3050  1.63.2.1      phil 	if (vap->iv_state == IEEE80211_S_CAC) {
   3051  1.63.2.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
   3052  1.63.2.1      phil 		    "block %s frame in CAC state", "probe response");
   3053  1.63.2.1      phil 		vap->iv_stats.is_tx_badstate++;
   3054  1.63.2.1      phil 		return EIO;		/* XXX */
   3055  1.63.2.1      phil 	}
   3056  1.63.2.1      phil 
   3057  1.63.2.1      phil 	/*
   3058  1.63.2.1      phil 	 * Hold a reference on the node so it doesn't go away until after
   3059  1.63.2.1      phil 	 * the xmit is complete all the way in the driver.  On error we
   3060  1.63.2.1      phil 	 * will remove our reference.
   3061  1.63.2.1      phil 	 */
   3062  1.63.2.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
   3063  1.63.2.1      phil 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
   3064  1.63.2.1      phil 	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
   3065  1.63.2.1      phil 	    ieee80211_node_refcnt(bss)+1);
   3066  1.63.2.1      phil 	ieee80211_ref_node(bss);
   3067  1.63.2.1      phil 
   3068  1.63.2.1      phil 	m = ieee80211_alloc_proberesp(bss, legacy);
   3069  1.63.2.1      phil 	if (m == NULL) {
   3070  1.63.2.1      phil 		ieee80211_free_node(bss);
   3071  1.63.2.1      phil 		return ENOMEM;
   3072  1.63.2.1      phil 	}
   3073  1.63.2.1      phil 
   3074  1.63.2.1      phil 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
   3075  1.63.2.1      phil 	KASSERT(m != NULL, ("no room for header"));
   3076      1.29    dyoung 
   3077  1.63.2.1      phil 	IEEE80211_TX_LOCK(ic);
   3078  1.63.2.1      phil 	ieee80211_send_setup(bss, m,
   3079  1.63.2.1      phil 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
   3080  1.63.2.1      phil 	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
   3081  1.63.2.1      phil 	/* XXX power management? */
   3082  1.63.2.1      phil 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
   3083       1.1    dyoung 
   3084  1.63.2.1      phil 	M_WME_SETAC(m, WME_AC_BE);
   3085  1.63.2.1      phil 
   3086  1.63.2.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
   3087  1.63.2.1      phil 	    "send probe resp on channel %u to %s%s\n",
   3088  1.63.2.1      phil 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
   3089  1.63.2.1      phil 	    legacy ? " <legacy>" : "");
   3090  1.63.2.1      phil 	IEEE80211_NODE_STAT(bss, tx_mgmt);
   3091  1.63.2.1      phil 
   3092  1.63.2.1      phil 	ret = ieee80211_raw_output(vap, bss, m, NULL);
   3093  1.63.2.1      phil 	IEEE80211_TX_UNLOCK(ic);
   3094  1.63.2.1      phil 	return (ret);
   3095       1.1    dyoung }
   3096       1.6    dyoung 
   3097      1.29    dyoung /*
   3098  1.63.2.1      phil  * Allocate and build a RTS (Request To Send) control frame.
   3099      1.50  jmcneill  */
   3100      1.50  jmcneill struct mbuf *
   3101  1.63.2.1      phil ieee80211_alloc_rts(struct ieee80211com *ic,
   3102  1.63.2.1      phil 	const uint8_t ra[IEEE80211_ADDR_LEN],
   3103  1.63.2.1      phil 	const uint8_t ta[IEEE80211_ADDR_LEN],
   3104  1.63.2.1      phil 	uint16_t dur)
   3105      1.50  jmcneill {
   3106      1.50  jmcneill 	struct ieee80211_frame_rts *rts;
   3107      1.50  jmcneill 	struct mbuf *m;
   3108      1.50  jmcneill 
   3109  1.63.2.1      phil 	/* XXX honor ic_headroom */
   3110  1.63.2.1      phil 	m = m_gethdr(M_NOWAIT, MT_DATA);
   3111  1.63.2.1      phil 	if (m != NULL) {
   3112  1.63.2.1      phil 		rts = mtod(m, struct ieee80211_frame_rts *);
   3113  1.63.2.1      phil 		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
   3114  1.63.2.1      phil 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
   3115  1.63.2.1      phil 		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   3116  1.63.2.1      phil 		*(u_int16_t *)rts->i_dur = htole16(dur);
   3117  1.63.2.1      phil 		IEEE80211_ADDR_COPY(rts->i_ra, ra);
   3118  1.63.2.1      phil 		IEEE80211_ADDR_COPY(rts->i_ta, ta);
   3119      1.50  jmcneill 
   3120  1.63.2.1      phil 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
   3121  1.63.2.1      phil 	}
   3122      1.50  jmcneill 	return m;
   3123      1.50  jmcneill }
   3124      1.50  jmcneill 
   3125      1.50  jmcneill /*
   3126  1.63.2.1      phil  * Allocate and build a CTS (Clear To Send) control frame.
   3127      1.50  jmcneill  */
   3128      1.50  jmcneill struct mbuf *
   3129  1.63.2.1      phil ieee80211_alloc_cts(struct ieee80211com *ic,
   3130  1.63.2.1      phil 	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
   3131      1.50  jmcneill {
   3132      1.50  jmcneill 	struct ieee80211_frame_cts *cts;
   3133      1.50  jmcneill 	struct mbuf *m;
   3134      1.50  jmcneill 
   3135  1.63.2.1      phil 	/* XXX honor ic_headroom */
   3136  1.63.2.1      phil 	m = m_gethdr(M_NOWAIT, MT_DATA);
   3137  1.63.2.1      phil 	if (m != NULL) {
   3138  1.63.2.1      phil 		cts = mtod(m, struct ieee80211_frame_cts *);
   3139  1.63.2.1      phil 		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
   3140  1.63.2.1      phil 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
   3141  1.63.2.1      phil 		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   3142  1.63.2.1      phil 		*(u_int16_t *)cts->i_dur = htole16(dur);
   3143  1.63.2.1      phil 		IEEE80211_ADDR_COPY(cts->i_ra, ra);
   3144      1.50  jmcneill 
   3145  1.63.2.1      phil 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
   3146  1.63.2.1      phil 	}
   3147      1.50  jmcneill 	return m;
   3148      1.50  jmcneill }
   3149      1.50  jmcneill 
   3150      1.50  jmcneill /*
   3151  1.63.2.1      phil  * Wrapper for CTS/RTS frame allocation.
   3152      1.29    dyoung  */
   3153      1.29    dyoung struct mbuf *
   3154  1.63.2.1      phil ieee80211_alloc_prot(struct ieee80211_node *ni, const struct mbuf *m,
   3155  1.63.2.1      phil     uint8_t rate, int prot)
   3156      1.29    dyoung {
   3157  1.63.2.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   3158  1.63.2.1      phil 	const struct ieee80211_frame *wh;
   3159  1.63.2.1      phil 	struct mbuf *mprot;
   3160  1.63.2.1      phil 	uint16_t dur;
   3161  1.63.2.1      phil 	int pktlen, isshort;
   3162  1.63.2.1      phil 
   3163  1.63.2.1      phil 	KASSERT(prot == IEEE80211_PROT_RTSCTS ||
   3164  1.63.2.1      phil 	    prot == IEEE80211_PROT_CTSONLY,
   3165  1.63.2.1      phil 	    ("wrong protection type %d", prot));
   3166  1.63.2.1      phil 
   3167  1.63.2.1      phil 	wh = mtod(m, const struct ieee80211_frame *);
   3168  1.63.2.1      phil 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
   3169  1.63.2.1      phil 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
   3170  1.63.2.1      phil 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
   3171  1.63.2.1      phil 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
   3172  1.63.2.1      phil 
   3173  1.63.2.1      phil 	if (prot == IEEE80211_PROT_RTSCTS) {
   3174  1.63.2.1      phil 		/* NB: CTS is the same size as an ACK */
   3175  1.63.2.1      phil 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
   3176  1.63.2.1      phil 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
   3177  1.63.2.1      phil 	} else
   3178  1.63.2.1      phil 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
   3179  1.63.2.1      phil 
   3180  1.63.2.1      phil 	return (mprot);
   3181  1.63.2.1      phil }
   3182  1.63.2.1      phil 
   3183  1.63.2.1      phil static void
   3184  1.63.2.1      phil ieee80211_tx_mgt_timeout(void *arg)
   3185  1.63.2.1      phil {
   3186  1.63.2.1      phil 	struct ieee80211vap *vap = arg;
   3187  1.63.2.1      phil 
   3188  1.63.2.1      phil 	IEEE80211_LOCK(vap->iv_ic);
   3189  1.63.2.1      phil 	if (vap->iv_state != IEEE80211_S_INIT &&
   3190  1.63.2.1      phil 	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
   3191  1.63.2.1      phil 		/*
   3192  1.63.2.1      phil 		 * NB: it's safe to specify a timeout as the reason here;
   3193  1.63.2.1      phil 		 *     it'll only be used in the right state.
   3194  1.63.2.1      phil 		 */
   3195  1.63.2.1      phil 		ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
   3196  1.63.2.1      phil 			IEEE80211_SCAN_FAIL_TIMEOUT);
   3197  1.63.2.1      phil 	}
   3198  1.63.2.1      phil 	IEEE80211_UNLOCK(vap->iv_ic);
   3199  1.63.2.1      phil }
   3200  1.63.2.1      phil 
   3201  1.63.2.1      phil /*
   3202  1.63.2.1      phil  * This is the callback set on net80211-sourced transmitted
   3203  1.63.2.1      phil  * authentication request frames.
   3204  1.63.2.1      phil  *
   3205  1.63.2.1      phil  * This does a couple of things:
   3206  1.63.2.1      phil  *
   3207  1.63.2.1      phil  * + If the frame transmitted was a success, it schedules a future
   3208  1.63.2.1      phil  *   event which will transition the interface to scan.
   3209  1.63.2.1      phil  *   If a state transition _then_ occurs before that event occurs,
   3210  1.63.2.1      phil  *   said state transition will cancel this callout.
   3211  1.63.2.1      phil  *
   3212  1.63.2.1      phil  * + If the frame transmit was a failure, it immediately schedules
   3213  1.63.2.1      phil  *   the transition back to scan.
   3214  1.63.2.1      phil  */
   3215  1.63.2.1      phil static void
   3216  1.63.2.1      phil ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
   3217  1.63.2.1      phil {
   3218  1.63.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   3219  1.63.2.1      phil 	enum ieee80211_state ostate = (enum ieee80211_state) arg;
   3220  1.63.2.1      phil 
   3221  1.63.2.1      phil 	/*
   3222  1.63.2.1      phil 	 * Frame transmit completed; arrange timer callback.  If
   3223  1.63.2.1      phil 	 * transmit was successfully we wait for response.  Otherwise
   3224  1.63.2.1      phil 	 * we arrange an immediate callback instead of doing the
   3225  1.63.2.1      phil 	 * callback directly since we don't know what state the driver
   3226  1.63.2.1      phil 	 * is in (e.g. what locks it is holding).  This work should
   3227  1.63.2.1      phil 	 * not be too time-critical and not happen too often so the
   3228  1.63.2.1      phil 	 * added overhead is acceptable.
   3229  1.63.2.1      phil 	 *
   3230  1.63.2.1      phil 	 * XXX what happens if !acked but response shows up before callback?
   3231  1.63.2.1      phil 	 */
   3232  1.63.2.1      phil 	if (vap->iv_state == ostate) {
   3233  1.63.2.1      phil 		callout_reset(&vap->iv_mgtsend,
   3234  1.63.2.1      phil 			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
   3235  1.63.2.1      phil 			ieee80211_tx_mgt_timeout, vap);
   3236  1.63.2.1      phil 	}
   3237  1.63.2.1      phil }
   3238      1.29    dyoung 
   3239  1.63.2.1      phil static void
   3240  1.63.2.1      phil ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
   3241  1.63.2.1      phil 	struct ieee80211_node *ni)
   3242  1.63.2.1      phil {
   3243  1.63.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   3244  1.63.2.1      phil 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
   3245  1.63.2.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   3246  1.63.2.1      phil 	struct ieee80211_rateset *rs = &ni->ni_rates;
   3247  1.63.2.1      phil 	uint16_t capinfo;
   3248      1.61      maxv 
   3249      1.29    dyoung 	/*
   3250      1.29    dyoung 	 * beacon frame format
   3251  1.63.2.1      phil 	 *
   3252  1.63.2.1      phil 	 * TODO: update to 802.11-2012; a lot of stuff has changed;
   3253  1.63.2.1      phil 	 * vendor extensions should be at the end, etc.
   3254  1.63.2.1      phil 	 *
   3255      1.29    dyoung 	 *	[8] time stamp
   3256      1.29    dyoung 	 *	[2] beacon interval
   3257      1.29    dyoung 	 *	[2] cabability information
   3258      1.29    dyoung 	 *	[tlv] ssid
   3259      1.29    dyoung 	 *	[tlv] supported rates
   3260      1.29    dyoung 	 *	[3] parameter set (DS)
   3261  1.63.2.1      phil 	 *	[8] CF parameter set (optional)
   3262      1.29    dyoung 	 *	[tlv] parameter set (IBSS/TIM)
   3263  1.63.2.1      phil 	 *	[tlv] country (optional)
   3264  1.63.2.1      phil 	 *	[3] power control (optional)
   3265  1.63.2.1      phil 	 *	[5] channel switch announcement (CSA) (optional)
   3266  1.63.2.1      phil 	 * XXX TODO: Quiet
   3267  1.63.2.1      phil 	 * XXX TODO: IBSS DFS
   3268  1.63.2.1      phil 	 * XXX TODO: TPC report
   3269      1.29    dyoung 	 *	[tlv] extended rate phy (ERP)
   3270      1.29    dyoung 	 *	[tlv] extended supported rates
   3271  1.63.2.1      phil 	 *	[tlv] RSN parameters
   3272  1.63.2.1      phil 	 * XXX TODO: BSSLOAD
   3273  1.63.2.1      phil 	 * (XXX EDCA parameter set, QoS capability?)
   3274  1.63.2.1      phil 	 * XXX TODO: AP channel report
   3275      1.61      maxv 	 *
   3276  1.63.2.1      phil 	 *	[tlv] HT capabilities
   3277  1.63.2.1      phil 	 *	[tlv] HT information
   3278  1.63.2.1      phil 	 *	XXX TODO: 20/40 BSS coexistence
   3279  1.63.2.1      phil 	 * Mesh:
   3280  1.63.2.1      phil 	 * XXX TODO: Meshid
   3281  1.63.2.1      phil 	 * XXX TODO: mesh config
   3282  1.63.2.1      phil 	 * XXX TODO: mesh awake window
   3283  1.63.2.1      phil 	 * XXX TODO: beacon timing (mesh, etc)
   3284  1.63.2.1      phil 	 * XXX TODO: MCCAOP Advertisement Overview
   3285  1.63.2.1      phil 	 * XXX TODO: MCCAOP Advertisement
   3286  1.63.2.1      phil 	 * XXX TODO: Mesh channel switch parameters
   3287  1.63.2.1      phil 	 * VHT:
   3288  1.63.2.1      phil 	 * XXX TODO: VHT capabilities
   3289  1.63.2.1      phil 	 * XXX TODO: VHT operation
   3290  1.63.2.1      phil 	 * XXX TODO: VHT transmit power envelope
   3291  1.63.2.1      phil 	 * XXX TODO: channel switch wrapper element
   3292  1.63.2.1      phil 	 * XXX TODO: extended BSS load element
   3293  1.63.2.1      phil 	 *
   3294  1.63.2.1      phil 	 * XXX Vendor-specific OIDs (e.g. Atheros)
   3295  1.63.2.1      phil 	 *	[tlv] WPA parameters
   3296  1.63.2.1      phil 	 *	[tlv] WME parameters
   3297  1.63.2.1      phil 	 *	[tlv] Vendor OUI HT capabilities (optional)
   3298  1.63.2.1      phil 	 *	[tlv] Vendor OUI HT information (optional)
   3299  1.63.2.1      phil 	 *	[tlv] Atheros capabilities (optional)
   3300  1.63.2.1      phil 	 *	[tlv] TDMA parameters (optional)
   3301  1.63.2.1      phil 	 *	[tlv] Mesh ID (MBSS)
   3302  1.63.2.1      phil 	 *	[tlv] Mesh Conf (MBSS)
   3303  1.63.2.1      phil 	 *	[tlv] application data (optional)
   3304      1.29    dyoung 	 */
   3305  1.63.2.1      phil 
   3306  1.63.2.1      phil 	memset(bo, 0, sizeof(*bo));
   3307      1.29    dyoung 
   3308      1.29    dyoung 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
   3309      1.29    dyoung 	frm += 8;
   3310  1.63.2.1      phil 	*(uint16_t *)frm = htole16(ni->ni_intval);
   3311      1.29    dyoung 	frm += 2;
   3312  1.63.2.1      phil 	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
   3313  1.63.2.1      phil 	bo->bo_caps = (uint16_t *)frm;
   3314  1.63.2.1      phil 	*(uint16_t *)frm = htole16(capinfo);
   3315      1.29    dyoung 	frm += 2;
   3316      1.29    dyoung 	*frm++ = IEEE80211_ELEMID_SSID;
   3317  1.63.2.1      phil 	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
   3318      1.29    dyoung 		*frm++ = ni->ni_esslen;
   3319      1.29    dyoung 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
   3320      1.29    dyoung 		frm += ni->ni_esslen;
   3321      1.29    dyoung 	} else
   3322      1.29    dyoung 		*frm++ = 0;
   3323      1.29    dyoung 	frm = ieee80211_add_rates(frm, rs);
   3324  1.63.2.1      phil 	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
   3325      1.29    dyoung 		*frm++ = IEEE80211_ELEMID_DSPARMS;
   3326      1.29    dyoung 		*frm++ = 1;
   3327      1.29    dyoung 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
   3328      1.29    dyoung 	}
   3329  1.63.2.1      phil 	if (ic->ic_flags & IEEE80211_F_PCF) {
   3330  1.63.2.1      phil 		bo->bo_cfp = frm;
   3331  1.63.2.1      phil 		frm = ieee80211_add_cfparms(frm, ic);
   3332  1.63.2.1      phil 	}
   3333      1.29    dyoung 	bo->bo_tim = frm;
   3334  1.63.2.1      phil 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
   3335      1.29    dyoung 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
   3336      1.29    dyoung 		*frm++ = 2;
   3337      1.29    dyoung 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
   3338      1.29    dyoung 		bo->bo_tim_len = 0;
   3339  1.63.2.1      phil 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
   3340  1.63.2.1      phil 	    vap->iv_opmode == IEEE80211_M_MBSS) {
   3341  1.63.2.1      phil 		/* TIM IE is the same for Mesh and Hostap */
   3342  1.63.2.1      phil 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
   3343      1.29    dyoung 
   3344      1.29    dyoung 		tie->tim_ie = IEEE80211_ELEMID_TIM;
   3345      1.29    dyoung 		tie->tim_len = 4;	/* length */
   3346      1.29    dyoung 		tie->tim_count = 0;	/* DTIM count */
   3347  1.63.2.1      phil 		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
   3348      1.29    dyoung 		tie->tim_bitctl = 0;	/* bitmap control */
   3349      1.29    dyoung 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
   3350      1.29    dyoung 		frm += sizeof(struct ieee80211_tim_ie);
   3351      1.29    dyoung 		bo->bo_tim_len = 1;
   3352      1.29    dyoung 	}
   3353  1.63.2.1      phil 	bo->bo_tim_trailer = frm;
   3354  1.63.2.1      phil 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
   3355  1.63.2.1      phil 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
   3356  1.63.2.1      phil 		frm = ieee80211_add_countryie(frm, ic);
   3357  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_DOTH) {
   3358  1.63.2.1      phil 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
   3359  1.63.2.1      phil 			frm = ieee80211_add_powerconstraint(frm, vap);
   3360  1.63.2.1      phil 		bo->bo_csa = frm;
   3361  1.63.2.1      phil 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
   3362  1.63.2.1      phil 			frm = ieee80211_add_csa(frm, vap);
   3363  1.63.2.1      phil 	} else
   3364  1.63.2.1      phil 		bo->bo_csa = frm;
   3365  1.63.2.1      phil 
   3366  1.63.2.1      phil 	bo->bo_quiet = NULL;
   3367  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_DOTH) {
   3368  1.63.2.1      phil 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
   3369  1.63.2.1      phil 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
   3370  1.63.2.1      phil 		    (vap->iv_quiet == 1)) {
   3371  1.63.2.1      phil 			/*
   3372  1.63.2.1      phil 			 * We only insert the quiet IE offset if
   3373  1.63.2.1      phil 			 * the quiet IE is enabled.  Otherwise don't
   3374  1.63.2.1      phil 			 * put it here or we'll just overwrite
   3375  1.63.2.1      phil 			 * some other beacon contents.
   3376  1.63.2.1      phil 			 */
   3377  1.63.2.1      phil 			if (vap->iv_quiet) {
   3378  1.63.2.1      phil 				bo->bo_quiet = frm;
   3379  1.63.2.1      phil 				frm = ieee80211_add_quiet(frm,vap, 0);
   3380  1.63.2.1      phil 			}
   3381  1.63.2.1      phil 		}
   3382  1.63.2.1      phil 	}
   3383  1.63.2.1      phil 
   3384  1.63.2.1      phil 	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
   3385  1.63.2.1      phil 		bo->bo_erp = frm;
   3386  1.63.2.1      phil 		frm = ieee80211_add_erp(frm, ic);
   3387  1.63.2.1      phil 	}
   3388  1.63.2.1      phil 	frm = ieee80211_add_xrates(frm, rs);
   3389  1.63.2.1      phil 	frm = ieee80211_add_rsn(frm, vap);
   3390  1.63.2.1      phil 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
   3391  1.63.2.1      phil 		frm = ieee80211_add_htcap(frm, ni);
   3392  1.63.2.1      phil 		bo->bo_htinfo = frm;
   3393  1.63.2.1      phil 		frm = ieee80211_add_htinfo(frm, ni);
   3394  1.63.2.1      phil 	}
   3395  1.63.2.1      phil 
   3396  1.63.2.1      phil 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
   3397  1.63.2.1      phil 		frm = ieee80211_add_vhtcap(frm, ni);
   3398  1.63.2.1      phil 		bo->bo_vhtinfo = frm;
   3399  1.63.2.1      phil 		frm = ieee80211_add_vhtinfo(frm, ni);
   3400  1.63.2.1      phil 		/* Transmit power envelope */
   3401  1.63.2.1      phil 		/* Channel switch wrapper element */
   3402  1.63.2.1      phil 		/* Extended bss load element */
   3403  1.63.2.1      phil 	}
   3404      1.61      maxv 
   3405  1.63.2.1      phil 	frm = ieee80211_add_wpa(frm, vap);
   3406  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_WME) {
   3407      1.29    dyoung 		bo->bo_wme = frm;
   3408      1.29    dyoung 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
   3409  1.63.2.1      phil 	}
   3410  1.63.2.1      phil 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
   3411  1.63.2.1      phil 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
   3412  1.63.2.1      phil 		frm = ieee80211_add_htcap_vendor(frm, ni);
   3413  1.63.2.1      phil 		frm = ieee80211_add_htinfo_vendor(frm, ni);
   3414      1.29    dyoung 	}
   3415      1.61      maxv 
   3416  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   3417  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
   3418  1.63.2.1      phil 		bo->bo_ath = frm;
   3419  1.63.2.1      phil 		frm = ieee80211_add_athcaps(frm, ni);
   3420  1.63.2.1      phil 	}
   3421  1.63.2.1      phil #endif
   3422  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_TDMA
   3423  1.63.2.1      phil 	if (vap->iv_caps & IEEE80211_C_TDMA) {
   3424  1.63.2.1      phil 		bo->bo_tdma = frm;
   3425  1.63.2.1      phil 		frm = ieee80211_add_tdma(frm, vap);
   3426  1.63.2.1      phil 	}
   3427  1.63.2.1      phil #endif
   3428  1.63.2.1      phil 	if (vap->iv_appie_beacon != NULL) {
   3429  1.63.2.1      phil 		bo->bo_appie = frm;
   3430  1.63.2.1      phil 		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
   3431  1.63.2.1      phil 		frm = add_appie(frm, vap->iv_appie_beacon);
   3432  1.63.2.1      phil 	}
   3433      1.61      maxv 
   3434  1.63.2.1      phil 	/* XXX TODO: move meshid/meshconf up to before vendor extensions? */
   3435  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   3436  1.63.2.1      phil 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
   3437  1.63.2.1      phil 		frm = ieee80211_add_meshid(frm, vap);
   3438  1.63.2.1      phil 		bo->bo_meshconf = frm;
   3439  1.63.2.1      phil 		frm = ieee80211_add_meshconf(frm, vap);
   3440  1.63.2.1      phil 	}
   3441  1.63.2.1      phil #endif
   3442  1.63.2.1      phil 	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
   3443  1.63.2.1      phil 	bo->bo_csa_trailer_len = frm - bo->bo_csa;
   3444  1.63.2.1      phil 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
   3445  1.63.2.1      phil }
   3446      1.61      maxv 
   3447  1.63.2.1      phil /*
   3448  1.63.2.1      phil  * Allocate a beacon frame and fillin the appropriate bits.
   3449  1.63.2.1      phil  */
   3450  1.63.2.1      phil struct mbuf *
   3451  1.63.2.1      phil ieee80211_beacon_alloc(struct ieee80211_node *ni)
   3452  1.63.2.1      phil {
   3453  1.63.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   3454  1.63.2.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   3455  1.63.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
   3456  1.63.2.1      phil 	struct ieee80211_frame *wh;
   3457  1.63.2.1      phil 	struct mbuf *m;
   3458  1.63.2.1      phil 	int pktlen;
   3459  1.63.2.1      phil 	uint8_t *frm;
   3460      1.61      maxv 
   3461  1.63.2.1      phil 	/*
   3462  1.63.2.1      phil 	 * Update the "We're putting the quiet IE in the beacon" state.
   3463  1.63.2.1      phil 	 */
   3464  1.63.2.1      phil 	if (vap->iv_quiet == 1)
   3465  1.63.2.1      phil 		vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
   3466  1.63.2.1      phil 	else if (vap->iv_quiet == 0)
   3467  1.63.2.1      phil 		vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
   3468      1.29    dyoung 
   3469  1.63.2.1      phil 	/*
   3470  1.63.2.1      phil 	 * beacon frame format
   3471  1.63.2.1      phil 	 *
   3472  1.63.2.1      phil 	 * Note: This needs updating for 802.11-2012.
   3473  1.63.2.1      phil 	 *
   3474  1.63.2.1      phil 	 *	[8] time stamp
   3475  1.63.2.1      phil 	 *	[2] beacon interval
   3476  1.63.2.1      phil 	 *	[2] cabability information
   3477  1.63.2.1      phil 	 *	[tlv] ssid
   3478  1.63.2.1      phil 	 *	[tlv] supported rates
   3479  1.63.2.1      phil 	 *	[3] parameter set (DS)
   3480  1.63.2.1      phil 	 *	[8] CF parameter set (optional)
   3481  1.63.2.1      phil 	 *	[tlv] parameter set (IBSS/TIM)
   3482  1.63.2.1      phil 	 *	[tlv] country (optional)
   3483  1.63.2.1      phil 	 *	[3] power control (optional)
   3484  1.63.2.1      phil 	 *	[5] channel switch announcement (CSA) (optional)
   3485  1.63.2.1      phil 	 *	[tlv] extended rate phy (ERP)
   3486  1.63.2.1      phil 	 *	[tlv] extended supported rates
   3487  1.63.2.1      phil 	 *	[tlv] RSN parameters
   3488  1.63.2.1      phil 	 *	[tlv] HT capabilities
   3489  1.63.2.1      phil 	 *	[tlv] HT information
   3490  1.63.2.1      phil 	 *	[tlv] VHT capabilities
   3491  1.63.2.1      phil 	 *	[tlv] VHT operation
   3492  1.63.2.1      phil 	 *	[tlv] Vendor OUI HT capabilities (optional)
   3493  1.63.2.1      phil 	 *	[tlv] Vendor OUI HT information (optional)
   3494  1.63.2.1      phil 	 * XXX Vendor-specific OIDs (e.g. Atheros)
   3495  1.63.2.1      phil 	 *	[tlv] WPA parameters
   3496  1.63.2.1      phil 	 *	[tlv] WME parameters
   3497  1.63.2.1      phil 	 *	[tlv] TDMA parameters (optional)
   3498  1.63.2.1      phil 	 *	[tlv] Mesh ID (MBSS)
   3499  1.63.2.1      phil 	 *	[tlv] Mesh Conf (MBSS)
   3500  1.63.2.1      phil 	 *	[tlv] application data (optional)
   3501  1.63.2.1      phil 	 * NB: we allocate the max space required for the TIM bitmap.
   3502  1.63.2.1      phil 	 * XXX how big is this?
   3503  1.63.2.1      phil 	 */
   3504  1.63.2.1      phil 	pktlen =   8					/* time stamp */
   3505  1.63.2.1      phil 		 + sizeof(uint16_t)			/* beacon interval */
   3506  1.63.2.1      phil 		 + sizeof(uint16_t)			/* capabilities */
   3507  1.63.2.1      phil 		 + 2 + ni->ni_esslen			/* ssid */
   3508  1.63.2.1      phil 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
   3509  1.63.2.1      phil 	         + 2 + 1				/* DS parameters */
   3510  1.63.2.1      phil 		 + 2 + 6				/* CF parameters */
   3511  1.63.2.1      phil 		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
   3512  1.63.2.1      phil 		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
   3513  1.63.2.1      phil 		 + 2 + 1				/* power control */
   3514  1.63.2.1      phil 		 + sizeof(struct ieee80211_csa_ie)	/* CSA */
   3515  1.63.2.1      phil 		 + sizeof(struct ieee80211_quiet_ie)	/* Quiet */
   3516  1.63.2.1      phil 		 + 2 + 1				/* ERP */
   3517  1.63.2.1      phil 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
   3518  1.63.2.1      phil 		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
   3519  1.63.2.1      phil 			2*sizeof(struct ieee80211_ie_wpa) : 0)
   3520  1.63.2.1      phil 		 /* XXX conditional? */
   3521  1.63.2.1      phil 		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
   3522  1.63.2.1      phil 		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
   3523  1.63.2.1      phil 		 + sizeof(struct ieee80211_ie_vhtcap)/* VHT caps */
   3524  1.63.2.1      phil 		 + sizeof(struct ieee80211_ie_vht_operation)/* VHT info */
   3525  1.63.2.1      phil 		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
   3526  1.63.2.1      phil 			sizeof(struct ieee80211_wme_param) : 0)
   3527  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   3528  1.63.2.1      phil 		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
   3529  1.63.2.1      phil #endif
   3530  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_TDMA
   3531  1.63.2.1      phil 		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
   3532  1.63.2.1      phil 			sizeof(struct ieee80211_tdma_param) : 0)
   3533  1.63.2.1      phil #endif
   3534  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   3535  1.63.2.1      phil 		 + 2 + ni->ni_meshidlen
   3536  1.63.2.1      phil 		 + sizeof(struct ieee80211_meshconf_ie)
   3537  1.63.2.1      phil #endif
   3538  1.63.2.1      phil 		 + IEEE80211_MAX_APPIE
   3539  1.63.2.1      phil 		 ;
   3540  1.63.2.1      phil 	m = ieee80211_getmgtframe(&frm,
   3541  1.63.2.1      phil 		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
   3542  1.63.2.1      phil 	if (m == NULL) {
   3543  1.63.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
   3544  1.63.2.1      phil 			"%s: cannot get buf; size %u\n", __func__, pktlen);
   3545  1.63.2.1      phil 		vap->iv_stats.is_tx_nobuf++;
   3546  1.63.2.1      phil 		return NULL;
   3547  1.63.2.1      phil 	}
   3548  1.63.2.1      phil 	ieee80211_beacon_construct(m, frm, ni);
   3549      1.61      maxv 
   3550  1.63.2.1      phil 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
   3551  1.63.2.1      phil 	KASSERT(m != NULL, ("no space for 802.11 header?"));
   3552      1.29    dyoung 	wh = mtod(m, struct ieee80211_frame *);
   3553      1.29    dyoung 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
   3554      1.29    dyoung 	    IEEE80211_FC0_SUBTYPE_BEACON;
   3555      1.29    dyoung 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   3556  1.63.2.1      phil 	*(uint16_t *)wh->i_dur = 0;
   3557      1.29    dyoung 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
   3558  1.63.2.1      phil 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
   3559      1.29    dyoung 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
   3560  1.63.2.1      phil 	*(uint16_t *)wh->i_seq = 0;
   3561      1.29    dyoung 
   3562      1.29    dyoung 	return m;
   3563      1.29    dyoung }
   3564      1.29    dyoung 
   3565      1.29    dyoung /*
   3566      1.29    dyoung  * Update the dynamic parts of a beacon frame based on the current state.
   3567      1.29    dyoung  */
   3568      1.29    dyoung int
   3569  1.63.2.1      phil ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
   3570      1.29    dyoung {
   3571  1.63.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   3572  1.63.2.1      phil 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
   3573  1.63.2.1      phil 	struct ieee80211com *ic = ni->ni_ic;
   3574      1.29    dyoung 	int len_changed = 0;
   3575  1.63.2.1      phil 	uint16_t capinfo;
   3576  1.63.2.1      phil 	struct ieee80211_frame *wh;
   3577  1.63.2.1      phil 	ieee80211_seq seqno;
   3578  1.63.2.1      phil 
   3579  1.63.2.1      phil 	IEEE80211_LOCK(ic);
   3580  1.63.2.1      phil 	/*
   3581  1.63.2.1      phil 	 * Handle 11h channel change when we've reached the count.
   3582  1.63.2.1      phil 	 * We must recalculate the beacon frame contents to account
   3583  1.63.2.1      phil 	 * for the new channel.  Note we do this only for the first
   3584  1.63.2.1      phil 	 * vap that reaches this point; subsequent vaps just update
   3585  1.63.2.1      phil 	 * their beacon state to reflect the recalculated channel.
   3586  1.63.2.1      phil 	 */
   3587  1.63.2.1      phil 	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
   3588  1.63.2.1      phil 	    vap->iv_csa_count == ic->ic_csa_count) {
   3589  1.63.2.1      phil 		vap->iv_csa_count = 0;
   3590  1.63.2.1      phil 		/*
   3591  1.63.2.1      phil 		 * Effect channel change before reconstructing the beacon
   3592  1.63.2.1      phil 		 * frame contents as many places reference ni_chan.
   3593  1.63.2.1      phil 		 */
   3594  1.63.2.1      phil 		if (ic->ic_csa_newchan != NULL)
   3595  1.63.2.1      phil 			ieee80211_csa_completeswitch(ic);
   3596  1.63.2.1      phil 		/*
   3597  1.63.2.1      phil 		 * NB: ieee80211_beacon_construct clears all pending
   3598  1.63.2.1      phil 		 * updates in bo_flags so we don't need to explicitly
   3599  1.63.2.1      phil 		 * clear IEEE80211_BEACON_CSA.
   3600  1.63.2.1      phil 		 */
   3601  1.63.2.1      phil 		ieee80211_beacon_construct(m,
   3602  1.63.2.1      phil 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
   3603  1.63.2.1      phil 
   3604  1.63.2.1      phil 		/* XXX do WME aggressive mode processing? */
   3605  1.63.2.1      phil 		IEEE80211_UNLOCK(ic);
   3606  1.63.2.1      phil 		return 1;		/* just assume length changed */
   3607  1.63.2.1      phil 	}
   3608  1.63.2.1      phil 
   3609  1.63.2.1      phil 	/*
   3610  1.63.2.1      phil 	 * Handle the quiet time element being added and removed.
   3611  1.63.2.1      phil 	 * Again, for now we just cheat and reconstruct the whole
   3612  1.63.2.1      phil 	 * beacon - that way the gap is provided as appropriate.
   3613  1.63.2.1      phil 	 *
   3614  1.63.2.1      phil 	 * So, track whether we have already added the IE versus
   3615  1.63.2.1      phil 	 * whether we want to be adding the IE.
   3616  1.63.2.1      phil 	 */
   3617  1.63.2.1      phil 	if ((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) &&
   3618  1.63.2.1      phil 	    (vap->iv_quiet == 0)) {
   3619  1.63.2.1      phil 		/*
   3620  1.63.2.1      phil 		 * Quiet time beacon IE enabled, but it's disabled;
   3621  1.63.2.1      phil 		 * recalc
   3622  1.63.2.1      phil 		 */
   3623  1.63.2.1      phil 		vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
   3624  1.63.2.1      phil 		ieee80211_beacon_construct(m,
   3625  1.63.2.1      phil 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
   3626  1.63.2.1      phil 		/* XXX do WME aggressive mode processing? */
   3627  1.63.2.1      phil 		IEEE80211_UNLOCK(ic);
   3628  1.63.2.1      phil 		return 1;		/* just assume length changed */
   3629  1.63.2.1      phil 	}
   3630  1.63.2.1      phil 
   3631  1.63.2.1      phil 	if (((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) == 0) &&
   3632  1.63.2.1      phil 	    (vap->iv_quiet == 1)) {
   3633  1.63.2.1      phil 		/*
   3634  1.63.2.1      phil 		 * Quiet time beacon IE disabled, but it's now enabled;
   3635  1.63.2.1      phil 		 * recalc
   3636  1.63.2.1      phil 		 */
   3637  1.63.2.1      phil 		vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
   3638  1.63.2.1      phil 		ieee80211_beacon_construct(m,
   3639  1.63.2.1      phil 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
   3640  1.63.2.1      phil 		/* XXX do WME aggressive mode processing? */
   3641  1.63.2.1      phil 		IEEE80211_UNLOCK(ic);
   3642  1.63.2.1      phil 		return 1;		/* just assume length changed */
   3643  1.63.2.1      phil 	}
   3644  1.63.2.1      phil 
   3645  1.63.2.1      phil 	wh = mtod(m, struct ieee80211_frame *);
   3646      1.29    dyoung 
   3647  1.63.2.1      phil 	/*
   3648  1.63.2.1      phil 	 * XXX TODO Strictly speaking this should be incremented with the TX
   3649  1.63.2.1      phil 	 * lock held so as to serialise access to the non-qos TID sequence
   3650  1.63.2.1      phil 	 * number space.
   3651  1.63.2.1      phil 	 *
   3652  1.63.2.1      phil 	 * If the driver identifies it does its own TX seqno management then
   3653  1.63.2.1      phil 	 * we can skip this (and still not do the TX seqno.)
   3654  1.63.2.1      phil 	 */
   3655  1.63.2.1      phil 	seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
   3656  1.63.2.1      phil 	*(uint16_t *)&wh->i_seq[0] =
   3657  1.63.2.1      phil 		htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
   3658  1.63.2.1      phil 	M_SEQNO_SET(m, seqno);
   3659      1.61      maxv 
   3660      1.29    dyoung 	/* XXX faster to recalculate entirely or just changes? */
   3661  1.63.2.1      phil 	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
   3662      1.29    dyoung 	*bo->bo_caps = htole16(capinfo);
   3663      1.29    dyoung 
   3664  1.63.2.1      phil 	if (vap->iv_flags & IEEE80211_F_WME) {
   3665      1.29    dyoung 		struct ieee80211_wme_state *wme = &ic->ic_wme;
   3666      1.29    dyoung 
   3667      1.29    dyoung 		/*
   3668  1.63.2.1      phil 		 * Check for aggressive mode change.  When there is
   3669      1.29    dyoung 		 * significant high priority traffic in the BSS
   3670      1.29    dyoung 		 * throttle back BE traffic by using conservative
   3671  1.63.2.1      phil 		 * parameters.  Otherwise BE uses aggressive params
   3672      1.29    dyoung 		 * to optimize performance of legacy/non-QoS traffic.
   3673      1.29    dyoung 		 */
   3674      1.29    dyoung 		if (wme->wme_flags & WME_F_AGGRMODE) {
   3675      1.29    dyoung 			if (wme->wme_hipri_traffic >
   3676      1.29    dyoung 			    wme->wme_hipri_switch_thresh) {
   3677  1.63.2.1      phil 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
   3678      1.29    dyoung 				    "%s: traffic %u, disable aggressive mode\n",
   3679      1.29    dyoung 				    __func__, wme->wme_hipri_traffic);
   3680      1.29    dyoung 				wme->wme_flags &= ~WME_F_AGGRMODE;
   3681  1.63.2.1      phil 				ieee80211_wme_updateparams_locked(vap);
   3682      1.29    dyoung 				wme->wme_hipri_traffic =
   3683      1.29    dyoung 					wme->wme_hipri_switch_hysteresis;
   3684      1.29    dyoung 			} else
   3685      1.29    dyoung 				wme->wme_hipri_traffic = 0;
   3686      1.29    dyoung 		} else {
   3687      1.29    dyoung 			if (wme->wme_hipri_traffic <=
   3688      1.29    dyoung 			    wme->wme_hipri_switch_thresh) {
   3689  1.63.2.1      phil 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
   3690      1.29    dyoung 				    "%s: traffic %u, enable aggressive mode\n",
   3691      1.29    dyoung 				    __func__, wme->wme_hipri_traffic);
   3692      1.29    dyoung 				wme->wme_flags |= WME_F_AGGRMODE;
   3693  1.63.2.1      phil 				ieee80211_wme_updateparams_locked(vap);
   3694      1.29    dyoung 				wme->wme_hipri_traffic = 0;
   3695      1.29    dyoung 			} else
   3696      1.29    dyoung 				wme->wme_hipri_traffic =
   3697      1.29    dyoung 					wme->wme_hipri_switch_hysteresis;
   3698      1.29    dyoung 		}
   3699  1.63.2.1      phil 		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
   3700  1.63.2.1      phil 			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
   3701  1.63.2.1      phil 			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
   3702      1.29    dyoung 		}
   3703      1.29    dyoung 	}
   3704      1.29    dyoung 
   3705  1.63.2.1      phil 	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
   3706  1.63.2.1      phil 		ieee80211_ht_update_beacon(vap, bo);
   3707  1.63.2.1      phil 		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
   3708  1.63.2.1      phil 	}
   3709  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_TDMA
   3710  1.63.2.1      phil 	if (vap->iv_caps & IEEE80211_C_TDMA) {
   3711  1.63.2.1      phil 		/*
   3712  1.63.2.1      phil 		 * NB: the beacon is potentially updated every TBTT.
   3713  1.63.2.1      phil 		 */
   3714  1.63.2.1      phil 		ieee80211_tdma_update_beacon(vap, bo);
   3715  1.63.2.1      phil 	}
   3716  1.63.2.1      phil #endif
   3717  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   3718  1.63.2.1      phil 	if (vap->iv_opmode == IEEE80211_M_MBSS)
   3719  1.63.2.1      phil 		ieee80211_mesh_update_beacon(vap, bo);
   3720  1.63.2.1      phil #endif
   3721  1.63.2.1      phil 
   3722  1.63.2.1      phil 	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
   3723  1.63.2.1      phil 	    vap->iv_opmode == IEEE80211_M_MBSS) {	/* NB: no IBSS support*/
   3724      1.29    dyoung 		struct ieee80211_tim_ie *tie =
   3725  1.63.2.1      phil 			(struct ieee80211_tim_ie *) bo->bo_tim;
   3726  1.63.2.1      phil 		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
   3727      1.29    dyoung 			u_int timlen, timoff, i;
   3728      1.29    dyoung 			/*
   3729      1.29    dyoung 			 * ATIM/DTIM needs updating.  If it fits in the
   3730      1.29    dyoung 			 * current space allocated then just copy in the
   3731      1.29    dyoung 			 * new bits.  Otherwise we need to move any trailing
   3732      1.29    dyoung 			 * data to make room.  Note that we know there is
   3733      1.29    dyoung 			 * contiguous space because ieee80211_beacon_allocate
   3734      1.29    dyoung 			 * insures there is space in the mbuf to write a
   3735  1.63.2.1      phil 			 * maximal-size virtual bitmap (based on iv_max_aid).
   3736      1.29    dyoung 			 */
   3737      1.29    dyoung 			/*
   3738      1.29    dyoung 			 * Calculate the bitmap size and offset, copy any
   3739      1.29    dyoung 			 * trailer out of the way, and then copy in the
   3740      1.29    dyoung 			 * new bitmap and update the information element.
   3741      1.29    dyoung 			 * Note that the tim bitmap must contain at least
   3742      1.29    dyoung 			 * one byte and any offset must be even.
   3743      1.29    dyoung 			 */
   3744  1.63.2.1      phil 			if (vap->iv_ps_pending != 0) {
   3745      1.29    dyoung 				timoff = 128;		/* impossibly large */
   3746  1.63.2.1      phil 				for (i = 0; i < vap->iv_tim_len; i++)
   3747  1.63.2.1      phil 					if (vap->iv_tim_bitmap[i]) {
   3748      1.29    dyoung 						timoff = i &~ 1;
   3749      1.29    dyoung 						break;
   3750      1.29    dyoung 					}
   3751  1.63.2.1      phil 				KASSERT(timoff != 128, ("tim bitmap empty!"));
   3752  1.63.2.1      phil 				for (i = vap->iv_tim_len-1; i >= timoff; i--)
   3753  1.63.2.1      phil 					if (vap->iv_tim_bitmap[i])
   3754      1.29    dyoung 						break;
   3755      1.29    dyoung 				timlen = 1 + (i - timoff);
   3756      1.29    dyoung 			} else {
   3757      1.29    dyoung 				timoff = 0;
   3758      1.29    dyoung 				timlen = 1;
   3759      1.29    dyoung 			}
   3760  1.63.2.1      phil 
   3761  1.63.2.1      phil 			/*
   3762  1.63.2.1      phil 			 * TODO: validate this!
   3763  1.63.2.1      phil 			 */
   3764      1.29    dyoung 			if (timlen != bo->bo_tim_len) {
   3765      1.29    dyoung 				/* copy up/down trailer */
   3766  1.63.2.1      phil 				int adjust = tie->tim_bitmap+timlen
   3767  1.63.2.1      phil 					   - bo->bo_tim_trailer;
   3768  1.63.2.1      phil 				ovbcopy(bo->bo_tim_trailer,
   3769  1.63.2.1      phil 				    bo->bo_tim_trailer+adjust,
   3770  1.63.2.1      phil 				    bo->bo_tim_trailer_len);
   3771  1.63.2.1      phil 				bo->bo_tim_trailer += adjust;
   3772  1.63.2.1      phil 				bo->bo_erp += adjust;
   3773  1.63.2.1      phil 				bo->bo_htinfo += adjust;
   3774  1.63.2.1      phil 				bo->bo_vhtinfo += adjust;
   3775  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   3776  1.63.2.1      phil 				bo->bo_ath += adjust;
   3777  1.63.2.1      phil #endif
   3778  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_TDMA
   3779  1.63.2.1      phil 				bo->bo_tdma += adjust;
   3780  1.63.2.1      phil #endif
   3781  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   3782  1.63.2.1      phil 				bo->bo_meshconf += adjust;
   3783  1.63.2.1      phil #endif
   3784  1.63.2.1      phil 				bo->bo_appie += adjust;
   3785  1.63.2.1      phil 				bo->bo_wme += adjust;
   3786  1.63.2.1      phil 				bo->bo_csa += adjust;
   3787  1.63.2.1      phil 				bo->bo_quiet += adjust;
   3788      1.29    dyoung 				bo->bo_tim_len = timlen;
   3789      1.29    dyoung 
   3790      1.29    dyoung 				/* update information element */
   3791      1.29    dyoung 				tie->tim_len = 3 + timlen;
   3792      1.29    dyoung 				tie->tim_bitctl = timoff;
   3793      1.29    dyoung 				len_changed = 1;
   3794      1.29    dyoung 			}
   3795  1.63.2.1      phil 			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
   3796      1.29    dyoung 				bo->bo_tim_len);
   3797      1.29    dyoung 
   3798  1.63.2.1      phil 			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
   3799      1.29    dyoung 
   3800  1.63.2.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
   3801      1.29    dyoung 				"%s: TIM updated, pending %u, off %u, len %u\n",
   3802  1.63.2.1      phil 				__func__, vap->iv_ps_pending, timoff, timlen);
   3803      1.29    dyoung 		}
   3804      1.29    dyoung 		/* count down DTIM period */
   3805      1.29    dyoung 		if (tie->tim_count == 0)
   3806      1.29    dyoung 			tie->tim_count = tie->tim_period - 1;
   3807      1.29    dyoung 		else
   3808      1.29    dyoung 			tie->tim_count--;
   3809      1.29    dyoung 		/* update state for buffered multicast frames on DTIM */
   3810  1.63.2.1      phil 		if (mcast && tie->tim_count == 0)
   3811      1.29    dyoung 			tie->tim_bitctl |= 1;
   3812      1.29    dyoung 		else
   3813      1.29    dyoung 			tie->tim_bitctl &= ~1;
   3814  1.63.2.1      phil 		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
   3815  1.63.2.1      phil 			struct ieee80211_csa_ie *csa =
   3816  1.63.2.1      phil 			    (struct ieee80211_csa_ie *) bo->bo_csa;
   3817  1.63.2.1      phil 
   3818  1.63.2.1      phil 			/*
   3819  1.63.2.1      phil 			 * Insert or update CSA ie.  If we're just starting
   3820  1.63.2.1      phil 			 * to count down to the channel switch then we need
   3821  1.63.2.1      phil 			 * to insert the CSA ie.  Otherwise we just need to
   3822  1.63.2.1      phil 			 * drop the count.  The actual change happens above
   3823  1.63.2.1      phil 			 * when the vap's count reaches the target count.
   3824  1.63.2.1      phil 			 */
   3825  1.63.2.1      phil 			if (vap->iv_csa_count == 0) {
   3826  1.63.2.1      phil 				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
   3827  1.63.2.1      phil 				bo->bo_erp += sizeof(*csa);
   3828  1.63.2.1      phil 				bo->bo_htinfo += sizeof(*csa);
   3829  1.63.2.1      phil 				bo->bo_vhtinfo += sizeof(*csa);
   3830  1.63.2.1      phil 				bo->bo_wme += sizeof(*csa);
   3831  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   3832  1.63.2.1      phil 				bo->bo_ath += sizeof(*csa);
   3833  1.63.2.1      phil #endif
   3834  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_TDMA
   3835  1.63.2.1      phil 				bo->bo_tdma += sizeof(*csa);
   3836  1.63.2.1      phil #endif
   3837  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_MESH
   3838  1.63.2.1      phil 				bo->bo_meshconf += sizeof(*csa);
   3839  1.63.2.1      phil #endif
   3840  1.63.2.1      phil 				bo->bo_appie += sizeof(*csa);
   3841  1.63.2.1      phil 				bo->bo_csa_trailer_len += sizeof(*csa);
   3842  1.63.2.1      phil 				bo->bo_quiet += sizeof(*csa);
   3843  1.63.2.1      phil 				bo->bo_tim_trailer_len += sizeof(*csa);
   3844  1.63.2.1      phil 				m->m_len += sizeof(*csa);
   3845  1.63.2.1      phil 				m->m_pkthdr.len += sizeof(*csa);
   3846  1.63.2.1      phil 
   3847  1.63.2.1      phil 				ieee80211_add_csa(bo->bo_csa, vap);
   3848  1.63.2.1      phil 			} else
   3849  1.63.2.1      phil 				csa->csa_count--;
   3850  1.63.2.1      phil 			vap->iv_csa_count++;
   3851  1.63.2.1      phil 			/* NB: don't clear IEEE80211_BEACON_CSA */
   3852  1.63.2.1      phil 		}
   3853  1.63.2.1      phil 
   3854  1.63.2.1      phil 		/*
   3855  1.63.2.1      phil 		 * Only add the quiet time IE if we've enabled it
   3856  1.63.2.1      phil 		 * as appropriate.
   3857  1.63.2.1      phil 		 */
   3858  1.63.2.1      phil 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
   3859  1.63.2.1      phil 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
   3860  1.63.2.1      phil 			if (vap->iv_quiet &&
   3861  1.63.2.1      phil 			    (vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE)) {
   3862  1.63.2.1      phil 				ieee80211_add_quiet(bo->bo_quiet, vap, 1);
   3863  1.63.2.1      phil 			}
   3864  1.63.2.1      phil 		}
   3865  1.63.2.1      phil 		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
   3866  1.63.2.1      phil 			/*
   3867  1.63.2.1      phil 			 * ERP element needs updating.
   3868  1.63.2.1      phil 			 */
   3869  1.63.2.1      phil 			(void) ieee80211_add_erp(bo->bo_erp, ic);
   3870  1.63.2.1      phil 			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
   3871  1.63.2.1      phil 		}
   3872  1.63.2.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
   3873  1.63.2.1      phil 		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
   3874  1.63.2.1      phil 			ieee80211_add_athcaps(bo->bo_ath, ni);
   3875  1.63.2.1      phil 			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
   3876  1.63.2.1      phil 		}
   3877  1.63.2.1      phil #endif
   3878      1.29    dyoung 	}
   3879  1.63.2.1      phil 	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
   3880  1.63.2.1      phil 		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
   3881  1.63.2.1      phil 		int aielen;
   3882  1.63.2.1      phil 		uint8_t *frm;
   3883  1.63.2.1      phil 
   3884  1.63.2.1      phil 		aielen = 0;
   3885  1.63.2.1      phil 		if (aie != NULL)
   3886  1.63.2.1      phil 			aielen += aie->ie_len;
   3887  1.63.2.1      phil 		if (aielen != bo->bo_appie_len) {
   3888  1.63.2.1      phil 			/* copy up/down trailer */
   3889  1.63.2.1      phil 			int adjust = aielen - bo->bo_appie_len;
   3890  1.63.2.1      phil 			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
   3891  1.63.2.1      phil 				bo->bo_tim_trailer_len);
   3892  1.63.2.1      phil 			bo->bo_tim_trailer += adjust;
   3893  1.63.2.1      phil 			bo->bo_appie += adjust;
   3894  1.63.2.1      phil 			bo->bo_appie_len = aielen;
   3895      1.61      maxv 
   3896  1.63.2.1      phil 			len_changed = 1;
   3897  1.63.2.1      phil 		}
   3898  1.63.2.1      phil 		frm = bo->bo_appie;
   3899  1.63.2.1      phil 		if (aie != NULL)
   3900  1.63.2.1      phil 			frm  = add_appie(frm, aie);
   3901  1.63.2.1      phil 		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
   3902  1.63.2.1      phil 	}
   3903  1.63.2.1      phil 	IEEE80211_UNLOCK(ic);
   3904      1.29    dyoung 
   3905      1.29    dyoung 	return len_changed;
   3906      1.29    dyoung }
   3907      1.29    dyoung 
   3908      1.29    dyoung /*
   3909  1.63.2.1      phil  * Do Ethernet-LLC encapsulation for each payload in a fast frame
   3910  1.63.2.1      phil  * tunnel encapsulation.  The frame is assumed to have an Ethernet
   3911  1.63.2.1      phil  * header at the front that must be stripped before prepending the
   3912  1.63.2.1      phil  * LLC followed by the Ethernet header passed in (with an Ethernet
   3913  1.63.2.1      phil  * type that specifies the payload size).
   3914      1.29    dyoung  */
   3915  1.63.2.1      phil struct mbuf *
   3916  1.63.2.1      phil ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
   3917  1.63.2.1      phil 	const struct ether_header *eh)
   3918       1.6    dyoung {
   3919  1.63.2.1      phil 	struct llc *llc;
   3920  1.63.2.1      phil 	uint16_t payload;
   3921       1.6    dyoung 
   3922  1.63.2.1      phil 	/* XXX optimize by combining m_adj+M_PREPEND */
   3923  1.63.2.1      phil 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
   3924  1.63.2.1      phil 	llc = mtod(m, struct llc *);
   3925  1.63.2.1      phil 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
   3926  1.63.2.1      phil 	llc->llc_control = LLC_UI;
   3927  1.63.2.1      phil 	llc->llc_snap.org_code[0] = 0;
   3928  1.63.2.1      phil 	llc->llc_snap.org_code[1] = 0;
   3929  1.63.2.1      phil 	llc->llc_snap.org_code[2] = 0;
   3930  1.63.2.1      phil 	llc->llc_snap.ether_type = eh->ether_type;
   3931  1.63.2.1      phil 	payload = m->m_pkthdr.len;		/* NB: w/o Ethernet header */
   3932      1.61      maxv 
   3933  1.63.2.1      phil 	M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
   3934  1.63.2.1      phil 	if (m == NULL) {		/* XXX cannot happen */
   3935  1.63.2.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
   3936  1.63.2.1      phil 			"%s: no space for ether_header\n", __func__);
   3937  1.63.2.1      phil 		vap->iv_stats.is_tx_nobuf++;
   3938  1.63.2.1      phil 		return NULL;
   3939       1.6    dyoung 	}
   3940  1.63.2.1      phil 	ETHER_HEADER_COPY(mtod(m, void *), eh);
   3941  1.63.2.1      phil 	mtod(m, struct ether_header *)->ether_type = htons(payload);
   3942  1.63.2.1      phil 	return m;
   3943  1.63.2.1      phil }
   3944      1.61      maxv 
   3945  1.63.2.1      phil /*
   3946  1.63.2.1      phil  * Complete an mbuf transmission.
   3947  1.63.2.1      phil  *
   3948  1.63.2.1      phil  * For now, this simply processes a completed frame after the
   3949  1.63.2.1      phil  * driver has completed it's transmission and/or retransmission.
   3950  1.63.2.1      phil  * It assumes the frame is an 802.11 encapsulated frame.
   3951  1.63.2.1      phil  *
   3952  1.63.2.1      phil  * Later on it will grow to become the exit path for a given frame
   3953  1.63.2.1      phil  * from the driver and, depending upon how it's been encapsulated
   3954  1.63.2.1      phil  * and already transmitted, it may end up doing A-MPDU retransmission,
   3955  1.63.2.1      phil  * power save requeuing, etc.
   3956  1.63.2.1      phil  *
   3957  1.63.2.1      phil  * In order for the above to work, the driver entry point to this
   3958  1.63.2.1      phil  * must not hold any driver locks.  Thus, the driver needs to delay
   3959  1.63.2.1      phil  * any actual mbuf completion until it can release said locks.
   3960  1.63.2.1      phil  *
   3961  1.63.2.1      phil  * This frees the mbuf and if the mbuf has a node reference,
   3962  1.63.2.1      phil  * the node reference will be freed.
   3963  1.63.2.1      phil  */
   3964  1.63.2.1      phil void
   3965  1.63.2.1      phil ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
   3966  1.63.2.1      phil {
   3967  1.63.2.1      phil 
   3968  1.63.2.1      phil 	if (ni != NULL) {
   3969  1.63.2.1      phil 		struct ifnet *ifp = ni->ni_vap->iv_ifp;
   3970      1.29    dyoung 
   3971  1.63.2.1      phil 		if (status == 0) {
   3972  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
   3973  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
   3974  1.63.2.1      phil 			if (m->m_flags & M_MCAST)
   3975  1.63.2.1      phil 				if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
   3976  1.63.2.1      phil 		} else
   3977  1.63.2.1      phil 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
   3978  1.63.2.1      phil 		if (m->m_flags & M_TXCB)
   3979  1.63.2.1      phil 			ieee80211_process_callback(ni, m, status);
   3980  1.63.2.1      phil 		ieee80211_free_node(ni);
   3981  1.63.2.1      phil 	}
   3982  1.63.2.1      phil 	m_freem(m);
   3983       1.6    dyoung }
   3984