1 /* $OpenBSD: ieee80211_output.c,v 1.119 2017/10/21 20:15:17 patrick Exp $ */ 2 /* $NetBSD: ieee80211_output.c,v 1.13 2004/05/31 11:02:55 dyoung Exp $ */ 3 4 /*- 5 * Copyright (c) 2001 Atsushi Onoe 6 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 7 * Copyright (c) 2007-2009 Damien Bergamini 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include "bpfilter.h" 34 #include "vlan.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/mbuf.h> 39 #include <sys/kernel.h> 40 #include <sys/socket.h> 41 #include <sys/sockio.h> 42 #include <sys/endian.h> 43 #include <sys/errno.h> 44 #include <sys/sysctl.h> 45 46 #include <net/if.h> 47 #include <net/if_dl.h> 48 #include <net/if_media.h> 49 #include <net/if_llc.h> 50 #include <net/bpf.h> 51 52 #include <netinet/in.h> 53 #include <netinet/if_ether.h> 54 #include <netinet/ip.h> 55 #ifdef INET6 56 #include <netinet/ip6.h> 57 #endif 58 59 #if NVLAN > 0 60 #include <net/if_vlan_var.h> 61 #endif 62 63 #include <net80211/ieee80211_var.h> 64 #include <net80211/ieee80211_priv.h> 65 66 int ieee80211_mgmt_output(struct ifnet *, struct ieee80211_node *, 67 struct mbuf *, int); 68 u_int8_t *ieee80211_add_rsn_body(u_int8_t *, struct ieee80211com *, 69 const struct ieee80211_node *, int); 70 struct mbuf *ieee80211_getmgmt(int, int, u_int); 71 struct mbuf *ieee80211_get_probe_req(struct ieee80211com *, 72 struct ieee80211_node *); 73 #ifndef IEEE80211_STA_ONLY 74 struct mbuf *ieee80211_get_probe_resp(struct ieee80211com *, 75 struct ieee80211_node *); 76 #endif 77 struct mbuf *ieee80211_get_auth(struct ieee80211com *, 78 struct ieee80211_node *, u_int16_t, u_int16_t); 79 struct mbuf *ieee80211_get_deauth(struct ieee80211com *, 80 struct ieee80211_node *, u_int16_t); 81 struct mbuf *ieee80211_get_assoc_req(struct ieee80211com *, 82 struct ieee80211_node *, int); 83 #ifndef IEEE80211_STA_ONLY 84 struct mbuf *ieee80211_get_assoc_resp(struct ieee80211com *, 85 struct ieee80211_node *, u_int16_t); 86 #endif 87 struct mbuf *ieee80211_get_disassoc(struct ieee80211com *, 88 struct ieee80211_node *, u_int16_t); 89 struct mbuf *ieee80211_get_addba_req(struct ieee80211com *, 90 struct ieee80211_node *, u_int8_t); 91 struct mbuf *ieee80211_get_addba_resp(struct ieee80211com *, 92 struct ieee80211_node *, u_int8_t, u_int8_t, u_int16_t); 93 struct mbuf *ieee80211_get_delba(struct ieee80211com *, 94 struct ieee80211_node *, u_int8_t, u_int8_t, u_int16_t); 95 uint8_t *ieee80211_add_wme_info(uint8_t *, struct ieee80211com *); 96 #ifndef IEEE80211_STA_ONLY 97 uint8_t *ieee80211_add_wme_param(uint8_t *, struct ieee80211com *); 98 #endif 99 struct mbuf *ieee80211_get_sa_query(struct ieee80211com *, 100 struct ieee80211_node *, u_int8_t); 101 struct mbuf *ieee80211_get_action(struct ieee80211com *, 102 struct ieee80211_node *, u_int8_t, u_int8_t, int); 103 104 /* 105 * IEEE 802.11 output routine. Normally this will directly call the 106 * Ethernet output routine because 802.11 encapsulation is called 107 * later by the driver. This function can be used to send raw frames 108 * if the mbuf has been tagged with a 802.11 data link type. 109 */ 110 int 111 ieee80211_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 112 struct rtentry *rt) 113 { 114 struct ieee80211_frame *wh; 115 struct m_tag *mtag; 116 int error = 0; 117 118 /* Interface has to be up and running */ 119 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != 120 (IFF_UP | IFF_RUNNING)) { 121 error = ENETDOWN; 122 goto bad; 123 } 124 125 /* Try to get the DLT from a mbuf tag */ 126 if ((mtag = m_tag_find(m, PACKET_TAG_DLT, NULL)) != NULL) { 127 struct ieee80211com *ic = (void *)ifp; 128 u_int dlt = *(u_int *)(mtag + 1); 129 130 /* Fallback to ethernet for non-802.11 linktypes */ 131 if (!(dlt == DLT_IEEE802_11 || dlt == DLT_IEEE802_11_RADIO)) 132 goto fallback; 133 134 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) 135 return (EINVAL); 136 wh = mtod(m, struct ieee80211_frame *); 137 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 138 IEEE80211_FC0_VERSION_0) 139 return (EINVAL); 140 if (!(ic->ic_caps & IEEE80211_C_RAWCTL) && 141 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 142 IEEE80211_FC0_TYPE_CTL) 143 return (EINVAL); 144 145 return (if_enqueue(ifp, m)); 146 } 147 148 fallback: 149 return (ether_output(ifp, m, dst, rt)); 150 151 bad: 152 m_freem(m); 153 return (error); 154 } 155 156 /* 157 * Send a management frame to the specified node. The node pointer 158 * must have a reference as the pointer will be passed to the driver 159 * and potentially held for a long time. If the frame is successfully 160 * dispatched to the driver, then it is responsible for freeing the 161 * reference (and potentially free'ing up any associated storage). 162 */ 163 int 164 ieee80211_mgmt_output(struct ifnet *ifp, struct ieee80211_node *ni, 165 struct mbuf *m, int type) 166 { 167 struct ieee80211com *ic = (void *)ifp; 168 struct ieee80211_frame *wh; 169 170 if (ni == NULL) 171 panic("null node"); 172 ni->ni_inact = 0; 173 174 /* 175 * We want to pass the node down to the driver's start 176 * routine. We could stick this in an m_tag and tack that 177 * on to the mbuf. However that's rather expensive to do 178 * for every frame so instead we stuff it in a special pkthdr 179 * field. 180 */ 181 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 182 if (m == NULL) 183 return ENOMEM; 184 m->m_pkthdr.ph_cookie = ni; 185 186 wh = mtod(m, struct ieee80211_frame *); 187 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type; 188 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 189 *(u_int16_t *)&wh->i_dur[0] = 0; 190 *(u_int16_t *)&wh->i_seq[0] = 191 htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT); 192 ni->ni_txseq++; 193 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr); 194 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 195 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 196 197 /* check if protection is required for this mgmt frame */ 198 if ((ic->ic_caps & IEEE80211_C_MFP) && 199 (type == IEEE80211_FC0_SUBTYPE_DISASSOC || 200 type == IEEE80211_FC0_SUBTYPE_DEAUTH || 201 type == IEEE80211_FC0_SUBTYPE_ACTION)) { 202 /* 203 * Hack: we should not set the Protected bit in outgoing 204 * group management frames, however it is used as an 205 * indication to the drivers that they must encrypt the 206 * frame. Drivers should clear this bit from group 207 * management frames (software crypto code will do it). 208 * XXX could use an mbuf flag.. 209 */ 210 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 211 (ni->ni_flags & IEEE80211_NODE_TXMGMTPROT)) 212 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED; 213 } 214 215 if (ifp->if_flags & IFF_DEBUG) { 216 /* avoid to print too many frames */ 217 if ( 218 #ifndef IEEE80211_STA_ONLY 219 ic->ic_opmode == IEEE80211_M_IBSS || 220 #endif 221 #ifdef IEEE80211_DEBUG 222 ieee80211_debug > 1 || 223 #endif 224 (type & IEEE80211_FC0_SUBTYPE_MASK) != 225 IEEE80211_FC0_SUBTYPE_PROBE_RESP) 226 printf("%s: sending %s to %s on channel %u mode %s\n", 227 ifp->if_xname, 228 ieee80211_mgt_subtype_name[ 229 (type & IEEE80211_FC0_SUBTYPE_MASK) 230 >> IEEE80211_FC0_SUBTYPE_SHIFT], 231 ether_sprintf(ni->ni_macaddr), 232 ieee80211_chan2ieee(ic, ni->ni_chan), 233 ieee80211_phymode_name[ic->ic_curmode]); 234 } 235 236 #ifndef IEEE80211_STA_ONLY 237 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 238 ieee80211_pwrsave(ic, m, ni) != 0) 239 return 0; 240 #endif 241 mq_enqueue(&ic->ic_mgtq, m); 242 ifp->if_timer = 1; 243 if_start(ifp); 244 return 0; 245 } 246 247 /*- 248 * EDCA tables are computed using the following formulas: 249 * 250 * 1) EDCATable (non-AP QSTA) 251 * 252 * AC CWmin CWmax AIFSN TXOP limit(ms) 253 * ------------------------------------------------------------- 254 * AC_BK aCWmin aCWmax 7 0 255 * AC_BE aCWmin aCWmax 3 0 256 * AC_VI (aCWmin+1)/2-1 aCWmin 2 agn=3.008 b=6.016 others=0 257 * AC_VO (aCWmin+1)/4-1 (aCWmin+1)/2-1 2 agn=1.504 b=3.264 others=0 258 * 259 * 2) QAPEDCATable (QAP) 260 * 261 * AC CWmin CWmax AIFSN TXOP limit(ms) 262 * ------------------------------------------------------------- 263 * AC_BK aCWmin aCWmax 7 0 264 * AC_BE aCWmin 4*(aCWmin+1)-1 3 0 265 * AC_VI (aCWmin+1)/2-1 aCWmin 1 agn=3.008 b=6.016 others=0 266 * AC_VO (aCWmin+1)/4-1 (aCWmin+1)/2-1 1 agn=1.504 b=3.264 others=0 267 * 268 * and the following aCWmin/aCWmax values: 269 * 270 * PHY aCWmin aCWmax 271 * --------------------------- 272 * 11A 15 1023 273 * 11B 31 1023 274 * 11G 15* 1023 (*) aCWmin(1) 275 * 11N 15 1023 276 */ 277 const struct ieee80211_edca_ac_params 278 ieee80211_edca_table[IEEE80211_MODE_MAX][EDCA_NUM_AC] = { 279 [IEEE80211_MODE_11B] = { 280 [EDCA_AC_BK] = { 5, 10, 7, 0 }, 281 [EDCA_AC_BE] = { 5, 10, 3, 0 }, 282 [EDCA_AC_VI] = { 4, 5, 2, 188 }, 283 [EDCA_AC_VO] = { 3, 4, 2, 102 } 284 }, 285 [IEEE80211_MODE_11A] = { 286 [EDCA_AC_BK] = { 4, 10, 7, 0 }, 287 [EDCA_AC_BE] = { 4, 10, 3, 0 }, 288 [EDCA_AC_VI] = { 3, 4, 2, 94 }, 289 [EDCA_AC_VO] = { 2, 3, 2, 47 } 290 }, 291 [IEEE80211_MODE_11G] = { 292 [EDCA_AC_BK] = { 4, 10, 7, 0 }, 293 [EDCA_AC_BE] = { 4, 10, 3, 0 }, 294 [EDCA_AC_VI] = { 3, 4, 2, 94 }, 295 [EDCA_AC_VO] = { 2, 3, 2, 47 } 296 }, 297 [IEEE80211_MODE_11N] = { 298 [EDCA_AC_BK] = { 4, 10, 7, 0 }, 299 [EDCA_AC_BE] = { 4, 10, 3, 0 }, 300 [EDCA_AC_VI] = { 3, 4, 2, 94 }, 301 [EDCA_AC_VO] = { 2, 3, 2, 47 } 302 }, 303 }; 304 305 #ifndef IEEE80211_STA_ONLY 306 const struct ieee80211_edca_ac_params 307 ieee80211_qap_edca_table[IEEE80211_MODE_MAX][EDCA_NUM_AC] = { 308 [IEEE80211_MODE_11B] = { 309 [EDCA_AC_BK] = { 5, 10, 7, 0 }, 310 [EDCA_AC_BE] = { 5, 7, 3, 0 }, 311 [EDCA_AC_VI] = { 4, 5, 1, 188 }, 312 [EDCA_AC_VO] = { 3, 4, 1, 102 } 313 }, 314 [IEEE80211_MODE_11A] = { 315 [EDCA_AC_BK] = { 4, 10, 7, 0 }, 316 [EDCA_AC_BE] = { 4, 6, 3, 0 }, 317 [EDCA_AC_VI] = { 3, 4, 1, 94 }, 318 [EDCA_AC_VO] = { 2, 3, 1, 47 } 319 }, 320 [IEEE80211_MODE_11G] = { 321 [EDCA_AC_BK] = { 4, 10, 7, 0 }, 322 [EDCA_AC_BE] = { 4, 6, 3, 0 }, 323 [EDCA_AC_VI] = { 3, 4, 1, 94 }, 324 [EDCA_AC_VO] = { 2, 3, 1, 47 } 325 }, 326 [IEEE80211_MODE_11N] = { 327 [EDCA_AC_BK] = { 4, 10, 7, 0 }, 328 [EDCA_AC_BE] = { 4, 6, 3, 0 }, 329 [EDCA_AC_VI] = { 3, 4, 1, 94 }, 330 [EDCA_AC_VO] = { 2, 3, 1, 47 } 331 }, 332 }; 333 #endif /* IEEE80211_STA_ONLY */ 334 335 /* 336 * Return the EDCA Access Category to be used for transmitting a frame with 337 * user-priority `up'. 338 */ 339 enum ieee80211_edca_ac 340 ieee80211_up_to_ac(struct ieee80211com *ic, int up) 341 { 342 /* see Table 9-1 */ 343 static const enum ieee80211_edca_ac up_to_ac[] = { 344 EDCA_AC_BE, /* BE */ 345 EDCA_AC_BK, /* BK */ 346 EDCA_AC_BK, /* -- */ 347 EDCA_AC_BE, /* EE */ 348 EDCA_AC_VI, /* CL */ 349 EDCA_AC_VI, /* VI */ 350 EDCA_AC_VO, /* VO */ 351 EDCA_AC_VO /* NC */ 352 }; 353 enum ieee80211_edca_ac ac; 354 355 ac = (up <= 7) ? up_to_ac[up] : EDCA_AC_BE; 356 357 #ifndef IEEE80211_STA_ONLY 358 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 359 return ac; 360 #endif 361 /* 362 * We do not support the admission control procedure defined in 363 * IEEE Std 802.11-2012 section 9.19.4.2.3. The spec says that 364 * non-AP QSTAs that don't support this procedure shall use EDCA 365 * parameters of a lower priority AC that does not require 366 * admission control. 367 */ 368 while (ac != EDCA_AC_BK && ic->ic_edca_ac[ac].ac_acm) { 369 switch (ac) { 370 case EDCA_AC_BK: 371 /* can't get there */ 372 break; 373 case EDCA_AC_BE: 374 /* BE shouldn't require admission control */ 375 ac = EDCA_AC_BK; 376 break; 377 case EDCA_AC_VI: 378 ac = EDCA_AC_BE; 379 break; 380 case EDCA_AC_VO: 381 ac = EDCA_AC_VI; 382 break; 383 } 384 } 385 return ac; 386 } 387 388 /* 389 * Get mbuf's user-priority: if mbuf is not VLAN tagged, select user-priority 390 * based on the DSCP (Differentiated Services Codepoint) field. 391 */ 392 int 393 ieee80211_classify(struct ieee80211com *ic, struct mbuf *m) 394 { 395 struct ether_header *eh; 396 u_int8_t ds_field; 397 #if NVLAN > 0 398 if (m->m_flags & M_VLANTAG) /* use VLAN 802.1D user-priority */ 399 return EVL_PRIOFTAG(m->m_pkthdr.ether_vtag); 400 #endif 401 eh = mtod(m, struct ether_header *); 402 if (eh->ether_type == htons(ETHERTYPE_IP)) { 403 struct ip *ip = (struct ip *)&eh[1]; 404 if (ip->ip_v != 4) 405 return 0; 406 ds_field = ip->ip_tos; 407 } 408 #ifdef INET6 409 else if (eh->ether_type == htons(ETHERTYPE_IPV6)) { 410 struct ip6_hdr *ip6 = (struct ip6_hdr *)&eh[1]; 411 u_int32_t flowlabel; 412 413 flowlabel = ntohl(ip6->ip6_flow); 414 if ((flowlabel >> 28) != 6) 415 return 0; 416 ds_field = (flowlabel >> 20) & 0xff; 417 } 418 #endif /* INET6 */ 419 else /* neither IPv4 nor IPv6 */ 420 return 0; 421 422 /* 423 * Map Differentiated Services Codepoint field (see RFC2474). 424 * Preserves backward compatibility with IP Precedence field. 425 */ 426 switch (ds_field & 0xfc) { 427 case IPTOS_PREC_PRIORITY: 428 return 2; 429 case IPTOS_PREC_IMMEDIATE: 430 return 1; 431 case IPTOS_PREC_FLASH: 432 return 3; 433 case IPTOS_PREC_FLASHOVERRIDE: 434 return 4; 435 case IPTOS_PREC_CRITIC_ECP: 436 return 5; 437 case IPTOS_PREC_INTERNETCONTROL: 438 return 6; 439 case IPTOS_PREC_NETCONTROL: 440 return 7; 441 } 442 return 0; /* default to Best-Effort */ 443 } 444 445 /* 446 * Encapsulate an outbound data frame. The mbuf chain is updated and 447 * a reference to the destination node is returned. If an error is 448 * encountered NULL is returned and the node reference will also be NULL. 449 * 450 * NB: The caller is responsible for free'ing a returned node reference. 451 * The convention is ic_bss is not reference counted; the caller must 452 * maintain that. 453 */ 454 struct mbuf * 455 ieee80211_encap(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node **pni) 456 { 457 struct ieee80211com *ic = (void *)ifp; 458 struct ether_header eh; 459 struct ieee80211_frame *wh; 460 struct ieee80211_node *ni = NULL; 461 struct llc *llc; 462 struct m_tag *mtag; 463 u_int8_t *addr; 464 u_int dlt, hdrlen; 465 int addqos, tid; 466 467 /* Handle raw frames if mbuf is tagged as 802.11 */ 468 if ((mtag = m_tag_find(m, PACKET_TAG_DLT, NULL)) != NULL) { 469 dlt = *(u_int *)(mtag + 1); 470 471 if (!(dlt == DLT_IEEE802_11 || dlt == DLT_IEEE802_11_RADIO)) 472 goto fallback; 473 474 wh = mtod(m, struct ieee80211_frame *); 475 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 476 case IEEE80211_FC1_DIR_NODS: 477 case IEEE80211_FC1_DIR_FROMDS: 478 addr = wh->i_addr1; 479 break; 480 case IEEE80211_FC1_DIR_DSTODS: 481 case IEEE80211_FC1_DIR_TODS: 482 addr = wh->i_addr3; 483 break; 484 default: 485 goto bad; 486 } 487 488 ni = ieee80211_find_txnode(ic, addr); 489 if (ni == NULL) 490 ni = ieee80211_ref_node(ic->ic_bss); 491 if (ni == NULL) { 492 printf("%s: no node for dst %s, " 493 "discard raw tx frame\n", ifp->if_xname, 494 ether_sprintf(addr)); 495 ic->ic_stats.is_tx_nonode++; 496 goto bad; 497 } 498 ni->ni_inact = 0; 499 500 *pni = ni; 501 return (m); 502 } 503 504 fallback: 505 if (m->m_len < sizeof(struct ether_header)) { 506 m = m_pullup(m, sizeof(struct ether_header)); 507 if (m == NULL) { 508 ic->ic_stats.is_tx_nombuf++; 509 goto bad; 510 } 511 } 512 memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header)); 513 514 ni = ieee80211_find_txnode(ic, eh.ether_dhost); 515 if (ni == NULL) { 516 DPRINTF(("no node for dst %s, discard frame\n", 517 ether_sprintf(eh.ether_dhost))); 518 ic->ic_stats.is_tx_nonode++; 519 goto bad; 520 } 521 522 if ((ic->ic_flags & IEEE80211_F_RSNON) && 523 !ni->ni_port_valid && 524 eh.ether_type != htons(ETHERTYPE_PAE)) { 525 DPRINTF(("port not valid: %s\n", 526 ether_sprintf(eh.ether_dhost))); 527 ic->ic_stats.is_tx_noauth++; 528 goto bad; 529 } 530 531 if ((ic->ic_flags & IEEE80211_F_COUNTERM) && 532 ni->ni_rsncipher == IEEE80211_CIPHER_TKIP) 533 /* XXX TKIP countermeasures! */; 534 535 ni->ni_inact = 0; 536 537 if ((ic->ic_flags & IEEE80211_F_QOS) && 538 (ni->ni_flags & IEEE80211_NODE_QOS) && 539 /* do not QoS-encapsulate EAPOL frames */ 540 eh.ether_type != htons(ETHERTYPE_PAE)) { 541 tid = ieee80211_classify(ic, m); 542 hdrlen = sizeof(struct ieee80211_qosframe); 543 addqos = 1; 544 } else { 545 hdrlen = sizeof(struct ieee80211_frame); 546 addqos = 0; 547 } 548 m_adj(m, sizeof(struct ether_header) - LLC_SNAPFRAMELEN); 549 llc = mtod(m, struct llc *); 550 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 551 llc->llc_control = LLC_UI; 552 llc->llc_snap.org_code[0] = 0; 553 llc->llc_snap.org_code[1] = 0; 554 llc->llc_snap.org_code[2] = 0; 555 llc->llc_snap.ether_type = eh.ether_type; 556 M_PREPEND(m, hdrlen, M_DONTWAIT); 557 if (m == NULL) { 558 ic->ic_stats.is_tx_nombuf++; 559 goto bad; 560 } 561 wh = mtod(m, struct ieee80211_frame *); 562 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA; 563 *(u_int16_t *)&wh->i_dur[0] = 0; 564 if (addqos) { 565 struct ieee80211_qosframe *qwh = 566 (struct ieee80211_qosframe *)wh; 567 u_int16_t qos = tid; 568 569 if (ic->ic_tid_noack & (1 << tid)) 570 qos |= IEEE80211_QOS_ACK_POLICY_NOACK; 571 else if (ni->ni_tx_ba[tid].ba_state == IEEE80211_BA_AGREED) 572 qos |= IEEE80211_QOS_ACK_POLICY_BA; 573 qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS; 574 *(u_int16_t *)qwh->i_qos = htole16(qos); 575 *(u_int16_t *)qwh->i_seq = 576 htole16(ni->ni_qos_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT); 577 ni->ni_qos_txseqs[tid]++; 578 } else { 579 *(u_int16_t *)&wh->i_seq[0] = 580 htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT); 581 ni->ni_txseq++; 582 } 583 switch (ic->ic_opmode) { 584 case IEEE80211_M_STA: 585 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS; 586 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid); 587 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 588 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost); 589 break; 590 #ifndef IEEE80211_STA_ONLY 591 case IEEE80211_M_IBSS: 592 case IEEE80211_M_AHDEMO: 593 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 594 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 595 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost); 596 IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid); 597 break; 598 case IEEE80211_M_HOSTAP: 599 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS; 600 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost); 601 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid); 602 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost); 603 break; 604 #endif 605 default: 606 /* should not get there */ 607 goto bad; 608 } 609 610 if ((ic->ic_flags & IEEE80211_F_WEPON) || 611 ((ic->ic_flags & IEEE80211_F_RSNON) && 612 (ni->ni_flags & IEEE80211_NODE_TXPROT))) 613 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED; 614 615 #ifndef IEEE80211_STA_ONLY 616 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 617 ieee80211_pwrsave(ic, m, ni) != 0) { 618 *pni = NULL; 619 return NULL; 620 } 621 #endif 622 *pni = ni; 623 return m; 624 bad: 625 m_freem(m); 626 if (ni != NULL) 627 ieee80211_release_node(ic, ni); 628 *pni = NULL; 629 return NULL; 630 } 631 632 /* 633 * Add a Capability Information field to a frame (see 7.3.1.4). 634 */ 635 u_int8_t * 636 ieee80211_add_capinfo(u_int8_t *frm, struct ieee80211com *ic, 637 const struct ieee80211_node *ni) 638 { 639 u_int16_t capinfo; 640 641 #ifndef IEEE80211_STA_ONLY 642 if (ic->ic_opmode == IEEE80211_M_IBSS) 643 capinfo = IEEE80211_CAPINFO_IBSS; 644 else if (ic->ic_opmode == IEEE80211_M_HOSTAP) 645 capinfo = IEEE80211_CAPINFO_ESS; 646 else 647 #endif 648 capinfo = 0; 649 #ifndef IEEE80211_STA_ONLY 650 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 651 (ic->ic_flags & (IEEE80211_F_WEPON | IEEE80211_F_RSNON))) 652 capinfo |= IEEE80211_CAPINFO_PRIVACY; 653 #endif 654 /* NB: some 11a AP's reject the request when short preamble is set */ 655 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 656 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 657 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 658 if (ic->ic_flags & IEEE80211_F_SHSLOT) 659 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 660 LE_WRITE_2(frm, capinfo); 661 return frm + 2; 662 } 663 664 /* 665 * Add an SSID element to a frame (see 7.3.2.1). 666 */ 667 u_int8_t * 668 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len) 669 { 670 *frm++ = IEEE80211_ELEMID_SSID; 671 *frm++ = len; 672 memcpy(frm, ssid, len); 673 return frm + len; 674 } 675 676 /* 677 * Add a supported rates element to a frame (see 7.3.2.2). 678 */ 679 u_int8_t * 680 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs) 681 { 682 int nrates; 683 684 *frm++ = IEEE80211_ELEMID_RATES; 685 nrates = min(rs->rs_nrates, IEEE80211_RATE_SIZE); 686 *frm++ = nrates; 687 memcpy(frm, rs->rs_rates, nrates); 688 return frm + nrates; 689 } 690 691 #ifndef IEEE80211_STA_ONLY 692 /* 693 * Add a DS Parameter Set element to a frame (see 7.3.2.4). 694 */ 695 u_int8_t * 696 ieee80211_add_ds_params(u_int8_t *frm, struct ieee80211com *ic, 697 const struct ieee80211_node *ni) 698 { 699 *frm++ = IEEE80211_ELEMID_DSPARMS; 700 *frm++ = 1; 701 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 702 return frm; 703 } 704 705 /* 706 * Add a TIM element to a frame (see 7.3.2.6 and Annex L). 707 */ 708 u_int8_t * 709 ieee80211_add_tim(u_int8_t *frm, struct ieee80211com *ic) 710 { 711 u_int i, offset = 0, len; 712 713 /* find first non-zero octet in the virtual bit map */ 714 for (i = 0; i < ic->ic_tim_len && ic->ic_tim_bitmap[i] == 0; i++); 715 716 /* clear the lsb as it is reserved for the broadcast indication bit */ 717 if (i < ic->ic_tim_len) 718 offset = i & ~1; 719 720 /* find last non-zero octet in the virtual bit map */ 721 for (i = ic->ic_tim_len - 1; i > 0 && ic->ic_tim_bitmap[i] == 0; i--); 722 723 len = i - offset + 1; 724 725 *frm++ = IEEE80211_ELEMID_TIM; 726 *frm++ = len + 3; /* length */ 727 *frm++ = ic->ic_dtim_count; /* DTIM count */ 728 *frm++ = ic->ic_dtim_period; /* DTIM period */ 729 730 /* Bitmap Control */ 731 *frm = offset; 732 /* set broadcast/multicast indication bit if necessary */ 733 if (ic->ic_dtim_count == 0 && ic->ic_tim_mcast_pending) 734 *frm |= 0x01; 735 frm++; 736 737 /* Partial Virtual Bitmap */ 738 memcpy(frm, &ic->ic_tim_bitmap[offset], len); 739 return frm + len; 740 } 741 742 /* 743 * Add an IBSS Parameter Set element to a frame (see 7.3.2.7). 744 */ 745 u_int8_t * 746 ieee80211_add_ibss_params(u_int8_t *frm, const struct ieee80211_node *ni) 747 { 748 *frm++ = IEEE80211_ELEMID_IBSSPARMS; 749 *frm++ = 2; 750 LE_WRITE_2(frm, 0); /* TODO: ATIM window */ 751 return frm + 2; 752 } 753 754 /* 755 * Add an EDCA Parameter Set element to a frame (see 7.3.2.29). 756 */ 757 u_int8_t * 758 ieee80211_add_edca_params(u_int8_t *frm, struct ieee80211com *ic) 759 { 760 const struct ieee80211_edca_ac_params *edca; 761 int aci; 762 763 *frm++ = IEEE80211_ELEMID_EDCAPARMS; 764 *frm++ = 18; /* length */ 765 *frm++ = 0; /* QoS Info */ 766 *frm++ = 0; /* reserved */ 767 768 /* setup AC Parameter Records */ 769 edca = ieee80211_edca_table[ic->ic_curmode]; 770 for (aci = 0; aci < EDCA_NUM_AC; aci++) { 771 const struct ieee80211_edca_ac_params *ac = &edca[aci]; 772 773 *frm++ = (aci << 5) | ((ac->ac_acm & 0x1) << 4) | 774 (ac->ac_aifsn & 0xf); 775 *frm++ = (ac->ac_ecwmax << 4) | 776 (ac->ac_ecwmin & 0xf); 777 LE_WRITE_2(frm, ac->ac_txoplimit); frm += 2; 778 } 779 return frm; 780 } 781 782 /* 783 * Add an ERP element to a frame (see 7.3.2.13). 784 */ 785 u_int8_t * 786 ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic) 787 { 788 u_int8_t erp; 789 int nonerpsta = 0; 790 791 *frm++ = IEEE80211_ELEMID_ERP; 792 *frm++ = 1; 793 erp = 0; 794 /* 795 * The NonERP_Present bit shall be set to 1 when a NonERP STA 796 * is associated with the BSS. 797 */ 798 ieee80211_iterate_nodes(ic, ieee80211_count_nonerpsta, &nonerpsta); 799 if (nonerpsta != 0) 800 erp |= IEEE80211_ERP_NON_ERP_PRESENT; 801 /* 802 * If one or more NonERP STAs are associated in the BSS, the 803 * Use_Protection bit shall be set to 1 in transmitted ERP 804 * Information Elements. 805 */ 806 if (ic->ic_flags & IEEE80211_F_USEPROT) 807 erp |= IEEE80211_ERP_USE_PROTECTION; 808 /* 809 * The Barker_Preamble_Mode bit shall be set to 1 by the ERP 810 * Information Element sender if one or more associated NonERP 811 * STAs are not short preamble capable. 812 */ 813 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 814 erp |= IEEE80211_ERP_BARKER_MODE; 815 *frm++ = erp; 816 return frm; 817 } 818 #endif /* IEEE80211_STA_ONLY */ 819 820 /* 821 * Add a QoS Capability element to a frame (see 7.3.2.35). 822 */ 823 u_int8_t * 824 ieee80211_add_qos_capability(u_int8_t *frm, struct ieee80211com *ic) 825 { 826 *frm++ = IEEE80211_ELEMID_QOS_CAP; 827 *frm++ = 1; 828 *frm++ = 0; /* QoS Info */ 829 return frm; 830 } 831 832 /* 833 * Add a Wifi-Alliance WME (aka WMM) info element to a frame. 834 * WME is a requirement for Wifi-Alliance compliance and some 835 * 11n APs will not negotiate HT if this element is missing. 836 */ 837 uint8_t * 838 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211com *ic) 839 { 840 *frm++ = IEEE80211_ELEMID_VENDOR; 841 *frm++ = 7; 842 memcpy(frm, MICROSOFT_OUI, 3); frm += 3; 843 *frm++ = 2; /* OUI type */ 844 *frm++ = 0; /* OUI subtype */ 845 *frm++ = 1; /* version */ 846 *frm++ = 0; /* info */ 847 848 return frm; 849 } 850 851 #ifndef IEEE80211_STA_ONLY 852 /* 853 * Add a Wifi-Alliance WMM (aka WME) parameter element to a frame. 854 */ 855 uint8_t * 856 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211com *ic) 857 { 858 const struct ieee80211_edca_ac_params *edca; 859 int aci; 860 861 *frm++ = IEEE80211_ELEMID_VENDOR; 862 *frm++ = 24; 863 memcpy(frm, MICROSOFT_OUI, 3); frm += 3; 864 *frm++ = 2; /* OUI type */ 865 *frm++ = 1; /* OUI subtype */ 866 *frm++ = 1; /* version */ 867 *frm++ = 0; /* info */ 868 *frm++ = 0; /* reserved */ 869 870 /* setup AC Parameter Records */ 871 edca = ieee80211_edca_table[ic->ic_curmode]; 872 for (aci = 0; aci < EDCA_NUM_AC; aci++) { 873 const struct ieee80211_edca_ac_params *ac = &edca[aci]; 874 875 *frm++ = (aci << 5) | ((ac->ac_acm & 0x1) << 4) | 876 (ac->ac_aifsn & 0xf); 877 *frm++ = (ac->ac_ecwmax << 4) | 878 (ac->ac_ecwmin & 0xf); 879 LE_WRITE_2(frm, ac->ac_txoplimit); frm += 2; 880 } 881 882 return frm; 883 } 884 #endif 885 886 /* 887 * Add an RSN element to a frame (see 802.11-2012 8.4.2.27) 888 */ 889 u_int8_t * 890 ieee80211_add_rsn_body(u_int8_t *frm, struct ieee80211com *ic, 891 const struct ieee80211_node *ni, int wpa) 892 { 893 const u_int8_t *oui = wpa ? MICROSOFT_OUI : IEEE80211_OUI; 894 u_int8_t *pcount; 895 u_int16_t count; 896 897 /* write Version field */ 898 LE_WRITE_2(frm, 1); frm += 2; 899 900 /* write Group Data Cipher Suite field (see 802.11-2012 Table 8-99) */ 901 memcpy(frm, oui, 3); frm += 3; 902 switch (ni->ni_rsngroupcipher) { 903 case IEEE80211_CIPHER_WEP40: 904 *frm++ = 1; 905 break; 906 case IEEE80211_CIPHER_TKIP: 907 *frm++ = 2; 908 break; 909 case IEEE80211_CIPHER_CCMP: 910 *frm++ = 4; 911 break; 912 case IEEE80211_CIPHER_WEP104: 913 *frm++ = 5; 914 break; 915 default: 916 /* can't get there */ 917 panic("invalid group data cipher!"); 918 } 919 920 pcount = frm; frm += 2; 921 count = 0; 922 /* write Pairwise Cipher Suite List */ 923 if (ni->ni_rsnciphers & IEEE80211_CIPHER_USEGROUP) { 924 memcpy(frm, oui, 3); frm += 3; 925 *frm++ = 0; 926 count++; 927 } 928 if (ni->ni_rsnciphers & IEEE80211_CIPHER_TKIP) { 929 memcpy(frm, oui, 3); frm += 3; 930 *frm++ = 2; 931 count++; 932 } 933 if (ni->ni_rsnciphers & IEEE80211_CIPHER_CCMP) { 934 memcpy(frm, oui, 3); frm += 3; 935 *frm++ = 4; 936 count++; 937 } 938 /* write Pairwise Cipher Suite Count field */ 939 LE_WRITE_2(pcount, count); 940 941 pcount = frm; frm += 2; 942 count = 0; 943 /* write AKM Suite List (see Table 20dc) */ 944 if (ni->ni_rsnakms & IEEE80211_AKM_8021X) { 945 memcpy(frm, oui, 3); frm += 3; 946 *frm++ = 1; 947 count++; 948 } 949 if (ni->ni_rsnakms & IEEE80211_AKM_PSK) { 950 memcpy(frm, oui, 3); frm += 3; 951 *frm++ = 2; 952 count++; 953 } 954 if (!wpa && (ni->ni_rsnakms & IEEE80211_AKM_SHA256_8021X)) { 955 memcpy(frm, oui, 3); frm += 3; 956 *frm++ = 5; 957 count++; 958 } 959 if (!wpa && (ni->ni_rsnakms & IEEE80211_AKM_SHA256_PSK)) { 960 memcpy(frm, oui, 3); frm += 3; 961 *frm++ = 6; 962 count++; 963 } 964 /* write AKM Suite List Count field */ 965 LE_WRITE_2(pcount, count); 966 967 if (wpa) 968 return frm; 969 970 /* write RSN Capabilities field */ 971 LE_WRITE_2(frm, ni->ni_rsncaps); frm += 2; 972 973 if (ni->ni_flags & IEEE80211_NODE_PMKID) { 974 /* write PMKID Count field */ 975 LE_WRITE_2(frm, 1); frm += 2; 976 /* write PMKID List (only 1) */ 977 memcpy(frm, ni->ni_pmkid, IEEE80211_PMKID_LEN); 978 frm += IEEE80211_PMKID_LEN; 979 } else { 980 /* no PMKID (PMKID Count=0) */ 981 LE_WRITE_2(frm, 0); frm += 2; 982 } 983 984 if (!(ic->ic_caps & IEEE80211_C_MFP)) 985 return frm; 986 987 /* write Group Integrity Cipher Suite field */ 988 memcpy(frm, oui, 3); frm += 3; 989 switch (ic->ic_rsngroupmgmtcipher) { 990 case IEEE80211_CIPHER_BIP: 991 *frm++ = 6; 992 break; 993 default: 994 /* can't get there */ 995 panic("invalid integrity group cipher!"); 996 } 997 return frm; 998 } 999 1000 u_int8_t * 1001 ieee80211_add_rsn(u_int8_t *frm, struct ieee80211com *ic, 1002 const struct ieee80211_node *ni) 1003 { 1004 u_int8_t *plen; 1005 1006 *frm++ = IEEE80211_ELEMID_RSN; 1007 plen = frm++; /* length filled in later */ 1008 frm = ieee80211_add_rsn_body(frm, ic, ni, 0); 1009 1010 /* write length field */ 1011 *plen = frm - plen - 1; 1012 return frm; 1013 } 1014 1015 /* 1016 * Add a vendor-specific WPA element to a frame. 1017 * This is required for compatibility with Wi-Fi Alliance WPA. 1018 */ 1019 u_int8_t * 1020 ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic, 1021 const struct ieee80211_node *ni) 1022 { 1023 u_int8_t *plen; 1024 1025 *frm++ = IEEE80211_ELEMID_VENDOR; 1026 plen = frm++; /* length filled in later */ 1027 memcpy(frm, MICROSOFT_OUI, 3); frm += 3; 1028 *frm++ = 1; /* WPA */ 1029 frm = ieee80211_add_rsn_body(frm, ic, ni, 1); 1030 1031 /* write length field */ 1032 *plen = frm - plen - 1; 1033 return frm; 1034 } 1035 1036 /* 1037 * Add an extended supported rates element to a frame (see 7.3.2.14). 1038 */ 1039 u_int8_t * 1040 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs) 1041 { 1042 int nrates; 1043 1044 KASSERT(rs->rs_nrates > IEEE80211_RATE_SIZE); 1045 1046 *frm++ = IEEE80211_ELEMID_XRATES; 1047 nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 1048 *frm++ = nrates; 1049 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 1050 return frm + nrates; 1051 } 1052 1053 /* 1054 * Add an HT Capabilities element to a frame (see 7.3.2.57). 1055 */ 1056 u_int8_t * 1057 ieee80211_add_htcaps(u_int8_t *frm, struct ieee80211com *ic) 1058 { 1059 *frm++ = IEEE80211_ELEMID_HTCAPS; 1060 *frm++ = 26; 1061 LE_WRITE_2(frm, ic->ic_htcaps); frm += 2; 1062 *frm++ = ic->ic_ampdu_params; 1063 memcpy(frm, ic->ic_sup_mcs, 10); frm += 10; 1064 LE_WRITE_2(frm, (ic->ic_max_rxrate & IEEE80211_MCS_RX_RATE_HIGH)); 1065 frm += 2; 1066 *frm++ = ic->ic_tx_mcs_set; 1067 *frm++ = 0; /* reserved */ 1068 *frm++ = 0; /* reserved */ 1069 *frm++ = 0; /* reserved */ 1070 LE_WRITE_2(frm, ic->ic_htxcaps); frm += 2; 1071 LE_WRITE_4(frm, ic->ic_txbfcaps); frm += 4; 1072 *frm++ = ic->ic_aselcaps; 1073 return frm; 1074 } 1075 1076 #ifndef IEEE80211_STA_ONLY 1077 /* 1078 * Add an HT Operation element to a frame (see 7.3.2.58). 1079 */ 1080 u_int8_t * 1081 ieee80211_add_htop(u_int8_t *frm, struct ieee80211com *ic) 1082 { 1083 *frm++ = IEEE80211_ELEMID_HTOP; 1084 *frm++ = 22; 1085 *frm++ = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan); 1086 *frm++ = ic->ic_bss->ni_htop0; 1087 LE_WRITE_2(frm, ic->ic_bss->ni_htop1); frm += 2; 1088 LE_WRITE_2(frm, ic->ic_bss->ni_htop2); frm += 2; 1089 memset(frm, 0, 16); frm += 16; 1090 return frm; 1091 } 1092 #endif /* !IEEE80211_STA_ONLY */ 1093 1094 #ifndef IEEE80211_STA_ONLY 1095 /* 1096 * Add a Timeout Interval element to a frame (see 7.3.2.49). 1097 */ 1098 u_int8_t * 1099 ieee80211_add_tie(u_int8_t *frm, u_int8_t type, u_int32_t value) 1100 { 1101 *frm++ = IEEE80211_ELEMID_TIE; 1102 *frm++ = 5; /* length */ 1103 *frm++ = type; /* Timeout Interval type */ 1104 LE_WRITE_4(frm, value); 1105 return frm + 4; 1106 } 1107 #endif 1108 1109 struct mbuf * 1110 ieee80211_getmgmt(int flags, int type, u_int pktlen) 1111 { 1112 struct mbuf *m; 1113 1114 /* reserve space for 802.11 header */ 1115 pktlen += sizeof(struct ieee80211_frame); 1116 1117 if (pktlen > MCLBYTES) 1118 panic("management frame too large: %u", pktlen); 1119 MGETHDR(m, flags, type); 1120 if (m == NULL) 1121 return NULL; 1122 if (pktlen > MHLEN) { 1123 MCLGET(m, flags); 1124 if (!(m->m_flags & M_EXT)) 1125 return m_free(m); 1126 } 1127 m->m_data += sizeof(struct ieee80211_frame); 1128 return m; 1129 } 1130 1131 /*- 1132 * Probe request frame format: 1133 * [tlv] SSID 1134 * [tlv] Supported rates 1135 * [tlv] Extended Supported Rates (802.11g) 1136 * [tlv] HT Capabilities (802.11n) 1137 */ 1138 struct mbuf * 1139 ieee80211_get_probe_req(struct ieee80211com *ic, struct ieee80211_node *ni) 1140 { 1141 const struct ieee80211_rateset *rs = 1142 &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)]; 1143 struct mbuf *m; 1144 u_int8_t *frm; 1145 1146 m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 1147 2 + ic->ic_des_esslen + 1148 2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) + 1149 ((rs->rs_nrates > IEEE80211_RATE_SIZE) ? 1150 2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) + 1151 ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 9 : 0)); 1152 if (m == NULL) 1153 return NULL; 1154 1155 frm = mtod(m, u_int8_t *); 1156 frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen); 1157 frm = ieee80211_add_rates(frm, rs); 1158 if (rs->rs_nrates > IEEE80211_RATE_SIZE) 1159 frm = ieee80211_add_xrates(frm, rs); 1160 if (ic->ic_flags & IEEE80211_F_HTON) { 1161 frm = ieee80211_add_htcaps(frm, ic); 1162 frm = ieee80211_add_wme_info(frm, ic); 1163 } 1164 1165 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1166 1167 return m; 1168 } 1169 1170 #ifndef IEEE80211_STA_ONLY 1171 /*- 1172 * Probe response frame format: 1173 * [8] Timestamp 1174 * [2] Beacon interval 1175 * [2] Capability 1176 * [tlv] Service Set Identifier (SSID) 1177 * [tlv] Supported rates 1178 * [tlv] DS Parameter Set (802.11g) 1179 * [tlv] ERP Information (802.11g) 1180 * [tlv] Extended Supported Rates (802.11g) 1181 * [tlv] RSN (802.11i) 1182 * [tlv] EDCA Parameter Set (802.11e) 1183 * [tlv] HT Capabilities (802.11n) 1184 * [tlv] HT Operation (802.11n) 1185 */ 1186 struct mbuf * 1187 ieee80211_get_probe_resp(struct ieee80211com *ic, struct ieee80211_node *ni) 1188 { 1189 const struct ieee80211_rateset *rs = &ic->ic_bss->ni_rates; 1190 struct mbuf *m; 1191 u_int8_t *frm; 1192 1193 m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 1194 8 + 2 + 2 + 1195 2 + ni->ni_esslen + 1196 2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) + 1197 2 + 1 + 1198 ((ic->ic_opmode == IEEE80211_M_IBSS) ? 2 + 2 : 0) + 1199 ((ic->ic_curmode == IEEE80211_MODE_11G) ? 2 + 1 : 0) + 1200 ((rs->rs_nrates > IEEE80211_RATE_SIZE) ? 1201 2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) + 1202 (((ic->ic_flags & IEEE80211_F_RSNON) && 1203 (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_RSN)) ? 1204 2 + IEEE80211_RSNIE_MAXLEN : 0) + 1205 ((ic->ic_flags & IEEE80211_F_QOS) ? 2 + 18 : 0) + 1206 (((ic->ic_flags & IEEE80211_F_RSNON) && 1207 (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_WPA)) ? 1208 2 + IEEE80211_WPAIE_MAXLEN : 0) + 1209 ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 24 + 26 : 0)); 1210 if (m == NULL) 1211 return NULL; 1212 1213 frm = mtod(m, u_int8_t *); 1214 memset(frm, 0, 8); frm += 8; /* timestamp is set by hardware */ 1215 LE_WRITE_2(frm, ic->ic_bss->ni_intval); frm += 2; 1216 frm = ieee80211_add_capinfo(frm, ic, ni); 1217 frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid, 1218 ic->ic_bss->ni_esslen); 1219 frm = ieee80211_add_rates(frm, rs); 1220 frm = ieee80211_add_ds_params(frm, ic, ni); 1221 if (ic->ic_opmode == IEEE80211_M_IBSS) 1222 frm = ieee80211_add_ibss_params(frm, ni); 1223 if (ic->ic_curmode == IEEE80211_MODE_11G) 1224 frm = ieee80211_add_erp(frm, ic); 1225 if (rs->rs_nrates > IEEE80211_RATE_SIZE) 1226 frm = ieee80211_add_xrates(frm, rs); 1227 if ((ic->ic_flags & IEEE80211_F_RSNON) && 1228 (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_RSN)) 1229 frm = ieee80211_add_rsn(frm, ic, ic->ic_bss); 1230 if (ic->ic_flags & IEEE80211_F_QOS) 1231 frm = ieee80211_add_edca_params(frm, ic); 1232 if ((ic->ic_flags & IEEE80211_F_RSNON) && 1233 (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_WPA)) 1234 frm = ieee80211_add_wpa(frm, ic, ic->ic_bss); 1235 if (ic->ic_flags & IEEE80211_F_HTON) { 1236 frm = ieee80211_add_htcaps(frm, ic); 1237 frm = ieee80211_add_htop(frm, ic); 1238 frm = ieee80211_add_wme_param(frm, ic); 1239 } 1240 1241 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1242 1243 return m; 1244 } 1245 #endif /* IEEE80211_STA_ONLY */ 1246 1247 /*- 1248 * Authentication frame format: 1249 * [2] Authentication algorithm number 1250 * [2] Authentication transaction sequence number 1251 * [2] Status code 1252 */ 1253 struct mbuf * 1254 ieee80211_get_auth(struct ieee80211com *ic, struct ieee80211_node *ni, 1255 u_int16_t status, u_int16_t seq) 1256 { 1257 struct mbuf *m; 1258 u_int8_t *frm; 1259 1260 MGETHDR(m, M_DONTWAIT, MT_DATA); 1261 if (m == NULL) 1262 return NULL; 1263 MH_ALIGN(m, 2 * 3); 1264 m->m_pkthdr.len = m->m_len = 2 * 3; 1265 1266 frm = mtod(m, u_int8_t *); 1267 LE_WRITE_2(frm, IEEE80211_AUTH_ALG_OPEN); frm += 2; 1268 LE_WRITE_2(frm, seq); frm += 2; 1269 LE_WRITE_2(frm, status); 1270 1271 return m; 1272 } 1273 1274 /*- 1275 * Deauthentication frame format: 1276 * [2] Reason code 1277 */ 1278 struct mbuf * 1279 ieee80211_get_deauth(struct ieee80211com *ic, struct ieee80211_node *ni, 1280 u_int16_t reason) 1281 { 1282 struct mbuf *m; 1283 1284 MGETHDR(m, M_DONTWAIT, MT_DATA); 1285 if (m == NULL) 1286 return NULL; 1287 MH_ALIGN(m, 2); 1288 1289 m->m_pkthdr.len = m->m_len = 2; 1290 *mtod(m, u_int16_t *) = htole16(reason); 1291 1292 return m; 1293 } 1294 1295 /*- 1296 * (Re)Association request frame format: 1297 * [2] Capability information 1298 * [2] Listen interval 1299 * [6*] Current AP address (Reassociation only) 1300 * [tlv] SSID 1301 * [tlv] Supported rates 1302 * [tlv] Extended Supported Rates (802.11g) 1303 * [tlv] RSN (802.11i) 1304 * [tlv] QoS Capability (802.11e) 1305 * [tlv] HT Capabilities (802.11n) 1306 */ 1307 struct mbuf * 1308 ieee80211_get_assoc_req(struct ieee80211com *ic, struct ieee80211_node *ni, 1309 int type) 1310 { 1311 const struct ieee80211_rateset *rs = &ni->ni_rates; 1312 struct mbuf *m; 1313 u_int8_t *frm; 1314 u_int16_t capinfo; 1315 1316 m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 1317 2 + 2 + 1318 ((type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) ? 1319 IEEE80211_ADDR_LEN : 0) + 1320 2 + ni->ni_esslen + 1321 2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) + 1322 ((rs->rs_nrates > IEEE80211_RATE_SIZE) ? 1323 2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) + 1324 (((ic->ic_flags & IEEE80211_F_RSNON) && 1325 (ni->ni_rsnprotos & IEEE80211_PROTO_RSN)) ? 1326 2 + IEEE80211_RSNIE_MAXLEN : 0) + 1327 ((ni->ni_flags & IEEE80211_NODE_QOS) ? 2 + 1 : 0) + 1328 (((ic->ic_flags & IEEE80211_F_RSNON) && 1329 (ni->ni_rsnprotos & IEEE80211_PROTO_WPA)) ? 1330 2 + IEEE80211_WPAIE_MAXLEN : 0) + 1331 ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 9 : 0)); 1332 if (m == NULL) 1333 return NULL; 1334 1335 frm = mtod(m, u_int8_t *); 1336 capinfo = IEEE80211_CAPINFO_ESS; 1337 if (ic->ic_flags & IEEE80211_F_WEPON) 1338 capinfo |= IEEE80211_CAPINFO_PRIVACY; 1339 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1340 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 1341 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 1342 if (ic->ic_caps & IEEE80211_C_SHSLOT) 1343 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 1344 LE_WRITE_2(frm, capinfo); frm += 2; 1345 LE_WRITE_2(frm, ic->ic_lintval); frm += 2; 1346 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 1347 IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid); 1348 frm += IEEE80211_ADDR_LEN; 1349 } 1350 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 1351 frm = ieee80211_add_rates(frm, rs); 1352 if (rs->rs_nrates > IEEE80211_RATE_SIZE) 1353 frm = ieee80211_add_xrates(frm, rs); 1354 if ((ic->ic_flags & IEEE80211_F_RSNON) && 1355 (ni->ni_rsnprotos & IEEE80211_PROTO_RSN)) 1356 frm = ieee80211_add_rsn(frm, ic, ni); 1357 if (ni->ni_flags & IEEE80211_NODE_QOS) 1358 frm = ieee80211_add_qos_capability(frm, ic); 1359 if ((ic->ic_flags & IEEE80211_F_RSNON) && 1360 (ni->ni_rsnprotos & IEEE80211_PROTO_WPA)) 1361 frm = ieee80211_add_wpa(frm, ic, ni); 1362 if (ic->ic_flags & IEEE80211_F_HTON) { 1363 frm = ieee80211_add_htcaps(frm, ic); 1364 frm = ieee80211_add_wme_info(frm, ic); 1365 } 1366 1367 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1368 1369 return m; 1370 } 1371 1372 #ifndef IEEE80211_STA_ONLY 1373 /*- 1374 * (Re)Association response frame format: 1375 * [2] Capability information 1376 * [2] Status code 1377 * [2] Association ID (AID) 1378 * [tlv] Supported rates 1379 * [tlv] Extended Supported Rates (802.11g) 1380 * [tlv] EDCA Parameter Set (802.11e) 1381 * [tlv] Timeout Interval (802.11w) 1382 * [tlv] HT Capabilities (802.11n) 1383 * [tlv] HT Operation (802.11n) 1384 */ 1385 struct mbuf * 1386 ieee80211_get_assoc_resp(struct ieee80211com *ic, struct ieee80211_node *ni, 1387 u_int16_t status) 1388 { 1389 const struct ieee80211_rateset *rs = &ni->ni_rates; 1390 struct mbuf *m; 1391 u_int8_t *frm; 1392 1393 m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 1394 2 + 2 + 2 + 1395 2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) + 1396 ((rs->rs_nrates > IEEE80211_RATE_SIZE) ? 1397 2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) + 1398 ((ni->ni_flags & IEEE80211_NODE_QOS) ? 2 + 18 : 0) + 1399 ((status == IEEE80211_STATUS_TRY_AGAIN_LATER) ? 2 + 7 : 0) + 1400 ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 24 + 26 : 0)); 1401 if (m == NULL) 1402 return NULL; 1403 1404 frm = mtod(m, u_int8_t *); 1405 frm = ieee80211_add_capinfo(frm, ic, ni); 1406 LE_WRITE_2(frm, status); frm += 2; 1407 if (status == IEEE80211_STATUS_SUCCESS) 1408 LE_WRITE_2(frm, ni->ni_associd); 1409 else 1410 LE_WRITE_2(frm, 0); 1411 frm += 2; 1412 frm = ieee80211_add_rates(frm, rs); 1413 if (rs->rs_nrates > IEEE80211_RATE_SIZE) 1414 frm = ieee80211_add_xrates(frm, rs); 1415 if (ni->ni_flags & IEEE80211_NODE_QOS) 1416 frm = ieee80211_add_edca_params(frm, ic); 1417 if ((ni->ni_flags & IEEE80211_NODE_MFP) && 1418 status == IEEE80211_STATUS_TRY_AGAIN_LATER) { 1419 /* Association Comeback Time */ 1420 frm = ieee80211_add_tie(frm, 3, 1000 /* XXX */); 1421 } 1422 if (ic->ic_flags & IEEE80211_F_HTON) { 1423 frm = ieee80211_add_htcaps(frm, ic); 1424 frm = ieee80211_add_htop(frm, ic); 1425 frm = ieee80211_add_wme_param(frm, ic); 1426 } 1427 1428 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1429 1430 return m; 1431 } 1432 #endif /* IEEE80211_STA_ONLY */ 1433 1434 /*- 1435 * Disassociation frame format: 1436 * [2] Reason code 1437 */ 1438 struct mbuf * 1439 ieee80211_get_disassoc(struct ieee80211com *ic, struct ieee80211_node *ni, 1440 u_int16_t reason) 1441 { 1442 struct mbuf *m; 1443 1444 MGETHDR(m, M_DONTWAIT, MT_DATA); 1445 if (m == NULL) 1446 return NULL; 1447 MH_ALIGN(m, 2); 1448 1449 m->m_pkthdr.len = m->m_len = 2; 1450 *mtod(m, u_int16_t *) = htole16(reason); 1451 1452 return m; 1453 } 1454 1455 /*- 1456 * ADDBA Request frame format: 1457 * [1] Category 1458 * [1] Action 1459 * [1] Dialog Token 1460 * [2] Block Ack Parameter Set 1461 * [2] Block Ack Timeout Value 1462 * [2] Block Ack Starting Sequence Control 1463 */ 1464 struct mbuf * 1465 ieee80211_get_addba_req(struct ieee80211com *ic, struct ieee80211_node *ni, 1466 u_int8_t tid) 1467 { 1468 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid]; 1469 struct mbuf *m; 1470 u_int8_t *frm; 1471 1472 m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 9); 1473 if (m == NULL) 1474 return m; 1475 1476 frm = mtod(m, u_int8_t *); 1477 *frm++ = IEEE80211_CATEG_BA; 1478 *frm++ = IEEE80211_ACTION_ADDBA_REQ; 1479 *frm++ = ba->ba_token; 1480 LE_WRITE_2(frm, ba->ba_params); frm += 2; 1481 LE_WRITE_2(frm, ba->ba_timeout_val / IEEE80211_DUR_TU); frm += 2; 1482 LE_WRITE_2(frm, ba->ba_winstart); frm += 2; 1483 1484 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1485 1486 return m; 1487 } 1488 1489 /*- 1490 * ADDBA Response frame format: 1491 * [1] Category 1492 * [1] Action 1493 * [1] Dialog Token 1494 * [2] Status Code 1495 * [2] Block Ack Parameter Set 1496 * [2] Block Ack Timeout Value 1497 */ 1498 struct mbuf * 1499 ieee80211_get_addba_resp(struct ieee80211com *ic, struct ieee80211_node *ni, 1500 u_int8_t tid, u_int8_t token, u_int16_t status) 1501 { 1502 struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid]; 1503 struct mbuf *m; 1504 u_int8_t *frm; 1505 u_int16_t params; 1506 1507 m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 9); 1508 if (m == NULL) 1509 return m; 1510 1511 frm = mtod(m, u_int8_t *); 1512 *frm++ = IEEE80211_CATEG_BA; 1513 *frm++ = IEEE80211_ACTION_ADDBA_RESP; 1514 *frm++ = token; 1515 LE_WRITE_2(frm, status); frm += 2; 1516 if (status == 0) 1517 params = ba->ba_params; 1518 else 1519 params = tid << IEEE80211_ADDBA_TID_SHIFT; 1520 LE_WRITE_2(frm, params); frm += 2; 1521 if (status == 0) 1522 LE_WRITE_2(frm, ba->ba_timeout_val / IEEE80211_DUR_TU); 1523 else 1524 LE_WRITE_2(frm, 0); 1525 frm += 2; 1526 1527 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1528 1529 return m; 1530 } 1531 1532 /*- 1533 * DELBA frame format: 1534 * [1] Category 1535 * [1] Action 1536 * [2] DELBA Parameter Set 1537 * [2] Reason Code 1538 */ 1539 struct mbuf * 1540 ieee80211_get_delba(struct ieee80211com *ic, struct ieee80211_node *ni, 1541 u_int8_t tid, u_int8_t dir, u_int16_t reason) 1542 { 1543 struct mbuf *m; 1544 u_int8_t *frm; 1545 u_int16_t params; 1546 1547 m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 6); 1548 if (m == NULL) 1549 return m; 1550 1551 frm = mtod(m, u_int8_t *); 1552 *frm++ = IEEE80211_CATEG_BA; 1553 *frm++ = IEEE80211_ACTION_DELBA; 1554 params = tid << 12; 1555 if (dir) 1556 params |= IEEE80211_DELBA_INITIATOR; 1557 LE_WRITE_2(frm, params); frm += 2; 1558 LE_WRITE_2(frm, reason); frm += 2; 1559 1560 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1561 1562 return m; 1563 } 1564 1565 /*- 1566 * SA Query Request/Reponse frame format: 1567 * [1] Category 1568 * [1] Action 1569 * [16] Transaction Identifier 1570 */ 1571 struct mbuf * 1572 ieee80211_get_sa_query(struct ieee80211com *ic, struct ieee80211_node *ni, 1573 u_int8_t action) 1574 { 1575 struct mbuf *m; 1576 u_int8_t *frm; 1577 1578 m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 4); 1579 if (m == NULL) 1580 return NULL; 1581 1582 frm = mtod(m, u_int8_t *); 1583 *frm++ = IEEE80211_CATEG_SA_QUERY; 1584 *frm++ = action; /* ACTION_SA_QUERY_REQ/RESP */ 1585 LE_WRITE_2(frm, ni->ni_sa_query_trid); frm += 2; 1586 1587 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1588 1589 return m; 1590 } 1591 1592 struct mbuf * 1593 ieee80211_get_action(struct ieee80211com *ic, struct ieee80211_node *ni, 1594 u_int8_t categ, u_int8_t action, int arg) 1595 { 1596 struct mbuf *m = NULL; 1597 1598 switch (categ) { 1599 case IEEE80211_CATEG_BA: 1600 switch (action) { 1601 case IEEE80211_ACTION_ADDBA_REQ: 1602 m = ieee80211_get_addba_req(ic, ni, arg & 0xffff); 1603 break; 1604 case IEEE80211_ACTION_ADDBA_RESP: 1605 m = ieee80211_get_addba_resp(ic, ni, arg & 0xff, 1606 arg >> 8, arg >> 16); 1607 break; 1608 case IEEE80211_ACTION_DELBA: 1609 m = ieee80211_get_delba(ic, ni, arg & 0xff, arg >> 8, 1610 arg >> 16); 1611 break; 1612 } 1613 break; 1614 case IEEE80211_CATEG_SA_QUERY: 1615 switch (action) { 1616 #ifndef IEEE80211_STA_ONLY 1617 case IEEE80211_ACTION_SA_QUERY_REQ: 1618 #endif 1619 case IEEE80211_ACTION_SA_QUERY_RESP: 1620 m = ieee80211_get_sa_query(ic, ni, action); 1621 break; 1622 } 1623 break; 1624 } 1625 return m; 1626 } 1627 1628 /* 1629 * Send a management frame. The node is for the destination (or ic_bss 1630 * when in station mode). Nodes other than ic_bss have their reference 1631 * count bumped to reflect our use for an indeterminant time. 1632 */ 1633 int 1634 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni, 1635 int type, int arg1, int arg2) 1636 { 1637 #define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0) 1638 struct ifnet *ifp = &ic->ic_if; 1639 struct mbuf *m; 1640 int ret, timer; 1641 1642 if (ni == NULL) 1643 panic("null node"); 1644 1645 /* 1646 * Hold a reference on the node so it doesn't go away until after 1647 * the xmit is complete all the way in the driver. On error we 1648 * will remove our reference. 1649 */ 1650 ieee80211_ref_node(ni); 1651 timer = 0; 1652 switch (type) { 1653 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 1654 if ((m = ieee80211_get_probe_req(ic, ni)) == NULL) 1655 senderr(ENOMEM, is_tx_nombuf); 1656 1657 timer = IEEE80211_TRANS_WAIT; 1658 break; 1659 #ifndef IEEE80211_STA_ONLY 1660 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1661 if ((m = ieee80211_get_probe_resp(ic, ni)) == NULL) 1662 senderr(ENOMEM, is_tx_nombuf); 1663 break; 1664 #endif 1665 case IEEE80211_FC0_SUBTYPE_AUTH: 1666 m = ieee80211_get_auth(ic, ni, arg1 >> 16, arg1 & 0xffff); 1667 if (m == NULL) 1668 senderr(ENOMEM, is_tx_nombuf); 1669 1670 if (ic->ic_opmode == IEEE80211_M_STA) 1671 timer = IEEE80211_TRANS_WAIT; 1672 break; 1673 1674 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1675 if ((m = ieee80211_get_deauth(ic, ni, arg1)) == NULL) 1676 senderr(ENOMEM, is_tx_nombuf); 1677 1678 if (ifp->if_flags & IFF_DEBUG) { 1679 printf("%s: station %s deauthenticate (reason %d)\n", 1680 ifp->if_xname, ether_sprintf(ni->ni_macaddr), 1681 arg1); 1682 } 1683 break; 1684 1685 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1686 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1687 if ((m = ieee80211_get_assoc_req(ic, ni, type)) == NULL) 1688 senderr(ENOMEM, is_tx_nombuf); 1689 1690 timer = IEEE80211_TRANS_WAIT; 1691 break; 1692 #ifndef IEEE80211_STA_ONLY 1693 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 1694 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 1695 if ((m = ieee80211_get_assoc_resp(ic, ni, arg1)) == NULL) 1696 senderr(ENOMEM, is_tx_nombuf); 1697 break; 1698 #endif 1699 case IEEE80211_FC0_SUBTYPE_DISASSOC: 1700 if ((m = ieee80211_get_disassoc(ic, ni, arg1)) == NULL) 1701 senderr(ENOMEM, is_tx_nombuf); 1702 1703 if (ifp->if_flags & IFF_DEBUG) { 1704 printf("%s: station %s disassociate (reason %d)\n", 1705 ifp->if_xname, ether_sprintf(ni->ni_macaddr), 1706 arg1); 1707 } 1708 break; 1709 1710 case IEEE80211_FC0_SUBTYPE_ACTION: 1711 m = ieee80211_get_action(ic, ni, arg1 >> 16, arg1 & 0xffff, 1712 arg2); 1713 if (m == NULL) 1714 senderr(ENOMEM, is_tx_nombuf); 1715 break; 1716 1717 default: 1718 DPRINTF(("invalid mgmt frame type %u\n", type)); 1719 senderr(EINVAL, is_tx_unknownmgt); 1720 /* NOTREACHED */ 1721 } 1722 1723 ret = ieee80211_mgmt_output(ifp, ni, m, type); 1724 if (ret == 0) { 1725 if (timer) 1726 ic->ic_mgt_timer = timer; 1727 } else { 1728 bad: 1729 ieee80211_release_node(ic, ni); 1730 } 1731 return ret; 1732 #undef senderr 1733 } 1734 1735 /* 1736 * Build a RTS (Request To Send) control frame (see 7.2.1.1). 1737 */ 1738 struct mbuf * 1739 ieee80211_get_rts(struct ieee80211com *ic, const struct ieee80211_frame *wh, 1740 u_int16_t dur) 1741 { 1742 struct ieee80211_frame_rts *rts; 1743 struct mbuf *m; 1744 1745 MGETHDR(m, M_DONTWAIT, MT_DATA); 1746 if (m == NULL) 1747 return NULL; 1748 1749 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts); 1750 1751 rts = mtod(m, struct ieee80211_frame_rts *); 1752 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL | 1753 IEEE80211_FC0_SUBTYPE_RTS; 1754 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1755 *(u_int16_t *)rts->i_dur = htole16(dur); 1756 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1); 1757 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2); 1758 1759 return m; 1760 } 1761 1762 /* 1763 * Build a CTS-to-self (Clear To Send) control frame (see 7.2.1.2). 1764 */ 1765 struct mbuf * 1766 ieee80211_get_cts_to_self(struct ieee80211com *ic, u_int16_t dur) 1767 { 1768 struct ieee80211_frame_cts *cts; 1769 struct mbuf *m; 1770 1771 MGETHDR(m, M_DONTWAIT, MT_DATA); 1772 if (m == NULL) 1773 return NULL; 1774 1775 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts); 1776 1777 cts = mtod(m, struct ieee80211_frame_cts *); 1778 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL | 1779 IEEE80211_FC0_SUBTYPE_CTS; 1780 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1781 *(u_int16_t *)cts->i_dur = htole16(dur); 1782 IEEE80211_ADDR_COPY(cts->i_ra, ic->ic_myaddr); 1783 1784 return m; 1785 } 1786 1787 #ifndef IEEE80211_STA_ONLY 1788 /*- 1789 * Beacon frame format: 1790 * [8] Timestamp 1791 * [2] Beacon interval 1792 * [2] Capability 1793 * [tlv] Service Set Identifier (SSID) 1794 * [tlv] Supported rates 1795 * [tlv] DS Parameter Set (802.11g) 1796 * [tlv] IBSS Parameter Set 1797 * [tlv] Traffic Indication Map (TIM) 1798 * [tlv] ERP Information (802.11g) 1799 * [tlv] Extended Supported Rates (802.11g) 1800 * [tlv] RSN (802.11i) 1801 * [tlv] EDCA Parameter Set (802.11e) 1802 * [tlv] HT Capabilities (802.11n) 1803 * [tlv] HT Operation (802.11n) 1804 */ 1805 struct mbuf * 1806 ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni) 1807 { 1808 const struct ieee80211_rateset *rs = &ni->ni_rates; 1809 struct ieee80211_frame *wh; 1810 struct mbuf *m; 1811 u_int8_t *frm; 1812 1813 m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 1814 8 + 2 + 2 + 1815 2 + ((ic->ic_flags & IEEE80211_F_HIDENWID) ? 0 : ni->ni_esslen) + 1816 2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) + 1817 2 + 1 + 1818 2 + ((ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 254) + 1819 ((ic->ic_curmode == IEEE80211_MODE_11G) ? 2 + 1 : 0) + 1820 ((rs->rs_nrates > IEEE80211_RATE_SIZE) ? 1821 2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) + 1822 (((ic->ic_flags & IEEE80211_F_RSNON) && 1823 (ni->ni_rsnprotos & IEEE80211_PROTO_RSN)) ? 1824 2 + IEEE80211_RSNIE_MAXLEN : 0) + 1825 ((ic->ic_flags & IEEE80211_F_QOS) ? 2 + 18 : 0) + 1826 (((ic->ic_flags & IEEE80211_F_RSNON) && 1827 (ni->ni_rsnprotos & IEEE80211_PROTO_WPA)) ? 1828 2 + IEEE80211_WPAIE_MAXLEN : 0) + 1829 ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 24 + 26 : 0)); 1830 if (m == NULL) 1831 return NULL; 1832 1833 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT); 1834 if (m == NULL) 1835 return NULL; 1836 wh = mtod(m, struct ieee80211_frame *); 1837 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 1838 IEEE80211_FC0_SUBTYPE_BEACON; 1839 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1840 *(u_int16_t *)wh->i_dur = 0; 1841 IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr); 1842 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 1843 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid); 1844 *(u_int16_t *)wh->i_seq = 0; 1845 1846 frm = (u_int8_t *)&wh[1]; 1847 memset(frm, 0, 8); frm += 8; /* timestamp is set by hardware */ 1848 LE_WRITE_2(frm, ni->ni_intval); frm += 2; 1849 frm = ieee80211_add_capinfo(frm, ic, ni); 1850 if (ic->ic_flags & IEEE80211_F_HIDENWID) 1851 frm = ieee80211_add_ssid(frm, NULL, 0); 1852 else 1853 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen); 1854 frm = ieee80211_add_rates(frm, rs); 1855 frm = ieee80211_add_ds_params(frm, ic, ni); 1856 if (ic->ic_opmode == IEEE80211_M_IBSS) 1857 frm = ieee80211_add_ibss_params(frm, ni); 1858 else 1859 frm = ieee80211_add_tim(frm, ic); 1860 if (ic->ic_curmode == IEEE80211_MODE_11G) 1861 frm = ieee80211_add_erp(frm, ic); 1862 if (rs->rs_nrates > IEEE80211_RATE_SIZE) 1863 frm = ieee80211_add_xrates(frm, rs); 1864 if ((ic->ic_flags & IEEE80211_F_RSNON) && 1865 (ni->ni_rsnprotos & IEEE80211_PROTO_RSN)) 1866 frm = ieee80211_add_rsn(frm, ic, ni); 1867 if (ic->ic_flags & IEEE80211_F_QOS) 1868 frm = ieee80211_add_edca_params(frm, ic); 1869 if ((ic->ic_flags & IEEE80211_F_RSNON) && 1870 (ni->ni_rsnprotos & IEEE80211_PROTO_WPA)) 1871 frm = ieee80211_add_wpa(frm, ic, ni); 1872 if (ic->ic_flags & IEEE80211_F_HTON) { 1873 frm = ieee80211_add_htcaps(frm, ic); 1874 frm = ieee80211_add_htop(frm, ic); 1875 frm = ieee80211_add_wme_param(frm, ic); 1876 } 1877 1878 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 1879 m->m_pkthdr.ph_cookie = ni; 1880 1881 return m; 1882 } 1883 1884 /* 1885 * Check if an outgoing MSDU or management frame should be buffered into 1886 * the AP for power management. Return 1 if the frame was buffered into 1887 * the AP, or 0 if the frame shall be transmitted immediately. 1888 */ 1889 int 1890 ieee80211_pwrsave(struct ieee80211com *ic, struct mbuf *m, 1891 struct ieee80211_node *ni) 1892 { 1893 const struct ieee80211_frame *wh; 1894 int pssta = 0; 1895 1896 KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP); 1897 if (!(ic->ic_caps & IEEE80211_C_APPMGT)) 1898 return 0; 1899 1900 wh = mtod(m, struct ieee80211_frame *); 1901 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1902 /* 1903 * Buffer group addressed MSDUs with the Order bit clear 1904 * if any associated STAs are in PS mode. 1905 */ 1906 ieee80211_iterate_nodes(ic, ieee80211_count_pssta, &pssta); 1907 if ((wh->i_fc[1] & IEEE80211_FC1_ORDER) || pssta == 0) 1908 return 0; 1909 ic->ic_tim_mcast_pending = 1; 1910 } else { 1911 /* 1912 * Buffer MSDUs, A-MSDUs or management frames destined for 1913 * PS STAs. 1914 */ 1915 if (ni->ni_pwrsave == IEEE80211_PS_AWAKE || 1916 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 1917 IEEE80211_FC0_TYPE_CTL) 1918 return 0; 1919 if (mq_empty(&ni->ni_savedq)) 1920 (*ic->ic_set_tim)(ic, ni->ni_associd, 1); 1921 } 1922 /* NB: ni == ic->ic_bss for broadcast/multicast */ 1923 /* 1924 * Similar to ieee80211_mgmt_output, store the node in a 1925 * special pkthdr field. 1926 */ 1927 m->m_pkthdr.ph_cookie = ni; 1928 mq_enqueue(&ni->ni_savedq, m); 1929 return 1; 1930 } 1931 #endif /* IEEE80211_STA_ONLY */ 1932