]> git.ipfire.org Git - people/ms/linux.git/blame - net/netfilter/nfnetlink_queue.c
netfilter: nfnetlink_queue: don't handle options after unbind
[people/ms/linux.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
7b8002a1
PNA
960static int nfqnl_recv_verdict_batch(struct net *net, struct sock *ctnl,
961 struct sk_buff *skb,
962 const struct nlmsghdr *nlh,
963 const struct nlattr * const nfqa[])
97d32cf9 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);
e8179610
G
972 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
973
974 queue = verdict_instance_lookup(q, queue_num,
975 NETLINK_CB(skb).portid);
97d32cf9
FW
976 if (IS_ERR(queue))
977 return PTR_ERR(queue);
978
979 vhdr = verdicthdr_get(nfqa);
980 if (!vhdr)
981 return -EINVAL;
982
983 verdict = ntohl(vhdr->verdict);
984 maxid = ntohl(vhdr->id);
985
986 spin_lock_bh(&queue->lock);
987
988 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
989 if (nfq_id_after(entry->id, maxid))
990 break;
991 __dequeue_entry(queue, entry);
992 list_add_tail(&entry->list, &batch_list);
993 }
994
995 spin_unlock_bh(&queue->lock);
996
997 if (list_empty(&batch_list))
998 return -ENOENT;
999
1000 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
1001 if (nfqa[NFQA_MARK])
1002 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
1003 nf_reinject(entry, verdict);
1004 }
1005 return 0;
1006}
1007
a4b4766c 1008static struct nf_conn *nfqnl_ct_parse(struct nfnl_ct_hook *nfnl_ct,
b7bd1809
PNA
1009 const struct nlmsghdr *nlh,
1010 const struct nlattr * const nfqa[],
1011 struct nf_queue_entry *entry,
1012 enum ip_conntrack_info *ctinfo)
1013{
1014 struct nf_conn *ct;
1015
a4b4766c 1016 ct = nfnl_ct->get_ct(entry->skb, ctinfo);
b7bd1809
PNA
1017 if (ct == NULL)
1018 return NULL;
1019
a4b4766c 1020 if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0)
b7bd1809
PNA
1021 return NULL;
1022
1023 if (nfqa[NFQA_EXP])
a4b4766c 1024 nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct,
b7bd1809
PNA
1025 NETLINK_CB(entry->skb).portid,
1026 nlmsg_report(nlh));
1027 return ct;
1028}
1029
7b8002a1
PNA
1030static int nfqnl_recv_verdict(struct net *net, struct sock *ctnl,
1031 struct sk_buff *skb,
1032 const struct nlmsghdr *nlh,
1033 const struct nlattr * const nfqa[])
7af4cc3f 1034{
3da07c0c 1035 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7af4cc3f 1036 u_int16_t queue_num = ntohs(nfmsg->res_id);
7af4cc3f
HW
1037 struct nfqnl_msg_verdict_hdr *vhdr;
1038 struct nfqnl_instance *queue;
1039 unsigned int verdict;
02f014d8 1040 struct nf_queue_entry *entry;
8c88f87c 1041 enum ip_conntrack_info uninitialized_var(ctinfo);
a4b4766c 1042 struct nfnl_ct_hook *nfnl_ct;
8c88f87c 1043 struct nf_conn *ct = NULL;
e8179610 1044 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
7af4cc3f 1045
e8179610
G
1046 queue = instance_lookup(q, queue_num);
1047 if (!queue)
1048 queue = verdict_instance_lookup(q, queue_num,
1049 NETLINK_CB(skb).portid);
97d32cf9
FW
1050 if (IS_ERR(queue))
1051 return PTR_ERR(queue);
7af4cc3f 1052
97d32cf9
FW
1053 vhdr = verdicthdr_get(nfqa);
1054 if (!vhdr)
84a797dd 1055 return -EINVAL;
7af4cc3f 1056
7af4cc3f
HW
1057 verdict = ntohl(vhdr->verdict);
1058
b43d8d85 1059 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
84a797dd
ED
1060 if (entry == NULL)
1061 return -ENOENT;
7af4cc3f 1062
8e662164
AB
1063 /* rcu lock already held from nfnl->call_rcu. */
1064 nfnl_ct = rcu_dereference(nfnl_ct_hook);
1065
bd077937 1066 if (nfqa[NFQA_CT]) {
a4b4766c
KM
1067 if (nfnl_ct != NULL)
1068 ct = nfqnl_ct_parse(nfnl_ct, nlh, nfqa, entry, &ctinfo);
bd077937 1069 }
9cb01766 1070
df6fb868 1071 if (nfqa[NFQA_PAYLOAD]) {
8c88f87c
PNA
1072 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
1073 int diff = payload_len - entry->skb->len;
1074
df6fb868 1075 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
8c88f87c 1076 payload_len, entry, diff) < 0)
7af4cc3f 1077 verdict = NF_DROP;
8c88f87c 1078
b7bd1809 1079 if (ct && diff)
a4b4766c 1080 nfnl_ct->seq_adjust(entry->skb, ct, ctinfo, diff);
7af4cc3f
HW
1081 }
1082
df6fb868 1083 if (nfqa[NFQA_MARK])
ea3a66ff 1084 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
601e68e1 1085
4b3d15ef 1086 nf_reinject(entry, verdict);
7af4cc3f
HW
1087 return 0;
1088}
1089
7b8002a1
PNA
1090static int nfqnl_recv_unsupp(struct net *net, struct sock *ctnl,
1091 struct sk_buff *skb, const struct nlmsghdr *nlh,
1092 const struct nlattr * const nfqa[])
7af4cc3f
HW
1093{
1094 return -ENOTSUPP;
1095}
1096
5bf75853
PM
1097static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
1098 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
1099 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
838ab636
HW
1100};
1101
e3ac5298 1102static const struct nf_queue_handler nfqh = {
8405a8ff
EB
1103 .outfn = &nfqnl_enqueue_packet,
1104 .nf_hook_drop = &nfqnl_nf_hook_drop,
bbd86b9f
HW
1105};
1106
7b8002a1
PNA
1107static int nfqnl_recv_config(struct net *net, struct sock *ctnl,
1108 struct sk_buff *skb, const struct nlmsghdr *nlh,
1109 const struct nlattr * const nfqa[])
7af4cc3f 1110{
3da07c0c 1111 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7af4cc3f
HW
1112 u_int16_t queue_num = ntohs(nfmsg->res_id);
1113 struct nfqnl_instance *queue;
9872bec7 1114 struct nfqnl_msg_config_cmd *cmd = NULL;
e8179610 1115 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
60d2c7f9 1116 __u32 flags = 0, mask = 0;
838ab636 1117 int ret = 0;
7af4cc3f 1118
9872bec7
PM
1119 if (nfqa[NFQA_CFG_CMD]) {
1120 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
1121
0360ae41 1122 /* Obsolete commands without queue context */
9872bec7 1123 switch (cmd->command) {
0360ae41
FW
1124 case NFQNL_CFG_CMD_PF_BIND: return 0;
1125 case NFQNL_CFG_CMD_PF_UNBIND: return 0;
9872bec7 1126 }
9872bec7
PM
1127 }
1128
60d2c7f9
KM
1129 /* Check if we support these flags in first place, dependencies should
1130 * be there too not to break atomicity.
1131 */
1132 if (nfqa[NFQA_CFG_FLAGS]) {
1133 if (!nfqa[NFQA_CFG_MASK]) {
1134 /* A mask is needed to specify which flags are being
1135 * changed.
1136 */
1137 return -EINVAL;
1138 }
1139
1140 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
1141 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
1142
1143 if (flags >= NFQA_CFG_F_MAX)
1144 return -EOPNOTSUPP;
1145
1146#if !IS_ENABLED(CONFIG_NETWORK_SECMARK)
1147 if (flags & mask & NFQA_CFG_F_SECCTX)
1148 return -EOPNOTSUPP;
1149#endif
1150 }
1151
9872bec7 1152 rcu_read_lock();
e8179610 1153 queue = instance_lookup(q, queue_num);
15e47304 1154 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
a3c8e7fd 1155 ret = -EPERM;
9872bec7 1156 goto err_out_unlock;
a3c8e7fd
PM
1157 }
1158
9872bec7 1159 if (cmd != NULL) {
7af4cc3f
HW
1160 switch (cmd->command) {
1161 case NFQNL_CFG_CMD_BIND:
9872bec7
PM
1162 if (queue) {
1163 ret = -EBUSY;
1164 goto err_out_unlock;
1165 }
e8179610
G
1166 queue = instance_create(q, queue_num,
1167 NETLINK_CB(skb).portid);
baab2ce7
PM
1168 if (IS_ERR(queue)) {
1169 ret = PTR_ERR(queue);
9872bec7
PM
1170 goto err_out_unlock;
1171 }
7af4cc3f
HW
1172 break;
1173 case NFQNL_CFG_CMD_UNBIND:
9872bec7
PM
1174 if (!queue) {
1175 ret = -ENODEV;
1176 goto err_out_unlock;
1177 }
e8179610 1178 instance_destroy(q, queue);
17bc6b48 1179 goto err_out_unlock;
7af4cc3f 1180 case NFQNL_CFG_CMD_PF_BIND:
7af4cc3f 1181 case NFQNL_CFG_CMD_PF_UNBIND:
7af4cc3f
HW
1182 break;
1183 default:
cd21f0ac 1184 ret = -ENOTSUPP;
838ab636 1185 break;
7af4cc3f 1186 }
7af4cc3f
HW
1187 }
1188
60d2c7f9
KM
1189 if (!queue) {
1190 ret = -ENODEV;
1191 goto err_out_unlock;
1192 }
1193
df6fb868 1194 if (nfqa[NFQA_CFG_PARAMS]) {
60d2c7f9
KM
1195 struct nfqnl_msg_config_params *params =
1196 nla_data(nfqa[NFQA_CFG_PARAMS]);
7af4cc3f
HW
1197
1198 nfqnl_set_mode(queue, params->copy_mode,
1199 ntohl(params->copy_range));
1200 }
1201
df6fb868 1202 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
60d2c7f9 1203 __be32 *queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
a3c8e7fd 1204
829e17a1
EL
1205 spin_lock_bh(&queue->lock);
1206 queue->queue_maxlen = ntohl(*queue_maxlen);
1207 spin_unlock_bh(&queue->lock);
1208 }
1209
fdb694a0 1210 if (nfqa[NFQA_CFG_FLAGS]) {
fdb694a0
KK
1211 spin_lock_bh(&queue->lock);
1212 queue->flags &= ~mask;
1213 queue->flags |= flags & mask;
1214 spin_unlock_bh(&queue->lock);
1215 }
1216
9872bec7
PM
1217err_out_unlock:
1218 rcu_read_unlock();
838ab636 1219 return ret;
7af4cc3f
HW
1220}
1221
7c8d4cb4 1222static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
84a797dd 1223 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
37d2e7a2 1224 .attr_count = NFQA_MAX, },
84a797dd 1225 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
5bf75853
PM
1226 .attr_count = NFQA_MAX,
1227 .policy = nfqa_verdict_policy },
7af4cc3f 1228 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
5bf75853
PM
1229 .attr_count = NFQA_CFG_MAX,
1230 .policy = nfqa_cfg_policy },
97d32cf9
FW
1231 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
1232 .attr_count = NFQA_MAX,
1233 .policy = nfqa_verdict_batch_policy },
7af4cc3f
HW
1234};
1235
7c8d4cb4 1236static const struct nfnetlink_subsystem nfqnl_subsys = {
7af4cc3f
HW
1237 .name = "nf_queue",
1238 .subsys_id = NFNL_SUBSYS_QUEUE,
1239 .cb_count = NFQNL_MSG_MAX,
7af4cc3f
HW
1240 .cb = nfqnl_cb,
1241};
1242
838ab636
HW
1243#ifdef CONFIG_PROC_FS
1244struct iter_state {
e8179610 1245 struct seq_net_private p;
838ab636
HW
1246 unsigned int bucket;
1247};
1248
1249static struct hlist_node *get_first(struct seq_file *seq)
1250{
1251 struct iter_state *st = seq->private;
e8179610
G
1252 struct net *net;
1253 struct nfnl_queue_net *q;
838ab636
HW
1254
1255 if (!st)
1256 return NULL;
1257
e8179610
G
1258 net = seq_file_net(seq);
1259 q = nfnl_queue_pernet(net);
838ab636 1260 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
e8179610
G
1261 if (!hlist_empty(&q->instance_table[st->bucket]))
1262 return q->instance_table[st->bucket].first;
838ab636
HW
1263 }
1264 return NULL;
1265}
1266
1267static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
1268{
1269 struct iter_state *st = seq->private;
e8179610 1270 struct net *net = seq_file_net(seq);
838ab636
HW
1271
1272 h = h->next;
1273 while (!h) {
e8179610
G
1274 struct nfnl_queue_net *q;
1275
838ab636
HW
1276 if (++st->bucket >= INSTANCE_BUCKETS)
1277 return NULL;
1278
e8179610
G
1279 q = nfnl_queue_pernet(net);
1280 h = q->instance_table[st->bucket].first;
838ab636
HW
1281 }
1282 return h;
1283}
1284
1285static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1286{
1287 struct hlist_node *head;
1288 head = get_first(seq);
1289
1290 if (head)
1291 while (pos && (head = get_next(seq, head)))
1292 pos--;
1293 return pos ? NULL : head;
1294}
1295
e8179610
G
1296static void *seq_start(struct seq_file *s, loff_t *pos)
1297 __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
838ab636 1298{
e8179610
G
1299 spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
1300 return get_idx(s, *pos);
838ab636
HW
1301}
1302
1303static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1304{
1305 (*pos)++;
1306 return get_next(s, v);
1307}
1308
1309static void seq_stop(struct seq_file *s, void *v)
e8179610 1310 __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
838ab636 1311{
e8179610 1312 spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
838ab636
HW
1313}
1314
1315static int seq_show(struct seq_file *s, void *v)
1316{
1317 const struct nfqnl_instance *inst = v;
1318
6b46f7b7 1319 seq_printf(s, "%5u %6u %5u %1u %5u %5u %5u %8u %2d\n",
e71456ae
SRRH
1320 inst->queue_num,
1321 inst->peer_portid, inst->queue_total,
1322 inst->copy_mode, inst->copy_range,
1323 inst->queue_dropped, inst->queue_user_dropped,
1324 inst->id_sequence, 1);
861fb107 1325 return 0;
838ab636
HW
1326}
1327
56b3d975 1328static const struct seq_operations nfqnl_seq_ops = {
838ab636
HW
1329 .start = seq_start,
1330 .next = seq_next,
1331 .stop = seq_stop,
1332 .show = seq_show,
1333};
1334
1335static int nfqnl_open(struct inode *inode, struct file *file)
1336{
e8179610 1337 return seq_open_net(inode, file, &nfqnl_seq_ops,
e2da5913 1338 sizeof(struct iter_state));
838ab636
HW
1339}
1340
da7071d7 1341static const struct file_operations nfqnl_file_ops = {
838ab636
HW
1342 .owner = THIS_MODULE,
1343 .open = nfqnl_open,
1344 .read = seq_read,
1345 .llseek = seq_lseek,
e8179610 1346 .release = seq_release_net,
838ab636
HW
1347};
1348
1349#endif /* PROC_FS */
1350
e8179610 1351static int __net_init nfnl_queue_net_init(struct net *net)
7af4cc3f 1352{
e8179610
G
1353 unsigned int i;
1354 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
601e68e1 1355
838ab636 1356 for (i = 0; i < INSTANCE_BUCKETS; i++)
e8179610
G
1357 INIT_HLIST_HEAD(&q->instance_table[i]);
1358
1359 spin_lock_init(&q->instances_lock);
1360
1361#ifdef CONFIG_PROC_FS
1362 if (!proc_create("nfnetlink_queue", 0440,
1363 net->nf.proc_netfilter, &nfqnl_file_ops))
1364 return -ENOMEM;
1365#endif
1366 return 0;
1367}
1368
1369static void __net_exit nfnl_queue_net_exit(struct net *net)
1370{
e778f56e 1371#ifdef CONFIG_PROC_FS
e8179610 1372 remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
e778f56e 1373#endif
e8179610
G
1374}
1375
1376static struct pernet_operations nfnl_queue_net_ops = {
1377 .init = nfnl_queue_net_init,
1378 .exit = nfnl_queue_net_exit,
1379 .id = &nfnl_queue_net_id,
1380 .size = sizeof(struct nfnl_queue_net),
1381};
1382
1383static int __init nfnetlink_queue_init(void)
1384{
3bfe0498
FR
1385 int status;
1386
1387 status = register_pernet_subsys(&nfnl_queue_net_ops);
1388 if (status < 0) {
1389 pr_err("nf_queue: failed to register pernet ops\n");
1390 goto out;
1391 }
838ab636 1392
7af4cc3f
HW
1393 netlink_register_notifier(&nfqnl_rtnl_notifier);
1394 status = nfnetlink_subsys_register(&nfqnl_subsys);
1395 if (status < 0) {
e8179610 1396 pr_err("nf_queue: failed to create netlink socket\n");
7af4cc3f
HW
1397 goto cleanup_netlink_notifier;
1398 }
1399
1400 register_netdevice_notifier(&nfqnl_dev_notifier);
0360ae41 1401 nf_register_queue_handler(&nfqh);
7af4cc3f
HW
1402 return status;
1403
7af4cc3f
HW
1404cleanup_netlink_notifier:
1405 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
639e077b 1406 unregister_pernet_subsys(&nfnl_queue_net_ops);
3bfe0498 1407out:
7af4cc3f
HW
1408 return status;
1409}
1410
65b4b4e8 1411static void __exit nfnetlink_queue_fini(void)
7af4cc3f 1412{
0360ae41 1413 nf_unregister_queue_handler();
32292a7f 1414 unregister_netdevice_notifier(&nfqnl_dev_notifier);
32292a7f
PM
1415 nfnetlink_subsys_unregister(&nfqnl_subsys);
1416 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
3bfe0498 1417 unregister_pernet_subsys(&nfnl_queue_net_ops);
67137f3c
JDB
1418
1419 rcu_barrier(); /* Wait for completion of call_rcu()'s */
7af4cc3f
HW
1420}
1421
1422MODULE_DESCRIPTION("netfilter packet queue handler");
1423MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1424MODULE_LICENSE("GPL");
1425MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1426
65b4b4e8
AM
1427module_init(nfnetlink_queue_init);
1428module_exit(nfnetlink_queue_fini);