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