ieee80211_superg.c revision 271861
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 271861 2014-09-19 09:20:55Z glebius $"); 28190391Ssam 29190391Ssam#include "opt_wlan.h" 30190391Ssam 31246226Sadrian#ifdef IEEE80211_SUPPORT_SUPERG 32246226Sadrian 33190391Ssam#include <sys/param.h> 34190391Ssam#include <sys/systm.h> 35190391Ssam#include <sys/mbuf.h> 36190391Ssam#include <sys/kernel.h> 37190391Ssam#include <sys/endian.h> 38190391Ssam 39190391Ssam#include <sys/socket.h> 40190391Ssam 41190391Ssam#include <net/if.h> 42257284Sglebius#include <net/if_var.h> 43190391Ssam#include <net/if_llc.h> 44190391Ssam#include <net/if_media.h> 45257284Sglebius#include <net/bpf.h> 46257284Sglebius#include <net/ethernet.h> 47190391Ssam 48190391Ssam#include <net80211/ieee80211_var.h> 49190391Ssam#include <net80211/ieee80211_input.h> 50190391Ssam#include <net80211/ieee80211_phy.h> 51190391Ssam#include <net80211/ieee80211_superg.h> 52190391Ssam 53190455Ssam/* 54190455Ssam * Atheros fast-frame encapsulation format. 55190455Ssam * FF max payload: 56190455Ssam * 802.2 + FFHDR + HPAD + 802.3 + 802.2 + 1500 + SPAD + 802.3 + 802.2 + 1500: 57190455Ssam * 8 + 4 + 4 + 14 + 8 + 1500 + 6 + 14 + 8 + 1500 58190455Ssam * = 3066 59190455Ssam */ 60190455Ssam/* fast frame header is 32-bits */ 61190455Ssam#define ATH_FF_PROTO 0x0000003f /* protocol */ 62190455Ssam#define ATH_FF_PROTO_S 0 63190455Ssam#define ATH_FF_FTYPE 0x000000c0 /* frame type */ 64190455Ssam#define ATH_FF_FTYPE_S 6 65190455Ssam#define ATH_FF_HLEN32 0x00000300 /* optional hdr length */ 66190455Ssam#define ATH_FF_HLEN32_S 8 67190455Ssam#define ATH_FF_SEQNUM 0x001ffc00 /* sequence number */ 68190455Ssam#define ATH_FF_SEQNUM_S 10 69190455Ssam#define ATH_FF_OFFSET 0xffe00000 /* offset to 2nd payload */ 70190455Ssam#define ATH_FF_OFFSET_S 21 71190455Ssam 72190455Ssam#define ATH_FF_MAX_HDR_PAD 4 73190455Ssam#define ATH_FF_MAX_SEP_PAD 6 74190455Ssam#define ATH_FF_MAX_HDR 30 75190455Ssam 76190455Ssam#define ATH_FF_PROTO_L2TUNNEL 0 /* L2 tunnel protocol */ 77190455Ssam#define ATH_FF_ETH_TYPE 0x88bd /* Ether type for encapsulated frames */ 78190455Ssam#define ATH_FF_SNAP_ORGCODE_0 0x00 79190455Ssam#define ATH_FF_SNAP_ORGCODE_1 0x03 80190455Ssam#define ATH_FF_SNAP_ORGCODE_2 0x7f 81190455Ssam 82190579Ssam#define ATH_FF_TXQMIN 2 /* min txq depth for staging */ 83190579Ssam#define ATH_FF_TXQMAX 50 /* maximum # of queued frames allowed */ 84190579Ssam#define ATH_FF_STAGEMAX 5 /* max waiting period for staged frame*/ 85190579Ssam 86190391Ssam#define ETHER_HEADER_COPY(dst, src) \ 87190391Ssam memcpy(dst, src, sizeof(struct ether_header)) 88190391Ssam 89193115Ssamstatic int ieee80211_ffppsmin = 2; /* pps threshold for ff aggregation */ 90193115SsamSYSCTL_INT(_net_wlan, OID_AUTO, ffppsmin, CTLTYPE_INT | CTLFLAG_RW, 91193115Ssam &ieee80211_ffppsmin, 0, "min packet rate before fast-frame staging"); 92193115Ssamstatic int ieee80211_ffagemax = -1; /* max time frames held on stage q */ 93193115SsamSYSCTL_PROC(_net_wlan, OID_AUTO, ffagemax, CTLTYPE_INT | CTLFLAG_RW, 94193115Ssam &ieee80211_ffagemax, 0, ieee80211_sysctl_msecs_ticks, "I", 95193115Ssam "max hold time for fast-frame staging (ms)"); 96190579Ssam 97190391Ssamvoid 98190391Ssamieee80211_superg_attach(struct ieee80211com *ic) 99190391Ssam{ 100191753Ssam struct ieee80211_superg *sg; 101191753Ssam 102191753Ssam if (ic->ic_caps & IEEE80211_C_FF) { 103191753Ssam sg = (struct ieee80211_superg *) malloc( 104191753Ssam sizeof(struct ieee80211_superg), M_80211_VAP, 105191753Ssam M_NOWAIT | M_ZERO); 106191753Ssam if (sg == NULL) { 107191753Ssam printf("%s: cannot allocate SuperG state block\n", 108191753Ssam __func__); 109191753Ssam return; 110191753Ssam } 111191753Ssam ic->ic_superg = sg; 112191753Ssam } 113190579Ssam ieee80211_ffagemax = msecs_to_ticks(150); 114190391Ssam} 115190391Ssam 116190391Ssamvoid 117190391Ssamieee80211_superg_detach(struct ieee80211com *ic) 118190391Ssam{ 119191753Ssam if (ic->ic_superg != NULL) { 120191753Ssam free(ic->ic_superg, M_80211_VAP); 121191753Ssam ic->ic_superg = NULL; 122191753Ssam } 123190391Ssam} 124190391Ssam 125190391Ssamvoid 126190391Ssamieee80211_superg_vattach(struct ieee80211vap *vap) 127190391Ssam{ 128191753Ssam struct ieee80211com *ic = vap->iv_ic; 129191753Ssam 130191753Ssam if (ic->ic_superg == NULL) /* NB: can't do fast-frames w/o state */ 131191753Ssam vap->iv_caps &= ~IEEE80211_C_FF; 132190391Ssam if (vap->iv_caps & IEEE80211_C_FF) 133190391Ssam vap->iv_flags |= IEEE80211_F_FF; 134190450Ssam /* NB: we only implement sta mode */ 135190450Ssam if (vap->iv_opmode == IEEE80211_M_STA && 136190450Ssam (vap->iv_caps & IEEE80211_C_TURBOP)) 137190391Ssam vap->iv_flags |= IEEE80211_F_TURBOP; 138190391Ssam} 139190391Ssam 140190391Ssamvoid 141190391Ssamieee80211_superg_vdetach(struct ieee80211vap *vap) 142190391Ssam{ 143190391Ssam} 144190391Ssam 145190391Ssam#define ATH_OUI_BYTES 0x00, 0x03, 0x7f 146190391Ssam/* 147190391Ssam * Add a WME information element to a frame. 148190391Ssam */ 149190391Ssamuint8_t * 150190451Ssamieee80211_add_ath(uint8_t *frm, uint8_t caps, ieee80211_keyix defkeyix) 151190391Ssam{ 152190391Ssam static const struct ieee80211_ath_ie info = { 153190391Ssam .ath_id = IEEE80211_ELEMID_VENDOR, 154190391Ssam .ath_len = sizeof(struct ieee80211_ath_ie) - 2, 155190391Ssam .ath_oui = { ATH_OUI_BYTES }, 156190391Ssam .ath_oui_type = ATH_OUI_TYPE, 157190391Ssam .ath_oui_subtype= ATH_OUI_SUBTYPE, 158190391Ssam .ath_version = ATH_OUI_VERSION, 159190391Ssam }; 160190391Ssam struct ieee80211_ath_ie *ath = (struct ieee80211_ath_ie *) frm; 161190391Ssam 162190391Ssam memcpy(frm, &info, sizeof(info)); 163190391Ssam ath->ath_capability = caps; 164190451Ssam if (defkeyix != IEEE80211_KEYIX_NONE) { 165190451Ssam ath->ath_defkeyix[0] = (defkeyix & 0xff); 166190451Ssam ath->ath_defkeyix[1] = ((defkeyix >> 8) & 0xff); 167190451Ssam } else { 168190451Ssam ath->ath_defkeyix[0] = 0xff; 169190451Ssam ath->ath_defkeyix[1] = 0x7f; 170190451Ssam } 171190391Ssam return frm + sizeof(info); 172190391Ssam} 173190391Ssam#undef ATH_OUI_BYTES 174190391Ssam 175190451Ssamuint8_t * 176190451Ssamieee80211_add_athcaps(uint8_t *frm, const struct ieee80211_node *bss) 177190451Ssam{ 178190451Ssam const struct ieee80211vap *vap = bss->ni_vap; 179190451Ssam 180190451Ssam return ieee80211_add_ath(frm, 181190451Ssam vap->iv_flags & IEEE80211_F_ATHEROS, 182190451Ssam ((vap->iv_flags & IEEE80211_F_WPA) == 0 && 183190451Ssam bss->ni_authmode != IEEE80211_AUTH_8021X) ? 184190451Ssam vap->iv_def_txkey : IEEE80211_KEYIX_NONE); 185190451Ssam} 186190451Ssam 187190391Ssamvoid 188190391Ssamieee80211_parse_ath(struct ieee80211_node *ni, uint8_t *ie) 189190391Ssam{ 190190391Ssam const struct ieee80211_ath_ie *ath = 191190391Ssam (const struct ieee80211_ath_ie *) ie; 192190391Ssam 193190391Ssam ni->ni_ath_flags = ath->ath_capability; 194190391Ssam ni->ni_ath_defkeyix = LE_READ_2(&ath->ath_defkeyix); 195190391Ssam} 196190391Ssam 197190391Ssamint 198190391Ssamieee80211_parse_athparams(struct ieee80211_node *ni, uint8_t *frm, 199190391Ssam const struct ieee80211_frame *wh) 200190391Ssam{ 201190391Ssam struct ieee80211vap *vap = ni->ni_vap; 202190391Ssam const struct ieee80211_ath_ie *ath; 203190391Ssam u_int len = frm[1]; 204190391Ssam int capschanged; 205190391Ssam uint16_t defkeyix; 206190391Ssam 207190391Ssam if (len < sizeof(struct ieee80211_ath_ie)-2) { 208190391Ssam IEEE80211_DISCARD_IE(vap, 209190391Ssam IEEE80211_MSG_ELEMID | IEEE80211_MSG_SUPERG, 210190391Ssam wh, "Atheros", "too short, len %u", len); 211190391Ssam return -1; 212190391Ssam } 213190391Ssam ath = (const struct ieee80211_ath_ie *)frm; 214190391Ssam capschanged = (ni->ni_ath_flags != ath->ath_capability); 215190391Ssam defkeyix = LE_READ_2(ath->ath_defkeyix); 216190391Ssam if (capschanged || defkeyix != ni->ni_ath_defkeyix) { 217190391Ssam ni->ni_ath_flags = ath->ath_capability; 218190391Ssam ni->ni_ath_defkeyix = defkeyix; 219190391Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 220190391Ssam "ath ie change: new caps 0x%x defkeyix 0x%x", 221190391Ssam ni->ni_ath_flags, ni->ni_ath_defkeyix); 222190391Ssam } 223190391Ssam if (IEEE80211_ATH_CAP(vap, ni, ATHEROS_CAP_TURBO_PRIME)) { 224190391Ssam uint16_t curflags, newflags; 225190391Ssam 226190391Ssam /* 227190391Ssam * Check for turbo mode switch. Calculate flags 228190391Ssam * for the new mode and effect the switch. 229190391Ssam */ 230190391Ssam newflags = curflags = vap->iv_ic->ic_bsschan->ic_flags; 231190391Ssam /* NB: BOOST is not in ic_flags, so get it from the ie */ 232190391Ssam if (ath->ath_capability & ATHEROS_CAP_BOOST) 233190391Ssam newflags |= IEEE80211_CHAN_TURBO; 234190391Ssam else 235190391Ssam newflags &= ~IEEE80211_CHAN_TURBO; 236190391Ssam if (newflags != curflags) 237190391Ssam ieee80211_dturbo_switch(vap, newflags); 238190391Ssam } 239190391Ssam return capschanged; 240190391Ssam} 241190391Ssam 242190391Ssam/* 243190391Ssam * Decap the encapsulated frame pair and dispatch the first 244190391Ssam * for delivery. The second frame is returned for delivery 245190391Ssam * via the normal path. 246190391Ssam */ 247190391Ssamstruct mbuf * 248190391Ssamieee80211_ff_decap(struct ieee80211_node *ni, struct mbuf *m) 249190391Ssam{ 250190391Ssam#define FF_LLC_SIZE (sizeof(struct ether_header) + sizeof(struct llc)) 251190391Ssam#define MS(x,f) (((x) & f) >> f##_S) 252190391Ssam struct ieee80211vap *vap = ni->ni_vap; 253190391Ssam struct llc *llc; 254190391Ssam uint32_t ath; 255190391Ssam struct mbuf *n; 256190391Ssam int framelen; 257190391Ssam 258190391Ssam /* NB: we assume caller does this check for us */ 259190391Ssam KASSERT(IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF), 260190391Ssam ("ff not negotiated")); 261190391Ssam /* 262190391Ssam * Check for fast-frame tunnel encapsulation. 263190391Ssam */ 264190391Ssam if (m->m_pkthdr.len < 3*FF_LLC_SIZE) 265190391Ssam return m; 266190391Ssam if (m->m_len < FF_LLC_SIZE && 267190391Ssam (m = m_pullup(m, FF_LLC_SIZE)) == NULL) { 268190391Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 269190391Ssam ni->ni_macaddr, "fast-frame", 270190391Ssam "%s", "m_pullup(llc) failed"); 271190391Ssam vap->iv_stats.is_rx_tooshort++; 272190391Ssam return NULL; 273190391Ssam } 274190391Ssam llc = (struct llc *)(mtod(m, uint8_t *) + 275190391Ssam sizeof(struct ether_header)); 276190391Ssam if (llc->llc_snap.ether_type != htons(ATH_FF_ETH_TYPE)) 277190391Ssam return m; 278190391Ssam m_adj(m, FF_LLC_SIZE); 279190391Ssam m_copydata(m, 0, sizeof(uint32_t), (caddr_t) &ath); 280190391Ssam if (MS(ath, ATH_FF_PROTO) != ATH_FF_PROTO_L2TUNNEL) { 281190391Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 282190391Ssam ni->ni_macaddr, "fast-frame", 283190391Ssam "unsupport tunnel protocol, header 0x%x", ath); 284190391Ssam vap->iv_stats.is_ff_badhdr++; 285190391Ssam m_freem(m); 286190391Ssam return NULL; 287190391Ssam } 288190391Ssam /* NB: skip header and alignment padding */ 289190391Ssam m_adj(m, roundup(sizeof(uint32_t) - 2, 4) + 2); 290190391Ssam 291190391Ssam vap->iv_stats.is_ff_decap++; 292190391Ssam 293190391Ssam /* 294190391Ssam * Decap the first frame, bust it apart from the 295190391Ssam * second and deliver; then decap the second frame 296190391Ssam * and return it to the caller for normal delivery. 297190391Ssam */ 298190391Ssam m = ieee80211_decap1(m, &framelen); 299190391Ssam if (m == NULL) { 300190391Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 301190391Ssam ni->ni_macaddr, "fast-frame", "%s", "first decap failed"); 302190391Ssam vap->iv_stats.is_ff_tooshort++; 303190391Ssam return NULL; 304190391Ssam } 305190391Ssam n = m_split(m, framelen, M_NOWAIT); 306190391Ssam if (n == NULL) { 307190391Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 308190391Ssam ni->ni_macaddr, "fast-frame", 309190391Ssam "%s", "unable to split encapsulated frames"); 310190391Ssam vap->iv_stats.is_ff_split++; 311190391Ssam m_freem(m); /* NB: must reclaim */ 312190391Ssam return NULL; 313190391Ssam } 314190391Ssam /* XXX not right for WDS */ 315190391Ssam vap->iv_deliver_data(vap, ni, m); /* 1st of pair */ 316190391Ssam 317190391Ssam /* 318190391Ssam * Decap second frame. 319190391Ssam */ 320190391Ssam m_adj(n, roundup2(framelen, 4) - framelen); /* padding */ 321190391Ssam n = ieee80211_decap1(n, &framelen); 322190391Ssam if (n == NULL) { 323190391Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 324190391Ssam ni->ni_macaddr, "fast-frame", "%s", "second decap failed"); 325190391Ssam vap->iv_stats.is_ff_tooshort++; 326190391Ssam } 327190391Ssam /* XXX verify framelen against mbuf contents */ 328190391Ssam return n; /* 2nd delivered by caller */ 329190391Ssam#undef MS 330190391Ssam#undef FF_LLC_SIZE 331190391Ssam} 332190391Ssam 333190391Ssam/* 334190391Ssam * Fast frame encapsulation. There must be two packets 335190391Ssam * chained with m_nextpkt. We do header adjustment for 336190391Ssam * each, add the tunnel encapsulation, and then concatenate 337190391Ssam * the mbuf chains to form a single frame for transmission. 338190391Ssam */ 339190391Ssamstruct mbuf * 340190391Ssamieee80211_ff_encap(struct ieee80211vap *vap, struct mbuf *m1, int hdrspace, 341190391Ssam struct ieee80211_key *key) 342190391Ssam{ 343190391Ssam struct mbuf *m2; 344190391Ssam struct ether_header eh1, eh2; 345190391Ssam struct llc *llc; 346190391Ssam struct mbuf *m; 347190391Ssam int pad; 348190391Ssam 349190391Ssam m2 = m1->m_nextpkt; 350190391Ssam if (m2 == NULL) { 351190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 352190391Ssam "%s: only one frame\n", __func__); 353190391Ssam goto bad; 354190391Ssam } 355190391Ssam m1->m_nextpkt = NULL; 356190391Ssam /* 357190579Ssam * Include fast frame headers in adjusting header layout. 358190391Ssam */ 359190579Ssam KASSERT(m1->m_len >= sizeof(eh1), ("no ethernet header!")); 360190579Ssam ETHER_HEADER_COPY(&eh1, mtod(m1, caddr_t)); 361190391Ssam m1 = ieee80211_mbuf_adjust(vap, 362190391Ssam hdrspace + sizeof(struct llc) + sizeof(uint32_t) + 2 + 363190391Ssam sizeof(struct ether_header), 364190391Ssam key, m1); 365190391Ssam if (m1 == NULL) { 366190391Ssam /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 367190391Ssam m_freem(m2); 368190391Ssam goto bad; 369190391Ssam } 370190391Ssam 371190391Ssam /* 372190391Ssam * Copy second frame's Ethernet header out of line 373190391Ssam * and adjust for encapsulation headers. Note that 374190391Ssam * we make room for padding in case there isn't room 375190391Ssam * at the end of first frame. 376190391Ssam */ 377190391Ssam KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!")); 378190391Ssam ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t)); 379190391Ssam m2 = ieee80211_mbuf_adjust(vap, 380190391Ssam ATH_FF_MAX_HDR_PAD + sizeof(struct ether_header), 381190391Ssam NULL, m2); 382190391Ssam if (m2 == NULL) { 383190391Ssam /* NB: ieee80211_mbuf_adjust handles msgs+statistics */ 384190391Ssam goto bad; 385190391Ssam } 386190391Ssam 387190391Ssam /* 388190391Ssam * Now do tunnel encapsulation. First, each 389190391Ssam * frame gets a standard encapsulation. 390190391Ssam */ 391254900Sadrian m1 = ieee80211_ff_encap1(vap, m1, &eh1); 392190391Ssam if (m1 == NULL) 393190391Ssam goto bad; 394254900Sadrian m2 = ieee80211_ff_encap1(vap, m2, &eh2); 395190391Ssam if (m2 == NULL) 396190391Ssam goto bad; 397190391Ssam 398190391Ssam /* 399190391Ssam * Pad leading frame to a 4-byte boundary. If there 400190391Ssam * is space at the end of the first frame, put it 401190391Ssam * there; otherwise prepend to the front of the second 402190391Ssam * frame. We know doing the second will always work 403190391Ssam * because we reserve space above. We prefer appending 404190391Ssam * as this typically has better DMA alignment properties. 405190391Ssam */ 406190391Ssam for (m = m1; m->m_next != NULL; m = m->m_next) 407190391Ssam ; 408190391Ssam pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len; 409190391Ssam if (pad) { 410190391Ssam if (M_TRAILINGSPACE(m) < pad) { /* prepend to second */ 411190391Ssam m2->m_data -= pad; 412190391Ssam m2->m_len += pad; 413190391Ssam m2->m_pkthdr.len += pad; 414190391Ssam } else { /* append to first */ 415190391Ssam m->m_len += pad; 416190391Ssam m1->m_pkthdr.len += pad; 417190391Ssam } 418190391Ssam } 419190391Ssam 420190391Ssam /* 421190391Ssam * Now, stick 'em together and prepend the tunnel headers; 422190391Ssam * first the Atheros tunnel header (all zero for now) and 423190391Ssam * then a special fast frame LLC. 424190391Ssam * 425190391Ssam * XXX optimize by prepending together 426190391Ssam */ 427190391Ssam m->m_next = m2; /* NB: last mbuf from above */ 428190391Ssam m1->m_pkthdr.len += m2->m_pkthdr.len; 429243882Sglebius M_PREPEND(m1, sizeof(uint32_t)+2, M_NOWAIT); 430190391Ssam if (m1 == NULL) { /* XXX cannot happen */ 431190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 432190391Ssam "%s: no space for tunnel header\n", __func__); 433190391Ssam vap->iv_stats.is_tx_nobuf++; 434190391Ssam return NULL; 435190391Ssam } 436190391Ssam memset(mtod(m1, void *), 0, sizeof(uint32_t)+2); 437190391Ssam 438243882Sglebius M_PREPEND(m1, sizeof(struct llc), M_NOWAIT); 439190391Ssam if (m1 == NULL) { /* XXX cannot happen */ 440190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 441190391Ssam "%s: no space for llc header\n", __func__); 442190391Ssam vap->iv_stats.is_tx_nobuf++; 443190391Ssam return NULL; 444190391Ssam } 445190391Ssam llc = mtod(m1, struct llc *); 446190391Ssam llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP; 447190391Ssam llc->llc_control = LLC_UI; 448190391Ssam llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0; 449190391Ssam llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1; 450190391Ssam llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2; 451190391Ssam llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE); 452190391Ssam 453190391Ssam vap->iv_stats.is_ff_encap++; 454190391Ssam 455190391Ssam return m1; 456190391Ssambad: 457190391Ssam if (m1 != NULL) 458190391Ssam m_freem(m1); 459190391Ssam if (m2 != NULL) 460190391Ssam m_freem(m2); 461190391Ssam return NULL; 462190391Ssam} 463190391Ssam 464190579Ssamstatic void 465190579Ssamff_transmit(struct ieee80211_node *ni, struct mbuf *m) 466190579Ssam{ 467190579Ssam struct ieee80211vap *vap = ni->ni_vap; 468248069Sadrian struct ieee80211com *ic = ni->ni_ic; 469190579Ssam int error; 470190579Ssam 471248069Sadrian IEEE80211_TX_LOCK_ASSERT(vap->iv_ic); 472248069Sadrian 473190579Ssam /* encap and xmit */ 474190579Ssam m = ieee80211_encap(vap, ni, m); 475190579Ssam if (m != NULL) { 476190579Ssam struct ifnet *ifp = vap->iv_ifp; 477190579Ssam 478254082Sadrian error = ieee80211_parent_xmitpkt(ic, m);; 479190579Ssam if (error != 0) { 480190579Ssam /* NB: IFQ_HANDOFF reclaims mbuf */ 481190579Ssam ieee80211_free_node(ni); 482190579Ssam } else { 483271861Sglebius if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 484190579Ssam } 485190579Ssam } else 486190579Ssam ieee80211_free_node(ni); 487190579Ssam} 488190579Ssam 489190391Ssam/* 490190579Ssam * Flush frames to device; note we re-use the linked list 491190579Ssam * the frames were stored on and use the sentinel (unchanged) 492190579Ssam * which may be non-NULL. 493190579Ssam */ 494190579Ssamstatic void 495190579Ssamff_flush(struct mbuf *head, struct mbuf *last) 496190579Ssam{ 497190579Ssam struct mbuf *m, *next; 498190579Ssam struct ieee80211_node *ni; 499190579Ssam struct ieee80211vap *vap; 500190579Ssam 501190579Ssam for (m = head; m != last; m = next) { 502190579Ssam next = m->m_nextpkt; 503190579Ssam m->m_nextpkt = NULL; 504190579Ssam 505190579Ssam ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 506190579Ssam vap = ni->ni_vap; 507190579Ssam 508190579Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 509190579Ssam "%s: flush frame, age %u", __func__, M_AGE_GET(m)); 510190579Ssam vap->iv_stats.is_ff_flush++; 511190579Ssam 512190579Ssam ff_transmit(ni, m); 513190579Ssam } 514190579Ssam} 515190579Ssam 516190579Ssam/* 517190579Ssam * Age frames on the staging queue. 518244044Sadrian * 519244044Sadrian * This is called without the comlock held, but it does all its work 520244044Sadrian * behind the comlock. Because of this, it's possible that the 521244044Sadrian * staging queue will be serviced between the function which called 522244044Sadrian * it and now; thus simply checking that the queue has work in it 523244044Sadrian * may fail. 524244044Sadrian * 525244044Sadrian * See PR kern/174283 for more details. 526190579Ssam */ 527190579Ssamvoid 528191753Ssamieee80211_ff_age(struct ieee80211com *ic, struct ieee80211_stageq *sq, 529191753Ssam int quanta) 530190579Ssam{ 531190579Ssam struct mbuf *m, *head; 532190579Ssam struct ieee80211_node *ni; 533190579Ssam struct ieee80211_tx_ampdu *tap; 534190579Ssam 535244044Sadrian#if 0 536190579Ssam KASSERT(sq->head != NULL, ("stageq empty")); 537244044Sadrian#endif 538190579Ssam 539190579Ssam IEEE80211_LOCK(ic); 540190579Ssam head = sq->head; 541190579Ssam while ((m = sq->head) != NULL && M_AGE_GET(m) < quanta) { 542234324Sadrian int tid = WME_AC_TO_TID(M_WME_GETAC(m)); 543234324Sadrian 544190579Ssam /* clear tap ref to frame */ 545190579Ssam ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 546234324Sadrian tap = &ni->ni_tx_ampdu[tid]; 547190579Ssam KASSERT(tap->txa_private == m, ("staging queue empty")); 548190579Ssam tap->txa_private = NULL; 549190579Ssam 550190579Ssam sq->head = m->m_nextpkt; 551190579Ssam sq->depth--; 552190579Ssam } 553190579Ssam if (m == NULL) 554190579Ssam sq->tail = NULL; 555190579Ssam else 556190579Ssam M_AGE_SUB(m, quanta); 557190579Ssam IEEE80211_UNLOCK(ic); 558190579Ssam 559248069Sadrian IEEE80211_TX_LOCK(ic); 560190579Ssam ff_flush(head, m); 561248069Sadrian IEEE80211_TX_UNLOCK(ic); 562190579Ssam} 563190579Ssam 564190579Ssamstatic void 565244051Sadrianstageq_add(struct ieee80211com *ic, struct ieee80211_stageq *sq, struct mbuf *m) 566190579Ssam{ 567190579Ssam int age = ieee80211_ffagemax; 568244051Sadrian 569244051Sadrian IEEE80211_LOCK_ASSERT(ic); 570244051Sadrian 571190579Ssam if (sq->tail != NULL) { 572190579Ssam sq->tail->m_nextpkt = m; 573190579Ssam age -= M_AGE_GET(sq->head); 574190579Ssam } else 575190579Ssam sq->head = m; 576190579Ssam KASSERT(age >= 0, ("age %d", age)); 577190579Ssam M_AGE_SET(m, age); 578190579Ssam m->m_nextpkt = NULL; 579190579Ssam sq->tail = m; 580190579Ssam sq->depth++; 581190579Ssam} 582190579Ssam 583190579Ssamstatic void 584244051Sadrianstageq_remove(struct ieee80211com *ic, struct ieee80211_stageq *sq, struct mbuf *mstaged) 585190579Ssam{ 586190579Ssam struct mbuf *m, *mprev; 587190579Ssam 588244051Sadrian IEEE80211_LOCK_ASSERT(ic); 589244051Sadrian 590190579Ssam mprev = NULL; 591190579Ssam for (m = sq->head; m != NULL; m = m->m_nextpkt) { 592190579Ssam if (m == mstaged) { 593190579Ssam if (mprev == NULL) 594190579Ssam sq->head = m->m_nextpkt; 595190579Ssam else 596190579Ssam mprev->m_nextpkt = m->m_nextpkt; 597190579Ssam if (sq->tail == m) 598190579Ssam sq->tail = mprev; 599190579Ssam sq->depth--; 600190579Ssam return; 601190579Ssam } 602190579Ssam mprev = m; 603190579Ssam } 604190579Ssam printf("%s: packet not found\n", __func__); 605190579Ssam} 606190579Ssam 607190579Ssamstatic uint32_t 608190579Ssamff_approx_txtime(struct ieee80211_node *ni, 609190579Ssam const struct mbuf *m1, const struct mbuf *m2) 610190579Ssam{ 611190579Ssam struct ieee80211com *ic = ni->ni_ic; 612190579Ssam struct ieee80211vap *vap = ni->ni_vap; 613190579Ssam uint32_t framelen; 614190579Ssam 615190579Ssam /* 616190579Ssam * Approximate the frame length to be transmitted. A swag to add 617190579Ssam * the following maximal values to the skb payload: 618190579Ssam * - 32: 802.11 encap + CRC 619190579Ssam * - 24: encryption overhead (if wep bit) 620190579Ssam * - 4 + 6: fast-frame header and padding 621190579Ssam * - 16: 2 LLC FF tunnel headers 622190579Ssam * - 14: 1 802.3 FF tunnel header (mbuf already accounts for 2nd) 623190579Ssam */ 624190579Ssam framelen = m1->m_pkthdr.len + 32 + 625190579Ssam ATH_FF_MAX_HDR_PAD + ATH_FF_MAX_SEP_PAD + ATH_FF_MAX_HDR; 626190579Ssam if (vap->iv_flags & IEEE80211_F_PRIVACY) 627190579Ssam framelen += 24; 628190579Ssam if (m2 != NULL) 629190579Ssam framelen += m2->m_pkthdr.len; 630190579Ssam return ieee80211_compute_duration(ic->ic_rt, framelen, ni->ni_txrate, 0); 631190579Ssam} 632190579Ssam 633190579Ssam/* 634190579Ssam * Check if the supplied frame can be partnered with an existing 635190579Ssam * or pending frame. Return a reference to any frame that should be 636190579Ssam * sent on return; otherwise return NULL. 637190579Ssam */ 638190579Ssamstruct mbuf * 639190579Ssamieee80211_ff_check(struct ieee80211_node *ni, struct mbuf *m) 640190579Ssam{ 641190579Ssam struct ieee80211vap *vap = ni->ni_vap; 642190579Ssam struct ieee80211com *ic = ni->ni_ic; 643191753Ssam struct ieee80211_superg *sg = ic->ic_superg; 644190579Ssam const int pri = M_WME_GETAC(m); 645190579Ssam struct ieee80211_stageq *sq; 646190579Ssam struct ieee80211_tx_ampdu *tap; 647190579Ssam struct mbuf *mstaged; 648190579Ssam uint32_t txtime, limit; 649190579Ssam 650248069Sadrian IEEE80211_TX_UNLOCK_ASSERT(ic); 651248069Sadrian 652190579Ssam /* 653190579Ssam * Check if the supplied frame can be aggregated. 654190579Ssam * 655190579Ssam * NB: we allow EAPOL frames to be aggregated with other ucast traffic. 656190579Ssam * Do 802.1x EAPOL frames proceed in the clear? Then they couldn't 657190579Ssam * be aggregated with other types of frames when encryption is on? 658190579Ssam */ 659190579Ssam IEEE80211_LOCK(ic); 660234324Sadrian tap = &ni->ni_tx_ampdu[WME_AC_TO_TID(pri)]; 661190579Ssam mstaged = tap->txa_private; /* NB: we reuse AMPDU state */ 662190579Ssam ieee80211_txampdu_count_packet(tap); 663190579Ssam 664190579Ssam /* 665190579Ssam * When not in station mode never aggregate a multicast 666190579Ssam * frame; this insures, for example, that a combined frame 667190579Ssam * does not require multiple encryption keys. 668190579Ssam */ 669190579Ssam if (vap->iv_opmode != IEEE80211_M_STA && 670190579Ssam ETHER_IS_MULTICAST(mtod(m, struct ether_header *)->ether_dhost)) { 671190579Ssam /* XXX flush staged frame? */ 672190579Ssam IEEE80211_UNLOCK(ic); 673190579Ssam return m; 674190579Ssam } 675190579Ssam /* 676190579Ssam * If there is no frame to combine with and the pps is 677190579Ssam * too low; then do not attempt to aggregate this frame. 678190579Ssam */ 679190579Ssam if (mstaged == NULL && 680190579Ssam ieee80211_txampdu_getpps(tap) < ieee80211_ffppsmin) { 681190579Ssam IEEE80211_UNLOCK(ic); 682190579Ssam return m; 683190579Ssam } 684191753Ssam sq = &sg->ff_stageq[pri]; 685190579Ssam /* 686190579Ssam * Check the txop limit to insure the aggregate fits. 687190579Ssam */ 688190579Ssam limit = IEEE80211_TXOP_TO_US( 689190579Ssam ic->ic_wme.wme_chanParams.cap_wmeParams[pri].wmep_txopLimit); 690190579Ssam if (limit != 0 && 691190579Ssam (txtime = ff_approx_txtime(ni, m, mstaged)) > limit) { 692190579Ssam /* 693190579Ssam * Aggregate too long, return to the caller for direct 694190579Ssam * transmission. In addition, flush any pending frame 695190579Ssam * before sending this one. 696190579Ssam */ 697190579Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 698190579Ssam "%s: txtime %u exceeds txop limit %u\n", 699190579Ssam __func__, txtime, limit); 700190579Ssam 701190579Ssam tap->txa_private = NULL; 702190579Ssam if (mstaged != NULL) 703244051Sadrian stageq_remove(ic, sq, mstaged); 704190579Ssam IEEE80211_UNLOCK(ic); 705190579Ssam 706190579Ssam if (mstaged != NULL) { 707248069Sadrian IEEE80211_TX_LOCK(ic); 708190579Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 709190579Ssam "%s: flush staged frame", __func__); 710190579Ssam /* encap and xmit */ 711190579Ssam ff_transmit(ni, mstaged); 712248069Sadrian IEEE80211_TX_UNLOCK(ic); 713190579Ssam } 714190579Ssam return m; /* NB: original frame */ 715190579Ssam } 716190579Ssam /* 717190579Ssam * An aggregation candidate. If there's a frame to partner 718190579Ssam * with then combine and return for processing. Otherwise 719190579Ssam * save this frame and wait for a partner to show up (or 720190579Ssam * the frame to be flushed). Note that staged frames also 721190579Ssam * hold their node reference. 722190579Ssam */ 723190579Ssam if (mstaged != NULL) { 724190579Ssam tap->txa_private = NULL; 725244051Sadrian stageq_remove(ic, sq, mstaged); 726190579Ssam IEEE80211_UNLOCK(ic); 727190579Ssam 728190579Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 729190579Ssam "%s: aggregate fast-frame", __func__); 730190579Ssam /* 731190579Ssam * Release the node reference; we only need 732190579Ssam * the one already in mstaged. 733190579Ssam */ 734190579Ssam KASSERT(mstaged->m_pkthdr.rcvif == (void *)ni, 735190579Ssam ("rcvif %p ni %p", mstaged->m_pkthdr.rcvif, ni)); 736190579Ssam ieee80211_free_node(ni); 737190579Ssam 738190579Ssam m->m_nextpkt = NULL; 739190579Ssam mstaged->m_nextpkt = m; 740190579Ssam mstaged->m_flags |= M_FF; /* NB: mark for encap work */ 741190579Ssam } else { 742190579Ssam KASSERT(tap->txa_private == NULL, 743190579Ssam ("txa_private %p", tap->txa_private)); 744190579Ssam tap->txa_private = m; 745190579Ssam 746244051Sadrian stageq_add(ic, sq, m); 747190579Ssam IEEE80211_UNLOCK(ic); 748190579Ssam 749190579Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 750190579Ssam "%s: stage frame, %u queued", __func__, sq->depth); 751190579Ssam /* NB: mstaged is NULL */ 752190579Ssam } 753190579Ssam return mstaged; 754190579Ssam} 755190579Ssam 756190579Ssamvoid 757190579Ssamieee80211_ff_node_init(struct ieee80211_node *ni) 758190579Ssam{ 759190579Ssam /* 760190579Ssam * Clean FF state on re-associate. This handles the case 761190579Ssam * where a station leaves w/o notifying us and then returns 762190579Ssam * before node is reaped for inactivity. 763190579Ssam */ 764190579Ssam ieee80211_ff_node_cleanup(ni); 765190579Ssam} 766190579Ssam 767190579Ssamvoid 768190579Ssamieee80211_ff_node_cleanup(struct ieee80211_node *ni) 769190579Ssam{ 770190579Ssam struct ieee80211com *ic = ni->ni_ic; 771191753Ssam struct ieee80211_superg *sg = ic->ic_superg; 772190579Ssam struct ieee80211_tx_ampdu *tap; 773244025Sadrian struct mbuf *m, *next_m, *head; 774234324Sadrian int tid; 775190579Ssam 776190579Ssam IEEE80211_LOCK(ic); 777190579Ssam head = NULL; 778234324Sadrian for (tid = 0; tid < WME_NUM_TID; tid++) { 779234324Sadrian int ac = TID_TO_WME_AC(tid); 780234324Sadrian 781234324Sadrian tap = &ni->ni_tx_ampdu[tid]; 782190579Ssam m = tap->txa_private; 783190579Ssam if (m != NULL) { 784190579Ssam tap->txa_private = NULL; 785244051Sadrian stageq_remove(ic, &sg->ff_stageq[ac], m); 786190579Ssam m->m_nextpkt = head; 787190579Ssam head = m; 788190579Ssam } 789190579Ssam } 790190579Ssam IEEE80211_UNLOCK(ic); 791190579Ssam 792244025Sadrian /* 793244025Sadrian * Free mbufs, taking care to not dereference the mbuf after 794244025Sadrian * we free it (hence grabbing m_nextpkt before we free it.) 795244025Sadrian */ 796244025Sadrian m = head; 797244025Sadrian while (m != NULL) { 798244025Sadrian next_m = m->m_nextpkt; 799190579Ssam m_freem(m); 800190579Ssam ieee80211_free_node(ni); 801244025Sadrian m = next_m; 802190579Ssam } 803190579Ssam} 804190579Ssam 805190579Ssam/* 806190391Ssam * Switch between turbo and non-turbo operating modes. 807190391Ssam * Use the specified channel flags to locate the new 808190391Ssam * channel, update 802.11 state, and then call back into 809190391Ssam * the driver to effect the change. 810190391Ssam */ 811190391Ssamvoid 812190391Ssamieee80211_dturbo_switch(struct ieee80211vap *vap, int newflags) 813190391Ssam{ 814190391Ssam struct ieee80211com *ic = vap->iv_ic; 815190391Ssam struct ieee80211_channel *chan; 816190391Ssam 817190391Ssam chan = ieee80211_find_channel(ic, ic->ic_bsschan->ic_freq, newflags); 818190391Ssam if (chan == NULL) { /* XXX should not happen */ 819190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 820190391Ssam "%s: no channel with freq %u flags 0x%x\n", 821190391Ssam __func__, ic->ic_bsschan->ic_freq, newflags); 822190391Ssam return; 823190391Ssam } 824190391Ssam 825190391Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, 826190391Ssam "%s: %s -> %s (freq %u flags 0x%x)\n", __func__, 827190391Ssam ieee80211_phymode_name[ieee80211_chan2mode(ic->ic_bsschan)], 828190391Ssam ieee80211_phymode_name[ieee80211_chan2mode(chan)], 829190391Ssam chan->ic_freq, chan->ic_flags); 830190391Ssam 831190391Ssam ic->ic_bsschan = chan; 832190391Ssam ic->ic_prevchan = ic->ic_curchan; 833190391Ssam ic->ic_curchan = chan; 834190532Ssam ic->ic_rt = ieee80211_get_ratetable(chan); 835190391Ssam ic->ic_set_channel(ic); 836192468Ssam ieee80211_radiotap_chan_change(ic); 837190391Ssam /* NB: do not need to reset ERP state 'cuz we're in sta mode */ 838190391Ssam} 839190391Ssam 840190391Ssam/* 841190391Ssam * Return the current ``state'' of an Atheros capbility. 842190391Ssam * If associated in station mode report the negotiated 843190391Ssam * setting. Otherwise report the current setting. 844190391Ssam */ 845190391Ssamstatic int 846190391Ssamgetathcap(struct ieee80211vap *vap, int cap) 847190391Ssam{ 848190391Ssam if (vap->iv_opmode == IEEE80211_M_STA && 849190391Ssam vap->iv_state == IEEE80211_S_RUN) 850190391Ssam return IEEE80211_ATH_CAP(vap, vap->iv_bss, cap) != 0; 851190391Ssam else 852190391Ssam return (vap->iv_flags & cap) != 0; 853190391Ssam} 854190391Ssam 855190391Ssamstatic int 856190391Ssamsuperg_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 857190391Ssam{ 858190391Ssam switch (ireq->i_type) { 859190391Ssam case IEEE80211_IOC_FF: 860190391Ssam ireq->i_val = getathcap(vap, IEEE80211_F_FF); 861190391Ssam break; 862190391Ssam case IEEE80211_IOC_TURBOP: 863190391Ssam ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP); 864190391Ssam break; 865190391Ssam default: 866190391Ssam return ENOSYS; 867190391Ssam } 868190391Ssam return 0; 869190391Ssam} 870190391SsamIEEE80211_IOCTL_GET(superg, superg_ioctl_get80211); 871190391Ssam 872190391Ssamstatic int 873190391Ssamsuperg_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 874190391Ssam{ 875190391Ssam switch (ireq->i_type) { 876190391Ssam case IEEE80211_IOC_FF: 877190391Ssam if (ireq->i_val) { 878190391Ssam if ((vap->iv_caps & IEEE80211_C_FF) == 0) 879190391Ssam return EOPNOTSUPP; 880190391Ssam vap->iv_flags |= IEEE80211_F_FF; 881190391Ssam } else 882190391Ssam vap->iv_flags &= ~IEEE80211_F_FF; 883190530Ssam return ENETRESET; 884190391Ssam case IEEE80211_IOC_TURBOP: 885190391Ssam if (ireq->i_val) { 886190391Ssam if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0) 887190391Ssam return EOPNOTSUPP; 888190391Ssam vap->iv_flags |= IEEE80211_F_TURBOP; 889190391Ssam } else 890190391Ssam vap->iv_flags &= ~IEEE80211_F_TURBOP; 891190391Ssam return ENETRESET; 892190391Ssam default: 893190391Ssam return ENOSYS; 894190391Ssam } 895190391Ssam return 0; 896190391Ssam} 897190391SsamIEEE80211_IOCTL_SET(superg, superg_ioctl_set80211); 898246226Sadrian 899246226Sadrian#endif /* IEEE80211_SUPPORT_SUPERG */ 900