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