1b032f27cSSam Leffler /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
4b032f27cSSam Leffler * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5b032f27cSSam Leffler * All rights reserved.
6b032f27cSSam Leffler *
7b032f27cSSam Leffler * Redistribution and use in source and binary forms, with or without
8b032f27cSSam Leffler * modification, are permitted provided that the following conditions
9b032f27cSSam Leffler * are met:
10b032f27cSSam Leffler * 1. Redistributions of source code must retain the above copyright
11b032f27cSSam Leffler * notice, this list of conditions and the following disclaimer.
12b032f27cSSam Leffler * 2. Redistributions in binary form must reproduce the above copyright
13b032f27cSSam Leffler * notice, this list of conditions and the following disclaimer in the
14b032f27cSSam Leffler * documentation and/or other materials provided with the distribution.
15b032f27cSSam Leffler *
16b032f27cSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17b032f27cSSam Leffler * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18b032f27cSSam Leffler * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19b032f27cSSam Leffler * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20b032f27cSSam Leffler * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21b032f27cSSam Leffler * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22b032f27cSSam Leffler * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23b032f27cSSam Leffler * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24b032f27cSSam Leffler * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25b032f27cSSam Leffler * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b032f27cSSam Leffler */
27b032f27cSSam Leffler
28b032f27cSSam Leffler /*
29b032f27cSSam Leffler * IEEE 802.11 Monitor mode support.
30b032f27cSSam Leffler */
31b032f27cSSam Leffler #include "opt_inet.h"
32b032f27cSSam Leffler #include "opt_wlan.h"
33b032f27cSSam Leffler
34b032f27cSSam Leffler #include <sys/param.h>
35b032f27cSSam Leffler #include <sys/systm.h>
36b032f27cSSam Leffler #include <sys/mbuf.h>
37b032f27cSSam Leffler #include <sys/malloc.h>
38b032f27cSSam Leffler #include <sys/kernel.h>
39b032f27cSSam Leffler
40b032f27cSSam Leffler #include <sys/socket.h>
41b032f27cSSam Leffler #include <sys/sockio.h>
42b032f27cSSam Leffler #include <sys/endian.h>
43b032f27cSSam Leffler #include <sys/errno.h>
44b032f27cSSam Leffler #include <sys/proc.h>
45b032f27cSSam Leffler #include <sys/sysctl.h>
46b032f27cSSam Leffler
47b032f27cSSam Leffler #include <net/if.h>
4876039bc8SGleb Smirnoff #include <net/if_var.h>
49b032f27cSSam Leffler #include <net/if_media.h>
50b032f27cSSam Leffler #include <net/if_llc.h>
51b032f27cSSam Leffler #include <net/ethernet.h>
52b032f27cSSam Leffler
53b032f27cSSam Leffler #include <net/bpf.h>
54b032f27cSSam Leffler
55b032f27cSSam Leffler #include <net80211/ieee80211_var.h>
56b032f27cSSam Leffler #include <net80211/ieee80211_monitor.h>
57b032f27cSSam Leffler
58b032f27cSSam Leffler static void monitor_vattach(struct ieee80211vap *);
59b032f27cSSam Leffler static int monitor_newstate(struct ieee80211vap *, enum ieee80211_state, int);
60b032f27cSSam Leffler static int monitor_input(struct ieee80211_node *ni, struct mbuf *m,
61c79f192cSAdrian Chadd const struct ieee80211_rx_stats *rxs, int rssi, int nf);
62b032f27cSSam Leffler
63b032f27cSSam Leffler void
ieee80211_monitor_attach(struct ieee80211com * ic)64b032f27cSSam Leffler ieee80211_monitor_attach(struct ieee80211com *ic)
65b032f27cSSam Leffler {
66b032f27cSSam Leffler ic->ic_vattach[IEEE80211_M_MONITOR] = monitor_vattach;
67b032f27cSSam Leffler }
68b032f27cSSam Leffler
69b032f27cSSam Leffler void
ieee80211_monitor_detach(struct ieee80211com * ic)70b032f27cSSam Leffler ieee80211_monitor_detach(struct ieee80211com *ic)
71b032f27cSSam Leffler {
72b032f27cSSam Leffler }
73b032f27cSSam Leffler
74b032f27cSSam Leffler static void
monitor_vdetach(struct ieee80211vap * vap)75b032f27cSSam Leffler monitor_vdetach(struct ieee80211vap *vap)
76b032f27cSSam Leffler {
77b032f27cSSam Leffler }
78b032f27cSSam Leffler
79b032f27cSSam Leffler static void
monitor_vattach(struct ieee80211vap * vap)80b032f27cSSam Leffler monitor_vattach(struct ieee80211vap *vap)
81b032f27cSSam Leffler {
82b032f27cSSam Leffler vap->iv_newstate = monitor_newstate;
83b032f27cSSam Leffler vap->iv_input = monitor_input;
84b032f27cSSam Leffler vap->iv_opdetach = monitor_vdetach;
85b032f27cSSam Leffler }
86b032f27cSSam Leffler
87b032f27cSSam Leffler /*
88b032f27cSSam Leffler * IEEE80211_M_MONITOR vap state machine handler.
89b032f27cSSam Leffler */
90b032f27cSSam Leffler static int
monitor_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)91b032f27cSSam Leffler monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
92b032f27cSSam Leffler {
93b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic;
94b032f27cSSam Leffler enum ieee80211_state ostate;
95b032f27cSSam Leffler
96b032f27cSSam Leffler IEEE80211_LOCK_ASSERT(ic);
97b032f27cSSam Leffler
98b032f27cSSam Leffler ostate = vap->iv_state;
99b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
100b032f27cSSam Leffler __func__, ieee80211_state_name[ostate],
101b032f27cSSam Leffler ieee80211_state_name[nstate], arg);
102b032f27cSSam Leffler vap->iv_state = nstate; /* state transition */
103b032f27cSSam Leffler if (nstate == IEEE80211_S_RUN) {
104b032f27cSSam Leffler switch (ostate) {
105b032f27cSSam Leffler case IEEE80211_S_INIT:
106b032f27cSSam Leffler ieee80211_create_ibss(vap, ic->ic_curchan);
107b032f27cSSam Leffler break;
108b032f27cSSam Leffler default:
109b032f27cSSam Leffler break;
110b032f27cSSam Leffler }
111b032f27cSSam Leffler /*
112b032f27cSSam Leffler * NB: this shouldn't be here but many people use
113b032f27cSSam Leffler * monitor mode for raw packets; once we switch
114b032f27cSSam Leffler * them over to adhoc demo mode remove this.
115b032f27cSSam Leffler */
116b032f27cSSam Leffler ieee80211_node_authorize(vap->iv_bss);
117b032f27cSSam Leffler }
118b032f27cSSam Leffler return 0;
119b032f27cSSam Leffler }
120b032f27cSSam Leffler
121b032f27cSSam Leffler /*
122b032f27cSSam Leffler * Process a received frame in monitor mode.
123b032f27cSSam Leffler */
124b032f27cSSam Leffler static int
monitor_input(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_rx_stats * rxs,int rssi,int nf)125c79f192cSAdrian Chadd monitor_input(struct ieee80211_node *ni, struct mbuf *m,
126c79f192cSAdrian Chadd const struct ieee80211_rx_stats *rxs, int rssi, int nf)
127b032f27cSSam Leffler {
128b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap;
12913471c2bSSam Leffler struct ifnet *ifp = vap->iv_ifp;
13013471c2bSSam Leffler
131dea45121SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
132b032f27cSSam Leffler
133a6c3cf3eSSam Leffler if (ieee80211_radiotap_active_vap(vap))
1345463c4a4SSam Leffler ieee80211_radiotap_rx(vap, m);
135b032f27cSSam Leffler m_freem(m);
136b032f27cSSam Leffler return -1;
137b032f27cSSam Leffler }
138