blob: eab309e3d42ed2e36adc349bd2fedaf815ebc43d [file] [log] [blame]
Harald Welte7af4cc32005-08-09 19:44:15 -07001/*
2 * This is a module which is used for queueing packets and communicating with
3 * userspace via nfetlink.
4 *
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
6 *
7 * Based on the old ipv4-only ip_queue.c:
8 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
9 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/init.h>
19#include <linux/spinlock.h>
20#include <linux/notifier.h>
21#include <linux/netdevice.h>
22#include <linux/netfilter.h>
Harald Welte838ab632005-08-09 19:50:45 -070023#include <linux/proc_fs.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070024#include <linux/netfilter_ipv4.h>
25#include <linux/netfilter_ipv6.h>
26#include <linux/netfilter/nfnetlink.h>
27#include <linux/netfilter/nfnetlink_queue.h>
28#include <linux/list.h>
29#include <net/sock.h>
30
31#include <asm/atomic.h>
32
33#define NFQNL_QMAX_DEFAULT 1024
34
35#if 0
36#define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
37 __FILE__, __LINE__, __FUNCTION__, \
38 ## args)
39#else
40#define QDEBUG(x, ...)
41#endif
42
43struct nfqnl_queue_entry {
44 struct list_head list;
45 struct nf_info *info;
46 struct sk_buff *skb;
47 unsigned int id;
48};
49
50struct nfqnl_instance {
51 struct hlist_node hlist; /* global list of queues */
Harald Welte838ab632005-08-09 19:50:45 -070052 atomic_t use;
Harald Welte7af4cc32005-08-09 19:44:15 -070053
54 int peer_pid;
55 unsigned int queue_maxlen;
56 unsigned int copy_range;
57 unsigned int queue_total;
58 unsigned int queue_dropped;
59 unsigned int queue_user_dropped;
60
61 atomic_t id_sequence; /* 'sequence' of pkt ids */
62
63 u_int16_t queue_num; /* number of this queue */
64 u_int8_t copy_mode;
65
66 spinlock_t lock;
67
68 struct list_head queue_list; /* packets in queue */
69};
70
71typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
72
73static DEFINE_RWLOCK(instances_lock);
74
75u_int64_t htonll(u_int64_t in)
76{
77 u_int64_t out;
78 int i;
79
80 for (i = 0; i < sizeof(u_int64_t); i++)
81 ((u_int8_t *)&out)[sizeof(u_int64_t)-1] = ((u_int8_t *)&in)[i];
82
83 return out;
84}
85
86#define INSTANCE_BUCKETS 16
87static struct hlist_head instance_table[INSTANCE_BUCKETS];
88
89static inline u_int8_t instance_hashfn(u_int16_t queue_num)
90{
91 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
92}
93
94static struct nfqnl_instance *
95__instance_lookup(u_int16_t queue_num)
96{
97 struct hlist_head *head;
98 struct hlist_node *pos;
99 struct nfqnl_instance *inst;
100
101 head = &instance_table[instance_hashfn(queue_num)];
102 hlist_for_each_entry(inst, pos, head, hlist) {
103 if (inst->queue_num == queue_num)
104 return inst;
105 }
106 return NULL;
107}
108
109static struct nfqnl_instance *
Harald Welte838ab632005-08-09 19:50:45 -0700110instance_lookup_get(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -0700111{
112 struct nfqnl_instance *inst;
113
114 read_lock_bh(&instances_lock);
115 inst = __instance_lookup(queue_num);
Harald Welte838ab632005-08-09 19:50:45 -0700116 if (inst)
117 atomic_inc(&inst->use);
Harald Welte7af4cc32005-08-09 19:44:15 -0700118 read_unlock_bh(&instances_lock);
119
120 return inst;
121}
122
Harald Welte838ab632005-08-09 19:50:45 -0700123static void
124instance_put(struct nfqnl_instance *inst)
125{
126 if (inst && atomic_dec_and_test(&inst->use)) {
127 QDEBUG("kfree(inst=%p)\n", inst);
128 kfree(inst);
129 }
130}
131
Harald Welte7af4cc32005-08-09 19:44:15 -0700132static struct nfqnl_instance *
133instance_create(u_int16_t queue_num, int pid)
134{
135 struct nfqnl_instance *inst;
136
137 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
138
139 write_lock_bh(&instances_lock);
140 if (__instance_lookup(queue_num)) {
141 inst = NULL;
142 QDEBUG("aborting, instance already exists\n");
143 goto out_unlock;
144 }
145
146 inst = kmalloc(sizeof(*inst), GFP_ATOMIC);
147 if (!inst)
148 goto out_unlock;
149
150 memset(inst, 0, sizeof(*inst));
151 inst->queue_num = queue_num;
152 inst->peer_pid = pid;
153 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
154 inst->copy_range = 0xfffff;
155 inst->copy_mode = NFQNL_COPY_NONE;
156 atomic_set(&inst->id_sequence, 0);
Harald Welte838ab632005-08-09 19:50:45 -0700157 /* needs to be two, since we _put() after creation */
158 atomic_set(&inst->use, 2);
Harald Welte7af4cc32005-08-09 19:44:15 -0700159 inst->lock = SPIN_LOCK_UNLOCKED;
160 INIT_LIST_HEAD(&inst->queue_list);
161
162 if (!try_module_get(THIS_MODULE))
163 goto out_free;
164
165 hlist_add_head(&inst->hlist,
166 &instance_table[instance_hashfn(queue_num)]);
167
168 write_unlock_bh(&instances_lock);
169
170 QDEBUG("successfully created new instance\n");
171
172 return inst;
173
174out_free:
175 kfree(inst);
176out_unlock:
177 write_unlock_bh(&instances_lock);
178 return NULL;
179}
180
181static void nfqnl_flush(struct nfqnl_instance *queue, int verdict);
182
183static void
184_instance_destroy2(struct nfqnl_instance *inst, int lock)
185{
186 /* first pull it out of the global list */
187 if (lock)
188 write_lock_bh(&instances_lock);
189
190 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
191 inst, inst->queue_num);
192 hlist_del(&inst->hlist);
193
194 if (lock)
195 write_unlock_bh(&instances_lock);
196
197 /* then flush all pending skbs from the queue */
198 nfqnl_flush(inst, NF_DROP);
199
Harald Welte838ab632005-08-09 19:50:45 -0700200 /* and finally put the refcount */
201 instance_put(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700202
203 module_put(THIS_MODULE);
204}
205
206static inline void
207__instance_destroy(struct nfqnl_instance *inst)
208{
209 _instance_destroy2(inst, 0);
210}
211
212static inline void
213instance_destroy(struct nfqnl_instance *inst)
214{
215 _instance_destroy2(inst, 1);
216}
217
218
219
220static void
221issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
222{
223 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
224
225 /* TCP input path (and probably other bits) assume to be called
226 * from softirq context, not from syscall, like issue_verdict is
227 * called. TCP input path deadlocks with locks taken from timer
228 * softirq, e.g. We therefore emulate this by local_bh_disable() */
229
230 local_bh_disable();
231 nf_reinject(entry->skb, entry->info, verdict);
232 local_bh_enable();
233
234 kfree(entry);
235}
236
237static inline void
238__enqueue_entry(struct nfqnl_instance *queue,
239 struct nfqnl_queue_entry *entry)
240{
241 list_add(&entry->list, &queue->queue_list);
242 queue->queue_total++;
243}
244
245/*
246 * Find and return a queued entry matched by cmpfn, or return the last
247 * entry if cmpfn is NULL.
248 */
249static inline struct nfqnl_queue_entry *
250__find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
251 unsigned long data)
252{
253 struct list_head *p;
254
255 list_for_each_prev(p, &queue->queue_list) {
256 struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
257
258 if (!cmpfn || cmpfn(entry, data))
259 return entry;
260 }
261 return NULL;
262}
263
264static inline void
265__dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
266{
267 list_del(&entry->list);
268 q->queue_total--;
269}
270
271static inline struct nfqnl_queue_entry *
272__find_dequeue_entry(struct nfqnl_instance *queue,
273 nfqnl_cmpfn cmpfn, unsigned long data)
274{
275 struct nfqnl_queue_entry *entry;
276
277 entry = __find_entry(queue, cmpfn, data);
278 if (entry == NULL)
279 return NULL;
280
281 __dequeue_entry(queue, entry);
282 return entry;
283}
284
285
286static inline void
287__nfqnl_flush(struct nfqnl_instance *queue, int verdict)
288{
289 struct nfqnl_queue_entry *entry;
290
291 while ((entry = __find_dequeue_entry(queue, NULL, 0)))
292 issue_verdict(entry, verdict);
293}
294
295static inline int
296__nfqnl_set_mode(struct nfqnl_instance *queue,
297 unsigned char mode, unsigned int range)
298{
299 int status = 0;
300
301 switch (mode) {
302 case NFQNL_COPY_NONE:
303 case NFQNL_COPY_META:
304 queue->copy_mode = mode;
305 queue->copy_range = 0;
306 break;
307
308 case NFQNL_COPY_PACKET:
309 queue->copy_mode = mode;
310 /* we're using struct nfattr which has 16bit nfa_len */
311 if (range > 0xffff)
312 queue->copy_range = 0xffff;
313 else
314 queue->copy_range = range;
315 break;
316
317 default:
318 status = -EINVAL;
319
320 }
321 return status;
322}
323
324static struct nfqnl_queue_entry *
325find_dequeue_entry(struct nfqnl_instance *queue,
326 nfqnl_cmpfn cmpfn, unsigned long data)
327{
328 struct nfqnl_queue_entry *entry;
329
330 spin_lock_bh(&queue->lock);
331 entry = __find_dequeue_entry(queue, cmpfn, data);
332 spin_unlock_bh(&queue->lock);
333
334 return entry;
335}
336
337static void
338nfqnl_flush(struct nfqnl_instance *queue, int verdict)
339{
340 spin_lock_bh(&queue->lock);
341 __nfqnl_flush(queue, verdict);
342 spin_unlock_bh(&queue->lock);
343}
344
345static struct sk_buff *
346nfqnl_build_packet_message(struct nfqnl_instance *queue,
347 struct nfqnl_queue_entry *entry, int *errp)
348{
349 unsigned char *old_tail;
350 size_t size;
351 size_t data_len = 0;
352 struct sk_buff *skb;
353 struct nfqnl_msg_packet_hdr pmsg;
354 struct nlmsghdr *nlh;
355 struct nfgenmsg *nfmsg;
356 unsigned int tmp_uint;
357
358 QDEBUG("entered\n");
359
360 /* all macros expand to constant values at compile time */
361 size = NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hdr))
362 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
363 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
364 + NLMSG_SPACE(sizeof(u_int32_t)) /* mark */
365 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hw))
366 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_timestamp));
367
368 spin_lock_bh(&queue->lock);
369
370 switch (queue->copy_mode) {
371 case NFQNL_COPY_META:
372 case NFQNL_COPY_NONE:
373 data_len = 0;
374 break;
375
376 case NFQNL_COPY_PACKET:
377 if (queue->copy_range == 0
378 || queue->copy_range > entry->skb->len)
379 data_len = entry->skb->len;
380 else
381 data_len = queue->copy_range;
382
383 size += NLMSG_SPACE(data_len);
384 break;
385
386 default:
387 *errp = -EINVAL;
388 spin_unlock_bh(&queue->lock);
389 return NULL;
390 }
391
392 spin_unlock_bh(&queue->lock);
393
394 skb = alloc_skb(size, GFP_ATOMIC);
395 if (!skb)
396 goto nlmsg_failure;
397
398 old_tail= skb->tail;
399 nlh = NLMSG_PUT(skb, 0, 0,
400 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
401 sizeof(struct nfgenmsg));
402 nfmsg = NLMSG_DATA(nlh);
403 nfmsg->nfgen_family = entry->info->pf;
404 nfmsg->version = NFNETLINK_V0;
405 nfmsg->res_id = htons(queue->queue_num);
406
407 pmsg.packet_id = htonl(entry->id);
408 pmsg.hw_protocol = htons(entry->skb->protocol);
409 pmsg.hook = entry->info->hook;
410
411 NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
412
413 if (entry->info->indev) {
414 tmp_uint = htonl(entry->info->indev->ifindex);
415 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
416 }
417
418 if (entry->info->outdev) {
419 tmp_uint = htonl(entry->info->outdev->ifindex);
420 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
421 }
422
423 if (entry->skb->nfmark) {
424 tmp_uint = htonl(entry->skb->nfmark);
425 NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
426 }
427
428 if (entry->info->indev && entry->skb->dev
429 && entry->skb->dev->hard_header_parse) {
430 struct nfqnl_msg_packet_hw phw;
431
432 phw.hw_addrlen =
433 entry->skb->dev->hard_header_parse(entry->skb,
434 phw.hw_addr);
435 phw.hw_addrlen = htons(phw.hw_addrlen);
436 NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
437 }
438
439 if (entry->skb->stamp.tv_sec) {
440 struct nfqnl_msg_packet_timestamp ts;
441
442 ts.sec = htonll(entry->skb->stamp.tv_sec);
443 ts.usec = htonll(entry->skb->stamp.tv_usec);
444
445 NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
446 }
447
448 if (data_len) {
449 struct nfattr *nfa;
450 int size = NFA_LENGTH(data_len);
451
452 if (skb_tailroom(skb) < (int)NFA_SPACE(data_len)) {
453 printk(KERN_WARNING "nf_queue: no tailroom!\n");
454 goto nlmsg_failure;
455 }
456
457 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
458 nfa->nfa_type = NFQA_PAYLOAD;
459 nfa->nfa_len = size;
460
461 if (skb_copy_bits(entry->skb, 0, NFA_DATA(nfa), data_len))
462 BUG();
463 }
464
465 nlh->nlmsg_len = skb->tail - old_tail;
466 return skb;
467
468nlmsg_failure:
469nfattr_failure:
470 if (skb)
471 kfree_skb(skb);
472 *errp = -EINVAL;
473 if (net_ratelimit())
474 printk(KERN_ERR "nf_queue: error creating packet message\n");
475 return NULL;
476}
477
478static int
479nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
480 unsigned int queuenum, void *data)
481{
482 int status = -EINVAL;
483 struct sk_buff *nskb;
484 struct nfqnl_instance *queue;
485 struct nfqnl_queue_entry *entry;
486
487 QDEBUG("entered\n");
488
Harald Welte838ab632005-08-09 19:50:45 -0700489 queue = instance_lookup_get(queuenum);
Harald Welte7af4cc32005-08-09 19:44:15 -0700490 if (!queue) {
491 QDEBUG("no queue instance matching\n");
492 return -EINVAL;
493 }
494
495 if (queue->copy_mode == NFQNL_COPY_NONE) {
496 QDEBUG("mode COPY_NONE, aborting\n");
Harald Welte838ab632005-08-09 19:50:45 -0700497 status = -EAGAIN;
498 goto err_out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700499 }
500
501 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
502 if (entry == NULL) {
503 if (net_ratelimit())
504 printk(KERN_ERR
505 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
Harald Welte838ab632005-08-09 19:50:45 -0700506 status = -ENOMEM;
507 goto err_out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700508 }
509
510 entry->info = info;
511 entry->skb = skb;
512 entry->id = atomic_inc_return(&queue->id_sequence);
513
514 nskb = nfqnl_build_packet_message(queue, entry, &status);
515 if (nskb == NULL)
516 goto err_out_free;
517
518 spin_lock_bh(&queue->lock);
519
520 if (!queue->peer_pid)
521 goto err_out_free_nskb;
522
523 if (queue->queue_total >= queue->queue_maxlen) {
524 queue->queue_dropped++;
525 status = -ENOSPC;
526 if (net_ratelimit())
527 printk(KERN_WARNING "ip_queue: full at %d entries, "
528 "dropping packets(s). Dropped: %d\n",
529 queue->queue_total, queue->queue_dropped);
530 goto err_out_free_nskb;
531 }
532
533 /* nfnetlink_unicast will either free the nskb or add it to a socket */
534 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
535 if (status < 0) {
536 queue->queue_user_dropped++;
537 goto err_out_unlock;
538 }
539
540 __enqueue_entry(queue, entry);
541
542 spin_unlock_bh(&queue->lock);
Harald Welte838ab632005-08-09 19:50:45 -0700543 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700544 return status;
545
546err_out_free_nskb:
547 kfree_skb(nskb);
548
549err_out_unlock:
550 spin_unlock_bh(&queue->lock);
551
552err_out_free:
553 kfree(entry);
Harald Welte838ab632005-08-09 19:50:45 -0700554err_out_put:
555 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700556 return status;
557}
558
559static int
560nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
561{
562 int diff;
563
564 diff = data_len - e->skb->len;
565 if (diff < 0)
566 skb_trim(e->skb, data_len);
567 else if (diff > 0) {
568 if (data_len > 0xFFFF)
569 return -EINVAL;
570 if (diff > skb_tailroom(e->skb)) {
571 struct sk_buff *newskb;
572
573 newskb = skb_copy_expand(e->skb,
574 skb_headroom(e->skb),
575 diff,
576 GFP_ATOMIC);
577 if (newskb == NULL) {
578 printk(KERN_WARNING "ip_queue: OOM "
579 "in mangle, dropping packet\n");
580 return -ENOMEM;
581 }
582 if (e->skb->sk)
583 skb_set_owner_w(newskb, e->skb->sk);
584 kfree_skb(e->skb);
585 e->skb = newskb;
586 }
587 skb_put(e->skb, diff);
588 }
589 if (!skb_make_writable(&e->skb, data_len))
590 return -ENOMEM;
591 memcpy(e->skb->data, data, data_len);
592
593 return 0;
594}
595
596static inline int
597id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
598{
599 return (id == e->id);
600}
601
602static int
603nfqnl_set_mode(struct nfqnl_instance *queue,
604 unsigned char mode, unsigned int range)
605{
606 int status;
607
608 spin_lock_bh(&queue->lock);
609 status = __nfqnl_set_mode(queue, mode, range);
610 spin_unlock_bh(&queue->lock);
611
612 return status;
613}
614
615static int
616dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
617{
618 if (entry->info->indev)
619 if (entry->info->indev->ifindex == ifindex)
620 return 1;
621
622 if (entry->info->outdev)
623 if (entry->info->outdev->ifindex == ifindex)
624 return 1;
625
626 return 0;
627}
628
629/* drop all packets with either indev or outdev == ifindex from all queue
630 * instances */
631static void
632nfqnl_dev_drop(int ifindex)
633{
634 int i;
635
636 QDEBUG("entering for ifindex %u\n", ifindex);
637
638 /* this only looks like we have to hold the readlock for a way too long
639 * time, issue_verdict(), nf_reinject(), ... - but we always only
640 * issue NF_DROP, which is processed directly in nf_reinject() */
641 read_lock_bh(&instances_lock);
642
643 for (i = 0; i < INSTANCE_BUCKETS; i++) {
644 struct hlist_node *tmp;
645 struct nfqnl_instance *inst;
646 struct hlist_head *head = &instance_table[i];
647
648 hlist_for_each_entry(inst, tmp, head, hlist) {
649 struct nfqnl_queue_entry *entry;
650 while ((entry = find_dequeue_entry(inst, dev_cmp,
651 ifindex)) != NULL)
652 issue_verdict(entry, NF_DROP);
653 }
654 }
655
656 read_unlock_bh(&instances_lock);
657}
658
659#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
660
661static int
662nfqnl_rcv_dev_event(struct notifier_block *this,
663 unsigned long event, void *ptr)
664{
665 struct net_device *dev = ptr;
666
667 /* Drop any packets associated with the downed device */
668 if (event == NETDEV_DOWN)
669 nfqnl_dev_drop(dev->ifindex);
670 return NOTIFY_DONE;
671}
672
673static struct notifier_block nfqnl_dev_notifier = {
674 .notifier_call = nfqnl_rcv_dev_event,
675};
676
677static int
678nfqnl_rcv_nl_event(struct notifier_block *this,
679 unsigned long event, void *ptr)
680{
681 struct netlink_notify *n = ptr;
682
683 if (event == NETLINK_URELEASE &&
684 n->protocol == NETLINK_NETFILTER && n->pid) {
685 int i;
686
687 /* destroy all instances for this pid */
688 write_lock_bh(&instances_lock);
689 for (i = 0; i < INSTANCE_BUCKETS; i++) {
690 struct hlist_node *tmp, *t2;
691 struct nfqnl_instance *inst;
692 struct hlist_head *head = &instance_table[i];
693
694 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
695 if (n->pid == inst->peer_pid)
696 __instance_destroy(inst);
697 }
698 }
699 write_unlock_bh(&instances_lock);
700 }
701 return NOTIFY_DONE;
702}
703
704static struct notifier_block nfqnl_rtnl_notifier = {
705 .notifier_call = nfqnl_rcv_nl_event,
706};
707
Harald Welte838ab632005-08-09 19:50:45 -0700708static const int nfqa_verdict_min[NFQA_MAX] = {
709 [NFQA_VERDICT_HDR-1] = sizeof(struct nfqnl_msg_verdict_hdr),
710 [NFQA_MARK-1] = sizeof(u_int32_t),
711 [NFQA_PAYLOAD-1] = 0,
712};
713
Harald Welte7af4cc32005-08-09 19:44:15 -0700714static int
715nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
716 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
717{
718 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
719 u_int16_t queue_num = ntohs(nfmsg->res_id);
720
721 struct nfqnl_msg_verdict_hdr *vhdr;
722 struct nfqnl_instance *queue;
723 unsigned int verdict;
724 struct nfqnl_queue_entry *entry;
Harald Welte838ab632005-08-09 19:50:45 -0700725 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700726
Harald Welte838ab632005-08-09 19:50:45 -0700727 if (nfattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) {
728 QDEBUG("bad attribute size\n");
729 return -EINVAL;
730 }
731
732 queue = instance_lookup_get(queue_num);
Harald Welte7af4cc32005-08-09 19:44:15 -0700733 if (!queue)
734 return -ENODEV;
735
Harald Welte838ab632005-08-09 19:50:45 -0700736 if (queue->peer_pid != NETLINK_CB(skb).pid) {
737 err = -EPERM;
738 goto err_out_put;
739 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700740
Harald Welte838ab632005-08-09 19:50:45 -0700741 if (!nfqa[NFQA_VERDICT_HDR-1]) {
742 err = -EINVAL;
743 goto err_out_put;
744 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700745
746 vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]);
747 verdict = ntohl(vhdr->verdict);
748
Harald Welte838ab632005-08-09 19:50:45 -0700749 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
750 err = -EINVAL;
751 goto err_out_put;
752 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700753
754 entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
Harald Welte838ab632005-08-09 19:50:45 -0700755 if (entry == NULL) {
756 err = -ENOENT;
757 goto err_out_put;
758 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700759
760 if (nfqa[NFQA_PAYLOAD-1]) {
761 if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]),
762 NFA_PAYLOAD(nfqa[NFQA_PAYLOAD-1]), entry) < 0)
763 verdict = NF_DROP;
764 }
765
766 if (nfqa[NFQA_MARK-1])
767 skb->nfmark = ntohl(*(u_int32_t *)NFA_DATA(nfqa[NFQA_MARK-1]));
768
769 issue_verdict(entry, verdict);
Harald Welte838ab632005-08-09 19:50:45 -0700770 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700771 return 0;
Harald Welte838ab632005-08-09 19:50:45 -0700772
773err_out_put:
774 instance_put(queue);
775 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700776}
777
778static int
779nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
780 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
781{
782 return -ENOTSUPP;
783}
784
Harald Welte838ab632005-08-09 19:50:45 -0700785static const int nfqa_cfg_min[NFQA_CFG_MAX] = {
786 [NFQA_CFG_CMD-1] = sizeof(struct nfqnl_msg_config_cmd),
787 [NFQA_CFG_PARAMS-1] = sizeof(struct nfqnl_msg_config_params),
788};
789
Harald Welte7af4cc32005-08-09 19:44:15 -0700790static int
791nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
792 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
793{
794 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
795 u_int16_t queue_num = ntohs(nfmsg->res_id);
796 struct nfqnl_instance *queue;
Harald Welte838ab632005-08-09 19:50:45 -0700797 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700798
799 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
800
Harald Welte838ab632005-08-09 19:50:45 -0700801 if (nfattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) {
802 QDEBUG("bad attribute size\n");
803 return -EINVAL;
804 }
805
806 queue = instance_lookup_get(queue_num);
Harald Welte7af4cc32005-08-09 19:44:15 -0700807 if (nfqa[NFQA_CFG_CMD-1]) {
808 struct nfqnl_msg_config_cmd *cmd;
809 cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]);
810 QDEBUG("found CFG_CMD\n");
811
812 switch (cmd->command) {
813 case NFQNL_CFG_CMD_BIND:
814 if (queue)
815 return -EBUSY;
816
817 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
818 if (!queue)
819 return -EINVAL;
820 break;
821 case NFQNL_CFG_CMD_UNBIND:
822 if (!queue)
823 return -ENODEV;
824
Harald Welte838ab632005-08-09 19:50:45 -0700825 if (queue->peer_pid != NETLINK_CB(skb).pid) {
826 ret = -EPERM;
827 goto out_put;
828 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700829
830 instance_destroy(queue);
831 break;
832 case NFQNL_CFG_CMD_PF_BIND:
833 QDEBUG("registering queue handler for pf=%u\n",
834 ntohs(cmd->pf));
Harald Welte838ab632005-08-09 19:50:45 -0700835 ret = nf_register_queue_handler(ntohs(cmd->pf),
836 nfqnl_enqueue_packet,
837 NULL);
Harald Welte7af4cc32005-08-09 19:44:15 -0700838
839 break;
840 case NFQNL_CFG_CMD_PF_UNBIND:
841 QDEBUG("unregistering queue handler for pf=%u\n",
842 ntohs(cmd->pf));
843 /* This is a bug and a feature. We can unregister
844 * other handlers(!) */
Harald Welte838ab632005-08-09 19:50:45 -0700845 ret = nf_unregister_queue_handler(ntohs(cmd->pf));
Harald Welte7af4cc32005-08-09 19:44:15 -0700846 break;
847 default:
Harald Welte838ab632005-08-09 19:50:45 -0700848 ret = -EINVAL;
849 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700850 }
851 } else {
852 if (!queue) {
853 QDEBUG("no config command, and no instance ENOENT\n");
Harald Welte838ab632005-08-09 19:50:45 -0700854 ret = -ENOENT;
855 goto out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700856 }
857
858 if (queue->peer_pid != NETLINK_CB(skb).pid) {
859 QDEBUG("no config command, and wrong pid\n");
Harald Welte838ab632005-08-09 19:50:45 -0700860 ret = -EPERM;
861 goto out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700862 }
863 }
864
865 if (nfqa[NFQA_CFG_PARAMS-1]) {
866 struct nfqnl_msg_config_params *params;
867 params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]);
868
869 nfqnl_set_mode(queue, params->copy_mode,
870 ntohl(params->copy_range));
871 }
872
Harald Welte838ab632005-08-09 19:50:45 -0700873out_put:
874 instance_put(queue);
875 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700876}
877
878static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
879 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
880 .cap_required = CAP_NET_ADMIN },
881 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
882 .cap_required = CAP_NET_ADMIN },
883 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
884 .cap_required = CAP_NET_ADMIN },
885};
886
887static struct nfnetlink_subsystem nfqnl_subsys = {
888 .name = "nf_queue",
889 .subsys_id = NFNL_SUBSYS_QUEUE,
890 .cb_count = NFQNL_MSG_MAX,
891 .attr_count = NFQA_MAX,
892 .cb = nfqnl_cb,
893};
894
Harald Welte838ab632005-08-09 19:50:45 -0700895#ifdef CONFIG_PROC_FS
896struct iter_state {
897 unsigned int bucket;
898};
899
900static struct hlist_node *get_first(struct seq_file *seq)
901{
902 struct iter_state *st = seq->private;
903
904 if (!st)
905 return NULL;
906
907 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
908 if (!hlist_empty(&instance_table[st->bucket]))
909 return instance_table[st->bucket].first;
910 }
911 return NULL;
912}
913
914static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
915{
916 struct iter_state *st = seq->private;
917
918 h = h->next;
919 while (!h) {
920 if (++st->bucket >= INSTANCE_BUCKETS)
921 return NULL;
922
923 h = instance_table[st->bucket].first;
924 }
925 return h;
926}
927
928static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
929{
930 struct hlist_node *head;
931 head = get_first(seq);
932
933 if (head)
934 while (pos && (head = get_next(seq, head)))
935 pos--;
936 return pos ? NULL : head;
937}
938
939static void *seq_start(struct seq_file *seq, loff_t *pos)
940{
941 read_lock_bh(&instances_lock);
942 return get_idx(seq, *pos);
943}
944
945static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
946{
947 (*pos)++;
948 return get_next(s, v);
949}
950
951static void seq_stop(struct seq_file *s, void *v)
952{
953 read_unlock_bh(&instances_lock);
954}
955
956static int seq_show(struct seq_file *s, void *v)
957{
958 const struct nfqnl_instance *inst = v;
959
960 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
961 inst->queue_num,
962 inst->peer_pid, inst->queue_total,
963 inst->copy_mode, inst->copy_range,
964 inst->queue_dropped, inst->queue_user_dropped,
965 atomic_read(&inst->id_sequence),
966 atomic_read(&inst->use));
967}
968
969static struct seq_operations nfqnl_seq_ops = {
970 .start = seq_start,
971 .next = seq_next,
972 .stop = seq_stop,
973 .show = seq_show,
974};
975
976static int nfqnl_open(struct inode *inode, struct file *file)
977{
978 struct seq_file *seq;
979 struct iter_state *is;
980 int ret;
981
982 is = kmalloc(sizeof(*is), GFP_KERNEL);
983 if (!is)
984 return -ENOMEM;
985 memset(is, 0, sizeof(*is));
986 ret = seq_open(file, &nfqnl_seq_ops);
987 if (ret < 0)
988 goto out_free;
989 seq = file->private_data;
990 seq->private = is;
991 return ret;
992out_free:
993 kfree(is);
994 return ret;
995}
996
997static struct file_operations nfqnl_file_ops = {
998 .owner = THIS_MODULE,
999 .open = nfqnl_open,
1000 .read = seq_read,
1001 .llseek = seq_lseek,
1002 .release = seq_release_private,
1003};
1004
1005#endif /* PROC_FS */
1006
Harald Welte7af4cc32005-08-09 19:44:15 -07001007static int
1008init_or_cleanup(int init)
1009{
Harald Welte838ab632005-08-09 19:50:45 -07001010 int i, status = -ENOMEM;
1011#ifdef CONFIG_PROC_FS
1012 struct proc_dir_entry *proc_nfqueue;
1013#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001014
1015 if (!init)
1016 goto cleanup;
1017
Harald Welte838ab632005-08-09 19:50:45 -07001018 for (i = 0; i < INSTANCE_BUCKETS; i++)
1019 INIT_HLIST_HEAD(&instance_table[i]);
1020
Harald Welte7af4cc32005-08-09 19:44:15 -07001021 netlink_register_notifier(&nfqnl_rtnl_notifier);
1022 status = nfnetlink_subsys_register(&nfqnl_subsys);
1023 if (status < 0) {
1024 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1025 goto cleanup_netlink_notifier;
1026 }
1027
Harald Welte838ab632005-08-09 19:50:45 -07001028#ifdef CONFIG_PROC_FS
1029 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1030 proc_net_netfilter);
1031 if (!proc_nfqueue)
1032 goto cleanup_subsys;
1033 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1034#endif
1035
Harald Welte7af4cc32005-08-09 19:44:15 -07001036 register_netdevice_notifier(&nfqnl_dev_notifier);
Harald Welte838ab632005-08-09 19:50:45 -07001037
Harald Welte7af4cc32005-08-09 19:44:15 -07001038 return status;
1039
1040cleanup:
1041 nf_unregister_queue_handlers(nfqnl_enqueue_packet);
1042 unregister_netdevice_notifier(&nfqnl_dev_notifier);
Harald Welte838ab632005-08-09 19:50:45 -07001043#ifdef CONFIG_PROC_FS
1044cleanup_subsys:
1045#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001046 nfnetlink_subsys_unregister(&nfqnl_subsys);
Harald Welte7af4cc32005-08-09 19:44:15 -07001047cleanup_netlink_notifier:
1048 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1049 return status;
1050}
1051
1052static int __init init(void)
1053{
1054
1055 return init_or_cleanup(1);
1056}
1057
1058static void __exit fini(void)
1059{
1060 init_or_cleanup(0);
1061}
1062
1063MODULE_DESCRIPTION("netfilter packet queue handler");
1064MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1065MODULE_LICENSE("GPL");
1066MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1067
1068module_init(init);
1069module_exit(fini);