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