1753c7e08SAugustin Cavalier /*-
2*86021fd4SAugustin Cavalier * SPDX-License-Identifier: BSD-2-Clause
38244a9baSAugustin Cavalier *
4753c7e08SAugustin Cavalier * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5753c7e08SAugustin Cavalier * All rights reserved.
6753c7e08SAugustin Cavalier *
7753c7e08SAugustin Cavalier * Redistribution and use in source and binary forms, with or without
8753c7e08SAugustin Cavalier * modification, are permitted provided that the following conditions
9753c7e08SAugustin Cavalier * are met:
10753c7e08SAugustin Cavalier * 1. Redistributions of source code must retain the above copyright
11753c7e08SAugustin Cavalier * notice, this list of conditions and the following disclaimer.
12753c7e08SAugustin Cavalier * 2. Redistributions in binary form must reproduce the above copyright
13753c7e08SAugustin Cavalier * notice, this list of conditions and the following disclaimer in the
14753c7e08SAugustin Cavalier * documentation and/or other materials provided with the distribution.
15753c7e08SAugustin Cavalier *
16753c7e08SAugustin Cavalier * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17753c7e08SAugustin Cavalier * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18753c7e08SAugustin Cavalier * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19753c7e08SAugustin Cavalier * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20753c7e08SAugustin Cavalier * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21753c7e08SAugustin Cavalier * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22753c7e08SAugustin Cavalier * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23753c7e08SAugustin Cavalier * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24753c7e08SAugustin Cavalier * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25753c7e08SAugustin Cavalier * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26753c7e08SAugustin Cavalier */
27753c7e08SAugustin Cavalier
28753c7e08SAugustin Cavalier /*
29753c7e08SAugustin Cavalier * IEEE 802.11 Monitor mode support.
30753c7e08SAugustin Cavalier */
31753c7e08SAugustin Cavalier #include "opt_inet.h"
32753c7e08SAugustin Cavalier #include "opt_wlan.h"
33753c7e08SAugustin Cavalier
34753c7e08SAugustin Cavalier #include <sys/param.h>
35753c7e08SAugustin Cavalier #include <sys/systm.h>
36753c7e08SAugustin Cavalier #include <sys/mbuf.h>
37753c7e08SAugustin Cavalier #include <sys/malloc.h>
38753c7e08SAugustin Cavalier #include <sys/kernel.h>
39753c7e08SAugustin Cavalier
40753c7e08SAugustin Cavalier #include <sys/socket.h>
41753c7e08SAugustin Cavalier #include <sys/sockio.h>
42753c7e08SAugustin Cavalier #include <sys/endian.h>
43753c7e08SAugustin Cavalier #include <sys/errno.h>
44753c7e08SAugustin Cavalier #include <sys/proc.h>
45753c7e08SAugustin Cavalier #include <sys/sysctl.h>
46753c7e08SAugustin Cavalier
47753c7e08SAugustin Cavalier #include <net/if.h>
48753c7e08SAugustin Cavalier #include <net/if_var.h>
49753c7e08SAugustin Cavalier #include <net/if_media.h>
50753c7e08SAugustin Cavalier #include <net/if_llc.h>
51753c7e08SAugustin Cavalier #include <net/ethernet.h>
52753c7e08SAugustin Cavalier
53753c7e08SAugustin Cavalier #include <net/bpf.h>
54753c7e08SAugustin Cavalier
55753c7e08SAugustin Cavalier #include <net80211/ieee80211_var.h>
56753c7e08SAugustin Cavalier #include <net80211/ieee80211_monitor.h>
57753c7e08SAugustin Cavalier
58753c7e08SAugustin Cavalier static void monitor_vattach(struct ieee80211vap *);
59753c7e08SAugustin Cavalier static int monitor_newstate(struct ieee80211vap *, enum ieee80211_state, int);
60753c7e08SAugustin Cavalier static int monitor_input(struct ieee80211_node *ni, struct mbuf *m,
61753c7e08SAugustin Cavalier const struct ieee80211_rx_stats *rxs, int rssi, int nf);
62753c7e08SAugustin Cavalier
63753c7e08SAugustin Cavalier void
ieee80211_monitor_attach(struct ieee80211com * ic)64753c7e08SAugustin Cavalier ieee80211_monitor_attach(struct ieee80211com *ic)
65753c7e08SAugustin Cavalier {
66753c7e08SAugustin Cavalier ic->ic_vattach[IEEE80211_M_MONITOR] = monitor_vattach;
67753c7e08SAugustin Cavalier }
68753c7e08SAugustin Cavalier
69753c7e08SAugustin Cavalier void
ieee80211_monitor_detach(struct ieee80211com * ic)70753c7e08SAugustin Cavalier ieee80211_monitor_detach(struct ieee80211com *ic)
71753c7e08SAugustin Cavalier {
72753c7e08SAugustin Cavalier }
73753c7e08SAugustin Cavalier
74753c7e08SAugustin Cavalier static void
monitor_vdetach(struct ieee80211vap * vap)75753c7e08SAugustin Cavalier monitor_vdetach(struct ieee80211vap *vap)
76753c7e08SAugustin Cavalier {
77753c7e08SAugustin Cavalier }
78753c7e08SAugustin Cavalier
79753c7e08SAugustin Cavalier static void
monitor_vattach(struct ieee80211vap * vap)80753c7e08SAugustin Cavalier monitor_vattach(struct ieee80211vap *vap)
81753c7e08SAugustin Cavalier {
82753c7e08SAugustin Cavalier vap->iv_newstate = monitor_newstate;
83753c7e08SAugustin Cavalier vap->iv_input = monitor_input;
84753c7e08SAugustin Cavalier vap->iv_opdetach = monitor_vdetach;
85753c7e08SAugustin Cavalier }
86753c7e08SAugustin Cavalier
87753c7e08SAugustin Cavalier /*
88753c7e08SAugustin Cavalier * IEEE80211_M_MONITOR vap state machine handler.
89753c7e08SAugustin Cavalier */
90753c7e08SAugustin Cavalier static int
monitor_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)91753c7e08SAugustin Cavalier monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
92753c7e08SAugustin Cavalier {
93753c7e08SAugustin Cavalier struct ieee80211com *ic = vap->iv_ic;
94753c7e08SAugustin Cavalier enum ieee80211_state ostate;
95753c7e08SAugustin Cavalier
96753c7e08SAugustin Cavalier IEEE80211_LOCK_ASSERT(ic);
97753c7e08SAugustin Cavalier
98753c7e08SAugustin Cavalier ostate = vap->iv_state;
99753c7e08SAugustin Cavalier IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
100753c7e08SAugustin Cavalier __func__, ieee80211_state_name[ostate],
101753c7e08SAugustin Cavalier ieee80211_state_name[nstate], arg);
102753c7e08SAugustin Cavalier vap->iv_state = nstate; /* state transition */
103753c7e08SAugustin Cavalier if (nstate == IEEE80211_S_RUN) {
104753c7e08SAugustin Cavalier switch (ostate) {
105753c7e08SAugustin Cavalier case IEEE80211_S_INIT:
106753c7e08SAugustin Cavalier ieee80211_create_ibss(vap, ic->ic_curchan);
107753c7e08SAugustin Cavalier break;
108753c7e08SAugustin Cavalier default:
109753c7e08SAugustin Cavalier break;
110753c7e08SAugustin Cavalier }
111753c7e08SAugustin Cavalier /*
112753c7e08SAugustin Cavalier * NB: this shouldn't be here but many people use
113753c7e08SAugustin Cavalier * monitor mode for raw packets; once we switch
114753c7e08SAugustin Cavalier * them over to adhoc demo mode remove this.
115753c7e08SAugustin Cavalier */
116753c7e08SAugustin Cavalier ieee80211_node_authorize(vap->iv_bss);
117753c7e08SAugustin Cavalier }
118753c7e08SAugustin Cavalier return 0;
119753c7e08SAugustin Cavalier }
120753c7e08SAugustin Cavalier
121753c7e08SAugustin Cavalier /*
122753c7e08SAugustin Cavalier * Process a received frame in monitor mode.
123753c7e08SAugustin Cavalier */
124753c7e08SAugustin Cavalier static int
monitor_input(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_rx_stats * rxs,int rssi,int nf)125753c7e08SAugustin Cavalier monitor_input(struct ieee80211_node *ni, struct mbuf *m,
126753c7e08SAugustin Cavalier const struct ieee80211_rx_stats *rxs, int rssi, int nf)
127753c7e08SAugustin Cavalier {
128753c7e08SAugustin Cavalier struct ieee80211vap *vap = ni->ni_vap;
129753c7e08SAugustin Cavalier struct ifnet *ifp = vap->iv_ifp;
130753c7e08SAugustin Cavalier
131753c7e08SAugustin Cavalier if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
132753c7e08SAugustin Cavalier
133753c7e08SAugustin Cavalier if (ieee80211_radiotap_active_vap(vap))
134753c7e08SAugustin Cavalier ieee80211_radiotap_rx(vap, m);
135753c7e08SAugustin Cavalier m_freem(m);
136753c7e08SAugustin Cavalier return -1;
137753c7e08SAugustin Cavalier }
138