Home | History | Annotate | Line # | Download | only in net80211
ieee80211_output.c revision 1.23
      1  1.23   dyoung /*	$NetBSD: ieee80211_output.c,v 1.23 2004/12/27 09:25:05 dyoung Exp $	*/
      2   1.1   dyoung /*-
      3   1.1   dyoung  * Copyright (c) 2001 Atsushi Onoe
      4   1.1   dyoung  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      5   1.1   dyoung  * All rights reserved.
      6   1.1   dyoung  *
      7   1.1   dyoung  * Redistribution and use in source and binary forms, with or without
      8   1.1   dyoung  * modification, are permitted provided that the following conditions
      9   1.1   dyoung  * are met:
     10   1.1   dyoung  * 1. Redistributions of source code must retain the above copyright
     11   1.1   dyoung  *    notice, this list of conditions and the following disclaimer.
     12   1.1   dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   dyoung  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   dyoung  *    documentation and/or other materials provided with the distribution.
     15   1.1   dyoung  * 3. The name of the author may not be used to endorse or promote products
     16   1.1   dyoung  *    derived from this software without specific prior written permission.
     17   1.1   dyoung  *
     18   1.1   dyoung  * Alternatively, this software may be distributed under the terms of the
     19   1.1   dyoung  * GNU General Public License ("GPL") version 2 as published by the Free
     20   1.1   dyoung  * Software Foundation.
     21   1.1   dyoung  *
     22   1.1   dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1   dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1   dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1   dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1   dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1   dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1   dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1   dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1   dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1   dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1   dyoung  */
     33   1.1   dyoung 
     34   1.1   dyoung #include <sys/cdefs.h>
     35   1.4   dyoung #ifdef __FreeBSD__
     36  1.12   dyoung __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_output.c,v 1.10 2004/04/02 23:25:39 sam Exp $");
     37   1.4   dyoung #else
     38  1.23   dyoung __KERNEL_RCSID(0, "$NetBSD: ieee80211_output.c,v 1.23 2004/12/27 09:25:05 dyoung Exp $");
     39   1.4   dyoung #endif
     40   1.1   dyoung 
     41   1.1   dyoung #include "opt_inet.h"
     42   1.1   dyoung 
     43   1.6   dyoung #ifdef __NetBSD__
     44   1.6   dyoung #include "bpfilter.h"
     45   1.6   dyoung #endif /* __NetBSD__ */
     46   1.6   dyoung 
     47   1.1   dyoung #include <sys/param.h>
     48   1.1   dyoung #include <sys/systm.h>
     49   1.1   dyoung #include <sys/mbuf.h>
     50   1.1   dyoung #include <sys/malloc.h>
     51   1.1   dyoung #include <sys/kernel.h>
     52   1.1   dyoung #include <sys/socket.h>
     53   1.1   dyoung #include <sys/sockio.h>
     54   1.1   dyoung #include <sys/endian.h>
     55   1.1   dyoung #include <sys/errno.h>
     56   1.5   dyoung #ifdef __FreeBSD__
     57   1.1   dyoung #include <sys/bus.h>
     58   1.5   dyoung #endif
     59   1.1   dyoung #include <sys/proc.h>
     60   1.1   dyoung #include <sys/sysctl.h>
     61   1.1   dyoung 
     62   1.2   dyoung #ifdef __FreeBSD__
     63   1.1   dyoung #include <machine/atomic.h>
     64   1.2   dyoung #endif
     65   1.2   dyoung 
     66   1.1   dyoung #include <net/if.h>
     67   1.1   dyoung #include <net/if_dl.h>
     68   1.1   dyoung #include <net/if_media.h>
     69   1.1   dyoung #include <net/if_arp.h>
     70   1.2   dyoung #ifdef __FreeBSD__
     71   1.1   dyoung #include <net/ethernet.h>
     72   1.5   dyoung #else
     73   1.5   dyoung #include <net/if_ether.h>
     74   1.2   dyoung #endif
     75   1.1   dyoung #include <net/if_llc.h>
     76   1.1   dyoung 
     77   1.5   dyoung #include <net80211/ieee80211_var.h>
     78   1.2   dyoung #include <net80211/ieee80211_compat.h>
     79   1.1   dyoung 
     80   1.6   dyoung #if NBPFILTER > 0
     81   1.1   dyoung #include <net/bpf.h>
     82   1.6   dyoung #endif
     83   1.1   dyoung 
     84   1.1   dyoung #ifdef INET
     85   1.1   dyoung #include <netinet/in.h>
     86   1.5   dyoung #ifdef __FreeBSD__
     87   1.1   dyoung #include <netinet/if_ether.h>
     88   1.5   dyoung #else
     89   1.5   dyoung #include <net/if_ether.h>
     90   1.5   dyoung #endif
     91   1.1   dyoung #endif
     92   1.1   dyoung 
     93  1.15  mycroft #ifdef IEEE80211_DEBUG
     94  1.15  mycroft /*
     95  1.15  mycroft  * Decide if an outbound management frame should be
     96  1.15  mycroft  * printed when debugging is enabled.  This filters some
     97  1.15  mycroft  * of the less interesting frames that come frequently
     98  1.15  mycroft  * (e.g. beacons).
     99  1.15  mycroft  */
    100  1.15  mycroft static __inline int
    101  1.15  mycroft doprint(struct ieee80211com *ic, int subtype)
    102  1.15  mycroft {
    103  1.15  mycroft 	switch (subtype) {
    104  1.15  mycroft 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    105  1.15  mycroft 		return (ic->ic_opmode == IEEE80211_M_IBSS);
    106  1.15  mycroft 	}
    107  1.15  mycroft 	return 1;
    108  1.15  mycroft }
    109  1.15  mycroft #endif
    110  1.15  mycroft 
    111   1.1   dyoung /*
    112   1.1   dyoung  * Send a management frame to the specified node.  The node pointer
    113   1.1   dyoung  * must have a reference as the pointer will be passed to the driver
    114   1.1   dyoung  * and potentially held for a long time.  If the frame is successfully
    115   1.1   dyoung  * dispatched to the driver, then it is responsible for freeing the
    116   1.1   dyoung  * reference (and potentially free'ing up any associated storage).
    117   1.1   dyoung  */
    118   1.1   dyoung static int
    119   1.1   dyoung ieee80211_mgmt_output(struct ifnet *ifp, struct ieee80211_node *ni,
    120   1.1   dyoung     struct mbuf *m, int type)
    121   1.1   dyoung {
    122   1.1   dyoung 	struct ieee80211com *ic = (void *)ifp;
    123   1.1   dyoung 	struct ieee80211_frame *wh;
    124   1.1   dyoung 
    125  1.11   dyoung 	IASSERT(ni != NULL, ("null node"));
    126   1.1   dyoung 	ni->ni_inact = 0;
    127   1.1   dyoung 
    128   1.1   dyoung 	/*
    129   1.1   dyoung 	 * Yech, hack alert!  We want to pass the node down to the
    130   1.1   dyoung 	 * driver's start routine.  If we don't do so then the start
    131   1.1   dyoung 	 * routine must immediately look it up again and that can
    132   1.1   dyoung 	 * cause a lock order reversal if, for example, this frame
    133   1.1   dyoung 	 * is being sent because the station is being timedout and
    134   1.1   dyoung 	 * the frame being sent is a DEAUTH message.  We could stick
    135   1.1   dyoung 	 * this in an m_tag and tack that on to the mbuf.  However
    136   1.1   dyoung 	 * that's rather expensive to do for every frame so instead
    137   1.1   dyoung 	 * we stuff it in the rcvif field since outbound frames do
    138   1.1   dyoung 	 * not (presently) use this.
    139   1.1   dyoung 	 */
    140   1.1   dyoung 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
    141   1.1   dyoung 	if (m == NULL)
    142   1.1   dyoung 		return ENOMEM;
    143   1.5   dyoung #ifdef __FreeBSD__
    144   1.1   dyoung 	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
    145   1.5   dyoung #endif
    146   1.1   dyoung 	m->m_pkthdr.rcvif = (void *)ni;
    147   1.1   dyoung 
    148   1.1   dyoung 	wh = mtod(m, struct ieee80211_frame *);
    149   1.1   dyoung 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
    150   1.1   dyoung 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    151   1.2   dyoung 	*(u_int16_t *)&wh->i_dur[0] = 0;
    152   1.2   dyoung 	*(u_int16_t *)&wh->i_seq[0] =
    153   1.1   dyoung 	    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
    154   1.1   dyoung 	ni->ni_txseq++;
    155   1.1   dyoung 	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
    156   1.1   dyoung 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
    157   1.1   dyoung 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
    158   1.1   dyoung 
    159   1.7   dyoung 	if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
    160   1.7   dyoung 		m->m_flags &= ~M_LINK0;
    161  1.14  mycroft 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
    162  1.14  mycroft 			("%s: encrypting frame for %s\n",
    163  1.14  mycroft 			__func__, ether_sprintf(wh->i_addr1)));
    164   1.7   dyoung 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
    165   1.7   dyoung 	}
    166   1.1   dyoung #ifdef IEEE80211_DEBUG
    167  1.15  mycroft 	/* avoid printing too many frames */
    168  1.15  mycroft 	if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
    169  1.15  mycroft 	    ieee80211_msg_dumppkts(ic)) {
    170  1.15  mycroft 		if_printf(ifp, "sending %s to %s on channel %u\n",
    171  1.15  mycroft 		    ieee80211_mgt_subtype_name[
    172  1.15  mycroft 		    (type & IEEE80211_FC0_SUBTYPE_MASK)
    173  1.15  mycroft 		    >> IEEE80211_FC0_SUBTYPE_SHIFT],
    174  1.15  mycroft 		    ether_sprintf(ni->ni_macaddr),
    175  1.15  mycroft 		    ieee80211_chan2ieee(ic, ni->ni_chan));
    176  1.15  mycroft 	}
    177   1.1   dyoung #endif
    178   1.1   dyoung 	IF_ENQUEUE(&ic->ic_mgtq, m);
    179   1.1   dyoung 	ifp->if_timer = 1;
    180   1.1   dyoung 	(*ifp->if_start)(ifp);
    181   1.1   dyoung 	return 0;
    182   1.1   dyoung }
    183   1.1   dyoung 
    184   1.1   dyoung /*
    185   1.1   dyoung  * Encapsulate an outbound data frame.  The mbuf chain is updated and
    186   1.1   dyoung  * a reference to the destination node is returned.  If an error is
    187   1.1   dyoung  * encountered NULL is returned and the node reference will also be NULL.
    188   1.1   dyoung  *
    189   1.1   dyoung  * NB: The caller is responsible for free'ing a returned node reference.
    190   1.1   dyoung  *     The convention is ic_bss is not reference counted; the caller must
    191   1.1   dyoung  *     maintain that.
    192   1.1   dyoung  */
    193   1.1   dyoung struct mbuf *
    194   1.1   dyoung ieee80211_encap(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node **pni)
    195   1.1   dyoung {
    196   1.1   dyoung 	struct ieee80211com *ic = (void *)ifp;
    197   1.1   dyoung 	struct ether_header eh;
    198   1.1   dyoung 	struct ieee80211_frame *wh;
    199   1.1   dyoung 	struct ieee80211_node *ni = NULL;
    200   1.1   dyoung 	struct llc *llc;
    201   1.1   dyoung 
    202   1.1   dyoung 	if (m->m_len < sizeof(struct ether_header)) {
    203   1.1   dyoung 		m = m_pullup(m, sizeof(struct ether_header));
    204   1.1   dyoung 		if (m == NULL) {
    205  1.10   dyoung 			ic->ic_stats.is_tx_nombuf++;
    206   1.1   dyoung 			goto bad;
    207   1.1   dyoung 		}
    208   1.1   dyoung 	}
    209   1.1   dyoung 	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
    210   1.1   dyoung 
    211  1.12   dyoung 	ni = ieee80211_find_txnode(ic, eh.ether_dhost);
    212  1.12   dyoung 	if (ni == NULL) {
    213  1.14  mycroft 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
    214  1.14  mycroft 			("%s: no node for dst %s, discard frame\n",
    215  1.12   dyoung 			__func__, ether_sprintf(eh.ether_dhost)));
    216  1.12   dyoung 		ic->ic_stats.is_tx_nonode++;
    217   1.8   dyoung 		goto bad;
    218  1.12   dyoung 	}
    219   1.1   dyoung 	ni->ni_inact = 0;
    220   1.1   dyoung 
    221   1.1   dyoung 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
    222   1.1   dyoung 	llc = mtod(m, struct llc *);
    223   1.1   dyoung 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
    224   1.1   dyoung 	llc->llc_control = LLC_UI;
    225   1.1   dyoung 	llc->llc_snap.org_code[0] = 0;
    226   1.1   dyoung 	llc->llc_snap.org_code[1] = 0;
    227   1.1   dyoung 	llc->llc_snap.org_code[2] = 0;
    228   1.1   dyoung 	llc->llc_snap.ether_type = eh.ether_type;
    229   1.1   dyoung 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
    230  1.10   dyoung 	if (m == NULL) {
    231  1.10   dyoung 		ic->ic_stats.is_tx_nombuf++;
    232   1.1   dyoung 		goto bad;
    233  1.10   dyoung 	}
    234   1.1   dyoung 	wh = mtod(m, struct ieee80211_frame *);
    235   1.1   dyoung 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
    236   1.2   dyoung 	*(u_int16_t *)&wh->i_dur[0] = 0;
    237   1.2   dyoung 	*(u_int16_t *)&wh->i_seq[0] =
    238   1.1   dyoung 	    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
    239   1.1   dyoung 	ni->ni_txseq++;
    240   1.1   dyoung 	switch (ic->ic_opmode) {
    241   1.1   dyoung 	case IEEE80211_M_STA:
    242   1.1   dyoung 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
    243   1.1   dyoung 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
    244   1.1   dyoung 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
    245   1.1   dyoung 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
    246   1.1   dyoung 		break;
    247   1.1   dyoung 	case IEEE80211_M_IBSS:
    248   1.1   dyoung 	case IEEE80211_M_AHDEMO:
    249   1.1   dyoung 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    250   1.1   dyoung 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
    251   1.1   dyoung 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
    252   1.1   dyoung 		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
    253   1.1   dyoung 		break;
    254   1.1   dyoung 	case IEEE80211_M_HOSTAP:
    255   1.1   dyoung 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
    256   1.1   dyoung 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
    257   1.1   dyoung 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
    258   1.1   dyoung 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
    259   1.1   dyoung 		break;
    260   1.1   dyoung 	case IEEE80211_M_MONITOR:
    261   1.1   dyoung 		goto bad;
    262   1.1   dyoung 	}
    263  1.16  mycroft 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
    264  1.13   dyoung 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
    265   1.1   dyoung 	*pni = ni;
    266   1.1   dyoung 	return m;
    267   1.1   dyoung bad:
    268   1.1   dyoung 	if (m != NULL)
    269   1.1   dyoung 		m_freem(m);
    270  1.17   dyoung 	if (ni != NULL)
    271  1.17   dyoung 		ieee80211_release_node(ic, ni);
    272   1.1   dyoung 	*pni = NULL;
    273   1.1   dyoung 	return NULL;
    274   1.1   dyoung }
    275   1.1   dyoung 
    276   1.1   dyoung /*
    277  1.18   dyoung  * Arguments in:
    278  1.18   dyoung  *
    279  1.18   dyoung  * paylen:  payload length (no FCS, no WEP header)
    280  1.18   dyoung  *
    281  1.18   dyoung  * hdrlen:  header length
    282  1.18   dyoung  *
    283  1.18   dyoung  * rate:    MSDU speed, units 500kb/s
    284  1.18   dyoung  *
    285  1.18   dyoung  * flags:   IEEE80211_F_SHPREAMBLE (use short preamble),
    286  1.18   dyoung  *          IEEE80211_F_SHSLOT (use short slot length)
    287  1.18   dyoung  *
    288  1.18   dyoung  * Arguments out:
    289  1.18   dyoung  *
    290  1.21  mycroft  * d:       802.11 Duration field for RTS,
    291  1.21  mycroft  *          802.11 Duration field for data frame,
    292  1.18   dyoung  *          PLCP Length for data frame,
    293  1.21  mycroft  *          residual octets at end of data slot
    294  1.18   dyoung  */
    295  1.18   dyoung static int
    296  1.23   dyoung ieee80211_compute_duration1(int len, int use_ack, uint32_t flags, int rate,
    297  1.18   dyoung     struct ieee80211_duration *d)
    298  1.18   dyoung {
    299  1.22  mycroft 	int pre, ctsrate;
    300  1.23   dyoung 	int ack, bitlen, data_dur, remainder;
    301  1.18   dyoung 
    302  1.18   dyoung 	/* RTS reserves medium for SIFS | CTS | SIFS | (DATA) | SIFS | ACK
    303  1.18   dyoung 	 * DATA reserves medium for SIFS | ACK
    304  1.22  mycroft 	 *
    305  1.22  mycroft 	 * XXXMYC: no ACK on multicast/broadcast or control packets
    306  1.18   dyoung 	 */
    307  1.18   dyoung 
    308  1.18   dyoung 	bitlen = len * 8;
    309  1.18   dyoung 
    310  1.22  mycroft 	pre = IEEE80211_DUR_DS_SIFS;
    311  1.22  mycroft 	if ((flags & IEEE80211_F_SHPREAMBLE) != 0)
    312  1.22  mycroft 		pre += IEEE80211_DUR_DS_SHORT_PREAMBLE + IEEE80211_DUR_DS_FAST_PLCPHDR;
    313  1.22  mycroft 	else
    314  1.22  mycroft 		pre += IEEE80211_DUR_DS_LONG_PREAMBLE + IEEE80211_DUR_DS_SLOW_PLCPHDR;
    315  1.22  mycroft 
    316  1.18   dyoung #if 0
    317  1.18   dyoung 	/* RTS is always sent at 1 Mb/s.  (XXX Really?) */
    318  1.18   dyoung 	d->d_rts_plcp_len = sizeof(struct ieee80211_frame_rts) * 8;
    319  1.18   dyoung #endif
    320  1.20  mycroft 	d->d_residue = 0;
    321  1.20  mycroft 	data_dur = (bitlen * 2) / rate;
    322  1.22  mycroft 	remainder = (bitlen * 2) % rate;
    323  1.22  mycroft 	if (remainder != 0) {
    324  1.22  mycroft 		d->d_residue = (rate - remainder) / 16;
    325  1.22  mycroft 		data_dur++;
    326  1.22  mycroft 	}
    327  1.18   dyoung 
    328  1.18   dyoung 	switch (rate) {
    329  1.18   dyoung 	case 2:		/* 1 Mb/s */
    330  1.18   dyoung 	case 4:		/* 2 Mb/s */
    331  1.18   dyoung 		/* 1 - 2 Mb/s WLAN: send ACK/CTS at 1 Mb/s */
    332  1.22  mycroft 		ctsrate = 2;
    333  1.18   dyoung 		break;
    334  1.18   dyoung 	case 11:	/* 5.5 Mb/s */
    335  1.18   dyoung 	case 22:	/* 11  Mb/s */
    336  1.20  mycroft 	case 44:	/* 22  Mb/s */
    337  1.18   dyoung 		/* 5.5 - 11 Mb/s WLAN: send ACK/CTS at 2 Mb/s */
    338  1.22  mycroft 		ctsrate = 4;
    339  1.18   dyoung 		break;
    340  1.18   dyoung 	default:
    341  1.18   dyoung 		/* TBD */
    342  1.18   dyoung 		return -1;
    343  1.18   dyoung 	}
    344  1.18   dyoung 
    345  1.19   dyoung 	d->d_plcp_len = data_dur;
    346  1.19   dyoung 
    347  1.23   dyoung 	ack = (use_ack) ? pre + (IEEE80211_DUR_DS_SLOW_ACK * 2) / ctsrate : 0;
    348  1.23   dyoung 
    349  1.22  mycroft 	d->d_rts_dur =
    350  1.22  mycroft 	    pre + (IEEE80211_DUR_DS_SLOW_CTS * 2) / ctsrate +
    351  1.22  mycroft 	    pre + data_dur +
    352  1.23   dyoung 	    ack;
    353  1.18   dyoung 
    354  1.23   dyoung 	d->d_data_dur = ack;
    355  1.18   dyoung 
    356  1.18   dyoung 	return 0;
    357  1.18   dyoung }
    358  1.18   dyoung 
    359  1.18   dyoung /*
    360  1.18   dyoung  * Arguments in:
    361  1.18   dyoung  *
    362  1.18   dyoung  * wh:      802.11 header
    363  1.18   dyoung  *
    364  1.18   dyoung  * paylen:  payload length (no FCS, no WEP header)
    365  1.18   dyoung  *
    366  1.18   dyoung  * rate:    MSDU speed, units 500kb/s
    367  1.18   dyoung  *
    368  1.18   dyoung  * fraglen: fragment length, set to maximum (or higher) for no
    369  1.18   dyoung  *          fragmentation
    370  1.18   dyoung  *
    371  1.18   dyoung  * flags:   IEEE80211_F_PRIVACY (hardware adds WEP),
    372  1.18   dyoung  *          IEEE80211_F_SHPREAMBLE (use short preamble),
    373  1.18   dyoung  *          IEEE80211_F_SHSLOT (use short slot length)
    374  1.18   dyoung  *
    375  1.18   dyoung  * Arguments out:
    376  1.18   dyoung  *
    377  1.18   dyoung  * d0: 802.11 Duration fields (RTS/Data), PLCP Length, Service fields
    378  1.18   dyoung  *     of first/only fragment
    379  1.18   dyoung  *
    380  1.18   dyoung  * dn: 802.11 Duration fields (RTS/Data), PLCP Length, Service fields
    381  1.18   dyoung  *     of first/only fragment
    382  1.18   dyoung  */
    383  1.18   dyoung int
    384  1.19   dyoung ieee80211_compute_duration(struct ieee80211_frame *wh, int len,
    385  1.18   dyoung     uint32_t flags, int fraglen, int rate, struct ieee80211_duration *d0,
    386  1.19   dyoung     struct ieee80211_duration *dn, int *npktp, int debug)
    387  1.18   dyoung {
    388  1.23   dyoung 	int ack, rc;
    389  1.19   dyoung 	int firstlen, hdrlen, lastlen, lastlen0, npkt, overlen, paylen;
    390  1.18   dyoung 
    391  1.18   dyoung 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
    392  1.18   dyoung 		hdrlen = sizeof(struct ieee80211_frame_addr4);
    393  1.18   dyoung 	else
    394  1.18   dyoung 		hdrlen = sizeof(struct ieee80211_frame);
    395  1.18   dyoung 
    396  1.19   dyoung 	paylen = len - hdrlen;
    397  1.19   dyoung 
    398  1.18   dyoung 	if ((flags & IEEE80211_F_PRIVACY) != 0) {
    399  1.18   dyoung 		overlen = IEEE80211_WEP_TOTLEN + IEEE80211_CRC_LEN;
    400  1.18   dyoung #if 0	/* 802.11 lets us extend a fragment's length by the length of
    401  1.18   dyoung 	 * a WEP header, AFTER fragmentation.  Might want to disable
    402  1.18   dyoung 	 * this while fancy link adaptations are running.
    403  1.18   dyoung 	 */
    404  1.18   dyoung 		fraglen -= IEEE80211_WEP_TOTLEN;
    405  1.18   dyoung #endif
    406  1.18   dyoung #if 0	/* Ditto CRC? */
    407  1.18   dyoung 		fraglen -= IEEE80211_CRC_LEN;
    408  1.18   dyoung #endif
    409  1.18   dyoung 	} else
    410  1.18   dyoung 		overlen = IEEE80211_CRC_LEN;
    411  1.18   dyoung 
    412  1.18   dyoung 	npkt = paylen / fraglen;
    413  1.18   dyoung 	lastlen0 = paylen % fraglen;
    414  1.18   dyoung 
    415  1.18   dyoung 	if (npkt == 0) {
    416  1.18   dyoung 		/* no fragments */
    417  1.18   dyoung 		lastlen = paylen + overlen;
    418  1.18   dyoung 	} else if (lastlen0 != 0) {
    419  1.18   dyoung 		/* a short fragment */
    420  1.18   dyoung 		lastlen = lastlen0 + overlen;
    421  1.18   dyoung 		npkt++;
    422  1.18   dyoung 	} else
    423  1.18   dyoung 		lastlen = fraglen + overlen;
    424  1.18   dyoung 
    425  1.18   dyoung 	if (npktp != NULL)
    426  1.18   dyoung 		*npktp = npkt;
    427  1.18   dyoung 
    428  1.18   dyoung 	if (npkt > 1)
    429  1.18   dyoung 		firstlen = fraglen + overlen;
    430  1.18   dyoung 	else
    431  1.18   dyoung 		firstlen = paylen + overlen;
    432  1.18   dyoung 
    433  1.19   dyoung 	if (debug) {
    434  1.19   dyoung 		printf("%s: npkt %d firstlen %d lastlen0 %d lastlen %d "
    435  1.19   dyoung 		    "fraglen %d overlen %d len %d rate %d flags %08x\n",
    436  1.19   dyoung 		    __func__, npkt, firstlen, lastlen0, lastlen, fraglen,
    437  1.19   dyoung 		    overlen, len, rate, flags);
    438  1.19   dyoung 	}
    439  1.23   dyoung 
    440  1.23   dyoung 	ack = !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
    441  1.23   dyoung 	    (wh->i_fc[1] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL;
    442  1.23   dyoung 
    443  1.23   dyoung 	rc = ieee80211_compute_duration1(firstlen + hdrlen,
    444  1.23   dyoung 	    ack, flags, rate, d0);
    445  1.18   dyoung 	if (rc == -1)
    446  1.18   dyoung 		return rc;
    447  1.19   dyoung 
    448  1.19   dyoung 	if (npkt <= 1) {
    449  1.18   dyoung 		*dn = *d0;
    450  1.18   dyoung 		return 0;
    451  1.18   dyoung 	}
    452  1.23   dyoung 	return ieee80211_compute_duration1(lastlen + hdrlen, ack, flags, rate,
    453  1.23   dyoung 	    dn);
    454  1.18   dyoung }
    455  1.18   dyoung 
    456  1.18   dyoung /*
    457   1.1   dyoung  * Add a supported rates element id to a frame.
    458   1.1   dyoung  */
    459   1.1   dyoung u_int8_t *
    460   1.1   dyoung ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    461   1.1   dyoung {
    462   1.1   dyoung 	int nrates;
    463   1.1   dyoung 
    464   1.1   dyoung 	*frm++ = IEEE80211_ELEMID_RATES;
    465   1.1   dyoung 	nrates = rs->rs_nrates;
    466   1.1   dyoung 	if (nrates > IEEE80211_RATE_SIZE)
    467   1.1   dyoung 		nrates = IEEE80211_RATE_SIZE;
    468   1.1   dyoung 	*frm++ = nrates;
    469   1.1   dyoung 	memcpy(frm, rs->rs_rates, nrates);
    470   1.1   dyoung 	return frm + nrates;
    471   1.1   dyoung }
    472   1.1   dyoung 
    473   1.1   dyoung /*
    474   1.1   dyoung  * Add an extended supported rates element id to a frame.
    475   1.1   dyoung  */
    476   1.1   dyoung u_int8_t *
    477   1.1   dyoung ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    478   1.1   dyoung {
    479   1.1   dyoung 	/*
    480   1.1   dyoung 	 * Add an extended supported rates element if operating in 11g mode.
    481   1.1   dyoung 	 */
    482   1.1   dyoung 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
    483   1.1   dyoung 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
    484   1.1   dyoung 		*frm++ = IEEE80211_ELEMID_XRATES;
    485   1.1   dyoung 		*frm++ = nrates;
    486   1.1   dyoung 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
    487   1.1   dyoung 		frm += nrates;
    488   1.1   dyoung 	}
    489   1.1   dyoung 	return frm;
    490   1.1   dyoung }
    491   1.1   dyoung 
    492   1.1   dyoung /*
    493   1.1   dyoung  * Add an ssid elemet to a frame.
    494   1.1   dyoung  */
    495   1.1   dyoung static u_int8_t *
    496   1.1   dyoung ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
    497   1.1   dyoung {
    498   1.1   dyoung 	*frm++ = IEEE80211_ELEMID_SSID;
    499   1.1   dyoung 	*frm++ = len;
    500   1.1   dyoung 	memcpy(frm, ssid, len);
    501   1.1   dyoung 	return frm + len;
    502   1.1   dyoung }
    503   1.1   dyoung 
    504   1.1   dyoung static struct mbuf *
    505   1.1   dyoung ieee80211_getmbuf(int flags, int type, u_int pktlen)
    506   1.1   dyoung {
    507   1.1   dyoung 	struct mbuf *m;
    508   1.1   dyoung 
    509  1.11   dyoung 	IASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
    510   1.2   dyoung #ifdef __FreeBSD__
    511   1.3   dyoung 	if (pktlen <= MHLEN)
    512   1.1   dyoung 		MGETHDR(m, flags, type);
    513   1.1   dyoung 	else
    514   1.1   dyoung 		m = m_getcl(flags, type, M_PKTHDR);
    515   1.2   dyoung #else
    516   1.2   dyoung 	MGETHDR(m, flags, type);
    517   1.2   dyoung 	if (m != NULL && pktlen > MHLEN)
    518   1.2   dyoung 		MCLGET(m, flags);
    519   1.2   dyoung #endif
    520   1.1   dyoung 	return m;
    521   1.1   dyoung }
    522   1.1   dyoung 
    523   1.1   dyoung /*
    524   1.1   dyoung  * Send a management frame.  The node is for the destination (or ic_bss
    525   1.1   dyoung  * when in station mode).  Nodes other than ic_bss have their reference
    526   1.1   dyoung  * count bumped to reflect our use for an indeterminant time.
    527   1.1   dyoung  */
    528   1.1   dyoung int
    529   1.1   dyoung ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
    530   1.1   dyoung 	int type, int arg)
    531   1.1   dyoung {
    532  1.10   dyoung #define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
    533   1.1   dyoung 	struct ifnet *ifp = &ic->ic_if;
    534   1.1   dyoung 	struct mbuf *m;
    535   1.1   dyoung 	u_int8_t *frm;
    536   1.1   dyoung 	enum ieee80211_phymode mode;
    537   1.1   dyoung 	u_int16_t capinfo;
    538   1.7   dyoung 	int has_challenge, is_shared_key, ret, timer;
    539   1.1   dyoung 
    540  1.11   dyoung 	IASSERT(ni != NULL, ("null node"));
    541   1.1   dyoung 
    542   1.1   dyoung 	/*
    543   1.1   dyoung 	 * Hold a reference on the node so it doesn't go away until after
    544   1.1   dyoung 	 * the xmit is complete all the way in the driver.  On error we
    545   1.1   dyoung 	 * will remove our reference.
    546   1.1   dyoung 	 */
    547  1.17   dyoung 	ieee80211_ref_node(ni);
    548   1.1   dyoung 	timer = 0;
    549   1.1   dyoung 	switch (type) {
    550   1.1   dyoung 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
    551   1.1   dyoung 		/*
    552   1.1   dyoung 		 * prreq frame format
    553   1.1   dyoung 		 *	[tlv] ssid
    554   1.1   dyoung 		 *	[tlv] supported rates
    555   1.1   dyoung 		 *	[tlv] extended supported rates
    556   1.1   dyoung 		 */
    557   1.1   dyoung 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    558   1.1   dyoung 			 2 + ic->ic_des_esslen
    559   1.1   dyoung 		       + 2 + IEEE80211_RATE_SIZE
    560   1.1   dyoung 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    561   1.1   dyoung 		if (m == NULL)
    562  1.10   dyoung 			senderr(ENOMEM, is_tx_nombuf);
    563   1.1   dyoung 		m->m_data += sizeof(struct ieee80211_frame);
    564   1.1   dyoung 		frm = mtod(m, u_int8_t *);
    565   1.1   dyoung 		frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
    566   1.1   dyoung 		mode = ieee80211_chan2mode(ic, ni->ni_chan);
    567   1.1   dyoung 		frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
    568   1.1   dyoung 		frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
    569   1.1   dyoung 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    570   1.1   dyoung 
    571   1.1   dyoung 		timer = IEEE80211_TRANS_WAIT;
    572   1.1   dyoung 		break;
    573   1.1   dyoung 
    574   1.1   dyoung 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    575   1.1   dyoung 		/*
    576   1.1   dyoung 		 * probe response frame format
    577   1.1   dyoung 		 *	[8] time stamp
    578   1.1   dyoung 		 *	[2] beacon interval
    579   1.1   dyoung 		 *	[2] cabability information
    580   1.1   dyoung 		 *	[tlv] ssid
    581   1.1   dyoung 		 *	[tlv] supported rates
    582  1.10   dyoung 		 *	[tlv] parameter set (FH/DS)
    583   1.1   dyoung 		 *	[tlv] parameter set (IBSS)
    584   1.1   dyoung 		 *	[tlv] extended supported rates
    585   1.1   dyoung 		 */
    586   1.1   dyoung 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    587   1.1   dyoung 			 8 + 2 + 2 + 2
    588   1.1   dyoung 		       + 2 + ni->ni_esslen
    589   1.1   dyoung 		       + 2 + IEEE80211_RATE_SIZE
    590  1.10   dyoung 		       + (ic->ic_phytype == IEEE80211_T_FH ? 7 : 3)
    591   1.1   dyoung 		       + 6
    592   1.1   dyoung 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    593   1.1   dyoung 		if (m == NULL)
    594  1.10   dyoung 			senderr(ENOMEM, is_tx_nombuf);
    595   1.1   dyoung 		m->m_data += sizeof(struct ieee80211_frame);
    596   1.1   dyoung 		frm = mtod(m, u_int8_t *);
    597   1.1   dyoung 
    598   1.1   dyoung 		memset(frm, 0, 8);	/* timestamp should be filled later */
    599   1.1   dyoung 		frm += 8;
    600   1.1   dyoung 		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
    601   1.1   dyoung 		frm += 2;
    602   1.1   dyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    603   1.1   dyoung 			capinfo = IEEE80211_CAPINFO_IBSS;
    604   1.1   dyoung 		else
    605   1.1   dyoung 			capinfo = IEEE80211_CAPINFO_ESS;
    606  1.16  mycroft 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
    607   1.1   dyoung 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    608  1.10   dyoung 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    609  1.10   dyoung 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    610  1.10   dyoung 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    611   1.1   dyoung 		*(u_int16_t *)frm = htole16(capinfo);
    612   1.1   dyoung 		frm += 2;
    613   1.1   dyoung 
    614   1.1   dyoung 		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
    615   1.1   dyoung 				ic->ic_bss->ni_esslen);
    616   1.1   dyoung 		frm = ieee80211_add_rates(frm, &ic->ic_bss->ni_rates);
    617   1.1   dyoung 
    618  1.10   dyoung 		if (ic->ic_phytype == IEEE80211_T_FH) {
    619   1.6   dyoung                         *frm++ = IEEE80211_ELEMID_FHPARMS;
    620   1.6   dyoung                         *frm++ = 5;
    621   1.6   dyoung                         *frm++ = ni->ni_fhdwell & 0x00ff;
    622   1.6   dyoung                         *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
    623   1.6   dyoung                         *frm++ = IEEE80211_FH_CHANSET(
    624   1.6   dyoung 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    625   1.6   dyoung                         *frm++ = IEEE80211_FH_CHANPAT(
    626   1.6   dyoung 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    627   1.6   dyoung                         *frm++ = ni->ni_fhindex;
    628  1.10   dyoung 		} else {
    629  1.10   dyoung 			*frm++ = IEEE80211_ELEMID_DSPARMS;
    630  1.10   dyoung 			*frm++ = 1;
    631  1.10   dyoung 			*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
    632  1.10   dyoung 		}
    633   1.6   dyoung 
    634   1.1   dyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
    635   1.1   dyoung 			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
    636   1.1   dyoung 			*frm++ = 2;
    637   1.1   dyoung 			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
    638   1.1   dyoung 		} else {	/* IEEE80211_M_HOSTAP */
    639   1.1   dyoung 			/* TODO: TIM */
    640   1.1   dyoung 			*frm++ = IEEE80211_ELEMID_TIM;
    641   1.1   dyoung 			*frm++ = 4;	/* length */
    642   1.1   dyoung 			*frm++ = 0;	/* DTIM count */
    643   1.1   dyoung 			*frm++ = 1;	/* DTIM period */
    644   1.1   dyoung 			*frm++ = 0;	/* bitmap control */
    645   1.1   dyoung 			*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
    646   1.1   dyoung 		}
    647   1.1   dyoung 		frm = ieee80211_add_xrates(frm, &ic->ic_bss->ni_rates);
    648   1.1   dyoung 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    649   1.1   dyoung 		break;
    650   1.1   dyoung 
    651   1.1   dyoung 	case IEEE80211_FC0_SUBTYPE_AUTH:
    652   1.1   dyoung 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    653   1.1   dyoung 		if (m == NULL)
    654  1.10   dyoung 			senderr(ENOMEM, is_tx_nombuf);
    655   1.7   dyoung 
    656   1.7   dyoung 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
    657   1.7   dyoung 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
    658   1.7   dyoung 		    ni->ni_challenge != NULL);
    659   1.7   dyoung 
    660   1.7   dyoung 		is_shared_key = has_challenge || (ni->ni_challenge != NULL &&
    661   1.7   dyoung 		    arg == IEEE80211_AUTH_SHARED_PASS);
    662   1.7   dyoung 
    663   1.7   dyoung 		if (has_challenge) {
    664   1.7   dyoung 			MH_ALIGN(m, 2 * 3 + 2 + IEEE80211_CHALLENGE_LEN);
    665   1.7   dyoung 			m->m_pkthdr.len = m->m_len =
    666   1.7   dyoung 			    2 * 3 + 2 + IEEE80211_CHALLENGE_LEN;
    667   1.7   dyoung 		} else {
    668   1.7   dyoung 			MH_ALIGN(m, 2 * 3);
    669   1.7   dyoung 			m->m_pkthdr.len = m->m_len = 2 * 3;
    670   1.7   dyoung 		}
    671   1.1   dyoung 		frm = mtod(m, u_int8_t *);
    672   1.7   dyoung 		((u_int16_t *)frm)[0] =
    673   1.7   dyoung 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
    674   1.7   dyoung 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
    675   1.1   dyoung 		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
    676   1.1   dyoung 		((u_int16_t *)frm)[2] = 0;		/* status */
    677   1.7   dyoung 
    678   1.7   dyoung 		if (has_challenge) {
    679   1.7   dyoung 			((u_int16_t *)frm)[3] =
    680   1.7   dyoung 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
    681   1.7   dyoung 			    IEEE80211_ELEMID_CHALLENGE);
    682   1.7   dyoung 			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
    683   1.7   dyoung 			    IEEE80211_CHALLENGE_LEN);
    684   1.7   dyoung 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
    685  1.14  mycroft 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
    686  1.14  mycroft 				    ("%s: request encrypt frame\n", __func__));
    687   1.7   dyoung 				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
    688   1.7   dyoung 			}
    689   1.7   dyoung 		}
    690   1.1   dyoung 		if (ic->ic_opmode == IEEE80211_M_STA)
    691   1.1   dyoung 			timer = IEEE80211_TRANS_WAIT;
    692   1.1   dyoung 		break;
    693   1.1   dyoung 
    694   1.1   dyoung 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
    695  1.15  mycroft 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
    696  1.15  mycroft 			("send station %s deauthenticate (reason %d)\n",
    697  1.15  mycroft 			ether_sprintf(ni->ni_macaddr), arg));
    698   1.1   dyoung 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    699   1.1   dyoung 		if (m == NULL)
    700  1.10   dyoung 			senderr(ENOMEM, is_tx_nombuf);
    701   1.1   dyoung 		MH_ALIGN(m, 2);
    702   1.1   dyoung 		m->m_pkthdr.len = m->m_len = 2;
    703   1.1   dyoung 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    704   1.1   dyoung 		break;
    705   1.1   dyoung 
    706   1.1   dyoung 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
    707   1.1   dyoung 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
    708   1.1   dyoung 		/*
    709   1.1   dyoung 		 * asreq frame format
    710   1.1   dyoung 		 *	[2] capability information
    711   1.1   dyoung 		 *	[2] listen interval
    712   1.1   dyoung 		 *	[6*] current AP address (reassoc only)
    713   1.1   dyoung 		 *	[tlv] ssid
    714   1.1   dyoung 		 *	[tlv] supported rates
    715   1.1   dyoung 		 *	[tlv] extended supported rates
    716   1.1   dyoung 		 */
    717   1.1   dyoung 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    718   1.1   dyoung 			 sizeof(capinfo)
    719   1.1   dyoung 		       + sizeof(u_int16_t)
    720   1.1   dyoung 		       + IEEE80211_ADDR_LEN
    721   1.1   dyoung 		       + 2 + ni->ni_esslen
    722   1.1   dyoung 		       + 2 + IEEE80211_RATE_SIZE
    723   1.1   dyoung 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    724   1.1   dyoung 		if (m == NULL)
    725  1.10   dyoung 			senderr(ENOMEM, is_tx_nombuf);
    726   1.1   dyoung 		m->m_data += sizeof(struct ieee80211_frame);
    727   1.1   dyoung 		frm = mtod(m, u_int8_t *);
    728   1.1   dyoung 
    729   1.1   dyoung 		capinfo = 0;
    730   1.1   dyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    731   1.1   dyoung 			capinfo |= IEEE80211_CAPINFO_IBSS;
    732   1.1   dyoung 		else		/* IEEE80211_M_STA */
    733   1.1   dyoung 			capinfo |= IEEE80211_CAPINFO_ESS;
    734  1.16  mycroft 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
    735   1.1   dyoung 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    736  1.10   dyoung 		/*
    737  1.10   dyoung 		 * NB: Some 11a AP's reject the request when
    738  1.10   dyoung 		 *     short premable is set.
    739  1.10   dyoung 		 */
    740  1.10   dyoung 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    741  1.10   dyoung 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    742   1.1   dyoung 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    743   1.1   dyoung 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
    744   1.1   dyoung 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
    745   1.1   dyoung 		*(u_int16_t *)frm = htole16(capinfo);
    746   1.1   dyoung 		frm += 2;
    747   1.1   dyoung 
    748   1.1   dyoung 		*(u_int16_t *)frm = htole16(ic->ic_lintval);
    749   1.1   dyoung 		frm += 2;
    750   1.1   dyoung 
    751   1.1   dyoung 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
    752   1.1   dyoung 			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
    753   1.1   dyoung 			frm += IEEE80211_ADDR_LEN;
    754   1.1   dyoung 		}
    755   1.1   dyoung 
    756   1.1   dyoung 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
    757   1.1   dyoung 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    758   1.1   dyoung 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    759   1.1   dyoung 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    760   1.1   dyoung 
    761   1.1   dyoung 		timer = IEEE80211_TRANS_WAIT;
    762   1.1   dyoung 		break;
    763   1.1   dyoung 
    764   1.1   dyoung 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
    765   1.1   dyoung 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
    766   1.1   dyoung 		/*
    767   1.1   dyoung 		 * asreq frame format
    768   1.1   dyoung 		 *	[2] capability information
    769   1.1   dyoung 		 *	[2] status
    770   1.1   dyoung 		 *	[2] association ID
    771   1.1   dyoung 		 *	[tlv] supported rates
    772   1.1   dyoung 		 *	[tlv] extended supported rates
    773   1.1   dyoung 		 */
    774   1.1   dyoung 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    775   1.1   dyoung 			 sizeof(capinfo)
    776   1.1   dyoung 		       + sizeof(u_int16_t)
    777   1.1   dyoung 		       + sizeof(u_int16_t)
    778   1.1   dyoung 		       + 2 + IEEE80211_RATE_SIZE
    779   1.1   dyoung 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    780   1.1   dyoung 		if (m == NULL)
    781  1.10   dyoung 			senderr(ENOMEM, is_tx_nombuf);
    782   1.1   dyoung 		m->m_data += sizeof(struct ieee80211_frame);
    783   1.1   dyoung 		frm = mtod(m, u_int8_t *);
    784   1.1   dyoung 
    785   1.1   dyoung 		capinfo = IEEE80211_CAPINFO_ESS;
    786  1.16  mycroft 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
    787   1.1   dyoung 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    788  1.10   dyoung 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    789  1.10   dyoung 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    790  1.10   dyoung 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    791   1.1   dyoung 		*(u_int16_t *)frm = htole16(capinfo);
    792   1.1   dyoung 		frm += 2;
    793   1.1   dyoung 
    794   1.1   dyoung 		*(u_int16_t *)frm = htole16(arg);	/* status */
    795   1.1   dyoung 		frm += 2;
    796   1.1   dyoung 
    797   1.1   dyoung 		if (arg == IEEE80211_STATUS_SUCCESS)
    798   1.1   dyoung 			*(u_int16_t *)frm = htole16(ni->ni_associd);
    799   1.1   dyoung 		frm += 2;
    800   1.1   dyoung 
    801   1.1   dyoung 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    802   1.1   dyoung 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    803   1.1   dyoung 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    804   1.1   dyoung 		break;
    805   1.1   dyoung 
    806   1.1   dyoung 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
    807  1.15  mycroft 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
    808  1.15  mycroft 			("send station %s disassociate (reason %d)\n",
    809  1.15  mycroft 			ether_sprintf(ni->ni_macaddr), arg));
    810   1.1   dyoung 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    811   1.1   dyoung 		if (m == NULL)
    812  1.10   dyoung 			senderr(ENOMEM, is_tx_nombuf);
    813   1.1   dyoung 		MH_ALIGN(m, 2);
    814   1.1   dyoung 		m->m_pkthdr.len = m->m_len = 2;
    815   1.1   dyoung 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    816   1.1   dyoung 		break;
    817   1.1   dyoung 
    818   1.1   dyoung 	default:
    819  1.14  mycroft 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    820  1.14  mycroft 			("%s: invalid mgmt frame type %u\n", __func__, type));
    821  1.10   dyoung 		senderr(EINVAL, is_tx_unknownmgt);
    822   1.1   dyoung 		/* NOTREACHED */
    823   1.1   dyoung 	}
    824   1.1   dyoung 
    825   1.1   dyoung 	ret = ieee80211_mgmt_output(ifp, ni, m, type);
    826   1.1   dyoung 	if (ret == 0) {
    827   1.1   dyoung 		if (timer)
    828   1.1   dyoung 			ic->ic_mgt_timer = timer;
    829   1.1   dyoung 	} else {
    830   1.1   dyoung bad:
    831  1.17   dyoung 		ieee80211_release_node(ic, ni);
    832   1.1   dyoung 	}
    833   1.1   dyoung 	return ret;
    834   1.1   dyoung #undef senderr
    835   1.1   dyoung }
    836   1.6   dyoung 
    837   1.6   dyoung void
    838   1.6   dyoung ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
    839   1.6   dyoung 		  struct mbuf *m)
    840   1.6   dyoung {
    841   1.6   dyoung 	/* Store the new packet on our queue, changing the TIM if necessary */
    842   1.6   dyoung 
    843   1.6   dyoung 	if (IF_IS_EMPTY(&ni->ni_savedq)) {
    844   1.6   dyoung 		ic->ic_set_tim(ic, ni->ni_associd, 1);
    845   1.6   dyoung 	}
    846   1.6   dyoung 	if (ni->ni_savedq.ifq_len >= IEEE80211_PS_MAX_QUEUE) {
    847   1.6   dyoung 		IF_DROP(&ni->ni_savedq);
    848   1.6   dyoung 		m_freem(m);
    849   1.6   dyoung 		if (ic->ic_if.if_flags & IFF_DEBUG)
    850   1.6   dyoung 			printf("%s: station %s power save queue overflow"
    851   1.6   dyoung 			       " of size %d drops %d\n",
    852   1.6   dyoung 			       ic->ic_if.if_xname,
    853   1.6   dyoung 			       ether_sprintf(ni->ni_macaddr),
    854   1.6   dyoung 			       IEEE80211_PS_MAX_QUEUE,
    855   1.6   dyoung 			       ni->ni_savedq.ifq_drops);
    856   1.6   dyoung 	} else {
    857   1.9   dyoung 		/* Similar to ieee80211_mgmt_output, store the node in
    858   1.9   dyoung 		 * the rcvif field.
    859   1.9   dyoung 		 */
    860   1.6   dyoung 		IF_ENQUEUE(&ni->ni_savedq, m);
    861   1.9   dyoung 		m->m_pkthdr.rcvif = (void *)ni;
    862   1.6   dyoung 	}
    863   1.6   dyoung }
    864   1.6   dyoung 
    865