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