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