xref: /freebsd/sys/net80211/ieee80211_alq.c (revision efe3b0de1438e7a8473d92f2be57072394559e3c)
1 /*-
2  * Copyright (c) 2011 Adrian Chadd, Xenion Lty Ltd
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  * net80211 fast-logging support, primarily for debugging.
33  *
34  * This implements a single debugging queue which includes
35  * per-device enumeration where needed.
36  */
37 
38 #include "opt_wlan.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/malloc.h>
44 #include <sys/endian.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/pcpu.h>
48 #include <sys/proc.h>
49 #include <sys/ucred.h>
50 #include <sys/alq.h>
51 
52 #include <sys/socket.h>
53 
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/if_media.h>
57 #include <net/ethernet.h>
58 
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_freebsd.h>
61 #include <net80211/ieee80211_alq.h>
62 
63 static struct alq *ieee80211_alq;
64 static int ieee80211_alq_lost;
65 static int ieee80211_alq_logged;
66 static char ieee80211_alq_logfile[MAXPATHLEN] = "/tmp/net80211.log";
67 static unsigned int ieee80211_alq_qsize = 64*1024;
68 
69 static int
70 ieee80211_alq_setlogging(int enable)
71 {
72 	int error;
73 
74 	if (enable) {
75 		if (ieee80211_alq)
76 			alq_close(ieee80211_alq);
77 
78 		error = alq_open(&ieee80211_alq,
79 		    ieee80211_alq_logfile,
80 		    curthread->td_ucred,
81 		    ALQ_DEFAULT_CMODE,
82 		    ieee80211_alq_qsize, 0);
83 		ieee80211_alq_lost = 0;
84 		ieee80211_alq_logged = 0;
85 		printf("net80211: logging to %s enabled; "
86 		    "struct size %d bytes\n",
87 		    ieee80211_alq_logfile,
88 		    (int) sizeof(struct ieee80211_alq_rec));
89 	} else {
90 		if (ieee80211_alq)
91 			alq_close(ieee80211_alq);
92 		ieee80211_alq = NULL;
93 		printf("net80211: logging disabled\n");
94 		error = 0;
95 	}
96 	return (error);
97 }
98 
99 static int
100 sysctl_ieee80211_alq_log(SYSCTL_HANDLER_ARGS)
101 {
102 	int error, enable;
103 
104 	enable = (ieee80211_alq != NULL);
105 	error = sysctl_handle_int(oidp, &enable, 0, req);
106 	if (error || !req->newptr)
107 		return (error);
108 	else
109 		return (ieee80211_alq_setlogging(enable));
110 }
111 
112 SYSCTL_PROC(_net_wlan, OID_AUTO, alq, CTLTYPE_INT|CTLFLAG_RW,
113 	0, 0, sysctl_ieee80211_alq_log, "I", "Enable net80211 alq logging");
114 SYSCTL_INT(_net_wlan, OID_AUTO, alq_size, CTLFLAG_RW,
115 	&ieee80211_alq_qsize, 0, "In-memory log size (bytes)");
116 SYSCTL_INT(_net_wlan, OID_AUTO, alq_lost, CTLFLAG_RW,
117 	&ieee80211_alq_lost, 0, "Debugging operations not logged");
118 SYSCTL_INT(_net_wlan, OID_AUTO, alq_logged, CTLFLAG_RW,
119 	&ieee80211_alq_logged, 0, "Debugging operations logged");
120 
121 static struct ale *
122 ieee80211_alq_get(size_t len)
123 {
124 	struct ale *ale;
125 
126 	ale = alq_getn(ieee80211_alq, len + sizeof(struct ieee80211_alq_rec),
127 	    ALQ_NOWAIT);
128 	if (!ale)
129 		ieee80211_alq_lost++;
130 	else
131 		ieee80211_alq_logged++;
132 	return ale;
133 }
134 
135 int
136 ieee80211_alq_log(struct ieee80211com *ic, struct ieee80211vap *vap,
137     uint32_t op, uint32_t flags, uint16_t srcid, const uint8_t *src,
138     size_t len)
139 {
140 	struct ale *ale;
141 	struct ieee80211_alq_rec *r;
142 	char *dst;
143 
144 	/* Don't log if we're disabled */
145 	if (ieee80211_alq == NULL)
146 		return (0);
147 
148 	if (len > IEEE80211_ALQ_MAX_PAYLOAD)
149 		return (ENOMEM);
150 
151 	ale = ieee80211_alq_get(len);
152 	if (! ale)
153 		return (ENOMEM);
154 
155 	r = (struct ieee80211_alq_rec *) ale->ae_data;
156 	dst = ((char *) r) + sizeof(struct ieee80211_alq_rec);
157 	r->r_timestamp = htobe64(ticks);
158 	if (vap != NULL) {
159 		r->r_wlan = htobe16(vap->iv_ifp->if_dunit);
160 	} else {
161 		r->r_wlan = 0xffff;
162 	}
163 	r->r_src = htobe16(srcid);
164 	r->r_flags = htobe32(flags);
165 	r->r_op = htobe32(op);
166 	r->r_len = htobe32(len + sizeof(struct ieee80211_alq_rec));
167 	r->r_threadid = htobe32((uint32_t) curthread->td_tid);
168 
169 	if (src != NULL)
170 		memcpy(dst, src, len);
171 
172 	alq_post(ieee80211_alq, ale);
173 
174 	return (0);
175 }
176