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