ieee80211_output.c revision 148936
1193323Sed/*- 2193323Sed * Copyright (c) 2001 Atsushi Onoe 3193323Sed * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 4193323Sed * All rights reserved. 5193323Sed * 6193323Sed * Redistribution and use in source and binary forms, with or without 7193323Sed * modification, are permitted provided that the following conditions 8193323Sed * are met: 9193323Sed * 1. Redistributions of source code must retain the above copyright 10193323Sed * notice, this list of conditions and the following disclaimer. 11193323Sed * 2. Redistributions in binary form must reproduce the above copyright 12193323Sed * notice, this list of conditions and the following disclaimer in the 13201360Srdivacky * documentation and/or other materials provided with the distribution. 14193323Sed * 3. The name of the author may not be used to endorse or promote products 15252723Sdim * derived from this software without specific prior written permission. 16252723Sdim * 17252723Sdim * Alternatively, this software may be distributed under the terms of the 18252723Sdim * GNU General Public License ("GPL") version 2 as published by the Free 19252723Sdim * Software Foundation. 20252723Sdim * 21252723Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22245431Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23193323Sed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24193323Sed * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25193323Sed * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26193323Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27193323Sed * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28252723Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29252723Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30252723Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31252723Sdim */ 32252723Sdim 33252723Sdim#include <sys/cdefs.h> 34252723Sdim__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 148936 2005-08-10 16:22:30Z sam $"); 35252723Sdim 36252723Sdim#include "opt_inet.h" 37193323Sed 38202375Srdivacky#include <sys/param.h> 39198090Srdivacky#include <sys/systm.h> 40195098Sed#include <sys/mbuf.h> 41193323Sed#include <sys/kernel.h> 42252723Sdim#include <sys/endian.h> 43193323Sed 44252723Sdim#include <sys/socket.h> 45252723Sdim 46252723Sdim#include <net/bpf.h> 47252723Sdim#include <net/ethernet.h> 48252723Sdim#include <net/if.h> 49252723Sdim#include <net/if_llc.h> 50252723Sdim#include <net/if_media.h> 51193323Sed#include <net/if_vlan_var.h> 52193323Sed 53193323Sed#include <net80211/ieee80211_var.h> 54193323Sed 55193323Sed#ifdef INET 56193323Sed#include <netinet/in.h> 57198090Srdivacky#include <netinet/if_ether.h> 58193323Sed#include <netinet/in_systm.h> 59193323Sed#include <netinet/ip.h> 60193323Sed#endif 61193323Sed 62245431Sdim#ifdef IEEE80211_DEBUG 63245431Sdim/* 64245431Sdim * Decide if an outbound management frame should be 65193323Sed * printed when debugging is enabled. This filters some 66193323Sed * of the less interesting frames that come frequently 67193323Sed * (e.g. beacons). 68193323Sed */ 69193323Sedstatic __inline int 70193323Seddoprint(struct ieee80211com *ic, int subtype) 71193323Sed{ 72193323Sed switch (subtype) { 73193323Sed case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 74193323Sed return (ic->ic_opmode == IEEE80211_M_IBSS); 75193323Sed } 76193323Sed return 1; 77193323Sed} 78198090Srdivacky#endif 79193323Sed 80193323Sed/* 81193323Sed * Set the direction field and address fields of an outgoing 82193323Sed * non-QoS frame. Note this should be called early on in 83193323Sed * constructing a frame as it sets i_fc[1]; other bits can 84193323Sed * then be or'd in. 85252723Sdim */ 86252723Sdimstatic void 87193323Sedieee80211_send_setup(struct ieee80211com *ic, 88193323Sed struct ieee80211_node *ni, 89193323Sed struct ieee80211_frame *wh, 90193323Sed int type, 91193323Sed const u_int8_t sa[IEEE80211_ADDR_LEN], 92193323Sed const u_int8_t da[IEEE80211_ADDR_LEN], 93193323Sed const u_int8_t bssid[IEEE80211_ADDR_LEN]) 94193323Sed{ 95193323Sed#define WH4(wh) ((struct ieee80211_frame_addr4 *)wh) 96193323Sed 97193323Sed wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type; 98193323Sed if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) { 99218893Sdim switch (ic->ic_opmode) { 100193323Sed case IEEE80211_M_STA: 101193323Sed wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 102193323Sed IEEE80211_ADDR_COPY(wh->i_addr1, bssid); 103193323Sed IEEE80211_ADDR_COPY(wh->i_addr2, sa); 104193323Sed IEEE80211_ADDR_COPY(wh->i_addr3, da); 105193323Sed break; 106193323Sed case IEEE80211_M_IBSS: 107193323Sed case IEEE80211_M_AHDEMO: 108193323Sed wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 109193323Sed IEEE80211_ADDR_COPY(wh->i_addr1, da); 110193323Sed IEEE80211_ADDR_COPY(wh->i_addr2, sa); 111193323Sed IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 112193323Sed break; 113193323Sed case IEEE80211_M_HOSTAP: 114235633Sdim wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 115235633Sdim IEEE80211_ADDR_COPY(wh->i_addr1, da); 116235633Sdim IEEE80211_ADDR_COPY(wh->i_addr2, bssid); 117235633Sdim IEEE80211_ADDR_COPY(wh->i_addr3, sa); 118235633Sdim break; 119235633Sdim case IEEE80211_M_MONITOR: /* NB: to quiet compiler */ 120235633Sdim break; 121193323Sed } 122235633Sdim } else { 123245431Sdim wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 124245431Sdim IEEE80211_ADDR_COPY(wh->i_addr1, da); 125193323Sed IEEE80211_ADDR_COPY(wh->i_addr2, sa); 126245431Sdim IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 127245431Sdim } 128193323Sed *(u_int16_t *)&wh->i_dur[0] = 0; 129193323Sed /* NB: use non-QoS tid */ 130193323Sed *(u_int16_t *)&wh->i_seq[0] = 131193323Sed htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT); 132193323Sed ni->ni_txseqs[0]++; 133235633Sdim#undef WH4 134235633Sdim} 135193323Sed 136193323Sed/* 137193323Sed * Send a management frame to the specified node. The node pointer 138193323Sed * must have a reference as the pointer will be passed to the driver 139193323Sed * and potentially held for a long time. If the frame is successfully 140193323Sed * dispatched to the driver, then it is responsible for freeing the 141193323Sed * reference (and potentially free'ing up any associated storage). 142193323Sed */ 143193323Sedstatic int 144193323Sedieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni, 145193323Sed struct mbuf *m, int type) 146193323Sed{ 147218893Sdim struct ifnet *ifp = ic->ic_ifp; 148193323Sed struct ieee80211_frame *wh; 149193323Sed 150193323Sed KASSERT(ni != NULL, ("null node")); 151193323Sed 152193323Sed /* 153193323Sed * Yech, hack alert! We want to pass the node down to the 154193323Sed * driver's start routine. If we don't do so then the start 155193323Sed * routine must immediately look it up again and that can 156193323Sed * cause a lock order reversal if, for example, this frame 157193323Sed * is being sent because the station is being timedout and 158193323Sed * the frame being sent is a DEAUTH message. We could stick 159193323Sed * this in an m_tag and tack that on to the mbuf. However 160193323Sed * that's rather expensive to do for every frame so instead 161193574Sed * we stuff it in the rcvif field since outbound frames do 162193323Sed * not (presently) use this. 163193323Sed */ 164245431Sdim M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 165245431Sdim if (m == NULL) 166193323Sed return ENOMEM; 167245431Sdim KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 168245431Sdim m->m_pkthdr.rcvif = (void *)ni; 169193323Sed 170193323Sed wh = mtod(m, struct ieee80211_frame *); 171193323Sed ieee80211_send_setup(ic, ni, wh, 172193323Sed IEEE80211_FC0_TYPE_MGT | type, 173193574Sed ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid); 174193323Sed if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) { 175193323Sed m->m_flags &= ~M_LINK0; 176193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 177193323Sed "[%s] encrypting frame (%s)\n", 178193323Sed ether_sprintf(wh->i_addr1), __func__); 179193323Sed wh->i_fc[1] |= IEEE80211_FC1_WEP; 180193323Sed } 181193323Sed#ifdef IEEE80211_DEBUG 182193323Sed /* avoid printing too many frames */ 183193323Sed if ((ieee80211_msg_debug(ic) && doprint(ic, type)) || 184193323Sed ieee80211_msg_dumppkts(ic)) { 185193323Sed printf("[%s] send %s on channel %u\n", 186193323Sed ether_sprintf(wh->i_addr1), 187193323Sed ieee80211_mgt_subtype_name[ 188193323Sed (type & IEEE80211_FC0_SUBTYPE_MASK) >> 189193323Sed IEEE80211_FC0_SUBTYPE_SHIFT], 190193323Sed ieee80211_chan2ieee(ic, ic->ic_curchan)); 191193323Sed } 192193323Sed#endif 193193323Sed IEEE80211_NODE_STAT(ni, tx_mgmt); 194218893Sdim IF_ENQUEUE(&ic->ic_mgtq, m); 195218893Sdim ifp->if_timer = 1; 196193323Sed if_start(ifp); 197193323Sed return 0; 198193323Sed} 199193323Sed 200193323Sed/* 201193323Sed * Send a null data frame to the specified node. 202193323Sed * 203193323Sed * NB: the caller is assumed to have setup a node reference 204245431Sdim * for use; this is necessary to deal with a race condition 205245431Sdim * when probing for inactive stations. 206245431Sdim */ 207245431Sdimint 208245431Sdimieee80211_send_nulldata(struct ieee80211_node *ni) 209245431Sdim{ 210245431Sdim struct ieee80211com *ic = ni->ni_ic; 211245431Sdim struct ifnet *ifp = ic->ic_ifp; 212245431Sdim struct mbuf *m; 213245431Sdim struct ieee80211_frame *wh; 214245431Sdim 215245431Sdim MGETHDR(m, M_NOWAIT, MT_HEADER); 216245431Sdim if (m == NULL) { 217245431Sdim /* XXX debug msg */ 218245431Sdim ic->ic_stats.is_tx_nobuf++; 219245431Sdim ieee80211_unref_node(&ni); 220193323Sed return ENOMEM; 221193323Sed } 222193323Sed m->m_pkthdr.rcvif = (void *) ni; 223193323Sed 224193323Sed wh = mtod(m, struct ieee80211_frame *); 225193323Sed ieee80211_send_setup(ic, ni, wh, 226193323Sed IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA, 227193323Sed ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid); 228193323Sed /* NB: power management bit is never sent by an AP */ 229193323Sed if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 230193323Sed ic->ic_opmode != IEEE80211_M_HOSTAP) 231193323Sed wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT; 232193323Sed m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame); 233193323Sed 234193323Sed IEEE80211_NODE_STAT(ni, tx_data); 235193323Sed 236193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 237193323Sed "[%s] send null data frame on channel %u, pwr mgt %s\n", 238193323Sed ether_sprintf(ni->ni_macaddr), 239193323Sed ieee80211_chan2ieee(ic, ic->ic_curchan), 240193323Sed wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis"); 241193323Sed 242193323Sed IF_ENQUEUE(&ic->ic_mgtq, m); /* cheat */ 243193323Sed if_start(ifp); 244193323Sed 245193323Sed return 0; 246193323Sed} 247193323Sed 248193323Sed/* 249193323Sed * Assign priority to a frame based on any vlan tag assigned 250193323Sed * to the station and/or any Diffserv setting in an IP header. 251193323Sed * Finally, if an ACM policy is setup (in station mode) it's 252193323Sed * applied. 253198090Srdivacky */ 254193323Sedint 255193323Sedieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni) 256193323Sed{ 257193323Sed int v_wme_ac, d_wme_ac, ac; 258193323Sed#ifdef INET 259193323Sed struct ether_header *eh; 260193323Sed#endif 261193323Sed 262193323Sed if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) { 263193323Sed ac = WME_AC_BE; 264193323Sed goto done; 265193323Sed } 266193323Sed 267193323Sed /* 268193323Sed * If node has a vlan tag then all traffic 269193323Sed * to it must have a matching tag. 270193323Sed */ 271193323Sed v_wme_ac = 0; 272193323Sed if (ni->ni_vlan != 0) { 273193323Sed struct m_tag *mtag = VLAN_OUTPUT_TAG(ic->ic_ifp, m); 274193323Sed if (mtag == NULL) { 275193323Sed IEEE80211_NODE_STAT(ni, tx_novlantag); 276193323Sed return 1; 277193323Sed } 278193323Sed if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) != 279193323Sed EVL_VLANOFTAG(ni->ni_vlan)) { 280193323Sed IEEE80211_NODE_STAT(ni, tx_vlanmismatch); 281193323Sed return 1; 282193323Sed } 283193323Sed /* map vlan priority to AC */ 284193323Sed switch (EVL_PRIOFTAG(ni->ni_vlan)) { 285193323Sed case 1: 286193323Sed case 2: 287193323Sed v_wme_ac = WME_AC_BK; 288193323Sed break; 289193323Sed case 0: 290193323Sed case 3: 291193323Sed v_wme_ac = WME_AC_BE; 292193323Sed break; 293193323Sed case 4: 294193323Sed case 5: 295193323Sed v_wme_ac = WME_AC_VI; 296193323Sed break; 297193323Sed case 6: 298193323Sed case 7: 299193323Sed v_wme_ac = WME_AC_VO; 300193323Sed break; 301193323Sed } 302193323Sed } 303193323Sed 304193323Sed#ifdef INET 305193323Sed eh = mtod(m, struct ether_header *); 306193323Sed if (eh->ether_type == htons(ETHERTYPE_IP)) { 307193323Sed const struct ip *ip = (struct ip *) 308193323Sed (mtod(m, u_int8_t *) + sizeof (*eh)); 309193323Sed /* 310193323Sed * IP frame, map the TOS field. 311193323Sed */ 312193323Sed switch (ip->ip_tos) { 313193323Sed case 0x08: 314193323Sed case 0x20: 315193323Sed d_wme_ac = WME_AC_BK; /* background */ 316193323Sed break; 317193323Sed case 0x28: 318193323Sed case 0xa0: 319193323Sed d_wme_ac = WME_AC_VI; /* video */ 320193323Sed break; 321193323Sed case 0x30: /* voice */ 322193323Sed case 0xe0: 323193323Sed case 0x88: /* XXX UPSD */ 324193323Sed case 0xb8: 325193323Sed d_wme_ac = WME_AC_VO; 326193323Sed break; 327193323Sed default: 328193323Sed d_wme_ac = WME_AC_BE; 329193323Sed break; 330193323Sed } 331193323Sed } else { 332193323Sed#endif /* INET */ 333193323Sed d_wme_ac = WME_AC_BE; 334193323Sed#ifdef INET 335193323Sed } 336193323Sed#endif 337193323Sed /* 338193323Sed * Use highest priority AC. 339193323Sed */ 340193323Sed if (v_wme_ac > d_wme_ac) 341193323Sed ac = v_wme_ac; 342193323Sed else 343193323Sed ac = d_wme_ac; 344193323Sed 345193323Sed /* 346193323Sed * Apply ACM policy. 347193323Sed */ 348193323Sed if (ic->ic_opmode == IEEE80211_M_STA) { 349193323Sed static const int acmap[4] = { 350193323Sed WME_AC_BK, /* WME_AC_BE */ 351193323Sed WME_AC_BK, /* WME_AC_BK */ 352193323Sed WME_AC_BE, /* WME_AC_VI */ 353193323Sed WME_AC_VI, /* WME_AC_VO */ 354193323Sed }; 355193323Sed while (ac != WME_AC_BK && 356193323Sed ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm) 357193323Sed ac = acmap[ac]; 358193323Sed } 359193323Seddone: 360193323Sed M_WME_SETAC(m, ac); 361193323Sed return 0; 362193323Sed} 363193323Sed 364193323Sed/* 365193323Sed * Insure there is sufficient contiguous space to encapsulate the 366193323Sed * 802.11 data frame. If room isn't already there, arrange for it. 367195098Sed * Drivers and cipher modules assume we have done the necessary work 368195098Sed * and fail rudely if they don't find the space they need. 369198090Srdivacky */ 370193323Sedstatic struct mbuf * 371193323Sedieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize, 372193323Sed struct ieee80211_key *key, struct mbuf *m) 373193323Sed{ 374193323Sed#define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc)) 375193323Sed int needed_space = hdrsize; 376193323Sed 377193323Sed if (key != NULL) { 378193323Sed /* XXX belongs in crypto code? */ 379193323Sed needed_space += key->wk_cipher->ic_header; 380193323Sed /* XXX frags */ 381193323Sed } 382193323Sed /* 383193323Sed * We know we are called just before stripping an Ethernet 384193323Sed * header and prepending an LLC header. This means we know 385193323Sed * there will be 386193323Sed * sizeof(struct ether_header) - sizeof(struct llc) 387195098Sed * bytes recovered to which we need additional space for the 388245431Sdim * 802.11 header and any crypto header. 389193323Sed */ 390193323Sed /* XXX check trailing space and copy instead? */ 391193323Sed if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) { 392193323Sed struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type); 393193323Sed if (n == NULL) { 394193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 395193323Sed "%s: cannot expand storage\n", __func__); 396193323Sed ic->ic_stats.is_tx_nobuf++; 397235633Sdim m_freem(m); 398235633Sdim return NULL; 399235633Sdim } 400193323Sed KASSERT(needed_space <= MHLEN, 401193323Sed ("not enough room, need %u got %zu\n", needed_space, MHLEN)); 402193323Sed /* 403193323Sed * Setup new mbuf to have leading space to prepend the 404193323Sed * 802.11 header and any crypto header bits that are 405193323Sed * required (the latter are added when the driver calls 406193323Sed * back to ieee80211_crypto_encap to do crypto encapsulation). 407193323Sed */ 408193323Sed /* NB: must be first 'cuz it clobbers m_data */ 409193323Sed m_move_pkthdr(n, m); 410195098Sed n->m_len = 0; /* NB: m_gethdr does not set */ 411193323Sed n->m_data += needed_space; 412193323Sed /* 413193323Sed * Pull up Ethernet header to create the expected layout. 414193323Sed * We could use m_pullup but that's overkill (i.e. we don't 415193323Sed * need the actual data) and it cannot fail so do it inline 416193323Sed * for speed. 417193323Sed */ 418226890Sdim /* NB: struct ether_header is known to be contiguous */ 419193323Sed n->m_len += sizeof(struct ether_header); 420193323Sed m->m_len -= sizeof(struct ether_header); 421195098Sed m->m_data += sizeof(struct ether_header); 422193323Sed /* 423193323Sed * Replace the head of the chain. 424245431Sdim */ 425245431Sdim n->m_next = m; 426245431Sdim m = n; 427245431Sdim } 428245431Sdim return m; 429245431Sdim#undef TO_BE_RECLAIMED 430245431Sdim} 431193323Sed 432193323Sed#define KEY_UNDEFINED(k) ((k).wk_cipher == &ieee80211_cipher_none) 433193323Sed/* 434193323Sed * Return the transmit key to use in sending a unicast frame. 435245431Sdim * If a unicast key is set we use that. When no unicast key is set 436193323Sed * we fall back to the default transmit key. 437193323Sed */ 438193323Sedstatic __inline struct ieee80211_key * 439193323Sedieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni) 440193323Sed{ 441193323Sed if (KEY_UNDEFINED(ni->ni_ucastkey)) { 442245431Sdim if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 443193323Sed KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey])) 444193323Sed return NULL; 445193323Sed return &ic->ic_nw_keys[ic->ic_def_txkey]; 446193323Sed } else { 447193323Sed return &ni->ni_ucastkey; 448193323Sed } 449193323Sed} 450193323Sed 451193323Sed/* 452193323Sed * Return the transmit key to use in sending a multicast frame. 453193323Sed * Multicast traffic always uses the group key which is installed as 454193323Sed * the default tx key. 455193323Sed */ 456226890Sdimstatic __inline struct ieee80211_key * 457226890Sdimieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni) 458226890Sdim{ 459193323Sed if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE || 460193323Sed KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey])) 461193323Sed return NULL; 462245431Sdim return &ic->ic_nw_keys[ic->ic_def_txkey]; 463193323Sed} 464193323Sed 465245431Sdim/* 466245431Sdim * Encapsulate an outbound data frame. The mbuf chain is updated. 467245431Sdim * If an error is encountered NULL is returned. The caller is required 468245431Sdim * to provide a node reference and pullup the ethernet header in the 469245431Sdim * first mbuf. 470193323Sed */ 471193323Sedstruct mbuf * 472198090Srdivackyieee80211_encap(struct ieee80211com *ic, struct mbuf *m, 473193323Sed struct ieee80211_node *ni) 474193323Sed{ 475193323Sed struct ether_header eh; 476193323Sed struct ieee80211_frame *wh; 477198892Srdivacky struct ieee80211_key *key; 478198892Srdivacky struct llc *llc; 479245431Sdim int hdrsize, datalen, addqos; 480245431Sdim 481245431Sdim KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!")); 482245431Sdim memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header)); 483198892Srdivacky 484198892Srdivacky /* 485193323Sed * Insure space for additional headers. First identify 486245431Sdim * transmit key to use in calculating any buffer adjustments 487245431Sdim * required. This is also used below to do privacy 488245431Sdim * encapsulation work. Then calculate the 802.11 header 489245431Sdim * size and any padding required by the driver. 490193323Sed * 491193323Sed * Note key may be NULL if we fall back to the default 492193323Sed * transmit key and that is not set. In that case the 493193323Sed * buffer may not be expanded as needed by the cipher 494193323Sed * routines, but they will/should discard it. 495193323Sed */ 496193323Sed if (ic->ic_flags & IEEE80211_F_PRIVACY) { 497193323Sed if (ic->ic_opmode == IEEE80211_M_STA || 498193323Sed !IEEE80211_IS_MULTICAST(eh.ether_dhost)) 499193323Sed key = ieee80211_crypto_getucastkey(ic, ni); 500193323Sed else 501193323Sed key = ieee80211_crypto_getmcastkey(ic, ni); 502193323Sed if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) { 503193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 504193323Sed "[%s] no default transmit key (%s) deftxkey %u\n", 505193323Sed ether_sprintf(eh.ether_dhost), __func__, 506204642Srdivacky ic->ic_def_txkey); 507193323Sed ic->ic_stats.is_tx_nodefkey++; 508193323Sed } 509193323Sed } else 510204642Srdivacky key = NULL; 511235633Sdim /* XXX 4-address format */ 512193323Sed /* 513193323Sed * XXX Some ap's don't handle QoS-encapsulated EAPOL 514193323Sed * frames so suppress use. This may be an issue if other 515193323Sed * ap's require all data frames to be QoS-encapsulated 516193323Sed * once negotiated in which case we'll need to make this 517193323Sed * configurable. 518204642Srdivacky */ 519235633Sdim addqos = (ni->ni_flags & IEEE80211_NODE_QOS) && 520235633Sdim eh.ether_type != htons(ETHERTYPE_PAE); 521193323Sed if (addqos) 522193323Sed hdrsize = sizeof(struct ieee80211_qosframe); 523193323Sed else 524193323Sed hdrsize = sizeof(struct ieee80211_frame); 525193323Sed if (ic->ic_flags & IEEE80211_F_DATAPAD) 526193323Sed hdrsize = roundup(hdrsize, sizeof(u_int32_t)); 527193323Sed m = ieee80211_mbuf_adjust(ic, hdrsize, key, m); 528193323Sed if (m == NULL) { 529218893Sdim /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 530193323Sed goto bad; 531193323Sed } 532193323Sed 533193323Sed /* NB: this could be optimized because of ieee80211_mbuf_adjust */ 534193323Sed m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 535193323Sed llc = mtod(m, struct llc *); 536193323Sed llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 537193323Sed llc->llc_control = LLC_UI; 538193323Sed llc->llc_snap.org_code[0] = 0; 539193323Sed llc->llc_snap.org_code[1] = 0; 540193323Sed llc->llc_snap.org_code[2] = 0; 541218893Sdim llc->llc_snap.ether_type = eh.ether_type; 542193323Sed datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */ 543193323Sed 544193323Sed M_PREPEND(m, hdrsize, M_DONTWAIT); 545193323Sed if (m == NULL) { 546193323Sed ic->ic_stats.is_tx_nobuf++; 547193323Sed goto bad; 548193323Sed } 549193323Sed wh = mtod(m, struct ieee80211_frame *); 550193323Sed wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 551193323Sed *(u_int16_t *)wh->i_dur = 0; 552193323Sed switch (ic->ic_opmode) { 553193323Sed case IEEE80211_M_STA: 554193323Sed wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 555193323Sed IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 556193323Sed IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 557193323Sed IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 558193323Sed break; 559193323Sed case IEEE80211_M_IBSS: 560193323Sed case IEEE80211_M_AHDEMO: 561193323Sed wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 562193323Sed IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 563193323Sed IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 564193323Sed /* 565193323Sed * NB: always use the bssid from ic_bss as the 566193323Sed * neighbor's may be stale after an ibss merge 567193323Sed */ 568193323Sed IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid); 569245431Sdim break; 570193323Sed case IEEE80211_M_HOSTAP: 571193323Sed wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 572193323Sed IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 573193323Sed IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 574193323Sed IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 575193323Sed break; 576245431Sdim case IEEE80211_M_MONITOR: 577245431Sdim goto bad; 578193323Sed } 579193323Sed if (m->m_flags & M_MORE_DATA) 580193323Sed wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 581193323Sed if (addqos) { 582193323Sed struct ieee80211_qosframe *qwh = 583193323Sed (struct ieee80211_qosframe *) wh; 584193323Sed int ac, tid; 585193323Sed 586193323Sed ac = M_WME_GETAC(m); 587193323Sed /* map from access class/queue to 11e header priorty value */ 588193323Sed tid = WME_AC_TO_TID(ac); 589193323Sed qwh->i_qos[0] = tid & IEEE80211_QOS_TID; 590193323Sed if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy) 591193323Sed qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S; 592193323Sed qwh->i_qos[1] = 0; 593193323Sed qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS; 594193323Sed 595193323Sed *(u_int16_t *)wh->i_seq = 596193323Sed htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT); 597193323Sed ni->ni_txseqs[tid]++; 598245431Sdim } else { 599193323Sed *(u_int16_t *)wh->i_seq = 600235633Sdim htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT); 601235633Sdim ni->ni_txseqs[0]++; 602235633Sdim } 603235633Sdim if (key != NULL) { 604235633Sdim /* 605235633Sdim * IEEE 802.1X: send EAPOL frames always in the clear. 606245431Sdim * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set. 607193323Sed */ 608193323Sed if (eh.ether_type != htons(ETHERTYPE_PAE) || 609193323Sed ((ic->ic_flags & IEEE80211_F_WPA) && 610193323Sed (ic->ic_opmode == IEEE80211_M_STA ? 611193323Sed !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) { 612193323Sed wh->i_fc[1] |= IEEE80211_FC1_WEP; 613193323Sed /* XXX do fragmentation */ 614193323Sed if (!ieee80211_crypto_enmic(ic, key, m, 0)) { 615193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT, 616193323Sed "[%s] enmic failed, discard frame\n", 617193323Sed ether_sprintf(eh.ether_dhost)); 618193323Sed ic->ic_stats.is_crypto_enmicfail++; 619193323Sed goto bad; 620193323Sed } 621193323Sed } 622193323Sed } 623193323Sed 624193323Sed IEEE80211_NODE_STAT(ni, tx_data); 625193323Sed IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen); 626193323Sed 627193323Sed return m; 628193323Sedbad: 629193323Sed if (m != NULL) 630193323Sed m_freem(m); 631193323Sed return NULL; 632193323Sed} 633193323Sed 634193323Sed/* 635193323Sed * Add a supported rates element id to a frame. 636193323Sed */ 637200581Srdivackystatic u_int8_t * 638206083Srdivackyieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs) 639224145Sdim{ 640206083Srdivacky int nrates; 641206083Srdivacky 642193323Sed *frm++ = IEEE80211_ELEMID_RATES; 643193323Sed nrates = rs->rs_nrates; 644193323Sed if (nrates > IEEE80211_RATE_SIZE) 645193323Sed nrates = IEEE80211_RATE_SIZE; 646193323Sed *frm++ = nrates; 647193323Sed memcpy(frm, rs->rs_rates, nrates); 648193323Sed return frm + nrates; 649193323Sed} 650193323Sed 651193323Sed/* 652193323Sed * Add an extended supported rates element id to a frame. 653193323Sed */ 654193323Sedstatic u_int8_t * 655193323Sedieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs) 656193323Sed{ 657193323Sed /* 658193323Sed * Add an extended supported rates element if operating in 11g mode. 659193323Sed */ 660193323Sed if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 661195098Sed int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 662195098Sed *frm++ = IEEE80211_ELEMID_XRATES; 663195098Sed *frm++ = nrates; 664195098Sed memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 665195098Sed frm += nrates; 666193323Sed } 667195098Sed return frm; 668193323Sed} 669198090Srdivacky 670193323Sed/* 671193323Sed * Add an ssid elemet to a frame. 672193323Sed */ 673198090Srdivackystatic u_int8_t * 674198090Srdivackyieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len) 675193323Sed{ 676193323Sed *frm++ = IEEE80211_ELEMID_SSID; 677193323Sed *frm++ = len; 678193323Sed memcpy(frm, ssid, len); 679193323Sed return frm + len; 680218893Sdim} 681218893Sdim 682193323Sed/* 683193323Sed * Add an erp element to a frame. 684193323Sed */ 685193323Sedstatic u_int8_t * 686193323Sedieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic) 687193323Sed{ 688193323Sed u_int8_t erp; 689218893Sdim 690193323Sed *frm++ = IEEE80211_ELEMID_ERP; 691193323Sed *frm++ = 1; 692202375Srdivacky erp = 0; 693198090Srdivacky if (ic->ic_nonerpsta != 0) 694193323Sed erp |= IEEE80211_ERP_NON_ERP_PRESENT; 695193323Sed if (ic->ic_flags & IEEE80211_F_USEPROT) 696193323Sed erp |= IEEE80211_ERP_USE_PROTECTION; 697193323Sed if (ic->ic_flags & IEEE80211_F_USEBARKER) 698193323Sed erp |= IEEE80211_ERP_LONG_PREAMBLE; 699193323Sed *frm++ = erp; 700193323Sed return frm; 701193323Sed} 702193323Sed 703193323Sedstatic u_int8_t * 704193323Sedieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie) 705245431Sdim{ 706193323Sed#define WPA_OUI_BYTES 0x00, 0x50, 0xf2 707193323Sed#define ADDSHORT(frm, v) do { \ 708193323Sed frm[0] = (v) & 0xff; \ 709193323Sed frm[1] = (v) >> 8; \ 710193323Sed frm += 2; \ 711193323Sed} while (0) 712193323Sed#define ADDSELECTOR(frm, sel) do { \ 713193323Sed memcpy(frm, sel, 4); \ 714245431Sdim frm += 4; \ 715193323Sed} while (0) 716245431Sdim static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE }; 717245431Sdim static const u_int8_t cipher_suite[][4] = { 718245431Sdim { WPA_OUI_BYTES, WPA_CSE_WEP40 }, /* NB: 40-bit */ 719193323Sed { WPA_OUI_BYTES, WPA_CSE_TKIP }, 720193323Sed { 0x00, 0x00, 0x00, 0x00 }, /* XXX WRAP */ 721193323Sed { WPA_OUI_BYTES, WPA_CSE_CCMP }, 722193323Sed { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */ 723193323Sed { WPA_OUI_BYTES, WPA_CSE_NULL }, 724245431Sdim }; 725245431Sdim static const u_int8_t wep104_suite[4] = 726245431Sdim { WPA_OUI_BYTES, WPA_CSE_WEP104 }; 727193323Sed static const u_int8_t key_mgt_unspec[4] = 728193323Sed { WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC }; 729193323Sed static const u_int8_t key_mgt_psk[4] = 730193323Sed { WPA_OUI_BYTES, WPA_ASE_8021X_PSK }; 731193323Sed const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn; 732193323Sed u_int8_t *frm = ie; 733193323Sed u_int8_t *selcnt; 734193323Sed 735193323Sed *frm++ = IEEE80211_ELEMID_VENDOR; 736193323Sed *frm++ = 0; /* length filled in below */ 737193323Sed memcpy(frm, oui, sizeof(oui)); /* WPA OUI */ 738193323Sed frm += sizeof(oui); 739193323Sed ADDSHORT(frm, WPA_VERSION); 740193323Sed 741193323Sed /* XXX filter out CKIP */ 742200581Srdivacky 743200581Srdivacky /* multicast cipher */ 744193323Sed if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP && 745193323Sed rsn->rsn_mcastkeylen >= 13) 746193323Sed ADDSELECTOR(frm, wep104_suite); 747193323Sed else 748193323Sed ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]); 749193323Sed 750193323Sed /* unicast cipher list */ 751193323Sed selcnt = frm; 752193323Sed ADDSHORT(frm, 0); /* selector count */ 753193323Sed if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) { 754193323Sed selcnt[0]++; 755193323Sed ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]); 756193323Sed } 757193323Sed if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) { 758193323Sed selcnt[0]++; 759193323Sed ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]); 760200581Srdivacky } 761200581Srdivacky 762193323Sed /* authenticator selector list */ 763193323Sed selcnt = frm; 764193323Sed ADDSHORT(frm, 0); /* selector count */ 765193323Sed if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) { 766193323Sed selcnt[0]++; 767193323Sed ADDSELECTOR(frm, key_mgt_unspec); 768193323Sed } 769193323Sed if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) { 770193323Sed selcnt[0]++; 771193323Sed ADDSELECTOR(frm, key_mgt_psk); 772193323Sed } 773193323Sed 774193323Sed /* optional capabilities */ 775193323Sed if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH) 776193323Sed ADDSHORT(frm, rsn->rsn_caps); 777193323Sed 778200581Srdivacky /* calculate element length */ 779200581Srdivacky ie[1] = frm - ie - 2; 780193323Sed KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa), 781193323Sed ("WPA IE too big, %u > %zu", 782218893Sdim ie[1]+2, sizeof(struct ieee80211_ie_wpa))); 783218893Sdim return frm; 784218893Sdim#undef ADDSHORT 785193323Sed#undef ADDSELECTOR 786193323Sed#undef WPA_OUI_BYTES 787193323Sed} 788193323Sed 789198090Srdivackystatic u_int8_t * 790193323Sedieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie) 791193323Sed{ 792193323Sed#define RSN_OUI_BYTES 0x00, 0x0f, 0xac 793193323Sed#define ADDSHORT(frm, v) do { \ 794193323Sed frm[0] = (v) & 0xff; \ 795193323Sed frm[1] = (v) >> 8; \ 796193323Sed frm += 2; \ 797193323Sed} while (0) 798193323Sed#define ADDSELECTOR(frm, sel) do { \ 799193323Sed memcpy(frm, sel, 4); \ 800193323Sed frm += 4; \ 801193323Sed} while (0) 802193323Sed static const u_int8_t cipher_suite[][4] = { 803193323Sed { RSN_OUI_BYTES, RSN_CSE_WEP40 }, /* NB: 40-bit */ 804193323Sed { RSN_OUI_BYTES, RSN_CSE_TKIP }, 805193323Sed { RSN_OUI_BYTES, RSN_CSE_WRAP }, 806193323Sed { RSN_OUI_BYTES, RSN_CSE_CCMP }, 807198090Srdivacky { 0x00, 0x00, 0x00, 0x00 }, /* XXX CKIP */ 808226890Sdim { RSN_OUI_BYTES, RSN_CSE_NULL }, 809193323Sed }; 810193323Sed static const u_int8_t wep104_suite[4] = 811193323Sed { RSN_OUI_BYTES, RSN_CSE_WEP104 }; 812193323Sed static const u_int8_t key_mgt_unspec[4] = 813226890Sdim { RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC }; 814226890Sdim static const u_int8_t key_mgt_psk[4] = 815226890Sdim { RSN_OUI_BYTES, RSN_ASE_8021X_PSK }; 816193323Sed const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn; 817193323Sed u_int8_t *frm = ie; 818193323Sed u_int8_t *selcnt; 819193323Sed 820193323Sed *frm++ = IEEE80211_ELEMID_RSN; 821218893Sdim *frm++ = 0; /* length filled in below */ 822218893Sdim ADDSHORT(frm, RSN_VERSION); 823218893Sdim 824218893Sdim /* XXX filter out CKIP */ 825218893Sdim 826218893Sdim /* multicast cipher */ 827218893Sdim if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP && 828218893Sdim rsn->rsn_mcastkeylen >= 13) 829218893Sdim ADDSELECTOR(frm, wep104_suite); 830218893Sdim else 831218893Sdim ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]); 832218893Sdim 833218893Sdim /* unicast cipher list */ 834218893Sdim selcnt = frm; 835218893Sdim ADDSHORT(frm, 0); /* selector count */ 836218893Sdim if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) { 837218893Sdim selcnt[0]++; 838218893Sdim ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]); 839218893Sdim } 840218893Sdim if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) { 841218893Sdim selcnt[0]++; 842218893Sdim ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]); 843218893Sdim } 844218893Sdim 845218893Sdim /* authenticator selector list */ 846218893Sdim selcnt = frm; 847218893Sdim ADDSHORT(frm, 0); /* selector count */ 848218893Sdim if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) { 849218893Sdim selcnt[0]++; 850218893Sdim ADDSELECTOR(frm, key_mgt_unspec); 851218893Sdim } 852218893Sdim if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) { 853218893Sdim selcnt[0]++; 854218893Sdim ADDSELECTOR(frm, key_mgt_psk); 855218893Sdim } 856218893Sdim 857218893Sdim /* optional capabilities */ 858218893Sdim ADDSHORT(frm, rsn->rsn_caps); 859198090Srdivacky /* XXX PMKID */ 860193323Sed 861193323Sed /* calculate element length */ 862198090Srdivacky ie[1] = frm - ie - 2; 863226890Sdim KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa), 864198090Srdivacky ("RSN IE too big, %u > %zu", 865198090Srdivacky ie[1]+2, sizeof(struct ieee80211_ie_wpa))); 866193323Sed return frm; 867263509Sdim#undef ADDSELECTOR 868193323Sed#undef ADDSHORT 869193323Sed#undef RSN_OUI_BYTES 870193323Sed} 871235633Sdim 872263509Sdim/* 873263509Sdim * Add a WPA/RSN element to a frame. 874263509Sdim */ 875263509Sdimstatic u_int8_t * 876193323Sedieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic) 877205218Srdivacky{ 878193323Sed 879193323Sed KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!")); 880263509Sdim if (ic->ic_flags & IEEE80211_F_WPA2) 881263509Sdim frm = ieee80211_setup_rsn_ie(ic, frm); 882193323Sed if (ic->ic_flags & IEEE80211_F_WPA1) 883252723Sdim frm = ieee80211_setup_wpa_ie(ic, frm); 884263509Sdim return frm; 885198090Srdivacky} 886193323Sed 887193323Sed#define WME_OUI_BYTES 0x00, 0x50, 0xf2 888193323Sed/* 889245431Sdim * Add a WME information element to a frame. 890193323Sed */ 891205218Srdivackystatic u_int8_t * 892193323Sedieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme) 893193323Sed{ 894193323Sed static const struct ieee80211_wme_info info = { 895193323Sed .wme_id = IEEE80211_ELEMID_VENDOR, 896193323Sed .wme_len = sizeof(struct ieee80211_wme_info) - 2, 897193323Sed .wme_oui = { WME_OUI_BYTES }, 898193323Sed .wme_type = WME_OUI_TYPE, 899193323Sed .wme_subtype = WME_INFO_OUI_SUBTYPE, 900193323Sed .wme_version = WME_VERSION, 901193323Sed .wme_info = 0, 902193323Sed }; 903193323Sed memcpy(frm, &info, sizeof(info)); 904193323Sed return frm + sizeof(info); 905193323Sed} 906193323Sed 907193323Sed/* 908193323Sed * Add a WME parameters element to a frame. 909193323Sed */ 910193323Sedstatic u_int8_t * 911193323Sedieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme) 912193323Sed{ 913193323Sed#define SM(_v, _f) (((_v) << _f##_S) & _f) 914193323Sed#define ADDSHORT(frm, v) do { \ 915193323Sed frm[0] = (v) & 0xff; \ 916193323Sed frm[1] = (v) >> 8; \ 917206083Srdivacky frm += 2; \ 918193323Sed} while (0) 919193323Sed /* NB: this works 'cuz a param has an info at the front */ 920263509Sdim static const struct ieee80211_wme_info param = { 921226890Sdim .wme_id = IEEE80211_ELEMID_VENDOR, 922226890Sdim .wme_len = sizeof(struct ieee80211_wme_param) - 2, 923226890Sdim .wme_oui = { WME_OUI_BYTES }, 924226890Sdim .wme_type = WME_OUI_TYPE, 925226890Sdim .wme_subtype = WME_PARAM_OUI_SUBTYPE, 926263509Sdim .wme_version = WME_VERSION, 927198090Srdivacky }; 928198090Srdivacky int i; 929198090Srdivacky 930198090Srdivacky memcpy(frm, ¶m, sizeof(param)); 931198090Srdivacky frm += __offsetof(struct ieee80211_wme_info, wme_info); 932263509Sdim *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */ 933198090Srdivacky *frm++ = 0; /* reserved field */ 934198090Srdivacky for (i = 0; i < WME_NUM_AC; i++) { 935198090Srdivacky const struct wmeParams *ac = 936198090Srdivacky &wme->wme_bssChanParams.cap_wmeParams[i]; 937198090Srdivacky *frm++ = SM(i, WME_PARAM_ACI) 938263509Sdim | SM(ac->wmep_acm, WME_PARAM_ACM) 939200581Srdivacky | SM(ac->wmep_aifsn, WME_PARAM_AIFSN) 940200581Srdivacky ; 941200581Srdivacky *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX) 942193323Sed | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN) 943200581Srdivacky ; 944200581Srdivacky ADDSHORT(frm, ac->wmep_txopLimit); 945193323Sed } 946193323Sed return frm; 947193323Sed#undef SM 948193323Sed#undef ADDSHORT 949193323Sed} 950193323Sed#undef WME_OUI_BYTES 951193323Sed 952263509Sdim/* 953204642Srdivacky * Send a probe request frame with the specified ssid 954193323Sed * and any optional information element data. 955193323Sed */ 956193323Sedint 957193323Sedieee80211_send_probereq(struct ieee80211_node *ni, 958193323Sed const u_int8_t sa[IEEE80211_ADDR_LEN], 959198090Srdivacky const u_int8_t da[IEEE80211_ADDR_LEN], 960204642Srdivacky const u_int8_t bssid[IEEE80211_ADDR_LEN], 961193323Sed const u_int8_t *ssid, size_t ssidlen, 962193323Sed const void *optie, size_t optielen) 963193323Sed{ 964193323Sed struct ieee80211com *ic = ni->ni_ic; 965193323Sed enum ieee80211_phymode mode; 966193323Sed struct ieee80211_frame *wh; 967198090Srdivacky struct mbuf *m; 968198090Srdivacky u_int8_t *frm; 969193323Sed 970193323Sed /* 971198090Srdivacky * Hold a reference on the node so it doesn't go away until after 972193323Sed * the xmit is complete all the way in the driver. On error we 973193323Sed * will remove our reference. 974204642Srdivacky */ 975226890Sdim IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 976226890Sdim "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 977263509Sdim __func__, __LINE__, 978263509Sdim ni, ether_sprintf(ni->ni_macaddr), 979226890Sdim ieee80211_node_refcnt(ni)+1); 980226890Sdim ieee80211_ref_node(ni); 981226890Sdim 982226890Sdim /* 983263509Sdim * prreq frame format 984226890Sdim * [tlv] ssid 985263509Sdim * [tlv] supported rates 986226890Sdim * [tlv] extended supported rates 987226890Sdim * [tlv] user-specified ie's 988226890Sdim */ 989263509Sdim m = ieee80211_getmgtframe(&frm, 990263509Sdim 2 + IEEE80211_NWID_LEN 991263509Sdim + 2 + IEEE80211_RATE_SIZE 992263509Sdim + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 993263509Sdim + (optie != NULL ? optielen : 0) 994263509Sdim ); 995263509Sdim if (m == NULL) { 996263509Sdim ic->ic_stats.is_tx_nobuf++; 997263509Sdim ieee80211_free_node(ni); 998263509Sdim return ENOMEM; 999263509Sdim } 1000263509Sdim 1001263509Sdim frm = ieee80211_add_ssid(frm, ssid, ssidlen); 1002263509Sdim mode = ieee80211_chan2mode(ic, ic->ic_curchan); 1003226890Sdim frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]); 1004263509Sdim frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]); 1005263509Sdim 1006263509Sdim if (optie != NULL) { 1007263509Sdim memcpy(frm, optie, optielen); 1008263509Sdim frm += optielen; 1009263509Sdim } 1010263509Sdim m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1011263509Sdim 1012263509Sdim M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1013263509Sdim if (m == NULL) 1014263509Sdim return ENOMEM; 1015263509Sdim KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 1016263509Sdim m->m_pkthdr.rcvif = (void *)ni; 1017263509Sdim 1018263509Sdim wh = mtod(m, struct ieee80211_frame *); 1019263509Sdim ieee80211_send_setup(ic, ni, wh, 1020263509Sdim IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ, 1021263509Sdim sa, da, bssid); 1022263509Sdim /* XXX power management? */ 1023263509Sdim 1024263509Sdim IEEE80211_NODE_STAT(ni, tx_probereq); 1025263509Sdim IEEE80211_NODE_STAT(ni, tx_mgmt); 1026263509Sdim 1027263509Sdim IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 1028263509Sdim "[%s] send probe req on channel %u\n", 1029263509Sdim ether_sprintf(wh->i_addr1), 1030263509Sdim ieee80211_chan2ieee(ic, ic->ic_curchan)); 1031263509Sdim 1032263509Sdim IF_ENQUEUE(&ic->ic_mgtq, m); 1033263509Sdim if_start(ic->ic_ifp); 1034263509Sdim return 0; 1035263509Sdim} 1036263509Sdim 1037263509Sdim/* 1038226890Sdim * Send a management frame. The node is for the destination (or ic_bss 1039193323Sed * when in station mode). Nodes other than ic_bss have their reference 1040193323Sed * count bumped to reflect our use for an indeterminant time. 1041193323Sed */ 1042193323Sedint 1043226890Sdimieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni, 1044193323Sed int type, int arg) 1045193323Sed{ 1046201360Srdivacky#define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0) 1047193323Sed struct mbuf *m; 1048193323Sed u_int8_t *frm; 1049201360Srdivacky u_int16_t capinfo; 1050193323Sed int has_challenge, is_shared_key, ret, timer, status; 1051226890Sdim 1052193323Sed KASSERT(ni != NULL, ("null node")); 1053193323Sed 1054193323Sed /* 1055193323Sed * Hold a reference on the node so it doesn't go away until after 1056193323Sed * the xmit is complete all the way in the driver. On error we 1057193323Sed * will remove our reference. 1058193323Sed */ 1059193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, 1060263509Sdim "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1061193323Sed __func__, __LINE__, 1062193323Sed ni, ether_sprintf(ni->ni_macaddr), 1063193323Sed ieee80211_node_refcnt(ni)+1); 1064193323Sed ieee80211_ref_node(ni); 1065193323Sed 1066263509Sdim timer = 0; 1067193323Sed switch (type) { 1068193323Sed case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1069193323Sed /* 1070198090Srdivacky * probe response frame format 1071198090Srdivacky * [8] time stamp 1072193323Sed * [2] beacon interval 1073193323Sed * [2] cabability information 1074198090Srdivacky * [tlv] ssid 1075193323Sed * [tlv] supported rates 1076193323Sed * [tlv] parameter set (FH/DS) 1077204642Srdivacky * [tlv] parameter set (IBSS) 1078193323Sed * [tlv] extended rate phy (ERP) 1079193323Sed * [tlv] extended supported rates 1080193323Sed * [tlv] WPA 1081193323Sed * [tlv] WME (optional) 1082193323Sed */ 1083193323Sed m = ieee80211_getmgtframe(&frm, 1084193323Sed 8 1085193323Sed + sizeof(u_int16_t) 1086193323Sed + sizeof(u_int16_t) 1087193323Sed + 2 + IEEE80211_NWID_LEN 1088201360Srdivacky + 2 + IEEE80211_RATE_SIZE 1089193323Sed + 7 /* max(7,3) */ 1090193323Sed + 6 1091201360Srdivacky + 3 1092193323Sed + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1093205407Srdivacky /* XXX !WPA1+WPA2 fits w/o a cluster */ 1094193323Sed + (ic->ic_flags & IEEE80211_F_WPA ? 1095193323Sed 2*sizeof(struct ieee80211_ie_wpa) : 0) 1096193323Sed + sizeof(struct ieee80211_wme_param) 1097193323Sed ); 1098193323Sed if (m == NULL) 1099193323Sed senderr(ENOMEM, is_tx_nobuf); 1100193323Sed 1101193323Sed memset(frm, 0, 8); /* timestamp should be filled later */ 1102263509Sdim frm += 8; 1103263509Sdim *(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval); 1104193323Sed frm += 2; 1105193323Sed if (ic->ic_opmode == IEEE80211_M_IBSS) 1106193323Sed capinfo = IEEE80211_CAPINFO_IBSS; 1107193323Sed else 1108198090Srdivacky capinfo = IEEE80211_CAPINFO_ESS; 1109204642Srdivacky if (ic->ic_flags & IEEE80211_F_PRIVACY) 1110193323Sed capinfo |= IEEE80211_CAPINFO_PRIVACY; 1111193323Sed if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1112208599Srdivacky IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 1113193323Sed capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1114252723Sdim if (ic->ic_flags & IEEE80211_F_SHSLOT) 1115252723Sdim capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1116208599Srdivacky *(u_int16_t *)frm = htole16(capinfo); 1117208599Srdivacky frm += 2; 1118252723Sdim 1119208599Srdivacky frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid, 1120208599Srdivacky ic->ic_bss->ni_esslen); 1121235633Sdim frm = ieee80211_add_rates(frm, &ni->ni_rates); 1122235633Sdim 1123193323Sed if (ic->ic_phytype == IEEE80211_T_FH) { 1124193323Sed *frm++ = IEEE80211_ELEMID_FHPARMS; 1125263509Sdim *frm++ = 5; 1126198090Srdivacky *frm++ = ni->ni_fhdwell & 0x00ff; 1127195098Sed *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff; 1128195098Sed *frm++ = IEEE80211_FH_CHANSET( 1129195098Sed ieee80211_chan2ieee(ic, ic->ic_curchan)); 1130195098Sed *frm++ = IEEE80211_FH_CHANPAT( 1131263509Sdim ieee80211_chan2ieee(ic, ic->ic_curchan)); 1132198090Srdivacky *frm++ = ni->ni_fhindex; 1133193323Sed } else { 1134263509Sdim *frm++ = IEEE80211_ELEMID_DSPARMS; 1135193323Sed *frm++ = 1; 1136245431Sdim *frm++ = ieee80211_chan2ieee(ic, ic->ic_curchan); 1137193323Sed } 1138193323Sed 1139193323Sed if (ic->ic_opmode == IEEE80211_M_IBSS) { 1140193323Sed *frm++ = IEEE80211_ELEMID_IBSSPARMS; 1141193323Sed *frm++ = 2; 1142193323Sed *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 1143193323Sed } 1144193323Sed if (ic->ic_flags & IEEE80211_F_WPA) 1145195098Sed frm = ieee80211_add_wpa(frm, ic); 1146193323Sed if (ic->ic_curmode == IEEE80211_MODE_11G) 1147193323Sed frm = ieee80211_add_erp(frm, ic); 1148193323Sed frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1149193323Sed if (ic->ic_flags & IEEE80211_F_WME) 1150193323Sed frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1151193323Sed m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1152193323Sed break; 1153193323Sed 1154193323Sed case IEEE80211_FC0_SUBTYPE_AUTH: 1155195098Sed status = arg >> 16; 1156245431Sdim arg &= 0xffff; 1157193323Sed has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE || 1158201360Srdivacky arg == IEEE80211_AUTH_SHARED_RESPONSE) && 1159193323Sed ni->ni_challenge != NULL); 1160201360Srdivacky 1161263509Sdim /* 1162263509Sdim * Deduce whether we're doing open authentication or 1163205407Srdivacky * shared key authentication. We do the latter if 1164193323Sed * we're in the middle of a shared key authentication 1165193323Sed * handshake or if we're initiating an authentication 1166193323Sed * request and configured to use shared key. 1167193323Sed */ 1168193323Sed is_shared_key = has_challenge || 1169198090Srdivacky arg >= IEEE80211_AUTH_SHARED_RESPONSE || 1170193323Sed (arg == IEEE80211_AUTH_SHARED_REQUEST && 1171193323Sed ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED); 1172193323Sed 1173193323Sed m = ieee80211_getmgtframe(&frm, 1174193323Sed 3 * sizeof(u_int16_t) 1175201360Srdivacky + (has_challenge && status == IEEE80211_STATUS_SUCCESS ? 1176193323Sed sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0) 1177201360Srdivacky ); 1178205407Srdivacky if (m == NULL) 1179193323Sed senderr(ENOMEM, is_tx_nobuf); 1180193323Sed 1181193323Sed ((u_int16_t *)frm)[0] = 1182193323Sed (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED) 1183193323Sed : htole16(IEEE80211_AUTH_ALG_OPEN); 1184198090Srdivacky ((u_int16_t *)frm)[1] = htole16(arg); /* sequence number */ 1185195098Sed ((u_int16_t *)frm)[2] = htole16(status);/* status */ 1186195098Sed 1187195098Sed if (has_challenge && status == IEEE80211_STATUS_SUCCESS) { 1188193323Sed ((u_int16_t *)frm)[3] = 1189193323Sed htole16((IEEE80211_CHALLENGE_LEN << 8) | 1190193323Sed IEEE80211_ELEMID_CHALLENGE); 1191193323Sed memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge, 1192195098Sed IEEE80211_CHALLENGE_LEN); 1193193323Sed m->m_pkthdr.len = m->m_len = 1194201360Srdivacky 4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN; 1195193323Sed if (arg == IEEE80211_AUTH_SHARED_RESPONSE) { 1196201360Srdivacky IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 1197205407Srdivacky "[%s] request encrypt frame (%s)\n", 1198205407Srdivacky ether_sprintf(ni->ni_macaddr), __func__); 1199193323Sed m->m_flags |= M_LINK0; /* WEP-encrypt, please */ 1200193323Sed } 1201193323Sed } else 1202193323Sed m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t); 1203193323Sed 1204207618Srdivacky /* XXX not right for shared key */ 1205193323Sed if (status == IEEE80211_STATUS_SUCCESS) 1206198090Srdivacky IEEE80211_NODE_STAT(ni, tx_auth); 1207195098Sed else 1208195098Sed IEEE80211_NODE_STAT(ni, tx_auth_fail); 1209195098Sed 1210193323Sed if (ic->ic_opmode == IEEE80211_M_STA) 1211263509Sdim timer = IEEE80211_TRANS_WAIT; 1212263509Sdim break; 1213193323Sed 1214193323Sed case IEEE80211_FC0_SUBTYPE_DEAUTH: 1215193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 1216193323Sed "[%s] send station deauthenticate (reason %d)\n", 1217193323Sed ether_sprintf(ni->ni_macaddr), arg); 1218193323Sed m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t)); 1219195098Sed if (m == NULL) 1220193323Sed senderr(ENOMEM, is_tx_nobuf); 1221201360Srdivacky *(u_int16_t *)frm = htole16(arg); /* reason */ 1222193323Sed m->m_pkthdr.len = m->m_len = sizeof(u_int16_t); 1223201360Srdivacky 1224205407Srdivacky IEEE80211_NODE_STAT(ni, tx_deauth); 1225205407Srdivacky IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg); 1226193323Sed 1227193323Sed ieee80211_node_unauthorize(ni); /* port closed */ 1228193323Sed break; 1229193323Sed 1230193323Sed case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1231193323Sed case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1232198090Srdivacky /* 1233193323Sed * asreq frame format 1234195098Sed * [2] capability information 1235195098Sed * [2] listen interval 1236195098Sed * [6*] current AP address (reassoc only) 1237195098Sed * [tlv] ssid 1238193323Sed * [tlv] supported rates 1239263509Sdim * [tlv] extended supported rates 1240263509Sdim * [tlv] WME 1241193323Sed * [tlv] user-specified ie's 1242193323Sed */ 1243193323Sed m = ieee80211_getmgtframe(&frm, 1244193323Sed sizeof(u_int16_t) 1245193323Sed + sizeof(u_int16_t) 1246226890Sdim + IEEE80211_ADDR_LEN 1247195098Sed + 2 + IEEE80211_NWID_LEN 1248193323Sed + 2 + IEEE80211_RATE_SIZE 1249201360Srdivacky + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1250193323Sed + sizeof(struct ieee80211_wme_info) 1251201360Srdivacky + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0) 1252205407Srdivacky ); 1253205407Srdivacky if (m == NULL) 1254193323Sed senderr(ENOMEM, is_tx_nobuf); 1255193323Sed 1256193323Sed capinfo = 0; 1257193323Sed if (ic->ic_opmode == IEEE80211_M_IBSS) 1258193323Sed capinfo |= IEEE80211_CAPINFO_IBSS; 1259245431Sdim else /* IEEE80211_M_STA */ 1260245431Sdim capinfo |= IEEE80211_CAPINFO_ESS; 1261245431Sdim if (ic->ic_flags & IEEE80211_F_PRIVACY) 1262245431Sdim capinfo |= IEEE80211_CAPINFO_PRIVACY; 1263245431Sdim /* 1264245431Sdim * NB: Some 11a AP's reject the request when 1265245431Sdim * short premable is set. 1266245431Sdim */ 1267245431Sdim if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1268245431Sdim IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 1269245431Sdim capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1270245431Sdim if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) && 1271245431Sdim (ic->ic_caps & IEEE80211_C_SHSLOT)) 1272245431Sdim capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1273245431Sdim *(u_int16_t *)frm = htole16(capinfo); 1274245431Sdim frm += 2; 1275245431Sdim 1276245431Sdim *(u_int16_t *)frm = htole16(ic->ic_lintval); 1277193323Sed frm += 2; 1278193323Sed 1279193323Sed if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 1280193323Sed IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid); 1281193323Sed frm += IEEE80211_ADDR_LEN; 1282201360Srdivacky } 1283193323Sed 1284201360Srdivacky frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 1285205407Srdivacky frm = ieee80211_add_rates(frm, &ni->ni_rates); 1286193323Sed frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1287193323Sed if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 1288193323Sed frm = ieee80211_add_wme_info(frm, &ic->ic_wme); 1289193323Sed if (ic->ic_opt_ie != NULL) { 1290193323Sed memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len); 1291198090Srdivacky frm += ic->ic_opt_ie_len; 1292198090Srdivacky } 1293198090Srdivacky m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1294198090Srdivacky 1295193323Sed timer = IEEE80211_TRANS_WAIT; 1296193323Sed break; 1297198090Srdivacky 1298193323Sed case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 1299193323Sed case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 1300205407Srdivacky /* 1301193323Sed * asreq frame format 1302193323Sed * [2] capability information 1303193323Sed * [2] status 1304193323Sed * [2] association ID 1305198090Srdivacky * [tlv] supported rates 1306193323Sed * [tlv] extended supported rates 1307193323Sed * [tlv] WME (if enabled and STA enabled) 1308205407Srdivacky */ 1309193323Sed m = ieee80211_getmgtframe(&frm, 1310193323Sed sizeof(u_int16_t) 1311193323Sed + sizeof(u_int16_t) 1312193323Sed + sizeof(u_int16_t) 1313198090Srdivacky + 2 + IEEE80211_RATE_SIZE 1314195098Sed + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1315195098Sed + sizeof(struct ieee80211_wme_param) 1316195098Sed ); 1317195098Sed if (m == NULL) 1318193323Sed senderr(ENOMEM, is_tx_nobuf); 1319205407Srdivacky 1320193323Sed capinfo = IEEE80211_CAPINFO_ESS; 1321193323Sed if (ic->ic_flags & IEEE80211_F_PRIVACY) 1322193323Sed capinfo |= IEEE80211_CAPINFO_PRIVACY; 1323193323Sed if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1324193323Sed IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 1325193323Sed capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1326193323Sed if (ic->ic_flags & IEEE80211_F_SHSLOT) 1327193323Sed capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1328193323Sed *(u_int16_t *)frm = htole16(capinfo); 1329205407Srdivacky frm += 2; 1330193323Sed 1331193323Sed *(u_int16_t *)frm = htole16(arg); /* status */ 1332193323Sed frm += 2; 1333201360Srdivacky 1334193323Sed if (arg == IEEE80211_STATUS_SUCCESS) { 1335193323Sed *(u_int16_t *)frm = htole16(ni->ni_associd); 1336193323Sed IEEE80211_NODE_STAT(ni, tx_assoc); 1337193323Sed } else 1338193323Sed IEEE80211_NODE_STAT(ni, tx_assoc_fail); 1339193323Sed frm += 2; 1340193323Sed 1341193323Sed frm = ieee80211_add_rates(frm, &ni->ni_rates); 1342193323Sed frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1343193323Sed if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 1344193323Sed frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1345193323Sed m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1346193323Sed break; 1347193323Sed 1348193323Sed case IEEE80211_FC0_SUBTYPE_DISASSOC: 1349193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1350193323Sed "[%s] send station disassociate (reason %d)\n", 1351263509Sdim ether_sprintf(ni->ni_macaddr), arg); 1352193323Sed m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t)); 1353263509Sdim if (m == NULL) 1354263509Sdim senderr(ENOMEM, is_tx_nobuf); 1355193323Sed *(u_int16_t *)frm = htole16(arg); /* reason */ 1356193323Sed m->m_pkthdr.len = m->m_len = sizeof(u_int16_t); 1357193323Sed 1358198090Srdivacky IEEE80211_NODE_STAT(ni, tx_disassoc); 1359193323Sed IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg); 1360198090Srdivacky break; 1361193323Sed 1362193323Sed default: 1363193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1364193323Sed "[%s] invalid mgmt frame type %u\n", 1365193323Sed ether_sprintf(ni->ni_macaddr), type); 1366193323Sed senderr(EINVAL, is_tx_unknownmgt); 1367193323Sed /* NOTREACHED */ 1368198090Srdivacky } 1369193323Sed 1370193323Sed ret = ieee80211_mgmt_output(ic, ni, m, type); 1371193323Sed if (ret == 0) { 1372193323Sed if (timer) 1373193323Sed ic->ic_mgt_timer = timer; 1374193323Sed } else { 1375198090Srdivackybad: 1376193323Sed ieee80211_free_node(ni); 1377193323Sed } 1378193323Sed return ret; 1379198090Srdivacky#undef senderr 1380193323Sed} 1381193323Sed 1382193323Sed/* 1383193323Sed * Allocate a beacon frame and fillin the appropriate bits. 1384193323Sed */ 1385193323Sedstruct mbuf * 1386193323Sedieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni, 1387193323Sed struct ieee80211_beacon_offsets *bo) 1388193323Sed{ 1389193323Sed struct ifnet *ifp = ic->ic_ifp; 1390193323Sed struct ieee80211_frame *wh; 1391193323Sed struct mbuf *m; 1392193323Sed int pktlen; 1393193323Sed u_int8_t *frm, *efrm; 1394193323Sed u_int16_t capinfo; 1395193323Sed struct ieee80211_rateset *rs; 1396193323Sed 1397193323Sed /* 1398193323Sed * beacon frame format 1399193323Sed * [8] time stamp 1400193323Sed * [2] beacon interval 1401193323Sed * [2] cabability information 1402198090Srdivacky * [tlv] ssid 1403263509Sdim * [tlv] supported rates 1404193323Sed * [3] parameter set (DS) 1405193323Sed * [tlv] parameter set (IBSS/TIM) 1406193323Sed * [tlv] extended rate phy (ERP) 1407193323Sed * [tlv] extended supported rates 1408263509Sdim * [tlv] WME parameters 1409193323Sed * [tlv] WPA/RSN parameters 1410193323Sed * XXX Vendor-specific OIDs (e.g. Atheros) 1411193323Sed * NB: we allocate the max space required for the TIM bitmap. 1412193323Sed */ 1413193323Sed rs = &ni->ni_rates; 1414193323Sed pktlen = 8 /* time stamp */ 1415193323Sed + sizeof(u_int16_t) /* beacon interval */ 1416198090Srdivacky + sizeof(u_int16_t) /* capabilities */ 1417193323Sed + 2 + ni->ni_esslen /* ssid */ 1418201360Srdivacky + 2 + IEEE80211_RATE_SIZE /* supported rates */ 1419193323Sed + 2 + 1 /* DS parameters */ 1420198090Srdivacky + 2 + 4 + ic->ic_tim_len /* DTIM/IBSSPARMS */ 1421193323Sed + 2 + 1 /* ERP */ 1422193323Sed + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1423193323Sed + (ic->ic_caps & IEEE80211_C_WME ? /* WME */ 1424193323Sed sizeof(struct ieee80211_wme_param) : 0) 1425193323Sed + (ic->ic_caps & IEEE80211_C_WPA ? /* WPA 1+2 */ 1426198090Srdivacky 2*sizeof(struct ieee80211_ie_wpa) : 0) 1427205407Srdivacky ; 1428263509Sdim m = ieee80211_getmgtframe(&frm, pktlen); 1429263509Sdim if (m == NULL) { 1430263509Sdim IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1431193323Sed "%s: cannot get buf; size %u\n", __func__, pktlen); 1432193323Sed ic->ic_stats.is_tx_nobuf++; 1433193323Sed return NULL; 1434193323Sed } 1435193323Sed 1436263509Sdim memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */ 1437193323Sed frm += 8; 1438193323Sed *(u_int16_t *)frm = htole16(ni->ni_intval); 1439193323Sed frm += 2; 1440193323Sed if (ic->ic_opmode == IEEE80211_M_IBSS) 1441193323Sed capinfo = IEEE80211_CAPINFO_IBSS; 1442193323Sed else 1443193323Sed capinfo = IEEE80211_CAPINFO_ESS; 1444193323Sed if (ic->ic_flags & IEEE80211_F_PRIVACY) 1445193323Sed capinfo |= IEEE80211_CAPINFO_PRIVACY; 1446193323Sed if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1447199481Srdivacky IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 1448199481Srdivacky capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1449193323Sed if (ic->ic_flags & IEEE80211_F_SHSLOT) 1450201360Srdivacky capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1451193323Sed bo->bo_caps = (u_int16_t *)frm; 1452201360Srdivacky *(u_int16_t *)frm = htole16(capinfo); 1453263509Sdim frm += 2; 1454263509Sdim *frm++ = IEEE80211_ELEMID_SSID; 1455263509Sdim if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) { 1456193323Sed *frm++ = ni->ni_esslen; 1457193323Sed memcpy(frm, ni->ni_essid, ni->ni_esslen); 1458193323Sed frm += ni->ni_esslen; 1459193323Sed } else 1460193323Sed *frm++ = 0; 1461198090Srdivacky frm = ieee80211_add_rates(frm, rs); 1462193323Sed if (ic->ic_curmode != IEEE80211_MODE_FH) { 1463193323Sed *frm++ = IEEE80211_ELEMID_DSPARMS; 1464193323Sed *frm++ = 1; 1465193323Sed *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 1466201360Srdivacky } 1467193323Sed bo->bo_tim = frm; 1468201360Srdivacky if (ic->ic_opmode == IEEE80211_M_IBSS) { 1469205407Srdivacky *frm++ = IEEE80211_ELEMID_IBSSPARMS; 1470193323Sed *frm++ = 2; 1471193323Sed *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 1472193323Sed bo->bo_tim_len = 0; 1473193323Sed } else { 1474193323Sed struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm; 1475235633Sdim 1476235633Sdim tie->tim_ie = IEEE80211_ELEMID_TIM; 1477235633Sdim tie->tim_len = 4; /* length */ 1478235633Sdim tie->tim_count = 0; /* DTIM count */ 1479235633Sdim tie->tim_period = ic->ic_dtim_period; /* DTIM period */ 1480235633Sdim tie->tim_bitctl = 0; /* bitmap control */ 1481235633Sdim tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */ 1482235633Sdim frm += sizeof(struct ieee80211_tim_ie); 1483235633Sdim bo->bo_tim_len = 1; 1484235633Sdim } 1485235633Sdim bo->bo_trailer = frm; 1486235633Sdim if (ic->ic_flags & IEEE80211_F_WME) { 1487235633Sdim bo->bo_wme = frm; 1488235633Sdim frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1489263509Sdim ic->ic_flags &= ~IEEE80211_F_WMEUPDATE; 1490193323Sed } 1491193323Sed if (ic->ic_flags & IEEE80211_F_WPA) 1492205218Srdivacky frm = ieee80211_add_wpa(frm, ic); 1493205218Srdivacky if (ic->ic_curmode == IEEE80211_MODE_11G) 1494193323Sed frm = ieee80211_add_erp(frm, ic); 1495201360Srdivacky efrm = ieee80211_add_xrates(frm, rs); 1496193323Sed bo->bo_trailer_len = efrm - bo->bo_trailer; 1497218893Sdim m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *); 1498263509Sdim 1499263509Sdim M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1500193323Sed KASSERT(m != NULL, ("no space for 802.11 header?")); 1501193323Sed wh = mtod(m, struct ieee80211_frame *); 1502193323Sed wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 1503193323Sed IEEE80211_FC0_SUBTYPE_BEACON; 1504193323Sed wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1505205218Srdivacky *(u_int16_t *)wh->i_dur = 0; 1506207618Srdivacky IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 1507245431Sdim IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 1508199989Srdivacky IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 1509199989Srdivacky *(u_int16_t *)wh->i_seq = 0; 1510198892Srdivacky 1511198892Srdivacky return m; 1512198892Srdivacky} 1513199989Srdivacky 1514198892Srdivacky/* 1515245431Sdim * Update the dynamic parts of a beacon frame based on the current state. 1516199989Srdivacky */ 1517198892Srdivackyint 1518201360Srdivackyieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni, 1519198892Srdivacky struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast) 1520201360Srdivacky{ 1521245431Sdim int len_changed = 0; 1522245431Sdim u_int16_t capinfo; 1523198892Srdivacky 1524198892Srdivacky IEEE80211_BEACON_LOCK(ic); 1525198892Srdivacky /* XXX faster to recalculate entirely or just changes? */ 1526198892Srdivacky if (ic->ic_opmode == IEEE80211_M_IBSS) 1527198892Srdivacky capinfo = IEEE80211_CAPINFO_IBSS; 1528193323Sed else 1529204642Srdivacky capinfo = IEEE80211_CAPINFO_ESS; 1530193323Sed if (ic->ic_flags & IEEE80211_F_PRIVACY) 1531193323Sed capinfo |= IEEE80211_CAPINFO_PRIVACY; 1532193323Sed if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1533193323Sed IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 1534193323Sed capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1535193323Sed if (ic->ic_flags & IEEE80211_F_SHSLOT) 1536193323Sed capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1537201360Srdivacky *bo->bo_caps = htole16(capinfo); 1538193323Sed 1539193323Sed if (ic->ic_flags & IEEE80211_F_WME) { 1540205407Srdivacky struct ieee80211_wme_state *wme = &ic->ic_wme; 1541193323Sed 1542193323Sed /* 1543193323Sed * Check for agressive mode change. When there is 1544193323Sed * significant high priority traffic in the BSS 1545193323Sed * throttle back BE traffic by using conservative 1546207618Srdivacky * parameters. Otherwise BE uses agressive params 1547207618Srdivacky * to optimize performance of legacy/non-QoS traffic. 1548207618Srdivacky */ 1549207618Srdivacky if (wme->wme_flags & WME_F_AGGRMODE) { 1550207618Srdivacky if (wme->wme_hipri_traffic > 1551218893Sdim wme->wme_hipri_switch_thresh) { 1552207618Srdivacky IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 1553207618Srdivacky "%s: traffic %u, disable aggressive mode\n", 1554207618Srdivacky __func__, wme->wme_hipri_traffic); 1555218893Sdim wme->wme_flags &= ~WME_F_AGGRMODE; 1556207618Srdivacky ieee80211_wme_updateparams_locked(ic); 1557207618Srdivacky wme->wme_hipri_traffic = 1558207618Srdivacky wme->wme_hipri_switch_hysteresis; 1559207618Srdivacky } else 1560207618Srdivacky wme->wme_hipri_traffic = 0; 1561207618Srdivacky } else { 1562263509Sdim if (wme->wme_hipri_traffic <= 1563263509Sdim wme->wme_hipri_switch_thresh) { 1564263509Sdim IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 1565263509Sdim "%s: traffic %u, enable aggressive mode\n", 1566263509Sdim __func__, wme->wme_hipri_traffic); 1567263509Sdim wme->wme_flags |= WME_F_AGGRMODE; 1568263509Sdim ieee80211_wme_updateparams_locked(ic); 1569263509Sdim wme->wme_hipri_traffic = 0; 1570207618Srdivacky } else 1571263509Sdim wme->wme_hipri_traffic = 1572263509Sdim wme->wme_hipri_switch_hysteresis; 1573263509Sdim } 1574263509Sdim if (ic->ic_flags & IEEE80211_F_WMEUPDATE) { 1575263509Sdim (void) ieee80211_add_wme_param(bo->bo_wme, wme); 1576263509Sdim ic->ic_flags &= ~IEEE80211_F_WMEUPDATE; 1577263509Sdim } 1578263509Sdim } 1579263509Sdim 1580263509Sdim if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/ 1581263509Sdim struct ieee80211_tim_ie *tie = 1582263509Sdim (struct ieee80211_tim_ie *) bo->bo_tim; 1583193323Sed if (ic->ic_flags & IEEE80211_F_TIMUPDATE) { 1584193323Sed u_int timlen, timoff, i; 1585221345Sdim /* 1586198090Srdivacky * ATIM/DTIM needs updating. If it fits in the 1587263509Sdim * current space allocated then just copy in the 1588193323Sed * new bits. Otherwise we need to move any trailing 1589193323Sed * data to make room. Note that we know there is 1590193323Sed * contiguous space because ieee80211_beacon_allocate 1591263509Sdim * insures there is space in the mbuf to write a 1592193323Sed * maximal-size virtual bitmap (based on ic_max_aid). 1593193323Sed */ 1594193323Sed /* 1595193323Sed * Calculate the bitmap size and offset, copy any 1596198090Srdivacky * trailer out of the way, and then copy in the 1597193323Sed * new bitmap and update the information element. 1598198090Srdivacky * Note that the tim bitmap must contain at least 1599226890Sdim * one byte and any offset must be even. 1600263509Sdim */ 1601193323Sed if (ic->ic_ps_pending != 0) { 1602263509Sdim timoff = 128; /* impossibly large */ 1603193323Sed for (i = 0; i < ic->ic_tim_len; i++) 1604199481Srdivacky if (ic->ic_tim_bitmap[i]) { 1605263509Sdim timoff = i &~ 1; 1606193323Sed break; 1607193323Sed } 1608193323Sed KASSERT(timoff != 128, ("tim bitmap empty!")); 1609193323Sed for (i = ic->ic_tim_len-1; i >= timoff; i--) 1610198090Srdivacky if (ic->ic_tim_bitmap[i]) 1611193323Sed break; 1612193323Sed timlen = 1 + (i - timoff); 1613226890Sdim } else { 1614226890Sdim timoff = 0; 1615263509Sdim timlen = 1; 1616263509Sdim } 1617193323Sed if (timlen != bo->bo_tim_len) { 1618193323Sed /* copy up/down trailer */ 1619193323Sed ovbcopy(bo->bo_trailer, tie->tim_bitmap+timlen, 1620193323Sed bo->bo_trailer_len); 1621199481Srdivacky bo->bo_trailer = tie->tim_bitmap+timlen; 1622263509Sdim bo->bo_wme = bo->bo_trailer; 1623193323Sed bo->bo_tim_len = timlen; 1624193323Sed 1625198090Srdivacky /* update information element */ 1626263509Sdim tie->tim_len = 3 + timlen; 1627193323Sed tie->tim_bitctl = timoff; 1628193323Sed len_changed = 1; 1629193323Sed } 1630193323Sed memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff, 1631193323Sed bo->bo_tim_len); 1632193323Sed 1633263509Sdim ic->ic_flags &= ~IEEE80211_F_TIMUPDATE; 1634263509Sdim 1635263509Sdim IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 1636263509Sdim "%s: TIM updated, pending %u, off %u, len %u\n", 1637263509Sdim __func__, ic->ic_ps_pending, timoff, timlen); 1638263509Sdim } 1639193323Sed /* count down DTIM period */ 1640193323Sed if (tie->tim_count == 0) 1641193323Sed tie->tim_count = tie->tim_period - 1; 1642193323Sed else 1643193323Sed tie->tim_count--; 1644193323Sed /* update state for buffered multicast frames on DTIM */ 1645193323Sed if (mcast && (tie->tim_count == 1 || tie->tim_period == 1)) 1646193323Sed tie->tim_bitctl |= 1; 1647193323Sed else 1648193323Sed tie->tim_bitctl &= ~1; 1649193323Sed } 1650193323Sed IEEE80211_BEACON_UNLOCK(ic); 1651193323Sed 1652193323Sed return len_changed; 1653193323Sed} 1654193323Sed 1655193323Sed/* 1656193323Sed * Save an outbound packet for a node in power-save sleep state. 1657193323Sed * The new packet is placed on the node's saved queue, and the TIM 1658193323Sed * is changed, if necessary. 1659193323Sed */ 1660198090Srdivackyvoid 1661193323Sedieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni, 1662193323Sed struct mbuf *m) 1663193323Sed{ 1664193323Sed int qlen, age; 1665193323Sed 1666193323Sed IEEE80211_NODE_SAVEQ_LOCK(ni); 1667193323Sed if (_IF_QFULL(&ni->ni_savedq)) { 1668193323Sed _IF_DROP(&ni->ni_savedq); 1669193323Sed IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1670193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1671193323Sed "[%s] pwr save q overflow, drops %d (size %d)\n", 1672193323Sed ether_sprintf(ni->ni_macaddr), 1673193323Sed ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE); 1674193323Sed#ifdef IEEE80211_DEBUG 1675193323Sed if (ieee80211_msg_dumppkts(ic)) 1676193323Sed ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1); 1677193323Sed#endif 1678193323Sed m_freem(m); 1679193323Sed return; 1680193323Sed } 1681193323Sed /* 1682193323Sed * Tag the frame with it's expiry time and insert 1683193323Sed * it in the queue. The aging interval is 4 times 1684193323Sed * the listen interval specified by the station. 1685193323Sed * Frames that sit around too long are reclaimed 1686193323Sed * using this information. 1687193323Sed */ 1688193323Sed /* XXX handle overflow? */ 1689193323Sed age = ((ni->ni_intval * ic->ic_bintval) << 2) / 1024; /* TU -> secs */ 1690193323Sed _IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age); 1691193323Sed IEEE80211_NODE_SAVEQ_UNLOCK(ni); 1692193323Sed 1693193323Sed IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 1694193323Sed "[%s] save frame with age %d, %u now queued\n", 1695193323Sed ether_sprintf(ni->ni_macaddr), age, qlen); 1696193323Sed 1697193323Sed if (qlen == 1) 1698193323Sed ic->ic_set_tim(ni, 1); 1699193323Sed} 1700193323Sed