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