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