Skip to contentSkip to conversation

netfilter hook in packet capture

DISCUSSION SVR Anand 2 participants 3 messages Jan 1, 2004 – Jan 3, 2004

Subsystems (3)
lore
Share & tools
lore

SVR Anand

author
#0 lore raw

Hi,

I am hoping that this mail will be relevant to those who have an user level application that does firewalling by promiscuously capturing packets, apply extensive decision making rules. I have written one such application and found what I am going to say below has helped me a lot.

In the above mentioned scenario, there are many instances I preferred to do the following in the kernel itself as much as possible before further processing done in the user firewall code. What I wanted was
i) selective packet capture ii) DoS protection iii) Externally controlling the kernel level filter rules without disrupting the firewall application iv) Ease of filter management vi) Logging v) Performance ...

I found that iptables/netfilter satisfies all my requirements as compared to the existing bpf filter. Hence all I had to do was include NF_HOOK at couple of places in af_packet.c and I have the netfilter features accessible to me. With the above trivial inclusion I am currently running my application with all the initial work done by netfilter. Thanks netfilter group!

While the sanctity or appropriateness or compliance of the above patch within packet capture scheme of things can be frowned upon, since its utility has been beneficial to me, I thought I would share it with you. If you find it useful I can send you rather simple patch which perhaps you could have easily guessed it!

Anand

#1 lore raw
Quoted 5 lines · SVR Anand

While the sanctity or appropriateness or compliance of the above patch within packet capture scheme of things can be frowned upon, since its utility has been beneficial to me, I thought I would share it with you. If you find it useful I can send you rather simple patch which perhaps you could have easily guessed it!

We would definitely be interested in such a patch. Please first submit it to the netfilter development team, and we will then push it for kernel inclusion (after any changes/suggestions that we might have).

Please send the patch to [email protected]

Thanks a lot!

Quoted 1 line

Anand

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
author
#2 lore raw

Harald Welte,

Thanks for your response.

I am sending the patch to the file
/usr/src/linux-2.6.0/net/packet/af_packet.c

which will give access to the netfilter hook before giving the captured packets to the application. I have used the feature to perform the initial packet filtering, as well as DoS protection at the kernel itself so that the "accepted" packets are processed by my IDS like application which examines these packets at lot more depth.

Thus I have a complete firewall system running partly at the kernel, and partly at the user land. Apart from off-loading part of the job, the flexibility of being able to use iptables externally to control the interesting packets that can be given to the application is something that is appealing.

Here is the simple patch. I guess it can be applied any kernel with NF_HOOK support. I hope others find it useful too.

Anand

---------------------------------------------------------------------

af_packet.c renamed from af_packet.c.org +35 -0
@@ -72,8 +72,15 @@
 #include <net/inet_common.h>
 #endif
 
+#define PACKET_IP_TABLES
+
+#ifdef PACKET_IP_TABLES
+#include <linux/netfilter_ipv4.h>
+#endif
+
 #define CONFIG_SOCK_PACKET	1
 
+
 /*
    Proposed replacement for SIOC{ADD,DEL}MULTI and
    IFF_PROMISC, IFF_ALLMULTI flags.
@@ -272,6 +279,11 @@
 	return 0;
 }
 
+static inline int tpacket_local_deliver_finish(struct sk_buff *skb)
+{
+
+	return 0;
+}
 
 /*
  *	Output a raw packet to a device layer. This bypasses all the other
@@ -418,6 +430,7 @@
 	u8 * skb_head = skb->data;
 	int skb_len = skb->len;
 	unsigned snaplen;
+	int err;
 
 	if (skb->pkt_type == PACKET_LOOPBACK)
 		goto drop;
@@ -427,6 +440,17 @@
 
 	skb->dev = dev;
 
+
+#ifdef PACKET_IP_TABLES
+
+    if (skb->protocol == __constant_htons(ETH_P_IP)) {
+    err = NF_HOOK(PF_INET, NF_IP_LOCAL_IN, skb, skb->dev, NULL,
+			                       tpacket_local_deliver_finish);
+	if (err < 0) return 0;
+     }
+#endif
+
+
 	if (dev->hard_header) {
 		/* The device has an explicit notion of ll header,
 		   exported to higher levels.
@@ -521,10 +545,21 @@
 	unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
 	unsigned short macoff, netoff;
 	struct sk_buff *copy_skb = NULL;
+	int err;
 
 	if (skb->pkt_type == PACKET_LOOPBACK)
 		goto drop;
 
+#ifdef PACKET_IP_TABLES
+
+    if (skb->protocol == __constant_htons(ETH_P_IP)) {
+    err = NF_HOOK(PF_INET, NF_IP_LOCAL_IN, skb, skb->dev, NULL,
+			                       tpacket_local_deliver_finish);
+	if (err < 0) return 0;
+     }
+#endif
+
+
 	sk = pt->af_packet_priv;
 	po = pkt_sk(sk);
 

---------------------------------------------------------------------

Quoted 14 lines

We would definitely be interested in such a patch. Please first submit it to the netfilter development team, and we will then push it for kernel inclusion (after any changes/suggestions that we might have).

Please send the patch to [email protected]

Thanks a lot!

--=20
- Harald Welte <[email protected]> http://www.netfilter.org/