Skip to contentSkip to conversation

patch-o-matic-ng/patchlets/ipsec-02-input-hooks/linux-2.6.12.patch

PATCH/FIX Thomas Heinz 1 participant 1 message Oct 1, 2005

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Hi

The ipsec input hooks patch for linux 2.6.12 does not compile due to a missing #endif in net/core/netfilter.c. The attached patch adds it. Moreover, the "EXPORT_SYMBOL(nf_rcv_postxfrm_local);" at the end of net/core/netfilter.c is protected by an #ifdef.

Regards,

Thomas

include/linux/netfilter_ipv4.h +54 -0
diff -Nurp linux-2.6.12.org/include/linux/netfilter_ipv4.h linux-2.6.12/include/linux/netfilter_ipv4.h
@@ -7,6 +7,8 @@
 
 #include <linux/config.h>
 #include <linux/netfilter.h>
+#include <linux/netdevice.h>
+#include <net/protocol.h>
 
 /* IP Cache bits. */
 /* Src IP address. */
@@ -88,6 +90,58 @@ extern int ip_route_me_harder(struct sk_
    Returns true or false. */
 extern int skb_ip_make_writable(struct sk_buff **pskb,
 				unsigned int writable_len);
+
+#if defined(CONFIG_XFRM) && defined(CONFIG_NETFILTER)
+#include <net/route.h>
+#include <net/xfrm.h>
+
+static inline int nf_hook_input_cond(struct sk_buff *skb)
+{
+	return !skb->sp || skb->sp->decap_done;
+}
+
+static inline int
+nf_xfrm_local_done(struct sk_buff *skb, struct net_protocol *ipprot)
+{
+	return skb->sp && !skb->sp->decap_done
+	       && (!ipprot || !ipprot->xfrm_prot);
+}
+
+static inline int nf_xfrm_nonlocal_done(struct sk_buff *skb)
+{
+	return skb->sp && !skb->sp->decap_done
+	       && !(((struct rtable *)skb->dst)->rt_flags & RTCF_LOCAL);
+}
+
+extern int nf_rcv_postxfrm_local(struct sk_buff *skb);
+extern int nf_rcv_postxfrm_nonlocal(struct sk_buff *skb);
+#else /* CONFIG_XFRM */
+static inline int nf_hook_input_cond(struct sk_buff *skb)
+{
+	return 1;
+}
+
+static inline int
+nf_xfrm_local_done(struct sk_buff *skb, struct net_protocol *ipprot)
+{
+	return 0;
+}
+
+static inline int nf_xfrm_nonlocal_done(struct sk_buff *skb)
+{
+	return 0;
+}
+
+static inline int nf_rcv_postxfrm_local(struct sk_buff *skb)
+{
+	return 0;
+}
+
+static inline int nf_rcv_postxfrm_nonlocal(struct sk_buff *skb)
+{
+	return 0; 
+}
+#endif /* CONFIG_XFRM */
 #endif /*__KERNEL__*/
 
 #endif /*__LINUX_IP_NETFILTER_H*/
include/net/protocol.h +1 -0
diff -Nurp linux-2.6.12.org/include/net/protocol.h linux-2.6.12/include/net/protocol.h
@@ -38,6 +38,7 @@ struct net_protocol {
 	int			(*handler)(struct sk_buff *skb);
 	void			(*err_handler)(struct sk_buff *skb, u32 info);
 	int			no_policy;
+	int			xfrm_prot;
 };
 
 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
include/net/xfrm.h +3 -0
diff -Nurp linux-2.6.12.org/include/net/xfrm.h linux-2.6.12/include/net/xfrm.h
@@ -546,6 +546,9 @@ struct sec_path
 {
 	atomic_t		refcnt;
 	int			len;
+#ifdef CONFIG_NETFILTER
+	int			decap_done;
+#endif
 	struct sec_decap_state	x[XFRM_MAX_DEPTH];
 };
 
net/core/netfilter.c +27 -1
diff -Nurp linux-2.6.12.org/net/core/netfilter.c linux-2.6.12/net/core/netfilter.c
@@ -27,7 +27,8 @@
 #include <linux/icmp.h>
 #include <net/sock.h>
 #include <net/route.h>
-#include <linux/ip.h>
+#include <net/xfrm.h>
+#include <net/ip.h>
 
 /* In this code, we can be waiting indefinitely for userspace to
  * service a packet if a hook returns NF_QUEUE.  We could keep a count
@@ -651,6 +652,28 @@ int ip_route_me_harder(struct sk_buff **
 }
 EXPORT_SYMBOL(ip_route_me_harder);
 
+#ifdef CONFIG_XFRM
+inline int nf_rcv_postxfrm_nonlocal(struct sk_buff *skb)
+{
+	skb->sp->decap_done = 1;
+	dst_release(skb->dst);
+	skb->dst = NULL;
+	nf_reset(skb);
+	return netif_rx(skb);
+}
+
+int nf_rcv_postxfrm_local(struct sk_buff *skb)
+{
+	__skb_push(skb, skb->data - skb->nh.raw);
+	/* Fix header len and checksum if last xfrm was transport mode */
+	if (!skb->sp->x[skb->sp->len - 1].xvec->props.mode) {
+		skb->nh.iph->tot_len = htons(skb->len);
+		ip_send_check(skb->nh.iph);
+	}
+	return nf_rcv_postxfrm_nonlocal(skb);
+}
+#endif
+ 
 int skb_ip_make_writable(struct sk_buff **pskb, unsigned int writable_len)
 {
 	struct sk_buff *nskb;
@@ -784,3 +807,6 @@ EXPORT_SYMBOL(nf_setsockopt);
 EXPORT_SYMBOL(nf_unregister_hook);
 EXPORT_SYMBOL(nf_unregister_queue_handler);
 EXPORT_SYMBOL(nf_unregister_sockopt);
+#ifdef CONFIG_XFRM
+EXPORT_SYMBOL(nf_rcv_postxfrm_local);
+#endif
net/ipv4/ah4.c +1 -0
diff -Nurp linux-2.6.12.org/net/ipv4/ah4.c linux-2.6.12/net/ipv4/ah4.c
@@ -306,6 +306,7 @@ static struct net_protocol ah4_protocol 
 	.handler	=	xfrm4_rcv,
 	.err_handler	=	ah4_err,
 	.no_policy	=	1,
+	.xfrm_prot	=	1,
 };
 
 static int __init ah4_init(void)
net/ipv4/esp4.c +1 -0
diff -Nurp linux-2.6.12.org/net/ipv4/esp4.c linux-2.6.12/net/ipv4/esp4.c
@@ -472,6 +472,7 @@ static struct net_protocol esp4_protocol
 	.handler	=	xfrm4_rcv,
 	.err_handler	=	esp4_err,
 	.no_policy	=	1,
+	.xfrm_prot	=	1,
 };
 
 static int __init esp4_init(void)
net/ipv4/ip_input.c +14 -5
diff -Nurp linux-2.6.12.org/net/ipv4/ip_input.c linux-2.6.12/net/ipv4/ip_input.c
@@ -224,6 +224,12 @@ static inline int ip_local_deliver_finis
 	resubmit:
 		hash = protocol & (MAX_INET_PROTOS - 1);
 		raw_sk = sk_head(&raw_v4_htable[hash]);
+		ipprot = rcu_dereference(inet_protos[hash]);
+
+		if (nf_xfrm_local_done(skb, ipprot)) {
+			nf_rcv_postxfrm_local(skb);
+			goto out;
+		}
 
 		/* If there maybe a raw socket we must check - if not we
 		 * don't care less
@@ -231,7 +237,7 @@ static inline int ip_local_deliver_finis
 		if (raw_sk)
 			raw_v4_input(skb, skb->nh.iph, hash);
 
-		if ((ipprot = rcu_dereference(inet_protos[hash])) != NULL) {
+		if (ipprot != NULL) {
 			int ret;
 
 			if (!ipprot->no_policy &&
@@ -278,8 +284,8 @@ int ip_local_deliver(struct sk_buff *skb
 			return 0;
 	}
 
-	return NF_HOOK(PF_INET, NF_IP_LOCAL_IN, skb, skb->dev, NULL,
-		       ip_local_deliver_finish);
+	return NF_HOOK_COND(PF_INET, NF_IP_LOCAL_IN, skb, skb->dev, NULL,
+	                    ip_local_deliver_finish, nf_hook_input_cond(skb));
 }
 
 static inline int ip_rcv_finish(struct sk_buff *skb)
@@ -296,6 +302,9 @@ static inline int ip_rcv_finish(struct s
 			goto drop; 
 	}
 
+	if (nf_xfrm_nonlocal_done(skb))
+		return nf_rcv_postxfrm_nonlocal(skb);
+
 #ifdef CONFIG_NET_CLS_ROUTE
 	if (skb->dst->tclassid) {
 		struct ip_rt_acct *st = ip_rt_acct + 256*smp_processor_id();
@@ -416,8 +425,8 @@ int ip_rcv(struct sk_buff *skb, struct n
 		}
 	}
 
-	return NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, dev, NULL,
-		       ip_rcv_finish);
+	return NF_HOOK_COND(PF_INET, NF_IP_PRE_ROUTING, skb, dev, NULL,
+	                    ip_rcv_finish, nf_hook_input_cond(skb));
 
 inhdr_error:
 	IP_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
net/ipv4/ipcomp.c +1 -0
diff -Nurp linux-2.6.12.org/net/ipv4/ipcomp.c linux-2.6.12/net/ipv4/ipcomp.c
@@ -491,6 +491,7 @@ static struct net_protocol ipcomp4_proto
 	.handler	=	xfrm4_rcv,
 	.err_handler	=	ipcomp4_err,
 	.no_policy	=	1,
+	.xfrm_prot	=	1,
 };
 
 static int __init ipcomp4_init(void)
net/ipv4/ipip.c +5 -0
diff -Nurp linux-2.6.12.org/net/ipv4/ipip.c linux-2.6.12/net/ipv4/ipip.c
@@ -476,6 +476,11 @@ static int ipip_rcv(struct sk_buff *skb)
 
 	read_lock(&ipip_lock);
 	if ((tunnel = ipip_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
+		/* IPIP packets decapsulated by IPsec missed netfilter hooks */
+		if (nf_xfrm_local_done(skb, NULL)) {
+			nf_rcv_postxfrm_local(skb);
+			return 0;
+		}
 		if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
 			read_unlock(&ipip_lock);
 			kfree_skb(skb);
net/ipv4/xfrm4_tunnel.c +1 -0
diff -Nurp linux-2.6.12.org/net/ipv4/xfrm4_tunnel.c linux-2.6.12/net/ipv4/xfrm4_tunnel.c
@@ -115,6 +115,7 @@ static struct net_protocol ipip_protocol
 	.handler	=	ipip_rcv,
 	.err_handler	=	ipip_err,
 	.no_policy	=	1,
+	.xfrm_prot	=	1,
 };
 
 static int __init ipip_init(void)
net/xfrm/xfrm_input.c +3 -0
diff -Nurp linux-2.6.12.org/net/xfrm/xfrm_input.c linux-2.6.12/net/xfrm/xfrm_input.c
@@ -31,6 +31,9 @@ struct sec_path *secpath_dup(struct sec_
 	if (!sp)
 		return NULL;
 
+#ifdef CONFIG_NETFILTER
+	sp->decap_done = 0;
+#endif
 	sp->len = 0;
 	if (src) {
 		int i;