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