-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.c
310 lines (242 loc) · 6.44 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright (C) 2013:
* Simon Wunderlich <[email protected]>
* Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#include "wifi_statistics.h"
struct list_head monif_list;
void ws_monif_free_rcu(struct rcu_head *head)
{
struct ws_monif *monif;
monif = container_of(head, struct ws_monif, rcu);
dev_put(monif->net_dev);
kfree(monif);
}
struct ws_monif *ws_monif_get(const struct net_device *net_dev)
{
struct ws_monif *monif;
rcu_read_lock();
list_for_each_entry_rcu(monif, &monif_list, list) {
if (monif->net_dev == net_dev &&
atomic_inc_not_zero(&monif->refcount))
goto out;
}
monif = NULL;
out:
rcu_read_unlock();
return monif;
}
static inline void
ws_monif_free_ref(struct ws_monif *monif)
{
if (atomic_dec_and_test(&monif->refcount))
call_rcu(&monif->rcu, ws_monif_free_rcu);
}
bool ws_is_badfcs(struct ieee80211_radiotap_header *rthdr, int len)
{
struct ieee80211_radiotap_iterator iterator;
int ret;
ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, len, NULL);
while (!ret) {
ret = ieee80211_radiotap_iterator_next(&iterator);
if (ret)
continue;
switch (iterator.this_arg_index) {
case IEEE80211_RADIOTAP_FLAGS:
if (*iterator.this_arg & (IEEE80211_RADIOTAP_F_BADFCS))
return true;
else
return false;
break;
default:
break;
}
}
return false;
}
rx_handler_result_t ws_handle_frame(struct sk_buff **pskb)
{
struct ieee80211_radiotap_header *rthdr;
struct ieee80211_hdr *hdr;
struct ws_sta *ws_sta = NULL;
struct ws_monif *monif = NULL;
struct sk_buff *skb = NULL;
__le16 fc;
int len, hdrlen;
static u8 nullmac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0};
u8 *mac;
skb = skb_clone(*pskb, GFP_ATOMIC);
if (unlikely(!skb))
goto end;
monif = ws_monif_get(skb->dev);
if (!monif)
goto end;
if (!atomic_read(&monif->active))
goto end;
if (unlikely(!pskb_may_pull(skb, sizeof(*rthdr))))
goto end;
len = ieee80211_get_radiotap_len(skb->data);
rthdr = (struct ieee80211_radiotap_header *)skb->data;
hdr = (struct ieee80211_hdr *)skb_pull(skb, len);
if (unlikely(!pskb_may_pull(skb, sizeof(fc))))
goto end;
fc = hdr->frame_control;
hdrlen = ieee80211_get_hdrlen_from_skb(skb);
if (unlikely(!hdrlen))
goto end;
if (unlikely(!pskb_may_pull(skb, hdrlen)))
goto end;
if (ieee80211_is_cts(fc) || ieee80211_is_ack(fc)) {
mac = nullmac;
} else {
/* transmitter address is always addr2:
* * SA in an IBSS frame or To-AP frame
* * BSSID in an in a From-AP frame
* * TA in a 4 address frame
*/
mac = hdr->addr2;
/* if the frame has a bad FCS, try to find the station
* in the hash, assuming that the source mac is still usable.
* If it can't be found, use the packet on the null source.
*/
if (ws_is_badfcs(rthdr, len)) {
ws_sta = ws_hash_find(&monif->hash, mac);
if (ws_sta)
goto have_ws_sta;
else
mac = nullmac;
}
}
ws_sta = ws_hash_get(&monif->hash, mac);
if (!ws_sta)
goto end;
have_ws_sta:
ws_sta_general(ws_sta, skb);
ws_sta_parse_radiotap(ws_sta, rthdr, len);
ws_sta_parse_ieee80211_hdr(ws_sta, hdr, hdrlen);
ws_sta_free_ref(ws_sta);
end:
if (monif)
ws_monif_free_ref(monif);
kfree_skb(skb);
return RX_HANDLER_PASS; /* continue processing */
}
/* caller must hold rtnl lock */
int ws_monif_activate(struct ws_monif *monif)
{
int err = 0;
void *p = NULL; /* own struct? */
if (atomic_read(&monif->active))
return -EBUSY;
err = netdev_rx_handler_register(monif->net_dev, ws_handle_frame, p);
if (err)
return 0;
err = ws_hash_init(&monif->hash);
if (err)
goto free_rx_handler;
atomic_set(&monif->active, 1);
return 0;
free_rx_handler:
netdev_rx_handler_unregister(monif->net_dev);
return err;
}
/* caller must hold rtnl lock */
int ws_monif_deactivate(struct ws_monif *monif)
{
if (!atomic_read(&monif->active))
return -EBUSY;
netdev_rx_handler_unregister(monif->net_dev);
ws_hash_free(&monif->hash);
atomic_set(&monif->active, 0);
return 0;
}
int ws_monif_register(struct net_device *net_dev)
{
struct ws_monif *monif = NULL;
if (!net_dev->ieee80211_ptr)
return -EINVAL;
if (net_dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MONITOR)
return -EINVAL;
monif = kzalloc(sizeof(*monif), GFP_KERNEL);
if (!monif)
return -ENOMEM;
dev_hold(net_dev);
atomic_set(&monif->active, 0);
monif->net_dev = net_dev;
monif->ws_mode = MODE_RESET;
atomic_set(&monif->refcount, 2);
list_add_tail_rcu(&monif->list, &monif_list);
/* add to list */
ws_debugfs_monif_init(monif);
return 0;
}
void ws_monif_unregister(struct ws_monif *monif)
{
list_del_rcu(&monif->list);
ws_monif_free_ref(monif); /* for list */
/* deactivate */
ws_monif_deactivate(monif);
/* clean up */
ws_debugfs_monif_clean(monif);
ws_monif_free_ref(monif); /* for struct */
}
static int ws_if_notification(struct notifier_block *this,
unsigned long event, void *ptr)
{
struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
struct ws_monif *monif;
monif = ws_monif_get(net_dev);
switch (event) {
case NETDEV_REGISTER:
if (!monif)
ws_monif_register(net_dev);
break;
case NETDEV_UNREGISTER:
if (monif)
ws_monif_unregister(monif);
break;
default:
break;
}
if (monif)
ws_monif_free_ref(monif);
return NOTIFY_DONE;
}
struct notifier_block if_notifier = {
.notifier_call = ws_if_notification,
};
static int __init ws_init(void)
{
INIT_LIST_HEAD(&monif_list);
ws_debugfs_init();
register_netdevice_notifier(&if_notifier);
return 0;
}
static void __exit ws_exit(void)
{
unregister_netdevice_notifier(&if_notifier);
ws_debugfs_destroy();
WARN_ON(!list_empty(&monif_list));
rcu_barrier();
}
module_init(ws_init);
module_exit(ws_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR(WIFI_STATS_DRIVER_AUTHOR);
MODULE_DESCRIPTION(WIFI_STATS_DRIVER_DESC);
MODULE_SUPPORTED_DEVICE(WIFI_STATS_DRIVER_DEVICE);
MODULE_VERSION(WIFI_STATS_SOURCE_VERSION);