1 /*
2 * Broadcom Dongle Host Driver (DHD), Linux monitor network interface
3 *
4 * Copyright (C) 1999-2019, Broadcom.
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
16 * of the license of that module. An independent module is a module which is
17 * not 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 *
25 * <<Broadcom-WL-IPTag/Open:>>
26 *
27 * $Id: wl_linux_mon.c 576195 2015-08-01 18:21:54Z $
28 */
29
30 #include <osl.h>
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>
39
40 #include <wlioctl.h>
41 #include <bcmutils.h>
42 #include <dhd_dbg.h>
43 #include <dngl_stats.h>
44 #include <dhd.h>
45
46 typedef enum monitor_states {
47 MONITOR_STATE_DEINIT = 0x0,
48 MONITOR_STATE_INIT = 0x1,
49 MONITOR_STATE_INTERFACE_ADDED = 0x2,
50 MONITOR_STATE_INTERFACE_DELETED = 0x4
51 } monitor_states_t;
52 int dhd_add_monitor(const char *name, struct net_device **new_ndev);
53 int magiclink_add_p2p(const char *name, struct ether_addr *p2p_dev_addr,
54 struct net_device **new_ndev);
55 extern netdev_tx_t dhd_start_xmit(struct sk_buff *skb, struct net_device *net);
56 int dhd_del_monitor(struct net_device *ndev);
57 int dhd_monitor_init(void *dhd_pub);
58 int dhd_monitor_uninit(void);
59
60 /**
61 * Local declarations and defintions (not exposed)
62 */
63 #ifndef DHD_MAX_IFS
64 #define DHD_MAX_IFS 16
65 #endif // endif
66 #define MON_PRINT(format, ...) \
67 printk("DHD-MON: %s " format, __func__, ##__VA_ARGS__)
68 #define MON_TRACE MON_PRINT
69
70 typedef struct monitor_interface {
71 int radiotap_enabled;
72 struct net_device
73 *real_ndev; /* The real interface that the monitor is on */
74 struct net_device *mon_ndev;
75 } monitor_interface;
76
77 typedef struct dhd_linux_monitor {
78 void *dhd_pub;
79 monitor_states_t monitor_state;
80 monitor_interface mon_if[DHD_MAX_IFS];
81 struct mutex lock; /* lock to protect mon_if */
82 } dhd_linux_monitor_t;
83
84 static dhd_linux_monitor_t g_monitor;
85
86 static struct net_device *lookup_real_netdev(const char *name);
87 static monitor_interface *ndev_to_monif(struct net_device *ndev);
88 static int dhd_mon_if_open(struct net_device *ndev);
89 static int dhd_mon_if_stop(struct net_device *ndev);
90 static netdev_tx_t dhd_mon_if_subif_start_xmit(struct sk_buff *skb,
91 struct net_device *ndev);
92 static void dhd_mon_if_set_multicast_list(struct net_device *ndev);
93 static int dhd_mon_if_change_mac(struct net_device *ndev, void *addr);
94
95 static const struct net_device_ops dhd_mon_if_ops = {
96 .ndo_open = dhd_mon_if_open,
97 .ndo_stop = dhd_mon_if_stop,
98 .ndo_start_xmit = dhd_mon_if_subif_start_xmit,
99 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
100 .ndo_set_rx_mode = dhd_mon_if_set_multicast_list,
101 #else
102 .ndo_set_multicast_list = dhd_mon_if_set_multicast_list,
103 #endif // endif
104 .ndo_set_mac_address = dhd_mon_if_change_mac,
105 };
106
107 /**
108 * Local static function defintions
109 */
110
111 /* Look up dhd's net device table to find a match (e.g. interface "eth0" is a
112 * match for "mon.eth0" "p2p-eth0-0" is a match for "mon.p2p-eth0-0")
113 */
lookup_real_netdev(const char * name)114 static struct net_device *lookup_real_netdev(const char *name)
115 {
116 struct net_device *ndev_found = NULL;
117
118 int i;
119 int len = 0;
120 int last_name_len = 0;
121 struct net_device *ndev;
122
123 /* We need to find interface "p2p-p2p-0" corresponding to monitor interface
124 * "mon-p2p-0", Once mon iface name reaches IFNAMSIZ, it is reset to p2p0-0
125 * and corresponding mon iface would be mon-p2p0-0.
126 */
127 for (i = 0; i < DHD_MAX_IFS; i++) {
128 ndev = dhd_idx2net(g_monitor.dhd_pub, i);
129 /* Skip "p2p" and look for "-p2p0-x" in monitor interface name. If it
130 * it matches, then this netdev is the corresponding real_netdev.
131 */
132 if (ndev && strstr(ndev->name, "p2p-p2p0")) {
133 len = strlen("p2p");
134 } else {
135 /* if p2p- is not present, then the IFNAMSIZ have reached and name
136 * would have got reset. In this casse,look for p2p0-x in mon-p2p0-x
137 */
138 len = 0;
139 }
140 if (ndev && strstr(name, (ndev->name + len))) {
141 if (strlen(ndev->name) > last_name_len) {
142 ndev_found = ndev;
143 last_name_len = strlen(ndev->name);
144 }
145 }
146 }
147
148 return ndev_found;
149 }
150
ndev_to_monif(struct net_device * ndev)151 static monitor_interface *ndev_to_monif(struct net_device *ndev)
152 {
153 int i;
154
155 for (i = 0; i < DHD_MAX_IFS; i++) {
156 if (g_monitor.mon_if[i].mon_ndev == ndev) {
157 return &g_monitor.mon_if[i];
158 }
159 }
160
161 return NULL;
162 }
163
dhd_mon_if_open(struct net_device * ndev)164 static int dhd_mon_if_open(struct net_device *ndev)
165 {
166 int ret = 0;
167
168 MON_PRINT("enter\n");
169 return ret;
170 }
171
dhd_mon_if_stop(struct net_device * ndev)172 static int dhd_mon_if_stop(struct net_device *ndev)
173 {
174 int ret = 0;
175
176 MON_PRINT("enter\n");
177 return ret;
178 }
179
dhd_mon_if_subif_start_xmit(struct sk_buff * skb,struct net_device * ndev)180 static netdev_tx_t dhd_mon_if_subif_start_xmit(struct sk_buff *skb,
181 struct net_device *ndev)
182 {
183 int ret = 0;
184 int rtap_len;
185 int qos_len = 0;
186 int dot11_hdr_len = 24;
187 int snap_len = 6;
188 unsigned char *pdata;
189 unsigned short frame_ctl;
190 unsigned char src_mac_addr[6];
191 unsigned char dst_mac_addr[6];
192 struct ieee80211_hdr *dot11_hdr;
193 struct ieee80211_radiotap_header *rtap_hdr;
194 monitor_interface *mon_if;
195
196 MON_PRINT("enter\n");
197
198 mon_if = ndev_to_monif(ndev);
199 if (mon_if == NULL || mon_if->real_ndev == NULL) {
200 MON_PRINT(" cannot find matched net dev, skip the packet\n");
201 goto fail;
202 }
203
204 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header))) {
205 goto fail;
206 }
207
208 rtap_hdr = (struct ieee80211_radiotap_header *)skb->data;
209 if (unlikely(rtap_hdr->it_version)) {
210 goto fail;
211 }
212
213 rtap_len = ieee80211_get_radiotap_len(skb->data);
214 if (unlikely(skb->len < rtap_len)) {
215 goto fail;
216 }
217
218 MON_PRINT("radiotap len (should be 14): %d\n", rtap_len);
219
220 /* Skip the ratio tap header */
221 skb_pull(skb, rtap_len);
222
223 dot11_hdr = (struct ieee80211_hdr *)skb->data;
224 frame_ctl = le16_to_cpu(dot11_hdr->frame_control);
225 /* Check if the QoS bit is set */
226 if ((frame_ctl & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
227 /* Check if this ia a Wireless Distribution System (WDS) frame
228 * which has 4 MAC addresses
229 */
230 if (dot11_hdr->frame_control & 0x0080) {
231 qos_len = 0x2;
232 }
233 if ((dot11_hdr->frame_control & 0x0300) == 0x0300) {
234 dot11_hdr_len += 0x6;
235 }
236
237 memcpy(dst_mac_addr, dot11_hdr->addr1, sizeof(dst_mac_addr));
238 memcpy(src_mac_addr, dot11_hdr->addr2, sizeof(src_mac_addr));
239
240 /* Skip the 802.11 header, QoS (if any) and SNAP, but leave spaces for
241 * for two MAC addresses
242 */
243 skb_pull(skb,
244 dot11_hdr_len + qos_len + snap_len - sizeof(src_mac_addr) * 0x2);
245 pdata = (unsigned char *)skb->data;
246 memcpy(pdata, dst_mac_addr, sizeof(dst_mac_addr));
247 memcpy(pdata + sizeof(dst_mac_addr), src_mac_addr,
248 sizeof(src_mac_addr));
249 PKTSETPRIO(skb, 0);
250
251 MON_PRINT("if name: %s, matched if name %s\n", ndev->name,
252 mon_if->real_ndev->name);
253
254 /* Use the real net device to transmit the packet */
255 ret = dhd_start_xmit(skb, mon_if->real_ndev);
256
257 return ret;
258 }
259 fail:
260 dev_kfree_skb(skb);
261 return 0;
262 }
263
dhd_mon_if_set_multicast_list(struct net_device * ndev)264 static void dhd_mon_if_set_multicast_list(struct net_device *ndev)
265 {
266 monitor_interface *mon_if;
267
268 mon_if = ndev_to_monif(ndev);
269 if (mon_if == NULL || mon_if->real_ndev == NULL) {
270 MON_PRINT(" cannot find matched net dev, skip the packet\n");
271 } else {
272 MON_PRINT("enter, if name: %s, matched if name %s\n", ndev->name,
273 mon_if->real_ndev->name);
274 }
275 }
276
dhd_mon_if_change_mac(struct net_device * ndev,void * addr)277 static int dhd_mon_if_change_mac(struct net_device *ndev, void *addr)
278 {
279 int ret = 0;
280 monitor_interface *mon_if;
281
282 mon_if = ndev_to_monif(ndev);
283 if (mon_if == NULL || mon_if->real_ndev == NULL) {
284 MON_PRINT(" cannot find matched net dev, skip the packet\n");
285 } else {
286 MON_PRINT("enter, if name: %s, matched if name %s\n", ndev->name,
287 mon_if->real_ndev->name);
288 }
289 return ret;
290 }
291
292 /**
293 * Global function definitions (declared in dhd_linux_mon.h)
294 */
295
dhd_add_monitor(const char * name,struct net_device ** new_ndev)296 int dhd_add_monitor(const char *name, struct net_device **new_ndev)
297 {
298 int i;
299 int idx = -1;
300 int ret = 0;
301 struct net_device *ndev = NULL;
302 dhd_linux_monitor_t **dhd_mon;
303
304 mutex_lock(&g_monitor.lock);
305
306 MON_TRACE("enter, if name: %s\n", name);
307 if (!name || !new_ndev) {
308 MON_PRINT("invalid parameters\n");
309 ret = -EINVAL;
310 goto out;
311 }
312
313 /*
314 * Find a vacancy
315 */
316 for (i = 0; i < DHD_MAX_IFS; i++) {
317 if (g_monitor.mon_if[i].mon_ndev == NULL) {
318 idx = i;
319 break;
320 }
321 }
322 if (idx == -1) {
323 MON_PRINT("exceeds maximum interfaces\n");
324 ret = -EFAULT;
325 goto out;
326 }
327
328 ndev = alloc_etherdev(sizeof(dhd_linux_monitor_t *));
329 if (!ndev) {
330 MON_PRINT("failed to allocate memory\n");
331 ret = -ENOMEM;
332 goto out;
333 }
334
335 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
336 strncpy(ndev->name, name, IFNAMSIZ);
337 ndev->name[IFNAMSIZ - 1] = 0;
338 ndev->netdev_ops = &dhd_mon_if_ops;
339
340 ret = register_netdevice(ndev);
341 if (ret) {
342 MON_PRINT(" register_netdevice failed (%d)\n", ret);
343 goto out;
344 }
345
346 *new_ndev = ndev;
347 g_monitor.mon_if[idx].radiotap_enabled = TRUE;
348 g_monitor.mon_if[idx].mon_ndev = ndev;
349 g_monitor.mon_if[idx].real_ndev = lookup_real_netdev(name);
350 dhd_mon = (dhd_linux_monitor_t **)netdev_priv(ndev);
351 *dhd_mon = &g_monitor;
352 g_monitor.monitor_state = MONITOR_STATE_INTERFACE_ADDED;
353 MON_PRINT("net device returned: 0x%p\n", ndev);
354 MON_PRINT("found a matched net device, name %s\n",
355 g_monitor.mon_if[idx].real_ndev->name);
356
357 out:
358 if (ret && ndev) {
359 free_netdev(ndev);
360 }
361
362 mutex_unlock(&g_monitor.lock);
363 return ret;
364 }
365
magiclink_add_p2p(const char * name,struct ether_addr * p2p_dev_addr,struct net_device ** new_ndev)366 int magiclink_add_p2p(const char *name, struct ether_addr *p2p_dev_addr,
367 struct net_device **new_ndev)
368 {
369 int i;
370 int idx = -1;
371 int ret = 0;
372 struct net_device *ndev = NULL;
373 dhd_linux_monitor_t **dhd_mon;
374 mutex_lock(&g_monitor.lock);
375
376 MON_TRACE("enter, if name: %s\n", name);
377 if (!name || !new_ndev) {
378 MON_PRINT("invalid parameters\n");
379 ret = -EINVAL;
380 goto out;
381 }
382
383 /*
384 * Find a vacancy
385 */
386 for (i = 0; i < DHD_MAX_IFS; i++) {
387 if (g_monitor.mon_if[i].mon_ndev == NULL) {
388 idx = i;
389 break;
390 }
391 }
392 if (idx == -1) {
393 MON_PRINT("exceeds maximum interfaces\n");
394 ret = -EFAULT;
395 goto out;
396 }
397
398 ndev = alloc_etherdev(sizeof(struct net_device));
399 if (!ndev) {
400 MON_PRINT("failed to allocate memory\n");
401 ret = -ENOMEM;
402 goto out;
403 }
404
405 strncpy(ndev->name, name, IFNAMSIZ);
406 ndev->name[IFNAMSIZ - 1] = 0;
407 ndev->netdev_ops = &dhd_mon_if_ops;
408
409 memcpy(ndev->dev_addr, p2p_dev_addr, sizeof(struct ether_addr));
410 ret = register_netdevice(ndev);
411 if (ret) {
412 MON_PRINT(" register_netdevice failed (%d)\n", ret);
413 goto out;
414 }
415
416 *new_ndev = ndev;
417 g_monitor.mon_if[idx].radiotap_enabled = TRUE;
418 g_monitor.mon_if[idx].mon_ndev = ndev;
419 g_monitor.mon_if[idx].real_ndev = lookup_real_netdev(name);
420 dhd_mon = (dhd_linux_monitor_t **)netdev_priv(ndev);
421 *dhd_mon = &g_monitor;
422 g_monitor.monitor_state = MONITOR_STATE_INTERFACE_ADDED;
423 MON_PRINT("net device returned: 0x%p\n", ndev);
424 MON_PRINT("found a matched net device, name %s\n",
425 g_monitor.mon_if[idx].real_ndev->name);
426 out:
427 if (ret && ndev) {
428 free_netdev(ndev);
429 }
430
431 mutex_unlock(&g_monitor.lock);
432 return ret;
433 }
434
dhd_del_monitor(struct net_device * ndev)435 int dhd_del_monitor(struct net_device *ndev)
436 {
437 int i;
438 if (!ndev) {
439 return -EINVAL;
440 }
441 mutex_lock(&g_monitor.lock);
442 for (i = 0; i < DHD_MAX_IFS; i++) {
443 if (g_monitor.mon_if[i].mon_ndev == ndev ||
444 g_monitor.mon_if[i].real_ndev == ndev) {
445 g_monitor.mon_if[i].real_ndev = NULL;
446 unregister_netdevice(g_monitor.mon_if[i].mon_ndev);
447 free_netdev(g_monitor.mon_if[i].mon_ndev);
448 g_monitor.mon_if[i].mon_ndev = NULL;
449 g_monitor.monitor_state = MONITOR_STATE_INTERFACE_DELETED;
450 break;
451 }
452 }
453
454 if (g_monitor.monitor_state != MONITOR_STATE_INTERFACE_DELETED) {
455 MON_PRINT("IF not found in monitor array, is this a monitor IF? 0x%p\n",
456 ndev);
457 }
458 mutex_unlock(&g_monitor.lock);
459
460 return 0;
461 }
462
dhd_monitor_init(void * dhd_pub)463 int dhd_monitor_init(void *dhd_pub)
464 {
465 if (g_monitor.monitor_state == MONITOR_STATE_DEINIT) {
466 g_monitor.dhd_pub = dhd_pub;
467 mutex_init(&g_monitor.lock);
468 g_monitor.monitor_state = MONITOR_STATE_INIT;
469 }
470 return 0;
471 }
472
dhd_monitor_uninit(void)473 int dhd_monitor_uninit(void)
474 {
475 int i;
476 struct net_device *ndev;
477 if (g_monitor.monitor_state != MONITOR_STATE_DEINIT) {
478 mutex_lock(&g_monitor.lock);
479 for (i = 0; i < DHD_MAX_IFS; i++) {
480 ndev = g_monitor.mon_if[i].mon_ndev;
481 if (ndev) {
482 unregister_netdevice(ndev);
483 free_netdev(ndev);
484 g_monitor.mon_if[i].real_ndev = NULL;
485 g_monitor.mon_if[i].mon_ndev = NULL;
486 }
487 }
488 g_monitor.monitor_state = MONITOR_STATE_DEINIT;
489 mutex_unlock(&g_monitor.lock);
490 }
491 return 0;
492 }
493