1 /*-
2  * Copyright (c) 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 
26 #include <sys/cdefs.h>
27 #ifdef __FreeBSD__
28 __FBSDID("$FreeBSD$");
29 #endif
30 
31 /*
32  * IEEE 802.11 send/recv action frame support.
33  */
34 
35 #include "opt_inet.h"
36 #include "opt_wlan.h"
37 
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/systm.h>
42 
43 #include <sys/socket.h>
44 
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_media.h>
48 #include <net/ethernet.h>
49 
50 #include <netproto/802_11/ieee80211_var.h>
51 #include <netproto/802_11/ieee80211_action.h>
52 #include <netproto/802_11/ieee80211_mesh.h>
53 
54 static int
send_inval(struct ieee80211_node * ni,int cat,int act,void * sa)55 send_inval(struct ieee80211_node *ni, int cat, int act, void *sa)
56 {
57 	return EINVAL;
58 }
59 
60 static ieee80211_send_action_func *ba_send_action[8] = {
61 	send_inval, send_inval, send_inval, send_inval,
62 	send_inval, send_inval, send_inval, send_inval,
63 };
64 static ieee80211_send_action_func *ht_send_action[8] = {
65 	send_inval, send_inval, send_inval, send_inval,
66 	send_inval, send_inval, send_inval, send_inval,
67 };
68 static ieee80211_send_action_func *meshpl_send_action[8] = {
69 	send_inval, send_inval, send_inval, send_inval,
70 	send_inval, send_inval, send_inval, send_inval,
71 };
72 static ieee80211_send_action_func *meshaction_send_action[12] = {
73 	send_inval, send_inval, send_inval, send_inval,
74 	send_inval, send_inval, send_inval, send_inval,
75 	send_inval, send_inval, send_inval, send_inval,
76 };
77 static ieee80211_send_action_func *vendor_send_action[8] = {
78 	send_inval, send_inval, send_inval, send_inval,
79 	send_inval, send_inval, send_inval, send_inval,
80 };
81 
82 int
ieee80211_send_action_register(int cat,int act,ieee80211_send_action_func * f)83 ieee80211_send_action_register(int cat, int act, ieee80211_send_action_func *f)
84 {
85 	switch (cat) {
86 	case IEEE80211_ACTION_CAT_BA:
87 		if (act >= nitems(ba_send_action))
88 			break;
89 		ba_send_action[act] = f;
90 		return 0;
91 	case IEEE80211_ACTION_CAT_HT:
92 		if (act >= nitems(ht_send_action))
93 			break;
94 		ht_send_action[act] = f;
95 		return 0;
96 	case IEEE80211_ACTION_CAT_SELF_PROT:
97 		if (act >= nitems(meshpl_send_action))
98 			break;
99 		meshpl_send_action[act] = f;
100 		return 0;
101 	case IEEE80211_ACTION_CAT_MESH:
102 		if (act >= nitems(meshaction_send_action))
103 			break;
104 		meshaction_send_action[act] = f;
105 		return 0;
106 		break;
107 	case IEEE80211_ACTION_CAT_VENDOR:
108 		if (act >= nitems(vendor_send_action))
109 			break;
110 		vendor_send_action[act] = f;
111 		return 0;
112 	}
113 	return EINVAL;
114 }
115 
116 void
ieee80211_send_action_unregister(int cat,int act)117 ieee80211_send_action_unregister(int cat, int act)
118 {
119 	ieee80211_send_action_register(cat, act, send_inval);
120 }
121 
122 int
ieee80211_send_action(struct ieee80211_node * ni,int cat,int act,void * sa)123 ieee80211_send_action(struct ieee80211_node *ni, int cat, int act, void *sa)
124 {
125 	ieee80211_send_action_func *f = send_inval;
126 
127 	switch (cat) {
128 	case IEEE80211_ACTION_CAT_BA:
129 		if (act < nitems(ba_send_action))
130 			f = ba_send_action[act];
131 		break;
132 	case IEEE80211_ACTION_CAT_HT:
133 		if (act < nitems(ht_send_action))
134 			f = ht_send_action[act];
135 		break;
136 	case IEEE80211_ACTION_CAT_SELF_PROT:
137 		if (act < nitems(meshpl_send_action))
138 			f = meshpl_send_action[act];
139 		break;
140 	case IEEE80211_ACTION_CAT_MESH:
141 		if (act < nitems(meshaction_send_action))
142 			f = meshaction_send_action[act];
143 		break;
144 	case IEEE80211_ACTION_CAT_VENDOR:
145 		if (act < nitems(vendor_send_action))
146 			f = vendor_send_action[act];
147 		break;
148 	}
149 	return f(ni, cat, act, sa);
150 }
151 
152 static int
recv_inval(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)153 recv_inval(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
154 	const uint8_t *frm, const uint8_t *efrm)
155 {
156 	return EINVAL;
157 }
158 
159 static ieee80211_recv_action_func *ba_recv_action[8] = {
160 	recv_inval, recv_inval, recv_inval, recv_inval,
161 	recv_inval, recv_inval, recv_inval, recv_inval,
162 };
163 static ieee80211_recv_action_func *ht_recv_action[8] = {
164 	recv_inval, recv_inval, recv_inval, recv_inval,
165 	recv_inval, recv_inval, recv_inval, recv_inval,
166 };
167 static ieee80211_recv_action_func *meshpl_recv_action[8] = {
168 	recv_inval, recv_inval, recv_inval, recv_inval,
169 	recv_inval, recv_inval, recv_inval, recv_inval,
170 };
171 static ieee80211_recv_action_func *meshaction_recv_action[12] = {
172 	recv_inval, recv_inval, recv_inval, recv_inval,
173 	recv_inval, recv_inval, recv_inval, recv_inval,
174 	recv_inval, recv_inval, recv_inval, recv_inval,
175 };
176 static ieee80211_recv_action_func *vendor_recv_action[8] = {
177 	recv_inval, recv_inval, recv_inval, recv_inval,
178 	recv_inval, recv_inval, recv_inval, recv_inval,
179 };
180 
181 int
ieee80211_recv_action_register(int cat,int act,ieee80211_recv_action_func * f)182 ieee80211_recv_action_register(int cat, int act, ieee80211_recv_action_func *f)
183 {
184 	switch (cat) {
185 	case IEEE80211_ACTION_CAT_BA:
186 		if (act >= nitems(ba_recv_action))
187 			break;
188 		ba_recv_action[act] = f;
189 		return 0;
190 	case IEEE80211_ACTION_CAT_HT:
191 		if (act >= nitems(ht_recv_action))
192 			break;
193 		ht_recv_action[act] = f;
194 		return 0;
195 	case IEEE80211_ACTION_CAT_SELF_PROT:
196 		if (act >= nitems(meshpl_recv_action))
197 			break;
198 		meshpl_recv_action[act] = f;
199 		return 0;
200 	case IEEE80211_ACTION_CAT_MESH:
201 		if (act >= nitems(meshaction_recv_action))
202 			break;
203 		meshaction_recv_action[act] = f;
204 		return 0;
205 	case IEEE80211_ACTION_CAT_VENDOR:
206 		if (act >= nitems(vendor_recv_action))
207 			break;
208 		vendor_recv_action[act] = f;
209 		return 0;
210 	}
211 	return EINVAL;
212 }
213 
214 void
ieee80211_recv_action_unregister(int cat,int act)215 ieee80211_recv_action_unregister(int cat, int act)
216 {
217 	ieee80211_recv_action_register(cat, act, recv_inval);
218 }
219 
220 int
ieee80211_recv_action(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)221 ieee80211_recv_action(struct ieee80211_node *ni,
222 	const struct ieee80211_frame *wh,
223 	const uint8_t *frm, const uint8_t *efrm)
224 {
225 	ieee80211_recv_action_func *f = recv_inval;
226 	struct ieee80211vap *vap = ni->ni_vap;
227 	const struct ieee80211_action *ia =
228 	    (const struct ieee80211_action *) frm;
229 
230 	switch (ia->ia_category) {
231 	case IEEE80211_ACTION_CAT_BA:
232 		if (ia->ia_action < nitems(ba_recv_action))
233 			f = ba_recv_action[ia->ia_action];
234 		break;
235 	case IEEE80211_ACTION_CAT_HT:
236 		if (ia->ia_action < nitems(ht_recv_action))
237 			f = ht_recv_action[ia->ia_action];
238 		break;
239 	case IEEE80211_ACTION_CAT_SELF_PROT:
240 		if (ia->ia_action < nitems(meshpl_recv_action))
241 			f = meshpl_recv_action[ia->ia_action];
242 		break;
243 	case IEEE80211_ACTION_CAT_MESH:
244 		if (ni == vap->iv_bss ||
245 		    ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
246 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
247 			    ni->ni_macaddr, NULL,
248 			    "peer link not yet established (%d), cat %s act %u",
249 			    ni->ni_mlstate, "mesh action", ia->ia_action);
250 			vap->iv_stats.is_mesh_nolink++;
251 			break;
252 		}
253 		if (ia->ia_action < nitems(meshaction_recv_action))
254 			f = meshaction_recv_action[ia->ia_action];
255 		break;
256 	case IEEE80211_ACTION_CAT_VENDOR:
257 		if (ia->ia_action < nitems(vendor_recv_action))
258 			f = vendor_recv_action[ia->ia_action];
259 		break;
260 	}
261 	return f(ni, wh, frm, efrm);
262 }
263