📄 ieee80211_scan_ap.c.svn-base
字号:
/*- * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting * Copyright (c) 2005 Matt Mackall <mpm@selenic.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * Alternatively, this software may be distributed under the terms of the * GNU General Public License ("GPL") version 2 as published by the Free * Software Foundation. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $Id$ */#ifndef EXPORT_SYMTAB#define EXPORT_SYMTAB#endif/* * IEEE 802.11 ap scanning support. */#ifndef AUTOCONF_INCLUDED#include <linux/config.h>#endif#include <linux/version.h>#include <linux/module.h>#include <linux/skbuff.h>#include <linux/netdevice.h>#include <linux/init.h>#include <linux/delay.h>#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)/* Copied from Linux lib/sort.c */static void u32_swap(void *a, void *b, int size){ u32 t = *(u32 *)a; *(u32 *)a = *(u32 *)b; *(u32 *)b = t;}static void generic_swap(void *a, void *b, int size){ char t; do { t = *(char *)a; *(char *)a++ = *(char *)b; *(char *)b++ = t; } while (--size > 0);}/** * sort - sort an array of elements * @base: pointer to data to sort * @num: number of elements * @size: size of each element * @cmp: pointer to comparison function * @swap: pointer to swap function or NULL * * This function does a heapsort on the given array. You may provide a * swap function optimized to your element type. * * Sorting time is O(n log n) both on average and worst-case. While * qsort is about 20% faster on average, it suffers from exploitable * O(n*n) worst-case behavior and extra memory requirements that make * it less suitable for kernel use. */void sort(void *base, size_t num, size_t size, int (*cmp)(const void *, const void *), void (*swap)(void *, void *, int size)){ /* pre-scale counters for performance */ int i = (num/2 - 1) * size, n = num * size, c, r; if (!swap) swap = (size == 4 ? u32_swap : generic_swap); /* heapify */ for ( ; i >= 0; i -= size) { for (r = i; r * 2 + size < n; r = c) { c = r * 2 + size; if (c < n - size && cmp(base + c, base + c + size) < 0) c += size; if (cmp(base + r, base + c) >= 0) break; swap(base + r, base + c, size); } } /* sort */ for (i = n - size; i > 0; i -= size) { swap(base, base + i, size); for (r = 0; r * 2 + size < i; r = c) { c = r * 2 + size; if (c < i - size && cmp(base + c, base + c + size) < 0) c += size; if (cmp(base + r, base + c) >= 0) break; swap(base + r, base + c, size); } }}#else#include <linux/sort.h>#endif#include "if_media.h"#include <net80211/ieee80211_var.h>#define AP_PURGE_SCANS 2 /* age for purging entries (scans) */#define RSSI_LPF_LEN 10#define RSSI_EP_MULTIPLIER (1 << 7) /* pow2 to optimize out * and / */#define RSSI_IN(x) ((x) * RSSI_EP_MULTIPLIER)#define LPF_RSSI(x, y, len) (((x) * ((len) - 1) + (y)) / (len))#define RSSI_LPF(x, y) do { \ if ((y) >= -20) \ x = LPF_RSSI((x), RSSI_IN((y)), RSSI_LPF_LEN); \} while (0)#define EP_RND(x, mul) \ ((((x)%(mul)) >= ((mul)/2)) ? howmany(x, mul) : (x)/(mul))#define RSSI_GET(x) EP_RND(x, RSSI_EP_MULTIPLIER)#define AP_HASHSIZE 32/* simple hash is enough for variation of macaddr */#define AP_HASH(addr) \ (((const u_int8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % AP_HASHSIZE)#define SCAN_AP_LOCK_INIT(_st, _name) \ spin_lock_init(&(_st)->as_lock)#define SCAN_AP_LOCK_DESTROY(_st)#define SCAN_AP_LOCK_IRQ(_st) do { \ unsigned long __stlockflags; \ spin_lock_irqsave(&(_st)->as_lock, __stlockflags);#define SCAN_AP_UNLOCK_IRQ(_st) \ spin_unlock_irqrestore(&(_st)->as_lock, __stlockflags); \} while (0)#define SCAN_AP_UNLOCK_IRQ_EARLY(_st) \ spin_unlock_irqrestore(&(_st)->as_lock, __stlockflags);#define SCAN_AP_GEN_LOCK_INIT(_st, _name) \ spin_lock_init(&(_st)->as_scanlock)#define SCAN_AP_GEN_LOCK_DESTROY(_st)#define SCAN_AP_GEN_LOCK(_st) spin_lock(&(_st)->as_scanlock);#define SCAN_AP_GEN_UNLOCK(_st) spin_unlock(&(_st)->as_scanlock);struct scan_entry { struct ieee80211_scan_entry base; TAILQ_ENTRY(scan_entry) se_list; LIST_ENTRY(scan_entry) se_hash; u_int8_t se_seen; /* seen during current scan */ u_int8_t se_notseen; /* not seen in previous scans */ u_int32_t se_avgrssi; /* LPF rssi state */ unsigned long se_lastupdate; /* time of last update */ unsigned long se_lastfail; /* time of last failure */ unsigned long se_lastassoc; /* time of last association */ u_int se_scangen; /* iterator scan gen# */};struct ap_state { unsigned int as_vap_desired_mode; /* Used for channel selection, * vap->iv_des_mode */ unsigned int as_required_mode; /* Used for channel selection, * filtered version of * as_vap_desired_mode */ int as_maxrssi[IEEE80211_CHAN_MAX]; /* Used for channel selection */ /* These fields are just for scan caching for returning responses to * wireless extensions. i.e. show peers, APs, etc. */ spinlock_t as_lock; /* on scan table */ int as_newscan; /* trigger for updating * seen/not-seen for aging */ TAILQ_HEAD(, scan_entry) as_entry; /* all entries */ ATH_LIST_HEAD(, scan_entry) as_hash[AP_HASHSIZE]; spinlock_t as_scanlock; /* on as_scangen */ u_int as_scangen; /* gen# for iterator */ struct IEEE80211_TQ_STRUCT as_actiontq; /* tasklet for "action" */ struct ieee80211_scan_entry as_selbss; /* selected bss for action tasklet */ int (*as_action)(struct ieee80211vap *, const struct ieee80211_scan_entry *);};static int ap_flush(struct ieee80211_scan_state *);static void action_tasklet(IEEE80211_TQUEUE_ARG);static struct ieee80211_channel *find11gchannel(struct ieee80211com *ic, int i, int freq);static const u_int chanflags[] = { IEEE80211_CHAN_B, /* IEEE80211_MODE_AUTO */ IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */ IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */ IEEE80211_CHAN_A, /* IEEE80211_MODE_TURBO_A */ /* for turbo mode * look for AP in * normal channel */ IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_TURBO_G */ IEEE80211_CHAN_ST, /* IEEE80211_MODE_TURBO_STATIC_A */};static const u_int16_t rcl1[] = /* 8 FCC channel: 52, 56, 60, 64, * 36, 40, 44, 48 */{ 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 };static const u_int16_t rcl2[] = /* 4 MKK channels: 34, 38, 42, 46 */{ 5170, 5190, 5210, 5230 };static const u_int16_t rcl3[] = /* 2.4Ghz ch: 1,6,11,7,13 */{ 2412, 2437, 2462, 2442, 2472 };static const u_int16_t rcl4[] = /* 5 FCC channel: 149, 153, 161, 165 */{ 5745, 5765, 5785, 5805, 5825 };static const u_int16_t rcl7[] = /* 11 ETSI channel: 100, 104, 108, 112, * 116, 120, 124, 128, * 132, 136, 140 */{ 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 };static const u_int16_t rcl8[] = /* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */{ 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 };static const u_int16_t rcl9[] = /* 2.4Ghz ch: 14 */{ 2484 };static const u_int16_t rcl10[] = /* Added Korean channels 2312-2372 */{ 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 };static const u_int16_t rcl11[] = /* Added Japan channels in 4.9/5.0 spectrum */{ 5040, 5060, 5080, 4920, 4940, 4960, 4980 };#ifdef ATH_TURBO_SCANstatic const u_int16_t rcl5[] = /* 3 static turbo channels */{ 5210, 5250, 5290 };static const u_int16_t rcl6[] = /* 2 static turbo channels */{ 5760, 5800 };static const u_int16_t rcl6x[] = /* 4 FCC3 turbo channels */{ 5540, 5580, 5620, 5660 };static const u_int16_t rcl12[] = /* 2.4Ghz Turbo channel 6 */{ 2437 };static const u_int16_t rcl13[] = /* dynamic Turbo channels */{ 5200, 5240, 5280, 5765, 5805 };#endif /* ATH_TURBO_SCAN */struct scanlist { u_int16_t mode; u_int16_t count; const u_int16_t *list;};#define IEEE80211_MODE_TURBO_STATIC_A IEEE80211_MODE_MAX#define X(a) .count = ARRAY_SIZE(a), .list = astatic const struct scanlist staScanTable[] = { { IEEE80211_MODE_11B, X(rcl3) }, { IEEE80211_MODE_11A, X(rcl1) }, { IEEE80211_MODE_11A, X(rcl2) }, { IEEE80211_MODE_11B, X(rcl8) }, { IEEE80211_MODE_11B, X(rcl9) }, { IEEE80211_MODE_11A, X(rcl4) },#ifdef ATH_TURBO_SCAN { IEEE80211_MODE_TURBO_STATIC_A, X(rcl5) }, { IEEE80211_MODE_TURBO_STATIC_A, X(rcl6) }, { IEEE80211_MODE_TURBO_A, X(rcl6x) }, { IEEE80211_MODE_TURBO_A, X(rcl13) },#endif /* ATH_TURBO_SCAN */ { IEEE80211_MODE_11A, X(rcl7) }, { IEEE80211_MODE_11B, X(rcl10) }, { IEEE80211_MODE_11A, X(rcl11) },#ifdef ATH_TURBO_SCAN { IEEE80211_MODE_TURBO_G, X(rcl12) },#endif /* ATH_TURBO_SCAN */ { .list = NULL }};#undef X/* This function must be invoked with locks acquired */static voidadd_channels(struct ieee80211com *ic, struct ieee80211_scan_state *ss, enum ieee80211_phymode mode, const u_int16_t freq[], int nfreq){ struct ieee80211_channel *c, *cg; u_int modeflags; int i; KASSERT(mode < ARRAY_SIZE(chanflags), ("Unexpected mode %u", mode)); modeflags = chanflags[mode]; for (i = 0; i < nfreq; i++) { c = ieee80211_find_channel(ic, freq[i], modeflags); if ((c == NULL) || isclr(ic->ic_chan_active, c->ic_ieee)) continue; if (mode == IEEE80211_MODE_AUTO) { /* XXX special-case 11b/g channels so we select * the g channel if both are present. */ if (IEEE80211_IS_CHAN_B(c) && (cg = find11gchannel(ic, i, c->ic_freq)) != NULL) c = cg; } if (ss->ss_last >= IEEE80211_SCAN_MAX) break; ss->ss_chans[ss->ss_last++] = c; }}/* This function must be invoked with locks acquired */static intchecktable(const struct scanlist *scan, const struct ieee80211_channel *c){ int i; for (; scan->list != NULL; scan++) { for (i = 0; i < scan->count; i++) if (scan->list[i] == c->ic_freq) return 1; } return 0;}/* * Attach prior to any scanning work. */static intap_attach(struct ieee80211_scan_state *ss){ struct ap_state *as; _MOD_INC_USE(THIS_MODULE, return 0); MALLOC(as, struct ap_state *, sizeof(struct ap_state), M_80211_SCAN, M_NOWAIT | M_ZERO); if (as == NULL) return 0; SCAN_AP_LOCK_INIT(as, "scan_ap"); SCAN_AP_GEN_LOCK_INIT(as, "scan_ap_gen"); TAILQ_INIT(&as->as_entry); IEEE80211_INIT_TQUEUE(&as->as_actiontq, action_tasklet, ss); ss->ss_priv = as; ap_flush(ss); return 1;}/* * Cleanup any private state. */static intap_detach(struct ieee80211_scan_state *ss){ struct ap_state *as = ss->ss_priv; if (as != NULL) { ap_flush(ss); IEEE80211_CANCEL_TQUEUE(&as->as_actiontq); FREE(as, M_80211_SCAN); } _MOD_DEC_USE(THIS_MODULE); return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -