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