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