]> git.ipfire.org Git - people/ms/linux.git/blame - net/netfilter/nfnetlink_queue.c
[NETFILTER]: nfnetlink_log: use netlink policy
[people/ms/linux.git] / net / netfilter / nfnetlink_queue.c
CommitLineData
7af4cc3f
HW
1/*
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>
838ab636 23#include <linux/proc_fs.h>
7af4cc3f
HW
24#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
fbcd923c
HW
33#ifdef CONFIG_BRIDGE_NETFILTER
34#include "../bridge/br_private.h"
35#endif
36
7af4cc3f
HW
37#define NFQNL_QMAX_DEFAULT 1024
38
39#if 0
40#define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
41 __FILE__, __LINE__, __FUNCTION__, \
42 ## args)
43#else
44#define QDEBUG(x, ...)
45#endif
46
47struct nfqnl_queue_entry {
48 struct list_head list;
49 struct nf_info *info;
50 struct sk_buff *skb;
51 unsigned int id;
52};
53
54struct nfqnl_instance {
55 struct hlist_node hlist; /* global list of queues */
838ab636 56 atomic_t use;
7af4cc3f
HW
57
58 int peer_pid;
59 unsigned int queue_maxlen;
60 unsigned int copy_range;
61 unsigned int queue_total;
62 unsigned int queue_dropped;
63 unsigned int queue_user_dropped;
64
65 atomic_t id_sequence; /* 'sequence' of pkt ids */
66
67 u_int16_t queue_num; /* number of this queue */
68 u_int8_t copy_mode;
69
70 spinlock_t lock;
71
72 struct list_head queue_list; /* packets in queue */
73};
74
75typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
76
77static DEFINE_RWLOCK(instances_lock);
78
7af4cc3f
HW
79#define INSTANCE_BUCKETS 16
80static struct hlist_head instance_table[INSTANCE_BUCKETS];
81
82static inline u_int8_t instance_hashfn(u_int16_t queue_num)
83{
84 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
85}
86
87static struct nfqnl_instance *
88__instance_lookup(u_int16_t queue_num)
89{
90 struct hlist_head *head;
91 struct hlist_node *pos;
92 struct nfqnl_instance *inst;
93
94 head = &instance_table[instance_hashfn(queue_num)];
95 hlist_for_each_entry(inst, pos, head, hlist) {
96 if (inst->queue_num == queue_num)
97 return inst;
98 }
99 return NULL;
100}
101
102static struct nfqnl_instance *
838ab636 103instance_lookup_get(u_int16_t queue_num)
7af4cc3f
HW
104{
105 struct nfqnl_instance *inst;
106
107 read_lock_bh(&instances_lock);
108 inst = __instance_lookup(queue_num);
838ab636
HW
109 if (inst)
110 atomic_inc(&inst->use);
7af4cc3f
HW
111 read_unlock_bh(&instances_lock);
112
113 return inst;
114}
115
838ab636
HW
116static void
117instance_put(struct nfqnl_instance *inst)
118{
119 if (inst && atomic_dec_and_test(&inst->use)) {
120 QDEBUG("kfree(inst=%p)\n", inst);
121 kfree(inst);
122 }
123}
124
7af4cc3f
HW
125static struct nfqnl_instance *
126instance_create(u_int16_t queue_num, int pid)
127{
128 struct nfqnl_instance *inst;
129
130 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
131
601e68e1 132 write_lock_bh(&instances_lock);
7af4cc3f
HW
133 if (__instance_lookup(queue_num)) {
134 inst = NULL;
135 QDEBUG("aborting, instance already exists\n");
136 goto out_unlock;
137 }
138
10dfdc69 139 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
7af4cc3f
HW
140 if (!inst)
141 goto out_unlock;
142
7af4cc3f
HW
143 inst->queue_num = queue_num;
144 inst->peer_pid = pid;
145 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
146 inst->copy_range = 0xfffff;
147 inst->copy_mode = NFQNL_COPY_NONE;
148 atomic_set(&inst->id_sequence, 0);
838ab636
HW
149 /* needs to be two, since we _put() after creation */
150 atomic_set(&inst->use, 2);
181a46a5 151 spin_lock_init(&inst->lock);
7af4cc3f
HW
152 INIT_LIST_HEAD(&inst->queue_list);
153
154 if (!try_module_get(THIS_MODULE))
155 goto out_free;
156
601e68e1 157 hlist_add_head(&inst->hlist,
7af4cc3f
HW
158 &instance_table[instance_hashfn(queue_num)]);
159
160 write_unlock_bh(&instances_lock);
161
162 QDEBUG("successfully created new instance\n");
163
164 return inst;
165
166out_free:
167 kfree(inst);
168out_unlock:
169 write_unlock_bh(&instances_lock);
170 return NULL;
171}
172
173static void nfqnl_flush(struct nfqnl_instance *queue, int verdict);
174
175static void
176_instance_destroy2(struct nfqnl_instance *inst, int lock)
177{
178 /* first pull it out of the global list */
179 if (lock)
180 write_lock_bh(&instances_lock);
181
182 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
183 inst, inst->queue_num);
184 hlist_del(&inst->hlist);
185
186 if (lock)
187 write_unlock_bh(&instances_lock);
188
189 /* then flush all pending skbs from the queue */
190 nfqnl_flush(inst, NF_DROP);
191
838ab636
HW
192 /* and finally put the refcount */
193 instance_put(inst);
7af4cc3f
HW
194
195 module_put(THIS_MODULE);
196}
197
198static inline void
199__instance_destroy(struct nfqnl_instance *inst)
200{
201 _instance_destroy2(inst, 0);
202}
203
204static inline void
205instance_destroy(struct nfqnl_instance *inst)
206{
207 _instance_destroy2(inst, 1);
208}
209
210
211
212static void
213issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
214{
215 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
216
217 /* TCP input path (and probably other bits) assume to be called
218 * from softirq context, not from syscall, like issue_verdict is
219 * called. TCP input path deadlocks with locks taken from timer
220 * softirq, e.g. We therefore emulate this by local_bh_disable() */
221
222 local_bh_disable();
223 nf_reinject(entry->skb, entry->info, verdict);
224 local_bh_enable();
225
226 kfree(entry);
227}
228
229static inline void
230__enqueue_entry(struct nfqnl_instance *queue,
231 struct nfqnl_queue_entry *entry)
232{
233 list_add(&entry->list, &queue->queue_list);
234 queue->queue_total++;
235}
236
237/*
238 * Find and return a queued entry matched by cmpfn, or return the last
239 * entry if cmpfn is NULL.
240 */
241static inline struct nfqnl_queue_entry *
601e68e1 242__find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
7af4cc3f
HW
243 unsigned long data)
244{
245 struct list_head *p;
246
247 list_for_each_prev(p, &queue->queue_list) {
248 struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
601e68e1 249
7af4cc3f
HW
250 if (!cmpfn || cmpfn(entry, data))
251 return entry;
252 }
253 return NULL;
254}
255
256static inline void
257__dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
258{
259 list_del(&entry->list);
260 q->queue_total--;
261}
262
263static inline struct nfqnl_queue_entry *
264__find_dequeue_entry(struct nfqnl_instance *queue,
265 nfqnl_cmpfn cmpfn, unsigned long data)
266{
267 struct nfqnl_queue_entry *entry;
268
269 entry = __find_entry(queue, cmpfn, data);
270 if (entry == NULL)
271 return NULL;
272
273 __dequeue_entry(queue, entry);
274 return entry;
275}
276
277
278static inline void
279__nfqnl_flush(struct nfqnl_instance *queue, int verdict)
280{
281 struct nfqnl_queue_entry *entry;
601e68e1 282
7af4cc3f
HW
283 while ((entry = __find_dequeue_entry(queue, NULL, 0)))
284 issue_verdict(entry, verdict);
285}
286
287static inline int
288__nfqnl_set_mode(struct nfqnl_instance *queue,
289 unsigned char mode, unsigned int range)
290{
291 int status = 0;
601e68e1 292
7af4cc3f
HW
293 switch (mode) {
294 case NFQNL_COPY_NONE:
295 case NFQNL_COPY_META:
296 queue->copy_mode = mode;
297 queue->copy_range = 0;
298 break;
601e68e1 299
7af4cc3f
HW
300 case NFQNL_COPY_PACKET:
301 queue->copy_mode = mode;
df6fb868 302 /* we're using struct nlattr which has 16bit nla_len */
7af4cc3f
HW
303 if (range > 0xffff)
304 queue->copy_range = 0xffff;
305 else
306 queue->copy_range = range;
307 break;
601e68e1 308
7af4cc3f
HW
309 default:
310 status = -EINVAL;
311
312 }
313 return status;
314}
315
316static struct nfqnl_queue_entry *
317find_dequeue_entry(struct nfqnl_instance *queue,
318 nfqnl_cmpfn cmpfn, unsigned long data)
319{
320 struct nfqnl_queue_entry *entry;
601e68e1 321
7af4cc3f
HW
322 spin_lock_bh(&queue->lock);
323 entry = __find_dequeue_entry(queue, cmpfn, data);
324 spin_unlock_bh(&queue->lock);
325
326 return entry;
327}
328
329static void
330nfqnl_flush(struct nfqnl_instance *queue, int verdict)
331{
332 spin_lock_bh(&queue->lock);
333 __nfqnl_flush(queue, verdict);
334 spin_unlock_bh(&queue->lock);
335}
336
337static struct sk_buff *
338nfqnl_build_packet_message(struct nfqnl_instance *queue,
339 struct nfqnl_queue_entry *entry, int *errp)
340{
27a884dc 341 sk_buff_data_t old_tail;
7af4cc3f
HW
342 size_t size;
343 size_t data_len = 0;
344 struct sk_buff *skb;
345 struct nfqnl_msg_packet_hdr pmsg;
346 struct nlmsghdr *nlh;
347 struct nfgenmsg *nfmsg;
3e4ead4f
JJ
348 struct nf_info *entinf = entry->info;
349 struct sk_buff *entskb = entry->skb;
350 struct net_device *indev;
351 struct net_device *outdev;
98a4a861 352 __be32 tmp_uint;
7af4cc3f
HW
353
354 QDEBUG("entered\n");
355
df6fb868
PM
356 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
357 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
358 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
359 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
fbcd923c 360#ifdef CONFIG_BRIDGE_NETFILTER
df6fb868
PM
361 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
362 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
fbcd923c 363#endif
df6fb868
PM
364 + nla_total_size(sizeof(u_int32_t)) /* mark */
365 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
366 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
7af4cc3f 367
3e4ead4f
JJ
368 outdev = entinf->outdev;
369
7af4cc3f 370 spin_lock_bh(&queue->lock);
601e68e1 371
7af4cc3f
HW
372 switch (queue->copy_mode) {
373 case NFQNL_COPY_META:
374 case NFQNL_COPY_NONE:
375 data_len = 0;
376 break;
601e68e1 377
7af4cc3f 378 case NFQNL_COPY_PACKET:
84fa7933
PM
379 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
380 entskb->ip_summed == CHECKSUM_COMPLETE) &&
381 (*errp = skb_checksum_help(entskb))) {
e7dfb09a
PM
382 spin_unlock_bh(&queue->lock);
383 return NULL;
384 }
601e68e1 385 if (queue->copy_range == 0
3e4ead4f
JJ
386 || queue->copy_range > entskb->len)
387 data_len = entskb->len;
7af4cc3f
HW
388 else
389 data_len = queue->copy_range;
601e68e1 390
df6fb868 391 size += nla_total_size(data_len);
7af4cc3f 392 break;
601e68e1 393
7af4cc3f
HW
394 default:
395 *errp = -EINVAL;
396 spin_unlock_bh(&queue->lock);
397 return NULL;
398 }
399
400 spin_unlock_bh(&queue->lock);
401
402 skb = alloc_skb(size, GFP_ATOMIC);
403 if (!skb)
404 goto nlmsg_failure;
601e68e1 405
27a884dc 406 old_tail = skb->tail;
601e68e1 407 nlh = NLMSG_PUT(skb, 0, 0,
7af4cc3f
HW
408 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
409 sizeof(struct nfgenmsg));
410 nfmsg = NLMSG_DATA(nlh);
3e4ead4f 411 nfmsg->nfgen_family = entinf->pf;
7af4cc3f
HW
412 nfmsg->version = NFNETLINK_V0;
413 nfmsg->res_id = htons(queue->queue_num);
414
415 pmsg.packet_id = htonl(entry->id);
febf0a43 416 pmsg.hw_protocol = entskb->protocol;
3e4ead4f 417 pmsg.hook = entinf->hook;
7af4cc3f 418
df6fb868 419 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
7af4cc3f 420
3e4ead4f
JJ
421 indev = entinf->indev;
422 if (indev) {
423 tmp_uint = htonl(indev->ifindex);
fbcd923c 424#ifndef CONFIG_BRIDGE_NETFILTER
df6fb868 425 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
fbcd923c 426#else
3e4ead4f 427 if (entinf->pf == PF_BRIDGE) {
fbcd923c 428 /* Case 1: indev is physical input device, we need to
601e68e1 429 * look for bridge group (when called from
fbcd923c 430 * netfilter_bridge) */
df6fb868 431 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
fbcd923c
HW
432 &tmp_uint);
433 /* this is the bridge group "brX" */
3e4ead4f 434 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
df6fb868 435 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
fbcd923c
HW
436 &tmp_uint);
437 } else {
438 /* Case 2: indev is bridge group, we need to look for
439 * physical device (when called from ipv4) */
df6fb868 440 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
fbcd923c 441 &tmp_uint);
3e4ead4f
JJ
442 if (entskb->nf_bridge
443 && entskb->nf_bridge->physindev) {
444 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
df6fb868 445 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
fbcd923c
HW
446 sizeof(tmp_uint), &tmp_uint);
447 }
448 }
449#endif
7af4cc3f
HW
450 }
451
3e4ead4f
JJ
452 if (outdev) {
453 tmp_uint = htonl(outdev->ifindex);
fbcd923c 454#ifndef CONFIG_BRIDGE_NETFILTER
df6fb868 455 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
fbcd923c 456#else
3e4ead4f 457 if (entinf->pf == PF_BRIDGE) {
fbcd923c 458 /* Case 1: outdev is physical output device, we need to
601e68e1 459 * look for bridge group (when called from
fbcd923c 460 * netfilter_bridge) */
df6fb868 461 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
fbcd923c
HW
462 &tmp_uint);
463 /* this is the bridge group "brX" */
3e4ead4f 464 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
df6fb868 465 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
fbcd923c
HW
466 &tmp_uint);
467 } else {
468 /* Case 2: outdev is bridge group, we need to look for
469 * physical output device (when called from ipv4) */
df6fb868 470 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
fbcd923c 471 &tmp_uint);
3e4ead4f
JJ
472 if (entskb->nf_bridge
473 && entskb->nf_bridge->physoutdev) {
474 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
df6fb868 475 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
fbcd923c
HW
476 sizeof(tmp_uint), &tmp_uint);
477 }
478 }
479#endif
7af4cc3f
HW
480 }
481
82e91ffe
TG
482 if (entskb->mark) {
483 tmp_uint = htonl(entskb->mark);
df6fb868 484 NLA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
7af4cc3f
HW
485 }
486
b95cce35 487 if (indev && entskb->dev) {
7af4cc3f 488 struct nfqnl_msg_packet_hw phw;
b95cce35
SH
489 int len = dev_parse_header(entskb, phw.hw_addr);
490 if (len) {
491 phw.hw_addrlen = htons(len);
df6fb868 492 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
b95cce35 493 }
7af4cc3f
HW
494 }
495
b7aa0bf7 496 if (entskb->tstamp.tv64) {
7af4cc3f 497 struct nfqnl_msg_packet_timestamp ts;
b7aa0bf7
ED
498 struct timeval tv = ktime_to_timeval(entskb->tstamp);
499 ts.sec = cpu_to_be64(tv.tv_sec);
500 ts.usec = cpu_to_be64(tv.tv_usec);
7af4cc3f 501
df6fb868 502 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
7af4cc3f
HW
503 }
504
505 if (data_len) {
df6fb868
PM
506 struct nlattr *nla;
507 int size = nla_attr_size(data_len);
7af4cc3f 508
df6fb868 509 if (skb_tailroom(skb) < nla_total_size(data_len)) {
7af4cc3f
HW
510 printk(KERN_WARNING "nf_queue: no tailroom!\n");
511 goto nlmsg_failure;
512 }
513
df6fb868
PM
514 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
515 nla->nla_type = NFQA_PAYLOAD;
516 nla->nla_len = size;
7af4cc3f 517
df6fb868 518 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
7af4cc3f
HW
519 BUG();
520 }
601e68e1 521
7af4cc3f
HW
522 nlh->nlmsg_len = skb->tail - old_tail;
523 return skb;
524
525nlmsg_failure:
df6fb868 526nla_put_failure:
7af4cc3f
HW
527 if (skb)
528 kfree_skb(skb);
529 *errp = -EINVAL;
530 if (net_ratelimit())
531 printk(KERN_ERR "nf_queue: error creating packet message\n");
532 return NULL;
533}
534
535static int
601e68e1 536nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
7af4cc3f
HW
537 unsigned int queuenum, void *data)
538{
539 int status = -EINVAL;
540 struct sk_buff *nskb;
541 struct nfqnl_instance *queue;
542 struct nfqnl_queue_entry *entry;
543
544 QDEBUG("entered\n");
545
838ab636 546 queue = instance_lookup_get(queuenum);
7af4cc3f
HW
547 if (!queue) {
548 QDEBUG("no queue instance matching\n");
549 return -EINVAL;
550 }
551
552 if (queue->copy_mode == NFQNL_COPY_NONE) {
553 QDEBUG("mode COPY_NONE, aborting\n");
838ab636
HW
554 status = -EAGAIN;
555 goto err_out_put;
7af4cc3f
HW
556 }
557
558 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
559 if (entry == NULL) {
560 if (net_ratelimit())
601e68e1 561 printk(KERN_ERR
7af4cc3f 562 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
838ab636
HW
563 status = -ENOMEM;
564 goto err_out_put;
7af4cc3f
HW
565 }
566
567 entry->info = info;
568 entry->skb = skb;
569 entry->id = atomic_inc_return(&queue->id_sequence);
570
571 nskb = nfqnl_build_packet_message(queue, entry, &status);
572 if (nskb == NULL)
573 goto err_out_free;
601e68e1 574
7af4cc3f 575 spin_lock_bh(&queue->lock);
601e68e1 576
7af4cc3f 577 if (!queue->peer_pid)
601e68e1 578 goto err_out_free_nskb;
7af4cc3f
HW
579
580 if (queue->queue_total >= queue->queue_maxlen) {
601e68e1 581 queue->queue_dropped++;
7af4cc3f
HW
582 status = -ENOSPC;
583 if (net_ratelimit())
601e68e1
YH
584 printk(KERN_WARNING "nf_queue: full at %d entries, "
585 "dropping packets(s). Dropped: %d\n",
7af4cc3f
HW
586 queue->queue_total, queue->queue_dropped);
587 goto err_out_free_nskb;
588 }
589
590 /* nfnetlink_unicast will either free the nskb or add it to a socket */
591 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
592 if (status < 0) {
601e68e1 593 queue->queue_user_dropped++;
7af4cc3f
HW
594 goto err_out_unlock;
595 }
596
597 __enqueue_entry(queue, entry);
598
599 spin_unlock_bh(&queue->lock);
838ab636 600 instance_put(queue);
7af4cc3f
HW
601 return status;
602
603err_out_free_nskb:
601e68e1
YH
604 kfree_skb(nskb);
605
7af4cc3f
HW
606err_out_unlock:
607 spin_unlock_bh(&queue->lock);
608
609err_out_free:
610 kfree(entry);
838ab636
HW
611err_out_put:
612 instance_put(queue);
7af4cc3f
HW
613 return status;
614}
615
616static int
617nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
618{
619 int diff;
620
621 diff = data_len - e->skb->len;
d8a585d7
PM
622 if (diff < 0) {
623 if (pskb_trim(e->skb, data_len))
624 return -ENOMEM;
625 } else if (diff > 0) {
7af4cc3f
HW
626 if (data_len > 0xFFFF)
627 return -EINVAL;
628 if (diff > skb_tailroom(e->skb)) {
629 struct sk_buff *newskb;
601e68e1 630
7af4cc3f 631 newskb = skb_copy_expand(e->skb,
601e68e1
YH
632 skb_headroom(e->skb),
633 diff,
634 GFP_ATOMIC);
7af4cc3f 635 if (newskb == NULL) {
1158ba27 636 printk(KERN_WARNING "nf_queue: OOM "
7af4cc3f
HW
637 "in mangle, dropping packet\n");
638 return -ENOMEM;
639 }
640 if (e->skb->sk)
641 skb_set_owner_w(newskb, e->skb->sk);
642 kfree_skb(e->skb);
643 e->skb = newskb;
644 }
645 skb_put(e->skb, diff);
646 }
647 if (!skb_make_writable(&e->skb, data_len))
648 return -ENOMEM;
27d7ff46 649 skb_copy_to_linear_data(e->skb, data, data_len);
e7dfb09a 650 e->skb->ip_summed = CHECKSUM_NONE;
7af4cc3f
HW
651 return 0;
652}
653
654static inline int
655id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
656{
657 return (id == e->id);
658}
659
660static int
661nfqnl_set_mode(struct nfqnl_instance *queue,
662 unsigned char mode, unsigned int range)
663{
664 int status;
665
666 spin_lock_bh(&queue->lock);
667 status = __nfqnl_set_mode(queue, mode, range);
668 spin_unlock_bh(&queue->lock);
669
670 return status;
671}
672
673static int
674dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
675{
3e4ead4f 676 struct nf_info *entinf = entry->info;
601e68e1 677
3e4ead4f
JJ
678 if (entinf->indev)
679 if (entinf->indev->ifindex == ifindex)
7af4cc3f 680 return 1;
3e4ead4f
JJ
681 if (entinf->outdev)
682 if (entinf->outdev->ifindex == ifindex)
7af4cc3f 683 return 1;
ef47c6a7
PM
684#ifdef CONFIG_BRIDGE_NETFILTER
685 if (entry->skb->nf_bridge) {
686 if (entry->skb->nf_bridge->physindev &&
687 entry->skb->nf_bridge->physindev->ifindex == ifindex)
688 return 1;
689 if (entry->skb->nf_bridge->physoutdev &&
690 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
691 return 1;
692 }
693#endif
7af4cc3f
HW
694 return 0;
695}
696
697/* drop all packets with either indev or outdev == ifindex from all queue
698 * instances */
699static void
700nfqnl_dev_drop(int ifindex)
701{
702 int i;
601e68e1 703
7af4cc3f
HW
704 QDEBUG("entering for ifindex %u\n", ifindex);
705
706 /* this only looks like we have to hold the readlock for a way too long
707 * time, issue_verdict(), nf_reinject(), ... - but we always only
708 * issue NF_DROP, which is processed directly in nf_reinject() */
709 read_lock_bh(&instances_lock);
710
711 for (i = 0; i < INSTANCE_BUCKETS; i++) {
712 struct hlist_node *tmp;
713 struct nfqnl_instance *inst;
714 struct hlist_head *head = &instance_table[i];
715
716 hlist_for_each_entry(inst, tmp, head, hlist) {
717 struct nfqnl_queue_entry *entry;
601e68e1 718 while ((entry = find_dequeue_entry(inst, dev_cmp,
7af4cc3f
HW
719 ifindex)) != NULL)
720 issue_verdict(entry, NF_DROP);
721 }
722 }
723
724 read_unlock_bh(&instances_lock);
725}
726
727#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
728
729static int
730nfqnl_rcv_dev_event(struct notifier_block *this,
731 unsigned long event, void *ptr)
732{
733 struct net_device *dev = ptr;
734
e9dc8653
EB
735 if (dev->nd_net != &init_net)
736 return NOTIFY_DONE;
737
7af4cc3f
HW
738 /* Drop any packets associated with the downed device */
739 if (event == NETDEV_DOWN)
740 nfqnl_dev_drop(dev->ifindex);
741 return NOTIFY_DONE;
742}
743
744static struct notifier_block nfqnl_dev_notifier = {
745 .notifier_call = nfqnl_rcv_dev_event,
746};
747
748static int
749nfqnl_rcv_nl_event(struct notifier_block *this,
750 unsigned long event, void *ptr)
751{
752 struct netlink_notify *n = ptr;
753
754 if (event == NETLINK_URELEASE &&
755 n->protocol == NETLINK_NETFILTER && n->pid) {
756 int i;
757
758 /* destroy all instances for this pid */
759 write_lock_bh(&instances_lock);
760 for (i = 0; i < INSTANCE_BUCKETS; i++) {
761 struct hlist_node *tmp, *t2;
762 struct nfqnl_instance *inst;
763 struct hlist_head *head = &instance_table[i];
764
765 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
b4b51029
EB
766 if ((n->net == &init_net) &&
767 (n->pid == inst->peer_pid))
7af4cc3f
HW
768 __instance_destroy(inst);
769 }
770 }
771 write_unlock_bh(&instances_lock);
772 }
773 return NOTIFY_DONE;
774}
775
776static struct notifier_block nfqnl_rtnl_notifier = {
777 .notifier_call = nfqnl_rcv_nl_event,
778};
779
df6fb868
PM
780static const int nfqa_verdict_min[NFQA_MAX+1] = {
781 [NFQA_VERDICT_HDR] = sizeof(struct nfqnl_msg_verdict_hdr),
782 [NFQA_MARK] = sizeof(u_int32_t),
783 [NFQA_PAYLOAD] = 0,
838ab636
HW
784};
785
7af4cc3f
HW
786static int
787nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
df6fb868 788 struct nlmsghdr *nlh, struct nlattr *nfqa[])
7af4cc3f
HW
789{
790 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
791 u_int16_t queue_num = ntohs(nfmsg->res_id);
792
793 struct nfqnl_msg_verdict_hdr *vhdr;
794 struct nfqnl_instance *queue;
795 unsigned int verdict;
796 struct nfqnl_queue_entry *entry;
838ab636 797 int err;
7af4cc3f 798
fdf70832 799 if (nlattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) {
838ab636
HW
800 QDEBUG("bad attribute size\n");
801 return -EINVAL;
802 }
803
804 queue = instance_lookup_get(queue_num);
7af4cc3f
HW
805 if (!queue)
806 return -ENODEV;
807
838ab636
HW
808 if (queue->peer_pid != NETLINK_CB(skb).pid) {
809 err = -EPERM;
810 goto err_out_put;
811 }
7af4cc3f 812
df6fb868 813 if (!nfqa[NFQA_VERDICT_HDR]) {
838ab636
HW
814 err = -EINVAL;
815 goto err_out_put;
816 }
7af4cc3f 817
df6fb868 818 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
7af4cc3f
HW
819 verdict = ntohl(vhdr->verdict);
820
838ab636
HW
821 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
822 err = -EINVAL;
823 goto err_out_put;
824 }
7af4cc3f
HW
825
826 entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
838ab636
HW
827 if (entry == NULL) {
828 err = -ENOENT;
829 goto err_out_put;
830 }
7af4cc3f 831
df6fb868
PM
832 if (nfqa[NFQA_PAYLOAD]) {
833 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
834 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
7af4cc3f
HW
835 verdict = NF_DROP;
836 }
837
df6fb868 838 if (nfqa[NFQA_MARK])
82e91ffe 839 entry->skb->mark = ntohl(*(__be32 *)
df6fb868 840 nla_data(nfqa[NFQA_MARK]));
601e68e1 841
7af4cc3f 842 issue_verdict(entry, verdict);
838ab636 843 instance_put(queue);
7af4cc3f 844 return 0;
838ab636
HW
845
846err_out_put:
847 instance_put(queue);
848 return err;
7af4cc3f
HW
849}
850
851static int
852nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
df6fb868 853 struct nlmsghdr *nlh, struct nlattr *nfqa[])
7af4cc3f
HW
854{
855 return -ENOTSUPP;
856}
857
df6fb868
PM
858static const int nfqa_cfg_min[NFQA_CFG_MAX+1] = {
859 [NFQA_CFG_CMD] = sizeof(struct nfqnl_msg_config_cmd),
860 [NFQA_CFG_PARAMS] = sizeof(struct nfqnl_msg_config_params),
838ab636
HW
861};
862
bbd86b9f
HW
863static struct nf_queue_handler nfqh = {
864 .name = "nf_queue",
865 .outfn = &nfqnl_enqueue_packet,
866};
867
7af4cc3f
HW
868static int
869nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
df6fb868 870 struct nlmsghdr *nlh, struct nlattr *nfqa[])
7af4cc3f
HW
871{
872 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
873 u_int16_t queue_num = ntohs(nfmsg->res_id);
874 struct nfqnl_instance *queue;
838ab636 875 int ret = 0;
7af4cc3f
HW
876
877 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
878
fdf70832 879 if (nlattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) {
838ab636
HW
880 QDEBUG("bad attribute size\n");
881 return -EINVAL;
882 }
883
884 queue = instance_lookup_get(queue_num);
df6fb868 885 if (nfqa[NFQA_CFG_CMD]) {
7af4cc3f 886 struct nfqnl_msg_config_cmd *cmd;
df6fb868 887 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
7af4cc3f
HW
888 QDEBUG("found CFG_CMD\n");
889
890 switch (cmd->command) {
891 case NFQNL_CFG_CMD_BIND:
892 if (queue)
893 return -EBUSY;
894
895 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
896 if (!queue)
897 return -EINVAL;
898 break;
899 case NFQNL_CFG_CMD_UNBIND:
900 if (!queue)
901 return -ENODEV;
902
838ab636
HW
903 if (queue->peer_pid != NETLINK_CB(skb).pid) {
904 ret = -EPERM;
905 goto out_put;
906 }
7af4cc3f
HW
907
908 instance_destroy(queue);
909 break;
910 case NFQNL_CFG_CMD_PF_BIND:
911 QDEBUG("registering queue handler for pf=%u\n",
912 ntohs(cmd->pf));
bbd86b9f 913 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
7af4cc3f
HW
914 break;
915 case NFQNL_CFG_CMD_PF_UNBIND:
916 QDEBUG("unregistering queue handler for pf=%u\n",
917 ntohs(cmd->pf));
ce7663d8 918 ret = nf_unregister_queue_handler(ntohs(cmd->pf), &nfqh);
7af4cc3f
HW
919 break;
920 default:
838ab636
HW
921 ret = -EINVAL;
922 break;
7af4cc3f
HW
923 }
924 } else {
925 if (!queue) {
926 QDEBUG("no config command, and no instance ENOENT\n");
838ab636
HW
927 ret = -ENOENT;
928 goto out_put;
7af4cc3f
HW
929 }
930
931 if (queue->peer_pid != NETLINK_CB(skb).pid) {
932 QDEBUG("no config command, and wrong pid\n");
838ab636
HW
933 ret = -EPERM;
934 goto out_put;
7af4cc3f
HW
935 }
936 }
937
df6fb868 938 if (nfqa[NFQA_CFG_PARAMS]) {
7af4cc3f 939 struct nfqnl_msg_config_params *params;
7af4cc3f 940
406dbfc9
PM
941 if (!queue) {
942 ret = -ENOENT;
943 goto out_put;
944 }
df6fb868 945 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
7af4cc3f
HW
946 nfqnl_set_mode(queue, params->copy_mode,
947 ntohl(params->copy_range));
948 }
949
df6fb868 950 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
829e17a1 951 __be32 *queue_maxlen;
df6fb868 952 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
829e17a1
EL
953 spin_lock_bh(&queue->lock);
954 queue->queue_maxlen = ntohl(*queue_maxlen);
955 spin_unlock_bh(&queue->lock);
956 }
957
838ab636
HW
958out_put:
959 instance_put(queue);
960 return ret;
7af4cc3f
HW
961}
962
7c8d4cb4 963static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
7af4cc3f 964 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
37d2e7a2 965 .attr_count = NFQA_MAX, },
7af4cc3f 966 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
37d2e7a2 967 .attr_count = NFQA_MAX, },
7af4cc3f 968 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
37d2e7a2 969 .attr_count = NFQA_CFG_MAX, },
7af4cc3f
HW
970};
971
7c8d4cb4 972static const struct nfnetlink_subsystem nfqnl_subsys = {
7af4cc3f
HW
973 .name = "nf_queue",
974 .subsys_id = NFNL_SUBSYS_QUEUE,
975 .cb_count = NFQNL_MSG_MAX,
7af4cc3f
HW
976 .cb = nfqnl_cb,
977};
978
838ab636
HW
979#ifdef CONFIG_PROC_FS
980struct iter_state {
981 unsigned int bucket;
982};
983
984static struct hlist_node *get_first(struct seq_file *seq)
985{
986 struct iter_state *st = seq->private;
987
988 if (!st)
989 return NULL;
990
991 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
992 if (!hlist_empty(&instance_table[st->bucket]))
993 return instance_table[st->bucket].first;
994 }
995 return NULL;
996}
997
998static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
999{
1000 struct iter_state *st = seq->private;
1001
1002 h = h->next;
1003 while (!h) {
1004 if (++st->bucket >= INSTANCE_BUCKETS)
1005 return NULL;
1006
1007 h = instance_table[st->bucket].first;
1008 }
1009 return h;
1010}
1011
1012static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1013{
1014 struct hlist_node *head;
1015 head = get_first(seq);
1016
1017 if (head)
1018 while (pos && (head = get_next(seq, head)))
1019 pos--;
1020 return pos ? NULL : head;
1021}
1022
1023static void *seq_start(struct seq_file *seq, loff_t *pos)
1024{
1025 read_lock_bh(&instances_lock);
1026 return get_idx(seq, *pos);
1027}
1028
1029static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1030{
1031 (*pos)++;
1032 return get_next(s, v);
1033}
1034
1035static void seq_stop(struct seq_file *s, void *v)
1036{
1037 read_unlock_bh(&instances_lock);
1038}
1039
1040static int seq_show(struct seq_file *s, void *v)
1041{
1042 const struct nfqnl_instance *inst = v;
1043
1044 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1045 inst->queue_num,
1046 inst->peer_pid, inst->queue_total,
1047 inst->copy_mode, inst->copy_range,
1048 inst->queue_dropped, inst->queue_user_dropped,
1049 atomic_read(&inst->id_sequence),
1050 atomic_read(&inst->use));
1051}
1052
56b3d975 1053static const struct seq_operations nfqnl_seq_ops = {
838ab636
HW
1054 .start = seq_start,
1055 .next = seq_next,
1056 .stop = seq_stop,
1057 .show = seq_show,
1058};
1059
1060static int nfqnl_open(struct inode *inode, struct file *file)
1061{
1062 struct seq_file *seq;
1063 struct iter_state *is;
1064 int ret;
1065
10dfdc69 1066 is = kzalloc(sizeof(*is), GFP_KERNEL);
838ab636
HW
1067 if (!is)
1068 return -ENOMEM;
838ab636
HW
1069 ret = seq_open(file, &nfqnl_seq_ops);
1070 if (ret < 0)
1071 goto out_free;
1072 seq = file->private_data;
1073 seq->private = is;
1074 return ret;
1075out_free:
1076 kfree(is);
1077 return ret;
1078}
1079
da7071d7 1080static const struct file_operations nfqnl_file_ops = {
838ab636
HW
1081 .owner = THIS_MODULE,
1082 .open = nfqnl_open,
1083 .read = seq_read,
1084 .llseek = seq_lseek,
1085 .release = seq_release_private,
1086};
1087
1088#endif /* PROC_FS */
1089
32292a7f 1090static int __init nfnetlink_queue_init(void)
7af4cc3f 1091{
838ab636
HW
1092 int i, status = -ENOMEM;
1093#ifdef CONFIG_PROC_FS
1094 struct proc_dir_entry *proc_nfqueue;
1095#endif
601e68e1 1096
838ab636
HW
1097 for (i = 0; i < INSTANCE_BUCKETS; i++)
1098 INIT_HLIST_HEAD(&instance_table[i]);
1099
7af4cc3f
HW
1100 netlink_register_notifier(&nfqnl_rtnl_notifier);
1101 status = nfnetlink_subsys_register(&nfqnl_subsys);
1102 if (status < 0) {
1103 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1104 goto cleanup_netlink_notifier;
1105 }
1106
838ab636
HW
1107#ifdef CONFIG_PROC_FS
1108 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1109 proc_net_netfilter);
1110 if (!proc_nfqueue)
1111 goto cleanup_subsys;
1112 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1113#endif
1114
7af4cc3f
HW
1115 register_netdevice_notifier(&nfqnl_dev_notifier);
1116 return status;
1117
838ab636
HW
1118#ifdef CONFIG_PROC_FS
1119cleanup_subsys:
7af4cc3f 1120 nfnetlink_subsys_unregister(&nfqnl_subsys);
32292a7f 1121#endif
7af4cc3f
HW
1122cleanup_netlink_notifier:
1123 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1124 return status;
1125}
1126
65b4b4e8 1127static void __exit nfnetlink_queue_fini(void)
7af4cc3f 1128{
32292a7f
PM
1129 nf_unregister_queue_handlers(&nfqh);
1130 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1131#ifdef CONFIG_PROC_FS
1132 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1133#endif
1134 nfnetlink_subsys_unregister(&nfqnl_subsys);
1135 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
7af4cc3f
HW
1136}
1137
1138MODULE_DESCRIPTION("netfilter packet queue handler");
1139MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1140MODULE_LICENSE("GPL");
1141MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1142
65b4b4e8
AM
1143module_init(nfnetlink_queue_init);
1144module_exit(nfnetlink_queue_fini);