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