xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/wl_linux_mon.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Broadcom Dongle Host Driver (DHD), Linux monitor network interface
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 1999-2017, Broadcom Corporation
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *      Unless you and Broadcom execute a separate written software license
9*4882a593Smuzhiyun  * agreement governing use of this software, this software is licensed to you
10*4882a593Smuzhiyun  * under the terms of the GNU General Public License version 2 (the "GPL"),
11*4882a593Smuzhiyun  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
12*4882a593Smuzhiyun  * following added to such license:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *      As a special exception, the copyright holders of this software give you
15*4882a593Smuzhiyun  * permission to link this software with independent modules, and to copy and
16*4882a593Smuzhiyun  * distribute the resulting executable under terms of your choice, provided that
17*4882a593Smuzhiyun  * you also meet, for each linked independent module, the terms and conditions of
18*4882a593Smuzhiyun  * the license of that module.  An independent module is a module which is not
19*4882a593Smuzhiyun  * derived from this software.  The special exception does not apply to any
20*4882a593Smuzhiyun  * modifications of the software.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  *      Notwithstanding the above, under no circumstances may you combine this
23*4882a593Smuzhiyun  * software in any way with any other Broadcom software provided under a license
24*4882a593Smuzhiyun  * other than the GPL, without Broadcom's express prior written consent.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * <<Broadcom-WL-IPTag/Open:>>
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * $Id: wl_linux_mon.c 576195 2015-08-01 18:21:54Z $
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #include <osl.h>
33*4882a593Smuzhiyun #include <linux/string.h>
34*4882a593Smuzhiyun #include <linux/module.h>
35*4882a593Smuzhiyun #include <linux/netdevice.h>
36*4882a593Smuzhiyun #include <linux/etherdevice.h>
37*4882a593Smuzhiyun #include <linux/if_arp.h>
38*4882a593Smuzhiyun #include <linux/ieee80211.h>
39*4882a593Smuzhiyun #include <linux/rtnetlink.h>
40*4882a593Smuzhiyun #include <net/ieee80211_radiotap.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include <wlioctl.h>
43*4882a593Smuzhiyun #include <bcmutils.h>
44*4882a593Smuzhiyun #include <dhd_dbg.h>
45*4882a593Smuzhiyun #include <dngl_stats.h>
46*4882a593Smuzhiyun #include <dhd.h>
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun typedef enum monitor_states
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	MONITOR_STATE_DEINIT = 0x0,
51*4882a593Smuzhiyun 	MONITOR_STATE_INIT = 0x1,
52*4882a593Smuzhiyun 	MONITOR_STATE_INTERFACE_ADDED = 0x2,
53*4882a593Smuzhiyun 	MONITOR_STATE_INTERFACE_DELETED = 0x4
54*4882a593Smuzhiyun } monitor_states_t;
55*4882a593Smuzhiyun int dhd_add_monitor(const char *name, struct net_device **new_ndev);
56*4882a593Smuzhiyun extern int dhd_start_xmit(struct sk_buff *skb, struct net_device *net);
57*4882a593Smuzhiyun int dhd_del_monitor(struct net_device *ndev);
58*4882a593Smuzhiyun int dhd_monitor_init(void *dhd_pub);
59*4882a593Smuzhiyun int dhd_monitor_uninit(void);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /**
62*4882a593Smuzhiyun  * Local declarations and defintions (not exposed)
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun #ifndef DHD_MAX_IFS
65*4882a593Smuzhiyun #define DHD_MAX_IFS 16
66*4882a593Smuzhiyun #endif // endif
67*4882a593Smuzhiyun #define MON_PRINT(format, ...) printk("DHD-MON: %s " format, __func__, ##__VA_ARGS__)
68*4882a593Smuzhiyun #define MON_TRACE MON_PRINT
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun typedef struct monitor_interface {
71*4882a593Smuzhiyun 	int radiotap_enabled;
72*4882a593Smuzhiyun 	struct net_device* real_ndev;	/* The real interface that the monitor is on */
73*4882a593Smuzhiyun 	struct net_device* mon_ndev;
74*4882a593Smuzhiyun } monitor_interface;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun typedef struct dhd_linux_monitor {
77*4882a593Smuzhiyun 	void *dhd_pub;
78*4882a593Smuzhiyun 	monitor_states_t monitor_state;
79*4882a593Smuzhiyun 	monitor_interface mon_if[DHD_MAX_IFS];
80*4882a593Smuzhiyun 	struct mutex lock;		/* lock to protect mon_if */
81*4882a593Smuzhiyun } dhd_linux_monitor_t;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun static dhd_linux_monitor_t g_monitor;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun static struct net_device* lookup_real_netdev(const char *name);
86*4882a593Smuzhiyun static monitor_interface* ndev_to_monif(struct net_device *ndev);
87*4882a593Smuzhiyun static int dhd_mon_if_open(struct net_device *ndev);
88*4882a593Smuzhiyun static int dhd_mon_if_stop(struct net_device *ndev);
89*4882a593Smuzhiyun static int dhd_mon_if_subif_start_xmit(struct sk_buff *skb, struct net_device *ndev);
90*4882a593Smuzhiyun static void dhd_mon_if_set_multicast_list(struct net_device *ndev);
91*4882a593Smuzhiyun static int dhd_mon_if_change_mac(struct net_device *ndev, void *addr);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun static const struct net_device_ops dhd_mon_if_ops = {
94*4882a593Smuzhiyun 	.ndo_open		= dhd_mon_if_open,
95*4882a593Smuzhiyun 	.ndo_stop		= dhd_mon_if_stop,
96*4882a593Smuzhiyun 	.ndo_start_xmit		= dhd_mon_if_subif_start_xmit,
97*4882a593Smuzhiyun #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
98*4882a593Smuzhiyun 	.ndo_set_rx_mode = dhd_mon_if_set_multicast_list,
99*4882a593Smuzhiyun #else
100*4882a593Smuzhiyun 	.ndo_set_multicast_list = dhd_mon_if_set_multicast_list,
101*4882a593Smuzhiyun #endif // endif
102*4882a593Smuzhiyun 	.ndo_set_mac_address 	= dhd_mon_if_change_mac,
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /**
106*4882a593Smuzhiyun  * Local static function defintions
107*4882a593Smuzhiyun  */
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /* Look up dhd's net device table to find a match (e.g. interface "eth0" is a match for "mon.eth0"
110*4882a593Smuzhiyun  * "p2p-eth0-0" is a match for "mon.p2p-eth0-0")
111*4882a593Smuzhiyun  */
lookup_real_netdev(const char * name)112*4882a593Smuzhiyun static struct net_device* lookup_real_netdev(const char *name)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	struct net_device *ndev_found = NULL;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	int i;
117*4882a593Smuzhiyun 	int len = 0;
118*4882a593Smuzhiyun 	int last_name_len = 0;
119*4882a593Smuzhiyun 	struct net_device *ndev;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* We need to find interface "p2p-p2p-0" corresponding to monitor interface "mon-p2p-0",
122*4882a593Smuzhiyun 	 * Once mon iface name reaches IFNAMSIZ, it is reset to p2p0-0 and corresponding mon
123*4882a593Smuzhiyun 	 * iface would be mon-p2p0-0.
124*4882a593Smuzhiyun 	 */
125*4882a593Smuzhiyun 	for (i = 0; i < DHD_MAX_IFS; i++) {
126*4882a593Smuzhiyun 		ndev = dhd_idx2net(g_monitor.dhd_pub, i);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 		/* Skip "p2p" and look for "-p2p0-x" in monitor interface name. If it
129*4882a593Smuzhiyun 		 * it matches, then this netdev is the corresponding real_netdev.
130*4882a593Smuzhiyun 		 */
131*4882a593Smuzhiyun 		if (ndev && strstr(ndev->name, "p2p-p2p0")) {
132*4882a593Smuzhiyun 			len = strlen("p2p");
133*4882a593Smuzhiyun 		} else {
134*4882a593Smuzhiyun 		/* if p2p- is not present, then the IFNAMSIZ have reached and name
135*4882a593Smuzhiyun 		 * would have got reset. In this casse,look for p2p0-x in mon-p2p0-x
136*4882a593Smuzhiyun 		 */
137*4882a593Smuzhiyun 			len = 0;
138*4882a593Smuzhiyun 		}
139*4882a593Smuzhiyun 		if (ndev && strstr(name, (ndev->name + len))) {
140*4882a593Smuzhiyun 			if (strlen(ndev->name) > last_name_len) {
141*4882a593Smuzhiyun 				ndev_found = ndev;
142*4882a593Smuzhiyun 				last_name_len = strlen(ndev->name);
143*4882a593Smuzhiyun 			}
144*4882a593Smuzhiyun 		}
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	return ndev_found;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
ndev_to_monif(struct net_device * ndev)150*4882a593Smuzhiyun static monitor_interface* ndev_to_monif(struct net_device *ndev)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	int i;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	for (i = 0; i < DHD_MAX_IFS; i++) {
155*4882a593Smuzhiyun 		if (g_monitor.mon_if[i].mon_ndev == ndev)
156*4882a593Smuzhiyun 			return &g_monitor.mon_if[i];
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	return NULL;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
dhd_mon_if_open(struct net_device * ndev)162*4882a593Smuzhiyun static int dhd_mon_if_open(struct net_device *ndev)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	int ret = 0;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	MON_PRINT("enter\n");
167*4882a593Smuzhiyun 	return ret;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
dhd_mon_if_stop(struct net_device * ndev)170*4882a593Smuzhiyun static int dhd_mon_if_stop(struct net_device *ndev)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	int ret = 0;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	MON_PRINT("enter\n");
175*4882a593Smuzhiyun 	return ret;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
dhd_mon_if_subif_start_xmit(struct sk_buff * skb,struct net_device * ndev)178*4882a593Smuzhiyun static int dhd_mon_if_subif_start_xmit(struct sk_buff *skb, struct net_device *ndev)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	int ret = 0;
181*4882a593Smuzhiyun 	int rtap_len;
182*4882a593Smuzhiyun 	int qos_len = 0;
183*4882a593Smuzhiyun 	int dot11_hdr_len = 24;
184*4882a593Smuzhiyun 	int snap_len = 6;
185*4882a593Smuzhiyun 	unsigned char *pdata;
186*4882a593Smuzhiyun 	unsigned short frame_ctl;
187*4882a593Smuzhiyun 	unsigned char src_mac_addr[6];
188*4882a593Smuzhiyun 	unsigned char dst_mac_addr[6];
189*4882a593Smuzhiyun 	struct ieee80211_hdr *dot11_hdr;
190*4882a593Smuzhiyun 	struct ieee80211_radiotap_header *rtap_hdr;
191*4882a593Smuzhiyun 	monitor_interface* mon_if;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	MON_PRINT("enter\n");
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	mon_if = ndev_to_monif(ndev);
196*4882a593Smuzhiyun 	if (mon_if == NULL || mon_if->real_ndev == NULL) {
197*4882a593Smuzhiyun 		MON_PRINT(" cannot find matched net dev, skip the packet\n");
198*4882a593Smuzhiyun 		goto fail;
199*4882a593Smuzhiyun 	}
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
202*4882a593Smuzhiyun 		goto fail;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	rtap_hdr = (struct ieee80211_radiotap_header *)skb->data;
205*4882a593Smuzhiyun 	if (unlikely(rtap_hdr->it_version))
206*4882a593Smuzhiyun 		goto fail;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	rtap_len = ieee80211_get_radiotap_len(skb->data);
209*4882a593Smuzhiyun 	if (unlikely(skb->len < rtap_len))
210*4882a593Smuzhiyun 		goto fail;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	MON_PRINT("radiotap len (should be 14): %d\n", rtap_len);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	/* Skip the ratio tap header */
215*4882a593Smuzhiyun 	skb_pull(skb, rtap_len);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	dot11_hdr = (struct ieee80211_hdr *)skb->data;
218*4882a593Smuzhiyun 	frame_ctl = le16_to_cpu(dot11_hdr->frame_control);
219*4882a593Smuzhiyun 	/* Check if the QoS bit is set */
220*4882a593Smuzhiyun 	if ((frame_ctl & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
221*4882a593Smuzhiyun 		/* Check if this ia a Wireless Distribution System (WDS) frame
222*4882a593Smuzhiyun 		 * which has 4 MAC addresses
223*4882a593Smuzhiyun 		 */
224*4882a593Smuzhiyun 		if (dot11_hdr->frame_control & 0x0080)
225*4882a593Smuzhiyun 			qos_len = 2;
226*4882a593Smuzhiyun 		if ((dot11_hdr->frame_control & 0x0300) == 0x0300)
227*4882a593Smuzhiyun 			dot11_hdr_len += 6;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 		memcpy(dst_mac_addr, dot11_hdr->addr1, sizeof(dst_mac_addr));
230*4882a593Smuzhiyun 		memcpy(src_mac_addr, dot11_hdr->addr2, sizeof(src_mac_addr));
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 		/* Skip the 802.11 header, QoS (if any) and SNAP, but leave spaces for
233*4882a593Smuzhiyun 		 * for two MAC addresses
234*4882a593Smuzhiyun 		 */
235*4882a593Smuzhiyun 		skb_pull(skb, dot11_hdr_len + qos_len + snap_len - sizeof(src_mac_addr) * 2);
236*4882a593Smuzhiyun 		pdata = (unsigned char*)skb->data;
237*4882a593Smuzhiyun 		memcpy(pdata, dst_mac_addr, sizeof(dst_mac_addr));
238*4882a593Smuzhiyun 		memcpy(pdata + sizeof(dst_mac_addr), src_mac_addr, sizeof(src_mac_addr));
239*4882a593Smuzhiyun 		PKTSETPRIO(skb, 0);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 		MON_PRINT("if name: %s, matched if name %s\n", ndev->name, mon_if->real_ndev->name);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 		/* Use the real net device to transmit the packet */
244*4882a593Smuzhiyun 		ret = dhd_start_xmit(skb, mon_if->real_ndev);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 		return ret;
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun fail:
249*4882a593Smuzhiyun 	dev_kfree_skb(skb);
250*4882a593Smuzhiyun 	return 0;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
dhd_mon_if_set_multicast_list(struct net_device * ndev)253*4882a593Smuzhiyun static void dhd_mon_if_set_multicast_list(struct net_device *ndev)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	monitor_interface* mon_if;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	mon_if = ndev_to_monif(ndev);
258*4882a593Smuzhiyun 	if (mon_if == NULL || mon_if->real_ndev == NULL) {
259*4882a593Smuzhiyun 		MON_PRINT(" cannot find matched net dev, skip the packet\n");
260*4882a593Smuzhiyun 	} else {
261*4882a593Smuzhiyun 		MON_PRINT("enter, if name: %s, matched if name %s\n",
262*4882a593Smuzhiyun 		ndev->name, mon_if->real_ndev->name);
263*4882a593Smuzhiyun 	}
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
dhd_mon_if_change_mac(struct net_device * ndev,void * addr)266*4882a593Smuzhiyun static int dhd_mon_if_change_mac(struct net_device *ndev, void *addr)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	int ret = 0;
269*4882a593Smuzhiyun 	monitor_interface* mon_if;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	mon_if = ndev_to_monif(ndev);
272*4882a593Smuzhiyun 	if (mon_if == NULL || mon_if->real_ndev == NULL) {
273*4882a593Smuzhiyun 		MON_PRINT(" cannot find matched net dev, skip the packet\n");
274*4882a593Smuzhiyun 	} else {
275*4882a593Smuzhiyun 		MON_PRINT("enter, if name: %s, matched if name %s\n",
276*4882a593Smuzhiyun 		ndev->name, mon_if->real_ndev->name);
277*4882a593Smuzhiyun 	}
278*4882a593Smuzhiyun 	return ret;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun /**
282*4882a593Smuzhiyun  * Global function definitions (declared in dhd_linux_mon.h)
283*4882a593Smuzhiyun  */
284*4882a593Smuzhiyun 
dhd_add_monitor(const char * name,struct net_device ** new_ndev)285*4882a593Smuzhiyun int dhd_add_monitor(const char *name, struct net_device **new_ndev)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	int i;
288*4882a593Smuzhiyun 	int idx = -1;
289*4882a593Smuzhiyun 	int ret = 0;
290*4882a593Smuzhiyun 	struct net_device* ndev = NULL;
291*4882a593Smuzhiyun 	dhd_linux_monitor_t **dhd_mon;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	mutex_lock(&g_monitor.lock);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	MON_TRACE("enter, if name: %s\n", name);
296*4882a593Smuzhiyun 	if (!name || !new_ndev) {
297*4882a593Smuzhiyun 		MON_PRINT("invalid parameters\n");
298*4882a593Smuzhiyun 		ret = -EINVAL;
299*4882a593Smuzhiyun 		goto out;
300*4882a593Smuzhiyun 	}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	/*
303*4882a593Smuzhiyun 	 * Find a vacancy
304*4882a593Smuzhiyun 	 */
305*4882a593Smuzhiyun 	for (i = 0; i < DHD_MAX_IFS; i++)
306*4882a593Smuzhiyun 		if (g_monitor.mon_if[i].mon_ndev == NULL) {
307*4882a593Smuzhiyun 			idx = i;
308*4882a593Smuzhiyun 			break;
309*4882a593Smuzhiyun 		}
310*4882a593Smuzhiyun 	if (idx == -1) {
311*4882a593Smuzhiyun 		MON_PRINT("exceeds maximum interfaces\n");
312*4882a593Smuzhiyun 		ret = -EFAULT;
313*4882a593Smuzhiyun 		goto out;
314*4882a593Smuzhiyun 	}
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	ndev = alloc_etherdev(sizeof(dhd_linux_monitor_t*));
317*4882a593Smuzhiyun 	if (!ndev) {
318*4882a593Smuzhiyun 		MON_PRINT("failed to allocate memory\n");
319*4882a593Smuzhiyun 		ret = -ENOMEM;
320*4882a593Smuzhiyun 		goto out;
321*4882a593Smuzhiyun 	}
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	ndev->type = ARPHRD_IEEE80211_RADIOTAP;
324*4882a593Smuzhiyun 	strncpy(ndev->name, name, IFNAMSIZ);
325*4882a593Smuzhiyun 	ndev->name[IFNAMSIZ - 1] = 0;
326*4882a593Smuzhiyun 	ndev->netdev_ops = &dhd_mon_if_ops;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	ret = register_netdevice(ndev);
329*4882a593Smuzhiyun 	if (ret) {
330*4882a593Smuzhiyun 		MON_PRINT(" register_netdevice failed (%d)\n", ret);
331*4882a593Smuzhiyun 		goto out;
332*4882a593Smuzhiyun 	}
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	*new_ndev = ndev;
335*4882a593Smuzhiyun 	g_monitor.mon_if[idx].radiotap_enabled = TRUE;
336*4882a593Smuzhiyun 	g_monitor.mon_if[idx].mon_ndev = ndev;
337*4882a593Smuzhiyun 	g_monitor.mon_if[idx].real_ndev = lookup_real_netdev(name);
338*4882a593Smuzhiyun 	dhd_mon = (dhd_linux_monitor_t **)netdev_priv(ndev);
339*4882a593Smuzhiyun 	*dhd_mon = &g_monitor;
340*4882a593Smuzhiyun 	g_monitor.monitor_state = MONITOR_STATE_INTERFACE_ADDED;
341*4882a593Smuzhiyun 	MON_PRINT("net device returned: 0x%p\n", ndev);
342*4882a593Smuzhiyun 	MON_PRINT("found a matched net device, name %s\n", g_monitor.mon_if[idx].real_ndev->name);
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun out:
345*4882a593Smuzhiyun 	if (ret && ndev)
346*4882a593Smuzhiyun 		free_netdev(ndev);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	mutex_unlock(&g_monitor.lock);
349*4882a593Smuzhiyun 	return ret;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
dhd_del_monitor(struct net_device * ndev)353*4882a593Smuzhiyun int dhd_del_monitor(struct net_device *ndev)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	int i;
356*4882a593Smuzhiyun 	if (!ndev)
357*4882a593Smuzhiyun 		return -EINVAL;
358*4882a593Smuzhiyun 	mutex_lock(&g_monitor.lock);
359*4882a593Smuzhiyun 	for (i = 0; i < DHD_MAX_IFS; i++) {
360*4882a593Smuzhiyun 		if (g_monitor.mon_if[i].mon_ndev == ndev ||
361*4882a593Smuzhiyun 			g_monitor.mon_if[i].real_ndev == ndev) {
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 			g_monitor.mon_if[i].real_ndev = NULL;
364*4882a593Smuzhiyun 			unregister_netdevice(g_monitor.mon_if[i].mon_ndev);
365*4882a593Smuzhiyun 			free_netdev(g_monitor.mon_if[i].mon_ndev);
366*4882a593Smuzhiyun 			g_monitor.mon_if[i].mon_ndev = NULL;
367*4882a593Smuzhiyun 			g_monitor.monitor_state = MONITOR_STATE_INTERFACE_DELETED;
368*4882a593Smuzhiyun 			break;
369*4882a593Smuzhiyun 		}
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	if (g_monitor.monitor_state != MONITOR_STATE_INTERFACE_DELETED)
373*4882a593Smuzhiyun 		MON_PRINT("IF not found in monitor array, is this a monitor IF? 0x%p\n", ndev);
374*4882a593Smuzhiyun 	mutex_unlock(&g_monitor.lock);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	return 0;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
dhd_monitor_init(void * dhd_pub)379*4882a593Smuzhiyun int dhd_monitor_init(void *dhd_pub)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun 	if (g_monitor.monitor_state == MONITOR_STATE_DEINIT) {
382*4882a593Smuzhiyun 		g_monitor.dhd_pub = dhd_pub;
383*4882a593Smuzhiyun 		mutex_init(&g_monitor.lock);
384*4882a593Smuzhiyun 		g_monitor.monitor_state = MONITOR_STATE_INIT;
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 	return 0;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun 
dhd_monitor_uninit(void)389*4882a593Smuzhiyun int dhd_monitor_uninit(void)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun 	int i;
392*4882a593Smuzhiyun 	struct net_device *ndev;
393*4882a593Smuzhiyun 	mutex_lock(&g_monitor.lock);
394*4882a593Smuzhiyun 	if (g_monitor.monitor_state != MONITOR_STATE_DEINIT) {
395*4882a593Smuzhiyun 		for (i = 0; i < DHD_MAX_IFS; i++) {
396*4882a593Smuzhiyun 			ndev = g_monitor.mon_if[i].mon_ndev;
397*4882a593Smuzhiyun 			if (ndev) {
398*4882a593Smuzhiyun 				unregister_netdevice(ndev);
399*4882a593Smuzhiyun 				free_netdev(ndev);
400*4882a593Smuzhiyun 				g_monitor.mon_if[i].real_ndev = NULL;
401*4882a593Smuzhiyun 				g_monitor.mon_if[i].mon_ndev = NULL;
402*4882a593Smuzhiyun 			}
403*4882a593Smuzhiyun 		}
404*4882a593Smuzhiyun 		g_monitor.monitor_state = MONITOR_STATE_DEINIT;
405*4882a593Smuzhiyun 	}
406*4882a593Smuzhiyun 	mutex_unlock(&g_monitor.lock);
407*4882a593Smuzhiyun 	return 0;
408*4882a593Smuzhiyun }
409