ieee80211_scan.h revision 186904
1170530Ssam/*-
2186904Ssam * Copyright (c) 2005-2009 Sam Leffler, Errno Consulting
3170530Ssam * All rights reserved.
4170530Ssam *
5170530Ssam * Redistribution and use in source and binary forms, with or without
6170530Ssam * modification, are permitted provided that the following conditions
7170530Ssam * are met:
8170530Ssam * 1. Redistributions of source code must retain the above copyright
9170530Ssam *    notice, this list of conditions and the following disclaimer.
10170530Ssam * 2. Redistributions in binary form must reproduce the above copyright
11170530Ssam *    notice, this list of conditions and the following disclaimer in the
12170530Ssam *    documentation and/or other materials provided with the distribution.
13170530Ssam *
14170530Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15170530Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16170530Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17170530Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18170530Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19170530Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20170530Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21170530Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22170530Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23170530Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24170530Ssam *
25170530Ssam * $FreeBSD: head/sys/net80211/ieee80211_scan.h 186904 2009-01-08 17:12:47Z sam $
26170530Ssam */
27170530Ssam#ifndef _NET80211_IEEE80211_SCAN_H_
28170530Ssam#define _NET80211_IEEE80211_SCAN_H_
29170530Ssam
30178354Ssam/*
31178354Ssam * 802.11 scanning support.
32178354Ssam *
33178354Ssam * Scanning is the procedure by which a station locates a bss to join
34178354Ssam * (infrastructure/ibss mode), or a channel to use (when operating as
35178354Ssam * an ap or ibss master).  Scans are either "active" or "passive".  An
36178354Ssam * active scan causes one or more probe request frames to be sent on
37178354Ssam * visiting each channel.  A passive request causes each channel in the
38178354Ssam * scan set to be visited but no frames to be transmitted; the station
39178354Ssam * only listens for traffic.  Note that active scanning may still need
40178354Ssam * to listen for traffic before sending probe request frames depending
41178354Ssam * on regulatory constraints; the 802.11 layer handles this by generating
42178354Ssam * a callback when scanning on a ``passive channel'' when the
43178354Ssam * IEEE80211_FEXT_PROBECHAN flag is set.
44178354Ssam *
45178354Ssam * A scan operation involves constructing a set of channels to inspec
46178354Ssam * (the scan set), visiting each channel and collecting information
47178354Ssam * (e.g. what bss are present), and then analyzing the results to make
48178354Ssam * decisions like which bss to join.  This process needs to be as fast
49178354Ssam * as possible so we do things like intelligently construct scan sets
50178354Ssam * and dwell on a channel only as long as necessary.  The scan code also
51178354Ssam * maintains a cache of recent scan results and uses it to bypass scanning
52178354Ssam * whenever possible.  The scan cache is also used to enable roaming
53178354Ssam * between access points when operating in infrastructure mode.
54178354Ssam *
55178354Ssam * Scanning is handled with pluggable modules that implement "policy"
56178354Ssam * per-operating mode.  The core scanning support provides an
57178354Ssam * instrastructure to support these modules and exports a common api
58178354Ssam * to the rest of the 802.11 layer.  Policy modules decide what
59178354Ssam * channels to visit, what state to record to make decisions (e.g. ap
60178354Ssam * mode scanning for auto channel selection keeps significantly less
61178354Ssam * state than sta mode scanning for an ap to associate to), and selects
62178354Ssam * the final station/channel to return as the result of a scan.
63178354Ssam *
64178354Ssam * Scanning is done synchronously when initially bringing a vap to an
65178354Ssam * operational state and optionally in the background to maintain the
66178354Ssam * scan cache for doing roaming and rogue ap monitoring.  Scanning is
67178354Ssam * not tied to the 802.11 state machine that governs vaps though there
68178354Ssam * is linkage to the IEEE80211_SCAN state.  Only one vap at a time may
69178354Ssam * be scanning; this scheduling policy is handled in ieee80211_new_state
70178354Ssam * and is invisible to the scanning code.
71178354Ssam*/
72170530Ssam#define	IEEE80211_SCAN_MAX	IEEE80211_CHAN_MAX
73170530Ssam
74178354Ssamstruct ieee80211_scanner;			/* scan policy state */
75170530Ssam
76170530Ssamstruct ieee80211_scan_ssid {
77178354Ssam	int	 len;				/* length in bytes */
78178354Ssam	uint8_t ssid[IEEE80211_NWID_LEN];	/* ssid contents */
79170530Ssam};
80178354Ssam#define	IEEE80211_SCAN_MAX_SSID	1		/* max # ssid's to probe */
81170530Ssam
82178354Ssam/*
83178354Ssam * Scan state visible to the 802.11 layer.  Scan parameters and
84178354Ssam * results are stored in this data structure.  The ieee80211_scan_state
85178354Ssam * structure is extended with space that is maintained private to
86178354Ssam * the core scanning support.  We allocate one instance and link it
87178354Ssam * to the ieee80211com structure; then share it between all associated
88178354Ssam * vaps.  We could allocate multiple of these, e.g. to hold multiple
89178354Ssam * scan results, but this is sufficient for current needs.
90178354Ssam */
91170530Ssamstruct ieee80211_scan_state {
92178354Ssam	struct ieee80211vap *ss_vap;
93170530Ssam	const struct ieee80211_scanner *ss_ops;	/* policy hookup, see below */
94170530Ssam	void		*ss_priv;		/* scanner private state */
95170530Ssam	uint16_t	ss_flags;
96170530Ssam#define	IEEE80211_SCAN_NOPICK	0x0001		/* scan only, no selection */
97170530Ssam#define	IEEE80211_SCAN_ACTIVE	0x0002		/* active scan (probe req) */
98170530Ssam#define	IEEE80211_SCAN_PICK1ST	0x0004		/* ``hey sailor'' mode */
99170530Ssam#define	IEEE80211_SCAN_BGSCAN	0x0008		/* bg scan, exit ps at end */
100170530Ssam#define	IEEE80211_SCAN_ONCE	0x0010		/* do one complete pass */
101178354Ssam#define	IEEE80211_SCAN_NOBCAST	0x0020		/* no broadcast probe req */
102178354Ssam#define	IEEE80211_SCAN_NOJOIN	0x0040		/* no auto-sequencing */
103170530Ssam#define	IEEE80211_SCAN_GOTPICK	0x1000		/* got candidate, can stop */
104170530Ssam	uint8_t		ss_nssid;		/* # ssid's to probe/match */
105170530Ssam	struct ieee80211_scan_ssid ss_ssid[IEEE80211_SCAN_MAX_SSID];
106170530Ssam						/* ssid's to probe/match */
107170530Ssam						/* ordered channel set */
108170530Ssam	struct ieee80211_channel *ss_chans[IEEE80211_SCAN_MAX];
109170530Ssam	uint16_t	ss_next;		/* ix of next chan to scan */
110170530Ssam	uint16_t	ss_last;		/* ix+1 of last chan to scan */
111170530Ssam	unsigned long	ss_mindwell;		/* min dwell on channel */
112170530Ssam	unsigned long	ss_maxdwell;		/* max dwell on channel */
113170530Ssam};
114170530Ssam
115170530Ssam/*
116170530Ssam * The upper 16 bits of the flags word is used to communicate
117170530Ssam * information to the scanning code that is NOT recorded in
118170530Ssam * ss_flags.  It might be better to split this stuff out into
119170530Ssam * a separate variable to avoid confusion.
120170530Ssam */
121178354Ssam#define	IEEE80211_SCAN_FLUSH	0x00010000	/* flush candidate table */
122178354Ssam#define	IEEE80211_SCAN_NOSSID	0x80000000	/* don't update ssid list */
123170530Ssam
124170530Ssamstruct ieee80211com;
125170530Ssamvoid	ieee80211_scan_attach(struct ieee80211com *);
126170530Ssamvoid	ieee80211_scan_detach(struct ieee80211com *);
127178354Ssamvoid	ieee80211_scan_vattach(struct ieee80211vap *);
128178354Ssamvoid	ieee80211_scan_vdetach(struct ieee80211vap *);
129170530Ssam
130170530Ssamvoid	ieee80211_scan_dump_channels(const struct ieee80211_scan_state *);
131170530Ssam
132170530Ssam#define	IEEE80211_SCAN_FOREVER	0x7fffffff
133178354Ssamint	ieee80211_start_scan(struct ieee80211vap *, int flags,
134178354Ssam		u_int duration, u_int mindwell, u_int maxdwell,
135170530Ssam		u_int nssid, const struct ieee80211_scan_ssid ssids[]);
136178354Ssamint	ieee80211_check_scan(struct ieee80211vap *, int flags,
137178354Ssam		u_int duration, u_int mindwell, u_int maxdwell,
138170530Ssam		u_int nssid, const struct ieee80211_scan_ssid ssids[]);
139178354Ssamint	ieee80211_check_scan_current(struct ieee80211vap *);
140178354Ssamint	ieee80211_bg_scan(struct ieee80211vap *, int);
141178354Ssamvoid	ieee80211_cancel_scan(struct ieee80211vap *);
142178354Ssamvoid	ieee80211_cancel_anyscan(struct ieee80211vap *);
143178354Ssamvoid	ieee80211_scan_next(struct ieee80211vap *);
144178354Ssamvoid	ieee80211_scan_done(struct ieee80211vap *);
145178354Ssamvoid	ieee80211_probe_curchan(struct ieee80211vap *, int);
146178354Ssamstruct ieee80211_channel *ieee80211_scan_pickchannel(struct ieee80211com *, int);
147170530Ssam
148170530Ssamstruct ieee80211_scanparams;
149178354Ssamvoid	ieee80211_add_scan(struct ieee80211vap *,
150170530Ssam		const struct ieee80211_scanparams *,
151170530Ssam		const struct ieee80211_frame *,
152170530Ssam		int subtype, int rssi, int noise, int rstamp);
153170530Ssamvoid	ieee80211_scan_timeout(struct ieee80211com *);
154170530Ssam
155178354Ssamvoid	ieee80211_scan_assoc_success(struct ieee80211vap *,
156170530Ssam		const uint8_t mac[IEEE80211_ADDR_LEN]);
157170530Ssamenum {
158170530Ssam	IEEE80211_SCAN_FAIL_TIMEOUT	= 1,	/* no response to mgmt frame */
159170530Ssam	IEEE80211_SCAN_FAIL_STATUS	= 2	/* negative response to " " */
160170530Ssam};
161178354Ssamvoid	ieee80211_scan_assoc_fail(struct ieee80211vap *,
162170530Ssam		const uint8_t mac[IEEE80211_ADDR_LEN], int reason);
163178354Ssamvoid	ieee80211_scan_flush(struct ieee80211vap *);
164170530Ssam
165170530Ssamstruct ieee80211_scan_entry;
166170530Ssamtypedef void ieee80211_scan_iter_func(void *,
167170530Ssam		const struct ieee80211_scan_entry *);
168178354Ssamvoid	ieee80211_scan_iterate(struct ieee80211vap *,
169170530Ssam		ieee80211_scan_iter_func, void *);
170178354Ssamenum {
171178354Ssam	IEEE80211_BPARSE_BADIELEN	= 0x01,	/* ie len past end of frame */
172178354Ssam	IEEE80211_BPARSE_RATES_INVALID	= 0x02,	/* invalid RATES ie */
173178354Ssam	IEEE80211_BPARSE_XRATES_INVALID	= 0x04,	/* invalid XRATES ie */
174178354Ssam	IEEE80211_BPARSE_SSID_INVALID	= 0x08,	/* invalid SSID ie */
175178354Ssam	IEEE80211_BPARSE_CHAN_INVALID	= 0x10,	/* invalid FH/DSPARMS chan */
176178354Ssam	IEEE80211_BPARSE_OFFCHAN	= 0x20,	/* DSPARMS chan != curchan */
177178354Ssam	IEEE80211_BPARSE_BINTVAL_INVALID= 0x40,	/* invalid beacon interval */
178178354Ssam};
179170530Ssam
180170530Ssam/*
181170530Ssam * Parameters supplied when adding/updating an entry in a
182170530Ssam * scan cache.  Pointer variables should be set to NULL
183170530Ssam * if no data is available.  Pointer references can be to
184170530Ssam * local data; any information that is saved will be copied.
185170530Ssam * All multi-byte values must be in host byte order.
186170530Ssam */
187170530Ssamstruct ieee80211_scanparams {
188178354Ssam	uint8_t		status;		/* bitmask of IEEE80211_BPARSE_* */
189178354Ssam	uint8_t		chan;		/* channel # from FH/DSPARMS */
190178354Ssam	uint8_t		bchan;		/* curchan's channel # */
191178354Ssam	uint8_t		fhindex;
192178354Ssam	uint16_t	fhdwell;	/* FHSS dwell interval */
193170530Ssam	uint16_t	capinfo;	/* 802.11 capabilities */
194178354Ssam	uint16_t	erp;		/* NB: 0x100 indicates ie present */
195170530Ssam	uint16_t	bintval;
196170530Ssam	uint8_t		timoff;
197178354Ssam	uint8_t		*ies;		/* all captured ies */
198178354Ssam	size_t		ies_len;	/* length of all captured ies */
199170530Ssam	uint8_t		*tim;
200170530Ssam	uint8_t		*tstamp;
201170530Ssam	uint8_t		*country;
202170530Ssam	uint8_t		*ssid;
203170530Ssam	uint8_t		*rates;
204170530Ssam	uint8_t		*xrates;
205170530Ssam	uint8_t		*doth;
206170530Ssam	uint8_t		*wpa;
207170530Ssam	uint8_t		*rsn;
208170530Ssam	uint8_t		*wme;
209170530Ssam	uint8_t		*htcap;
210170530Ssam	uint8_t		*htinfo;
211170530Ssam	uint8_t		*ath;
212186904Ssam	uint8_t		*tdma;
213170530Ssam};
214170530Ssam
215170530Ssam/*
216170530Ssam * Scan cache entry format used when exporting data from a policy
217170530Ssam * module; this data may be represented some other way internally.
218170530Ssam */
219170530Ssamstruct ieee80211_scan_entry {
220170530Ssam	uint8_t		se_macaddr[IEEE80211_ADDR_LEN];
221170530Ssam	uint8_t		se_bssid[IEEE80211_ADDR_LEN];
222178354Ssam	/* XXX can point inside se_ies */
223170530Ssam	uint8_t		se_ssid[2+IEEE80211_NWID_LEN];
224170530Ssam	uint8_t		se_rates[2+IEEE80211_RATE_MAXSIZE];
225170530Ssam	uint8_t		se_xrates[2+IEEE80211_RATE_MAXSIZE];
226170530Ssam	uint32_t	se_rstamp;	/* recv timestamp */
227170530Ssam	union {
228170530Ssam		uint8_t		data[8];
229178354Ssam		u_int64_t	tsf;
230170530Ssam	} se_tstamp;			/* from last rcv'd beacon */
231170530Ssam	uint16_t	se_intval;	/* beacon interval (host byte order) */
232170530Ssam	uint16_t	se_capinfo;	/* capabilities (host byte order) */
233170530Ssam	struct ieee80211_channel *se_chan;/* channel where sta found */
234170530Ssam	uint16_t	se_timoff;	/* byte offset to TIM ie */
235170530Ssam	uint16_t	se_fhdwell;	/* FH only (host byte order) */
236170530Ssam	uint8_t		se_fhindex;	/* FH only */
237178354Ssam	uint8_t		se_dtimperiod;	/* DTIM period */
238178354Ssam	uint16_t	se_erp;		/* ERP from beacon/probe resp */
239170530Ssam	int8_t		se_rssi;	/* avg'd recv ssi */
240170530Ssam	int8_t		se_noise;	/* noise floor */
241178354Ssam	uint8_t		se_cc[2];	/* captured country code */
242178354Ssam	struct ieee80211_ies se_ies;	/* captured ie's */
243170530Ssam	u_int		se_age;		/* age of entry (0 on create) */
244170530Ssam};
245170530SsamMALLOC_DECLARE(M_80211_SCAN);
246170530Ssam
247170530Ssam/*
248170530Ssam * Template for an in-kernel scan policy module.
249170530Ssam * Modules register with the scanning code and are
250170530Ssam * typically loaded as needed.
251170530Ssam */
252170530Ssamstruct ieee80211_scanner {
253170530Ssam	const char *scan_name;		/* printable name */
254170530Ssam	int	(*scan_attach)(struct ieee80211_scan_state *);
255170530Ssam	int	(*scan_detach)(struct ieee80211_scan_state *);
256170530Ssam	int	(*scan_start)(struct ieee80211_scan_state *,
257178354Ssam			struct ieee80211vap *);
258170530Ssam	int	(*scan_restart)(struct ieee80211_scan_state *,
259178354Ssam			struct ieee80211vap *);
260170530Ssam	int	(*scan_cancel)(struct ieee80211_scan_state *,
261178354Ssam			struct ieee80211vap *);
262170530Ssam	int	(*scan_end)(struct ieee80211_scan_state *,
263178354Ssam			struct ieee80211vap *);
264170530Ssam	int	(*scan_flush)(struct ieee80211_scan_state *);
265178354Ssam	struct ieee80211_channel *(*scan_pickchan)(
266178354Ssam			struct ieee80211_scan_state *, int);
267170530Ssam	/* add an entry to the cache */
268170530Ssam	int	(*scan_add)(struct ieee80211_scan_state *,
269170530Ssam			const struct ieee80211_scanparams *,
270170530Ssam			const struct ieee80211_frame *,
271170530Ssam			int subtype, int rssi, int noise, int rstamp);
272170530Ssam	/* age and/or purge entries in the cache */
273170530Ssam	void	(*scan_age)(struct ieee80211_scan_state *);
274170530Ssam	/* note that association failed for an entry */
275170530Ssam	void	(*scan_assoc_fail)(struct ieee80211_scan_state *,
276170530Ssam			const uint8_t macaddr[IEEE80211_ADDR_LEN],
277170530Ssam			int reason);
278170530Ssam	/* note that association succeed for an entry */
279170530Ssam	void	(*scan_assoc_success)(struct ieee80211_scan_state *,
280170530Ssam			const uint8_t macaddr[IEEE80211_ADDR_LEN]);
281170530Ssam	/* iterate over entries in the scan cache */
282170530Ssam	void	(*scan_iterate)(struct ieee80211_scan_state *,
283170530Ssam			ieee80211_scan_iter_func *, void *);
284170530Ssam};
285170530Ssamvoid	ieee80211_scanner_register(enum ieee80211_opmode,
286170530Ssam		const struct ieee80211_scanner *);
287170530Ssamvoid	ieee80211_scanner_unregister(enum ieee80211_opmode,
288170530Ssam		const struct ieee80211_scanner *);
289170530Ssamvoid	ieee80211_scanner_unregister_all(const struct ieee80211_scanner *);
290170530Ssamconst struct ieee80211_scanner *ieee80211_scanner_get(enum ieee80211_opmode);
291170530Ssam#endif /* _NET80211_IEEE80211_SCAN_H_ */
292