xref: /titanic_51/usr/src/uts/common/io/net80211/net80211_impl.h (revision 24db46411fd54f70c35b94bb952eb7ba040e43b4)
1 /*
2  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 2001 Atsushi Onoe
8  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * Alternatively, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") version 2 as published by the Free
24  * Software Foundation.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #pragma ident	"%Z%%M%	%I%	%E% SMI"
39 
40 #ifndef _SYS_NET80211_IMPL_H
41 #define	_SYS_NET80211_IMPL_H
42 
43 #include <sys/sysmacros.h>
44 #include <sys/list.h>
45 #include <sys/note.h>
46 #include <sys/net80211_proto.h>
47 #include <sys/net80211.h>
48 #include <sys/mac_wifi.h>
49 
50 /*
51  * IEEE802.11 kernel support module
52  */
53 
54 #ifdef	__cplusplus
55 extern "C" {
56 #endif
57 
58 #define	IEEE80211_TXPOWER_MAX	100	/* .5 dbM */
59 #define	IEEE80211_TXPOWER_MIN	0	/* kill radio */
60 
61 #define	IEEE80211_DTIM_MAX	15	/* max DTIM period */
62 #define	IEEE80211_DTIM_MIN	1	/* min DTIM period */
63 #define	IEEE80211_DTIM_DEFAULT	1	/* default DTIM period */
64 
65 /* NB: min+max come from WiFi requirements */
66 #define	IEEE80211_BINTVAL_MAX	1000	/* max beacon interval (TU's) */
67 #define	IEEE80211_BINTVAL_MIN	25	/* min beacon interval (TU's) */
68 #define	IEEE80211_BINTVAL_DEFAULT 100	/* default beacon interval (TU's) */
69 
70 #define	IEEE80211_BMISS_MAX	2	/* maximum consecutive bmiss allowed */
71 #define	IEEE80211_SWBMISS_THRESHOLD 50	/* s/w bmiss threshold (TU's) */
72 #define	IEEE80211_HWBMISS_DEFAULT 7	/* h/w bmiss threshold (beacons) */
73 
74 #define	IEEE80211_PS_SLEEP	0x1	/* STA is in power saving mode */
75 #define	IEEE80211_PS_MAX_QUEUE	50	/* maximum saved packets */
76 
77 #define	IEEE80211_RTS_DEFAULT	IEEE80211_RTS_MAX
78 #define	IEEE80211_FRAG_DEFAULT	IEEE80211_FRAG_MAX
79 
80 /*
81  * The RSSI values of two node are taken as almost the same when
82  * the difference between these two node's RSSI values is within
83  * IEEE80211_RSSI_CMP_THRESHOLD
84  */
85 #define	IEEE80211_RSSI_CMP_THRESHOLD	5
86 
87 /*
88  * Each ieee80211com instance has a single timer that fires once a
89  * second.  This is used to initiate various work depending on the
90  * state of the instance: scanning (passive or active), ``transition''
91  * (waiting for a response to a management frame when operating
92  * as a station), and node inactivity processing (when operating
93  * as an AP).  For inactivity processing each node has a timeout
94  * set in it's in_inact field that is decremented on each timeout
95  * and the node is reclaimed when the counter goes to zero.  We
96  * use different inactivity timeout values depending on whether
97  * the node is associated and authorized (either by 802.1x or
98  * open/shared key authentication) or associated but yet to be
99  * authorized.  The latter timeout is shorter to more aggressively
100  * reclaim nodes that leave part way through the 802.1x exchange.
101  *
102  * IEEE80211_INACT_WAIT defines node table's inactivity interval in
103  * seconds. On timeout, node table's registered nt_timeout callback
104  * function is executed. Each node in the node table has a timeout
105  * set in its in_inact field with IEEE80211_INACT_<state>. In
106  * nt_timeout function, node table is iterated and each node's
107  * in_inact is decremented. So IEEE80211_INACT_<state> is defined in
108  * the form [inact_sec]/IEEE80211_INACT_WAIT.
109  *
110  */
111 #define	IEEE80211_INACT_WAIT	15	/* inactivity interval (secs) */
112 #define	IEEE80211_INACT_INIT	(30/IEEE80211_INACT_WAIT)	/* initial */
113 #define	IEEE80211_INACT_ASSOC	(180/IEEE80211_INACT_WAIT)
114 					/* associated but not authorized */
115 #define	IEEE80211_INACT_RUN	(300/IEEE80211_INACT_WAIT)	/* authorized */
116 #define	IEEE80211_INACT_PROBE	(30/IEEE80211_INACT_WAIT)	/* probe */
117 #define	IEEE80211_INACT_SCAN	(300/IEEE80211_INACT_WAIT)	/* scanned */
118 
119 #define	IEEE80211_TRANS_WAIT 	5	/* mgt frame tx timer (secs) */
120 
121 /*
122  * Useful combinations of channel characteristics.
123  */
124 #define	IEEE80211_CHAN_FHSS	\
125 	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_GFSK)
126 #define	IEEE80211_CHAN_A	\
127 	(IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_OFDM)
128 #define	IEEE80211_CHAN_B	\
129 	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_CCK)
130 #define	IEEE80211_CHAN_PUREG	\
131 	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_OFDM)
132 #define	IEEE80211_CHAN_G	\
133 	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_DYN)
134 #define	IEEE80211_CHAN_T	\
135 	(IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_OFDM | IEEE80211_CHAN_TURBO)
136 #define	IEEE80211_CHAN_108G	\
137 	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_OFDM | IEEE80211_CHAN_TURBO)
138 
139 #define	IEEE80211_CHAN_ALL	\
140 	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_GFSK | \
141 	IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | IEEE80211_CHAN_DYN)
142 #define	IEEE80211_CHAN_ALLTURBO	\
143 	(IEEE80211_CHAN_ALL | IEEE80211_CHAN_TURBO)
144 
145 #define	IEEE80211_IS_CHAN_FHSS(_c)	\
146 	(((_c)->ich_flags & IEEE80211_CHAN_FHSS) == IEEE80211_CHAN_FHSS)
147 #define	IEEE80211_IS_CHAN_A(_c)		\
148 	(((_c)->ich_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)
149 #define	IEEE80211_IS_CHAN_B(_c)		\
150 	(((_c)->ich_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)
151 #define	IEEE80211_IS_CHAN_PUREG(_c)	\
152 	(((_c)->ich_flags & IEEE80211_CHAN_PUREG) == IEEE80211_CHAN_PUREG)
153 #define	IEEE80211_IS_CHAN_G(_c)		\
154 	(((_c)->ich_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)
155 #define	IEEE80211_IS_CHAN_ANYG(_c)	\
156 	(IEEE80211_IS_CHAN_PUREG(_c) || IEEE80211_IS_CHAN_G(_c))
157 #define	IEEE80211_IS_CHAN_T(_c)		\
158 	(((_c)->ich_flags & IEEE80211_CHAN_T) == IEEE80211_CHAN_T)
159 #define	IEEE80211_IS_CHAN_108G(_c)	\
160 	(((_c)->ich_flags & IEEE80211_CHAN_108G) == IEEE80211_CHAN_108G)
161 
162 #define	IEEE80211_IS_CHAN_OFDM(_c)	\
163 	((_c)->ich_flags & IEEE80211_CHAN_OFDM)
164 #define	IEEE80211_IS_CHAN_CCK(_c)	\
165 	((_c)->ich_flags & IEEE80211_CHAN_CCK)
166 #define	IEEE80211_IS_CHAN_GFSK(_c)	\
167 	((_c)->ich_flags & IEEE80211_CHAN_GFSK)
168 #define	IEEE80211_IS_CHAN_PASSIVE(_c)	\
169 	((_c)->ich_flags & IEEE80211_CHAN_PASSIVE)
170 
171 /* ni_chan encoding for FH phy */
172 #define	IEEE80211_FH_CHANMOD	80
173 #define	IEEE80211_FH_CHAN(set, pat)	\
174 	(((set) - 1) * IEEE80211_FH_CHANMOD + (pat))
175 #define	IEEE80211_FH_CHANSET(chan)	\
176 	((chan) / IEEE80211_FH_CHANMOD + 1)
177 #define	IEEE80211_FH_CHANPAT(chan)	\
178 	((chan) % IEEE80211_FH_CHANMOD)
179 
180 #define	IEEE80211_NODE_AUTH	0x0001		/* authorized for data */
181 #define	IEEE80211_NODE_QOS	0x0002		/* QoS enabled */
182 #define	IEEE80211_NODE_ERP	0x0004		/* ERP enabled */
183 #define	IEEE80211_NODE_PWR_MGT	0x0010		/* power save mode enabled */
184 #define	IEEE80211_NODE_AREF	0x0020		/* authentication ref held */
185 
186 #define	IEEE80211_MAXRSSI	127
187 
188 /* Debug Flags */
189 #define	IEEE80211_MSG_DEBUG	0x40000000	/* IFF_DEBUG equivalent */
190 #define	IEEE80211_MSG_DUMPPKTS	0x20000000	/* IFF_LINK2 equivalant */
191 #define	IEEE80211_MSG_CRYPTO	0x10000000	/* crypto work */
192 #define	IEEE80211_MSG_INPUT	0x08000000	/* input handling */
193 #define	IEEE80211_MSG_XRATE	0x04000000	/* rate set handling */
194 #define	IEEE80211_MSG_ELEMID	0x02000000	/* element id parsing */
195 #define	IEEE80211_MSG_NODE	0x01000000	/* node handling */
196 #define	IEEE80211_MSG_ASSOC	0x00800000	/* association handling */
197 #define	IEEE80211_MSG_AUTH	0x00400000	/* authentication handling */
198 #define	IEEE80211_MSG_SCAN	0x00200000	/* scanning */
199 #define	IEEE80211_MSG_OUTPUT	0x00100000	/* output handling */
200 #define	IEEE80211_MSG_STATE	0x00080000	/* state machine */
201 #define	IEEE80211_MSG_POWER	0x00040000	/* power save handling */
202 #define	IEEE80211_MSG_DOT1X	0x00020000	/* 802.1x authenticator */
203 #define	IEEE80211_MSG_DOT1XSM	0x00010000	/* 802.1x state machine */
204 #define	IEEE80211_MSG_RADIUS	0x00008000	/* 802.1x radius client */
205 #define	IEEE80211_MSG_RADDUMP	0x00004000	/* dump 802.1x radius packets */
206 #define	IEEE80211_MSG_RADKEYS	0x00002000	/* dump 802.1x keys */
207 #define	IEEE80211_MSG_WPA	0x00001000	/* WPA/RSN protocol */
208 #define	IEEE80211_MSG_ACL	0x00000800	/* ACL handling */
209 #define	IEEE80211_MSG_WME	0x00000400	/* WME protocol */
210 #define	IEEE80211_MSG_SUPERG	0x00000200	/* Atheros SuperG protocol */
211 #define	IEEE80211_MSG_DOTH	0x00000100	/* 802.11h support */
212 #define	IEEE80211_MSG_INACT	0x00000080	/* inactivity handling */
213 #define	IEEE80211_MSG_ROAM	0x00000040	/* sta-mode roaming */
214 #define	IEEE80211_MSG_CONFIG	0x00000020	/* wificonfig/dladm */
215 #define	IEEE80211_MSG_ANY	0xffffffff	/* anything */
216 
217 /* Error flags returned by ieee80211_match_bss */
218 #define	IEEE80211_BADCHAN	0x01
219 #define	IEEE80211_BADOPMODE	0x02
220 #define	IEEE80211_BADPRIVACY	0x04
221 #define	IEEE80211_BADRATE	0x08
222 #define	IEEE80211_BADESSID	0x10
223 #define	IEEE80211_BADBSSID	0x20
224 #define	IEEE80211_NODEFAIL	0x40
225 
226 typedef struct ieee80211_impl {
227 	struct ieee80211com	*ic;
228 	uint8_t			im_chan_avail[IEEE80211_CHAN_BYTES];
229 	uint8_t			im_chan_scan[IEEE80211_CHAN_BYTES];
230 
231 	uint8_t			im_bmiss_count;	/* current beacon miss count */
232 	int32_t			im_bmiss_max;	/* max bmiss before scan */
233 	timeout_id_t		im_swbmiss;
234 	uint16_t		im_swbmiss_count; /* beacons in last period */
235 	uint16_t		im_swbmiss_period;	/* s/w bmiss period */
236 
237 	int32_t			im_mgt_timer;	/* mgmt timeout, secs */
238 	int32_t			im_inact_timer;	/* inactivity timer wait, sec */
239 	int32_t			im_inact_init;	/* initial setting */
240 	int32_t			im_inact_assoc;	/* assoc but not authorized */
241 	int32_t			im_inact_run;	/* authorized setting */
242 	int32_t			im_inact_probe;	/* inactive probe time */
243 
244 	kcondvar_t		im_scan_cv;	/* wait scan complete */
245 } ieee80211_impl_t;
246 
247 /*
248  * Parameters supplied when adding/updating an entry in a
249  * scan cache.  Pointer variables should be set to NULL
250  * if no data is available.  Pointer references can be to
251  * local data; any information that is saved will be copied.
252  * All multi-byte values must be in host byte order.
253  */
254 struct ieee80211_scanparams {
255 	uint16_t		capinfo;	/* 802.11 capabilities */
256 	enum ieee80211_phytype	phytype;
257 	uint16_t		fhdwell;	/* FHSS dwell interval */
258 	uint8_t			chan;
259 	uint8_t			bchan;
260 	uint8_t			fhindex;
261 	uint8_t			erp;
262 	uint16_t		bintval;
263 	uint8_t			timoff;
264 	uint8_t			*tim;
265 	uint8_t			*tstamp;
266 	uint8_t			*country;
267 	uint8_t			*ssid;
268 	uint8_t			*rates;
269 	uint8_t			*xrates;
270 	uint8_t			*wpa;
271 	uint8_t			*wme;
272 };
273 
274 #define	IEEE80211_SEND_MGMT(_ic, _in, _type, _arg)			\
275 	((*(_ic)->ic_send_mgmt)((_ic), (_in), (_type), (_arg)))
276 
277 /* Verify the existence and length of __elem or get out. */
278 #define	IEEE80211_VERIFY_ELEMENT(__elem, __maxlen, __func) do {		\
279 	_NOTE(CONSTCOND)						\
280 	if ((__elem) == NULL) {						\
281 		ieee80211_err("ieee80211: no #__elem \n");		\
282 		__func;							\
283 	}								\
284 	if ((__elem)[1] > (__maxlen)) {					\
285 		ieee80211_err("ieee80211: bad "#__elem " len %d\n",	\
286 		    (__elem)[1]);					\
287 		__func;							\
288 	}								\
289 	_NOTE(CONSTCOND)						\
290 } while (0)
291 
292 #define	IEEE80211_VERIFY_LENGTH(_len, _minlen, _func) do {		\
293 	_NOTE(CONSTCOND)						\
294 	if ((_len) < (_minlen)) {					\
295 		ieee80211_dbg(IEEE80211_MSG_ELEMID,			\
296 		    "ie of type %s too short",				\
297 		    ieee80211_mgt_subtype_name[subtype >>		\
298 			IEEE80211_FC0_SUBTYPE_SHIFT]);			\
299 		_func;							\
300 	}								\
301 	_NOTE(CONSTCOND)						\
302 } while (0)
303 
304 #define	IEEE80211_VERIFY_SSID(_in, _ssid, _func) do {			\
305 	_NOTE(CONSTCOND)						\
306 	ASSERT((_in) != NULL);						\
307 	if ((_ssid)[1] != 0 &&						\
308 	    ((_ssid)[1] != (_in)->in_esslen ||				\
309 	    bcmp((_ssid) + 2, (_in)->in_essid, (_ssid)[1]) != 0)) {	\
310 		_func;							\
311 	}								\
312 	_NOTE(CONSTCOND)						\
313 } while (0)
314 
315 #define	ieee80211_setbit(a, i)	((a)[(i)/NBBY] |= (1 << ((i)%NBBY)))
316 #define	ieee80211_clrbit(a, i)	((a)[(i)/NBBY] &= ~(1 << ((i)%NBBY)))
317 #define	ieee80211_isset(a, i)	((a)[(i)/NBBY] & (1 << ((i)%NBBY)))
318 #define	ieee80211_isclr(a, i)	(!((a)[(i)/NBBY] & (1 << ((i)%NBBY))))
319 
320 #define	IEEE80211_N(a)		(sizeof (a) / sizeof (a[0]))
321 
322 #define	IEEE80211_LOCK(_ic)		\
323 	mutex_enter(&(_ic)->ic_genlock)
324 #define	IEEE80211_UNLOCK(_ic)		\
325 	mutex_exit(&(_ic)->ic_genlock)
326 #define	IEEE80211_IS_LOCKED(_ic)	\
327 	mutex_owned(&(_ic)->ic_genlock)
328 #define	IEEE80211_LOCK_ASSERT(_ic)	\
329 	ASSERT(mutex_owned(&(_ic)->ic_genlock))
330 
331 #define	IEEE80211_NODE_LOCK(_nt)		\
332 	mutex_enter(&(_nt)->nt_nodelock)
333 #define	IEEE80211_NODE_UNLOCK(_nt)		\
334 	mutex_exit(&(_nt)->nt_nodelock)
335 #define	IEEE80211_NODE_IS_LOCKED(_nt)		\
336 	mutex_owned(&(_nt)->nt_nodelock)
337 #define	IEEE80211_NODE_LOCK_ASSERT(_nt)		\
338 	ASSERT(mutex_owned(&(_nt)->nt_nodelock))
339 #define	ieee80211_node_hash(addr)		\
340 	(((uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % IEEE80211_NODE_HASHSIZE)
341 
342 #define	IEEE80211_SCAN_LOCK(_nt)	mutex_enter(&(_nt)->nt_scanlock)
343 #define	IEEE80211_SCAN_UNLOCK(_nt)	mutex_exit(&(_nt)->nt_scanlock)
344 
345 #define	IEEE80211_RV(v)			((v) & IEEE80211_RATE_VAL)
346 
347 #define	IEEE80211_SUBTYPE_NAME(subtype)		\
348 	ieee80211_mgt_subtype_name[(subtype) >> IEEE80211_FC0_SUBTYPE_SHIFT]
349 
350 extern const char *ieee80211_mgt_subtype_name[];
351 extern const char *ieee80211_phymode_name[];
352 
353 void ieee80211_err(const int8_t *, ...);
354 void ieee80211_dbg(uint32_t, const int8_t *, ...);
355 int ieee80211_hdrspace(const void *);
356 
357 void ieee80211_notify(ieee80211com_t *, wpa_event_type);
358 void ieee80211_mac_update(ieee80211com_t *);
359 
360 uint64_t ieee80211_read_6(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
361 
362 /* node */
363 void ieee80211_node_attach(ieee80211com_t *);
364 void ieee80211_node_lateattach(ieee80211com_t *);
365 void ieee80211_node_detach(ieee80211com_t *);
366 void ieee80211_reset_bss(ieee80211com_t *);
367 void ieee80211_cancel_scan(ieee80211com_t *);
368 void ieee80211_add_scan(ieee80211com_t *, const struct ieee80211_scanparams *,
369     const struct ieee80211_frame *, int, int, int);
370 void ieee80211_init_neighbor(ieee80211_node_t *, const struct ieee80211_frame *,
371     const struct ieee80211_scanparams *);
372 ieee80211_node_t *ieee80211_add_neighbor(ieee80211com_t *,
373     const struct ieee80211_frame *, const struct ieee80211_scanparams *);
374 void ieee80211_create_ibss(ieee80211com_t *, struct ieee80211_channel *);
375 ieee80211_node_t *ieee80211_fakeup_adhoc_node(ieee80211_node_table_t *,
376     const uint8_t *);
377 ieee80211_node_t *ieee80211_tmp_node(ieee80211com_t *, const uint8_t *);
378 
379 /* proto */
380 void ieee80211_proto_attach(ieee80211com_t *);
381 int ieee80211_fix_rate(ieee80211_node_t *, int);
382 void ieee80211_setbasicrates(struct ieee80211_rateset *,
383     enum ieee80211_phymode);
384 void ieee80211_reset_erp(ieee80211com_t *);
385 void ieee80211_set_shortslottime(ieee80211com_t *, boolean_t);
386 
387 /* input */
388 int ieee80211_setup_rates(ieee80211_node_t *, const uint8_t *,
389     const uint8_t *, int);
390 void ieee80211_recv_mgmt(ieee80211com_t *, mblk_t *, ieee80211_node_t *,
391     int, int, uint32_t);
392 
393 /* output */
394 int ieee80211_send_probereq(ieee80211_node_t *, const uint8_t *,
395     const uint8_t *, const uint8_t *, const uint8_t *, size_t, const void *,
396     size_t);
397 int ieee80211_send_mgmt(ieee80211com_t *, ieee80211_node_t *, int, int);
398 int ieee80211_send_nulldata(ieee80211_node_t *);
399 
400 /* crypto */
401 struct ieee80211_key *ieee80211_crypto_getkey(ieee80211com_t *);
402 uint8_t ieee80211_crypto_getciphertype(ieee80211com_t *);
403 
404 /* generic */
405 mblk_t *ieee80211_getmgtframe(uint8_t **, int);
406 void ieee80211_notify_node_join(ieee80211com_t *, ieee80211_node_t *);
407 void ieee80211_notify_node_leave(ieee80211com_t *, ieee80211_node_t *);
408 
409 #ifdef	__cplusplus
410 }
411 #endif
412 
413 #endif	/* _SYS_NET80211_IMPL_H */
414