ipv6: Set skb->protocol properly for local output
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / net / netfilter / nfnetlink_queue.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>
b7bd1809 31#include <linux/netfilter/nf_conntrack_common.h>
7af4cc3f
HW
32#include <linux/list.h>
33#include <net/sock.h>
83111e7f 34#include <net/tcp_states.h>
c01cd429 35#include <net/netfilter/nf_queue.h>
e8179610 36#include <net/netns/generic.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
cc6bc448 58 u32 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 *
cc6bc448 113instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 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
ef493bd9
RK
281static u32 nfqnl_get_sk_secctx(struct sk_buff *skb, char **secdata)
282{
283 u32 seclen = 0;
284#if IS_ENABLED(CONFIG_NETWORK_SECMARK)
285 if (!skb || !sk_fullsock(skb->sk))
286 return 0;
287
288 read_lock_bh(&skb->sk->sk_callback_lock);
289
290 if (skb->secmark)
291 security_secid_to_secctx(skb->secmark, secdata, &seclen);
292
293 read_unlock_bh(&skb->sk->sk_callback_lock);
294#endif
295 return seclen;
296}
297
7af4cc3f 298static struct sk_buff *
74332687 299nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
5863702a
ED
300 struct nf_queue_entry *entry,
301 __be32 **packet_id_ptr)
7af4cc3f 302{
7af4cc3f 303 size_t size;
6bb0fef4 304 size_t data_len = 0, cap_len = 0, rem_len = 0;
af2806f8 305 unsigned int hlen = 0;
7af4cc3f 306 struct sk_buff *skb;
5863702a
ED
307 struct nlattr *nla;
308 struct nfqnl_msg_packet_hdr *pmsg;
7af4cc3f
HW
309 struct nlmsghdr *nlh;
310 struct nfgenmsg *nfmsg;
3e4ead4f
JJ
311 struct sk_buff *entskb = entry->skb;
312 struct net_device *indev;
313 struct net_device *outdev;
9cb01766
PNA
314 struct nf_conn *ct = NULL;
315 enum ip_conntrack_info uninitialized_var(ctinfo);
a4b4766c 316 struct nfnl_ct_hook *nfnl_ct;
496e4ae7 317 bool csum_verify;
ef493bd9
RK
318 char *secdata = NULL;
319 u32 seclen = 0;
7af4cc3f 320
573ce260 321 size = nlmsg_total_size(sizeof(struct nfgenmsg))
df6fb868
PM
322 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
323 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
324 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
1109a90c 325#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
df6fb868
PM
326 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
327 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
fbcd923c 328#endif
df6fb868
PM
329 + nla_total_size(sizeof(u_int32_t)) /* mark */
330 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
7237190d 331 + nla_total_size(sizeof(u_int32_t)) /* skbinfo */
ae08ce00
ED
332 + nla_total_size(sizeof(u_int32_t)); /* cap_len */
333
334 if (entskb->tstamp.tv64)
335 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
7af4cc3f 336
1d1de89b
DM
337 if (entry->state.hook <= NF_INET_FORWARD ||
338 (entry->state.hook == NF_INET_POST_ROUTING && entskb->sk == NULL))
496e4ae7
FW
339 csum_verify = !skb_csum_unnecessary(entskb);
340 else
341 csum_verify = false;
342
1d1de89b 343 outdev = entry->state.out;
3e4ead4f 344
c463ac97 345 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
7af4cc3f
HW
346 case NFQNL_COPY_META:
347 case NFQNL_COPY_NONE:
7af4cc3f 348 break;
601e68e1 349
7af4cc3f 350 case NFQNL_COPY_PACKET:
00bd1cc2
FW
351 if (!(queue->flags & NFQA_CFG_F_GSO) &&
352 entskb->ip_summed == CHECKSUM_PARTIAL &&
c463ac97 353 skb_checksum_help(entskb))
e7dfb09a 354 return NULL;
c463ac97
ED
355
356 data_len = ACCESS_ONCE(queue->copy_range);
9cefbbc9 357 if (data_len > entskb->len)
3e4ead4f 358 data_len = entskb->len;
601e68e1 359
af2806f8
TG
360 hlen = skb_zerocopy_headlen(entskb);
361 hlen = min_t(unsigned int, hlen, data_len);
ae08ce00 362 size += sizeof(struct nlattr) + hlen;
6ee584be 363 cap_len = entskb->len;
6bb0fef4 364 rem_len = data_len - hlen;
7af4cc3f 365 break;
7af4cc3f
HW
366 }
367
8e662164
AB
368 nfnl_ct = rcu_dereference(nfnl_ct_hook);
369
b7bd1809 370 if (queue->flags & NFQA_CFG_F_CONNTRACK) {
a4b4766c
KM
371 if (nfnl_ct != NULL) {
372 ct = nfnl_ct->get_ct(entskb, &ctinfo);
b7bd1809 373 if (ct != NULL)
a4b4766c 374 size += nfnl_ct->build_size(ct);
b7bd1809
PNA
375 }
376 }
7af4cc3f 377
08c0cad6
VG
378 if (queue->flags & NFQA_CFG_F_UID_GID) {
379 size += (nla_total_size(sizeof(u_int32_t)) /* uid */
380 + nla_total_size(sizeof(u_int32_t))); /* gid */
381 }
382
ef493bd9
RK
383 if ((queue->flags & NFQA_CFG_F_SECCTX) && entskb->sk) {
384 seclen = nfqnl_get_sk_secctx(entskb, &secdata);
385 if (seclen)
386 size += nla_total_size(seclen);
387 }
388
6bb0fef4 389 skb = __netlink_alloc_skb(net->nfnl, size, rem_len, queue->peer_portid,
3ab1f683 390 GFP_ATOMIC);
36d5fe6a
ZK
391 if (!skb) {
392 skb_tx_error(entskb);
3da07c0c 393 return NULL;
36d5fe6a 394 }
601e68e1 395
3da07c0c 396 nlh = nlmsg_put(skb, 0, 0,
7af4cc3f 397 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
3da07c0c
DM
398 sizeof(struct nfgenmsg), 0);
399 if (!nlh) {
36d5fe6a 400 skb_tx_error(entskb);
3da07c0c
DM
401 kfree_skb(skb);
402 return NULL;
403 }
404 nfmsg = nlmsg_data(nlh);
1d1de89b 405 nfmsg->nfgen_family = entry->state.pf;
7af4cc3f
HW
406 nfmsg->version = NFNETLINK_V0;
407 nfmsg->res_id = htons(queue->queue_num);
408
5863702a
ED
409 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
410 pmsg = nla_data(nla);
411 pmsg->hw_protocol = entskb->protocol;
1d1de89b 412 pmsg->hook = entry->state.hook;
5863702a 413 *packet_id_ptr = &pmsg->packet_id;
7af4cc3f 414
1d1de89b 415 indev = entry->state.in;
3e4ead4f 416 if (indev) {
1109a90c 417#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
a447189e
DM
418 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
419 goto nla_put_failure;
fbcd923c 420#else
1d1de89b 421 if (entry->state.pf == PF_BRIDGE) {
fbcd923c 422 /* Case 1: indev is physical input device, we need to
601e68e1 423 * look for bridge group (when called from
fbcd923c 424 * netfilter_bridge) */
a447189e
DM
425 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
426 htonl(indev->ifindex)) ||
fbcd923c 427 /* this is the bridge group "brX" */
f350a0a8 428 /* rcu_read_lock()ed by __nf_queue */
a447189e
DM
429 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
430 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
431 goto nla_put_failure;
fbcd923c 432 } else {
c737b7c4
FW
433 int physinif;
434
fbcd923c
HW
435 /* Case 2: indev is bridge group, we need to look for
436 * physical device (when called from ipv4) */
a447189e
DM
437 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
438 htonl(indev->ifindex)))
439 goto nla_put_failure;
c737b7c4
FW
440
441 physinif = nf_bridge_get_physinif(entskb);
442 if (physinif &&
a447189e 443 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
c737b7c4 444 htonl(physinif)))
a447189e 445 goto nla_put_failure;
fbcd923c
HW
446 }
447#endif
7af4cc3f
HW
448 }
449
3e4ead4f 450 if (outdev) {
1109a90c 451#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
a447189e
DM
452 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
453 goto nla_put_failure;
fbcd923c 454#else
1d1de89b 455 if (entry->state.pf == PF_BRIDGE) {
fbcd923c 456 /* Case 1: outdev is physical output device, we need to
601e68e1 457 * look for bridge group (when called from
fbcd923c 458 * netfilter_bridge) */
a447189e
DM
459 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
460 htonl(outdev->ifindex)) ||
fbcd923c 461 /* this is the bridge group "brX" */
f350a0a8 462 /* rcu_read_lock()ed by __nf_queue */
a447189e
DM
463 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
464 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
465 goto nla_put_failure;
fbcd923c 466 } else {
c737b7c4
FW
467 int physoutif;
468
fbcd923c
HW
469 /* Case 2: outdev is bridge group, we need to look for
470 * physical output device (when called from ipv4) */
a447189e
DM
471 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
472 htonl(outdev->ifindex)))
473 goto nla_put_failure;
c737b7c4
FW
474
475 physoutif = nf_bridge_get_physoutif(entskb);
476 if (physoutif &&
a447189e 477 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
c737b7c4 478 htonl(physoutif)))
a447189e 479 goto nla_put_failure;
fbcd923c
HW
480 }
481#endif
7af4cc3f
HW
482 }
483
a447189e
DM
484 if (entskb->mark &&
485 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
486 goto nla_put_failure;
7af4cc3f 487
2c38de4c
NC
488 if (indev && entskb->dev &&
489 entskb->mac_header != entskb->network_header) {
7af4cc3f 490 struct nfqnl_msg_packet_hw phw;
e4d091d7
DC
491 int len;
492
493 memset(&phw, 0, sizeof(phw));
494 len = dev_parse_header(entskb, phw.hw_addr);
b95cce35
SH
495 if (len) {
496 phw.hw_addrlen = htons(len);
a447189e
DM
497 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
498 goto nla_put_failure;
b95cce35 499 }
7af4cc3f
HW
500 }
501
b7aa0bf7 502 if (entskb->tstamp.tv64) {
7af4cc3f 503 struct nfqnl_msg_packet_timestamp ts;
b28b1e82
PNA
504 struct timespec64 kts = ktime_to_timespec64(skb->tstamp);
505
506 ts.sec = cpu_to_be64(kts.tv_sec);
507 ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
7af4cc3f 508
a447189e
DM
509 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
510 goto nla_put_failure;
7af4cc3f
HW
511 }
512
08c0cad6
VG
513 if ((queue->flags & NFQA_CFG_F_UID_GID) && entskb->sk &&
514 nfqnl_put_sk_uidgid(skb, entskb->sk) < 0)
515 goto nla_put_failure;
516
ef493bd9
RK
517 if (seclen && nla_put(skb, NFQA_SECCTX, seclen, secdata))
518 goto nla_put_failure;
519
a4b4766c 520 if (ct && nfnl_ct->build(skb, ct, ctinfo, NFQA_CT, NFQA_CT_INFO) < 0)
ae08ce00
ED
521 goto nla_put_failure;
522
7f87712c
FW
523 if (cap_len > data_len &&
524 nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
ae08ce00
ED
525 goto nla_put_failure;
526
496e4ae7 527 if (nfqnl_put_packet_info(skb, entskb, csum_verify))
7237190d
FW
528 goto nla_put_failure;
529
7af4cc3f 530 if (data_len) {
df6fb868 531 struct nlattr *nla;
7af4cc3f 532
ae08ce00
ED
533 if (skb_tailroom(skb) < sizeof(*nla) + hlen)
534 goto nla_put_failure;
7af4cc3f 535
ae08ce00 536 nla = (struct nlattr *)skb_put(skb, sizeof(*nla));
df6fb868 537 nla->nla_type = NFQA_PAYLOAD;
ae08ce00 538 nla->nla_len = nla_attr_size(data_len);
7af4cc3f 539
36d5fe6a
ZK
540 if (skb_zerocopy(skb, entskb, data_len, hlen))
541 goto nla_put_failure;
7af4cc3f 542 }
601e68e1 543
ae08ce00 544 nlh->nlmsg_len = skb->len;
7af4cc3f
HW
545 return skb;
546
df6fb868 547nla_put_failure:
36d5fe6a 548 skb_tx_error(entskb);
a6729955 549 kfree_skb(skb);
e87cc472 550 net_err_ratelimited("nf_queue: error creating packet message\n");
7af4cc3f
HW
551 return NULL;
552}
553
554static int
a5fedd43
FW
555__nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
556 struct nf_queue_entry *entry)
7af4cc3f 557{
7af4cc3f 558 struct sk_buff *nskb;
f1585086 559 int err = -ENOBUFS;
5863702a 560 __be32 *packet_id_ptr;
fdb694a0 561 int failopen = 0;
7af4cc3f 562
74332687 563 nskb = nfqnl_build_packet_message(net, queue, entry, &packet_id_ptr);
f1585086
FW
564 if (nskb == NULL) {
565 err = -ENOMEM;
0ef0f465 566 goto err_out;
f1585086 567 }
7af4cc3f 568 spin_lock_bh(&queue->lock);
601e68e1 569
7af4cc3f 570 if (queue->queue_total >= queue->queue_maxlen) {
fdb694a0
KK
571 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
572 failopen = 1;
573 err = 0;
574 } else {
575 queue->queue_dropped++;
576 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
577 queue->queue_total);
578 }
7af4cc3f
HW
579 goto err_out_free_nskb;
580 }
5863702a
ED
581 entry->id = ++queue->id_sequence;
582 *packet_id_ptr = htonl(entry->id);
7af4cc3f
HW
583
584 /* nfnetlink_unicast will either free the nskb or add it to a socket */
e8179610 585 err = nfnetlink_unicast(nskb, net, queue->peer_portid, MSG_DONTWAIT);
0ef0f465 586 if (err < 0) {
601e68e1 587 queue->queue_user_dropped++;
7af4cc3f
HW
588 goto err_out_unlock;
589 }
590
591 __enqueue_entry(queue, entry);
592
593 spin_unlock_bh(&queue->lock);
0ef0f465 594 return 0;
7af4cc3f
HW
595
596err_out_free_nskb:
601e68e1 597 kfree_skb(nskb);
7af4cc3f
HW
598err_out_unlock:
599 spin_unlock_bh(&queue->lock);
fdb694a0
KK
600 if (failopen)
601 nf_reinject(entry, NF_ACCEPT);
0ef0f465 602err_out:
f1585086 603 return err;
7af4cc3f
HW
604}
605
a5fedd43
FW
606static struct nf_queue_entry *
607nf_queue_entry_dup(struct nf_queue_entry *e)
608{
609 struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
ed78d09d
FW
610 if (entry)
611 nf_queue_entry_get_refs(entry);
612 return entry;
a5fedd43
FW
613}
614
1109a90c 615#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
a5fedd43
FW
616/* When called from bridge netfilter, skb->data must point to MAC header
617 * before calling skb_gso_segment(). Else, original MAC header is lost
618 * and segmented skbs will be sent to wrong destination.
619 */
620static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
621{
622 if (skb->nf_bridge)
623 __skb_push(skb, skb->network_header - skb->mac_header);
624}
625
626static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
627{
628 if (skb->nf_bridge)
629 __skb_pull(skb, skb->network_header - skb->mac_header);
630}
631#else
632#define nf_bridge_adjust_skb_data(s) do {} while (0)
633#define nf_bridge_adjust_segmented_data(s) do {} while (0)
634#endif
635
636static void free_entry(struct nf_queue_entry *entry)
637{
638 nf_queue_entry_release_refs(entry);
639 kfree(entry);
640}
641
642static int
643__nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue,
644 struct sk_buff *skb, struct nf_queue_entry *entry)
645{
646 int ret = -ENOMEM;
647 struct nf_queue_entry *entry_seg;
648
649 nf_bridge_adjust_segmented_data(skb);
650
651 if (skb->next == NULL) { /* last packet, no need to copy entry */
652 struct sk_buff *gso_skb = entry->skb;
653 entry->skb = skb;
654 ret = __nfqnl_enqueue_packet(net, queue, entry);
655 if (ret)
656 entry->skb = gso_skb;
657 return ret;
658 }
659
660 skb->next = NULL;
661
662 entry_seg = nf_queue_entry_dup(entry);
663 if (entry_seg) {
664 entry_seg->skb = skb;
665 ret = __nfqnl_enqueue_packet(net, queue, entry_seg);
666 if (ret)
667 free_entry(entry_seg);
668 }
669 return ret;
670}
671
672static int
673nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
674{
675 unsigned int queued;
676 struct nfqnl_instance *queue;
677 struct sk_buff *skb, *segs;
678 int err = -ENOBUFS;
9dff2c96 679 struct net *net = entry->state.net;
a5fedd43
FW
680 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
681
682 /* rcu_read_lock()ed by nf_hook_slow() */
683 queue = instance_lookup(q, queuenum);
684 if (!queue)
685 return -ESRCH;
686
687 if (queue->copy_mode == NFQNL_COPY_NONE)
688 return -EINVAL;
689
a5fedd43
FW
690 skb = entry->skb;
691
1d1de89b 692 switch (entry->state.pf) {
a5fedd43
FW
693 case NFPROTO_IPV4:
694 skb->protocol = htons(ETH_P_IP);
695 break;
696 case NFPROTO_IPV6:
697 skb->protocol = htons(ETH_P_IPV6);
698 break;
699 }
700
7b8dfe28
PNA
701 if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(skb))
702 return __nfqnl_enqueue_packet(net, queue, entry);
703
a5fedd43
FW
704 nf_bridge_adjust_skb_data(skb);
705 segs = skb_gso_segment(skb, 0);
706 /* Does not use PTR_ERR to limit the number of error codes that can be
ed78d09d 707 * returned by nf_queue. For instance, callers rely on -ESRCH to
a5fedd43
FW
708 * mean 'ignore this hook'.
709 */
330966e5 710 if (IS_ERR_OR_NULL(segs))
a5fedd43
FW
711 goto out_err;
712 queued = 0;
713 err = 0;
714 do {
715 struct sk_buff *nskb = segs->next;
716 if (err == 0)
717 err = __nfqnl_enqueue_packet_gso(net, queue,
718 segs, entry);
719 if (err == 0)
720 queued++;
721 else
722 kfree_skb(segs);
723 segs = nskb;
724 } while (segs);
725
726 if (queued) {
727 if (err) /* some segments are already queued */
728 free_entry(entry);
729 kfree_skb(skb);
730 return 0;
731 }
732 out_err:
733 nf_bridge_adjust_segmented_data(skb);
734 return err;
735}
736
7af4cc3f 737static int
8c88f87c 738nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
7af4cc3f 739{
e2b58a67 740 struct sk_buff *nskb;
7af4cc3f 741
d8a585d7
PM
742 if (diff < 0) {
743 if (pskb_trim(e->skb, data_len))
744 return -ENOMEM;
745 } else if (diff > 0) {
7af4cc3f
HW
746 if (data_len > 0xFFFF)
747 return -EINVAL;
748 if (diff > skb_tailroom(e->skb)) {
9a732ed6
AE
749 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
750 diff, GFP_ATOMIC);
e2b58a67 751 if (!nskb) {
1158ba27 752 printk(KERN_WARNING "nf_queue: OOM "
7af4cc3f 753 "in mangle, dropping packet\n");
e2b58a67 754 return -ENOMEM;
7af4cc3f 755 }
e2b58a67
PM
756 kfree_skb(e->skb);
757 e->skb = nskb;
7af4cc3f
HW
758 }
759 skb_put(e->skb, diff);
760 }
37d41879 761 if (!skb_make_writable(e->skb, data_len))
7af4cc3f 762 return -ENOMEM;
27d7ff46 763 skb_copy_to_linear_data(e->skb, data, data_len);
e7dfb09a 764 e->skb->ip_summed = CHECKSUM_NONE;
7af4cc3f
HW
765 return 0;
766}
767
7af4cc3f
HW
768static int
769nfqnl_set_mode(struct nfqnl_instance *queue,
770 unsigned char mode, unsigned int range)
771{
c5de0dfd 772 int status = 0;
7af4cc3f
HW
773
774 spin_lock_bh(&queue->lock);
c5de0dfd
PM
775 switch (mode) {
776 case NFQNL_COPY_NONE:
777 case NFQNL_COPY_META:
778 queue->copy_mode = mode;
779 queue->copy_range = 0;
780 break;
781
782 case NFQNL_COPY_PACKET:
783 queue->copy_mode = mode;
9cefbbc9
FW
784 if (range == 0 || range > NFQNL_MAX_COPY_RANGE)
785 queue->copy_range = NFQNL_MAX_COPY_RANGE;
c5de0dfd
PM
786 else
787 queue->copy_range = range;
788 break;
789
790 default:
791 status = -EINVAL;
792
793 }
7af4cc3f
HW
794 spin_unlock_bh(&queue->lock);
795
796 return status;
797}
798
799static int
02f014d8 800dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
7af4cc3f 801{
1d1de89b
DM
802 if (entry->state.in)
803 if (entry->state.in->ifindex == ifindex)
7af4cc3f 804 return 1;
1d1de89b
DM
805 if (entry->state.out)
806 if (entry->state.out->ifindex == ifindex)
7af4cc3f 807 return 1;
1109a90c 808#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
ef47c6a7 809 if (entry->skb->nf_bridge) {
c737b7c4
FW
810 int physinif, physoutif;
811
812 physinif = nf_bridge_get_physinif(entry->skb);
813 physoutif = nf_bridge_get_physoutif(entry->skb);
814
815 if (physinif == ifindex || physoutif == ifindex)
ef47c6a7
PM
816 return 1;
817 }
818#endif
7af4cc3f
HW
819 return 0;
820}
821
822/* drop all packets with either indev or outdev == ifindex from all queue
823 * instances */
824static void
e8179610 825nfqnl_dev_drop(struct net *net, int ifindex)
7af4cc3f
HW
826{
827 int i;
e8179610 828 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
601e68e1 829
9872bec7 830 rcu_read_lock();
7af4cc3f 831
9872bec7 832 for (i = 0; i < INSTANCE_BUCKETS; i++) {
7af4cc3f 833 struct nfqnl_instance *inst;
e8179610 834 struct hlist_head *head = &q->instance_table[i];
7af4cc3f 835
b67bfe0d 836 hlist_for_each_entry_rcu(inst, head, hlist)
b43d8d85 837 nfqnl_flush(inst, dev_cmp, ifindex);
7af4cc3f
HW
838 }
839
9872bec7 840 rcu_read_unlock();
7af4cc3f
HW
841}
842
7af4cc3f
HW
843static int
844nfqnl_rcv_dev_event(struct notifier_block *this,
845 unsigned long event, void *ptr)
846{
351638e7 847 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
7af4cc3f
HW
848
849 /* Drop any packets associated with the downed device */
850 if (event == NETDEV_DOWN)
e8179610 851 nfqnl_dev_drop(dev_net(dev), dev->ifindex);
7af4cc3f
HW
852 return NOTIFY_DONE;
853}
854
855static struct notifier_block nfqnl_dev_notifier = {
856 .notifier_call = nfqnl_rcv_dev_event,
857};
858
8405a8ff
EB
859static int nf_hook_cmp(struct nf_queue_entry *entry, unsigned long ops_ptr)
860{
861 return entry->elem == (struct nf_hook_ops *)ops_ptr;
862}
863
864static void nfqnl_nf_hook_drop(struct net *net, struct nf_hook_ops *hook)
865{
866 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
867 int i;
868
869 rcu_read_lock();
870 for (i = 0; i < INSTANCE_BUCKETS; i++) {
871 struct nfqnl_instance *inst;
872 struct hlist_head *head = &q->instance_table[i];
873
874 hlist_for_each_entry_rcu(inst, head, hlist)
875 nfqnl_flush(inst, nf_hook_cmp, (unsigned long)hook);
876 }
877 rcu_read_unlock();
878}
879
7af4cc3f
HW
880static int
881nfqnl_rcv_nl_event(struct notifier_block *this,
882 unsigned long event, void *ptr)
883{
884 struct netlink_notify *n = ptr;
e8179610 885 struct nfnl_queue_net *q = nfnl_queue_pernet(n->net);
7af4cc3f 886
dee5817e 887 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
7af4cc3f
HW
888 int i;
889
15e47304 890 /* destroy all instances for this portid */
e8179610 891 spin_lock(&q->instances_lock);
9872bec7 892 for (i = 0; i < INSTANCE_BUCKETS; i++) {
b67bfe0d 893 struct hlist_node *t2;
7af4cc3f 894 struct nfqnl_instance *inst;
e8179610 895 struct hlist_head *head = &q->instance_table[i];
7af4cc3f 896
b67bfe0d 897 hlist_for_each_entry_safe(inst, t2, head, hlist) {
e8179610 898 if (n->portid == inst->peer_portid)
7af4cc3f
HW
899 __instance_destroy(inst);
900 }
901 }
e8179610 902 spin_unlock(&q->instances_lock);
7af4cc3f
HW
903 }
904 return NOTIFY_DONE;
905}
906
907static struct notifier_block nfqnl_rtnl_notifier = {
908 .notifier_call = nfqnl_rcv_nl_event,
909};
910
5bf75853
PM
911static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
912 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
913 [NFQA_MARK] = { .type = NLA_U32 },
914 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
9cb01766 915 [NFQA_CT] = { .type = NLA_UNSPEC },
bd077937 916 [NFQA_EXP] = { .type = NLA_UNSPEC },
838ab636
HW
917};
918
97d32cf9
FW
919static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
920 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
921 [NFQA_MARK] = { .type = NLA_U32 },
922};
923
e8179610 924static struct nfqnl_instance *
cc6bc448 925verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, u32 nlportid)
97d32cf9
FW
926{
927 struct nfqnl_instance *queue;
928
e8179610 929 queue = instance_lookup(q, queue_num);
97d32cf9
FW
930 if (!queue)
931 return ERR_PTR(-ENODEV);
932
15e47304 933 if (queue->peer_portid != nlportid)
97d32cf9
FW
934 return ERR_PTR(-EPERM);
935
936 return queue;
937}
938
939static struct nfqnl_msg_verdict_hdr*
940verdicthdr_get(const struct nlattr * const nfqa[])
941{
942 struct nfqnl_msg_verdict_hdr *vhdr;
943 unsigned int verdict;
944
945 if (!nfqa[NFQA_VERDICT_HDR])
946 return NULL;
947
948 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
c6675233
FW
949 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
950 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
97d32cf9
FW
951 return NULL;
952 return vhdr;
953}
954
955static int nfq_id_after(unsigned int id, unsigned int max)
956{
957 return (int)(id - max) > 0;
958}
959
960static int
961nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
962 const struct nlmsghdr *nlh,
963 const struct nlattr * const nfqa[])
964{
3da07c0c 965 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
97d32cf9
FW
966 struct nf_queue_entry *entry, *tmp;
967 unsigned int verdict, maxid;
968 struct nfqnl_msg_verdict_hdr *vhdr;
969 struct nfqnl_instance *queue;
970 LIST_HEAD(batch_list);
971 u16 queue_num = ntohs(nfmsg->res_id);
972
e8179610
G
973 struct net *net = sock_net(ctnl);
974 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
975
976 queue = verdict_instance_lookup(q, queue_num,
977 NETLINK_CB(skb).portid);
97d32cf9
FW
978 if (IS_ERR(queue))
979 return PTR_ERR(queue);
980
981 vhdr = verdicthdr_get(nfqa);
982 if (!vhdr)
983 return -EINVAL;
984
985 verdict = ntohl(vhdr->verdict);
986 maxid = ntohl(vhdr->id);
987
988 spin_lock_bh(&queue->lock);
989
990 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
991 if (nfq_id_after(entry->id, maxid))
992 break;
993 __dequeue_entry(queue, entry);
994 list_add_tail(&entry->list, &batch_list);
995 }
996
997 spin_unlock_bh(&queue->lock);
998
999 if (list_empty(&batch_list))
1000 return -ENOENT;
1001
1002 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
1003 if (nfqa[NFQA_MARK])
1004 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
1005 nf_reinject(entry, verdict);
1006 }
1007 return 0;
1008}
1009
a4b4766c 1010static struct nf_conn *nfqnl_ct_parse(struct nfnl_ct_hook *nfnl_ct,
b7bd1809
PNA
1011 const struct nlmsghdr *nlh,
1012 const struct nlattr * const nfqa[],
1013 struct nf_queue_entry *entry,
1014 enum ip_conntrack_info *ctinfo)
1015{
1016 struct nf_conn *ct;
1017
a4b4766c 1018 ct = nfnl_ct->get_ct(entry->skb, ctinfo);
b7bd1809
PNA
1019 if (ct == NULL)
1020 return NULL;
1021
a4b4766c 1022 if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0)
b7bd1809
PNA
1023 return NULL;
1024
1025 if (nfqa[NFQA_EXP])
a4b4766c 1026 nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct,
b7bd1809
PNA
1027 NETLINK_CB(entry->skb).portid,
1028 nlmsg_report(nlh));
1029 return ct;
1030}
1031
7af4cc3f
HW
1032static int
1033nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1034 const struct nlmsghdr *nlh,
1035 const struct nlattr * const nfqa[])
7af4cc3f 1036{
3da07c0c 1037 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7af4cc3f
HW
1038 u_int16_t queue_num = ntohs(nfmsg->res_id);
1039
1040 struct nfqnl_msg_verdict_hdr *vhdr;
1041 struct nfqnl_instance *queue;
1042 unsigned int verdict;
02f014d8 1043 struct nf_queue_entry *entry;
8c88f87c 1044 enum ip_conntrack_info uninitialized_var(ctinfo);
a4b4766c 1045 struct nfnl_ct_hook *nfnl_ct;
8c88f87c 1046 struct nf_conn *ct = NULL;
7af4cc3f 1047
e8179610
G
1048 struct net *net = sock_net(ctnl);
1049 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
7af4cc3f 1050
e8179610
G
1051 queue = instance_lookup(q, queue_num);
1052 if (!queue)
1053 queue = verdict_instance_lookup(q, queue_num,
1054 NETLINK_CB(skb).portid);
97d32cf9
FW
1055 if (IS_ERR(queue))
1056 return PTR_ERR(queue);
7af4cc3f 1057
97d32cf9
FW
1058 vhdr = verdicthdr_get(nfqa);
1059 if (!vhdr)
84a797dd 1060 return -EINVAL;
7af4cc3f 1061
7af4cc3f
HW
1062 verdict = ntohl(vhdr->verdict);
1063
b43d8d85 1064 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
84a797dd
ED
1065 if (entry == NULL)
1066 return -ENOENT;
7af4cc3f 1067
8e662164
AB
1068 /* rcu lock already held from nfnl->call_rcu. */
1069 nfnl_ct = rcu_dereference(nfnl_ct_hook);
1070
bd077937 1071 if (nfqa[NFQA_CT]) {
a4b4766c
KM
1072 if (nfnl_ct != NULL)
1073 ct = nfqnl_ct_parse(nfnl_ct, nlh, nfqa, entry, &ctinfo);
bd077937 1074 }
9cb01766 1075
df6fb868 1076 if (nfqa[NFQA_PAYLOAD]) {
8c88f87c
PNA
1077 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
1078 int diff = payload_len - entry->skb->len;
1079
df6fb868 1080 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
8c88f87c 1081 payload_len, entry, diff) < 0)
7af4cc3f 1082 verdict = NF_DROP;
8c88f87c 1083
b7bd1809 1084 if (ct && diff)
a4b4766c 1085 nfnl_ct->seq_adjust(entry->skb, ct, ctinfo, diff);
7af4cc3f
HW
1086 }
1087
df6fb868 1088 if (nfqa[NFQA_MARK])
ea3a66ff 1089 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
601e68e1 1090
4b3d15ef 1091 nf_reinject(entry, verdict);
7af4cc3f
HW
1092 return 0;
1093}
1094
1095static int
1096nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1097 const struct nlmsghdr *nlh,
1098 const struct nlattr * const nfqa[])
7af4cc3f
HW
1099{
1100 return -ENOTSUPP;
1101}
1102
5bf75853
PM
1103static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
1104 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
1105 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
838ab636
HW
1106};
1107
e3ac5298 1108static const struct nf_queue_handler nfqh = {
8405a8ff
EB
1109 .outfn = &nfqnl_enqueue_packet,
1110 .nf_hook_drop = &nfqnl_nf_hook_drop,
bbd86b9f
HW
1111};
1112
7af4cc3f
HW
1113static int
1114nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1115 const struct nlmsghdr *nlh,
1116 const struct nlattr * const nfqa[])
7af4cc3f 1117{
3da07c0c 1118 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7af4cc3f
HW
1119 u_int16_t queue_num = ntohs(nfmsg->res_id);
1120 struct nfqnl_instance *queue;
9872bec7 1121 struct nfqnl_msg_config_cmd *cmd = NULL;
e8179610
G
1122 struct net *net = sock_net(ctnl);
1123 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
838ab636 1124 int ret = 0;
7af4cc3f 1125
9872bec7
PM
1126 if (nfqa[NFQA_CFG_CMD]) {
1127 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
1128
0360ae41 1129 /* Obsolete commands without queue context */
9872bec7 1130 switch (cmd->command) {
0360ae41
FW
1131 case NFQNL_CFG_CMD_PF_BIND: return 0;
1132 case NFQNL_CFG_CMD_PF_UNBIND: return 0;
9872bec7 1133 }
9872bec7
PM
1134 }
1135
1136 rcu_read_lock();
e8179610 1137 queue = instance_lookup(q, queue_num);
15e47304 1138 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
a3c8e7fd 1139 ret = -EPERM;
9872bec7 1140 goto err_out_unlock;
a3c8e7fd
PM
1141 }
1142
9872bec7 1143 if (cmd != NULL) {
7af4cc3f
HW
1144 switch (cmd->command) {
1145 case NFQNL_CFG_CMD_BIND:
9872bec7
PM
1146 if (queue) {
1147 ret = -EBUSY;
1148 goto err_out_unlock;
1149 }
e8179610
G
1150 queue = instance_create(q, queue_num,
1151 NETLINK_CB(skb).portid);
baab2ce7
PM
1152 if (IS_ERR(queue)) {
1153 ret = PTR_ERR(queue);
9872bec7
PM
1154 goto err_out_unlock;
1155 }
7af4cc3f
HW
1156 break;
1157 case NFQNL_CFG_CMD_UNBIND:
9872bec7
PM
1158 if (!queue) {
1159 ret = -ENODEV;
1160 goto err_out_unlock;
1161 }
e8179610 1162 instance_destroy(q, queue);
7af4cc3f
HW
1163 break;
1164 case NFQNL_CFG_CMD_PF_BIND:
7af4cc3f 1165 case NFQNL_CFG_CMD_PF_UNBIND:
7af4cc3f
HW
1166 break;
1167 default:
cd21f0ac 1168 ret = -ENOTSUPP;
838ab636 1169 break;
7af4cc3f 1170 }
7af4cc3f
HW
1171 }
1172
df6fb868 1173 if (nfqa[NFQA_CFG_PARAMS]) {
7af4cc3f 1174 struct nfqnl_msg_config_params *params;
7af4cc3f 1175
406dbfc9 1176 if (!queue) {
a3c8e7fd 1177 ret = -ENODEV;
9872bec7 1178 goto err_out_unlock;
406dbfc9 1179 }
df6fb868 1180 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
7af4cc3f
HW
1181 nfqnl_set_mode(queue, params->copy_mode,
1182 ntohl(params->copy_range));
1183 }
1184
df6fb868 1185 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
829e17a1 1186 __be32 *queue_maxlen;
a3c8e7fd
PM
1187
1188 if (!queue) {
1189 ret = -ENODEV;
9872bec7 1190 goto err_out_unlock;
a3c8e7fd 1191 }
df6fb868 1192 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
829e17a1
EL
1193 spin_lock_bh(&queue->lock);
1194 queue->queue_maxlen = ntohl(*queue_maxlen);
1195 spin_unlock_bh(&queue->lock);
1196 }
1197
fdb694a0
KK
1198 if (nfqa[NFQA_CFG_FLAGS]) {
1199 __u32 flags, mask;
1200
1201 if (!queue) {
1202 ret = -ENODEV;
1203 goto err_out_unlock;
1204 }
1205
1206 if (!nfqa[NFQA_CFG_MASK]) {
1207 /* A mask is needed to specify which flags are being
1208 * changed.
1209 */
1210 ret = -EINVAL;
1211 goto err_out_unlock;
1212 }
1213
1214 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
1215 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
1216
46ba5a25
KK
1217 if (flags >= NFQA_CFG_F_MAX) {
1218 ret = -EOPNOTSUPP;
1219 goto err_out_unlock;
1220 }
ef493bd9
RK
1221#if !IS_ENABLED(CONFIG_NETWORK_SECMARK)
1222 if (flags & mask & NFQA_CFG_F_SECCTX) {
1223 ret = -EOPNOTSUPP;
1224 goto err_out_unlock;
1225 }
1226#endif
fdb694a0
KK
1227 spin_lock_bh(&queue->lock);
1228 queue->flags &= ~mask;
1229 queue->flags |= flags & mask;
1230 spin_unlock_bh(&queue->lock);
1231 }
1232
9872bec7
PM
1233err_out_unlock:
1234 rcu_read_unlock();
838ab636 1235 return ret;
7af4cc3f
HW
1236}
1237
7c8d4cb4 1238static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
84a797dd 1239 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
37d2e7a2 1240 .attr_count = NFQA_MAX, },
84a797dd 1241 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
5bf75853
PM
1242 .attr_count = NFQA_MAX,
1243 .policy = nfqa_verdict_policy },
7af4cc3f 1244 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
5bf75853
PM
1245 .attr_count = NFQA_CFG_MAX,
1246 .policy = nfqa_cfg_policy },
97d32cf9
FW
1247 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
1248 .attr_count = NFQA_MAX,
1249 .policy = nfqa_verdict_batch_policy },
7af4cc3f
HW
1250};
1251
7c8d4cb4 1252static const struct nfnetlink_subsystem nfqnl_subsys = {
7af4cc3f
HW
1253 .name = "nf_queue",
1254 .subsys_id = NFNL_SUBSYS_QUEUE,
1255 .cb_count = NFQNL_MSG_MAX,
7af4cc3f
HW
1256 .cb = nfqnl_cb,
1257};
1258
838ab636
HW
1259#ifdef CONFIG_PROC_FS
1260struct iter_state {
e8179610 1261 struct seq_net_private p;
838ab636
HW
1262 unsigned int bucket;
1263};
1264
1265static struct hlist_node *get_first(struct seq_file *seq)
1266{
1267 struct iter_state *st = seq->private;
e8179610
G
1268 struct net *net;
1269 struct nfnl_queue_net *q;
838ab636
HW
1270
1271 if (!st)
1272 return NULL;
1273
e8179610
G
1274 net = seq_file_net(seq);
1275 q = nfnl_queue_pernet(net);
838ab636 1276 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
e8179610
G
1277 if (!hlist_empty(&q->instance_table[st->bucket]))
1278 return q->instance_table[st->bucket].first;
838ab636
HW
1279 }
1280 return NULL;
1281}
1282
1283static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
1284{
1285 struct iter_state *st = seq->private;
e8179610 1286 struct net *net = seq_file_net(seq);
838ab636
HW
1287
1288 h = h->next;
1289 while (!h) {
e8179610
G
1290 struct nfnl_queue_net *q;
1291
838ab636
HW
1292 if (++st->bucket >= INSTANCE_BUCKETS)
1293 return NULL;
1294
e8179610
G
1295 q = nfnl_queue_pernet(net);
1296 h = q->instance_table[st->bucket].first;
838ab636
HW
1297 }
1298 return h;
1299}
1300
1301static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1302{
1303 struct hlist_node *head;
1304 head = get_first(seq);
1305
1306 if (head)
1307 while (pos && (head = get_next(seq, head)))
1308 pos--;
1309 return pos ? NULL : head;
1310}
1311
e8179610
G
1312static void *seq_start(struct seq_file *s, loff_t *pos)
1313 __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
838ab636 1314{
e8179610
G
1315 spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
1316 return get_idx(s, *pos);
838ab636
HW
1317}
1318
1319static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1320{
1321 (*pos)++;
1322 return get_next(s, v);
1323}
1324
1325static void seq_stop(struct seq_file *s, void *v)
e8179610 1326 __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
838ab636 1327{
e8179610 1328 spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
838ab636
HW
1329}
1330
1331static int seq_show(struct seq_file *s, void *v)
1332{
1333 const struct nfqnl_instance *inst = v;
1334
6b46f7b7 1335 seq_printf(s, "%5u %6u %5u %1u %5u %5u %5u %8u %2d\n",
e71456ae
SRRH
1336 inst->queue_num,
1337 inst->peer_portid, inst->queue_total,
1338 inst->copy_mode, inst->copy_range,
1339 inst->queue_dropped, inst->queue_user_dropped,
1340 inst->id_sequence, 1);
861fb107 1341 return 0;
838ab636
HW
1342}
1343
56b3d975 1344static const struct seq_operations nfqnl_seq_ops = {
838ab636
HW
1345 .start = seq_start,
1346 .next = seq_next,
1347 .stop = seq_stop,
1348 .show = seq_show,
1349};
1350
1351static int nfqnl_open(struct inode *inode, struct file *file)
1352{
e8179610 1353 return seq_open_net(inode, file, &nfqnl_seq_ops,
e2da5913 1354 sizeof(struct iter_state));
838ab636
HW
1355}
1356
da7071d7 1357static const struct file_operations nfqnl_file_ops = {
838ab636
HW
1358 .owner = THIS_MODULE,
1359 .open = nfqnl_open,
1360 .read = seq_read,
1361 .llseek = seq_lseek,
e8179610 1362 .release = seq_release_net,
838ab636
HW
1363};
1364
1365#endif /* PROC_FS */
1366
e8179610 1367static int __net_init nfnl_queue_net_init(struct net *net)
7af4cc3f 1368{
e8179610
G
1369 unsigned int i;
1370 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
601e68e1 1371
838ab636 1372 for (i = 0; i < INSTANCE_BUCKETS; i++)
e8179610
G
1373 INIT_HLIST_HEAD(&q->instance_table[i]);
1374
1375 spin_lock_init(&q->instances_lock);
1376
1377#ifdef CONFIG_PROC_FS
1378 if (!proc_create("nfnetlink_queue", 0440,
1379 net->nf.proc_netfilter, &nfqnl_file_ops))
1380 return -ENOMEM;
1381#endif
1382 return 0;
1383}
1384
1385static void __net_exit nfnl_queue_net_exit(struct net *net)
1386{
e778f56e 1387#ifdef CONFIG_PROC_FS
e8179610 1388 remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
e778f56e 1389#endif
e8179610
G
1390}
1391
1392static struct pernet_operations nfnl_queue_net_ops = {
1393 .init = nfnl_queue_net_init,
1394 .exit = nfnl_queue_net_exit,
1395 .id = &nfnl_queue_net_id,
1396 .size = sizeof(struct nfnl_queue_net),
1397};
1398
1399static int __init nfnetlink_queue_init(void)
1400{
3bfe0498
FR
1401 int status;
1402
1403 status = register_pernet_subsys(&nfnl_queue_net_ops);
1404 if (status < 0) {
1405 pr_err("nf_queue: failed to register pernet ops\n");
1406 goto out;
1407 }
838ab636 1408
7af4cc3f
HW
1409 netlink_register_notifier(&nfqnl_rtnl_notifier);
1410 status = nfnetlink_subsys_register(&nfqnl_subsys);
1411 if (status < 0) {
e8179610 1412 pr_err("nf_queue: failed to create netlink socket\n");
7af4cc3f
HW
1413 goto cleanup_netlink_notifier;
1414 }
1415
1416 register_netdevice_notifier(&nfqnl_dev_notifier);
0360ae41 1417 nf_register_queue_handler(&nfqh);
7af4cc3f
HW
1418 return status;
1419
7af4cc3f
HW
1420cleanup_netlink_notifier:
1421 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
639e077b 1422 unregister_pernet_subsys(&nfnl_queue_net_ops);
3bfe0498 1423out:
7af4cc3f
HW
1424 return status;
1425}
1426
65b4b4e8 1427static void __exit nfnetlink_queue_fini(void)
7af4cc3f 1428{
0360ae41 1429 nf_unregister_queue_handler();
32292a7f 1430 unregister_netdevice_notifier(&nfqnl_dev_notifier);
32292a7f
PM
1431 nfnetlink_subsys_unregister(&nfqnl_subsys);
1432 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
3bfe0498 1433 unregister_pernet_subsys(&nfnl_queue_net_ops);
67137f3c
JDB
1434
1435 rcu_barrier(); /* Wait for completion of call_rcu()'s */
7af4cc3f
HW
1436}
1437
1438MODULE_DESCRIPTION("netfilter packet queue handler");
1439MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1440MODULE_LICENSE("GPL");
1441MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1442
65b4b4e8
AM
1443module_init(nfnetlink_queue_init);
1444module_exit(nfnetlink_queue_fini);