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