1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2001 Atsushi Onoe 5 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 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/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/mbuf.h> 39 #include <sys/endian.h> 40 41 #include <sys/socket.h> 42 43 #include <net/bpf.h> 44 #include <net/ethernet.h> 45 #include <net/if.h> 46 #include <net/if_var.h> 47 #include <net/if_llc.h> 48 #include <net/if_media.h> 49 #include <net/if_private.h> 50 #include <net/if_vlan_var.h> 51 52 #include <net80211/ieee80211_var.h> 53 #include <net80211/ieee80211_regdomain.h> 54 #ifdef IEEE80211_SUPPORT_SUPERG 55 #include <net80211/ieee80211_superg.h> 56 #endif 57 #ifdef IEEE80211_SUPPORT_TDMA 58 #include <net80211/ieee80211_tdma.h> 59 #endif 60 #include <net80211/ieee80211_wds.h> 61 #include <net80211/ieee80211_mesh.h> 62 #include <net80211/ieee80211_vht.h> 63 64 #if defined(INET) || defined(INET6) 65 #include <netinet/in.h> 66 #endif 67 68 #ifdef INET 69 #include <netinet/if_ether.h> 70 #include <netinet/in_systm.h> 71 #include <netinet/ip.h> 72 #endif 73 #ifdef INET6 74 #include <netinet/ip6.h> 75 #endif 76 77 #include <security/mac/mac_framework.h> 78 79 #define ETHER_HEADER_COPY(dst, src) \ 80 memcpy(dst, src, sizeof(struct ether_header)) 81 82 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *, 83 u_int hdrsize, u_int ciphdrsize, u_int mtu); 84 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int); 85 86 #ifdef IEEE80211_DEBUG 87 /* 88 * Decide if an outbound management frame should be 89 * printed when debugging is enabled. This filters some 90 * of the less interesting frames that come frequently 91 * (e.g. beacons). 92 */ 93 static __inline int 94 doprint(struct ieee80211vap *vap, int subtype) 95 { 96 switch (subtype) { 97 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 98 return (vap->iv_opmode == IEEE80211_M_IBSS); 99 } 100 return 1; 101 } 102 #endif 103 104 /* 105 * Transmit a frame to the given destination on the given VAP. 106 * 107 * It's up to the caller to figure out the details of who this 108 * is going to and resolving the node. 109 * 110 * This routine takes care of queuing it for power save, 111 * A-MPDU state stuff, fast-frames state stuff, encapsulation 112 * if required, then passing it up to the driver layer. 113 * 114 * This routine (for now) consumes the mbuf and frees the node 115 * reference; it ideally will return a TX status which reflects 116 * whether the mbuf was consumed or not, so the caller can 117 * free the mbuf (if appropriate) and the node reference (again, 118 * if appropriate.) 119 */ 120 int 121 ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m, 122 struct ieee80211_node *ni) 123 { 124 struct ieee80211com *ic = vap->iv_ic; 125 struct ifnet *ifp = vap->iv_ifp; 126 int mcast; 127 int do_ampdu = 0; 128 #ifdef IEEE80211_SUPPORT_SUPERG 129 int do_amsdu = 0; 130 int do_ampdu_amsdu = 0; 131 int no_ampdu = 1; /* Will be set to 0 if ampdu is active */ 132 int do_ff = 0; 133 #endif 134 135 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 136 (m->m_flags & M_PWR_SAV) == 0) { 137 /* 138 * Station in power save mode; pass the frame 139 * to the 802.11 layer and continue. We'll get 140 * the frame back when the time is right. 141 * XXX lose WDS vap linkage? 142 */ 143 if (ieee80211_pwrsave(ni, m) != 0) 144 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 145 ieee80211_free_node(ni); 146 147 /* 148 * We queued it fine, so tell the upper layer 149 * that we consumed it. 150 */ 151 return (0); 152 } 153 /* calculate priority so drivers can find the tx queue */ 154 if (ieee80211_classify(ni, m)) { 155 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT, 156 ni->ni_macaddr, NULL, 157 "%s", "classification failure"); 158 vap->iv_stats.is_tx_classify++; 159 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 160 m_freem(m); 161 ieee80211_free_node(ni); 162 163 /* XXX better status? */ 164 return (0); 165 } 166 /* 167 * Stash the node pointer. Note that we do this after 168 * any call to ieee80211_dwds_mcast because that code 169 * uses any existing value for rcvif to identify the 170 * interface it (might have been) received on. 171 */ 172 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 173 m->m_pkthdr.rcvif = (void *)ni; 174 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0; 175 176 BPF_MTAP(ifp, m); /* 802.3 tx */ 177 178 /* 179 * Figure out if we can do A-MPDU, A-MSDU or FF. 180 * 181 * A-MPDU depends upon vap/node config. 182 * A-MSDU depends upon vap/node config. 183 * FF depends upon vap config, IE and whether 184 * it's 11abg (and not 11n/11ac/etc.) 185 * 186 * Note that these flags indiciate whether we can do 187 * it at all, rather than the situation (eg traffic type.) 188 */ 189 do_ampdu = ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) && 190 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)); 191 #ifdef IEEE80211_SUPPORT_SUPERG 192 do_amsdu = ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) && 193 (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)); 194 do_ff = 195 ((ni->ni_flags & IEEE80211_NODE_HT) == 0) && 196 ((ni->ni_flags & IEEE80211_NODE_VHT) == 0) && 197 (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)); 198 #endif 199 200 /* 201 * Check if A-MPDU tx aggregation is setup or if we 202 * should try to enable it. The sta must be associated 203 * with HT and A-MPDU enabled for use. When the policy 204 * routine decides we should enable A-MPDU we issue an 205 * ADDBA request and wait for a reply. The frame being 206 * encapsulated will go out w/o using A-MPDU, or possibly 207 * it might be collected by the driver and held/retransmit. 208 * The default ic_ampdu_enable routine handles staggering 209 * ADDBA requests in case the receiver NAK's us or we are 210 * otherwise unable to establish a BA stream. 211 * 212 * Don't treat group-addressed frames as candidates for aggregation; 213 * net80211 doesn't support 802.11aa-2012 and so group addressed 214 * frames will always have sequence numbers allocated from the NON_QOS 215 * TID. 216 */ 217 if (do_ampdu) { 218 if ((m->m_flags & M_EAPOL) == 0 && (! mcast)) { 219 int tid = WME_AC_TO_TID(M_WME_GETAC(m)); 220 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid]; 221 222 ieee80211_txampdu_count_packet(tap); 223 if (IEEE80211_AMPDU_RUNNING(tap)) { 224 /* 225 * Operational, mark frame for aggregation. 226 * 227 * XXX do tx aggregation here 228 */ 229 m->m_flags |= M_AMPDU_MPDU; 230 } else if (!IEEE80211_AMPDU_REQUESTED(tap) && 231 ic->ic_ampdu_enable(ni, tap)) { 232 /* 233 * Not negotiated yet, request service. 234 */ 235 ieee80211_ampdu_request(ni, tap); 236 /* XXX hold frame for reply? */ 237 } 238 /* 239 * Now update the no-ampdu flag. A-MPDU may have been 240 * started or administratively disabled above; so now we 241 * know whether we're running yet or not. 242 * 243 * This will let us know whether we should be doing A-MSDU 244 * at this point. We only do A-MSDU if we're either not 245 * doing A-MPDU, or A-MPDU is NACKed, or A-MPDU + A-MSDU 246 * is available. 247 * 248 * Whilst here, update the amsdu-ampdu flag. The above may 249 * have also set or cleared the amsdu-in-ampdu txa_flags 250 * combination so we can correctly do A-MPDU + A-MSDU. 251 */ 252 #ifdef IEEE80211_SUPPORT_SUPERG 253 no_ampdu = (! IEEE80211_AMPDU_RUNNING(tap) 254 || (IEEE80211_AMPDU_NACKED(tap))); 255 do_ampdu_amsdu = IEEE80211_AMPDU_RUNNING_AMSDU(tap); 256 #endif 257 } 258 } 259 260 #ifdef IEEE80211_SUPPORT_SUPERG 261 /* 262 * Check for AMSDU/FF; queue for aggregation 263 * 264 * Note: we don't bother trying to do fast frames or 265 * A-MSDU encapsulation for 802.3 drivers. Now, we 266 * likely could do it for FF (because it's a magic 267 * atheros tunnel LLC type) but I don't think we're going 268 * to really need to. For A-MSDU we'd have to set the 269 * A-MSDU QoS bit in the wifi header, so we just plain 270 * can't do it. 271 */ 272 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) { 273 if ((! mcast) && 274 (do_ampdu_amsdu || (no_ampdu && do_amsdu)) && 275 ieee80211_amsdu_tx_ok(ni)) { 276 m = ieee80211_amsdu_check(ni, m); 277 if (m == NULL) { 278 /* NB: any ni ref held on stageq */ 279 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 280 "%s: amsdu_check queued frame\n", 281 __func__); 282 return (0); 283 } 284 } else if ((! mcast) && do_ff) { 285 m = ieee80211_ff_check(ni, m); 286 if (m == NULL) { 287 /* NB: any ni ref held on stageq */ 288 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 289 "%s: ff_check queued frame\n", 290 __func__); 291 return (0); 292 } 293 } 294 } 295 #endif /* IEEE80211_SUPPORT_SUPERG */ 296 297 /* 298 * Grab the TX lock - serialise the TX process from this 299 * point (where TX state is being checked/modified) 300 * through to driver queue. 301 */ 302 IEEE80211_TX_LOCK(ic); 303 304 /* 305 * XXX make the encap and transmit code a separate function 306 * so things like the FF (and later A-MSDU) path can just call 307 * it for flushed frames. 308 */ 309 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) { 310 /* 311 * Encapsulate the packet in prep for transmission. 312 */ 313 m = ieee80211_encap(vap, ni, m); 314 if (m == NULL) { 315 /* NB: stat+msg handled in ieee80211_encap */ 316 IEEE80211_TX_UNLOCK(ic); 317 ieee80211_free_node(ni); 318 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 319 return (ENOBUFS); 320 } 321 } 322 (void) ieee80211_parent_xmitpkt(ic, m); 323 324 /* 325 * Unlock at this point - no need to hold it across 326 * ieee80211_free_node() (ie, the comlock) 327 */ 328 IEEE80211_TX_UNLOCK(ic); 329 ic->ic_lastdata = ticks; 330 331 return (0); 332 } 333 334 /* 335 * Send the given mbuf through the given vap. 336 * 337 * This consumes the mbuf regardless of whether the transmit 338 * was successful or not. 339 * 340 * This does none of the initial checks that ieee80211_start() 341 * does (eg CAC timeout, interface wakeup) - the caller must 342 * do this first. 343 */ 344 static int 345 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m) 346 { 347 #define IS_DWDS(vap) \ 348 (vap->iv_opmode == IEEE80211_M_WDS && \ 349 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) 350 struct ieee80211com *ic = vap->iv_ic; 351 struct ifnet *ifp = vap->iv_ifp; 352 struct ieee80211_node *ni; 353 struct ether_header *eh; 354 355 /* 356 * Cancel any background scan. 357 */ 358 if (ic->ic_flags & IEEE80211_F_SCAN) 359 ieee80211_cancel_anyscan(vap); 360 /* 361 * Find the node for the destination so we can do 362 * things like power save and fast frames aggregation. 363 * 364 * NB: past this point various code assumes the first 365 * mbuf has the 802.3 header present (and contiguous). 366 */ 367 ni = NULL; 368 if (m->m_len < sizeof(struct ether_header) && 369 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) { 370 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 371 "discard frame, %s\n", "m_pullup failed"); 372 vap->iv_stats.is_tx_nobuf++; /* XXX */ 373 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 374 return (ENOBUFS); 375 } 376 eh = mtod(m, struct ether_header *); 377 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 378 if (IS_DWDS(vap)) { 379 /* 380 * Only unicast frames from the above go out 381 * DWDS vaps; multicast frames are handled by 382 * dispatching the frame as it comes through 383 * the AP vap (see below). 384 */ 385 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS, 386 eh->ether_dhost, "mcast", "%s", "on DWDS"); 387 vap->iv_stats.is_dwds_mcast++; 388 m_freem(m); 389 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 390 /* XXX better status? */ 391 return (ENOBUFS); 392 } 393 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 394 /* 395 * Spam DWDS vap's w/ multicast traffic. 396 */ 397 /* XXX only if dwds in use? */ 398 ieee80211_dwds_mcast(vap, m); 399 } 400 } 401 #ifdef IEEE80211_SUPPORT_MESH 402 if (vap->iv_opmode != IEEE80211_M_MBSS) { 403 #endif 404 ni = ieee80211_find_txnode(vap, eh->ether_dhost); 405 if (ni == NULL) { 406 /* NB: ieee80211_find_txnode does stat+msg */ 407 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 408 m_freem(m); 409 /* XXX better status? */ 410 return (ENOBUFS); 411 } 412 if (ni->ni_associd == 0 && 413 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) { 414 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT, 415 eh->ether_dhost, NULL, 416 "sta not associated (type 0x%04x)", 417 htons(eh->ether_type)); 418 vap->iv_stats.is_tx_notassoc++; 419 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 420 m_freem(m); 421 ieee80211_free_node(ni); 422 /* XXX better status? */ 423 return (ENOBUFS); 424 } 425 #ifdef IEEE80211_SUPPORT_MESH 426 } else { 427 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) { 428 /* 429 * Proxy station only if configured. 430 */ 431 if (!ieee80211_mesh_isproxyena(vap)) { 432 IEEE80211_DISCARD_MAC(vap, 433 IEEE80211_MSG_OUTPUT | 434 IEEE80211_MSG_MESH, 435 eh->ether_dhost, NULL, 436 "%s", "proxy not enabled"); 437 vap->iv_stats.is_mesh_notproxy++; 438 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 439 m_freem(m); 440 /* XXX better status? */ 441 return (ENOBUFS); 442 } 443 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 444 "forward frame from DS SA(%6D), DA(%6D)\n", 445 eh->ether_shost, ":", 446 eh->ether_dhost, ":"); 447 ieee80211_mesh_proxy_check(vap, eh->ether_shost); 448 } 449 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m); 450 if (ni == NULL) { 451 /* 452 * NB: ieee80211_mesh_discover holds/disposes 453 * frame (e.g. queueing on path discovery). 454 */ 455 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 456 /* XXX better status? */ 457 return (ENOBUFS); 458 } 459 } 460 #endif 461 462 /* 463 * We've resolved the sender, so attempt to transmit it. 464 */ 465 466 if (vap->iv_state == IEEE80211_S_SLEEP) { 467 /* 468 * In power save; queue frame and then wakeup device 469 * for transmit. 470 */ 471 ic->ic_lastdata = ticks; 472 if (ieee80211_pwrsave(ni, m) != 0) 473 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 474 ieee80211_free_node(ni); 475 ieee80211_new_state(vap, IEEE80211_S_RUN, 0); 476 return (0); 477 } 478 479 if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0) 480 return (ENOBUFS); 481 return (0); 482 #undef IS_DWDS 483 } 484 485 /* 486 * Start method for vap's. All packets from the stack come 487 * through here. We handle common processing of the packets 488 * before dispatching them to the underlying device. 489 * 490 * if_transmit() requires that the mbuf be consumed by this call 491 * regardless of the return condition. 492 */ 493 int 494 ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m) 495 { 496 struct ieee80211vap *vap = ifp->if_softc; 497 struct ieee80211com *ic = vap->iv_ic; 498 499 /* 500 * No data frames go out unless we're running. 501 * Note in particular this covers CAC and CSA 502 * states (though maybe we should check muting 503 * for CSA). 504 */ 505 if (vap->iv_state != IEEE80211_S_RUN && 506 vap->iv_state != IEEE80211_S_SLEEP) { 507 IEEE80211_LOCK(ic); 508 /* re-check under the com lock to avoid races */ 509 if (vap->iv_state != IEEE80211_S_RUN && 510 vap->iv_state != IEEE80211_S_SLEEP) { 511 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 512 "%s: ignore queue, in %s state\n", 513 __func__, ieee80211_state_name[vap->iv_state]); 514 vap->iv_stats.is_tx_badstate++; 515 IEEE80211_UNLOCK(ic); 516 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 517 m_freem(m); 518 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 519 return (ENETDOWN); 520 } 521 IEEE80211_UNLOCK(ic); 522 } 523 524 /* 525 * Sanitize mbuf flags for net80211 use. We cannot 526 * clear M_PWR_SAV or M_MORE_DATA because these may 527 * be set for frames that are re-submitted from the 528 * power save queue. 529 * 530 * NB: This must be done before ieee80211_classify as 531 * it marks EAPOL in frames with M_EAPOL. 532 */ 533 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA); 534 535 /* 536 * Bump to the packet transmission path. 537 * The mbuf will be consumed here. 538 */ 539 return (ieee80211_start_pkt(vap, m)); 540 } 541 542 void 543 ieee80211_vap_qflush(struct ifnet *ifp) 544 { 545 546 /* Empty for now */ 547 } 548 549 /* 550 * 802.11 raw output routine. 551 * 552 * XXX TODO: this (and other send routines) should correctly 553 * XXX keep the pwr mgmt bit set if it decides to call into the 554 * XXX driver to send a frame whilst the state is SLEEP. 555 * 556 * Otherwise the peer may decide that we're awake and flood us 557 * with traffic we are still too asleep to receive! 558 */ 559 int 560 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni, 561 struct mbuf *m, const struct ieee80211_bpf_params *params) 562 { 563 struct ieee80211com *ic = vap->iv_ic; 564 int error; 565 566 /* 567 * Set node - the caller has taken a reference, so ensure 568 * that the mbuf has the same node value that 569 * it would if it were going via the normal path. 570 */ 571 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 572 m->m_pkthdr.rcvif = (void *)ni; 573 574 /* 575 * Attempt to add bpf transmit parameters. 576 * 577 * For now it's ok to fail; the raw_xmit api still takes 578 * them as an option. 579 * 580 * Later on when ic_raw_xmit() has params removed, 581 * they'll have to be added - so fail the transmit if 582 * they can't be. 583 */ 584 if (params) 585 (void) ieee80211_add_xmit_params(m, params); 586 587 error = ic->ic_raw_xmit(ni, m, params); 588 if (error) { 589 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1); 590 ieee80211_free_node(ni); 591 } 592 return (error); 593 } 594 595 static int 596 ieee80211_validate_frame(struct mbuf *m, 597 const struct ieee80211_bpf_params *params) 598 { 599 struct ieee80211_frame *wh; 600 int type; 601 602 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack)) 603 return (EINVAL); 604 605 wh = mtod(m, struct ieee80211_frame *); 606 if (!IEEE80211_IS_FC0_CHECK_VER(wh, IEEE80211_FC0_VERSION_0)) 607 return (EINVAL); 608 609 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 610 if (type != IEEE80211_FC0_TYPE_DATA) { 611 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != 612 IEEE80211_FC1_DIR_NODS) 613 return (EINVAL); 614 615 if (type != IEEE80211_FC0_TYPE_MGT && 616 (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) != 0) 617 return (EINVAL); 618 619 /* XXX skip other field checks? */ 620 } 621 622 if ((params && (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0) || 623 (IEEE80211_IS_PROTECTED(wh))) { 624 int subtype; 625 626 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 627 628 /* 629 * See IEEE Std 802.11-2012, 630 * 8.2.4.1.9 'Protected Frame field' 631 */ 632 /* XXX no support for robust management frames yet. */ 633 if (!(type == IEEE80211_FC0_TYPE_DATA || 634 (type == IEEE80211_FC0_TYPE_MGT && 635 subtype == IEEE80211_FC0_SUBTYPE_AUTH))) 636 return (EINVAL); 637 638 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED; 639 } 640 641 if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh)) 642 return (EINVAL); 643 644 return (0); 645 } 646 647 static int 648 ieee80211_validate_rate(struct ieee80211_node *ni, uint8_t rate) 649 { 650 struct ieee80211com *ic = ni->ni_ic; 651 652 if (IEEE80211_IS_HT_RATE(rate)) { 653 if ((ic->ic_htcaps & IEEE80211_HTC_HT) == 0) 654 return (EINVAL); 655 656 rate = IEEE80211_RV(rate); 657 if (rate <= 31) { 658 if (rate > ic->ic_txstream * 8 - 1) 659 return (EINVAL); 660 661 return (0); 662 } 663 664 if (rate == 32) { 665 if ((ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0) 666 return (EINVAL); 667 668 return (0); 669 } 670 671 if ((ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) == 0) 672 return (EINVAL); 673 674 switch (ic->ic_txstream) { 675 case 0: 676 case 1: 677 return (EINVAL); 678 case 2: 679 if (rate > 38) 680 return (EINVAL); 681 682 return (0); 683 case 3: 684 if (rate > 52) 685 return (EINVAL); 686 687 return (0); 688 case 4: 689 default: 690 if (rate > 76) 691 return (EINVAL); 692 693 return (0); 694 } 695 } 696 697 if (!ieee80211_isratevalid(ic->ic_rt, rate)) 698 return (EINVAL); 699 700 return (0); 701 } 702 703 static int 704 ieee80211_sanitize_rates(struct ieee80211_node *ni, struct mbuf *m, 705 const struct ieee80211_bpf_params *params) 706 { 707 int error; 708 709 if (!params) 710 return (0); /* nothing to do */ 711 712 /* NB: most drivers assume that ibp_rate0 is set (!= 0). */ 713 if (params->ibp_rate0 != 0) { 714 error = ieee80211_validate_rate(ni, params->ibp_rate0); 715 if (error != 0) 716 return (error); 717 } else { 718 /* XXX pre-setup some default (e.g., mgmt / mcast) rate */ 719 /* XXX __DECONST? */ 720 (void) m; 721 } 722 723 if (params->ibp_rate1 != 0 && 724 (error = ieee80211_validate_rate(ni, params->ibp_rate1)) != 0) 725 return (error); 726 727 if (params->ibp_rate2 != 0 && 728 (error = ieee80211_validate_rate(ni, params->ibp_rate2)) != 0) 729 return (error); 730 731 if (params->ibp_rate3 != 0 && 732 (error = ieee80211_validate_rate(ni, params->ibp_rate3)) != 0) 733 return (error); 734 735 return (0); 736 } 737 738 /* 739 * 802.11 output routine. This is (currently) used only to 740 * connect bpf write calls to the 802.11 layer for injecting 741 * raw 802.11 frames. 742 */ 743 int 744 ieee80211_output(struct ifnet *ifp, struct mbuf *m, 745 const struct sockaddr *dst, struct route *ro) 746 { 747 #define senderr(e) do { error = (e); goto bad;} while (0) 748 const struct ieee80211_bpf_params *params = NULL; 749 struct ieee80211_node *ni = NULL; 750 struct ieee80211vap *vap; 751 struct ieee80211_frame *wh; 752 struct ieee80211com *ic = NULL; 753 int error; 754 int ret; 755 756 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) { 757 /* 758 * Short-circuit requests if the vap is marked OACTIVE 759 * as this can happen because a packet came down through 760 * ieee80211_start before the vap entered RUN state in 761 * which case it's ok to just drop the frame. This 762 * should not be necessary but callers of if_output don't 763 * check OACTIVE. 764 */ 765 senderr(ENETDOWN); 766 } 767 vap = ifp->if_softc; 768 ic = vap->iv_ic; 769 /* 770 * Hand to the 802.3 code if not tagged as 771 * a raw 802.11 frame. 772 */ 773 if (dst->sa_family != AF_IEEE80211) 774 return vap->iv_output(ifp, m, dst, ro); 775 #ifdef MAC 776 error = mac_ifnet_check_transmit(ifp, m); 777 if (error) 778 senderr(error); 779 #endif 780 if (ifp->if_flags & IFF_MONITOR) 781 senderr(ENETDOWN); 782 if (!IFNET_IS_UP_RUNNING(ifp)) 783 senderr(ENETDOWN); 784 if (vap->iv_state == IEEE80211_S_CAC) { 785 IEEE80211_DPRINTF(vap, 786 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 787 "block %s frame in CAC state\n", "raw data"); 788 vap->iv_stats.is_tx_badstate++; 789 senderr(EIO); /* XXX */ 790 } else if (vap->iv_state == IEEE80211_S_SCAN) 791 senderr(EIO); 792 /* XXX bypass bridge, pfil, carp, etc. */ 793 794 /* 795 * NB: DLT_IEEE802_11_RADIO identifies the parameters are 796 * present by setting the sa_len field of the sockaddr (yes, 797 * this is a hack). 798 * NB: we assume sa_data is suitably aligned to cast. 799 */ 800 if (dst->sa_len != 0) 801 params = (const struct ieee80211_bpf_params *)dst->sa_data; 802 803 error = ieee80211_validate_frame(m, params); 804 if (error != 0) 805 senderr(error); 806 807 wh = mtod(m, struct ieee80211_frame *); 808 809 /* locate destination node */ 810 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 811 case IEEE80211_FC1_DIR_NODS: 812 case IEEE80211_FC1_DIR_FROMDS: 813 ni = ieee80211_find_txnode(vap, wh->i_addr1); 814 break; 815 case IEEE80211_FC1_DIR_TODS: 816 case IEEE80211_FC1_DIR_DSTODS: 817 ni = ieee80211_find_txnode(vap, wh->i_addr3); 818 break; 819 default: 820 senderr(EDOOFUS); 821 } 822 if (ni == NULL) { 823 /* 824 * Permit packets w/ bpf params through regardless 825 * (see below about sa_len). 826 */ 827 if (dst->sa_len == 0) 828 senderr(EHOSTUNREACH); 829 ni = ieee80211_ref_node(vap->iv_bss); 830 } 831 832 /* 833 * Sanitize mbuf for net80211 flags leaked from above. 834 * 835 * NB: This must be done before ieee80211_classify as 836 * it marks EAPOL in frames with M_EAPOL. 837 */ 838 m->m_flags &= ~M_80211_TX; 839 m->m_flags |= M_ENCAP; /* mark encapsulated */ 840 841 if (IEEE80211_IS_DATA(wh)) { 842 /* calculate priority so drivers can find the tx queue */ 843 if (ieee80211_classify(ni, m)) 844 senderr(EIO); /* XXX */ 845 846 /* NB: ieee80211_encap does not include 802.11 header */ 847 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, 848 m->m_pkthdr.len - ieee80211_hdrsize(wh)); 849 } else 850 M_WME_SETAC(m, WME_AC_BE); 851 852 error = ieee80211_sanitize_rates(ni, m, params); 853 if (error != 0) 854 senderr(error); 855 856 IEEE80211_NODE_STAT(ni, tx_data); 857 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 858 IEEE80211_NODE_STAT(ni, tx_mcast); 859 m->m_flags |= M_MCAST; 860 } else 861 IEEE80211_NODE_STAT(ni, tx_ucast); 862 863 IEEE80211_TX_LOCK(ic); 864 ret = ieee80211_raw_output(vap, ni, m, params); 865 IEEE80211_TX_UNLOCK(ic); 866 return (ret); 867 bad: 868 if (m != NULL) 869 m_freem(m); 870 if (ni != NULL) 871 ieee80211_free_node(ni); 872 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 873 return error; 874 #undef senderr 875 } 876 877 /* 878 * Set the direction field and address fields of an outgoing 879 * frame. Note this should be called early on in constructing 880 * a frame as it sets i_fc[1]; other bits can then be or'd in. 881 */ 882 void 883 ieee80211_send_setup( 884 struct ieee80211_node *ni, 885 struct mbuf *m, 886 int type, int tid, 887 const uint8_t sa[IEEE80211_ADDR_LEN], 888 const uint8_t da[IEEE80211_ADDR_LEN], 889 const uint8_t bssid[IEEE80211_ADDR_LEN]) 890 { 891 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh) 892 struct ieee80211vap *vap = ni->ni_vap; 893 struct ieee80211_tx_ampdu *tap; 894 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *); 895 ieee80211_seq seqno; 896 897 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic); 898 899 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type; 900 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) { 901 switch (vap->iv_opmode) { 902 case IEEE80211_M_STA: 903 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 904 IEEE80211_ADDR_COPY(wh->i_addr1, bssid); 905 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 906 IEEE80211_ADDR_COPY(wh->i_addr3, da); 907 break; 908 case IEEE80211_M_IBSS: 909 case IEEE80211_M_AHDEMO: 910 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 911 IEEE80211_ADDR_COPY(wh->i_addr1, da); 912 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 913 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 914 break; 915 case IEEE80211_M_HOSTAP: 916 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 917 IEEE80211_ADDR_COPY(wh->i_addr1, da); 918 IEEE80211_ADDR_COPY(wh->i_addr2, bssid); 919 IEEE80211_ADDR_COPY(wh->i_addr3, sa); 920 break; 921 case IEEE80211_M_WDS: 922 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 923 IEEE80211_ADDR_COPY(wh->i_addr1, da); 924 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 925 IEEE80211_ADDR_COPY(wh->i_addr3, da); 926 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa); 927 break; 928 case IEEE80211_M_MBSS: 929 #ifdef IEEE80211_SUPPORT_MESH 930 if (IEEE80211_IS_MULTICAST(da)) { 931 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 932 /* XXX next hop */ 933 IEEE80211_ADDR_COPY(wh->i_addr1, da); 934 IEEE80211_ADDR_COPY(wh->i_addr2, 935 vap->iv_myaddr); 936 } else { 937 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 938 IEEE80211_ADDR_COPY(wh->i_addr1, da); 939 IEEE80211_ADDR_COPY(wh->i_addr2, 940 vap->iv_myaddr); 941 IEEE80211_ADDR_COPY(wh->i_addr3, da); 942 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa); 943 } 944 #endif 945 break; 946 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */ 947 break; 948 } 949 } else { 950 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 951 IEEE80211_ADDR_COPY(wh->i_addr1, da); 952 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 953 #ifdef IEEE80211_SUPPORT_MESH 954 if (vap->iv_opmode == IEEE80211_M_MBSS) 955 IEEE80211_ADDR_COPY(wh->i_addr3, sa); 956 else 957 #endif 958 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 959 } 960 *(uint16_t *)&wh->i_dur[0] = 0; 961 962 /* 963 * XXX TODO: this is what the TX lock is for. 964 * Here we're incrementing sequence numbers, and they 965 * need to be in lock-step with what the driver is doing 966 * both in TX ordering and crypto encap (IV increment.) 967 * 968 * If the driver does seqno itself, then we can skip 969 * assigning sequence numbers here, and we can avoid 970 * requiring the TX lock. 971 */ 972 tap = &ni->ni_tx_ampdu[tid]; 973 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap)) { 974 m->m_flags |= M_AMPDU_MPDU; 975 976 /* NB: zero out i_seq field (for s/w encryption etc) */ 977 *(uint16_t *)&wh->i_seq[0] = 0; 978 } else { 979 if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK, 980 type & IEEE80211_FC0_SUBTYPE_MASK)) 981 /* 982 * 802.11-2012 9.3.2.10 - QoS multicast frames 983 * come out of a different seqno space. 984 */ 985 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 986 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 987 } else { 988 seqno = ni->ni_txseqs[tid]++; 989 } 990 else 991 seqno = 0; 992 993 *(uint16_t *)&wh->i_seq[0] = 994 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 995 M_SEQNO_SET(m, seqno); 996 } 997 998 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 999 m->m_flags |= M_MCAST; 1000 #undef WH4 1001 } 1002 1003 /* 1004 * Send a management frame to the specified node. The node pointer 1005 * must have a reference as the pointer will be passed to the driver 1006 * and potentially held for a long time. If the frame is successfully 1007 * dispatched to the driver, then it is responsible for freeing the 1008 * reference (and potentially free'ing up any associated storage); 1009 * otherwise deal with reclaiming any reference (on error). 1010 */ 1011 int 1012 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type, 1013 struct ieee80211_bpf_params *params) 1014 { 1015 struct ieee80211vap *vap = ni->ni_vap; 1016 struct ieee80211com *ic = ni->ni_ic; 1017 struct ieee80211_frame *wh; 1018 int ret; 1019 1020 KASSERT(ni != NULL, ("null node")); 1021 1022 if (vap->iv_state == IEEE80211_S_CAC) { 1023 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 1024 ni, "block %s frame in CAC state", 1025 ieee80211_mgt_subtype_name(type)); 1026 vap->iv_stats.is_tx_badstate++; 1027 ieee80211_free_node(ni); 1028 m_freem(m); 1029 return EIO; /* XXX */ 1030 } 1031 1032 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT); 1033 if (m == NULL) { 1034 ieee80211_free_node(ni); 1035 return ENOMEM; 1036 } 1037 1038 IEEE80211_TX_LOCK(ic); 1039 1040 wh = mtod(m, struct ieee80211_frame *); 1041 ieee80211_send_setup(ni, m, 1042 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID, 1043 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 1044 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) { 1045 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1, 1046 "encrypting frame (%s)", __func__); 1047 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED; 1048 } 1049 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1050 1051 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?")); 1052 M_WME_SETAC(m, params->ibp_pri); 1053 1054 #ifdef IEEE80211_DEBUG 1055 /* avoid printing too many frames */ 1056 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) || 1057 ieee80211_msg_dumppkts(vap)) { 1058 ieee80211_note(vap, "[%s] send %s on channel %u\n", 1059 ether_sprintf(wh->i_addr1), 1060 ieee80211_mgt_subtype_name(type), 1061 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1062 } 1063 #endif 1064 IEEE80211_NODE_STAT(ni, tx_mgmt); 1065 1066 ret = ieee80211_raw_output(vap, ni, m, params); 1067 IEEE80211_TX_UNLOCK(ic); 1068 return (ret); 1069 } 1070 1071 static void 1072 ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg, 1073 int status) 1074 { 1075 struct ieee80211vap *vap = ni->ni_vap; 1076 1077 wakeup(vap); 1078 } 1079 1080 /* 1081 * Send a null data frame to the specified node. If the station 1082 * is setup for QoS then a QoS Null Data frame is constructed. 1083 * If this is a WDS station then a 4-address frame is constructed. 1084 * 1085 * NB: the caller is assumed to have setup a node reference 1086 * for use; this is necessary to deal with a race condition 1087 * when probing for inactive stations. Like ieee80211_mgmt_output 1088 * we must cleanup any node reference on error; however we 1089 * can safely just unref it as we know it will never be the 1090 * last reference to the node. 1091 */ 1092 int 1093 ieee80211_send_nulldata(struct ieee80211_node *ni) 1094 { 1095 struct ieee80211vap *vap = ni->ni_vap; 1096 struct ieee80211com *ic = ni->ni_ic; 1097 struct mbuf *m; 1098 struct ieee80211_frame *wh; 1099 int hdrlen; 1100 uint8_t *frm; 1101 int ret; 1102 1103 if (vap->iv_state == IEEE80211_S_CAC) { 1104 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 1105 ni, "block %s frame in CAC state", "null data"); 1106 ieee80211_node_decref(ni); 1107 vap->iv_stats.is_tx_badstate++; 1108 return EIO; /* XXX */ 1109 } 1110 1111 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) 1112 hdrlen = sizeof(struct ieee80211_qosframe); 1113 else 1114 hdrlen = sizeof(struct ieee80211_frame); 1115 /* NB: only WDS vap's get 4-address frames */ 1116 if (vap->iv_opmode == IEEE80211_M_WDS) 1117 hdrlen += IEEE80211_ADDR_LEN; 1118 if (ic->ic_flags & IEEE80211_F_DATAPAD) 1119 hdrlen = roundup(hdrlen, sizeof(uint32_t)); 1120 1121 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0); 1122 if (m == NULL) { 1123 /* XXX debug msg */ 1124 ieee80211_node_decref(ni); 1125 vap->iv_stats.is_tx_nobuf++; 1126 return ENOMEM; 1127 } 1128 KASSERT(M_LEADINGSPACE(m) >= hdrlen, 1129 ("leading space %zd", M_LEADINGSPACE(m))); 1130 M_PREPEND(m, hdrlen, IEEE80211_M_NOWAIT); 1131 if (m == NULL) { 1132 /* NB: cannot happen */ 1133 ieee80211_free_node(ni); 1134 return ENOMEM; 1135 } 1136 1137 IEEE80211_TX_LOCK(ic); 1138 1139 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */ 1140 if (ni->ni_flags & IEEE80211_NODE_QOS) { 1141 const int tid = WME_AC_TO_TID(WME_AC_BE); 1142 uint8_t *qos; 1143 1144 ieee80211_send_setup(ni, m, 1145 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL, 1146 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 1147 1148 if (vap->iv_opmode == IEEE80211_M_WDS) 1149 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 1150 else 1151 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 1152 qos[0] = tid & IEEE80211_QOS_TID; 1153 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy) 1154 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 1155 qos[1] = 0; 1156 } else { 1157 ieee80211_send_setup(ni, m, 1158 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA, 1159 IEEE80211_NONQOS_TID, 1160 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 1161 } 1162 if (vap->iv_opmode != IEEE80211_M_WDS) { 1163 /* NB: power management bit is never sent by an AP */ 1164 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 1165 vap->iv_opmode != IEEE80211_M_HOSTAP) 1166 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT; 1167 } 1168 if ((ic->ic_flags & IEEE80211_F_SCAN) && 1169 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) { 1170 ieee80211_add_callback(m, ieee80211_nulldata_transmitted, 1171 NULL); 1172 } 1173 m->m_len = m->m_pkthdr.len = hdrlen; 1174 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1175 1176 M_WME_SETAC(m, WME_AC_BE); 1177 1178 IEEE80211_NODE_STAT(ni, tx_data); 1179 1180 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni, 1181 "send %snull data frame on channel %u, pwr mgt %s", 1182 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "", 1183 ieee80211_chan2ieee(ic, ic->ic_curchan), 1184 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis"); 1185 1186 ret = ieee80211_raw_output(vap, ni, m, NULL); 1187 IEEE80211_TX_UNLOCK(ic); 1188 return (ret); 1189 } 1190 1191 /* 1192 * Assign priority to a frame based on any vlan tag assigned 1193 * to the station and/or any Diffserv setting in an IP header. 1194 * Finally, if an ACM policy is setup (in station mode) it's 1195 * applied. 1196 */ 1197 int 1198 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m) 1199 { 1200 const struct ether_header *eh = NULL; 1201 uint16_t ether_type; 1202 int v_wme_ac, d_wme_ac, ac; 1203 1204 if (__predict_false(m->m_flags & M_ENCAP)) { 1205 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *); 1206 struct llc *llc; 1207 int hdrlen, subtype; 1208 1209 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1210 if (subtype & IEEE80211_FC0_SUBTYPE_NODATA) { 1211 ac = WME_AC_BE; 1212 goto done; 1213 } 1214 1215 hdrlen = ieee80211_hdrsize(wh); 1216 if (m->m_pkthdr.len < hdrlen + sizeof(*llc)) 1217 return 1; 1218 1219 llc = (struct llc *)mtodo(m, hdrlen); 1220 if (llc->llc_dsap != LLC_SNAP_LSAP || 1221 llc->llc_ssap != LLC_SNAP_LSAP || 1222 llc->llc_control != LLC_UI || 1223 llc->llc_snap.org_code[0] != 0 || 1224 llc->llc_snap.org_code[1] != 0 || 1225 llc->llc_snap.org_code[2] != 0) 1226 return 1; 1227 1228 ether_type = llc->llc_snap.ether_type; 1229 } else { 1230 eh = mtod(m, struct ether_header *); 1231 ether_type = eh->ether_type; 1232 } 1233 1234 /* 1235 * Always promote PAE/EAPOL frames to high priority. 1236 */ 1237 if (ether_type == htons(ETHERTYPE_PAE)) { 1238 /* NB: mark so others don't need to check header */ 1239 m->m_flags |= M_EAPOL; 1240 ac = WME_AC_VO; 1241 goto done; 1242 } 1243 /* 1244 * Non-qos traffic goes to BE. 1245 */ 1246 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) { 1247 ac = WME_AC_BE; 1248 goto done; 1249 } 1250 1251 /* 1252 * If node has a vlan tag then all traffic 1253 * to it must have a matching tag. 1254 */ 1255 v_wme_ac = 0; 1256 if (ni->ni_vlan != 0) { 1257 if ((m->m_flags & M_VLANTAG) == 0) { 1258 IEEE80211_NODE_STAT(ni, tx_novlantag); 1259 return 1; 1260 } 1261 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 1262 EVL_VLANOFTAG(ni->ni_vlan)) { 1263 IEEE80211_NODE_STAT(ni, tx_vlanmismatch); 1264 return 1; 1265 } 1266 /* map vlan priority to AC */ 1267 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan)); 1268 } 1269 1270 if (eh == NULL) 1271 goto no_eh; 1272 1273 /* XXX m_copydata may be too slow for fast path */ 1274 switch (ntohs(eh->ether_type)) { 1275 #ifdef INET 1276 case ETHERTYPE_IP: 1277 { 1278 uint8_t tos; 1279 /* 1280 * IP frame, map the DSCP bits from the TOS field. 1281 */ 1282 /* NB: ip header may not be in first mbuf */ 1283 m_copydata(m, sizeof(struct ether_header) + 1284 offsetof(struct ip, ip_tos), sizeof(tos), &tos); 1285 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */ 1286 d_wme_ac = TID_TO_WME_AC(tos); 1287 break; 1288 } 1289 #endif 1290 #ifdef INET6 1291 case ETHERTYPE_IPV6: 1292 { 1293 uint32_t flow; 1294 uint8_t tos; 1295 /* 1296 * IPv6 frame, map the DSCP bits from the traffic class field. 1297 */ 1298 m_copydata(m, sizeof(struct ether_header) + 1299 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow), 1300 (caddr_t) &flow); 1301 tos = (uint8_t)(ntohl(flow) >> 20); 1302 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */ 1303 d_wme_ac = TID_TO_WME_AC(tos); 1304 break; 1305 } 1306 #endif 1307 default: 1308 no_eh: 1309 d_wme_ac = WME_AC_BE; 1310 break; 1311 } 1312 1313 /* 1314 * Use highest priority AC. 1315 */ 1316 if (v_wme_ac > d_wme_ac) 1317 ac = v_wme_ac; 1318 else 1319 ac = d_wme_ac; 1320 1321 /* 1322 * Apply ACM policy. 1323 */ 1324 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) { 1325 static const int acmap[4] = { 1326 WME_AC_BK, /* WME_AC_BE */ 1327 WME_AC_BK, /* WME_AC_BK */ 1328 WME_AC_BE, /* WME_AC_VI */ 1329 WME_AC_VI, /* WME_AC_VO */ 1330 }; 1331 struct ieee80211com *ic = ni->ni_ic; 1332 1333 while (ac != WME_AC_BK && 1334 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm) 1335 ac = acmap[ac]; 1336 } 1337 done: 1338 M_WME_SETAC(m, ac); 1339 return 0; 1340 } 1341 1342 /* 1343 * Insure there is sufficient contiguous space to encapsulate the 1344 * 802.11 data frame. If room isn't already there, arrange for it. 1345 * Drivers and cipher modules assume we have done the necessary work 1346 * and fail rudely if they don't find the space they need. 1347 */ 1348 struct mbuf * 1349 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize, 1350 struct ieee80211_key *key, struct mbuf *m) 1351 { 1352 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc)) 1353 int needed_space = vap->iv_ic->ic_headroom + hdrsize; 1354 1355 if (key != NULL) { 1356 /* XXX belongs in crypto code? */ 1357 needed_space += key->wk_cipher->ic_header; 1358 /* XXX frags */ 1359 /* 1360 * When crypto is being done in the host we must insure 1361 * the data are writable for the cipher routines; clone 1362 * a writable mbuf chain. 1363 * XXX handle SWMIC specially 1364 */ 1365 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) { 1366 m = m_unshare(m, IEEE80211_M_NOWAIT); 1367 if (m == NULL) { 1368 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 1369 "%s: cannot get writable mbuf\n", __func__); 1370 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */ 1371 return NULL; 1372 } 1373 } 1374 } 1375 /* 1376 * We know we are called just before stripping an Ethernet 1377 * header and prepending an LLC header. This means we know 1378 * there will be 1379 * sizeof(struct ether_header) - sizeof(struct llc) 1380 * bytes recovered to which we need additional space for the 1381 * 802.11 header and any crypto header. 1382 */ 1383 /* XXX check trailing space and copy instead? */ 1384 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) { 1385 struct mbuf *n = m_gethdr(IEEE80211_M_NOWAIT, m->m_type); 1386 if (n == NULL) { 1387 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 1388 "%s: cannot expand storage\n", __func__); 1389 vap->iv_stats.is_tx_nobuf++; 1390 m_freem(m); 1391 return NULL; 1392 } 1393 KASSERT(needed_space <= MHLEN, 1394 ("not enough room, need %u got %d\n", needed_space, MHLEN)); 1395 /* 1396 * Setup new mbuf to have leading space to prepend the 1397 * 802.11 header and any crypto header bits that are 1398 * required (the latter are added when the driver calls 1399 * back to ieee80211_crypto_encap to do crypto encapsulation). 1400 */ 1401 /* NB: must be first 'cuz it clobbers m_data */ 1402 m_move_pkthdr(n, m); 1403 n->m_len = 0; /* NB: m_gethdr does not set */ 1404 n->m_data += needed_space; 1405 /* 1406 * Pull up Ethernet header to create the expected layout. 1407 * We could use m_pullup but that's overkill (i.e. we don't 1408 * need the actual data) and it cannot fail so do it inline 1409 * for speed. 1410 */ 1411 /* NB: struct ether_header is known to be contiguous */ 1412 n->m_len += sizeof(struct ether_header); 1413 m->m_len -= sizeof(struct ether_header); 1414 m->m_data += sizeof(struct ether_header); 1415 /* 1416 * Replace the head of the chain. 1417 */ 1418 n->m_next = m; 1419 m = n; 1420 } 1421 return m; 1422 #undef TO_BE_RECLAIMED 1423 } 1424 1425 /* 1426 * Return the transmit key to use in sending a unicast frame. 1427 * If a unicast key is set we use that. When no unicast key is set 1428 * we fall back to the default transmit key. 1429 */ 1430 static __inline struct ieee80211_key * 1431 ieee80211_crypto_getucastkey(struct ieee80211vap *vap, 1432 struct ieee80211_node *ni) 1433 { 1434 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 1435 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE || 1436 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey])) 1437 return NULL; 1438 return &vap->iv_nw_keys[vap->iv_def_txkey]; 1439 } else { 1440 return &ni->ni_ucastkey; 1441 } 1442 } 1443 1444 /* 1445 * Return the transmit key to use in sending a multicast frame. 1446 * Multicast traffic always uses the group key which is installed as 1447 * the default tx key. 1448 */ 1449 static __inline struct ieee80211_key * 1450 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap, 1451 struct ieee80211_node *ni) 1452 { 1453 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE || 1454 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey])) 1455 return NULL; 1456 return &vap->iv_nw_keys[vap->iv_def_txkey]; 1457 } 1458 1459 /* 1460 * Encapsulate an outbound data frame. The mbuf chain is updated. 1461 * If an error is encountered NULL is returned. The caller is required 1462 * to provide a node reference and pullup the ethernet header in the 1463 * first mbuf. 1464 * 1465 * NB: Packet is assumed to be processed by ieee80211_classify which 1466 * marked EAPOL frames w/ M_EAPOL. 1467 */ 1468 struct mbuf * 1469 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni, 1470 struct mbuf *m) 1471 { 1472 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh)) 1473 #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc) 1474 struct ieee80211com *ic = ni->ni_ic; 1475 #ifdef IEEE80211_SUPPORT_MESH 1476 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1477 struct ieee80211_meshcntl_ae10 *mc; 1478 struct ieee80211_mesh_route *rt = NULL; 1479 int dir = -1; 1480 #endif 1481 struct ether_header eh; 1482 struct ieee80211_frame *wh; 1483 struct ieee80211_key *key; 1484 struct llc *llc; 1485 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr, is_mcast; 1486 ieee80211_seq seqno; 1487 int meshhdrsize, meshae; 1488 uint8_t *qos; 1489 int is_amsdu = 0; 1490 1491 IEEE80211_TX_LOCK_ASSERT(ic); 1492 1493 is_mcast = !! (m->m_flags & (M_MCAST | M_BCAST)); 1494 1495 /* 1496 * Copy existing Ethernet header to a safe place. The 1497 * rest of the code assumes it's ok to strip it when 1498 * reorganizing state for the final encapsulation. 1499 */ 1500 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!")); 1501 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t)); 1502 1503 /* 1504 * Insure space for additional headers. First identify 1505 * transmit key to use in calculating any buffer adjustments 1506 * required. This is also used below to do privacy 1507 * encapsulation work. Then calculate the 802.11 header 1508 * size and any padding required by the driver. 1509 * 1510 * Note key may be NULL if we fall back to the default 1511 * transmit key and that is not set. In that case the 1512 * buffer may not be expanded as needed by the cipher 1513 * routines, but they will/should discard it. 1514 */ 1515 if (vap->iv_flags & IEEE80211_F_PRIVACY) { 1516 if (vap->iv_opmode == IEEE80211_M_STA || 1517 !IEEE80211_IS_MULTICAST(eh.ether_dhost) || 1518 (vap->iv_opmode == IEEE80211_M_WDS && 1519 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) { 1520 key = ieee80211_crypto_getucastkey(vap, ni); 1521 } else if ((vap->iv_opmode == IEEE80211_M_WDS) && 1522 (! (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) { 1523 /* 1524 * Use ucastkey for DWDS transmit nodes, multicast 1525 * or otherwise. 1526 * 1527 * This is required to ensure that multicast frames 1528 * from a DWDS AP to a DWDS STA is encrypted with 1529 * a key that can actually work. 1530 * 1531 * There's no default key for multicast traffic 1532 * on a DWDS WDS VAP node (note NOT the DWDS enabled 1533 * AP VAP, the dynamically created per-STA WDS node) 1534 * so encap fails and transmit fails. 1535 */ 1536 key = ieee80211_crypto_getucastkey(vap, ni); 1537 } else { 1538 key = ieee80211_crypto_getmcastkey(vap, ni); 1539 } 1540 if (key == NULL && (m->m_flags & M_EAPOL) == 0) { 1541 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 1542 eh.ether_dhost, 1543 "no default transmit key (%s) deftxkey %u", 1544 __func__, vap->iv_def_txkey); 1545 vap->iv_stats.is_tx_nodefkey++; 1546 goto bad; 1547 } 1548 } else 1549 key = NULL; 1550 /* 1551 * XXX Some ap's don't handle QoS-encapsulated EAPOL 1552 * frames so suppress use. This may be an issue if other 1553 * ap's require all data frames to be QoS-encapsulated 1554 * once negotiated in which case we'll need to make this 1555 * configurable. 1556 * 1557 * Don't send multicast QoS frames. 1558 * Technically multicast frames can be QoS if all stations in the 1559 * BSS are also QoS. 1560 * 1561 * NB: mesh data frames are QoS, including multicast frames. 1562 */ 1563 addqos = 1564 (((is_mcast == 0) && (ni->ni_flags & 1565 (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))) || 1566 (vap->iv_opmode == IEEE80211_M_MBSS)) && 1567 (m->m_flags & M_EAPOL) == 0; 1568 1569 if (addqos) 1570 hdrsize = sizeof(struct ieee80211_qosframe); 1571 else 1572 hdrsize = sizeof(struct ieee80211_frame); 1573 #ifdef IEEE80211_SUPPORT_MESH 1574 if (vap->iv_opmode == IEEE80211_M_MBSS) { 1575 /* 1576 * Mesh data frames are encapsulated according to the 1577 * rules of Section 11B.8.5 (p.139 of D3.0 spec). 1578 * o Group Addressed data (aka multicast) originating 1579 * at the local sta are sent w/ 3-address format and 1580 * address extension mode 00 1581 * o Individually Addressed data (aka unicast) originating 1582 * at the local sta are sent w/ 4-address format and 1583 * address extension mode 00 1584 * o Group Addressed data forwarded from a non-mesh sta are 1585 * sent w/ 3-address format and address extension mode 01 1586 * o Individually Address data from another sta are sent 1587 * w/ 4-address format and address extension mode 10 1588 */ 1589 is4addr = 0; /* NB: don't use, disable */ 1590 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) { 1591 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost); 1592 KASSERT(rt != NULL, ("route is NULL")); 1593 dir = IEEE80211_FC1_DIR_DSTODS; 1594 hdrsize += IEEE80211_ADDR_LEN; 1595 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) { 1596 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate, 1597 vap->iv_myaddr)) { 1598 IEEE80211_NOTE_MAC(vap, 1599 IEEE80211_MSG_MESH, 1600 eh.ether_dhost, 1601 "%s", "trying to send to ourself"); 1602 goto bad; 1603 } 1604 meshae = IEEE80211_MESH_AE_10; 1605 meshhdrsize = 1606 sizeof(struct ieee80211_meshcntl_ae10); 1607 } else { 1608 meshae = IEEE80211_MESH_AE_00; 1609 meshhdrsize = 1610 sizeof(struct ieee80211_meshcntl); 1611 } 1612 } else { 1613 dir = IEEE80211_FC1_DIR_FROMDS; 1614 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) { 1615 /* proxy group */ 1616 meshae = IEEE80211_MESH_AE_01; 1617 meshhdrsize = 1618 sizeof(struct ieee80211_meshcntl_ae01); 1619 } else { 1620 /* group */ 1621 meshae = IEEE80211_MESH_AE_00; 1622 meshhdrsize = sizeof(struct ieee80211_meshcntl); 1623 } 1624 } 1625 } else { 1626 #endif 1627 /* 1628 * 4-address frames need to be generated for: 1629 * o packets sent through a WDS vap (IEEE80211_M_WDS) 1630 * o packets sent through a vap marked for relaying 1631 * (e.g. a station operating with dynamic WDS) 1632 */ 1633 is4addr = vap->iv_opmode == IEEE80211_M_WDS || 1634 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) && 1635 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)); 1636 if (is4addr) 1637 hdrsize += IEEE80211_ADDR_LEN; 1638 meshhdrsize = meshae = 0; 1639 #ifdef IEEE80211_SUPPORT_MESH 1640 } 1641 #endif 1642 /* 1643 * Honor driver DATAPAD requirement. 1644 */ 1645 if (ic->ic_flags & IEEE80211_F_DATAPAD) 1646 hdrspace = roundup(hdrsize, sizeof(uint32_t)); 1647 else 1648 hdrspace = hdrsize; 1649 1650 if (__predict_true((m->m_flags & M_FF) == 0)) { 1651 /* 1652 * Normal frame. 1653 */ 1654 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m); 1655 if (m == NULL) { 1656 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 1657 goto bad; 1658 } 1659 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */ 1660 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 1661 llc = mtod(m, struct llc *); 1662 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 1663 llc->llc_control = LLC_UI; 1664 llc->llc_snap.org_code[0] = 0; 1665 llc->llc_snap.org_code[1] = 0; 1666 llc->llc_snap.org_code[2] = 0; 1667 llc->llc_snap.ether_type = eh.ether_type; 1668 } else { 1669 #ifdef IEEE80211_SUPPORT_SUPERG 1670 /* 1671 * Aggregated frame. Check if it's for AMSDU or FF. 1672 * 1673 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented 1674 * anywhere for some reason. But, since 11n requires 1675 * AMSDU RX, we can just assume "11n" == "AMSDU". 1676 */ 1677 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__); 1678 if (ieee80211_amsdu_tx_ok(ni)) { 1679 m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key); 1680 is_amsdu = 1; 1681 } else { 1682 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key); 1683 } 1684 if (m == NULL) 1685 #endif 1686 goto bad; 1687 } 1688 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */ 1689 1690 M_PREPEND(m, hdrspace + meshhdrsize, IEEE80211_M_NOWAIT); 1691 if (m == NULL) { 1692 vap->iv_stats.is_tx_nobuf++; 1693 goto bad; 1694 } 1695 wh = mtod(m, struct ieee80211_frame *); 1696 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 1697 *(uint16_t *)wh->i_dur = 0; 1698 qos = NULL; /* NB: quiet compiler */ 1699 if (is4addr) { 1700 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 1701 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr); 1702 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1703 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 1704 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost); 1705 } else switch (vap->iv_opmode) { 1706 case IEEE80211_M_STA: 1707 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 1708 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 1709 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 1710 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 1711 break; 1712 case IEEE80211_M_IBSS: 1713 case IEEE80211_M_AHDEMO: 1714 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1715 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1716 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 1717 /* 1718 * NB: always use the bssid from iv_bss as the 1719 * neighbor's may be stale after an ibss merge 1720 */ 1721 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid); 1722 break; 1723 case IEEE80211_M_HOSTAP: 1724 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 1725 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1726 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 1727 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 1728 break; 1729 #ifdef IEEE80211_SUPPORT_MESH 1730 case IEEE80211_M_MBSS: 1731 /* NB: offset by hdrspace to deal with DATAPAD */ 1732 mc = (struct ieee80211_meshcntl_ae10 *) 1733 (mtod(m, uint8_t *) + hdrspace); 1734 wh->i_fc[1] = dir; 1735 switch (meshae) { 1736 case IEEE80211_MESH_AE_00: /* no proxy */ 1737 mc->mc_flags = 0; 1738 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */ 1739 IEEE80211_ADDR_COPY(wh->i_addr1, 1740 ni->ni_macaddr); 1741 IEEE80211_ADDR_COPY(wh->i_addr2, 1742 vap->iv_myaddr); 1743 IEEE80211_ADDR_COPY(wh->i_addr3, 1744 eh.ether_dhost); 1745 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, 1746 eh.ether_shost); 1747 qos =((struct ieee80211_qosframe_addr4 *) 1748 wh)->i_qos; 1749 } else if (dir == IEEE80211_FC1_DIR_FROMDS) { 1750 /* mcast */ 1751 IEEE80211_ADDR_COPY(wh->i_addr1, 1752 eh.ether_dhost); 1753 IEEE80211_ADDR_COPY(wh->i_addr2, 1754 vap->iv_myaddr); 1755 IEEE80211_ADDR_COPY(wh->i_addr3, 1756 eh.ether_shost); 1757 qos = ((struct ieee80211_qosframe *) 1758 wh)->i_qos; 1759 } 1760 break; 1761 case IEEE80211_MESH_AE_01: /* mcast, proxy */ 1762 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 1763 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1764 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1765 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr); 1766 mc->mc_flags = 1; 1767 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4, 1768 eh.ether_shost); 1769 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 1770 break; 1771 case IEEE80211_MESH_AE_10: /* ucast, proxy */ 1772 KASSERT(rt != NULL, ("route is NULL")); 1773 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop); 1774 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1775 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate); 1776 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr); 1777 mc->mc_flags = IEEE80211_MESH_AE_10; 1778 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost); 1779 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost); 1780 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 1781 break; 1782 default: 1783 KASSERT(0, ("meshae %d", meshae)); 1784 break; 1785 } 1786 mc->mc_ttl = ms->ms_ttl; 1787 ms->ms_seq++; 1788 le32enc(mc->mc_seq, ms->ms_seq); 1789 break; 1790 #endif 1791 case IEEE80211_M_WDS: /* NB: is4addr should always be true */ 1792 default: 1793 goto bad; 1794 } 1795 if (m->m_flags & M_MORE_DATA) 1796 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 1797 if (addqos) { 1798 int ac, tid; 1799 1800 if (is4addr) { 1801 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 1802 /* NB: mesh case handled earlier */ 1803 } else if (vap->iv_opmode != IEEE80211_M_MBSS) 1804 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 1805 ac = M_WME_GETAC(m); 1806 /* map from access class/queue to 11e header priorty value */ 1807 tid = WME_AC_TO_TID(ac); 1808 qos[0] = tid & IEEE80211_QOS_TID; 1809 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy) 1810 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 1811 #ifdef IEEE80211_SUPPORT_MESH 1812 if (vap->iv_opmode == IEEE80211_M_MBSS) 1813 qos[1] = IEEE80211_QOS_MC; 1814 else 1815 #endif 1816 qos[1] = 0; 1817 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS_DATA; 1818 1819 /* 1820 * If this is an A-MSDU then ensure we set the 1821 * relevant field. 1822 */ 1823 if (is_amsdu) 1824 qos[0] |= IEEE80211_QOS_AMSDU; 1825 1826 /* 1827 * XXX TODO TX lock is needed for atomic updates of sequence 1828 * numbers. If the driver does it, then don't do it here; 1829 * and we don't need the TX lock held. 1830 */ 1831 if ((m->m_flags & M_AMPDU_MPDU) == 0) { 1832 /* 1833 * 802.11-2012 9.3.2.10 - 1834 * 1835 * If this is a multicast frame then we need 1836 * to ensure that the sequence number comes from 1837 * a separate seqno space and not the TID space. 1838 * 1839 * Otherwise multicast frames may actually cause 1840 * holes in the TX blockack window space and 1841 * upset various things. 1842 */ 1843 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1844 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 1845 else 1846 seqno = ni->ni_txseqs[tid]++; 1847 1848 /* 1849 * NB: don't assign a sequence # to potential 1850 * aggregates; we expect this happens at the 1851 * point the frame comes off any aggregation q 1852 * as otherwise we may introduce holes in the 1853 * BA sequence space and/or make window accouting 1854 * more difficult. 1855 * 1856 * XXX may want to control this with a driver 1857 * capability; this may also change when we pull 1858 * aggregation up into net80211 1859 */ 1860 *(uint16_t *)wh->i_seq = 1861 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 1862 M_SEQNO_SET(m, seqno); 1863 } else { 1864 /* NB: zero out i_seq field (for s/w encryption etc) */ 1865 *(uint16_t *)wh->i_seq = 0; 1866 } 1867 } else { 1868 /* 1869 * XXX TODO TX lock is needed for atomic updates of sequence 1870 * numbers. If the driver does it, then don't do it here; 1871 * and we don't need the TX lock held. 1872 */ 1873 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 1874 *(uint16_t *)wh->i_seq = 1875 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 1876 M_SEQNO_SET(m, seqno); 1877 1878 /* 1879 * XXX TODO: we shouldn't allow EAPOL, etc that would 1880 * be forced to be non-QoS traffic to be A-MSDU encapsulated. 1881 */ 1882 if (is_amsdu) 1883 printf("%s: XXX ERROR: is_amsdu set; not QoS!\n", 1884 __func__); 1885 } 1886 1887 /* 1888 * Check if xmit fragmentation is required. 1889 * 1890 * If the hardware does fragmentation offload, then don't bother 1891 * doing it here. 1892 */ 1893 if (IEEE80211_CONF_FRAG_OFFLOAD(ic)) 1894 txfrag = 0; 1895 else 1896 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold && 1897 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 1898 (vap->iv_caps & IEEE80211_C_TXFRAG) && 1899 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0); 1900 1901 if (key != NULL) { 1902 /* 1903 * IEEE 802.1X: send EAPOL frames always in the clear. 1904 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set. 1905 */ 1906 if ((m->m_flags & M_EAPOL) == 0 || 1907 ((vap->iv_flags & IEEE80211_F_WPA) && 1908 (vap->iv_opmode == IEEE80211_M_STA ? 1909 !IEEE80211_KEY_UNDEFINED(key) : 1910 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) { 1911 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED; 1912 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) { 1913 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, 1914 eh.ether_dhost, 1915 "%s", "enmic failed, discard frame"); 1916 vap->iv_stats.is_crypto_enmicfail++; 1917 goto bad; 1918 } 1919 } 1920 } 1921 if (txfrag && !ieee80211_fragment(vap, m, hdrsize, 1922 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold)) 1923 goto bad; 1924 1925 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1926 1927 IEEE80211_NODE_STAT(ni, tx_data); 1928 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1929 IEEE80211_NODE_STAT(ni, tx_mcast); 1930 m->m_flags |= M_MCAST; 1931 } else 1932 IEEE80211_NODE_STAT(ni, tx_ucast); 1933 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen); 1934 1935 return m; 1936 bad: 1937 if (m != NULL) 1938 m_freem(m); 1939 return NULL; 1940 #undef WH4 1941 #undef MC01 1942 } 1943 1944 void 1945 ieee80211_free_mbuf(struct mbuf *m) 1946 { 1947 struct mbuf *next; 1948 1949 if (m == NULL) 1950 return; 1951 1952 do { 1953 next = m->m_nextpkt; 1954 m->m_nextpkt = NULL; 1955 m_freem(m); 1956 } while ((m = next) != NULL); 1957 } 1958 1959 /* 1960 * Fragment the frame according to the specified mtu. 1961 * The size of the 802.11 header (w/o padding) is provided 1962 * so we don't need to recalculate it. We create a new 1963 * mbuf for each fragment and chain it through m_nextpkt; 1964 * we might be able to optimize this by reusing the original 1965 * packet's mbufs but that is significantly more complicated. 1966 */ 1967 static int 1968 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0, 1969 u_int hdrsize, u_int ciphdrsize, u_int mtu) 1970 { 1971 struct ieee80211com *ic = vap->iv_ic; 1972 struct ieee80211_frame *wh, *whf; 1973 struct mbuf *m, *prev; 1974 u_int totalhdrsize, fragno, fragsize, off, remainder, payload; 1975 u_int hdrspace; 1976 1977 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?")); 1978 KASSERT(m0->m_pkthdr.len > mtu, 1979 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu)); 1980 1981 /* 1982 * Honor driver DATAPAD requirement. 1983 */ 1984 if (ic->ic_flags & IEEE80211_F_DATAPAD) 1985 hdrspace = roundup(hdrsize, sizeof(uint32_t)); 1986 else 1987 hdrspace = hdrsize; 1988 1989 wh = mtod(m0, struct ieee80211_frame *); 1990 /* NB: mark the first frag; it will be propagated below */ 1991 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG; 1992 totalhdrsize = hdrspace + ciphdrsize; 1993 fragno = 1; 1994 off = mtu - ciphdrsize; 1995 remainder = m0->m_pkthdr.len - off; 1996 prev = m0; 1997 do { 1998 fragsize = MIN(totalhdrsize + remainder, mtu); 1999 m = m_get2(fragsize, IEEE80211_M_NOWAIT, MT_DATA, M_PKTHDR); 2000 if (m == NULL) 2001 goto bad; 2002 /* leave room to prepend any cipher header */ 2003 m_align(m, fragsize - ciphdrsize); 2004 2005 /* 2006 * Form the header in the fragment. Note that since 2007 * we mark the first fragment with the MORE_FRAG bit 2008 * it automatically is propagated to each fragment; we 2009 * need only clear it on the last fragment (done below). 2010 * NB: frag 1+ dont have Mesh Control field present. 2011 */ 2012 whf = mtod(m, struct ieee80211_frame *); 2013 memcpy(whf, wh, hdrsize); 2014 #ifdef IEEE80211_SUPPORT_MESH 2015 if (vap->iv_opmode == IEEE80211_M_MBSS) 2016 ieee80211_getqos(wh)[1] &= ~IEEE80211_QOS_MC; 2017 #endif 2018 *(uint16_t *)&whf->i_seq[0] |= htole16( 2019 (fragno & IEEE80211_SEQ_FRAG_MASK) << 2020 IEEE80211_SEQ_FRAG_SHIFT); 2021 fragno++; 2022 2023 payload = fragsize - totalhdrsize; 2024 /* NB: destination is known to be contiguous */ 2025 2026 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace); 2027 m->m_len = hdrspace + payload; 2028 m->m_pkthdr.len = hdrspace + payload; 2029 m->m_flags |= M_FRAG; 2030 2031 /* chain up the fragment */ 2032 prev->m_nextpkt = m; 2033 prev = m; 2034 2035 /* deduct fragment just formed */ 2036 remainder -= payload; 2037 off += payload; 2038 } while (remainder != 0); 2039 2040 /* set the last fragment */ 2041 m->m_flags |= M_LASTFRAG; 2042 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG; 2043 2044 /* strip first mbuf now that everything has been copied */ 2045 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize))); 2046 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 2047 2048 vap->iv_stats.is_tx_fragframes++; 2049 vap->iv_stats.is_tx_frags += fragno-1; 2050 2051 return 1; 2052 bad: 2053 /* reclaim fragments but leave original frame for caller to free */ 2054 ieee80211_free_mbuf(m0->m_nextpkt); 2055 m0->m_nextpkt = NULL; 2056 return 0; 2057 } 2058 2059 /* 2060 * Add a supported rates element id to a frame. 2061 */ 2062 uint8_t * 2063 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs) 2064 { 2065 int nrates; 2066 2067 *frm++ = IEEE80211_ELEMID_RATES; 2068 nrates = rs->rs_nrates; 2069 if (nrates > IEEE80211_RATE_SIZE) 2070 nrates = IEEE80211_RATE_SIZE; 2071 *frm++ = nrates; 2072 memcpy(frm, rs->rs_rates, nrates); 2073 return frm + nrates; 2074 } 2075 2076 /* 2077 * Add an extended supported rates element id to a frame. 2078 */ 2079 uint8_t * 2080 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs) 2081 { 2082 /* 2083 * Add an extended supported rates element if operating in 11g mode. 2084 */ 2085 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 2086 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 2087 *frm++ = IEEE80211_ELEMID_XRATES; 2088 *frm++ = nrates; 2089 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 2090 frm += nrates; 2091 } 2092 return frm; 2093 } 2094 2095 /* 2096 * Add an ssid element to a frame. 2097 */ 2098 uint8_t * 2099 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len) 2100 { 2101 *frm++ = IEEE80211_ELEMID_SSID; 2102 *frm++ = len; 2103 memcpy(frm, ssid, len); 2104 return frm + len; 2105 } 2106 2107 /* 2108 * Add an erp element to a frame. 2109 */ 2110 static uint8_t * 2111 ieee80211_add_erp(uint8_t *frm, struct ieee80211vap *vap) 2112 { 2113 struct ieee80211com *ic = vap->iv_ic; 2114 uint8_t erp; 2115 2116 *frm++ = IEEE80211_ELEMID_ERP; 2117 *frm++ = 1; 2118 erp = 0; 2119 2120 /* 2121 * TODO: This uses the global flags for now because 2122 * the per-VAP flags are fine for per-VAP, but don't 2123 * take into account which VAPs share the same channel 2124 * and which are on different channels. 2125 * 2126 * ERP and HT/VHT protection mode is a function of 2127 * how many stations are on a channel, not specifically 2128 * the VAP or global. But, until we grow that status, 2129 * the global flag will have to do. 2130 */ 2131 if (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) 2132 erp |= IEEE80211_ERP_NON_ERP_PRESENT; 2133 2134 /* 2135 * TODO: same as above; these should be based not 2136 * on the vap or ic flags, but instead on a combination 2137 * of per-VAP and channels. 2138 */ 2139 if (ic->ic_flags & IEEE80211_F_USEPROT) 2140 erp |= IEEE80211_ERP_USE_PROTECTION; 2141 if (ic->ic_flags & IEEE80211_F_USEBARKER) 2142 erp |= IEEE80211_ERP_LONG_PREAMBLE; 2143 *frm++ = erp; 2144 return frm; 2145 } 2146 2147 /* 2148 * Add a CFParams element to a frame. 2149 */ 2150 static uint8_t * 2151 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic) 2152 { 2153 #define ADDSHORT(frm, v) do { \ 2154 le16enc(frm, v); \ 2155 frm += 2; \ 2156 } while (0) 2157 *frm++ = IEEE80211_ELEMID_CFPARMS; 2158 *frm++ = 6; 2159 *frm++ = 0; /* CFP count */ 2160 *frm++ = 2; /* CFP period */ 2161 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */ 2162 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */ 2163 return frm; 2164 #undef ADDSHORT 2165 } 2166 2167 static __inline uint8_t * 2168 add_appie(uint8_t *frm, const struct ieee80211_appie *ie) 2169 { 2170 memcpy(frm, ie->ie_data, ie->ie_len); 2171 return frm + ie->ie_len; 2172 } 2173 2174 static __inline uint8_t * 2175 add_ie(uint8_t *frm, const uint8_t *ie) 2176 { 2177 memcpy(frm, ie, 2 + ie[1]); 2178 return frm + 2 + ie[1]; 2179 } 2180 2181 #define WME_OUI_BYTES 0x00, 0x50, 0xf2 2182 /* 2183 * Add a WME information element to a frame. 2184 */ 2185 uint8_t * 2186 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme, 2187 struct ieee80211_node *ni) 2188 { 2189 static const uint8_t oui[4] = { WME_OUI_BYTES, WME_OUI_TYPE }; 2190 struct ieee80211vap *vap = ni->ni_vap; 2191 2192 *frm++ = IEEE80211_ELEMID_VENDOR; 2193 *frm++ = sizeof(struct ieee80211_wme_info) - 2; 2194 memcpy(frm, oui, sizeof(oui)); 2195 frm += sizeof(oui); 2196 *frm++ = WME_INFO_OUI_SUBTYPE; 2197 *frm++ = WME_VERSION; 2198 2199 /* QoS info field depends upon operating mode */ 2200 switch (vap->iv_opmode) { 2201 case IEEE80211_M_HOSTAP: 2202 *frm = wme->wme_bssChanParams.cap_info; 2203 if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD) 2204 *frm |= WME_CAPINFO_UAPSD_EN; 2205 frm++; 2206 break; 2207 case IEEE80211_M_STA: 2208 /* 2209 * NB: UAPSD drivers must set this up in their 2210 * VAP creation method. 2211 */ 2212 *frm++ = vap->iv_uapsdinfo; 2213 break; 2214 default: 2215 *frm++ = 0; 2216 break; 2217 } 2218 2219 return frm; 2220 } 2221 2222 /* 2223 * Add a WME parameters element to a frame. 2224 */ 2225 static uint8_t * 2226 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme, 2227 int uapsd_enable) 2228 { 2229 #define ADDSHORT(frm, v) do { \ 2230 le16enc(frm, v); \ 2231 frm += 2; \ 2232 } while (0) 2233 /* NB: this works 'cuz a param has an info at the front */ 2234 static const struct ieee80211_wme_info param = { 2235 .wme_id = IEEE80211_ELEMID_VENDOR, 2236 .wme_len = sizeof(struct ieee80211_wme_param) - 2, 2237 .wme_oui = { WME_OUI_BYTES }, 2238 .wme_type = WME_OUI_TYPE, 2239 .wme_subtype = WME_PARAM_OUI_SUBTYPE, 2240 .wme_version = WME_VERSION, 2241 }; 2242 int i; 2243 2244 memcpy(frm, ¶m, sizeof(param)); 2245 frm += __offsetof(struct ieee80211_wme_info, wme_info); 2246 *frm = wme->wme_bssChanParams.cap_info; /* AC info */ 2247 if (uapsd_enable) 2248 *frm |= WME_CAPINFO_UAPSD_EN; 2249 frm++; 2250 *frm++ = 0; /* reserved field */ 2251 /* XXX TODO - U-APSD bits - SP, flags below */ 2252 for (i = 0; i < WME_NUM_AC; i++) { 2253 const struct wmeParams *ac = 2254 &wme->wme_bssChanParams.cap_wmeParams[i]; 2255 *frm++ = _IEEE80211_SHIFTMASK(i, WME_PARAM_ACI) 2256 | _IEEE80211_SHIFTMASK(ac->wmep_acm, WME_PARAM_ACM) 2257 | _IEEE80211_SHIFTMASK(ac->wmep_aifsn, WME_PARAM_AIFSN) 2258 ; 2259 *frm++ = _IEEE80211_SHIFTMASK(ac->wmep_logcwmax, 2260 WME_PARAM_LOGCWMAX) 2261 | _IEEE80211_SHIFTMASK(ac->wmep_logcwmin, 2262 WME_PARAM_LOGCWMIN) 2263 ; 2264 ADDSHORT(frm, ac->wmep_txopLimit); 2265 } 2266 return frm; 2267 #undef ADDSHORT 2268 } 2269 #undef WME_OUI_BYTES 2270 2271 /* 2272 * Add an 11h Power Constraint element to a frame. 2273 */ 2274 static uint8_t * 2275 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap) 2276 { 2277 const struct ieee80211_channel *c = vap->iv_bss->ni_chan; 2278 /* XXX per-vap tx power limit? */ 2279 int8_t limit = vap->iv_ic->ic_txpowlimit / 2; 2280 2281 frm[0] = IEEE80211_ELEMID_PWRCNSTR; 2282 frm[1] = 1; 2283 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0; 2284 return frm + 3; 2285 } 2286 2287 /* 2288 * Add an 11h Power Capability element to a frame. 2289 */ 2290 static uint8_t * 2291 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c) 2292 { 2293 frm[0] = IEEE80211_ELEMID_PWRCAP; 2294 frm[1] = 2; 2295 frm[2] = c->ic_minpower; 2296 frm[3] = c->ic_maxpower; 2297 return frm + 4; 2298 } 2299 2300 /* 2301 * Add an 11h Supported Channels element to a frame. 2302 */ 2303 static uint8_t * 2304 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic) 2305 { 2306 static const int ielen = 26; 2307 2308 frm[0] = IEEE80211_ELEMID_SUPPCHAN; 2309 frm[1] = ielen; 2310 /* XXX not correct */ 2311 memcpy(frm+2, ic->ic_chan_avail, ielen); 2312 return frm + 2 + ielen; 2313 } 2314 2315 /* 2316 * Add an 11h Quiet time element to a frame. 2317 */ 2318 static uint8_t * 2319 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap, int update) 2320 { 2321 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm; 2322 2323 quiet->quiet_ie = IEEE80211_ELEMID_QUIET; 2324 quiet->len = 6; 2325 2326 /* 2327 * Only update every beacon interval - otherwise probe responses 2328 * would update the quiet count value. 2329 */ 2330 if (update) { 2331 if (vap->iv_quiet_count_value == 1) 2332 vap->iv_quiet_count_value = vap->iv_quiet_count; 2333 else if (vap->iv_quiet_count_value > 1) 2334 vap->iv_quiet_count_value--; 2335 } 2336 2337 if (vap->iv_quiet_count_value == 0) { 2338 /* value 0 is reserved as per 802.11h standerd */ 2339 vap->iv_quiet_count_value = 1; 2340 } 2341 2342 quiet->tbttcount = vap->iv_quiet_count_value; 2343 quiet->period = vap->iv_quiet_period; 2344 quiet->duration = htole16(vap->iv_quiet_duration); 2345 quiet->offset = htole16(vap->iv_quiet_offset); 2346 return frm + sizeof(*quiet); 2347 } 2348 2349 /* 2350 * Add an 11h Channel Switch Announcement element to a frame. 2351 * Note that we use the per-vap CSA count to adjust the global 2352 * counter so we can use this routine to form probe response 2353 * frames and get the current count. 2354 */ 2355 static uint8_t * 2356 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap) 2357 { 2358 struct ieee80211com *ic = vap->iv_ic; 2359 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm; 2360 2361 csa->csa_ie = IEEE80211_ELEMID_CSA; 2362 csa->csa_len = 3; 2363 csa->csa_mode = 1; /* XXX force quiet on channel */ 2364 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan); 2365 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count; 2366 return frm + sizeof(*csa); 2367 } 2368 2369 /* 2370 * Add an 11h country information element to a frame. 2371 */ 2372 static uint8_t * 2373 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic) 2374 { 2375 2376 if (ic->ic_countryie == NULL || 2377 ic->ic_countryie_chan != ic->ic_bsschan) { 2378 /* 2379 * Handle lazy construction of ie. This is done on 2380 * first use and after a channel change that requires 2381 * re-calculation. 2382 */ 2383 if (ic->ic_countryie != NULL) 2384 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE); 2385 ic->ic_countryie = ieee80211_alloc_countryie(ic); 2386 if (ic->ic_countryie == NULL) 2387 return frm; 2388 ic->ic_countryie_chan = ic->ic_bsschan; 2389 } 2390 return add_appie(frm, ic->ic_countryie); 2391 } 2392 2393 uint8_t * 2394 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap) 2395 { 2396 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL) 2397 return (add_ie(frm, vap->iv_wpa_ie)); 2398 else { 2399 /* XXX else complain? */ 2400 return (frm); 2401 } 2402 } 2403 2404 uint8_t * 2405 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap) 2406 { 2407 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL) 2408 return (add_ie(frm, vap->iv_rsn_ie)); 2409 else { 2410 /* XXX else complain? */ 2411 return (frm); 2412 } 2413 } 2414 2415 uint8_t * 2416 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni) 2417 { 2418 if (ni->ni_flags & IEEE80211_NODE_QOS) { 2419 *frm++ = IEEE80211_ELEMID_QOS; 2420 *frm++ = 1; 2421 *frm++ = 0; 2422 } 2423 2424 return (frm); 2425 } 2426 2427 /* 2428 * ieee80211_send_probereq(): send a probe request frame with the specified ssid 2429 * and any optional information element data; some helper functions as FW based 2430 * HW scans need some of that information passed too. 2431 */ 2432 static uint32_t 2433 ieee80211_probereq_ie_len(struct ieee80211vap *vap, struct ieee80211com *ic) 2434 { 2435 const struct ieee80211_rateset *rs; 2436 2437 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 2438 2439 /* 2440 * prreq frame format 2441 * [tlv] ssid 2442 * [tlv] supported rates 2443 * [tlv] extended supported rates (if needed) 2444 * [tlv] HT cap (optional) 2445 * [tlv] VHT cap (optional) 2446 * [tlv] WPA (optional) 2447 * [tlv] user-specified ie's 2448 */ 2449 return ( 2 + IEEE80211_NWID_LEN 2450 + 2 + IEEE80211_RATE_SIZE 2451 + ((rs->rs_nrates > IEEE80211_RATE_SIZE) ? 2452 2 + (rs->rs_nrates - IEEE80211_RATE_SIZE) : 0) 2453 + (((vap->iv_opmode == IEEE80211_M_IBSS) && 2454 (vap->iv_flags_ht & IEEE80211_FHT_HT)) ? 2455 sizeof(struct ieee80211_ie_htcap) : 0) 2456 #ifdef notyet 2457 + sizeof(struct ieee80211_ie_htinfo) /* XXX not needed? */ 2458 + 2 + sizeof(struct ieee80211_vht_cap) 2459 #endif 2460 + ((vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL) ? 2461 vap->iv_wpa_ie[1] : 0) 2462 + (vap->iv_appie_probereq != NULL ? 2463 vap->iv_appie_probereq->ie_len : 0) 2464 ); 2465 } 2466 2467 int 2468 ieee80211_probereq_ie(struct ieee80211vap *vap, struct ieee80211com *ic, 2469 uint8_t **frmp, uint32_t *frmlen, const uint8_t *ssid, size_t ssidlen, 2470 bool alloc) 2471 { 2472 const struct ieee80211_rateset *rs; 2473 uint8_t *frm; 2474 uint32_t len; 2475 2476 if (!alloc && (frmp == NULL || frmlen == NULL)) 2477 return (EINVAL); 2478 2479 len = ieee80211_probereq_ie_len(vap, ic); 2480 if (!alloc && len > *frmlen) 2481 return (ENOBUFS); 2482 2483 /* For HW scans we usually do not pass in the SSID as IE. */ 2484 if (ssidlen == -1) 2485 len -= (2 + IEEE80211_NWID_LEN); 2486 2487 if (alloc) { 2488 frm = IEEE80211_MALLOC(len, M_80211_VAP, 2489 IEEE80211_M_WAITOK | IEEE80211_M_ZERO); 2490 *frmp = frm; 2491 *frmlen = len; 2492 } else 2493 frm = *frmp; 2494 2495 if (ssidlen != -1) 2496 frm = ieee80211_add_ssid(frm, ssid, ssidlen); 2497 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 2498 frm = ieee80211_add_rates(frm, rs); 2499 frm = ieee80211_add_xrates(frm, rs); 2500 2501 /* 2502 * Note: we can't use bss; we don't have one yet. 2503 * 2504 * So, we should announce our capabilities 2505 * in this channel mode (2g/5g), not the 2506 * channel details itself. 2507 */ 2508 if ((vap->iv_opmode == IEEE80211_M_IBSS) && 2509 (vap->iv_flags_ht & IEEE80211_FHT_HT)) { 2510 struct ieee80211_channel *c; 2511 2512 /* 2513 * Get the HT channel that we should try upgrading to. 2514 * If we can do 40MHz then this'll upgrade it appropriately. 2515 */ 2516 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, 2517 vap->iv_flags_ht); 2518 frm = ieee80211_add_htcap_ch(frm, vap, c); 2519 } 2520 2521 /* 2522 * XXX TODO: need to figure out what/how to update the 2523 * VHT channel. 2524 */ 2525 #ifdef notyet 2526 if (vap->iv_vht_flags & IEEE80211_FVHT_VHT) { 2527 struct ieee80211_channel *c; 2528 2529 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, 2530 vap->iv_flags_ht); 2531 c = ieee80211_vht_adjust_channel(ic, c, vap->iv_vht_flags); 2532 frm = ieee80211_add_vhtcap_ch(frm, vap, c); 2533 } 2534 #endif 2535 2536 frm = ieee80211_add_wpa(frm, vap); 2537 if (vap->iv_appie_probereq != NULL) 2538 frm = add_appie(frm, vap->iv_appie_probereq); 2539 2540 if (!alloc) { 2541 *frmp = frm; 2542 *frmlen = len; 2543 } 2544 2545 return (0); 2546 } 2547 2548 int 2549 ieee80211_send_probereq(struct ieee80211_node *ni, 2550 const uint8_t sa[IEEE80211_ADDR_LEN], 2551 const uint8_t da[IEEE80211_ADDR_LEN], 2552 const uint8_t bssid[IEEE80211_ADDR_LEN], 2553 const uint8_t *ssid, size_t ssidlen) 2554 { 2555 struct ieee80211vap *vap = ni->ni_vap; 2556 struct ieee80211com *ic = ni->ni_ic; 2557 struct ieee80211_node *bss; 2558 const struct ieee80211_txparam *tp; 2559 struct ieee80211_bpf_params params; 2560 struct mbuf *m; 2561 uint8_t *frm; 2562 uint32_t frmlen; 2563 int ret; 2564 2565 bss = ieee80211_ref_node(vap->iv_bss); 2566 2567 if (vap->iv_state == IEEE80211_S_CAC) { 2568 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni, 2569 "block %s frame in CAC state", "probe request"); 2570 vap->iv_stats.is_tx_badstate++; 2571 ieee80211_free_node(bss); 2572 return EIO; /* XXX */ 2573 } 2574 2575 /* 2576 * Hold a reference on the node so it doesn't go away until after 2577 * the xmit is complete all the way in the driver. On error we 2578 * will remove our reference. 2579 */ 2580 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2581 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 2582 __func__, __LINE__, 2583 ni, ether_sprintf(ni->ni_macaddr), 2584 ieee80211_node_refcnt(ni)+1); 2585 ieee80211_ref_node(ni); 2586 2587 /* See comments above for entire frame format. */ 2588 frmlen = ieee80211_probereq_ie_len(vap, ic); 2589 m = ieee80211_getmgtframe(&frm, 2590 ic->ic_headroom + sizeof(struct ieee80211_frame), frmlen); 2591 if (m == NULL) { 2592 vap->iv_stats.is_tx_nobuf++; 2593 ieee80211_free_node(ni); 2594 ieee80211_free_node(bss); 2595 return ENOMEM; 2596 } 2597 2598 ret = ieee80211_probereq_ie(vap, ic, &frm, &frmlen, ssid, ssidlen, 2599 false); 2600 KASSERT(ret == 0, 2601 ("%s: ieee80211_probereq_ie failed: %d\n", __func__, ret)); 2602 2603 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2604 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame), 2605 ("leading space %zd", M_LEADINGSPACE(m))); 2606 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT); 2607 if (m == NULL) { 2608 /* NB: cannot happen */ 2609 ieee80211_free_node(ni); 2610 ieee80211_free_node(bss); 2611 return ENOMEM; 2612 } 2613 2614 IEEE80211_TX_LOCK(ic); 2615 ieee80211_send_setup(ni, m, 2616 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ, 2617 IEEE80211_NONQOS_TID, sa, da, bssid); 2618 /* XXX power management? */ 2619 m->m_flags |= M_ENCAP; /* mark encapsulated */ 2620 2621 M_WME_SETAC(m, WME_AC_BE); 2622 2623 IEEE80211_NODE_STAT(ni, tx_probereq); 2624 IEEE80211_NODE_STAT(ni, tx_mgmt); 2625 2626 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 2627 "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n", 2628 ieee80211_chan2ieee(ic, ic->ic_curchan), 2629 ether_sprintf(bssid), 2630 sa, ":", 2631 da, ":", 2632 ssidlen, ssid); 2633 2634 memset(¶ms, 0, sizeof(params)); 2635 params.ibp_pri = M_WME_GETAC(m); 2636 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 2637 params.ibp_rate0 = tp->mgmtrate; 2638 if (IEEE80211_IS_MULTICAST(da)) { 2639 params.ibp_flags |= IEEE80211_BPF_NOACK; 2640 params.ibp_try0 = 1; 2641 } else 2642 params.ibp_try0 = tp->maxretry; 2643 params.ibp_power = ni->ni_txpower; 2644 ret = ieee80211_raw_output(vap, ni, m, ¶ms); 2645 IEEE80211_TX_UNLOCK(ic); 2646 ieee80211_free_node(bss); 2647 return (ret); 2648 } 2649 2650 /* 2651 * Calculate capability information for mgt frames. 2652 */ 2653 uint16_t 2654 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan) 2655 { 2656 uint16_t capinfo; 2657 2658 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode")); 2659 2660 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 2661 capinfo = IEEE80211_CAPINFO_ESS; 2662 else if (vap->iv_opmode == IEEE80211_M_IBSS) 2663 capinfo = IEEE80211_CAPINFO_IBSS; 2664 else 2665 capinfo = 0; 2666 if (vap->iv_flags & IEEE80211_F_PRIVACY) 2667 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2668 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) && 2669 IEEE80211_IS_CHAN_2GHZ(chan)) 2670 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2671 if (vap->iv_flags & IEEE80211_F_SHSLOT) 2672 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2673 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH)) 2674 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT; 2675 return capinfo; 2676 } 2677 2678 /* 2679 * Send a management frame. The node is for the destination (or ic_bss 2680 * when in station mode). Nodes other than ic_bss have their reference 2681 * count bumped to reflect our use for an indeterminant time. 2682 */ 2683 int 2684 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg) 2685 { 2686 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT) 2687 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0) 2688 struct ieee80211vap *vap = ni->ni_vap; 2689 struct ieee80211com *ic = ni->ni_ic; 2690 struct ieee80211_node *bss = vap->iv_bss; 2691 struct ieee80211_bpf_params params; 2692 struct mbuf *m; 2693 uint8_t *frm; 2694 uint16_t capinfo; 2695 int has_challenge, is_shared_key, ret, status; 2696 2697 KASSERT(ni != NULL, ("null node")); 2698 2699 /* 2700 * Hold a reference on the node so it doesn't go away until after 2701 * the xmit is complete all the way in the driver. On error we 2702 * will remove our reference. 2703 */ 2704 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2705 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 2706 __func__, __LINE__, 2707 ni, ether_sprintf(ni->ni_macaddr), 2708 ieee80211_node_refcnt(ni)+1); 2709 ieee80211_ref_node(ni); 2710 2711 memset(¶ms, 0, sizeof(params)); 2712 switch (type) { 2713 case IEEE80211_FC0_SUBTYPE_AUTH: 2714 status = arg >> 16; 2715 arg &= 0xffff; 2716 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE || 2717 arg == IEEE80211_AUTH_SHARED_RESPONSE) && 2718 ni->ni_challenge != NULL); 2719 2720 /* 2721 * Deduce whether we're doing open authentication or 2722 * shared key authentication. We do the latter if 2723 * we're in the middle of a shared key authentication 2724 * handshake or if we're initiating an authentication 2725 * request and configured to use shared key. 2726 */ 2727 is_shared_key = has_challenge || 2728 arg >= IEEE80211_AUTH_SHARED_RESPONSE || 2729 (arg == IEEE80211_AUTH_SHARED_REQUEST && 2730 bss->ni_authmode == IEEE80211_AUTH_SHARED); 2731 2732 m = ieee80211_getmgtframe(&frm, 2733 ic->ic_headroom + sizeof(struct ieee80211_frame), 2734 3 * sizeof(uint16_t) 2735 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ? 2736 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)); 2737 if (m == NULL) 2738 senderr(ENOMEM, is_tx_nobuf); 2739 2740 ((uint16_t *)frm)[0] = 2741 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED) 2742 : htole16(IEEE80211_AUTH_ALG_OPEN); 2743 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */ 2744 ((uint16_t *)frm)[2] = htole16(status);/* status */ 2745 2746 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) { 2747 ((uint16_t *)frm)[3] = 2748 htole16((IEEE80211_CHALLENGE_LEN << 8) | 2749 IEEE80211_ELEMID_CHALLENGE); 2750 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge, 2751 IEEE80211_CHALLENGE_LEN); 2752 m->m_pkthdr.len = m->m_len = 2753 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN; 2754 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) { 2755 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 2756 "request encrypt frame (%s)", __func__); 2757 /* mark frame for encryption */ 2758 params.ibp_flags |= IEEE80211_BPF_CRYPTO; 2759 } 2760 } else 2761 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t); 2762 2763 /* XXX not right for shared key */ 2764 if (status == IEEE80211_STATUS_SUCCESS) 2765 IEEE80211_NODE_STAT(ni, tx_auth); 2766 else 2767 IEEE80211_NODE_STAT(ni, tx_auth_fail); 2768 2769 if (vap->iv_opmode == IEEE80211_M_STA) 2770 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 2771 (void *) vap->iv_state); 2772 break; 2773 2774 case IEEE80211_FC0_SUBTYPE_DEAUTH: 2775 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 2776 "send station deauthenticate (reason: %d (%s))", arg, 2777 ieee80211_reason_to_string(arg)); 2778 m = ieee80211_getmgtframe(&frm, 2779 ic->ic_headroom + sizeof(struct ieee80211_frame), 2780 sizeof(uint16_t)); 2781 if (m == NULL) 2782 senderr(ENOMEM, is_tx_nobuf); 2783 *(uint16_t *)frm = htole16(arg); /* reason */ 2784 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 2785 2786 IEEE80211_NODE_STAT(ni, tx_deauth); 2787 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg); 2788 2789 ieee80211_node_unauthorize(ni); /* port closed */ 2790 break; 2791 2792 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 2793 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 2794 /* 2795 * asreq frame format 2796 * [2] capability information 2797 * [2] listen interval 2798 * [6*] current AP address (reassoc only) 2799 * [tlv] ssid 2800 * [tlv] supported rates 2801 * [tlv] extended supported rates 2802 * [4] power capability (optional) 2803 * [28] supported channels (optional) 2804 * [tlv] HT capabilities 2805 * [tlv] VHT capabilities 2806 * [tlv] WME (optional) 2807 * [tlv] Vendor OUI HT capabilities (optional) 2808 * [tlv] Atheros capabilities (if negotiated) 2809 * [tlv] AppIE's (optional) 2810 */ 2811 m = ieee80211_getmgtframe(&frm, 2812 ic->ic_headroom + sizeof(struct ieee80211_frame), 2813 sizeof(uint16_t) 2814 + sizeof(uint16_t) 2815 + IEEE80211_ADDR_LEN 2816 + 2 + IEEE80211_NWID_LEN 2817 + 2 + IEEE80211_RATE_SIZE 2818 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2819 + 4 2820 + 2 + 26 2821 + sizeof(struct ieee80211_wme_info) 2822 + sizeof(struct ieee80211_ie_htcap) 2823 + 2 + sizeof(struct ieee80211_vht_cap) 2824 + 4 + sizeof(struct ieee80211_ie_htcap) 2825 #ifdef IEEE80211_SUPPORT_SUPERG 2826 + sizeof(struct ieee80211_ath_ie) 2827 #endif 2828 + (vap->iv_appie_wpa != NULL ? 2829 vap->iv_appie_wpa->ie_len : 0) 2830 + (vap->iv_appie_assocreq != NULL ? 2831 vap->iv_appie_assocreq->ie_len : 0) 2832 ); 2833 if (m == NULL) 2834 senderr(ENOMEM, is_tx_nobuf); 2835 2836 KASSERT(vap->iv_opmode == IEEE80211_M_STA, 2837 ("wrong mode %u", vap->iv_opmode)); 2838 capinfo = IEEE80211_CAPINFO_ESS; 2839 if (vap->iv_flags & IEEE80211_F_PRIVACY) 2840 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2841 /* 2842 * NB: Some 11a AP's reject the request when 2843 * short preamble is set. 2844 */ 2845 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) && 2846 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 2847 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2848 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) && 2849 (ic->ic_caps & IEEE80211_C_SHSLOT)) 2850 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2851 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) && 2852 (vap->iv_flags & IEEE80211_F_DOTH)) 2853 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT; 2854 *(uint16_t *)frm = htole16(capinfo); 2855 frm += 2; 2856 2857 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!")); 2858 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval, 2859 bss->ni_intval)); 2860 frm += 2; 2861 2862 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 2863 IEEE80211_ADDR_COPY(frm, bss->ni_bssid); 2864 frm += IEEE80211_ADDR_LEN; 2865 } 2866 2867 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 2868 frm = ieee80211_add_rates(frm, &ni->ni_rates); 2869 frm = ieee80211_add_rsn(frm, vap); 2870 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 2871 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) { 2872 frm = ieee80211_add_powercapability(frm, 2873 ic->ic_curchan); 2874 frm = ieee80211_add_supportedchannels(frm, ic); 2875 } 2876 2877 /* 2878 * Check the channel - we may be using an 11n NIC with an 2879 * 11n capable station, but we're configured to be an 11b 2880 * channel. 2881 */ 2882 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) && 2883 IEEE80211_IS_CHAN_HT(ni->ni_chan) && 2884 ni->ni_ies.htcap_ie != NULL && 2885 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) { 2886 frm = ieee80211_add_htcap(frm, ni); 2887 } 2888 2889 if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) && 2890 IEEE80211_IS_CHAN_VHT(ni->ni_chan) && 2891 ni->ni_ies.vhtcap_ie != NULL && 2892 ni->ni_ies.vhtcap_ie[0] == IEEE80211_ELEMID_VHT_CAP) { 2893 frm = ieee80211_add_vhtcap(frm, ni); 2894 } 2895 2896 frm = ieee80211_add_wpa(frm, vap); 2897 if ((vap->iv_flags & IEEE80211_F_WME) && 2898 ni->ni_ies.wme_ie != NULL) 2899 frm = ieee80211_add_wme_info(frm, &ic->ic_wme, ni); 2900 2901 /* 2902 * Same deal - only send HT info if we're on an 11n 2903 * capable channel. 2904 */ 2905 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) && 2906 IEEE80211_IS_CHAN_HT(ni->ni_chan) && 2907 ni->ni_ies.htcap_ie != NULL && 2908 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) { 2909 frm = ieee80211_add_htcap_vendor(frm, ni); 2910 } 2911 #ifdef IEEE80211_SUPPORT_SUPERG 2912 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) { 2913 frm = ieee80211_add_ath(frm, 2914 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS), 2915 ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 2916 ni->ni_authmode != IEEE80211_AUTH_8021X) ? 2917 vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 2918 } 2919 #endif /* IEEE80211_SUPPORT_SUPERG */ 2920 if (vap->iv_appie_assocreq != NULL) 2921 frm = add_appie(frm, vap->iv_appie_assocreq); 2922 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2923 2924 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 2925 (void *) vap->iv_state); 2926 break; 2927 2928 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 2929 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 2930 /* 2931 * asresp frame format 2932 * [2] capability information 2933 * [2] status 2934 * [2] association ID 2935 * [tlv] supported rates 2936 * [tlv] extended supported rates 2937 * [tlv] HT capabilities (standard, if STA enabled) 2938 * [tlv] HT information (standard, if STA enabled) 2939 * [tlv] VHT capabilities (standard, if STA enabled) 2940 * [tlv] VHT information (standard, if STA enabled) 2941 * [tlv] WME (if configured and STA enabled) 2942 * [tlv] HT capabilities (vendor OUI, if STA enabled) 2943 * [tlv] HT information (vendor OUI, if STA enabled) 2944 * [tlv] Atheros capabilities (if STA enabled) 2945 * [tlv] AppIE's (optional) 2946 */ 2947 m = ieee80211_getmgtframe(&frm, 2948 ic->ic_headroom + sizeof(struct ieee80211_frame), 2949 sizeof(uint16_t) 2950 + sizeof(uint16_t) 2951 + sizeof(uint16_t) 2952 + 2 + IEEE80211_RATE_SIZE 2953 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2954 + sizeof(struct ieee80211_ie_htcap) + 4 2955 + sizeof(struct ieee80211_ie_htinfo) + 4 2956 + 2 + sizeof(struct ieee80211_vht_cap) 2957 + 2 + sizeof(struct ieee80211_vht_operation) 2958 + sizeof(struct ieee80211_wme_param) 2959 #ifdef IEEE80211_SUPPORT_SUPERG 2960 + sizeof(struct ieee80211_ath_ie) 2961 #endif 2962 + (vap->iv_appie_assocresp != NULL ? 2963 vap->iv_appie_assocresp->ie_len : 0) 2964 ); 2965 if (m == NULL) 2966 senderr(ENOMEM, is_tx_nobuf); 2967 2968 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan); 2969 *(uint16_t *)frm = htole16(capinfo); 2970 frm += 2; 2971 2972 *(uint16_t *)frm = htole16(arg); /* status */ 2973 frm += 2; 2974 2975 if (arg == IEEE80211_STATUS_SUCCESS) { 2976 *(uint16_t *)frm = htole16(ni->ni_associd); 2977 IEEE80211_NODE_STAT(ni, tx_assoc); 2978 } else 2979 IEEE80211_NODE_STAT(ni, tx_assoc_fail); 2980 frm += 2; 2981 2982 frm = ieee80211_add_rates(frm, &ni->ni_rates); 2983 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 2984 /* NB: respond according to what we received */ 2985 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) { 2986 frm = ieee80211_add_htcap(frm, ni); 2987 frm = ieee80211_add_htinfo(frm, ni); 2988 } 2989 if ((vap->iv_flags & IEEE80211_F_WME) && 2990 ni->ni_ies.wme_ie != NULL) 2991 frm = ieee80211_add_wme_param(frm, &ic->ic_wme, 2992 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)); 2993 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) { 2994 frm = ieee80211_add_htcap_vendor(frm, ni); 2995 frm = ieee80211_add_htinfo_vendor(frm, ni); 2996 } 2997 if (ni->ni_flags & IEEE80211_NODE_VHT) { 2998 frm = ieee80211_add_vhtcap(frm, ni); 2999 frm = ieee80211_add_vhtinfo(frm, ni); 3000 } 3001 #ifdef IEEE80211_SUPPORT_SUPERG 3002 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) 3003 frm = ieee80211_add_ath(frm, 3004 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS), 3005 ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 3006 ni->ni_authmode != IEEE80211_AUTH_8021X) ? 3007 vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 3008 #endif /* IEEE80211_SUPPORT_SUPERG */ 3009 if (vap->iv_appie_assocresp != NULL) 3010 frm = add_appie(frm, vap->iv_appie_assocresp); 3011 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 3012 break; 3013 3014 case IEEE80211_FC0_SUBTYPE_DISASSOC: 3015 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 3016 "send station disassociate (reason: %d (%s))", arg, 3017 ieee80211_reason_to_string(arg)); 3018 m = ieee80211_getmgtframe(&frm, 3019 ic->ic_headroom + sizeof(struct ieee80211_frame), 3020 sizeof(uint16_t)); 3021 if (m == NULL) 3022 senderr(ENOMEM, is_tx_nobuf); 3023 *(uint16_t *)frm = htole16(arg); /* reason */ 3024 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 3025 3026 IEEE80211_NODE_STAT(ni, tx_disassoc); 3027 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg); 3028 break; 3029 3030 default: 3031 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni, 3032 "invalid mgmt frame type %u", type); 3033 senderr(EINVAL, is_tx_unknownmgt); 3034 /* NOTREACHED */ 3035 } 3036 3037 /* NB: force non-ProbeResp frames to the highest queue */ 3038 params.ibp_pri = WME_AC_VO; 3039 params.ibp_rate0 = bss->ni_txparms->mgmtrate; 3040 /* NB: we know all frames are unicast */ 3041 params.ibp_try0 = bss->ni_txparms->maxretry; 3042 params.ibp_power = bss->ni_txpower; 3043 return ieee80211_mgmt_output(ni, m, type, ¶ms); 3044 bad: 3045 ieee80211_free_node(ni); 3046 return ret; 3047 #undef senderr 3048 #undef HTFLAGS 3049 } 3050 3051 /* 3052 * Return an mbuf with a probe response frame in it. 3053 * Space is left to prepend and 802.11 header at the 3054 * front but it's left to the caller to fill in. 3055 */ 3056 struct mbuf * 3057 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy) 3058 { 3059 struct ieee80211vap *vap = bss->ni_vap; 3060 struct ieee80211com *ic = bss->ni_ic; 3061 const struct ieee80211_rateset *rs; 3062 struct mbuf *m; 3063 uint16_t capinfo; 3064 uint8_t *frm; 3065 3066 /* 3067 * probe response frame format 3068 * [8] time stamp 3069 * [2] beacon interval 3070 * [2] cabability information 3071 * [tlv] ssid 3072 * [tlv] supported rates 3073 * [tlv] parameter set (FH/DS) 3074 * [tlv] parameter set (IBSS) 3075 * [tlv] country (optional) 3076 * [3] power control (optional) 3077 * [5] channel switch announcement (CSA) (optional) 3078 * [tlv] extended rate phy (ERP) 3079 * [tlv] extended supported rates 3080 * [tlv] RSN (optional) 3081 * [tlv] HT capabilities 3082 * [tlv] HT information 3083 * [tlv] VHT capabilities 3084 * [tlv] VHT information 3085 * [tlv] WPA (optional) 3086 * [tlv] WME (optional) 3087 * [tlv] Vendor OUI HT capabilities (optional) 3088 * [tlv] Vendor OUI HT information (optional) 3089 * [tlv] Atheros capabilities 3090 * [tlv] AppIE's (optional) 3091 * [tlv] Mesh ID (MBSS) 3092 * [tlv] Mesh Conf (MBSS) 3093 */ 3094 m = ieee80211_getmgtframe(&frm, 3095 ic->ic_headroom + sizeof(struct ieee80211_frame), 3096 8 3097 + sizeof(uint16_t) 3098 + sizeof(uint16_t) 3099 + 2 + IEEE80211_NWID_LEN 3100 + 2 + IEEE80211_RATE_SIZE 3101 + 7 /* max(7,3) */ 3102 + IEEE80211_COUNTRY_MAX_SIZE 3103 + 3 3104 + sizeof(struct ieee80211_csa_ie) 3105 + sizeof(struct ieee80211_quiet_ie) 3106 + 3 3107 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 3108 + sizeof(struct ieee80211_ie_wpa) 3109 + sizeof(struct ieee80211_ie_htcap) 3110 + sizeof(struct ieee80211_ie_htinfo) 3111 + sizeof(struct ieee80211_ie_wpa) 3112 + sizeof(struct ieee80211_wme_param) 3113 + 4 + sizeof(struct ieee80211_ie_htcap) 3114 + 4 + sizeof(struct ieee80211_ie_htinfo) 3115 + 2 + sizeof(struct ieee80211_vht_cap) 3116 + 2 + sizeof(struct ieee80211_vht_operation) 3117 #ifdef IEEE80211_SUPPORT_SUPERG 3118 + sizeof(struct ieee80211_ath_ie) 3119 #endif 3120 #ifdef IEEE80211_SUPPORT_MESH 3121 + 2 + IEEE80211_MESHID_LEN 3122 + sizeof(struct ieee80211_meshconf_ie) 3123 #endif 3124 + (vap->iv_appie_proberesp != NULL ? 3125 vap->iv_appie_proberesp->ie_len : 0) 3126 ); 3127 if (m == NULL) { 3128 vap->iv_stats.is_tx_nobuf++; 3129 return NULL; 3130 } 3131 3132 memset(frm, 0, 8); /* timestamp should be filled later */ 3133 frm += 8; 3134 *(uint16_t *)frm = htole16(bss->ni_intval); 3135 frm += 2; 3136 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan); 3137 *(uint16_t *)frm = htole16(capinfo); 3138 frm += 2; 3139 3140 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen); 3141 rs = ieee80211_get_suprates(ic, bss->ni_chan); 3142 frm = ieee80211_add_rates(frm, rs); 3143 3144 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) { 3145 *frm++ = IEEE80211_ELEMID_FHPARMS; 3146 *frm++ = 5; 3147 *frm++ = bss->ni_fhdwell & 0x00ff; 3148 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff; 3149 *frm++ = IEEE80211_FH_CHANSET( 3150 ieee80211_chan2ieee(ic, bss->ni_chan)); 3151 *frm++ = IEEE80211_FH_CHANPAT( 3152 ieee80211_chan2ieee(ic, bss->ni_chan)); 3153 *frm++ = bss->ni_fhindex; 3154 } else { 3155 *frm++ = IEEE80211_ELEMID_DSPARMS; 3156 *frm++ = 1; 3157 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan); 3158 } 3159 3160 if (vap->iv_opmode == IEEE80211_M_IBSS) { 3161 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 3162 *frm++ = 2; 3163 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 3164 } 3165 if ((vap->iv_flags & IEEE80211_F_DOTH) || 3166 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) 3167 frm = ieee80211_add_countryie(frm, ic); 3168 if (vap->iv_flags & IEEE80211_F_DOTH) { 3169 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan)) 3170 frm = ieee80211_add_powerconstraint(frm, vap); 3171 if (ic->ic_flags & IEEE80211_F_CSAPENDING) 3172 frm = ieee80211_add_csa(frm, vap); 3173 } 3174 if (vap->iv_flags & IEEE80211_F_DOTH) { 3175 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) && 3176 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) { 3177 if (vap->iv_quiet) 3178 frm = ieee80211_add_quiet(frm, vap, 0); 3179 } 3180 } 3181 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan)) 3182 frm = ieee80211_add_erp(frm, vap); 3183 frm = ieee80211_add_xrates(frm, rs); 3184 frm = ieee80211_add_rsn(frm, vap); 3185 /* 3186 * NB: legacy 11b clients do not get certain ie's. 3187 * The caller identifies such clients by passing 3188 * a token in legacy to us. Could expand this to be 3189 * any legacy client for stuff like HT ie's. 3190 */ 3191 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) && 3192 legacy != IEEE80211_SEND_LEGACY_11B) { 3193 frm = ieee80211_add_htcap(frm, bss); 3194 frm = ieee80211_add_htinfo(frm, bss); 3195 } 3196 if (IEEE80211_IS_CHAN_VHT(bss->ni_chan) && 3197 legacy != IEEE80211_SEND_LEGACY_11B) { 3198 frm = ieee80211_add_vhtcap(frm, bss); 3199 frm = ieee80211_add_vhtinfo(frm, bss); 3200 } 3201 frm = ieee80211_add_wpa(frm, vap); 3202 if (vap->iv_flags & IEEE80211_F_WME) 3203 frm = ieee80211_add_wme_param(frm, &ic->ic_wme, 3204 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)); 3205 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) && 3206 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) && 3207 legacy != IEEE80211_SEND_LEGACY_11B) { 3208 frm = ieee80211_add_htcap_vendor(frm, bss); 3209 frm = ieee80211_add_htinfo_vendor(frm, bss); 3210 } 3211 #ifdef IEEE80211_SUPPORT_SUPERG 3212 if ((vap->iv_flags & IEEE80211_F_ATHEROS) && 3213 legacy != IEEE80211_SEND_LEGACY_11B) 3214 frm = ieee80211_add_athcaps(frm, bss); 3215 #endif 3216 if (vap->iv_appie_proberesp != NULL) 3217 frm = add_appie(frm, vap->iv_appie_proberesp); 3218 #ifdef IEEE80211_SUPPORT_MESH 3219 if (vap->iv_opmode == IEEE80211_M_MBSS) { 3220 frm = ieee80211_add_meshid(frm, vap); 3221 frm = ieee80211_add_meshconf(frm, vap); 3222 } 3223 #endif 3224 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 3225 3226 return m; 3227 } 3228 3229 /* 3230 * Send a probe response frame to the specified mac address. 3231 * This does not go through the normal mgt frame api so we 3232 * can specify the destination address and re-use the bss node 3233 * for the sta reference. 3234 */ 3235 int 3236 ieee80211_send_proberesp(struct ieee80211vap *vap, 3237 const uint8_t da[IEEE80211_ADDR_LEN], int legacy) 3238 { 3239 struct ieee80211_node *bss = vap->iv_bss; 3240 struct ieee80211com *ic = vap->iv_ic; 3241 struct mbuf *m; 3242 int ret; 3243 3244 if (vap->iv_state == IEEE80211_S_CAC) { 3245 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss, 3246 "block %s frame in CAC state", "probe response"); 3247 vap->iv_stats.is_tx_badstate++; 3248 return EIO; /* XXX */ 3249 } 3250 3251 /* 3252 * Hold a reference on the node so it doesn't go away until after 3253 * the xmit is complete all the way in the driver. On error we 3254 * will remove our reference. 3255 */ 3256 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 3257 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 3258 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr), 3259 ieee80211_node_refcnt(bss)+1); 3260 ieee80211_ref_node(bss); 3261 3262 m = ieee80211_alloc_proberesp(bss, legacy); 3263 if (m == NULL) { 3264 ieee80211_free_node(bss); 3265 return ENOMEM; 3266 } 3267 3268 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT); 3269 KASSERT(m != NULL, ("no room for header")); 3270 3271 IEEE80211_TX_LOCK(ic); 3272 ieee80211_send_setup(bss, m, 3273 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP, 3274 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid); 3275 /* XXX power management? */ 3276 m->m_flags |= M_ENCAP; /* mark encapsulated */ 3277 3278 M_WME_SETAC(m, WME_AC_BE); 3279 3280 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 3281 "send probe resp on channel %u to %s%s\n", 3282 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da), 3283 legacy ? " <legacy>" : ""); 3284 IEEE80211_NODE_STAT(bss, tx_mgmt); 3285 3286 ret = ieee80211_raw_output(vap, bss, m, NULL); 3287 IEEE80211_TX_UNLOCK(ic); 3288 return (ret); 3289 } 3290 3291 /* 3292 * Allocate and build a RTS (Request To Send) control frame. 3293 */ 3294 struct mbuf * 3295 ieee80211_alloc_rts(struct ieee80211com *ic, 3296 const uint8_t ra[IEEE80211_ADDR_LEN], 3297 const uint8_t ta[IEEE80211_ADDR_LEN], 3298 uint16_t dur) 3299 { 3300 struct ieee80211_frame_rts *rts; 3301 struct mbuf *m; 3302 3303 /* XXX honor ic_headroom */ 3304 m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA); 3305 if (m != NULL) { 3306 rts = mtod(m, struct ieee80211_frame_rts *); 3307 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | 3308 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS; 3309 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 3310 *(u_int16_t *)rts->i_dur = htole16(dur); 3311 IEEE80211_ADDR_COPY(rts->i_ra, ra); 3312 IEEE80211_ADDR_COPY(rts->i_ta, ta); 3313 3314 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts); 3315 } 3316 return m; 3317 } 3318 3319 /* 3320 * Allocate and build a CTS (Clear To Send) control frame. 3321 */ 3322 struct mbuf * 3323 ieee80211_alloc_cts(struct ieee80211com *ic, 3324 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur) 3325 { 3326 struct ieee80211_frame_cts *cts; 3327 struct mbuf *m; 3328 3329 /* XXX honor ic_headroom */ 3330 m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA); 3331 if (m != NULL) { 3332 cts = mtod(m, struct ieee80211_frame_cts *); 3333 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 | 3334 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS; 3335 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 3336 *(u_int16_t *)cts->i_dur = htole16(dur); 3337 IEEE80211_ADDR_COPY(cts->i_ra, ra); 3338 3339 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts); 3340 } 3341 return m; 3342 } 3343 3344 /* 3345 * Wrapper for CTS/RTS frame allocation. 3346 */ 3347 struct mbuf * 3348 ieee80211_alloc_prot(struct ieee80211_node *ni, const struct mbuf *m, 3349 uint8_t rate, int prot) 3350 { 3351 struct ieee80211com *ic = ni->ni_ic; 3352 struct ieee80211vap *vap = ni->ni_vap; 3353 const struct ieee80211_frame *wh; 3354 struct mbuf *mprot; 3355 uint16_t dur; 3356 int pktlen, isshort; 3357 3358 KASSERT(prot == IEEE80211_PROT_RTSCTS || 3359 prot == IEEE80211_PROT_CTSONLY, 3360 ("wrong protection type %d", prot)); 3361 3362 wh = mtod(m, const struct ieee80211_frame *); 3363 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 3364 isshort = (vap->iv_flags & IEEE80211_F_SHPREAMBLE) != 0; 3365 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 3366 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 3367 3368 if (prot == IEEE80211_PROT_RTSCTS) { 3369 /* NB: CTS is the same size as an ACK */ 3370 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 3371 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 3372 } else 3373 mprot = ieee80211_alloc_cts(ic, vap->iv_myaddr, dur); 3374 3375 return (mprot); 3376 } 3377 3378 static void 3379 ieee80211_tx_mgt_timeout(void *arg) 3380 { 3381 struct ieee80211vap *vap = arg; 3382 3383 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 3384 "vap %p mode %s state %s flags %#x & %#x\n", vap, 3385 ieee80211_opmode_name[vap->iv_opmode], 3386 ieee80211_state_name[vap->iv_state], 3387 vap->iv_ic->ic_flags, IEEE80211_F_SCAN); 3388 3389 IEEE80211_LOCK(vap->iv_ic); 3390 if (vap->iv_state != IEEE80211_S_INIT && 3391 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) { 3392 /* 3393 * NB: it's safe to specify a timeout as the reason here; 3394 * it'll only be used in the right state. 3395 */ 3396 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN, 3397 IEEE80211_SCAN_FAIL_TIMEOUT); 3398 } 3399 IEEE80211_UNLOCK(vap->iv_ic); 3400 } 3401 3402 /* 3403 * This is the callback set on net80211-sourced transmitted 3404 * authentication request frames. 3405 * 3406 * This does a couple of things: 3407 * 3408 * + If the frame transmitted was a success, it schedules a future 3409 * event which will transition the interface to scan. 3410 * If a state transition _then_ occurs before that event occurs, 3411 * said state transition will cancel this callout. 3412 * 3413 * + If the frame transmit was a failure, it immediately schedules 3414 * the transition back to scan. 3415 */ 3416 static void 3417 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status) 3418 { 3419 struct ieee80211vap *vap = ni->ni_vap; 3420 enum ieee80211_state ostate = (enum ieee80211_state)(uintptr_t)arg; 3421 3422 /* 3423 * Frame transmit completed; arrange timer callback. If 3424 * transmit was successfully we wait for response. Otherwise 3425 * we arrange an immediate callback instead of doing the 3426 * callback directly since we don't know what state the driver 3427 * is in (e.g. what locks it is holding). This work should 3428 * not be too time-critical and not happen too often so the 3429 * added overhead is acceptable. 3430 * 3431 * XXX what happens if !acked but response shows up before callback? 3432 */ 3433 if (vap->iv_state == ostate) { 3434 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 3435 "ni %p mode %s state %s arg %p status %d\n", ni, 3436 ieee80211_opmode_name[vap->iv_opmode], 3437 ieee80211_state_name[vap->iv_state], arg, status); 3438 3439 callout_reset(&vap->iv_mgtsend, 3440 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0, 3441 ieee80211_tx_mgt_timeout, vap); 3442 } 3443 } 3444 3445 static void 3446 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm, 3447 struct ieee80211_node *ni) 3448 { 3449 struct ieee80211vap *vap = ni->ni_vap; 3450 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 3451 struct ieee80211com *ic = ni->ni_ic; 3452 struct ieee80211_rateset *rs = &ni->ni_rates; 3453 uint16_t capinfo; 3454 3455 /* 3456 * beacon frame format 3457 * 3458 * TODO: update to 802.11-2012; a lot of stuff has changed; 3459 * vendor extensions should be at the end, etc. 3460 * 3461 * [8] time stamp 3462 * [2] beacon interval 3463 * [2] cabability information 3464 * [tlv] ssid 3465 * [tlv] supported rates 3466 * [3] parameter set (DS) 3467 * [8] CF parameter set (optional) 3468 * [tlv] parameter set (IBSS/TIM) 3469 * [tlv] country (optional) 3470 * [3] power control (optional) 3471 * [5] channel switch announcement (CSA) (optional) 3472 * XXX TODO: Quiet 3473 * XXX TODO: IBSS DFS 3474 * XXX TODO: TPC report 3475 * [tlv] extended rate phy (ERP) 3476 * [tlv] extended supported rates 3477 * [tlv] RSN parameters 3478 * XXX TODO: BSSLOAD 3479 * (XXX EDCA parameter set, QoS capability?) 3480 * XXX TODO: AP channel report 3481 * 3482 * [tlv] HT capabilities 3483 * [tlv] HT information 3484 * XXX TODO: 20/40 BSS coexistence 3485 * Mesh: 3486 * XXX TODO: Meshid 3487 * XXX TODO: mesh config 3488 * XXX TODO: mesh awake window 3489 * XXX TODO: beacon timing (mesh, etc) 3490 * XXX TODO: MCCAOP Advertisement Overview 3491 * XXX TODO: MCCAOP Advertisement 3492 * XXX TODO: Mesh channel switch parameters 3493 * VHT: 3494 * XXX TODO: VHT capabilities 3495 * XXX TODO: VHT operation 3496 * XXX TODO: VHT transmit power envelope 3497 * XXX TODO: channel switch wrapper element 3498 * XXX TODO: extended BSS load element 3499 * 3500 * XXX Vendor-specific OIDs (e.g. Atheros) 3501 * [tlv] WPA parameters 3502 * [tlv] WME parameters 3503 * [tlv] Vendor OUI HT capabilities (optional) 3504 * [tlv] Vendor OUI HT information (optional) 3505 * [tlv] Atheros capabilities (optional) 3506 * [tlv] TDMA parameters (optional) 3507 * [tlv] Mesh ID (MBSS) 3508 * [tlv] Mesh Conf (MBSS) 3509 * [tlv] application data (optional) 3510 */ 3511 3512 memset(bo, 0, sizeof(*bo)); 3513 3514 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */ 3515 frm += 8; 3516 *(uint16_t *)frm = htole16(ni->ni_intval); 3517 frm += 2; 3518 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan); 3519 bo->bo_caps = (uint16_t *)frm; 3520 *(uint16_t *)frm = htole16(capinfo); 3521 frm += 2; 3522 *frm++ = IEEE80211_ELEMID_SSID; 3523 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) { 3524 *frm++ = ni->ni_esslen; 3525 memcpy(frm, ni->ni_essid, ni->ni_esslen); 3526 frm += ni->ni_esslen; 3527 } else 3528 *frm++ = 0; 3529 frm = ieee80211_add_rates(frm, rs); 3530 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) { 3531 *frm++ = IEEE80211_ELEMID_DSPARMS; 3532 *frm++ = 1; 3533 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 3534 } 3535 if (ic->ic_flags & IEEE80211_F_PCF) { 3536 bo->bo_cfp = frm; 3537 frm = ieee80211_add_cfparms(frm, ic); 3538 } 3539 bo->bo_tim = frm; 3540 if (vap->iv_opmode == IEEE80211_M_IBSS) { 3541 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 3542 *frm++ = 2; 3543 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 3544 bo->bo_tim_len = 0; 3545 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP || 3546 vap->iv_opmode == IEEE80211_M_MBSS) { 3547 /* TIM IE is the same for Mesh and Hostap */ 3548 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm; 3549 3550 tie->tim_ie = IEEE80211_ELEMID_TIM; 3551 tie->tim_len = 4; /* length */ 3552 tie->tim_count = 0; /* DTIM count */ 3553 tie->tim_period = vap->iv_dtim_period; /* DTIM period */ 3554 tie->tim_bitctl = 0; /* bitmap control */ 3555 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */ 3556 frm += sizeof(struct ieee80211_tim_ie); 3557 bo->bo_tim_len = 1; 3558 } 3559 bo->bo_tim_trailer = frm; 3560 if ((vap->iv_flags & IEEE80211_F_DOTH) || 3561 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) 3562 frm = ieee80211_add_countryie(frm, ic); 3563 if (vap->iv_flags & IEEE80211_F_DOTH) { 3564 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)) 3565 frm = ieee80211_add_powerconstraint(frm, vap); 3566 bo->bo_csa = frm; 3567 if (ic->ic_flags & IEEE80211_F_CSAPENDING) 3568 frm = ieee80211_add_csa(frm, vap); 3569 } else 3570 bo->bo_csa = frm; 3571 3572 bo->bo_quiet = NULL; 3573 if (vap->iv_flags & IEEE80211_F_DOTH) { 3574 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) && 3575 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) && 3576 (vap->iv_quiet == 1)) { 3577 /* 3578 * We only insert the quiet IE offset if 3579 * the quiet IE is enabled. Otherwise don't 3580 * put it here or we'll just overwrite 3581 * some other beacon contents. 3582 */ 3583 if (vap->iv_quiet) { 3584 bo->bo_quiet = frm; 3585 frm = ieee80211_add_quiet(frm,vap, 0); 3586 } 3587 } 3588 } 3589 3590 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) { 3591 bo->bo_erp = frm; 3592 frm = ieee80211_add_erp(frm, vap); 3593 } 3594 frm = ieee80211_add_xrates(frm, rs); 3595 frm = ieee80211_add_rsn(frm, vap); 3596 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 3597 frm = ieee80211_add_htcap(frm, ni); 3598 bo->bo_htinfo = frm; 3599 frm = ieee80211_add_htinfo(frm, ni); 3600 } 3601 3602 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) { 3603 frm = ieee80211_add_vhtcap(frm, ni); 3604 bo->bo_vhtinfo = frm; 3605 frm = ieee80211_add_vhtinfo(frm, ni); 3606 /* Transmit power envelope */ 3607 /* Channel switch wrapper element */ 3608 /* Extended bss load element */ 3609 } 3610 3611 frm = ieee80211_add_wpa(frm, vap); 3612 if (vap->iv_flags & IEEE80211_F_WME) { 3613 bo->bo_wme = frm; 3614 frm = ieee80211_add_wme_param(frm, &ic->ic_wme, 3615 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)); 3616 } 3617 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && 3618 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) { 3619 frm = ieee80211_add_htcap_vendor(frm, ni); 3620 frm = ieee80211_add_htinfo_vendor(frm, ni); 3621 } 3622 3623 #ifdef IEEE80211_SUPPORT_SUPERG 3624 if (vap->iv_flags & IEEE80211_F_ATHEROS) { 3625 bo->bo_ath = frm; 3626 frm = ieee80211_add_athcaps(frm, ni); 3627 } 3628 #endif 3629 #ifdef IEEE80211_SUPPORT_TDMA 3630 if (vap->iv_caps & IEEE80211_C_TDMA) { 3631 bo->bo_tdma = frm; 3632 frm = ieee80211_add_tdma(frm, vap); 3633 } 3634 #endif 3635 if (vap->iv_appie_beacon != NULL) { 3636 bo->bo_appie = frm; 3637 bo->bo_appie_len = vap->iv_appie_beacon->ie_len; 3638 frm = add_appie(frm, vap->iv_appie_beacon); 3639 } 3640 3641 /* XXX TODO: move meshid/meshconf up to before vendor extensions? */ 3642 #ifdef IEEE80211_SUPPORT_MESH 3643 if (vap->iv_opmode == IEEE80211_M_MBSS) { 3644 frm = ieee80211_add_meshid(frm, vap); 3645 bo->bo_meshconf = frm; 3646 frm = ieee80211_add_meshconf(frm, vap); 3647 } 3648 #endif 3649 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer; 3650 bo->bo_csa_trailer_len = frm - bo->bo_csa; 3651 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 3652 } 3653 3654 /* 3655 * Allocate a beacon frame and fillin the appropriate bits. 3656 */ 3657 struct mbuf * 3658 ieee80211_beacon_alloc(struct ieee80211_node *ni) 3659 { 3660 struct ieee80211vap *vap = ni->ni_vap; 3661 struct ieee80211com *ic = ni->ni_ic; 3662 struct ifnet *ifp = vap->iv_ifp; 3663 struct ieee80211_frame *wh; 3664 struct mbuf *m; 3665 int pktlen; 3666 uint8_t *frm; 3667 3668 /* 3669 * Update the "We're putting the quiet IE in the beacon" state. 3670 */ 3671 if (vap->iv_quiet == 1) 3672 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE; 3673 else if (vap->iv_quiet == 0) 3674 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE; 3675 3676 /* 3677 * beacon frame format 3678 * 3679 * Note: This needs updating for 802.11-2012. 3680 * 3681 * [8] time stamp 3682 * [2] beacon interval 3683 * [2] cabability information 3684 * [tlv] ssid 3685 * [tlv] supported rates 3686 * [3] parameter set (DS) 3687 * [8] CF parameter set (optional) 3688 * [tlv] parameter set (IBSS/TIM) 3689 * [tlv] country (optional) 3690 * [3] power control (optional) 3691 * [5] channel switch announcement (CSA) (optional) 3692 * [tlv] extended rate phy (ERP) 3693 * [tlv] extended supported rates 3694 * [tlv] RSN parameters 3695 * [tlv] HT capabilities 3696 * [tlv] HT information 3697 * [tlv] VHT capabilities 3698 * [tlv] VHT operation 3699 * [tlv] Vendor OUI HT capabilities (optional) 3700 * [tlv] Vendor OUI HT information (optional) 3701 * XXX Vendor-specific OIDs (e.g. Atheros) 3702 * [tlv] WPA parameters 3703 * [tlv] WME parameters 3704 * [tlv] TDMA parameters (optional) 3705 * [tlv] Mesh ID (MBSS) 3706 * [tlv] Mesh Conf (MBSS) 3707 * [tlv] application data (optional) 3708 * NB: we allocate the max space required for the TIM bitmap. 3709 * XXX how big is this? 3710 */ 3711 pktlen = 8 /* time stamp */ 3712 + sizeof(uint16_t) /* beacon interval */ 3713 + sizeof(uint16_t) /* capabilities */ 3714 + 2 + ni->ni_esslen /* ssid */ 3715 + 2 + IEEE80211_RATE_SIZE /* supported rates */ 3716 + 2 + 1 /* DS parameters */ 3717 + 2 + 6 /* CF parameters */ 3718 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */ 3719 + IEEE80211_COUNTRY_MAX_SIZE /* country */ 3720 + 2 + 1 /* power control */ 3721 + sizeof(struct ieee80211_csa_ie) /* CSA */ 3722 + sizeof(struct ieee80211_quiet_ie) /* Quiet */ 3723 + 2 + 1 /* ERP */ 3724 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 3725 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */ 3726 2*sizeof(struct ieee80211_ie_wpa) : 0) 3727 /* XXX conditional? */ 3728 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */ 3729 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */ 3730 + 2 + sizeof(struct ieee80211_vht_cap)/* VHT caps */ 3731 + 2 + sizeof(struct ieee80211_vht_operation)/* VHT info */ 3732 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */ 3733 sizeof(struct ieee80211_wme_param) : 0) 3734 #ifdef IEEE80211_SUPPORT_SUPERG 3735 + sizeof(struct ieee80211_ath_ie) /* ATH */ 3736 #endif 3737 #ifdef IEEE80211_SUPPORT_TDMA 3738 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */ 3739 sizeof(struct ieee80211_tdma_param) : 0) 3740 #endif 3741 #ifdef IEEE80211_SUPPORT_MESH 3742 + 2 + ni->ni_meshidlen 3743 + sizeof(struct ieee80211_meshconf_ie) 3744 #endif 3745 + IEEE80211_MAX_APPIE 3746 ; 3747 m = ieee80211_getmgtframe(&frm, 3748 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen); 3749 if (m == NULL) { 3750 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 3751 "%s: cannot get buf; size %u\n", __func__, pktlen); 3752 vap->iv_stats.is_tx_nobuf++; 3753 return NULL; 3754 } 3755 ieee80211_beacon_construct(m, frm, ni); 3756 3757 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT); 3758 KASSERT(m != NULL, ("no space for 802.11 header?")); 3759 wh = mtod(m, struct ieee80211_frame *); 3760 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 3761 IEEE80211_FC0_SUBTYPE_BEACON; 3762 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 3763 *(uint16_t *)wh->i_dur = 0; 3764 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 3765 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 3766 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 3767 *(uint16_t *)wh->i_seq = 0; 3768 3769 return m; 3770 } 3771 3772 /* 3773 * Update the dynamic parts of a beacon frame based on the current state. 3774 */ 3775 int 3776 ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast) 3777 { 3778 struct ieee80211vap *vap = ni->ni_vap; 3779 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 3780 struct ieee80211com *ic = ni->ni_ic; 3781 int len_changed = 0; 3782 uint16_t capinfo; 3783 struct ieee80211_frame *wh; 3784 ieee80211_seq seqno; 3785 3786 IEEE80211_LOCK(ic); 3787 /* 3788 * Handle 11h channel change when we've reached the count. 3789 * We must recalculate the beacon frame contents to account 3790 * for the new channel. Note we do this only for the first 3791 * vap that reaches this point; subsequent vaps just update 3792 * their beacon state to reflect the recalculated channel. 3793 */ 3794 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) && 3795 vap->iv_csa_count == ic->ic_csa_count) { 3796 vap->iv_csa_count = 0; 3797 /* 3798 * Effect channel change before reconstructing the beacon 3799 * frame contents as many places reference ni_chan. 3800 */ 3801 if (ic->ic_csa_newchan != NULL) 3802 ieee80211_csa_completeswitch(ic); 3803 /* 3804 * NB: ieee80211_beacon_construct clears all pending 3805 * updates in bo_flags so we don't need to explicitly 3806 * clear IEEE80211_BEACON_CSA. 3807 */ 3808 ieee80211_beacon_construct(m, 3809 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni); 3810 3811 /* XXX do WME aggressive mode processing? */ 3812 IEEE80211_UNLOCK(ic); 3813 return 1; /* just assume length changed */ 3814 } 3815 3816 /* 3817 * Handle the quiet time element being added and removed. 3818 * Again, for now we just cheat and reconstruct the whole 3819 * beacon - that way the gap is provided as appropriate. 3820 * 3821 * So, track whether we have already added the IE versus 3822 * whether we want to be adding the IE. 3823 */ 3824 if ((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) && 3825 (vap->iv_quiet == 0)) { 3826 /* 3827 * Quiet time beacon IE enabled, but it's disabled; 3828 * recalc 3829 */ 3830 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE; 3831 ieee80211_beacon_construct(m, 3832 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni); 3833 /* XXX do WME aggressive mode processing? */ 3834 IEEE80211_UNLOCK(ic); 3835 return 1; /* just assume length changed */ 3836 } 3837 3838 if (((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) == 0) && 3839 (vap->iv_quiet == 1)) { 3840 /* 3841 * Quiet time beacon IE disabled, but it's now enabled; 3842 * recalc 3843 */ 3844 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE; 3845 ieee80211_beacon_construct(m, 3846 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni); 3847 /* XXX do WME aggressive mode processing? */ 3848 IEEE80211_UNLOCK(ic); 3849 return 1; /* just assume length changed */ 3850 } 3851 3852 wh = mtod(m, struct ieee80211_frame *); 3853 3854 /* 3855 * XXX TODO Strictly speaking this should be incremented with the TX 3856 * lock held so as to serialise access to the non-qos TID sequence 3857 * number space. 3858 * 3859 * If the driver identifies it does its own TX seqno management then 3860 * we can skip this (and still not do the TX seqno.) 3861 */ 3862 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++; 3863 *(uint16_t *)&wh->i_seq[0] = 3864 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 3865 M_SEQNO_SET(m, seqno); 3866 3867 /* XXX faster to recalculate entirely or just changes? */ 3868 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan); 3869 *bo->bo_caps = htole16(capinfo); 3870 3871 if (vap->iv_flags & IEEE80211_F_WME) { 3872 struct ieee80211_wme_state *wme = &ic->ic_wme; 3873 3874 /* 3875 * Check for aggressive mode change. When there is 3876 * significant high priority traffic in the BSS 3877 * throttle back BE traffic by using conservative 3878 * parameters. Otherwise BE uses aggressive params 3879 * to optimize performance of legacy/non-QoS traffic. 3880 */ 3881 if (wme->wme_flags & WME_F_AGGRMODE) { 3882 if (wme->wme_hipri_traffic > 3883 wme->wme_hipri_switch_thresh) { 3884 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 3885 "%s: traffic %u, disable aggressive mode\n", 3886 __func__, wme->wme_hipri_traffic); 3887 wme->wme_flags &= ~WME_F_AGGRMODE; 3888 ieee80211_wme_updateparams_locked(vap); 3889 wme->wme_hipri_traffic = 3890 wme->wme_hipri_switch_hysteresis; 3891 } else 3892 wme->wme_hipri_traffic = 0; 3893 } else { 3894 if (wme->wme_hipri_traffic <= 3895 wme->wme_hipri_switch_thresh) { 3896 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 3897 "%s: traffic %u, enable aggressive mode\n", 3898 __func__, wme->wme_hipri_traffic); 3899 wme->wme_flags |= WME_F_AGGRMODE; 3900 ieee80211_wme_updateparams_locked(vap); 3901 wme->wme_hipri_traffic = 0; 3902 } else 3903 wme->wme_hipri_traffic = 3904 wme->wme_hipri_switch_hysteresis; 3905 } 3906 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) { 3907 (void) ieee80211_add_wme_param(bo->bo_wme, wme, 3908 vap->iv_flags_ext & IEEE80211_FEXT_UAPSD); 3909 clrbit(bo->bo_flags, IEEE80211_BEACON_WME); 3910 } 3911 } 3912 3913 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) { 3914 ieee80211_ht_update_beacon(vap, bo); 3915 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO); 3916 } 3917 #ifdef IEEE80211_SUPPORT_TDMA 3918 if (vap->iv_caps & IEEE80211_C_TDMA) { 3919 /* 3920 * NB: the beacon is potentially updated every TBTT. 3921 */ 3922 ieee80211_tdma_update_beacon(vap, bo); 3923 } 3924 #endif 3925 #ifdef IEEE80211_SUPPORT_MESH 3926 if (vap->iv_opmode == IEEE80211_M_MBSS) 3927 ieee80211_mesh_update_beacon(vap, bo); 3928 #endif 3929 3930 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 3931 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/ 3932 struct ieee80211_tim_ie *tie = 3933 (struct ieee80211_tim_ie *) bo->bo_tim; 3934 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) { 3935 u_int timlen, timoff, i; 3936 /* 3937 * ATIM/DTIM needs updating. If it fits in the 3938 * current space allocated then just copy in the 3939 * new bits. Otherwise we need to move any trailing 3940 * data to make room. Note that we know there is 3941 * contiguous space because ieee80211_beacon_allocate 3942 * insures there is space in the mbuf to write a 3943 * maximal-size virtual bitmap (based on iv_max_aid). 3944 */ 3945 /* 3946 * Calculate the bitmap size and offset, copy any 3947 * trailer out of the way, and then copy in the 3948 * new bitmap and update the information element. 3949 * Note that the tim bitmap must contain at least 3950 * one byte and any offset must be even. 3951 */ 3952 if (vap->iv_ps_pending != 0) { 3953 timoff = 128; /* impossibly large */ 3954 for (i = 0; i < vap->iv_tim_len; i++) 3955 if (vap->iv_tim_bitmap[i]) { 3956 timoff = i &~ 1; 3957 break; 3958 } 3959 KASSERT(timoff != 128, ("tim bitmap empty!")); 3960 for (i = vap->iv_tim_len-1; i >= timoff; i--) 3961 if (vap->iv_tim_bitmap[i]) 3962 break; 3963 timlen = 1 + (i - timoff); 3964 } else { 3965 timoff = 0; 3966 timlen = 1; 3967 } 3968 3969 /* 3970 * TODO: validate this! 3971 */ 3972 if (timlen != bo->bo_tim_len) { 3973 /* copy up/down trailer */ 3974 int adjust = tie->tim_bitmap+timlen 3975 - bo->bo_tim_trailer; 3976 ovbcopy(bo->bo_tim_trailer, 3977 bo->bo_tim_trailer+adjust, 3978 bo->bo_tim_trailer_len); 3979 bo->bo_tim_trailer += adjust; 3980 bo->bo_erp += adjust; 3981 bo->bo_htinfo += adjust; 3982 bo->bo_vhtinfo += adjust; 3983 #ifdef IEEE80211_SUPPORT_SUPERG 3984 bo->bo_ath += adjust; 3985 #endif 3986 #ifdef IEEE80211_SUPPORT_TDMA 3987 bo->bo_tdma += adjust; 3988 #endif 3989 #ifdef IEEE80211_SUPPORT_MESH 3990 bo->bo_meshconf += adjust; 3991 #endif 3992 bo->bo_appie += adjust; 3993 bo->bo_wme += adjust; 3994 bo->bo_csa += adjust; 3995 bo->bo_quiet += adjust; 3996 bo->bo_tim_len = timlen; 3997 3998 /* update information element */ 3999 tie->tim_len = 3 + timlen; 4000 tie->tim_bitctl = timoff; 4001 len_changed = 1; 4002 } 4003 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff, 4004 bo->bo_tim_len); 4005 4006 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM); 4007 4008 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER, 4009 "%s: TIM updated, pending %u, off %u, len %u\n", 4010 __func__, vap->iv_ps_pending, timoff, timlen); 4011 } 4012 /* count down DTIM period */ 4013 if (tie->tim_count == 0) 4014 tie->tim_count = tie->tim_period - 1; 4015 else 4016 tie->tim_count--; 4017 /* update state for buffered multicast frames on DTIM */ 4018 if (mcast && tie->tim_count == 0) 4019 tie->tim_bitctl |= 1; 4020 else 4021 tie->tim_bitctl &= ~1; 4022 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) { 4023 struct ieee80211_csa_ie *csa = 4024 (struct ieee80211_csa_ie *) bo->bo_csa; 4025 4026 /* 4027 * Insert or update CSA ie. If we're just starting 4028 * to count down to the channel switch then we need 4029 * to insert the CSA ie. Otherwise we just need to 4030 * drop the count. The actual change happens above 4031 * when the vap's count reaches the target count. 4032 */ 4033 if (vap->iv_csa_count == 0) { 4034 memmove(&csa[1], csa, bo->bo_csa_trailer_len); 4035 bo->bo_erp += sizeof(*csa); 4036 bo->bo_htinfo += sizeof(*csa); 4037 bo->bo_vhtinfo += sizeof(*csa); 4038 bo->bo_wme += sizeof(*csa); 4039 #ifdef IEEE80211_SUPPORT_SUPERG 4040 bo->bo_ath += sizeof(*csa); 4041 #endif 4042 #ifdef IEEE80211_SUPPORT_TDMA 4043 bo->bo_tdma += sizeof(*csa); 4044 #endif 4045 #ifdef IEEE80211_SUPPORT_MESH 4046 bo->bo_meshconf += sizeof(*csa); 4047 #endif 4048 bo->bo_appie += sizeof(*csa); 4049 bo->bo_csa_trailer_len += sizeof(*csa); 4050 bo->bo_quiet += sizeof(*csa); 4051 bo->bo_tim_trailer_len += sizeof(*csa); 4052 m->m_len += sizeof(*csa); 4053 m->m_pkthdr.len += sizeof(*csa); 4054 4055 ieee80211_add_csa(bo->bo_csa, vap); 4056 } else 4057 csa->csa_count--; 4058 vap->iv_csa_count++; 4059 /* NB: don't clear IEEE80211_BEACON_CSA */ 4060 } 4061 4062 /* 4063 * Only add the quiet time IE if we've enabled it 4064 * as appropriate. 4065 */ 4066 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) && 4067 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) { 4068 if (vap->iv_quiet && 4069 (vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE)) { 4070 ieee80211_add_quiet(bo->bo_quiet, vap, 1); 4071 } 4072 } 4073 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) { 4074 /* 4075 * ERP element needs updating. 4076 */ 4077 (void) ieee80211_add_erp(bo->bo_erp, vap); 4078 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP); 4079 } 4080 #ifdef IEEE80211_SUPPORT_SUPERG 4081 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) { 4082 ieee80211_add_athcaps(bo->bo_ath, ni); 4083 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH); 4084 } 4085 #endif 4086 } 4087 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) { 4088 const struct ieee80211_appie *aie = vap->iv_appie_beacon; 4089 int aielen; 4090 uint8_t *frm; 4091 4092 aielen = 0; 4093 if (aie != NULL) 4094 aielen += aie->ie_len; 4095 if (aielen != bo->bo_appie_len) { 4096 /* copy up/down trailer */ 4097 int adjust = aielen - bo->bo_appie_len; 4098 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust, 4099 bo->bo_tim_trailer_len); 4100 bo->bo_tim_trailer += adjust; 4101 bo->bo_appie += adjust; 4102 bo->bo_appie_len = aielen; 4103 4104 len_changed = 1; 4105 } 4106 frm = bo->bo_appie; 4107 if (aie != NULL) 4108 frm = add_appie(frm, aie); 4109 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE); 4110 } 4111 IEEE80211_UNLOCK(ic); 4112 4113 return len_changed; 4114 } 4115 4116 /* 4117 * Do Ethernet-LLC encapsulation for each payload in a fast frame 4118 * tunnel encapsulation. The frame is assumed to have an Ethernet 4119 * header at the front that must be stripped before prepending the 4120 * LLC followed by the Ethernet header passed in (with an Ethernet 4121 * type that specifies the payload size). 4122 */ 4123 struct mbuf * 4124 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m, 4125 const struct ether_header *eh) 4126 { 4127 struct llc *llc; 4128 uint16_t payload; 4129 4130 /* XXX optimize by combining m_adj+M_PREPEND */ 4131 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 4132 llc = mtod(m, struct llc *); 4133 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 4134 llc->llc_control = LLC_UI; 4135 llc->llc_snap.org_code[0] = 0; 4136 llc->llc_snap.org_code[1] = 0; 4137 llc->llc_snap.org_code[2] = 0; 4138 llc->llc_snap.ether_type = eh->ether_type; 4139 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */ 4140 4141 M_PREPEND(m, sizeof(struct ether_header), IEEE80211_M_NOWAIT); 4142 if (m == NULL) { /* XXX cannot happen */ 4143 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 4144 "%s: no space for ether_header\n", __func__); 4145 vap->iv_stats.is_tx_nobuf++; 4146 return NULL; 4147 } 4148 ETHER_HEADER_COPY(mtod(m, void *), eh); 4149 mtod(m, struct ether_header *)->ether_type = htons(payload); 4150 return m; 4151 } 4152 4153 /* 4154 * Complete an mbuf transmission. 4155 * 4156 * For now, this simply processes a completed frame after the 4157 * driver has completed it's transmission and/or retransmission. 4158 * It assumes the frame is an 802.11 encapsulated frame. 4159 * 4160 * Later on it will grow to become the exit path for a given frame 4161 * from the driver and, depending upon how it's been encapsulated 4162 * and already transmitted, it may end up doing A-MPDU retransmission, 4163 * power save requeuing, etc. 4164 * 4165 * In order for the above to work, the driver entry point to this 4166 * must not hold any driver locks. Thus, the driver needs to delay 4167 * any actual mbuf completion until it can release said locks. 4168 * 4169 * This frees the mbuf and if the mbuf has a node reference, 4170 * the node reference will be freed. 4171 */ 4172 void 4173 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status) 4174 { 4175 4176 if (ni != NULL) { 4177 struct ifnet *ifp = ni->ni_vap->iv_ifp; 4178 4179 if (status == 0) { 4180 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len); 4181 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 4182 if (m->m_flags & M_MCAST) 4183 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 4184 } else 4185 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 4186 if (m->m_flags & M_TXCB) { 4187 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 4188 "ni %p vap %p mode %s state %s m %p status %d\n", ni, ni->ni_vap, 4189 ieee80211_opmode_name[ni->ni_vap->iv_opmode], 4190 ieee80211_state_name[ni->ni_vap->iv_state], m, status); 4191 ieee80211_process_callback(ni, m, status); 4192 } 4193 ieee80211_free_node(ni); 4194 } 4195 m_freem(m); 4196 } 4197