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