ieee80211_output.c revision 162375
1198892Srdivacky/*- 2198892Srdivacky * Copyright (c) 2001 Atsushi Onoe 3198892Srdivacky * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 4198892Srdivacky * All rights reserved. 5198892Srdivacky * 6198892Srdivacky * Redistribution and use in source and binary forms, with or without 7198892Srdivacky * modification, are permitted provided that the following conditions 8198892Srdivacky * are met: 9198892Srdivacky * 1. Redistributions of source code must retain the above copyright 10198892Srdivacky * notice, this list of conditions and the following disclaimer. 11198892Srdivacky * 2. Redistributions in binary form must reproduce the above copyright 12198892Srdivacky * notice, this list of conditions and the following disclaimer in the 13198892Srdivacky * documentation and/or other materials provided with the distribution. 14198892Srdivacky * 3. The name of the author may not be used to endorse or promote products 15198892Srdivacky * derived from this software without specific prior written permission. 16198892Srdivacky * 17198892Srdivacky * Alternatively, this software may be distributed under the terms of the 18198892Srdivacky * GNU General Public License ("GPL") version 2 as published by the Free 19198892Srdivacky * Software Foundation. 20198892Srdivacky * 21198892Srdivacky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22198892Srdivacky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23198892Srdivacky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24198892Srdivacky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25198892Srdivacky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26198892Srdivacky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27198892Srdivacky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28198892Srdivacky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29198892Srdivacky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30198892Srdivacky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31198892Srdivacky */ 32198892Srdivacky 33198892Srdivacky#include <sys/cdefs.h> 34198892Srdivacky__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 162375 2006-09-17 13:33:30Z andre $"); 35198892Srdivacky 36198892Srdivacky#include "opt_inet.h" 37198892Srdivacky 38198892Srdivacky#include <sys/param.h> 39198892Srdivacky#include <sys/systm.h> 40198892Srdivacky#include <sys/mbuf.h> 41198892Srdivacky#include <sys/kernel.h> 42198892Srdivacky#include <sys/endian.h> 43198892Srdivacky 44198892Srdivacky#include <sys/socket.h> 45198892Srdivacky 46198892Srdivacky#include <net/bpf.h> 47198892Srdivacky#include <net/ethernet.h> 48198892Srdivacky#include <net/if.h> 49198892Srdivacky#include <net/if_llc.h> 50198892Srdivacky#include <net/if_media.h> 51198892Srdivacky#include <net/if_vlan_var.h> 52198892Srdivacky 53198892Srdivacky#include <net80211/ieee80211_var.h> 54198892Srdivacky 55198892Srdivacky#ifdef INET 56198892Srdivacky#include <netinet/in.h> 57198892Srdivacky#include <netinet/if_ether.h> 58198892Srdivacky#include <netinet/in_systm.h> 59198892Srdivacky#include <netinet/ip.h> 60198892Srdivacky#endif 61198892Srdivacky 62198892Srdivacky#ifdef IEEE80211_DEBUG 63198892Srdivacky/* 64198892Srdivacky * Decide if an outbound management frame should be 65198892Srdivacky * printed when debugging is enabled. This filters some 66202375Srdivacky * of the less interesting frames that come frequently 67198892Srdivacky * (e.g. beacons). 68198892Srdivacky */ 69198892Srdivackystatic __inline int 70198892Srdivackydoprint(struct ieee80211com *ic, int subtype) 71198892Srdivacky{ 72198892Srdivacky switch (subtype) { 73198892Srdivacky case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 74198892Srdivacky return (ic->ic_opmode == IEEE80211_M_IBSS); 75198892Srdivacky } 76198892Srdivacky return 1; 77198892Srdivacky} 78198892Srdivacky#endif 79198892Srdivacky 80198892Srdivacky/* 81198892Srdivacky * Set the direction field and address fields of an outgoing 82198892Srdivacky * non-QoS frame. Note this should be called early on in 83198892Srdivacky * constructing a frame as it sets i_fc[1]; other bits can 84198892Srdivacky * then be or'd in. 85198892Srdivacky */ 86198892Srdivackystatic void 87198892Srdivackyieee80211_send_setup(struct ieee80211com *ic, 88198892Srdivacky struct ieee80211_node *ni, 89198892Srdivacky struct ieee80211_frame *wh, 90198892Srdivacky int type, 91198892Srdivacky const u_int8_t sa[IEEE80211_ADDR_LEN], 92198892Srdivacky const u_int8_t da[IEEE80211_ADDR_LEN], 93198892Srdivacky const u_int8_t bssid[IEEE80211_ADDR_LEN]) 94198892Srdivacky{ 95198892Srdivacky#define WH4(wh) ((struct ieee80211_frame_addr4 *)wh) 96198892Srdivacky 97198892Srdivacky wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type; 98198892Srdivacky if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) { 99198892Srdivacky switch (ic->ic_opmode) { 100198892Srdivacky case IEEE80211_M_STA: 101198892Srdivacky wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 102198892Srdivacky IEEE80211_ADDR_COPY(wh->i_addr1, bssid); 103198892Srdivacky IEEE80211_ADDR_COPY(wh->i_addr2, sa); 104198892Srdivacky IEEE80211_ADDR_COPY(wh->i_addr3, da); 105198892Srdivacky break; 106198892Srdivacky case IEEE80211_M_IBSS: 107198892Srdivacky case IEEE80211_M_AHDEMO: 108198892Srdivacky wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 109198892Srdivacky IEEE80211_ADDR_COPY(wh->i_addr1, da); 110198892Srdivacky IEEE80211_ADDR_COPY(wh->i_addr2, sa); 111199481Srdivacky IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 112199481Srdivacky break; 113202375Srdivacky case IEEE80211_M_HOSTAP: 114199481Srdivacky wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 115199481Srdivacky IEEE80211_ADDR_COPY(wh->i_addr1, da); 116199481Srdivacky IEEE80211_ADDR_COPY(wh->i_addr2, bssid); 117199481Srdivacky IEEE80211_ADDR_COPY(wh->i_addr3, sa); 118199481Srdivacky break; 119202375Srdivacky case IEEE80211_M_MONITOR: /* NB: to quiet compiler */ 120199481Srdivacky break; 121199481Srdivacky } 122199481Srdivacky } else { 123198892Srdivacky wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 124198892Srdivacky IEEE80211_ADDR_COPY(wh->i_addr1, da); 125198892Srdivacky IEEE80211_ADDR_COPY(wh->i_addr2, sa); 126198892Srdivacky IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 127198892Srdivacky } 128202375Srdivacky *(u_int16_t *)&wh->i_dur[0] = 0; 129198892Srdivacky /* NB: use non-QoS tid */ 130198892Srdivacky *(u_int16_t *)&wh->i_seq[0] = 131198892Srdivacky htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT); 132198892Srdivacky ni->ni_txseqs[0]++; 133198892Srdivacky#undef WH4 134198892Srdivacky} 135198892Srdivacky 136198892Srdivacky/* 137198892Srdivacky * Send a management frame to the specified node. The node pointer 138198892Srdivacky * must have a reference as the pointer will be passed to the driver 139198892Srdivacky * and potentially held for a long time. If the frame is successfully 140198892Srdivacky * dispatched to the driver, then it is responsible for freeing the 141202375Srdivacky * reference (and potentially free'ing up any associated storage). 142198892Srdivacky */ 143202375Srdivackystatic int 144198892Srdivackyieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni, 145198892Srdivacky struct mbuf *m, int type, int timer) 146198892Srdivacky{ 147198892Srdivacky struct ifnet *ifp = ic->ic_ifp; 148198892Srdivacky struct ieee80211_frame *wh; 149198892Srdivacky 150198892Srdivacky KASSERT(ni != NULL, ("null node")); 151198892Srdivacky 152198892Srdivacky /* 153198892Srdivacky * Yech, hack alert! We want to pass the node down to the 154198892Srdivacky * driver's start routine. If we don't do so then the start 155198892Srdivacky * routine must immediately look it up again and that can 156198892Srdivacky * cause a lock order reversal if, for example, this frame 157198892Srdivacky * is being sent because the station is being timedout and 158198892Srdivacky * the frame being sent is a DEAUTH message. We could stick 159198892Srdivacky * this in an m_tag and tack that on to the mbuf. However 160198892Srdivacky * that's rather expensive to do for every frame so instead 161198892Srdivacky * we stuff it in the rcvif field since outbound frames do 162198892Srdivacky * not (presently) use this. 163198892Srdivacky */ 164198892Srdivacky M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 165198892Srdivacky if (m == NULL) 166198892Srdivacky return ENOMEM; 167198892Srdivacky KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 168198892Srdivacky m->m_pkthdr.rcvif = (void *)ni; 169202375Srdivacky 170198892Srdivacky wh = mtod(m, struct ieee80211_frame *); 171198892Srdivacky ieee80211_send_setup(ic, ni, wh, 172202375Srdivacky IEEE80211_FC0_TYPE_MGT | type, 173198892Srdivacky ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid); 174198892Srdivacky if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) { 175202375Srdivacky m->m_flags &= ~M_LINK0; 176198892Srdivacky IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 177202375Srdivacky "[%s] encrypting frame (%s)\n", 178198892Srdivacky ether_sprintf(wh->i_addr1), __func__); 179202375Srdivacky wh->i_fc[1] |= IEEE80211_FC1_WEP; 180198892Srdivacky } 181198892Srdivacky#ifdef IEEE80211_DEBUG 182198892Srdivacky /* avoid printing too many frames */ 183198892Srdivacky if ((ieee80211_msg_debug(ic) && doprint(ic, type)) || 184198892Srdivacky ieee80211_msg_dumppkts(ic)) { 185198892Srdivacky printf("[%s] send %s on channel %u\n", 186198892Srdivacky ether_sprintf(wh->i_addr1), 187198892Srdivacky ieee80211_mgt_subtype_name[ 188198892Srdivacky (type & IEEE80211_FC0_SUBTYPE_MASK) >> 189198892Srdivacky IEEE80211_FC0_SUBTYPE_SHIFT], 190198892Srdivacky ieee80211_chan2ieee(ic, ic->ic_curchan)); 191198892Srdivacky } 192198892Srdivacky#endif 193198892Srdivacky IEEE80211_NODE_STAT(ni, tx_mgmt); 194198892Srdivacky IF_ENQUEUE(&ic->ic_mgtq, m); 195198892Srdivacky if (timer) { 196198892Srdivacky /* 197201360Srdivacky * Set the mgt frame timeout. 198198892Srdivacky */ 199198892Srdivacky ic->ic_mgt_timer = timer; 200198892Srdivacky ifp->if_timer = 1; 201198892Srdivacky } 202198892Srdivacky if_start(ifp); 203198892Srdivacky return 0; 204198892Srdivacky} 205198892Srdivacky 206198892Srdivacky/* 207198892Srdivacky * Raw packet transmit stub for legacy drivers. 208198892Srdivacky * Send the packet through the mgt q so we bypass 209198892Srdivacky * the normal encapsulation work. 210198892Srdivacky */ 211198892Srdivackyint 212198892Srdivackyieee80211_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 213198892Srdivacky const struct ieee80211_bpf_params *params) 214198892Srdivacky{ 215198892Srdivacky struct ieee80211com *ic = ni->ni_ic; 216198892Srdivacky struct ifnet *ifp = ic->ic_ifp; 217198892Srdivacky 218198892Srdivacky m->m_pkthdr.rcvif = (void *) ni; 219198892Srdivacky IF_ENQUEUE(&ic->ic_mgtq, m); 220198892Srdivacky if_start(ifp); 221198892Srdivacky ifp->if_opackets++; 222198892Srdivacky 223198892Srdivacky return 0; 224198892Srdivacky} 225201360Srdivacky 226198892Srdivacky/* 227198892Srdivacky * 802.11 output routine. This is (currently) used only to 228198892Srdivacky * connect bpf write calls to the 802.11 layer for injecting 229198892Srdivacky * raw 802.11 frames. Note we locate the ieee80211com from 230198892Srdivacky * the ifnet using a spare field setup at attach time. This 231198892Srdivacky * will go away when the virtual ap support comes in. 232198892Srdivacky */ 233198892Srdivackyint 234198892Srdivackyieee80211_output(struct ifnet *ifp, struct mbuf *m, 235198892Srdivacky struct sockaddr *dst, struct rtentry *rt0) 236198892Srdivacky{ 237198892Srdivacky#define senderr(e) do { error = (e); goto bad;} while (0) 238198892Srdivacky struct ieee80211com *ic = ifp->if_spare2; /* XXX */ 239198892Srdivacky struct ieee80211_node *ni = NULL; 240198892Srdivacky struct ieee80211_frame *wh; 241198892Srdivacky int error; 242198892Srdivacky 243198892Srdivacky /* 244198892Srdivacky * Hand to the 802.3 code if not tagged as 245198892Srdivacky * a raw 802.11 frame. 246198892Srdivacky */ 247201360Srdivacky if (dst->sa_family != AF_IEEE80211) 248198892Srdivacky return ether_output(ifp, m, dst, rt0); 249198892Srdivacky#ifdef MAC 250198892Srdivacky error = mac_check_ifnet_transmit(ifp, m); 251198892Srdivacky if (error) 252198892Srdivacky senderr(error); 253198892Srdivacky#endif 254198892Srdivacky if (ifp->if_flags & IFF_MONITOR) 255198892Srdivacky senderr(ENETDOWN); 256198892Srdivacky if ((ifp->if_flags & IFF_UP) == 0) 257198892Srdivacky senderr(ENETDOWN); 258198892Srdivacky 259198892Srdivacky /* XXX bypass bridge, pfil, carp, etc. */ 260198892Srdivacky 261198892Srdivacky if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack)) 262198892Srdivacky senderr(EIO); /* XXX */ 263198892Srdivacky wh = mtod(m, struct ieee80211_frame *); 264198892Srdivacky if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 265198892Srdivacky IEEE80211_FC0_VERSION_0) 266198892Srdivacky senderr(EIO); /* XXX */ 267198892Srdivacky 268198892Srdivacky /* locate destination node */ 269198892Srdivacky switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 270198892Srdivacky case IEEE80211_FC1_DIR_NODS: 271198892Srdivacky case IEEE80211_FC1_DIR_FROMDS: 272198892Srdivacky ni = ieee80211_find_txnode(ic, wh->i_addr1); 273198892Srdivacky break; 274198892Srdivacky case IEEE80211_FC1_DIR_TODS: 275198892Srdivacky case IEEE80211_FC1_DIR_DSTODS: 276198892Srdivacky if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) 277198892Srdivacky senderr(EIO); /* XXX */ 278198892Srdivacky ni = ieee80211_find_txnode(ic, wh->i_addr3); 279198892Srdivacky break; 280198892Srdivacky default: 281198892Srdivacky senderr(EIO); /* XXX */ 282198892Srdivacky } 283198892Srdivacky if (ni == NULL) { 284198892Srdivacky /* 285198892Srdivacky * Permit packets w/ bpf params through regardless 286198892Srdivacky * (see below about sa_len). 287198892Srdivacky */ 288198892Srdivacky if (dst->sa_len == 0) 289198892Srdivacky senderr(EHOSTUNREACH); 290198892Srdivacky ni = ieee80211_ref_node(ic->ic_bss); 291198892Srdivacky } 292198892Srdivacky 293198892Srdivacky /* XXX ctrl frames should go through */ 294198892Srdivacky if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 295198892Srdivacky (m->m_flags & M_PWR_SAV) == 0) { 296198892Srdivacky /* 297198892Srdivacky * Station in power save mode; pass the frame 298201360Srdivacky * to the 802.11 layer and continue. We'll get 299198892Srdivacky * the frame back when the time is right. 300198892Srdivacky */ 301198892Srdivacky ieee80211_pwrsave(ic, ni, m); 302198892Srdivacky error = 0; 303198892Srdivacky goto reclaim; 304198892Srdivacky } 305198892Srdivacky 306198892Srdivacky /* calculate priority so drivers can find the tx queue */ 307198892Srdivacky /* XXX assumes an 802.3 frame */ 308198892Srdivacky if (ieee80211_classify(ic, m, ni)) 309198892Srdivacky senderr(EIO); /* XXX */ 310198892Srdivacky 311198892Srdivacky BPF_MTAP(ifp, m); 312198892Srdivacky /* 313198892Srdivacky * NB: DLT_IEEE802_11_RADIO identifies the parameters are 314198892Srdivacky * present by setting the sa_len field of the sockaddr (yes, 315198892Srdivacky * this is a hack). 316198892Srdivacky * NB: we assume sa_data is suitably aligned to cast. 317198892Srdivacky */ 318198892Srdivacky return ic->ic_raw_xmit(ni, m, (const struct ieee80211_bpf_params *) 319198892Srdivacky (dst->sa_len ? dst->sa_data : NULL)); 320198892Srdivackybad: 321198892Srdivacky if (m != NULL) 322198892Srdivacky m_freem(m); 323198892Srdivackyreclaim: 324198892Srdivacky if (ni != NULL) 325198892Srdivacky ieee80211_free_node(ni); 326198892Srdivacky return error; 327198892Srdivacky#undef senderr 328198892Srdivacky} 329198892Srdivacky 330198892Srdivacky/* 331198892Srdivacky * Send a null data frame to the specified node. 332198892Srdivacky * 333198892Srdivacky * NB: the caller is assumed to have setup a node reference 334198892Srdivacky * for use; this is necessary to deal with a race condition 335198892Srdivacky * when probing for inactive stations. 336198892Srdivacky */ 337198892Srdivackyint 338198892Srdivackyieee80211_send_nulldata(struct ieee80211_node *ni) 339198892Srdivacky{ 340198892Srdivacky struct ieee80211com *ic = ni->ni_ic; 341198892Srdivacky struct ifnet *ifp = ic->ic_ifp; 342198892Srdivacky struct mbuf *m; 343198892Srdivacky struct ieee80211_frame *wh; 344198892Srdivacky 345198892Srdivacky MGETHDR(m, M_NOWAIT, MT_DATA); 346198892Srdivacky if (m == NULL) { 347198892Srdivacky /* XXX debug msg */ 348198892Srdivacky ic->ic_stats.is_tx_nobuf++; 349198892Srdivacky ieee80211_unref_node(&ni); 350198892Srdivacky return ENOMEM; 351198892Srdivacky } 352198892Srdivacky m->m_pkthdr.rcvif = (void *) ni; 353198892Srdivacky 354198892Srdivacky wh = mtod(m, struct ieee80211_frame *); 355198892Srdivacky ieee80211_send_setup(ic, ni, wh, 356198892Srdivacky IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA, 357198892Srdivacky ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid); 358198892Srdivacky /* NB: power management bit is never sent by an AP */ 359198892Srdivacky if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 360198892Srdivacky ic->ic_opmode != IEEE80211_M_HOSTAP) 361198892Srdivacky wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT; 362198892Srdivacky m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame); 363198892Srdivacky 364198892Srdivacky IEEE80211_NODE_STAT(ni, tx_data); 365199481Srdivacky 366198892Srdivacky IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 367198892Srdivacky "[%s] send null data frame on channel %u, pwr mgt %s\n", 368198892Srdivacky ether_sprintf(ni->ni_macaddr), 369198892Srdivacky ieee80211_chan2ieee(ic, ic->ic_curchan), 370198892Srdivacky wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis"); 371198892Srdivacky 372198892Srdivacky IF_ENQUEUE(&ic->ic_mgtq, m); /* cheat */ 373198892Srdivacky if_start(ifp); 374198892Srdivacky 375198892Srdivacky return 0; 376198892Srdivacky} 377198892Srdivacky 378198892Srdivacky/* 379198892Srdivacky * Assign priority to a frame based on any vlan tag assigned 380198892Srdivacky * to the station and/or any Diffserv setting in an IP header. 381198892Srdivacky * Finally, if an ACM policy is setup (in station mode) it's 382198892Srdivacky * applied. 383 */ 384int 385ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni) 386{ 387 int v_wme_ac, d_wme_ac, ac; 388#ifdef INET 389 struct ether_header *eh; 390#endif 391 392 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) { 393 ac = WME_AC_BE; 394 goto done; 395 } 396 397 /* 398 * If node has a vlan tag then all traffic 399 * to it must have a matching tag. 400 */ 401 v_wme_ac = 0; 402 if (ni->ni_vlan != 0) { 403 if ((m->m_flags & M_VLANTAG) == 0) { 404 IEEE80211_NODE_STAT(ni, tx_novlantag); 405 return 1; 406 } 407 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 408 EVL_VLANOFTAG(ni->ni_vlan)) { 409 IEEE80211_NODE_STAT(ni, tx_vlanmismatch); 410 return 1; 411 } 412 /* map vlan priority to AC */ 413 switch (EVL_PRIOFTAG(ni->ni_vlan)) { 414 case 1: 415 case 2: 416 v_wme_ac = WME_AC_BK; 417 break; 418 case 0: 419 case 3: 420 v_wme_ac = WME_AC_BE; 421 break; 422 case 4: 423 case 5: 424 v_wme_ac = WME_AC_VI; 425 break; 426 case 6: 427 case 7: 428 v_wme_ac = WME_AC_VO; 429 break; 430 } 431 } 432 433#ifdef INET 434 eh = mtod(m, struct ether_header *); 435 if (eh->ether_type == htons(ETHERTYPE_IP)) { 436 const struct ip *ip = (struct ip *) 437 (mtod(m, u_int8_t *) + sizeof (*eh)); 438 /* 439 * IP frame, map the TOS field. 440 */ 441 switch (ip->ip_tos) { 442 case 0x08: 443 case 0x20: 444 d_wme_ac = WME_AC_BK; /* background */ 445 break; 446 case 0x28: 447 case 0xa0: 448 d_wme_ac = WME_AC_VI; /* video */ 449 break; 450 case 0x30: /* voice */ 451 case 0xe0: 452 case 0x88: /* XXX UPSD */ 453 case 0xb8: 454 d_wme_ac = WME_AC_VO; 455 break; 456 default: 457 d_wme_ac = WME_AC_BE; 458 break; 459 } 460 } else { 461#endif /* INET */ 462 d_wme_ac = WME_AC_BE; 463#ifdef INET 464 } 465#endif 466 /* 467 * Use highest priority AC. 468 */ 469 if (v_wme_ac > d_wme_ac) 470 ac = v_wme_ac; 471 else 472 ac = d_wme_ac; 473 474 /* 475 * Apply ACM policy. 476 */ 477 if (ic->ic_opmode == IEEE80211_M_STA) { 478 static const int acmap[4] = { 479 WME_AC_BK, /* WME_AC_BE */ 480 WME_AC_BK, /* WME_AC_BK */ 481 WME_AC_BE, /* WME_AC_VI */ 482 WME_AC_VI, /* WME_AC_VO */ 483 }; 484 while (ac != WME_AC_BK && 485 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm) 486 ac = acmap[ac]; 487 } 488done: 489 M_WME_SETAC(m, ac); 490 return 0; 491} 492 493/* 494 * Insure there is sufficient contiguous space to encapsulate the 495 * 802.11 data frame. If room isn't already there, arrange for it. 496 * Drivers and cipher modules assume we have done the necessary work 497 * and fail rudely if they don't find the space they need. 498 */ 499static struct mbuf * 500ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize, 501 struct ieee80211_key *key, struct mbuf *m) 502{ 503#define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc)) 504 int needed_space = hdrsize; 505 506 if (key != NULL) { 507 /* XXX belongs in crypto code? */ 508 needed_space += key->wk_cipher->ic_header; 509 /* XXX frags */ 510 /* 511 * When crypto is being done in the host we must insure 512 * the data are writable for the cipher routines; clone 513 * a writable mbuf chain. 514 * XXX handle SWMIC specially 515 */ 516 if (key->wk_flags & (IEEE80211_KEY_SWCRYPT|IEEE80211_KEY_SWMIC)) { 517 m = m_unshare(m, M_NOWAIT); 518 if (m == NULL) { 519 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 520 "%s: cannot get writable mbuf\n", __func__); 521 ic->ic_stats.is_tx_nobuf++; /* XXX new stat */ 522 return NULL; 523 } 524 } 525 } 526 /* 527 * We know we are called just before stripping an Ethernet 528 * header and prepending an LLC header. This means we know 529 * there will be 530 * sizeof(struct ether_header) - sizeof(struct llc) 531 * bytes recovered to which we need additional space for the 532 * 802.11 header and any crypto header. 533 */ 534 /* XXX check trailing space and copy instead? */ 535 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) { 536 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type); 537 if (n == NULL) { 538 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 539 "%s: cannot expand storage\n", __func__); 540 ic->ic_stats.is_tx_nobuf++; 541 m_freem(m); 542 return NULL; 543 } 544 KASSERT(needed_space <= MHLEN, 545 ("not enough room, need %u got %zu\n", needed_space, MHLEN)); 546 /* 547 * Setup new mbuf to have leading space to prepend the 548 * 802.11 header and any crypto header bits that are 549 * required (the latter are added when the driver calls 550 * back to ieee80211_crypto_encap to do crypto encapsulation). 551 */ 552 /* NB: must be first 'cuz it clobbers m_data */ 553 m_move_pkthdr(n, m); 554 n->m_len = 0; /* NB: m_gethdr does not set */ 555 n->m_data += needed_space; 556 /* 557 * Pull up Ethernet header to create the expected layout. 558 * We could use m_pullup but that's overkill (i.e. we don't 559 * need the actual data) and it cannot fail so do it inline 560 * for speed. 561 */ 562 /* NB: struct ether_header is known to be contiguous */ 563 n->m_len += sizeof(struct ether_header); 564 m->m_len -= sizeof(struct ether_header); 565 m->m_data += sizeof(struct ether_header); 566 /* 567 * Replace the head of the chain. 568 */ 569 n->m_next = m; 570 m = n; 571 } 572 return m; 573#undef TO_BE_RECLAIMED 574} 575 576#define KEY_UNDEFINED(k) ((k).wk_cipher == &ieee80211_cipher_none) 577/* 578 * Return the transmit key to use in sending a unicast frame. 579 * If a unicast key is set we use that. When no unicast key is set 580 * we fall back to the default transmit key. 581 */ 582static __inline struct ieee80211_key * 583ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni) 584{ 585 if (KEY_UNDEFINED(ni->ni_ucastkey)) { 586 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 587 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey])) 588 return NULL; 589 return &ic->ic_nw_keys[ic->ic_def_txkey]; 590 } else { 591 return &ni->ni_ucastkey; 592 } 593} 594 595/* 596 * Return the transmit key to use in sending a multicast frame. 597 * Multicast traffic always uses the group key which is installed as 598 * the default tx key. 599 */ 600static __inline struct ieee80211_key * 601ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni) 602{ 603 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 604 KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey])) 605 return NULL; 606 return &ic->ic_nw_keys[ic->ic_def_txkey]; 607} 608 609/* 610 * Encapsulate an outbound data frame. The mbuf chain is updated. 611 * If an error is encountered NULL is returned. The caller is required 612 * to provide a node reference and pullup the ethernet header in the 613 * first mbuf. 614 */ 615struct mbuf * 616ieee80211_encap(struct ieee80211com *ic, struct mbuf *m, 617 struct ieee80211_node *ni) 618{ 619 struct ether_header eh; 620 struct ieee80211_frame *wh; 621 struct ieee80211_key *key; 622 struct llc *llc; 623 int hdrsize, datalen, addqos; 624 625 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!")); 626 memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header)); 627 628 /* 629 * Insure space for additional headers. First identify 630 * transmit key to use in calculating any buffer adjustments 631 * required. This is also used below to do privacy 632 * encapsulation work. Then calculate the 802.11 header 633 * size and any padding required by the driver. 634 * 635 * Note key may be NULL if we fall back to the default 636 * transmit key and that is not set. In that case the 637 * buffer may not be expanded as needed by the cipher 638 * routines, but they will/should discard it. 639 */ 640 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 641 if (ic->ic_opmode == IEEE80211_M_STA || 642 !IEEE80211_IS_MULTICAST(eh.ether_dhost)) 643 key = ieee80211_crypto_getucastkey(ic, ni); 644 else 645 key = ieee80211_crypto_getmcastkey(ic, ni); 646 if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) { 647 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 648 "[%s] no default transmit key (%s) deftxkey %u\n", 649 ether_sprintf(eh.ether_dhost), __func__, 650 ic->ic_def_txkey); 651 ic->ic_stats.is_tx_nodefkey++; 652 } 653 } else 654 key = NULL; 655 /* XXX 4-address format */ 656 /* 657 * XXX Some ap's don't handle QoS-encapsulated EAPOL 658 * frames so suppress use. This may be an issue if other 659 * ap's require all data frames to be QoS-encapsulated 660 * once negotiated in which case we'll need to make this 661 * configurable. 662 */ 663 addqos = (ni->ni_flags & IEEE80211_NODE_QOS) && 664 eh.ether_type != htons(ETHERTYPE_PAE); 665 if (addqos) 666 hdrsize = sizeof(struct ieee80211_qosframe); 667 else 668 hdrsize = sizeof(struct ieee80211_frame); 669 if (ic->ic_flags & IEEE80211_F_DATAPAD) 670 hdrsize = roundup(hdrsize, sizeof(u_int32_t)); 671 m = ieee80211_mbuf_adjust(ic, hdrsize, key, m); 672 if (m == NULL) { 673 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 674 goto bad; 675 } 676 677 /* NB: this could be optimized because of ieee80211_mbuf_adjust */ 678 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 679 llc = mtod(m, struct llc *); 680 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 681 llc->llc_control = LLC_UI; 682 llc->llc_snap.org_code[0] = 0; 683 llc->llc_snap.org_code[1] = 0; 684 llc->llc_snap.org_code[2] = 0; 685 llc->llc_snap.ether_type = eh.ether_type; 686 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */ 687 688 M_PREPEND(m, hdrsize, M_DONTWAIT); 689 if (m == NULL) { 690 ic->ic_stats.is_tx_nobuf++; 691 goto bad; 692 } 693 wh = mtod(m, struct ieee80211_frame *); 694 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 695 *(u_int16_t *)wh->i_dur = 0; 696 switch (ic->ic_opmode) { 697 case IEEE80211_M_STA: 698 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 699 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 700 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 701 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 702 break; 703 case IEEE80211_M_IBSS: 704 case IEEE80211_M_AHDEMO: 705 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 706 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 707 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 708 /* 709 * NB: always use the bssid from ic_bss as the 710 * neighbor's may be stale after an ibss merge 711 */ 712 IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid); 713 break; 714 case IEEE80211_M_HOSTAP: 715 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 716 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 717 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 718 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 719 break; 720 case IEEE80211_M_MONITOR: 721 goto bad; 722 } 723 if (m->m_flags & M_MORE_DATA) 724 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 725 if (addqos) { 726 struct ieee80211_qosframe *qwh = 727 (struct ieee80211_qosframe *) wh; 728 int ac, tid; 729 730 ac = M_WME_GETAC(m); 731 /* map from access class/queue to 11e header priorty value */ 732 tid = WME_AC_TO_TID(ac); 733 qwh->i_qos[0] = tid & IEEE80211_QOS_TID; 734 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy) 735 qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S; 736 qwh->i_qos[1] = 0; 737 qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS; 738 739 *(u_int16_t *)wh->i_seq = 740 htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT); 741 ni->ni_txseqs[tid]++; 742 } else { 743 *(u_int16_t *)wh->i_seq = 744 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT); 745 ni->ni_txseqs[0]++; 746 } 747 if (key != NULL) { 748 /* 749 * IEEE 802.1X: send EAPOL frames always in the clear. 750 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set. 751 */ 752 if (eh.ether_type != htons(ETHERTYPE_PAE) || 753 ((ic->ic_flags & IEEE80211_F_WPA) && 754 (ic->ic_opmode == IEEE80211_M_STA ? 755 !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) { 756 wh->i_fc[1] |= IEEE80211_FC1_WEP; 757 /* XXX do fragmentation */ 758 if (!ieee80211_crypto_enmic(ic, key, m, 0)) { 759 IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 760 "[%s] enmic failed, discard frame\n", 761 ether_sprintf(eh.ether_dhost)); 762 ic->ic_stats.is_crypto_enmicfail++; 763 goto bad; 764 } 765 } 766 } 767 768 IEEE80211_NODE_STAT(ni, tx_data); 769 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 770 IEEE80211_NODE_STAT(ni, tx_mcast); 771 else 772 IEEE80211_NODE_STAT(ni, tx_ucast); 773 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen); 774 775 return m; 776bad: 777 if (m != NULL) 778 m_freem(m); 779 return NULL; 780} 781 782/* 783 * Add a supported rates element id to a frame. 784 */ 785static u_int8_t * 786ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs) 787{ 788 int nrates; 789 790 *frm++ = IEEE80211_ELEMID_RATES; 791 nrates = rs->rs_nrates; 792 if (nrates > IEEE80211_RATE_SIZE) 793 nrates = IEEE80211_RATE_SIZE; 794 *frm++ = nrates; 795 memcpy(frm, rs->rs_rates, nrates); 796 return frm + nrates; 797} 798 799/* 800 * Add an extended supported rates element id to a frame. 801 */ 802static u_int8_t * 803ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs) 804{ 805 /* 806 * Add an extended supported rates element if operating in 11g mode. 807 */ 808 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 809 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 810 *frm++ = IEEE80211_ELEMID_XRATES; 811 *frm++ = nrates; 812 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 813 frm += nrates; 814 } 815 return frm; 816} 817 818/* 819 * Add an ssid elemet to a frame. 820 */ 821static u_int8_t * 822ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len) 823{ 824 *frm++ = IEEE80211_ELEMID_SSID; 825 *frm++ = len; 826 memcpy(frm, ssid, len); 827 return frm + len; 828} 829 830/* 831 * Add an erp element to a frame. 832 */ 833static u_int8_t * 834ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic) 835{ 836 u_int8_t erp; 837 838 *frm++ = IEEE80211_ELEMID_ERP; 839 *frm++ = 1; 840 erp = 0; 841 if (ic->ic_nonerpsta != 0) 842 erp |= IEEE80211_ERP_NON_ERP_PRESENT; 843 if (ic->ic_flags & IEEE80211_F_USEPROT) 844 erp |= IEEE80211_ERP_USE_PROTECTION; 845 if (ic->ic_flags & IEEE80211_F_USEBARKER) 846 erp |= IEEE80211_ERP_LONG_PREAMBLE; 847 *frm++ = erp; 848 return frm; 849} 850 851static u_int8_t * 852ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie) 853{ 854#define WPA_OUI_BYTES 0x00, 0x50, 0xf2 855#define ADDSHORT(frm, v) do { \ 856 frm[0] = (v) & 0xff; \ 857 frm[1] = (v) >> 8; \ 858 frm += 2; \ 859} while (0) 860#define ADDSELECTOR(frm, sel) do { \ 861 memcpy(frm, sel, 4); \ 862 frm += 4; \ 863} while (0) 864 static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE }; 865 static const u_int8_t cipher_suite[][4] = { 866 { WPA_OUI_BYTES, WPA_CSE_WEP40 }, /* NB: 40-bit */ 867 { WPA_OUI_BYTES, WPA_CSE_TKIP }, 868 { 0x00, 0x00, 0x00, 0x00 }, /* XXX WRAP */ 869 { WPA_OUI_BYTES, WPA_CSE_CCMP }, 870 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */ 871 { WPA_OUI_BYTES, WPA_CSE_NULL }, 872 }; 873 static const u_int8_t wep104_suite[4] = 874 { WPA_OUI_BYTES, WPA_CSE_WEP104 }; 875 static const u_int8_t key_mgt_unspec[4] = 876 { WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC }; 877 static const u_int8_t key_mgt_psk[4] = 878 { WPA_OUI_BYTES, WPA_ASE_8021X_PSK }; 879 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn; 880 u_int8_t *frm = ie; 881 u_int8_t *selcnt; 882 883 *frm++ = IEEE80211_ELEMID_VENDOR; 884 *frm++ = 0; /* length filled in below */ 885 memcpy(frm, oui, sizeof(oui)); /* WPA OUI */ 886 frm += sizeof(oui); 887 ADDSHORT(frm, WPA_VERSION); 888 889 /* XXX filter out CKIP */ 890 891 /* multicast cipher */ 892 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP && 893 rsn->rsn_mcastkeylen >= 13) 894 ADDSELECTOR(frm, wep104_suite); 895 else 896 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]); 897 898 /* unicast cipher list */ 899 selcnt = frm; 900 ADDSHORT(frm, 0); /* selector count */ 901 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) { 902 selcnt[0]++; 903 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]); 904 } 905 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) { 906 selcnt[0]++; 907 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]); 908 } 909 910 /* authenticator selector list */ 911 selcnt = frm; 912 ADDSHORT(frm, 0); /* selector count */ 913 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) { 914 selcnt[0]++; 915 ADDSELECTOR(frm, key_mgt_unspec); 916 } 917 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) { 918 selcnt[0]++; 919 ADDSELECTOR(frm, key_mgt_psk); 920 } 921 922 /* optional capabilities */ 923 if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH) 924 ADDSHORT(frm, rsn->rsn_caps); 925 926 /* calculate element length */ 927 ie[1] = frm - ie - 2; 928 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa), 929 ("WPA IE too big, %u > %zu", 930 ie[1]+2, sizeof(struct ieee80211_ie_wpa))); 931 return frm; 932#undef ADDSHORT 933#undef ADDSELECTOR 934#undef WPA_OUI_BYTES 935} 936 937static u_int8_t * 938ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie) 939{ 940#define RSN_OUI_BYTES 0x00, 0x0f, 0xac 941#define ADDSHORT(frm, v) do { \ 942 frm[0] = (v) & 0xff; \ 943 frm[1] = (v) >> 8; \ 944 frm += 2; \ 945} while (0) 946#define ADDSELECTOR(frm, sel) do { \ 947 memcpy(frm, sel, 4); \ 948 frm += 4; \ 949} while (0) 950 static const u_int8_t cipher_suite[][4] = { 951 { RSN_OUI_BYTES, RSN_CSE_WEP40 }, /* NB: 40-bit */ 952 { RSN_OUI_BYTES, RSN_CSE_TKIP }, 953 { RSN_OUI_BYTES, RSN_CSE_WRAP }, 954 { RSN_OUI_BYTES, RSN_CSE_CCMP }, 955 { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */ 956 { RSN_OUI_BYTES, RSN_CSE_NULL }, 957 }; 958 static const u_int8_t wep104_suite[4] = 959 { RSN_OUI_BYTES, RSN_CSE_WEP104 }; 960 static const u_int8_t key_mgt_unspec[4] = 961 { RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC }; 962 static const u_int8_t key_mgt_psk[4] = 963 { RSN_OUI_BYTES, RSN_ASE_8021X_PSK }; 964 const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn; 965 u_int8_t *frm = ie; 966 u_int8_t *selcnt; 967 968 *frm++ = IEEE80211_ELEMID_RSN; 969 *frm++ = 0; /* length filled in below */ 970 ADDSHORT(frm, RSN_VERSION); 971 972 /* XXX filter out CKIP */ 973 974 /* multicast cipher */ 975 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP && 976 rsn->rsn_mcastkeylen >= 13) 977 ADDSELECTOR(frm, wep104_suite); 978 else 979 ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]); 980 981 /* unicast cipher list */ 982 selcnt = frm; 983 ADDSHORT(frm, 0); /* selector count */ 984 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) { 985 selcnt[0]++; 986 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]); 987 } 988 if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) { 989 selcnt[0]++; 990 ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]); 991 } 992 993 /* authenticator selector list */ 994 selcnt = frm; 995 ADDSHORT(frm, 0); /* selector count */ 996 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) { 997 selcnt[0]++; 998 ADDSELECTOR(frm, key_mgt_unspec); 999 } 1000 if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) { 1001 selcnt[0]++; 1002 ADDSELECTOR(frm, key_mgt_psk); 1003 } 1004 1005 /* optional capabilities */ 1006 ADDSHORT(frm, rsn->rsn_caps); 1007 /* XXX PMKID */ 1008 1009 /* calculate element length */ 1010 ie[1] = frm - ie - 2; 1011 KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa), 1012 ("RSN IE too big, %u > %zu", 1013 ie[1]+2, sizeof(struct ieee80211_ie_wpa))); 1014 return frm; 1015#undef ADDSELECTOR 1016#undef ADDSHORT 1017#undef RSN_OUI_BYTES 1018} 1019 1020/* 1021 * Add a WPA/RSN element to a frame. 1022 */ 1023static u_int8_t * 1024ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic) 1025{ 1026 1027 KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!")); 1028 if (ic->ic_flags & IEEE80211_F_WPA2) 1029 frm = ieee80211_setup_rsn_ie(ic, frm); 1030 if (ic->ic_flags & IEEE80211_F_WPA1) 1031 frm = ieee80211_setup_wpa_ie(ic, frm); 1032 return frm; 1033} 1034 1035#define WME_OUI_BYTES 0x00, 0x50, 0xf2 1036/* 1037 * Add a WME information element to a frame. 1038 */ 1039static u_int8_t * 1040ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme) 1041{ 1042 static const struct ieee80211_wme_info info = { 1043 .wme_id = IEEE80211_ELEMID_VENDOR, 1044 .wme_len = sizeof(struct ieee80211_wme_info) - 2, 1045 .wme_oui = { WME_OUI_BYTES }, 1046 .wme_type = WME_OUI_TYPE, 1047 .wme_subtype = WME_INFO_OUI_SUBTYPE, 1048 .wme_version = WME_VERSION, 1049 .wme_info = 0, 1050 }; 1051 memcpy(frm, &info, sizeof(info)); 1052 return frm + sizeof(info); 1053} 1054 1055/* 1056 * Add a WME parameters element to a frame. 1057 */ 1058static u_int8_t * 1059ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme) 1060{ 1061#define SM(_v, _f) (((_v) << _f##_S) & _f) 1062#define ADDSHORT(frm, v) do { \ 1063 frm[0] = (v) & 0xff; \ 1064 frm[1] = (v) >> 8; \ 1065 frm += 2; \ 1066} while (0) 1067 /* NB: this works 'cuz a param has an info at the front */ 1068 static const struct ieee80211_wme_info param = { 1069 .wme_id = IEEE80211_ELEMID_VENDOR, 1070 .wme_len = sizeof(struct ieee80211_wme_param) - 2, 1071 .wme_oui = { WME_OUI_BYTES }, 1072 .wme_type = WME_OUI_TYPE, 1073 .wme_subtype = WME_PARAM_OUI_SUBTYPE, 1074 .wme_version = WME_VERSION, 1075 }; 1076 int i; 1077 1078 memcpy(frm, ¶m, sizeof(param)); 1079 frm += __offsetof(struct ieee80211_wme_info, wme_info); 1080 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */ 1081 *frm++ = 0; /* reserved field */ 1082 for (i = 0; i < WME_NUM_AC; i++) { 1083 const struct wmeParams *ac = 1084 &wme->wme_bssChanParams.cap_wmeParams[i]; 1085 *frm++ = SM(i, WME_PARAM_ACI) 1086 | SM(ac->wmep_acm, WME_PARAM_ACM) 1087 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN) 1088 ; 1089 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX) 1090 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN) 1091 ; 1092 ADDSHORT(frm, ac->wmep_txopLimit); 1093 } 1094 return frm; 1095#undef SM 1096#undef ADDSHORT 1097} 1098#undef WME_OUI_BYTES 1099 1100/* 1101 * Send a probe request frame with the specified ssid 1102 * and any optional information element data. 1103 */ 1104int 1105ieee80211_send_probereq(struct ieee80211_node *ni, 1106 const u_int8_t sa[IEEE80211_ADDR_LEN], 1107 const u_int8_t da[IEEE80211_ADDR_LEN], 1108 const u_int8_t bssid[IEEE80211_ADDR_LEN], 1109 const u_int8_t *ssid, size_t ssidlen, 1110 const void *optie, size_t optielen) 1111{ 1112 struct ieee80211com *ic = ni->ni_ic; 1113 enum ieee80211_phymode mode; 1114 struct ieee80211_frame *wh; 1115 struct mbuf *m; 1116 u_int8_t *frm; 1117 1118 /* 1119 * Hold a reference on the node so it doesn't go away until after 1120 * the xmit is complete all the way in the driver. On error we 1121 * will remove our reference. 1122 */ 1123 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1124 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1125 __func__, __LINE__, 1126 ni, ether_sprintf(ni->ni_macaddr), 1127 ieee80211_node_refcnt(ni)+1); 1128 ieee80211_ref_node(ni); 1129 1130 /* 1131 * prreq frame format 1132 * [tlv] ssid 1133 * [tlv] supported rates 1134 * [tlv] extended supported rates 1135 * [tlv] user-specified ie's 1136 */ 1137 m = ieee80211_getmgtframe(&frm, 1138 2 + IEEE80211_NWID_LEN 1139 + 2 + IEEE80211_RATE_SIZE 1140 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1141 + (optie != NULL ? optielen : 0) 1142 ); 1143 if (m == NULL) { 1144 ic->ic_stats.is_tx_nobuf++; 1145 ieee80211_free_node(ni); 1146 return ENOMEM; 1147 } 1148 1149 frm = ieee80211_add_ssid(frm, ssid, ssidlen); 1150 mode = ieee80211_chan2mode(ic, ic->ic_curchan); 1151 frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]); 1152 frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]); 1153 1154 if (optie != NULL) { 1155 memcpy(frm, optie, optielen); 1156 frm += optielen; 1157 } 1158 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1159 1160 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1161 if (m == NULL) 1162 return ENOMEM; 1163 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 1164 m->m_pkthdr.rcvif = (void *)ni; 1165 1166 wh = mtod(m, struct ieee80211_frame *); 1167 ieee80211_send_setup(ic, ni, wh, 1168 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ, 1169 sa, da, bssid); 1170 /* XXX power management? */ 1171 1172 IEEE80211_NODE_STAT(ni, tx_probereq); 1173 IEEE80211_NODE_STAT(ni, tx_mgmt); 1174 1175 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 1176 "[%s] send probe req on channel %u\n", 1177 ether_sprintf(wh->i_addr1), 1178 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1179 1180 IF_ENQUEUE(&ic->ic_mgtq, m); 1181 if_start(ic->ic_ifp); 1182 return 0; 1183} 1184 1185/* 1186 * Calculate capability information for mgt frames. 1187 */ 1188static u_int16_t 1189getcapinfo(struct ieee80211com *ic, struct ieee80211_channel *chan) 1190{ 1191 u_int16_t capinfo; 1192 1193 KASSERT(ic->ic_opmode != IEEE80211_M_STA, ("station mode")); 1194 1195 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1196 capinfo = IEEE80211_CAPINFO_ESS; 1197 else if (ic->ic_opmode == IEEE80211_M_IBSS) 1198 capinfo = IEEE80211_CAPINFO_IBSS; 1199 else 1200 capinfo = 0; 1201 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1202 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1203 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1204 IEEE80211_IS_CHAN_2GHZ(chan)) 1205 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1206 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1207 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1208 return capinfo; 1209} 1210 1211/* 1212 * Send a management frame. The node is for the destination (or ic_bss 1213 * when in station mode). Nodes other than ic_bss have their reference 1214 * count bumped to reflect our use for an indeterminant time. 1215 */ 1216int 1217ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni, 1218 int type, int arg) 1219{ 1220#define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0) 1221 struct mbuf *m; 1222 u_int8_t *frm; 1223 u_int16_t capinfo; 1224 int has_challenge, is_shared_key, ret, timer, status; 1225 1226 KASSERT(ni != NULL, ("null node")); 1227 1228 /* 1229 * Hold a reference on the node so it doesn't go away until after 1230 * the xmit is complete all the way in the driver. On error we 1231 * will remove our reference. 1232 */ 1233 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1234 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1235 __func__, __LINE__, 1236 ni, ether_sprintf(ni->ni_macaddr), 1237 ieee80211_node_refcnt(ni)+1); 1238 ieee80211_ref_node(ni); 1239 1240 timer = 0; 1241 switch (type) { 1242 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1243 /* 1244 * probe response frame format 1245 * [8] time stamp 1246 * [2] beacon interval 1247 * [2] cabability information 1248 * [tlv] ssid 1249 * [tlv] supported rates 1250 * [tlv] parameter set (FH/DS) 1251 * [tlv] parameter set (IBSS) 1252 * [tlv] extended rate phy (ERP) 1253 * [tlv] extended supported rates 1254 * [tlv] WPA 1255 * [tlv] WME (optional) 1256 */ 1257 m = ieee80211_getmgtframe(&frm, 1258 8 1259 + sizeof(u_int16_t) 1260 + sizeof(u_int16_t) 1261 + 2 + IEEE80211_NWID_LEN 1262 + 2 + IEEE80211_RATE_SIZE 1263 + 7 /* max(7,3) */ 1264 + 6 1265 + 3 1266 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1267 /* XXX !WPA1+WPA2 fits w/o a cluster */ 1268 + (ic->ic_flags & IEEE80211_F_WPA ? 1269 2*sizeof(struct ieee80211_ie_wpa) : 0) 1270 + sizeof(struct ieee80211_wme_param) 1271 ); 1272 if (m == NULL) 1273 senderr(ENOMEM, is_tx_nobuf); 1274 1275 memset(frm, 0, 8); /* timestamp should be filled later */ 1276 frm += 8; 1277 *(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval); 1278 frm += 2; 1279 capinfo = getcapinfo(ic, ic->ic_curchan); 1280 *(u_int16_t *)frm = htole16(capinfo); 1281 frm += 2; 1282 1283 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid, 1284 ic->ic_bss->ni_esslen); 1285 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1286 1287 if (ic->ic_phytype == IEEE80211_T_FH) { 1288 *frm++ = IEEE80211_ELEMID_FHPARMS; 1289 *frm++ = 5; 1290 *frm++ = ni->ni_fhdwell & 0x00ff; 1291 *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff; 1292 *frm++ = IEEE80211_FH_CHANSET( 1293 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1294 *frm++ = IEEE80211_FH_CHANPAT( 1295 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1296 *frm++ = ni->ni_fhindex; 1297 } else { 1298 *frm++ = IEEE80211_ELEMID_DSPARMS; 1299 *frm++ = 1; 1300 *frm++ = ieee80211_chan2ieee(ic, ic->ic_curchan); 1301 } 1302 1303 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1304 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 1305 *frm++ = 2; 1306 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 1307 } 1308 if (ic->ic_flags & IEEE80211_F_WPA) 1309 frm = ieee80211_add_wpa(frm, ic); 1310 if (ic->ic_curmode == IEEE80211_MODE_11G) 1311 frm = ieee80211_add_erp(frm, ic); 1312 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1313 if (ic->ic_flags & IEEE80211_F_WME) 1314 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1315 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1316 break; 1317 1318 case IEEE80211_FC0_SUBTYPE_AUTH: 1319 status = arg >> 16; 1320 arg &= 0xffff; 1321 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE || 1322 arg == IEEE80211_AUTH_SHARED_RESPONSE) && 1323 ni->ni_challenge != NULL); 1324 1325 /* 1326 * Deduce whether we're doing open authentication or 1327 * shared key authentication. We do the latter if 1328 * we're in the middle of a shared key authentication 1329 * handshake or if we're initiating an authentication 1330 * request and configured to use shared key. 1331 */ 1332 is_shared_key = has_challenge || 1333 arg >= IEEE80211_AUTH_SHARED_RESPONSE || 1334 (arg == IEEE80211_AUTH_SHARED_REQUEST && 1335 ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED); 1336 1337 m = ieee80211_getmgtframe(&frm, 1338 3 * sizeof(u_int16_t) 1339 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ? 1340 sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0) 1341 ); 1342 if (m == NULL) 1343 senderr(ENOMEM, is_tx_nobuf); 1344 1345 ((u_int16_t *)frm)[0] = 1346 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED) 1347 : htole16(IEEE80211_AUTH_ALG_OPEN); 1348 ((u_int16_t *)frm)[1] = htole16(arg); /* sequence number */ 1349 ((u_int16_t *)frm)[2] = htole16(status);/* status */ 1350 1351 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) { 1352 ((u_int16_t *)frm)[3] = 1353 htole16((IEEE80211_CHALLENGE_LEN << 8) | 1354 IEEE80211_ELEMID_CHALLENGE); 1355 memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge, 1356 IEEE80211_CHALLENGE_LEN); 1357 m->m_pkthdr.len = m->m_len = 1358 4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN; 1359 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) { 1360 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 1361 "[%s] request encrypt frame (%s)\n", 1362 ether_sprintf(ni->ni_macaddr), __func__); 1363 m->m_flags |= M_LINK0; /* WEP-encrypt, please */ 1364 } 1365 } else 1366 m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t); 1367 1368 /* XXX not right for shared key */ 1369 if (status == IEEE80211_STATUS_SUCCESS) 1370 IEEE80211_NODE_STAT(ni, tx_auth); 1371 else 1372 IEEE80211_NODE_STAT(ni, tx_auth_fail); 1373 1374 if (ic->ic_opmode == IEEE80211_M_STA) 1375 timer = IEEE80211_TRANS_WAIT; 1376 break; 1377 1378 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1379 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 1380 "[%s] send station deauthenticate (reason %d)\n", 1381 ether_sprintf(ni->ni_macaddr), arg); 1382 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t)); 1383 if (m == NULL) 1384 senderr(ENOMEM, is_tx_nobuf); 1385 *(u_int16_t *)frm = htole16(arg); /* reason */ 1386 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t); 1387 1388 IEEE80211_NODE_STAT(ni, tx_deauth); 1389 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg); 1390 1391 ieee80211_node_unauthorize(ni); /* port closed */ 1392 break; 1393 1394 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1395 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1396 /* 1397 * asreq frame format 1398 * [2] capability information 1399 * [2] listen interval 1400 * [6*] current AP address (reassoc only) 1401 * [tlv] ssid 1402 * [tlv] supported rates 1403 * [tlv] extended supported rates 1404 * [tlv] WME 1405 * [tlv] user-specified ie's 1406 */ 1407 m = ieee80211_getmgtframe(&frm, 1408 sizeof(u_int16_t) 1409 + sizeof(u_int16_t) 1410 + IEEE80211_ADDR_LEN 1411 + 2 + IEEE80211_NWID_LEN 1412 + 2 + IEEE80211_RATE_SIZE 1413 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1414 + sizeof(struct ieee80211_wme_info) 1415 + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0) 1416 ); 1417 if (m == NULL) 1418 senderr(ENOMEM, is_tx_nobuf); 1419 1420 KASSERT(ic->ic_opmode == IEEE80211_M_STA, 1421 ("wrong mode %u", ic->ic_opmode)); 1422 capinfo = IEEE80211_CAPINFO_ESS; 1423 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1424 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1425 /* 1426 * NB: Some 11a AP's reject the request when 1427 * short premable is set. 1428 */ 1429 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1430 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 1431 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1432 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) && 1433 (ic->ic_caps & IEEE80211_C_SHSLOT)) 1434 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1435 *(u_int16_t *)frm = htole16(capinfo); 1436 frm += 2; 1437 1438 *(u_int16_t *)frm = htole16(ic->ic_lintval); 1439 frm += 2; 1440 1441 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 1442 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid); 1443 frm += IEEE80211_ADDR_LEN; 1444 } 1445 1446 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 1447 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1448 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1449 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 1450 frm = ieee80211_add_wme_info(frm, &ic->ic_wme); 1451 if (ic->ic_opt_ie != NULL) { 1452 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len); 1453 frm += ic->ic_opt_ie_len; 1454 } 1455 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1456 1457 timer = IEEE80211_TRANS_WAIT; 1458 break; 1459 1460 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 1461 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 1462 /* 1463 * asreq frame format 1464 * [2] capability information 1465 * [2] status 1466 * [2] association ID 1467 * [tlv] supported rates 1468 * [tlv] extended supported rates 1469 * [tlv] WME (if enabled and STA enabled) 1470 */ 1471 m = ieee80211_getmgtframe(&frm, 1472 sizeof(u_int16_t) 1473 + sizeof(u_int16_t) 1474 + sizeof(u_int16_t) 1475 + 2 + IEEE80211_RATE_SIZE 1476 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1477 + sizeof(struct ieee80211_wme_param) 1478 ); 1479 if (m == NULL) 1480 senderr(ENOMEM, is_tx_nobuf); 1481 1482 capinfo = getcapinfo(ic, ic->ic_curchan); 1483 *(u_int16_t *)frm = htole16(capinfo); 1484 frm += 2; 1485 1486 *(u_int16_t *)frm = htole16(arg); /* status */ 1487 frm += 2; 1488 1489 if (arg == IEEE80211_STATUS_SUCCESS) { 1490 *(u_int16_t *)frm = htole16(ni->ni_associd); 1491 IEEE80211_NODE_STAT(ni, tx_assoc); 1492 } else 1493 IEEE80211_NODE_STAT(ni, tx_assoc_fail); 1494 frm += 2; 1495 1496 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1497 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1498 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 1499 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1500 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1501 break; 1502 1503 case IEEE80211_FC0_SUBTYPE_DISASSOC: 1504 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1505 "[%s] send station disassociate (reason %d)\n", 1506 ether_sprintf(ni->ni_macaddr), arg); 1507 m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t)); 1508 if (m == NULL) 1509 senderr(ENOMEM, is_tx_nobuf); 1510 *(u_int16_t *)frm = htole16(arg); /* reason */ 1511 m->m_pkthdr.len = m->m_len = sizeof(u_int16_t); 1512 1513 IEEE80211_NODE_STAT(ni, tx_disassoc); 1514 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg); 1515 break; 1516 1517 default: 1518 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1519 "[%s] invalid mgmt frame type %u\n", 1520 ether_sprintf(ni->ni_macaddr), type); 1521 senderr(EINVAL, is_tx_unknownmgt); 1522 /* NOTREACHED */ 1523 } 1524 ret = ieee80211_mgmt_output(ic, ni, m, type, timer); 1525 if (ret != 0) { 1526bad: 1527 ieee80211_free_node(ni); 1528 } 1529 return ret; 1530#undef senderr 1531} 1532 1533/* 1534 * Allocate a beacon frame and fillin the appropriate bits. 1535 */ 1536struct mbuf * 1537ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni, 1538 struct ieee80211_beacon_offsets *bo) 1539{ 1540 struct ifnet *ifp = ic->ic_ifp; 1541 struct ieee80211_frame *wh; 1542 struct mbuf *m; 1543 int pktlen; 1544 u_int8_t *frm, *efrm; 1545 u_int16_t capinfo; 1546 struct ieee80211_rateset *rs; 1547 1548 /* 1549 * beacon frame format 1550 * [8] time stamp 1551 * [2] beacon interval 1552 * [2] cabability information 1553 * [tlv] ssid 1554 * [tlv] supported rates 1555 * [3] parameter set (DS) 1556 * [tlv] parameter set (IBSS/TIM) 1557 * [tlv] extended rate phy (ERP) 1558 * [tlv] extended supported rates 1559 * [tlv] WME parameters 1560 * [tlv] WPA/RSN parameters 1561 * XXX Vendor-specific OIDs (e.g. Atheros) 1562 * NB: we allocate the max space required for the TIM bitmap. 1563 */ 1564 rs = &ni->ni_rates; 1565 pktlen = 8 /* time stamp */ 1566 + sizeof(u_int16_t) /* beacon interval */ 1567 + sizeof(u_int16_t) /* capabilities */ 1568 + 2 + ni->ni_esslen /* ssid */ 1569 + 2 + IEEE80211_RATE_SIZE /* supported rates */ 1570 + 2 + 1 /* DS parameters */ 1571 + 2 + 4 + ic->ic_tim_len /* DTIM/IBSSPARMS */ 1572 + 2 + 1 /* ERP */ 1573 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1574 + (ic->ic_caps & IEEE80211_C_WME ? /* WME */ 1575 sizeof(struct ieee80211_wme_param) : 0) 1576 + (ic->ic_caps & IEEE80211_C_WPA ? /* WPA 1+2 */ 1577 2*sizeof(struct ieee80211_ie_wpa) : 0) 1578 ; 1579 m = ieee80211_getmgtframe(&frm, pktlen); 1580 if (m == NULL) { 1581 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1582 "%s: cannot get buf; size %u\n", __func__, pktlen); 1583 ic->ic_stats.is_tx_nobuf++; 1584 return NULL; 1585 } 1586 1587 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */ 1588 frm += 8; 1589 *(u_int16_t *)frm = htole16(ni->ni_intval); 1590 frm += 2; 1591 capinfo = getcapinfo(ic, ni->ni_chan); 1592 bo->bo_caps = (u_int16_t *)frm; 1593 *(u_int16_t *)frm = htole16(capinfo); 1594 frm += 2; 1595 *frm++ = IEEE80211_ELEMID_SSID; 1596 if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) { 1597 *frm++ = ni->ni_esslen; 1598 memcpy(frm, ni->ni_essid, ni->ni_esslen); 1599 frm += ni->ni_esslen; 1600 } else 1601 *frm++ = 0; 1602 frm = ieee80211_add_rates(frm, rs); 1603 if (ic->ic_curmode != IEEE80211_MODE_FH) { 1604 *frm++ = IEEE80211_ELEMID_DSPARMS; 1605 *frm++ = 1; 1606 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 1607 } 1608 bo->bo_tim = frm; 1609 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1610 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 1611 *frm++ = 2; 1612 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 1613 bo->bo_tim_len = 0; 1614 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 1615 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm; 1616 1617 tie->tim_ie = IEEE80211_ELEMID_TIM; 1618 tie->tim_len = 4; /* length */ 1619 tie->tim_count = 0; /* DTIM count */ 1620 tie->tim_period = ic->ic_dtim_period; /* DTIM period */ 1621 tie->tim_bitctl = 0; /* bitmap control */ 1622 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */ 1623 frm += sizeof(struct ieee80211_tim_ie); 1624 bo->bo_tim_len = 1; 1625 } 1626 bo->bo_trailer = frm; 1627 if (ic->ic_flags & IEEE80211_F_WME) { 1628 bo->bo_wme = frm; 1629 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1630 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE; 1631 } 1632 if (ic->ic_flags & IEEE80211_F_WPA) 1633 frm = ieee80211_add_wpa(frm, ic); 1634 if (ic->ic_curmode == IEEE80211_MODE_11G) { 1635 bo->bo_erp = frm; 1636 frm = ieee80211_add_erp(frm, ic); 1637 } 1638 efrm = ieee80211_add_xrates(frm, rs); 1639 bo->bo_trailer_len = efrm - bo->bo_trailer; 1640 m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *); 1641 1642 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1643 KASSERT(m != NULL, ("no space for 802.11 header?")); 1644 wh = mtod(m, struct ieee80211_frame *); 1645 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 1646 IEEE80211_FC0_SUBTYPE_BEACON; 1647 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1648 *(u_int16_t *)wh->i_dur = 0; 1649 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 1650 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 1651 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 1652 *(u_int16_t *)wh->i_seq = 0; 1653 1654 return m; 1655} 1656 1657/* 1658 * Update the dynamic parts of a beacon frame based on the current state. 1659 */ 1660int 1661ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni, 1662 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast) 1663{ 1664 int len_changed = 0; 1665 u_int16_t capinfo; 1666 1667 IEEE80211_BEACON_LOCK(ic); 1668 /* XXX faster to recalculate entirely or just changes? */ 1669 capinfo = getcapinfo(ic, ni->ni_chan); 1670 *bo->bo_caps = htole16(capinfo); 1671 1672 if (ic->ic_flags & IEEE80211_F_WME) { 1673 struct ieee80211_wme_state *wme = &ic->ic_wme; 1674 1675 /* 1676 * Check for agressive mode change. When there is 1677 * significant high priority traffic in the BSS 1678 * throttle back BE traffic by using conservative 1679 * parameters. Otherwise BE uses agressive params 1680 * to optimize performance of legacy/non-QoS traffic. 1681 */ 1682 if (wme->wme_flags & WME_F_AGGRMODE) { 1683 if (wme->wme_hipri_traffic > 1684 wme->wme_hipri_switch_thresh) { 1685 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 1686 "%s: traffic %u, disable aggressive mode\n", 1687 __func__, wme->wme_hipri_traffic); 1688 wme->wme_flags &= ~WME_F_AGGRMODE; 1689 ieee80211_wme_updateparams_locked(ic); 1690 wme->wme_hipri_traffic = 1691 wme->wme_hipri_switch_hysteresis; 1692 } else 1693 wme->wme_hipri_traffic = 0; 1694 } else { 1695 if (wme->wme_hipri_traffic <= 1696 wme->wme_hipri_switch_thresh) { 1697 IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 1698 "%s: traffic %u, enable aggressive mode\n", 1699 __func__, wme->wme_hipri_traffic); 1700 wme->wme_flags |= WME_F_AGGRMODE; 1701 ieee80211_wme_updateparams_locked(ic); 1702 wme->wme_hipri_traffic = 0; 1703 } else 1704 wme->wme_hipri_traffic = 1705 wme->wme_hipri_switch_hysteresis; 1706 } 1707 if (ic->ic_flags & IEEE80211_F_WMEUPDATE) { 1708 (void) ieee80211_add_wme_param(bo->bo_wme, wme); 1709 ic->ic_flags &= ~IEEE80211_F_WMEUPDATE; 1710 } 1711 } 1712 1713 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/ 1714 struct ieee80211_tim_ie *tie = 1715 (struct ieee80211_tim_ie *) bo->bo_tim; 1716 if (ic->ic_flags & IEEE80211_F_TIMUPDATE) { 1717 u_int timlen, timoff, i; 1718 /* 1719 * ATIM/DTIM needs updating. If it fits in the 1720 * current space allocated then just copy in the 1721 * new bits. Otherwise we need to move any trailing 1722 * data to make room. Note that we know there is 1723 * contiguous space because ieee80211_beacon_allocate 1724 * insures there is space in the mbuf to write a 1725 * maximal-size virtual bitmap (based on ic_max_aid). 1726 */ 1727 /* 1728 * Calculate the bitmap size and offset, copy any 1729 * trailer out of the way, and then copy in the 1730 * new bitmap and update the information element. 1731 * Note that the tim bitmap must contain at least 1732 * one byte and any offset must be even. 1733 */ 1734 if (ic->ic_ps_pending != 0) { 1735 timoff = 128; /* impossibly large */ 1736 for (i = 0; i < ic->ic_tim_len; i++) 1737 if (ic->ic_tim_bitmap[i]) { 1738 timoff = i &~ 1; 1739 break; 1740 } 1741 KASSERT(timoff != 128, ("tim bitmap empty!")); 1742 for (i = ic->ic_tim_len-1; i >= timoff; i--) 1743 if (ic->ic_tim_bitmap[i]) 1744 break; 1745 timlen = 1 + (i - timoff); 1746 } else { 1747 timoff = 0; 1748 timlen = 1; 1749 } 1750 if (timlen != bo->bo_tim_len) { 1751 /* copy up/down trailer */ 1752 int adjust = tie->tim_bitmap+timlen 1753 - bo->bo_trailer; 1754 ovbcopy(bo->bo_trailer, bo->bo_trailer+adjust, 1755 bo->bo_trailer_len); 1756 bo->bo_trailer += adjust; 1757 bo->bo_wme += adjust; 1758 bo->bo_erp += adjust; 1759 bo->bo_tim_len = timlen; 1760 1761 /* update information element */ 1762 tie->tim_len = 3 + timlen; 1763 tie->tim_bitctl = timoff; 1764 len_changed = 1; 1765 } 1766 memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff, 1767 bo->bo_tim_len); 1768 1769 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE; 1770 1771 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 1772 "%s: TIM updated, pending %u, off %u, len %u\n", 1773 __func__, ic->ic_ps_pending, timoff, timlen); 1774 } 1775 /* count down DTIM period */ 1776 if (tie->tim_count == 0) 1777 tie->tim_count = tie->tim_period - 1; 1778 else 1779 tie->tim_count--; 1780 /* update state for buffered multicast frames on DTIM */ 1781 if (mcast && tie->tim_count == 0) 1782 tie->tim_bitctl |= 1; 1783 else 1784 tie->tim_bitctl &= ~1; 1785 if (ic->ic_flags_ext & IEEE80211_FEXT_ERPUPDATE) { 1786 /* 1787 * ERP element needs updating. 1788 */ 1789 (void) ieee80211_add_erp(bo->bo_erp, ic); 1790 ic->ic_flags_ext &= ~IEEE80211_FEXT_ERPUPDATE; 1791 } 1792 } 1793 IEEE80211_BEACON_UNLOCK(ic); 1794 1795 return len_changed; 1796} 1797 1798/* 1799 * Save an outbound packet for a node in power-save sleep state. 1800 * The new packet is placed on the node's saved queue, and the TIM 1801 * is changed, if necessary. 1802 */ 1803void 1804ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni, 1805 struct mbuf *m) 1806{ 1807 int qlen, age; 1808 1809 IEEE80211_NODE_SAVEQ_LOCK(ni); 1810 if (_IF_QFULL(&ni->ni_savedq)) { 1811 _IF_DROP(&ni->ni_savedq); 1812 IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1813 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1814 "[%s] pwr save q overflow, drops %d (size %d)\n", 1815 ether_sprintf(ni->ni_macaddr), 1816 ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE); 1817#ifdef IEEE80211_DEBUG 1818 if (ieee80211_msg_dumppkts(ic)) 1819 ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1); 1820#endif 1821 m_freem(m); 1822 return; 1823 } 1824 /* 1825 * Tag the frame with it's expiry time and insert 1826 * it in the queue. The aging interval is 4 times 1827 * the listen interval specified by the station. 1828 * Frames that sit around too long are reclaimed 1829 * using this information. 1830 */ 1831 /* XXX handle overflow? */ 1832 age = ((ni->ni_intval * ic->ic_bintval) << 2) / 1024; /* TU -> secs */ 1833 _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age); 1834 IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1835 1836 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 1837 "[%s] save frame with age %d, %u now queued\n", 1838 ether_sprintf(ni->ni_macaddr), age, qlen); 1839 1840 if (qlen == 1) 1841 ic->ic_set_tim(ni, 1); 1842} 1843