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