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