xref: /haiku/src/libs/compat/freebsd_wlan/net80211/ieee80211_alq.c (revision b8a45b3a2df2379b4301bf3bd5949b9a105be4ba)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Adrian Chadd, Xenion Lty Ltd
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #ifdef __FreeBSD__
30 #endif
31 
32 /*
33  * net80211 fast-logging support, primarily for debugging.
34  *
35  * This implements a single debugging queue which includes
36  * per-device enumeration where needed.
37  */
38 
39 #include "opt_wlan.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/malloc.h>
45 #include <sys/endian.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/pcpu.h>
49 #include <sys/proc.h>
50 #include <sys/ucred.h>
51 #include <sys/alq.h>
52 
53 #include <sys/socket.h>
54 
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_media.h>
58 #include <net/ethernet.h>
59 
60 #include <net80211/ieee80211_var.h>
61 #include <net80211/ieee80211_freebsd.h>
62 #include <net80211/ieee80211_alq.h>
63 
64 static struct alq *ieee80211_alq;
65 static int ieee80211_alq_lost;
66 static int ieee80211_alq_logged;
67 static char ieee80211_alq_logfile[MAXPATHLEN] = "/tmp/net80211.log";
68 static unsigned int ieee80211_alq_qsize = 64*1024;
69 
70 static int
71 ieee80211_alq_setlogging(int enable)
72 {
73 	int error;
74 
75 	if (enable) {
76 		if (ieee80211_alq)
77 			alq_close(ieee80211_alq);
78 
79 		error = alq_open(&ieee80211_alq,
80 		    ieee80211_alq_logfile,
81 		    curthread->td_ucred,
82 		    ALQ_DEFAULT_CMODE,
83 		    ieee80211_alq_qsize, 0);
84 		ieee80211_alq_lost = 0;
85 		ieee80211_alq_logged = 0;
86 		printf("net80211: logging to %s enabled; "
87 		    "struct size %d bytes\n",
88 		    ieee80211_alq_logfile,
89 		    (int) sizeof(struct ieee80211_alq_rec));
90 	} else {
91 		if (ieee80211_alq)
92 			alq_close(ieee80211_alq);
93 		ieee80211_alq = NULL;
94 		printf("net80211: logging disabled\n");
95 		error = 0;
96 	}
97 	return (error);
98 }
99 
100 static int
101 sysctl_ieee80211_alq_log(SYSCTL_HANDLER_ARGS)
102 {
103 	int error, enable;
104 
105 	enable = (ieee80211_alq != NULL);
106 	error = sysctl_handle_int(oidp, &enable, 0, req);
107 	if (error || !req->newptr)
108 		return (error);
109 	else
110 		return (ieee80211_alq_setlogging(enable));
111 }
112 
113 SYSCTL_PROC(_net_wlan, OID_AUTO, alq,
114     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
115     sysctl_ieee80211_alq_log, "I",
116     "Enable net80211 alq logging");
117 SYSCTL_INT(_net_wlan, OID_AUTO, alq_size, CTLFLAG_RW,
118 	&ieee80211_alq_qsize, 0, "In-memory log size (bytes)");
119 SYSCTL_INT(_net_wlan, OID_AUTO, alq_lost, CTLFLAG_RW,
120 	&ieee80211_alq_lost, 0, "Debugging operations not logged");
121 SYSCTL_INT(_net_wlan, OID_AUTO, alq_logged, CTLFLAG_RW,
122 	&ieee80211_alq_logged, 0, "Debugging operations logged");
123 
124 static struct ale *
125 ieee80211_alq_get(size_t len)
126 {
127 	struct ale *ale;
128 
129 	ale = alq_getn(ieee80211_alq, len + sizeof(struct ieee80211_alq_rec),
130 	    ALQ_NOWAIT);
131 	if (!ale)
132 		ieee80211_alq_lost++;
133 	else
134 		ieee80211_alq_logged++;
135 	return ale;
136 }
137 
138 int
139 ieee80211_alq_log(struct ieee80211com *ic, struct ieee80211vap *vap,
140     uint32_t op, uint32_t flags, uint16_t srcid, const uint8_t *src,
141     size_t len)
142 {
143 	struct ale *ale;
144 	struct ieee80211_alq_rec *r;
145 	char *dst;
146 
147 	/* Don't log if we're disabled */
148 	if (ieee80211_alq == NULL)
149 		return (0);
150 
151 	if (len > IEEE80211_ALQ_MAX_PAYLOAD)
152 		return (ENOMEM);
153 
154 	ale = ieee80211_alq_get(len);
155 	if (! ale)
156 		return (ENOMEM);
157 
158 	r = (struct ieee80211_alq_rec *) ale->ae_data;
159 	dst = ((char *) r) + sizeof(struct ieee80211_alq_rec);
160 	r->r_timestamp = htobe64(ticks);
161 	if (vap != NULL) {
162 		r->r_wlan = htobe16(vap->iv_ifp->if_dunit);
163 	} else {
164 		r->r_wlan = 0xffff;
165 	}
166 	r->r_src = htobe16(srcid);
167 	r->r_flags = htobe32(flags);
168 	r->r_op = htobe32(op);
169 	r->r_len = htobe32(len + sizeof(struct ieee80211_alq_rec));
170 	r->r_threadid = htobe32((uint32_t) curthread->td_tid);
171 
172 	if (src != NULL)
173 		memcpy(dst, src, len);
174 
175 	alq_post(ieee80211_alq, ale);
176 
177 	return (0);
178 }
179