OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv4/] [netfilter/] [ip_queue.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * This is a module which is used for queueing IPv4 packets and
3
 * communicating with userspace via netlink.
4
 *
5
 * (C) 2000-2002 James Morris, this code is GPL.
6
 *
7
 * 2000-03-27: Simplified code (thanks to Andi Kleen for clues).
8
 * 2000-05-20: Fixed notifier problems (following Miguel Freitas' report).
9
 * 2000-06-19: Fixed so nfmark is copied to metadata (reported by Sebastian
10
 *             Zander).
11
 * 2000-08-01: Added Nick Williams' MAC support.
12
 * 2002-06-25: Code cleanup.
13
 *
14
 */
15
#include <linux/module.h>
16
#include <linux/skbuff.h>
17
#include <linux/init.h>
18
#include <linux/ip.h>
19
#include <linux/notifier.h>
20
#include <linux/netdevice.h>
21
#include <linux/netfilter.h>
22
#include <linux/netfilter_ipv4/ip_queue.h>
23
#include <linux/netfilter_ipv4/ip_tables.h>
24
#include <linux/netlink.h>
25
#include <linux/spinlock.h>
26
#include <linux/brlock.h>
27
#include <linux/sysctl.h>
28
#include <linux/proc_fs.h>
29
#include <net/sock.h>
30
#include <net/route.h>
31
 
32
#define IPQ_QMAX_DEFAULT 1024
33
#define IPQ_PROC_FS_NAME "ip_queue"
34
#define NET_IPQ_QMAX 2088
35
#define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
36
 
37
struct ipq_rt_info {
38
        __u8 tos;
39
        __u32 daddr;
40
        __u32 saddr;
41
};
42
 
43
struct ipq_queue_entry {
44
        struct list_head list;
45
        struct nf_info *info;
46
        struct sk_buff *skb;
47
        struct ipq_rt_info rt_info;
48
};
49
 
50
typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
51
 
52
static unsigned char copy_mode = IPQ_COPY_NONE;
53
static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
54
static rwlock_t queue_lock = RW_LOCK_UNLOCKED;
55
static int peer_pid;
56
static unsigned int copy_range;
57
static unsigned int queue_total;
58
static struct sock *ipqnl;
59
static LIST_HEAD(queue_list);
60
static DECLARE_MUTEX(ipqnl_sem);
61
 
62
static void
63
ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
64
{
65
        nf_reinject(entry->skb, entry->info, verdict);
66
        kfree(entry);
67
}
68
 
69
static inline int
70
__ipq_enqueue_entry(struct ipq_queue_entry *entry)
71
{
72
       if (queue_total >= queue_maxlen) {
73
               if (net_ratelimit())
74
                       printk(KERN_WARNING "ip_queue: full at %d entries, "
75
                              "dropping packet(s).\n", queue_total);
76
               return -ENOSPC;
77
       }
78
       list_add(&entry->list, &queue_list);
79
       queue_total++;
80
       return 0;
81
}
82
 
83
/*
84
 * Find and return a queued entry matched by cmpfn, or return the last
85
 * entry if cmpfn is NULL.
86
 */
87
static inline struct ipq_queue_entry *
88
__ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
89
{
90
        struct list_head *p;
91
 
92
        list_for_each_prev(p, &queue_list) {
93
                struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
94
 
95
                if (!cmpfn || cmpfn(entry, data))
96
                        return entry;
97
        }
98
        return NULL;
99
}
100
 
101
static inline void
102
__ipq_dequeue_entry(struct ipq_queue_entry *entry)
103
{
104
        list_del(&entry->list);
105
        queue_total--;
106
}
107
 
108
static inline struct ipq_queue_entry *
109
__ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
110
{
111
        struct ipq_queue_entry *entry;
112
 
113
        entry = __ipq_find_entry(cmpfn, data);
114
        if (entry == NULL)
115
                return NULL;
116
 
117
        __ipq_dequeue_entry(entry);
118
        return entry;
119
}
120
 
121
 
122
static inline void
123
__ipq_flush(int verdict)
124
{
125
        struct ipq_queue_entry *entry;
126
 
127
        while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
128
                ipq_issue_verdict(entry, verdict);
129
}
130
 
131
static inline int
132
__ipq_set_mode(unsigned char mode, unsigned int range)
133
{
134
        int status = 0;
135
 
136
        switch(mode) {
137
        case IPQ_COPY_NONE:
138
        case IPQ_COPY_META:
139
                copy_mode = mode;
140
                copy_range = 0;
141
                break;
142
 
143
        case IPQ_COPY_PACKET:
144
                copy_mode = mode;
145
                copy_range = range;
146
                if (copy_range > 0xFFFF)
147
                        copy_range = 0xFFFF;
148
                break;
149
 
150
        default:
151
                status = -EINVAL;
152
 
153
        }
154
        return status;
155
}
156
 
157
static inline void
158
__ipq_reset(void)
159
{
160
        peer_pid = 0;
161
        __ipq_set_mode(IPQ_COPY_NONE, 0);
162
        __ipq_flush(NF_DROP);
163
}
164
 
165
static struct ipq_queue_entry *
166
ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
167
{
168
        struct ipq_queue_entry *entry;
169
 
170
        write_lock_bh(&queue_lock);
171
        entry = __ipq_find_dequeue_entry(cmpfn, data);
172
        write_unlock_bh(&queue_lock);
173
        return entry;
174
}
175
 
176
static void
177
ipq_flush(int verdict)
178
{
179
        write_lock_bh(&queue_lock);
180
        __ipq_flush(verdict);
181
        write_unlock_bh(&queue_lock);
182
}
183
 
184
static struct sk_buff *
185
ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
186
{
187
        unsigned char *old_tail;
188
        size_t size = 0;
189
        size_t data_len = 0;
190
        struct sk_buff *skb;
191
        struct ipq_packet_msg *pmsg;
192
        struct nlmsghdr *nlh;
193
 
194
        read_lock_bh(&queue_lock);
195
 
196
        switch (copy_mode) {
197
        case IPQ_COPY_META:
198
        case IPQ_COPY_NONE:
199
                size = NLMSG_SPACE(sizeof(*pmsg));
200
                data_len = 0;
201
                break;
202
 
203
        case IPQ_COPY_PACKET:
204
                if (copy_range == 0 || copy_range > entry->skb->len)
205
                        data_len = entry->skb->len;
206
                else
207
                        data_len = copy_range;
208
 
209
                size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
210
                break;
211
 
212
        default:
213
                *errp = -EINVAL;
214
                read_unlock_bh(&queue_lock);
215
                return NULL;
216
        }
217
 
218
        read_unlock_bh(&queue_lock);
219
 
220
        skb = alloc_skb(size, GFP_ATOMIC);
221
        if (!skb)
222
                goto nlmsg_failure;
223
 
224
        old_tail= skb->tail;
225
        nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
226
        pmsg = NLMSG_DATA(nlh);
227
        memset(pmsg, 0, sizeof(*pmsg));
228
 
229
        pmsg->packet_id       = (unsigned long )entry;
230
        pmsg->data_len        = data_len;
231
        pmsg->timestamp_sec   = entry->skb->stamp.tv_sec;
232
        pmsg->timestamp_usec  = entry->skb->stamp.tv_usec;
233
        pmsg->mark            = entry->skb->nfmark;
234
        pmsg->hook            = entry->info->hook;
235
        pmsg->hw_protocol     = entry->skb->protocol;
236
 
237
        if (entry->info->indev)
238
                strcpy(pmsg->indev_name, entry->info->indev->name);
239
        else
240
                pmsg->indev_name[0] = '\0';
241
 
242
        if (entry->info->outdev)
243
                strcpy(pmsg->outdev_name, entry->info->outdev->name);
244
        else
245
                pmsg->outdev_name[0] = '\0';
246
 
247
        if (entry->info->indev && entry->skb->dev) {
248
                pmsg->hw_type = entry->skb->dev->type;
249
                if (entry->skb->dev->hard_header_parse)
250
                        pmsg->hw_addrlen =
251
                                entry->skb->dev->hard_header_parse(entry->skb,
252
                                                                   pmsg->hw_addr);
253
        }
254
 
255
        if (data_len)
256
                memcpy(pmsg->payload, entry->skb->data, data_len);
257
 
258
        nlh->nlmsg_len = skb->tail - old_tail;
259
        return skb;
260
 
261
nlmsg_failure:
262
        if (skb)
263
                kfree_skb(skb);
264
        *errp = -EINVAL;
265
        printk(KERN_ERR "ip_queue: error creating packet message\n");
266
        return NULL;
267
}
268
 
269
static int
270
ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
271
{
272
        int status = -EINVAL;
273
        struct sk_buff *nskb;
274
        struct ipq_queue_entry *entry;
275
 
276
        if (copy_mode == IPQ_COPY_NONE)
277
                return -EAGAIN;
278
 
279
        entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
280
        if (entry == NULL) {
281
                printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
282
                return -ENOMEM;
283
        }
284
 
285
        entry->info = info;
286
        entry->skb = skb;
287
 
288
        if (entry->info->hook == NF_IP_LOCAL_OUT) {
289
                struct iphdr *iph = skb->nh.iph;
290
 
291
                entry->rt_info.tos = iph->tos;
292
                entry->rt_info.daddr = iph->daddr;
293
                entry->rt_info.saddr = iph->saddr;
294
        }
295
 
296
        nskb = ipq_build_packet_message(entry, &status);
297
        if (nskb == NULL)
298
                goto err_out_free;
299
 
300
        write_lock_bh(&queue_lock);
301
 
302
        if (!peer_pid)
303
                goto err_out_free_nskb;
304
 
305
        /* netlink_unicast will either free the nskb or attach it to a socket */
306
        status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
307
        if (status < 0)
308
                goto err_out_unlock;
309
 
310
        status = __ipq_enqueue_entry(entry);
311
        if (status < 0)
312
                goto err_out_unlock;
313
 
314
        write_unlock_bh(&queue_lock);
315
        return status;
316
 
317
err_out_free_nskb:
318
        kfree_skb(nskb);
319
 
320
err_out_unlock:
321
        write_unlock_bh(&queue_lock);
322
 
323
err_out_free:
324
        kfree(entry);
325
        return status;
326
}
327
 
328
static int
329
ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
330
{
331
        int diff;
332
        struct iphdr *user_iph = (struct iphdr *)v->payload;
333
 
334
        if (v->data_len < sizeof(*user_iph))
335
                return 0;
336
        diff = v->data_len - e->skb->len;
337
        if (diff < 0)
338
                skb_trim(e->skb, v->data_len);
339
        else if (diff > 0) {
340
                if (v->data_len > 0xFFFF)
341
                        return -EINVAL;
342
                if (diff > skb_tailroom(e->skb)) {
343
                        struct sk_buff *newskb;
344
 
345
                        newskb = skb_copy_expand(e->skb,
346
                                                 skb_headroom(e->skb),
347
                                                 diff,
348
                                                 GFP_ATOMIC);
349
                        if (newskb == NULL) {
350
                                printk(KERN_WARNING "ip_queue: OOM "
351
                                      "in mangle, dropping packet\n");
352
                                return -ENOMEM;
353
                        }
354
                        if (e->skb->sk)
355
                                skb_set_owner_w(newskb, e->skb->sk);
356
                        kfree_skb(e->skb);
357
                        e->skb = newskb;
358
                }
359
                skb_put(e->skb, diff);
360
        }
361
        memcpy(e->skb->data, v->payload, v->data_len);
362
        e->skb->nfcache |= NFC_ALTERED;
363
 
364
        /*
365
         * Extra routing may needed on local out, as the QUEUE target never
366
         * returns control to the table.
367
         */
368
        if (e->info->hook == NF_IP_LOCAL_OUT) {
369
                struct iphdr *iph = e->skb->nh.iph;
370
 
371
                if (!(iph->tos == e->rt_info.tos
372
                      && iph->daddr == e->rt_info.daddr
373
                      && iph->saddr == e->rt_info.saddr))
374
                        return ip_route_me_harder(&e->skb);
375
        }
376
        return 0;
377
}
378
 
379
static inline int
380
id_cmp(struct ipq_queue_entry *e, unsigned long id)
381
{
382
        return (id == (unsigned long )e);
383
}
384
 
385
static int
386
ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
387
{
388
        struct ipq_queue_entry *entry;
389
 
390
        if (vmsg->value > NF_MAX_VERDICT)
391
                return -EINVAL;
392
 
393
        entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
394
        if (entry == NULL)
395
                return -ENOENT;
396
        else {
397
                int verdict = vmsg->value;
398
 
399
                if (vmsg->data_len && vmsg->data_len == len)
400
                        if (ipq_mangle_ipv4(vmsg, entry) < 0)
401
                                verdict = NF_DROP;
402
 
403
                ipq_issue_verdict(entry, verdict);
404
                return 0;
405
        }
406
}
407
 
408
static int
409
ipq_set_mode(unsigned char mode, unsigned int range)
410
{
411
        int status;
412
 
413
        write_lock_bh(&queue_lock);
414
        status = __ipq_set_mode(mode, range);
415
        write_unlock_bh(&queue_lock);
416
        return status;
417
}
418
 
419
static int
420
ipq_receive_peer(struct ipq_peer_msg *pmsg,
421
                 unsigned char type, unsigned int len)
422
{
423
        int status = 0;
424
 
425
        if (len < sizeof(*pmsg))
426
                return -EINVAL;
427
 
428
        switch (type) {
429
        case IPQM_MODE:
430
                status = ipq_set_mode(pmsg->msg.mode.value,
431
                                      pmsg->msg.mode.range);
432
                break;
433
 
434
        case IPQM_VERDICT:
435
                if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
436
                        status = -EINVAL;
437
                else
438
                        status = ipq_set_verdict(&pmsg->msg.verdict,
439
                                                 len - sizeof(*pmsg));
440
                        break;
441
        default:
442
                status = -EINVAL;
443
        }
444
        return status;
445
}
446
 
447
static int
448
dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
449
{
450
        if (entry->info->indev)
451
                if (entry->info->indev->ifindex == ifindex)
452
                        return 1;
453
 
454
        if (entry->info->outdev)
455
                if (entry->info->outdev->ifindex == ifindex)
456
                        return 1;
457
 
458
        return 0;
459
}
460
 
461
static void
462
ipq_dev_drop(int ifindex)
463
{
464
        struct ipq_queue_entry *entry;
465
 
466
        while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
467
                ipq_issue_verdict(entry, NF_DROP);
468
}
469
 
470
#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
471
 
472
static inline void
473
ipq_rcv_skb(struct sk_buff *skb)
474
{
475
        int status, type, pid, flags, nlmsglen, skblen;
476
        struct nlmsghdr *nlh;
477
 
478
        skblen = skb->len;
479
        if (skblen < sizeof(*nlh))
480
                return;
481
 
482
        nlh = (struct nlmsghdr *)skb->data;
483
        nlmsglen = nlh->nlmsg_len;
484
        if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
485
                return;
486
 
487
        pid = nlh->nlmsg_pid;
488
        flags = nlh->nlmsg_flags;
489
 
490
        if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
491
                RCV_SKB_FAIL(-EINVAL);
492
 
493
        if (flags & MSG_TRUNC)
494
                RCV_SKB_FAIL(-ECOMM);
495
 
496
        type = nlh->nlmsg_type;
497
        if (type < NLMSG_NOOP || type >= IPQM_MAX)
498
                RCV_SKB_FAIL(-EINVAL);
499
 
500
        if (type <= IPQM_BASE)
501
                return;
502
 
503
        if(!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
504
                RCV_SKB_FAIL(-EPERM);
505
 
506
        write_lock_bh(&queue_lock);
507
 
508
        if (peer_pid) {
509
                if (peer_pid != pid) {
510
                        write_unlock_bh(&queue_lock);
511
                        RCV_SKB_FAIL(-EBUSY);
512
                }
513
        }
514
        else
515
                peer_pid = pid;
516
 
517
        write_unlock_bh(&queue_lock);
518
 
519
        status = ipq_receive_peer(NLMSG_DATA(nlh), type,
520
                                  skblen - NLMSG_LENGTH(0));
521
        if (status < 0)
522
                RCV_SKB_FAIL(status);
523
 
524
        if (flags & NLM_F_ACK)
525
                netlink_ack(skb, nlh, 0);
526
        return;
527
}
528
 
529
static void
530
ipq_rcv_sk(struct sock *sk, int len)
531
{
532
        do {
533
                struct sk_buff *skb;
534
 
535
                if (down_trylock(&ipqnl_sem))
536
                        return;
537
 
538
                while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
539
                        ipq_rcv_skb(skb);
540
                        kfree_skb(skb);
541
                }
542
 
543
                up(&ipqnl_sem);
544
 
545
        } while (ipqnl && ipqnl->receive_queue.qlen);
546
}
547
 
548
static int
549
ipq_rcv_dev_event(struct notifier_block *this,
550
                  unsigned long event, void *ptr)
551
{
552
        struct net_device *dev = ptr;
553
 
554
        /* Drop any packets associated with the downed device */
555
        if (event == NETDEV_DOWN)
556
                ipq_dev_drop(dev->ifindex);
557
        return NOTIFY_DONE;
558
}
559
 
560
static struct notifier_block ipq_dev_notifier = {
561
        ipq_rcv_dev_event,
562
        NULL,
563
 
564
};
565
 
566
static int
567
ipq_rcv_nl_event(struct notifier_block *this,
568
                 unsigned long event, void *ptr)
569
{
570
        struct netlink_notify *n = ptr;
571
 
572
        if (event == NETLINK_URELEASE &&
573
            n->protocol == NETLINK_FIREWALL && n->pid) {
574
                write_lock_bh(&queue_lock);
575
                if (n->pid == peer_pid)
576
                        __ipq_reset();
577
                write_unlock_bh(&queue_lock);
578
        }
579
        return NOTIFY_DONE;
580
}
581
 
582
static struct notifier_block ipq_nl_notifier = {
583
        ipq_rcv_nl_event,
584
        NULL,
585
 
586
};
587
 
588
static struct ctl_table_header *ipq_sysctl_header;
589
 
590
static ctl_table ipq_table[] = {
591
        { NET_IPQ_QMAX, NET_IPQ_QMAX_NAME, &queue_maxlen,
592
          sizeof(queue_maxlen), 0644,  NULL, proc_dointvec },
593
        { 0 }
594
};
595
 
596
static ctl_table ipq_dir_table[] = {
597
        {NET_IPV4, "ipv4", NULL, 0, 0555, ipq_table, 0, 0, 0, 0, 0},
598
        { 0 }
599
};
600
 
601
static ctl_table ipq_root_table[] = {
602
        {CTL_NET, "net", NULL, 0, 0555, ipq_dir_table, 0, 0, 0, 0, 0},
603
        { 0 }
604
};
605
 
606
static int
607
ipq_get_info(char *buffer, char **start, off_t offset, int length)
608
{
609
        int len;
610
 
611
        read_lock_bh(&queue_lock);
612
 
613
        len = sprintf(buffer,
614
                      "Peer PID          : %d\n"
615
                      "Copy mode         : %hu\n"
616
                      "Copy range        : %u\n"
617
                      "Queue length      : %u\n"
618
                      "Queue max. length : %u\n",
619
                      peer_pid,
620
                      copy_mode,
621
                      copy_range,
622
                      queue_total,
623
                      queue_maxlen);
624
 
625
        read_unlock_bh(&queue_lock);
626
 
627
        *start = buffer + offset;
628
        len -= offset;
629
        if (len > length)
630
                len = length;
631
        else if (len < 0)
632
                len = 0;
633
        return len;
634
}
635
 
636
static int
637
init_or_cleanup(int init)
638
{
639
        int status = -ENOMEM;
640
        struct proc_dir_entry *proc;
641
 
642
        if (!init)
643
                goto cleanup;
644
 
645
        netlink_register_notifier(&ipq_nl_notifier);
646
        ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk);
647
        if (ipqnl == NULL) {
648
                printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
649
                goto cleanup_netlink_notifier;
650
        }
651
 
652
        proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
653
        if (proc)
654
                proc->owner = THIS_MODULE;
655
        else {
656
                printk(KERN_ERR "ip_queue: failed to create proc entry\n");
657
                goto cleanup_ipqnl;
658
        }
659
 
660
        register_netdevice_notifier(&ipq_dev_notifier);
661
        ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
662
 
663
        status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL);
664
        if (status < 0) {
665
                printk(KERN_ERR "ip_queue: failed to register queue handler\n");
666
                goto cleanup_sysctl;
667
        }
668
        return status;
669
 
670
cleanup:
671
        nf_unregister_queue_handler(PF_INET);
672
        br_write_lock_bh(BR_NETPROTO_LOCK);
673
        br_write_unlock_bh(BR_NETPROTO_LOCK);
674
        ipq_flush(NF_DROP);
675
 
676
cleanup_sysctl:
677
        unregister_sysctl_table(ipq_sysctl_header);
678
        unregister_netdevice_notifier(&ipq_dev_notifier);
679
        proc_net_remove(IPQ_PROC_FS_NAME);
680
 
681
cleanup_ipqnl:
682
        sock_release(ipqnl->socket);
683
        down(&ipqnl_sem);
684
        up(&ipqnl_sem);
685
 
686
cleanup_netlink_notifier:
687
        netlink_unregister_notifier(&ipq_nl_notifier);
688
        return status;
689
}
690
 
691
static int __init init(void)
692
{
693
 
694
        return init_or_cleanup(1);
695
}
696
 
697
static void __exit fini(void)
698
{
699
        init_or_cleanup(0);
700
}
701
 
702
MODULE_DESCRIPTION("IPv4 packet queue handler");
703
MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
704
MODULE_LICENSE("GPL");
705
 
706
module_init(init);
707
module_exit(fini);

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.