ieee80211_superg.c revision 193115
1190391Ssam/*- 2190391Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 3190391Ssam * All rights reserved. 4190391Ssam * 5190391Ssam * Redistribution and use in source and binary forms, with or without 6190391Ssam * modification, are permitted provided that the following conditions 7190391Ssam * are met: 8190391Ssam * 1. Redistributions of source code must retain the above copyright 9190391Ssam * notice, this list of conditions and the following disclaimer. 10190391Ssam * 2. Redistributions in binary form must reproduce the above copyright 11190391Ssam * notice, this list of conditions and the following disclaimer in the 12190391Ssam * documentation and/or other materials provided with the distribution. 13190391Ssam * 14190391Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15190391Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16190391Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17190391Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18190391Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19190391Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20190391Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21190391Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22190391Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23190391Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24190391Ssam */ 25190391Ssam 26190391Ssam#include <sys/cdefs.h> 27190391Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_superg.c 193115 2009-05-30 20:11:23Z sam $"); 28190391Ssam 29190391Ssam#include "opt_wlan.h" 30190391Ssam 31190391Ssam#include <sys/param.h> 32190391Ssam#include <sys/systm.h> 33190391Ssam#include <sys/mbuf.h> 34190391Ssam#include <sys/kernel.h> 35190391Ssam#include <sys/endian.h> 36190391Ssam 37190391Ssam#include <sys/socket.h> 38190391Ssam 39190391Ssam#include <net/bpf.h> 40190391Ssam#include <net/ethernet.h> 41190391Ssam#include <net/if.h> 42190391Ssam#include <net/if_llc.h> 43190391Ssam#include <net/if_media.h> 44190391Ssam 45190391Ssam#include <net80211/ieee80211_var.h> 46190391Ssam#include <net80211/ieee80211_input.h> 47190391Ssam#include <net80211/ieee80211_phy.h> 48190391Ssam#include <net80211/ieee80211_superg.h> 49190391Ssam 50190455Ssam/* 51190455Ssam * Atheros fast-frame encapsulation format. 52190455Ssam * FF max payload: 53190455Ssam * 802.2 + FFHDR + HPAD + 802.3 + 802.2 + 1500 + SPAD + 802.3 + 802.2 + 1500: 54190455Ssam * 8 + 4 + 4 + 14 + 8 + 1500 + 6 + 14 + 8 + 1500 55190455Ssam * = 3066 56190455Ssam */ 57190455Ssam/* fast frame header is 32-bits */ 58190455Ssam#define ATH_FF_PROTO 0x0000003f /* protocol */ 59190455Ssam#define ATH_FF_PROTO_S 0 60190455Ssam#define ATH_FF_FTYPE 0x000000c0 /* frame type */ 61190455Ssam#define ATH_FF_FTYPE_S 6 62190455Ssam#define ATH_FF_HLEN32 0x00000300 /* optional hdr length */ 63190455Ssam#define ATH_FF_HLEN32_S 8 64190455Ssam#define ATH_FF_SEQNUM 0x001ffc00 /* sequence number */ 65190455Ssam#define ATH_FF_SEQNUM_S 10 66190455Ssam#define ATH_FF_OFFSET 0xffe00000 /* offset to 2nd payload */ 67190455Ssam#define ATH_FF_OFFSET_S 21 68190455Ssam 69190455Ssam#define ATH_FF_MAX_HDR_PAD 4 70190455Ssam#define ATH_FF_MAX_SEP_PAD 6 71190455Ssam#define ATH_FF_MAX_HDR 30 72190455Ssam 73190455Ssam#define ATH_FF_PROTO_L2TUNNEL 0 /* L2 tunnel protocol */ 74190455Ssam#define ATH_FF_ETH_TYPE 0x88bd /* Ether type for encapsulated frames */ 75190455Ssam#define ATH_FF_SNAP_ORGCODE_0 0x00 76190455Ssam#define ATH_FF_SNAP_ORGCODE_1 0x03 77190455Ssam#define ATH_FF_SNAP_ORGCODE_2 0x7f 78190455Ssam 79190579Ssam#define ATH_FF_TXQMIN 2 /* min txq depth for staging */ 80190579Ssam#define ATH_FF_TXQMAX 50 /* maximum # of queued frames allowed */ 81190579Ssam#define ATH_FF_STAGEMAX 5 /* max waiting period for staged frame*/ 82190579Ssam 83190391Ssam#define ETHER_HEADER_COPY(dst, src) \ 84190391Ssam memcpy(dst, src, sizeof(struct ether_header)) 85190391Ssam 86193115Ssamstatic int ieee80211_ffppsmin = 2; /* pps threshold for ff aggregation */ 87193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, ffppsmin, CTLTYPE_INT | CTLFLAG_RW, 88193115Ssam &ieee80211_ffppsmin, 0, "min packet rate before fast-frame staging"); 89193115Ssamstatic int ieee80211_ffagemax = -1; /* max time frames held on stage q */ 90193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, ffagemax, CTLTYPE_INT | CTLFLAG_RW, 91193115Ssam &ieee80211_ffagemax, 0, ieee80211_sysctl_msecs_ticks, "I", 92193115Ssam "max hold time for fast-frame staging (ms)"); 93190579Ssam 94190391Ssamvoid 95190391Ssamieee80211_superg_attach(struct ieee80211com *ic) 96190391Ssam{ 97191753Ssam struct ieee80211_superg *sg; 98191753Ssam 99191753Ssam if (ic->ic_caps & IEEE80211_C_FF) { 100191753Ssam sg = (struct ieee80211_superg *) malloc( 101191753Ssam sizeof(struct ieee80211_superg), M_80211_VAP, 102191753Ssam M_NOWAIT | M_ZERO); 103191753Ssam if (sg == NULL) { 104191753Ssam printf("%s: cannot allocate SuperG state block\n", 105191753Ssam __func__); 106191753Ssam return; 107191753Ssam } 108191753Ssam ic->ic_superg = sg; 109191753Ssam } 110190579Ssam ieee80211_ffagemax = msecs_to_ticks(150); 111190391Ssam} 112190391Ssam 113190391Ssamvoid 114190391Ssamieee80211_superg_detach(struct ieee80211com *ic) 115190391Ssam{ 116191753Ssam if (ic->ic_superg != NULL) { 117191753Ssam free(ic->ic_superg, M_80211_VAP); 118191753Ssam ic->ic_superg = NULL; 119191753Ssam } 120190391Ssam} 121190391Ssam 122190391Ssamvoid 123190391Ssamieee80211_superg_vattach(struct ieee80211vap *vap) 124190391Ssam{ 125191753Ssam struct ieee80211com *ic = vap->iv_ic; 126191753Ssam 127191753Ssam if (ic->ic_superg == NULL) /* NB: can't do fast-frames w/o state */ 128191753Ssam vap->iv_caps &= ~IEEE80211_C_FF; 129190391Ssam if (vap->iv_caps & IEEE80211_C_FF) 130190391Ssam vap->iv_flags |= IEEE80211_F_FF; 131190450Ssam /* NB: we only implement sta mode */ 132190450Ssam if (vap->iv_opmode == IEEE80211_M_STA && 133190450Ssam (vap->iv_caps & IEEE80211_C_TURBOP)) 134190391Ssam vap->iv_flags |= IEEE80211_F_TURBOP; 135190391Ssam} 136190391Ssam 137190391Ssamvoid 138190391Ssamieee80211_superg_vdetach(struct ieee80211vap *vap) 139190391Ssam{ 140190391Ssam} 141190391Ssam 142190391Ssam#define ATH_OUI_BYTES 0x00, 0x03, 0x7f 143190391Ssam/* 144190391Ssam * Add a WME information element to a frame. 145190391Ssam */ 146190391Ssamuint8_t * 147190451Ssamieee80211_add_ath(uint8_t *frm, uint8_t caps, ieee80211_keyix defkeyix) 148190391Ssam{ 149190391Ssam static const struct ieee80211_ath_ie info = { 150190391Ssam .ath_id = IEEE80211_ELEMID_VENDOR, 151190391Ssam .ath_len = sizeof(struct ieee80211_ath_ie) - 2, 152190391Ssam .ath_oui = { ATH_OUI_BYTES }, 153190391Ssam .ath_oui_type = ATH_OUI_TYPE, 154190391Ssam .ath_oui_subtype= ATH_OUI_SUBTYPE, 155190391Ssam .ath_version = ATH_OUI_VERSION, 156190391Ssam }; 157190391Ssam struct ieee80211_ath_ie *ath = (struct ieee80211_ath_ie *) frm; 158190391Ssam 159190391Ssam memcpy(frm, &info, sizeof(info)); 160190391Ssam ath->ath_capability = caps; 161190451Ssam if (defkeyix != IEEE80211_KEYIX_NONE) { 162190451Ssam ath->ath_defkeyix[0] = (defkeyix & 0xff); 163190451Ssam ath->ath_defkeyix[1] = ((defkeyix >> 8) & 0xff); 164190451Ssam } else { 165190451Ssam ath->ath_defkeyix[0] = 0xff; 166190451Ssam ath->ath_defkeyix[1] = 0x7f; 167190451Ssam } 168190391Ssam return frm + sizeof(info); 169190391Ssam} 170190391Ssam#undef ATH_OUI_BYTES 171190391Ssam 172190451Ssamuint8_t * 173190451Ssamieee80211_add_athcaps(uint8_t *frm, const struct ieee80211_node *bss) 174190451Ssam{ 175190451Ssam const struct ieee80211vap *vap = bss->ni_vap; 176190451Ssam 177190451Ssam return ieee80211_add_ath(frm, 178190451Ssam vap->iv_flags & IEEE80211_F_ATHEROS, 179190451Ssam ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 180190451Ssam bss->ni_authmode != IEEE80211_AUTH_8021X) ? 181190451Ssam vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 182190451Ssam} 183190451Ssam 184190391Ssamvoid 185190391Ssamieee80211_parse_ath(struct ieee80211_node *ni, uint8_t *ie) 186190391Ssam{ 187190391Ssam const struct ieee80211_ath_ie *ath = 188190391Ssam (const struct ieee80211_ath_ie *) ie; 189190391Ssam 190190391Ssam ni->ni_ath_flags = ath->ath_capability; 191190391Ssam ni->ni_ath_defkeyix = LE_READ_2(&ath->ath_defkeyix); 192190391Ssam} 193190391Ssam 194190391Ssamint 195190391Ssamieee80211_parse_athparams(struct ieee80211_node *ni, uint8_t *frm, 196190391Ssam const struct ieee80211_frame *wh) 197190391Ssam{ 198190391Ssam struct ieee80211vap *vap = ni->ni_vap; 199190391Ssam const struct ieee80211_ath_ie *ath; 200190391Ssam u_int len = frm[1]; 201190391Ssam int capschanged; 202190391Ssam uint16_t defkeyix; 203190391Ssam 204190391Ssam if (len < sizeof(struct ieee80211_ath_ie)-2) { 205190391Ssam IEEE80211_DISCARD_IE(vap, 206190391Ssam IEEE80211_MSG_ELEMID | IEEE80211_MSG_SUPERG, 207190391Ssam wh, "Atheros", "too short, len %u", len); 208190391Ssam return -1; 209190391Ssam } 210190391Ssam ath = (const struct ieee80211_ath_ie *)frm; 211190391Ssam capschanged = (ni->ni_ath_flags != ath->ath_capability); 212190391Ssam defkeyix = LE_READ_2(ath->ath_defkeyix); 213190391Ssam if (capschanged || defkeyix != ni->ni_ath_defkeyix) { 214190391Ssam ni->ni_ath_flags = ath->ath_capability; 215190391Ssam ni->ni_ath_defkeyix = defkeyix; 216190391Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 217190391Ssam "ath ie change: new caps 0x%x defkeyix 0x%x", 218190391Ssam ni->ni_ath_flags, ni->ni_ath_defkeyix); 219190391Ssam } 220190391Ssam if (IEEE80211_ATH_CAP(vap, ni, ATHEROS_CAP_TURBO_PRIME)) { 221190391Ssam uint16_t curflags, newflags; 222190391Ssam 223190391Ssam /* 224190391Ssam * Check for turbo mode switch. Calculate flags 225190391Ssam * for the new mode and effect the switch. 226190391Ssam */ 227190391Ssam newflags = curflags = vap->iv_ic->ic_bsschan->ic_flags; 228190391Ssam /* NB: BOOST is not in ic_flags, so get it from the ie */ 229190391Ssam if (ath->ath_capability & ATHEROS_CAP_BOOST) 230190391Ssam newflags |= IEEE80211_CHAN_TURBO; 231190391Ssam else 232190391Ssam newflags &= ~IEEE80211_CHAN_TURBO; 233190391Ssam if (newflags != curflags) 234190391Ssam ieee80211_dturbo_switch(vap, newflags); 235190391Ssam } 236190391Ssam return capschanged; 237190391Ssam} 238190391Ssam 239190391Ssam/* 240190391Ssam * Decap the encapsulated frame pair and dispatch the first 241190391Ssam * for delivery. The second frame is returned for delivery 242190391Ssam * via the normal path. 243190391Ssam */ 244190391Ssamstruct mbuf * 245190391Ssamieee80211_ff_decap(struct ieee80211_node *ni, struct mbuf *m) 246190391Ssam{ 247190391Ssam#define FF_LLC_SIZE (sizeof(struct ether_header) + sizeof(struct llc)) 248190391Ssam#define MS(x,f) (((x) & f) >> f##_S) 249190391Ssam struct ieee80211vap *vap = ni->ni_vap; 250190391Ssam struct llc *llc; 251190391Ssam uint32_t ath; 252190391Ssam struct mbuf *n; 253190391Ssam int framelen; 254190391Ssam 255190391Ssam /* NB: we assume caller does this check for us */ 256190391Ssam KASSERT(IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF), 257190391Ssam ("ff not negotiated")); 258190391Ssam /* 259190391Ssam * Check for fast-frame tunnel encapsulation. 260190391Ssam */ 261190391Ssam if (m->m_pkthdr.len < 3*FF_LLC_SIZE) 262190391Ssam return m; 263190391Ssam if (m->m_len < FF_LLC_SIZE && 264190391Ssam (m = m_pullup(m, FF_LLC_SIZE)) == NULL) { 265190391Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 266190391Ssam ni->ni_macaddr, "fast-frame", 267190391Ssam "%s", "m_pullup(llc) failed"); 268190391Ssam vap->iv_stats.is_rx_tooshort++; 269190391Ssam return NULL; 270190391Ssam } 271190391Ssam llc = (struct llc *)(mtod(m, uint8_t *) + 272190391Ssam sizeof(struct ether_header)); 273190391Ssam if (llc->llc_snap.ether_type != htons(ATH_FF_ETH_TYPE)) 274190391Ssam return m; 275190391Ssam m_adj(m, FF_LLC_SIZE); 276190391Ssam m_copydata(m, 0, sizeof(uint32_t), (caddr_t) &ath); 277190391Ssam if (MS(ath, ATH_FF_PROTO) != ATH_FF_PROTO_L2TUNNEL) { 278190391Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 279190391Ssam ni->ni_macaddr, "fast-frame", 280190391Ssam "unsupport tunnel protocol, header 0x%x", ath); 281190391Ssam vap->iv_stats.is_ff_badhdr++; 282190391Ssam m_freem(m); 283190391Ssam return NULL; 284190391Ssam } 285190391Ssam /* NB: skip header and alignment padding */ 286190391Ssam m_adj(m, roundup(sizeof(uint32_t) - 2, 4) + 2); 287190391Ssam 288190391Ssam vap->iv_stats.is_ff_decap++; 289190391Ssam 290190391Ssam /* 291190391Ssam * Decap the first frame, bust it apart from the 292190391Ssam * second and deliver; then decap the second frame 293190391Ssam * and return it to the caller for normal delivery. 294190391Ssam */ 295190391Ssam m = ieee80211_decap1(m, &framelen); 296190391Ssam if (m == NULL) { 297190391Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 298190391Ssam ni->ni_macaddr, "fast-frame", "%s", "first decap failed"); 299190391Ssam vap->iv_stats.is_ff_tooshort++; 300190391Ssam return NULL; 301190391Ssam } 302190391Ssam n = m_split(m, framelen, M_NOWAIT); 303190391Ssam if (n == NULL) { 304190391Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 305190391Ssam ni->ni_macaddr, "fast-frame", 306190391Ssam "%s", "unable to split encapsulated frames"); 307190391Ssam vap->iv_stats.is_ff_split++; 308190391Ssam m_freem(m); /* NB: must reclaim */ 309190391Ssam return NULL; 310190391Ssam } 311190391Ssam /* XXX not right for WDS */ 312190391Ssam vap->iv_deliver_data(vap, ni, m); /* 1st of pair */ 313190391Ssam 314190391Ssam /* 315190391Ssam * Decap second frame. 316190391Ssam */ 317190391Ssam m_adj(n, roundup2(framelen, 4) - framelen); /* padding */ 318190391Ssam n = ieee80211_decap1(n, &framelen); 319190391Ssam if (n == NULL) { 320190391Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 321190391Ssam ni->ni_macaddr, "fast-frame", "%s", "second decap failed"); 322190391Ssam vap->iv_stats.is_ff_tooshort++; 323190391Ssam } 324190391Ssam /* XXX verify framelen against mbuf contents */ 325190391Ssam return n; /* 2nd delivered by caller */ 326190391Ssam#undef MS 327190391Ssam#undef FF_LLC_SIZE 328190391Ssam} 329190391Ssam 330190391Ssam/* 331190391Ssam * Do Ethernet-LLC encapsulation for each payload in a fast frame 332190391Ssam * tunnel encapsulation. The frame is assumed to have an Ethernet 333190391Ssam * header at the front that must be stripped before prepending the 334190391Ssam * LLC followed by the Ethernet header passed in (with an Ethernet 335190391Ssam * type that specifies the payload size). 336190391Ssam */ 337190391Ssamstatic struct mbuf * 338190391Ssamff_encap1(struct ieee80211vap *vap, struct mbuf *m, 339190391Ssam const struct ether_header *eh) 340190391Ssam{ 341190391Ssam struct llc *llc; 342190391Ssam uint16_t payload; 343190391Ssam 344190391Ssam /* XXX optimize by combining m_adj+M_PREPEND */ 345190391Ssam m_adj(m, sizeof(struct ether_header) - sizeof(struct llc)); 346190391Ssam llc = mtod(m, struct llc *); 347190391Ssam llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 348190391Ssam llc->llc_control = LLC_UI; 349190391Ssam llc->llc_snap.org_code[0] = 0; 350190391Ssam llc->llc_snap.org_code[1] = 0; 351190391Ssam llc->llc_snap.org_code[2] = 0; 352190391Ssam llc->llc_snap.ether_type = eh->ether_type; 353190391Ssam payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */ 354190391Ssam 355190391Ssam M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT); 356190391Ssam if (m == NULL) { /* XXX cannot happen */ 357190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 358190391Ssam "%s: no space for ether_header\n", __func__); 359190391Ssam vap->iv_stats.is_tx_nobuf++; 360190391Ssam return NULL; 361190391Ssam } 362190391Ssam ETHER_HEADER_COPY(mtod(m, void *), eh); 363190391Ssam mtod(m, struct ether_header *)->ether_type = htons(payload); 364190391Ssam return m; 365190391Ssam} 366190391Ssam 367190391Ssam/* 368190391Ssam * Fast frame encapsulation. There must be two packets 369190391Ssam * chained with m_nextpkt. We do header adjustment for 370190391Ssam * each, add the tunnel encapsulation, and then concatenate 371190391Ssam * the mbuf chains to form a single frame for transmission. 372190391Ssam */ 373190391Ssamstruct mbuf * 374190391Ssamieee80211_ff_encap(struct ieee80211vap *vap, struct mbuf *m1, int hdrspace, 375190391Ssam struct ieee80211_key *key) 376190391Ssam{ 377190391Ssam struct mbuf *m2; 378190391Ssam struct ether_header eh1, eh2; 379190391Ssam struct llc *llc; 380190391Ssam struct mbuf *m; 381190391Ssam int pad; 382190391Ssam 383190391Ssam m2 = m1->m_nextpkt; 384190391Ssam if (m2 == NULL) { 385190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 386190391Ssam "%s: only one frame\n", __func__); 387190391Ssam goto bad; 388190391Ssam } 389190391Ssam m1->m_nextpkt = NULL; 390190391Ssam /* 391190579Ssam * Include fast frame headers in adjusting header layout. 392190391Ssam */ 393190579Ssam KASSERT(m1->m_len >= sizeof(eh1), ("no ethernet header!")); 394190579Ssam ETHER_HEADER_COPY(&eh1, mtod(m1, caddr_t)); 395190391Ssam m1 = ieee80211_mbuf_adjust(vap, 396190391Ssam hdrspace + sizeof(struct llc) + sizeof(uint32_t) + 2 + 397190391Ssam sizeof(struct ether_header), 398190391Ssam key, m1); 399190391Ssam if (m1 == NULL) { 400190391Ssam /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 401190391Ssam m_freem(m2); 402190391Ssam goto bad; 403190391Ssam } 404190391Ssam 405190391Ssam /* 406190391Ssam * Copy second frame's Ethernet header out of line 407190391Ssam * and adjust for encapsulation headers. Note that 408190391Ssam * we make room for padding in case there isn't room 409190391Ssam * at the end of first frame. 410190391Ssam */ 411190391Ssam KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!")); 412190391Ssam ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t)); 413190391Ssam m2 = ieee80211_mbuf_adjust(vap, 414190391Ssam ATH_FF_MAX_HDR_PAD + sizeof(struct ether_header), 415190391Ssam NULL, m2); 416190391Ssam if (m2 == NULL) { 417190391Ssam /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 418190391Ssam goto bad; 419190391Ssam } 420190391Ssam 421190391Ssam /* 422190391Ssam * Now do tunnel encapsulation. First, each 423190391Ssam * frame gets a standard encapsulation. 424190391Ssam */ 425190391Ssam m1 = ff_encap1(vap, m1, &eh1); 426190391Ssam if (m1 == NULL) 427190391Ssam goto bad; 428190391Ssam m2 = ff_encap1(vap, m2, &eh2); 429190391Ssam if (m2 == NULL) 430190391Ssam goto bad; 431190391Ssam 432190391Ssam /* 433190391Ssam * Pad leading frame to a 4-byte boundary. If there 434190391Ssam * is space at the end of the first frame, put it 435190391Ssam * there; otherwise prepend to the front of the second 436190391Ssam * frame. We know doing the second will always work 437190391Ssam * because we reserve space above. We prefer appending 438190391Ssam * as this typically has better DMA alignment properties. 439190391Ssam */ 440190391Ssam for (m = m1; m->m_next != NULL; m = m->m_next) 441190391Ssam ; 442190391Ssam pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len; 443190391Ssam if (pad) { 444190391Ssam if (M_TRAILINGSPACE(m) < pad) { /* prepend to second */ 445190391Ssam m2->m_data -= pad; 446190391Ssam m2->m_len += pad; 447190391Ssam m2->m_pkthdr.len += pad; 448190391Ssam } else { /* append to first */ 449190391Ssam m->m_len += pad; 450190391Ssam m1->m_pkthdr.len += pad; 451190391Ssam } 452190391Ssam } 453190391Ssam 454190391Ssam /* 455190391Ssam * Now, stick 'em together and prepend the tunnel headers; 456190391Ssam * first the Atheros tunnel header (all zero for now) and 457190391Ssam * then a special fast frame LLC. 458190391Ssam * 459190391Ssam * XXX optimize by prepending together 460190391Ssam */ 461190391Ssam m->m_next = m2; /* NB: last mbuf from above */ 462190391Ssam m1->m_pkthdr.len += m2->m_pkthdr.len; 463190391Ssam M_PREPEND(m1, sizeof(uint32_t)+2, M_DONTWAIT); 464190391Ssam if (m1 == NULL) { /* XXX cannot happen */ 465190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 466190391Ssam "%s: no space for tunnel header\n", __func__); 467190391Ssam vap->iv_stats.is_tx_nobuf++; 468190391Ssam return NULL; 469190391Ssam } 470190391Ssam memset(mtod(m1, void *), 0, sizeof(uint32_t)+2); 471190391Ssam 472190391Ssam M_PREPEND(m1, sizeof(struct llc), M_DONTWAIT); 473190391Ssam if (m1 == NULL) { /* XXX cannot happen */ 474190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 475190391Ssam "%s: no space for llc header\n", __func__); 476190391Ssam vap->iv_stats.is_tx_nobuf++; 477190391Ssam return NULL; 478190391Ssam } 479190391Ssam llc = mtod(m1, struct llc *); 480190391Ssam llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 481190391Ssam llc->llc_control = LLC_UI; 482190391Ssam llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0; 483190391Ssam llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1; 484190391Ssam llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2; 485190391Ssam llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE); 486190391Ssam 487190391Ssam vap->iv_stats.is_ff_encap++; 488190391Ssam 489190391Ssam return m1; 490190391Ssambad: 491190391Ssam if (m1 != NULL) 492190391Ssam m_freem(m1); 493190391Ssam if (m2 != NULL) 494190391Ssam m_freem(m2); 495190391Ssam return NULL; 496190391Ssam} 497190391Ssam 498190579Ssamstatic void 499190579Ssamff_transmit(struct ieee80211_node *ni, struct mbuf *m) 500190579Ssam{ 501190579Ssam struct ieee80211vap *vap = ni->ni_vap; 502190579Ssam int error; 503190579Ssam 504190579Ssam /* encap and xmit */ 505190579Ssam m = ieee80211_encap(vap, ni, m); 506190579Ssam if (m != NULL) { 507190579Ssam struct ifnet *ifp = vap->iv_ifp; 508190579Ssam struct ifnet *parent = ni->ni_ic->ic_ifp; 509190579Ssam 510190579Ssam error = parent->if_transmit(parent, m); 511190579Ssam if (error != 0) { 512190579Ssam /* NB: IFQ_HANDOFF reclaims mbuf */ 513190579Ssam ieee80211_free_node(ni); 514190579Ssam } else { 515190579Ssam ifp->if_opackets++; 516190579Ssam } 517190579Ssam } else 518190579Ssam ieee80211_free_node(ni); 519190579Ssam} 520190579Ssam 521190391Ssam/* 522190579Ssam * Flush frames to device; note we re-use the linked list 523190579Ssam * the frames were stored on and use the sentinel (unchanged) 524190579Ssam * which may be non-NULL. 525190579Ssam */ 526190579Ssamstatic void 527190579Ssamff_flush(struct mbuf *head, struct mbuf *last) 528190579Ssam{ 529190579Ssam struct mbuf *m, *next; 530190579Ssam struct ieee80211_node *ni; 531190579Ssam struct ieee80211vap *vap; 532190579Ssam 533190579Ssam for (m = head; m != last; m = next) { 534190579Ssam next = m->m_nextpkt; 535190579Ssam m->m_nextpkt = NULL; 536190579Ssam 537190579Ssam ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 538190579Ssam vap = ni->ni_vap; 539190579Ssam 540190579Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 541190579Ssam "%s: flush frame, age %u", __func__, M_AGE_GET(m)); 542190579Ssam vap->iv_stats.is_ff_flush++; 543190579Ssam 544190579Ssam ff_transmit(ni, m); 545190579Ssam } 546190579Ssam} 547190579Ssam 548190579Ssam/* 549190579Ssam * Age frames on the staging queue. 550190579Ssam */ 551190579Ssamvoid 552191753Ssamieee80211_ff_age(struct ieee80211com *ic, struct ieee80211_stageq *sq, 553191753Ssam int quanta) 554190579Ssam{ 555191753Ssam struct ieee80211_superg *sg = ic->ic_superg; 556190579Ssam struct mbuf *m, *head; 557190579Ssam struct ieee80211_node *ni; 558190579Ssam struct ieee80211_tx_ampdu *tap; 559190579Ssam 560190579Ssam KASSERT(sq->head != NULL, ("stageq empty")); 561190579Ssam 562190579Ssam IEEE80211_LOCK(ic); 563190579Ssam head = sq->head; 564190579Ssam while ((m = sq->head) != NULL && M_AGE_GET(m) < quanta) { 565190579Ssam /* clear tap ref to frame */ 566190579Ssam ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 567190579Ssam tap = &ni->ni_tx_ampdu[M_WME_GETAC(m)]; 568190579Ssam KASSERT(tap->txa_private == m, ("staging queue empty")); 569190579Ssam tap->txa_private = NULL; 570190579Ssam 571190579Ssam sq->head = m->m_nextpkt; 572190579Ssam sq->depth--; 573191753Ssam sg->ff_stageqdepth--; 574190579Ssam } 575190579Ssam if (m == NULL) 576190579Ssam sq->tail = NULL; 577190579Ssam else 578190579Ssam M_AGE_SUB(m, quanta); 579190579Ssam IEEE80211_UNLOCK(ic); 580190579Ssam 581190579Ssam ff_flush(head, m); 582190579Ssam} 583190579Ssam 584190579Ssamstatic void 585190579Ssamstageq_add(struct ieee80211_stageq *sq, struct mbuf *m) 586190579Ssam{ 587190579Ssam int age = ieee80211_ffagemax; 588190579Ssam if (sq->tail != NULL) { 589190579Ssam sq->tail->m_nextpkt = m; 590190579Ssam age -= M_AGE_GET(sq->head); 591190579Ssam } else 592190579Ssam sq->head = m; 593190579Ssam KASSERT(age >= 0, ("age %d", age)); 594190579Ssam M_AGE_SET(m, age); 595190579Ssam m->m_nextpkt = NULL; 596190579Ssam sq->tail = m; 597190579Ssam sq->depth++; 598190579Ssam} 599190579Ssam 600190579Ssamstatic void 601190579Ssamstageq_remove(struct ieee80211_stageq *sq, struct mbuf *mstaged) 602190579Ssam{ 603190579Ssam struct mbuf *m, *mprev; 604190579Ssam 605190579Ssam mprev = NULL; 606190579Ssam for (m = sq->head; m != NULL; m = m->m_nextpkt) { 607190579Ssam if (m == mstaged) { 608190579Ssam if (mprev == NULL) 609190579Ssam sq->head = m->m_nextpkt; 610190579Ssam else 611190579Ssam mprev->m_nextpkt = m->m_nextpkt; 612190579Ssam if (sq->tail == m) 613190579Ssam sq->tail = mprev; 614190579Ssam sq->depth--; 615190579Ssam return; 616190579Ssam } 617190579Ssam mprev = m; 618190579Ssam } 619190579Ssam printf("%s: packet not found\n", __func__); 620190579Ssam} 621190579Ssam 622190579Ssamstatic uint32_t 623190579Ssamff_approx_txtime(struct ieee80211_node *ni, 624190579Ssam const struct mbuf *m1, const struct mbuf *m2) 625190579Ssam{ 626190579Ssam struct ieee80211com *ic = ni->ni_ic; 627190579Ssam struct ieee80211vap *vap = ni->ni_vap; 628190579Ssam uint32_t framelen; 629190579Ssam 630190579Ssam /* 631190579Ssam * Approximate the frame length to be transmitted. A swag to add 632190579Ssam * the following maximal values to the skb payload: 633190579Ssam * - 32: 802.11 encap + CRC 634190579Ssam * - 24: encryption overhead (if wep bit) 635190579Ssam * - 4 + 6: fast-frame header and padding 636190579Ssam * - 16: 2 LLC FF tunnel headers 637190579Ssam * - 14: 1 802.3 FF tunnel header (mbuf already accounts for 2nd) 638190579Ssam */ 639190579Ssam framelen = m1->m_pkthdr.len + 32 + 640190579Ssam ATH_FF_MAX_HDR_PAD + ATH_FF_MAX_SEP_PAD + ATH_FF_MAX_HDR; 641190579Ssam if (vap->iv_flags & IEEE80211_F_PRIVACY) 642190579Ssam framelen += 24; 643190579Ssam if (m2 != NULL) 644190579Ssam framelen += m2->m_pkthdr.len; 645190579Ssam return ieee80211_compute_duration(ic->ic_rt, framelen, ni->ni_txrate, 0); 646190579Ssam} 647190579Ssam 648190579Ssam/* 649190579Ssam * Check if the supplied frame can be partnered with an existing 650190579Ssam * or pending frame. Return a reference to any frame that should be 651190579Ssam * sent on return; otherwise return NULL. 652190579Ssam */ 653190579Ssamstruct mbuf * 654190579Ssamieee80211_ff_check(struct ieee80211_node *ni, struct mbuf *m) 655190579Ssam{ 656190579Ssam struct ieee80211vap *vap = ni->ni_vap; 657190579Ssam struct ieee80211com *ic = ni->ni_ic; 658191753Ssam struct ieee80211_superg *sg = ic->ic_superg; 659190579Ssam const int pri = M_WME_GETAC(m); 660190579Ssam struct ieee80211_stageq *sq; 661190579Ssam struct ieee80211_tx_ampdu *tap; 662190579Ssam struct mbuf *mstaged; 663190579Ssam uint32_t txtime, limit; 664190579Ssam 665190579Ssam /* 666190579Ssam * Check if the supplied frame can be aggregated. 667190579Ssam * 668190579Ssam * NB: we allow EAPOL frames to be aggregated with other ucast traffic. 669190579Ssam * Do 802.1x EAPOL frames proceed in the clear? Then they couldn't 670190579Ssam * be aggregated with other types of frames when encryption is on? 671190579Ssam */ 672190579Ssam IEEE80211_LOCK(ic); 673190579Ssam tap = &ni->ni_tx_ampdu[pri]; 674190579Ssam mstaged = tap->txa_private; /* NB: we reuse AMPDU state */ 675190579Ssam ieee80211_txampdu_count_packet(tap); 676190579Ssam 677190579Ssam /* 678190579Ssam * When not in station mode never aggregate a multicast 679190579Ssam * frame; this insures, for example, that a combined frame 680190579Ssam * does not require multiple encryption keys. 681190579Ssam */ 682190579Ssam if (vap->iv_opmode != IEEE80211_M_STA && 683190579Ssam ETHER_IS_MULTICAST(mtod(m, struct ether_header *)->ether_dhost)) { 684190579Ssam /* XXX flush staged frame? */ 685190579Ssam IEEE80211_UNLOCK(ic); 686190579Ssam return m; 687190579Ssam } 688190579Ssam /* 689190579Ssam * If there is no frame to combine with and the pps is 690190579Ssam * too low; then do not attempt to aggregate this frame. 691190579Ssam */ 692190579Ssam if (mstaged == NULL && 693190579Ssam ieee80211_txampdu_getpps(tap) < ieee80211_ffppsmin) { 694190579Ssam IEEE80211_UNLOCK(ic); 695190579Ssam return m; 696190579Ssam } 697191753Ssam sq = &sg->ff_stageq[pri]; 698190579Ssam /* 699190579Ssam * Check the txop limit to insure the aggregate fits. 700190579Ssam */ 701190579Ssam limit = IEEE80211_TXOP_TO_US( 702190579Ssam ic->ic_wme.wme_chanParams.cap_wmeParams[pri].wmep_txopLimit); 703190579Ssam if (limit != 0 && 704190579Ssam (txtime = ff_approx_txtime(ni, m, mstaged)) > limit) { 705190579Ssam /* 706190579Ssam * Aggregate too long, return to the caller for direct 707190579Ssam * transmission. In addition, flush any pending frame 708190579Ssam * before sending this one. 709190579Ssam */ 710190579Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 711190579Ssam "%s: txtime %u exceeds txop limit %u\n", 712190579Ssam __func__, txtime, limit); 713190579Ssam 714190579Ssam tap->txa_private = NULL; 715190579Ssam if (mstaged != NULL) 716190579Ssam stageq_remove(sq, mstaged); 717190579Ssam IEEE80211_UNLOCK(ic); 718190579Ssam 719190579Ssam if (mstaged != NULL) { 720190579Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 721190579Ssam "%s: flush staged frame", __func__); 722190579Ssam /* encap and xmit */ 723190579Ssam ff_transmit(ni, mstaged); 724190579Ssam } 725190579Ssam return m; /* NB: original frame */ 726190579Ssam } 727190579Ssam /* 728190579Ssam * An aggregation candidate. If there's a frame to partner 729190579Ssam * with then combine and return for processing. Otherwise 730190579Ssam * save this frame and wait for a partner to show up (or 731190579Ssam * the frame to be flushed). Note that staged frames also 732190579Ssam * hold their node reference. 733190579Ssam */ 734190579Ssam if (mstaged != NULL) { 735190579Ssam tap->txa_private = NULL; 736190579Ssam stageq_remove(sq, mstaged); 737190579Ssam IEEE80211_UNLOCK(ic); 738190579Ssam 739190579Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 740190579Ssam "%s: aggregate fast-frame", __func__); 741190579Ssam /* 742190579Ssam * Release the node reference; we only need 743190579Ssam * the one already in mstaged. 744190579Ssam */ 745190579Ssam KASSERT(mstaged->m_pkthdr.rcvif == (void *)ni, 746190579Ssam ("rcvif %p ni %p", mstaged->m_pkthdr.rcvif, ni)); 747190579Ssam ieee80211_free_node(ni); 748190579Ssam 749190579Ssam m->m_nextpkt = NULL; 750190579Ssam mstaged->m_nextpkt = m; 751190579Ssam mstaged->m_flags |= M_FF; /* NB: mark for encap work */ 752190579Ssam } else { 753190579Ssam KASSERT(tap->txa_private == NULL, 754190579Ssam ("txa_private %p", tap->txa_private)); 755190579Ssam tap->txa_private = m; 756190579Ssam 757190579Ssam stageq_add(sq, m); 758191753Ssam sg->ff_stageqdepth++; 759190579Ssam IEEE80211_UNLOCK(ic); 760190579Ssam 761190579Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 762190579Ssam "%s: stage frame, %u queued", __func__, sq->depth); 763190579Ssam /* NB: mstaged is NULL */ 764190579Ssam } 765190579Ssam return mstaged; 766190579Ssam} 767190579Ssam 768190579Ssamvoid 769190579Ssamieee80211_ff_node_init(struct ieee80211_node *ni) 770190579Ssam{ 771190579Ssam /* 772190579Ssam * Clean FF state on re-associate. This handles the case 773190579Ssam * where a station leaves w/o notifying us and then returns 774190579Ssam * before node is reaped for inactivity. 775190579Ssam */ 776190579Ssam ieee80211_ff_node_cleanup(ni); 777190579Ssam} 778190579Ssam 779190579Ssamvoid 780190579Ssamieee80211_ff_node_cleanup(struct ieee80211_node *ni) 781190579Ssam{ 782190579Ssam struct ieee80211com *ic = ni->ni_ic; 783191753Ssam struct ieee80211_superg *sg = ic->ic_superg; 784190579Ssam struct ieee80211_tx_ampdu *tap; 785190579Ssam struct mbuf *m, *head; 786190579Ssam int ac; 787190579Ssam 788190579Ssam IEEE80211_LOCK(ic); 789190579Ssam head = NULL; 790190579Ssam for (ac = 0; ac < WME_NUM_AC; ac++) { 791190579Ssam tap = &ni->ni_tx_ampdu[ac]; 792190579Ssam m = tap->txa_private; 793190579Ssam if (m != NULL) { 794190579Ssam tap->txa_private = NULL; 795191753Ssam stageq_remove(&sg->ff_stageq[ac], m); 796190579Ssam m->m_nextpkt = head; 797190579Ssam head = m; 798190579Ssam } 799190579Ssam } 800190579Ssam IEEE80211_UNLOCK(ic); 801190579Ssam 802190579Ssam for (m = head; m != NULL; m = m->m_nextpkt) { 803190579Ssam m_freem(m); 804190579Ssam ieee80211_free_node(ni); 805190579Ssam } 806190579Ssam} 807190579Ssam 808190579Ssam/* 809190391Ssam * Switch between turbo and non-turbo operating modes. 810190391Ssam * Use the specified channel flags to locate the new 811190391Ssam * channel, update 802.11 state, and then call back into 812190391Ssam * the driver to effect the change. 813190391Ssam */ 814190391Ssamvoid 815190391Ssamieee80211_dturbo_switch(struct ieee80211vap *vap, int newflags) 816190391Ssam{ 817190391Ssam struct ieee80211com *ic = vap->iv_ic; 818190391Ssam struct ieee80211_channel *chan; 819190391Ssam 820190391Ssam chan = ieee80211_find_channel(ic, ic->ic_bsschan->ic_freq, newflags); 821190391Ssam if (chan == NULL) { /* XXX should not happen */ 822190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 823190391Ssam "%s: no channel with freq %u flags 0x%x\n", 824190391Ssam __func__, ic->ic_bsschan->ic_freq, newflags); 825190391Ssam return; 826190391Ssam } 827190391Ssam 828190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 829190391Ssam "%s: %s -> %s (freq %u flags 0x%x)\n", __func__, 830190391Ssam ieee80211_phymode_name[ieee80211_chan2mode(ic->ic_bsschan)], 831190391Ssam ieee80211_phymode_name[ieee80211_chan2mode(chan)], 832190391Ssam chan->ic_freq, chan->ic_flags); 833190391Ssam 834190391Ssam ic->ic_bsschan = chan; 835190391Ssam ic->ic_prevchan = ic->ic_curchan; 836190391Ssam ic->ic_curchan = chan; 837190532Ssam ic->ic_rt = ieee80211_get_ratetable(chan); 838190391Ssam ic->ic_set_channel(ic); 839192468Ssam ieee80211_radiotap_chan_change(ic); 840190391Ssam /* NB: do not need to reset ERP state 'cuz we're in sta mode */ 841190391Ssam} 842190391Ssam 843190391Ssam/* 844190391Ssam * Return the current ``state'' of an Atheros capbility. 845190391Ssam * If associated in station mode report the negotiated 846190391Ssam * setting. Otherwise report the current setting. 847190391Ssam */ 848190391Ssamstatic int 849190391Ssamgetathcap(struct ieee80211vap *vap, int cap) 850190391Ssam{ 851190391Ssam if (vap->iv_opmode == IEEE80211_M_STA && 852190391Ssam vap->iv_state == IEEE80211_S_RUN) 853190391Ssam return IEEE80211_ATH_CAP(vap, vap->iv_bss, cap) != 0; 854190391Ssam else 855190391Ssam return (vap->iv_flags & cap) != 0; 856190391Ssam} 857190391Ssam 858190391Ssamstatic int 859190391Ssamsuperg_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 860190391Ssam{ 861190391Ssam switch (ireq->i_type) { 862190391Ssam case IEEE80211_IOC_FF: 863190391Ssam ireq->i_val = getathcap(vap, IEEE80211_F_FF); 864190391Ssam break; 865190391Ssam case IEEE80211_IOC_TURBOP: 866190391Ssam ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP); 867190391Ssam break; 868190391Ssam default: 869190391Ssam return ENOSYS; 870190391Ssam } 871190391Ssam return 0; 872190391Ssam} 873190391SsamIEEE80211_IOCTL_GET(superg, superg_ioctl_get80211); 874190391Ssam 875190391Ssamstatic int 876190391Ssamsuperg_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 877190391Ssam{ 878190391Ssam switch (ireq->i_type) { 879190391Ssam case IEEE80211_IOC_FF: 880190391Ssam if (ireq->i_val) { 881190391Ssam if ((vap->iv_caps & IEEE80211_C_FF) == 0) 882190391Ssam return EOPNOTSUPP; 883190391Ssam vap->iv_flags |= IEEE80211_F_FF; 884190391Ssam } else 885190391Ssam vap->iv_flags &= ~IEEE80211_F_FF; 886190530Ssam return ENETRESET; 887190391Ssam case IEEE80211_IOC_TURBOP: 888190391Ssam if (ireq->i_val) { 889190391Ssam if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0) 890190391Ssam return EOPNOTSUPP; 891190391Ssam vap->iv_flags |= IEEE80211_F_TURBOP; 892190391Ssam } else 893190391Ssam vap->iv_flags &= ~IEEE80211_F_TURBOP; 894190391Ssam return ENETRESET; 895190391Ssam default: 896190391Ssam return ENOSYS; 897190391Ssam } 898190391Ssam return 0; 899190391Ssam} 900190391SsamIEEE80211_IOCTL_SET(superg, superg_ioctl_set80211); 901