ieee80211_node.h revision 148863
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 148863 2005-08-08 18:46:36Z 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 */
110138568Ssam	u_int16_t		ni_txseqs[17];	/* tx seq per-tid */
111138568Ssam	u_int16_t		ni_rxseqs[17];	/* rx seq previous per-tid*/
112138568Ssam	u_int32_t		ni_rxfragstamp;	/* time stamp of last rx frag */
113138568Ssam	struct mbuf		*ni_rxfrag[3];	/* rx frag reassembly */
114138568Ssam	struct ieee80211_rsnparms ni_rsn;	/* RSN/WPA parameters */
115138568Ssam	struct ieee80211_key	ni_ucastkey;	/* unicast key */
116116742Ssam
117116742Ssam	/* hardware */
118119150Ssam	u_int32_t		ni_rstamp;	/* recv timestamp */
119116742Ssam	u_int8_t		ni_rssi;	/* recv ssi */
120116742Ssam
121116742Ssam	/* header */
122116742Ssam	u_int8_t		ni_macaddr[IEEE80211_ADDR_LEN];
123116742Ssam	u_int8_t		ni_bssid[IEEE80211_ADDR_LEN];
124116742Ssam
125116742Ssam	/* beacon, probe response */
126138568Ssam	union {
127138568Ssam		u_int8_t	data[8];
128138568Ssam		u_int64_t	tsf;
129138568Ssam	} ni_tstamp;				/* from last rcv'd beacon */
130116742Ssam	u_int16_t		ni_intval;	/* beacon interval */
131116742Ssam	u_int16_t		ni_capinfo;	/* capabilities */
132116742Ssam	u_int8_t		ni_esslen;
133116742Ssam	u_int8_t		ni_essid[IEEE80211_NWID_LEN];
134116742Ssam	struct ieee80211_rateset ni_rates;	/* negotiated rate set */
135116742Ssam	struct ieee80211_channel *ni_chan;
136116742Ssam	u_int16_t		ni_fhdwell;	/* FH only */
137116742Ssam	u_int8_t		ni_fhindex;	/* FH only */
138138568Ssam	u_int8_t		ni_erp;		/* ERP from beacon/probe resp */
139138568Ssam	u_int16_t		ni_timoff;	/* byte offset to TIM ie */
140147152Ssam	u_int8_t		ni_dtim_period;	/* DTIM period */
141147152Ssam	u_int8_t		ni_dtim_count;	/* DTIM count for last bcn */
142116742Ssam
143116742Ssam	/* others */
144116742Ssam	int			ni_fails;	/* failure count to associate */
145138568Ssam	short			ni_inact;	/* inactivity mark count */
146138568Ssam	short			ni_inact_reload;/* inactivity reload value */
147116742Ssam	int			ni_txrate;	/* index to ni_rates[] */
148138568Ssam	struct	ifqueue		ni_savedq;	/* ps-poll queue */
149138568Ssam	struct ieee80211_nodestats ni_stats;	/* per-node statistics */
150116742Ssam};
151138568SsamMALLOC_DECLARE(M_80211_NODE);
152116742Ssam
153138568Ssam#define	IEEE80211_NODE_AID(ni)	IEEE80211_AID(ni->ni_associd)
154138568Ssam
155138568Ssam#define	IEEE80211_NODE_STAT(ni,stat)	(ni->ni_stats.ns_##stat++)
156138568Ssam#define	IEEE80211_NODE_STAT_ADD(ni,stat,v)	(ni->ni_stats.ns_##stat += v)
157138568Ssam#define	IEEE80211_NODE_STAT_SET(ni,stat,v)	(ni->ni_stats.ns_##stat = v)
158138568Ssam
159116742Ssamstatic __inline struct ieee80211_node *
160116742Ssamieee80211_ref_node(struct ieee80211_node *ni)
161116742Ssam{
162138568Ssam	ieee80211_node_incref(ni);
163116742Ssam	return ni;
164116742Ssam}
165116742Ssam
166116742Ssamstatic __inline void
167116742Ssamieee80211_unref_node(struct ieee80211_node **ni)
168116742Ssam{
169138568Ssam	ieee80211_node_decref(*ni);
170116742Ssam	*ni = NULL;			/* guard against use */
171116742Ssam}
172116742Ssam
173116742Ssamstruct ieee80211com;
174116742Ssam
175144618Ssamvoid	ieee80211_node_attach(struct ieee80211com *);
176144618Ssamvoid	ieee80211_node_lateattach(struct ieee80211com *);
177144618Ssamvoid	ieee80211_node_detach(struct ieee80211com *);
178127877Ssam
179138568Ssamstatic __inline int
180138568Ssamieee80211_node_is_authorized(const struct ieee80211_node *ni)
181138568Ssam{
182138568Ssam	return (ni->ni_flags & IEEE80211_NODE_AUTH);
183138568Ssam}
184116742Ssam
185148302Ssamvoid	ieee80211_node_authorize(struct ieee80211_node *);
186148302Ssamvoid	ieee80211_node_unauthorize(struct ieee80211_node *);
187138568Ssam
188144618Ssamvoid	ieee80211_begin_scan(struct ieee80211com *, int);
189144618Ssamint	ieee80211_next_scan(struct ieee80211com *);
190144618Ssamvoid	ieee80211_create_ibss(struct ieee80211com*, struct ieee80211_channel *);
191144618Ssamvoid	ieee80211_reset_bss(struct ieee80211com *);
192144618Ssamvoid	ieee80211_cancel_scan(struct ieee80211com *);
193144618Ssamvoid	ieee80211_end_scan(struct ieee80211com *);
194148306Ssamint	ieee80211_ibss_merge(struct ieee80211_node *);
195144618Ssamint	ieee80211_sta_join(struct ieee80211com *, struct ieee80211_node *);
196144618Ssamvoid	ieee80211_sta_leave(struct ieee80211com *, struct ieee80211_node *);
197138568Ssam
198138568Ssam/*
199138568Ssam * Table of ieee80211_node instances.  Each ieee80211com
200138568Ssam * has at least one for holding the scan candidates.
201138568Ssam * When operating as an access point or in ibss mode there
202138568Ssam * is a second table for associated stations or neighbors.
203138568Ssam */
204138568Ssamstruct ieee80211_node_table {
205138568Ssam	struct ieee80211com	*nt_ic;		/* back reference */
206138568Ssam	ieee80211_node_lock_t	nt_nodelock;	/* on node table */
207138568Ssam	TAILQ_HEAD(, ieee80211_node) nt_node;	/* information of all nodes */
208138568Ssam	LIST_HEAD(, ieee80211_node) nt_hash[IEEE80211_NODE_HASHSIZE];
209138568Ssam	const char		*nt_name;	/* for debugging */
210138568Ssam	ieee80211_scan_lock_t	nt_scanlock;	/* on nt_scangen */
211138568Ssam	u_int			nt_scangen;	/* gen# for timeout scan */
212138568Ssam	int			nt_inact_timer;	/* inactivity timer */
213138568Ssam	int			nt_inact_init;	/* initial node inact setting */
214148863Ssam	struct ieee80211_node	**nt_keyixmap;	/* key ix -> node map */
215148863Ssam	int			nt_keyixmax;	/* keyixmap size */
216138568Ssam
217138568Ssam	void			(*nt_timeout)(struct ieee80211_node_table *);
218138568Ssam};
219144618Ssamvoid	ieee80211_node_table_reset(struct ieee80211_node_table *);
220138568Ssam
221144618Ssamstruct ieee80211_node *ieee80211_alloc_node(
222138568Ssam		struct ieee80211_node_table *, const u_int8_t *);
223148777Ssamstruct ieee80211_node *ieee80211_tmp_node(struct ieee80211com *,
224148777Ssam		const u_int8_t *macaddr);
225144618Ssamstruct ieee80211_node *ieee80211_dup_bss(struct ieee80211_node_table *,
226138568Ssam		const u_int8_t *);
227138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
228144618Ssamvoid	ieee80211_free_node_debug(struct ieee80211_node *,
229138568Ssam		const char *func, int line);
230144618Ssamstruct ieee80211_node *ieee80211_find_node_debug(
231138568Ssam		struct ieee80211_node_table *, const u_int8_t *,
232138568Ssam		const char *func, int line);
233144618Ssamstruct ieee80211_node * ieee80211_find_rxnode_debug(
234138568Ssam		struct ieee80211com *, const struct ieee80211_frame_min *,
235138568Ssam		const char *func, int line);
236148863Ssamstruct ieee80211_node * ieee80211_find_rxnode_withkey_debug(
237148863Ssam		struct ieee80211com *,
238148863Ssam		const struct ieee80211_frame_min *, u_int16_t keyix,
239148863Ssam		const char *func, int line);
240144618Ssamstruct ieee80211_node *ieee80211_find_txnode_debug(
241138568Ssam		struct ieee80211com *, const u_int8_t *,
242138568Ssam		const char *func, int line);
243144618Ssamstruct ieee80211_node *ieee80211_find_node_with_channel_debug(
244138568Ssam		struct ieee80211_node_table *, const u_int8_t *macaddr,
245138568Ssam		struct ieee80211_channel *, const char *func, int line);
246144618Ssamstruct ieee80211_node *ieee80211_find_node_with_ssid_debug(
247138568Ssam		struct ieee80211_node_table *, const u_int8_t *macaddr,
248138568Ssam		u_int ssidlen, const u_int8_t *ssid,
249138568Ssam		const char *func, int line);
250138568Ssam#define	ieee80211_free_node(ni) \
251138568Ssam	ieee80211_free_node_debug(ni, __func__, __LINE__)
252138568Ssam#define	ieee80211_find_node(nt, mac) \
253138568Ssam	ieee80211_find_node_debug(nt, mac, __func__, __LINE__)
254138568Ssam#define	ieee80211_find_rxnode(nt, wh) \
255138568Ssam	ieee80211_find_rxnode_debug(nt, wh, __func__, __LINE__)
256148863Ssam#define	ieee80211_find_rxnode_withkey(nt, wh, keyix) \
257148863Ssam	ieee80211_find_rxnode_withkey_debug(nt, wh, keyix, __func__, __LINE__)
258138568Ssam#define	ieee80211_find_txnode(nt, mac) \
259138568Ssam	ieee80211_find_txnode_debug(nt, mac, __func__, __LINE__)
260138568Ssam#define	ieee80211_find_node_with_channel(nt, mac, c) \
261138568Ssam	ieee80211_find_node_with_channel_debug(nt, mac, c, __func__, __LINE__)
262138568Ssam#define	ieee80211_find_node_with_ssid(nt, mac, sl, ss) \
263138568Ssam	ieee80211_find_node_with_ssid_debug(nt, mac, sl, ss, __func__, __LINE__)
264138568Ssam#else
265144618Ssamvoid	ieee80211_free_node(struct ieee80211_node *);
266144618Ssamstruct ieee80211_node *ieee80211_find_node(
267138568Ssam		struct ieee80211_node_table *, const u_int8_t *);
268144618Ssamstruct ieee80211_node * ieee80211_find_rxnode(
269138568Ssam		struct ieee80211com *, const struct ieee80211_frame_min *);
270148863Ssamstruct ieee80211_node * ieee80211_find_rxnode_withkey(struct ieee80211com *,
271148863Ssam		const struct ieee80211_frame_min *, u_int16_t keyix);
272144618Ssamstruct ieee80211_node *ieee80211_find_txnode(
273138568Ssam		struct ieee80211com *, const u_int8_t *);
274144618Ssamstruct ieee80211_node *ieee80211_find_node_with_channel(
275138568Ssam		struct ieee80211_node_table *, const u_int8_t *macaddr,
276138568Ssam		struct ieee80211_channel *);
277144618Ssamstruct ieee80211_node *ieee80211_find_node_with_ssid(
278138568Ssam		struct ieee80211_node_table *, const u_int8_t *macaddr,
279138568Ssam		u_int ssidlen, const u_int8_t *ssid);
280138568Ssam#endif
281148863Ssamint	ieee80211_node_delucastkey(struct ieee80211_node *);
282138568Ssam
283116742Ssamtypedef void ieee80211_iter_func(void *, struct ieee80211_node *);
284144618Ssamvoid	ieee80211_iterate_nodes(struct ieee80211_node_table *,
285116742Ssam		ieee80211_iter_func *, void *);
286116742Ssam
287144618Ssamvoid	ieee80211_dump_node(struct ieee80211_node_table *,
288138568Ssam		struct ieee80211_node *);
289144618Ssamvoid	ieee80211_dump_nodes(struct ieee80211_node_table *);
290138568Ssam
291144618Ssamstruct ieee80211_node *ieee80211_fakeup_adhoc_node(
292144618Ssam		struct ieee80211_node_table *, const u_int8_t macaddr[]);
293144618Ssamvoid	ieee80211_node_join(struct ieee80211com *, struct ieee80211_node *,int);
294144618Ssamvoid	ieee80211_node_leave(struct ieee80211com *, struct ieee80211_node *);
295144618Ssamu_int8_t ieee80211_getrssi(struct ieee80211com *ic);
296116742Ssam#endif /* _NET80211_IEEE80211_NODE_H_ */
297