1 /*-
2  * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD: head/sys/net80211/ieee80211_input.h 195757 2009-07-18 20:19:53Z sam $
26  */
27 #ifndef _NET80211_IEEE80211_INPUT_H_
28 #define _NET80211_IEEE80211_INPUT_H_
29 
30 /* Verify the existence and length of __elem or get out. */
31 #define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen, _action) do {	\
32 	if ((__elem) == NULL) {						\
33 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ELEMID,		\
34 		    wh, NULL, "%s", "no " #__elem );			\
35 		vap->iv_stats.is_rx_elem_missing++;			\
36 		_action;						\
37 	} else if ((__elem)[1] > (__maxlen)) {				\
38 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ELEMID,		\
39 		    wh, NULL, "bad " #__elem " len %d", (__elem)[1]);	\
40 		vap->iv_stats.is_rx_elem_toobig++;			\
41 		_action;						\
42 	}								\
43 } while (0)
44 
45 #define	IEEE80211_VERIFY_LENGTH(_len, _minlen, _action) do {		\
46 	if ((_len) < (_minlen)) {					\
47 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ELEMID,		\
48 		    wh, NULL, "ie too short, got %jd, expected %jd",	\
49 		    (intmax_t)(_len), (intmax_t)(_minlen));		\
50 		vap->iv_stats.is_rx_elem_toosmall++;			\
51 		_action;						\
52 	}								\
53 } while (0)
54 
55 void	ieee80211_ssid_mismatch(struct ieee80211vap *, const char *tag,
56 	uint8_t mac[IEEE80211_ADDR_LEN], uint8_t *ssid);
57 
58 #ifdef IEEE80211_DEBUG
59 
60 #define	IEEE80211_VERIFY_SSID(_ni, _ssid, _action) do {			\
61 	if ((_ssid)[1] != 0 &&						\
62 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
63 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
64 		if (ieee80211_msg_input(vap))				\
65 			ieee80211_ssid_mismatch(vap, 			\
66 			    ieee80211_mgt_subtype_name[subtype >>	\
67 				IEEE80211_FC0_SUBTYPE_SHIFT],		\
68 				wh->i_addr2, _ssid);			\
69 		vap->iv_stats.is_rx_ssidmismatch++;			\
70 		_action;						\
71 	}								\
72 } while (0)
73 #else /* !IEEE80211_DEBUG */
74 #define	IEEE80211_VERIFY_SSID(_ni, _ssid, _action) do {			\
75 	if ((_ssid)[1] != 0 &&						\
76 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
77 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
78 		vap->iv_stats.is_rx_ssidmismatch++;			\
79 		_action;						\
80 	}								\
81 } while (0)
82 #endif /* !IEEE80211_DEBUG */
83 
84 /* unalligned little endian access */
85 #define LE_READ_2(p)					\
86 	((uint16_t)					\
87 	 ((((const uint8_t *)(p))[0]      ) |		\
88 	  (((const uint8_t *)(p))[1] <<  8)))
89 #define LE_READ_4(p)					\
90 	((uint32_t)					\
91 	 ((((const uint8_t *)(p))[0]      ) |		\
92 	  (((const uint8_t *)(p))[1] <<  8) |		\
93 	  (((const uint8_t *)(p))[2] << 16) |		\
94 	  (((const uint8_t *)(p))[3] << 24)))
95 
96 static __inline int
97 iswpaoui(const uint8_t *frm)
98 {
99 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
100 }
101 
102 static __inline int
103 iswmeoui(const uint8_t *frm)
104 {
105 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
106 }
107 
108 static __inline int
109 iswmeparam(const uint8_t *frm)
110 {
111 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
112 		frm[6] == WME_PARAM_OUI_SUBTYPE;
113 }
114 
115 static __inline int
116 iswmeinfo(const uint8_t *frm)
117 {
118 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
119 		frm[6] == WME_INFO_OUI_SUBTYPE;
120 }
121 
122 static __inline int
123 isatherosoui(const uint8_t *frm)
124 {
125 	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
126 }
127 
128 static __inline int
129 istdmaoui(const uint8_t *frm)
130 {
131 	return frm[1] > 3 && LE_READ_4(frm+2) == ((TDMA_OUI_TYPE<<24)|TDMA_OUI);
132 }
133 
134 static __inline int
135 ishtcapoui(const uint8_t *frm)
136 {
137 	return frm[1] > 3 && LE_READ_4(frm+2) == ((BCM_OUI_HTCAP<<24)|BCM_OUI);
138 }
139 
140 static __inline int
141 ishtinfooui(const uint8_t *frm)
142 {
143 	return frm[1] > 3 && LE_READ_4(frm+2) == ((BCM_OUI_HTINFO<<24)|BCM_OUI);
144 }
145 
146 void	ieee80211_deliver_data(struct ieee80211vap *,
147 		struct ieee80211_node *, struct mbuf *);
148 struct mbuf *ieee80211_defrag(struct ieee80211_node *,
149 		struct mbuf *, int);
150 struct mbuf *ieee80211_realign(struct ieee80211vap *, struct mbuf *, size_t);
151 struct mbuf *ieee80211_decap(struct ieee80211vap *, struct mbuf *, int);
152 struct mbuf *ieee80211_decap1(struct mbuf *, int *);
153 int	ieee80211_setup_rates(struct ieee80211_node *ni,
154 		const uint8_t *rates, const uint8_t *xrates, int flags);
155 void ieee80211_send_error(struct ieee80211_node *,
156 		const uint8_t mac[IEEE80211_ADDR_LEN], int subtype, int arg);
157 int	ieee80211_alloc_challenge(struct ieee80211_node *);
158 int	ieee80211_parse_beacon(struct ieee80211_node *, struct mbuf *,
159 		struct ieee80211_scanparams *);
160 int	ieee80211_parse_action(struct ieee80211_node *, struct mbuf *);
161 #endif /* _NET80211_IEEE80211_INPUT_H_ */
162