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