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