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