2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef _NET80211_IEEE80211_NODE_H_
29 #define _NET80211_IEEE80211_NODE_H_
31 #include <net80211/ieee80211_ioctl.h> /* for ieee80211_nodestats */
32 #include <net80211/ieee80211_ht.h> /* for aggregation state */
35 * Each ieee80211com instance has a single timer that fires every
36 * IEEE80211_INACT_WAIT seconds to handle "inactivity processing".
37 * This is used to do node inactivity processing when operating
38 * as an AP or in adhoc mode. For inactivity processing each node
39 * has a timeout set in it's ni_inact field that is decremented
40 * on each timeout and the node is reclaimed when the counter goes
41 * to zero. We use different inactivity timeout values depending
42 * on whether the node is associated and authorized (either by
43 * 802.1x or open/shared key authentication) or associated but yet
44 * to be authorized. The latter timeout is shorter to more aggressively
45 * reclaim nodes that leave part way through the 802.1x exchange.
47 #define IEEE80211_INACT_WAIT 15 /* inactivity interval (secs) */
48 #define IEEE80211_INACT_INIT (30/IEEE80211_INACT_WAIT) /* initial */
49 #define IEEE80211_INACT_AUTH (180/IEEE80211_INACT_WAIT) /* associated but not authorized */
50 #define IEEE80211_INACT_RUN (300/IEEE80211_INACT_WAIT) /* authorized */
51 #define IEEE80211_INACT_PROBE (30/IEEE80211_INACT_WAIT) /* probe */
52 #define IEEE80211_INACT_SCAN (300/IEEE80211_INACT_WAIT) /* scanned */
54 #define IEEE80211_TRANS_WAIT 2 /* mgt frame tx timer (secs) */
56 /* threshold for aging overlapping non-ERP bss */
57 #define IEEE80211_NONERP_PRESENT_AGE msecs_to_ticks(60*1000)
59 #define IEEE80211_NODE_HASHSIZE 32
60 /* simple hash is enough for variation of macaddr */
61 #define IEEE80211_NODE_HASH(addr) \
62 (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % \
63 IEEE80211_NODE_HASHSIZE)
65 struct ieee80211_node_table;
70 * Information element ``blob''. We use this structure
71 * to capture management frame payloads that need to be
72 * retained. Information elemnts within the payload that
73 * we need to consult have references recorded.
75 struct ieee80211_ies {
76 /* the following are either NULL or point within data */
77 uint8_t *wpa_ie; /* captured WPA ie */
78 uint8_t *rsn_ie; /* captured RSN ie */
79 uint8_t *wme_ie; /* captured WME ie */
80 uint8_t *ath_ie; /* captured Atheros ie */
81 uint8_t *htcap_ie; /* captured HTCAP ie */
82 uint8_t *htinfo_ie; /* captured HTINFO ie */
83 /* NB: these must be the last members of this structure */
84 uint8_t *data; /* frame data > 802.11 header */
85 int len; /* data size in bytes */
89 * Node specific information. Note that drivers are expected
90 * to derive from this structure to add device-specific per-node
91 * state. This is done by overriding the ic_node_* methods in
92 * the ieee80211com structure.
94 struct ieee80211_node {
95 struct ieee80211vap *ni_vap; /* associated vap */
96 struct ieee80211com *ni_ic; /* copy from vap to save deref*/
97 struct ieee80211_node_table *ni_table; /* NB: may be NULL */
98 TAILQ_ENTRY(ieee80211_node) ni_list; /* list of all nodes */
99 LIST_ENTRY(ieee80211_node) ni_hash; /* hash collision list */
100 u_int ni_refcnt; /* count of held references */
101 u_int ni_scangen; /* gen# for timeout scan */
103 #define IEEE80211_NODE_AUTH 0x000001 /* authorized for data */
104 #define IEEE80211_NODE_QOS 0x000002 /* QoS enabled */
105 #define IEEE80211_NODE_ERP 0x000004 /* ERP enabled */
106 /* NB: this must have the same value as IEEE80211_FC1_PWR_MGT */
107 #define IEEE80211_NODE_PWR_MGT 0x000010 /* power save mode enabled */
108 #define IEEE80211_NODE_AREF 0x000020 /* authentication ref held */
109 #define IEEE80211_NODE_HT 0x000040 /* HT enabled */
110 #define IEEE80211_NODE_HTCOMPAT 0x000080 /* HT setup w/ vendor OUI's */
111 #define IEEE80211_NODE_WPS 0x000100 /* WPS association */
112 #define IEEE80211_NODE_TSN 0x000200 /* TSN association */
113 #define IEEE80211_NODE_AMPDU_RX 0x000400 /* AMPDU rx enabled */
114 #define IEEE80211_NODE_AMPDU_TX 0x000800 /* AMPDU tx enabled */
115 #define IEEE80211_NODE_MIMO_PS 0x001000 /* MIMO power save enabled */
116 #define IEEE80211_NODE_MIMO_RTS 0x002000 /* send RTS in MIMO PS */
117 #define IEEE80211_NODE_RIFS 0x004000 /* RIFS enabled */
118 #define IEEE80211_NODE_SGI20 0x008000 /* Short GI in HT20 enabled */
119 #define IEEE80211_NODE_SGI40 0x010000 /* Short GI in HT40 enabled */
120 uint16_t ni_associd; /* association ID */
121 uint16_t ni_vlan; /* vlan tag */
122 uint16_t ni_txpower; /* current transmit power */
123 uint8_t ni_authmode; /* authentication algorithm */
124 uint8_t ni_ath_flags; /* Atheros feature flags */
125 /* NB: These must have the same values as IEEE80211_ATHC_* */
126 #define IEEE80211_NODE_TURBOP 0x0001 /* Turbo prime enable */
127 #define IEEE80211_NODE_COMP 0x0002 /* Compresssion enable */
128 #define IEEE80211_NODE_FF 0x0004 /* Fast Frame capable */
129 #define IEEE80211_NODE_XR 0x0008 /* Atheros WME enable */
130 #define IEEE80211_NODE_AR 0x0010 /* AR capable */
131 #define IEEE80211_NODE_BOOST 0x0080 /* Dynamic Turbo boosted */
132 uint16_t ni_ath_defkeyix;/* Atheros def key index */
133 const struct ieee80211_txparam *ni_txparms;
134 uint32_t ni_jointime; /* time of join (secs) */
135 uint32_t *ni_challenge; /* shared-key challenge */
136 struct ieee80211_ies ni_ies; /* captured ie's */
138 uint16_t ni_txseqs[IEEE80211_TID_SIZE];
139 /* rx seq previous per-tid*/
140 uint16_t ni_rxseqs[IEEE80211_TID_SIZE];
141 uint32_t ni_rxfragstamp; /* time stamp of last rx frag */
142 struct mbuf *ni_rxfrag[3]; /* rx frag reassembly */
143 struct ieee80211_key ni_ucastkey; /* unicast key */
146 uint32_t ni_rstamp; /* recv timestamp */
147 uint32_t ni_avgrssi; /* recv ssi state */
148 int8_t ni_noise; /* noise floor */
151 uint8_t ni_macaddr[IEEE80211_ADDR_LEN];
152 uint8_t ni_bssid[IEEE80211_ADDR_LEN];
154 /* beacon, probe response */
158 } ni_tstamp; /* from last rcv'd beacon */
159 uint16_t ni_intval; /* beacon interval */
160 uint16_t ni_capinfo; /* capabilities */
162 uint8_t ni_essid[IEEE80211_NWID_LEN];
163 struct ieee80211_rateset ni_rates; /* negotiated rate set */
164 struct ieee80211_channel *ni_chan;
165 uint16_t ni_fhdwell; /* FH only */
166 uint8_t ni_fhindex; /* FH only */
167 uint16_t ni_erp; /* ERP from beacon/probe resp */
168 uint16_t ni_timoff; /* byte offset to TIM ie */
169 uint8_t ni_dtim_period; /* DTIM period */
170 uint8_t ni_dtim_count; /* DTIM count for last bcn */
173 uint16_t ni_htcap; /* HT capabilities */
174 uint8_t ni_htparam; /* HT params */
175 uint8_t ni_htctlchan; /* HT control channel */
176 uint8_t ni_ht2ndchan; /* HT 2nd channel */
177 uint8_t ni_htopmode; /* HT operating mode */
178 uint8_t ni_htstbc; /* HT */
179 uint8_t ni_chw; /* negotiated channel width */
180 struct ieee80211_htrateset ni_htrates; /* negotiated ht rate set */
181 struct ieee80211_tx_ampdu ni_tx_ampdu[WME_NUM_AC];
182 struct ieee80211_rx_ampdu ni_rx_ampdu[WME_NUM_TID];
185 short ni_inact; /* inactivity mark count */
186 short ni_inact_reload;/* inactivity reload value */
187 int ni_txrate; /* legacy rate/MCS */
188 struct ifqueue ni_savedq; /* ps-poll queue */
189 struct ieee80211_nodestats ni_stats; /* per-node statistics */
191 struct ieee80211vap *ni_wdsvap; /* associated WDS vap */
192 /* XXX move to vap? */
193 struct ifqueue ni_wdsq; /* wds pending queue */
195 MALLOC_DECLARE(M_80211_NODE);
196 MALLOC_DECLARE(M_80211_NODE_IE);
198 #define IEEE80211_NODE_ATH (IEEE80211_NODE_FF | IEEE80211_NODE_TURBOP)
199 #define IEEE80211_NODE_AMPDU \
200 (IEEE80211_NODE_AMPDU_RX | IEEE80211_NODE_AMPDU_TX)
201 #define IEEE80211_NODE_HT_ALL \
202 (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT | \
203 IEEE80211_NODE_AMPDU | IEEE80211_NODE_MIMO_PS | \
204 IEEE80211_NODE_MIMO_RTS | IEEE80211_NODE_RIFS | \
205 IEEE80211_NODE_SGI20 | IEEE80211_NODE_SGI40)
207 #define IEEE80211_NODE_AID(ni) IEEE80211_AID(ni->ni_associd)
209 #define IEEE80211_NODE_STAT(ni,stat) (ni->ni_stats.ns_##stat++)
210 #define IEEE80211_NODE_STAT_ADD(ni,stat,v) (ni->ni_stats.ns_##stat += v)
211 #define IEEE80211_NODE_STAT_SET(ni,stat,v) (ni->ni_stats.ns_##stat = v)
214 * Filtered rssi calculation support. The receive rssi is maintained
215 * as an average over the last 10 frames received using a low pass filter
216 * (all frames for now, possibly need to be more selective). Calculations
217 * are designed such that a good compiler can optimize them. The avg
218 * rssi state should be initialized to IEEE80211_RSSI_DUMMY_MARKER and
219 * each sample incorporated with IEEE80211_RSSI_LPF. Use IEEE80211_RSSI_GET
220 * to extract the current value.
222 * Note that we assume rssi data are in the range [-127..127] and we
223 * discard values <-20. This is consistent with assumptions throughout
224 * net80211 that signal strength data are in .5 dBm units relative to
225 * the current noise floor (linear, not log).
227 #define IEEE80211_RSSI_LPF_LEN 10
228 #define IEEE80211_RSSI_DUMMY_MARKER 127
229 /* NB: pow2 to optimize out * and / */
230 #define IEEE80211_RSSI_EP_MULTIPLIER (1<<7)
231 #define IEEE80211_RSSI_IN(x) ((x) * IEEE80211_RSSI_EP_MULTIPLIER)
232 #define _IEEE80211_RSSI_LPF(x, y, len) \
233 (((x) != IEEE80211_RSSI_DUMMY_MARKER) ? (((x) * ((len) - 1) + (y)) / (len)) : (y))
234 #define IEEE80211_RSSI_LPF(x, y) do { \
236 x = _IEEE80211_RSSI_LPF((x), IEEE80211_RSSI_IN((y)), \
237 IEEE80211_RSSI_LPF_LEN); \
240 #define IEEE80211_RSSI_EP_RND(x, mul) \
241 ((((x) % (mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
242 #define IEEE80211_RSSI_GET(x) \
243 IEEE80211_RSSI_EP_RND(x, IEEE80211_RSSI_EP_MULTIPLIER)
245 static __inline struct ieee80211_node *
246 ieee80211_ref_node(struct ieee80211_node *ni)
248 ieee80211_node_incref(ni);
253 ieee80211_unref_node(struct ieee80211_node **ni)
255 ieee80211_node_decref(*ni);
256 *ni = NULL; /* guard against use */
261 void ieee80211_node_attach(struct ieee80211com *);
262 void ieee80211_node_lateattach(struct ieee80211com *);
263 void ieee80211_node_detach(struct ieee80211com *);
264 void ieee80211_node_vattach(struct ieee80211vap *);
265 void ieee80211_node_latevattach(struct ieee80211vap *);
266 void ieee80211_node_vdetach(struct ieee80211vap *);
269 ieee80211_node_is_authorized(const struct ieee80211_node *ni)
271 return (ni->ni_flags & IEEE80211_NODE_AUTH);
274 void ieee80211_node_authorize(struct ieee80211_node *);
275 void ieee80211_node_unauthorize(struct ieee80211_node *);
277 void ieee80211_node_set_chan(struct ieee80211_node *,
278 struct ieee80211_channel *);
279 void ieee80211_create_ibss(struct ieee80211vap*, struct ieee80211_channel *);
280 void ieee80211_reset_bss(struct ieee80211vap *);
281 void ieee80211_sync_curchan(struct ieee80211com *);
282 void ieee80211_setcurchan(struct ieee80211com *, struct ieee80211_channel *);
283 int ieee80211_ibss_merge(struct ieee80211_node *);
284 struct ieee80211_scan_entry;
285 int ieee80211_sta_join(struct ieee80211vap *,
286 const struct ieee80211_scan_entry *);
287 void ieee80211_sta_leave(struct ieee80211_node *);
288 void ieee80211_node_deauth(struct ieee80211_node *, int);
290 int ieee80211_ies_init(struct ieee80211_ies *, const uint8_t *, int);
291 void ieee80211_ies_cleanup(struct ieee80211_ies *);
292 void ieee80211_ies_expand(struct ieee80211_ies *);
293 #define ieee80211_ies_setie(_ies, _ie, _off) do { \
294 (_ies)._ie = (_ies).data + (_off); \
298 * Table of ieee80211_node instances. Each ieee80211com
299 * has one that holds association stations (when operating
300 * as an ap) or neighbors (in ibss mode).
302 * XXX embed this in ieee80211com instead of indirect?
304 struct ieee80211_node_table {
305 struct ieee80211com *nt_ic; /* back reference */
306 ieee80211_node_lock_t nt_nodelock; /* on node table */
307 TAILQ_HEAD(, ieee80211_node) nt_node; /* information of all nodes */
308 LIST_HEAD(, ieee80211_node) nt_hash[IEEE80211_NODE_HASHSIZE];
309 struct ieee80211_node **nt_keyixmap; /* key ix -> node map */
310 int nt_keyixmax; /* keyixmap size */
311 const char *nt_name; /* table name for debug msgs */
312 ieee80211_scan_lock_t nt_scanlock; /* on nt_scangen */
313 u_int nt_scangen; /* gen# for iterators */
314 int nt_inact_init; /* initial node inact setting */
317 struct ieee80211_node *ieee80211_alloc_node(struct ieee80211_node_table *,
318 struct ieee80211vap *,
319 const uint8_t macaddr[IEEE80211_ADDR_LEN]);
320 struct ieee80211_node *ieee80211_tmp_node(struct ieee80211vap *,
321 const uint8_t macaddr[IEEE80211_ADDR_LEN]);
322 struct ieee80211_node *ieee80211_dup_bss(struct ieee80211vap *,
323 const uint8_t macaddr[IEEE80211_ADDR_LEN]);
324 struct ieee80211_node *ieee80211_node_create_wds(struct ieee80211vap *,
325 const uint8_t bssid[IEEE80211_ADDR_LEN],
326 struct ieee80211_channel *);
327 #ifdef IEEE80211_DEBUG_REFCNT
328 void ieee80211_free_node_debug(struct ieee80211_node *,
329 const char *func, int line);
330 struct ieee80211_node *ieee80211_find_node_locked_debug(
331 struct ieee80211_node_table *,
332 const uint8_t macaddr[IEEE80211_ADDR_LEN],
333 const char *func, int line);
334 struct ieee80211_node *ieee80211_find_node_debug(struct ieee80211_node_table *,
335 const uint8_t macaddr[IEEE80211_ADDR_LEN],
336 const char *func, int line);
337 struct ieee80211_node *ieee80211_find_vap_node_locked_debug(
338 struct ieee80211_node_table *,
339 const struct ieee80211vap *vap,
340 const uint8_t macaddr[IEEE80211_ADDR_LEN],
341 const char *func, int line);
342 struct ieee80211_node *ieee80211_find_vap_node_debug(
343 struct ieee80211_node_table *,
344 const struct ieee80211vap *vap,
345 const uint8_t macaddr[IEEE80211_ADDR_LEN],
346 const char *func, int line);
347 struct ieee80211_node * ieee80211_find_rxnode_debug(struct ieee80211com *,
348 const struct ieee80211_frame_min *,
349 const char *func, int line);
350 struct ieee80211_node * ieee80211_find_rxnode_withkey_debug(
351 struct ieee80211com *,
352 const struct ieee80211_frame_min *, uint16_t keyix,
353 const char *func, int line);
354 struct ieee80211_node *ieee80211_find_txnode_debug(struct ieee80211vap *,
356 const char *func, int line);
357 #define ieee80211_free_node(ni) \
358 ieee80211_free_node_debug(ni, __func__, __LINE__)
359 #define ieee80211_find_node_locked(nt, mac) \
360 ieee80211_find_node_locked_debug(nt, mac, __func__, __LINE__)
361 #define ieee80211_find_node(nt, mac) \
362 ieee80211_find_node_debug(nt, mac, __func__, __LINE__)
363 #define ieee80211_find_vap_node_locked(nt, vap, mac) \
364 ieee80211_find_vap_node_locked_debug(nt, vap, mac, __func__, __LINE__)
365 #define ieee80211_find_vap_node(nt, vap, mac) \
366 ieee80211_find_vap_node_debug(nt, vap, mac, __func__, __LINE__)
367 #define ieee80211_find_rxnode(ic, wh) \
368 ieee80211_find_rxnode_debug(ic, wh, __func__, __LINE__)
369 #define ieee80211_find_rxnode_withkey(ic, wh, keyix) \
370 ieee80211_find_rxnode_withkey_debug(ic, wh, keyix, __func__, __LINE__)
371 #define ieee80211_find_txnode(vap, mac) \
372 ieee80211_find_txnode_debug(vap, mac, __func__, __LINE__)
374 void ieee80211_free_node(struct ieee80211_node *);
375 struct ieee80211_node *ieee80211_find_node_locked(struct ieee80211_node_table *,
376 const uint8_t macaddr[IEEE80211_ADDR_LEN]);
377 struct ieee80211_node *ieee80211_find_node(struct ieee80211_node_table *,
378 const uint8_t macaddr[IEEE80211_ADDR_LEN]);
379 struct ieee80211_node *ieee80211_find_vap_node_locked(
380 struct ieee80211_node_table *, const struct ieee80211vap *,
381 const uint8_t macaddr[IEEE80211_ADDR_LEN]);
382 struct ieee80211_node *ieee80211_find_vap_node(
383 struct ieee80211_node_table *, const struct ieee80211vap *,
384 const uint8_t macaddr[IEEE80211_ADDR_LEN]);
385 struct ieee80211_node * ieee80211_find_rxnode(struct ieee80211com *,
386 const struct ieee80211_frame_min *);
387 struct ieee80211_node * ieee80211_find_rxnode_withkey(struct ieee80211com *,
388 const struct ieee80211_frame_min *, uint16_t keyix);
389 struct ieee80211_node *ieee80211_find_txnode(struct ieee80211vap *,
390 const uint8_t macaddr[IEEE80211_ADDR_LEN]);
392 int ieee80211_node_delucastkey(struct ieee80211_node *);
393 void ieee80211_node_timeout(void *arg);
395 typedef void ieee80211_iter_func(void *, struct ieee80211_node *);
396 void ieee80211_iterate_nodes(struct ieee80211_node_table *,
397 ieee80211_iter_func *, void *);
399 void ieee80211_notify_erp(struct ieee80211com *);
400 void ieee80211_dump_node(struct ieee80211_node_table *,
401 struct ieee80211_node *);
402 void ieee80211_dump_nodes(struct ieee80211_node_table *);
404 struct ieee80211_node *ieee80211_fakeup_adhoc_node(struct ieee80211vap *,
405 const uint8_t macaddr[IEEE80211_ADDR_LEN]);
406 struct ieee80211_scanparams;
407 void ieee80211_init_neighbor(struct ieee80211_node *,
408 const struct ieee80211_frame *,
409 const struct ieee80211_scanparams *);
410 struct ieee80211_node *ieee80211_add_neighbor(struct ieee80211vap *,
411 const struct ieee80211_frame *,
412 const struct ieee80211_scanparams *);
413 void ieee80211_node_join(struct ieee80211_node *,int);
414 void ieee80211_node_leave(struct ieee80211_node *);
415 int8_t ieee80211_getrssi(struct ieee80211vap *);
416 void ieee80211_getsignal(struct ieee80211vap *, int8_t *, int8_t *);
417 #endif /* _NET80211_IEEE80211_NODE_H_ */