aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/bcmdhd/dhd_linux_mon.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/wireless/bcmdhd/dhd_linux_mon.c')
-rw-r--r--drivers/net/wireless/bcmdhd/dhd_linux_mon.c393
1 files changed, 393 insertions, 0 deletions
diff --git a/drivers/net/wireless/bcmdhd/dhd_linux_mon.c b/drivers/net/wireless/bcmdhd/dhd_linux_mon.c
new file mode 100644
index 00000000000..dd9c71f75be
--- /dev/null
+++ b/drivers/net/wireless/bcmdhd/dhd_linux_mon.c
@@ -0,0 +1,393 @@
1/*
2 * Broadcom Dongle Host Driver (DHD), Linux monitor network interface
3 *
4 * Copyright (C) 1999-2011, Broadcom Corporation
5 *
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:
11 *
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.
19 *
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.
23 *
24 * $Id: dhd_linux_mon.c,v 1.131.2.55 2011-02-09 05:31:56 Exp $
25 */
26
27#include <linux/string.h>
28#include <linux/module.h>
29#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/if_arp.h>
32#include <linux/ieee80211.h>
33#include <linux/rtnetlink.h>
34#include <net/ieee80211_radiotap.h>
35
36#include <wlioctl.h>
37#include <bcmutils.h>
38#include <linux_osl.h>
39#include <dhd_dbg.h>
40#include <dngl_stats.h>
41#include <dhd.h>
42
43typedef enum monitor_states
44{
45 MONITOR_STATE_DEINIT = 0x0,
46 MONITOR_STATE_INIT = 0x1,
47 MONITOR_STATE_INTERFACE_ADDED = 0x2,
48 MONITOR_STATE_INTERFACE_DELETED = 0x4
49} monitor_states_t;
50extern int dhd_start_xmit(struct sk_buff *skb, struct net_device *net);
51
52/**
53 * Local declarations and defintions (not exposed)
54 */
55#define MON_PRINT(format, ...) printk("DHD-MON: %s " format, __func__, ##__VA_ARGS__)
56#define MON_TRACE MON_PRINT
57
58typedef struct monitor_interface {
59 int radiotap_enabled;
60 struct net_device* real_ndev; /* The real interface that the monitor is on */
61 struct net_device* mon_ndev;
62} monitor_interface;
63
64typedef struct dhd_linux_monitor {
65 void *dhd_pub;
66 monitor_states_t monitor_state;
67 monitor_interface mon_if[DHD_MAX_IFS];
68 struct mutex lock; /* lock to protect mon_if */
69} dhd_linux_monitor_t;
70
71static dhd_linux_monitor_t g_monitor;
72
73static struct net_device* lookup_real_netdev(char *name);
74static monitor_interface* ndev_to_monif(struct net_device *ndev);
75static int dhd_mon_if_open(struct net_device *ndev);
76static int dhd_mon_if_stop(struct net_device *ndev);
77static int dhd_mon_if_subif_start_xmit(struct sk_buff *skb, struct net_device *ndev);
78static void dhd_mon_if_set_multicast_list(struct net_device *ndev);
79static int dhd_mon_if_change_mac(struct net_device *ndev, void *addr);
80
81static const struct net_device_ops dhd_mon_if_ops = {
82 .ndo_open = dhd_mon_if_open,
83 .ndo_stop = dhd_mon_if_stop,
84 .ndo_start_xmit = dhd_mon_if_subif_start_xmit,
85 .ndo_set_multicast_list = dhd_mon_if_set_multicast_list,
86 .ndo_set_mac_address = dhd_mon_if_change_mac,
87};
88
89/**
90 * Local static function defintions
91 */
92
93/* Look up dhd's net device table to find a match (e.g. interface "eth0" is a match for "mon.eth0"
94 * "p2p-eth0-0" is a match for "mon.p2p-eth0-0")
95 */
96static struct net_device* lookup_real_netdev(char *name)
97{
98 int i;
99 int last_name_len = 0;
100 struct net_device *ndev;
101 struct net_device *ndev_found = NULL;
102
103 /* We want to find interface "p2p-eth0-0" for monitor interface "mon.p2p-eth0-0", so
104 * we skip "eth0" even if "mon.p2p-eth0-0" contains "eth0"
105 */
106 for (i = 0; i < DHD_MAX_IFS; i++) {
107 ndev = dhd_idx2net(g_monitor.dhd_pub, i);
108 if (ndev && strstr(name, ndev->name)) {
109 if (strlen(ndev->name) > last_name_len) {
110 ndev_found = ndev;
111 last_name_len = strlen(ndev->name);
112 }
113 }
114 }
115
116 return ndev_found;
117}
118
119static monitor_interface* ndev_to_monif(struct net_device *ndev)
120{
121 int i;
122
123 for (i = 0; i < DHD_MAX_IFS; i++) {
124 if (g_monitor.mon_if[i].mon_ndev == ndev)
125 return &g_monitor.mon_if[i];
126 }
127
128 return NULL;
129}
130
131static int dhd_mon_if_open(struct net_device *ndev)
132{
133 int ret = 0;
134
135 MON_PRINT("enter\n");
136 return ret;
137}
138
139static int dhd_mon_if_stop(struct net_device *ndev)
140{
141 int ret = 0;
142
143 MON_PRINT("enter\n");
144 return ret;
145}
146
147static int dhd_mon_if_subif_start_xmit(struct sk_buff *skb, struct net_device *ndev)
148{
149 int ret = 0;
150 int rtap_len;
151 int qos_len = 0;
152 int dot11_hdr_len = 24;
153 int snap_len = 6;
154 unsigned char *pdata;
155 unsigned short frame_ctl;
156 unsigned char src_mac_addr[6];
157 unsigned char dst_mac_addr[6];
158 struct ieee80211_hdr *dot11_hdr;
159 struct ieee80211_radiotap_header *rtap_hdr;
160 monitor_interface* mon_if;
161
162 MON_PRINT("enter\n");
163
164 mon_if = ndev_to_monif(ndev);
165 if (mon_if == NULL || mon_if->real_ndev == NULL) {
166 MON_PRINT(" cannot find matched net dev, skip the packet\n");
167 goto fail;
168 }
169
170 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
171 goto fail;
172
173 rtap_hdr = (struct ieee80211_radiotap_header *)skb->data;
174 if (unlikely(rtap_hdr->it_version))
175 goto fail;
176
177 rtap_len = ieee80211_get_radiotap_len(skb->data);
178 if (unlikely(skb->len < rtap_len))
179 goto fail;
180
181 MON_PRINT("radiotap len (should be 14): %d\n", rtap_len);
182
183 /* Skip the ratio tap header */
184 skb_pull(skb, rtap_len);
185
186 dot11_hdr = (struct ieee80211_hdr *)skb->data;
187 frame_ctl = le16_to_cpu(dot11_hdr->frame_control);
188 /* Check if the QoS bit is set */
189 if ((frame_ctl & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
190 /* Check if this ia a Wireless Distribution System (WDS) frame
191 * which has 4 MAC addresses
192 */
193 if (dot11_hdr->frame_control & 0x0080)
194 qos_len = 2;
195 if ((dot11_hdr->frame_control & 0x0300) == 0x0300)
196 dot11_hdr_len += 6;
197
198 memcpy(dst_mac_addr, dot11_hdr->addr1, sizeof(dst_mac_addr));
199 memcpy(src_mac_addr, dot11_hdr->addr2, sizeof(src_mac_addr));
200
201 /* Skip the 802.11 header, QoS (if any) and SNAP, but leave spaces for
202 * for two MAC addresses
203 */
204 skb_pull(skb, dot11_hdr_len + qos_len + snap_len - sizeof(src_mac_addr) * 2);
205 pdata = (unsigned char*)skb->data;
206 memcpy(pdata, dst_mac_addr, sizeof(dst_mac_addr));
207 memcpy(pdata + sizeof(dst_mac_addr), src_mac_addr, sizeof(src_mac_addr));
208
209 MON_PRINT("if name: %s, matched if name %s\n", ndev->name, mon_if->real_ndev->name);
210
211 /* Use the real net device to transmit the packet */
212 ret = dhd_start_xmit(skb, mon_if->real_ndev);
213
214 return ret;
215 }
216fail:
217 dev_kfree_skb(skb);
218 return 0;
219}
220
221static void dhd_mon_if_set_multicast_list(struct net_device *ndev)
222{
223 monitor_interface* mon_if;
224
225 mon_if = ndev_to_monif(ndev);
226 if (mon_if == NULL || mon_if->real_ndev == NULL) {
227 MON_PRINT(" cannot find matched net dev, skip the packet\n");
228 }
229
230 MON_PRINT("enter, if name: %s, matched if name %s\n", ndev->name, mon_if->real_ndev->name);
231}
232
233static int dhd_mon_if_change_mac(struct net_device *ndev, void *addr)
234{
235 int ret = 0;
236 monitor_interface* mon_if;
237
238 mon_if = ndev_to_monif(ndev);
239 if (mon_if == NULL || mon_if->real_ndev == NULL) {
240 MON_PRINT(" cannot find matched net dev, skip the packet\n");
241 }
242
243 MON_PRINT("enter, if name: %s, matched if name %s\n", ndev->name, mon_if->real_ndev->name);
244 return ret;
245}
246
247/**
248 * Global function definitions (declared in dhd_linux_mon.h)
249 */
250
251int dhd_add_monitor(char *name, struct net_device **new_ndev)
252{
253 int i;
254 int idx = -1;
255 int ret = 0;
256 struct net_device* ndev = NULL;
257 dhd_linux_monitor_t **dhd_mon;
258
259 mutex_lock(&g_monitor.lock);
260
261 MON_TRACE("enter, if name: %s\n", name);
262 if (!name || !new_ndev) {
263 MON_PRINT("invalid parameters\n");
264 ret = -EINVAL;
265 goto out;
266 }
267
268 /*
269 * Find a vacancy
270 */
271 for (i = 0; i < DHD_MAX_IFS; i++)
272 if (g_monitor.mon_if[i].mon_ndev == NULL) {
273 idx = i;
274 break;
275 }
276 if (idx == -1) {
277 MON_PRINT("exceeds maximum interfaces\n");
278 ret = -EFAULT;
279 goto out;
280 }
281
282 ndev = alloc_etherdev(sizeof(dhd_linux_monitor_t*));
283 if (!ndev) {
284 MON_PRINT("failed to allocate memory\n");
285 ret = -ENOMEM;
286 goto out;
287 }
288
289 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
290 strncpy(ndev->name, name, IFNAMSIZ);
291 ndev->name[IFNAMSIZ - 1] = 0;
292 ndev->netdev_ops = &dhd_mon_if_ops;
293
294 ret = register_netdevice(ndev);
295 if (ret) {
296 MON_PRINT(" register_netdevice failed (%d)\n", ret);
297 goto out;
298 }
299
300 *new_ndev = ndev;
301 g_monitor.mon_if[idx].radiotap_enabled = TRUE;
302 g_monitor.mon_if[idx].mon_ndev = ndev;
303 g_monitor.mon_if[idx].real_ndev = lookup_real_netdev(name);
304 dhd_mon = (dhd_linux_monitor_t **)netdev_priv(ndev);
305 *dhd_mon = &g_monitor;
306 g_monitor.monitor_state = MONITOR_STATE_INTERFACE_ADDED;
307 MON_PRINT("net device returned: 0x%p\n", ndev);
308 MON_PRINT("found a matched net device, name %s\n", g_monitor.mon_if[idx].real_ndev->name);
309
310out:
311 if (ret && ndev)
312 free_netdev(ndev);
313
314 mutex_unlock(&g_monitor.lock);
315 return ret;
316
317}
318
319int dhd_del_monitor(struct net_device *ndev)
320{
321 int i;
322 bool rollback_lock = false;
323 if (!ndev)
324 return -EINVAL;
325 mutex_lock(&g_monitor.lock);
326 for (i = 0; i < DHD_MAX_IFS; i++) {
327 if (g_monitor.mon_if[i].mon_ndev == ndev ||
328 g_monitor.mon_if[i].real_ndev == ndev) {
329 g_monitor.mon_if[i].real_ndev = NULL;
330 if (rtnl_is_locked()) {
331 rtnl_unlock();
332 rollback_lock = true;
333 }
334 unregister_netdev(g_monitor.mon_if[i].mon_ndev);
335 free_netdev(g_monitor.mon_if[i].mon_ndev);
336 g_monitor.mon_if[i].mon_ndev = NULL;
337 g_monitor.monitor_state = MONITOR_STATE_INTERFACE_DELETED;
338 break;
339 }
340 }
341 if (rollback_lock) {
342 rtnl_lock();
343 rollback_lock = false;
344 }
345
346 if (g_monitor.monitor_state !=
347 MONITOR_STATE_INTERFACE_DELETED)
348 MON_PRINT("interface not found in monitor IF array, is this a monitor IF? 0x%p\n",
349 ndev);
350 mutex_unlock(&g_monitor.lock);
351
352 return 0;
353}
354
355int dhd_monitor_init(void *dhd_pub)
356{
357 if (g_monitor.monitor_state == MONITOR_STATE_DEINIT) {
358 g_monitor.dhd_pub = dhd_pub;
359 mutex_init(&g_monitor.lock);
360 g_monitor.monitor_state = MONITOR_STATE_INIT;
361 }
362 return 0;
363}
364
365int dhd_monitor_uninit(void)
366{
367 int i;
368 struct net_device *ndev;
369 bool rollback_lock = false;
370 mutex_lock(&g_monitor.lock);
371 if (g_monitor.monitor_state != MONITOR_STATE_DEINIT) {
372 for (i = 0; i < DHD_MAX_IFS; i++) {
373 ndev = g_monitor.mon_if[i].mon_ndev;
374 if (ndev) {
375 if (rtnl_is_locked()) {
376 rtnl_unlock();
377 rollback_lock = true;
378 }
379 unregister_netdev(ndev);
380 free_netdev(ndev);
381 g_monitor.mon_if[i].real_ndev = NULL;
382 g_monitor.mon_if[i].mon_ndev = NULL;
383 if (rollback_lock) {
384 rtnl_lock();
385 rollback_lock = false;
386 }
387 }
388 }
389 g_monitor.monitor_state = MONITOR_STATE_DEINIT;
390 }
391 mutex_unlock(&g_monitor.lock);
392 return 0;
393}