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