ieee80211_node.h revision 167439
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3139530Ssam * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116742Ssam *    notice, this list of conditions and the following disclaimer.
11116742Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116742Ssam *    notice, this list of conditions and the following disclaimer in the
13116742Ssam *    documentation and/or other materials provided with the distribution.
14116904Ssam * 3. The name of the author may not be used to endorse or promote products
15116904Ssam *    derived from this software without specific prior written permission.
16116742Ssam *
17116904Ssam * Alternatively, this software may be distributed under the terms of the
18116904Ssam * GNU General Public License ("GPL") version 2 as published by the Free
19116904Ssam * Software Foundation.
20116742Ssam *
21116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31116904Ssam *
32116742Ssam * $FreeBSD: head/sys/net80211/ieee80211_node.h 167439 2007-03-11 07:06:08Z sam $
33116742Ssam */
34116742Ssam#ifndef _NET80211_IEEE80211_NODE_H_
35116742Ssam#define _NET80211_IEEE80211_NODE_H_
36116742Ssam
37138568Ssam#include <net80211/ieee80211_ioctl.h>		/* for ieee80211_nodestats */
38116742Ssam
39138568Ssam/*
40138568Ssam * Each ieee80211com instance has a single timer that fires once a
41138568Ssam * second.  This is used to initiate various work depending on the
42138568Ssam * state of the instance: scanning (passive or active), ``transition''
43138568Ssam * (waiting for a response to a management frame when operating
44138568Ssam * as a station), and node inactivity processing (when operating
45138568Ssam * as an AP).  For inactivity processing each node has a timeout
46138568Ssam * set in it's ni_inact field that is decremented on each timeout
47138568Ssam * and the node is reclaimed when the counter goes to zero.  We
48138568Ssam * use different inactivity timeout values depending on whether
49138568Ssam * the node is associated and authorized (either by 802.1x or
50138568Ssam * open/shared key authentication) or associated but yet to be
51138568Ssam * authorized.  The latter timeout is shorter to more aggressively
52138568Ssam * reclaim nodes that leave part way through the 802.1x exchange.
53138568Ssam */
54138568Ssam#define	IEEE80211_INACT_WAIT	15		/* inactivity interval (secs) */
55138568Ssam#define	IEEE80211_INACT_INIT	(30/IEEE80211_INACT_WAIT)	/* initial */
56138568Ssam#define	IEEE80211_INACT_AUTH	(180/IEEE80211_INACT_WAIT)	/* associated but not authorized */
57138568Ssam#define	IEEE80211_INACT_RUN	(300/IEEE80211_INACT_WAIT)	/* authorized */
58138568Ssam#define	IEEE80211_INACT_PROBE	(30/IEEE80211_INACT_WAIT)	/* probe */
59138568Ssam#define	IEEE80211_INACT_SCAN	(300/IEEE80211_INACT_WAIT)	/* scanned */
60138568Ssam
61138568Ssam#define	IEEE80211_TRANS_WAIT 	5		/* mgt frame tx timer (secs) */
62138568Ssam
63116742Ssam#define	IEEE80211_NODE_HASHSIZE	32
64116742Ssam/* simple hash is enough for variation of macaddr */
65116742Ssam#define	IEEE80211_NODE_HASH(addr)	\
66138568Ssam	(((const u_int8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % \
67138568Ssam		IEEE80211_NODE_HASHSIZE)
68116742Ssam
69138568Ssamstruct ieee80211_rsnparms {
70138568Ssam	u_int8_t	rsn_mcastcipher;	/* mcast/group cipher */
71138568Ssam	u_int8_t	rsn_mcastkeylen;	/* mcast key length */
72138568Ssam	u_int8_t	rsn_ucastcipherset;	/* unicast cipher set */
73138568Ssam	u_int8_t	rsn_ucastcipher;	/* selected unicast cipher */
74138568Ssam	u_int8_t	rsn_ucastkeylen;	/* unicast key length */
75138568Ssam	u_int8_t	rsn_keymgmtset;		/* key mangement algorithms */
76138568Ssam	u_int8_t	rsn_keymgmt;		/* selected key mgmt algo */
77138568Ssam	u_int16_t	rsn_caps;		/* capabilities */
78116742Ssam};
79116742Ssam
80138568Ssamstruct ieee80211_node_table;
81138568Ssamstruct ieee80211com;
82138568Ssam
83116742Ssam/*
84116742Ssam * Node specific information.  Note that drivers are expected
85116742Ssam * to derive from this structure to add device-specific per-node
86116742Ssam * state.  This is done by overriding the ic_node_* methods in
87116742Ssam * the ieee80211com structure.
88116742Ssam */
89116742Ssamstruct ieee80211_node {
90138568Ssam	struct ieee80211com	*ni_ic;
91138568Ssam	struct ieee80211_node_table *ni_table;
92116742Ssam	TAILQ_ENTRY(ieee80211_node)	ni_list;
93116742Ssam	LIST_ENTRY(ieee80211_node)	ni_hash;
94116742Ssam	u_int			ni_refcnt;
95120483Ssam	u_int			ni_scangen;	/* gen# for timeout scan */
96138568Ssam	u_int8_t		ni_authmode;	/* authentication algorithm */
97138568Ssam	u_int16_t		ni_flags;	/* special-purpose state */
98138568Ssam#define	IEEE80211_NODE_AUTH	0x0001		/* authorized for data */
99138568Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
100138568Ssam#define	IEEE80211_NODE_ERP	0x0004		/* ERP enabled */
101138568Ssam/* NB: this must have the same value as IEEE80211_FC1_PWR_MGT */
102138568Ssam#define	IEEE80211_NODE_PWR_MGT	0x0010		/* power save mode enabled */
103147788Ssam#define	IEEE80211_NODE_AREF	0x0020		/* authentication ref held */
104138568Ssam	u_int16_t		ni_associd;	/* assoc response */
105138568Ssam	u_int16_t		ni_txpower;	/* current transmit power */
106138568Ssam	u_int16_t		ni_vlan;	/* vlan tag */
107138568Ssam	u_int32_t		*ni_challenge;	/* shared-key challenge */
108138568Ssam	u_int8_t		*ni_wpa_ie;	/* captured WPA/RSN ie */
109138568Ssam	u_int8_t		*ni_wme_ie;	/* captured WME ie */
110167439Ssam#define	IEEE80211_NONQOS_TID	16		/* index for non-QoS sta */
111138568Ssam	u_int16_t		ni_txseqs[17];	/* tx seq per-tid */
112138568Ssam	u_int16_t		ni_rxseqs[17];	/* rx seq previous per-tid*/
113138568Ssam	u_int32_t		ni_rxfragstamp;	/* time stamp of last rx frag */
114138568Ssam	struct mbuf		*ni_rxfrag[3];	/* rx frag reassembly */
115138568Ssam	struct ieee80211_rsnparms ni_rsn;	/* RSN/WPA parameters */
116138568Ssam	struct ieee80211_key	ni_ucastkey;	/* unicast key */
117116742Ssam
118116742Ssam	/* hardware */
119119150Ssam	u_int32_t		ni_rstamp;	/* recv timestamp */
120116742Ssam	u_int8_t		ni_rssi;	/* recv ssi */
121116742Ssam
122116742Ssam	/* header */
123116742Ssam	u_int8_t		ni_macaddr[IEEE80211_ADDR_LEN];
124116742Ssam	u_int8_t		ni_bssid[IEEE80211_ADDR_LEN];
125116742Ssam
126116742Ssam	/* beacon, probe response */
127138568Ssam	union {
128138568Ssam		u_int8_t	data[8];
129138568Ssam		u_int64_t	tsf;
130138568Ssam	} ni_tstamp;				/* from last rcv'd beacon */
131116742Ssam	u_int16_t		ni_intval;	/* beacon interval */
132116742Ssam	u_int16_t		ni_capinfo;	/* capabilities */
133116742Ssam	u_int8_t		ni_esslen;
134116742Ssam	u_int8_t		ni_essid[IEEE80211_NWID_LEN];
135116742Ssam	struct ieee80211_rateset ni_rates;	/* negotiated rate set */
136148936Ssam	struct ieee80211_channel *ni_chan;	/* XXX multiple uses */
137116742Ssam	u_int16_t		ni_fhdwell;	/* FH only */
138116742Ssam	u_int8_t		ni_fhindex;	/* FH only */
139138568Ssam	u_int8_t		ni_erp;		/* ERP from beacon/probe resp */
140138568Ssam	u_int16_t		ni_timoff;	/* byte offset to TIM ie */
141147152Ssam	u_int8_t		ni_dtim_period;	/* DTIM period */
142147152Ssam	u_int8_t		ni_dtim_count;	/* DTIM count for last bcn */
143116742Ssam
144116742Ssam	/* others */
145116742Ssam	int			ni_fails;	/* failure count to associate */
146138568Ssam	short			ni_inact;	/* inactivity mark count */
147138568Ssam	short			ni_inact_reload;/* inactivity reload value */
148116742Ssam	int			ni_txrate;	/* index to ni_rates[] */
149138568Ssam	struct	ifqueue		ni_savedq;	/* ps-poll queue */
150138568Ssam	struct ieee80211_nodestats ni_stats;	/* per-node statistics */
151116742Ssam};
152138568SsamMALLOC_DECLARE(M_80211_NODE);
153116742Ssam
154138568Ssam#define	IEEE80211_NODE_AID(ni)	IEEE80211_AID(ni->ni_associd)
155138568Ssam
156138568Ssam#define	IEEE80211_NODE_STAT(ni,stat)	(ni->ni_stats.ns_##stat++)
157138568Ssam#define	IEEE80211_NODE_STAT_ADD(ni,stat,v)	(ni->ni_stats.ns_##stat += v)
158138568Ssam#define	IEEE80211_NODE_STAT_SET(ni,stat,v)	(ni->ni_stats.ns_##stat = v)
159138568Ssam
160116742Ssamstatic __inline struct ieee80211_node *
161116742Ssamieee80211_ref_node(struct ieee80211_node *ni)
162116742Ssam{
163138568Ssam	ieee80211_node_incref(ni);
164116742Ssam	return ni;
165116742Ssam}
166116742Ssam
167116742Ssamstatic __inline void
168116742Ssamieee80211_unref_node(struct ieee80211_node **ni)
169116742Ssam{
170138568Ssam	ieee80211_node_decref(*ni);
171116742Ssam	*ni = NULL;			/* guard against use */
172116742Ssam}
173116742Ssam
174116742Ssamstruct ieee80211com;
175116742Ssam
176144618Ssamvoid	ieee80211_node_attach(struct ieee80211com *);
177144618Ssamvoid	ieee80211_node_lateattach(struct ieee80211com *);
178144618Ssamvoid	ieee80211_node_detach(struct ieee80211com *);
179127877Ssam
180138568Ssamstatic __inline int
181138568Ssamieee80211_node_is_authorized(const struct ieee80211_node *ni)
182138568Ssam{
183138568Ssam	return (ni->ni_flags & IEEE80211_NODE_AUTH);
184138568Ssam}
185116742Ssam
186148302Ssamvoid	ieee80211_node_authorize(struct ieee80211_node *);
187148302Ssamvoid	ieee80211_node_unauthorize(struct ieee80211_node *);
188138568Ssam
189144618Ssamvoid	ieee80211_begin_scan(struct ieee80211com *, int);
190144618Ssamint	ieee80211_next_scan(struct ieee80211com *);
191156358Ssamvoid	ieee80211_probe_curchan(struct ieee80211com *, int);
192144618Ssamvoid	ieee80211_create_ibss(struct ieee80211com*, struct ieee80211_channel *);
193144618Ssamvoid	ieee80211_reset_bss(struct ieee80211com *);
194144618Ssamvoid	ieee80211_cancel_scan(struct ieee80211com *);
195144618Ssamvoid	ieee80211_end_scan(struct ieee80211com *);
196148306Ssamint	ieee80211_ibss_merge(struct ieee80211_node *);
197144618Ssamint	ieee80211_sta_join(struct ieee80211com *, struct ieee80211_node *);
198144618Ssamvoid	ieee80211_sta_leave(struct ieee80211com *, struct ieee80211_node *);
199138568Ssam
200138568Ssam/*
201138568Ssam * Table of ieee80211_node instances.  Each ieee80211com
202138568Ssam * has at least one for holding the scan candidates.
203138568Ssam * When operating as an access point or in ibss mode there
204138568Ssam * is a second table for associated stations or neighbors.
205138568Ssam */
206138568Ssamstruct ieee80211_node_table {
207138568Ssam	struct ieee80211com	*nt_ic;		/* back reference */
208138568Ssam	ieee80211_node_lock_t	nt_nodelock;	/* on node table */
209138568Ssam	TAILQ_HEAD(, ieee80211_node) nt_node;	/* information of all nodes */
210138568Ssam	LIST_HEAD(, ieee80211_node) nt_hash[IEEE80211_NODE_HASHSIZE];
211138568Ssam	const char		*nt_name;	/* for debugging */
212138568Ssam	ieee80211_scan_lock_t	nt_scanlock;	/* on nt_scangen */
213138568Ssam	u_int			nt_scangen;	/* gen# for timeout scan */
214138568Ssam	int			nt_inact_timer;	/* inactivity timer */
215138568Ssam	int			nt_inact_init;	/* initial node inact setting */
216148863Ssam	struct ieee80211_node	**nt_keyixmap;	/* key ix -> node map */
217148863Ssam	int			nt_keyixmax;	/* keyixmap size */
218138568Ssam
219138568Ssam	void			(*nt_timeout)(struct ieee80211_node_table *);
220138568Ssam};
221144618Ssamvoid	ieee80211_node_table_reset(struct ieee80211_node_table *);
222138568Ssam
223144618Ssamstruct ieee80211_node *ieee80211_alloc_node(
224138568Ssam		struct ieee80211_node_table *, const u_int8_t *);
225148777Ssamstruct ieee80211_node *ieee80211_tmp_node(struct ieee80211com *,
226148777Ssam		const u_int8_t *macaddr);
227144618Ssamstruct ieee80211_node *ieee80211_dup_bss(struct ieee80211_node_table *,
228138568Ssam		const u_int8_t *);
229138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
230144618Ssamvoid	ieee80211_free_node_debug(struct ieee80211_node *,
231138568Ssam		const char *func, int line);
232144618Ssamstruct ieee80211_node *ieee80211_find_node_debug(
233138568Ssam		struct ieee80211_node_table *, const u_int8_t *,
234138568Ssam		const char *func, int line);
235144618Ssamstruct ieee80211_node * ieee80211_find_rxnode_debug(
236138568Ssam		struct ieee80211com *, const struct ieee80211_frame_min *,
237138568Ssam		const char *func, int line);
238148863Ssamstruct ieee80211_node * ieee80211_find_rxnode_withkey_debug(
239148863Ssam		struct ieee80211com *,
240148863Ssam		const struct ieee80211_frame_min *, u_int16_t keyix,
241148863Ssam		const char *func, int line);
242144618Ssamstruct ieee80211_node *ieee80211_find_txnode_debug(
243138568Ssam		struct ieee80211com *, const u_int8_t *,
244138568Ssam		const char *func, int line);
245144618Ssamstruct ieee80211_node *ieee80211_find_node_with_channel_debug(
246138568Ssam		struct ieee80211_node_table *, const u_int8_t *macaddr,
247138568Ssam		struct ieee80211_channel *, const char *func, int line);
248144618Ssamstruct ieee80211_node *ieee80211_find_node_with_ssid_debug(
249138568Ssam		struct ieee80211_node_table *, const u_int8_t *macaddr,
250138568Ssam		u_int ssidlen, const u_int8_t *ssid,
251138568Ssam		const char *func, int line);
252138568Ssam#define	ieee80211_free_node(ni) \
253138568Ssam	ieee80211_free_node_debug(ni, __func__, __LINE__)
254138568Ssam#define	ieee80211_find_node(nt, mac) \
255138568Ssam	ieee80211_find_node_debug(nt, mac, __func__, __LINE__)
256138568Ssam#define	ieee80211_find_rxnode(nt, wh) \
257138568Ssam	ieee80211_find_rxnode_debug(nt, wh, __func__, __LINE__)
258148863Ssam#define	ieee80211_find_rxnode_withkey(nt, wh, keyix) \
259148863Ssam	ieee80211_find_rxnode_withkey_debug(nt, wh, keyix, __func__, __LINE__)
260138568Ssam#define	ieee80211_find_txnode(nt, mac) \
261138568Ssam	ieee80211_find_txnode_debug(nt, mac, __func__, __LINE__)
262138568Ssam#define	ieee80211_find_node_with_channel(nt, mac, c) \
263138568Ssam	ieee80211_find_node_with_channel_debug(nt, mac, c, __func__, __LINE__)
264138568Ssam#define	ieee80211_find_node_with_ssid(nt, mac, sl, ss) \
265138568Ssam	ieee80211_find_node_with_ssid_debug(nt, mac, sl, ss, __func__, __LINE__)
266138568Ssam#else
267144618Ssamvoid	ieee80211_free_node(struct ieee80211_node *);
268144618Ssamstruct ieee80211_node *ieee80211_find_node(
269138568Ssam		struct ieee80211_node_table *, const u_int8_t *);
270144618Ssamstruct ieee80211_node * ieee80211_find_rxnode(
271138568Ssam		struct ieee80211com *, const struct ieee80211_frame_min *);
272148863Ssamstruct ieee80211_node * ieee80211_find_rxnode_withkey(struct ieee80211com *,
273148863Ssam		const struct ieee80211_frame_min *, u_int16_t keyix);
274144618Ssamstruct ieee80211_node *ieee80211_find_txnode(
275138568Ssam		struct ieee80211com *, const u_int8_t *);
276144618Ssamstruct ieee80211_node *ieee80211_find_node_with_channel(
277138568Ssam		struct ieee80211_node_table *, const u_int8_t *macaddr,
278138568Ssam		struct ieee80211_channel *);
279144618Ssamstruct ieee80211_node *ieee80211_find_node_with_ssid(
280138568Ssam		struct ieee80211_node_table *, const u_int8_t *macaddr,
281138568Ssam		u_int ssidlen, const u_int8_t *ssid);
282138568Ssam#endif
283148863Ssamint	ieee80211_node_delucastkey(struct ieee80211_node *);
284138568Ssam
285116742Ssamtypedef void ieee80211_iter_func(void *, struct ieee80211_node *);
286144618Ssamvoid	ieee80211_iterate_nodes(struct ieee80211_node_table *,
287116742Ssam		ieee80211_iter_func *, void *);
288116742Ssam
289144618Ssamvoid	ieee80211_dump_node(struct ieee80211_node_table *,
290138568Ssam		struct ieee80211_node *);
291144618Ssamvoid	ieee80211_dump_nodes(struct ieee80211_node_table *);
292138568Ssam
293144618Ssamstruct ieee80211_node *ieee80211_fakeup_adhoc_node(
294144618Ssam		struct ieee80211_node_table *, const u_int8_t macaddr[]);
295144618Ssamvoid	ieee80211_node_join(struct ieee80211com *, struct ieee80211_node *,int);
296144618Ssamvoid	ieee80211_node_leave(struct ieee80211com *, struct ieee80211_node *);
297144618Ssamu_int8_t ieee80211_getrssi(struct ieee80211com *ic);
298148936Ssam
299148936Ssam/*
300148936Ssam * Parameters supplied when adding/updating an entry in a
301148936Ssam * scan cache.  Pointer variables should be set to NULL
302148936Ssam * if no data is available.  Pointer references can be to
303148936Ssam * local data; any information that is saved will be copied.
304148936Ssam * All multi-byte values must be in host byte order.
305148936Ssam */
306148936Ssamstruct ieee80211_scanparams {
307148936Ssam	u_int16_t	capinfo;	/* 802.11 capabilities */
308148936Ssam	u_int16_t	fhdwell;	/* FHSS dwell interval */
309148936Ssam	u_int8_t	chan;		/* */
310148936Ssam	u_int8_t	bchan;
311148936Ssam	u_int8_t	fhindex;
312148936Ssam	u_int8_t	erp;
313148936Ssam	u_int16_t	bintval;
314148936Ssam	u_int8_t	timoff;
315148936Ssam	u_int8_t	*tim;
316148936Ssam	u_int8_t	*tstamp;
317148936Ssam	u_int8_t	*country;
318148936Ssam	u_int8_t	*ssid;
319148936Ssam	u_int8_t	*rates;
320148936Ssam	u_int8_t	*xrates;
321148936Ssam	u_int8_t	*wpa;
322148936Ssam	u_int8_t	*wme;
323148936Ssam};
324148936Ssam
325148936Ssamvoid	ieee80211_add_scan(struct ieee80211com *,
326148936Ssam		const struct ieee80211_scanparams *,
327148936Ssam		const struct ieee80211_frame *,
328148936Ssam		int subtype, int rssi, int rstamp);
329153073Ssamvoid	ieee80211_init_neighbor(struct ieee80211_node *,
330153073Ssam		const struct ieee80211_frame *,
331153073Ssam		const struct ieee80211_scanparams *);
332148936Ssamstruct ieee80211_node *ieee80211_add_neighbor(struct ieee80211com *,
333148936Ssam		const struct ieee80211_frame *,
334148936Ssam		const struct ieee80211_scanparams *);
335116742Ssam#endif /* _NET80211_IEEE80211_NODE_H_ */
336