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