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