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