]> err.no Git - linux-2.6/blob - net/ipv4/netfilter/ip_queue.c
[NETFILTER]: nf_queue: move list_head/skb/id to struct nf_info
[linux-2.6] / net / ipv4 / netfilter / ip_queue.c
1 /*
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 <jmorris@intercode.com.au>
6  * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
15 #include <linux/ip.h>
16 #include <linux/notifier.h>
17 #include <linux/netdevice.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4/ip_queue.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/netlink.h>
22 #include <linux/spinlock.h>
23 #include <linux/sysctl.h>
24 #include <linux/proc_fs.h>
25 #include <linux/seq_file.h>
26 #include <linux/security.h>
27 #include <linux/mutex.h>
28 #include <net/net_namespace.h>
29 #include <net/sock.h>
30 #include <net/route.h>
31 #include <net/netfilter/nf_queue.h>
32
33 #define IPQ_QMAX_DEFAULT 1024
34 #define IPQ_PROC_FS_NAME "ip_queue"
35 #define NET_IPQ_QMAX 2088
36 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
37
38 typedef int (*ipq_cmpfn)(struct nf_queue_entry *, unsigned long);
39
40 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
41 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
42 static DEFINE_RWLOCK(queue_lock);
43 static int peer_pid __read_mostly;
44 static unsigned int copy_range __read_mostly;
45 static unsigned int queue_total;
46 static unsigned int queue_dropped = 0;
47 static unsigned int queue_user_dropped = 0;
48 static struct sock *ipqnl __read_mostly;
49 static LIST_HEAD(queue_list);
50 static DEFINE_MUTEX(ipqnl_mutex);
51
52 static void
53 ipq_issue_verdict(struct nf_queue_entry *entry, int verdict)
54 {
55         /* TCP input path (and probably other bits) assume to be called
56          * from softirq context, not from syscall, like ipq_issue_verdict is
57          * called.  TCP input path deadlocks with locks taken from timer
58          * softirq, e.g.  We therefore emulate this by local_bh_disable() */
59
60         local_bh_disable();
61         nf_reinject(entry, verdict);
62         local_bh_enable();
63 }
64
65 static inline void
66 __ipq_enqueue_entry(struct nf_queue_entry *entry)
67 {
68        list_add_tail(&entry->list, &queue_list);
69        queue_total++;
70 }
71
72 static inline int
73 __ipq_set_mode(unsigned char mode, unsigned int range)
74 {
75         int status = 0;
76
77         switch(mode) {
78         case IPQ_COPY_NONE:
79         case IPQ_COPY_META:
80                 copy_mode = mode;
81                 copy_range = 0;
82                 break;
83
84         case IPQ_COPY_PACKET:
85                 copy_mode = mode;
86                 copy_range = range;
87                 if (copy_range > 0xFFFF)
88                         copy_range = 0xFFFF;
89                 break;
90
91         default:
92                 status = -EINVAL;
93
94         }
95         return status;
96 }
97
98 static void __ipq_flush(ipq_cmpfn cmpfn, unsigned long data);
99
100 static inline void
101 __ipq_reset(void)
102 {
103         peer_pid = 0;
104         net_disable_timestamp();
105         __ipq_set_mode(IPQ_COPY_NONE, 0);
106         __ipq_flush(NULL, 0);
107 }
108
109 static struct nf_queue_entry *
110 ipq_find_dequeue_entry(unsigned long id)
111 {
112         struct nf_queue_entry *entry = NULL, *i;
113
114         write_lock_bh(&queue_lock);
115
116         list_for_each_entry(i, &queue_list, list) {
117                 if ((unsigned long)i == id) {
118                         entry = i;
119                         break;
120                 }
121         }
122
123         if (entry) {
124                 list_del(&entry->list);
125                 queue_total--;
126         }
127
128         write_unlock_bh(&queue_lock);
129         return entry;
130 }
131
132 static void
133 __ipq_flush(ipq_cmpfn cmpfn, unsigned long data)
134 {
135         struct nf_queue_entry *entry, *next;
136
137         list_for_each_entry_safe(entry, next, &queue_list, list) {
138                 if (!cmpfn || cmpfn(entry, data)) {
139                         list_del(&entry->list);
140                         queue_total--;
141                         ipq_issue_verdict(entry, NF_DROP);
142                 }
143         }
144 }
145
146 static void
147 ipq_flush(ipq_cmpfn cmpfn, unsigned long data)
148 {
149         write_lock_bh(&queue_lock);
150         __ipq_flush(cmpfn, data);
151         write_unlock_bh(&queue_lock);
152 }
153
154 static struct sk_buff *
155 ipq_build_packet_message(struct nf_queue_entry *entry, int *errp)
156 {
157         sk_buff_data_t old_tail;
158         size_t size = 0;
159         size_t data_len = 0;
160         struct sk_buff *skb;
161         struct ipq_packet_msg *pmsg;
162         struct nlmsghdr *nlh;
163         struct timeval tv;
164
165         read_lock_bh(&queue_lock);
166
167         switch (copy_mode) {
168         case IPQ_COPY_META:
169         case IPQ_COPY_NONE:
170                 size = NLMSG_SPACE(sizeof(*pmsg));
171                 data_len = 0;
172                 break;
173
174         case IPQ_COPY_PACKET:
175                 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
176                      entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
177                     (*errp = skb_checksum_help(entry->skb))) {
178                         read_unlock_bh(&queue_lock);
179                         return NULL;
180                 }
181                 if (copy_range == 0 || copy_range > entry->skb->len)
182                         data_len = entry->skb->len;
183                 else
184                         data_len = copy_range;
185
186                 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
187                 break;
188
189         default:
190                 *errp = -EINVAL;
191                 read_unlock_bh(&queue_lock);
192                 return NULL;
193         }
194
195         read_unlock_bh(&queue_lock);
196
197         skb = alloc_skb(size, GFP_ATOMIC);
198         if (!skb)
199                 goto nlmsg_failure;
200
201         old_tail = skb->tail;
202         nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
203         pmsg = NLMSG_DATA(nlh);
204         memset(pmsg, 0, sizeof(*pmsg));
205
206         pmsg->packet_id       = (unsigned long )entry;
207         pmsg->data_len        = data_len;
208         tv = ktime_to_timeval(entry->skb->tstamp);
209         pmsg->timestamp_sec   = tv.tv_sec;
210         pmsg->timestamp_usec  = tv.tv_usec;
211         pmsg->mark            = entry->skb->mark;
212         pmsg->hook            = entry->hook;
213         pmsg->hw_protocol     = entry->skb->protocol;
214
215         if (entry->indev)
216                 strcpy(pmsg->indev_name, entry->indev->name);
217         else
218                 pmsg->indev_name[0] = '\0';
219
220         if (entry->outdev)
221                 strcpy(pmsg->outdev_name, entry->outdev->name);
222         else
223                 pmsg->outdev_name[0] = '\0';
224
225         if (entry->indev && entry->skb->dev) {
226                 pmsg->hw_type = entry->skb->dev->type;
227                 pmsg->hw_addrlen = dev_parse_header(entry->skb,
228                                                     pmsg->hw_addr);
229         }
230
231         if (data_len)
232                 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
233                         BUG();
234
235         nlh->nlmsg_len = skb->tail - old_tail;
236         return skb;
237
238 nlmsg_failure:
239         if (skb)
240                 kfree_skb(skb);
241         *errp = -EINVAL;
242         printk(KERN_ERR "ip_queue: error creating packet message\n");
243         return NULL;
244 }
245
246 static int
247 ipq_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
248 {
249         int status = -EINVAL;
250         struct sk_buff *nskb;
251
252         if (copy_mode == IPQ_COPY_NONE)
253                 return -EAGAIN;
254
255         nskb = ipq_build_packet_message(entry, &status);
256         if (nskb == NULL)
257                 return status;
258
259         write_lock_bh(&queue_lock);
260
261         if (!peer_pid)
262                 goto err_out_free_nskb;
263
264         if (queue_total >= queue_maxlen) {
265                 queue_dropped++;
266                 status = -ENOSPC;
267                 if (net_ratelimit())
268                           printk (KERN_WARNING "ip_queue: full at %d entries, "
269                                   "dropping packets(s). Dropped: %d\n", queue_total,
270                                   queue_dropped);
271                 goto err_out_free_nskb;
272         }
273
274         /* netlink_unicast will either free the nskb or attach it to a socket */
275         status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
276         if (status < 0) {
277                 queue_user_dropped++;
278                 goto err_out_unlock;
279         }
280
281         __ipq_enqueue_entry(entry);
282
283         write_unlock_bh(&queue_lock);
284         return status;
285
286 err_out_free_nskb:
287         kfree_skb(nskb);
288
289 err_out_unlock:
290         write_unlock_bh(&queue_lock);
291         return status;
292 }
293
294 static int
295 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct nf_queue_entry *e)
296 {
297         int diff;
298         int err;
299         struct iphdr *user_iph = (struct iphdr *)v->payload;
300
301         if (v->data_len < sizeof(*user_iph))
302                 return 0;
303         diff = v->data_len - e->skb->len;
304         if (diff < 0) {
305                 if (pskb_trim(e->skb, v->data_len))
306                         return -ENOMEM;
307         } else if (diff > 0) {
308                 if (v->data_len > 0xFFFF)
309                         return -EINVAL;
310                 if (diff > skb_tailroom(e->skb)) {
311                         err = pskb_expand_head(e->skb, 0,
312                                                diff - skb_tailroom(e->skb),
313                                                GFP_ATOMIC);
314                         if (err) {
315                                 printk(KERN_WARNING "ip_queue: error "
316                                       "in mangle, dropping packet: %d\n", -err);
317                                 return err;
318                         }
319                 }
320                 skb_put(e->skb, diff);
321         }
322         if (!skb_make_writable(e->skb, v->data_len))
323                 return -ENOMEM;
324         skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
325         e->skb->ip_summed = CHECKSUM_NONE;
326
327         return 0;
328 }
329
330 static int
331 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
332 {
333         struct nf_queue_entry *entry;
334
335         if (vmsg->value > NF_MAX_VERDICT)
336                 return -EINVAL;
337
338         entry = ipq_find_dequeue_entry(vmsg->id);
339         if (entry == NULL)
340                 return -ENOENT;
341         else {
342                 int verdict = vmsg->value;
343
344                 if (vmsg->data_len && vmsg->data_len == len)
345                         if (ipq_mangle_ipv4(vmsg, entry) < 0)
346                                 verdict = NF_DROP;
347
348                 ipq_issue_verdict(entry, verdict);
349                 return 0;
350         }
351 }
352
353 static int
354 ipq_set_mode(unsigned char mode, unsigned int range)
355 {
356         int status;
357
358         write_lock_bh(&queue_lock);
359         status = __ipq_set_mode(mode, range);
360         write_unlock_bh(&queue_lock);
361         return status;
362 }
363
364 static int
365 ipq_receive_peer(struct ipq_peer_msg *pmsg,
366                  unsigned char type, unsigned int len)
367 {
368         int status = 0;
369
370         if (len < sizeof(*pmsg))
371                 return -EINVAL;
372
373         switch (type) {
374         case IPQM_MODE:
375                 status = ipq_set_mode(pmsg->msg.mode.value,
376                                       pmsg->msg.mode.range);
377                 break;
378
379         case IPQM_VERDICT:
380                 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
381                         status = -EINVAL;
382                 else
383                         status = ipq_set_verdict(&pmsg->msg.verdict,
384                                                  len - sizeof(*pmsg));
385                         break;
386         default:
387                 status = -EINVAL;
388         }
389         return status;
390 }
391
392 static int
393 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
394 {
395         if (entry->indev)
396                 if (entry->indev->ifindex == ifindex)
397                         return 1;
398         if (entry->outdev)
399                 if (entry->outdev->ifindex == ifindex)
400                         return 1;
401 #ifdef CONFIG_BRIDGE_NETFILTER
402         if (entry->skb->nf_bridge) {
403                 if (entry->skb->nf_bridge->physindev &&
404                     entry->skb->nf_bridge->physindev->ifindex == ifindex)
405                         return 1;
406                 if (entry->skb->nf_bridge->physoutdev &&
407                     entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
408                         return 1;
409         }
410 #endif
411         return 0;
412 }
413
414 static void
415 ipq_dev_drop(int ifindex)
416 {
417         ipq_flush(dev_cmp, ifindex);
418 }
419
420 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
421
422 static inline void
423 __ipq_rcv_skb(struct sk_buff *skb)
424 {
425         int status, type, pid, flags, nlmsglen, skblen;
426         struct nlmsghdr *nlh;
427
428         skblen = skb->len;
429         if (skblen < sizeof(*nlh))
430                 return;
431
432         nlh = nlmsg_hdr(skb);
433         nlmsglen = nlh->nlmsg_len;
434         if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
435                 return;
436
437         pid = nlh->nlmsg_pid;
438         flags = nlh->nlmsg_flags;
439
440         if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
441                 RCV_SKB_FAIL(-EINVAL);
442
443         if (flags & MSG_TRUNC)
444                 RCV_SKB_FAIL(-ECOMM);
445
446         type = nlh->nlmsg_type;
447         if (type < NLMSG_NOOP || type >= IPQM_MAX)
448                 RCV_SKB_FAIL(-EINVAL);
449
450         if (type <= IPQM_BASE)
451                 return;
452
453         if (security_netlink_recv(skb, CAP_NET_ADMIN))
454                 RCV_SKB_FAIL(-EPERM);
455
456         write_lock_bh(&queue_lock);
457
458         if (peer_pid) {
459                 if (peer_pid != pid) {
460                         write_unlock_bh(&queue_lock);
461                         RCV_SKB_FAIL(-EBUSY);
462                 }
463         } else {
464                 net_enable_timestamp();
465                 peer_pid = pid;
466         }
467
468         write_unlock_bh(&queue_lock);
469
470         status = ipq_receive_peer(NLMSG_DATA(nlh), type,
471                                   nlmsglen - NLMSG_LENGTH(0));
472         if (status < 0)
473                 RCV_SKB_FAIL(status);
474
475         if (flags & NLM_F_ACK)
476                 netlink_ack(skb, nlh, 0);
477         return;
478 }
479
480 static void
481 ipq_rcv_skb(struct sk_buff *skb)
482 {
483         mutex_lock(&ipqnl_mutex);
484         __ipq_rcv_skb(skb);
485         mutex_unlock(&ipqnl_mutex);
486 }
487
488 static int
489 ipq_rcv_dev_event(struct notifier_block *this,
490                   unsigned long event, void *ptr)
491 {
492         struct net_device *dev = ptr;
493
494         if (dev->nd_net != &init_net)
495                 return NOTIFY_DONE;
496
497         /* Drop any packets associated with the downed device */
498         if (event == NETDEV_DOWN)
499                 ipq_dev_drop(dev->ifindex);
500         return NOTIFY_DONE;
501 }
502
503 static struct notifier_block ipq_dev_notifier = {
504         .notifier_call  = ipq_rcv_dev_event,
505 };
506
507 static int
508 ipq_rcv_nl_event(struct notifier_block *this,
509                  unsigned long event, void *ptr)
510 {
511         struct netlink_notify *n = ptr;
512
513         if (event == NETLINK_URELEASE &&
514             n->protocol == NETLINK_FIREWALL && n->pid) {
515                 write_lock_bh(&queue_lock);
516                 if ((n->net == &init_net) && (n->pid == peer_pid))
517                         __ipq_reset();
518                 write_unlock_bh(&queue_lock);
519         }
520         return NOTIFY_DONE;
521 }
522
523 static struct notifier_block ipq_nl_notifier = {
524         .notifier_call  = ipq_rcv_nl_event,
525 };
526
527 static struct ctl_table_header *ipq_sysctl_header;
528
529 static ctl_table ipq_table[] = {
530         {
531                 .ctl_name       = NET_IPQ_QMAX,
532                 .procname       = NET_IPQ_QMAX_NAME,
533                 .data           = &queue_maxlen,
534                 .maxlen         = sizeof(queue_maxlen),
535                 .mode           = 0644,
536                 .proc_handler   = proc_dointvec
537         },
538         { .ctl_name = 0 }
539 };
540
541 static ctl_table ipq_dir_table[] = {
542         {
543                 .ctl_name       = NET_IPV4,
544                 .procname       = "ipv4",
545                 .mode           = 0555,
546                 .child          = ipq_table
547         },
548         { .ctl_name = 0 }
549 };
550
551 static ctl_table ipq_root_table[] = {
552         {
553                 .ctl_name       = CTL_NET,
554                 .procname       = "net",
555                 .mode           = 0555,
556                 .child          = ipq_dir_table
557         },
558         { .ctl_name = 0 }
559 };
560
561 static int ip_queue_show(struct seq_file *m, void *v)
562 {
563         read_lock_bh(&queue_lock);
564
565         seq_printf(m,
566                       "Peer PID          : %d\n"
567                       "Copy mode         : %hu\n"
568                       "Copy range        : %u\n"
569                       "Queue length      : %u\n"
570                       "Queue max. length : %u\n"
571                       "Queue dropped     : %u\n"
572                       "Netlink dropped   : %u\n",
573                       peer_pid,
574                       copy_mode,
575                       copy_range,
576                       queue_total,
577                       queue_maxlen,
578                       queue_dropped,
579                       queue_user_dropped);
580
581         read_unlock_bh(&queue_lock);
582         return 0;
583 }
584
585 static int ip_queue_open(struct inode *inode, struct file *file)
586 {
587         return single_open(file, ip_queue_show, NULL);
588 }
589
590 static const struct file_operations ip_queue_proc_fops = {
591         .open           = ip_queue_open,
592         .read           = seq_read,
593         .llseek         = seq_lseek,
594         .release        = single_release,
595         .owner          = THIS_MODULE,
596 };
597
598 static const struct nf_queue_handler nfqh = {
599         .name   = "ip_queue",
600         .outfn  = &ipq_enqueue_packet,
601 };
602
603 static int __init ip_queue_init(void)
604 {
605         int status = -ENOMEM;
606         struct proc_dir_entry *proc;
607
608         netlink_register_notifier(&ipq_nl_notifier);
609         ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
610                                       ipq_rcv_skb, NULL, THIS_MODULE);
611         if (ipqnl == NULL) {
612                 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
613                 goto cleanup_netlink_notifier;
614         }
615
616         proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
617         if (proc) {
618                 proc->owner = THIS_MODULE;
619                 proc->proc_fops = &ip_queue_proc_fops;
620         } else {
621                 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
622                 goto cleanup_ipqnl;
623         }
624
625         register_netdevice_notifier(&ipq_dev_notifier);
626         ipq_sysctl_header = register_sysctl_table(ipq_root_table);
627
628         status = nf_register_queue_handler(PF_INET, &nfqh);
629         if (status < 0) {
630                 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
631                 goto cleanup_sysctl;
632         }
633         return status;
634
635 cleanup_sysctl:
636         unregister_sysctl_table(ipq_sysctl_header);
637         unregister_netdevice_notifier(&ipq_dev_notifier);
638         proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
639 cleanup_ipqnl:
640         sock_release(ipqnl->sk_socket);
641         mutex_lock(&ipqnl_mutex);
642         mutex_unlock(&ipqnl_mutex);
643
644 cleanup_netlink_notifier:
645         netlink_unregister_notifier(&ipq_nl_notifier);
646         return status;
647 }
648
649 static void __exit ip_queue_fini(void)
650 {
651         nf_unregister_queue_handlers(&nfqh);
652         synchronize_net();
653         ipq_flush(NULL, 0);
654
655         unregister_sysctl_table(ipq_sysctl_header);
656         unregister_netdevice_notifier(&ipq_dev_notifier);
657         proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
658
659         sock_release(ipqnl->sk_socket);
660         mutex_lock(&ipqnl_mutex);
661         mutex_unlock(&ipqnl_mutex);
662
663         netlink_unregister_notifier(&ipq_nl_notifier);
664 }
665
666 MODULE_DESCRIPTION("IPv4 packet queue handler");
667 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
668 MODULE_LICENSE("GPL");
669
670 module_init(ip_queue_init);
671 module_exit(ip_queue_fini);