Netfilter, IPsec & nf_queue
Subsystems (4)
While continuing work on the input patch for Netfilter+IPsec, I noticed
two problems. The input-patch adds the nf_postxfrm_input andnf_postxfrm_nonlocal functions which pass packets to the hooks after
decapsulation and return drop/continue to the caller. The NF_HOOK macro
can't be used, at least in ip_local_deliver_finish, because the function
can't be split up in an immediate part and an outfn-part. The nf_postxfrm_* functions pass a dummy-outfn to NF_HOOK and use the the return code to decide what to do with the packet.
The first problem is that if the packet is replaced within a hook, it is only visible to the dummy-outfn, not to the caller. This could be fixed in multiple ways, for example by using a special nf_hook_slow function which takes a struct sk_buff **, or by using per-cpu skb-pointers to preserve the new skb in the dummy-outfn for the caller.
The second problem arises when packets are queued and reinjected from a hook, nf_reinject will continue hook traversal and call the dummy-outfn if everything is ok. The dummy-outfn will just return and leak the packet.
I can't think of a way to fix the nf_reinject problem while retaining the nf_postxfrm_* functions, and unfortunately (otherwise I would have never added them) I also can't think of a way to do it without them.
Ideas ?
Regards
Patrick
===== include/linux/netfilter_ipv4.h 1.6 vs edited =====
include/linux/netfilter_ipv4.h +15 -0
@@ -83,6 +83,21 @@Returns true or false. */ extern int skb_ip_make_writable (struct sk_buff **pskb,unsigned int writable_len); + +#ifdef CONFIG_XFRM +extern int nf_postxfrm_input (struct sk_buff *skb); +extern int nf_postxfrm_nonlocal (struct sk_buff *skb); +#else /* CONFIG_XFRM */ +static inline int nf_postxfrm_input (struct sk_buff *skb) +{ +return 0 ; +} + +static inline int nf_postxfrm_nonlocal (struct sk_buff *skb) +{ +return 0 ; +} +#endif /* CONFIG_XFRM */ #endif /*__KERNEL__*/ #endif /*__LINUX_IP_NETFILTER_H*/
===== include/net/protocol.h 1.10 vs edited =====
include/net/protocol.h +1 -0
@@ -39,6 +39,7 @@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 )
===== net/core/netfilter.c 1.28 vs edited =====
net/core/netfilter.c +76 -3
103 lines not on this page · raw message
===== net/ipv4/ah4.c 1.30 vs edited =====
net/ipv4/ah4.c +1 -0
@@ -344,6 +344,7 @@ .handler = xfrm4_rcv, .err_handler = ah4_err, .no_policy = 1, + .xfrm_prot = 1, };static int __init ah4_init (void )
===== net/ipv4/esp4.c 1.37 vs edited =====
net/ipv4/esp4.c +1 -0
@@ -577,6 +577,7 @@ .handler = xfrm4_rcv, .err_handler = esp4_err, .no_policy = 1, + .xfrm_prot = 1, };static int __init esp4_init (void )
===== net/ipv4/ip_input.c 1.21 vs edited =====
net/ipv4/ip_input.c +16 -6
@@ -224,6 +224,12 @@ resubmit: hash = protocol & (MAX_INET_PROTOS -1 ); raw_sk =sk_head (&raw_v4_htable[hash]); + ipprot = inet_protos[hash]; +smp_read_barrier_depends (); + +if (skb->sp && (!ipprot || !ipprot->xfrm_prot)) +if (nf_postxfrm_input (skb)) + goto out;/* If there maybe a raw socket we must check - if not we * don't care less
@@ -231,10 +237,9 @@if (raw_sk)raw_v4_input (skb, skb->nh.iph, hash); -if ((ipprot = inet_protos[hash]) !=NULL ) { +if (ipprot !=NULL ) {int ret; -smp_read_barrier_depends ();if (!ipprot->no_policy && !xfrm4_policy_check (NULL ,XFRM_POLICY_IN , skb)) {kfree_skb (skb);
@@ -279,8 +284,8 @@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, !skb->sp); }static inline int ip_rcv_finish (struct sk_buff *skb)
@@ -296,6 +301,10 @@if (ip_route_input (skb, iph->daddr, iph->saddr, iph->tos, dev)) goto drop; } +if (skb->sp && !(((struct rtable *)skb->dst)->rt_flags&RTCF_LOCAL )) +if (nf_postxfrm_nonlocal (skb)) + goto out; +#ifdef CONFIG_NET_CLS_ROUTE if (skb->dst->tclassid) {
@@ -352,6 +361,7 @@IP_INC_STATS_BH (IpInHdrErrors); drop:kfree_skb (skb); +out:return NET_RX_DROP ; }
@@ -418,8 +428,8 @@ } } -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, !skb->sp); inhdr_error:IP_INC_STATS_BH (IpInHdrErrors);
===== net/ipv4/ipcomp.c 1.19 vs edited =====
net/ipv4/ipcomp.c +1 -0
@@ -408,6 +408,7 @@ .handler = xfrm4_rcv, .err_handler = ipcomp4_err, .no_policy = 1, + .xfrm_prot = 1, };static int __init ipcomp4_init (void )
===== net/ipv4/xfrm4_tunnel.c 1.10 vs edited =====
net/ipv4/xfrm4_tunnel.c +1 -0
@@ -171,6 +171,7 @@ .handler = ipip_rcv, .err_handler = ipip_err, .no_policy = 1, + .xfrm_prot = 1, };static int __init ipip_init (void )