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