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 (!IEEE80211_CONF_AMPDU_OFFLOAD(ic) && 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 (ieee80211_vap_ifp_check_is_monitor(vap)) 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 896 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic); 897 898 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type; 899 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) { 900 switch (vap->iv_opmode) { 901 case IEEE80211_M_STA: 902 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 903 IEEE80211_ADDR_COPY(wh->i_addr1, bssid); 904 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 905 IEEE80211_ADDR_COPY(wh->i_addr3, da); 906 break; 907 case IEEE80211_M_IBSS: 908 case IEEE80211_M_AHDEMO: 909 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 910 IEEE80211_ADDR_COPY(wh->i_addr1, da); 911 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 912 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 913 break; 914 case IEEE80211_M_HOSTAP: 915 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 916 IEEE80211_ADDR_COPY(wh->i_addr1, da); 917 IEEE80211_ADDR_COPY(wh->i_addr2, bssid); 918 IEEE80211_ADDR_COPY(wh->i_addr3, sa); 919 break; 920 case IEEE80211_M_WDS: 921 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 922 IEEE80211_ADDR_COPY(wh->i_addr1, da); 923 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 924 IEEE80211_ADDR_COPY(wh->i_addr3, da); 925 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa); 926 break; 927 case IEEE80211_M_MBSS: 928 #ifdef IEEE80211_SUPPORT_MESH 929 if (IEEE80211_IS_MULTICAST(da)) { 930 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 931 /* XXX next hop */ 932 IEEE80211_ADDR_COPY(wh->i_addr1, da); 933 IEEE80211_ADDR_COPY(wh->i_addr2, 934 vap->iv_myaddr); 935 } else { 936 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 937 IEEE80211_ADDR_COPY(wh->i_addr1, da); 938 IEEE80211_ADDR_COPY(wh->i_addr2, 939 vap->iv_myaddr); 940 IEEE80211_ADDR_COPY(wh->i_addr3, da); 941 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa); 942 } 943 #endif 944 break; 945 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */ 946 break; 947 } 948 } else { 949 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 950 IEEE80211_ADDR_COPY(wh->i_addr1, da); 951 IEEE80211_ADDR_COPY(wh->i_addr2, sa); 952 #ifdef IEEE80211_SUPPORT_MESH 953 if (vap->iv_opmode == IEEE80211_M_MBSS) 954 IEEE80211_ADDR_COPY(wh->i_addr3, sa); 955 else 956 #endif 957 IEEE80211_ADDR_COPY(wh->i_addr3, bssid); 958 } 959 *(uint16_t *)&wh->i_dur[0] = 0; 960 961 /* 962 * XXX TODO: this is what the TX lock is for. 963 * Here we're incrementing sequence numbers, and they 964 * need to be in lock-step with what the driver is doing 965 * both in TX ordering and crypto encap (IV increment.) 966 * 967 * If the driver does seqno itself, then we can skip 968 * assigning sequence numbers here, and we can avoid 969 * requiring the TX lock. 970 */ 971 tap = &ni->ni_tx_ampdu[tid]; 972 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap)) { 973 m->m_flags |= M_AMPDU_MPDU; 974 975 /* NB: zero out i_seq field (for s/w encryption etc) */ 976 *(uint16_t *)&wh->i_seq[0] = 0; 977 } else if (!IEEE80211_CONF_SEQNO_OFFLOAD(ni->ni_ic)) 978 ieee80211_output_seqno_assign(ni, tid, m); 979 980 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 981 m->m_flags |= M_MCAST; 982 #undef WH4 983 } 984 985 /* 986 * Send a management frame to the specified node. The node pointer 987 * must have a reference as the pointer will be passed to the driver 988 * and potentially held for a long time. If the frame is successfully 989 * dispatched to the driver, then it is responsible for freeing the 990 * reference (and potentially free'ing up any associated storage); 991 * otherwise deal with reclaiming any reference (on error). 992 */ 993 int 994 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type, 995 struct ieee80211_bpf_params *params) 996 { 997 struct ieee80211vap *vap = ni->ni_vap; 998 struct ieee80211com *ic = ni->ni_ic; 999 struct ieee80211_frame *wh; 1000 int ret; 1001 1002 KASSERT(ni != NULL, ("null node")); 1003 1004 if (vap->iv_state == IEEE80211_S_CAC) { 1005 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 1006 ni, "block %s frame in CAC state", 1007 ieee80211_mgt_subtype_name(type)); 1008 vap->iv_stats.is_tx_badstate++; 1009 ieee80211_free_node(ni); 1010 m_freem(m); 1011 return EIO; /* XXX */ 1012 } 1013 1014 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT); 1015 if (m == NULL) { 1016 ieee80211_free_node(ni); 1017 return ENOMEM; 1018 } 1019 1020 IEEE80211_TX_LOCK(ic); 1021 1022 wh = mtod(m, struct ieee80211_frame *); 1023 ieee80211_send_setup(ni, m, 1024 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID, 1025 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 1026 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) { 1027 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1, 1028 "encrypting frame (%s)", __func__); 1029 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED; 1030 } 1031 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1032 1033 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?")); 1034 M_WME_SETAC(m, params->ibp_pri); 1035 1036 #ifdef IEEE80211_DEBUG 1037 /* avoid printing too many frames */ 1038 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) || 1039 ieee80211_msg_dumppkts(vap)) { 1040 ieee80211_note(vap, "[%s] send %s on channel %u\n", 1041 ether_sprintf(wh->i_addr1), 1042 ieee80211_mgt_subtype_name(type), 1043 ieee80211_chan2ieee(ic, ic->ic_curchan)); 1044 } 1045 #endif 1046 IEEE80211_NODE_STAT(ni, tx_mgmt); 1047 1048 ret = ieee80211_raw_output(vap, ni, m, params); 1049 IEEE80211_TX_UNLOCK(ic); 1050 return (ret); 1051 } 1052 1053 static void 1054 ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg, 1055 int status) 1056 { 1057 struct ieee80211vap *vap = ni->ni_vap; 1058 1059 wakeup(vap); 1060 } 1061 1062 /* 1063 * Send a null data frame to the specified node. If the station 1064 * is setup for QoS then a QoS Null Data frame is constructed. 1065 * If this is a WDS station then a 4-address frame is constructed. 1066 * 1067 * NB: the caller is assumed to have setup a node reference 1068 * for use; this is necessary to deal with a race condition 1069 * when probing for inactive stations. Like ieee80211_mgmt_output 1070 * we must cleanup any node reference on error; however we 1071 * can safely just unref it as we know it will never be the 1072 * last reference to the node. 1073 */ 1074 int 1075 ieee80211_send_nulldata(struct ieee80211_node *ni) 1076 { 1077 struct ieee80211vap *vap = ni->ni_vap; 1078 struct ieee80211com *ic = ni->ni_ic; 1079 struct mbuf *m; 1080 struct ieee80211_frame *wh; 1081 int hdrlen; 1082 uint8_t *frm; 1083 int ret; 1084 1085 if (vap->iv_state == IEEE80211_S_CAC) { 1086 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH, 1087 ni, "block %s frame in CAC state", "null data"); 1088 ieee80211_node_decref(ni); 1089 vap->iv_stats.is_tx_badstate++; 1090 return EIO; /* XXX */ 1091 } 1092 1093 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) 1094 hdrlen = sizeof(struct ieee80211_qosframe); 1095 else 1096 hdrlen = sizeof(struct ieee80211_frame); 1097 /* NB: only WDS vap's get 4-address frames */ 1098 if (vap->iv_opmode == IEEE80211_M_WDS) 1099 hdrlen += IEEE80211_ADDR_LEN; 1100 if (ic->ic_flags & IEEE80211_F_DATAPAD) 1101 hdrlen = roundup(hdrlen, sizeof(uint32_t)); 1102 1103 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0); 1104 if (m == NULL) { 1105 /* XXX debug msg */ 1106 ieee80211_node_decref(ni); 1107 vap->iv_stats.is_tx_nobuf++; 1108 return ENOMEM; 1109 } 1110 KASSERT(M_LEADINGSPACE(m) >= hdrlen, 1111 ("leading space %zd", M_LEADINGSPACE(m))); 1112 M_PREPEND(m, hdrlen, IEEE80211_M_NOWAIT); 1113 if (m == NULL) { 1114 /* NB: cannot happen */ 1115 ieee80211_free_node(ni); 1116 return ENOMEM; 1117 } 1118 1119 IEEE80211_TX_LOCK(ic); 1120 1121 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */ 1122 if (ni->ni_flags & IEEE80211_NODE_QOS) { 1123 const int tid = WME_AC_TO_TID(WME_AC_BE); 1124 uint8_t *qos; 1125 1126 ieee80211_send_setup(ni, m, 1127 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL, 1128 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 1129 1130 if (vap->iv_opmode == IEEE80211_M_WDS) 1131 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 1132 else 1133 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 1134 qos[0] = tid & IEEE80211_QOS_TID; 1135 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy) 1136 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 1137 qos[1] = 0; 1138 } else { 1139 ieee80211_send_setup(ni, m, 1140 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA, 1141 IEEE80211_NONQOS_TID, 1142 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid); 1143 } 1144 if (vap->iv_opmode != IEEE80211_M_WDS) { 1145 /* NB: power management bit is never sent by an AP */ 1146 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 1147 vap->iv_opmode != IEEE80211_M_HOSTAP) 1148 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT; 1149 } 1150 if ((ic->ic_flags & IEEE80211_F_SCAN) && 1151 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) { 1152 ieee80211_add_callback(m, ieee80211_nulldata_transmitted, 1153 NULL); 1154 } 1155 m->m_len = m->m_pkthdr.len = hdrlen; 1156 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1157 1158 M_WME_SETAC(m, WME_AC_BE); 1159 1160 IEEE80211_NODE_STAT(ni, tx_data); 1161 1162 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni, 1163 "send %snull data frame on channel %u, pwr mgt %s", 1164 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "", 1165 ieee80211_chan2ieee(ic, ic->ic_curchan), 1166 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis"); 1167 1168 ret = ieee80211_raw_output(vap, ni, m, NULL); 1169 IEEE80211_TX_UNLOCK(ic); 1170 return (ret); 1171 } 1172 1173 /* 1174 * Assign priority to a frame based on any vlan tag assigned 1175 * to the station and/or any Diffserv setting in an IP header. 1176 * Finally, if an ACM policy is setup (in station mode) it's 1177 * applied. 1178 */ 1179 int 1180 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m) 1181 { 1182 const struct ether_header *eh = NULL; 1183 uint16_t ether_type; 1184 int v_wme_ac, d_wme_ac, ac; 1185 1186 if (__predict_false(m->m_flags & M_ENCAP)) { 1187 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *); 1188 struct llc *llc; 1189 int hdrlen, subtype; 1190 1191 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1192 if (subtype & IEEE80211_FC0_SUBTYPE_NODATA) { 1193 ac = WME_AC_BE; 1194 goto done; 1195 } 1196 1197 hdrlen = ieee80211_hdrsize(wh); 1198 if (m->m_pkthdr.len < hdrlen + sizeof(*llc)) 1199 return 1; 1200 1201 llc = (struct llc *)mtodo(m, hdrlen); 1202 if (llc->llc_dsap != LLC_SNAP_LSAP || 1203 llc->llc_ssap != LLC_SNAP_LSAP || 1204 llc->llc_control != LLC_UI || 1205 llc->llc_snap.org_code[0] != 0 || 1206 llc->llc_snap.org_code[1] != 0 || 1207 llc->llc_snap.org_code[2] != 0) 1208 return 1; 1209 1210 ether_type = llc->llc_snap.ether_type; 1211 } else { 1212 eh = mtod(m, struct ether_header *); 1213 ether_type = eh->ether_type; 1214 } 1215 1216 /* 1217 * Always promote PAE/EAPOL frames to high priority. 1218 */ 1219 if (ether_type == htons(ETHERTYPE_PAE)) { 1220 /* NB: mark so others don't need to check header */ 1221 m->m_flags |= M_EAPOL; 1222 ac = WME_AC_VO; 1223 goto done; 1224 } 1225 /* 1226 * Non-qos traffic goes to BE. 1227 */ 1228 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) { 1229 ac = WME_AC_BE; 1230 goto done; 1231 } 1232 1233 /* 1234 * If node has a vlan tag then all traffic 1235 * to it must have a matching tag. 1236 */ 1237 v_wme_ac = 0; 1238 if (ni->ni_vlan != 0) { 1239 if ((m->m_flags & M_VLANTAG) == 0) { 1240 IEEE80211_NODE_STAT(ni, tx_novlantag); 1241 return 1; 1242 } 1243 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 1244 EVL_VLANOFTAG(ni->ni_vlan)) { 1245 IEEE80211_NODE_STAT(ni, tx_vlanmismatch); 1246 return 1; 1247 } 1248 /* map vlan priority to AC */ 1249 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan)); 1250 } 1251 1252 if (eh == NULL) 1253 goto no_eh; 1254 1255 /* XXX m_copydata may be too slow for fast path */ 1256 switch (ntohs(eh->ether_type)) { 1257 #ifdef INET 1258 case ETHERTYPE_IP: 1259 { 1260 uint8_t tos; 1261 /* 1262 * IP frame, map the DSCP bits from the TOS field. 1263 */ 1264 /* NB: ip header may not be in first mbuf */ 1265 m_copydata(m, sizeof(struct ether_header) + 1266 offsetof(struct ip, ip_tos), sizeof(tos), &tos); 1267 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */ 1268 d_wme_ac = TID_TO_WME_AC(tos); 1269 break; 1270 } 1271 #endif 1272 #ifdef INET6 1273 case ETHERTYPE_IPV6: 1274 { 1275 uint32_t flow; 1276 uint8_t tos; 1277 /* 1278 * IPv6 frame, map the DSCP bits from the traffic class field. 1279 */ 1280 m_copydata(m, sizeof(struct ether_header) + 1281 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow), 1282 (caddr_t) &flow); 1283 tos = (uint8_t)(ntohl(flow) >> 20); 1284 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */ 1285 d_wme_ac = TID_TO_WME_AC(tos); 1286 break; 1287 } 1288 #endif 1289 default: 1290 no_eh: 1291 d_wme_ac = WME_AC_BE; 1292 break; 1293 } 1294 1295 /* 1296 * Use highest priority AC. 1297 */ 1298 if (v_wme_ac > d_wme_ac) 1299 ac = v_wme_ac; 1300 else 1301 ac = d_wme_ac; 1302 1303 /* 1304 * Apply ACM policy. 1305 */ 1306 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) { 1307 static const int acmap[4] = { 1308 WME_AC_BK, /* WME_AC_BE */ 1309 WME_AC_BK, /* WME_AC_BK */ 1310 WME_AC_BE, /* WME_AC_VI */ 1311 WME_AC_VI, /* WME_AC_VO */ 1312 }; 1313 struct ieee80211com *ic = ni->ni_ic; 1314 1315 while (ac != WME_AC_BK && 1316 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm) 1317 ac = acmap[ac]; 1318 } 1319 done: 1320 M_WME_SETAC(m, ac); 1321 return 0; 1322 } 1323 1324 /* 1325 * Insure there is sufficient contiguous space to encapsulate the 1326 * 802.11 data frame. If room isn't already there, arrange for it. 1327 * Drivers and cipher modules assume we have done the necessary work 1328 * and fail rudely if they don't find the space they need. 1329 */ 1330 struct mbuf * 1331 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize, 1332 struct ieee80211_key *key, struct mbuf *m) 1333 { 1334 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc)) 1335 int needed_space = vap->iv_ic->ic_headroom + hdrsize; 1336 1337 if (key != NULL) { 1338 /* XXX belongs in crypto code? */ 1339 needed_space += key->wk_cipher->ic_header; 1340 /* XXX frags */ 1341 /* 1342 * When crypto is being done in the host we must insure 1343 * the data are writable for the cipher routines; clone 1344 * a writable mbuf chain. 1345 * XXX handle SWMIC specially 1346 */ 1347 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) { 1348 m = m_unshare(m, IEEE80211_M_NOWAIT); 1349 if (m == NULL) { 1350 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 1351 "%s: cannot get writable mbuf\n", __func__); 1352 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */ 1353 return NULL; 1354 } 1355 } 1356 } 1357 /* 1358 * We know we are called just before stripping an Ethernet 1359 * header and prepending an LLC header. This means we know 1360 * there will be 1361 * sizeof(struct ether_header) - sizeof(struct llc) 1362 * bytes recovered to which we need additional space for the 1363 * 802.11 header and any crypto header. 1364 */ 1365 /* XXX check trailing space and copy instead? */ 1366 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) { 1367 struct mbuf *n = m_gethdr(IEEE80211_M_NOWAIT, m->m_type); 1368 if (n == NULL) { 1369 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT, 1370 "%s: cannot expand storage\n", __func__); 1371 vap->iv_stats.is_tx_nobuf++; 1372 m_freem(m); 1373 return NULL; 1374 } 1375 KASSERT(needed_space <= MHLEN, 1376 ("not enough room, need %u got %d\n", needed_space, MHLEN)); 1377 /* 1378 * Setup new mbuf to have leading space to prepend the 1379 * 802.11 header and any crypto header bits that are 1380 * required (the latter are added when the driver calls 1381 * back to ieee80211_crypto_encap to do crypto encapsulation). 1382 */ 1383 /* NB: must be first 'cuz it clobbers m_data */ 1384 m_move_pkthdr(n, m); 1385 n->m_len = 0; /* NB: m_gethdr does not set */ 1386 n->m_data += needed_space; 1387 /* 1388 * Pull up Ethernet header to create the expected layout. 1389 * We could use m_pullup but that's overkill (i.e. we don't 1390 * need the actual data) and it cannot fail so do it inline 1391 * for speed. 1392 */ 1393 /* NB: struct ether_header is known to be contiguous */ 1394 n->m_len += sizeof(struct ether_header); 1395 m->m_len -= sizeof(struct ether_header); 1396 m->m_data += sizeof(struct ether_header); 1397 /* 1398 * Replace the head of the chain. 1399 */ 1400 n->m_next = m; 1401 m = n; 1402 } 1403 return m; 1404 #undef TO_BE_RECLAIMED 1405 } 1406 1407 /* 1408 * Return the transmit key to use in sending a unicast frame. 1409 * If a unicast key is set we use that. When no unicast key is set 1410 * we fall back to the default transmit key. 1411 */ 1412 static __inline struct ieee80211_key * 1413 ieee80211_crypto_getucastkey(struct ieee80211vap *vap, 1414 struct ieee80211_node *ni) 1415 { 1416 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 1417 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE || 1418 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey])) 1419 return NULL; 1420 return &vap->iv_nw_keys[vap->iv_def_txkey]; 1421 } else { 1422 return &ni->ni_ucastkey; 1423 } 1424 } 1425 1426 /* 1427 * Return the transmit key to use in sending a multicast frame. 1428 * Multicast traffic always uses the group key which is installed as 1429 * the default tx key. 1430 */ 1431 static __inline struct ieee80211_key * 1432 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap, 1433 struct ieee80211_node *ni) 1434 { 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 } 1440 1441 /* 1442 * Encapsulate an outbound data frame. The mbuf chain is updated. 1443 * If an error is encountered NULL is returned. The caller is required 1444 * to provide a node reference and pullup the ethernet header in the 1445 * first mbuf. 1446 * 1447 * NB: Packet is assumed to be processed by ieee80211_classify which 1448 * marked EAPOL frames w/ M_EAPOL. 1449 */ 1450 struct mbuf * 1451 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni, 1452 struct mbuf *m) 1453 { 1454 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh)) 1455 #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc) 1456 struct ieee80211com *ic = ni->ni_ic; 1457 #ifdef IEEE80211_SUPPORT_MESH 1458 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1459 struct ieee80211_meshcntl_ae10 *mc; 1460 struct ieee80211_mesh_route *rt = NULL; 1461 int dir = -1; 1462 #endif 1463 struct ether_header eh; 1464 struct ieee80211_frame *wh; 1465 struct ieee80211_key *key; 1466 struct llc *llc; 1467 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr, is_mcast; 1468 int meshhdrsize, meshae; 1469 uint8_t *qos; 1470 int is_amsdu = 0; 1471 1472 IEEE80211_TX_LOCK_ASSERT(ic); 1473 1474 is_mcast = !! (m->m_flags & (M_MCAST | M_BCAST)); 1475 1476 /* 1477 * Copy existing Ethernet header to a safe place. The 1478 * rest of the code assumes it's ok to strip it when 1479 * reorganizing state for the final encapsulation. 1480 */ 1481 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!")); 1482 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t)); 1483 1484 /* 1485 * Insure space for additional headers. First identify 1486 * transmit key to use in calculating any buffer adjustments 1487 * required. This is also used below to do privacy 1488 * encapsulation work. Then calculate the 802.11 header 1489 * size and any padding required by the driver. 1490 * 1491 * Note key may be NULL if we fall back to the default 1492 * transmit key and that is not set. In that case the 1493 * buffer may not be expanded as needed by the cipher 1494 * routines, but they will/should discard it. 1495 */ 1496 if (vap->iv_flags & IEEE80211_F_PRIVACY) { 1497 if (vap->iv_opmode == IEEE80211_M_STA || 1498 !IEEE80211_IS_MULTICAST(eh.ether_dhost) || 1499 (vap->iv_opmode == IEEE80211_M_WDS && 1500 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) { 1501 key = ieee80211_crypto_getucastkey(vap, ni); 1502 } else if ((vap->iv_opmode == IEEE80211_M_WDS) && 1503 (! (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) { 1504 /* 1505 * Use ucastkey for DWDS transmit nodes, multicast 1506 * or otherwise. 1507 * 1508 * This is required to ensure that multicast frames 1509 * from a DWDS AP to a DWDS STA is encrypted with 1510 * a key that can actually work. 1511 * 1512 * There's no default key for multicast traffic 1513 * on a DWDS WDS VAP node (note NOT the DWDS enabled 1514 * AP VAP, the dynamically created per-STA WDS node) 1515 * so encap fails and transmit fails. 1516 */ 1517 key = ieee80211_crypto_getucastkey(vap, ni); 1518 } else { 1519 key = ieee80211_crypto_getmcastkey(vap, ni); 1520 } 1521 if (key == NULL && (m->m_flags & M_EAPOL) == 0) { 1522 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 1523 eh.ether_dhost, 1524 "no default transmit key (%s) deftxkey %u", 1525 __func__, vap->iv_def_txkey); 1526 vap->iv_stats.is_tx_nodefkey++; 1527 goto bad; 1528 } 1529 } else 1530 key = NULL; 1531 /* 1532 * XXX Some ap's don't handle QoS-encapsulated EAPOL 1533 * frames so suppress use. This may be an issue if other 1534 * ap's require all data frames to be QoS-encapsulated 1535 * once negotiated in which case we'll need to make this 1536 * configurable. 1537 * 1538 * Don't send multicast QoS frames. 1539 * Technically multicast frames can be QoS if all stations in the 1540 * BSS are also QoS. 1541 * 1542 * NB: mesh data frames are QoS, including multicast frames. 1543 */ 1544 addqos = 1545 (((is_mcast == 0) && (ni->ni_flags & 1546 (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))) || 1547 (vap->iv_opmode == IEEE80211_M_MBSS)) && 1548 (m->m_flags & M_EAPOL) == 0; 1549 1550 if (addqos) 1551 hdrsize = sizeof(struct ieee80211_qosframe); 1552 else 1553 hdrsize = sizeof(struct ieee80211_frame); 1554 #ifdef IEEE80211_SUPPORT_MESH 1555 if (vap->iv_opmode == IEEE80211_M_MBSS) { 1556 /* 1557 * Mesh data frames are encapsulated according to the 1558 * rules of Section 11B.8.5 (p.139 of D3.0 spec). 1559 * o Group Addressed data (aka multicast) originating 1560 * at the local sta are sent w/ 3-address format and 1561 * address extension mode 00 1562 * o Individually Addressed data (aka unicast) originating 1563 * at the local sta are sent w/ 4-address format and 1564 * address extension mode 00 1565 * o Group Addressed data forwarded from a non-mesh sta are 1566 * sent w/ 3-address format and address extension mode 01 1567 * o Individually Address data from another sta are sent 1568 * w/ 4-address format and address extension mode 10 1569 */ 1570 is4addr = 0; /* NB: don't use, disable */ 1571 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) { 1572 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost); 1573 KASSERT(rt != NULL, ("route is NULL")); 1574 dir = IEEE80211_FC1_DIR_DSTODS; 1575 hdrsize += IEEE80211_ADDR_LEN; 1576 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) { 1577 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate, 1578 vap->iv_myaddr)) { 1579 IEEE80211_NOTE_MAC(vap, 1580 IEEE80211_MSG_MESH, 1581 eh.ether_dhost, 1582 "%s", "trying to send to ourself"); 1583 goto bad; 1584 } 1585 meshae = IEEE80211_MESH_AE_10; 1586 meshhdrsize = 1587 sizeof(struct ieee80211_meshcntl_ae10); 1588 } else { 1589 meshae = IEEE80211_MESH_AE_00; 1590 meshhdrsize = 1591 sizeof(struct ieee80211_meshcntl); 1592 } 1593 } else { 1594 dir = IEEE80211_FC1_DIR_FROMDS; 1595 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) { 1596 /* proxy group */ 1597 meshae = IEEE80211_MESH_AE_01; 1598 meshhdrsize = 1599 sizeof(struct ieee80211_meshcntl_ae01); 1600 } else { 1601 /* group */ 1602 meshae = IEEE80211_MESH_AE_00; 1603 meshhdrsize = sizeof(struct ieee80211_meshcntl); 1604 } 1605 } 1606 } else { 1607 #endif 1608 /* 1609 * 4-address frames need to be generated for: 1610 * o packets sent through a WDS vap (IEEE80211_M_WDS) 1611 * o packets sent through a vap marked for relaying 1612 * (e.g. a station operating with dynamic WDS) 1613 */ 1614 is4addr = vap->iv_opmode == IEEE80211_M_WDS || 1615 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) && 1616 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)); 1617 if (is4addr) 1618 hdrsize += IEEE80211_ADDR_LEN; 1619 meshhdrsize = meshae = 0; 1620 #ifdef IEEE80211_SUPPORT_MESH 1621 } 1622 #endif 1623 /* 1624 * Honor driver DATAPAD requirement. 1625 */ 1626 if (ic->ic_flags & IEEE80211_F_DATAPAD) 1627 hdrspace = roundup(hdrsize, sizeof(uint32_t)); 1628 else 1629 hdrspace = hdrsize; 1630 1631 if (__predict_true((m->m_flags & M_FF) == 0)) { 1632 /* 1633 * Normal frame. 1634 */ 1635 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m); 1636 if (m == NULL) { 1637 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 1638 goto bad; 1639 } 1640 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */ 1641 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 1642 llc = mtod(m, struct llc *); 1643 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 1644 llc->llc_control = LLC_UI; 1645 llc->llc_snap.org_code[0] = 0; 1646 llc->llc_snap.org_code[1] = 0; 1647 llc->llc_snap.org_code[2] = 0; 1648 llc->llc_snap.ether_type = eh.ether_type; 1649 } else { 1650 #ifdef IEEE80211_SUPPORT_SUPERG 1651 /* 1652 * Aggregated frame. Check if it's for AMSDU or FF. 1653 * 1654 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented 1655 * anywhere for some reason. But, since 11n requires 1656 * AMSDU RX, we can just assume "11n" == "AMSDU". 1657 */ 1658 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__); 1659 if (ieee80211_amsdu_tx_ok(ni)) { 1660 m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key); 1661 is_amsdu = 1; 1662 } else { 1663 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key); 1664 } 1665 if (m == NULL) 1666 #endif 1667 goto bad; 1668 } 1669 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */ 1670 1671 M_PREPEND(m, hdrspace + meshhdrsize, IEEE80211_M_NOWAIT); 1672 if (m == NULL) { 1673 vap->iv_stats.is_tx_nobuf++; 1674 goto bad; 1675 } 1676 wh = mtod(m, struct ieee80211_frame *); 1677 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 1678 *(uint16_t *)wh->i_dur = 0; 1679 qos = NULL; /* NB: quiet compiler */ 1680 if (is4addr) { 1681 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS; 1682 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr); 1683 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1684 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 1685 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost); 1686 } else switch (vap->iv_opmode) { 1687 case IEEE80211_M_STA: 1688 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 1689 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 1690 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 1691 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 1692 break; 1693 case IEEE80211_M_IBSS: 1694 case IEEE80211_M_AHDEMO: 1695 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1696 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1697 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 1698 /* 1699 * NB: always use the bssid from iv_bss as the 1700 * neighbor's may be stale after an ibss merge 1701 */ 1702 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid); 1703 break; 1704 case IEEE80211_M_HOSTAP: 1705 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 1706 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1707 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 1708 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 1709 break; 1710 #ifdef IEEE80211_SUPPORT_MESH 1711 case IEEE80211_M_MBSS: 1712 /* NB: offset by hdrspace to deal with DATAPAD */ 1713 mc = (struct ieee80211_meshcntl_ae10 *) 1714 (mtod(m, uint8_t *) + hdrspace); 1715 wh->i_fc[1] = dir; 1716 switch (meshae) { 1717 case IEEE80211_MESH_AE_00: /* no proxy */ 1718 mc->mc_flags = 0; 1719 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */ 1720 IEEE80211_ADDR_COPY(wh->i_addr1, 1721 ni->ni_macaddr); 1722 IEEE80211_ADDR_COPY(wh->i_addr2, 1723 vap->iv_myaddr); 1724 IEEE80211_ADDR_COPY(wh->i_addr3, 1725 eh.ether_dhost); 1726 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, 1727 eh.ether_shost); 1728 qos =((struct ieee80211_qosframe_addr4 *) 1729 wh)->i_qos; 1730 } else if (dir == IEEE80211_FC1_DIR_FROMDS) { 1731 /* mcast */ 1732 IEEE80211_ADDR_COPY(wh->i_addr1, 1733 eh.ether_dhost); 1734 IEEE80211_ADDR_COPY(wh->i_addr2, 1735 vap->iv_myaddr); 1736 IEEE80211_ADDR_COPY(wh->i_addr3, 1737 eh.ether_shost); 1738 qos = ((struct ieee80211_qosframe *) 1739 wh)->i_qos; 1740 } 1741 break; 1742 case IEEE80211_MESH_AE_01: /* mcast, proxy */ 1743 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 1744 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 1745 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1746 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr); 1747 mc->mc_flags = 1; 1748 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4, 1749 eh.ether_shost); 1750 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 1751 break; 1752 case IEEE80211_MESH_AE_10: /* ucast, proxy */ 1753 KASSERT(rt != NULL, ("route is NULL")); 1754 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop); 1755 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 1756 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate); 1757 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr); 1758 mc->mc_flags = IEEE80211_MESH_AE_10; 1759 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost); 1760 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost); 1761 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 1762 break; 1763 default: 1764 KASSERT(0, ("meshae %d", meshae)); 1765 break; 1766 } 1767 mc->mc_ttl = ms->ms_ttl; 1768 ms->ms_seq++; 1769 le32enc(mc->mc_seq, ms->ms_seq); 1770 break; 1771 #endif 1772 case IEEE80211_M_WDS: /* NB: is4addr should always be true */ 1773 default: 1774 goto bad; 1775 } 1776 if (m->m_flags & M_MORE_DATA) 1777 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 1778 if (addqos) { 1779 int ac, tid; 1780 1781 if (is4addr) { 1782 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos; 1783 /* NB: mesh case handled earlier */ 1784 } else if (vap->iv_opmode != IEEE80211_M_MBSS) 1785 qos = ((struct ieee80211_qosframe *) wh)->i_qos; 1786 ac = M_WME_GETAC(m); 1787 /* map from access class/queue to 11e header priorty value */ 1788 tid = WME_AC_TO_TID(ac); 1789 qos[0] = tid & IEEE80211_QOS_TID; 1790 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy) 1791 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK; 1792 #ifdef IEEE80211_SUPPORT_MESH 1793 if (vap->iv_opmode == IEEE80211_M_MBSS) 1794 qos[1] = IEEE80211_QOS_MC; 1795 else 1796 #endif 1797 qos[1] = 0; 1798 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS_DATA; 1799 1800 /* 1801 * If this is an A-MSDU then ensure we set the 1802 * relevant field. 1803 */ 1804 if (is_amsdu) 1805 qos[0] |= IEEE80211_QOS_AMSDU; 1806 1807 /* 1808 * XXX TODO TX lock is needed for atomic updates of sequence 1809 * numbers. If the driver does it, then don't do it here; 1810 * and we don't need the TX lock held. 1811 */ 1812 if ((m->m_flags & M_AMPDU_MPDU) == 0) { 1813 if (!IEEE80211_CONF_SEQNO_OFFLOAD(ic)) 1814 ieee80211_output_seqno_assign(ni, tid, m); 1815 } else { 1816 /* 1817 * NB: don't assign a sequence # to potential 1818 * aggregates; we expect this happens at the 1819 * point the frame comes off any aggregation q 1820 * as otherwise we may introduce holes in the 1821 * BA sequence space and/or make window accouting 1822 * more difficult. 1823 * 1824 * XXX may want to control this with a driver 1825 * capability; this may also change when we pull 1826 * aggregation up into net80211 1827 */ 1828 /* NB: zero out i_seq field (for s/w encryption etc) */ 1829 *(uint16_t *)wh->i_seq = 0; 1830 } 1831 } else { 1832 if (!IEEE80211_CONF_SEQNO_OFFLOAD(ic)) 1833 ieee80211_output_seqno_assign(ni, IEEE80211_NONQOS_TID, 1834 m); 1835 /* 1836 * XXX TODO: we shouldn't allow EAPOL, etc that would 1837 * be forced to be non-QoS traffic to be A-MSDU encapsulated. 1838 */ 1839 if (is_amsdu) 1840 net80211_vap_printf(vap, 1841 "%s: XXX ERROR: is_amsdu set; not QoS!\n", 1842 __func__); 1843 } 1844 1845 /* 1846 * Check if xmit fragmentation is required. 1847 * 1848 * If the hardware does fragmentation offload, then don't bother 1849 * doing it here. 1850 * 1851 * Don't send AMPDU/FF/AMSDU through fragmentation. 1852 * 1853 * 802.11-2016 10.2.7 (Fragmentation/defragmentation overview) 1854 */ 1855 if (IEEE80211_CONF_FRAG_OFFLOAD(ic)) 1856 txfrag = 0; 1857 else 1858 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold && 1859 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 1860 (vap->iv_caps & IEEE80211_C_TXFRAG) && 1861 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0); 1862 1863 if (key != NULL) { 1864 /* 1865 * IEEE 802.1X: send EAPOL frames always in the clear. 1866 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set. 1867 */ 1868 if ((m->m_flags & M_EAPOL) == 0 || 1869 ((vap->iv_flags & IEEE80211_F_WPA) && 1870 (vap->iv_opmode == IEEE80211_M_STA ? 1871 !IEEE80211_KEY_UNDEFINED(key) : 1872 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) { 1873 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED; 1874 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) { 1875 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, 1876 eh.ether_dhost, 1877 "%s", "enmic failed, discard frame"); 1878 vap->iv_stats.is_crypto_enmicfail++; 1879 goto bad; 1880 } 1881 } 1882 } 1883 if (txfrag && !ieee80211_fragment(vap, m, hdrsize, 1884 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold)) 1885 goto bad; 1886 1887 m->m_flags |= M_ENCAP; /* mark encapsulated */ 1888 1889 IEEE80211_NODE_STAT(ni, tx_data); 1890 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1891 IEEE80211_NODE_STAT(ni, tx_mcast); 1892 m->m_flags |= M_MCAST; 1893 } else 1894 IEEE80211_NODE_STAT(ni, tx_ucast); 1895 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen); 1896 1897 return m; 1898 bad: 1899 if (m != NULL) 1900 m_freem(m); 1901 return NULL; 1902 #undef WH4 1903 #undef MC01 1904 } 1905 1906 /** 1907 * @brief Free an 802.11 frame mbuf. 1908 * 1909 * Note that since a "frame" may consist of an mbuf packet 1910 * list containing the 802.11 fragments that make up said 1911 * frame, it will free everything in the mbuf packet list. 1912 * 1913 * @param m mbuf packet list to free 1914 */ 1915 void 1916 ieee80211_free_mbuf(struct mbuf *m) 1917 { 1918 struct mbuf *next; 1919 1920 if (m == NULL) 1921 return; 1922 1923 do { 1924 next = m->m_nextpkt; 1925 m->m_nextpkt = NULL; 1926 m_freem(m); 1927 } while ((m = next) != NULL); 1928 } 1929 1930 /** 1931 * @brief Fragment the frame according to the specified mtu. 1932 * 1933 * This implements the fragmentation part of 802.11-2016 10.2.7 1934 * (Fragmentation/defragmentation overview.) 1935 * 1936 * The size of the 802.11 header (w/o padding) is provided 1937 * so we don't need to recalculate it. We create a new 1938 * mbuf for each fragment and chain it through m_nextpkt; 1939 * we might be able to optimize this by reusing the original 1940 * packet's mbufs but that is significantly more complicated. 1941 * 1942 * A node reference is NOT acquired for each fragment in 1943 * the list - the caller is assumed to have taken a node 1944 * reference for the whole list. The fragment mbufs do not 1945 * have a node pointer. 1946 * 1947 * Fragments will have the sequence number and fragment numbers 1948 * assigned. However, Fragments will NOT have a sequence number 1949 * assigned via M_SEQNO_SET. 1950 * 1951 * This must be called after assigning sequence numbers; it 1952 * modifies the i_seq field in the 802.11 header to include 1953 * the fragment number. 1954 * 1955 * @param vap ieee80211vap interface 1956 * @param m0 pointer to mbuf list to fragment 1957 * @param hdrsize header size to reserver 1958 * @param ciphdrsize crypto cipher header size to reserve 1959 * @param mtu maximum fragment size 1960 * @retval 1 if successful, with the mbuf pointed at by m0 1961 * turned into an mbuf list of fragments (with the original 1962 * mbuf being truncated.) 1963 * @retval 0 if failure, the mbuf needs to be freed by the caller 1964 */ 1965 static int 1966 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0, 1967 u_int hdrsize, u_int ciphdrsize, u_int mtu) 1968 { 1969 struct ieee80211com *ic = vap->iv_ic; 1970 struct ieee80211_frame *wh, *whf; 1971 struct mbuf *m, *prev; 1972 u_int totalhdrsize, fragno, fragsize, off, remainder, payload; 1973 u_int hdrspace; 1974 1975 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?")); 1976 KASSERT(m0->m_pkthdr.len > mtu, 1977 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu)); 1978 1979 /* 1980 * Honor driver DATAPAD requirement. 1981 */ 1982 if (ic->ic_flags & IEEE80211_F_DATAPAD) 1983 hdrspace = roundup(hdrsize, sizeof(uint32_t)); 1984 else 1985 hdrspace = hdrsize; 1986 1987 wh = mtod(m0, struct ieee80211_frame *); 1988 /* NB: mark the first frag; it will be propagated below */ 1989 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG; 1990 totalhdrsize = hdrspace + ciphdrsize; 1991 fragno = 1; 1992 off = mtu - ciphdrsize; 1993 remainder = m0->m_pkthdr.len - off; 1994 prev = m0; 1995 do { 1996 fragsize = MIN(totalhdrsize + remainder, mtu); 1997 m = m_get2(fragsize, IEEE80211_M_NOWAIT, MT_DATA, M_PKTHDR); 1998 if (m == NULL) 1999 goto bad; 2000 /* leave room to prepend any cipher header */ 2001 m_align(m, fragsize - ciphdrsize); 2002 2003 /* 2004 * Form the header in the fragment. Note that since 2005 * we mark the first fragment with the MORE_FRAG bit 2006 * it automatically is propagated to each fragment; we 2007 * need only clear it on the last fragment (done below). 2008 * NB: frag 1+ dont have Mesh Control field present. 2009 */ 2010 whf = mtod(m, struct ieee80211_frame *); 2011 memcpy(whf, wh, hdrsize); 2012 #ifdef IEEE80211_SUPPORT_MESH 2013 if (vap->iv_opmode == IEEE80211_M_MBSS) 2014 ieee80211_getqos(wh)[1] &= ~IEEE80211_QOS_MC; 2015 #endif 2016 *(uint16_t *)&whf->i_seq[0] |= htole16( 2017 (fragno & IEEE80211_SEQ_FRAG_MASK) << 2018 IEEE80211_SEQ_FRAG_SHIFT); 2019 fragno++; 2020 2021 payload = fragsize - totalhdrsize; 2022 /* NB: destination is known to be contiguous */ 2023 2024 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace); 2025 m->m_len = hdrspace + payload; 2026 m->m_pkthdr.len = hdrspace + payload; 2027 m->m_flags |= M_FRAG; 2028 2029 /* chain up the fragment */ 2030 prev->m_nextpkt = m; 2031 prev = m; 2032 2033 /* deduct fragment just formed */ 2034 remainder -= payload; 2035 off += payload; 2036 } while (remainder != 0); 2037 2038 /* set the last fragment */ 2039 m->m_flags |= M_LASTFRAG; 2040 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG; 2041 2042 /* strip first mbuf now that everything has been copied */ 2043 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize))); 2044 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 2045 2046 vap->iv_stats.is_tx_fragframes++; 2047 vap->iv_stats.is_tx_frags += fragno-1; 2048 2049 return 1; 2050 bad: 2051 /* reclaim fragments but leave original frame for caller to free */ 2052 ieee80211_free_mbuf(m0->m_nextpkt); 2053 m0->m_nextpkt = NULL; 2054 return 0; 2055 } 2056 2057 /* 2058 * Add a supported rates element id to a frame. 2059 */ 2060 uint8_t * 2061 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs) 2062 { 2063 int nrates; 2064 2065 *frm++ = IEEE80211_ELEMID_RATES; 2066 nrates = rs->rs_nrates; 2067 if (nrates > IEEE80211_RATE_SIZE) 2068 nrates = IEEE80211_RATE_SIZE; 2069 *frm++ = nrates; 2070 memcpy(frm, rs->rs_rates, nrates); 2071 return frm + nrates; 2072 } 2073 2074 /* 2075 * Add an extended supported rates element id to a frame. 2076 */ 2077 uint8_t * 2078 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs) 2079 { 2080 /* 2081 * Add an extended supported rates element if operating in 11g mode. 2082 */ 2083 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 2084 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 2085 *frm++ = IEEE80211_ELEMID_XRATES; 2086 *frm++ = nrates; 2087 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 2088 frm += nrates; 2089 } 2090 return frm; 2091 } 2092 2093 /* 2094 * Add an ssid element to a frame. 2095 */ 2096 uint8_t * 2097 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len) 2098 { 2099 *frm++ = IEEE80211_ELEMID_SSID; 2100 *frm++ = len; 2101 memcpy(frm, ssid, len); 2102 return frm + len; 2103 } 2104 2105 /* 2106 * Add an erp element to a frame. 2107 */ 2108 static uint8_t * 2109 ieee80211_add_erp(uint8_t *frm, struct ieee80211vap *vap) 2110 { 2111 struct ieee80211com *ic = vap->iv_ic; 2112 uint8_t erp; 2113 2114 *frm++ = IEEE80211_ELEMID_ERP; 2115 *frm++ = 1; 2116 erp = 0; 2117 2118 /* 2119 * TODO: This uses the global flags for now because 2120 * the per-VAP flags are fine for per-VAP, but don't 2121 * take into account which VAPs share the same channel 2122 * and which are on different channels. 2123 * 2124 * ERP and HT/VHT protection mode is a function of 2125 * how many stations are on a channel, not specifically 2126 * the VAP or global. But, until we grow that status, 2127 * the global flag will have to do. 2128 */ 2129 if (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) 2130 erp |= IEEE80211_ERP_NON_ERP_PRESENT; 2131 2132 /* 2133 * TODO: same as above; these should be based not 2134 * on the vap or ic flags, but instead on a combination 2135 * of per-VAP and channels. 2136 */ 2137 if (ic->ic_flags & IEEE80211_F_USEPROT) 2138 erp |= IEEE80211_ERP_USE_PROTECTION; 2139 if (ic->ic_flags & IEEE80211_F_USEBARKER) 2140 erp |= IEEE80211_ERP_LONG_PREAMBLE; 2141 *frm++ = erp; 2142 return frm; 2143 } 2144 2145 /* 2146 * Add a CFParams element to a frame. 2147 */ 2148 static uint8_t * 2149 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic) 2150 { 2151 #define ADDSHORT(frm, v) do { \ 2152 le16enc(frm, v); \ 2153 frm += 2; \ 2154 } while (0) 2155 *frm++ = IEEE80211_ELEMID_CFPARMS; 2156 *frm++ = 6; 2157 *frm++ = 0; /* CFP count */ 2158 *frm++ = 2; /* CFP period */ 2159 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */ 2160 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */ 2161 return frm; 2162 #undef ADDSHORT 2163 } 2164 2165 static __inline uint8_t * 2166 add_appie(uint8_t *frm, const struct ieee80211_appie *ie) 2167 { 2168 memcpy(frm, ie->ie_data, ie->ie_len); 2169 return frm + ie->ie_len; 2170 } 2171 2172 static __inline uint8_t * 2173 add_ie(uint8_t *frm, const uint8_t *ie) 2174 { 2175 memcpy(frm, ie, 2 + ie[1]); 2176 return frm + 2 + ie[1]; 2177 } 2178 2179 #define WME_OUI_BYTES 0x00, 0x50, 0xf2 2180 /* 2181 * Add a WME information element to a frame. 2182 */ 2183 uint8_t * 2184 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme, 2185 struct ieee80211_node *ni) 2186 { 2187 static const uint8_t oui[4] = { WME_OUI_BYTES, WME_OUI_TYPE }; 2188 struct ieee80211vap *vap = ni->ni_vap; 2189 2190 *frm++ = IEEE80211_ELEMID_VENDOR; 2191 *frm++ = sizeof(struct ieee80211_wme_info) - 2; 2192 memcpy(frm, oui, sizeof(oui)); 2193 frm += sizeof(oui); 2194 *frm++ = WME_INFO_OUI_SUBTYPE; 2195 *frm++ = WME_VERSION; 2196 2197 /* QoS info field depends upon operating mode */ 2198 switch (vap->iv_opmode) { 2199 case IEEE80211_M_HOSTAP: 2200 *frm = wme->wme_bssChanParams.cap_info; 2201 if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD) 2202 *frm |= WME_CAPINFO_UAPSD_EN; 2203 frm++; 2204 break; 2205 case IEEE80211_M_STA: 2206 /* 2207 * NB: UAPSD drivers must set this up in their 2208 * VAP creation method. 2209 */ 2210 *frm++ = vap->iv_uapsdinfo; 2211 break; 2212 default: 2213 *frm++ = 0; 2214 break; 2215 } 2216 2217 return frm; 2218 } 2219 2220 /* 2221 * Add a WME parameters element to a frame. 2222 */ 2223 static uint8_t * 2224 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme, 2225 int uapsd_enable) 2226 { 2227 #define ADDSHORT(frm, v) do { \ 2228 le16enc(frm, v); \ 2229 frm += 2; \ 2230 } while (0) 2231 /* NB: this works 'cuz a param has an info at the front */ 2232 static const struct ieee80211_wme_info param = { 2233 .wme_id = IEEE80211_ELEMID_VENDOR, 2234 .wme_len = sizeof(struct ieee80211_wme_param) - 2, 2235 .wme_oui = { WME_OUI_BYTES }, 2236 .wme_type = WME_OUI_TYPE, 2237 .wme_subtype = WME_PARAM_OUI_SUBTYPE, 2238 .wme_version = WME_VERSION, 2239 }; 2240 int i; 2241 2242 memcpy(frm, ¶m, sizeof(param)); 2243 frm += __offsetof(struct ieee80211_wme_info, wme_info); 2244 *frm = wme->wme_bssChanParams.cap_info; /* AC info */ 2245 if (uapsd_enable) 2246 *frm |= WME_CAPINFO_UAPSD_EN; 2247 frm++; 2248 *frm++ = 0; /* reserved field */ 2249 /* XXX TODO - U-APSD bits - SP, flags below */ 2250 for (i = 0; i < WME_NUM_AC; i++) { 2251 const struct wmeParams *ac = 2252 &wme->wme_bssChanParams.cap_wmeParams[i]; 2253 *frm++ = _IEEE80211_SHIFTMASK(i, WME_PARAM_ACI) 2254 | _IEEE80211_SHIFTMASK(ac->wmep_acm, WME_PARAM_ACM) 2255 | _IEEE80211_SHIFTMASK(ac->wmep_aifsn, WME_PARAM_AIFSN) 2256 ; 2257 *frm++ = _IEEE80211_SHIFTMASK(ac->wmep_logcwmax, 2258 WME_PARAM_LOGCWMAX) 2259 | _IEEE80211_SHIFTMASK(ac->wmep_logcwmin, 2260 WME_PARAM_LOGCWMIN) 2261 ; 2262 ADDSHORT(frm, ac->wmep_txopLimit); 2263 } 2264 return frm; 2265 #undef ADDSHORT 2266 } 2267 #undef WME_OUI_BYTES 2268 2269 /* 2270 * Add an 11h Power Constraint element to a frame. 2271 */ 2272 static uint8_t * 2273 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap) 2274 { 2275 const struct ieee80211_channel *c = vap->iv_bss->ni_chan; 2276 /* XXX per-vap tx power limit? */ 2277 int8_t limit = vap->iv_ic->ic_txpowlimit / 2; 2278 2279 frm[0] = IEEE80211_ELEMID_PWRCNSTR; 2280 frm[1] = 1; 2281 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0; 2282 return frm + 3; 2283 } 2284 2285 /* 2286 * Add an 11h Power Capability element to a frame. 2287 */ 2288 static uint8_t * 2289 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c) 2290 { 2291 frm[0] = IEEE80211_ELEMID_PWRCAP; 2292 frm[1] = 2; 2293 frm[2] = c->ic_minpower; 2294 frm[3] = c->ic_maxpower; 2295 return frm + 4; 2296 } 2297 2298 /* 2299 * Add an 11h Supported Channels element to a frame. 2300 */ 2301 static uint8_t * 2302 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic) 2303 { 2304 static const int ielen = 26; 2305 2306 frm[0] = IEEE80211_ELEMID_SUPPCHAN; 2307 frm[1] = ielen; 2308 /* XXX not correct */ 2309 memcpy(frm+2, ic->ic_chan_avail, ielen); 2310 return frm + 2 + ielen; 2311 } 2312 2313 /* 2314 * Add an 11h Quiet time element to a frame. 2315 */ 2316 static uint8_t * 2317 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap, int update) 2318 { 2319 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm; 2320 2321 quiet->quiet_ie = IEEE80211_ELEMID_QUIET; 2322 quiet->len = 6; 2323 2324 /* 2325 * Only update every beacon interval - otherwise probe responses 2326 * would update the quiet count value. 2327 */ 2328 if (update) { 2329 if (vap->iv_quiet_count_value == 1) 2330 vap->iv_quiet_count_value = vap->iv_quiet_count; 2331 else if (vap->iv_quiet_count_value > 1) 2332 vap->iv_quiet_count_value--; 2333 } 2334 2335 if (vap->iv_quiet_count_value == 0) { 2336 /* value 0 is reserved as per 802.11h standerd */ 2337 vap->iv_quiet_count_value = 1; 2338 } 2339 2340 quiet->tbttcount = vap->iv_quiet_count_value; 2341 quiet->period = vap->iv_quiet_period; 2342 quiet->duration = htole16(vap->iv_quiet_duration); 2343 quiet->offset = htole16(vap->iv_quiet_offset); 2344 return frm + sizeof(*quiet); 2345 } 2346 2347 /* 2348 * Add an 11h Channel Switch Announcement element to a frame. 2349 * Note that we use the per-vap CSA count to adjust the global 2350 * counter so we can use this routine to form probe response 2351 * frames and get the current count. 2352 */ 2353 static uint8_t * 2354 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap) 2355 { 2356 struct ieee80211com *ic = vap->iv_ic; 2357 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm; 2358 2359 csa->csa_ie = IEEE80211_ELEMID_CSA; 2360 csa->csa_len = 3; 2361 csa->csa_mode = 1; /* XXX force quiet on channel */ 2362 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan); 2363 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count; 2364 return frm + sizeof(*csa); 2365 } 2366 2367 /* 2368 * Add an 11h country information element to a frame. 2369 */ 2370 static uint8_t * 2371 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic) 2372 { 2373 2374 if (ic->ic_countryie == NULL || 2375 ic->ic_countryie_chan != ic->ic_bsschan) { 2376 /* 2377 * Handle lazy construction of ie. This is done on 2378 * first use and after a channel change that requires 2379 * re-calculation. 2380 */ 2381 if (ic->ic_countryie != NULL) 2382 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE); 2383 ic->ic_countryie = ieee80211_alloc_countryie(ic); 2384 if (ic->ic_countryie == NULL) 2385 return frm; 2386 ic->ic_countryie_chan = ic->ic_bsschan; 2387 } 2388 return add_appie(frm, ic->ic_countryie); 2389 } 2390 2391 uint8_t * 2392 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap) 2393 { 2394 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL) 2395 return (add_ie(frm, vap->iv_wpa_ie)); 2396 else { 2397 /* XXX else complain? */ 2398 return (frm); 2399 } 2400 } 2401 2402 uint8_t * 2403 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap) 2404 { 2405 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL) 2406 return (add_ie(frm, vap->iv_rsn_ie)); 2407 else { 2408 /* XXX else complain? */ 2409 return (frm); 2410 } 2411 } 2412 2413 uint8_t * 2414 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni) 2415 { 2416 if (ni->ni_flags & IEEE80211_NODE_QOS) { 2417 *frm++ = IEEE80211_ELEMID_QOS; 2418 *frm++ = 1; 2419 *frm++ = 0; 2420 } 2421 2422 return (frm); 2423 } 2424 2425 /* 2426 * ieee80211_send_probereq(): send a probe request frame with the specified ssid 2427 * and any optional information element data; some helper functions as FW based 2428 * HW scans need some of that information passed too. 2429 */ 2430 static uint32_t 2431 ieee80211_probereq_ie_len(struct ieee80211vap *vap, struct ieee80211com *ic) 2432 { 2433 const struct ieee80211_rateset *rs; 2434 2435 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 2436 2437 /* 2438 * prreq frame format 2439 * [tlv] ssid 2440 * [tlv] supported rates 2441 * [tlv] extended supported rates (if needed) 2442 * [tlv] HT cap (optional) 2443 * [tlv] VHT cap (optional) 2444 * [tlv] WPA (optional) 2445 * [tlv] user-specified ie's 2446 */ 2447 return ( 2 + IEEE80211_NWID_LEN 2448 + 2 + IEEE80211_RATE_SIZE 2449 + ((rs->rs_nrates > IEEE80211_RATE_SIZE) ? 2450 2 + (rs->rs_nrates - IEEE80211_RATE_SIZE) : 0) 2451 + (((vap->iv_opmode == IEEE80211_M_IBSS) && 2452 (vap->iv_flags_ht & IEEE80211_FHT_HT)) ? 2453 sizeof(struct ieee80211_ie_htcap) : 0) 2454 #ifdef notyet 2455 + sizeof(struct ieee80211_ie_htinfo) /* XXX not needed? */ 2456 + 2 + sizeof(struct ieee80211_vht_cap) 2457 #endif 2458 + ((vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL) ? 2459 vap->iv_wpa_ie[1] : 0) 2460 + (vap->iv_appie_probereq != NULL ? 2461 vap->iv_appie_probereq->ie_len : 0) 2462 ); 2463 } 2464 2465 int 2466 ieee80211_probereq_ie(struct ieee80211vap *vap, struct ieee80211com *ic, 2467 uint8_t **frmp, uint32_t *frmlen, const uint8_t *ssid, size_t ssidlen, 2468 bool alloc) 2469 { 2470 const struct ieee80211_rateset *rs; 2471 uint8_t *frm; 2472 uint32_t len; 2473 2474 if (!alloc && (frmp == NULL || frmlen == NULL)) 2475 return (EINVAL); 2476 2477 len = ieee80211_probereq_ie_len(vap, ic); 2478 if (!alloc && len > *frmlen) 2479 return (ENOBUFS); 2480 2481 /* For HW scans we usually do not pass in the SSID as IE. */ 2482 if (ssidlen == -1) 2483 len -= (2 + IEEE80211_NWID_LEN); 2484 2485 if (alloc) { 2486 frm = IEEE80211_MALLOC(len, M_80211_VAP, 2487 IEEE80211_M_WAITOK | IEEE80211_M_ZERO); 2488 *frmp = frm; 2489 *frmlen = len; 2490 } else 2491 frm = *frmp; 2492 2493 if (ssidlen != -1) 2494 frm = ieee80211_add_ssid(frm, ssid, ssidlen); 2495 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 2496 frm = ieee80211_add_rates(frm, rs); 2497 frm = ieee80211_add_xrates(frm, rs); 2498 2499 /* 2500 * Note: we can't use bss; we don't have one yet. 2501 * 2502 * So, we should announce our capabilities 2503 * in this channel mode (2g/5g), not the 2504 * channel details itself. 2505 */ 2506 if ((vap->iv_opmode == IEEE80211_M_IBSS) && 2507 (vap->iv_flags_ht & IEEE80211_FHT_HT)) { 2508 struct ieee80211_channel *c; 2509 2510 /* 2511 * Get the HT channel that we should try upgrading to. 2512 * If we can do 40MHz then this'll upgrade it appropriately. 2513 */ 2514 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, 2515 vap->iv_flags_ht); 2516 frm = ieee80211_add_htcap_ch(frm, vap, c); 2517 } 2518 2519 /* 2520 * XXX TODO: need to figure out what/how to update the 2521 * VHT channel. 2522 */ 2523 #ifdef notyet 2524 if (vap->iv_vht_flags & IEEE80211_FVHT_VHT) { 2525 struct ieee80211_channel *c; 2526 2527 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan, 2528 vap->iv_flags_ht); 2529 c = ieee80211_vht_adjust_channel(ic, c, vap->iv_vht_flags); 2530 frm = ieee80211_add_vhtcap_ch(frm, vap, c); 2531 } 2532 #endif 2533 2534 frm = ieee80211_add_wpa(frm, vap); 2535 if (vap->iv_appie_probereq != NULL) 2536 frm = add_appie(frm, vap->iv_appie_probereq); 2537 2538 if (!alloc) { 2539 *frmp = frm; 2540 *frmlen = len; 2541 } 2542 2543 return (0); 2544 } 2545 2546 int 2547 ieee80211_send_probereq(struct ieee80211_node *ni, 2548 const uint8_t sa[IEEE80211_ADDR_LEN], 2549 const uint8_t da[IEEE80211_ADDR_LEN], 2550 const uint8_t bssid[IEEE80211_ADDR_LEN], 2551 const uint8_t *ssid, size_t ssidlen) 2552 { 2553 struct ieee80211vap *vap = ni->ni_vap; 2554 struct ieee80211com *ic = ni->ni_ic; 2555 struct ieee80211_node *bss; 2556 const struct ieee80211_txparam *tp; 2557 struct ieee80211_bpf_params params; 2558 struct mbuf *m; 2559 uint8_t *frm; 2560 uint32_t frmlen; 2561 int ret; 2562 2563 bss = ieee80211_ref_node(vap->iv_bss); 2564 2565 if (vap->iv_state == IEEE80211_S_CAC) { 2566 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni, 2567 "block %s frame in CAC state", "probe request"); 2568 vap->iv_stats.is_tx_badstate++; 2569 ieee80211_free_node(bss); 2570 return EIO; /* XXX */ 2571 } 2572 2573 /* 2574 * Hold a reference on the node so it doesn't go away until after 2575 * the xmit is complete all the way in the driver. On error we 2576 * will remove our reference. 2577 */ 2578 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2579 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 2580 __func__, __LINE__, 2581 ni, ether_sprintf(ni->ni_macaddr), 2582 ieee80211_node_refcnt(ni)+1); 2583 ieee80211_ref_node(ni); 2584 2585 /* See comments above for entire frame format. */ 2586 frmlen = ieee80211_probereq_ie_len(vap, ic); 2587 m = ieee80211_getmgtframe(&frm, 2588 ic->ic_headroom + sizeof(struct ieee80211_frame), frmlen); 2589 if (m == NULL) { 2590 vap->iv_stats.is_tx_nobuf++; 2591 ieee80211_free_node(ni); 2592 ieee80211_free_node(bss); 2593 return ENOMEM; 2594 } 2595 2596 ret = ieee80211_probereq_ie(vap, ic, &frm, &frmlen, ssid, ssidlen, 2597 false); 2598 KASSERT(ret == 0, 2599 ("%s: ieee80211_probereq_ie failed: %d\n", __func__, ret)); 2600 2601 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2602 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame), 2603 ("leading space %zd", M_LEADINGSPACE(m))); 2604 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT); 2605 if (m == NULL) { 2606 /* NB: cannot happen */ 2607 ieee80211_free_node(ni); 2608 ieee80211_free_node(bss); 2609 return ENOMEM; 2610 } 2611 2612 IEEE80211_TX_LOCK(ic); 2613 ieee80211_send_setup(ni, m, 2614 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ, 2615 IEEE80211_NONQOS_TID, sa, da, bssid); 2616 /* XXX power management? */ 2617 m->m_flags |= M_ENCAP; /* mark encapsulated */ 2618 2619 M_WME_SETAC(m, WME_AC_BE); 2620 2621 IEEE80211_NODE_STAT(ni, tx_probereq); 2622 IEEE80211_NODE_STAT(ni, tx_mgmt); 2623 2624 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 2625 "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n", 2626 ieee80211_chan2ieee(ic, ic->ic_curchan), 2627 ether_sprintf(bssid), 2628 sa, ":", 2629 da, ":", 2630 ssidlen, ssid); 2631 2632 memset(¶ms, 0, sizeof(params)); 2633 params.ibp_pri = M_WME_GETAC(m); 2634 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 2635 params.ibp_rate0 = tp->mgmtrate; 2636 if (IEEE80211_IS_MULTICAST(da)) { 2637 params.ibp_flags |= IEEE80211_BPF_NOACK; 2638 params.ibp_try0 = 1; 2639 } else 2640 params.ibp_try0 = tp->maxretry; 2641 params.ibp_power = ni->ni_txpower; 2642 ret = ieee80211_raw_output(vap, ni, m, ¶ms); 2643 IEEE80211_TX_UNLOCK(ic); 2644 ieee80211_free_node(bss); 2645 return (ret); 2646 } 2647 2648 /* 2649 * Calculate capability information for mgt frames. 2650 * 2651 * This fills out the 16 bit capability field in various management 2652 * frames for non-DMG STAs. DMG STAs are not supported. 2653 * 2654 * See 802.11-2020 9.4.1.4 (Capability Information Field) for the 2655 * field definitions. 2656 */ 2657 uint16_t 2658 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan) 2659 { 2660 uint16_t capinfo; 2661 2662 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode")); 2663 2664 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 2665 capinfo = IEEE80211_CAPINFO_ESS; 2666 else if (vap->iv_opmode == IEEE80211_M_IBSS) 2667 capinfo = IEEE80211_CAPINFO_IBSS; 2668 else 2669 capinfo = 0; 2670 if (vap->iv_flags & IEEE80211_F_PRIVACY) 2671 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2672 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) && 2673 IEEE80211_IS_CHAN_2GHZ(chan)) 2674 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2675 if (vap->iv_flags & IEEE80211_F_SHSLOT) 2676 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2677 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH)) 2678 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT; 2679 return capinfo; 2680 } 2681 2682 /* 2683 * Send a management frame. The node is for the destination (or ic_bss 2684 * when in station mode). Nodes other than ic_bss have their reference 2685 * count bumped to reflect our use for an indeterminant time. 2686 */ 2687 int 2688 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg) 2689 { 2690 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT) 2691 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0) 2692 struct ieee80211vap *vap = ni->ni_vap; 2693 struct ieee80211com *ic = ni->ni_ic; 2694 struct ieee80211_node *bss = vap->iv_bss; 2695 struct ieee80211_bpf_params params; 2696 struct mbuf *m; 2697 uint8_t *frm; 2698 uint16_t capinfo; 2699 int has_challenge, is_shared_key, ret, status; 2700 2701 KASSERT(ni != NULL, ("null node")); 2702 2703 /* 2704 * Hold a reference on the node so it doesn't go away until after 2705 * the xmit is complete all the way in the driver. On error we 2706 * will remove our reference. 2707 */ 2708 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2709 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 2710 __func__, __LINE__, 2711 ni, ether_sprintf(ni->ni_macaddr), 2712 ieee80211_node_refcnt(ni)+1); 2713 ieee80211_ref_node(ni); 2714 2715 memset(¶ms, 0, sizeof(params)); 2716 switch (type) { 2717 case IEEE80211_FC0_SUBTYPE_AUTH: 2718 status = arg >> 16; 2719 arg &= 0xffff; 2720 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE || 2721 arg == IEEE80211_AUTH_SHARED_RESPONSE) && 2722 ni->ni_challenge != NULL); 2723 2724 /* 2725 * Deduce whether we're doing open authentication or 2726 * shared key authentication. We do the latter if 2727 * we're in the middle of a shared key authentication 2728 * handshake or if we're initiating an authentication 2729 * request and configured to use shared key. 2730 */ 2731 is_shared_key = has_challenge || 2732 arg >= IEEE80211_AUTH_SHARED_RESPONSE || 2733 (arg == IEEE80211_AUTH_SHARED_REQUEST && 2734 bss->ni_authmode == IEEE80211_AUTH_SHARED); 2735 2736 m = ieee80211_getmgtframe(&frm, 2737 ic->ic_headroom + sizeof(struct ieee80211_frame), 2738 3 * sizeof(uint16_t) 2739 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ? 2740 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)); 2741 if (m == NULL) 2742 senderr(ENOMEM, is_tx_nobuf); 2743 2744 ((uint16_t *)frm)[0] = 2745 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED) 2746 : htole16(IEEE80211_AUTH_ALG_OPEN); 2747 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */ 2748 ((uint16_t *)frm)[2] = htole16(status);/* status */ 2749 2750 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) { 2751 ((uint16_t *)frm)[3] = 2752 htole16((IEEE80211_CHALLENGE_LEN << 8) | 2753 IEEE80211_ELEMID_CHALLENGE); 2754 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge, 2755 IEEE80211_CHALLENGE_LEN); 2756 m->m_pkthdr.len = m->m_len = 2757 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN; 2758 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) { 2759 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 2760 "request encrypt frame (%s)", __func__); 2761 /* mark frame for encryption */ 2762 params.ibp_flags |= IEEE80211_BPF_CRYPTO; 2763 } 2764 } else 2765 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t); 2766 2767 /* XXX not right for shared key */ 2768 if (status == IEEE80211_STATUS_SUCCESS) 2769 IEEE80211_NODE_STAT(ni, tx_auth); 2770 else 2771 IEEE80211_NODE_STAT(ni, tx_auth_fail); 2772 2773 if (vap->iv_opmode == IEEE80211_M_STA) 2774 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 2775 (void *) vap->iv_state); 2776 break; 2777 2778 case IEEE80211_FC0_SUBTYPE_DEAUTH: 2779 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 2780 "send station deauthenticate (reason: %d (%s))", arg, 2781 ieee80211_reason_to_string(arg)); 2782 m = ieee80211_getmgtframe(&frm, 2783 ic->ic_headroom + sizeof(struct ieee80211_frame), 2784 sizeof(uint16_t)); 2785 if (m == NULL) 2786 senderr(ENOMEM, is_tx_nobuf); 2787 *(uint16_t *)frm = htole16(arg); /* reason */ 2788 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 2789 2790 IEEE80211_NODE_STAT(ni, tx_deauth); 2791 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg); 2792 2793 ieee80211_node_unauthorize(ni); /* port closed */ 2794 break; 2795 2796 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 2797 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 2798 /* 2799 * asreq frame format 2800 * [2] capability information 2801 * [2] listen interval 2802 * [6*] current AP address (reassoc only) 2803 * [tlv] ssid 2804 * [tlv] supported rates 2805 * [tlv] extended supported rates 2806 * [4] power capability (optional) 2807 * [28] supported channels (optional) 2808 * [tlv] HT capabilities 2809 * [tlv] VHT capabilities 2810 * [tlv] WME (optional) 2811 * [tlv] Vendor OUI HT capabilities (optional) 2812 * [tlv] Atheros capabilities (if negotiated) 2813 * [tlv] AppIE's (optional) 2814 */ 2815 m = ieee80211_getmgtframe(&frm, 2816 ic->ic_headroom + sizeof(struct ieee80211_frame), 2817 sizeof(uint16_t) 2818 + sizeof(uint16_t) 2819 + IEEE80211_ADDR_LEN 2820 + 2 + IEEE80211_NWID_LEN 2821 + 2 + IEEE80211_RATE_SIZE 2822 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2823 + 4 2824 + 2 + 26 2825 + sizeof(struct ieee80211_wme_info) 2826 + sizeof(struct ieee80211_ie_htcap) 2827 + 2 + sizeof(struct ieee80211_vht_cap) 2828 + 4 + sizeof(struct ieee80211_ie_htcap) 2829 #ifdef IEEE80211_SUPPORT_SUPERG 2830 + sizeof(struct ieee80211_ath_ie) 2831 #endif 2832 + (vap->iv_appie_wpa != NULL ? 2833 vap->iv_appie_wpa->ie_len : 0) 2834 + (vap->iv_appie_assocreq != NULL ? 2835 vap->iv_appie_assocreq->ie_len : 0) 2836 ); 2837 if (m == NULL) 2838 senderr(ENOMEM, is_tx_nobuf); 2839 2840 KASSERT(vap->iv_opmode == IEEE80211_M_STA, 2841 ("wrong mode %u", vap->iv_opmode)); 2842 capinfo = IEEE80211_CAPINFO_ESS; 2843 if (vap->iv_flags & IEEE80211_F_PRIVACY) 2844 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2845 /* 2846 * NB: Some 11a AP's reject the request when 2847 * short preamble is set. 2848 */ 2849 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) && 2850 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 2851 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2852 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) && 2853 (ic->ic_caps & IEEE80211_C_SHSLOT)) 2854 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2855 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) && 2856 (vap->iv_flags & IEEE80211_F_DOTH)) 2857 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT; 2858 *(uint16_t *)frm = htole16(capinfo); 2859 frm += 2; 2860 2861 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!")); 2862 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval, 2863 bss->ni_intval)); 2864 frm += 2; 2865 2866 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 2867 IEEE80211_ADDR_COPY(frm, bss->ni_bssid); 2868 frm += IEEE80211_ADDR_LEN; 2869 } 2870 2871 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 2872 frm = ieee80211_add_rates(frm, &ni->ni_rates); 2873 frm = ieee80211_add_rsn(frm, vap); 2874 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 2875 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) { 2876 frm = ieee80211_add_powercapability(frm, 2877 ic->ic_curchan); 2878 frm = ieee80211_add_supportedchannels(frm, ic); 2879 } 2880 2881 /* 2882 * Check the channel - we may be using an 11n NIC with an 2883 * 11n capable station, but we're configured to be an 11b 2884 * channel. 2885 */ 2886 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) && 2887 IEEE80211_IS_CHAN_HT(ni->ni_chan) && 2888 ni->ni_ies.htcap_ie != NULL && 2889 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) { 2890 frm = ieee80211_add_htcap(frm, ni); 2891 } 2892 2893 if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) && 2894 IEEE80211_IS_CHAN_VHT(ni->ni_chan) && 2895 ni->ni_ies.vhtcap_ie != NULL && 2896 ni->ni_ies.vhtcap_ie[0] == IEEE80211_ELEMID_VHT_CAP) { 2897 frm = ieee80211_add_vhtcap(frm, ni); 2898 } 2899 2900 frm = ieee80211_add_wpa(frm, vap); 2901 if ((vap->iv_flags & IEEE80211_F_WME) && 2902 ni->ni_ies.wme_ie != NULL) 2903 frm = ieee80211_add_wme_info(frm, &ic->ic_wme, ni); 2904 2905 /* 2906 * Same deal - only send HT info if we're on an 11n 2907 * capable channel. 2908 */ 2909 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) && 2910 IEEE80211_IS_CHAN_HT(ni->ni_chan) && 2911 ni->ni_ies.htcap_ie != NULL && 2912 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) { 2913 frm = ieee80211_add_htcap_vendor(frm, ni); 2914 } 2915 #ifdef IEEE80211_SUPPORT_SUPERG 2916 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) { 2917 frm = ieee80211_add_ath(frm, 2918 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS), 2919 ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 2920 ni->ni_authmode != IEEE80211_AUTH_8021X) ? 2921 vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 2922 } 2923 #endif /* IEEE80211_SUPPORT_SUPERG */ 2924 if (vap->iv_appie_assocreq != NULL) 2925 frm = add_appie(frm, vap->iv_appie_assocreq); 2926 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2927 2928 ieee80211_add_callback(m, ieee80211_tx_mgt_cb, 2929 (void *) vap->iv_state); 2930 break; 2931 2932 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 2933 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 2934 /* 2935 * asresp frame format 2936 * [2] capability information 2937 * [2] status 2938 * [2] association ID 2939 * [tlv] supported rates 2940 * [tlv] extended supported rates 2941 * [tlv] HT capabilities (standard, if STA enabled) 2942 * [tlv] HT information (standard, if STA enabled) 2943 * [tlv] VHT capabilities (standard, if STA enabled) 2944 * [tlv] VHT information (standard, if STA enabled) 2945 * [tlv] WME (if configured and STA enabled) 2946 * [tlv] HT capabilities (vendor OUI, if STA enabled) 2947 * [tlv] HT information (vendor OUI, if STA enabled) 2948 * [tlv] Atheros capabilities (if STA enabled) 2949 * [tlv] AppIE's (optional) 2950 */ 2951 m = ieee80211_getmgtframe(&frm, 2952 ic->ic_headroom + sizeof(struct ieee80211_frame), 2953 sizeof(uint16_t) 2954 + sizeof(uint16_t) 2955 + sizeof(uint16_t) 2956 + 2 + IEEE80211_RATE_SIZE 2957 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2958 + sizeof(struct ieee80211_ie_htcap) + 4 2959 + sizeof(struct ieee80211_ie_htinfo) + 4 2960 + 2 + sizeof(struct ieee80211_vht_cap) 2961 + 2 + sizeof(struct ieee80211_vht_operation) 2962 + sizeof(struct ieee80211_wme_param) 2963 #ifdef IEEE80211_SUPPORT_SUPERG 2964 + sizeof(struct ieee80211_ath_ie) 2965 #endif 2966 + (vap->iv_appie_assocresp != NULL ? 2967 vap->iv_appie_assocresp->ie_len : 0) 2968 ); 2969 if (m == NULL) 2970 senderr(ENOMEM, is_tx_nobuf); 2971 2972 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan); 2973 *(uint16_t *)frm = htole16(capinfo); 2974 frm += 2; 2975 2976 *(uint16_t *)frm = htole16(arg); /* status */ 2977 frm += 2; 2978 2979 if (arg == IEEE80211_STATUS_SUCCESS) { 2980 *(uint16_t *)frm = htole16(ni->ni_associd); 2981 IEEE80211_NODE_STAT(ni, tx_assoc); 2982 } else 2983 IEEE80211_NODE_STAT(ni, tx_assoc_fail); 2984 frm += 2; 2985 2986 frm = ieee80211_add_rates(frm, &ni->ni_rates); 2987 frm = ieee80211_add_xrates(frm, &ni->ni_rates); 2988 /* NB: respond according to what we received */ 2989 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) { 2990 frm = ieee80211_add_htcap(frm, ni); 2991 frm = ieee80211_add_htinfo(frm, ni); 2992 } 2993 if ((vap->iv_flags & IEEE80211_F_WME) && 2994 ni->ni_ies.wme_ie != NULL) 2995 frm = ieee80211_add_wme_param(frm, &ic->ic_wme, 2996 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)); 2997 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) { 2998 frm = ieee80211_add_htcap_vendor(frm, ni); 2999 frm = ieee80211_add_htinfo_vendor(frm, ni); 3000 } 3001 if (ni->ni_flags & IEEE80211_NODE_VHT) { 3002 frm = ieee80211_add_vhtcap(frm, ni); 3003 frm = ieee80211_add_vhtinfo(frm, ni); 3004 } 3005 #ifdef IEEE80211_SUPPORT_SUPERG 3006 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) 3007 frm = ieee80211_add_ath(frm, 3008 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS), 3009 ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 3010 ni->ni_authmode != IEEE80211_AUTH_8021X) ? 3011 vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 3012 #endif /* IEEE80211_SUPPORT_SUPERG */ 3013 if (vap->iv_appie_assocresp != NULL) 3014 frm = add_appie(frm, vap->iv_appie_assocresp); 3015 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 3016 break; 3017 3018 case IEEE80211_FC0_SUBTYPE_DISASSOC: 3019 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 3020 "send station disassociate (reason: %d (%s))", arg, 3021 ieee80211_reason_to_string(arg)); 3022 m = ieee80211_getmgtframe(&frm, 3023 ic->ic_headroom + sizeof(struct ieee80211_frame), 3024 sizeof(uint16_t)); 3025 if (m == NULL) 3026 senderr(ENOMEM, is_tx_nobuf); 3027 *(uint16_t *)frm = htole16(arg); /* reason */ 3028 m->m_pkthdr.len = m->m_len = sizeof(uint16_t); 3029 3030 IEEE80211_NODE_STAT(ni, tx_disassoc); 3031 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg); 3032 break; 3033 3034 default: 3035 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni, 3036 "invalid mgmt frame type %u", type); 3037 senderr(EINVAL, is_tx_unknownmgt); 3038 /* NOTREACHED */ 3039 } 3040 3041 /* NB: force non-ProbeResp frames to the highest queue */ 3042 params.ibp_pri = WME_AC_VO; 3043 params.ibp_rate0 = bss->ni_txparms->mgmtrate; 3044 /* NB: we know all frames are unicast */ 3045 params.ibp_try0 = bss->ni_txparms->maxretry; 3046 params.ibp_power = bss->ni_txpower; 3047 return ieee80211_mgmt_output(ni, m, type, ¶ms); 3048 bad: 3049 ieee80211_free_node(ni); 3050 return ret; 3051 #undef senderr 3052 #undef HTFLAGS 3053 } 3054 3055 /* 3056 * Return an mbuf with a probe response frame in it. 3057 * Space is left to prepend and 802.11 header at the 3058 * front but it's left to the caller to fill in. 3059 */ 3060 struct mbuf * 3061 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy) 3062 { 3063 struct ieee80211vap *vap = bss->ni_vap; 3064 struct ieee80211com *ic = bss->ni_ic; 3065 const struct ieee80211_rateset *rs; 3066 struct mbuf *m; 3067 uint16_t capinfo; 3068 uint8_t *frm; 3069 3070 /* 3071 * probe response frame format 3072 * [8] time stamp 3073 * [2] beacon interval 3074 * [2] cabability information 3075 * [tlv] ssid 3076 * [tlv] supported rates 3077 * [tlv] parameter set (FH/DS) 3078 * [tlv] parameter set (IBSS) 3079 * [tlv] country (optional) 3080 * [3] power control (optional) 3081 * [5] channel switch announcement (CSA) (optional) 3082 * [tlv] extended rate phy (ERP) 3083 * [tlv] extended supported rates 3084 * [tlv] RSN (optional) 3085 * [tlv] HT capabilities 3086 * [tlv] HT information 3087 * [tlv] VHT capabilities 3088 * [tlv] VHT information 3089 * [tlv] WPA (optional) 3090 * [tlv] WME (optional) 3091 * [tlv] Vendor OUI HT capabilities (optional) 3092 * [tlv] Vendor OUI HT information (optional) 3093 * [tlv] Atheros capabilities 3094 * [tlv] AppIE's (optional) 3095 * [tlv] Mesh ID (MBSS) 3096 * [tlv] Mesh Conf (MBSS) 3097 */ 3098 m = ieee80211_getmgtframe(&frm, 3099 ic->ic_headroom + sizeof(struct ieee80211_frame), 3100 8 3101 + sizeof(uint16_t) 3102 + sizeof(uint16_t) 3103 + 2 + IEEE80211_NWID_LEN 3104 + 2 + IEEE80211_RATE_SIZE 3105 + 7 /* max(7,3) */ 3106 + IEEE80211_COUNTRY_MAX_SIZE 3107 + 3 3108 + sizeof(struct ieee80211_csa_ie) 3109 + sizeof(struct ieee80211_quiet_ie) 3110 + 3 3111 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 3112 + sizeof(struct ieee80211_ie_wpa) 3113 + sizeof(struct ieee80211_ie_htcap) 3114 + sizeof(struct ieee80211_ie_htinfo) 3115 + sizeof(struct ieee80211_ie_wpa) 3116 + sizeof(struct ieee80211_wme_param) 3117 + 4 + sizeof(struct ieee80211_ie_htcap) 3118 + 4 + sizeof(struct ieee80211_ie_htinfo) 3119 + 2 + sizeof(struct ieee80211_vht_cap) 3120 + 2 + sizeof(struct ieee80211_vht_operation) 3121 #ifdef IEEE80211_SUPPORT_SUPERG 3122 + sizeof(struct ieee80211_ath_ie) 3123 #endif 3124 #ifdef IEEE80211_SUPPORT_MESH 3125 + 2 + IEEE80211_MESHID_LEN 3126 + sizeof(struct ieee80211_meshconf_ie) 3127 #endif 3128 + (vap->iv_appie_proberesp != NULL ? 3129 vap->iv_appie_proberesp->ie_len : 0) 3130 ); 3131 if (m == NULL) { 3132 vap->iv_stats.is_tx_nobuf++; 3133 return NULL; 3134 } 3135 3136 memset(frm, 0, 8); /* timestamp should be filled later */ 3137 frm += 8; 3138 *(uint16_t *)frm = htole16(bss->ni_intval); 3139 frm += 2; 3140 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan); 3141 *(uint16_t *)frm = htole16(capinfo); 3142 frm += 2; 3143 3144 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen); 3145 rs = ieee80211_get_suprates(ic, bss->ni_chan); 3146 frm = ieee80211_add_rates(frm, rs); 3147 3148 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) { 3149 *frm++ = IEEE80211_ELEMID_FHPARMS; 3150 *frm++ = 5; 3151 *frm++ = bss->ni_fhdwell & 0x00ff; 3152 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff; 3153 *frm++ = IEEE80211_FH_CHANSET( 3154 ieee80211_chan2ieee(ic, bss->ni_chan)); 3155 *frm++ = IEEE80211_FH_CHANPAT( 3156 ieee80211_chan2ieee(ic, bss->ni_chan)); 3157 *frm++ = bss->ni_fhindex; 3158 } else { 3159 *frm++ = IEEE80211_ELEMID_DSPARMS; 3160 *frm++ = 1; 3161 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan); 3162 } 3163 3164 if (vap->iv_opmode == IEEE80211_M_IBSS) { 3165 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 3166 *frm++ = 2; 3167 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 3168 } 3169 if ((vap->iv_flags & IEEE80211_F_DOTH) || 3170 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) 3171 frm = ieee80211_add_countryie(frm, ic); 3172 if (vap->iv_flags & IEEE80211_F_DOTH) { 3173 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan)) 3174 frm = ieee80211_add_powerconstraint(frm, vap); 3175 if (ic->ic_flags & IEEE80211_F_CSAPENDING) 3176 frm = ieee80211_add_csa(frm, vap); 3177 } 3178 if (vap->iv_flags & IEEE80211_F_DOTH) { 3179 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) && 3180 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) { 3181 if (vap->iv_quiet) 3182 frm = ieee80211_add_quiet(frm, vap, 0); 3183 } 3184 } 3185 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan)) 3186 frm = ieee80211_add_erp(frm, vap); 3187 frm = ieee80211_add_xrates(frm, rs); 3188 frm = ieee80211_add_rsn(frm, vap); 3189 /* 3190 * NB: legacy 11b clients do not get certain ie's. 3191 * The caller identifies such clients by passing 3192 * a token in legacy to us. Could expand this to be 3193 * any legacy client for stuff like HT ie's. 3194 */ 3195 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) && 3196 legacy != IEEE80211_SEND_LEGACY_11B) { 3197 frm = ieee80211_add_htcap(frm, bss); 3198 frm = ieee80211_add_htinfo(frm, bss); 3199 } 3200 if (IEEE80211_IS_CHAN_VHT(bss->ni_chan) && 3201 legacy != IEEE80211_SEND_LEGACY_11B) { 3202 frm = ieee80211_add_vhtcap(frm, bss); 3203 frm = ieee80211_add_vhtinfo(frm, bss); 3204 } 3205 frm = ieee80211_add_wpa(frm, vap); 3206 if (vap->iv_flags & IEEE80211_F_WME) 3207 frm = ieee80211_add_wme_param(frm, &ic->ic_wme, 3208 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)); 3209 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) && 3210 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) && 3211 legacy != IEEE80211_SEND_LEGACY_11B) { 3212 frm = ieee80211_add_htcap_vendor(frm, bss); 3213 frm = ieee80211_add_htinfo_vendor(frm, bss); 3214 } 3215 #ifdef IEEE80211_SUPPORT_SUPERG 3216 if ((vap->iv_flags & IEEE80211_F_ATHEROS) && 3217 legacy != IEEE80211_SEND_LEGACY_11B) 3218 frm = ieee80211_add_athcaps(frm, bss); 3219 #endif 3220 if (vap->iv_appie_proberesp != NULL) 3221 frm = add_appie(frm, vap->iv_appie_proberesp); 3222 #ifdef IEEE80211_SUPPORT_MESH 3223 if (vap->iv_opmode == IEEE80211_M_MBSS) { 3224 frm = ieee80211_add_meshid(frm, vap); 3225 frm = ieee80211_add_meshconf(frm, vap); 3226 } 3227 #endif 3228 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 3229 3230 return m; 3231 } 3232 3233 /* 3234 * Send a probe response frame to the specified mac address. 3235 * This does not go through the normal mgt frame api so we 3236 * can specify the destination address and re-use the bss node 3237 * for the sta reference. 3238 */ 3239 int 3240 ieee80211_send_proberesp(struct ieee80211vap *vap, 3241 const uint8_t da[IEEE80211_ADDR_LEN], int legacy) 3242 { 3243 struct ieee80211_node *bss = vap->iv_bss; 3244 struct ieee80211com *ic = vap->iv_ic; 3245 struct mbuf *m; 3246 int ret; 3247 3248 if (vap->iv_state == IEEE80211_S_CAC) { 3249 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss, 3250 "block %s frame in CAC state", "probe response"); 3251 vap->iv_stats.is_tx_badstate++; 3252 return EIO; /* XXX */ 3253 } 3254 3255 /* 3256 * Hold a reference on the node so it doesn't go away until after 3257 * the xmit is complete all the way in the driver. On error we 3258 * will remove our reference. 3259 */ 3260 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 3261 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 3262 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr), 3263 ieee80211_node_refcnt(bss)+1); 3264 ieee80211_ref_node(bss); 3265 3266 m = ieee80211_alloc_proberesp(bss, legacy); 3267 if (m == NULL) { 3268 ieee80211_free_node(bss); 3269 return ENOMEM; 3270 } 3271 3272 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT); 3273 KASSERT(m != NULL, ("no room for header")); 3274 3275 IEEE80211_TX_LOCK(ic); 3276 ieee80211_send_setup(bss, m, 3277 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP, 3278 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid); 3279 /* XXX power management? */ 3280 m->m_flags |= M_ENCAP; /* mark encapsulated */ 3281 3282 M_WME_SETAC(m, WME_AC_BE); 3283 3284 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, 3285 "send probe resp on channel %u to %s%s\n", 3286 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da), 3287 legacy ? " <legacy>" : ""); 3288 IEEE80211_NODE_STAT(bss, tx_mgmt); 3289 3290 ret = ieee80211_raw_output(vap, bss, m, NULL); 3291 IEEE80211_TX_UNLOCK(ic); 3292 return (ret); 3293 } 3294 3295 /* 3296 * Allocate and build a RTS (Request To Send) control frame. 3297 */ 3298 struct mbuf * 3299 ieee80211_alloc_rts(struct ieee80211com *ic, 3300 const uint8_t ra[IEEE80211_ADDR_LEN], 3301 const uint8_t ta[IEEE80211_ADDR_LEN], 3302 uint16_t dur) 3303 { 3304 struct ieee80211_frame_rts *rts; 3305 struct mbuf *m; 3306 3307 /* XXX honor ic_headroom */ 3308 m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA); 3309 if (m != NULL) { 3310 rts = mtod(m, struct ieee80211_frame_rts *); 3311 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | 3312 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS; 3313 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 3314 *(u_int16_t *)rts->i_dur = htole16(dur); 3315 IEEE80211_ADDR_COPY(rts->i_ra, ra); 3316 IEEE80211_ADDR_COPY(rts->i_ta, ta); 3317 3318 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts); 3319 } 3320 return m; 3321 } 3322 3323 /* 3324 * Allocate and build a CTS (Clear To Send) control frame. 3325 */ 3326 struct mbuf * 3327 ieee80211_alloc_cts(struct ieee80211com *ic, 3328 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur) 3329 { 3330 struct ieee80211_frame_cts *cts; 3331 struct mbuf *m; 3332 3333 /* XXX honor ic_headroom */ 3334 m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA); 3335 if (m != NULL) { 3336 cts = mtod(m, struct ieee80211_frame_cts *); 3337 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 | 3338 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS; 3339 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 3340 *(u_int16_t *)cts->i_dur = htole16(dur); 3341 IEEE80211_ADDR_COPY(cts->i_ra, ra); 3342 3343 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts); 3344 } 3345 return m; 3346 } 3347 3348 /* 3349 * Wrapper for CTS/RTS frame allocation. 3350 */ 3351 struct mbuf * 3352 ieee80211_alloc_prot(struct ieee80211_node *ni, const struct mbuf *m, 3353 uint8_t rate, int prot) 3354 { 3355 struct ieee80211com *ic = ni->ni_ic; 3356 struct ieee80211vap *vap = ni->ni_vap; 3357 const struct ieee80211_frame *wh; 3358 struct mbuf *mprot; 3359 uint16_t dur; 3360 int pktlen, isshort; 3361 3362 KASSERT(prot == IEEE80211_PROT_RTSCTS || 3363 prot == IEEE80211_PROT_CTSONLY, 3364 ("wrong protection type %d", prot)); 3365 3366 wh = mtod(m, const struct ieee80211_frame *); 3367 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 3368 isshort = (vap->iv_flags & IEEE80211_F_SHPREAMBLE) != 0; 3369 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 3370 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 3371 3372 if (prot == IEEE80211_PROT_RTSCTS) { 3373 /* NB: CTS is the same size as an ACK */ 3374 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 3375 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 3376 } else 3377 mprot = ieee80211_alloc_cts(ic, vap->iv_myaddr, dur); 3378 3379 return (mprot); 3380 } 3381 3382 static void 3383 ieee80211_tx_mgt_timeout(void *arg) 3384 { 3385 struct ieee80211vap *vap = arg; 3386 3387 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 3388 "vap %p mode %s state %s flags %#x & %#x\n", vap, 3389 ieee80211_opmode_name[vap->iv_opmode], 3390 ieee80211_state_name[vap->iv_state], 3391 vap->iv_ic->ic_flags, IEEE80211_F_SCAN); 3392 3393 IEEE80211_LOCK(vap->iv_ic); 3394 if (vap->iv_state != IEEE80211_S_INIT && 3395 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) { 3396 /* 3397 * NB: it's safe to specify a timeout as the reason here; 3398 * it'll only be used in the right state. 3399 */ 3400 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN, 3401 IEEE80211_SCAN_FAIL_TIMEOUT); 3402 } 3403 IEEE80211_UNLOCK(vap->iv_ic); 3404 } 3405 3406 /* 3407 * This is the callback set on net80211-sourced transmitted 3408 * authentication request frames. 3409 * 3410 * This does a couple of things: 3411 * 3412 * + If the frame transmitted was a success, it schedules a future 3413 * event which will transition the interface to scan. 3414 * If a state transition _then_ occurs before that event occurs, 3415 * said state transition will cancel this callout. 3416 * 3417 * + If the frame transmit was a failure, it immediately schedules 3418 * the transition back to scan. 3419 */ 3420 static void 3421 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status) 3422 { 3423 struct ieee80211vap *vap = ni->ni_vap; 3424 enum ieee80211_state ostate = (enum ieee80211_state)(uintptr_t)arg; 3425 3426 /* 3427 * Frame transmit completed; arrange timer callback. If 3428 * transmit was successfully we wait for response. Otherwise 3429 * we arrange an immediate callback instead of doing the 3430 * callback directly since we don't know what state the driver 3431 * is in (e.g. what locks it is holding). This work should 3432 * not be too time-critical and not happen too often so the 3433 * added overhead is acceptable. 3434 * 3435 * XXX what happens if !acked but response shows up before callback? 3436 */ 3437 if (vap->iv_state == ostate) { 3438 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 3439 "ni %p mode %s state %s arg %p status %d\n", ni, 3440 ieee80211_opmode_name[vap->iv_opmode], 3441 ieee80211_state_name[vap->iv_state], arg, status); 3442 3443 callout_reset(&vap->iv_mgtsend, 3444 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0, 3445 ieee80211_tx_mgt_timeout, vap); 3446 } 3447 } 3448 3449 static void 3450 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm, 3451 struct ieee80211_node *ni) 3452 { 3453 struct ieee80211vap *vap = ni->ni_vap; 3454 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 3455 struct ieee80211com *ic = ni->ni_ic; 3456 struct ieee80211_rateset *rs = &ni->ni_rates; 3457 uint16_t capinfo; 3458 3459 /* 3460 * beacon frame format 3461 * 3462 * TODO: update to 802.11-2012; a lot of stuff has changed; 3463 * vendor extensions should be at the end, etc. 3464 * 3465 * [8] time stamp 3466 * [2] beacon interval 3467 * [2] cabability information 3468 * [tlv] ssid 3469 * [tlv] supported rates 3470 * [3] parameter set (DS) 3471 * [8] CF parameter set (optional) 3472 * [tlv] parameter set (IBSS/TIM) 3473 * [tlv] country (optional) 3474 * [3] power control (optional) 3475 * [5] channel switch announcement (CSA) (optional) 3476 * XXX TODO: Quiet 3477 * XXX TODO: IBSS DFS 3478 * XXX TODO: TPC report 3479 * [tlv] extended rate phy (ERP) 3480 * [tlv] extended supported rates 3481 * [tlv] RSN parameters 3482 * XXX TODO: BSSLOAD 3483 * (XXX EDCA parameter set, QoS capability?) 3484 * XXX TODO: AP channel report 3485 * 3486 * [tlv] HT capabilities 3487 * [tlv] HT information 3488 * XXX TODO: 20/40 BSS coexistence 3489 * Mesh: 3490 * XXX TODO: Meshid 3491 * XXX TODO: mesh config 3492 * XXX TODO: mesh awake window 3493 * XXX TODO: beacon timing (mesh, etc) 3494 * XXX TODO: MCCAOP Advertisement Overview 3495 * XXX TODO: MCCAOP Advertisement 3496 * XXX TODO: Mesh channel switch parameters 3497 * VHT: 3498 * XXX TODO: VHT capabilities 3499 * XXX TODO: VHT operation 3500 * XXX TODO: VHT transmit power envelope 3501 * XXX TODO: channel switch wrapper element 3502 * XXX TODO: extended BSS load element 3503 * 3504 * XXX Vendor-specific OIDs (e.g. Atheros) 3505 * [tlv] WPA parameters 3506 * [tlv] WME parameters 3507 * [tlv] Vendor OUI HT capabilities (optional) 3508 * [tlv] Vendor OUI HT information (optional) 3509 * [tlv] Atheros capabilities (optional) 3510 * [tlv] TDMA parameters (optional) 3511 * [tlv] Mesh ID (MBSS) 3512 * [tlv] Mesh Conf (MBSS) 3513 * [tlv] application data (optional) 3514 */ 3515 3516 memset(bo, 0, sizeof(*bo)); 3517 3518 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */ 3519 frm += 8; 3520 *(uint16_t *)frm = htole16(ni->ni_intval); 3521 frm += 2; 3522 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan); 3523 bo->bo_caps = (uint16_t *)frm; 3524 *(uint16_t *)frm = htole16(capinfo); 3525 frm += 2; 3526 *frm++ = IEEE80211_ELEMID_SSID; 3527 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) { 3528 *frm++ = ni->ni_esslen; 3529 memcpy(frm, ni->ni_essid, ni->ni_esslen); 3530 frm += ni->ni_esslen; 3531 } else 3532 *frm++ = 0; 3533 frm = ieee80211_add_rates(frm, rs); 3534 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) { 3535 *frm++ = IEEE80211_ELEMID_DSPARMS; 3536 *frm++ = 1; 3537 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 3538 } 3539 if (ic->ic_flags & IEEE80211_F_PCF) { 3540 bo->bo_cfp = frm; 3541 frm = ieee80211_add_cfparms(frm, ic); 3542 } 3543 bo->bo_tim = frm; 3544 if (vap->iv_opmode == IEEE80211_M_IBSS) { 3545 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 3546 *frm++ = 2; 3547 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 3548 bo->bo_tim_len = 0; 3549 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP || 3550 vap->iv_opmode == IEEE80211_M_MBSS) { 3551 /* TIM IE is the same for Mesh and Hostap */ 3552 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm; 3553 3554 tie->tim_ie = IEEE80211_ELEMID_TIM; 3555 tie->tim_len = 4; /* length */ 3556 tie->tim_count = 0; /* DTIM count */ 3557 tie->tim_period = vap->iv_dtim_period; /* DTIM period */ 3558 tie->tim_bitctl = 0; /* bitmap control */ 3559 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */ 3560 frm += sizeof(struct ieee80211_tim_ie); 3561 bo->bo_tim_len = 1; 3562 } 3563 bo->bo_tim_trailer = frm; 3564 if ((vap->iv_flags & IEEE80211_F_DOTH) || 3565 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) 3566 frm = ieee80211_add_countryie(frm, ic); 3567 if (vap->iv_flags & IEEE80211_F_DOTH) { 3568 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)) 3569 frm = ieee80211_add_powerconstraint(frm, vap); 3570 bo->bo_csa = frm; 3571 if (ic->ic_flags & IEEE80211_F_CSAPENDING) 3572 frm = ieee80211_add_csa(frm, vap); 3573 } else 3574 bo->bo_csa = frm; 3575 3576 bo->bo_quiet = NULL; 3577 if (vap->iv_flags & IEEE80211_F_DOTH) { 3578 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) && 3579 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) && 3580 (vap->iv_quiet == 1)) { 3581 /* 3582 * We only insert the quiet IE offset if 3583 * the quiet IE is enabled. Otherwise don't 3584 * put it here or we'll just overwrite 3585 * some other beacon contents. 3586 */ 3587 if (vap->iv_quiet) { 3588 bo->bo_quiet = frm; 3589 frm = ieee80211_add_quiet(frm,vap, 0); 3590 } 3591 } 3592 } 3593 3594 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) { 3595 bo->bo_erp = frm; 3596 frm = ieee80211_add_erp(frm, vap); 3597 } 3598 frm = ieee80211_add_xrates(frm, rs); 3599 frm = ieee80211_add_rsn(frm, vap); 3600 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 3601 frm = ieee80211_add_htcap(frm, ni); 3602 bo->bo_htinfo = frm; 3603 frm = ieee80211_add_htinfo(frm, ni); 3604 } 3605 3606 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) { 3607 frm = ieee80211_add_vhtcap(frm, ni); 3608 bo->bo_vhtinfo = frm; 3609 frm = ieee80211_add_vhtinfo(frm, ni); 3610 /* Transmit power envelope */ 3611 /* Channel switch wrapper element */ 3612 /* Extended bss load element */ 3613 } 3614 3615 frm = ieee80211_add_wpa(frm, vap); 3616 if (vap->iv_flags & IEEE80211_F_WME) { 3617 bo->bo_wme = frm; 3618 frm = ieee80211_add_wme_param(frm, &ic->ic_wme, 3619 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)); 3620 } 3621 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && 3622 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) { 3623 frm = ieee80211_add_htcap_vendor(frm, ni); 3624 frm = ieee80211_add_htinfo_vendor(frm, ni); 3625 } 3626 3627 #ifdef IEEE80211_SUPPORT_SUPERG 3628 if (vap->iv_flags & IEEE80211_F_ATHEROS) { 3629 bo->bo_ath = frm; 3630 frm = ieee80211_add_athcaps(frm, ni); 3631 } 3632 #endif 3633 #ifdef IEEE80211_SUPPORT_TDMA 3634 if (vap->iv_caps & IEEE80211_C_TDMA) { 3635 bo->bo_tdma = frm; 3636 frm = ieee80211_add_tdma(frm, vap); 3637 } 3638 #endif 3639 if (vap->iv_appie_beacon != NULL) { 3640 bo->bo_appie = frm; 3641 bo->bo_appie_len = vap->iv_appie_beacon->ie_len; 3642 frm = add_appie(frm, vap->iv_appie_beacon); 3643 } 3644 3645 /* XXX TODO: move meshid/meshconf up to before vendor extensions? */ 3646 #ifdef IEEE80211_SUPPORT_MESH 3647 if (vap->iv_opmode == IEEE80211_M_MBSS) { 3648 frm = ieee80211_add_meshid(frm, vap); 3649 bo->bo_meshconf = frm; 3650 frm = ieee80211_add_meshconf(frm, vap); 3651 } 3652 #endif 3653 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer; 3654 bo->bo_csa_trailer_len = frm - bo->bo_csa; 3655 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 3656 } 3657 3658 /* 3659 * Allocate a beacon frame and fillin the appropriate bits. 3660 */ 3661 struct mbuf * 3662 ieee80211_beacon_alloc(struct ieee80211_node *ni) 3663 { 3664 struct ieee80211vap *vap = ni->ni_vap; 3665 struct ieee80211com *ic = ni->ni_ic; 3666 struct ieee80211_frame *wh; 3667 struct mbuf *m; 3668 int pktlen; 3669 uint8_t *frm; 3670 3671 /* 3672 * Update the "We're putting the quiet IE in the beacon" state. 3673 */ 3674 if (vap->iv_quiet == 1) 3675 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE; 3676 else if (vap->iv_quiet == 0) 3677 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE; 3678 3679 /* 3680 * beacon frame format 3681 * 3682 * Note: This needs updating for 802.11-2012. 3683 * 3684 * [8] time stamp 3685 * [2] beacon interval 3686 * [2] cabability information 3687 * [tlv] ssid 3688 * [tlv] supported rates 3689 * [3] parameter set (DS) 3690 * [8] CF parameter set (optional) 3691 * [tlv] parameter set (IBSS/TIM) 3692 * [tlv] country (optional) 3693 * [3] power control (optional) 3694 * [5] channel switch announcement (CSA) (optional) 3695 * [tlv] extended rate phy (ERP) 3696 * [tlv] extended supported rates 3697 * [tlv] RSN parameters 3698 * [tlv] HT capabilities 3699 * [tlv] HT information 3700 * [tlv] VHT capabilities 3701 * [tlv] VHT operation 3702 * [tlv] Vendor OUI HT capabilities (optional) 3703 * [tlv] Vendor OUI HT information (optional) 3704 * XXX Vendor-specific OIDs (e.g. Atheros) 3705 * [tlv] WPA parameters 3706 * [tlv] WME parameters 3707 * [tlv] TDMA parameters (optional) 3708 * [tlv] Mesh ID (MBSS) 3709 * [tlv] Mesh Conf (MBSS) 3710 * [tlv] application data (optional) 3711 * NB: we allocate the max space required for the TIM bitmap. 3712 * XXX how big is this? 3713 */ 3714 pktlen = 8 /* time stamp */ 3715 + sizeof(uint16_t) /* beacon interval */ 3716 + sizeof(uint16_t) /* capabilities */ 3717 + 2 + ni->ni_esslen /* ssid */ 3718 + 2 + IEEE80211_RATE_SIZE /* supported rates */ 3719 + 2 + 1 /* DS parameters */ 3720 + 2 + 6 /* CF parameters */ 3721 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */ 3722 + IEEE80211_COUNTRY_MAX_SIZE /* country */ 3723 + 2 + 1 /* power control */ 3724 + sizeof(struct ieee80211_csa_ie) /* CSA */ 3725 + sizeof(struct ieee80211_quiet_ie) /* Quiet */ 3726 + 2 + 1 /* ERP */ 3727 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 3728 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */ 3729 2*sizeof(struct ieee80211_ie_wpa) : 0) 3730 /* XXX conditional? */ 3731 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */ 3732 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */ 3733 + 2 + sizeof(struct ieee80211_vht_cap)/* VHT caps */ 3734 + 2 + sizeof(struct ieee80211_vht_operation)/* VHT info */ 3735 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */ 3736 sizeof(struct ieee80211_wme_param) : 0) 3737 #ifdef IEEE80211_SUPPORT_SUPERG 3738 + sizeof(struct ieee80211_ath_ie) /* ATH */ 3739 #endif 3740 #ifdef IEEE80211_SUPPORT_TDMA 3741 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */ 3742 sizeof(struct ieee80211_tdma_param) : 0) 3743 #endif 3744 #ifdef IEEE80211_SUPPORT_MESH 3745 + 2 + ni->ni_meshidlen 3746 + sizeof(struct ieee80211_meshconf_ie) 3747 #endif 3748 + IEEE80211_MAX_APPIE 3749 ; 3750 m = ieee80211_getmgtframe(&frm, 3751 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen); 3752 if (m == NULL) { 3753 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 3754 "%s: cannot get buf; size %u\n", __func__, pktlen); 3755 vap->iv_stats.is_tx_nobuf++; 3756 return NULL; 3757 } 3758 ieee80211_beacon_construct(m, frm, ni); 3759 3760 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT); 3761 KASSERT(m != NULL, ("no space for 802.11 header?")); 3762 wh = mtod(m, struct ieee80211_frame *); 3763 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 3764 IEEE80211_FC0_SUBTYPE_BEACON; 3765 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 3766 *(uint16_t *)wh->i_dur = 0; 3767 IEEE80211_ADDR_COPY(wh->i_addr1, 3768 ieee80211_vap_get_broadcast_address(vap)); 3769 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 3770 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 3771 *(uint16_t *)wh->i_seq = 0; 3772 3773 return m; 3774 } 3775 3776 /* 3777 * Update the dynamic parts of a beacon frame based on the current state. 3778 */ 3779 int 3780 ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast) 3781 { 3782 struct ieee80211vap *vap = ni->ni_vap; 3783 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 3784 struct ieee80211com *ic = ni->ni_ic; 3785 int len_changed = 0; 3786 uint16_t capinfo; 3787 3788 IEEE80211_LOCK(ic); 3789 /* 3790 * Handle 11h channel change when we've reached the count. 3791 * We must recalculate the beacon frame contents to account 3792 * for the new channel. Note we do this only for the first 3793 * vap that reaches this point; subsequent vaps just update 3794 * their beacon state to reflect the recalculated channel. 3795 */ 3796 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) && 3797 vap->iv_csa_count == ic->ic_csa_count) { 3798 vap->iv_csa_count = 0; 3799 /* 3800 * Effect channel change before reconstructing the beacon 3801 * frame contents as many places reference ni_chan. 3802 */ 3803 if (ic->ic_csa_newchan != NULL) 3804 ieee80211_csa_completeswitch(ic); 3805 /* 3806 * NB: ieee80211_beacon_construct clears all pending 3807 * updates in bo_flags so we don't need to explicitly 3808 * clear IEEE80211_BEACON_CSA. 3809 */ 3810 ieee80211_beacon_construct(m, 3811 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni); 3812 3813 /* XXX do WME aggressive mode processing? */ 3814 IEEE80211_UNLOCK(ic); 3815 return 1; /* just assume length changed */ 3816 } 3817 3818 /* 3819 * Handle the quiet time element being added and removed. 3820 * Again, for now we just cheat and reconstruct the whole 3821 * beacon - that way the gap is provided as appropriate. 3822 * 3823 * So, track whether we have already added the IE versus 3824 * whether we want to be adding the IE. 3825 */ 3826 if ((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) && 3827 (vap->iv_quiet == 0)) { 3828 /* 3829 * Quiet time beacon IE enabled, but it's disabled; 3830 * recalc 3831 */ 3832 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE; 3833 ieee80211_beacon_construct(m, 3834 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni); 3835 /* XXX do WME aggressive mode processing? */ 3836 IEEE80211_UNLOCK(ic); 3837 return 1; /* just assume length changed */ 3838 } 3839 3840 if (((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) == 0) && 3841 (vap->iv_quiet == 1)) { 3842 /* 3843 * Quiet time beacon IE disabled, but it's now enabled; 3844 * recalc 3845 */ 3846 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE; 3847 ieee80211_beacon_construct(m, 3848 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni); 3849 /* XXX do WME aggressive mode processing? */ 3850 IEEE80211_UNLOCK(ic); 3851 return 1; /* just assume length changed */ 3852 } 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 3863 /* TODO: IEEE80211_CONF_SEQNO_OFFLOAD() */ 3864 ieee80211_output_beacon_seqno_assign(ni, m); 3865 3866 /* XXX faster to recalculate entirely or just changes? */ 3867 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan); 3868 *bo->bo_caps = htole16(capinfo); 3869 3870 if (vap->iv_flags & IEEE80211_F_WME) { 3871 struct ieee80211_wme_state *wme = &ic->ic_wme; 3872 3873 /* 3874 * Check for aggressive mode change. When there is 3875 * significant high priority traffic in the BSS 3876 * throttle back BE traffic by using conservative 3877 * parameters. Otherwise BE uses aggressive params 3878 * to optimize performance of legacy/non-QoS traffic. 3879 */ 3880 if (wme->wme_flags & WME_F_AGGRMODE) { 3881 if (wme->wme_hipri_traffic > 3882 wme->wme_hipri_switch_thresh) { 3883 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 3884 "%s: traffic %u, disable aggressive mode\n", 3885 __func__, wme->wme_hipri_traffic); 3886 wme->wme_flags &= ~WME_F_AGGRMODE; 3887 ieee80211_wme_updateparams_locked(vap); 3888 wme->wme_hipri_traffic = 3889 wme->wme_hipri_switch_hysteresis; 3890 } else 3891 wme->wme_hipri_traffic = 0; 3892 } else { 3893 if (wme->wme_hipri_traffic <= 3894 wme->wme_hipri_switch_thresh) { 3895 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME, 3896 "%s: traffic %u, enable aggressive mode\n", 3897 __func__, wme->wme_hipri_traffic); 3898 wme->wme_flags |= WME_F_AGGRMODE; 3899 ieee80211_wme_updateparams_locked(vap); 3900 wme->wme_hipri_traffic = 0; 3901 } else 3902 wme->wme_hipri_traffic = 3903 wme->wme_hipri_switch_hysteresis; 3904 } 3905 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) { 3906 (void) ieee80211_add_wme_param(bo->bo_wme, wme, 3907 vap->iv_flags_ext & IEEE80211_FEXT_UAPSD); 3908 clrbit(bo->bo_flags, IEEE80211_BEACON_WME); 3909 } 3910 } 3911 3912 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) { 3913 ieee80211_ht_update_beacon(vap, bo); 3914 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO); 3915 } 3916 #ifdef IEEE80211_SUPPORT_TDMA 3917 if (vap->iv_caps & IEEE80211_C_TDMA) { 3918 /* 3919 * NB: the beacon is potentially updated every TBTT. 3920 */ 3921 ieee80211_tdma_update_beacon(vap, bo); 3922 } 3923 #endif 3924 #ifdef IEEE80211_SUPPORT_MESH 3925 if (vap->iv_opmode == IEEE80211_M_MBSS) 3926 ieee80211_mesh_update_beacon(vap, bo); 3927 #endif 3928 3929 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 3930 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/ 3931 struct ieee80211_tim_ie *tie = 3932 (struct ieee80211_tim_ie *) bo->bo_tim; 3933 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) { 3934 u_int timlen, timoff, i; 3935 /* 3936 * ATIM/DTIM needs updating. If it fits in the 3937 * current space allocated then just copy in the 3938 * new bits. Otherwise we need to move any trailing 3939 * data to make room. Note that we know there is 3940 * contiguous space because ieee80211_beacon_allocate 3941 * insures there is space in the mbuf to write a 3942 * maximal-size virtual bitmap (based on iv_max_aid). 3943 */ 3944 /* 3945 * Calculate the bitmap size and offset, copy any 3946 * trailer out of the way, and then copy in the 3947 * new bitmap and update the information element. 3948 * Note that the tim bitmap must contain at least 3949 * one byte and any offset must be even. 3950 */ 3951 if (vap->iv_ps_pending != 0) { 3952 timoff = 128; /* impossibly large */ 3953 for (i = 0; i < vap->iv_tim_len; i++) 3954 if (vap->iv_tim_bitmap[i]) { 3955 timoff = i &~ 1; 3956 break; 3957 } 3958 KASSERT(timoff != 128, ("tim bitmap empty!")); 3959 for (i = vap->iv_tim_len-1; i >= timoff; i--) 3960 if (vap->iv_tim_bitmap[i]) 3961 break; 3962 timlen = 1 + (i - timoff); 3963 } else { 3964 timoff = 0; 3965 timlen = 1; 3966 } 3967 3968 /* 3969 * TODO: validate this! 3970 */ 3971 if (timlen != bo->bo_tim_len) { 3972 /* copy up/down trailer */ 3973 int adjust = tie->tim_bitmap+timlen 3974 - bo->bo_tim_trailer; 3975 ovbcopy(bo->bo_tim_trailer, 3976 bo->bo_tim_trailer+adjust, 3977 bo->bo_tim_trailer_len); 3978 bo->bo_tim_trailer += adjust; 3979 bo->bo_erp += adjust; 3980 bo->bo_htinfo += adjust; 3981 bo->bo_vhtinfo += adjust; 3982 #ifdef IEEE80211_SUPPORT_SUPERG 3983 bo->bo_ath += adjust; 3984 #endif 3985 #ifdef IEEE80211_SUPPORT_TDMA 3986 bo->bo_tdma += adjust; 3987 #endif 3988 #ifdef IEEE80211_SUPPORT_MESH 3989 bo->bo_meshconf += adjust; 3990 #endif 3991 bo->bo_appie += adjust; 3992 bo->bo_wme += adjust; 3993 bo->bo_csa += adjust; 3994 bo->bo_quiet += adjust; 3995 bo->bo_tim_len = timlen; 3996 3997 /* update information element */ 3998 tie->tim_len = 3 + timlen; 3999 tie->tim_bitctl = timoff; 4000 len_changed = 1; 4001 } 4002 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff, 4003 bo->bo_tim_len); 4004 4005 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM); 4006 4007 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER, 4008 "%s: TIM updated, pending %u, off %u, len %u\n", 4009 __func__, vap->iv_ps_pending, timoff, timlen); 4010 } 4011 /* count down DTIM period */ 4012 if (tie->tim_count == 0) 4013 tie->tim_count = tie->tim_period - 1; 4014 else 4015 tie->tim_count--; 4016 /* update state for buffered multicast frames on DTIM */ 4017 if (mcast && tie->tim_count == 0) 4018 tie->tim_bitctl |= 1; 4019 else 4020 tie->tim_bitctl &= ~1; 4021 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) { 4022 struct ieee80211_csa_ie *csa = 4023 (struct ieee80211_csa_ie *) bo->bo_csa; 4024 4025 /* 4026 * Insert or update CSA ie. If we're just starting 4027 * to count down to the channel switch then we need 4028 * to insert the CSA ie. Otherwise we just need to 4029 * drop the count. The actual change happens above 4030 * when the vap's count reaches the target count. 4031 */ 4032 if (vap->iv_csa_count == 0) { 4033 memmove(&csa[1], csa, bo->bo_csa_trailer_len); 4034 bo->bo_erp += sizeof(*csa); 4035 bo->bo_htinfo += sizeof(*csa); 4036 bo->bo_vhtinfo += sizeof(*csa); 4037 bo->bo_wme += sizeof(*csa); 4038 #ifdef IEEE80211_SUPPORT_SUPERG 4039 bo->bo_ath += sizeof(*csa); 4040 #endif 4041 #ifdef IEEE80211_SUPPORT_TDMA 4042 bo->bo_tdma += sizeof(*csa); 4043 #endif 4044 #ifdef IEEE80211_SUPPORT_MESH 4045 bo->bo_meshconf += sizeof(*csa); 4046 #endif 4047 bo->bo_appie += sizeof(*csa); 4048 bo->bo_csa_trailer_len += sizeof(*csa); 4049 bo->bo_quiet += sizeof(*csa); 4050 bo->bo_tim_trailer_len += sizeof(*csa); 4051 m->m_len += sizeof(*csa); 4052 m->m_pkthdr.len += sizeof(*csa); 4053 4054 ieee80211_add_csa(bo->bo_csa, vap); 4055 } else 4056 csa->csa_count--; 4057 vap->iv_csa_count++; 4058 /* NB: don't clear IEEE80211_BEACON_CSA */ 4059 } 4060 4061 /* 4062 * Only add the quiet time IE if we've enabled it 4063 * as appropriate. 4064 */ 4065 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) && 4066 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) { 4067 if (vap->iv_quiet && 4068 (vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE)) { 4069 ieee80211_add_quiet(bo->bo_quiet, vap, 1); 4070 } 4071 } 4072 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) { 4073 /* 4074 * ERP element needs updating. 4075 */ 4076 (void) ieee80211_add_erp(bo->bo_erp, vap); 4077 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP); 4078 } 4079 #ifdef IEEE80211_SUPPORT_SUPERG 4080 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) { 4081 ieee80211_add_athcaps(bo->bo_ath, ni); 4082 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH); 4083 } 4084 #endif 4085 } 4086 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) { 4087 const struct ieee80211_appie *aie = vap->iv_appie_beacon; 4088 int aielen; 4089 uint8_t *frm; 4090 4091 aielen = 0; 4092 if (aie != NULL) 4093 aielen += aie->ie_len; 4094 if (aielen != bo->bo_appie_len) { 4095 /* copy up/down trailer */ 4096 int adjust = aielen - bo->bo_appie_len; 4097 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust, 4098 bo->bo_tim_trailer_len); 4099 bo->bo_tim_trailer += adjust; 4100 bo->bo_appie += adjust; 4101 bo->bo_appie_len = aielen; 4102 4103 len_changed = 1; 4104 } 4105 frm = bo->bo_appie; 4106 if (aie != NULL) 4107 frm = add_appie(frm, aie); 4108 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE); 4109 } 4110 IEEE80211_UNLOCK(ic); 4111 4112 return len_changed; 4113 } 4114 4115 /* 4116 * Do Ethernet-LLC encapsulation for each payload in a fast frame 4117 * tunnel encapsulation. The frame is assumed to have an Ethernet 4118 * header at the front that must be stripped before prepending the 4119 * LLC followed by the Ethernet header passed in (with an Ethernet 4120 * type that specifies the payload size). 4121 */ 4122 struct mbuf * 4123 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m, 4124 const struct ether_header *eh) 4125 { 4126 struct llc *llc; 4127 uint16_t payload; 4128 4129 /* XXX optimize by combining m_adj+M_PREPEND */ 4130 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 4131 llc = mtod(m, struct llc *); 4132 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 4133 llc->llc_control = LLC_UI; 4134 llc->llc_snap.org_code[0] = 0; 4135 llc->llc_snap.org_code[1] = 0; 4136 llc->llc_snap.org_code[2] = 0; 4137 llc->llc_snap.ether_type = eh->ether_type; 4138 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */ 4139 4140 M_PREPEND(m, sizeof(struct ether_header), IEEE80211_M_NOWAIT); 4141 if (m == NULL) { /* XXX cannot happen */ 4142 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 4143 "%s: no space for ether_header\n", __func__); 4144 vap->iv_stats.is_tx_nobuf++; 4145 return NULL; 4146 } 4147 ETHER_HEADER_COPY(mtod(m, void *), eh); 4148 mtod(m, struct ether_header *)->ether_type = htons(payload); 4149 return m; 4150 } 4151 4152 /* 4153 * Complete an mbuf transmission. 4154 * 4155 * For now, this simply processes a completed frame after the 4156 * driver has completed it's transmission and/or retransmission. 4157 * It assumes the frame is an 802.11 encapsulated frame. 4158 * 4159 * Later on it will grow to become the exit path for a given frame 4160 * from the driver and, depending upon how it's been encapsulated 4161 * and already transmitted, it may end up doing A-MPDU retransmission, 4162 * power save requeuing, etc. 4163 * 4164 * In order for the above to work, the driver entry point to this 4165 * must not hold any driver locks. Thus, the driver needs to delay 4166 * any actual mbuf completion until it can release said locks. 4167 * 4168 * This frees the mbuf and if the mbuf has a node reference, 4169 * the node reference will be freed. 4170 */ 4171 void 4172 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status) 4173 { 4174 4175 if (ni != NULL) { 4176 struct ifnet *ifp = ni->ni_vap->iv_ifp; 4177 4178 if (status == 0) { 4179 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len); 4180 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 4181 if (m->m_flags & M_MCAST) 4182 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 4183 } else 4184 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 4185 if (m->m_flags & M_TXCB) { 4186 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 4187 "ni %p vap %p mode %s state %s m %p status %d\n", ni, ni->ni_vap, 4188 ieee80211_opmode_name[ni->ni_vap->iv_opmode], 4189 ieee80211_state_name[ni->ni_vap->iv_state], m, status); 4190 ieee80211_process_callback(ni, m, status); 4191 } 4192 ieee80211_free_node(ni); 4193 } 4194 m_freem(m); 4195 } 4196 4197 /** 4198 * @brief Assign a sequence number to the given frame. 4199 * 4200 * Check the frame type and TID and assign a suitable sequence number 4201 * from the correct sequence number space. 4202 * 4203 * This implements the components of 802.11-2020 10.3.2.14.2 4204 * (Transmitter Requirements) that net80211 currently supports. 4205 * 4206 * It assumes the mbuf has been encapsulated, and has the TID assigned 4207 * if it is a QoS frame. 4208 * 4209 * Note this also clears any existing fragment ID in the header, so it 4210 * must be called first before assigning fragment IDs. 4211 * 4212 * @param ni ieee80211_node this frame will be transmitted to 4213 * @param arg_tid A temporary check, existing callers may set 4214 * this to a TID variable they were using, and this routine 4215 * will verify it against what's in the frame and complain if 4216 * they don't match. For new callers, use -1. 4217 * @param m mbuf to populate the sequence number into 4218 */ 4219 void 4220 ieee80211_output_seqno_assign(struct ieee80211_node *ni, int arg_tid, 4221 struct mbuf *m) 4222 { 4223 struct ieee80211_frame *wh; 4224 ieee80211_seq seqno; 4225 uint8_t tid, type, subtype; 4226 4227 wh = mtod(m, struct ieee80211_frame *); 4228 tid = ieee80211_gettid(wh); 4229 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 4230 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 4231 4232 /* 4233 * Find places where the passed in TID doesn't match gettid() 4234 * and log. I'll have to then go and chase those down. 4235 * 4236 * If the caller knows its already setup the TID in the frame 4237 * correctly then it can pass in -1 and this check will be 4238 * skipped. 4239 */ 4240 if (arg_tid != -1 && tid != arg_tid) 4241 ic_printf(ni->ni_vap->iv_ic, 4242 "%s: called; TID mismatch; tid=%u, arg_tid=%d\n", 4243 __func__, tid, arg_tid); 4244 4245 4246 /* 802.11-2020 10.3.2.14.2 (Transmitter Requirements) sections */ 4247 4248 /* SNS7 - unicast PV1 management frame */ 4249 4250 /* SNS6 - unicast PV1 data frame */ 4251 4252 /* SNS5 - QoS NULL frames */ 4253 if (IEEE80211_QOS_HAS_SEQ(wh) && IEEE80211_IS_QOS_NULL(wh)) 4254 seqno = ieee80211_tx_seqno_fetch_incr(ni, IEEE80211_NONQOS_TID); 4255 4256 /* SNS4 - QMF STA transmitting a QMF */ 4257 4258 /* SNS3 - QoS STA; Time Priority Management frame */ 4259 4260 /* SNS2 - unicast QoS STA, data frame, excluding SNS5 */ 4261 else if (IEEE80211_QOS_HAS_SEQ(wh) && 4262 !IEEE80211_IS_MULTICAST(wh->i_addr1)) 4263 seqno = ieee80211_tx_seqno_fetch_incr(ni, tid); 4264 4265 /* SNS1 - Baseline (everything else) */ 4266 else if (IEEE80211_HAS_SEQ(type, subtype)) 4267 seqno = ieee80211_tx_seqno_fetch_incr(ni, IEEE80211_NONQOS_TID); 4268 else 4269 seqno = 0; 4270 4271 /* 4272 * Assign the sequence number, clearing out any existing 4273 * sequence and fragment numbers. 4274 */ 4275 *(uint16_t *)&wh->i_seq[0] = 4276 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 4277 M_SEQNO_SET(m, seqno); 4278 } 4279 4280 /** 4281 * @brief Assign a sequence number to the given beacon frame. 4282 * 4283 * TODO: update to 802.11-2020 10.3.2.14.2 (Transmitter Requirements) 4284 * 4285 * @param ni ieee80211_node this frame will be transmitted to 4286 * @param m mbuf to populate the sequence number into 4287 */ 4288 void 4289 ieee80211_output_beacon_seqno_assign(struct ieee80211_node *ni, struct mbuf *m) 4290 { 4291 struct ieee80211_frame *wh; 4292 ieee80211_seq seqno; 4293 4294 wh = mtod(m, struct ieee80211_frame *); 4295 4296 seqno = ieee80211_tx_seqno_fetch_incr(ni, IEEE80211_NONQOS_TID); 4297 *(uint16_t *)&wh->i_seq[0] = 4298 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT); 4299 M_SEQNO_SET(m, seqno); 4300 } 4301