Home | History | Annotate | Line # | Download | only in net80211
ieee80211_output.c revision 1.1.1.3
      1      1.1  dyoung /*-
      2      1.1  dyoung  * Copyright (c) 2001 Atsushi Onoe
      3      1.1  dyoung  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      4      1.1  dyoung  * All rights reserved.
      5      1.1  dyoung  *
      6      1.1  dyoung  * Redistribution and use in source and binary forms, with or without
      7      1.1  dyoung  * modification, are permitted provided that the following conditions
      8      1.1  dyoung  * are met:
      9      1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
     10      1.1  dyoung  *    notice, this list of conditions and the following disclaimer.
     11      1.1  dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  dyoung  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  dyoung  *    documentation and/or other materials provided with the distribution.
     14      1.1  dyoung  * 3. The name of the author may not be used to endorse or promote products
     15      1.1  dyoung  *    derived from this software without specific prior written permission.
     16      1.1  dyoung  *
     17      1.1  dyoung  * Alternatively, this software may be distributed under the terms of the
     18      1.1  dyoung  * GNU General Public License ("GPL") version 2 as published by the Free
     19      1.1  dyoung  * Software Foundation.
     20      1.1  dyoung  *
     21      1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1  dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1  dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1  dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1  dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1  dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1  dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1  dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1  dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1  dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1  dyoung  */
     32      1.1  dyoung 
     33      1.1  dyoung #include <sys/cdefs.h>
     34  1.1.1.3  dyoung __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_output.c,v 1.9 2003/10/17 23:15:30 sam Exp $");
     35      1.1  dyoung 
     36      1.1  dyoung #include "opt_inet.h"
     37      1.1  dyoung 
     38      1.1  dyoung #include <sys/param.h>
     39      1.1  dyoung #include <sys/systm.h>
     40      1.1  dyoung #include <sys/mbuf.h>
     41      1.1  dyoung #include <sys/malloc.h>
     42      1.1  dyoung #include <sys/kernel.h>
     43      1.1  dyoung #include <sys/socket.h>
     44      1.1  dyoung #include <sys/sockio.h>
     45      1.1  dyoung #include <sys/endian.h>
     46      1.1  dyoung #include <sys/errno.h>
     47      1.1  dyoung #include <sys/bus.h>
     48      1.1  dyoung #include <sys/proc.h>
     49      1.1  dyoung #include <sys/sysctl.h>
     50      1.1  dyoung 
     51      1.1  dyoung #include <machine/atomic.h>
     52      1.1  dyoung 
     53      1.1  dyoung #include <net/if.h>
     54      1.1  dyoung #include <net/if_dl.h>
     55      1.1  dyoung #include <net/if_media.h>
     56      1.1  dyoung #include <net/if_arp.h>
     57      1.1  dyoung #include <net/ethernet.h>
     58      1.1  dyoung #include <net/if_llc.h>
     59      1.1  dyoung 
     60      1.1  dyoung #include <net80211/ieee80211_var.h>
     61      1.1  dyoung 
     62      1.1  dyoung #include <net/bpf.h>
     63      1.1  dyoung 
     64      1.1  dyoung #ifdef INET
     65      1.1  dyoung #include <netinet/in.h>
     66      1.1  dyoung #include <netinet/if_ether.h>
     67      1.1  dyoung #endif
     68      1.1  dyoung 
     69      1.1  dyoung /*
     70      1.1  dyoung  * Send a management frame to the specified node.  The node pointer
     71      1.1  dyoung  * must have a reference as the pointer will be passed to the driver
     72      1.1  dyoung  * and potentially held for a long time.  If the frame is successfully
     73      1.1  dyoung  * dispatched to the driver, then it is responsible for freeing the
     74      1.1  dyoung  * reference (and potentially free'ing up any associated storage).
     75      1.1  dyoung  */
     76      1.1  dyoung static int
     77      1.1  dyoung ieee80211_mgmt_output(struct ifnet *ifp, struct ieee80211_node *ni,
     78      1.1  dyoung     struct mbuf *m, int type)
     79      1.1  dyoung {
     80      1.1  dyoung 	struct ieee80211com *ic = (void *)ifp;
     81      1.1  dyoung 	struct ieee80211_frame *wh;
     82      1.1  dyoung 
     83      1.1  dyoung 	KASSERT(ni != NULL, ("null node"));
     84      1.1  dyoung 	ni->ni_inact = 0;
     85      1.1  dyoung 
     86      1.1  dyoung 	/*
     87      1.1  dyoung 	 * Yech, hack alert!  We want to pass the node down to the
     88      1.1  dyoung 	 * driver's start routine.  If we don't do so then the start
     89      1.1  dyoung 	 * routine must immediately look it up again and that can
     90      1.1  dyoung 	 * cause a lock order reversal if, for example, this frame
     91      1.1  dyoung 	 * is being sent because the station is being timedout and
     92      1.1  dyoung 	 * the frame being sent is a DEAUTH message.  We could stick
     93      1.1  dyoung 	 * this in an m_tag and tack that on to the mbuf.  However
     94      1.1  dyoung 	 * that's rather expensive to do for every frame so instead
     95      1.1  dyoung 	 * we stuff it in the rcvif field since outbound frames do
     96      1.1  dyoung 	 * not (presently) use this.
     97      1.1  dyoung 	 */
     98      1.1  dyoung 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
     99      1.1  dyoung 	if (m == NULL)
    100      1.1  dyoung 		return ENOMEM;
    101      1.1  dyoung 	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
    102      1.1  dyoung 	m->m_pkthdr.rcvif = (void *)ni;
    103      1.1  dyoung 
    104      1.1  dyoung 	wh = mtod(m, struct ieee80211_frame *);
    105      1.1  dyoung 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
    106      1.1  dyoung 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    107      1.1  dyoung 	*(u_int16_t *)wh->i_dur = 0;
    108      1.1  dyoung 	*(u_int16_t *)wh->i_seq =
    109      1.1  dyoung 	    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
    110      1.1  dyoung 	ni->ni_txseq++;
    111      1.1  dyoung 	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
    112      1.1  dyoung 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
    113      1.1  dyoung 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
    114      1.1  dyoung 
    115      1.1  dyoung 	if (ifp->if_flags & IFF_DEBUG) {
    116      1.1  dyoung 		/* avoid to print too many frames */
    117      1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
    118      1.1  dyoung #ifdef IEEE80211_DEBUG
    119      1.1  dyoung 		    ieee80211_debug > 1 ||
    120      1.1  dyoung #endif
    121      1.1  dyoung 		    (type & IEEE80211_FC0_SUBTYPE_MASK) !=
    122      1.1  dyoung 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
    123      1.1  dyoung 			if_printf(ifp, "sending %s to %s on channel %u\n",
    124      1.1  dyoung 			    ieee80211_mgt_subtype_name[
    125      1.1  dyoung 			    (type & IEEE80211_FC0_SUBTYPE_MASK)
    126      1.1  dyoung 			    >> IEEE80211_FC0_SUBTYPE_SHIFT],
    127      1.1  dyoung 			    ether_sprintf(ni->ni_macaddr),
    128      1.1  dyoung 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    129      1.1  dyoung 	}
    130      1.1  dyoung 
    131      1.1  dyoung 	IF_ENQUEUE(&ic->ic_mgtq, m);
    132      1.1  dyoung 	ifp->if_timer = 1;
    133      1.1  dyoung 	(*ifp->if_start)(ifp);
    134      1.1  dyoung 	return 0;
    135      1.1  dyoung }
    136      1.1  dyoung 
    137      1.1  dyoung /*
    138      1.1  dyoung  * Encapsulate an outbound data frame.  The mbuf chain is updated and
    139      1.1  dyoung  * a reference to the destination node is returned.  If an error is
    140      1.1  dyoung  * encountered NULL is returned and the node reference will also be NULL.
    141      1.1  dyoung  *
    142      1.1  dyoung  * NB: The caller is responsible for free'ing a returned node reference.
    143      1.1  dyoung  *     The convention is ic_bss is not reference counted; the caller must
    144      1.1  dyoung  *     maintain that.
    145      1.1  dyoung  */
    146      1.1  dyoung struct mbuf *
    147      1.1  dyoung ieee80211_encap(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node **pni)
    148      1.1  dyoung {
    149      1.1  dyoung 	struct ieee80211com *ic = (void *)ifp;
    150      1.1  dyoung 	struct ether_header eh;
    151      1.1  dyoung 	struct ieee80211_frame *wh;
    152      1.1  dyoung 	struct ieee80211_node *ni = NULL;
    153      1.1  dyoung 	struct llc *llc;
    154      1.1  dyoung 
    155      1.1  dyoung 	if (m->m_len < sizeof(struct ether_header)) {
    156      1.1  dyoung 		m = m_pullup(m, sizeof(struct ether_header));
    157      1.1  dyoung 		if (m == NULL) {
    158  1.1.1.3  dyoung 			ic->ic_stats.is_tx_nombuf++;
    159      1.1  dyoung 			goto bad;
    160      1.1  dyoung 		}
    161      1.1  dyoung 	}
    162      1.1  dyoung 	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
    163      1.1  dyoung 
    164      1.1  dyoung 	if (ic->ic_opmode != IEEE80211_M_STA) {
    165      1.1  dyoung 		ni = ieee80211_find_node(ic, eh.ether_dhost);
    166      1.1  dyoung 		if (ni == NULL) {
    167      1.1  dyoung 			/*
    168  1.1.1.3  dyoung 			 * When not in station mode the destination
    169  1.1.1.3  dyoung 			 * address should always be in the node table
    170  1.1.1.3  dyoung 			 * if the device sends management frames to us;
    171  1.1.1.3  dyoung 			 * unless this is a multicast/broadcast frame.
    172  1.1.1.3  dyoung 			 * For devices that don't send management frames
    173  1.1.1.3  dyoung 			 * to the host we have to cheat; use the bss
    174  1.1.1.3  dyoung 			 * node instead; the card will/should clobber
    175  1.1.1.3  dyoung 			 * the bssid address as necessary.
    176  1.1.1.3  dyoung 			 *
    177  1.1.1.3  dyoung 			 * XXX this handles AHDEMO because all devices
    178  1.1.1.3  dyoung 			 *     that support it don't send mgmt frames;
    179  1.1.1.3  dyoung 			 *     but it might be better to test explicitly
    180      1.1  dyoung 			 */
    181  1.1.1.3  dyoung 			if (!IEEE80211_IS_MULTICAST(eh.ether_dhost) &&
    182  1.1.1.3  dyoung 			    (ic->ic_caps & IEEE80211_C_RCVMGT)) {
    183  1.1.1.3  dyoung 				IEEE80211_DPRINTF(("%s: no node for dst %s, "
    184  1.1.1.3  dyoung 					"discard frame\n", __func__,
    185  1.1.1.3  dyoung 					ether_sprintf(eh.ether_dhost)));
    186  1.1.1.3  dyoung 				ic->ic_stats.is_tx_nonode++;
    187      1.1  dyoung 				goto bad;
    188      1.1  dyoung 			}
    189      1.1  dyoung 			ni = ic->ic_bss;
    190      1.1  dyoung 		}
    191      1.1  dyoung 	} else
    192      1.1  dyoung 		ni = ic->ic_bss;
    193      1.1  dyoung 	ni->ni_inact = 0;
    194      1.1  dyoung 
    195      1.1  dyoung 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
    196      1.1  dyoung 	llc = mtod(m, struct llc *);
    197      1.1  dyoung 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
    198      1.1  dyoung 	llc->llc_control = LLC_UI;
    199      1.1  dyoung 	llc->llc_snap.org_code[0] = 0;
    200      1.1  dyoung 	llc->llc_snap.org_code[1] = 0;
    201      1.1  dyoung 	llc->llc_snap.org_code[2] = 0;
    202      1.1  dyoung 	llc->llc_snap.ether_type = eh.ether_type;
    203      1.1  dyoung 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
    204  1.1.1.3  dyoung 	if (m == NULL) {
    205  1.1.1.3  dyoung 		ic->ic_stats.is_tx_nombuf++;
    206      1.1  dyoung 		goto bad;
    207  1.1.1.3  dyoung 	}
    208      1.1  dyoung 	wh = mtod(m, struct ieee80211_frame *);
    209      1.1  dyoung 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
    210      1.1  dyoung 	*(u_int16_t *)wh->i_dur = 0;
    211      1.1  dyoung 	*(u_int16_t *)wh->i_seq =
    212      1.1  dyoung 	    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
    213      1.1  dyoung 	ni->ni_txseq++;
    214      1.1  dyoung 	switch (ic->ic_opmode) {
    215      1.1  dyoung 	case IEEE80211_M_STA:
    216      1.1  dyoung 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
    217      1.1  dyoung 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
    218      1.1  dyoung 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
    219      1.1  dyoung 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
    220      1.1  dyoung 		break;
    221      1.1  dyoung 	case IEEE80211_M_IBSS:
    222      1.1  dyoung 	case IEEE80211_M_AHDEMO:
    223      1.1  dyoung 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    224      1.1  dyoung 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
    225      1.1  dyoung 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
    226      1.1  dyoung 		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
    227      1.1  dyoung 		break;
    228      1.1  dyoung 	case IEEE80211_M_HOSTAP:
    229      1.1  dyoung 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
    230      1.1  dyoung 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
    231      1.1  dyoung 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
    232      1.1  dyoung 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
    233      1.1  dyoung 		break;
    234      1.1  dyoung 	case IEEE80211_M_MONITOR:
    235      1.1  dyoung 		goto bad;
    236      1.1  dyoung 	}
    237      1.1  dyoung 	*pni = ni;
    238      1.1  dyoung 	return m;
    239      1.1  dyoung bad:
    240      1.1  dyoung 	if (m != NULL)
    241      1.1  dyoung 		m_freem(m);
    242      1.1  dyoung 	if (ni && ni != ic->ic_bss)
    243      1.1  dyoung 		ieee80211_free_node(ic, ni);
    244      1.1  dyoung 	*pni = NULL;
    245      1.1  dyoung 	return NULL;
    246      1.1  dyoung }
    247      1.1  dyoung 
    248      1.1  dyoung /*
    249      1.1  dyoung  * Add a supported rates element id to a frame.
    250      1.1  dyoung  */
    251      1.1  dyoung u_int8_t *
    252      1.1  dyoung ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    253      1.1  dyoung {
    254      1.1  dyoung 	int nrates;
    255      1.1  dyoung 
    256      1.1  dyoung 	*frm++ = IEEE80211_ELEMID_RATES;
    257      1.1  dyoung 	nrates = rs->rs_nrates;
    258      1.1  dyoung 	if (nrates > IEEE80211_RATE_SIZE)
    259      1.1  dyoung 		nrates = IEEE80211_RATE_SIZE;
    260      1.1  dyoung 	*frm++ = nrates;
    261      1.1  dyoung 	memcpy(frm, rs->rs_rates, nrates);
    262      1.1  dyoung 	return frm + nrates;
    263      1.1  dyoung }
    264      1.1  dyoung 
    265      1.1  dyoung /*
    266      1.1  dyoung  * Add an extended supported rates element id to a frame.
    267      1.1  dyoung  */
    268      1.1  dyoung u_int8_t *
    269      1.1  dyoung ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    270      1.1  dyoung {
    271      1.1  dyoung 	/*
    272      1.1  dyoung 	 * Add an extended supported rates element if operating in 11g mode.
    273      1.1  dyoung 	 */
    274      1.1  dyoung 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
    275      1.1  dyoung 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
    276      1.1  dyoung 		*frm++ = IEEE80211_ELEMID_XRATES;
    277      1.1  dyoung 		*frm++ = nrates;
    278      1.1  dyoung 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
    279      1.1  dyoung 		frm += nrates;
    280      1.1  dyoung 	}
    281      1.1  dyoung 	return frm;
    282      1.1  dyoung }
    283      1.1  dyoung 
    284      1.1  dyoung /*
    285      1.1  dyoung  * Add an ssid elemet to a frame.
    286      1.1  dyoung  */
    287      1.1  dyoung static u_int8_t *
    288      1.1  dyoung ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
    289      1.1  dyoung {
    290      1.1  dyoung 	*frm++ = IEEE80211_ELEMID_SSID;
    291      1.1  dyoung 	*frm++ = len;
    292      1.1  dyoung 	memcpy(frm, ssid, len);
    293      1.1  dyoung 	return frm + len;
    294      1.1  dyoung }
    295      1.1  dyoung 
    296      1.1  dyoung static struct mbuf *
    297      1.1  dyoung ieee80211_getmbuf(int flags, int type, u_int pktlen)
    298      1.1  dyoung {
    299      1.1  dyoung 	struct mbuf *m;
    300      1.1  dyoung 
    301  1.1.1.2  dyoung 	KASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
    302  1.1.1.2  dyoung 	if (pktlen <= MHLEN)
    303      1.1  dyoung 		MGETHDR(m, flags, type);
    304      1.1  dyoung 	else
    305      1.1  dyoung 		m = m_getcl(flags, type, M_PKTHDR);
    306      1.1  dyoung 	return m;
    307      1.1  dyoung }
    308      1.1  dyoung 
    309      1.1  dyoung /*
    310      1.1  dyoung  * Send a management frame.  The node is for the destination (or ic_bss
    311      1.1  dyoung  * when in station mode).  Nodes other than ic_bss have their reference
    312      1.1  dyoung  * count bumped to reflect our use for an indeterminant time.
    313      1.1  dyoung  */
    314      1.1  dyoung int
    315      1.1  dyoung ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
    316      1.1  dyoung 	int type, int arg)
    317      1.1  dyoung {
    318  1.1.1.3  dyoung #define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
    319      1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
    320      1.1  dyoung 	struct mbuf *m;
    321      1.1  dyoung 	u_int8_t *frm;
    322      1.1  dyoung 	enum ieee80211_phymode mode;
    323      1.1  dyoung 	u_int16_t capinfo;
    324      1.1  dyoung 	int ret, timer;
    325      1.1  dyoung 
    326      1.1  dyoung 	KASSERT(ni != NULL, ("null node"));
    327      1.1  dyoung 
    328      1.1  dyoung 	/*
    329      1.1  dyoung 	 * Hold a reference on the node so it doesn't go away until after
    330      1.1  dyoung 	 * the xmit is complete all the way in the driver.  On error we
    331      1.1  dyoung 	 * will remove our reference.
    332      1.1  dyoung 	 */
    333      1.1  dyoung 	if (ni != ic->ic_bss)
    334      1.1  dyoung 		ieee80211_ref_node(ni);
    335      1.1  dyoung 	timer = 0;
    336      1.1  dyoung 	switch (type) {
    337      1.1  dyoung 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
    338      1.1  dyoung 		/*
    339      1.1  dyoung 		 * prreq frame format
    340      1.1  dyoung 		 *	[tlv] ssid
    341      1.1  dyoung 		 *	[tlv] supported rates
    342      1.1  dyoung 		 *	[tlv] extended supported rates
    343      1.1  dyoung 		 */
    344      1.1  dyoung 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    345      1.1  dyoung 			 2 + ic->ic_des_esslen
    346      1.1  dyoung 		       + 2 + IEEE80211_RATE_SIZE
    347      1.1  dyoung 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    348      1.1  dyoung 		if (m == NULL)
    349  1.1.1.3  dyoung 			senderr(ENOMEM, is_tx_nombuf);
    350      1.1  dyoung 		m->m_data += sizeof(struct ieee80211_frame);
    351      1.1  dyoung 		frm = mtod(m, u_int8_t *);
    352      1.1  dyoung 		frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
    353      1.1  dyoung 		mode = ieee80211_chan2mode(ic, ni->ni_chan);
    354      1.1  dyoung 		frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
    355      1.1  dyoung 		frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
    356      1.1  dyoung 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    357      1.1  dyoung 
    358      1.1  dyoung 		timer = IEEE80211_TRANS_WAIT;
    359      1.1  dyoung 		break;
    360      1.1  dyoung 
    361      1.1  dyoung 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    362      1.1  dyoung 		/*
    363      1.1  dyoung 		 * probe response frame format
    364      1.1  dyoung 		 *	[8] time stamp
    365      1.1  dyoung 		 *	[2] beacon interval
    366      1.1  dyoung 		 *	[2] cabability information
    367      1.1  dyoung 		 *	[tlv] ssid
    368      1.1  dyoung 		 *	[tlv] supported rates
    369  1.1.1.3  dyoung 		 *	[tlv] parameter set (FH/DS)
    370      1.1  dyoung 		 *	[tlv] parameter set (IBSS)
    371      1.1  dyoung 		 *	[tlv] extended supported rates
    372      1.1  dyoung 		 */
    373      1.1  dyoung 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    374      1.1  dyoung 			 8 + 2 + 2 + 2
    375      1.1  dyoung 		       + 2 + ni->ni_esslen
    376      1.1  dyoung 		       + 2 + IEEE80211_RATE_SIZE
    377  1.1.1.3  dyoung 		       + (ic->ic_phytype == IEEE80211_T_FH ? 7 : 3)
    378      1.1  dyoung 		       + 6
    379      1.1  dyoung 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    380      1.1  dyoung 		if (m == NULL)
    381  1.1.1.3  dyoung 			senderr(ENOMEM, is_tx_nombuf);
    382      1.1  dyoung 		m->m_data += sizeof(struct ieee80211_frame);
    383      1.1  dyoung 		frm = mtod(m, u_int8_t *);
    384      1.1  dyoung 
    385      1.1  dyoung 		memset(frm, 0, 8);	/* timestamp should be filled later */
    386      1.1  dyoung 		frm += 8;
    387      1.1  dyoung 		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
    388      1.1  dyoung 		frm += 2;
    389      1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    390      1.1  dyoung 			capinfo = IEEE80211_CAPINFO_IBSS;
    391      1.1  dyoung 		else
    392      1.1  dyoung 			capinfo = IEEE80211_CAPINFO_ESS;
    393      1.1  dyoung 		if (ic->ic_flags & IEEE80211_F_WEPON)
    394      1.1  dyoung 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    395  1.1.1.3  dyoung 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    396  1.1.1.3  dyoung 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    397  1.1.1.3  dyoung 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    398      1.1  dyoung 		*(u_int16_t *)frm = htole16(capinfo);
    399      1.1  dyoung 		frm += 2;
    400      1.1  dyoung 
    401      1.1  dyoung 		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
    402      1.1  dyoung 				ic->ic_bss->ni_esslen);
    403      1.1  dyoung 		frm = ieee80211_add_rates(frm, &ic->ic_bss->ni_rates);
    404      1.1  dyoung 
    405  1.1.1.3  dyoung 		if (ic->ic_phytype == IEEE80211_T_FH) {
    406  1.1.1.3  dyoung                         *frm++ = IEEE80211_ELEMID_FHPARMS;
    407  1.1.1.3  dyoung                         *frm++ = 5;
    408  1.1.1.3  dyoung                         *frm++ = ni->ni_fhdwell & 0x00ff;
    409  1.1.1.3  dyoung                         *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
    410  1.1.1.3  dyoung                         *frm++ = IEEE80211_FH_CHANSET(
    411  1.1.1.3  dyoung 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    412  1.1.1.3  dyoung                         *frm++ = IEEE80211_FH_CHANPAT(
    413  1.1.1.3  dyoung 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    414  1.1.1.3  dyoung                         *frm++ = ni->ni_fhindex;
    415  1.1.1.3  dyoung 		} else {
    416  1.1.1.3  dyoung 			*frm++ = IEEE80211_ELEMID_DSPARMS;
    417  1.1.1.3  dyoung 			*frm++ = 1;
    418  1.1.1.3  dyoung 			*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
    419  1.1.1.3  dyoung 		}
    420  1.1.1.3  dyoung 
    421      1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
    422      1.1  dyoung 			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
    423      1.1  dyoung 			*frm++ = 2;
    424      1.1  dyoung 			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
    425      1.1  dyoung 		} else {	/* IEEE80211_M_HOSTAP */
    426      1.1  dyoung 			/* TODO: TIM */
    427      1.1  dyoung 			*frm++ = IEEE80211_ELEMID_TIM;
    428      1.1  dyoung 			*frm++ = 4;	/* length */
    429      1.1  dyoung 			*frm++ = 0;	/* DTIM count */
    430      1.1  dyoung 			*frm++ = 1;	/* DTIM period */
    431      1.1  dyoung 			*frm++ = 0;	/* bitmap control */
    432      1.1  dyoung 			*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
    433      1.1  dyoung 		}
    434      1.1  dyoung 		frm = ieee80211_add_xrates(frm, &ic->ic_bss->ni_rates);
    435      1.1  dyoung 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    436      1.1  dyoung 		break;
    437      1.1  dyoung 
    438      1.1  dyoung 	case IEEE80211_FC0_SUBTYPE_AUTH:
    439      1.1  dyoung 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    440      1.1  dyoung 		if (m == NULL)
    441  1.1.1.3  dyoung 			senderr(ENOMEM, is_tx_nombuf);
    442      1.1  dyoung 		MH_ALIGN(m, 2 * 3);
    443      1.1  dyoung 		m->m_pkthdr.len = m->m_len = 6;
    444      1.1  dyoung 		frm = mtod(m, u_int8_t *);
    445      1.1  dyoung 		/* TODO: shared key auth */
    446      1.1  dyoung 		((u_int16_t *)frm)[0] = htole16(IEEE80211_AUTH_ALG_OPEN);
    447      1.1  dyoung 		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
    448      1.1  dyoung 		((u_int16_t *)frm)[2] = 0;		/* status */
    449      1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_STA)
    450      1.1  dyoung 			timer = IEEE80211_TRANS_WAIT;
    451      1.1  dyoung 		break;
    452      1.1  dyoung 
    453      1.1  dyoung 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
    454      1.1  dyoung 		if (ifp->if_flags & IFF_DEBUG)
    455      1.1  dyoung 			if_printf(ifp, "station %s deauthenticate (reason %d)\n",
    456      1.1  dyoung 			    ether_sprintf(ni->ni_macaddr), arg);
    457      1.1  dyoung 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    458      1.1  dyoung 		if (m == NULL)
    459  1.1.1.3  dyoung 			senderr(ENOMEM, is_tx_nombuf);
    460      1.1  dyoung 		MH_ALIGN(m, 2);
    461      1.1  dyoung 		m->m_pkthdr.len = m->m_len = 2;
    462      1.1  dyoung 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    463      1.1  dyoung 		break;
    464      1.1  dyoung 
    465      1.1  dyoung 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
    466      1.1  dyoung 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
    467      1.1  dyoung 		/*
    468      1.1  dyoung 		 * asreq frame format
    469      1.1  dyoung 		 *	[2] capability information
    470      1.1  dyoung 		 *	[2] listen interval
    471      1.1  dyoung 		 *	[6*] current AP address (reassoc only)
    472      1.1  dyoung 		 *	[tlv] ssid
    473      1.1  dyoung 		 *	[tlv] supported rates
    474      1.1  dyoung 		 *	[tlv] extended supported rates
    475      1.1  dyoung 		 */
    476      1.1  dyoung 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    477      1.1  dyoung 			 sizeof(capinfo)
    478      1.1  dyoung 		       + sizeof(u_int16_t)
    479      1.1  dyoung 		       + IEEE80211_ADDR_LEN
    480      1.1  dyoung 		       + 2 + ni->ni_esslen
    481      1.1  dyoung 		       + 2 + IEEE80211_RATE_SIZE
    482      1.1  dyoung 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    483      1.1  dyoung 		if (m == NULL)
    484  1.1.1.3  dyoung 			senderr(ENOMEM, is_tx_nombuf);
    485      1.1  dyoung 		m->m_data += sizeof(struct ieee80211_frame);
    486      1.1  dyoung 		frm = mtod(m, u_int8_t *);
    487      1.1  dyoung 
    488      1.1  dyoung 		capinfo = 0;
    489      1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    490      1.1  dyoung 			capinfo |= IEEE80211_CAPINFO_IBSS;
    491      1.1  dyoung 		else		/* IEEE80211_M_STA */
    492      1.1  dyoung 			capinfo |= IEEE80211_CAPINFO_ESS;
    493      1.1  dyoung 		if (ic->ic_flags & IEEE80211_F_WEPON)
    494      1.1  dyoung 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    495  1.1.1.3  dyoung 		/*
    496  1.1.1.3  dyoung 		 * NB: Some 11a AP's reject the request when
    497  1.1.1.3  dyoung 		 *     short premable is set.
    498  1.1.1.3  dyoung 		 */
    499  1.1.1.3  dyoung 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    500  1.1.1.3  dyoung 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    501      1.1  dyoung 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    502      1.1  dyoung 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
    503      1.1  dyoung 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
    504      1.1  dyoung 		*(u_int16_t *)frm = htole16(capinfo);
    505      1.1  dyoung 		frm += 2;
    506      1.1  dyoung 
    507      1.1  dyoung 		*(u_int16_t *)frm = htole16(ic->ic_lintval);
    508      1.1  dyoung 		frm += 2;
    509      1.1  dyoung 
    510      1.1  dyoung 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
    511      1.1  dyoung 			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
    512      1.1  dyoung 			frm += IEEE80211_ADDR_LEN;
    513      1.1  dyoung 		}
    514      1.1  dyoung 
    515      1.1  dyoung 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
    516      1.1  dyoung 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    517      1.1  dyoung 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    518      1.1  dyoung 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    519      1.1  dyoung 
    520      1.1  dyoung 		timer = IEEE80211_TRANS_WAIT;
    521      1.1  dyoung 		break;
    522      1.1  dyoung 
    523      1.1  dyoung 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
    524      1.1  dyoung 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
    525      1.1  dyoung 		/*
    526      1.1  dyoung 		 * asreq frame format
    527      1.1  dyoung 		 *	[2] capability information
    528      1.1  dyoung 		 *	[2] status
    529      1.1  dyoung 		 *	[2] association ID
    530      1.1  dyoung 		 *	[tlv] supported rates
    531      1.1  dyoung 		 *	[tlv] extended supported rates
    532      1.1  dyoung 		 */
    533      1.1  dyoung 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    534      1.1  dyoung 			 sizeof(capinfo)
    535      1.1  dyoung 		       + sizeof(u_int16_t)
    536      1.1  dyoung 		       + sizeof(u_int16_t)
    537      1.1  dyoung 		       + 2 + IEEE80211_RATE_SIZE
    538      1.1  dyoung 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    539      1.1  dyoung 		if (m == NULL)
    540  1.1.1.3  dyoung 			senderr(ENOMEM, is_tx_nombuf);
    541      1.1  dyoung 		m->m_data += sizeof(struct ieee80211_frame);
    542      1.1  dyoung 		frm = mtod(m, u_int8_t *);
    543      1.1  dyoung 
    544      1.1  dyoung 		capinfo = IEEE80211_CAPINFO_ESS;
    545      1.1  dyoung 		if (ic->ic_flags & IEEE80211_F_WEPON)
    546      1.1  dyoung 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    547  1.1.1.3  dyoung 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    548  1.1.1.3  dyoung 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    549  1.1.1.3  dyoung 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    550      1.1  dyoung 		*(u_int16_t *)frm = htole16(capinfo);
    551      1.1  dyoung 		frm += 2;
    552      1.1  dyoung 
    553      1.1  dyoung 		*(u_int16_t *)frm = htole16(arg);	/* status */
    554      1.1  dyoung 		frm += 2;
    555      1.1  dyoung 
    556      1.1  dyoung 		if (arg == IEEE80211_STATUS_SUCCESS)
    557      1.1  dyoung 			*(u_int16_t *)frm = htole16(ni->ni_associd);
    558      1.1  dyoung 		frm += 2;
    559      1.1  dyoung 
    560      1.1  dyoung 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    561      1.1  dyoung 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    562      1.1  dyoung 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    563      1.1  dyoung 		break;
    564      1.1  dyoung 
    565      1.1  dyoung 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
    566      1.1  dyoung 		if (ifp->if_flags & IFF_DEBUG)
    567      1.1  dyoung 			if_printf(ifp, "station %s disassociate (reason %d)\n",
    568      1.1  dyoung 			    ether_sprintf(ni->ni_macaddr), arg);
    569      1.1  dyoung 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    570      1.1  dyoung 		if (m == NULL)
    571  1.1.1.3  dyoung 			senderr(ENOMEM, is_tx_nombuf);
    572      1.1  dyoung 		MH_ALIGN(m, 2);
    573      1.1  dyoung 		m->m_pkthdr.len = m->m_len = 2;
    574      1.1  dyoung 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    575      1.1  dyoung 		break;
    576      1.1  dyoung 
    577      1.1  dyoung 	default:
    578      1.1  dyoung 		IEEE80211_DPRINTF(("%s: invalid mgmt frame type %u\n",
    579      1.1  dyoung 			__func__, type));
    580  1.1.1.3  dyoung 		senderr(EINVAL, is_tx_unknownmgt);
    581      1.1  dyoung 		/* NOTREACHED */
    582      1.1  dyoung 	}
    583      1.1  dyoung 
    584      1.1  dyoung 	ret = ieee80211_mgmt_output(ifp, ni, m, type);
    585      1.1  dyoung 	if (ret == 0) {
    586      1.1  dyoung 		if (timer)
    587      1.1  dyoung 			ic->ic_mgt_timer = timer;
    588      1.1  dyoung 	} else {
    589      1.1  dyoung bad:
    590      1.1  dyoung 		if (ni != ic->ic_bss)		/* remove ref we added */
    591      1.1  dyoung 			ieee80211_free_node(ic, ni);
    592      1.1  dyoung 	}
    593      1.1  dyoung 	return ret;
    594      1.1  dyoung #undef senderr
    595      1.1  dyoung }
    596