2 * Broadcom Dongle Host Driver (DHD), Linux monitor network interface
4 * Copyright (C) 1999-2016, Broadcom Corporation
6 * Unless you and Broadcom execute a separate written software license
7 * agreement governing use of this software, this software is licensed to you
8 * under the terms of the GNU General Public License version 2 (the "GPL"),
9 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10 * following added to such license:
12 * As a special exception, the copyright holders of this software give you
13 * permission to link this software with independent modules, and to copy and
14 * distribute the resulting executable under terms of your choice, provided that
15 * you also meet, for each linked independent module, the terms and conditions of
16 * the license of that module. An independent module is a module which is not
17 * derived from this software. The special exception does not apply to any
18 * modifications of the software.
20 * Notwithstanding the above, under no circumstances may you combine this
21 * software in any way with any other Broadcom software provided under a license
22 * other than the GPL, without Broadcom's express prior written consent.
25 * <<Broadcom-WL-IPTag/Open:>>
27 * $Id: wl_linux_mon.c 514727 2014-11-12 03:02:48Z $
31 #include <linux/string.h>
32 #include <linux/module.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/ieee80211.h>
37 #include <linux/rtnetlink.h>
38 #include <net/ieee80211_radiotap.h>
43 #include <dngl_stats.h>
46 typedef enum monitor_states
48 MONITOR_STATE_DEINIT
= 0x0,
49 MONITOR_STATE_INIT
= 0x1,
50 MONITOR_STATE_INTERFACE_ADDED
= 0x2,
51 MONITOR_STATE_INTERFACE_DELETED
= 0x4
53 int dhd_add_monitor(char *name
, struct net_device
**new_ndev
);
54 extern int dhd_start_xmit(struct sk_buff
*skb
, struct net_device
*net
);
55 int dhd_del_monitor(struct net_device
*ndev
);
56 int dhd_monitor_init(void *dhd_pub
);
57 int dhd_monitor_uninit(void);
60 * Local declarations and defintions (not exposed)
63 #define DHD_MAX_IFS 16
65 #define MON_PRINT(format, ...) printk("DHD-MON: %s " format, __func__, ##__VA_ARGS__)
66 #define MON_TRACE MON_PRINT
68 typedef struct monitor_interface
{
70 struct net_device
* real_ndev
; /* The real interface that the monitor is on */
71 struct net_device
* mon_ndev
;
74 typedef struct dhd_linux_monitor
{
76 monitor_states_t monitor_state
;
77 monitor_interface mon_if
[DHD_MAX_IFS
];
78 struct mutex lock
; /* lock to protect mon_if */
79 } dhd_linux_monitor_t
;
81 static dhd_linux_monitor_t g_monitor
;
83 static struct net_device
* lookup_real_netdev(char *name
);
84 static monitor_interface
* ndev_to_monif(struct net_device
*ndev
);
85 static int dhd_mon_if_open(struct net_device
*ndev
);
86 static int dhd_mon_if_stop(struct net_device
*ndev
);
87 static int dhd_mon_if_subif_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
);
88 static void dhd_mon_if_set_multicast_list(struct net_device
*ndev
);
89 static int dhd_mon_if_change_mac(struct net_device
*ndev
, void *addr
);
91 static const struct net_device_ops dhd_mon_if_ops
= {
92 .ndo_open
= dhd_mon_if_open
,
93 .ndo_stop
= dhd_mon_if_stop
,
94 .ndo_start_xmit
= dhd_mon_if_subif_start_xmit
,
95 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
96 .ndo_set_rx_mode
= dhd_mon_if_set_multicast_list
,
98 .ndo_set_multicast_list
= dhd_mon_if_set_multicast_list
,
100 .ndo_set_mac_address
= dhd_mon_if_change_mac
,
104 * Local static function defintions
107 /* Look up dhd's net device table to find a match (e.g. interface "eth0" is a match for "mon.eth0"
108 * "p2p-eth0-0" is a match for "mon.p2p-eth0-0")
110 static struct net_device
* lookup_real_netdev(char *name
)
112 struct net_device
*ndev_found
= NULL
;
116 int last_name_len
= 0;
117 struct net_device
*ndev
;
119 /* We need to find interface "p2p-p2p-0" corresponding to monitor interface "mon-p2p-0",
120 * Once mon iface name reaches IFNAMSIZ, it is reset to p2p0-0 and corresponding mon
121 * iface would be mon-p2p0-0.
123 for (i
= 0; i
< DHD_MAX_IFS
; i
++) {
124 ndev
= dhd_idx2net(g_monitor
.dhd_pub
, i
);
126 /* Skip "p2p" and look for "-p2p0-x" in monitor interface name. If it
127 * it matches, then this netdev is the corresponding real_netdev.
129 if (ndev
&& strstr(ndev
->name
, "p2p-p2p0")) {
132 /* if p2p- is not present, then the IFNAMSIZ have reached and name
133 * would have got reset. In this casse,look for p2p0-x in mon-p2p0-x
137 if (ndev
&& strstr(name
, (ndev
->name
+ len
))) {
138 if (strlen(ndev
->name
) > last_name_len
) {
140 last_name_len
= strlen(ndev
->name
);
148 static monitor_interface
* ndev_to_monif(struct net_device
*ndev
)
152 for (i
= 0; i
< DHD_MAX_IFS
; i
++) {
153 if (g_monitor
.mon_if
[i
].mon_ndev
== ndev
)
154 return &g_monitor
.mon_if
[i
];
160 static int dhd_mon_if_open(struct net_device
*ndev
)
164 MON_PRINT("enter\n");
168 static int dhd_mon_if_stop(struct net_device
*ndev
)
172 MON_PRINT("enter\n");
176 static int dhd_mon_if_subif_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
181 int dot11_hdr_len
= 24;
183 unsigned char *pdata
;
184 unsigned short frame_ctl
;
185 unsigned char src_mac_addr
[6];
186 unsigned char dst_mac_addr
[6];
187 struct ieee80211_hdr
*dot11_hdr
;
188 struct ieee80211_radiotap_header
*rtap_hdr
;
189 monitor_interface
* mon_if
;
191 MON_PRINT("enter\n");
193 mon_if
= ndev_to_monif(ndev
);
194 if (mon_if
== NULL
|| mon_if
->real_ndev
== NULL
) {
195 MON_PRINT(" cannot find matched net dev, skip the packet\n");
199 if (unlikely(skb
->len
< sizeof(struct ieee80211_radiotap_header
)))
202 rtap_hdr
= (struct ieee80211_radiotap_header
*)skb
->data
;
203 if (unlikely(rtap_hdr
->it_version
))
206 rtap_len
= ieee80211_get_radiotap_len(skb
->data
);
207 if (unlikely(skb
->len
< rtap_len
))
210 MON_PRINT("radiotap len (should be 14): %d\n", rtap_len
);
212 /* Skip the ratio tap header */
213 skb_pull(skb
, rtap_len
);
215 dot11_hdr
= (struct ieee80211_hdr
*)skb
->data
;
216 frame_ctl
= le16_to_cpu(dot11_hdr
->frame_control
);
217 /* Check if the QoS bit is set */
218 if ((frame_ctl
& IEEE80211_FCTL_FTYPE
) == IEEE80211_FTYPE_DATA
) {
219 /* Check if this ia a Wireless Distribution System (WDS) frame
220 * which has 4 MAC addresses
222 if (dot11_hdr
->frame_control
& 0x0080)
224 if ((dot11_hdr
->frame_control
& 0x0300) == 0x0300)
227 memcpy(dst_mac_addr
, dot11_hdr
->addr1
, sizeof(dst_mac_addr
));
228 memcpy(src_mac_addr
, dot11_hdr
->addr2
, sizeof(src_mac_addr
));
230 /* Skip the 802.11 header, QoS (if any) and SNAP, but leave spaces for
231 * for two MAC addresses
233 skb_pull(skb
, dot11_hdr_len
+ qos_len
+ snap_len
- sizeof(src_mac_addr
) * 2);
234 pdata
= (unsigned char*)skb
->data
;
235 memcpy(pdata
, dst_mac_addr
, sizeof(dst_mac_addr
));
236 memcpy(pdata
+ sizeof(dst_mac_addr
), src_mac_addr
, sizeof(src_mac_addr
));
239 MON_PRINT("if name: %s, matched if name %s\n", ndev
->name
, mon_if
->real_ndev
->name
);
241 /* Use the real net device to transmit the packet */
242 ret
= dhd_start_xmit(skb
, mon_if
->real_ndev
);
251 static void dhd_mon_if_set_multicast_list(struct net_device
*ndev
)
253 monitor_interface
* mon_if
;
255 mon_if
= ndev_to_monif(ndev
);
256 if (mon_if
== NULL
|| mon_if
->real_ndev
== NULL
) {
257 MON_PRINT(" cannot find matched net dev, skip the packet\n");
259 MON_PRINT("enter, if name: %s, matched if name %s\n",
260 ndev
->name
, mon_if
->real_ndev
->name
);
264 static int dhd_mon_if_change_mac(struct net_device
*ndev
, void *addr
)
267 monitor_interface
* mon_if
;
269 mon_if
= ndev_to_monif(ndev
);
270 if (mon_if
== NULL
|| mon_if
->real_ndev
== NULL
) {
271 MON_PRINT(" cannot find matched net dev, skip the packet\n");
273 MON_PRINT("enter, if name: %s, matched if name %s\n",
274 ndev
->name
, mon_if
->real_ndev
->name
);
280 * Global function definitions (declared in dhd_linux_mon.h)
283 int dhd_add_monitor(char *name
, struct net_device
**new_ndev
)
288 struct net_device
* ndev
= NULL
;
289 dhd_linux_monitor_t
**dhd_mon
;
291 mutex_lock(&g_monitor
.lock
);
293 MON_TRACE("enter, if name: %s\n", name
);
294 if (!name
|| !new_ndev
) {
295 MON_PRINT("invalid parameters\n");
303 for (i
= 0; i
< DHD_MAX_IFS
; i
++)
304 if (g_monitor
.mon_if
[i
].mon_ndev
== NULL
) {
309 MON_PRINT("exceeds maximum interfaces\n");
314 ndev
= alloc_etherdev(sizeof(dhd_linux_monitor_t
*));
316 MON_PRINT("failed to allocate memory\n");
321 ndev
->type
= ARPHRD_IEEE80211_RADIOTAP
;
322 strncpy(ndev
->name
, name
, IFNAMSIZ
);
323 ndev
->name
[IFNAMSIZ
- 1] = 0;
324 ndev
->netdev_ops
= &dhd_mon_if_ops
;
326 ret
= register_netdevice(ndev
);
328 MON_PRINT(" register_netdevice failed (%d)\n", ret
);
333 g_monitor
.mon_if
[idx
].radiotap_enabled
= TRUE
;
334 g_monitor
.mon_if
[idx
].mon_ndev
= ndev
;
335 g_monitor
.mon_if
[idx
].real_ndev
= lookup_real_netdev(name
);
336 dhd_mon
= (dhd_linux_monitor_t
**)netdev_priv(ndev
);
337 *dhd_mon
= &g_monitor
;
338 g_monitor
.monitor_state
= MONITOR_STATE_INTERFACE_ADDED
;
339 MON_PRINT("net device returned: 0x%p\n", ndev
);
340 MON_PRINT("found a matched net device, name %s\n", g_monitor
.mon_if
[idx
].real_ndev
->name
);
346 mutex_unlock(&g_monitor
.lock
);
351 int dhd_del_monitor(struct net_device
*ndev
)
356 mutex_lock(&g_monitor
.lock
);
357 for (i
= 0; i
< DHD_MAX_IFS
; i
++) {
358 if (g_monitor
.mon_if
[i
].mon_ndev
== ndev
||
359 g_monitor
.mon_if
[i
].real_ndev
== ndev
) {
361 g_monitor
.mon_if
[i
].real_ndev
= NULL
;
362 unregister_netdevice(g_monitor
.mon_if
[i
].mon_ndev
);
363 free_netdev(g_monitor
.mon_if
[i
].mon_ndev
);
364 g_monitor
.mon_if
[i
].mon_ndev
= NULL
;
365 g_monitor
.monitor_state
= MONITOR_STATE_INTERFACE_DELETED
;
370 if (g_monitor
.monitor_state
!= MONITOR_STATE_INTERFACE_DELETED
)
371 MON_PRINT("IF not found in monitor array, is this a monitor IF? 0x%p\n", ndev
);
372 mutex_unlock(&g_monitor
.lock
);
377 int dhd_monitor_init(void *dhd_pub
)
379 if (g_monitor
.monitor_state
== MONITOR_STATE_DEINIT
) {
380 g_monitor
.dhd_pub
= dhd_pub
;
381 mutex_init(&g_monitor
.lock
);
382 g_monitor
.monitor_state
= MONITOR_STATE_INIT
;
387 int dhd_monitor_uninit(void)
390 struct net_device
*ndev
;
391 mutex_lock(&g_monitor
.lock
);
392 if (g_monitor
.monitor_state
!= MONITOR_STATE_DEINIT
) {
393 for (i
= 0; i
< DHD_MAX_IFS
; i
++) {
394 ndev
= g_monitor
.mon_if
[i
].mon_ndev
;
396 unregister_netdevice(ndev
);
398 g_monitor
.mon_if
[i
].real_ndev
= NULL
;
399 g_monitor
.mon_if
[i
].mon_ndev
= NULL
;
402 g_monitor
.monitor_state
= MONITOR_STATE_DEINIT
;
404 mutex_unlock(&g_monitor
.lock
);