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