-
Notifications
You must be signed in to change notification settings - Fork 3
/
hook.c
546 lines (451 loc) · 11.3 KB
/
hook.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#include <linux/etherdevice.h>
#include <linux/fs.h>
#include <linux/ip.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/tcp.h>
#include <linux/version.h>
#include <linux/wait.h>
#include <asm/uaccess.h>
#include "hook.h"
#define NH_MINOR 214
static LIST_HEAD(current_skbs);
static DEFINE_SPINLOCK(skb_list_lock);
struct skb_entry {
struct list_head list;
struct sk_buff *skb;
};
#if defined(__DEBUG__)
static unsigned int list_size(struct list_head *head)
{
unsigned int count = 0;
struct list_head *pos;
list_for_each(pos, head)
count++;
return count;
}
#endif
/*
* Must hold skb_list_lock
*/
static struct skb_entry *is_hooked(struct sk_buff *skb)
{
struct skb_entry *e;
int found = 0;
list_for_each_entry(e, ¤t_skbs, list) {
if(e->skb == skb) {
found = 1;
break;
}
}
return found ? e : NULL;
}
static LIST_HEAD(nh_privs);
static DEFINE_SPINLOCK(nh_privs_lock);
struct nh_private {
struct list_head list;
struct nh_filter *filter;
struct nh_writer *writer;
struct sk_buff_head skb_queue ;
wait_queue_head_t wq;
};
static struct nf_hook_ops *cb_in_use[NF_IP_NUMHOOKS + 1];
enum {
CHECK_PROTO = (1 << 0),
CHECK_OUT = (1 << 1),
CHECK_IN = (1 << 2),
CHECK_SADDR = (1 << 3),
CHECK_DADDR = (1 << 4),
CHECK_SPORT = (1 << 5),
CHECK_DPORT = (1 << 6),
};
/*
* Must hold the nh_privs_lock
*/
static struct nh_private *pass(struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int hooknum)
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,22)
struct iphdr *iph = (struct iphdr *)skb_network_header(skb);
struct tcphdr *tph = (struct tcphdr *)skb_transport_header(skb);
#else
struct iphdr *iph = skb->nh.iph;
struct tcphdr *tph = skb->h.th;
#endif
struct nh_private *e;
list_for_each_entry(e, &nh_privs, list) {
if (e->filter->hooknum != hooknum) {
continue;
}
if ((e->filter->flags & CHECK_OUT) && e->filter->out != out) {
continue;
}
if ((e->filter->flags & CHECK_IN) && e->filter->in != in) {
continue;
}
if (iph) {
if ((e->filter->flags & CHECK_PROTO) && e->filter->proto != iph->protocol) {
continue;
}
if ((e->filter->flags & CHECK_SADDR) && e->filter->saddr != iph->saddr) {
continue;
}
if ((e->filter->flags & CHECK_DADDR) && e->filter->daddr != iph->daddr) {
continue;
}
}
if (tph) {
if ((e->filter->flags & CHECK_SPORT) && e->filter->sport != tph->source) {
continue;
}
if ((e->filter->flags & CHECK_DPORT) && e->filter->dport != tph->dest) {
continue;
}
}
return e;
}
return NULL;
}
static unsigned int nf_cb(
unsigned int hooknum,
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
struct sk_buff *skb,
#else
struct sk_buff **pskb,
#endif
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct nh_private *p;
struct skb_entry *e;
unsigned long flags;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
struct sk_buff **pskb = &skb;
#endif
/* if the packet was hooked once, don't send it back to user space */
spin_lock_irqsave(&skb_list_lock, flags);
e = is_hooked(*pskb);
if (e) {
list_del(&e->list);
spin_unlock_irqrestore(&skb_list_lock, flags);
kfree(e);
return NF_ACCEPT;
}
spin_unlock_irqrestore(&skb_list_lock, flags);
spin_lock_irqsave(&nh_privs_lock, flags);
p = pass(*pskb, in, out, hooknum);
if (p) {
skb_queue_tail(&p->skb_queue, *pskb);
spin_unlock_irqrestore(&nh_privs_lock, flags);
wake_up_interruptible(&p->wq);
return NF_STOLEN;
}
spin_unlock_irqrestore(&nh_privs_lock, flags);
return NF_ACCEPT;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
#define NET_NAMESPACE
#else
#define NET_NAMESPACE &init_net,
#endif
static int setup_filter(struct nh_private *p)
{
struct nh_filter *f = p->filter;
struct nf_hook_ops *nf_hook;
int ret = 0;
if (f->hooknum < 0 || f->hooknum > (NF_IP_NUMHOOKS - 1))
return -EINVAL;
nf_hook = kzalloc(sizeof(*nf_hook), GFP_KERNEL);
if (!nf_hook)
return -ENOMEM;
f->in = dev_get_by_name(NET_NAMESPACE f->in_dev);
if (f->in)
f->flags |= CHECK_IN;
f->out = dev_get_by_name(NET_NAMESPACE f->out_dev);
if (f->out)
f->flags |= CHECK_OUT;
if (f->saddr)
f->flags |= CHECK_SADDR;
if (f->daddr)
f->flags |= CHECK_DADDR;
if (f->dport)
f->flags |= CHECK_DPORT;
if (f->sport)
f->flags |= CHECK_SPORT;
if (f->proto)
f->flags |= CHECK_PROTO;
if (!cb_in_use[f->hooknum]) {
nf_hook->hook = nf_cb;
nf_hook->owner = THIS_MODULE;
nf_hook->pf = PF_INET;
nf_hook->hooknum = f->hooknum;
nf_hook->priority = f->priority;
ret = nf_register_hook(nf_hook);
if (ret < 0) {
printk("nf_hook: can't register netfilter hook.\n");
kfree(nf_hook);
goto err;
}
cb_in_use[f->hooknum] = nf_hook;
}
return 0;
err:
kfree(nf_hook);
return ret;
}
static int nh_open(struct inode *inode, struct file *file)
{
struct nh_private *p;
p = kzalloc(sizeof(*p), GFP_KERNEL);
if (!p)
return -ENOMEM;
init_waitqueue_head(&p->wq);
skb_queue_head_init(&p->skb_queue);
file->private_data = p;
return 0;
}
static int nh_release(struct inode *inode, struct file *file)
{
struct nh_private *p = file->private_data;
unsigned long flags;
spin_lock_irqsave(&nh_privs_lock, flags);
if (p->filter) {
list_del(&p->list);
if (p->filter->in)
dev_put(p->filter->in);
if (p->filter->out)
dev_put(p->filter->out);
kfree(p->filter);
}
while (!skb_queue_empty(&p->skb_queue)) {
struct sk_buff *skb;
skb = skb_dequeue(&p->skb_queue);
kfree_skb(skb);
}
if (p->writer) {
if (p->writer->dest_dev)
dev_put(p->writer->dest_dev);
kfree(p->writer);
}
spin_unlock_irqrestore(&nh_privs_lock, flags);
kfree(p);
return 0;
}
static ssize_t nh_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
struct nh_private *p;
struct sk_buff *skb;
int ret;
unsigned long flags;
p = file->private_data;
if (!p->writer) {
return -EBADF;
}
skb = dev_alloc_skb(count + 2);
if (!skb)
return -ENOMEM;
skb_reserve(skb, 2);
if (copy_from_user(skb_put(skb, count), buf, count)) {
kfree_skb(skb);
printk("nh_write: failed copy_from_user %d\n", count);
return -EFAULT;
}
if (p->writer->mode == TO_ROUTING_STACK) {
struct skb_entry *e;
e = kmalloc(sizeof(*e), GFP_ATOMIC);
if (!e) {
kfree_skb(skb);
return -ENOMEM;
}
e->skb = skb;
/* mark this skb as 'ours' */
spin_lock_irqsave(&skb_list_lock, flags);
list_add(&e->list, ¤t_skbs);
spin_unlock_irqrestore(&skb_list_lock, flags);
skb->dev = p->writer->dest_dev; /* needed for <= 2.6.18 */
skb->protocol = eth_type_trans(skb, p->writer->dest_dev);
ret = netif_rx_ni(skb);
} else {
struct net_device *dev;
skb->dev = p->writer->dest_dev;
skb->protocol = be16_to_cpu(0x0800);
skb_pull(skb, sizeof(struct ethhdr));
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,22)
skb_reset_network_header(skb);
#endif
skb->protocol = __constant_htons(ETH_P_IP);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
if (skb->dev->hard_header)
skb->dev->hard_header(skb, skb->dev, be16_to_cpu(skb->protocol), NULL, skb->dev->dev_addr, skb->len);
#else
dev_hard_header(skb, skb->dev, be16_to_cpu(skb->protocol), NULL, skb->dev->dev_addr, skb->len);
#endif
switch (p->writer->mode) {
case TO_INTERFACE:
dev = skb->dev;
spin_lock_irqsave(&dev->_xmit_lock, flags);
dev->xmit_lock_owner = smp_processor_id();
if (!netif_queue_stopped(skb->dev)
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
&& !netif_subqueue_stopped(skb->dev, skb)
#endif
) {
ret = skb->dev->hard_start_xmit(skb, skb->dev);
}
dev->xmit_lock_owner = -1;
spin_unlock_irqrestore(&dev->_xmit_lock, flags);
break;
case TO_INTERFACE_QUEUE:
ret = dev_queue_xmit(skb);
break;
default:
WARN_ON_ONCE(1);
}
}
return count;
}
static ssize_t nh_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
struct nh_private *p;
struct sk_buff *skb;
int ret;
unsigned long flags;
if (!count)
return count;
p = file->private_data;
if (!p->filter) {
return -EBADF;
}
wait_skb:
wait_event_interruptible(p->wq, !(skb_queue_empty(&p->skb_queue)));
if (signal_pending(current))
return -ERESTARTSYS;
spin_lock_irqsave(&nh_privs_lock, flags);
skb = skb_dequeue(&p->skb_queue);
spin_unlock_irqrestore(&nh_privs_lock, flags);
if (!skb)
goto wait_skb;
/* Save the dest mac now, it will be lost otherwise */
if (skb->dst && skb->dst->neighbour) {
struct ethhdr *eth;
skb_push(skb, sizeof(struct ethhdr));
eth = (struct ethhdr *)skb->data;
skb_pull(skb, sizeof(struct ethhdr));
memcpy(eth->h_dest, skb->dst->neighbour->ha, ETH_ALEN);
}
skb_push(skb, ETH_HLEN);
if (skb->len > count) {
kfree_skb(skb);
return -EINVAL;
}
ret = skb->len;
if (copy_to_user(buf, skb->data, skb->len)) {
printk("failed copy_to_user %d\n", skb->len);
kfree_skb(skb);
return -EFAULT;
}
kfree_skb(skb);
return ret;
}
static int nh_ioctl(struct inode *inode, struct file *file,
unsigned int req, unsigned long pointer)
{
struct nh_private *p;
struct nh_filter *filter;
struct nh_writer *writer;
int ret;
unsigned long flags;
p = file->private_data;
switch (req) {
case NH_SET_FILTER:
filter = kzalloc(sizeof(*filter), GFP_KERNEL);
if (!filter)
return -ENOMEM;
if (copy_from_user(filter, (void __user*)pointer, sizeof(*filter))) {
return -EFAULT;
}
p->filter = filter;
ret = setup_filter(p);
#if 0
printk("Got filter:\n\
u8 proto = %d\n\
u32 saddr = %ld\n\
u32 daddr = %ld\n\
u16 dport = %d\n\
u16 sport = %d\n\
char in_dev[255] = %s\n\
char out_dev[255] = %s\n\
int priority = %d\n\
int hooknum = %d\n\
int flags = %d\n", filter->proto, filter->saddr, filter->daddr, filter->dport, filter->sport,
filter->in_dev, filter->out_dev, filter->priority, filter->hooknum, filter->flags);
#endif
if (!ret) {
spin_lock_irqsave(&nh_privs_lock, flags);
list_add(&p->list, &nh_privs);
spin_unlock_irqrestore(&nh_privs_lock, flags);
} else {
kfree(p->filter);
}
return ret;
case NH_RM_FILTER:
if (p->filter) {
spin_lock_irqsave(&nh_privs_lock, flags);
list_del(&p->list);
spin_unlock_irqrestore(&nh_privs_lock, flags);
kfree(p->filter);
}
return 0;
case NH_SET_WRITE_MODE:
writer = kzalloc(sizeof(*writer), GFP_KERNEL);
if (!writer)
return -ENOMEM;
if (copy_from_user(writer, (void __user *)pointer, sizeof(*writer)))
return -EFAULT;
p->writer = writer;
p->writer->dest_dev = dev_get_by_name(NET_NAMESPACE writer->dest_dev_str);
return 0;
}
return -EINVAL;
}
static const struct file_operations net_hook_fops = {
.owner = THIS_MODULE,
.open = nh_open,
.release = nh_release,
.ioctl = nh_ioctl,
.read = nh_read,
.write = nh_write,
};
static struct miscdevice net_hook_dev = {
NH_MINOR,
"net_hook",
&net_hook_fops
};
static int __init nh_init(void)
{
int ret;
ret = misc_register(&net_hook_dev);
if (ret)
printk(KERN_ERR "net_hook: can't misc_register on minor %d\n", NH_MINOR);
printk("hk: module loaded\n");
return ret;
}
module_init(nh_init);
static void __exit nh_exit(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(cb_in_use); i++) {
if (cb_in_use[i]) {
nf_unregister_hook(cb_in_use[i]);
kfree(cb_in_use[i]);
}
}
misc_deregister(&net_hook_dev);
printk("hk: module unloaded\n");
}
module_exit(nh_exit);