Skip to contentSkip to conversation

ipt_ULOG delays packets caught by filter

PATCH ruby joker 2 participants 2 messages Jun 23, 2004 – Jul 21, 2004 netfilter

lore
Share & tools
lore

ruby joker

author
#0 lore raw

Harald is a bit busy, so I'm sending this to list, so maybe someone can check it.

When a packet hits ULOG target, it's stored in a buffer regardless of qthreshold value. It is copied to userspace only when another packet arrives (so it will 'push' the queved one) or flushtimeout time will pass. Delaying packets in logging utility isn't a good idea.

I've attached test code (test.c) if you want to reproduce this behaviour. It opens a netlink socket and report any packet received. Before running test, add a rule to netfilter, so that all tcp packets are caught, like this:

  # iptables -A INPUT -p tcp -j ULOG
  (qthreshold is 1 for default)
Now generate few tcp packets, by using simply:
  $ telnet localhost
By running tcpdump simultaneously you'll notice one packet is lost, when
others are reported at time. Lost packet will be send via netlink 10
secs after last in a row (because of default flushtimeout value).

I'm proposing a patch based on Linux 2.6.6. Any comments welcome.

rj

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/socket.h>
#include <asm/types.h>
#include <linux/netlink.h>

int main(int argc, char *argv[])

{
	struct msghdr hdr;
	struct sockaddr_nl addr;
	struct iovec msg;
	int s, i;
	time_t t;
	char buff[256];

	if ((s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_NFLOG)) == -1) {
		perror("socket()");
		return 1;
	}

	memset(&addr, 0x0, sizeof(struct sockaddr_nl));
	addr.nl_family = PF_NETLINK;
	addr.nl_groups = 0xffffffff;

	if (bind(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_nl)) == -1) {
		perror("bind()");
		return 1;
	}

	memset(&msg, 0x0, sizeof(struct iovec));
	msg.iov_base = buff;
	msg.iov_len = sizeof(buff);

	memset(&hdr, 0x0, sizeof(struct msghdr));
	memset(&addr, 0x0, sizeof(struct sockaddr_nl));
	hdr.msg_name = (void*)&addr;
	hdr.msg_namelen = sizeof(struct sockaddr_nl);
	hdr.msg_iov = &msg;
	hdr.msg_iovlen = 1;

	while (1) {
		if ((i = recvmsg(s, &hdr, 0)) != -1) {
			t = time(NULL);
			printf("packet caught: size %i -- %s", i, ctime(&t));
		}
		else {
			perror("recvmsg()");
			return 1;
		}
	}

	return 0;
}
ipv4/netfilter/ipt_ULOG.c +9 -9
@@ -97,7 +97,6 @@
 static ulog_buff_t ulog_buffers[ULOG_MAXNLGROUPS];	/* array of buffers */
 
 static struct sock *nflognl;	/* our socket */
-static size_t qlen;		/* current length of multipart-nlmsg */
 DECLARE_LOCK(ulog_lock);	/* spinlock */
 
 /* send one ulog_buff_t to userspace */
@@ -116,7 +115,7 @@
 
 	NETLINK_CB(ub->skb).dst_groups = (1 << nlgroupnum);
 	DEBUGP("ipt_ULOG: throwing %d packets to netlink mask %u\n",
-		ub->qlen, nlgroup);
+		ub->qlen, nlgroupnum);
 	netlink_broadcast(nflognl, ub->skb, 0, (1 << nlgroupnum), GFP_ATOMIC);
 
 	ub->qlen = 0;
@@ -126,7 +125,7 @@
 }
 
 
-/* timer function to flush queue in ULOG_FLUSH_INTERVAL time */
+/* timer function to flush queue in flushtimeout time */
 static void ulog_timer(unsigned long data)
 {
 	DEBUGP("ipt_ULOG: timer function called, calling ulog_send\n");
@@ -257,12 +256,6 @@
 		ub->lastnlh->nlmsg_flags |= NLM_F_MULTI;
 	}
 
-	/* if threshold is reached, send message to userspace */
-	if (qlen >= loginfo->qthreshold) {
-		if (loginfo->qthreshold > 1)
-			nlh->nlmsg_type = NLMSG_DONE;
-	}
-
 	ub->lastnlh = nlh;
 
 	/* if timer isn't already running, start it */
@@ -271,6 +264,13 @@
 		add_timer(&ub->timer);
 	}
 
+	/* if threshold is reached, send message to userspace */
+	if (ub->qlen >= loginfo->qthreshold) {
+		if (loginfo->qthreshold > 1)
+			nlh->nlmsg_type = NLMSG_DONE;
+		ulog_send(groupnum);
+	}
+
 	UNLOCK_BH(&ulog_lock);
 
 	return;
#1 lore raw
Quoted 2 lines · ruby joker

Harald is a bit busy, so I'm sending this to list, so maybe someone can check it.

Harald is now finally catching up with netfilter-devel of the last 7 weeks. I've put your patch into the set of pending patches.

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie