nfnetlink_log.c 22.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * This is a module which is used for logging packets to userspace via
 * nfetlink.
 *
 * (C) 2005 by Harald Welte <laforge@netfilter.org>
 *
 * Based on the old ipv4-only ipt_ULOG.c:
 * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/netlink.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nfnetlink_log.h>
#include <linux/spinlock.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/security.h>
#include <linux/list.h>
#include <linux/jhash.h>
#include <linux/random.h>
#include <net/sock.h>
32
#include <net/netfilter/nf_log.h>
33 34 35

#include <asm/atomic.h>

36 37 38 39
#ifdef CONFIG_BRIDGE_NETFILTER
#include "../bridge/br_private.h"
#endif

40
#define NFULNL_NLBUFSIZ_DEFAULT	NLMSG_GOODSIZE
41
#define NFULNL_TIMEOUT_DEFAULT 	HZ	/* every second */
42
#define NFULNL_QTHRESH_DEFAULT 	100	/* 100 packets */
43
#define NFULNL_COPY_RANGE_MAX	0xFFFF	/* max packet size is limited by 16-bit struct nfattr nfa_len field */
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

#define PRINTR(x, args...)	do { if (net_ratelimit()) \
				     printk(x, ## args); } while (0);

struct nfulnl_instance {
	struct hlist_node hlist;	/* global list of instances */
	spinlock_t lock;
	atomic_t use;			/* use count */

	unsigned int qlen;		/* number of nlmsgs in skb */
	struct sk_buff *skb;		/* pre-allocatd skb */
	struct timer_list timer;
	int peer_pid;			/* PID of the peer process */

	/* configurable parameters */
	unsigned int flushtimeout;	/* timeout until queue flush */
	unsigned int nlbufsiz;		/* netlink buffer allocation size */
	unsigned int qthreshold;	/* threshold of the queue */
	u_int32_t copy_range;
63
	u_int32_t seq;			/* instance-local sequential counter */
64
	u_int16_t group_num;		/* number of this queue */
65
	u_int16_t flags;
66
	u_int8_t copy_mode;
67 68 69
};

static DEFINE_RWLOCK(instances_lock);
70
static atomic_t global_seq;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

#define INSTANCE_BUCKETS	16
static struct hlist_head instance_table[INSTANCE_BUCKETS];
static unsigned int hash_init;

static inline u_int8_t instance_hashfn(u_int16_t group_num)
{
	return ((group_num & 0xff) % INSTANCE_BUCKETS);
}

static struct nfulnl_instance *
__instance_lookup(u_int16_t group_num)
{
	struct hlist_head *head;
	struct hlist_node *pos;
	struct nfulnl_instance *inst;

	head = &instance_table[instance_hashfn(group_num)];
	hlist_for_each_entry(inst, pos, head, hlist) {
		if (inst->group_num == group_num)
			return inst;
	}
	return NULL;
}

static inline void
instance_get(struct nfulnl_instance *inst)
{
	atomic_inc(&inst->use);
}

static struct nfulnl_instance *
instance_lookup_get(u_int16_t group_num)
{
	struct nfulnl_instance *inst;

	read_lock_bh(&instances_lock);
	inst = __instance_lookup(group_num);
	if (inst)
		instance_get(inst);
	read_unlock_bh(&instances_lock);

	return inst;
}

static void
instance_put(struct nfulnl_instance *inst)
{
	if (inst && atomic_dec_and_test(&inst->use)) {
		kfree(inst);
121
		module_put(THIS_MODULE);
122 123 124 125 126 127 128 129 130
	}
}

static void nfulnl_timer(unsigned long data);

static struct nfulnl_instance *
instance_create(u_int16_t group_num, int pid)
{
	struct nfulnl_instance *inst;
131
	int err;
132

133
	write_lock_bh(&instances_lock);
134
	if (__instance_lookup(group_num)) {
135
		err = -EEXIST;
136 137 138
		goto out_unlock;
	}

H
Harald Welte 已提交
139
	inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
140 141
	if (!inst) {
		err = -ENOMEM;
142
		goto out_unlock;
143
	}
144

145 146
	if (!try_module_get(THIS_MODULE)) {
		kfree(inst);
147
		err = -EAGAIN;
148 149 150
		goto out_unlock;
	}

151
	INIT_HLIST_NODE(&inst->hlist);
152
	spin_lock_init(&inst->lock);
153 154 155
	/* needs to be two, since we _put() after creation */
	atomic_set(&inst->use, 2);

P
Patrick McHardy 已提交
156
	setup_timer(&inst->timer, nfulnl_timer, (unsigned long)inst);
157 158 159 160 161 162 163 164

	inst->peer_pid = pid;
	inst->group_num = group_num;

	inst->qthreshold 	= NFULNL_QTHRESH_DEFAULT;
	inst->flushtimeout 	= NFULNL_TIMEOUT_DEFAULT;
	inst->nlbufsiz 		= NFULNL_NLBUFSIZ_DEFAULT;
	inst->copy_mode 	= NFULNL_COPY_PACKET;
165
	inst->copy_range 	= NFULNL_COPY_RANGE_MAX;
166

167
	hlist_add_head(&inst->hlist,
168 169 170 171 172 173 174 175
		       &instance_table[instance_hashfn(group_num)]);

	write_unlock_bh(&instances_lock);

	return inst;

out_unlock:
	write_unlock_bh(&instances_lock);
176
	return ERR_PTR(err);
177 178
}

179
static void __nfulnl_flush(struct nfulnl_instance *inst);
180 181

static void
182
__instance_destroy(struct nfulnl_instance *inst)
183 184 185 186 187 188 189
{
	/* first pull it out of the global list */
	hlist_del(&inst->hlist);

	/* then flush all pending packets from skb */

	spin_lock_bh(&inst->lock);
190 191
	if (inst->skb)
		__nfulnl_flush(inst);
192 193 194 195 196 197 198 199 200
	spin_unlock_bh(&inst->lock);

	/* and finally put the refcount */
	instance_put(inst);
}

static inline void
instance_destroy(struct nfulnl_instance *inst)
{
201 202 203
	write_lock_bh(&instances_lock);
	__instance_destroy(inst);
	write_unlock_bh(&instances_lock);
204 205 206 207 208 209 210 211 212
}

static int
nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
		  unsigned int range)
{
	int status = 0;

	spin_lock_bh(&inst->lock);
213

214 215 216 217 218 219
	switch (mode) {
	case NFULNL_COPY_NONE:
	case NFULNL_COPY_META:
		inst->copy_mode = mode;
		inst->copy_range = 0;
		break;
220

221 222
	case NFULNL_COPY_PACKET:
		inst->copy_mode = mode;
223 224
		inst->copy_range = min_t(unsigned int,
					 range, NFULNL_COPY_RANGE_MAX);
225
		break;
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	default:
		status = -EINVAL;
		break;
	}

	spin_unlock_bh(&inst->lock);

	return status;
}

static int
nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
{
	int status;

	spin_lock_bh(&inst->lock);
	if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
		status = -ERANGE;
	else if (nlbufsiz > 131072)
		status = -ERANGE;
	else {
		inst->nlbufsiz = nlbufsiz;
		status = 0;
	}
	spin_unlock_bh(&inst->lock);

	return status;
}

static int
nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
{
	spin_lock_bh(&inst->lock);
	inst->flushtimeout = timeout;
	spin_unlock_bh(&inst->lock);

	return 0;
}

static int
nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
{
	spin_lock_bh(&inst->lock);
	inst->qthreshold = qthresh;
	spin_unlock_bh(&inst->lock);

	return 0;
}

276 277 278 279
static int
nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
{
	spin_lock_bh(&inst->lock);
280
	inst->flags = flags;
281 282 283 284 285
	spin_unlock_bh(&inst->lock);

	return 0;
}

286 287
static struct sk_buff *
nfulnl_alloc_skb(unsigned int inst_size, unsigned int pkt_size)
288 289
{
	struct sk_buff *skb;
290
	unsigned int n;
291 292 293 294

	/* alloc skb which should be big enough for a whole multipart
	 * message.  WARNING: has to be <= 128k due to slab restrictions */

295 296
	n = max(inst_size, pkt_size);
	skb = alloc_skb(n, GFP_ATOMIC);
297 298 299 300
	if (!skb) {
		PRINTR("nfnetlink_log: can't alloc whole buffer (%u bytes)\n",
			inst_size);

301 302 303
		if (n > pkt_size) {
			/* try to allocate only as much as we need for current
			 * packet */
304

305 306 307 308 309
			skb = alloc_skb(pkt_size, GFP_ATOMIC);
			if (!skb)
				PRINTR("nfnetlink_log: can't even alloc %u "
				       "bytes\n", pkt_size);
		}
310 311 312 313 314 315 316 317
	}

	return skb;
}

static int
__nfulnl_send(struct nfulnl_instance *inst)
{
318
	int status = -1;
319 320

	if (inst->qlen > 1)
321 322 323
		NLMSG_PUT(inst->skb, 0, 0,
			  NLMSG_DONE,
			  sizeof(struct nfgenmsg));
324 325 326 327 328 329

	status = nfnetlink_unicast(inst->skb, inst->peer_pid, MSG_DONTWAIT);

	inst->qlen = 0;
	inst->skb = NULL;

330
nlmsg_failure:
331 332 333
	return status;
}

334 335 336 337 338 339 340 341 342 343
static void
__nfulnl_flush(struct nfulnl_instance *inst)
{
	/* timer holds a reference */
	if (del_timer(&inst->timer))
		instance_put(inst);
	if (inst->skb)
		__nfulnl_send(inst);
}

344 345
static void
nfulnl_timer(unsigned long data)
346
{
347
	struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
348 349

	spin_lock_bh(&inst->lock);
350 351
	if (inst->skb)
		__nfulnl_send(inst);
352
	spin_unlock_bh(&inst->lock);
353
	instance_put(inst);
354 355
}

356 357
/* This is an inline function, we don't really care about a long
 * list of arguments */
358
static inline int
359
__build_packet_message(struct nfulnl_instance *inst,
360
			const struct sk_buff *skb,
361 362 363 364 365 366
			unsigned int data_len,
			unsigned int pf,
			unsigned int hooknum,
			const struct net_device *indev,
			const struct net_device *outdev,
			const struct nf_loginfo *li,
367
			const char *prefix, unsigned int plen)
368 369 370 371
{
	struct nfulnl_msg_packet_hdr pmsg;
	struct nlmsghdr *nlh;
	struct nfgenmsg *nfmsg;
A
Al Viro 已提交
372
	__be32 tmp_uint;
373
	sk_buff_data_t old_tail = inst->skb->tail;
374

375
	nlh = NLMSG_PUT(inst->skb, 0, 0,
376 377 378 379 380 381 382
			NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET,
			sizeof(struct nfgenmsg));
	nfmsg = NLMSG_DATA(nlh);
	nfmsg->nfgen_family = pf;
	nfmsg->version = NFNETLINK_V0;
	nfmsg->res_id = htons(inst->group_num);

383
	pmsg.hw_protocol	= skb->protocol;
384 385
	pmsg.hook		= hooknum;

386
	NLA_PUT(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg);
387

388
	if (prefix)
389
		NLA_PUT(inst->skb, NFULA_PREFIX, plen, prefix);
390 391

	if (indev) {
392
#ifndef CONFIG_BRIDGE_NETFILTER
393 394
		NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
			     htonl(indev->ifindex));
395 396 397 398 399
#else
		if (pf == PF_BRIDGE) {
			/* Case 1: outdev is physical input device, we need to
			 * look for bridge group (when called from
			 * netfilter_bridge) */
400 401
			NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
				     htonl(indev->ifindex));
402
			/* this is the bridge group "brX" */
403 404
			NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
				     htonl(indev->br_port->br->dev->ifindex));
405 406 407
		} else {
			/* Case 2: indev is bridge group, we need to look for
			 * physical device (when called from ipv4) */
408 409 410 411 412
			NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
				     htonl(indev->ifindex));
			if (skb->nf_bridge && skb->nf_bridge->physindev)
				NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
					     htonl(skb->nf_bridge->physindev->ifindex));
413 414
		}
#endif
415 416 417 418
	}

	if (outdev) {
		tmp_uint = htonl(outdev->ifindex);
419
#ifndef CONFIG_BRIDGE_NETFILTER
420 421
		NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
			     htonl(outdev->ifindex));
422 423 424 425 426
#else
		if (pf == PF_BRIDGE) {
			/* Case 1: outdev is physical output device, we need to
			 * look for bridge group (when called from
			 * netfilter_bridge) */
427 428
			NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
				     htonl(outdev->ifindex));
429
			/* this is the bridge group "brX" */
430 431
			NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
				     htonl(outdev->br_port->br->dev->ifindex));
432 433 434
		} else {
			/* Case 2: indev is a bridge group, we need to look
			 * for physical device (when called from ipv4) */
435 436 437 438 439
			NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
				     htonl(outdev->ifindex));
			if (skb->nf_bridge && skb->nf_bridge->physoutdev)
				NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
					     htonl(skb->nf_bridge->physoutdev->ifindex));
440 441
		}
#endif
442 443
	}

444 445
	if (skb->mark)
		NLA_PUT_BE32(inst->skb, NFULA_MARK, htonl(skb->mark));
446

S
Stephen Hemminger 已提交
447
	if (indev && skb->dev) {
448
		struct nfulnl_msg_packet_hw phw;
S
Stephen Hemminger 已提交
449 450 451
		int len = dev_parse_header(skb, phw.hw_addr);
		if (len > 0) {
			phw.hw_addrlen = htons(len);
452
			NLA_PUT(inst->skb, NFULA_HWADDR, sizeof(phw), &phw);
S
Stephen Hemminger 已提交
453
		}
454 455
	}

456
	if (skb->tstamp.tv64) {
457
		struct nfulnl_msg_packet_timestamp ts;
458 459 460
		struct timeval tv = ktime_to_timeval(skb->tstamp);
		ts.sec = cpu_to_be64(tv.tv_sec);
		ts.usec = cpu_to_be64(tv.tv_usec);
461

462
		NLA_PUT(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts);
463 464 465 466 467 468
	}

	/* UID */
	if (skb->sk) {
		read_lock_bh(&skb->sk->sk_callback_lock);
		if (skb->sk->sk_socket && skb->sk->sk_socket->file) {
A
Al Viro 已提交
469
			__be32 uid = htonl(skb->sk->sk_socket->file->f_uid);
470
			/* need to unlock here since NLA_PUT may goto */
471
			read_unlock_bh(&skb->sk->sk_callback_lock);
472
			NLA_PUT_BE32(inst->skb, NFULA_UID, uid);
473 474 475 476
		} else
			read_unlock_bh(&skb->sk->sk_callback_lock);
	}

477
	/* local sequence number */
478 479 480
	if (inst->flags & NFULNL_CFG_F_SEQ)
		NLA_PUT_BE32(inst->skb, NFULA_SEQ, htonl(inst->seq++));

481
	/* global sequence number */
482 483 484
	if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
		NLA_PUT_BE32(inst->skb, NFULA_SEQ_GLOBAL,
			     htonl(atomic_inc_return(&global_seq)));
485

486
	if (data_len) {
487 488
		struct nlattr *nla;
		int size = nla_attr_size(data_len);
489

490
		if (skb_tailroom(inst->skb) < nla_total_size(data_len)) {
491 492 493 494
			printk(KERN_WARNING "nfnetlink_log: no tailroom!\n");
			goto nlmsg_failure;
		}

495 496 497
		nla = (struct nlattr *)skb_put(inst->skb, nla_total_size(data_len));
		nla->nla_type = NFULA_PAYLOAD;
		nla->nla_len = size;
498

499
		if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
500 501
			BUG();
	}
502

503 504 505 506
	nlh->nlmsg_len = inst->skb->tail - old_tail;
	return 0;

nlmsg_failure:
507
nla_put_failure:
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
	return -1;
}

#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)

static struct nf_loginfo default_loginfo = {
	.type =		NF_LOG_TYPE_ULOG,
	.u = {
		.ulog = {
			.copy_len	= 0xffff,
			.group		= 0,
			.qthreshold	= 1,
		},
	},
};

/* log handler for internal netfilter logging api */
static void
nfulnl_log_packet(unsigned int pf,
		  unsigned int hooknum,
		  const struct sk_buff *skb,
		  const struct net_device *in,
		  const struct net_device *out,
		  const struct nf_loginfo *li_user,
		  const char *prefix)
{
	unsigned int size, data_len;
	struct nfulnl_instance *inst;
	const struct nf_loginfo *li;
	unsigned int qthreshold;
539
	unsigned int plen;
540

541
	if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
542 543 544 545 546 547 548 549
		li = li_user;
	else
		li = &default_loginfo;

	inst = instance_lookup_get(li->u.ulog.group);
	if (!inst)
		return;

550 551
	plen = 0;
	if (prefix)
552
		plen = strlen(prefix) + 1;
553

554 555 556
	/* FIXME: do we want to make the size calculation conditional based on
	 * what is actually present?  way more branches and checks, but more
	 * memory efficient... */
557 558 559 560
	size =    NLMSG_ALIGN(sizeof(struct nfgenmsg))
		+ nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
561
#ifdef CONFIG_BRIDGE_NETFILTER
562 563
		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
		+ nla_total_size(sizeof(u_int32_t))	/* ifindex */
564
#endif
565 566 567 568 569
		+ nla_total_size(sizeof(u_int32_t))	/* mark */
		+ nla_total_size(sizeof(u_int32_t))	/* uid */
		+ nla_total_size(plen)			/* prefix */
		+ nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
		+ nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp));
570 571 572

	spin_lock_bh(&inst->lock);

573
	if (inst->flags & NFULNL_CFG_F_SEQ)
574
		size += nla_total_size(sizeof(u_int32_t));
575
	if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
576
		size += nla_total_size(sizeof(u_int32_t));
577

578 579 580 581
	qthreshold = inst->qthreshold;
	/* per-rule qthreshold overrides per-instance */
	if (qthreshold > li->u.ulog.qthreshold)
		qthreshold = li->u.ulog.qthreshold;
582

583 584 585 586 587
	switch (inst->copy_mode) {
	case NFULNL_COPY_META:
	case NFULNL_COPY_NONE:
		data_len = 0;
		break;
588

589
	case NFULNL_COPY_PACKET:
590
		if (inst->copy_range == 0
591 592 593 594
		    || inst->copy_range > skb->len)
			data_len = skb->len;
		else
			data_len = inst->copy_range;
595

596
		size += nla_total_size(data_len);
597
		break;
598

599
	default:
600
		goto unlock_and_release;
601 602
	}

603 604
	if (inst->skb &&
	    size > skb_tailroom(inst->skb) - sizeof(struct nfgenmsg)) {
605 606
		/* either the queue len is too high or we don't have
		 * enough room in the skb left. flush to userspace. */
607
		__nfulnl_flush(inst);
608
	}
609

610 611 612
	if (!inst->skb) {
		inst->skb = nfulnl_alloc_skb(inst->nlbufsiz, size);
		if (!inst->skb)
613 614 615 616 617 618
			goto alloc_failure;
	}

	inst->qlen++;

	__build_packet_message(inst, skb, data_len, pf,
619
				hooknum, in, out, li, prefix, plen);
620

621 622
	if (inst->qlen >= qthreshold)
		__nfulnl_flush(inst);
623 624
	/* timer_pending always called within inst->lock, so there
	 * is no chance of a race here */
625
	else if (!timer_pending(&inst->timer)) {
626 627 628 629 630
		instance_get(inst);
		inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
		add_timer(&inst->timer);
	}

631 632 633
unlock_and_release:
	spin_unlock_bh(&inst->lock);
	instance_put(inst);
634 635 636 637
	return;

alloc_failure:
	/* FIXME: statistics */
638
	goto unlock_and_release;
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
}

static int
nfulnl_rcv_nl_event(struct notifier_block *this,
		   unsigned long event, void *ptr)
{
	struct netlink_notify *n = ptr;

	if (event == NETLINK_URELEASE &&
	    n->protocol == NETLINK_NETFILTER && n->pid) {
		int i;

		/* destroy all instances for this pid */
		write_lock_bh(&instances_lock);
		for  (i = 0; i < INSTANCE_BUCKETS; i++) {
			struct hlist_node *tmp, *t2;
			struct nfulnl_instance *inst;
			struct hlist_head *head = &instance_table[i];

			hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
659 660
				if ((n->net == &init_net) &&
				    (n->pid == inst->peer_pid))
661 662 663 664 665 666 667 668 669 670 671 672 673 674
					__instance_destroy(inst);
			}
		}
		write_unlock_bh(&instances_lock);
	}
	return NOTIFY_DONE;
}

static struct notifier_block nfulnl_rtnl_notifier = {
	.notifier_call	= nfulnl_rcv_nl_event,
};

static int
nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
675
		  struct nlmsghdr *nlh, struct nlattr *nfqa[])
676 677 678 679
{
	return -ENOTSUPP;
}

680
static const struct nf_logger nfulnl_logger = {
681 682 683 684 685
	.name	= "nfnetlink_log",
	.logfn	= &nfulnl_log_packet,
	.me	= THIS_MODULE,
};

686 687 688 689 690 691 692
static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
	[NFULA_CFG_CMD]		= { .len = sizeof(struct nfulnl_msg_config_cmd) },
	[NFULA_CFG_MODE]	= { .len = sizeof(struct nfulnl_msg_config_mode) },
	[NFULA_CFG_TIMEOUT]	= { .type = NLA_U32 },
	[NFULA_CFG_QTHRESH]	= { .type = NLA_U32 },
	[NFULA_CFG_NLBUFSIZ]	= { .type = NLA_U32 },
	[NFULA_CFG_FLAGS]	= { .type = NLA_U16 },
693 694 695 696
};

static int
nfulnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
697
		   struct nlmsghdr *nlh, struct nlattr *nfula[])
698 699 700 701 702 703 704
{
	struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
	u_int16_t group_num = ntohs(nfmsg->res_id);
	struct nfulnl_instance *inst;
	int ret = 0;

	inst = instance_lookup_get(group_num);
705 706 707 708 709
	if (inst && inst->peer_pid != NETLINK_CB(skb).pid) {
		ret = -EPERM;
		goto out_put;
	}

710
	if (nfula[NFULA_CFG_CMD]) {
711 712
		u_int8_t pf = nfmsg->nfgen_family;
		struct nfulnl_msg_config_cmd *cmd;
713

714
		cmd = nla_data(nfula[NFULA_CFG_CMD]);
715 716 717 718 719 720 721 722 723 724

		switch (cmd->command) {
		case NFULNL_CFG_CMD_BIND:
			if (inst) {
				ret = -EBUSY;
				goto out_put;
			}

			inst = instance_create(group_num,
					       NETLINK_CB(skb).pid);
725 726
			if (IS_ERR(inst)) {
				ret = PTR_ERR(inst);
727
				goto out;
728 729 730 731 732
			}
			break;
		case NFULNL_CFG_CMD_UNBIND:
			if (!inst) {
				ret = -ENODEV;
733
				goto out;
734 735 736
			}

			instance_destroy(inst);
737
			goto out;
738 739 740 741 742 743 744 745 746
		case NFULNL_CFG_CMD_PF_BIND:
			ret = nf_log_register(pf, &nfulnl_logger);
			break;
		case NFULNL_CFG_CMD_PF_UNBIND:
			/* This is a bug and a feature.  We cannot unregister
			 * other handlers, like nfnetlink_inst can */
			nf_log_unregister_pf(pf);
			break;
		default:
747
			ret = -ENOTSUPP;
748 749 750 751
			break;
		}
	}

752
	if (nfula[NFULA_CFG_MODE]) {
753
		struct nfulnl_msg_config_mode *params;
754
		params = nla_data(nfula[NFULA_CFG_MODE]);
755

756 757 758 759
		if (!inst) {
			ret = -ENODEV;
			goto out;
		}
760
		nfulnl_set_mode(inst, params->copy_mode,
761
				ntohl(params->copy_range));
762 763
	}

764
	if (nfula[NFULA_CFG_TIMEOUT]) {
765
		__be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
766

767 768 769 770
		if (!inst) {
			ret = -ENODEV;
			goto out;
		}
771 772 773
		nfulnl_set_timeout(inst, ntohl(timeout));
	}

774
	if (nfula[NFULA_CFG_NLBUFSIZ]) {
775
		__be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
776

777 778 779 780
		if (!inst) {
			ret = -ENODEV;
			goto out;
		}
781 782 783
		nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
	}

784
	if (nfula[NFULA_CFG_QTHRESH]) {
785
		__be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
786

787 788 789 790
		if (!inst) {
			ret = -ENODEV;
			goto out;
		}
791 792 793
		nfulnl_set_qthresh(inst, ntohl(qthresh));
	}

794
	if (nfula[NFULA_CFG_FLAGS]) {
795
		__be16 flags = nla_get_be16(nfula[NFULA_CFG_FLAGS]);
796 797 798 799 800

		if (!inst) {
			ret = -ENODEV;
			goto out;
		}
801
		nfulnl_set_flags(inst, ntohs(flags));
802 803
	}

804 805
out_put:
	instance_put(inst);
806
out:
807 808 809
	return ret;
}

810
static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
811
	[NFULNL_MSG_PACKET]	= { .call = nfulnl_recv_unsupp,
812
				    .attr_count = NFULA_MAX, },
813
	[NFULNL_MSG_CONFIG]	= { .call = nfulnl_recv_config,
814 815
				    .attr_count = NFULA_CFG_MAX,
				    .policy = nfula_cfg_policy },
816 817
};

818
static const struct nfnetlink_subsystem nfulnl_subsys = {
819 820 821 822 823 824 825 826 827 828 829
	.name		= "log",
	.subsys_id	= NFNL_SUBSYS_ULOG,
	.cb_count	= NFULNL_MSG_MAX,
	.cb		= nfulnl_cb,
};

#ifdef CONFIG_PROC_FS
struct iter_state {
	unsigned int bucket;
};

830
static struct hlist_node *get_first(struct iter_state *st)
831 832 833 834 835 836 837 838 839 840 841
{
	if (!st)
		return NULL;

	for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
		if (!hlist_empty(&instance_table[st->bucket]))
			return instance_table[st->bucket].first;
	}
	return NULL;
}

842
static struct hlist_node *get_next(struct iter_state *st, struct hlist_node *h)
843 844 845 846 847 848 849 850 851 852 853
{
	h = h->next;
	while (!h) {
		if (++st->bucket >= INSTANCE_BUCKETS)
			return NULL;

		h = instance_table[st->bucket].first;
	}
	return h;
}

854
static struct hlist_node *get_idx(struct iter_state *st, loff_t pos)
855 856
{
	struct hlist_node *head;
857
	head = get_first(st);
858 859

	if (head)
860
		while (pos && (head = get_next(st, head)))
861 862 863 864 865 866 867
			pos--;
	return pos ? NULL : head;
}

static void *seq_start(struct seq_file *seq, loff_t *pos)
{
	read_lock_bh(&instances_lock);
868
	return get_idx(seq->private, *pos);
869 870 871 872 873
}

static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
{
	(*pos)++;
874
	return get_next(s->private, v);
875 876 877 878 879 880 881 882 883 884 885
}

static void seq_stop(struct seq_file *s, void *v)
{
	read_unlock_bh(&instances_lock);
}

static int seq_show(struct seq_file *s, void *v)
{
	const struct nfulnl_instance *inst = v;

886
	return seq_printf(s, "%5d %6d %5d %1d %5d %6d %2d\n",
887
			  inst->group_num,
888
			  inst->peer_pid, inst->qlen,
889 890 891 892
			  inst->copy_mode, inst->copy_range,
			  inst->flushtimeout, atomic_read(&inst->use));
}

893
static const struct seq_operations nful_seq_ops = {
894 895 896 897 898 899 900 901
	.start	= seq_start,
	.next	= seq_next,
	.stop	= seq_stop,
	.show	= seq_show,
};

static int nful_open(struct inode *inode, struct file *file)
{
902 903
	return seq_open_private(file, &nful_seq_ops,
			sizeof(struct iter_state));
904 905
}

906
static const struct file_operations nful_file_ops = {
907 908 909 910 911 912 913 914 915
	.owner	 = THIS_MODULE,
	.open	 = nful_open,
	.read	 = seq_read,
	.llseek	 = seq_lseek,
	.release = seq_release_private,
};

#endif /* PROC_FS */

916
static int __init nfnetlink_log_init(void)
917 918 919 920 921
{
	int i, status = -ENOMEM;
#ifdef CONFIG_PROC_FS
	struct proc_dir_entry *proc_nful;
#endif
922

923 924
	for (i = 0; i < INSTANCE_BUCKETS; i++)
		INIT_HLIST_HEAD(&instance_table[i]);
925

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
	/* it's not really all that important to have a random value, so
	 * we can do this from the init function, even if there hasn't
	 * been that much entropy yet */
	get_random_bytes(&hash_init, sizeof(hash_init));

	netlink_register_notifier(&nfulnl_rtnl_notifier);
	status = nfnetlink_subsys_register(&nfulnl_subsys);
	if (status < 0) {
		printk(KERN_ERR "log: failed to create netlink socket\n");
		goto cleanup_netlink_notifier;
	}

#ifdef CONFIG_PROC_FS
	proc_nful = create_proc_entry("nfnetlink_log", 0440,
				      proc_net_netfilter);
	if (!proc_nful)
		goto cleanup_subsys;
	proc_nful->proc_fops = &nful_file_ops;
#endif
	return status;

#ifdef CONFIG_PROC_FS
cleanup_subsys:
	nfnetlink_subsys_unregister(&nfulnl_subsys);
950
#endif
951 952 953 954 955
cleanup_netlink_notifier:
	netlink_unregister_notifier(&nfulnl_rtnl_notifier);
	return status;
}

956
static void __exit nfnetlink_log_fini(void)
957
{
958
	nf_log_unregister(&nfulnl_logger);
959 960 961 962 963
#ifdef CONFIG_PROC_FS
	remove_proc_entry("nfnetlink_log", proc_net_netfilter);
#endif
	nfnetlink_subsys_unregister(&nfulnl_subsys);
	netlink_unregister_notifier(&nfulnl_rtnl_notifier);
964 965 966 967 968
}

MODULE_DESCRIPTION("netfilter userspace logging");
MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
MODULE_LICENSE("GPL");
969
MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
970

971 972
module_init(nfnetlink_log_init);
module_exit(nfnetlink_log_fini);