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