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