1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
25  *
26  * $FreeBSD: head/sys/net80211/ieee80211_proto.h 199187 2009-11-11 15:00:56Z antoine $
27  */
28 #ifndef _NET80211_IEEE80211_PROTO_H_
29 #define _NET80211_IEEE80211_PROTO_H_
30 
31 /*
32  * 802.11 protocol implementation definitions.
33  */
34 
35 enum ieee80211_state {
36 	IEEE80211_S_INIT	= 0,	/* default state */
37 	IEEE80211_S_SCAN	= 1,	/* scanning */
38 	IEEE80211_S_AUTH	= 2,	/* try to authenticate */
39 	IEEE80211_S_ASSOC	= 3,	/* try to assoc */
40 	IEEE80211_S_CAC		= 4,	/* doing channel availability check */
41 	IEEE80211_S_RUN		= 5,	/* operational (e.g. associated) */
42 	IEEE80211_S_CSA		= 6,	/* channel switch announce pending */
43 	IEEE80211_S_SLEEP	= 7,	/* power save */
44 };
45 #define	IEEE80211_S_MAX		(IEEE80211_S_SLEEP+1)
46 
47 #define	IEEE80211_SEND_MGMT(_ni,_type,_arg) \
48 	((*(_ni)->ni_ic->ic_send_mgmt)(_ni, _type, _arg))
49 
50 extern	const char *ieee80211_mgt_subtype_name[];
51 extern	const char *ieee80211_phymode_name[IEEE80211_MODE_MAX];
52 extern	const int ieee80211_opcap[IEEE80211_OPMODE_MAX];
53 
54 void	ieee80211_proto_attach(struct ieee80211com *);
55 void	ieee80211_proto_detach(struct ieee80211com *);
56 void	ieee80211_proto_vattach(struct ieee80211vap *);
57 void	ieee80211_proto_vdetach(struct ieee80211vap *);
58 
59 void	ieee80211_syncifflag_locked(struct ieee80211com *, int flag);
60 void	ieee80211_syncflag(struct ieee80211vap *, int flag);
61 void	ieee80211_syncflag_ht(struct ieee80211vap *, int flag);
62 void	ieee80211_syncflag_ext(struct ieee80211vap *, int flag);
63 
64 #define	ieee80211_input(ni, m, rssi, nf) \
65 	((ni)->ni_vap->iv_input(ni, m, rssi, nf))
66 int	ieee80211_input_all(struct ieee80211com *, struct mbuf *, int, int);
67 struct ieee80211_bpf_params;
68 int	ieee80211_mgmt_output(struct ieee80211_node *, struct mbuf *, int,
69 		struct ieee80211_bpf_params *);
70 int	ieee80211_raw_xmit(struct ieee80211_node *, struct mbuf *,
71 		const struct ieee80211_bpf_params *);
72 int	ieee80211_output(struct ifnet *, struct mbuf *,
73                struct sockaddr *, struct rtentry *rt);
74 void	ieee80211_send_setup(struct ieee80211_node *, struct mbuf *, int, int,
75         const uint8_t [IEEE80211_ADDR_LEN], const uint8_t [IEEE80211_ADDR_LEN],
76         const uint8_t [IEEE80211_ADDR_LEN]);
77 void	ieee80211_start(struct ifnet *, struct ifaltq_subque *);
78 int	ieee80211_send_nulldata(struct ieee80211_node *);
79 int	ieee80211_classify(struct ieee80211_node *, struct mbuf *m);
80 struct mbuf *ieee80211_mbuf_adjust(struct ieee80211vap *, int,
81 		struct ieee80211_key *, struct mbuf *);
82 struct mbuf *ieee80211_encap(struct ieee80211vap *, struct ieee80211_node *,
83 		struct mbuf *);
84 int	ieee80211_send_mgmt(struct ieee80211_node *, int, int);
85 struct ieee80211_appie;
86 int	ieee80211_send_probereq(struct ieee80211_node *ni,
87 		const uint8_t sa[IEEE80211_ADDR_LEN],
88 		const uint8_t da[IEEE80211_ADDR_LEN],
89 		const uint8_t bssid[IEEE80211_ADDR_LEN],
90 		const uint8_t *ssid, size_t ssidlen);
91 void    ieee80211_tx_complete(struct ieee80211_node *,
92 		struct mbuf *, int);
93 
94 /*
95  * The formation of ProbeResponse frames requires guidance to
96  * deal with legacy clients.  When the client is identified as
97  * "legacy 11b" ieee80211_send_proberesp is passed this token.
98  */
99 #define	IEEE80211_SEND_LEGACY_11B	0x1	/* legacy 11b client */
100 #define	IEEE80211_SEND_LEGACY_11	0x2	/* other legacy client */
101 #define	IEEE80211_SEND_LEGACY		0x3	/* any legacy client */
102 struct mbuf *ieee80211_alloc_proberesp(struct ieee80211_node *, int);
103 int	ieee80211_send_proberesp(struct ieee80211vap *,
104 		const uint8_t da[IEEE80211_ADDR_LEN], int);
105 struct mbuf *ieee80211_alloc_rts(struct ieee80211com *ic,
106 		const uint8_t [IEEE80211_ADDR_LEN],
107 		const uint8_t [IEEE80211_ADDR_LEN], uint16_t);
108 struct mbuf *ieee80211_alloc_cts(struct ieee80211com *,
109 		const uint8_t [IEEE80211_ADDR_LEN], uint16_t);
110 
111 uint8_t *ieee80211_add_rates(uint8_t *, const struct ieee80211_rateset *);
112 uint8_t *ieee80211_add_xrates(uint8_t *, const struct ieee80211_rateset *);
113 uint16_t ieee80211_getcapinfo(struct ieee80211vap *,
114 		struct ieee80211_channel *);
115 
116 void	ieee80211_reset_erp(struct ieee80211com *);
117 void	ieee80211_set_shortslottime(struct ieee80211com *, int onoff);
118 int	ieee80211_iserp_rateset(const struct ieee80211_rateset *);
119 void	ieee80211_setbasicrates(struct ieee80211_rateset *,
120 		enum ieee80211_phymode);
121 void	ieee80211_addbasicrates(struct ieee80211_rateset *,
122 		enum ieee80211_phymode);
123 
124 /*
125  * Return the size of the 802.11 header for a management or data frame.
126  */
127 static __inline int
128 ieee80211_hdrsize(const void *data)
129 {
130 	const struct ieee80211_frame *wh = data;
131 	int size = sizeof(struct ieee80211_frame);
132 
133 	/* NB: we don't handle control frames */
134 	KASSERT((wh->i_fc[0]&IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL,
135 		("%s: control frame", __func__));
136 	if (IEEE80211_IS_DSTODS(wh))
137 		size += IEEE80211_ADDR_LEN;
138 	if (IEEE80211_QOS_HAS_SEQ(wh))
139 		size += sizeof(uint16_t);
140 	return size;
141 }
142 
143 /*
144  * Like ieee80211_hdrsize, but handles any type of frame.
145  */
146 static __inline int
147 ieee80211_anyhdrsize(const void *data)
148 {
149 	const struct ieee80211_frame *wh = data;
150 
151 	if ((wh->i_fc[0]&IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
152 		switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
153 		case IEEE80211_FC0_SUBTYPE_CTS:
154 		case IEEE80211_FC0_SUBTYPE_ACK:
155 			return sizeof(struct ieee80211_frame_ack);
156 		case IEEE80211_FC0_SUBTYPE_BAR:
157 			return sizeof(struct ieee80211_frame_bar);
158 		}
159 		return sizeof(struct ieee80211_frame_min);
160 	} else
161 		return ieee80211_hdrsize(data);
162 }
163 
164 /*
165  * Template for an in-kernel authenticator.  Authenticators
166  * register with the protocol code and are typically loaded
167  * as separate modules as needed.  One special authenticator
168  * is xauth; it intercepts requests so that protocols like
169  * WPA can be handled in user space.
170  */
171 struct ieee80211_authenticator {
172 	const char *ia_name;		/* printable name */
173 	int	(*ia_attach)(struct ieee80211vap *);
174 	void	(*ia_detach)(struct ieee80211vap *);
175 	void	(*ia_node_join)(struct ieee80211_node *);
176 	void	(*ia_node_leave)(struct ieee80211_node *);
177 };
178 void	ieee80211_authenticator_register(int type,
179 		const struct ieee80211_authenticator *);
180 void	ieee80211_authenticator_unregister(int type);
181 const struct ieee80211_authenticator *ieee80211_authenticator_get(int auth);
182 
183 struct ieee80211req;
184 /*
185  * Template for an MAC ACL policy module.  Such modules
186  * register with the protocol code and are passed the sender's
187  * address of each received auth frame for validation.
188  */
189 struct ieee80211_aclator {
190 	const char *iac_name;		/* printable name */
191 	int	(*iac_attach)(struct ieee80211vap *);
192 	void	(*iac_detach)(struct ieee80211vap *);
193 	int	(*iac_check)(struct ieee80211vap *,
194 			const uint8_t mac[IEEE80211_ADDR_LEN]);
195 	int	(*iac_add)(struct ieee80211vap *,
196 			const uint8_t mac[IEEE80211_ADDR_LEN]);
197 	int	(*iac_remove)(struct ieee80211vap *,
198 			const uint8_t mac[IEEE80211_ADDR_LEN]);
199 	int	(*iac_flush)(struct ieee80211vap *);
200 	int	(*iac_setpolicy)(struct ieee80211vap *, int);
201 	int	(*iac_getpolicy)(struct ieee80211vap *);
202 	int	(*iac_setioctl)(struct ieee80211vap *, struct ieee80211req *);
203 	int	(*iac_getioctl)(struct ieee80211vap *, struct ieee80211req *);
204 };
205 void	ieee80211_aclator_register(const struct ieee80211_aclator *);
206 void	ieee80211_aclator_unregister(const struct ieee80211_aclator *);
207 const struct ieee80211_aclator *ieee80211_aclator_get(const char *name);
208 
209 /* flags for ieee80211_fix_rate() */
210 #define	IEEE80211_F_DOSORT	0x00000001	/* sort rate list */
211 #define	IEEE80211_F_DOFRATE	0x00000002	/* use fixed legacy rate */
212 #define	IEEE80211_F_DONEGO	0x00000004	/* calc negotiated rate */
213 #define	IEEE80211_F_DODEL	0x00000008	/* delete ignore rate */
214 #define	IEEE80211_F_DOBRS	0x00000010	/* check basic rate set */
215 #define	IEEE80211_F_JOIN	0x00000020	/* sta joining our bss */
216 #define	IEEE80211_F_DOFMCS	0x00000040	/* use fixed HT rate */
217 int	ieee80211_fix_rate(struct ieee80211_node *,
218 		struct ieee80211_rateset *, int);
219 
220 /*
221  * WME/WMM support.
222  */
223 struct wmeParams {
224 	uint8_t		wmep_acm;
225 	uint8_t		wmep_aifsn;
226 	uint8_t		wmep_logcwmin;		/* log2(cwmin) */
227 	uint8_t		wmep_logcwmax;		/* log2(cwmax) */
228 	uint8_t		wmep_txopLimit;
229 	uint8_t		wmep_noackPolicy;	/* 0 (ack), 1 (no ack) */
230 };
231 #define	IEEE80211_TXOP_TO_US(_txop)	((_txop)<<5)
232 #define	IEEE80211_US_TO_TXOP(_us)	((_us)>>5)
233 
234 struct chanAccParams {
235 	uint8_t		cap_info;		/* version of the current set */
236 	struct wmeParams cap_wmeParams[WME_NUM_AC];
237 };
238 
239 struct ieee80211_wme_state {
240 	u_int	wme_flags;
241 #define	WME_F_AGGRMODE	0x00000001	/* STATUS: WME agressive mode */
242 	u_int	wme_hipri_traffic;	/* VI/VO frames in beacon interval */
243 	u_int	wme_hipri_switch_thresh;/* agressive mode switch thresh */
244 	u_int	wme_hipri_switch_hysteresis;/* agressive mode switch hysteresis */
245 
246 	struct wmeParams wme_params[4];		/* from assoc resp for each AC*/
247 	struct chanAccParams wme_wmeChanParams;	/* WME params applied to self */
248 	struct chanAccParams wme_wmeBssChanParams;/* WME params bcast to stations */
249 	struct chanAccParams wme_chanParams;	/* params applied to self */
250 	struct chanAccParams wme_bssChanParams;	/* params bcast to stations */
251 
252 	int	(*wme_update)(struct ieee80211com *);
253 };
254 
255 void	ieee80211_wme_initparams(struct ieee80211vap *);
256 void	ieee80211_wme_updateparams(struct ieee80211vap *);
257 void	ieee80211_wme_updateparams_locked(struct ieee80211vap *);
258 
259 /*
260  * Return the WME TID from a QoS frame.  If no TID
261  * is present return the index for the "non-QoS" entry.
262  */
263 static __inline uint8_t
264 ieee80211_gettid(const struct ieee80211_frame *wh)
265 {
266 	uint8_t tid;
267 
268 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
269 		if (IEEE80211_IS_DSTODS(wh))
270 			tid = ((const struct ieee80211_qosframe_addr4 *)wh)->
271 				i_qos[0];
272 		else
273 			tid = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
274 		tid &= IEEE80211_QOS_TID;
275 	} else
276 		tid = IEEE80211_NONQOS_TID;
277 	return tid;
278 }
279 
280 void	ieee80211_waitfor_parent(struct ieee80211com *);
281 void	ieee80211_start_locked(struct ieee80211vap *);
282 void	ieee80211_init(void *);
283 void	ieee80211_start_all(struct ieee80211com *);
284 void	ieee80211_stop_locked(struct ieee80211vap *);
285 void	ieee80211_stop(struct ieee80211vap *);
286 void	ieee80211_stop_all(struct ieee80211com *);
287 void	ieee80211_suspend_all(struct ieee80211com *);
288 void	ieee80211_resume_all(struct ieee80211com *);
289 void	ieee80211_dturbo_switch(struct ieee80211vap *, int newflags);
290 void	ieee80211_swbmiss_callout(void *arg);
291 void	ieee80211_beacon_miss(struct ieee80211com *);
292 int	ieee80211_new_state(struct ieee80211vap *, enum ieee80211_state, int);
293 int     ieee80211_new_state_locked(struct ieee80211vap *, enum ieee80211_state,
294 		int);
295 void	ieee80211_print_essid(const uint8_t *, int);
296 void	ieee80211_dump_pkt(struct ieee80211com *,
297 		const uint8_t *, int, int, int);
298 
299 extern 	const char *ieee80211_opmode_name[];
300 extern	const char *ieee80211_state_name[IEEE80211_S_MAX];
301 extern	const char *ieee80211_wme_acnames[];
302 
303 /*
304  * Beacon frames constructed by ieee80211_beacon_alloc
305  * have the following structure filled in so drivers
306  * can update the frame later w/ minimal overhead.
307  */
308 struct ieee80211_beacon_offsets {
309 	uint8_t		bo_flags[4];	/* update/state flags */
310 	uint16_t	*bo_caps;	/* capabilities */
311 	uint8_t		*bo_cfp;	/* start of CFParms element */
312 	uint8_t		*bo_tim;	/* start of atim/dtim */
313 	uint8_t		*bo_wme;	/* start of WME parameters */
314 	uint8_t		*bo_tdma;	/* start of TDMA parameters */
315 	uint8_t		*bo_tim_trailer;/* start of fixed-size trailer */
316 	uint16_t	bo_tim_len;	/* atim/dtim length in bytes */
317 	uint16_t	bo_tim_trailer_len;/* tim trailer length in bytes */
318 	uint8_t		*bo_erp;	/* start of ERP element */
319 	uint8_t		*bo_htinfo;	/* start of HT info element */
320 	uint8_t		*bo_ath;	/* start of ATH parameters */
321 	uint8_t		*bo_appie;	/* start of AppIE element */
322 	uint16_t	bo_appie_len;	/* AppIE length in bytes */
323 	uint16_t	bo_csa_trailer_len;
324 	uint8_t		*bo_csa;	/* start of CSA element */
325 	uint8_t		*bo_meshconf;	/* start of MESHCONF element */
326 	uint8_t		*bo_spare[3];
327 };
328 struct mbuf *ieee80211_beacon_alloc(struct ieee80211_node *,
329 		struct ieee80211_beacon_offsets *);
330 
331 /*
332  * Beacon frame updates are signaled through calls to iv_update_beacon
333  * with one of the IEEE80211_BEACON_* tokens defined below.  For devices
334  * that construct beacon frames on the host this can trigger a rebuild
335  * or defer the processing.  For devices that offload beacon frame
336  * handling this callback can be used to signal a rebuild.  The bo_flags
337  * array in the ieee80211_beacon_offsets structure is intended to record
338  * deferred processing requirements; ieee80211_beacon_update uses the
339  * state to optimize work.  Since this structure is owned by the driver
340  * and not visible to the 802.11 layer drivers must supply an iv_update_beacon
341  * callback that marks the flag bits and schedules (as necessary) an update.
342  */
343 enum {
344 	IEEE80211_BEACON_CAPS	= 0,	/* capabilities */
345 	IEEE80211_BEACON_TIM	= 1,	/* DTIM/ATIM */
346 	IEEE80211_BEACON_WME	= 2,
347 	IEEE80211_BEACON_ERP	= 3,	/* Extended Rate Phy */
348 	IEEE80211_BEACON_HTINFO	= 4,	/* HT Information */
349 	IEEE80211_BEACON_APPIE	= 5,	/* Application IE's */
350 	IEEE80211_BEACON_CFP	= 6,	/* CFParms */
351 	IEEE80211_BEACON_CSA	= 7,	/* Channel Switch Announcement */
352 	IEEE80211_BEACON_TDMA	= 9,	/* TDMA Info */
353 	IEEE80211_BEACON_ATH	= 10,	/* ATH parameters */
354 	IEEE80211_BEACON_MESHCONF = 11,	/* Mesh Configuration */
355 };
356 int	ieee80211_beacon_update(struct ieee80211_node *,
357 		struct ieee80211_beacon_offsets *, struct mbuf *, int mcast);
358 
359 void	ieee80211_csa_startswitch(struct ieee80211com *,
360 		struct ieee80211_channel *, int mode, int count);
361 void	ieee80211_csa_completeswitch(struct ieee80211com *);
362 void	ieee80211_csa_cancelswitch(struct ieee80211com *);
363 void	ieee80211_cac_completeswitch(struct ieee80211vap *);
364 
365 /*
366  * Notification methods called from the 802.11 state machine.
367  * Note that while these are defined here, their implementation
368  * is OS-specific.
369  */
370 void	ieee80211_notify_node_join(struct ieee80211_node *, int newassoc);
371 void	ieee80211_notify_node_leave(struct ieee80211_node *);
372 void	ieee80211_notify_scan_done(struct ieee80211vap *);
373 void	ieee80211_notify_wds_discover(struct ieee80211_node *);
374 void	ieee80211_notify_csa(struct ieee80211com *,
375 		const struct ieee80211_channel *, int mode, int count);
376 void	ieee80211_notify_radar(struct ieee80211com *,
377 		const struct ieee80211_channel *);
378 enum ieee80211_notify_cac_event {
379 	IEEE80211_NOTIFY_CAC_START  = 0, /* CAC timer started */
380 	IEEE80211_NOTIFY_CAC_STOP   = 1, /* CAC intentionally stopped */
381 	IEEE80211_NOTIFY_CAC_RADAR  = 2, /* CAC stopped due to radar detectio */
382 	IEEE80211_NOTIFY_CAC_EXPIRE = 3, /* CAC expired w/o radar */
383 };
384 void	ieee80211_notify_cac(struct ieee80211com *,
385 		const struct ieee80211_channel *,
386 		enum ieee80211_notify_cac_event);
387 void	ieee80211_notify_node_deauth(struct ieee80211_node *);
388 void	ieee80211_notify_node_auth(struct ieee80211_node *);
389 void	ieee80211_notify_country(struct ieee80211vap *, const uint8_t [],
390 		const uint8_t cc[2]);
391 void	ieee80211_notify_radio(struct ieee80211com *, int);
392 #endif /* _NET80211_IEEE80211_PROTO_H_ */
393