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