xref: /linux/net/netfilter/nfnetlink_log.c (revision 387d744fa7e499d2c3748a4e60e02ebb24e7fb16)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This is a module which is used for logging packets to userspace via
4  * nfetlink.
5  *
6  * (C) 2005 by Harald Welte <laforge@netfilter.org>
7  * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
8  *
9  * Based on the old ipv4-only ipt_ULOG.c:
10  * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
11  */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
15 #include <linux/module.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/init.h>
19 #include <linux/ip.h>
20 #include <linux/ipv6.h>
21 #include <linux/netdevice.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter_bridge.h>
24 #include <net/netlink.h>
25 #include <linux/netfilter/nfnetlink.h>
26 #include <linux/netfilter/nfnetlink_log.h>
27 #include <linux/netfilter/nf_conntrack_common.h>
28 #include <linux/spinlock.h>
29 #include <linux/sysctl.h>
30 #include <linux/proc_fs.h>
31 #include <linux/security.h>
32 #include <linux/list.h>
33 #include <linux/slab.h>
34 #include <net/sock.h>
35 #include <net/netfilter/nf_log.h>
36 #include <net/netns/generic.h>
37 
38 #include <linux/atomic.h>
39 #include <linux/refcount.h>
40 
41 
42 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
43 #include "../bridge/br_private.h"
44 #endif
45 
46 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
47 #include <net/netfilter/nf_conntrack.h>
48 #endif
49 
50 #define NFULNL_COPY_DISABLED	0xff
51 #define NFULNL_NLBUFSIZ_DEFAULT	NLMSG_GOODSIZE
52 #define NFULNL_TIMEOUT_DEFAULT 	100	/* every second */
53 #define NFULNL_QTHRESH_DEFAULT 	100	/* 100 packets */
54 /* max packet size is limited by 16-bit struct nfattr nfa_len field */
55 #define NFULNL_COPY_RANGE_MAX	(0xFFFF - NLA_HDRLEN)
56 
57 #define PRINTR(x, args...)	do { if (net_ratelimit()) \
58 				     printk(x, ## args); } while (0);
59 
60 struct nfulnl_instance {
61 	struct hlist_node hlist;	/* global list of instances */
62 	spinlock_t lock;
63 	refcount_t use;			/* use count */
64 
65 	unsigned int qlen;		/* number of nlmsgs in skb */
66 	struct sk_buff *skb;		/* pre-allocatd skb */
67 	struct timer_list timer;
68 	struct net *net;
69 	netns_tracker ns_tracker;
70 	struct user_namespace *peer_user_ns;	/* User namespace of the peer process */
71 	u32 peer_portid;		/* PORTID of the peer process */
72 
73 	/* configurable parameters */
74 	unsigned int flushtimeout;	/* timeout until queue flush */
75 	unsigned int nlbufsiz;		/* netlink buffer allocation size */
76 	unsigned int qthreshold;	/* threshold of the queue */
77 	u_int32_t copy_range;
78 	u_int32_t seq;			/* instance-local sequential counter */
79 	u_int16_t group_num;		/* number of this queue */
80 	u_int16_t flags;
81 	u_int8_t copy_mode;
82 	struct rcu_head rcu;
83 };
84 
85 #define INSTANCE_BUCKETS	16
86 
87 static unsigned int nfnl_log_net_id __read_mostly;
88 
89 struct nfnl_log_net {
90 	spinlock_t instances_lock;
91 	struct hlist_head instance_table[INSTANCE_BUCKETS];
92 	atomic_t global_seq;
93 };
94 
95 static struct nfnl_log_net *nfnl_log_pernet(struct net *net)
96 {
97 	return net_generic(net, nfnl_log_net_id);
98 }
99 
100 static inline u_int8_t instance_hashfn(u_int16_t group_num)
101 {
102 	return ((group_num & 0xff) % INSTANCE_BUCKETS);
103 }
104 
105 static struct nfulnl_instance *
106 __instance_lookup(const struct nfnl_log_net *log, u16 group_num)
107 {
108 	const struct hlist_head *head;
109 	struct nfulnl_instance *inst;
110 
111 	head = &log->instance_table[instance_hashfn(group_num)];
112 	hlist_for_each_entry_rcu(inst, head, hlist) {
113 		if (inst->group_num == group_num)
114 			return inst;
115 	}
116 	return NULL;
117 }
118 
119 static inline void
120 instance_get(struct nfulnl_instance *inst)
121 {
122 	refcount_inc(&inst->use);
123 }
124 
125 static struct nfulnl_instance *
126 instance_lookup_get_rcu(const struct nfnl_log_net *log, u16 group_num)
127 {
128 	struct nfulnl_instance *inst;
129 
130 	inst = __instance_lookup(log, group_num);
131 	if (inst && !refcount_inc_not_zero(&inst->use))
132 		inst = NULL;
133 
134 	return inst;
135 }
136 
137 static struct nfulnl_instance *
138 instance_lookup_get(const struct nfnl_log_net *log, u16 group_num)
139 {
140 	struct nfulnl_instance *inst;
141 
142 	rcu_read_lock();
143 	inst = instance_lookup_get_rcu(log, group_num);
144 	rcu_read_unlock();
145 
146 	return inst;
147 }
148 
149 static void nfulnl_instance_free_rcu(struct rcu_head *head)
150 {
151 	struct nfulnl_instance *inst =
152 		container_of(head, struct nfulnl_instance, rcu);
153 
154 	put_net_track(inst->net, &inst->ns_tracker);
155 	kfree(inst);
156 	module_put(THIS_MODULE);
157 }
158 
159 static void
160 instance_put(struct nfulnl_instance *inst)
161 {
162 	if (inst && refcount_dec_and_test(&inst->use))
163 		call_rcu(&inst->rcu, nfulnl_instance_free_rcu);
164 }
165 
166 static void nfulnl_timer(struct timer_list *t);
167 
168 static struct nfulnl_instance *
169 instance_create(struct net *net, u_int16_t group_num,
170 		u32 portid, struct user_namespace *user_ns)
171 {
172 	struct nfulnl_instance *inst;
173 	struct nfnl_log_net *log = nfnl_log_pernet(net);
174 	int err;
175 
176 	spin_lock_bh(&log->instances_lock);
177 	if (__instance_lookup(log, group_num)) {
178 		err = -EEXIST;
179 		goto out_unlock;
180 	}
181 
182 	inst = kzalloc_obj(*inst, GFP_ATOMIC);
183 	if (!inst) {
184 		err = -ENOMEM;
185 		goto out_unlock;
186 	}
187 
188 	if (!try_module_get(THIS_MODULE)) {
189 		kfree(inst);
190 		err = -EAGAIN;
191 		goto out_unlock;
192 	}
193 
194 	INIT_HLIST_NODE(&inst->hlist);
195 	spin_lock_init(&inst->lock);
196 	/* needs to be two, since we _put() after creation */
197 	refcount_set(&inst->use, 2);
198 
199 	timer_setup(&inst->timer, nfulnl_timer, 0);
200 
201 	inst->net = get_net_track(net, &inst->ns_tracker, GFP_ATOMIC);
202 	inst->peer_user_ns = user_ns;
203 	inst->peer_portid = portid;
204 	inst->group_num = group_num;
205 
206 	inst->qthreshold 	= NFULNL_QTHRESH_DEFAULT;
207 	inst->flushtimeout 	= NFULNL_TIMEOUT_DEFAULT;
208 	inst->nlbufsiz 		= NFULNL_NLBUFSIZ_DEFAULT;
209 	inst->copy_mode 	= NFULNL_COPY_PACKET;
210 	inst->copy_range 	= NFULNL_COPY_RANGE_MAX;
211 
212 	hlist_add_head_rcu(&inst->hlist,
213 		       &log->instance_table[instance_hashfn(group_num)]);
214 
215 
216 	spin_unlock_bh(&log->instances_lock);
217 
218 	return inst;
219 
220 out_unlock:
221 	spin_unlock_bh(&log->instances_lock);
222 	return ERR_PTR(err);
223 }
224 
225 static void __nfulnl_flush(struct nfulnl_instance *inst);
226 
227 /* called with BH disabled */
228 static void
229 __instance_destroy(struct nfulnl_instance *inst)
230 {
231 	spin_lock(&inst->lock);
232 	if (inst->copy_mode == NFULNL_COPY_DISABLED) {
233 		/* attempt to UNBIND a queue already pending
234 		 * destruction via netlink close event. Ignore.
235 		 */
236 		spin_unlock(&inst->lock);
237 		return;
238 	}
239 
240 	/* first pull it out of the global list */
241 	hlist_del_rcu(&inst->hlist);
242 
243 	/* lockless readers wont be able to use us */
244 	inst->copy_mode = NFULNL_COPY_DISABLED;
245 
246 	if (inst->skb)
247 		__nfulnl_flush(inst);
248 	spin_unlock(&inst->lock);
249 
250 	/* and finally put the refcount */
251 	instance_put(inst);
252 }
253 
254 static inline void
255 instance_destroy(struct nfnl_log_net *log,
256 		 struct nfulnl_instance *inst)
257 {
258 	spin_lock_bh(&log->instances_lock);
259 	__instance_destroy(inst);
260 	spin_unlock_bh(&log->instances_lock);
261 }
262 
263 static int
264 nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
265 		  unsigned int range)
266 {
267 	int status = 0;
268 
269 	spin_lock_bh(&inst->lock);
270 
271 	switch (mode) {
272 	case NFULNL_COPY_NONE:
273 	case NFULNL_COPY_META:
274 		inst->copy_mode = mode;
275 		inst->copy_range = 0;
276 		break;
277 
278 	case NFULNL_COPY_PACKET:
279 		inst->copy_mode = mode;
280 		if (range == 0)
281 			range = NFULNL_COPY_RANGE_MAX;
282 		inst->copy_range = min_t(unsigned int,
283 					 range, NFULNL_COPY_RANGE_MAX);
284 		break;
285 
286 	default:
287 		status = -EINVAL;
288 		break;
289 	}
290 
291 	spin_unlock_bh(&inst->lock);
292 
293 	return status;
294 }
295 
296 static int
297 nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
298 {
299 	int status;
300 
301 	spin_lock_bh(&inst->lock);
302 	if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
303 		status = -ERANGE;
304 	else if (nlbufsiz > 131072)
305 		status = -ERANGE;
306 	else {
307 		inst->nlbufsiz = nlbufsiz;
308 		status = 0;
309 	}
310 	spin_unlock_bh(&inst->lock);
311 
312 	return status;
313 }
314 
315 static void
316 nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
317 {
318 	spin_lock_bh(&inst->lock);
319 	inst->flushtimeout = timeout;
320 	spin_unlock_bh(&inst->lock);
321 }
322 
323 static void
324 nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
325 {
326 	spin_lock_bh(&inst->lock);
327 	inst->qthreshold = qthresh;
328 	spin_unlock_bh(&inst->lock);
329 }
330 
331 static int
332 nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
333 {
334 	spin_lock_bh(&inst->lock);
335 	inst->flags = flags;
336 	spin_unlock_bh(&inst->lock);
337 
338 	return 0;
339 }
340 
341 static struct sk_buff *
342 nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size,
343 		 unsigned int pkt_size)
344 {
345 	struct sk_buff *skb;
346 	unsigned int n;
347 
348 	/* alloc skb which should be big enough for a whole multipart
349 	 * message.  WARNING: has to be <= 128k due to slab restrictions */
350 
351 	n = max(inst_size, pkt_size);
352 	skb = alloc_skb(n, GFP_ATOMIC | __GFP_NOWARN);
353 	if (!skb) {
354 		if (n > pkt_size) {
355 			/* try to allocate only as much as we need for current
356 			 * packet */
357 
358 			skb = alloc_skb(pkt_size, GFP_ATOMIC);
359 		}
360 	}
361 
362 	return skb;
363 }
364 
365 static void
366 __nfulnl_send(struct nfulnl_instance *inst)
367 {
368 	if (inst->qlen > 1) {
369 		struct nlmsghdr *nlh = nfnl_msg_put(inst->skb, 0, 0,
370 						    NLMSG_DONE, 0,
371 						    AF_UNSPEC, NFNETLINK_V0,
372 						    htons(inst->group_num));
373 		if (WARN_ONCE(!nlh, "bad nlskb size: %u, tailroom %d\n",
374 			      inst->skb->len, skb_tailroom(inst->skb))) {
375 			kfree_skb(inst->skb);
376 			goto out;
377 		}
378 	}
379 	nfnetlink_unicast(inst->skb, inst->net, inst->peer_portid);
380 out:
381 	inst->qlen = 0;
382 	inst->skb = NULL;
383 }
384 
385 static void
386 __nfulnl_flush(struct nfulnl_instance *inst)
387 {
388 	/* timer holds a reference */
389 	if (timer_delete(&inst->timer))
390 		instance_put(inst);
391 	if (inst->skb)
392 		__nfulnl_send(inst);
393 }
394 
395 static void
396 nfulnl_timer(struct timer_list *t)
397 {
398 	struct nfulnl_instance *inst = timer_container_of(inst, t, timer);
399 
400 	spin_lock_bh(&inst->lock);
401 	if (inst->skb)
402 		__nfulnl_send(inst);
403 	spin_unlock_bh(&inst->lock);
404 	instance_put(inst);
405 }
406 
407 static u32 nfulnl_get_bridge_size(const struct sk_buff *skb)
408 {
409 	u32 mac_len, size = 0;
410 
411 	if (!skb_mac_header_was_set(skb))
412 		return 0;
413 
414 	if (skb_vlan_tag_present(skb)) {
415 		size += nla_total_size(0); /* nested */
416 		size += nla_total_size(sizeof(u16)); /* id */
417 		size += nla_total_size(sizeof(u16)); /* tag */
418 	}
419 
420 	mac_len = skb_mac_header_len(skb);
421 	if (mac_len > 0)
422 		size += nla_total_size(mac_len);
423 
424 	return size;
425 }
426 
427 static int nfulnl_put_bridge(struct nfulnl_instance *inst, const struct sk_buff *skb)
428 {
429 	u32 mac_len;
430 
431 	if (!skb_mac_header_was_set(skb))
432 		return 0;
433 
434 	if (skb_vlan_tag_present(skb)) {
435 		struct nlattr *nest;
436 
437 		nest = nla_nest_start(inst->skb, NFULA_VLAN);
438 		if (!nest)
439 			goto nla_put_failure;
440 
441 		if (nla_put_be16(inst->skb, NFULA_VLAN_TCI, htons(skb->vlan_tci)) ||
442 		    nla_put_be16(inst->skb, NFULA_VLAN_PROTO, skb->vlan_proto))
443 			goto nla_put_failure;
444 
445 		nla_nest_end(inst->skb, nest);
446 	}
447 
448 	mac_len = skb_mac_header_len(skb);
449 	if (mac_len > 0 &&
450 	    nla_put(inst->skb, NFULA_L2HDR, mac_len, skb_mac_header(skb)))
451 		goto nla_put_failure;
452 
453 	return 0;
454 
455 nla_put_failure:
456 	return -1;
457 }
458 
459 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
460 static int nflog_put_master_ifindex(struct sk_buff *nlskb, int attr,
461 				    const struct net_device *dev)
462 {
463 	const struct net_device *upper;
464 
465 	if (dev && !netif_is_bridge_port(dev))
466 		return 0;
467 
468 	upper = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
469 	if (upper && nla_put_be32(nlskb, attr, htonl(upper->ifindex)))
470 		return -EMSGSIZE;
471 
472 	return 0;
473 }
474 #endif
475 
476 /* This is an inline function, we don't really care about a long
477  * list of arguments */
478 static inline int
479 __build_packet_message(struct nfnl_log_net *log,
480 			struct nfulnl_instance *inst,
481 			const struct sk_buff *skb,
482 			unsigned int data_len,
483 			u_int8_t pf,
484 			unsigned int hooknum,
485 			const struct net_device *indev,
486 			const struct net_device *outdev,
487 			const char *prefix, unsigned int plen,
488 			const struct nfnl_ct_hook *nfnl_ct,
489 			struct nf_conn *ct, enum ip_conntrack_info ctinfo)
490 {
491 	struct nfulnl_msg_packet_hdr pmsg;
492 	struct nlmsghdr *nlh;
493 	sk_buff_data_t old_tail = inst->skb->tail;
494 	struct sock *sk;
495 	const unsigned char *hwhdrp;
496 
497 	nlh = nfnl_msg_put(inst->skb, 0, 0,
498 			   nfnl_msg_type(NFNL_SUBSYS_ULOG, NFULNL_MSG_PACKET),
499 			   0, pf, NFNETLINK_V0, htons(inst->group_num));
500 	if (!nlh)
501 		return -1;
502 
503 	memset(&pmsg, 0, sizeof(pmsg));
504 	pmsg.hw_protocol	= skb->protocol;
505 	pmsg.hook		= hooknum;
506 
507 	if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg))
508 		goto nla_put_failure;
509 
510 	if (prefix &&
511 	    nla_put(inst->skb, NFULA_PREFIX, plen, prefix))
512 		goto nla_put_failure;
513 
514 	if (indev) {
515 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
516 		if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
517 				 htonl(indev->ifindex)))
518 			goto nla_put_failure;
519 #else
520 		if (pf == PF_BRIDGE) {
521 			/* Case 1: outdev is physical input device, we need to
522 			 * look for bridge group (when called from
523 			 * netfilter_bridge) */
524 			if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
525 					 htonl(indev->ifindex)) ||
526 			/* this is the bridge group "brX" */
527 			/* rcu_read_lock()ed by nf_hook_thresh or
528 			 * nf_log_packet.
529 			 */
530 			    nflog_put_master_ifindex(inst->skb, NFULA_IFINDEX_INDEV, indev))
531 				goto nla_put_failure;
532 		} else {
533 			int physinif;
534 
535 			/* Case 2: indev is bridge group, we need to look for
536 			 * physical device (when called from ipv4) */
537 			if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
538 					 htonl(indev->ifindex)))
539 				goto nla_put_failure;
540 
541 			physinif = nf_bridge_get_physinif(skb);
542 			if (physinif &&
543 			    nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
544 					 htonl(physinif)))
545 				goto nla_put_failure;
546 		}
547 #endif
548 	}
549 
550 	if (outdev) {
551 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
552 		if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
553 				 htonl(outdev->ifindex)))
554 			goto nla_put_failure;
555 #else
556 		if (pf == PF_BRIDGE) {
557 			/* Case 1: outdev is physical output device, we need to
558 			 * look for bridge group (when called from
559 			 * netfilter_bridge) */
560 			if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
561 					 htonl(outdev->ifindex)) ||
562 			/* this is the bridge group "brX" */
563 			/* rcu_read_lock()ed by nf_hook_thresh or
564 			 * nf_log_packet.
565 			 */
566 			    nflog_put_master_ifindex(inst->skb, NFULA_IFINDEX_OUTDEV, outdev))
567 				goto nla_put_failure;
568 		} else {
569 			struct net_device *physoutdev;
570 
571 			/* Case 2: indev is a bridge group, we need to look
572 			 * for physical device (when called from ipv4) */
573 			if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
574 					 htonl(outdev->ifindex)))
575 				goto nla_put_failure;
576 
577 			physoutdev = nf_bridge_get_physoutdev(skb);
578 			if (physoutdev &&
579 			    nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
580 					 htonl(physoutdev->ifindex)))
581 				goto nla_put_failure;
582 		}
583 #endif
584 	}
585 
586 	if (skb->mark &&
587 	    nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
588 		goto nla_put_failure;
589 
590 	if (indev && skb->dev &&
591 	    skb_mac_header_was_set(skb) &&
592 	    skb_mac_header_len(skb) != 0) {
593 		struct nfulnl_msg_packet_hw phw;
594 		int len;
595 
596 		memset(&phw, 0, sizeof(phw));
597 		len = dev_parse_header(skb, phw.hw_addr);
598 		if (len > 0) {
599 			phw.hw_addrlen = htons(len);
600 			if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
601 				goto nla_put_failure;
602 		}
603 	}
604 
605 	if (indev && skb_mac_header_was_set(skb)) {
606 		if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
607 		    nla_put_be16(inst->skb, NFULA_HWLEN,
608 				 htons(skb->dev->hard_header_len)))
609 			goto nla_put_failure;
610 
611 		hwhdrp = skb_mac_header(skb);
612 
613 		if (skb->dev->type == ARPHRD_SIT)
614 			hwhdrp -= ETH_HLEN;
615 
616 		if (hwhdrp >= skb->head &&
617 		    nla_put(inst->skb, NFULA_HWHEADER,
618 			    skb->dev->hard_header_len, hwhdrp))
619 			goto nla_put_failure;
620 	}
621 
622 	if (hooknum <= NF_INET_FORWARD) {
623 		struct timespec64 kts = ktime_to_timespec64(skb_tstamp_cond(skb, true));
624 		struct nfulnl_msg_packet_timestamp ts;
625 		ts.sec = cpu_to_be64(kts.tv_sec);
626 		ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
627 
628 		if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
629 			goto nla_put_failure;
630 	}
631 
632 	/* UID */
633 	sk = skb->sk;
634 	if (sk && sk_fullsock(sk)) {
635 		const struct socket *sock;
636 		const struct file *file;
637 
638 		/* The sk pointer remains valid as long as the skb is.
639 		 * The sk_socket and file pointer may become NULL
640 		 * if the socket is closed.
641 		 * Both structures (including file->cred) are RCU freed
642 		 * which means they can be accessed within a RCU read section.
643 		 */
644 		sock = READ_ONCE(sk->sk_socket);
645 		file = sock ? READ_ONCE(sock->file) : NULL;
646 		if (file) {
647 			const struct cred *cred = file->f_cred;
648 			struct user_namespace *user_ns = inst->peer_user_ns;
649 			__be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
650 			__be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
651 			if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
652 			    nla_put_be32(inst->skb, NFULA_GID, gid))
653 				goto nla_put_failure;
654 		}
655 	}
656 
657 	/* local sequence number */
658 	if ((inst->flags & NFULNL_CFG_F_SEQ) &&
659 	    nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++)))
660 		goto nla_put_failure;
661 
662 	/* global sequence number */
663 	if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) &&
664 	    nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL,
665 			 htonl(atomic_inc_return(&log->global_seq))))
666 		goto nla_put_failure;
667 
668 	if (ct && nfnl_ct->build(inst->skb, ct, ctinfo,
669 				 NFULA_CT, NFULA_CT_INFO) < 0)
670 		goto nla_put_failure;
671 
672 	if ((pf == NFPROTO_NETDEV || pf == NFPROTO_BRIDGE) &&
673 	    nfulnl_put_bridge(inst, skb) < 0)
674 		goto nla_put_failure;
675 
676 	if (data_len) {
677 		struct nlattr *nla;
678 
679 		nla = nla_reserve(inst->skb, NFULA_PAYLOAD, data_len);
680 		if (!nla)
681 			goto nla_put_failure;
682 
683 		if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
684 			goto nla_put_failure;
685 	}
686 
687 	nlh->nlmsg_len = inst->skb->tail - old_tail;
688 	return 0;
689 
690 nla_put_failure:
691 	PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
692 	return -1;
693 }
694 
695 static const struct nf_loginfo default_loginfo = {
696 	.type =		NF_LOG_TYPE_ULOG,
697 	.u = {
698 		.ulog = {
699 			.copy_len	= 0xffff,
700 			.group		= 0,
701 			.qthreshold	= 1,
702 		},
703 	},
704 };
705 
706 static unsigned int nfulnl_get_copy_len(const struct nf_loginfo *li,
707 					const struct sk_buff *skb,
708 					unsigned int copy_len)
709 {
710 	unsigned int len = skb->len;
711 
712 	if ((li->u.ulog.flags & NF_LOG_F_COPY_LEN) &&
713 	    li->u.ulog.copy_len < copy_len)
714 		copy_len = li->u.ulog.copy_len;
715 	if (!skb_frags_readable(skb))
716 		len = skb_headlen(skb);
717 
718 	return min(len, copy_len);
719 }
720 
721 /* log handler for internal netfilter logging api */
722 static void
723 nfulnl_log_packet(struct net *net,
724 		  u_int8_t pf,
725 		  unsigned int hooknum,
726 		  const struct sk_buff *skb,
727 		  const struct net_device *in,
728 		  const struct net_device *out,
729 		  const struct nf_loginfo *li_user,
730 		  const char *prefix)
731 {
732 	size_t size;
733 	unsigned int data_len;
734 	struct nfulnl_instance *inst;
735 	const struct nf_loginfo *li;
736 	unsigned int qthreshold;
737 	unsigned int plen = 0;
738 	struct nfnl_log_net *log = nfnl_log_pernet(net);
739 	const struct nfnl_ct_hook *nfnl_ct = NULL;
740 	enum ip_conntrack_info ctinfo = 0;
741 	struct nf_conn *ct = NULL;
742 
743 	if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
744 		li = li_user;
745 	else
746 		li = &default_loginfo;
747 
748 	inst = instance_lookup_get_rcu(log, li->u.ulog.group);
749 	if (!inst)
750 		return;
751 
752 	if (prefix)
753 		plen = strlen(prefix) + 1;
754 
755 	/* FIXME: do we want to make the size calculation conditional based on
756 	 * what is actually present?  way more branches and checks, but more
757 	 * memory efficient... */
758 	size = nlmsg_total_size(sizeof(struct nfgenmsg))
759 		+ nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
760 		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
761 		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
762 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
763 		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
764 		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
765 #endif
766 		+ nla_total_size(sizeof(u_int32_t))	/* mark */
767 		+ nla_total_size(sizeof(u_int32_t))	/* uid */
768 		+ nla_total_size(sizeof(u_int32_t))	/* gid */
769 		+ nla_total_size(plen)			/* prefix */
770 		+ nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
771 		+ nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp))
772 		+ nlmsg_total_size(sizeof(struct nfgenmsg));	/* NLMSG_DONE */
773 
774 	if (in && skb_mac_header_was_set(skb)) {
775 		size += nla_total_size(skb->dev->hard_header_len)
776 			+ nla_total_size(sizeof(u_int16_t))	/* hwtype */
777 			+ nla_total_size(sizeof(u_int16_t));	/* hwlen */
778 	}
779 
780 	spin_lock_bh(&inst->lock);
781 
782 	if (inst->flags & NFULNL_CFG_F_SEQ)
783 		size += nla_total_size(sizeof(u_int32_t));
784 	if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
785 		size += nla_total_size(sizeof(u_int32_t));
786 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
787 	if (inst->flags & NFULNL_CFG_F_CONNTRACK) {
788 		nfnl_ct = rcu_dereference(nfnl_ct_hook);
789 		if (nfnl_ct != NULL) {
790 			ct = nf_ct_get(skb, &ctinfo);
791 			if (ct != NULL)
792 				size += nfnl_ct->build_size(ct);
793 		}
794 	}
795 #endif
796 	if (pf == NFPROTO_NETDEV || pf == NFPROTO_BRIDGE)
797 		size += nfulnl_get_bridge_size(skb);
798 
799 	qthreshold = inst->qthreshold;
800 	/* per-rule qthreshold overrides per-instance */
801 	if (li->u.ulog.qthreshold)
802 		if (qthreshold > li->u.ulog.qthreshold)
803 			qthreshold = li->u.ulog.qthreshold;
804 
805 
806 	switch (inst->copy_mode) {
807 	case NFULNL_COPY_META:
808 	case NFULNL_COPY_NONE:
809 		data_len = 0;
810 		break;
811 
812 	case NFULNL_COPY_PACKET:
813 		data_len = nfulnl_get_copy_len(li, skb, inst->copy_range);
814 		size += nla_total_size(data_len);
815 		break;
816 
817 	case NFULNL_COPY_DISABLED:
818 	default:
819 		goto unlock_and_release;
820 	}
821 
822 	if (inst->skb && size > skb_tailroom(inst->skb)) {
823 		/* either the queue len is too high or we don't have
824 		 * enough room in the skb left. flush to userspace. */
825 		__nfulnl_flush(inst);
826 	}
827 
828 	if (!inst->skb) {
829 		inst->skb = nfulnl_alloc_skb(net, inst->peer_portid,
830 					     inst->nlbufsiz, size);
831 		if (!inst->skb)
832 			goto alloc_failure;
833 	}
834 
835 	inst->qlen++;
836 
837 	__build_packet_message(log, inst, skb, data_len, pf,
838 				hooknum, in, out, prefix, plen,
839 				nfnl_ct, ct, ctinfo);
840 
841 	if (inst->qlen >= qthreshold)
842 		__nfulnl_flush(inst);
843 	/* timer_pending always called within inst->lock, so there
844 	 * is no chance of a race here */
845 	else if (!timer_pending(&inst->timer)) {
846 		instance_get(inst);
847 		inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
848 		add_timer(&inst->timer);
849 	}
850 
851 unlock_and_release:
852 	spin_unlock_bh(&inst->lock);
853 	instance_put(inst);
854 	return;
855 
856 alloc_failure:
857 	/* FIXME: statistics */
858 	goto unlock_and_release;
859 }
860 
861 static int
862 nfulnl_rcv_nl_event(struct notifier_block *this,
863 		   unsigned long event, void *ptr)
864 {
865 	struct netlink_notify *n = ptr;
866 	struct nfnl_log_net *log = nfnl_log_pernet(n->net);
867 
868 	if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
869 		int i;
870 
871 		/* destroy all instances for this portid */
872 		spin_lock_bh(&log->instances_lock);
873 		for  (i = 0; i < INSTANCE_BUCKETS; i++) {
874 			struct hlist_node *t2;
875 			struct nfulnl_instance *inst;
876 			struct hlist_head *head = &log->instance_table[i];
877 
878 			hlist_for_each_entry_safe(inst, t2, head, hlist) {
879 				if (n->portid == inst->peer_portid)
880 					__instance_destroy(inst);
881 			}
882 		}
883 		spin_unlock_bh(&log->instances_lock);
884 	}
885 	return NOTIFY_DONE;
886 }
887 
888 static struct notifier_block nfulnl_rtnl_notifier = {
889 	.notifier_call	= nfulnl_rcv_nl_event,
890 };
891 
892 static int nfulnl_recv_unsupp(struct sk_buff *skb, const struct nfnl_info *info,
893 			      const struct nlattr * const nfula[])
894 {
895 	return -ENOTSUPP;
896 }
897 
898 static struct nf_logger nfulnl_logger __read_mostly = {
899 	.name	= "nfnetlink_log",
900 	.type	= NF_LOG_TYPE_ULOG,
901 	.logfn	= nfulnl_log_packet,
902 	.me	= THIS_MODULE,
903 };
904 
905 static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
906 	[NFULA_CFG_CMD]		= { .len = sizeof(struct nfulnl_msg_config_cmd) },
907 	[NFULA_CFG_MODE]	= { .len = sizeof(struct nfulnl_msg_config_mode) },
908 	[NFULA_CFG_TIMEOUT]	= { .type = NLA_U32 },
909 	[NFULA_CFG_QTHRESH]	= { .type = NLA_U32 },
910 	[NFULA_CFG_NLBUFSIZ]	= { .type = NLA_U32 },
911 	[NFULA_CFG_FLAGS]	= NLA_POLICY_MASK(NLA_BE16, NFULNL_CFG_F_SEQ |
912 						  NFULNL_CFG_F_SEQ_GLOBAL |
913 						  NFULNL_CFG_F_CONNTRACK),
914 };
915 
916 static int nfulnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,
917 			      const struct nlattr * const nfula[])
918 {
919 	struct nfnl_log_net *log = nfnl_log_pernet(info->net);
920 	u_int16_t group_num = ntohs(info->nfmsg->res_id);
921 	struct nfulnl_msg_config_cmd *cmd = NULL;
922 	struct nfulnl_instance *inst;
923 	u16 flags = 0;
924 	int ret = 0;
925 
926 	if (nfula[NFULA_CFG_CMD]) {
927 		u_int8_t pf = info->nfmsg->nfgen_family;
928 		cmd = nla_data(nfula[NFULA_CFG_CMD]);
929 
930 		/* Commands without queue context */
931 		switch (cmd->command) {
932 		case NFULNL_CFG_CMD_PF_BIND:
933 			return nf_log_bind_pf(info->net, pf, &nfulnl_logger);
934 		case NFULNL_CFG_CMD_PF_UNBIND:
935 			nf_log_unbind_pf(info->net, pf);
936 			return 0;
937 		}
938 	}
939 
940 	inst = instance_lookup_get(log, group_num);
941 	if (inst && inst->peer_portid != NETLINK_CB(skb).portid) {
942 		ret = -EPERM;
943 		goto out_put;
944 	}
945 
946 	/* Check if we support these flags in first place, dependencies should
947 	 * be there too not to break atomicity.
948 	 */
949 	if (nfula[NFULA_CFG_FLAGS]) {
950 		flags = ntohs(nla_get_be16(nfula[NFULA_CFG_FLAGS]));
951 
952 		if ((flags & NFULNL_CFG_F_CONNTRACK) &&
953 		    !rcu_access_pointer(nfnl_ct_hook)) {
954 #ifdef CONFIG_MODULES
955 			nfnl_unlock(NFNL_SUBSYS_ULOG);
956 			request_module("ip_conntrack_netlink");
957 			nfnl_lock(NFNL_SUBSYS_ULOG);
958 			if (rcu_access_pointer(nfnl_ct_hook)) {
959 				ret = -EAGAIN;
960 				goto out_put;
961 			}
962 #endif
963 			ret = -EOPNOTSUPP;
964 			goto out_put;
965 		}
966 	}
967 
968 	if (cmd != NULL) {
969 		switch (cmd->command) {
970 		case NFULNL_CFG_CMD_BIND:
971 			if (inst) {
972 				ret = -EBUSY;
973 				goto out_put;
974 			}
975 
976 			inst = instance_create(info->net, group_num,
977 					       NETLINK_CB(skb).portid,
978 					       sk_user_ns(NETLINK_CB(skb).sk));
979 			if (IS_ERR(inst)) {
980 				ret = PTR_ERR(inst);
981 				goto out;
982 			}
983 			break;
984 		case NFULNL_CFG_CMD_UNBIND:
985 			if (!inst) {
986 				ret = -ENODEV;
987 				goto out;
988 			}
989 
990 			instance_destroy(log, inst);
991 			goto out_put;
992 		default:
993 			ret = -ENOTSUPP;
994 			goto out_put;
995 		}
996 	} else if (!inst) {
997 		ret = -ENODEV;
998 		goto out;
999 	}
1000 
1001 	if (nfula[NFULA_CFG_MODE]) {
1002 		struct nfulnl_msg_config_mode *params =
1003 			nla_data(nfula[NFULA_CFG_MODE]);
1004 
1005 		nfulnl_set_mode(inst, params->copy_mode,
1006 				ntohl(params->copy_range));
1007 	}
1008 
1009 	if (nfula[NFULA_CFG_TIMEOUT]) {
1010 		__be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
1011 
1012 		nfulnl_set_timeout(inst, ntohl(timeout));
1013 	}
1014 
1015 	if (nfula[NFULA_CFG_NLBUFSIZ]) {
1016 		__be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
1017 
1018 		nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
1019 	}
1020 
1021 	if (nfula[NFULA_CFG_QTHRESH]) {
1022 		__be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
1023 
1024 		nfulnl_set_qthresh(inst, ntohl(qthresh));
1025 	}
1026 
1027 	if (nfula[NFULA_CFG_FLAGS])
1028 		nfulnl_set_flags(inst, flags);
1029 
1030 out_put:
1031 	instance_put(inst);
1032 out:
1033 	return ret;
1034 }
1035 
1036 static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
1037 	[NFULNL_MSG_PACKET]	= {
1038 		.call		= nfulnl_recv_unsupp,
1039 		.type		= NFNL_CB_MUTEX,
1040 		.attr_count	= NFULA_MAX,
1041 	},
1042 	[NFULNL_MSG_CONFIG]	= {
1043 		.call		= nfulnl_recv_config,
1044 		.type		= NFNL_CB_MUTEX,
1045 		.attr_count	= NFULA_CFG_MAX,
1046 		.policy		= nfula_cfg_policy
1047 	},
1048 };
1049 
1050 static const struct nfnetlink_subsystem nfulnl_subsys = {
1051 	.name		= "log",
1052 	.subsys_id	= NFNL_SUBSYS_ULOG,
1053 	.cb_count	= NFULNL_MSG_MAX,
1054 	.cb		= nfulnl_cb,
1055 };
1056 
1057 #ifdef CONFIG_PROC_FS
1058 struct iter_state {
1059 	struct seq_net_private p;
1060 	unsigned int bucket;
1061 };
1062 
1063 static struct hlist_node *get_first(struct net *net, struct iter_state *st)
1064 {
1065 	struct nfnl_log_net *log;
1066 	if (!st)
1067 		return NULL;
1068 
1069 	log = nfnl_log_pernet(net);
1070 
1071 	for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
1072 		struct hlist_head *head = &log->instance_table[st->bucket];
1073 
1074 		if (!hlist_empty(head))
1075 			return rcu_dereference(hlist_first_rcu(head));
1076 	}
1077 	return NULL;
1078 }
1079 
1080 static struct hlist_node *get_next(struct net *net, struct iter_state *st,
1081 				   struct hlist_node *h)
1082 {
1083 	h = rcu_dereference(hlist_next_rcu(h));
1084 	while (!h) {
1085 		struct nfnl_log_net *log;
1086 		struct hlist_head *head;
1087 
1088 		if (++st->bucket >= INSTANCE_BUCKETS)
1089 			return NULL;
1090 
1091 		log = nfnl_log_pernet(net);
1092 		head = &log->instance_table[st->bucket];
1093 		h = rcu_dereference(hlist_first_rcu(head));
1094 	}
1095 	return h;
1096 }
1097 
1098 static struct hlist_node *get_idx(struct net *net, struct iter_state *st,
1099 				  loff_t pos)
1100 {
1101 	struct hlist_node *head;
1102 	head = get_first(net, st);
1103 
1104 	if (head)
1105 		while (pos && (head = get_next(net, st, head)))
1106 			pos--;
1107 	return pos ? NULL : head;
1108 }
1109 
1110 static void *seq_start(struct seq_file *s, loff_t *pos)
1111 	__acquires(rcu)
1112 {
1113 	rcu_read_lock();
1114 	return get_idx(seq_file_net(s), s->private, *pos);
1115 }
1116 
1117 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1118 {
1119 	(*pos)++;
1120 	return get_next(seq_file_net(s), s->private, v);
1121 }
1122 
1123 static void seq_stop(struct seq_file *s, void *v)
1124 	__releases(rcu)
1125 {
1126 	rcu_read_unlock();
1127 }
1128 
1129 static int seq_show(struct seq_file *s, void *v)
1130 {
1131 	const struct nfulnl_instance *inst = v;
1132 
1133 	seq_printf(s, "%5u %6u %5u %1u %5u %6u %2u\n",
1134 		   inst->group_num,
1135 		   inst->peer_portid, inst->qlen,
1136 		   inst->copy_mode, inst->copy_range,
1137 		   inst->flushtimeout, refcount_read(&inst->use));
1138 
1139 	return 0;
1140 }
1141 
1142 static const struct seq_operations nful_seq_ops = {
1143 	.start	= seq_start,
1144 	.next	= seq_next,
1145 	.stop	= seq_stop,
1146 	.show	= seq_show,
1147 };
1148 #endif /* PROC_FS */
1149 
1150 static int __net_init nfnl_log_net_init(struct net *net)
1151 {
1152 	unsigned int i;
1153 	struct nfnl_log_net *log = nfnl_log_pernet(net);
1154 #ifdef CONFIG_PROC_FS
1155 	struct proc_dir_entry *proc;
1156 	kuid_t root_uid;
1157 	kgid_t root_gid;
1158 #endif
1159 
1160 	for (i = 0; i < INSTANCE_BUCKETS; i++)
1161 		INIT_HLIST_HEAD(&log->instance_table[i]);
1162 	spin_lock_init(&log->instances_lock);
1163 
1164 #ifdef CONFIG_PROC_FS
1165 	proc = proc_create_net("nfnetlink_log", 0440, net->nf.proc_netfilter,
1166 			&nful_seq_ops, sizeof(struct iter_state));
1167 	if (!proc)
1168 		return -ENOMEM;
1169 
1170 	root_uid = make_kuid(net->user_ns, 0);
1171 	root_gid = make_kgid(net->user_ns, 0);
1172 	if (uid_valid(root_uid) && gid_valid(root_gid))
1173 		proc_set_user(proc, root_uid, root_gid);
1174 #endif
1175 	return 0;
1176 }
1177 
1178 static void __net_exit nfnl_log_net_pre_exit(struct net *net)
1179 {
1180 #ifdef CONFIG_PROC_FS
1181 	remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter);
1182 #endif
1183 	nf_log_unset(net, &nfulnl_logger);
1184 }
1185 
1186 static void __net_exit nfnl_log_net_exit(struct net *net)
1187 {
1188 	struct nfnl_log_net *log = nfnl_log_pernet(net);
1189 	unsigned int i;
1190 
1191 	for (i = 0; i < INSTANCE_BUCKETS; i++)
1192 		WARN_ON_ONCE(!hlist_empty(&log->instance_table[i]));
1193 }
1194 
1195 static struct pernet_operations nfnl_log_net_ops = {
1196 	.init	= nfnl_log_net_init,
1197 	.pre_exit = nfnl_log_net_pre_exit,
1198 	.exit	= nfnl_log_net_exit,
1199 	.id	= &nfnl_log_net_id,
1200 	.size	= sizeof(struct nfnl_log_net),
1201 };
1202 
1203 static int __init nfnetlink_log_init(void)
1204 {
1205 	int status;
1206 
1207 	status = register_pernet_subsys(&nfnl_log_net_ops);
1208 	if (status < 0) {
1209 		pr_err("failed to register pernet ops\n");
1210 		goto out;
1211 	}
1212 
1213 	netlink_register_notifier(&nfulnl_rtnl_notifier);
1214 	status = nfnetlink_subsys_register(&nfulnl_subsys);
1215 	if (status < 0) {
1216 		pr_err("failed to create netlink socket\n");
1217 		goto cleanup_netlink_notifier;
1218 	}
1219 
1220 	status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
1221 	if (status < 0) {
1222 		pr_err("failed to register logger\n");
1223 		goto cleanup_subsys;
1224 	}
1225 
1226 	return status;
1227 
1228 cleanup_subsys:
1229 	nfnetlink_subsys_unregister(&nfulnl_subsys);
1230 cleanup_netlink_notifier:
1231 	netlink_unregister_notifier(&nfulnl_rtnl_notifier);
1232 	unregister_pernet_subsys(&nfnl_log_net_ops);
1233 out:
1234 	return status;
1235 }
1236 
1237 static void __exit nfnetlink_log_fini(void)
1238 {
1239 	nfnetlink_subsys_unregister(&nfulnl_subsys);
1240 	netlink_unregister_notifier(&nfulnl_rtnl_notifier);
1241 	nf_log_unregister(&nfulnl_logger);
1242 	unregister_pernet_subsys(&nfnl_log_net_ops);
1243 }
1244 
1245 MODULE_DESCRIPTION("netfilter userspace logging");
1246 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1247 MODULE_LICENSE("GPL");
1248 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
1249 MODULE_ALIAS_NF_LOGGER(AF_INET, 1);
1250 MODULE_ALIAS_NF_LOGGER(AF_INET6, 1);
1251 MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1);
1252 MODULE_ALIAS_NF_LOGGER(3, 1); /* NFPROTO_ARP */
1253 MODULE_ALIAS_NF_LOGGER(5, 1); /* NFPROTO_NETDEV */
1254 
1255 module_init(nfnetlink_log_init);
1256 module_exit(nfnetlink_log_fini);
1257