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