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