ieee80211_output.c revision 193658
1/*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 193658 2009-06-07 22:06:15Z sam $"); 29 30#include "opt_inet.h" 31#include "opt_inet6.h" 32#include "opt_wlan.h" 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/mbuf.h> 37#include <sys/kernel.h> 38#include <sys/endian.h> 39 40#include <sys/socket.h> 41 42#include <net/bpf.h> 43#include <net/ethernet.h> 44#include <net/if.h> 45#include <net/if_llc.h> 46#include <net/if_media.h> 47#include <net/if_vlan_var.h> 48 49#include <net80211/ieee80211_var.h> 50#include <net80211/ieee80211_regdomain.h> 51#ifdef IEEE80211_SUPPORT_SUPERG 52#include <net80211/ieee80211_superg.h> 53#endif 54#ifdef IEEE80211_SUPPORT_TDMA 55#include <net80211/ieee80211_tdma.h> 56#endif 57#include <net80211/ieee80211_wds.h> 58 59#ifdef INET 60#include <netinet/in.h> 61#include <netinet/if_ether.h> 62#include <netinet/in_systm.h> 63#include <netinet/ip.h> 64#endif 65#ifdef INET6 66#include <netinet/ip6.h> 67#endif 68 69#include <security/mac/mac_framework.h> 70 71#define ETHER_HEADER_COPY(dst, src) \ 72 memcpy(dst, src, sizeof(struct ether_header)) 73 74static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *, 75 u_int hdrsize, u_int ciphdrsize, u_int mtu); 76static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int); 77 78#ifdef IEEE80211_DEBUG 79/* 80 * Decide if an outbound management frame should be 81 * printed when debugging is enabled. This filters some 82 * of the less interesting frames that come frequently 83 * (e.g. beacons). 84 */ 85static __inline int 86doprint(struct ieee80211vap *vap, int subtype) 87{ 88 switch (subtype) { 89 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 90 return (vap->iv_opmode == IEEE80211_M_IBSS); 91 } 92 return 1; 93} 94#endif 95 96/* 97 * Start method for vap's. All packets from the stack come 98 * through here. We handle common processing of the packets 99 * before dispatching them to the underlying device. 100 */ 101void 102ieee80211_start(struct ifnet *ifp) 103{ 104#define IS_DWDS(vap) \ 105 (vap->iv_opmode == IEEE80211_M_WDS && \ 106 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) 107 struct ieee80211vap *vap = ifp->if_softc; 108 struct ieee80211com *ic = vap->iv_ic; 109 struct ifnet *parent = ic->ic_ifp; 110 struct ieee80211_node *ni; 111 struct mbuf *m; 112 struct ether_header *eh; 113 int error; 114 115 /* NB: parent must be up and running */ 116 if (!IFNET_IS_UP_RUNNING(parent)) { 117 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 118 "%s: ignore queue, parent %s not up+running\n", 119 __func__, parent->if_xname); 120 /* XXX stat */ 121 return; 122 } 123 if (vap->iv_state == IEEE80211_S_SLEEP) { 124 /* 125 * In power save, wakeup device for transmit. 126 */ 127 ieee80211_new_state(vap, IEEE80211_S_RUN, 0); 128 return; 129 } 130 /* 131 * No data frames go out unless we're running. 132 * Note in particular this covers CAC and CSA 133 * states (though maybe we should check muting 134 * for CSA). 135 */ 136 if (vap->iv_state != IEEE80211_S_RUN) { 137 IEEE80211_LOCK(ic); 138 /* re-check under the com lock to avoid races */ 139 if (vap->iv_state != IEEE80211_S_RUN) { 140 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 141 "%s: ignore queue, in %s state\n", 142 __func__, ieee80211_state_name[vap->iv_state]); 143 vap->iv_stats.is_tx_badstate++; 144 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 145 IEEE80211_UNLOCK(ic); 146 return; 147 } 148 IEEE80211_UNLOCK(ic); 149 } 150 for (;;) { 151 IFQ_DEQUEUE(&ifp->if_snd, m); 152 if (m == NULL) 153 break; 154 /* 155 * Sanitize mbuf flags for net80211 use. We cannot 156 * clear M_PWR_SAV because this may be set for frames 157 * that are re-submitted from the power save queue. 158 * 159 * NB: This must be done before ieee80211_classify as 160 * it marks EAPOL in frames with M_EAPOL. 161 */ 162 m->m_flags &= ~(M_80211_TX - M_PWR_SAV); 163 /* 164 * Cancel any background scan. 165 */ 166 if (ic->ic_flags & IEEE80211_F_SCAN) 167 ieee80211_cancel_anyscan(vap); 168 /* 169 * Find the node for the destination so we can do 170 * things like power save and fast frames aggregation. 171 * 172 * NB: past this point various code assumes the first 173 * mbuf has the 802.3 header present (and contiguous). 174 */ 175 ni = NULL; 176 if (m->m_len < sizeof(struct ether_header) && 177 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) { 178 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 179 "discard frame, %s\n", "m_pullup failed"); 180 vap->iv_stats.is_tx_nobuf++; /* XXX */ 181 ifp->if_oerrors++; 182 continue; 183 } 184 eh = mtod(m, struct ether_header *); 185 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 186 if (IS_DWDS(vap)) { 187 /* 188 * Only unicast frames from the above go out 189 * DWDS vaps; multicast frames are handled by 190 * dispatching the frame as it comes through 191 * the AP vap (see below). 192 */ 193 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS, 194 eh->ether_dhost, "mcast", "%s", "on DWDS"); 195 vap->iv_stats.is_dwds_mcast++; 196 m_freem(m); 197 continue; 198 } 199 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 200 /* 201 * Spam DWDS vap's w/ multicast traffic. 202 */ 203 /* XXX only if dwds in use? */ 204 ieee80211_dwds_mcast(vap, m); 205 } 206 } 207 ni = ieee80211_find_txnode(vap, eh->ether_dhost); 208 if (ni == NULL) { 209 /* NB: ieee80211_find_txnode does stat+msg */ 210 ifp->if_oerrors++; 211 m_freem(m); 212 continue; 213 } 214 if (ni->ni_associd == 0 && 215 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) { 216 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT, 217 eh->ether_dhost, NULL, 218 "sta not associated (type 0x%04x)", 219 htons(eh->ether_type)); 220 vap->iv_stats.is_tx_notassoc++; 221 ifp->if_oerrors++; 222 m_freem(m); 223 ieee80211_free_node(ni); 224 continue; 225 } 226 227 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 228 (m->m_flags & M_PWR_SAV) == 0) { 229 /* 230 * Station in power save mode; pass the frame 231 * to the 802.11 layer and continue. We'll get 232 * the frame back when the time is right. 233 * XXX lose WDS vap linkage? 234 */ 235 (void) ieee80211_pwrsave(ni, m); 236 ieee80211_free_node(ni); 237 continue; 238 } 239 /* calculate priority so drivers can find the tx queue */ 240 if (ieee80211_classify(ni, m)) { 241 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT, 242 eh->ether_dhost, NULL, 243 "%s", "classification failure"); 244 vap->iv_stats.is_tx_classify++; 245 ifp->if_oerrors++; 246 m_freem(m); 247 ieee80211_free_node(ni); 248 continue; 249 } 250 /* 251 * Stash the node pointer. Note that we do this after 252 * any call to ieee80211_dwds_mcast because that code 253 * uses any existing value for rcvif to identify the 254 * interface it (might have been) received on. 255 */ 256 m->m_pkthdr.rcvif = (void *)ni; 257 258 BPF_MTAP(ifp, m); /* 802.3 tx */ 259 260 /* 261 * Check if A-MPDU tx aggregation is setup or if we 262 * should try to enable it. The sta must be associated 263 * with HT and A-MPDU enabled for use. When the policy 264 * routine decides we should enable A-MPDU we issue an 265 * ADDBA request and wait for a reply. The frame being 266 * encapsulated will go out w/o using A-MPDU, or possibly 267 * it might be collected by the driver and held/retransmit. 268 * The default ic_ampdu_enable routine handles staggering 269 * ADDBA requests in case the receiver NAK's us or we are 270 * otherwise unable to establish a BA stream. 271 */ 272 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) && 273 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) && 274 (m->m_flags & M_EAPOL) == 0) { 275 const int ac = M_WME_GETAC(m); 276 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac]; 277 278 ieee80211_txampdu_count_packet(tap); 279 if (IEEE80211_AMPDU_RUNNING(tap)) { 280 /* 281 * Operational, mark frame for aggregation. 282 * 283 * XXX do tx aggregation here 284 */ 285 m->m_flags |= M_AMPDU_MPDU; 286 } else if (!IEEE80211_AMPDU_REQUESTED(tap) && 287 ic->ic_ampdu_enable(ni, tap)) { 288 /* 289 * Not negotiated yet, request service. 290 */ 291 ieee80211_ampdu_request(ni, tap); 292 /* XXX hold frame for reply? */ 293 } 294 } 295#ifdef IEEE80211_SUPPORT_SUPERG 296 else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) { 297 m = ieee80211_ff_check(ni, m); 298 if (m == NULL) { 299 /* NB: any ni ref held on stageq */ 300 continue; 301 } 302 } 303#endif /* IEEE80211_SUPPORT_SUPERG */ 304 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) { 305 /* 306 * Encapsulate the packet in prep for transmission. 307 */ 308 m = ieee80211_encap(vap, ni, m); 309 if (m == NULL) { 310 /* NB: stat+msg handled in ieee80211_encap */ 311 ieee80211_free_node(ni); 312 continue; 313 } 314 } 315 316 error = parent->if_transmit(parent, m); 317 if (error != 0) { 318 /* NB: IFQ_HANDOFF reclaims mbuf */ 319 ieee80211_free_node(ni); 320 } else { 321 ifp->if_opackets++; 322 } 323 ic->ic_lastdata = ticks; 324 } 325#undef IS_DWDS 326} 327 328/* 329 * 802.11 output routine. This is (currently) used only to 330 * connect bpf write calls to the 802.11 layer for injecting 331 * raw 802.11 frames. 332 */ 333int 334ieee80211_output(struct ifnet *ifp, struct mbuf *m, 335 struct sockaddr *dst, struct route *ro) 336{ 337#define senderr(e) do { error = (e); goto bad;} while (0) 338 struct ieee80211_node *ni = NULL; 339 struct ieee80211vap *vap; 340 struct ieee80211_frame *wh; 341 int error; 342 343 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) { 344 /* 345 * Short-circuit requests if the vap is marked OACTIVE 346 * as this is used when tearing down state to indicate 347 * the vap may be gone. This can also happen because a 348 * packet came down through ieee80211_start before the 349 * vap entered RUN state in which case it's also ok to 350 * just drop the frame. This should not be necessary 351 * but callers of if_output don't check OACTIVE. 352 */ 353 senderr(ENETDOWN); 354 } 355 vap = ifp->if_softc; 356 /* 357 * Hand to the 802.3 code if not tagged as 358 * a raw 802.11 frame. 359 */ 360 if (dst->sa_family != AF_IEEE80211) 361 return vap->iv_output(ifp, m, dst, ro); 362#ifdef MAC 363 error = mac_ifnet_check_transmit(ifp, m); 364 if (error) 365 senderr(error); 366#endif 367 if (ifp->if_flags & IFF_MONITOR) 368 senderr(ENETDOWN); 369 if (!IFNET_IS_UP_RUNNING(ifp)) 370 senderr(ENETDOWN); 371 if (vap->iv_state == IEEE80211_S_CAC) { 372 IEEE80211_DPRINTF(vap, 373 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 374 "block %s frame in CAC state\n", "raw data"); 375 vap->iv_stats.is_tx_badstate++; 376 senderr(EIO); /* XXX */ 377 } 378 /* XXX bypass bridge, pfil, carp, etc. */ 379 380 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack)) 381 senderr(EIO); /* XXX */ 382 wh = mtod(m, struct ieee80211_frame *); 383 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 384 IEEE80211_FC0_VERSION_0) 385 senderr(EIO); /* XXX */ 386 387 /* locate destination node */ 388 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 389 case IEEE80211_FC1_DIR_NODS: 390 case IEEE80211_FC1_DIR_FROMDS: 391 ni = ieee80211_find_txnode(vap, wh->i_addr1); 392 break; 393 case IEEE80211_FC1_DIR_TODS: 394 case IEEE80211_FC1_DIR_DSTODS: 395 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) 396 senderr(EIO); /* XXX */ 397 ni = ieee80211_find_txnode(vap, wh->i_addr3); 398 break; 399 default: 400 senderr(EIO); /* XXX */ 401 } 402 if (ni == NULL) { 403 /* 404 * Permit packets w/ bpf params through regardless 405 * (see below about sa_len). 406 */ 407 if (dst->sa_len == 0) 408 senderr(EHOSTUNREACH); 409 ni = ieee80211_ref_node(vap->iv_bss); 410 } 411 412 /* 413 * Sanitize mbuf for net80211 flags leaked from above. 414 * 415 * NB: This must be done before ieee80211_classify as 416 * it marks EAPOL in frames with M_EAPOL. 417 */ 418 m->m_flags &= ~M_80211_TX; 419 420 /* calculate priority so drivers can find the tx queue */ 421 /* XXX assumes an 802.3 frame */ 422 if (ieee80211_classify(ni, m)) 423 senderr(EIO); /* XXX */ 424 425 IEEE80211_NODE_STAT(ni, tx_data); 426 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 427 IEEE80211_NODE_STAT(ni, tx_mcast); 428 m->m_flags |= M_MCAST; 429 } else 430 IEEE80211_NODE_STAT(ni, tx_ucast); 431 /* NB: ieee80211_encap does not include 802.11 header */ 432 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len); 433 434 /* 435 * NB: DLT_IEEE802_11_RADIO identifies the parameters are 436 * present by setting the sa_len field of the sockaddr (yes, 437 * this is a hack). 438 * NB: we assume sa_data is suitably aligned to cast. 439 */ 440 return vap->iv_ic->ic_raw_xmit(ni, m, 441 (const struct ieee80211_bpf_params *)(dst->sa_len ? 442 dst->sa_data : NULL)); 443bad: 444 if (m != NULL) 445 m_freem(m); 446 if (ni != NULL) 447 ieee80211_free_node(ni); 448 return error; 449#undef senderr 450} 451 452/* 453 * Set the direction field and address fields of an outgoing 454 * frame. Note this should be called early on in constructing 455 * a frame as it sets i_fc[1]; other bits can then be or'd in. 456 */ 457static void 458ieee80211_send_setup( 459 struct ieee80211_node *ni, 460 struct mbuf *m, 461 int type, int tid, 462 const uint8_t sa[IEEE80211_ADDR_LEN], 463 const uint8_t da[IEEE80211_ADDR_LEN], 464 const uint8_t bssid[IEEE80211_ADDR_LEN]) 465{ 466#define WH4(wh) ((struct ieee80211_frame_addr4 *)wh) 467 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *); 468 ieee80211_seq seqno; 469 470 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type; 471 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) { 472 struct ieee80211vap *vap = ni->ni_vap; 473 474 switch (vap->iv_opmode) { 475 case IEEE80211_M_STA: 476 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 477 IEEE80211_ADDR_COPY(wh->i_addr1, bssid); 478 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 479 IEEE80211_ADDR_COPY(wh->i_addr3, da); 480 break; 481 case IEEE80211_M_IBSS: 482 case IEEE80211_M_AHDEMO: 483 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 484 IEEE80211_ADDR_COPY(wh->i_addr1, da); 485 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 486 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 487 break; 488 case IEEE80211_M_HOSTAP: 489 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 490 IEEE80211_ADDR_COPY(wh->i_addr1, da); 491 IEEE80211_ADDR_COPY(wh->i_addr2, bssid); 492 IEEE80211_ADDR_COPY(wh->i_addr3, sa); 493 break; 494 case IEEE80211_M_WDS: 495 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 496 IEEE80211_ADDR_COPY(wh->i_addr1, da); 497 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 498 IEEE80211_ADDR_COPY(wh->i_addr3, da); 499 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa); 500 break; 501 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */ 502 break; 503 } 504 } else { 505 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 506 IEEE80211_ADDR_COPY(wh->i_addr1, da); 507 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 508 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 509 } 510 *(uint16_t *)&wh->i_dur[0] = 0; 511 512 seqno = ni->ni_txseqs[tid]++; 513 *(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 514 M_SEQNO_SET(m, seqno); 515 516 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 517 m->m_flags |= M_MCAST; 518#undef WH4 519} 520 521/* 522 * Send a management frame to the specified node. The node pointer 523 * must have a reference as the pointer will be passed to the driver 524 * and potentially held for a long time. If the frame is successfully 525 * dispatched to the driver, then it is responsible for freeing the 526 * reference (and potentially free'ing up any associated storage); 527 * otherwise deal with reclaiming any reference (on error). 528 */ 529int 530ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type, 531 struct ieee80211_bpf_params *params) 532{ 533 struct ieee80211vap *vap = ni->ni_vap; 534 struct ieee80211com *ic = ni->ni_ic; 535 struct ieee80211_frame *wh; 536 537 KASSERT(ni != NULL, ("null node")); 538 539 if (vap->iv_state == IEEE80211_S_CAC) { 540 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 541 ni, "block %s frame in CAC state", 542 ieee80211_mgt_subtype_name[ 543 (type & IEEE80211_FC0_SUBTYPE_MASK) >> 544 IEEE80211_FC0_SUBTYPE_SHIFT]); 545 vap->iv_stats.is_tx_badstate++; 546 ieee80211_free_node(ni); 547 m_freem(m); 548 return EIO; /* XXX */ 549 } 550 551 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 552 if (m == NULL) { 553 ieee80211_free_node(ni); 554 return ENOMEM; 555 } 556 557 wh = mtod(m, struct ieee80211_frame *); 558 ieee80211_send_setup(ni, m, 559 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID, 560 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 561 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) { 562 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1, 563 "encrypting frame (%s)", __func__); 564 wh->i_fc[1] |= IEEE80211_FC1_WEP; 565 } 566 m->m_flags |= M_ENCAP; /* mark encapsulated */ 567 568 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?")); 569 M_WME_SETAC(m, params->ibp_pri); 570 571#ifdef IEEE80211_DEBUG 572 /* avoid printing too many frames */ 573 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) || 574 ieee80211_msg_dumppkts(vap)) { 575 printf("[%s] send %s on channel %u\n", 576 ether_sprintf(wh->i_addr1), 577 ieee80211_mgt_subtype_name[ 578 (type & IEEE80211_FC0_SUBTYPE_MASK) >> 579 IEEE80211_FC0_SUBTYPE_SHIFT], 580 ieee80211_chan2ieee(ic, ic->ic_curchan)); 581 } 582#endif 583 IEEE80211_NODE_STAT(ni, tx_mgmt); 584 585 return ic->ic_raw_xmit(ni, m, params); 586} 587 588/* 589 * Send a null data frame to the specified node. If the station 590 * is setup for QoS then a QoS Null Data frame is constructed. 591 * If this is a WDS station then a 4-address frame is constructed. 592 * 593 * NB: the caller is assumed to have setup a node reference 594 * for use; this is necessary to deal with a race condition 595 * when probing for inactive stations. Like ieee80211_mgmt_output 596 * we must cleanup any node reference on error; however we 597 * can safely just unref it as we know it will never be the 598 * last reference to the node. 599 */ 600int 601ieee80211_send_nulldata(struct ieee80211_node *ni) 602{ 603 struct ieee80211vap *vap = ni->ni_vap; 604 struct ieee80211com *ic = ni->ni_ic; 605 struct mbuf *m; 606 struct ieee80211_frame *wh; 607 int hdrlen; 608 uint8_t *frm; 609 610 if (vap->iv_state == IEEE80211_S_CAC) { 611 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 612 ni, "block %s frame in CAC state", "null data"); 613 ieee80211_unref_node(&ni); 614 vap->iv_stats.is_tx_badstate++; 615 return EIO; /* XXX */ 616 } 617 618 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) 619 hdrlen = sizeof(struct ieee80211_qosframe); 620 else 621 hdrlen = sizeof(struct ieee80211_frame); 622 /* NB: only WDS vap's get 4-address frames */ 623 if (vap->iv_opmode == IEEE80211_M_WDS) 624 hdrlen += IEEE80211_ADDR_LEN; 625 if (ic->ic_flags & IEEE80211_F_DATAPAD) 626 hdrlen = roundup(hdrlen, sizeof(uint32_t)); 627 628 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0); 629 if (m == NULL) { 630 /* XXX debug msg */ 631 ieee80211_unref_node(&ni); 632 vap->iv_stats.is_tx_nobuf++; 633 return ENOMEM; 634 } 635 KASSERT(M_LEADINGSPACE(m) >= hdrlen, 636 ("leading space %zd", M_LEADINGSPACE(m))); 637 M_PREPEND(m, hdrlen, M_DONTWAIT); 638 if (m == NULL) { 639 /* NB: cannot happen */ 640 ieee80211_free_node(ni); 641 return ENOMEM; 642 } 643 644 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */ 645 if (ni->ni_flags & IEEE80211_NODE_QOS) { 646 const int tid = WME_AC_TO_TID(WME_AC_BE); 647 uint8_t *qos; 648 649 ieee80211_send_setup(ni, m, 650 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL, 651 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 652 653 if (vap->iv_opmode == IEEE80211_M_WDS) 654 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 655 else 656 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 657 qos[0] = tid & IEEE80211_QOS_TID; 658 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy) 659 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 660 qos[1] = 0; 661 } else { 662 ieee80211_send_setup(ni, m, 663 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA, 664 IEEE80211_NONQOS_TID, 665 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 666 } 667 if (vap->iv_opmode != IEEE80211_M_WDS) { 668 /* NB: power management bit is never sent by an AP */ 669 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 670 vap->iv_opmode != IEEE80211_M_HOSTAP) 671 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT; 672 } 673 m->m_len = m->m_pkthdr.len = hdrlen; 674 m->m_flags |= M_ENCAP; /* mark encapsulated */ 675 676 M_WME_SETAC(m, WME_AC_BE); 677 678 IEEE80211_NODE_STAT(ni, tx_data); 679 680 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni, 681 "send %snull data frame on channel %u, pwr mgt %s", 682 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "", 683 ieee80211_chan2ieee(ic, ic->ic_curchan), 684 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis"); 685 686 return ic->ic_raw_xmit(ni, m, NULL); 687} 688 689/* 690 * Assign priority to a frame based on any vlan tag assigned 691 * to the station and/or any Diffserv setting in an IP header. 692 * Finally, if an ACM policy is setup (in station mode) it's 693 * applied. 694 */ 695int 696ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m) 697{ 698 const struct ether_header *eh = mtod(m, struct ether_header *); 699 int v_wme_ac, d_wme_ac, ac; 700 701 /* 702 * Always promote PAE/EAPOL frames to high priority. 703 */ 704 if (eh->ether_type == htons(ETHERTYPE_PAE)) { 705 /* NB: mark so others don't need to check header */ 706 m->m_flags |= M_EAPOL; 707 ac = WME_AC_VO; 708 goto done; 709 } 710 /* 711 * Non-qos traffic goes to BE. 712 */ 713 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) { 714 ac = WME_AC_BE; 715 goto done; 716 } 717 718 /* 719 * If node has a vlan tag then all traffic 720 * to it must have a matching tag. 721 */ 722 v_wme_ac = 0; 723 if (ni->ni_vlan != 0) { 724 if ((m->m_flags & M_VLANTAG) == 0) { 725 IEEE80211_NODE_STAT(ni, tx_novlantag); 726 return 1; 727 } 728 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 729 EVL_VLANOFTAG(ni->ni_vlan)) { 730 IEEE80211_NODE_STAT(ni, tx_vlanmismatch); 731 return 1; 732 } 733 /* map vlan priority to AC */ 734 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan)); 735 } 736 737 /* XXX m_copydata may be too slow for fast path */ 738#ifdef INET 739 if (eh->ether_type == htons(ETHERTYPE_IP)) { 740 uint8_t tos; 741 /* 742 * IP frame, map the DSCP bits from the TOS field. 743 */ 744 /* NB: ip header may not be in first mbuf */ 745 m_copydata(m, sizeof(struct ether_header) + 746 offsetof(struct ip, ip_tos), sizeof(tos), &tos); 747 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */ 748 d_wme_ac = TID_TO_WME_AC(tos); 749 } else { 750#endif /* INET */ 751#ifdef INET6 752 if (eh->ether_type == htons(ETHERTYPE_IPV6)) { 753 uint32_t flow; 754 uint8_t tos; 755 /* 756 * IPv6 frame, map the DSCP bits from the TOS field. 757 */ 758 m_copydata(m, sizeof(struct ether_header) + 759 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow), 760 (caddr_t) &flow); 761 tos = (uint8_t)(ntohl(flow) >> 20); 762 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */ 763 d_wme_ac = TID_TO_WME_AC(tos); 764 } else { 765#endif /* INET6 */ 766 d_wme_ac = WME_AC_BE; 767#ifdef INET6 768 } 769#endif 770#ifdef INET 771 } 772#endif 773 /* 774 * Use highest priority AC. 775 */ 776 if (v_wme_ac > d_wme_ac) 777 ac = v_wme_ac; 778 else 779 ac = d_wme_ac; 780 781 /* 782 * Apply ACM policy. 783 */ 784 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) { 785 static const int acmap[4] = { 786 WME_AC_BK, /* WME_AC_BE */ 787 WME_AC_BK, /* WME_AC_BK */ 788 WME_AC_BE, /* WME_AC_VI */ 789 WME_AC_VI, /* WME_AC_VO */ 790 }; 791 struct ieee80211com *ic = ni->ni_ic; 792 793 while (ac != WME_AC_BK && 794 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm) 795 ac = acmap[ac]; 796 } 797done: 798 M_WME_SETAC(m, ac); 799 return 0; 800} 801 802/* 803 * Insure there is sufficient contiguous space to encapsulate the 804 * 802.11 data frame. If room isn't already there, arrange for it. 805 * Drivers and cipher modules assume we have done the necessary work 806 * and fail rudely if they don't find the space they need. 807 */ 808struct mbuf * 809ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize, 810 struct ieee80211_key *key, struct mbuf *m) 811{ 812#define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc)) 813 int needed_space = vap->iv_ic->ic_headroom + hdrsize; 814 815 if (key != NULL) { 816 /* XXX belongs in crypto code? */ 817 needed_space += key->wk_cipher->ic_header; 818 /* XXX frags */ 819 /* 820 * When crypto is being done in the host we must insure 821 * the data are writable for the cipher routines; clone 822 * a writable mbuf chain. 823 * XXX handle SWMIC specially 824 */ 825 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) { 826 m = m_unshare(m, M_NOWAIT); 827 if (m == NULL) { 828 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 829 "%s: cannot get writable mbuf\n", __func__); 830 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */ 831 return NULL; 832 } 833 } 834 } 835 /* 836 * We know we are called just before stripping an Ethernet 837 * header and prepending an LLC header. This means we know 838 * there will be 839 * sizeof(struct ether_header) - sizeof(struct llc) 840 * bytes recovered to which we need additional space for the 841 * 802.11 header and any crypto header. 842 */ 843 /* XXX check trailing space and copy instead? */ 844 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) { 845 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type); 846 if (n == NULL) { 847 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 848 "%s: cannot expand storage\n", __func__); 849 vap->iv_stats.is_tx_nobuf++; 850 m_freem(m); 851 return NULL; 852 } 853 KASSERT(needed_space <= MHLEN, 854 ("not enough room, need %u got %zu\n", needed_space, MHLEN)); 855 /* 856 * Setup new mbuf to have leading space to prepend the 857 * 802.11 header and any crypto header bits that are 858 * required (the latter are added when the driver calls 859 * back to ieee80211_crypto_encap to do crypto encapsulation). 860 */ 861 /* NB: must be first 'cuz it clobbers m_data */ 862 m_move_pkthdr(n, m); 863 n->m_len = 0; /* NB: m_gethdr does not set */ 864 n->m_data += needed_space; 865 /* 866 * Pull up Ethernet header to create the expected layout. 867 * We could use m_pullup but that's overkill (i.e. we don't 868 * need the actual data) and it cannot fail so do it inline 869 * for speed. 870 */ 871 /* NB: struct ether_header is known to be contiguous */ 872 n->m_len += sizeof(struct ether_header); 873 m->m_len -= sizeof(struct ether_header); 874 m->m_data += sizeof(struct ether_header); 875 /* 876 * Replace the head of the chain. 877 */ 878 n->m_next = m; 879 m = n; 880 } 881 return m; 882#undef TO_BE_RECLAIMED 883} 884 885/* 886 * Return the transmit key to use in sending a unicast frame. 887 * If a unicast key is set we use that. When no unicast key is set 888 * we fall back to the default transmit key. 889 */ 890static __inline struct ieee80211_key * 891ieee80211_crypto_getucastkey(struct ieee80211vap *vap, 892 struct ieee80211_node *ni) 893{ 894 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 895 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE || 896 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey])) 897 return NULL; 898 return &vap->iv_nw_keys[vap->iv_def_txkey]; 899 } else { 900 return &ni->ni_ucastkey; 901 } 902} 903 904/* 905 * Return the transmit key to use in sending a multicast frame. 906 * Multicast traffic always uses the group key which is installed as 907 * the default tx key. 908 */ 909static __inline struct ieee80211_key * 910ieee80211_crypto_getmcastkey(struct ieee80211vap *vap, 911 struct ieee80211_node *ni) 912{ 913 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE || 914 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey])) 915 return NULL; 916 return &vap->iv_nw_keys[vap->iv_def_txkey]; 917} 918 919/* 920 * Encapsulate an outbound data frame. The mbuf chain is updated. 921 * If an error is encountered NULL is returned. The caller is required 922 * to provide a node reference and pullup the ethernet header in the 923 * first mbuf. 924 * 925 * NB: Packet is assumed to be processed by ieee80211_classify which 926 * marked EAPOL frames w/ M_EAPOL. 927 */ 928struct mbuf * 929ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni, 930 struct mbuf *m) 931{ 932#define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh)) 933 struct ieee80211com *ic = ni->ni_ic; 934 struct ether_header eh; 935 struct ieee80211_frame *wh; 936 struct ieee80211_key *key; 937 struct llc *llc; 938 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr; 939 ieee80211_seq seqno; 940 941 /* 942 * Copy existing Ethernet header to a safe place. The 943 * rest of the code assumes it's ok to strip it when 944 * reorganizing state for the final encapsulation. 945 */ 946 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!")); 947 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t)); 948 949 /* 950 * Insure space for additional headers. First identify 951 * transmit key to use in calculating any buffer adjustments 952 * required. This is also used below to do privacy 953 * encapsulation work. Then calculate the 802.11 header 954 * size and any padding required by the driver. 955 * 956 * Note key may be NULL if we fall back to the default 957 * transmit key and that is not set. In that case the 958 * buffer may not be expanded as needed by the cipher 959 * routines, but they will/should discard it. 960 */ 961 if (vap->iv_flags & IEEE80211_F_PRIVACY) { 962 if (vap->iv_opmode == IEEE80211_M_STA || 963 !IEEE80211_IS_MULTICAST(eh.ether_dhost) || 964 (vap->iv_opmode == IEEE80211_M_WDS && 965 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) 966 key = ieee80211_crypto_getucastkey(vap, ni); 967 else 968 key = ieee80211_crypto_getmcastkey(vap, ni); 969 if (key == NULL && (m->m_flags & M_EAPOL) == 0) { 970 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 971 eh.ether_dhost, 972 "no default transmit key (%s) deftxkey %u", 973 __func__, vap->iv_def_txkey); 974 vap->iv_stats.is_tx_nodefkey++; 975 goto bad; 976 } 977 } else 978 key = NULL; 979 /* 980 * XXX Some ap's don't handle QoS-encapsulated EAPOL 981 * frames so suppress use. This may be an issue if other 982 * ap's require all data frames to be QoS-encapsulated 983 * once negotiated in which case we'll need to make this 984 * configurable. 985 */ 986 addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) && 987 (m->m_flags & M_EAPOL) == 0; 988 if (addqos) 989 hdrsize = sizeof(struct ieee80211_qosframe); 990 else 991 hdrsize = sizeof(struct ieee80211_frame); 992 /* 993 * 4-address frames need to be generated for: 994 * o packets sent through a WDS vap (IEEE80211_M_WDS) 995 * o packets sent through a vap marked for relaying 996 * (e.g. a station operating with dynamic WDS) 997 */ 998 is4addr = vap->iv_opmode == IEEE80211_M_WDS || 999 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) && 1000 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)); 1001 if (is4addr) 1002 hdrsize += IEEE80211_ADDR_LEN; 1003 /* 1004 * Honor driver DATAPAD requirement. 1005 */ 1006 if (ic->ic_flags & IEEE80211_F_DATAPAD) 1007 hdrspace = roundup(hdrsize, sizeof(uint32_t)); 1008 else 1009 hdrspace = hdrsize; 1010 1011 if (__predict_true((m->m_flags & M_FF) == 0)) { 1012 /* 1013 * Normal frame. 1014 */ 1015 m = ieee80211_mbuf_adjust(vap, hdrspace, key, m); 1016 if (m == NULL) { 1017 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 1018 goto bad; 1019 } 1020 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */ 1021 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 1022 llc = mtod(m, struct llc *); 1023 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 1024 llc->llc_control = LLC_UI; 1025 llc->llc_snap.org_code[0] = 0; 1026 llc->llc_snap.org_code[1] = 0; 1027 llc->llc_snap.org_code[2] = 0; 1028 llc->llc_snap.ether_type = eh.ether_type; 1029 } else { 1030#ifdef IEEE80211_SUPPORT_SUPERG 1031 /* 1032 * Aggregated frame. 1033 */ 1034 m = ieee80211_ff_encap(vap, m, hdrspace, key); 1035 if (m == NULL) 1036#endif 1037 goto bad; 1038 } 1039 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */ 1040 1041 M_PREPEND(m, hdrspace, M_DONTWAIT); 1042 if (m == NULL) { 1043 vap->iv_stats.is_tx_nobuf++; 1044 goto bad; 1045 } 1046 wh = mtod(m, struct ieee80211_frame *); 1047 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 1048 *(uint16_t *)wh->i_dur = 0; 1049 if (is4addr) { 1050 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 1051 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr); 1052 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1053 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 1054 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost); 1055 } else switch (vap->iv_opmode) { 1056 case IEEE80211_M_STA: 1057 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 1058 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 1059 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 1060 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 1061 break; 1062 case IEEE80211_M_IBSS: 1063 case IEEE80211_M_AHDEMO: 1064 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1065 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1066 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 1067 /* 1068 * NB: always use the bssid from iv_bss as the 1069 * neighbor's may be stale after an ibss merge 1070 */ 1071 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid); 1072 break; 1073 case IEEE80211_M_HOSTAP: 1074 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 1075 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1076 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 1077 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 1078 break; 1079 case IEEE80211_M_MONITOR: 1080 case IEEE80211_M_WDS: /* NB: is4addr should always be true */ 1081 goto bad; 1082 } 1083 if (m->m_flags & M_MORE_DATA) 1084 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 1085 if (addqos) { 1086 uint8_t *qos; 1087 int ac, tid; 1088 1089 if (is4addr) { 1090 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 1091 } else 1092 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 1093 ac = M_WME_GETAC(m); 1094 /* map from access class/queue to 11e header priorty value */ 1095 tid = WME_AC_TO_TID(ac); 1096 qos[0] = tid & IEEE80211_QOS_TID; 1097 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy) 1098 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 1099 qos[1] = 0; 1100 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS; 1101 1102 if ((m->m_flags & M_AMPDU_MPDU) == 0) { 1103 /* 1104 * NB: don't assign a sequence # to potential 1105 * aggregates; we expect this happens at the 1106 * point the frame comes off any aggregation q 1107 * as otherwise we may introduce holes in the 1108 * BA sequence space and/or make window accouting 1109 * more difficult. 1110 * 1111 * XXX may want to control this with a driver 1112 * capability; this may also change when we pull 1113 * aggregation up into net80211 1114 */ 1115 seqno = ni->ni_txseqs[tid]++; 1116 *(uint16_t *)wh->i_seq = 1117 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 1118 M_SEQNO_SET(m, seqno); 1119 } 1120 } else { 1121 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 1122 *(uint16_t *)wh->i_seq = 1123 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 1124 M_SEQNO_SET(m, seqno); 1125 } 1126 1127 /* check if xmit fragmentation is required */ 1128 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold && 1129 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 1130 (vap->iv_caps & IEEE80211_C_TXFRAG) && 1131 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0); 1132 if (key != NULL) { 1133 /* 1134 * IEEE 802.1X: send EAPOL frames always in the clear. 1135 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set. 1136 */ 1137 if ((m->m_flags & M_EAPOL) == 0 || 1138 ((vap->iv_flags & IEEE80211_F_WPA) && 1139 (vap->iv_opmode == IEEE80211_M_STA ? 1140 !IEEE80211_KEY_UNDEFINED(key) : 1141 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) { 1142 wh->i_fc[1] |= IEEE80211_FC1_WEP; 1143 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) { 1144 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, 1145 eh.ether_dhost, 1146 "%s", "enmic failed, discard frame"); 1147 vap->iv_stats.is_crypto_enmicfail++; 1148 goto bad; 1149 } 1150 } 1151 } 1152 if (txfrag && !ieee80211_fragment(vap, m, hdrsize, 1153 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold)) 1154 goto bad; 1155 1156 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1157 1158 IEEE80211_NODE_STAT(ni, tx_data); 1159 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1160 IEEE80211_NODE_STAT(ni, tx_mcast); 1161 m->m_flags |= M_MCAST; 1162 } else 1163 IEEE80211_NODE_STAT(ni, tx_ucast); 1164 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen); 1165 1166 return m; 1167bad: 1168 if (m != NULL) 1169 m_freem(m); 1170 return NULL; 1171#undef WH4 1172} 1173 1174/* 1175 * Fragment the frame according to the specified mtu. 1176 * The size of the 802.11 header (w/o padding) is provided 1177 * so we don't need to recalculate it. We create a new 1178 * mbuf for each fragment and chain it through m_nextpkt; 1179 * we might be able to optimize this by reusing the original 1180 * packet's mbufs but that is significantly more complicated. 1181 */ 1182static int 1183ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0, 1184 u_int hdrsize, u_int ciphdrsize, u_int mtu) 1185{ 1186 struct ieee80211_frame *wh, *whf; 1187 struct mbuf *m, *prev, *next; 1188 u_int totalhdrsize, fragno, fragsize, off, remainder, payload; 1189 1190 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?")); 1191 KASSERT(m0->m_pkthdr.len > mtu, 1192 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu)); 1193 1194 wh = mtod(m0, struct ieee80211_frame *); 1195 /* NB: mark the first frag; it will be propagated below */ 1196 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG; 1197 totalhdrsize = hdrsize + ciphdrsize; 1198 fragno = 1; 1199 off = mtu - ciphdrsize; 1200 remainder = m0->m_pkthdr.len - off; 1201 prev = m0; 1202 do { 1203 fragsize = totalhdrsize + remainder; 1204 if (fragsize > mtu) 1205 fragsize = mtu; 1206 /* XXX fragsize can be >2048! */ 1207 KASSERT(fragsize < MCLBYTES, 1208 ("fragment size %u too big!", fragsize)); 1209 if (fragsize > MHLEN) 1210 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1211 else 1212 m = m_gethdr(M_DONTWAIT, MT_DATA); 1213 if (m == NULL) 1214 goto bad; 1215 /* leave room to prepend any cipher header */ 1216 m_align(m, fragsize - ciphdrsize); 1217 1218 /* 1219 * Form the header in the fragment. Note that since 1220 * we mark the first fragment with the MORE_FRAG bit 1221 * it automatically is propagated to each fragment; we 1222 * need only clear it on the last fragment (done below). 1223 */ 1224 whf = mtod(m, struct ieee80211_frame *); 1225 memcpy(whf, wh, hdrsize); 1226 *(uint16_t *)&whf->i_seq[0] |= htole16( 1227 (fragno & IEEE80211_SEQ_FRAG_MASK) << 1228 IEEE80211_SEQ_FRAG_SHIFT); 1229 fragno++; 1230 1231 payload = fragsize - totalhdrsize; 1232 /* NB: destination is known to be contiguous */ 1233 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize); 1234 m->m_len = hdrsize + payload; 1235 m->m_pkthdr.len = hdrsize + payload; 1236 m->m_flags |= M_FRAG; 1237 1238 /* chain up the fragment */ 1239 prev->m_nextpkt = m; 1240 prev = m; 1241 1242 /* deduct fragment just formed */ 1243 remainder -= payload; 1244 off += payload; 1245 } while (remainder != 0); 1246 1247 /* set the last fragment */ 1248 m->m_flags |= M_LASTFRAG; 1249 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG; 1250 1251 /* strip first mbuf now that everything has been copied */ 1252 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize))); 1253 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 1254 1255 vap->iv_stats.is_tx_fragframes++; 1256 vap->iv_stats.is_tx_frags += fragno-1; 1257 1258 return 1; 1259bad: 1260 /* reclaim fragments but leave original frame for caller to free */ 1261 for (m = m0->m_nextpkt; m != NULL; m = next) { 1262 next = m->m_nextpkt; 1263 m->m_nextpkt = NULL; /* XXX paranoid */ 1264 m_freem(m); 1265 } 1266 m0->m_nextpkt = NULL; 1267 return 0; 1268} 1269 1270/* 1271 * Add a supported rates element id to a frame. 1272 */ 1273static uint8_t * 1274ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs) 1275{ 1276 int nrates; 1277 1278 *frm++ = IEEE80211_ELEMID_RATES; 1279 nrates = rs->rs_nrates; 1280 if (nrates > IEEE80211_RATE_SIZE) 1281 nrates = IEEE80211_RATE_SIZE; 1282 *frm++ = nrates; 1283 memcpy(frm, rs->rs_rates, nrates); 1284 return frm + nrates; 1285} 1286 1287/* 1288 * Add an extended supported rates element id to a frame. 1289 */ 1290static uint8_t * 1291ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs) 1292{ 1293 /* 1294 * Add an extended supported rates element if operating in 11g mode. 1295 */ 1296 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 1297 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 1298 *frm++ = IEEE80211_ELEMID_XRATES; 1299 *frm++ = nrates; 1300 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 1301 frm += nrates; 1302 } 1303 return frm; 1304} 1305 1306/* 1307 * Add an ssid element to a frame. 1308 */ 1309static uint8_t * 1310ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len) 1311{ 1312 *frm++ = IEEE80211_ELEMID_SSID; 1313 *frm++ = len; 1314 memcpy(frm, ssid, len); 1315 return frm + len; 1316} 1317 1318/* 1319 * Add an erp element to a frame. 1320 */ 1321static uint8_t * 1322ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic) 1323{ 1324 uint8_t erp; 1325 1326 *frm++ = IEEE80211_ELEMID_ERP; 1327 *frm++ = 1; 1328 erp = 0; 1329 if (ic->ic_nonerpsta != 0) 1330 erp |= IEEE80211_ERP_NON_ERP_PRESENT; 1331 if (ic->ic_flags & IEEE80211_F_USEPROT) 1332 erp |= IEEE80211_ERP_USE_PROTECTION; 1333 if (ic->ic_flags & IEEE80211_F_USEBARKER) 1334 erp |= IEEE80211_ERP_LONG_PREAMBLE; 1335 *frm++ = erp; 1336 return frm; 1337} 1338 1339/* 1340 * Add a CFParams element to a frame. 1341 */ 1342static uint8_t * 1343ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic) 1344{ 1345#define ADDSHORT(frm, v) do { \ 1346 frm[0] = (v) & 0xff; \ 1347 frm[1] = (v) >> 8; \ 1348 frm += 2; \ 1349} while (0) 1350 *frm++ = IEEE80211_ELEMID_CFPARMS; 1351 *frm++ = 6; 1352 *frm++ = 0; /* CFP count */ 1353 *frm++ = 2; /* CFP period */ 1354 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */ 1355 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */ 1356 return frm; 1357#undef ADDSHORT 1358} 1359 1360static __inline uint8_t * 1361add_appie(uint8_t *frm, const struct ieee80211_appie *ie) 1362{ 1363 memcpy(frm, ie->ie_data, ie->ie_len); 1364 return frm + ie->ie_len; 1365} 1366 1367static __inline uint8_t * 1368add_ie(uint8_t *frm, const uint8_t *ie) 1369{ 1370 memcpy(frm, ie, 2 + ie[1]); 1371 return frm + 2 + ie[1]; 1372} 1373 1374#define WME_OUI_BYTES 0x00, 0x50, 0xf2 1375/* 1376 * Add a WME information element to a frame. 1377 */ 1378static uint8_t * 1379ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme) 1380{ 1381 static const struct ieee80211_wme_info info = { 1382 .wme_id = IEEE80211_ELEMID_VENDOR, 1383 .wme_len = sizeof(struct ieee80211_wme_info) - 2, 1384 .wme_oui = { WME_OUI_BYTES }, 1385 .wme_type = WME_OUI_TYPE, 1386 .wme_subtype = WME_INFO_OUI_SUBTYPE, 1387 .wme_version = WME_VERSION, 1388 .wme_info = 0, 1389 }; 1390 memcpy(frm, &info, sizeof(info)); 1391 return frm + sizeof(info); 1392} 1393 1394/* 1395 * Add a WME parameters element to a frame. 1396 */ 1397static uint8_t * 1398ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme) 1399{ 1400#define SM(_v, _f) (((_v) << _f##_S) & _f) 1401#define ADDSHORT(frm, v) do { \ 1402 frm[0] = (v) & 0xff; \ 1403 frm[1] = (v) >> 8; \ 1404 frm += 2; \ 1405} while (0) 1406 /* NB: this works 'cuz a param has an info at the front */ 1407 static const struct ieee80211_wme_info param = { 1408 .wme_id = IEEE80211_ELEMID_VENDOR, 1409 .wme_len = sizeof(struct ieee80211_wme_param) - 2, 1410 .wme_oui = { WME_OUI_BYTES }, 1411 .wme_type = WME_OUI_TYPE, 1412 .wme_subtype = WME_PARAM_OUI_SUBTYPE, 1413 .wme_version = WME_VERSION, 1414 }; 1415 int i; 1416 1417 memcpy(frm, ¶m, sizeof(param)); 1418 frm += __offsetof(struct ieee80211_wme_info, wme_info); 1419 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */ 1420 *frm++ = 0; /* reserved field */ 1421 for (i = 0; i < WME_NUM_AC; i++) { 1422 const struct wmeParams *ac = 1423 &wme->wme_bssChanParams.cap_wmeParams[i]; 1424 *frm++ = SM(i, WME_PARAM_ACI) 1425 | SM(ac->wmep_acm, WME_PARAM_ACM) 1426 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN) 1427 ; 1428 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX) 1429 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN) 1430 ; 1431 ADDSHORT(frm, ac->wmep_txopLimit); 1432 } 1433 return frm; 1434#undef SM 1435#undef ADDSHORT 1436} 1437#undef WME_OUI_BYTES 1438 1439/* 1440 * Add an 11h Power Constraint element to a frame. 1441 */ 1442static uint8_t * 1443ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap) 1444{ 1445 const struct ieee80211_channel *c = vap->iv_bss->ni_chan; 1446 /* XXX per-vap tx power limit? */ 1447 int8_t limit = vap->iv_ic->ic_txpowlimit / 2; 1448 1449 frm[0] = IEEE80211_ELEMID_PWRCNSTR; 1450 frm[1] = 1; 1451 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0; 1452 return frm + 3; 1453} 1454 1455/* 1456 * Add an 11h Power Capability element to a frame. 1457 */ 1458static uint8_t * 1459ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c) 1460{ 1461 frm[0] = IEEE80211_ELEMID_PWRCAP; 1462 frm[1] = 2; 1463 frm[2] = c->ic_minpower; 1464 frm[3] = c->ic_maxpower; 1465 return frm + 4; 1466} 1467 1468/* 1469 * Add an 11h Supported Channels element to a frame. 1470 */ 1471static uint8_t * 1472ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic) 1473{ 1474 static const int ielen = 26; 1475 1476 frm[0] = IEEE80211_ELEMID_SUPPCHAN; 1477 frm[1] = ielen; 1478 /* XXX not correct */ 1479 memcpy(frm+2, ic->ic_chan_avail, ielen); 1480 return frm + 2 + ielen; 1481} 1482 1483/* 1484 * Add an 11h Channel Switch Announcement element to a frame. 1485 * Note that we use the per-vap CSA count to adjust the global 1486 * counter so we can use this routine to form probe response 1487 * frames and get the current count. 1488 */ 1489static uint8_t * 1490ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap) 1491{ 1492 struct ieee80211com *ic = vap->iv_ic; 1493 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm; 1494 1495 csa->csa_ie = IEEE80211_ELEMID_CSA; 1496 csa->csa_len = 3; 1497 csa->csa_mode = 1; /* XXX force quiet on channel */ 1498 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan); 1499 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count; 1500 return frm + sizeof(*csa); 1501} 1502 1503/* 1504 * Add an 11h country information element to a frame. 1505 */ 1506static uint8_t * 1507ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic) 1508{ 1509 1510 if (ic->ic_countryie == NULL || 1511 ic->ic_countryie_chan != ic->ic_bsschan) { 1512 /* 1513 * Handle lazy construction of ie. This is done on 1514 * first use and after a channel change that requires 1515 * re-calculation. 1516 */ 1517 if (ic->ic_countryie != NULL) 1518 free(ic->ic_countryie, M_80211_NODE_IE); 1519 ic->ic_countryie = ieee80211_alloc_countryie(ic); 1520 if (ic->ic_countryie == NULL) 1521 return frm; 1522 ic->ic_countryie_chan = ic->ic_bsschan; 1523 } 1524 return add_appie(frm, ic->ic_countryie); 1525} 1526 1527/* 1528 * Send a probe request frame with the specified ssid 1529 * and any optional information element data. 1530 */ 1531int 1532ieee80211_send_probereq(struct ieee80211_node *ni, 1533 const uint8_t sa[IEEE80211_ADDR_LEN], 1534 const uint8_t da[IEEE80211_ADDR_LEN], 1535 const uint8_t bssid[IEEE80211_ADDR_LEN], 1536 const uint8_t *ssid, size_t ssidlen) 1537{ 1538 struct ieee80211vap *vap = ni->ni_vap; 1539 struct ieee80211com *ic = ni->ni_ic; 1540 const struct ieee80211_txparam *tp; 1541 struct ieee80211_bpf_params params; 1542 struct ieee80211_frame *wh; 1543 const struct ieee80211_rateset *rs; 1544 struct mbuf *m; 1545 uint8_t *frm; 1546 1547 if (vap->iv_state == IEEE80211_S_CAC) { 1548 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni, 1549 "block %s frame in CAC state", "probe request"); 1550 vap->iv_stats.is_tx_badstate++; 1551 return EIO; /* XXX */ 1552 } 1553 1554 /* 1555 * Hold a reference on the node so it doesn't go away until after 1556 * the xmit is complete all the way in the driver. On error we 1557 * will remove our reference. 1558 */ 1559 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1560 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1561 __func__, __LINE__, 1562 ni, ether_sprintf(ni->ni_macaddr), 1563 ieee80211_node_refcnt(ni)+1); 1564 ieee80211_ref_node(ni); 1565 1566 /* 1567 * prreq frame format 1568 * [tlv] ssid 1569 * [tlv] supported rates 1570 * [tlv] RSN (optional) 1571 * [tlv] extended supported rates 1572 * [tlv] WPA (optional) 1573 * [tlv] user-specified ie's 1574 */ 1575 m = ieee80211_getmgtframe(&frm, 1576 ic->ic_headroom + sizeof(struct ieee80211_frame), 1577 2 + IEEE80211_NWID_LEN 1578 + 2 + IEEE80211_RATE_SIZE 1579 + sizeof(struct ieee80211_ie_wpa) 1580 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1581 + sizeof(struct ieee80211_ie_wpa) 1582 + (vap->iv_appie_probereq != NULL ? 1583 vap->iv_appie_probereq->ie_len : 0) 1584 ); 1585 if (m == NULL) { 1586 vap->iv_stats.is_tx_nobuf++; 1587 ieee80211_free_node(ni); 1588 return ENOMEM; 1589 } 1590 1591 frm = ieee80211_add_ssid(frm, ssid, ssidlen); 1592 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 1593 frm = ieee80211_add_rates(frm, rs); 1594 if (vap->iv_flags & IEEE80211_F_WPA2) { 1595 if (vap->iv_rsn_ie != NULL) 1596 frm = add_ie(frm, vap->iv_rsn_ie); 1597 /* XXX else complain? */ 1598 } 1599 frm = ieee80211_add_xrates(frm, rs); 1600 if (vap->iv_flags & IEEE80211_F_WPA1) { 1601 if (vap->iv_wpa_ie != NULL) 1602 frm = add_ie(frm, vap->iv_wpa_ie); 1603 /* XXX else complain? */ 1604 } 1605 if (vap->iv_appie_probereq != NULL) 1606 frm = add_appie(frm, vap->iv_appie_probereq); 1607 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 1608 1609 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame), 1610 ("leading space %zd", M_LEADINGSPACE(m))); 1611 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1612 if (m == NULL) { 1613 /* NB: cannot happen */ 1614 ieee80211_free_node(ni); 1615 return ENOMEM; 1616 } 1617 1618 wh = mtod(m, struct ieee80211_frame *); 1619 ieee80211_send_setup(ni, m, 1620 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ, 1621 IEEE80211_NONQOS_TID, sa, da, bssid); 1622 /* XXX power management? */ 1623 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1624 1625 M_WME_SETAC(m, WME_AC_BE); 1626 1627 IEEE80211_NODE_STAT(ni, tx_probereq); 1628 IEEE80211_NODE_STAT(ni, tx_mgmt); 1629 1630 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 1631 "send probe req on channel %u bssid %s ssid \"%.*s\"\n", 1632 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid), 1633 ssidlen, ssid); 1634 1635 memset(¶ms, 0, sizeof(params)); 1636 params.ibp_pri = M_WME_GETAC(m); 1637 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1638 params.ibp_rate0 = tp->mgmtrate; 1639 if (IEEE80211_IS_MULTICAST(da)) { 1640 params.ibp_flags |= IEEE80211_BPF_NOACK; 1641 params.ibp_try0 = 1; 1642 } else 1643 params.ibp_try0 = tp->maxretry; 1644 params.ibp_power = ni->ni_txpower; 1645 return ic->ic_raw_xmit(ni, m, ¶ms); 1646} 1647 1648/* 1649 * Calculate capability information for mgt frames. 1650 */ 1651static uint16_t 1652getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan) 1653{ 1654 struct ieee80211com *ic = vap->iv_ic; 1655 uint16_t capinfo; 1656 1657 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode")); 1658 1659 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 1660 capinfo = IEEE80211_CAPINFO_ESS; 1661 else if (vap->iv_opmode == IEEE80211_M_IBSS) 1662 capinfo = IEEE80211_CAPINFO_IBSS; 1663 else 1664 capinfo = 0; 1665 if (vap->iv_flags & IEEE80211_F_PRIVACY) 1666 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1667 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1668 IEEE80211_IS_CHAN_2GHZ(chan)) 1669 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1670 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1671 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1672 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH)) 1673 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT; 1674 return capinfo; 1675} 1676 1677/* 1678 * Send a management frame. The node is for the destination (or ic_bss 1679 * when in station mode). Nodes other than ic_bss have their reference 1680 * count bumped to reflect our use for an indeterminant time. 1681 */ 1682int 1683ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg) 1684{ 1685#define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT) 1686#define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0) 1687 struct ieee80211vap *vap = ni->ni_vap; 1688 struct ieee80211com *ic = ni->ni_ic; 1689 struct ieee80211_node *bss = vap->iv_bss; 1690 struct ieee80211_bpf_params params; 1691 struct mbuf *m; 1692 uint8_t *frm; 1693 uint16_t capinfo; 1694 int has_challenge, is_shared_key, ret, status; 1695 1696 KASSERT(ni != NULL, ("null node")); 1697 1698 /* 1699 * Hold a reference on the node so it doesn't go away until after 1700 * the xmit is complete all the way in the driver. On error we 1701 * will remove our reference. 1702 */ 1703 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1704 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 1705 __func__, __LINE__, 1706 ni, ether_sprintf(ni->ni_macaddr), 1707 ieee80211_node_refcnt(ni)+1); 1708 ieee80211_ref_node(ni); 1709 1710 memset(¶ms, 0, sizeof(params)); 1711 switch (type) { 1712 1713 case IEEE80211_FC0_SUBTYPE_AUTH: 1714 status = arg >> 16; 1715 arg &= 0xffff; 1716 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE || 1717 arg == IEEE80211_AUTH_SHARED_RESPONSE) && 1718 ni->ni_challenge != NULL); 1719 1720 /* 1721 * Deduce whether we're doing open authentication or 1722 * shared key authentication. We do the latter if 1723 * we're in the middle of a shared key authentication 1724 * handshake or if we're initiating an authentication 1725 * request and configured to use shared key. 1726 */ 1727 is_shared_key = has_challenge || 1728 arg >= IEEE80211_AUTH_SHARED_RESPONSE || 1729 (arg == IEEE80211_AUTH_SHARED_REQUEST && 1730 bss->ni_authmode == IEEE80211_AUTH_SHARED); 1731 1732 m = ieee80211_getmgtframe(&frm, 1733 ic->ic_headroom + sizeof(struct ieee80211_frame), 1734 3 * sizeof(uint16_t) 1735 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ? 1736 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0) 1737 ); 1738 if (m == NULL) 1739 senderr(ENOMEM, is_tx_nobuf); 1740 1741 ((uint16_t *)frm)[0] = 1742 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED) 1743 : htole16(IEEE80211_AUTH_ALG_OPEN); 1744 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */ 1745 ((uint16_t *)frm)[2] = htole16(status);/* status */ 1746 1747 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) { 1748 ((uint16_t *)frm)[3] = 1749 htole16((IEEE80211_CHALLENGE_LEN << 8) | 1750 IEEE80211_ELEMID_CHALLENGE); 1751 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge, 1752 IEEE80211_CHALLENGE_LEN); 1753 m->m_pkthdr.len = m->m_len = 1754 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN; 1755 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) { 1756 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 1757 "request encrypt frame (%s)", __func__); 1758 /* mark frame for encryption */ 1759 params.ibp_flags |= IEEE80211_BPF_CRYPTO; 1760 } 1761 } else 1762 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t); 1763 1764 /* XXX not right for shared key */ 1765 if (status == IEEE80211_STATUS_SUCCESS) 1766 IEEE80211_NODE_STAT(ni, tx_auth); 1767 else 1768 IEEE80211_NODE_STAT(ni, tx_auth_fail); 1769 1770 if (vap->iv_opmode == IEEE80211_M_STA) 1771 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 1772 (void *) vap->iv_state); 1773 break; 1774 1775 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1776 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 1777 "send station deauthenticate (reason %d)", arg); 1778 m = ieee80211_getmgtframe(&frm, 1779 ic->ic_headroom + sizeof(struct ieee80211_frame), 1780 sizeof(uint16_t)); 1781 if (m == NULL) 1782 senderr(ENOMEM, is_tx_nobuf); 1783 *(uint16_t *)frm = htole16(arg); /* reason */ 1784 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 1785 1786 IEEE80211_NODE_STAT(ni, tx_deauth); 1787 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg); 1788 1789 ieee80211_node_unauthorize(ni); /* port closed */ 1790 break; 1791 1792 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1793 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1794 /* 1795 * asreq frame format 1796 * [2] capability information 1797 * [2] listen interval 1798 * [6*] current AP address (reassoc only) 1799 * [tlv] ssid 1800 * [tlv] supported rates 1801 * [tlv] extended supported rates 1802 * [4] power capability (optional) 1803 * [28] supported channels (optional) 1804 * [tlv] HT capabilities 1805 * [tlv] WME (optional) 1806 * [tlv] Vendor OUI HT capabilities (optional) 1807 * [tlv] Atheros capabilities (if negotiated) 1808 * [tlv] AppIE's (optional) 1809 */ 1810 m = ieee80211_getmgtframe(&frm, 1811 ic->ic_headroom + sizeof(struct ieee80211_frame), 1812 sizeof(uint16_t) 1813 + sizeof(uint16_t) 1814 + IEEE80211_ADDR_LEN 1815 + 2 + IEEE80211_NWID_LEN 1816 + 2 + IEEE80211_RATE_SIZE 1817 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1818 + 4 1819 + 2 + 26 1820 + sizeof(struct ieee80211_wme_info) 1821 + sizeof(struct ieee80211_ie_htcap) 1822 + 4 + sizeof(struct ieee80211_ie_htcap) 1823#ifdef IEEE80211_SUPPORT_SUPERG 1824 + sizeof(struct ieee80211_ath_ie) 1825#endif 1826 + (vap->iv_appie_wpa != NULL ? 1827 vap->iv_appie_wpa->ie_len : 0) 1828 + (vap->iv_appie_assocreq != NULL ? 1829 vap->iv_appie_assocreq->ie_len : 0) 1830 ); 1831 if (m == NULL) 1832 senderr(ENOMEM, is_tx_nobuf); 1833 1834 KASSERT(vap->iv_opmode == IEEE80211_M_STA, 1835 ("wrong mode %u", vap->iv_opmode)); 1836 capinfo = IEEE80211_CAPINFO_ESS; 1837 if (vap->iv_flags & IEEE80211_F_PRIVACY) 1838 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1839 /* 1840 * NB: Some 11a AP's reject the request when 1841 * short premable is set. 1842 */ 1843 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1844 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 1845 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1846 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) && 1847 (ic->ic_caps & IEEE80211_C_SHSLOT)) 1848 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1849 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) && 1850 (vap->iv_flags & IEEE80211_F_DOTH)) 1851 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT; 1852 *(uint16_t *)frm = htole16(capinfo); 1853 frm += 2; 1854 1855 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!")); 1856 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval, 1857 bss->ni_intval)); 1858 frm += 2; 1859 1860 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 1861 IEEE80211_ADDR_COPY(frm, bss->ni_bssid); 1862 frm += IEEE80211_ADDR_LEN; 1863 } 1864 1865 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 1866 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1867 if (vap->iv_flags & IEEE80211_F_WPA2) { 1868 if (vap->iv_rsn_ie != NULL) 1869 frm = add_ie(frm, vap->iv_rsn_ie); 1870 /* XXX else complain? */ 1871 } 1872 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1873 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) { 1874 frm = ieee80211_add_powercapability(frm, 1875 ic->ic_curchan); 1876 frm = ieee80211_add_supportedchannels(frm, ic); 1877 } 1878 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) && 1879 ni->ni_ies.htcap_ie != NULL && 1880 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) 1881 frm = ieee80211_add_htcap(frm, ni); 1882 if (vap->iv_flags & IEEE80211_F_WPA1) { 1883 if (vap->iv_wpa_ie != NULL) 1884 frm = add_ie(frm, vap->iv_wpa_ie); 1885 /* XXX else complain */ 1886 } 1887 if ((ic->ic_flags & IEEE80211_F_WME) && 1888 ni->ni_ies.wme_ie != NULL) 1889 frm = ieee80211_add_wme_info(frm, &ic->ic_wme); 1890 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) && 1891 ni->ni_ies.htcap_ie != NULL && 1892 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) 1893 frm = ieee80211_add_htcap_vendor(frm, ni); 1894#ifdef IEEE80211_SUPPORT_SUPERG 1895 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) { 1896 frm = ieee80211_add_ath(frm, 1897 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS), 1898 ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 1899 ni->ni_authmode != IEEE80211_AUTH_8021X) ? 1900 vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 1901 } 1902#endif /* IEEE80211_SUPPORT_SUPERG */ 1903 if (vap->iv_appie_assocreq != NULL) 1904 frm = add_appie(frm, vap->iv_appie_assocreq); 1905 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 1906 1907 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 1908 (void *) vap->iv_state); 1909 break; 1910 1911 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 1912 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 1913 /* 1914 * asresp frame format 1915 * [2] capability information 1916 * [2] status 1917 * [2] association ID 1918 * [tlv] supported rates 1919 * [tlv] extended supported rates 1920 * [tlv] HT capabilities (standard, if STA enabled) 1921 * [tlv] HT information (standard, if STA enabled) 1922 * [tlv] WME (if configured and STA enabled) 1923 * [tlv] HT capabilities (vendor OUI, if STA enabled) 1924 * [tlv] HT information (vendor OUI, if STA enabled) 1925 * [tlv] Atheros capabilities (if STA enabled) 1926 * [tlv] AppIE's (optional) 1927 */ 1928 m = ieee80211_getmgtframe(&frm, 1929 ic->ic_headroom + sizeof(struct ieee80211_frame), 1930 sizeof(uint16_t) 1931 + sizeof(uint16_t) 1932 + sizeof(uint16_t) 1933 + 2 + IEEE80211_RATE_SIZE 1934 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1935 + sizeof(struct ieee80211_ie_htcap) + 4 1936 + sizeof(struct ieee80211_ie_htinfo) + 4 1937 + sizeof(struct ieee80211_wme_param) 1938#ifdef IEEE80211_SUPPORT_SUPERG 1939 + sizeof(struct ieee80211_ath_ie) 1940#endif 1941 + (vap->iv_appie_assocresp != NULL ? 1942 vap->iv_appie_assocresp->ie_len : 0) 1943 ); 1944 if (m == NULL) 1945 senderr(ENOMEM, is_tx_nobuf); 1946 1947 capinfo = getcapinfo(vap, bss->ni_chan); 1948 *(uint16_t *)frm = htole16(capinfo); 1949 frm += 2; 1950 1951 *(uint16_t *)frm = htole16(arg); /* status */ 1952 frm += 2; 1953 1954 if (arg == IEEE80211_STATUS_SUCCESS) { 1955 *(uint16_t *)frm = htole16(ni->ni_associd); 1956 IEEE80211_NODE_STAT(ni, tx_assoc); 1957 } else 1958 IEEE80211_NODE_STAT(ni, tx_assoc_fail); 1959 frm += 2; 1960 1961 frm = ieee80211_add_rates(frm, &ni->ni_rates); 1962 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 1963 /* NB: respond according to what we received */ 1964 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) { 1965 frm = ieee80211_add_htcap(frm, ni); 1966 frm = ieee80211_add_htinfo(frm, ni); 1967 } 1968 if ((vap->iv_flags & IEEE80211_F_WME) && 1969 ni->ni_ies.wme_ie != NULL) 1970 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 1971 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) { 1972 frm = ieee80211_add_htcap_vendor(frm, ni); 1973 frm = ieee80211_add_htinfo_vendor(frm, ni); 1974 } 1975#ifdef IEEE80211_SUPPORT_SUPERG 1976 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) 1977 frm = ieee80211_add_ath(frm, 1978 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS), 1979 ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 1980 ni->ni_authmode != IEEE80211_AUTH_8021X) ? 1981 vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 1982#endif /* IEEE80211_SUPPORT_SUPERG */ 1983 if (vap->iv_appie_assocresp != NULL) 1984 frm = add_appie(frm, vap->iv_appie_assocresp); 1985 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 1986 break; 1987 1988 case IEEE80211_FC0_SUBTYPE_DISASSOC: 1989 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 1990 "send station disassociate (reason %d)", arg); 1991 m = ieee80211_getmgtframe(&frm, 1992 ic->ic_headroom + sizeof(struct ieee80211_frame), 1993 sizeof(uint16_t)); 1994 if (m == NULL) 1995 senderr(ENOMEM, is_tx_nobuf); 1996 *(uint16_t *)frm = htole16(arg); /* reason */ 1997 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 1998 1999 IEEE80211_NODE_STAT(ni, tx_disassoc); 2000 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg); 2001 break; 2002 2003 default: 2004 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni, 2005 "invalid mgmt frame type %u", type); 2006 senderr(EINVAL, is_tx_unknownmgt); 2007 /* NOTREACHED */ 2008 } 2009 2010 /* NB: force non-ProbeResp frames to the highest queue */ 2011 params.ibp_pri = WME_AC_VO; 2012 params.ibp_rate0 = bss->ni_txparms->mgmtrate; 2013 /* NB: we know all frames are unicast */ 2014 params.ibp_try0 = bss->ni_txparms->maxretry; 2015 params.ibp_power = bss->ni_txpower; 2016 return ieee80211_mgmt_output(ni, m, type, ¶ms); 2017bad: 2018 ieee80211_free_node(ni); 2019 return ret; 2020#undef senderr 2021#undef HTFLAGS 2022} 2023 2024/* 2025 * Return an mbuf with a probe response frame in it. 2026 * Space is left to prepend and 802.11 header at the 2027 * front but it's left to the caller to fill in. 2028 */ 2029struct mbuf * 2030ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy) 2031{ 2032 struct ieee80211vap *vap = bss->ni_vap; 2033 struct ieee80211com *ic = bss->ni_ic; 2034 const struct ieee80211_rateset *rs; 2035 struct mbuf *m; 2036 uint16_t capinfo; 2037 uint8_t *frm; 2038 2039 /* 2040 * probe response frame format 2041 * [8] time stamp 2042 * [2] beacon interval 2043 * [2] cabability information 2044 * [tlv] ssid 2045 * [tlv] supported rates 2046 * [tlv] parameter set (FH/DS) 2047 * [tlv] parameter set (IBSS) 2048 * [tlv] country (optional) 2049 * [3] power control (optional) 2050 * [5] channel switch announcement (CSA) (optional) 2051 * [tlv] extended rate phy (ERP) 2052 * [tlv] extended supported rates 2053 * [tlv] RSN (optional) 2054 * [tlv] HT capabilities 2055 * [tlv] HT information 2056 * [tlv] WPA (optional) 2057 * [tlv] WME (optional) 2058 * [tlv] Vendor OUI HT capabilities (optional) 2059 * [tlv] Vendor OUI HT information (optional) 2060 * [tlv] Atheros capabilities 2061 * [tlv] AppIE's (optional) 2062 */ 2063 m = ieee80211_getmgtframe(&frm, 2064 ic->ic_headroom + sizeof(struct ieee80211_frame), 2065 8 2066 + sizeof(uint16_t) 2067 + sizeof(uint16_t) 2068 + 2 + IEEE80211_NWID_LEN 2069 + 2 + IEEE80211_RATE_SIZE 2070 + 7 /* max(7,3) */ 2071 + IEEE80211_COUNTRY_MAX_SIZE 2072 + 3 2073 + sizeof(struct ieee80211_csa_ie) 2074 + 3 2075 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2076 + sizeof(struct ieee80211_ie_wpa) 2077 + sizeof(struct ieee80211_ie_htcap) 2078 + sizeof(struct ieee80211_ie_htinfo) 2079 + sizeof(struct ieee80211_ie_wpa) 2080 + sizeof(struct ieee80211_wme_param) 2081 + 4 + sizeof(struct ieee80211_ie_htcap) 2082 + 4 + sizeof(struct ieee80211_ie_htinfo) 2083#ifdef IEEE80211_SUPPORT_SUPERG 2084 + sizeof(struct ieee80211_ath_ie) 2085#endif 2086 + (vap->iv_appie_proberesp != NULL ? 2087 vap->iv_appie_proberesp->ie_len : 0) 2088 ); 2089 if (m == NULL) { 2090 vap->iv_stats.is_tx_nobuf++; 2091 return NULL; 2092 } 2093 2094 memset(frm, 0, 8); /* timestamp should be filled later */ 2095 frm += 8; 2096 *(uint16_t *)frm = htole16(bss->ni_intval); 2097 frm += 2; 2098 capinfo = getcapinfo(vap, bss->ni_chan); 2099 *(uint16_t *)frm = htole16(capinfo); 2100 frm += 2; 2101 2102 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen); 2103 rs = ieee80211_get_suprates(ic, bss->ni_chan); 2104 frm = ieee80211_add_rates(frm, rs); 2105 2106 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) { 2107 *frm++ = IEEE80211_ELEMID_FHPARMS; 2108 *frm++ = 5; 2109 *frm++ = bss->ni_fhdwell & 0x00ff; 2110 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff; 2111 *frm++ = IEEE80211_FH_CHANSET( 2112 ieee80211_chan2ieee(ic, bss->ni_chan)); 2113 *frm++ = IEEE80211_FH_CHANPAT( 2114 ieee80211_chan2ieee(ic, bss->ni_chan)); 2115 *frm++ = bss->ni_fhindex; 2116 } else { 2117 *frm++ = IEEE80211_ELEMID_DSPARMS; 2118 *frm++ = 1; 2119 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan); 2120 } 2121 2122 if (vap->iv_opmode == IEEE80211_M_IBSS) { 2123 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 2124 *frm++ = 2; 2125 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 2126 } 2127 if ((vap->iv_flags & IEEE80211_F_DOTH) || 2128 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) 2129 frm = ieee80211_add_countryie(frm, ic); 2130 if (vap->iv_flags & IEEE80211_F_DOTH) { 2131 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan)) 2132 frm = ieee80211_add_powerconstraint(frm, vap); 2133 if (ic->ic_flags & IEEE80211_F_CSAPENDING) 2134 frm = ieee80211_add_csa(frm, vap); 2135 } 2136 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan)) 2137 frm = ieee80211_add_erp(frm, ic); 2138 frm = ieee80211_add_xrates(frm, rs); 2139 if (vap->iv_flags & IEEE80211_F_WPA2) { 2140 if (vap->iv_rsn_ie != NULL) 2141 frm = add_ie(frm, vap->iv_rsn_ie); 2142 /* XXX else complain? */ 2143 } 2144 /* 2145 * NB: legacy 11b clients do not get certain ie's. 2146 * The caller identifies such clients by passing 2147 * a token in legacy to us. Could expand this to be 2148 * any legacy client for stuff like HT ie's. 2149 */ 2150 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) && 2151 legacy != IEEE80211_SEND_LEGACY_11B) { 2152 frm = ieee80211_add_htcap(frm, bss); 2153 frm = ieee80211_add_htinfo(frm, bss); 2154 } 2155 if (vap->iv_flags & IEEE80211_F_WPA1) { 2156 if (vap->iv_wpa_ie != NULL) 2157 frm = add_ie(frm, vap->iv_wpa_ie); 2158 /* XXX else complain? */ 2159 } 2160 if (vap->iv_flags & IEEE80211_F_WME) 2161 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 2162 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) && 2163 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) && 2164 legacy != IEEE80211_SEND_LEGACY_11B) { 2165 frm = ieee80211_add_htcap_vendor(frm, bss); 2166 frm = ieee80211_add_htinfo_vendor(frm, bss); 2167 } 2168#ifdef IEEE80211_SUPPORT_SUPERG 2169 if ((vap->iv_flags & IEEE80211_F_ATHEROS) && 2170 legacy != IEEE80211_SEND_LEGACY_11B) 2171 frm = ieee80211_add_athcaps(frm, bss); 2172#endif 2173 if (vap->iv_appie_proberesp != NULL) 2174 frm = add_appie(frm, vap->iv_appie_proberesp); 2175 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2176 2177 return m; 2178} 2179 2180/* 2181 * Send a probe response frame to the specified mac address. 2182 * This does not go through the normal mgt frame api so we 2183 * can specify the destination address and re-use the bss node 2184 * for the sta reference. 2185 */ 2186int 2187ieee80211_send_proberesp(struct ieee80211vap *vap, 2188 const uint8_t da[IEEE80211_ADDR_LEN], int legacy) 2189{ 2190 struct ieee80211_node *bss = vap->iv_bss; 2191 struct ieee80211com *ic = vap->iv_ic; 2192 struct ieee80211_frame *wh; 2193 struct mbuf *m; 2194 2195 if (vap->iv_state == IEEE80211_S_CAC) { 2196 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss, 2197 "block %s frame in CAC state", "probe response"); 2198 vap->iv_stats.is_tx_badstate++; 2199 return EIO; /* XXX */ 2200 } 2201 2202 /* 2203 * Hold a reference on the node so it doesn't go away until after 2204 * the xmit is complete all the way in the driver. On error we 2205 * will remove our reference. 2206 */ 2207 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2208 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 2209 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr), 2210 ieee80211_node_refcnt(bss)+1); 2211 ieee80211_ref_node(bss); 2212 2213 m = ieee80211_alloc_proberesp(bss, legacy); 2214 if (m == NULL) { 2215 ieee80211_free_node(bss); 2216 return ENOMEM; 2217 } 2218 2219 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 2220 KASSERT(m != NULL, ("no room for header")); 2221 2222 wh = mtod(m, struct ieee80211_frame *); 2223 ieee80211_send_setup(bss, m, 2224 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP, 2225 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid); 2226 /* XXX power management? */ 2227 m->m_flags |= M_ENCAP; /* mark encapsulated */ 2228 2229 M_WME_SETAC(m, WME_AC_BE); 2230 2231 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 2232 "send probe resp on channel %u to %s%s\n", 2233 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da), 2234 legacy ? " <legacy>" : ""); 2235 IEEE80211_NODE_STAT(bss, tx_mgmt); 2236 2237 return ic->ic_raw_xmit(bss, m, NULL); 2238} 2239 2240/* 2241 * Allocate and build a RTS (Request To Send) control frame. 2242 */ 2243struct mbuf * 2244ieee80211_alloc_rts(struct ieee80211com *ic, 2245 const uint8_t ra[IEEE80211_ADDR_LEN], 2246 const uint8_t ta[IEEE80211_ADDR_LEN], 2247 uint16_t dur) 2248{ 2249 struct ieee80211_frame_rts *rts; 2250 struct mbuf *m; 2251 2252 /* XXX honor ic_headroom */ 2253 m = m_gethdr(M_DONTWAIT, MT_DATA); 2254 if (m != NULL) { 2255 rts = mtod(m, struct ieee80211_frame_rts *); 2256 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | 2257 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS; 2258 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2259 *(u_int16_t *)rts->i_dur = htole16(dur); 2260 IEEE80211_ADDR_COPY(rts->i_ra, ra); 2261 IEEE80211_ADDR_COPY(rts->i_ta, ta); 2262 2263 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts); 2264 } 2265 return m; 2266} 2267 2268/* 2269 * Allocate and build a CTS (Clear To Send) control frame. 2270 */ 2271struct mbuf * 2272ieee80211_alloc_cts(struct ieee80211com *ic, 2273 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur) 2274{ 2275 struct ieee80211_frame_cts *cts; 2276 struct mbuf *m; 2277 2278 /* XXX honor ic_headroom */ 2279 m = m_gethdr(M_DONTWAIT, MT_DATA); 2280 if (m != NULL) { 2281 cts = mtod(m, struct ieee80211_frame_cts *); 2282 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 | 2283 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS; 2284 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2285 *(u_int16_t *)cts->i_dur = htole16(dur); 2286 IEEE80211_ADDR_COPY(cts->i_ra, ra); 2287 2288 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts); 2289 } 2290 return m; 2291} 2292 2293static void 2294ieee80211_tx_mgt_timeout(void *arg) 2295{ 2296 struct ieee80211_node *ni = arg; 2297 struct ieee80211vap *vap = ni->ni_vap; 2298 2299 if (vap->iv_state != IEEE80211_S_INIT && 2300 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) { 2301 /* 2302 * NB: it's safe to specify a timeout as the reason here; 2303 * it'll only be used in the right state. 2304 */ 2305 ieee80211_new_state(vap, IEEE80211_S_SCAN, 2306 IEEE80211_SCAN_FAIL_TIMEOUT); 2307 } 2308} 2309 2310static void 2311ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status) 2312{ 2313 struct ieee80211vap *vap = ni->ni_vap; 2314 enum ieee80211_state ostate = (enum ieee80211_state) arg; 2315 2316 /* 2317 * Frame transmit completed; arrange timer callback. If 2318 * transmit was successfuly we wait for response. Otherwise 2319 * we arrange an immediate callback instead of doing the 2320 * callback directly since we don't know what state the driver 2321 * is in (e.g. what locks it is holding). This work should 2322 * not be too time-critical and not happen too often so the 2323 * added overhead is acceptable. 2324 * 2325 * XXX what happens if !acked but response shows up before callback? 2326 */ 2327 if (vap->iv_state == ostate) 2328 callout_reset(&vap->iv_mgtsend, 2329 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0, 2330 ieee80211_tx_mgt_timeout, ni); 2331} 2332 2333static void 2334ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm, 2335 struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni) 2336{ 2337 struct ieee80211vap *vap = ni->ni_vap; 2338 struct ieee80211com *ic = ni->ni_ic; 2339 struct ieee80211_rateset *rs = &ni->ni_rates; 2340 uint16_t capinfo; 2341 2342 /* 2343 * beacon frame format 2344 * [8] time stamp 2345 * [2] beacon interval 2346 * [2] cabability information 2347 * [tlv] ssid 2348 * [tlv] supported rates 2349 * [3] parameter set (DS) 2350 * [8] CF parameter set (optional) 2351 * [tlv] parameter set (IBSS/TIM) 2352 * [tlv] country (optional) 2353 * [3] power control (optional) 2354 * [5] channel switch announcement (CSA) (optional) 2355 * [tlv] extended rate phy (ERP) 2356 * [tlv] extended supported rates 2357 * [tlv] RSN parameters 2358 * [tlv] HT capabilities 2359 * [tlv] HT information 2360 * XXX Vendor-specific OIDs (e.g. Atheros) 2361 * [tlv] WPA parameters 2362 * [tlv] WME parameters 2363 * [tlv] Vendor OUI HT capabilities (optional) 2364 * [tlv] Vendor OUI HT information (optional) 2365 * [tlv] Atheros capabilities (optional) 2366 * [tlv] TDMA parameters (optional) 2367 * [tlv] application data (optional) 2368 */ 2369 2370 memset(bo, 0, sizeof(*bo)); 2371 2372 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */ 2373 frm += 8; 2374 *(uint16_t *)frm = htole16(ni->ni_intval); 2375 frm += 2; 2376 capinfo = getcapinfo(vap, ni->ni_chan); 2377 bo->bo_caps = (uint16_t *)frm; 2378 *(uint16_t *)frm = htole16(capinfo); 2379 frm += 2; 2380 *frm++ = IEEE80211_ELEMID_SSID; 2381 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) { 2382 *frm++ = ni->ni_esslen; 2383 memcpy(frm, ni->ni_essid, ni->ni_esslen); 2384 frm += ni->ni_esslen; 2385 } else 2386 *frm++ = 0; 2387 frm = ieee80211_add_rates(frm, rs); 2388 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) { 2389 *frm++ = IEEE80211_ELEMID_DSPARMS; 2390 *frm++ = 1; 2391 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 2392 } 2393 if (ic->ic_flags & IEEE80211_F_PCF) { 2394 bo->bo_cfp = frm; 2395 frm = ieee80211_add_cfparms(frm, ic); 2396 } 2397 bo->bo_tim = frm; 2398 if (vap->iv_opmode == IEEE80211_M_IBSS) { 2399 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 2400 *frm++ = 2; 2401 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 2402 bo->bo_tim_len = 0; 2403 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 2404 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm; 2405 2406 tie->tim_ie = IEEE80211_ELEMID_TIM; 2407 tie->tim_len = 4; /* length */ 2408 tie->tim_count = 0; /* DTIM count */ 2409 tie->tim_period = vap->iv_dtim_period; /* DTIM period */ 2410 tie->tim_bitctl = 0; /* bitmap control */ 2411 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */ 2412 frm += sizeof(struct ieee80211_tim_ie); 2413 bo->bo_tim_len = 1; 2414 } 2415 bo->bo_tim_trailer = frm; 2416 if ((vap->iv_flags & IEEE80211_F_DOTH) || 2417 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) 2418 frm = ieee80211_add_countryie(frm, ic); 2419 if (vap->iv_flags & IEEE80211_F_DOTH) { 2420 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)) 2421 frm = ieee80211_add_powerconstraint(frm, vap); 2422 bo->bo_csa = frm; 2423 if (ic->ic_flags & IEEE80211_F_CSAPENDING) 2424 frm = ieee80211_add_csa(frm, vap); 2425 } else 2426 bo->bo_csa = frm; 2427 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) { 2428 bo->bo_erp = frm; 2429 frm = ieee80211_add_erp(frm, ic); 2430 } 2431 frm = ieee80211_add_xrates(frm, rs); 2432 if (vap->iv_flags & IEEE80211_F_WPA2) { 2433 if (vap->iv_rsn_ie != NULL) 2434 frm = add_ie(frm, vap->iv_rsn_ie); 2435 /* XXX else complain */ 2436 } 2437 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 2438 frm = ieee80211_add_htcap(frm, ni); 2439 bo->bo_htinfo = frm; 2440 frm = ieee80211_add_htinfo(frm, ni); 2441 } 2442 if (vap->iv_flags & IEEE80211_F_WPA1) { 2443 if (vap->iv_wpa_ie != NULL) 2444 frm = add_ie(frm, vap->iv_wpa_ie); 2445 /* XXX else complain */ 2446 } 2447 if (vap->iv_flags & IEEE80211_F_WME) { 2448 bo->bo_wme = frm; 2449 frm = ieee80211_add_wme_param(frm, &ic->ic_wme); 2450 } 2451 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && 2452 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) { 2453 frm = ieee80211_add_htcap_vendor(frm, ni); 2454 frm = ieee80211_add_htinfo_vendor(frm, ni); 2455 } 2456#ifdef IEEE80211_SUPPORT_SUPERG 2457 if (vap->iv_flags & IEEE80211_F_ATHEROS) { 2458 bo->bo_ath = frm; 2459 frm = ieee80211_add_athcaps(frm, ni); 2460 } 2461#endif 2462#ifdef IEEE80211_SUPPORT_TDMA 2463 if (vap->iv_caps & IEEE80211_C_TDMA) { 2464 bo->bo_tdma = frm; 2465 frm = ieee80211_add_tdma(frm, vap); 2466 } 2467#endif 2468 if (vap->iv_appie_beacon != NULL) { 2469 bo->bo_appie = frm; 2470 bo->bo_appie_len = vap->iv_appie_beacon->ie_len; 2471 frm = add_appie(frm, vap->iv_appie_beacon); 2472 } 2473 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer; 2474 bo->bo_csa_trailer_len = frm - bo->bo_csa; 2475 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2476} 2477 2478/* 2479 * Allocate a beacon frame and fillin the appropriate bits. 2480 */ 2481struct mbuf * 2482ieee80211_beacon_alloc(struct ieee80211_node *ni, 2483 struct ieee80211_beacon_offsets *bo) 2484{ 2485 struct ieee80211vap *vap = ni->ni_vap; 2486 struct ieee80211com *ic = ni->ni_ic; 2487 struct ifnet *ifp = vap->iv_ifp; 2488 struct ieee80211_frame *wh; 2489 struct mbuf *m; 2490 int pktlen; 2491 uint8_t *frm; 2492 2493 /* 2494 * beacon frame format 2495 * [8] time stamp 2496 * [2] beacon interval 2497 * [2] cabability information 2498 * [tlv] ssid 2499 * [tlv] supported rates 2500 * [3] parameter set (DS) 2501 * [8] CF parameter set (optional) 2502 * [tlv] parameter set (IBSS/TIM) 2503 * [tlv] country (optional) 2504 * [3] power control (optional) 2505 * [5] channel switch announcement (CSA) (optional) 2506 * [tlv] extended rate phy (ERP) 2507 * [tlv] extended supported rates 2508 * [tlv] RSN parameters 2509 * [tlv] HT capabilities 2510 * [tlv] HT information 2511 * [tlv] Vendor OUI HT capabilities (optional) 2512 * [tlv] Vendor OUI HT information (optional) 2513 * XXX Vendor-specific OIDs (e.g. Atheros) 2514 * [tlv] WPA parameters 2515 * [tlv] WME parameters 2516 * [tlv] TDMA parameters (optional) 2517 * [tlv] application data (optional) 2518 * NB: we allocate the max space required for the TIM bitmap. 2519 * XXX how big is this? 2520 */ 2521 pktlen = 8 /* time stamp */ 2522 + sizeof(uint16_t) /* beacon interval */ 2523 + sizeof(uint16_t) /* capabilities */ 2524 + 2 + ni->ni_esslen /* ssid */ 2525 + 2 + IEEE80211_RATE_SIZE /* supported rates */ 2526 + 2 + 1 /* DS parameters */ 2527 + 2 + 6 /* CF parameters */ 2528 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */ 2529 + IEEE80211_COUNTRY_MAX_SIZE /* country */ 2530 + 2 + 1 /* power control */ 2531 + sizeof(struct ieee80211_csa_ie) /* CSA */ 2532 + 2 + 1 /* ERP */ 2533 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2534 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */ 2535 2*sizeof(struct ieee80211_ie_wpa) : 0) 2536 /* XXX conditional? */ 2537 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */ 2538 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */ 2539 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */ 2540 sizeof(struct ieee80211_wme_param) : 0) 2541#ifdef IEEE80211_SUPPORT_SUPERG 2542 + sizeof(struct ieee80211_ath_ie) /* ATH */ 2543#endif 2544#ifdef IEEE80211_SUPPORT_TDMA 2545 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */ 2546 sizeof(struct ieee80211_tdma_param) : 0) 2547#endif 2548 + IEEE80211_MAX_APPIE 2549 ; 2550 m = ieee80211_getmgtframe(&frm, 2551 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen); 2552 if (m == NULL) { 2553 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 2554 "%s: cannot get buf; size %u\n", __func__, pktlen); 2555 vap->iv_stats.is_tx_nobuf++; 2556 return NULL; 2557 } 2558 ieee80211_beacon_construct(m, frm, bo, ni); 2559 2560 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 2561 KASSERT(m != NULL, ("no space for 802.11 header?")); 2562 wh = mtod(m, struct ieee80211_frame *); 2563 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 2564 IEEE80211_FC0_SUBTYPE_BEACON; 2565 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2566 *(uint16_t *)wh->i_dur = 0; 2567 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 2568 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 2569 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 2570 *(uint16_t *)wh->i_seq = 0; 2571 2572 return m; 2573} 2574 2575/* 2576 * Update the dynamic parts of a beacon frame based on the current state. 2577 */ 2578int 2579ieee80211_beacon_update(struct ieee80211_node *ni, 2580 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast) 2581{ 2582 struct ieee80211vap *vap = ni->ni_vap; 2583 struct ieee80211com *ic = ni->ni_ic; 2584 int len_changed = 0; 2585 uint16_t capinfo; 2586 2587 IEEE80211_LOCK(ic); 2588 /* 2589 * Handle 11h channel change when we've reached the count. 2590 * We must recalculate the beacon frame contents to account 2591 * for the new channel. Note we do this only for the first 2592 * vap that reaches this point; subsequent vaps just update 2593 * their beacon state to reflect the recalculated channel. 2594 */ 2595 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) && 2596 vap->iv_csa_count == ic->ic_csa_count) { 2597 vap->iv_csa_count = 0; 2598 /* 2599 * Effect channel change before reconstructing the beacon 2600 * frame contents as many places reference ni_chan. 2601 */ 2602 if (ic->ic_csa_newchan != NULL) 2603 ieee80211_csa_completeswitch(ic); 2604 /* 2605 * NB: ieee80211_beacon_construct clears all pending 2606 * updates in bo_flags so we don't need to explicitly 2607 * clear IEEE80211_BEACON_CSA. 2608 */ 2609 ieee80211_beacon_construct(m, 2610 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni); 2611 2612 /* XXX do WME aggressive mode processing? */ 2613 IEEE80211_UNLOCK(ic); 2614 return 1; /* just assume length changed */ 2615 } 2616 2617 /* XXX faster to recalculate entirely or just changes? */ 2618 capinfo = getcapinfo(vap, ni->ni_chan); 2619 *bo->bo_caps = htole16(capinfo); 2620 2621 if (vap->iv_flags & IEEE80211_F_WME) { 2622 struct ieee80211_wme_state *wme = &ic->ic_wme; 2623 2624 /* 2625 * Check for agressive mode change. When there is 2626 * significant high priority traffic in the BSS 2627 * throttle back BE traffic by using conservative 2628 * parameters. Otherwise BE uses agressive params 2629 * to optimize performance of legacy/non-QoS traffic. 2630 */ 2631 if (wme->wme_flags & WME_F_AGGRMODE) { 2632 if (wme->wme_hipri_traffic > 2633 wme->wme_hipri_switch_thresh) { 2634 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 2635 "%s: traffic %u, disable aggressive mode\n", 2636 __func__, wme->wme_hipri_traffic); 2637 wme->wme_flags &= ~WME_F_AGGRMODE; 2638 ieee80211_wme_updateparams_locked(vap); 2639 wme->wme_hipri_traffic = 2640 wme->wme_hipri_switch_hysteresis; 2641 } else 2642 wme->wme_hipri_traffic = 0; 2643 } else { 2644 if (wme->wme_hipri_traffic <= 2645 wme->wme_hipri_switch_thresh) { 2646 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 2647 "%s: traffic %u, enable aggressive mode\n", 2648 __func__, wme->wme_hipri_traffic); 2649 wme->wme_flags |= WME_F_AGGRMODE; 2650 ieee80211_wme_updateparams_locked(vap); 2651 wme->wme_hipri_traffic = 0; 2652 } else 2653 wme->wme_hipri_traffic = 2654 wme->wme_hipri_switch_hysteresis; 2655 } 2656 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) { 2657 (void) ieee80211_add_wme_param(bo->bo_wme, wme); 2658 clrbit(bo->bo_flags, IEEE80211_BEACON_WME); 2659 } 2660 } 2661 2662 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) { 2663 ieee80211_ht_update_beacon(vap, bo); 2664 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO); 2665 } 2666#ifdef IEEE80211_SUPPORT_TDMA 2667 if (vap->iv_caps & IEEE80211_C_TDMA) { 2668 /* 2669 * NB: the beacon is potentially updated every TBTT. 2670 */ 2671 ieee80211_tdma_update_beacon(vap, bo); 2672 } 2673#endif 2674 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { /* NB: no IBSS support*/ 2675 struct ieee80211_tim_ie *tie = 2676 (struct ieee80211_tim_ie *) bo->bo_tim; 2677 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) { 2678 u_int timlen, timoff, i; 2679 /* 2680 * ATIM/DTIM needs updating. If it fits in the 2681 * current space allocated then just copy in the 2682 * new bits. Otherwise we need to move any trailing 2683 * data to make room. Note that we know there is 2684 * contiguous space because ieee80211_beacon_allocate 2685 * insures there is space in the mbuf to write a 2686 * maximal-size virtual bitmap (based on iv_max_aid). 2687 */ 2688 /* 2689 * Calculate the bitmap size and offset, copy any 2690 * trailer out of the way, and then copy in the 2691 * new bitmap and update the information element. 2692 * Note that the tim bitmap must contain at least 2693 * one byte and any offset must be even. 2694 */ 2695 if (vap->iv_ps_pending != 0) { 2696 timoff = 128; /* impossibly large */ 2697 for (i = 0; i < vap->iv_tim_len; i++) 2698 if (vap->iv_tim_bitmap[i]) { 2699 timoff = i &~ 1; 2700 break; 2701 } 2702 KASSERT(timoff != 128, ("tim bitmap empty!")); 2703 for (i = vap->iv_tim_len-1; i >= timoff; i--) 2704 if (vap->iv_tim_bitmap[i]) 2705 break; 2706 timlen = 1 + (i - timoff); 2707 } else { 2708 timoff = 0; 2709 timlen = 1; 2710 } 2711 if (timlen != bo->bo_tim_len) { 2712 /* copy up/down trailer */ 2713 int adjust = tie->tim_bitmap+timlen 2714 - bo->bo_tim_trailer; 2715 ovbcopy(bo->bo_tim_trailer, 2716 bo->bo_tim_trailer+adjust, 2717 bo->bo_tim_trailer_len); 2718 bo->bo_tim_trailer += adjust; 2719 bo->bo_erp += adjust; 2720 bo->bo_htinfo += adjust; 2721#ifdef IEEE80211_SUPERG_SUPPORT 2722 bo->bo_ath += adjust; 2723#endif 2724#ifdef IEEE80211_TDMA_SUPPORT 2725 bo->bo_tdma += adjust; 2726#endif 2727 bo->bo_appie += adjust; 2728 bo->bo_wme += adjust; 2729 bo->bo_csa += adjust; 2730 bo->bo_tim_len = timlen; 2731 2732 /* update information element */ 2733 tie->tim_len = 3 + timlen; 2734 tie->tim_bitctl = timoff; 2735 len_changed = 1; 2736 } 2737 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff, 2738 bo->bo_tim_len); 2739 2740 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM); 2741 2742 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER, 2743 "%s: TIM updated, pending %u, off %u, len %u\n", 2744 __func__, vap->iv_ps_pending, timoff, timlen); 2745 } 2746 /* count down DTIM period */ 2747 if (tie->tim_count == 0) 2748 tie->tim_count = tie->tim_period - 1; 2749 else 2750 tie->tim_count--; 2751 /* update state for buffered multicast frames on DTIM */ 2752 if (mcast && tie->tim_count == 0) 2753 tie->tim_bitctl |= 1; 2754 else 2755 tie->tim_bitctl &= ~1; 2756 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) { 2757 struct ieee80211_csa_ie *csa = 2758 (struct ieee80211_csa_ie *) bo->bo_csa; 2759 2760 /* 2761 * Insert or update CSA ie. If we're just starting 2762 * to count down to the channel switch then we need 2763 * to insert the CSA ie. Otherwise we just need to 2764 * drop the count. The actual change happens above 2765 * when the vap's count reaches the target count. 2766 */ 2767 if (vap->iv_csa_count == 0) { 2768 memmove(&csa[1], csa, bo->bo_csa_trailer_len); 2769 bo->bo_erp += sizeof(*csa); 2770 bo->bo_htinfo += sizeof(*csa); 2771 bo->bo_wme += sizeof(*csa); 2772#ifdef IEEE80211_SUPERG_SUPPORT 2773 bo->bo_ath += sizeof(*csa); 2774#endif 2775#ifdef IEEE80211_TDMA_SUPPORT 2776 bo->bo_tdma += sizeof(*csa); 2777#endif 2778 bo->bo_appie += sizeof(*csa); 2779 bo->bo_csa_trailer_len += sizeof(*csa); 2780 bo->bo_tim_trailer_len += sizeof(*csa); 2781 m->m_len += sizeof(*csa); 2782 m->m_pkthdr.len += sizeof(*csa); 2783 2784 ieee80211_add_csa(bo->bo_csa, vap); 2785 } else 2786 csa->csa_count--; 2787 vap->iv_csa_count++; 2788 /* NB: don't clear IEEE80211_BEACON_CSA */ 2789 } 2790 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) { 2791 /* 2792 * ERP element needs updating. 2793 */ 2794 (void) ieee80211_add_erp(bo->bo_erp, ic); 2795 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP); 2796 } 2797#ifdef IEEE80211_SUPPORT_SUPERG 2798 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) { 2799 ieee80211_add_athcaps(bo->bo_ath, ni); 2800 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH); 2801 } 2802#endif 2803 } 2804 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) { 2805 const struct ieee80211_appie *aie = vap->iv_appie_beacon; 2806 int aielen; 2807 uint8_t *frm; 2808 2809 aielen = 0; 2810 if (aie != NULL) 2811 aielen += aie->ie_len; 2812 if (aielen != bo->bo_appie_len) { 2813 /* copy up/down trailer */ 2814 int adjust = aielen - bo->bo_appie_len; 2815 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust, 2816 bo->bo_tim_trailer_len); 2817 bo->bo_tim_trailer += adjust; 2818 bo->bo_appie += adjust; 2819 bo->bo_appie_len = aielen; 2820 2821 len_changed = 1; 2822 } 2823 frm = bo->bo_appie; 2824 if (aie != NULL) 2825 frm = add_appie(frm, aie); 2826 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE); 2827 } 2828 IEEE80211_UNLOCK(ic); 2829 2830 return len_changed; 2831} 2832