OSDN Git Service

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