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