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