ieee80211_output.c revision 186904
1169695Skan/*- 2169695Skan * Copyright (c) 2001 Atsushi Onoe 3169695Skan * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 4169695Skan * All rights reserved. 5169695Skan * 6169695Skan * Redistribution and use in source and binary forms, with or without 7169695Skan * modification, are permitted provided that the following conditions 8169695Skan * are met: 9169695Skan * 1. Redistributions of source code must retain the above copyright 10169695Skan * notice, this list of conditions and the following disclaimer. 11169695Skan * 2. Redistributions in binary form must reproduce the above copyright 12169695Skan * notice, this list of conditions and the following disclaimer in the 13169695Skan * documentation and/or other materials provided with the distribution. 14169695Skan * 15169695Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16169695Skan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17169695Skan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18169695Skan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19169695Skan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20169695Skan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21169695Skan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22169695Skan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23169695Skan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24169695Skan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25169695Skan */ 26169695Skan 27169695Skan#include <sys/cdefs.h> 28169695Skan__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 186904 2009-01-08 17:12:47Z sam $"); 29169695Skan 30169695Skan#include "opt_inet.h" 31169695Skan#include "opt_wlan.h" 32169695Skan 33169695Skan#include <sys/param.h> 34169695Skan#include <sys/systm.h> 35169695Skan#include <sys/mbuf.h> 36169695Skan#include <sys/kernel.h> 37169695Skan#include <sys/endian.h> 38169695Skan 39169695Skan#include <sys/socket.h> 40169695Skan 41169695Skan#include <net/bpf.h> 42169695Skan#include <net/ethernet.h> 43169695Skan#include <net/if.h> 44169695Skan#include <net/if_llc.h> 45169695Skan#include <net/if_media.h> 46169695Skan#include <net/if_vlan_var.h> 47169695Skan 48169695Skan#include <net80211/ieee80211_var.h> 49169695Skan#include <net80211/ieee80211_regdomain.h> 50169695Skan#ifdef IEEE80211_SUPPORT_TDMA 51169695Skan#include <net80211/ieee80211_tdma.h> 52169695Skan#endif 53169695Skan#include <net80211/ieee80211_wds.h> 54169695Skan 55169695Skan#ifdef INET 56169695Skan#include <netinet/in.h> 57169695Skan#include <netinet/if_ether.h> 58169695Skan#include <netinet/in_systm.h> 59169695Skan#include <netinet/ip.h> 60169695Skan#endif 61169695Skan 62169695Skan#define ETHER_HEADER_COPY(dst, src) \ 63169695Skan memcpy(dst, src, sizeof(struct ether_header)) 64169695Skan 65169695Skanstatic struct mbuf *ieee80211_encap_fastframe(struct ieee80211vap *, 66169695Skan struct mbuf *m1, const struct ether_header *eh1, 67169695Skan struct mbuf *m2, const struct ether_header *eh2); 68169695Skanstatic int ieee80211_fragment(struct ieee80211vap *, struct mbuf *, 69169695Skan u_int hdrsize, u_int ciphdrsize, u_int mtu); 70169695Skanstatic void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int); 71169695Skan 72169695Skan#ifdef IEEE80211_DEBUG 73169695Skan/* 74169695Skan * Decide if an outbound management frame should be 75169695Skan * printed when debugging is enabled. This filters some 76169695Skan * of the less interesting frames that come frequently 77169695Skan * (e.g. beacons). 78169695Skan */ 79169695Skanstatic __inline int 80169695Skandoprint(struct ieee80211vap *vap, int subtype) 81169695Skan{ 82169695Skan switch (subtype) { 83169695Skan case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 84169695Skan return (vap->iv_opmode == IEEE80211_M_IBSS); 85169695Skan } 86169695Skan return 1; 87169695Skan} 88169695Skan#endif 89169695Skan 90169695Skan/* 91169695Skan * Start method for vap's. All packets from the stack come 92169695Skan * through here. We handle common processing of the packets 93169695Skan * before dispatching them to the underlying device. 94169695Skan */ 95169695Skanvoid 96169695Skanieee80211_start(struct ifnet *ifp) 97169695Skan{ 98169695Skan#define IS_DWDS(vap) \ 99169695Skan (vap->iv_opmode == IEEE80211_M_WDS && \ 100169695Skan (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) 101169695Skan struct ieee80211vap *vap = ifp->if_softc; 102169695Skan struct ieee80211com *ic = vap->iv_ic; 103169695Skan struct ifnet *parent = ic->ic_ifp; 104169695Skan struct ieee80211_node *ni; 105169695Skan struct mbuf *m; 106169695Skan struct ether_header *eh; 107169695Skan int error; 108169695Skan 109169695Skan /* NB: parent must be up and running */ 110169695Skan if (!IFNET_IS_UP_RUNNING(parent)) { 111169695Skan IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 112169695Skan "%s: ignore queue, parent %s not up+running\n", 113169695Skan __func__, parent->if_xname); 114169695Skan /* XXX stat */ 115169695Skan return; 116169695Skan } 117169695Skan if (vap->iv_state == IEEE80211_S_SLEEP) { 118169695Skan /* 119169695Skan * In power save, wakeup device for transmit. 120169695Skan */ 121169695Skan ieee80211_new_state(vap, IEEE80211_S_RUN, 0); 122169695Skan return; 123169695Skan } 124169695Skan /* 125169695Skan * No data frames go out unless we're running. 126169695Skan * Note in particular this covers CAC and CSA 127169695Skan * states (though maybe we should check muting 128169695Skan * for CSA). 129169695Skan */ 130169695Skan if (vap->iv_state != IEEE80211_S_RUN) { 131169695Skan IEEE80211_LOCK(ic); 132169695Skan /* re-check under the com lock to avoid races */ 133169695Skan if (vap->iv_state != IEEE80211_S_RUN) { 134169695Skan IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 135169695Skan "%s: ignore queue, in %s state\n", 136169695Skan __func__, ieee80211_state_name[vap->iv_state]); 137169695Skan vap->iv_stats.is_tx_badstate++; 138169695Skan ifp->if_drv_flags |= IFF_DRV_OACTIVE; 139169695Skan IEEE80211_UNLOCK(ic); 140169695Skan return; 141169695Skan } 142169695Skan IEEE80211_UNLOCK(ic); 143169695Skan } 144169695Skan for (;;) { 145169695Skan IFQ_DEQUEUE(&ifp->if_snd, m); 146169695Skan if (m == NULL) 147169695Skan break; 148169695Skan /* 149169695Skan * Sanitize mbuf flags for net80211 use. We cannot 150169695Skan * clear M_PWR_SAV because this may be set for frames 151169695Skan * that are re-submitted from the power save queue. 152169695Skan * 153169695Skan * NB: This must be done before ieee80211_classify as 154169695Skan * it marks EAPOL in frames with M_EAPOL. 155169695Skan */ 156169695Skan m->m_flags &= ~(M_80211_TX - M_PWR_SAV); 157169695Skan /* 158169695Skan * Cancel any background scan. 159169695Skan */ 160169695Skan if (ic->ic_flags & IEEE80211_F_SCAN) 161169695Skan ieee80211_cancel_anyscan(vap); 162169695Skan /* 163169695Skan * Find the node for the destination so we can do 164169695Skan * things like power save and fast frames aggregation. 165169695Skan * 166169695Skan * NB: past this point various code assumes the first 167169695Skan * mbuf has the 802.3 header present (and contiguous). 168169695Skan */ 169169695Skan ni = NULL; 170169695Skan if (m->m_len < sizeof(struct ether_header) && 171169695Skan (m = m_pullup(m, sizeof(struct ether_header))) == NULL) { 172169695Skan IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 173169695Skan "discard frame, %s\n", "m_pullup failed"); 174169695Skan vap->iv_stats.is_tx_nobuf++; /* XXX */ 175169695Skan ifp->if_oerrors++; 176169695Skan continue; 177169695Skan } 178169695Skan eh = mtod(m, struct ether_header *); 179169695Skan if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 180169695Skan if (IS_DWDS(vap)) { 181169695Skan /* 182169695Skan * Only unicast frames from the above go out 183169695Skan * DWDS vaps; multicast frames are handled by 184169695Skan * dispatching the frame as it comes through 185169695Skan * the AP vap (see below). 186169695Skan */ 187169695Skan IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS, 188169695Skan eh->ether_dhost, "mcast", "%s", "on DWDS"); 189169695Skan vap->iv_stats.is_dwds_mcast++; 190169695Skan m_freem(m); 191169695Skan continue; 192169695Skan } 193169695Skan if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 194169695Skan /* 195169695Skan * Spam DWDS vap's w/ multicast traffic. 196169695Skan */ 197169695Skan /* XXX only if dwds in use? */ 198169695Skan ieee80211_dwds_mcast(vap, m); 199169695Skan } 200169695Skan } 201169695Skan ni = ieee80211_find_txnode(vap, eh->ether_dhost); 202169695Skan if (ni == NULL) { 203169695Skan /* NB: ieee80211_find_txnode does stat+msg */ 204169695Skan ifp->if_oerrors++; 205169695Skan m_freem(m); 206169695Skan continue; 207169695Skan } 208169695Skan /* XXX AUTH'd */ 209169695Skan if (ni->ni_associd == 0 && 210169695Skan (ni->ni_flags & IEEE80211_NODE_ASSOCID)) { 211169695Skan IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT, 212169695Skan eh->ether_dhost, NULL, 213169695Skan "sta not associated (type 0x%04x)", 214169695Skan htons(eh->ether_type)); 215169695Skan vap->iv_stats.is_tx_notassoc++; 216169695Skan ifp->if_oerrors++; 217169695Skan m_freem(m); 218169695Skan ieee80211_free_node(ni); 219169695Skan continue; 220169695Skan } 221169695Skan if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 222169695Skan (m->m_flags & M_PWR_SAV) == 0) { 223169695Skan /* 224169695Skan * Station in power save mode; pass the frame 225169695Skan * to the 802.11 layer and continue. We'll get 226169695Skan * the frame back when the time is right. 227169695Skan * XXX lose WDS vap linkage? 228169695Skan */ 229169695Skan (void) ieee80211_pwrsave(ni, m); 230169695Skan ieee80211_free_node(ni); 231169695Skan continue; 232169695Skan } 233169695Skan /* calculate priority so drivers can find the tx queue */ 234169695Skan if (ieee80211_classify(ni, m)) { 235169695Skan IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT, 236169695Skan eh->ether_dhost, NULL, 237169695Skan "%s", "classification failure"); 238169695Skan vap->iv_stats.is_tx_classify++; 239169695Skan ifp->if_oerrors++; 240169695Skan m_freem(m); 241169695Skan ieee80211_free_node(ni); 242169695Skan continue; 243169695Skan } 244169695Skan 245169695Skan BPF_MTAP(ifp, m); /* 802.11 tx path */ 246169695Skan 247169695Skan /* 248169695Skan * XXX When ni is associated with a WDS link then 249169695Skan * the vap will be the WDS vap but ni_vap will point 250169695Skan * to the ap vap the station associated to. Once 251169695Skan * we handoff the packet to the driver the callback 252169695Skan * to ieee80211_encap won't be able to tell if the 253169695Skan * packet should be encapsulated for WDS or not (e.g. 254169695Skan * multicast frames will not be handled correctly). 255169695Skan * We hack this by marking the mbuf so ieee80211_encap 256169695Skan * can do the right thing. 257169695Skan */ 258169695Skan if (vap->iv_opmode == IEEE80211_M_WDS) 259169695Skan m->m_flags |= M_WDS; 260169695Skan else 261169695Skan m->m_flags &= ~M_WDS; 262169695Skan 263169695Skan /* 264169695Skan * Stash the node pointer and hand the frame off to 265169695Skan * the underlying device. Note that we do this after 266169695Skan * any call to ieee80211_dwds_mcast because that code 267169695Skan * uses any existing value for rcvif. 268169695Skan */ 269169695Skan m->m_pkthdr.rcvif = (void *)ni; 270169695Skan 271169695Skan /* XXX defer if_start calls? */ 272169695Skan error = parent->if_transmit(parent, m); 273169695Skan if (error != 0) { 274169695Skan /* NB: IFQ_HANDOFF reclaims mbuf */ 275169695Skan ieee80211_free_node(ni); 276169695Skan } else { 277169695Skan ifp->if_opackets++; 278169695Skan } 279169695Skan ic->ic_lastdata = ticks; 280169695Skan } 281169695Skan#undef IS_DWDS 282169695Skan} 283169695Skan 284169695Skan/* 285169695Skan * 802.11 output routine. This is (currently) used only to 286169695Skan * connect bpf write calls to the 802.11 layer for injecting 287169695Skan * raw 802.11 frames. Note we locate the ieee80211com from 288169695Skan * the ifnet using a spare field setup at attach time. This 289169695Skan * will go away when the virtual ap support comes in. 290169695Skan */ 291169695Skanint 292169695Skanieee80211_output(struct ifnet *ifp, struct mbuf *m, 293169695Skan struct sockaddr *dst, struct rtentry *rt0) 294169695Skan{ 295169695Skan#define senderr(e) do { error = (e); goto bad;} while (0) 296169695Skan struct ieee80211_node *ni = NULL; 297169695Skan struct ieee80211vap *vap; 298169695Skan struct ieee80211_frame *wh; 299169695Skan int error; 300169695Skan 301169695Skan if (ifp->if_drv_flags & IFF_DRV_OACTIVE) { 302169695Skan /* 303169695Skan * Short-circuit requests if the vap is marked OACTIVE 304169695Skan * as this is used when tearing down state to indicate 305169695Skan * the vap may be gone. This can also happen because a 306169695Skan * packet came down through ieee80211_start before the 307169695Skan * vap entered RUN state in which case it's also ok to 308169695Skan * just drop the frame. This should not be necessary 309169695Skan * but callers of if_output don't check OACTIVE. 310169695Skan */ 311169695Skan senderr(ENETDOWN); 312169695Skan } 313169695Skan vap = ifp->if_softc; 314169695Skan /* 315169695Skan * Hand to the 802.3 code if not tagged as 316169695Skan * a raw 802.11 frame. 317169695Skan */ 318169695Skan if (dst->sa_family != AF_IEEE80211) 319169695Skan return vap->iv_output(ifp, m, dst, rt0); 320169695Skan#ifdef MAC 321169695Skan error = mac_check_ifnet_transmit(ifp, m); 322169695Skan if (error) 323169695Skan senderr(error); 324169695Skan#endif 325169695Skan if (ifp->if_flags & IFF_MONITOR) 326169695Skan senderr(ENETDOWN); 327169695Skan if (!IFNET_IS_UP_RUNNING(ifp)) 328169695Skan senderr(ENETDOWN); 329169695Skan if (vap->iv_state == IEEE80211_S_CAC) { 330169695Skan IEEE80211_DPRINTF(vap, 331169695Skan IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 332169695Skan "block %s frame in CAC state\n", "raw data"); 333169695Skan vap->iv_stats.is_tx_badstate++; 334169695Skan senderr(EIO); /* XXX */ 335169695Skan } 336169695Skan /* XXX bypass bridge, pfil, carp, etc. */ 337169695Skan 338169695Skan if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack)) 339169695Skan senderr(EIO); /* XXX */ 340169695Skan wh = mtod(m, struct ieee80211_frame *); 341169695Skan if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 342169695Skan IEEE80211_FC0_VERSION_0) 343169695Skan senderr(EIO); /* XXX */ 344169695Skan 345169695Skan /* locate destination node */ 346169695Skan switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 347169695Skan case IEEE80211_FC1_DIR_NODS: 348169695Skan case IEEE80211_FC1_DIR_FROMDS: 349169695Skan ni = ieee80211_find_txnode(vap, wh->i_addr1); 350169695Skan break; 351169695Skan case IEEE80211_FC1_DIR_TODS: 352169695Skan case IEEE80211_FC1_DIR_DSTODS: 353169695Skan if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) 354169695Skan senderr(EIO); /* XXX */ 355169695Skan ni = ieee80211_find_txnode(vap, wh->i_addr3); 356169695Skan break; 357169695Skan default: 358169695Skan senderr(EIO); /* XXX */ 359169695Skan } 360169695Skan if (ni == NULL) { 361169695Skan /* 362169695Skan * Permit packets w/ bpf params through regardless 363169695Skan * (see below about sa_len). 364169695Skan */ 365169695Skan if (dst->sa_len == 0) 366169695Skan senderr(EHOSTUNREACH); 367169695Skan ni = ieee80211_ref_node(vap->iv_bss); 368169695Skan } 369169695Skan 370169695Skan /* 371169695Skan * Sanitize mbuf for net80211 flags leaked from above. 372169695Skan * 373169695Skan * NB: This must be done before ieee80211_classify as 374169695Skan * it marks EAPOL in frames with M_EAPOL. 375169695Skan */ 376169695Skan m->m_flags &= ~M_80211_TX; 377169695Skan 378169695Skan /* calculate priority so drivers can find the tx queue */ 379169695Skan /* XXX assumes an 802.3 frame */ 380169695Skan if (ieee80211_classify(ni, m)) 381169695Skan senderr(EIO); /* XXX */ 382169695Skan 383169695Skan BPF_MTAP(ifp, m); 384169695Skan 385169695Skan /* 386169695Skan * NB: DLT_IEEE802_11_RADIO identifies the parameters are 387169695Skan * present by setting the sa_len field of the sockaddr (yes, 388169695Skan * this is a hack). 389169695Skan * NB: we assume sa_data is suitably aligned to cast. 390169695Skan */ 391169695Skan return vap->iv_ic->ic_raw_xmit(ni, m, 392169695Skan (const struct ieee80211_bpf_params *)(dst->sa_len ? 393169695Skan dst->sa_data : NULL)); 394169695Skanbad: 395169695Skan if (m != NULL) 396169695Skan m_freem(m); 397169695Skan if (ni != NULL) 398169695Skan ieee80211_free_node(ni); 399169695Skan return error; 400169695Skan#undef senderr 401169695Skan} 402169695Skan 403169695Skan/* 404169695Skan * Set the direction field and address fields of an outgoing 405169695Skan * frame. Note this should be called early on in constructing 406169695Skan * a frame as it sets i_fc[1]; other bits can then be or'd in. 407169695Skan */ 408169695Skanstatic void 409169695Skanieee80211_send_setup( 410169695Skan struct ieee80211_node *ni, 411169695Skan struct ieee80211_frame *wh, 412169695Skan int type, int tid, 413169695Skan const uint8_t sa[IEEE80211_ADDR_LEN], 414169695Skan const uint8_t da[IEEE80211_ADDR_LEN], 415169695Skan const uint8_t bssid[IEEE80211_ADDR_LEN]) 416169695Skan{ 417169695Skan#define WH4(wh) ((struct ieee80211_frame_addr4 *)wh) 418169695Skan 419169695Skan wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type; 420169695Skan if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) { 421169695Skan struct ieee80211vap *vap = ni->ni_vap; 422169695Skan 423169695Skan switch (vap->iv_opmode) { 424169695Skan case IEEE80211_M_STA: 425169695Skan wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 426169695Skan IEEE80211_ADDR_COPY(wh->i_addr1, bssid); 427169695Skan IEEE80211_ADDR_COPY(wh->i_addr2, sa); 428169695Skan IEEE80211_ADDR_COPY(wh->i_addr3, da); 429169695Skan break; 430169695Skan case IEEE80211_M_IBSS: 431169695Skan case IEEE80211_M_AHDEMO: 432169695Skan wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 433169695Skan IEEE80211_ADDR_COPY(wh->i_addr1, da); 434169695Skan IEEE80211_ADDR_COPY(wh->i_addr2, sa); 435169695Skan IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 436169695Skan break; 437169695Skan case IEEE80211_M_HOSTAP: 438169695Skan wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 439169695Skan IEEE80211_ADDR_COPY(wh->i_addr1, da); 440169695Skan IEEE80211_ADDR_COPY(wh->i_addr2, bssid); 441169695Skan IEEE80211_ADDR_COPY(wh->i_addr3, sa); 442169695Skan break; 443169695Skan case IEEE80211_M_WDS: 444169695Skan wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 445169695Skan IEEE80211_ADDR_COPY(wh->i_addr1, da); 446169695Skan IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 447169695Skan IEEE80211_ADDR_COPY(wh->i_addr3, da); 448169695Skan IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa); 449169695Skan break; 450169695Skan case IEEE80211_M_MONITOR: /* NB: to quiet compiler */ 451169695Skan break; 452169695Skan } 453169695Skan } else { 454169695Skan wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 455169695Skan IEEE80211_ADDR_COPY(wh->i_addr1, da); 456169695Skan IEEE80211_ADDR_COPY(wh->i_addr2, sa); 457169695Skan IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 458169695Skan } 459169695Skan *(uint16_t *)&wh->i_dur[0] = 0; 460169695Skan *(uint16_t *)&wh->i_seq[0] = 461169695Skan htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT); 462169695Skan ni->ni_txseqs[tid]++; 463169695Skan#undef WH4 464169695Skan} 465169695Skan 466169695Skan/* 467169695Skan * Send a management frame to the specified node. The node pointer 468169695Skan * must have a reference as the pointer will be passed to the driver 469169695Skan * and potentially held for a long time. If the frame is successfully 470169695Skan * dispatched to the driver, then it is responsible for freeing the 471169695Skan * reference (and potentially free'ing up any associated storage); 472169695Skan * otherwise deal with reclaiming any reference (on error). 473169695Skan */ 474169695Skanint 475169695Skanieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type, 476169695Skan struct ieee80211_bpf_params *params) 477169695Skan{ 478169695Skan struct ieee80211vap *vap = ni->ni_vap; 479169695Skan struct ieee80211com *ic = ni->ni_ic; 480169695Skan struct ieee80211_frame *wh; 481169695Skan 482169695Skan KASSERT(ni != NULL, ("null node")); 483169695Skan 484169695Skan if (vap->iv_state == IEEE80211_S_CAC) { 485169695Skan IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 486169695Skan ni, "block %s frame in CAC state", 487169695Skan ieee80211_mgt_subtype_name[ 488169695Skan (type & IEEE80211_FC0_SUBTYPE_MASK) >> 489169695Skan IEEE80211_FC0_SUBTYPE_SHIFT]); 490169695Skan vap->iv_stats.is_tx_badstate++; 491169695Skan ieee80211_free_node(ni); 492169695Skan m_freem(m); 493169695Skan return EIO; /* XXX */ 494169695Skan } 495169695Skan 496169695Skan M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 497169695Skan if (m == NULL) { 498169695Skan ieee80211_free_node(ni); 499169695Skan return ENOMEM; 500169695Skan } 501169695Skan 502169695Skan wh = mtod(m, struct ieee80211_frame *); 503169695Skan ieee80211_send_setup(ni, wh, 504169695Skan IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID, 505169695Skan vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 506169695Skan if (params->ibp_flags & IEEE80211_BPF_CRYPTO) { 507169695Skan IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1, 508169695Skan "encrypting frame (%s)", __func__); 509169695Skan wh->i_fc[1] |= IEEE80211_FC1_WEP; 510169695Skan } 511169695Skan m->m_flags |= M_ENCAP; /* mark encapsulated */ 512169695Skan 513169695Skan KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?")); 514169695Skan M_WME_SETAC(m, params->ibp_pri); 515169695Skan 516169695Skan#ifdef IEEE80211_DEBUG 517169695Skan /* avoid printing too many frames */ 518169695Skan if ((ieee80211_msg_debug(vap) && doprint(vap, type)) || 519169695Skan ieee80211_msg_dumppkts(vap)) { 520169695Skan printf("[%s] send %s on channel %u\n", 521169695Skan ether_sprintf(wh->i_addr1), 522169695Skan ieee80211_mgt_subtype_name[ 523169695Skan (type & IEEE80211_FC0_SUBTYPE_MASK) >> 524169695Skan IEEE80211_FC0_SUBTYPE_SHIFT], 525169695Skan ieee80211_chan2ieee(ic, ic->ic_curchan)); 526169695Skan } 527169695Skan#endif 528169695Skan IEEE80211_NODE_STAT(ni, tx_mgmt); 529169695Skan 530169695Skan return ic->ic_raw_xmit(ni, m, params); 531169695Skan} 532169695Skan 533169695Skan/* 534169695Skan * Send a null data frame to the specified node. If the station 535169695Skan * is setup for QoS then a QoS Null Data frame is constructed. 536169695Skan * If this is a WDS station then a 4-address frame is constructed. 537169695Skan * 538169695Skan * NB: the caller is assumed to have setup a node reference 539169695Skan * for use; this is necessary to deal with a race condition 540169695Skan * when probing for inactive stations. Like ieee80211_mgmt_output 541169695Skan * we must cleanup any node reference on error; however we 542169695Skan * can safely just unref it as we know it will never be the 543169695Skan * last reference to the node. 544169695Skan */ 545169695Skanint 546169695Skanieee80211_send_nulldata(struct ieee80211_node *ni) 547169695Skan{ 548169695Skan struct ieee80211vap *vap = ni->ni_vap; 549169695Skan struct ieee80211com *ic = ni->ni_ic; 550169695Skan struct mbuf *m; 551169695Skan struct ieee80211_frame *wh; 552169695Skan int hdrlen; 553169695Skan uint8_t *frm; 554169695Skan 555169695Skan if (vap->iv_state == IEEE80211_S_CAC) { 556169695Skan IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 557169695Skan ni, "block %s frame in CAC state", "null data"); 558169695Skan ieee80211_unref_node(&ni); 559169695Skan vap->iv_stats.is_tx_badstate++; 560169695Skan return EIO; /* XXX */ 561169695Skan } 562169695Skan 563169695Skan if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) 564169695Skan hdrlen = sizeof(struct ieee80211_qosframe); 565169695Skan else 566 hdrlen = sizeof(struct ieee80211_frame); 567 /* NB: only WDS vap's get 4-address frames */ 568 if (vap->iv_opmode == IEEE80211_M_WDS) 569 hdrlen += IEEE80211_ADDR_LEN; 570 if (ic->ic_flags & IEEE80211_F_DATAPAD) 571 hdrlen = roundup(hdrlen, sizeof(uint32_t)); 572 573 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0); 574 if (m == NULL) { 575 /* XXX debug msg */ 576 ieee80211_unref_node(&ni); 577 vap->iv_stats.is_tx_nobuf++; 578 return ENOMEM; 579 } 580 KASSERT(M_LEADINGSPACE(m) >= hdrlen, 581 ("leading space %zd", M_LEADINGSPACE(m))); 582 M_PREPEND(m, hdrlen, M_DONTWAIT); 583 if (m == NULL) { 584 /* NB: cannot happen */ 585 ieee80211_free_node(ni); 586 return ENOMEM; 587 } 588 589 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */ 590 if (ni->ni_flags & IEEE80211_NODE_QOS) { 591 const int tid = WME_AC_TO_TID(WME_AC_BE); 592 uint8_t *qos; 593 594 ieee80211_send_setup(ni, wh, 595 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL, 596 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 597 598 if (vap->iv_opmode == IEEE80211_M_WDS) 599 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 600 else 601 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 602 qos[0] = tid & IEEE80211_QOS_TID; 603 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy) 604 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 605 qos[1] = 0; 606 } else { 607 ieee80211_send_setup(ni, wh, 608 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA, 609 IEEE80211_NONQOS_TID, 610 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 611 } 612 if (vap->iv_opmode != IEEE80211_M_WDS) { 613 /* NB: power management bit is never sent by an AP */ 614 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 615 vap->iv_opmode != IEEE80211_M_HOSTAP) 616 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT; 617 } 618 m->m_len = m->m_pkthdr.len = hdrlen; 619 m->m_flags |= M_ENCAP; /* mark encapsulated */ 620 621 M_WME_SETAC(m, WME_AC_BE); 622 623 IEEE80211_NODE_STAT(ni, tx_data); 624 625 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni, 626 "send %snull data frame on channel %u, pwr mgt %s", 627 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "", 628 ieee80211_chan2ieee(ic, ic->ic_curchan), 629 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis"); 630 631 return ic->ic_raw_xmit(ni, m, NULL); 632} 633 634/* 635 * Assign priority to a frame based on any vlan tag assigned 636 * to the station and/or any Diffserv setting in an IP header. 637 * Finally, if an ACM policy is setup (in station mode) it's 638 * applied. 639 */ 640int 641ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m) 642{ 643 const struct ether_header *eh = mtod(m, struct ether_header *); 644 int v_wme_ac, d_wme_ac, ac; 645 646 /* 647 * Always promote PAE/EAPOL frames to high priority. 648 */ 649 if (eh->ether_type == htons(ETHERTYPE_PAE)) { 650 /* NB: mark so others don't need to check header */ 651 m->m_flags |= M_EAPOL; 652 ac = WME_AC_VO; 653 goto done; 654 } 655 /* 656 * Non-qos traffic goes to BE. 657 */ 658 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) { 659 ac = WME_AC_BE; 660 goto done; 661 } 662 663 /* 664 * If node has a vlan tag then all traffic 665 * to it must have a matching tag. 666 */ 667 v_wme_ac = 0; 668 if (ni->ni_vlan != 0) { 669 if ((m->m_flags & M_VLANTAG) == 0) { 670 IEEE80211_NODE_STAT(ni, tx_novlantag); 671 return 1; 672 } 673 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 674 EVL_VLANOFTAG(ni->ni_vlan)) { 675 IEEE80211_NODE_STAT(ni, tx_vlanmismatch); 676 return 1; 677 } 678 /* map vlan priority to AC */ 679 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan)); 680 } 681 682#ifdef INET 683 if (eh->ether_type == htons(ETHERTYPE_IP)) { 684 uint8_t tos; 685 /* 686 * IP frame, map the DSCP bits from the TOS field. 687 */ 688 /* XXX m_copydata may be too slow for fast path */ 689 /* NB: ip header may not be in first mbuf */ 690 m_copydata(m, sizeof(struct ether_header) + 691 offsetof(struct ip, ip_tos), sizeof(tos), &tos); 692 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */ 693 d_wme_ac = TID_TO_WME_AC(tos); 694 } else { 695#endif /* INET */ 696 d_wme_ac = WME_AC_BE; 697#ifdef INET 698 } 699#endif 700 /* 701 * Use highest priority AC. 702 */ 703 if (v_wme_ac > d_wme_ac) 704 ac = v_wme_ac; 705 else 706 ac = d_wme_ac; 707 708 /* 709 * Apply ACM policy. 710 */ 711 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) { 712 static const int acmap[4] = { 713 WME_AC_BK, /* WME_AC_BE */ 714 WME_AC_BK, /* WME_AC_BK */ 715 WME_AC_BE, /* WME_AC_VI */ 716 WME_AC_VI, /* WME_AC_VO */ 717 }; 718 struct ieee80211com *ic = ni->ni_ic; 719 720 while (ac != WME_AC_BK && 721 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm) 722 ac = acmap[ac]; 723 } 724done: 725 M_WME_SETAC(m, ac); 726 return 0; 727} 728 729/* 730 * Insure there is sufficient contiguous space to encapsulate the 731 * 802.11 data frame. If room isn't already there, arrange for it. 732 * Drivers and cipher modules assume we have done the necessary work 733 * and fail rudely if they don't find the space they need. 734 */ 735static struct mbuf * 736ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize, 737 struct ieee80211_key *key, struct mbuf *m) 738{ 739#define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc)) 740 int needed_space = vap->iv_ic->ic_headroom + hdrsize; 741 742 if (key != NULL) { 743 /* XXX belongs in crypto code? */ 744 needed_space += key->wk_cipher->ic_header; 745 /* XXX frags */ 746 /* 747 * When crypto is being done in the host we must insure 748 * the data are writable for the cipher routines; clone 749 * a writable mbuf chain. 750 * XXX handle SWMIC specially 751 */ 752 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) { 753 m = m_unshare(m, M_NOWAIT); 754 if (m == NULL) { 755 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 756 "%s: cannot get writable mbuf\n", __func__); 757 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */ 758 return NULL; 759 } 760 } 761 } 762 /* 763 * We know we are called just before stripping an Ethernet 764 * header and prepending an LLC header. This means we know 765 * there will be 766 * sizeof(struct ether_header) - sizeof(struct llc) 767 * bytes recovered to which we need additional space for the 768 * 802.11 header and any crypto header. 769 */ 770 /* XXX check trailing space and copy instead? */ 771 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) { 772 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type); 773 if (n == NULL) { 774 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 775 "%s: cannot expand storage\n", __func__); 776 vap->iv_stats.is_tx_nobuf++; 777 m_freem(m); 778 return NULL; 779 } 780 KASSERT(needed_space <= MHLEN, 781 ("not enough room, need %u got %zu\n", needed_space, MHLEN)); 782 /* 783 * Setup new mbuf to have leading space to prepend the 784 * 802.11 header and any crypto header bits that are 785 * required (the latter are added when the driver calls 786 * back to ieee80211_crypto_encap to do crypto encapsulation). 787 */ 788 /* NB: must be first 'cuz it clobbers m_data */ 789 m_move_pkthdr(n, m); 790 n->m_len = 0; /* NB: m_gethdr does not set */ 791 n->m_data += needed_space; 792 /* 793 * Pull up Ethernet header to create the expected layout. 794 * We could use m_pullup but that's overkill (i.e. we don't 795 * need the actual data) and it cannot fail so do it inline 796 * for speed. 797 */ 798 /* NB: struct ether_header is known to be contiguous */ 799 n->m_len += sizeof(struct ether_header); 800 m->m_len -= sizeof(struct ether_header); 801 m->m_data += sizeof(struct ether_header); 802 /* 803 * Replace the head of the chain. 804 */ 805 n->m_next = m; 806 m = n; 807 } 808 return m; 809#undef TO_BE_RECLAIMED 810} 811 812/* 813 * Return the transmit key to use in sending a unicast frame. 814 * If a unicast key is set we use that. When no unicast key is set 815 * we fall back to the default transmit key. 816 */ 817static __inline struct ieee80211_key * 818ieee80211_crypto_getucastkey(struct ieee80211vap *vap, 819 struct ieee80211_node *ni) 820{ 821 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 822 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE || 823 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey])) 824 return NULL; 825 return &vap->iv_nw_keys[vap->iv_def_txkey]; 826 } else { 827 return &ni->ni_ucastkey; 828 } 829} 830 831/* 832 * Return the transmit key to use in sending a multicast frame. 833 * Multicast traffic always uses the group key which is installed as 834 * the default tx key. 835 */ 836static __inline struct ieee80211_key * 837ieee80211_crypto_getmcastkey(struct ieee80211vap *vap, 838 struct ieee80211_node *ni) 839{ 840 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE || 841 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey])) 842 return NULL; 843 return &vap->iv_nw_keys[vap->iv_def_txkey]; 844} 845 846/* 847 * Encapsulate an outbound data frame. The mbuf chain is updated. 848 * If an error is encountered NULL is returned. The caller is required 849 * to provide a node reference and pullup the ethernet header in the 850 * first mbuf. 851 * 852 * NB: Packet is assumed to be processed by ieee80211_classify which 853 * marked EAPOL frames w/ M_EAPOL. 854 */ 855struct mbuf * 856ieee80211_encap(struct ieee80211_node *ni, struct mbuf *m) 857{ 858#define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh)) 859 struct ieee80211vap *vap = ni->ni_vap; 860 struct ieee80211com *ic = ni->ni_ic; 861 struct ether_header eh; 862 struct ieee80211_frame *wh; 863 struct ieee80211_key *key; 864 struct llc *llc; 865 int hdrsize, hdrspace, datalen, addqos, txfrag, isff, is4addr; 866 867 /* 868 * Copy existing Ethernet header to a safe place. The 869 * rest of the code assumes it's ok to strip it when 870 * reorganizing state for the final encapsulation. 871 */ 872 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!")); 873 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t)); 874 875 /* 876 * Insure space for additional headers. First identify 877 * transmit key to use in calculating any buffer adjustments 878 * required. This is also used below to do privacy 879 * encapsulation work. Then calculate the 802.11 header 880 * size and any padding required by the driver. 881 * 882 * Note key may be NULL if we fall back to the default 883 * transmit key and that is not set. In that case the 884 * buffer may not be expanded as needed by the cipher 885 * routines, but they will/should discard it. 886 */ 887 if (vap->iv_flags & IEEE80211_F_PRIVACY) { 888 if (vap->iv_opmode == IEEE80211_M_STA || 889 !IEEE80211_IS_MULTICAST(eh.ether_dhost) || 890 (vap->iv_opmode == IEEE80211_M_WDS && 891 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) 892 key = ieee80211_crypto_getucastkey(vap, ni); 893 else 894 key = ieee80211_crypto_getmcastkey(vap, ni); 895 if (key == NULL && (m->m_flags & M_EAPOL) == 0) { 896 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 897 eh.ether_dhost, 898 "no default transmit key (%s) deftxkey %u", 899 __func__, vap->iv_def_txkey); 900 vap->iv_stats.is_tx_nodefkey++; 901 goto bad; 902 } 903 } else 904 key = NULL; 905 /* 906 * XXX Some ap's don't handle QoS-encapsulated EAPOL 907 * frames so suppress use. This may be an issue if other 908 * ap's require all data frames to be QoS-encapsulated 909 * once negotiated in which case we'll need to make this 910 * configurable. 911 */ 912 addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) && 913 (m->m_flags & M_EAPOL) == 0; 914 if (addqos) 915 hdrsize = sizeof(struct ieee80211_qosframe); 916 else 917 hdrsize = sizeof(struct ieee80211_frame); 918 /* 919 * 4-address frames need to be generated for: 920 * o packets sent through a WDS vap (M_WDS || IEEE80211_M_WDS) 921 * o packets relayed by a station operating with dynamic WDS 922 * (IEEE80211_M_STA+IEEE80211_F_DWDS and src address) 923 */ 924 is4addr = (m->m_flags & M_WDS) || 925 vap->iv_opmode == IEEE80211_M_WDS || /* XXX redundant? */ 926 (vap->iv_opmode == IEEE80211_M_STA && 927 (vap->iv_flags & IEEE80211_F_DWDS) && 928 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)); 929 if (is4addr) 930 hdrsize += IEEE80211_ADDR_LEN; 931 /* 932 * Honor driver DATAPAD requirement. 933 */ 934 if (ic->ic_flags & IEEE80211_F_DATAPAD) 935 hdrspace = roundup(hdrsize, sizeof(uint32_t)); 936 else 937 hdrspace = hdrsize; 938 939 if ((isff = m->m_flags & M_FF) != 0) { 940 struct mbuf *m2; 941 struct ether_header eh2; 942 943 /* 944 * Fast frame encapsulation. There must be two packets 945 * chained with m_nextpkt. We do header adjustment for 946 * each, add the tunnel encapsulation, and then concatenate 947 * the mbuf chains to form a single frame for transmission. 948 */ 949 m2 = m->m_nextpkt; 950 if (m2 == NULL) { 951 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 952 "%s: only one frame\n", __func__); 953 goto bad; 954 } 955 m->m_nextpkt = NULL; 956 /* 957 * Include fast frame headers in adjusting header 958 * layout; this allocates space according to what 959 * ieee80211_encap_fastframe will do. 960 */ 961 m = ieee80211_mbuf_adjust(vap, 962 hdrspace + sizeof(struct llc) + sizeof(uint32_t) + 2 + 963 sizeof(struct ether_header), 964 key, m); 965 if (m == NULL) { 966 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 967 m_freem(m2); 968 goto bad; 969 } 970 /* 971 * Copy second frame's Ethernet header out of line 972 * and adjust for encapsulation headers. Note that 973 * we make room for padding in case there isn't room 974 * at the end of first frame. 975 */ 976 KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!")); 977 ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t)); 978 m2 = ieee80211_mbuf_adjust(vap, 979 ATH_FF_MAX_HDR_PAD + sizeof(struct ether_header), 980 NULL, m2); 981 if (m2 == NULL) { 982 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 983 goto bad; 984 } 985 m = ieee80211_encap_fastframe(vap, m, &eh, m2, &eh2); 986 if (m == NULL) 987 goto bad; 988 } else { 989 /* 990 * Normal frame. 991 */ 992 m = ieee80211_mbuf_adjust(vap, hdrspace, key, m); 993 if (m == NULL) { 994 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 995 goto bad; 996 } 997 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */ 998 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 999 llc = mtod(m, struct llc *); 1000 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 1001 llc->llc_control = LLC_UI; 1002 llc->llc_snap.org_code[0] = 0; 1003 llc->llc_snap.org_code[1] = 0; 1004 llc->llc_snap.org_code[2] = 0; 1005 llc->llc_snap.ether_type = eh.ether_type; 1006 } 1007 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */ 1008 1009 M_PREPEND(m, hdrspace, M_DONTWAIT); 1010 if (m == NULL) { 1011 vap->iv_stats.is_tx_nobuf++; 1012 goto bad; 1013 } 1014 wh = mtod(m, struct ieee80211_frame *); 1015 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 1016 *(uint16_t *)wh->i_dur = 0; 1017 if (is4addr) { 1018 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 1019 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr); 1020 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1021 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 1022 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost); 1023 } else switch (vap->iv_opmode) { 1024 case IEEE80211_M_STA: 1025 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 1026 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 1027 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 1028 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 1029 break; 1030 case IEEE80211_M_IBSS: 1031 case IEEE80211_M_AHDEMO: 1032 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1033 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1034 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 1035 /* 1036 * NB: always use the bssid from iv_bss as the 1037 * neighbor's may be stale after an ibss merge 1038 */ 1039 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid); 1040 break; 1041 case IEEE80211_M_HOSTAP: 1042 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 1043 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1044 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 1045 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 1046 break; 1047 case IEEE80211_M_MONITOR: 1048 case IEEE80211_M_WDS: /* NB: is4addr should always be true */ 1049 goto bad; 1050 } 1051 if (m->m_flags & M_MORE_DATA) 1052 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 1053 if (addqos) { 1054 uint8_t *qos; 1055 int ac, tid; 1056 1057 if (is4addr) { 1058 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 1059 } else 1060 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 1061 ac = M_WME_GETAC(m); 1062 /* map from access class/queue to 11e header priorty value */ 1063 tid = WME_AC_TO_TID(ac); 1064 qos[0] = tid & IEEE80211_QOS_TID; 1065 /* 1066 * Check if A-MPDU tx aggregation is setup or if we 1067 * should try to enable it. The sta must be associated 1068 * with HT and A-MPDU enabled for use. When the policy 1069 * routine decides we should enable A-MPDU we issue an 1070 * ADDBA request and wait for a reply. The frame being 1071 * encapsulated will go out w/o using A-MPDU, or possibly 1072 * it might be collected by the driver and held/retransmit. 1073 * The default ic_ampdu_enable routine handles staggering 1074 * ADDBA requests in case the receiver NAK's us or we are 1075 * otherwise unable to establish a BA stream. 1076 */ 1077 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) && 1078 (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_TX)) { 1079 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac]; 1080 1081 ieee80211_txampdu_count_packet(tap); 1082 if (IEEE80211_AMPDU_RUNNING(tap)) { 1083 /* 1084 * Operational, mark frame for aggregation. 1085 * 1086 * NB: We support only immediate BA's for 1087 * AMPDU which means we set the QoS control 1088 * field to "normal ack" (0) to get "implicit 1089 * block ack" behaviour. 1090 */ 1091 m->m_flags |= M_AMPDU_MPDU; 1092 } else if (!IEEE80211_AMPDU_REQUESTED(tap) && 1093 ic->ic_ampdu_enable(ni, tap)) { 1094 /* 1095 * Not negotiated yet, request service. 1096 */ 1097 ieee80211_ampdu_request(ni, tap); 1098 } 1099 } 1100 /* XXX works even when BA marked above */ 1101 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy) 1102 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 1103 qos[1] = 0; 1104 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS; 1105 1106 if ((m->m_flags & M_AMPDU_MPDU) == 0) { 1107 /* 1108 * NB: don't assign a sequence # to potential 1109 * aggregates; we expect this happens at the 1110 * point the frame comes off any aggregation q 1111 * as otherwise we may introduce holes in the 1112 * BA sequence space and/or make window accouting 1113 * more difficult. 1114 * 1115 * XXX may want to control this with a driver 1116 * capability; this may also change when we pull 1117 * aggregation up into net80211 1118 */ 1119 *(uint16_t *)wh->i_seq = 1120 htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT); 1121 ni->ni_txseqs[tid]++; 1122 } 1123 } else { 1124 *(uint16_t *)wh->i_seq = 1125 htole16(ni->ni_txseqs[IEEE80211_NONQOS_TID] << IEEE80211_SEQ_SEQ_SHIFT); 1126 ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 1127 } 1128 /* check if xmit fragmentation is required */ 1129 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold && 1130 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 1131 (vap->iv_caps & IEEE80211_C_TXFRAG) && 1132 !isff); /* NB: don't fragment ff's */ 1133 if (key != NULL) { 1134 /* 1135 * IEEE 802.1X: send EAPOL frames always in the clear. 1136 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set. 1137 */ 1138 if ((m->m_flags & M_EAPOL) == 0 || 1139 ((vap->iv_flags & IEEE80211_F_WPA) && 1140 (vap->iv_opmode == IEEE80211_M_STA ? 1141 !IEEE80211_KEY_UNDEFINED(key) : 1142 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) { 1143 wh->i_fc[1] |= IEEE80211_FC1_WEP; 1144 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) { 1145 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, 1146 eh.ether_dhost, 1147 "%s", "enmic failed, discard frame"); 1148 vap->iv_stats.is_crypto_enmicfail++; 1149 goto bad; 1150 } 1151 } 1152 } 1153 if (txfrag && !ieee80211_fragment(vap, m, hdrsize, 1154 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold)) 1155 goto bad; 1156 1157 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1158 1159 IEEE80211_NODE_STAT(ni, tx_data); 1160 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1161 IEEE80211_NODE_STAT(ni, tx_mcast); 1162 else 1163 IEEE80211_NODE_STAT(ni, tx_ucast); 1164 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen); 1165 1166 /* XXX fragmented frames not handled */ 1167 if (bpf_peers_present(vap->iv_rawbpf)) 1168 bpf_mtap(vap->iv_rawbpf, m); 1169 1170 return m; 1171bad: 1172 if (m != NULL) 1173 m_freem(m); 1174 return NULL; 1175#undef WH4 1176} 1177 1178/* 1179 * Do Ethernet-LLC encapsulation for each payload in a fast frame 1180 * tunnel encapsulation. The frame is assumed to have an Ethernet 1181 * header at the front that must be stripped before prepending the 1182 * LLC followed by the Ethernet header passed in (with an Ethernet 1183 * type that specifies the payload size). 1184 */ 1185static struct mbuf * 1186ieee80211_encap1(struct ieee80211vap *vap, struct mbuf *m, 1187 const struct ether_header *eh) 1188{ 1189 struct llc *llc; 1190 uint16_t payload; 1191 1192 /* XXX optimize by combining m_adj+M_PREPEND */ 1193 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 1194 llc = mtod(m, struct llc *); 1195 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 1196 llc->llc_control = LLC_UI; 1197 llc->llc_snap.org_code[0] = 0; 1198 llc->llc_snap.org_code[1] = 0; 1199 llc->llc_snap.org_code[2] = 0; 1200 llc->llc_snap.ether_type = eh->ether_type; 1201 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */ 1202 1203 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT); 1204 if (m == NULL) { /* XXX cannot happen */ 1205 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 1206 "%s: no space for ether_header\n", __func__); 1207 vap->iv_stats.is_tx_nobuf++; 1208 return NULL; 1209 } 1210 ETHER_HEADER_COPY(mtod(m, void *), eh); 1211 mtod(m, struct ether_header *)->ether_type = htons(payload); 1212 return m; 1213} 1214 1215/* 1216 * Do fast frame tunnel encapsulation. The two frames and 1217 * Ethernet headers are supplied. The caller is assumed to 1218 * have arrange for space in the mbuf chains for encapsulating 1219 * headers (to avoid major mbuf fragmentation). 1220 * 1221 * The encapsulated frame is returned or NULL if there is a 1222 * problem (should not happen). 1223 */ 1224static struct mbuf * 1225ieee80211_encap_fastframe(struct ieee80211vap *vap, 1226 struct mbuf *m1, const struct ether_header *eh1, 1227 struct mbuf *m2, const struct ether_header *eh2) 1228{ 1229 struct llc *llc; 1230 struct mbuf *m; 1231 int pad; 1232 1233 /* 1234 * First, each frame gets a standard encapsulation. 1235 */ 1236 m1 = ieee80211_encap1(vap, m1, eh1); 1237 if (m1 == NULL) { 1238 m_freem(m2); 1239 return NULL; 1240 } 1241 m2 = ieee80211_encap1(vap, m2, eh2); 1242 if (m2 == NULL) { 1243 m_freem(m1); 1244 return NULL; 1245 } 1246 1247 /* 1248 * Pad leading frame to a 4-byte boundary. If there 1249 * is space at the end of the first frame, put it 1250 * there; otherwise prepend to the front of the second 1251 * frame. We know doing the second will always work 1252 * because we reserve space above. We prefer appending 1253 * as this typically has better DMA alignment properties. 1254 */ 1255 for (m = m1; m->m_next != NULL; m = m->m_next) 1256 ; 1257 pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len; 1258 if (pad) { 1259 if (M_TRAILINGSPACE(m) < pad) { /* prepend to second */ 1260 m2->m_data -= pad; 1261 m2->m_len += pad; 1262 m2->m_pkthdr.len += pad; 1263 } else { /* append to first */ 1264 m->m_len += pad; 1265 m1->m_pkthdr.len += pad; 1266 } 1267 } 1268 1269 /* 1270 * Now, stick 'em together and prepend the tunnel headers; 1271 * first the Atheros tunnel header (all zero for now) and 1272 * then a special fast frame LLC. 1273 * 1274 * XXX optimize by prepending together 1275 */ 1276 m->m_next = m2; /* NB: last mbuf from above */ 1277 m1->m_pkthdr.len += m2->m_pkthdr.len; 1278 M_PREPEND(m1, sizeof(uint32_t)+2, M_DONTWAIT); 1279 if (m1 == NULL) { /* XXX cannot happen */ 1280 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 1281 "%s: no space for tunnel header\n", __func__); 1282 vap->iv_stats.is_tx_nobuf++; 1283 return NULL; 1284 } 1285 memset(mtod(m1, void *), 0, sizeof(uint32_t)+2); 1286 1287 M_PREPEND(m1, sizeof(struct llc), M_DONTWAIT); 1288 if (m1 == NULL) { /* XXX cannot happen */ 1289 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 1290 "%s: no space for llc header\n", __func__); 1291 vap->iv_stats.is_tx_nobuf++; 1292 return NULL; 1293 } 1294 llc = mtod(m1, struct llc *); 1295 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 1296 llc->llc_control = LLC_UI; 1297 llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0; 1298 llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1; 1299 llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2; 1300 llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE); 1301 1302 vap->iv_stats.is_ff_encap++; 1303 1304 return m1; 1305} 1306 1307/* 1308 * Fragment the frame according to the specified mtu. 1309 * The size of the 802.11 header (w/o padding) is provided 1310 * so we don't need to recalculate it. We create a new 1311 * mbuf for each fragment and chain it through m_nextpkt; 1312 * we might be able to optimize this by reusing the original 1313 * packet's mbufs but that is significantly more complicated. 1314 */ 1315static int 1316ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0, 1317 u_int hdrsize, u_int ciphdrsize, u_int mtu) 1318{ 1319 struct ieee80211_frame *wh, *whf; 1320 struct mbuf *m, *prev, *next; 1321 u_int totalhdrsize, fragno, fragsize, off, remainder, payload; 1322 1323 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?")); 1324 KASSERT(m0->m_pkthdr.len > mtu, 1325 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu)); 1326 1327 wh = mtod(m0, struct ieee80211_frame *); 1328 /* NB: mark the first frag; it will be propagated below */ 1329 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG; 1330 totalhdrsize = hdrsize + ciphdrsize; 1331 fragno = 1; 1332 off = mtu - ciphdrsize; 1333 remainder = m0->m_pkthdr.len - off; 1334 prev = m0; 1335 do { 1336 fragsize = totalhdrsize + remainder; 1337 if (fragsize > mtu) 1338 fragsize = mtu; 1339 /* XXX fragsize can be >2048! */ 1340 KASSERT(fragsize < MCLBYTES, 1341 ("fragment size %u too big!", fragsize)); 1342 if (fragsize > MHLEN) 1343 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1344 else 1345 m = m_gethdr(M_DONTWAIT, MT_DATA); 1346 if (m == NULL) 1347 goto bad; 1348 /* leave room to prepend any cipher header */ 1349 m_align(m, fragsize - ciphdrsize); 1350 1351 /* 1352 * Form the header in the fragment. Note that since 1353 * we mark the first fragment with the MORE_FRAG bit 1354 * it automatically is propagated to each fragment; we 1355 * need only clear it on the last fragment (done below). 1356 */ 1357 whf = mtod(m, struct ieee80211_frame *); 1358 memcpy(whf, wh, hdrsize); 1359 *(uint16_t *)&whf->i_seq[0] |= htole16( 1360 (fragno & IEEE80211_SEQ_FRAG_MASK) << 1361 IEEE80211_SEQ_FRAG_SHIFT); 1362 fragno++; 1363 1364 payload = fragsize - totalhdrsize; 1365 /* NB: destination is known to be contiguous */ 1366 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize); 1367 m->m_len = hdrsize + payload; 1368 m->m_pkthdr.len = hdrsize + payload; 1369 m->m_flags |= M_FRAG; 1370 1371 /* chain up the fragment */ 1372 prev->m_nextpkt = m; 1373 prev = m; 1374 1375 /* deduct fragment just formed */ 1376 remainder -= payload; 1377 off += payload; 1378 } while (remainder != 0); 1379 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG; 1380 1381 /* strip first mbuf now that everything has been copied */ 1382 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize))); 1383 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 1384 1385 vap->iv_stats.is_tx_fragframes++; 1386 vap->iv_stats.is_tx_frags += fragno-1; 1387 1388 return 1; 1389bad: 1390 /* reclaim fragments but leave original frame for caller to free */ 1391 for (m = m0->m_nextpkt; m != NULL; m = next) { 1392 next = m->m_nextpkt; 1393 m->m_nextpkt = NULL; /* XXX paranoid */ 1394 m_freem(m); 1395 } 1396 m0->m_nextpkt = NULL; 1397 return 0; 1398} 1399 1400/* 1401 * Add a supported rates element id to a frame. 1402 */ 1403static uint8_t * 1404ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs) 1405{ 1406 int nrates; 1407 1408 *frm++ = IEEE80211_ELEMID_RATES; 1409 nrates = rs->rs_nrates; 1410 if (nrates > IEEE80211_RATE_SIZE) 1411 nrates = IEEE80211_RATE_SIZE; 1412 *frm++ = nrates; 1413 memcpy(frm, rs->rs_rates, nrates); 1414 return frm + nrates; 1415} 1416 1417/* 1418 * Add an extended supported rates element id to a frame. 1419 */ 1420static uint8_t * 1421ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs) 1422{ 1423 /* 1424 * Add an extended supported rates element if operating in 11g mode. 1425 */ 1426 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 1427 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 1428 *frm++ = IEEE80211_ELEMID_XRATES; 1429 *frm++ = nrates; 1430 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 1431 frm += nrates; 1432 } 1433 return frm; 1434} 1435 1436/* 1437 * Add an ssid element to a frame. 1438 */ 1439static uint8_t * 1440ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len) 1441{ 1442 *frm++ = IEEE80211_ELEMID_SSID; 1443 *frm++ = len; 1444 memcpy(frm, ssid, len); 1445 return frm + len; 1446} 1447 1448/* 1449 * Add an erp element to a frame. 1450 */ 1451static uint8_t * 1452ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic) 1453{ 1454 uint8_t erp; 1455 1456 *frm++ = IEEE80211_ELEMID_ERP; 1457 *frm++ = 1; 1458 erp = 0; 1459 if (ic->ic_nonerpsta != 0) 1460 erp |= IEEE80211_ERP_NON_ERP_PRESENT; 1461 if (ic->ic_flags & IEEE80211_F_USEPROT) 1462 erp |= IEEE80211_ERP_USE_PROTECTION; 1463 if (ic->ic_flags & IEEE80211_F_USEBARKER) 1464 erp |= IEEE80211_ERP_LONG_PREAMBLE; 1465 *frm++ = erp; 1466 return frm; 1467} 1468 1469/* 1470 * Add a CFParams element to a frame. 1471 */ 1472static uint8_t * 1473ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic) 1474{ 1475#define ADDSHORT(frm, v) do { \ 1476 frm[0] = (v) & 0xff; \ 1477 frm[1] = (v) >> 8; \ 1478 frm += 2; \ 1479} while (0) 1480 *frm++ = IEEE80211_ELEMID_CFPARMS; 1481 *frm++ = 6; 1482 *frm++ = 0; /* CFP count */ 1483 *frm++ = 2; /* CFP period */ 1484 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */ 1485 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */ 1486 return frm; 1487#undef ADDSHORT 1488} 1489 1490static __inline uint8_t * 1491add_appie(uint8_t *frm, const struct ieee80211_appie *ie) 1492{ 1493 memcpy(frm, ie->ie_data, ie->ie_len); 1494 return frm + ie->ie_len; 1495} 1496 1497static __inline uint8_t * 1498add_ie(uint8_t *frm, const uint8_t *ie) 1499{ 1500 memcpy(frm, ie, 2 + ie[1]); 1501 return frm + 2 + ie[1]; 1502} 1503 1504#define WME_OUI_BYTES 0x00, 0x50, 0xf2 1505/* 1506 * Add a WME information element to a frame. 1507 */ 1508static uint8_t * 1509ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme) 1510{ 1511 static const struct ieee80211_wme_info info = { 1512 .wme_id = IEEE80211_ELEMID_VENDOR, 1513 .wme_len = sizeof(struct ieee80211_wme_info) - 2, 1514 .wme_oui = { WME_OUI_BYTES }, 1515 .wme_type = WME_OUI_TYPE, 1516 .wme_subtype = WME_INFO_OUI_SUBTYPE, 1517 .wme_version = WME_VERSION, 1518 .wme_info = 0, 1519 }; 1520 memcpy(frm, &info, sizeof(info)); 1521 return frm + sizeof(info); 1522} 1523 1524/* 1525 * Add a WME parameters element to a frame. 1526 */ 1527static uint8_t * 1528ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme) 1529{ 1530#define SM(_v, _f) (((_v) << _f##_S) & _f) 1531#define ADDSHORT(frm, v) do { \ 1532 frm[0] = (v) & 0xff; \ 1533 frm[1] = (v) >> 8; \ 1534 frm += 2; \ 1535} while (0) 1536 /* NB: this works 'cuz a param has an info at the front */ 1537 static const struct ieee80211_wme_info param = { 1538 .wme_id = IEEE80211_ELEMID_VENDOR, 1539 .wme_len = sizeof(struct ieee80211_wme_param) - 2, 1540 .wme_oui = { WME_OUI_BYTES }, 1541 .wme_type = WME_OUI_TYPE, 1542 .wme_subtype = WME_PARAM_OUI_SUBTYPE, 1543 .wme_version = WME_VERSION, 1544 }; 1545 int i; 1546 1547 memcpy(frm, ¶m, sizeof(param)); 1548 frm += __offsetof(struct ieee80211_wme_info, wme_info); 1549 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */ 1550 *frm++ = 0; /* reserved field */ 1551 for (i = 0; i < WME_NUM_AC; i++) { 1552 const struct wmeParams *ac = 1553 &wme->wme_bssChanParams.cap_wmeParams[i]; 1554 *frm++ = SM(i, WME_PARAM_ACI) 1555 | SM(ac->wmep_acm, WME_PARAM_ACM) 1556 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN) 1557 ; 1558 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX) 1559 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN) 1560 ; 1561 ADDSHORT(frm, ac->wmep_txopLimit); 1562 } 1563 return frm; 1564#undef SM 1565#undef ADDSHORT 1566} 1567#undef WME_OUI_BYTES 1568 1569#define ATH_OUI_BYTES 0x00, 0x03, 0x7f 1570/* 1571 * Add a WME information element to a frame. 1572 */ 1573static uint8_t * 1574ieee80211_add_ath(uint8_t *frm, uint8_t caps, uint16_t defkeyix) 1575{ 1576 static const struct ieee80211_ath_ie info = { 1577 .ath_id = IEEE80211_ELEMID_VENDOR, 1578 .ath_len = sizeof(struct ieee80211_ath_ie) - 2, 1579 .ath_oui = { ATH_OUI_BYTES }, 1580 .ath_oui_type = ATH_OUI_TYPE, 1581 .ath_oui_subtype= ATH_OUI_SUBTYPE, 1582 .ath_version = ATH_OUI_VERSION, 1583 }; 1584 struct ieee80211_ath_ie *ath = (struct ieee80211_ath_ie *) frm; 1585 1586 memcpy(frm, &info, sizeof(info)); 1587 ath->ath_capability = caps; 1588 ath->ath_defkeyix[0] = (defkeyix & 0xff); 1589 ath->ath_defkeyix[1] = ((defkeyix >> 8) & 0xff); 1590 return frm + sizeof(info); 1591} 1592#undef ATH_OUI_BYTES 1593 1594/* 1595 * Add an 11h Power Constraint element to a frame. 1596 */ 1597static uint8_t * 1598ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap) 1599{ 1600 const struct ieee80211_channel *c = vap->iv_bss->ni_chan; 1601 /* XXX per-vap tx power limit? */ 1602 int8_t limit = vap->iv_ic->ic_txpowlimit / 2; 1603 1604 frm[0] = IEEE80211_ELEMID_PWRCNSTR; 1605 frm[1] = 1; 1606 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0; 1607 return frm + 3; 1608} 1609 1610/* 1611 * Add an 11h Power Capability element to a frame. 1612 */ 1613static uint8_t * 1614ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c) 1615{ 1616 frm[0] = IEEE80211_ELEMID_PWRCAP; 1617 frm[1] = 2; 1618 frm[2] = c->ic_minpower; 1619 frm[3] = c->ic_maxpower; 1620 return frm + 4; 1621} 1622 1623/* 1624 * Add an 11h Supported Channels element to a frame. 1625 */ 1626static uint8_t * 1627ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic) 1628{ 1629 static const int ielen = 26; 1630 1631 frm[0] = IEEE80211_ELEMID_SUPPCHAN; 1632 frm[1] = ielen; 1633 /* XXX not correct */ 1634 memcpy(frm+2, ic->ic_chan_avail, ielen); 1635 return frm + 2 + ielen; 1636} 1637 1638/* 1639 * Add an 11h Channel Switch Announcement element to a frame. 1640 * Note that we use the per-vap CSA count to adjust the global 1641 * counter so we can use this routine to form probe response 1642 * frames and get the current count. 1643 */ 1644static uint8_t * 1645ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap) 1646{ 1647 struct ieee80211com *ic = vap->iv_ic; 1648 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm; 1649 1650 csa->csa_ie = IEEE80211_ELEMID_CHANSWITCHANN; 1651 csa->csa_len = 3; 1652 csa->csa_mode = 1; /* XXX force quiet on channel */ 1653 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan); 1654 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count; 1655 return frm + sizeof(*csa); 1656} 1657 1658/* 1659 * Add an 11h country information element to a frame. 1660 */ 1661static uint8_t * 1662ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic) 1663{ 1664 1665 if (ic->ic_countryie == NULL || 1666 ic->ic_countryie_chan != ic->ic_bsschan) { 1667 /* 1668 * Handle lazy construction of ie. This is done on 1669 * first use and after a channel change that requires 1670 * re-calculation. 1671 */ 1672 if (ic->ic_countryie != NULL) 1673 free(ic->ic_countryie, M_80211_NODE_IE); 1674 ic->ic_countryie = ieee80211_alloc_countryie(ic); 1675 if (ic->ic_countryie == NULL) 1676 return frm; 1677 ic->ic_countryie_chan = ic->ic_bsschan; 1678 } 1679 return add_appie(frm, ic->ic_countryie); 1680} 1681 1682/* 1683 * Send a probe request frame with the specified ssid 1684 * and any optional information element data. 1685 */ 1686int 1687ieee80211_send_probereq(struct ieee80211_node *ni, 1688 const uint8_t sa[IEEE80211_ADDR_LEN], 1689 const uint8_t da[IEEE80211_ADDR_LEN], 1690 const uint8_t bssid[IEEE80211_ADDR_LEN], 1691 const uint8_t *ssid, size_t ssidlen) 1692{ 1693 struct ieee80211vap *vap = ni->ni_vap; 1694 struct ieee80211com *ic = ni->ni_ic; 1695 const struct ieee80211_txparam *tp; 1696 struct ieee80211_bpf_params params; 1697 struct ieee80211_frame *wh; 1698 const struct ieee80211_rateset *rs; 1699 struct mbuf *m; 1700 uint8_t *frm; 1701 1702 if (vap->iv_state == IEEE80211_S_CAC) { 1703 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni, 1704 "block %s frame in CAC state", "probe request"); 1705 vap->iv_stats.is_tx_badstate++; 1706 return EIO; /* XXX */ 1707 } 1708 1709 /* 1710 * Hold a reference on the node so it doesn't go away until after 1711 * the xmit is complete all the way in the driver. On error we 1712 * will remove our reference. 1713 */ 1714 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1715 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1716 __func__, __LINE__, 1717 ni, ether_sprintf(ni->ni_macaddr), 1718 ieee80211_node_refcnt(ni)+1); 1719 ieee80211_ref_node(ni); 1720 1721 /* 1722 * prreq frame format 1723 * [tlv] ssid 1724 * [tlv] supported rates 1725 * [tlv] RSN (optional) 1726 * [tlv] extended supported rates 1727 * [tlv] WPA (optional) 1728 * [tlv] user-specified ie's 1729 */ 1730 m = ieee80211_getmgtframe(&frm, 1731 ic->ic_headroom + sizeof(struct ieee80211_frame), 1732 2 + IEEE80211_NWID_LEN 1733 + 2 + IEEE80211_RATE_SIZE 1734 + sizeof(struct ieee80211_ie_wpa) 1735 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1736 + sizeof(struct ieee80211_ie_wpa) 1737 + (vap->iv_appie_probereq != NULL ? 1738 vap->iv_appie_probereq->ie_len : 0) 1739 ); 1740 if (m == NULL) { 1741 vap->iv_stats.is_tx_nobuf++; 1742 ieee80211_free_node(ni); 1743 return ENOMEM; 1744 } 1745 1746 frm = ieee80211_add_ssid(frm, ssid, ssidlen); 1747 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 1748 frm = ieee80211_add_rates(frm, rs); 1749 if (vap->iv_flags & IEEE80211_F_WPA2) { 1750 if (vap->iv_rsn_ie != NULL) 1751 frm = add_ie(frm, vap->iv_rsn_ie); 1752 /* XXX else complain? */ 1753 } 1754 frm = ieee80211_add_xrates(frm, rs); 1755 if (vap->iv_flags & IEEE80211_F_WPA1) { 1756 if (vap->iv_wpa_ie != NULL) 1757 frm = add_ie(frm, vap->iv_wpa_ie); 1758 /* XXX else complain? */ 1759 } 1760 if (vap->iv_appie_probereq != NULL) 1761 frm = add_appie(frm, vap->iv_appie_probereq); 1762 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 1763 1764 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame), 1765 ("leading space %zd", M_LEADINGSPACE(m))); 1766 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1767 if (m == NULL) { 1768 /* NB: cannot happen */ 1769 ieee80211_free_node(ni); 1770 return ENOMEM; 1771 } 1772 1773 wh = mtod(m, struct ieee80211_frame *); 1774 ieee80211_send_setup(ni, wh, 1775 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ, 1776 IEEE80211_NONQOS_TID, sa, da, bssid); 1777 /* XXX power management? */ 1778 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1779 1780 M_WME_SETAC(m, WME_AC_BE); 1781 1782 IEEE80211_NODE_STAT(ni, tx_probereq); 1783 IEEE80211_NODE_STAT(ni, tx_mgmt); 1784 1785 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 1786 "send probe req on channel %u bssid %s ssid \"%.*s\"\n", 1787 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid), 1788 ssidlen, ssid); 1789 1790 memset(¶ms, 0, sizeof(params)); 1791 params.ibp_pri = M_WME_GETAC(m); 1792 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1793 params.ibp_rate0 = tp->mgmtrate; 1794 if (IEEE80211_IS_MULTICAST(da)) { 1795 params.ibp_flags |= IEEE80211_BPF_NOACK; 1796 params.ibp_try0 = 1; 1797 } else 1798 params.ibp_try0 = tp->maxretry; 1799 params.ibp_power = ni->ni_txpower; 1800 return ic->ic_raw_xmit(ni, m, ¶ms); 1801} 1802 1803/* 1804 * Calculate capability information for mgt frames. 1805 */ 1806static uint16_t 1807getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan) 1808{ 1809 struct ieee80211com *ic = vap->iv_ic; 1810 uint16_t capinfo; 1811 1812 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode")); 1813 1814 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 1815 capinfo = IEEE80211_CAPINFO_ESS; 1816 else if (vap->iv_opmode == IEEE80211_M_IBSS) 1817 capinfo = IEEE80211_CAPINFO_IBSS; 1818 else 1819 capinfo = 0; 1820 if (vap->iv_flags & IEEE80211_F_PRIVACY) 1821 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1822 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1823 IEEE80211_IS_CHAN_2GHZ(chan)) 1824 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1825 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1826 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1827 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH)) 1828 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT; 1829 return capinfo; 1830} 1831 1832/* 1833 * Send a management frame. The node is for the destination (or ic_bss 1834 * when in station mode). Nodes other than ic_bss have their reference 1835 * count bumped to reflect our use for an indeterminant time. 1836 */ 1837int 1838ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg) 1839{ 1840#define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT) 1841#define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0) 1842 struct ieee80211vap *vap = ni->ni_vap; 1843 struct ieee80211com *ic = ni->ni_ic; 1844 struct ieee80211_node *bss = vap->iv_bss; 1845 struct ieee80211_bpf_params params; 1846 struct mbuf *m; 1847 uint8_t *frm; 1848 uint16_t capinfo; 1849 int has_challenge, is_shared_key, ret, status; 1850 1851 KASSERT(ni != NULL, ("null node")); 1852 1853 /* 1854 * Hold a reference on the node so it doesn't go away until after 1855 * the xmit is complete all the way in the driver. On error we 1856 * will remove our reference. 1857 */ 1858 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1859 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1860 __func__, __LINE__, 1861 ni, ether_sprintf(ni->ni_macaddr), 1862 ieee80211_node_refcnt(ni)+1); 1863 ieee80211_ref_node(ni); 1864 1865 memset(¶ms, 0, sizeof(params)); 1866 switch (type) { 1867 1868 case IEEE80211_FC0_SUBTYPE_AUTH: 1869 status = arg >> 16; 1870 arg &= 0xffff; 1871 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE || 1872 arg == IEEE80211_AUTH_SHARED_RESPONSE) && 1873 ni->ni_challenge != NULL); 1874 1875 /* 1876 * Deduce whether we're doing open authentication or 1877 * shared key authentication. We do the latter if 1878 * we're in the middle of a shared key authentication 1879 * handshake or if we're initiating an authentication 1880 * request and configured to use shared key. 1881 */ 1882 is_shared_key = has_challenge || 1883 arg >= IEEE80211_AUTH_SHARED_RESPONSE || 1884 (arg == IEEE80211_AUTH_SHARED_REQUEST && 1885 bss->ni_authmode == IEEE80211_AUTH_SHARED); 1886 1887 m = ieee80211_getmgtframe(&frm, 1888 ic->ic_headroom + sizeof(struct ieee80211_frame), 1889 3 * sizeof(uint16_t) 1890 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ? 1891 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0) 1892 ); 1893 if (m == NULL) 1894 senderr(ENOMEM, is_tx_nobuf); 1895 1896 ((uint16_t *)frm)[0] = 1897 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED) 1898 : htole16(IEEE80211_AUTH_ALG_OPEN); 1899 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */ 1900 ((uint16_t *)frm)[2] = htole16(status);/* status */ 1901 1902 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) { 1903 ((uint16_t *)frm)[3] = 1904 htole16((IEEE80211_CHALLENGE_LEN << 8) | 1905 IEEE80211_ELEMID_CHALLENGE); 1906 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge, 1907 IEEE80211_CHALLENGE_LEN); 1908 m->m_pkthdr.len = m->m_len = 1909 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN; 1910 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) { 1911 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 1912 "request encrypt frame (%s)", __func__); 1913 /* mark frame for encryption */ 1914 params.ibp_flags |= IEEE80211_BPF_CRYPTO; 1915 } 1916 } else 1917 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t); 1918 1919 /* XXX not right for shared key */ 1920 if (status == IEEE80211_STATUS_SUCCESS) 1921 IEEE80211_NODE_STAT(ni, tx_auth); 1922 else 1923 IEEE80211_NODE_STAT(ni, tx_auth_fail); 1924 1925 if (vap->iv_opmode == IEEE80211_M_STA) 1926 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 1927 (void *) vap->iv_state); 1928 break; 1929 1930 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1931 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 1932 "send station deauthenticate (reason %d)", arg); 1933 m = ieee80211_getmgtframe(&frm, 1934 ic->ic_headroom + sizeof(struct ieee80211_frame), 1935 sizeof(uint16_t)); 1936 if (m == NULL) 1937 senderr(ENOMEM, is_tx_nobuf); 1938 *(uint16_t *)frm = htole16(arg); /* reason */ 1939 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 1940 1941 IEEE80211_NODE_STAT(ni, tx_deauth); 1942 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg); 1943 1944 ieee80211_node_unauthorize(ni); /* port closed */ 1945 break; 1946 1947 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1948 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1949 /* 1950 * asreq frame format 1951 * [2] capability information 1952 * [2] listen interval 1953 * [6*] current AP address (reassoc only) 1954 * [tlv] ssid 1955 * [tlv] supported rates 1956 * [tlv] extended supported rates 1957 * [4] power capability (optional) 1958 * [28] supported channels (optional) 1959 * [tlv] HT capabilities 1960 * [tlv] WME (optional) 1961 * [tlv] Vendor OUI HT capabilities (optional) 1962 * [tlv] Atheros capabilities (if negotiated) 1963 * [tlv] AppIE's (optional) 1964 */ 1965 m = ieee80211_getmgtframe(&frm, 1966 ic->ic_headroom + sizeof(struct ieee80211_frame), 1967 sizeof(uint16_t) 1968 + sizeof(uint16_t) 1969 + IEEE80211_ADDR_LEN 1970 + 2 + IEEE80211_NWID_LEN 1971 + 2 + IEEE80211_RATE_SIZE 1972 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1973 + 4 1974 + 2 + 26 1975 + sizeof(struct ieee80211_wme_info) 1976 + sizeof(struct ieee80211_ie_htcap) 1977 + 4 + sizeof(struct ieee80211_ie_htcap) 1978 + sizeof(struct ieee80211_ath_ie) 1979 + (vap->iv_appie_wpa != NULL ? 1980 vap->iv_appie_wpa->ie_len : 0) 1981 + (vap->iv_appie_assocreq != NULL ? 1982 vap->iv_appie_assocreq->ie_len : 0) 1983 ); 1984 if (m == NULL) 1985 senderr(ENOMEM, is_tx_nobuf); 1986 1987 KASSERT(vap->iv_opmode == IEEE80211_M_STA, 1988 ("wrong mode %u", vap->iv_opmode)); 1989 capinfo = IEEE80211_CAPINFO_ESS; 1990 if (vap->iv_flags & IEEE80211_F_PRIVACY) 1991 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1992 /* 1993 * NB: Some 11a AP's reject the request when 1994 * short premable is set. 1995 */ 1996 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1997 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 1998 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1999 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) && 2000 (ic->ic_caps & IEEE80211_C_SHSLOT)) 2001 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2002 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) && 2003 (vap->iv_flags & IEEE80211_F_DOTH)) 2004 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT; 2005 *(uint16_t *)frm = htole16(capinfo); 2006 frm += 2; 2007 2008 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!")); 2009 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval, 2010 bss->ni_intval)); 2011 frm += 2; 2012 2013 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 2014 IEEE80211_ADDR_COPY(frm, bss->ni_bssid); 2015 frm += IEEE80211_ADDR_LEN; 2016 } 2017 2018 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 2019 frm = ieee80211_add_rates(frm, &ni->ni_rates); 2020 if (vap->iv_flags & IEEE80211_F_WPA2) { 2021 if (vap->iv_rsn_ie != NULL) 2022 frm = add_ie(frm, vap->iv_rsn_ie); 2023 /* XXX else complain? */ 2024 } 2025 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 2026 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) { 2027 frm = ieee80211_add_powercapability(frm, 2028 ic->ic_curchan); 2029 frm = ieee80211_add_supportedchannels(frm, ic); 2030 } 2031 if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) && 2032 ni->ni_ies.htcap_ie != NULL && 2033 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) 2034 frm = ieee80211_add_htcap(frm, ni); 2035 if (vap->iv_flags & IEEE80211_F_WPA1) { 2036 if (vap->iv_wpa_ie != NULL) 2037 frm = add_ie(frm, vap->iv_wpa_ie); 2038 /* XXX else complain */ 2039 } 2040 if ((ic->ic_flags & IEEE80211_F_WME) && 2041 ni->ni_ies.wme_ie != NULL) 2042 frm = ieee80211_add_wme_info(frm, &ic->ic_wme); 2043 if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) && 2044 ni->ni_ies.htcap_ie != NULL && 2045 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) 2046 frm = ieee80211_add_htcap_vendor(frm, ni); 2047 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) 2048 frm = ieee80211_add_ath(frm, 2049 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS), 2050 (vap->iv_flags & IEEE80211_F_WPA) == 0 && 2051 ni->ni_authmode != IEEE80211_AUTH_8021X && 2052 vap->iv_def_txkey != IEEE80211_KEYIX_NONE ? 2053 vap->iv_def_txkey : 0x7fff); 2054 if (vap->iv_appie_assocreq != NULL) 2055 frm = add_appie(frm, vap->iv_appie_assocreq); 2056 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2057 2058 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 2059 (void *) vap->iv_state); 2060 break; 2061 2062 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 2063 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 2064 /* 2065 * asresp frame format 2066 * [2] capability information 2067 * [2] status 2068 * [2] association ID 2069 * [tlv] supported rates 2070 * [tlv] extended supported rates 2071 * [tlv] HT capabilities (standard, if STA enabled) 2072 * [tlv] HT information (standard, if STA enabled) 2073 * [tlv] WME (if configured and STA enabled) 2074 * [tlv] HT capabilities (vendor OUI, if STA enabled) 2075 * [tlv] HT information (vendor OUI, if STA enabled) 2076 * [tlv] Atheros capabilities (if STA enabled) 2077 * [tlv] AppIE's (optional) 2078 */ 2079 m = ieee80211_getmgtframe(&frm, 2080 ic->ic_headroom + sizeof(struct ieee80211_frame), 2081 sizeof(uint16_t) 2082 + sizeof(uint16_t) 2083 + sizeof(uint16_t) 2084 + 2 + IEEE80211_RATE_SIZE 2085 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2086 + sizeof(struct ieee80211_ie_htcap) + 4 2087 + sizeof(struct ieee80211_ie_htinfo) + 4 2088 + sizeof(struct ieee80211_wme_param) 2089 + sizeof(struct ieee80211_ath_ie) 2090 + (vap->iv_appie_assocresp != NULL ? 2091 vap->iv_appie_assocresp->ie_len : 0) 2092 ); 2093 if (m == NULL) 2094 senderr(ENOMEM, is_tx_nobuf); 2095 2096 capinfo = getcapinfo(vap, bss->ni_chan); 2097 *(uint16_t *)frm = htole16(capinfo); 2098 frm += 2; 2099 2100 *(uint16_t *)frm = htole16(arg); /* status */ 2101 frm += 2; 2102 2103 if (arg == IEEE80211_STATUS_SUCCESS) { 2104 *(uint16_t *)frm = htole16(ni->ni_associd); 2105 IEEE80211_NODE_STAT(ni, tx_assoc); 2106 } else 2107 IEEE80211_NODE_STAT(ni, tx_assoc_fail); 2108 frm += 2; 2109 2110 frm = ieee80211_add_rates(frm, &ni->ni_rates); 2111 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 2112 /* NB: respond according to what we received */ 2113 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) { 2114 frm = ieee80211_add_htcap(frm, ni); 2115 frm = ieee80211_add_htinfo(frm, ni); 2116 } 2117 if ((vap->iv_flags & IEEE80211_F_WME) && 2118 ni->ni_ies.wme_ie != NULL) 2119 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 2120 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) { 2121 frm = ieee80211_add_htcap_vendor(frm, ni); 2122 frm = ieee80211_add_htinfo_vendor(frm, ni); 2123 } 2124 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) 2125 frm = ieee80211_add_ath(frm, 2126 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS), 2127 ni->ni_ath_defkeyix); 2128 if (vap->iv_appie_assocresp != NULL) 2129 frm = add_appie(frm, vap->iv_appie_assocresp); 2130 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2131 break; 2132 2133 case IEEE80211_FC0_SUBTYPE_DISASSOC: 2134 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 2135 "send station disassociate (reason %d)", arg); 2136 m = ieee80211_getmgtframe(&frm, 2137 ic->ic_headroom + sizeof(struct ieee80211_frame), 2138 sizeof(uint16_t)); 2139 if (m == NULL) 2140 senderr(ENOMEM, is_tx_nobuf); 2141 *(uint16_t *)frm = htole16(arg); /* reason */ 2142 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 2143 2144 IEEE80211_NODE_STAT(ni, tx_disassoc); 2145 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg); 2146 break; 2147 2148 default: 2149 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni, 2150 "invalid mgmt frame type %u", type); 2151 senderr(EINVAL, is_tx_unknownmgt); 2152 /* NOTREACHED */ 2153 } 2154 2155 /* NB: force non-ProbeResp frames to the highest queue */ 2156 params.ibp_pri = WME_AC_VO; 2157 params.ibp_rate0 = bss->ni_txparms->mgmtrate; 2158 /* NB: we know all frames are unicast */ 2159 params.ibp_try0 = bss->ni_txparms->maxretry; 2160 params.ibp_power = bss->ni_txpower; 2161 return ieee80211_mgmt_output(ni, m, type, ¶ms); 2162bad: 2163 ieee80211_free_node(ni); 2164 return ret; 2165#undef senderr 2166#undef HTFLAGS 2167} 2168 2169/* 2170 * Return an mbuf with a probe response frame in it. 2171 * Space is left to prepend and 802.11 header at the 2172 * front but it's left to the caller to fill in. 2173 */ 2174struct mbuf * 2175ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy) 2176{ 2177 struct ieee80211vap *vap = bss->ni_vap; 2178 struct ieee80211com *ic = bss->ni_ic; 2179 const struct ieee80211_rateset *rs; 2180 struct mbuf *m; 2181 uint16_t capinfo; 2182 uint8_t *frm; 2183 2184 /* 2185 * probe response frame format 2186 * [8] time stamp 2187 * [2] beacon interval 2188 * [2] cabability information 2189 * [tlv] ssid 2190 * [tlv] supported rates 2191 * [tlv] parameter set (FH/DS) 2192 * [tlv] parameter set (IBSS) 2193 * [tlv] country (optional) 2194 * [3] power control (optional) 2195 * [5] channel switch announcement (CSA) (optional) 2196 * [tlv] extended rate phy (ERP) 2197 * [tlv] extended supported rates 2198 * [tlv] RSN (optional) 2199 * [tlv] HT capabilities 2200 * [tlv] HT information 2201 * [tlv] WPA (optional) 2202 * [tlv] WME (optional) 2203 * [tlv] Vendor OUI HT capabilities (optional) 2204 * [tlv] Vendor OUI HT information (optional) 2205 * [tlv] Atheros capabilities 2206 * [tlv] AppIE's (optional) 2207 */ 2208 m = ieee80211_getmgtframe(&frm, 2209 ic->ic_headroom + sizeof(struct ieee80211_frame), 2210 8 2211 + sizeof(uint16_t) 2212 + sizeof(uint16_t) 2213 + 2 + IEEE80211_NWID_LEN 2214 + 2 + IEEE80211_RATE_SIZE 2215 + 7 /* max(7,3) */ 2216 + IEEE80211_COUNTRY_MAX_SIZE 2217 + 3 2218 + sizeof(struct ieee80211_csa_ie) 2219 + 3 2220 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2221 + sizeof(struct ieee80211_ie_wpa) 2222 + sizeof(struct ieee80211_ie_htcap) 2223 + sizeof(struct ieee80211_ie_htinfo) 2224 + sizeof(struct ieee80211_ie_wpa) 2225 + sizeof(struct ieee80211_wme_param) 2226 + 4 + sizeof(struct ieee80211_ie_htcap) 2227 + 4 + sizeof(struct ieee80211_ie_htinfo) 2228 + sizeof(struct ieee80211_ath_ie) 2229 + (vap->iv_appie_proberesp != NULL ? 2230 vap->iv_appie_proberesp->ie_len : 0) 2231 ); 2232 if (m == NULL) { 2233 vap->iv_stats.is_tx_nobuf++; 2234 return NULL; 2235 } 2236 2237 memset(frm, 0, 8); /* timestamp should be filled later */ 2238 frm += 8; 2239 *(uint16_t *)frm = htole16(bss->ni_intval); 2240 frm += 2; 2241 capinfo = getcapinfo(vap, bss->ni_chan); 2242 *(uint16_t *)frm = htole16(capinfo); 2243 frm += 2; 2244 2245 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen); 2246 rs = ieee80211_get_suprates(ic, bss->ni_chan); 2247 frm = ieee80211_add_rates(frm, rs); 2248 2249 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) { 2250 *frm++ = IEEE80211_ELEMID_FHPARMS; 2251 *frm++ = 5; 2252 *frm++ = bss->ni_fhdwell & 0x00ff; 2253 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff; 2254 *frm++ = IEEE80211_FH_CHANSET( 2255 ieee80211_chan2ieee(ic, bss->ni_chan)); 2256 *frm++ = IEEE80211_FH_CHANPAT( 2257 ieee80211_chan2ieee(ic, bss->ni_chan)); 2258 *frm++ = bss->ni_fhindex; 2259 } else { 2260 *frm++ = IEEE80211_ELEMID_DSPARMS; 2261 *frm++ = 1; 2262 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan); 2263 } 2264 2265 if (vap->iv_opmode == IEEE80211_M_IBSS) { 2266 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 2267 *frm++ = 2; 2268 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 2269 } 2270 if ((vap->iv_flags & IEEE80211_F_DOTH) || 2271 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) 2272 frm = ieee80211_add_countryie(frm, ic); 2273 if (vap->iv_flags & IEEE80211_F_DOTH) { 2274 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan)) 2275 frm = ieee80211_add_powerconstraint(frm, vap); 2276 if (ic->ic_flags & IEEE80211_F_CSAPENDING) 2277 frm = ieee80211_add_csa(frm, vap); 2278 } 2279 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan)) 2280 frm = ieee80211_add_erp(frm, ic); 2281 frm = ieee80211_add_xrates(frm, rs); 2282 if (vap->iv_flags & IEEE80211_F_WPA2) { 2283 if (vap->iv_rsn_ie != NULL) 2284 frm = add_ie(frm, vap->iv_rsn_ie); 2285 /* XXX else complain? */ 2286 } 2287 /* 2288 * NB: legacy 11b clients do not get certain ie's. 2289 * The caller identifies such clients by passing 2290 * a token in legacy to us. Could expand this to be 2291 * any legacy client for stuff like HT ie's. 2292 */ 2293 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) && 2294 legacy != IEEE80211_SEND_LEGACY_11B) { 2295 frm = ieee80211_add_htcap(frm, bss); 2296 frm = ieee80211_add_htinfo(frm, bss); 2297 } 2298 if (vap->iv_flags & IEEE80211_F_WPA1) { 2299 if (vap->iv_wpa_ie != NULL) 2300 frm = add_ie(frm, vap->iv_wpa_ie); 2301 /* XXX else complain? */ 2302 } 2303 if (vap->iv_flags & IEEE80211_F_WME) 2304 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 2305 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) && 2306 (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) && 2307 legacy != IEEE80211_SEND_LEGACY_11B) { 2308 frm = ieee80211_add_htcap_vendor(frm, bss); 2309 frm = ieee80211_add_htinfo_vendor(frm, bss); 2310 } 2311 if (bss->ni_ies.ath_ie != NULL && legacy != IEEE80211_SEND_LEGACY_11B) 2312 frm = ieee80211_add_ath(frm, bss->ni_ath_flags, 2313 bss->ni_ath_defkeyix); 2314 if (vap->iv_appie_proberesp != NULL) 2315 frm = add_appie(frm, vap->iv_appie_proberesp); 2316 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2317 2318 return m; 2319} 2320 2321/* 2322 * Send a probe response frame to the specified mac address. 2323 * This does not go through the normal mgt frame api so we 2324 * can specify the destination address and re-use the bss node 2325 * for the sta reference. 2326 */ 2327int 2328ieee80211_send_proberesp(struct ieee80211vap *vap, 2329 const uint8_t da[IEEE80211_ADDR_LEN], int legacy) 2330{ 2331 struct ieee80211_node *bss = vap->iv_bss; 2332 struct ieee80211com *ic = vap->iv_ic; 2333 struct ieee80211_frame *wh; 2334 struct mbuf *m; 2335 2336 if (vap->iv_state == IEEE80211_S_CAC) { 2337 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss, 2338 "block %s frame in CAC state", "probe response"); 2339 vap->iv_stats.is_tx_badstate++; 2340 return EIO; /* XXX */ 2341 } 2342 2343 /* 2344 * Hold a reference on the node so it doesn't go away until after 2345 * the xmit is complete all the way in the driver. On error we 2346 * will remove our reference. 2347 */ 2348 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2349 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 2350 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr), 2351 ieee80211_node_refcnt(bss)+1); 2352 ieee80211_ref_node(bss); 2353 2354 m = ieee80211_alloc_proberesp(bss, legacy); 2355 if (m == NULL) { 2356 ieee80211_free_node(bss); 2357 return ENOMEM; 2358 } 2359 2360 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 2361 KASSERT(m != NULL, ("no room for header")); 2362 2363 wh = mtod(m, struct ieee80211_frame *); 2364 ieee80211_send_setup(bss, wh, 2365 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP, 2366 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid); 2367 /* XXX power management? */ 2368 m->m_flags |= M_ENCAP; /* mark encapsulated */ 2369 2370 M_WME_SETAC(m, WME_AC_BE); 2371 2372 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 2373 "send probe resp on channel %u to %s%s\n", 2374 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da), 2375 legacy ? " <legacy>" : ""); 2376 IEEE80211_NODE_STAT(bss, tx_mgmt); 2377 2378 return ic->ic_raw_xmit(bss, m, NULL); 2379} 2380 2381/* 2382 * Allocate and build a RTS (Request To Send) control frame. 2383 */ 2384struct mbuf * 2385ieee80211_alloc_rts(struct ieee80211com *ic, 2386 const uint8_t ra[IEEE80211_ADDR_LEN], 2387 const uint8_t ta[IEEE80211_ADDR_LEN], 2388 uint16_t dur) 2389{ 2390 struct ieee80211_frame_rts *rts; 2391 struct mbuf *m; 2392 2393 /* XXX honor ic_headroom */ 2394 m = m_gethdr(M_DONTWAIT, MT_DATA); 2395 if (m != NULL) { 2396 rts = mtod(m, struct ieee80211_frame_rts *); 2397 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | 2398 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS; 2399 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2400 *(u_int16_t *)rts->i_dur = htole16(dur); 2401 IEEE80211_ADDR_COPY(rts->i_ra, ra); 2402 IEEE80211_ADDR_COPY(rts->i_ta, ta); 2403 2404 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts); 2405 } 2406 return m; 2407} 2408 2409/* 2410 * Allocate and build a CTS (Clear To Send) control frame. 2411 */ 2412struct mbuf * 2413ieee80211_alloc_cts(struct ieee80211com *ic, 2414 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur) 2415{ 2416 struct ieee80211_frame_cts *cts; 2417 struct mbuf *m; 2418 2419 /* XXX honor ic_headroom */ 2420 m = m_gethdr(M_DONTWAIT, MT_DATA); 2421 if (m != NULL) { 2422 cts = mtod(m, struct ieee80211_frame_cts *); 2423 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 | 2424 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS; 2425 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2426 *(u_int16_t *)cts->i_dur = htole16(dur); 2427 IEEE80211_ADDR_COPY(cts->i_ra, ra); 2428 2429 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts); 2430 } 2431 return m; 2432} 2433 2434static void 2435ieee80211_tx_mgt_timeout(void *arg) 2436{ 2437 struct ieee80211_node *ni = arg; 2438 struct ieee80211vap *vap = ni->ni_vap; 2439 2440 if (vap->iv_state != IEEE80211_S_INIT && 2441 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) { 2442 /* 2443 * NB: it's safe to specify a timeout as the reason here; 2444 * it'll only be used in the right state. 2445 */ 2446 ieee80211_new_state(vap, IEEE80211_S_SCAN, 2447 IEEE80211_SCAN_FAIL_TIMEOUT); 2448 } 2449} 2450 2451static void 2452ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status) 2453{ 2454 struct ieee80211vap *vap = ni->ni_vap; 2455 enum ieee80211_state ostate = (enum ieee80211_state) arg; 2456 2457 /* 2458 * Frame transmit completed; arrange timer callback. If 2459 * transmit was successfuly we wait for response. Otherwise 2460 * we arrange an immediate callback instead of doing the 2461 * callback directly since we don't know what state the driver 2462 * is in (e.g. what locks it is holding). This work should 2463 * not be too time-critical and not happen too often so the 2464 * added overhead is acceptable. 2465 * 2466 * XXX what happens if !acked but response shows up before callback? 2467 */ 2468 if (vap->iv_state == ostate) 2469 callout_reset(&vap->iv_mgtsend, 2470 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0, 2471 ieee80211_tx_mgt_timeout, ni); 2472} 2473 2474static void 2475ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm, 2476 struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni) 2477{ 2478 struct ieee80211vap *vap = ni->ni_vap; 2479 struct ieee80211com *ic = ni->ni_ic; 2480 struct ieee80211_rateset *rs = &ni->ni_rates; 2481 uint16_t capinfo; 2482 2483 /* 2484 * beacon frame format 2485 * [8] time stamp 2486 * [2] beacon interval 2487 * [2] cabability information 2488 * [tlv] ssid 2489 * [tlv] supported rates 2490 * [3] parameter set (DS) 2491 * [8] CF parameter set (optional) 2492 * [tlv] parameter set (IBSS/TIM) 2493 * [tlv] country (optional) 2494 * [3] power control (optional) 2495 * [5] channel switch announcement (CSA) (optional) 2496 * [tlv] extended rate phy (ERP) 2497 * [tlv] extended supported rates 2498 * [tlv] RSN parameters 2499 * [tlv] HT capabilities 2500 * [tlv] HT information 2501 * XXX Vendor-specific OIDs (e.g. Atheros) 2502 * [tlv] WPA parameters 2503 * [tlv] WME parameters 2504 * [tlv] Vendor OUI HT capabilities (optional) 2505 * [tlv] Vendor OUI HT information (optional) 2506 * [tlv] TDMA parameters (optional) 2507 * [tlv] application data (optional) 2508 */ 2509 2510 memset(bo, 0, sizeof(*bo)); 2511 2512 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */ 2513 frm += 8; 2514 *(uint16_t *)frm = htole16(ni->ni_intval); 2515 frm += 2; 2516 capinfo = getcapinfo(vap, ni->ni_chan); 2517 bo->bo_caps = (uint16_t *)frm; 2518 *(uint16_t *)frm = htole16(capinfo); 2519 frm += 2; 2520 *frm++ = IEEE80211_ELEMID_SSID; 2521 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) { 2522 *frm++ = ni->ni_esslen; 2523 memcpy(frm, ni->ni_essid, ni->ni_esslen); 2524 frm += ni->ni_esslen; 2525 } else 2526 *frm++ = 0; 2527 frm = ieee80211_add_rates(frm, rs); 2528 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) { 2529 *frm++ = IEEE80211_ELEMID_DSPARMS; 2530 *frm++ = 1; 2531 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 2532 } 2533 if (ic->ic_flags & IEEE80211_F_PCF) { 2534 bo->bo_cfp = frm; 2535 frm = ieee80211_add_cfparms(frm, ic); 2536 } 2537 bo->bo_tim = frm; 2538 if (vap->iv_opmode == IEEE80211_M_IBSS) { 2539 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 2540 *frm++ = 2; 2541 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 2542 bo->bo_tim_len = 0; 2543 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 2544 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm; 2545 2546 tie->tim_ie = IEEE80211_ELEMID_TIM; 2547 tie->tim_len = 4; /* length */ 2548 tie->tim_count = 0; /* DTIM count */ 2549 tie->tim_period = vap->iv_dtim_period; /* DTIM period */ 2550 tie->tim_bitctl = 0; /* bitmap control */ 2551 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */ 2552 frm += sizeof(struct ieee80211_tim_ie); 2553 bo->bo_tim_len = 1; 2554 } 2555 bo->bo_tim_trailer = frm; 2556 if ((vap->iv_flags & IEEE80211_F_DOTH) || 2557 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) 2558 frm = ieee80211_add_countryie(frm, ic); 2559 if (vap->iv_flags & IEEE80211_F_DOTH) { 2560 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)) 2561 frm = ieee80211_add_powerconstraint(frm, vap); 2562 bo->bo_csa = frm; 2563 if (ic->ic_flags & IEEE80211_F_CSAPENDING) 2564 frm = ieee80211_add_csa(frm, vap); 2565 } else 2566 bo->bo_csa = frm; 2567 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) { 2568 bo->bo_erp = frm; 2569 frm = ieee80211_add_erp(frm, ic); 2570 } 2571 frm = ieee80211_add_xrates(frm, rs); 2572 if (vap->iv_flags & IEEE80211_F_WPA2) { 2573 if (vap->iv_rsn_ie != NULL) 2574 frm = add_ie(frm, vap->iv_rsn_ie); 2575 /* XXX else complain */ 2576 } 2577 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 2578 frm = ieee80211_add_htcap(frm, ni); 2579 bo->bo_htinfo = frm; 2580 frm = ieee80211_add_htinfo(frm, ni); 2581 } 2582 if (vap->iv_flags & IEEE80211_F_WPA1) { 2583 if (vap->iv_wpa_ie != NULL) 2584 frm = add_ie(frm, vap->iv_wpa_ie); 2585 /* XXX else complain */ 2586 } 2587 if (vap->iv_flags & IEEE80211_F_WME) { 2588 bo->bo_wme = frm; 2589 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 2590 } 2591 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && 2592 (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT)) { 2593 frm = ieee80211_add_htcap_vendor(frm, ni); 2594 frm = ieee80211_add_htinfo_vendor(frm, ni); 2595 } 2596#ifdef IEEE80211_SUPPORT_TDMA 2597 if (vap->iv_caps & IEEE80211_C_TDMA) { 2598 bo->bo_tdma = frm; 2599 frm = ieee80211_add_tdma(frm, vap); 2600 } 2601#endif 2602 if (vap->iv_appie_beacon != NULL) { 2603 bo->bo_appie = frm; 2604 bo->bo_appie_len = vap->iv_appie_beacon->ie_len; 2605 frm = add_appie(frm, vap->iv_appie_beacon); 2606 } 2607 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer; 2608 bo->bo_csa_trailer_len = frm - bo->bo_csa; 2609 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2610} 2611 2612/* 2613 * Allocate a beacon frame and fillin the appropriate bits. 2614 */ 2615struct mbuf * 2616ieee80211_beacon_alloc(struct ieee80211_node *ni, 2617 struct ieee80211_beacon_offsets *bo) 2618{ 2619 struct ieee80211vap *vap = ni->ni_vap; 2620 struct ieee80211com *ic = ni->ni_ic; 2621 struct ifnet *ifp = vap->iv_ifp; 2622 struct ieee80211_frame *wh; 2623 struct mbuf *m; 2624 int pktlen; 2625 uint8_t *frm; 2626 2627 /* 2628 * beacon frame format 2629 * [8] time stamp 2630 * [2] beacon interval 2631 * [2] cabability information 2632 * [tlv] ssid 2633 * [tlv] supported rates 2634 * [3] parameter set (DS) 2635 * [8] CF parameter set (optional) 2636 * [tlv] parameter set (IBSS/TIM) 2637 * [tlv] country (optional) 2638 * [3] power control (optional) 2639 * [5] channel switch announcement (CSA) (optional) 2640 * [tlv] extended rate phy (ERP) 2641 * [tlv] extended supported rates 2642 * [tlv] RSN parameters 2643 * [tlv] HT capabilities 2644 * [tlv] HT information 2645 * [tlv] Vendor OUI HT capabilities (optional) 2646 * [tlv] Vendor OUI HT information (optional) 2647 * XXX Vendor-specific OIDs (e.g. Atheros) 2648 * [tlv] WPA parameters 2649 * [tlv] WME parameters 2650 * [tlv] TDMA parameters (optional) 2651 * [tlv] application data (optional) 2652 * NB: we allocate the max space required for the TIM bitmap. 2653 * XXX how big is this? 2654 */ 2655 pktlen = 8 /* time stamp */ 2656 + sizeof(uint16_t) /* beacon interval */ 2657 + sizeof(uint16_t) /* capabilities */ 2658 + 2 + ni->ni_esslen /* ssid */ 2659 + 2 + IEEE80211_RATE_SIZE /* supported rates */ 2660 + 2 + 1 /* DS parameters */ 2661 + 2 + 6 /* CF parameters */ 2662 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */ 2663 + IEEE80211_COUNTRY_MAX_SIZE /* country */ 2664 + 2 + 1 /* power control */ 2665 + sizeof(struct ieee80211_csa_ie) /* CSA */ 2666 + 2 + 1 /* ERP */ 2667 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2668 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */ 2669 2*sizeof(struct ieee80211_ie_wpa) : 0) 2670 /* XXX conditional? */ 2671 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */ 2672 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */ 2673 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */ 2674 sizeof(struct ieee80211_wme_param) : 0) 2675#ifdef IEEE80211_SUPPORT_TDMA 2676 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */ 2677 sizeof(struct ieee80211_tdma_param) : 0) 2678#endif 2679 + IEEE80211_MAX_APPIE 2680 ; 2681 m = ieee80211_getmgtframe(&frm, 2682 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen); 2683 if (m == NULL) { 2684 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 2685 "%s: cannot get buf; size %u\n", __func__, pktlen); 2686 vap->iv_stats.is_tx_nobuf++; 2687 return NULL; 2688 } 2689 ieee80211_beacon_construct(m, frm, bo, ni); 2690 2691 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 2692 KASSERT(m != NULL, ("no space for 802.11 header?")); 2693 wh = mtod(m, struct ieee80211_frame *); 2694 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 2695 IEEE80211_FC0_SUBTYPE_BEACON; 2696 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2697 *(uint16_t *)wh->i_dur = 0; 2698 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 2699 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 2700 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 2701 *(uint16_t *)wh->i_seq = 0; 2702 2703 return m; 2704} 2705 2706/* 2707 * Update the dynamic parts of a beacon frame based on the current state. 2708 */ 2709int 2710ieee80211_beacon_update(struct ieee80211_node *ni, 2711 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast) 2712{ 2713 struct ieee80211vap *vap = ni->ni_vap; 2714 struct ieee80211com *ic = ni->ni_ic; 2715 int len_changed = 0; 2716 uint16_t capinfo; 2717 2718 IEEE80211_LOCK(ic); 2719 /* 2720 * Handle 11h channel change when we've reached the count. 2721 * We must recalculate the beacon frame contents to account 2722 * for the new channel. Note we do this only for the first 2723 * vap that reaches this point; subsequent vaps just update 2724 * their beacon state to reflect the recalculated channel. 2725 */ 2726 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) && 2727 vap->iv_csa_count == ic->ic_csa_count) { 2728 vap->iv_csa_count = 0; 2729 /* 2730 * Effect channel change before reconstructing the beacon 2731 * frame contents as many places reference ni_chan. 2732 */ 2733 if (ic->ic_csa_newchan != NULL) 2734 ieee80211_csa_completeswitch(ic); 2735 /* 2736 * NB: ieee80211_beacon_construct clears all pending 2737 * updates in bo_flags so we don't need to explicitly 2738 * clear IEEE80211_BEACON_CSA. 2739 */ 2740 ieee80211_beacon_construct(m, 2741 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni); 2742 2743 /* XXX do WME aggressive mode processing? */ 2744 IEEE80211_UNLOCK(ic); 2745 return 1; /* just assume length changed */ 2746 } 2747 2748 /* XXX faster to recalculate entirely or just changes? */ 2749 capinfo = getcapinfo(vap, ni->ni_chan); 2750 *bo->bo_caps = htole16(capinfo); 2751 2752 if (vap->iv_flags & IEEE80211_F_WME) { 2753 struct ieee80211_wme_state *wme = &ic->ic_wme; 2754 2755 /* 2756 * Check for agressive mode change. When there is 2757 * significant high priority traffic in the BSS 2758 * throttle back BE traffic by using conservative 2759 * parameters. Otherwise BE uses agressive params 2760 * to optimize performance of legacy/non-QoS traffic. 2761 */ 2762 if (wme->wme_flags & WME_F_AGGRMODE) { 2763 if (wme->wme_hipri_traffic > 2764 wme->wme_hipri_switch_thresh) { 2765 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 2766 "%s: traffic %u, disable aggressive mode\n", 2767 __func__, wme->wme_hipri_traffic); 2768 wme->wme_flags &= ~WME_F_AGGRMODE; 2769 ieee80211_wme_updateparams_locked(vap); 2770 wme->wme_hipri_traffic = 2771 wme->wme_hipri_switch_hysteresis; 2772 } else 2773 wme->wme_hipri_traffic = 0; 2774 } else { 2775 if (wme->wme_hipri_traffic <= 2776 wme->wme_hipri_switch_thresh) { 2777 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 2778 "%s: traffic %u, enable aggressive mode\n", 2779 __func__, wme->wme_hipri_traffic); 2780 wme->wme_flags |= WME_F_AGGRMODE; 2781 ieee80211_wme_updateparams_locked(vap); 2782 wme->wme_hipri_traffic = 0; 2783 } else 2784 wme->wme_hipri_traffic = 2785 wme->wme_hipri_switch_hysteresis; 2786 } 2787 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) { 2788 (void) ieee80211_add_wme_param(bo->bo_wme, wme); 2789 clrbit(bo->bo_flags, IEEE80211_BEACON_WME); 2790 } 2791 } 2792 2793 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) { 2794 ieee80211_ht_update_beacon(vap, bo); 2795 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO); 2796 } 2797#ifdef IEEE80211_SUPPORT_TDMA 2798 if (vap->iv_caps & IEEE80211_C_TDMA) { 2799 /* 2800 * NB: the beacon is potentially updated every TBTT. 2801 */ 2802 ieee80211_tdma_update_beacon(vap, bo); 2803 } 2804#endif 2805 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/ 2806 struct ieee80211_tim_ie *tie = 2807 (struct ieee80211_tim_ie *) bo->bo_tim; 2808 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) { 2809 u_int timlen, timoff, i; 2810 /* 2811 * ATIM/DTIM needs updating. If it fits in the 2812 * current space allocated then just copy in the 2813 * new bits. Otherwise we need to move any trailing 2814 * data to make room. Note that we know there is 2815 * contiguous space because ieee80211_beacon_allocate 2816 * insures there is space in the mbuf to write a 2817 * maximal-size virtual bitmap (based on iv_max_aid). 2818 */ 2819 /* 2820 * Calculate the bitmap size and offset, copy any 2821 * trailer out of the way, and then copy in the 2822 * new bitmap and update the information element. 2823 * Note that the tim bitmap must contain at least 2824 * one byte and any offset must be even. 2825 */ 2826 if (vap->iv_ps_pending != 0) { 2827 timoff = 128; /* impossibly large */ 2828 for (i = 0; i < vap->iv_tim_len; i++) 2829 if (vap->iv_tim_bitmap[i]) { 2830 timoff = i &~ 1; 2831 break; 2832 } 2833 KASSERT(timoff != 128, ("tim bitmap empty!")); 2834 for (i = vap->iv_tim_len-1; i >= timoff; i--) 2835 if (vap->iv_tim_bitmap[i]) 2836 break; 2837 timlen = 1 + (i - timoff); 2838 } else { 2839 timoff = 0; 2840 timlen = 1; 2841 } 2842 if (timlen != bo->bo_tim_len) { 2843 /* copy up/down trailer */ 2844 int adjust = tie->tim_bitmap+timlen 2845 - bo->bo_tim_trailer; 2846 ovbcopy(bo->bo_tim_trailer, 2847 bo->bo_tim_trailer+adjust, 2848 bo->bo_tim_trailer_len); 2849 bo->bo_tim_trailer += adjust; 2850 bo->bo_erp += adjust; 2851 bo->bo_htinfo += adjust; 2852 bo->bo_appie += adjust; 2853 bo->bo_wme += adjust; 2854 bo->bo_csa += adjust; 2855 bo->bo_tim_len = timlen; 2856 2857 /* update information element */ 2858 tie->tim_len = 3 + timlen; 2859 tie->tim_bitctl = timoff; 2860 len_changed = 1; 2861 } 2862 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff, 2863 bo->bo_tim_len); 2864 2865 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM); 2866 2867 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER, 2868 "%s: TIM updated, pending %u, off %u, len %u\n", 2869 __func__, vap->iv_ps_pending, timoff, timlen); 2870 } 2871 /* count down DTIM period */ 2872 if (tie->tim_count == 0) 2873 tie->tim_count = tie->tim_period - 1; 2874 else 2875 tie->tim_count--; 2876 /* update state for buffered multicast frames on DTIM */ 2877 if (mcast && tie->tim_count == 0) 2878 tie->tim_bitctl |= 1; 2879 else 2880 tie->tim_bitctl &= ~1; 2881 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) { 2882 struct ieee80211_csa_ie *csa = 2883 (struct ieee80211_csa_ie *) bo->bo_csa; 2884 2885 /* 2886 * Insert or update CSA ie. If we're just starting 2887 * to count down to the channel switch then we need 2888 * to insert the CSA ie. Otherwise we just need to 2889 * drop the count. The actual change happens above 2890 * when the vap's count reaches the target count. 2891 */ 2892 if (vap->iv_csa_count == 0) { 2893 memmove(&csa[1], csa, bo->bo_csa_trailer_len); 2894 bo->bo_erp += sizeof(*csa); 2895 bo->bo_wme += sizeof(*csa); 2896 bo->bo_appie += sizeof(*csa); 2897 bo->bo_csa_trailer_len += sizeof(*csa); 2898 bo->bo_tim_trailer_len += sizeof(*csa); 2899 m->m_len += sizeof(*csa); 2900 m->m_pkthdr.len += sizeof(*csa); 2901 2902 ieee80211_add_csa(bo->bo_csa, vap); 2903 } else 2904 csa->csa_count--; 2905 vap->iv_csa_count++; 2906 /* NB: don't clear IEEE80211_BEACON_CSA */ 2907 } 2908 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) { 2909 /* 2910 * ERP element needs updating. 2911 */ 2912 (void) ieee80211_add_erp(bo->bo_erp, ic); 2913 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP); 2914 } 2915 } 2916 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) { 2917 const struct ieee80211_appie *aie = vap->iv_appie_beacon; 2918 int aielen; 2919 uint8_t *frm; 2920 2921 aielen = 0; 2922 if (aie != NULL) 2923 aielen += aie->ie_len; 2924 if (aielen != bo->bo_appie_len) { 2925 /* copy up/down trailer */ 2926 int adjust = aielen - bo->bo_appie_len; 2927 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust, 2928 bo->bo_tim_trailer_len); 2929 bo->bo_tim_trailer += adjust; 2930 bo->bo_appie += adjust; 2931 bo->bo_appie_len = aielen; 2932 2933 len_changed = 1; 2934 } 2935 frm = bo->bo_appie; 2936 if (aie != NULL) 2937 frm = add_appie(frm, aie); 2938 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE); 2939 } 2940 IEEE80211_UNLOCK(ic); 2941 2942 return len_changed; 2943} 2944