ieee80211_superg.c revision 190532
1/*- 2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include <sys/cdefs.h> 27__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_superg.c 190532 2009-03-29 21:17:08Z sam $"); 28 29#include "opt_wlan.h" 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/mbuf.h> 34#include <sys/kernel.h> 35#include <sys/endian.h> 36 37#include <sys/socket.h> 38 39#include <net/bpf.h> 40#include <net/ethernet.h> 41#include <net/if.h> 42#include <net/if_llc.h> 43#include <net/if_media.h> 44 45#include <net80211/ieee80211_var.h> 46#include <net80211/ieee80211_input.h> 47#include <net80211/ieee80211_phy.h> 48#include <net80211/ieee80211_superg.h> 49 50/* 51 * Atheros fast-frame encapsulation format. 52 * FF max payload: 53 * 802.2 + FFHDR + HPAD + 802.3 + 802.2 + 1500 + SPAD + 802.3 + 802.2 + 1500: 54 * 8 + 4 + 4 + 14 + 8 + 1500 + 6 + 14 + 8 + 1500 55 * = 3066 56 */ 57/* fast frame header is 32-bits */ 58#define ATH_FF_PROTO 0x0000003f /* protocol */ 59#define ATH_FF_PROTO_S 0 60#define ATH_FF_FTYPE 0x000000c0 /* frame type */ 61#define ATH_FF_FTYPE_S 6 62#define ATH_FF_HLEN32 0x00000300 /* optional hdr length */ 63#define ATH_FF_HLEN32_S 8 64#define ATH_FF_SEQNUM 0x001ffc00 /* sequence number */ 65#define ATH_FF_SEQNUM_S 10 66#define ATH_FF_OFFSET 0xffe00000 /* offset to 2nd payload */ 67#define ATH_FF_OFFSET_S 21 68 69#define ATH_FF_MAX_HDR_PAD 4 70#define ATH_FF_MAX_SEP_PAD 6 71#define ATH_FF_MAX_HDR 30 72 73#define ATH_FF_PROTO_L2TUNNEL 0 /* L2 tunnel protocol */ 74#define ATH_FF_ETH_TYPE 0x88bd /* Ether type for encapsulated frames */ 75#define ATH_FF_SNAP_ORGCODE_0 0x00 76#define ATH_FF_SNAP_ORGCODE_1 0x03 77#define ATH_FF_SNAP_ORGCODE_2 0x7f 78 79#define ETHER_HEADER_COPY(dst, src) \ 80 memcpy(dst, src, sizeof(struct ether_header)) 81 82void 83ieee80211_superg_attach(struct ieee80211com *ic) 84{ 85} 86 87void 88ieee80211_superg_detach(struct ieee80211com *ic) 89{ 90} 91 92void 93ieee80211_superg_vattach(struct ieee80211vap *vap) 94{ 95 if (vap->iv_caps & IEEE80211_C_FF) 96 vap->iv_flags |= IEEE80211_F_FF; 97 /* NB: we only implement sta mode */ 98 if (vap->iv_opmode == IEEE80211_M_STA && 99 (vap->iv_caps & IEEE80211_C_TURBOP)) 100 vap->iv_flags |= IEEE80211_F_TURBOP; 101} 102 103void 104ieee80211_superg_vdetach(struct ieee80211vap *vap) 105{ 106} 107 108#define ATH_OUI_BYTES 0x00, 0x03, 0x7f 109/* 110 * Add a WME information element to a frame. 111 */ 112uint8_t * 113ieee80211_add_ath(uint8_t *frm, uint8_t caps, ieee80211_keyix defkeyix) 114{ 115 static const struct ieee80211_ath_ie info = { 116 .ath_id = IEEE80211_ELEMID_VENDOR, 117 .ath_len = sizeof(struct ieee80211_ath_ie) - 2, 118 .ath_oui = { ATH_OUI_BYTES }, 119 .ath_oui_type = ATH_OUI_TYPE, 120 .ath_oui_subtype= ATH_OUI_SUBTYPE, 121 .ath_version = ATH_OUI_VERSION, 122 }; 123 struct ieee80211_ath_ie *ath = (struct ieee80211_ath_ie *) frm; 124 125 memcpy(frm, &info, sizeof(info)); 126 ath->ath_capability = caps; 127 if (defkeyix != IEEE80211_KEYIX_NONE) { 128 ath->ath_defkeyix[0] = (defkeyix & 0xff); 129 ath->ath_defkeyix[1] = ((defkeyix >> 8) & 0xff); 130 } else { 131 ath->ath_defkeyix[0] = 0xff; 132 ath->ath_defkeyix[1] = 0x7f; 133 } 134 return frm + sizeof(info); 135} 136#undef ATH_OUI_BYTES 137 138uint8_t * 139ieee80211_add_athcaps(uint8_t *frm, const struct ieee80211_node *bss) 140{ 141 const struct ieee80211vap *vap = bss->ni_vap; 142 143 return ieee80211_add_ath(frm, 144 vap->iv_flags & IEEE80211_F_ATHEROS, 145 ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 146 bss->ni_authmode != IEEE80211_AUTH_8021X) ? 147 vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 148} 149 150void 151ieee80211_parse_ath(struct ieee80211_node *ni, uint8_t *ie) 152{ 153 const struct ieee80211_ath_ie *ath = 154 (const struct ieee80211_ath_ie *) ie; 155 156 ni->ni_ath_flags = ath->ath_capability; 157 ni->ni_ath_defkeyix = LE_READ_2(&ath->ath_defkeyix); 158} 159 160int 161ieee80211_parse_athparams(struct ieee80211_node *ni, uint8_t *frm, 162 const struct ieee80211_frame *wh) 163{ 164 struct ieee80211vap *vap = ni->ni_vap; 165 const struct ieee80211_ath_ie *ath; 166 u_int len = frm[1]; 167 int capschanged; 168 uint16_t defkeyix; 169 170 if (len < sizeof(struct ieee80211_ath_ie)-2) { 171 IEEE80211_DISCARD_IE(vap, 172 IEEE80211_MSG_ELEMID | IEEE80211_MSG_SUPERG, 173 wh, "Atheros", "too short, len %u", len); 174 return -1; 175 } 176 ath = (const struct ieee80211_ath_ie *)frm; 177 capschanged = (ni->ni_ath_flags != ath->ath_capability); 178 defkeyix = LE_READ_2(ath->ath_defkeyix); 179 if (capschanged || defkeyix != ni->ni_ath_defkeyix) { 180 ni->ni_ath_flags = ath->ath_capability; 181 ni->ni_ath_defkeyix = defkeyix; 182 IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 183 "ath ie change: new caps 0x%x defkeyix 0x%x", 184 ni->ni_ath_flags, ni->ni_ath_defkeyix); 185 } 186 if (IEEE80211_ATH_CAP(vap, ni, ATHEROS_CAP_TURBO_PRIME)) { 187 uint16_t curflags, newflags; 188 189 /* 190 * Check for turbo mode switch. Calculate flags 191 * for the new mode and effect the switch. 192 */ 193 newflags = curflags = vap->iv_ic->ic_bsschan->ic_flags; 194 /* NB: BOOST is not in ic_flags, so get it from the ie */ 195 if (ath->ath_capability & ATHEROS_CAP_BOOST) 196 newflags |= IEEE80211_CHAN_TURBO; 197 else 198 newflags &= ~IEEE80211_CHAN_TURBO; 199 if (newflags != curflags) 200 ieee80211_dturbo_switch(vap, newflags); 201 } 202 return capschanged; 203} 204 205/* 206 * Decap the encapsulated frame pair and dispatch the first 207 * for delivery. The second frame is returned for delivery 208 * via the normal path. 209 */ 210struct mbuf * 211ieee80211_ff_decap(struct ieee80211_node *ni, struct mbuf *m) 212{ 213#define FF_LLC_SIZE (sizeof(struct ether_header) + sizeof(struct llc)) 214#define MS(x,f) (((x) & f) >> f##_S) 215 struct ieee80211vap *vap = ni->ni_vap; 216 struct llc *llc; 217 uint32_t ath; 218 struct mbuf *n; 219 int framelen; 220 221 /* NB: we assume caller does this check for us */ 222 KASSERT(IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF), 223 ("ff not negotiated")); 224 /* 225 * Check for fast-frame tunnel encapsulation. 226 */ 227 if (m->m_pkthdr.len < 3*FF_LLC_SIZE) 228 return m; 229 if (m->m_len < FF_LLC_SIZE && 230 (m = m_pullup(m, FF_LLC_SIZE)) == NULL) { 231 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 232 ni->ni_macaddr, "fast-frame", 233 "%s", "m_pullup(llc) failed"); 234 vap->iv_stats.is_rx_tooshort++; 235 return NULL; 236 } 237 llc = (struct llc *)(mtod(m, uint8_t *) + 238 sizeof(struct ether_header)); 239 if (llc->llc_snap.ether_type != htons(ATH_FF_ETH_TYPE)) 240 return m; 241 m_adj(m, FF_LLC_SIZE); 242 m_copydata(m, 0, sizeof(uint32_t), (caddr_t) &ath); 243 if (MS(ath, ATH_FF_PROTO) != ATH_FF_PROTO_L2TUNNEL) { 244 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 245 ni->ni_macaddr, "fast-frame", 246 "unsupport tunnel protocol, header 0x%x", ath); 247 vap->iv_stats.is_ff_badhdr++; 248 m_freem(m); 249 return NULL; 250 } 251 /* NB: skip header and alignment padding */ 252 m_adj(m, roundup(sizeof(uint32_t) - 2, 4) + 2); 253 254 vap->iv_stats.is_ff_decap++; 255 256 /* 257 * Decap the first frame, bust it apart from the 258 * second and deliver; then decap the second frame 259 * and return it to the caller for normal delivery. 260 */ 261 m = ieee80211_decap1(m, &framelen); 262 if (m == NULL) { 263 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 264 ni->ni_macaddr, "fast-frame", "%s", "first decap failed"); 265 vap->iv_stats.is_ff_tooshort++; 266 return NULL; 267 } 268 n = m_split(m, framelen, M_NOWAIT); 269 if (n == NULL) { 270 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 271 ni->ni_macaddr, "fast-frame", 272 "%s", "unable to split encapsulated frames"); 273 vap->iv_stats.is_ff_split++; 274 m_freem(m); /* NB: must reclaim */ 275 return NULL; 276 } 277 /* XXX not right for WDS */ 278 vap->iv_deliver_data(vap, ni, m); /* 1st of pair */ 279 280 /* 281 * Decap second frame. 282 */ 283 m_adj(n, roundup2(framelen, 4) - framelen); /* padding */ 284 n = ieee80211_decap1(n, &framelen); 285 if (n == NULL) { 286 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 287 ni->ni_macaddr, "fast-frame", "%s", "second decap failed"); 288 vap->iv_stats.is_ff_tooshort++; 289 } 290 /* XXX verify framelen against mbuf contents */ 291 return n; /* 2nd delivered by caller */ 292#undef MS 293#undef FF_LLC_SIZE 294} 295 296/* 297 * Do Ethernet-LLC encapsulation for each payload in a fast frame 298 * tunnel encapsulation. The frame is assumed to have an Ethernet 299 * header at the front that must be stripped before prepending the 300 * LLC followed by the Ethernet header passed in (with an Ethernet 301 * type that specifies the payload size). 302 */ 303static struct mbuf * 304ff_encap1(struct ieee80211vap *vap, struct mbuf *m, 305 const struct ether_header *eh) 306{ 307 struct llc *llc; 308 uint16_t payload; 309 310 /* XXX optimize by combining m_adj+M_PREPEND */ 311 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 312 llc = mtod(m, struct llc *); 313 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 314 llc->llc_control = LLC_UI; 315 llc->llc_snap.org_code[0] = 0; 316 llc->llc_snap.org_code[1] = 0; 317 llc->llc_snap.org_code[2] = 0; 318 llc->llc_snap.ether_type = eh->ether_type; 319 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */ 320 321 M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT); 322 if (m == NULL) { /* XXX cannot happen */ 323 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 324 "%s: no space for ether_header\n", __func__); 325 vap->iv_stats.is_tx_nobuf++; 326 return NULL; 327 } 328 ETHER_HEADER_COPY(mtod(m, void *), eh); 329 mtod(m, struct ether_header *)->ether_type = htons(payload); 330 return m; 331} 332 333/* 334 * Fast frame encapsulation. There must be two packets 335 * chained with m_nextpkt. We do header adjustment for 336 * each, add the tunnel encapsulation, and then concatenate 337 * the mbuf chains to form a single frame for transmission. 338 */ 339struct mbuf * 340ieee80211_ff_encap(struct ieee80211vap *vap, struct mbuf *m1, int hdrspace, 341 struct ieee80211_key *key) 342{ 343 struct mbuf *m2; 344 struct ether_header eh1, eh2; 345 struct llc *llc; 346 struct mbuf *m; 347 int pad; 348 349 m2 = m1->m_nextpkt; 350 if (m2 == NULL) { 351 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 352 "%s: only one frame\n", __func__); 353 goto bad; 354 } 355 m1->m_nextpkt = NULL; 356 /* 357 * Include fast frame headers in adjusting header 358 * layout; this allocates space according to what 359 * ff_encap will do. 360 */ 361 m1 = ieee80211_mbuf_adjust(vap, 362 hdrspace + sizeof(struct llc) + sizeof(uint32_t) + 2 + 363 sizeof(struct ether_header), 364 key, m1); 365 if (m1 == NULL) { 366 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 367 m_freem(m2); 368 goto bad; 369 } 370 371 /* 372 * Copy second frame's Ethernet header out of line 373 * and adjust for encapsulation headers. Note that 374 * we make room for padding in case there isn't room 375 * at the end of first frame. 376 */ 377 KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!")); 378 ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t)); 379 m2 = ieee80211_mbuf_adjust(vap, 380 ATH_FF_MAX_HDR_PAD + sizeof(struct ether_header), 381 NULL, m2); 382 if (m2 == NULL) { 383 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 384 goto bad; 385 } 386 387 /* 388 * Now do tunnel encapsulation. First, each 389 * frame gets a standard encapsulation. 390 */ 391 m1 = ff_encap1(vap, m1, &eh1); 392 if (m1 == NULL) 393 goto bad; 394 m2 = ff_encap1(vap, m2, &eh2); 395 if (m2 == NULL) 396 goto bad; 397 398 /* 399 * Pad leading frame to a 4-byte boundary. If there 400 * is space at the end of the first frame, put it 401 * there; otherwise prepend to the front of the second 402 * frame. We know doing the second will always work 403 * because we reserve space above. We prefer appending 404 * as this typically has better DMA alignment properties. 405 */ 406 for (m = m1; m->m_next != NULL; m = m->m_next) 407 ; 408 pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len; 409 if (pad) { 410 if (M_TRAILINGSPACE(m) < pad) { /* prepend to second */ 411 m2->m_data -= pad; 412 m2->m_len += pad; 413 m2->m_pkthdr.len += pad; 414 } else { /* append to first */ 415 m->m_len += pad; 416 m1->m_pkthdr.len += pad; 417 } 418 } 419 420 /* 421 * Now, stick 'em together and prepend the tunnel headers; 422 * first the Atheros tunnel header (all zero for now) and 423 * then a special fast frame LLC. 424 * 425 * XXX optimize by prepending together 426 */ 427 m->m_next = m2; /* NB: last mbuf from above */ 428 m1->m_pkthdr.len += m2->m_pkthdr.len; 429 M_PREPEND(m1, sizeof(uint32_t)+2, M_DONTWAIT); 430 if (m1 == NULL) { /* XXX cannot happen */ 431 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 432 "%s: no space for tunnel header\n", __func__); 433 vap->iv_stats.is_tx_nobuf++; 434 return NULL; 435 } 436 memset(mtod(m1, void *), 0, sizeof(uint32_t)+2); 437 438 M_PREPEND(m1, sizeof(struct llc), M_DONTWAIT); 439 if (m1 == NULL) { /* XXX cannot happen */ 440 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 441 "%s: no space for llc header\n", __func__); 442 vap->iv_stats.is_tx_nobuf++; 443 return NULL; 444 } 445 llc = mtod(m1, struct llc *); 446 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 447 llc->llc_control = LLC_UI; 448 llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0; 449 llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1; 450 llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2; 451 llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE); 452 453 vap->iv_stats.is_ff_encap++; 454 455 return m1; 456bad: 457 if (m1 != NULL) 458 m_freem(m1); 459 if (m2 != NULL) 460 m_freem(m2); 461 return NULL; 462} 463 464/* 465 * Switch between turbo and non-turbo operating modes. 466 * Use the specified channel flags to locate the new 467 * channel, update 802.11 state, and then call back into 468 * the driver to effect the change. 469 */ 470void 471ieee80211_dturbo_switch(struct ieee80211vap *vap, int newflags) 472{ 473 struct ieee80211com *ic = vap->iv_ic; 474 struct ieee80211_channel *chan; 475 476 chan = ieee80211_find_channel(ic, ic->ic_bsschan->ic_freq, newflags); 477 if (chan == NULL) { /* XXX should not happen */ 478 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 479 "%s: no channel with freq %u flags 0x%x\n", 480 __func__, ic->ic_bsschan->ic_freq, newflags); 481 return; 482 } 483 484 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 485 "%s: %s -> %s (freq %u flags 0x%x)\n", __func__, 486 ieee80211_phymode_name[ieee80211_chan2mode(ic->ic_bsschan)], 487 ieee80211_phymode_name[ieee80211_chan2mode(chan)], 488 chan->ic_freq, chan->ic_flags); 489 490 ic->ic_bsschan = chan; 491 ic->ic_prevchan = ic->ic_curchan; 492 ic->ic_curchan = chan; 493 ic->ic_rt = ieee80211_get_ratetable(chan); 494 ic->ic_set_channel(ic); 495 /* NB: do not need to reset ERP state 'cuz we're in sta mode */ 496} 497 498/* 499 * Return the current ``state'' of an Atheros capbility. 500 * If associated in station mode report the negotiated 501 * setting. Otherwise report the current setting. 502 */ 503static int 504getathcap(struct ieee80211vap *vap, int cap) 505{ 506 if (vap->iv_opmode == IEEE80211_M_STA && 507 vap->iv_state == IEEE80211_S_RUN) 508 return IEEE80211_ATH_CAP(vap, vap->iv_bss, cap) != 0; 509 else 510 return (vap->iv_flags & cap) != 0; 511} 512 513static int 514superg_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 515{ 516 switch (ireq->i_type) { 517 case IEEE80211_IOC_FF: 518 ireq->i_val = getathcap(vap, IEEE80211_F_FF); 519 break; 520 case IEEE80211_IOC_TURBOP: 521 ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP); 522 break; 523 default: 524 return ENOSYS; 525 } 526 return 0; 527} 528IEEE80211_IOCTL_GET(superg, superg_ioctl_get80211); 529 530static int 531superg_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 532{ 533 switch (ireq->i_type) { 534 case IEEE80211_IOC_FF: 535 if (ireq->i_val) { 536 if ((vap->iv_caps & IEEE80211_C_FF) == 0) 537 return EOPNOTSUPP; 538 vap->iv_flags |= IEEE80211_F_FF; 539 } else 540 vap->iv_flags &= ~IEEE80211_F_FF; 541 return ENETRESET; 542 case IEEE80211_IOC_TURBOP: 543 if (ireq->i_val) { 544 if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0) 545 return EOPNOTSUPP; 546 vap->iv_flags |= IEEE80211_F_TURBOP; 547 } else 548 vap->iv_flags &= ~IEEE80211_F_TURBOP; 549 return ENETRESET; 550 default: 551 return ENOSYS; 552 } 553 return 0; 554} 555IEEE80211_IOCTL_SET(superg, superg_ioctl_set80211); 556