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