ieee80211_node.h revision 170530
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3170360Ssam * Copyright (c) 2002-2007 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.
14116742Ssam *
15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25116904Ssam *
26116742Ssam * $FreeBSD: head/sys/net80211/ieee80211_node.h 170530 2007-06-11 03:36:55Z sam $
27116742Ssam */
28116742Ssam#ifndef _NET80211_IEEE80211_NODE_H_
29116742Ssam#define _NET80211_IEEE80211_NODE_H_
30116742Ssam
31138568Ssam#include <net80211/ieee80211_ioctl.h>		/* for ieee80211_nodestats */
32170530Ssam#include <net80211/ieee80211_ht.h>		/* for aggregation state */
33116742Ssam
34138568Ssam/*
35138568Ssam * Each ieee80211com instance has a single timer that fires once a
36138568Ssam * second.  This is used to initiate various work depending on the
37138568Ssam * state of the instance: scanning (passive or active), ``transition''
38138568Ssam * (waiting for a response to a management frame when operating
39138568Ssam * as a station), and node inactivity processing (when operating
40138568Ssam * as an AP).  For inactivity processing each node has a timeout
41138568Ssam * set in it's ni_inact field that is decremented on each timeout
42138568Ssam * and the node is reclaimed when the counter goes to zero.  We
43138568Ssam * use different inactivity timeout values depending on whether
44138568Ssam * the node is associated and authorized (either by 802.1x or
45138568Ssam * open/shared key authentication) or associated but yet to be
46138568Ssam * authorized.  The latter timeout is shorter to more aggressively
47138568Ssam * reclaim nodes that leave part way through the 802.1x exchange.
48138568Ssam */
49138568Ssam#define	IEEE80211_INACT_WAIT	15		/* inactivity interval (secs) */
50138568Ssam#define	IEEE80211_INACT_INIT	(30/IEEE80211_INACT_WAIT)	/* initial */
51138568Ssam#define	IEEE80211_INACT_AUTH	(180/IEEE80211_INACT_WAIT)	/* associated but not authorized */
52138568Ssam#define	IEEE80211_INACT_RUN	(300/IEEE80211_INACT_WAIT)	/* authorized */
53138568Ssam#define	IEEE80211_INACT_PROBE	(30/IEEE80211_INACT_WAIT)	/* probe */
54138568Ssam#define	IEEE80211_INACT_SCAN	(300/IEEE80211_INACT_WAIT)	/* scanned */
55138568Ssam
56170530Ssam#define	IEEE80211_TRANS_WAIT 	2		/* mgt frame tx timer (secs) */
57138568Ssam
58116742Ssam#define	IEEE80211_NODE_HASHSIZE	32
59116742Ssam/* simple hash is enough for variation of macaddr */
60116742Ssam#define	IEEE80211_NODE_HASH(addr)	\
61170530Ssam	(((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % \
62138568Ssam		IEEE80211_NODE_HASHSIZE)
63116742Ssam
64138568Ssamstruct ieee80211_rsnparms {
65170530Ssam	uint8_t		rsn_mcastcipher;	/* mcast/group cipher */
66170530Ssam	uint8_t		rsn_mcastkeylen;	/* mcast key length */
67170530Ssam	uint8_t		rsn_ucastcipherset;	/* unicast cipher set */
68170530Ssam	uint8_t		rsn_ucastcipher;	/* selected unicast cipher */
69170530Ssam	uint8_t		rsn_ucastkeylen;	/* unicast key length */
70170530Ssam	uint8_t		rsn_keymgmtset;		/* key mangement algorithms */
71170530Ssam	uint8_t		rsn_keymgmt;		/* selected key mgmt algo */
72170530Ssam	uint16_t	rsn_caps;		/* capabilities */
73116742Ssam};
74116742Ssam
75138568Ssamstruct ieee80211_node_table;
76138568Ssamstruct ieee80211com;
77138568Ssam
78116742Ssam/*
79116742Ssam * Node specific information.  Note that drivers are expected
80116742Ssam * to derive from this structure to add device-specific per-node
81116742Ssam * state.  This is done by overriding the ic_node_* methods in
82116742Ssam * the ieee80211com structure.
83116742Ssam */
84116742Ssamstruct ieee80211_node {
85138568Ssam	struct ieee80211com	*ni_ic;
86138568Ssam	struct ieee80211_node_table *ni_table;
87116742Ssam	TAILQ_ENTRY(ieee80211_node)	ni_list;
88116742Ssam	LIST_ENTRY(ieee80211_node)	ni_hash;
89116742Ssam	u_int			ni_refcnt;
90120483Ssam	u_int			ni_scangen;	/* gen# for timeout scan */
91170530Ssam	uint8_t			ni_authmode;	/* authentication algorithm */
92170530Ssam	uint8_t			ni_ath_flags;	/* Atheros feature flags */
93170530Ssam	/* NB: These must have the same values as IEEE80211_ATHC_* */
94170530Ssam#define IEEE80211_NODE_TURBOP	0x0001		/* Turbo prime enable */
95170530Ssam#define IEEE80211_NODE_COMP	0x0002		/* Compresssion enable */
96170530Ssam#define IEEE80211_NODE_FF	0x0004          /* Fast Frame capable */
97170530Ssam#define IEEE80211_NODE_XR	0x0008		/* Atheros WME enable */
98170530Ssam#define IEEE80211_NODE_AR	0x0010		/* AR capable */
99170530Ssam#define IEEE80211_NODE_BOOST	0x0080
100170530Ssam#define IEEE80211_NODE_PSUPDATE	0x0200		/* power save state changed */
101170530Ssam#define	IEEE80211_NODE_CHWUPDATE 0x0400		/* 11n channel width change */
102170530Ssam	uint16_t		ni_flags;	/* special-purpose state */
103138568Ssam#define	IEEE80211_NODE_AUTH	0x0001		/* authorized for data */
104138568Ssam#define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
105138568Ssam#define	IEEE80211_NODE_ERP	0x0004		/* ERP enabled */
106138568Ssam/* NB: this must have the same value as IEEE80211_FC1_PWR_MGT */
107138568Ssam#define	IEEE80211_NODE_PWR_MGT	0x0010		/* power save mode enabled */
108147788Ssam#define	IEEE80211_NODE_AREF	0x0020		/* authentication ref held */
109170530Ssam#define	IEEE80211_NODE_HT	0x0040		/* HT enabled */
110170530Ssam#define	IEEE80211_NODE_HTCOMPAT	0x0080		/* HT setup w/ vendor OUI's */
111170530Ssam	uint16_t		ni_ath_defkeyix;/* Atheros def key index */
112170530Ssam	uint16_t		ni_associd;	/* assoc response */
113170530Ssam	uint16_t		ni_txpower;	/* current transmit power */
114170530Ssam	uint16_t		ni_vlan;	/* vlan tag */
115170530Ssam	uint32_t		*ni_challenge;	/* shared-key challenge */
116170530Ssam	uint8_t			*ni_wpa_ie;	/* captured WPA ie */
117170530Ssam	uint8_t			*ni_rsn_ie;	/* captured RSN ie */
118170530Ssam	uint8_t			*ni_wme_ie;	/* captured WME ie */
119170530Ssam	uint8_t			*ni_ath_ie;	/* captured Atheros ie */
120167439Ssam#define	IEEE80211_NONQOS_TID	16		/* index for non-QoS sta */
121170530Ssam	uint16_t		ni_txseqs[17];	/* tx seq per-tid */
122170530Ssam	uint16_t		ni_rxseqs[17];	/* rx seq previous per-tid*/
123170530Ssam	uint32_t		ni_rxfragstamp;	/* time stamp of last rx frag */
124138568Ssam	struct mbuf		*ni_rxfrag[3];	/* rx frag reassembly */
125138568Ssam	struct ieee80211_rsnparms ni_rsn;	/* RSN/WPA parameters */
126138568Ssam	struct ieee80211_key	ni_ucastkey;	/* unicast key */
127116742Ssam
128116742Ssam	/* hardware */
129170530Ssam	uint32_t		ni_rstamp;	/* recv timestamp */
130170530Ssam	int8_t			ni_rssi;	/* recv ssi */
131170530Ssam	int8_t			ni_noise;	/* noise floor */
132116742Ssam
133116742Ssam	/* header */
134170530Ssam	uint8_t			ni_macaddr[IEEE80211_ADDR_LEN];
135170530Ssam	uint8_t			ni_bssid[IEEE80211_ADDR_LEN];
136116742Ssam
137116742Ssam	/* beacon, probe response */
138138568Ssam	union {
139170530Ssam		uint8_t		data[8];
140170530Ssam		uint64_t	tsf;
141138568Ssam	} ni_tstamp;				/* from last rcv'd beacon */
142170530Ssam	uint16_t		ni_intval;	/* beacon interval */
143170530Ssam	uint16_t		ni_capinfo;	/* capabilities */
144170530Ssam	uint8_t			ni_esslen;
145170530Ssam	uint8_t			ni_essid[IEEE80211_NWID_LEN];
146116742Ssam	struct ieee80211_rateset ni_rates;	/* negotiated rate set */
147170530Ssam	struct ieee80211_channel *ni_chan;
148170530Ssam	uint16_t		ni_fhdwell;	/* FH only */
149170530Ssam	uint8_t			ni_fhindex;	/* FH only */
150170530Ssam	uint8_t			ni_erp;		/* ERP from beacon/probe resp */
151170530Ssam	uint16_t		ni_timoff;	/* byte offset to TIM ie */
152170530Ssam	uint8_t			ni_dtim_period;	/* DTIM period */
153170530Ssam	uint8_t			ni_dtim_count;	/* DTIM count for last bcn */
154116742Ssam
155170530Ssam	/* 11n state */
156170530Ssam	uint16_t		ni_htcap;	/* HT capabilities */
157170530Ssam	uint8_t			ni_htparam;	/* HT params */
158170530Ssam	uint8_t			ni_htctlchan;	/* HT control channel */
159170530Ssam	uint8_t			ni_ht2ndchan;	/* HT 2nd channel */
160170530Ssam	uint8_t			ni_htopmode;	/* HT operating mode */
161170530Ssam	uint8_t			ni_htstbc;	/* HT */
162170530Ssam	uint8_t			ni_reqcw;	/* requested tx channel width */
163170530Ssam	uint8_t			ni_chw;		/* negotiated channel width */
164170530Ssam	struct ieee80211_htrateset ni_htrates;	/* negotiated ht rate set */
165170530Ssam	struct ieee80211_tx_ampdu ni_tx_ampdu[WME_NUM_AC];
166170530Ssam	struct ieee80211_rx_ampdu ni_rx_ampdu[WME_NUM_TID];
167170530Ssam
168116742Ssam	/* others */
169116742Ssam	int			ni_fails;	/* failure count to associate */
170138568Ssam	short			ni_inact;	/* inactivity mark count */
171138568Ssam	short			ni_inact_reload;/* inactivity reload value */
172116742Ssam	int			ni_txrate;	/* index to ni_rates[] */
173138568Ssam	struct	ifqueue		ni_savedq;	/* ps-poll queue */
174138568Ssam	struct ieee80211_nodestats ni_stats;	/* per-node statistics */
175116742Ssam};
176138568SsamMALLOC_DECLARE(M_80211_NODE);
177116742Ssam
178170530Ssam#define	IEEE80211_NODE_ATH	(IEEE80211_NODE_FF | IEEE80211_NODE_TURBOP)
179170530Ssam
180138568Ssam#define	IEEE80211_NODE_AID(ni)	IEEE80211_AID(ni->ni_associd)
181138568Ssam
182138568Ssam#define	IEEE80211_NODE_STAT(ni,stat)	(ni->ni_stats.ns_##stat++)
183138568Ssam#define	IEEE80211_NODE_STAT_ADD(ni,stat,v)	(ni->ni_stats.ns_##stat += v)
184138568Ssam#define	IEEE80211_NODE_STAT_SET(ni,stat,v)	(ni->ni_stats.ns_##stat = v)
185138568Ssam
186116742Ssamstatic __inline struct ieee80211_node *
187116742Ssamieee80211_ref_node(struct ieee80211_node *ni)
188116742Ssam{
189138568Ssam	ieee80211_node_incref(ni);
190116742Ssam	return ni;
191116742Ssam}
192116742Ssam
193116742Ssamstatic __inline void
194116742Ssamieee80211_unref_node(struct ieee80211_node **ni)
195116742Ssam{
196138568Ssam	ieee80211_node_decref(*ni);
197116742Ssam	*ni = NULL;			/* guard against use */
198116742Ssam}
199116742Ssam
200116742Ssamstruct ieee80211com;
201116742Ssam
202144618Ssamvoid	ieee80211_node_attach(struct ieee80211com *);
203144618Ssamvoid	ieee80211_node_lateattach(struct ieee80211com *);
204144618Ssamvoid	ieee80211_node_detach(struct ieee80211com *);
205127877Ssam
206138568Ssamstatic __inline int
207138568Ssamieee80211_node_is_authorized(const struct ieee80211_node *ni)
208138568Ssam{
209138568Ssam	return (ni->ni_flags & IEEE80211_NODE_AUTH);
210138568Ssam}
211116742Ssam
212148302Ssamvoid	ieee80211_node_authorize(struct ieee80211_node *);
213148302Ssamvoid	ieee80211_node_unauthorize(struct ieee80211_node *);
214138568Ssam
215156358Ssamvoid	ieee80211_probe_curchan(struct ieee80211com *, int);
216144618Ssamvoid	ieee80211_create_ibss(struct ieee80211com*, struct ieee80211_channel *);
217144618Ssamvoid	ieee80211_reset_bss(struct ieee80211com *);
218148306Ssamint	ieee80211_ibss_merge(struct ieee80211_node *);
219170530Ssamstruct ieee80211_scan_entry;
220170530Ssamint	ieee80211_sta_join(struct ieee80211com *,
221170530Ssam		const struct ieee80211_scan_entry *);
222144618Ssamvoid	ieee80211_sta_leave(struct ieee80211com *, struct ieee80211_node *);
223138568Ssam
224138568Ssam/*
225138568Ssam * Table of ieee80211_node instances.  Each ieee80211com
226138568Ssam * has at least one for holding the scan candidates.
227138568Ssam * When operating as an access point or in ibss mode there
228138568Ssam * is a second table for associated stations or neighbors.
229138568Ssam */
230138568Ssamstruct ieee80211_node_table {
231138568Ssam	struct ieee80211com	*nt_ic;		/* back reference */
232138568Ssam	ieee80211_node_lock_t	nt_nodelock;	/* on node table */
233138568Ssam	TAILQ_HEAD(, ieee80211_node) nt_node;	/* information of all nodes */
234138568Ssam	LIST_HEAD(, ieee80211_node) nt_hash[IEEE80211_NODE_HASHSIZE];
235170530Ssam	struct ieee80211_node	**nt_keyixmap;	/* key ix -> node map */
236170530Ssam	int			nt_keyixmax;	/* keyixmap size */
237138568Ssam	const char		*nt_name;	/* for debugging */
238138568Ssam	ieee80211_scan_lock_t	nt_scanlock;	/* on nt_scangen */
239138568Ssam	u_int			nt_scangen;	/* gen# for timeout scan */
240138568Ssam	int			nt_inact_init;	/* initial node inact setting */
241138568Ssam};
242138568Ssam
243144618Ssamstruct ieee80211_node *ieee80211_alloc_node(
244170530Ssam		struct ieee80211_node_table *, const uint8_t *);
245148777Ssamstruct ieee80211_node *ieee80211_tmp_node(struct ieee80211com *,
246170530Ssam		const uint8_t *macaddr);
247144618Ssamstruct ieee80211_node *ieee80211_dup_bss(struct ieee80211_node_table *,
248170530Ssam		const uint8_t *);
249138568Ssam#ifdef IEEE80211_DEBUG_REFCNT
250144618Ssamvoid	ieee80211_free_node_debug(struct ieee80211_node *,
251138568Ssam		const char *func, int line);
252170530Ssamstruct ieee80211_node *ieee80211_find_node_debug(struct ieee80211_node_table *,
253170530Ssam		const uint8_t *,
254138568Ssam		const char *func, int line);
255170530Ssamstruct ieee80211_node * ieee80211_find_rxnode_debug(struct ieee80211com *,
256170530Ssam		const struct ieee80211_frame_min *,
257138568Ssam		const char *func, int line);
258148863Ssamstruct ieee80211_node * ieee80211_find_rxnode_withkey_debug(
259148863Ssam		struct ieee80211com *,
260170530Ssam		const struct ieee80211_frame_min *, uint16_t keyix,
261148863Ssam		const char *func, int line);
262170530Ssamstruct ieee80211_node * ieee80211_find_rxnode_withkey_debug(
263170530Ssam		struct ieee80211com *,
264170530Ssam		const struct ieee80211_frame_min *, uint16_t keyix,
265138568Ssam		const char *func, int line);
266170530Ssamstruct ieee80211_node *ieee80211_find_txnode_debug(struct ieee80211com *,
267170530Ssam		const uint8_t *,
268170530Ssam		const char *func, int line);
269144618Ssamstruct ieee80211_node *ieee80211_find_node_with_ssid_debug(
270170530Ssam		struct ieee80211_node_table *, const uint8_t *macaddr,
271170530Ssam		u_int ssidlen, const uint8_t *ssid,
272138568Ssam		const char *func, int line);
273138568Ssam#define	ieee80211_free_node(ni) \
274138568Ssam	ieee80211_free_node_debug(ni, __func__, __LINE__)
275138568Ssam#define	ieee80211_find_node(nt, mac) \
276138568Ssam	ieee80211_find_node_debug(nt, mac, __func__, __LINE__)
277138568Ssam#define	ieee80211_find_rxnode(nt, wh) \
278138568Ssam	ieee80211_find_rxnode_debug(nt, wh, __func__, __LINE__)
279148863Ssam#define	ieee80211_find_rxnode_withkey(nt, wh, keyix) \
280148863Ssam	ieee80211_find_rxnode_withkey_debug(nt, wh, keyix, __func__, __LINE__)
281138568Ssam#define	ieee80211_find_txnode(nt, mac) \
282138568Ssam	ieee80211_find_txnode_debug(nt, mac, __func__, __LINE__)
283138568Ssam#define	ieee80211_find_node_with_ssid(nt, mac, sl, ss) \
284138568Ssam	ieee80211_find_node_with_ssid_debug(nt, mac, sl, ss, __func__, __LINE__)
285138568Ssam#else
286144618Ssamvoid	ieee80211_free_node(struct ieee80211_node *);
287170530Ssamstruct ieee80211_node *ieee80211_find_node(struct ieee80211_node_table *,
288170530Ssam		const uint8_t *);
289170530Ssamstruct ieee80211_node * ieee80211_find_rxnode(struct ieee80211com *,
290170530Ssam		const struct ieee80211_frame_min *);
291148863Ssamstruct ieee80211_node * ieee80211_find_rxnode_withkey(struct ieee80211com *,
292170530Ssam		const struct ieee80211_frame_min *, uint16_t keyix);
293170530Ssamstruct ieee80211_node *ieee80211_find_txnode(struct ieee80211com *,
294170530Ssam		const uint8_t *);
295144618Ssamstruct ieee80211_node *ieee80211_find_node_with_ssid(
296170530Ssam		struct ieee80211_node_table *, const uint8_t *macaddr,
297170530Ssam		u_int ssidlen, const uint8_t *ssid);
298138568Ssam#endif
299148863Ssamint	ieee80211_node_delucastkey(struct ieee80211_node *);
300170530Ssamvoid	ieee80211_node_timeout(void *arg);
301138568Ssam
302116742Ssamtypedef void ieee80211_iter_func(void *, struct ieee80211_node *);
303144618Ssamvoid	ieee80211_iterate_nodes(struct ieee80211_node_table *,
304116742Ssam		ieee80211_iter_func *, void *);
305116742Ssam
306144618Ssamvoid	ieee80211_dump_node(struct ieee80211_node_table *,
307138568Ssam		struct ieee80211_node *);
308144618Ssamvoid	ieee80211_dump_nodes(struct ieee80211_node_table *);
309138568Ssam
310144618Ssamstruct ieee80211_node *ieee80211_fakeup_adhoc_node(
311170530Ssam		struct ieee80211_node_table *, const uint8_t macaddr[]);
312170530Ssamstruct ieee80211_scanparams;
313153073Ssamvoid	ieee80211_init_neighbor(struct ieee80211_node *,
314153073Ssam		const struct ieee80211_frame *,
315153073Ssam		const struct ieee80211_scanparams *);
316148936Ssamstruct ieee80211_node *ieee80211_add_neighbor(struct ieee80211com *,
317148936Ssam		const struct ieee80211_frame *,
318148936Ssam		const struct ieee80211_scanparams *);
319170530Ssamvoid	ieee80211_node_join(struct ieee80211com *, struct ieee80211_node *,int);
320170530Ssamvoid	ieee80211_node_leave(struct ieee80211com *, struct ieee80211_node *);
321170530Ssamint8_t	ieee80211_getrssi(struct ieee80211com *);
322170530Ssamvoid	ieee80211_getsignal(struct ieee80211com *, int8_t *, int8_t *);
323116742Ssam#endif /* _NET80211_IEEE80211_NODE_H_ */
324