xref: /freebsd/sys/net80211/ieee80211_monitor.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
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  * IEEE 802.11 Monitor mode support.
34  */
35 #include "opt_inet.h"
36 #include "opt_wlan.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/endian.h>
47 #include <sys/errno.h>
48 #include <sys/proc.h>
49 #include <sys/sysctl.h>
50 
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_media.h>
54 #include <net/if_llc.h>
55 #include <net/ethernet.h>
56 
57 #include <net/bpf.h>
58 
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_monitor.h>
61 
62 static void monitor_vattach(struct ieee80211vap *);
63 static int monitor_newstate(struct ieee80211vap *, enum ieee80211_state, int);
64 static int monitor_input(struct ieee80211_node *ni, struct mbuf *m,
65 	const struct ieee80211_rx_stats *rxs, int rssi, int nf);
66 
67 void
68 ieee80211_monitor_attach(struct ieee80211com *ic)
69 {
70 	ic->ic_vattach[IEEE80211_M_MONITOR] = monitor_vattach;
71 }
72 
73 void
74 ieee80211_monitor_detach(struct ieee80211com *ic)
75 {
76 }
77 
78 static void
79 monitor_vdetach(struct ieee80211vap *vap)
80 {
81 }
82 
83 static void
84 monitor_vattach(struct ieee80211vap *vap)
85 {
86 	vap->iv_newstate = monitor_newstate;
87 	vap->iv_input = monitor_input;
88 	vap->iv_opdetach = monitor_vdetach;
89 }
90 
91 /*
92  * IEEE80211_M_MONITOR vap state machine handler.
93  */
94 static int
95 monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
96 {
97 	struct ieee80211com *ic = vap->iv_ic;
98 	enum ieee80211_state ostate;
99 
100 	IEEE80211_LOCK_ASSERT(ic);
101 
102 	ostate = vap->iv_state;
103 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
104 	    __func__, ieee80211_state_name[ostate],
105 	    ieee80211_state_name[nstate], arg);
106 	vap->iv_state = nstate;			/* state transition */
107 	if (nstate == IEEE80211_S_RUN) {
108 		switch (ostate) {
109 		case IEEE80211_S_INIT:
110 			ieee80211_create_ibss(vap, ic->ic_curchan);
111 			break;
112 		default:
113 			break;
114 		}
115 		/*
116 		 * NB: this shouldn't be here but many people use
117 		 * monitor mode for raw packets; once we switch
118 		 * them over to adhoc demo mode remove this.
119 		 */
120 		ieee80211_node_authorize(vap->iv_bss);
121 	}
122 	return 0;
123 }
124 
125 /*
126  * Process a received frame in monitor mode.
127  */
128 static int
129 monitor_input(struct ieee80211_node *ni, struct mbuf *m,
130     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
131 {
132 	struct ieee80211vap *vap = ni->ni_vap;
133 	struct ifnet *ifp = vap->iv_ifp;
134 
135 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
136 
137 	if (ieee80211_radiotap_active_vap(vap))
138 		ieee80211_radiotap_rx(vap, m);
139 	m_freem(m);
140 	return -1;
141 }
142