: Netfilter IPsec support
Subsystems (8)
cover : Netfilter IPsec support
Hi Dave,
following are the remaining patches for netfilter IPsec support. They are missing the common-case optimization for inner transport mode SAs on the input path, but since its just an optimization, I think it can also be done later. One note: unfortunately I had to increase skb->cb to make room for two new fields in the IP6CB. Yoshifuji mentioned he needs more room for mobile IPv6 extensions anyway, so hopefully this will leave enough room for both. One idea to save some space again would be to introduce a struct that is a subset of inet6_skb_parm and only holds the fields needed after the packet is passed to the upper protocol and use that struct in tcp_skb_cb.
PATCH 1/10 : Netfilter IPsec output hooks
[XFRM]: Netfilter IPsec output hooks
Call netfilter hooks before IPsec transforms. Packets visit the FORWARD/LOCAL_OUT and POST_ROUTING hook before the first encapsulation and the LOCAL_OUT and POST_ROUTING hook before each following tunnel mode transform.
Patch from Herbert Xu <[email protected]>:
Move the loop from dst_output into xfrm4_output/xfrm6_output since they're the only ones who need to it. xfrm{4,6}_output_one() processes the first SA all subsequent transport mode SAs and is called in a loop that calls the netfilter hooks between each two calls.
In order to avoid the tail call issue, I've added the inline function nf_hook which is nf_hook_slow plus the empty list check.
---
commit b426272e4885bc398c9c01e42486669cd832706f
tree 98334b7544c7d01f238bc5edef47b95cd86ad619
parent d99cf9d679a520d67f81d805b7cb91c68e1847f0
author Patrick McHardy <[email protected]> Fri, 06 Jan 2006 21:11:59 +0100 committer Patrick McHardy <[email protected]> Fri, 06 Jan 2006 21:11:59 +0100
include/linux/netfilter.h | 61 +++++++++++++++++++++-------------- include/net/dst.h | 11 +----- net/ipv4/xfrm4_output.c | 75 ++++++++++++++++++++++++++++++++----------- net/ipv6/xfrm6_output.c | 79 +++++++++++++++++++++++++++++++++------------ 4 files changed, 152 insertions(+), 74 deletions(-)
include/linux/netfilter.h +37 -24
@@ -168,6 +168,37 @@ void nf_log_packet(int pf,const struct net_device *out, struct nf_loginfo *li, const char *fmt, ...); + +int nf_hook_slow (int pf,unsigned int hook,struct sk_buff **pskb, +struct net_device *indev,struct net_device *outdev, +int (*okfn)(struct sk_buff *),int thresh); + +/** +* nf_hook_thresh - call a netfilter hook +* +* Returns 1 if the hook has allowed the packet to pass. The function +* okfn must be invoked by the caller in this case. Any other return +* value indicates the packet has been consumed by the hook. +*/ +static inline int nf_hook_thresh (int pf,unsigned int hook, +struct sk_buff **pskb, +struct net_device *indev, +struct net_device *outdev, +int (*okfn)(struct sk_buff *),int thresh) +{ +#ifndef CONFIG_NETFILTER_DEBUG +if (list_empty (&nf_hooks[pf][hook])) +return 1 ; +#endif +return nf_hook_slow (pf, hook, pskb, indev, outdev, okfn, thresh); +} + +static inline int nf_hook (int pf,unsigned int hook,struct sk_buff **pskb, +struct net_device *indev,struct net_device *outdev, +int (*okfn)(struct sk_buff *)) +{ +return nf_hook_thresh (pf, hook, pskb, indev, outdev, okfn,INT_MIN ); +}/* Activate hook; either okfn or kfree_skb called, unless a hook returns NF_STOLEN (in which case, it's up to the hook to deal with
@@ -188,35 +219,17 @@ void nf_log_packet(int pf,/* This is gross, but inline doesn't cut it for avoiding the function call in fast path: gcc doesn't inline (needs value tracking?). --RR */ -#ifdef CONFIG_NETFILTER_DEBUG -#define NF_HOOK (pf, hook, skb, indev, outdev, okfn) \ -({int __ret; \ -if ((__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, INT_MIN)) == 1) \ - __ret = (okfn)(skb); \ -__ret;}) -#define NF_HOOK_THRESH (pf, hook, skb, indev, outdev, okfn, thresh) \ -({int __ret; \ -if ((__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, thresh)) == 1) \ - __ret = (okfn)(skb); \ -__ret;}) -#else -#define NF_HOOK (pf, hook, skb, indev, outdev, okfn) \ -({int __ret; \ -if (list_empty(&nf_hooks[pf][hook]) || \ - (__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, INT_MIN)) == 1) \ - __ret = (okfn)(skb); \ -__ret;}) + +/* HX: It's slightly less gross now. */ +#define NF_HOOK_THRESH (pf, hook, skb, indev, outdev, okfn, thresh) \ ({int __ret; \ -if (list_empty(&nf_hooks[pf][hook]) || \ - (__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, thresh)) == 1) \ +if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh)) == 1)\ __ret = (okfn)(skb); \ __ret;}) -#endif -int nf_hook_slow (int pf,unsigned int hook,struct sk_buff **pskb, -struct net_device *indev,struct net_device *outdev, -int (*okfn)(struct sk_buff *),int thresh); +#define NF_HOOK (pf, hook, skb, indev, outdev, okfn) \ + NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)/* Call setsockopt() */ int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
include/net/dst.h +1 -10
@@ -225,16 +225,7 @@ static inline void dst_set_expires(struc/* Output packet to network from transport. */ static inline int dst_output (struct sk_buff *skb) { -int err; - -for (;;) { - err = skb->dst->output (skb); - -if (likely (err ==0 )) -return err; -if (unlikely (err !=NET_XMIT_BYPASS )) -return err; - } +return skb->dst->output (skb); }/* Input packet from network to transport. */
net/ipv4/xfrm4_output.c +56 -19
101 lines not on this page · raw message
net/ipv6/xfrm6_output.c +58 -21
106 lines not on this page · raw message
PATCH 2/10 : Move nextheader offset to the IP6CB
In reply to Patrick McHardy[IPV6]: Move nextheader offset to the IP6CB
Move nextheader offset to the IP6CB to make it possible to pass a packet to ip6_input_finish multiple times and have it skip already parsed headers. As a nice side effect this gets rid of the manual hopopts skipping in ip6_input_finish.
---
commit 7398b0fba81fbae5139c7a23d27477424125e8b2
tree 872d80dd650b8c27492cc23b8b9e768ab10494da
parent b426272e4885bc398c9c01e42486669cd832706f
author Patrick McHardy <[email protected]> Fri, 06 Jan 2006 21:17:42 +0100 committer Patrick McHardy <[email protected]> Fri, 06 Jan 2006 21:17:42 +0100
include/linux/ipv6.h | 1 + include/net/protocol.h | 2 +- include/net/xfrm.h | 6 +++--- net/dccp/ipv6.c | 2 +- net/ipv6/exthdrs.c | 19 ++++++++++++------- net/ipv6/icmp.c | 4 ++-- net/ipv6/ip6_input.c | 21 ++++++--------------- net/ipv6/ip6_tunnel.c | 2 +- net/ipv6/reassembly.c | 11 +++++------ net/ipv6/tcp_ipv6.c | 2 +- net/ipv6/udp.c | 2 +- net/ipv6/xfrm6_input.c | 8 ++++---- net/ipv6/xfrm6_tunnel.c | 6 +++--- net/sctp/ipv6.c | 2 +- 14 files changed, 42 insertions(+), 46 deletions(-)
include/linux/ipv6.h +1 -0
@@ -191,6 +191,7 @@ struct inet6_skb_parm {__u16 srcrt;__u16 dst1;__u16 lastopt; +__u32 nhoff; };#define IP6CB (skb) ((struct inet6_skb_parm*)((skb)->cb))
include/net/protocol.h +1 -1
@@ -43,7 +43,7 @@ struct net_protocol { #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) struct inet6_protocol { - int (*handler)(struct sk_buff **skb, unsigned int *nhoffp); + int (*handler)(struct sk_buff **skb); void (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
include/net/xfrm.h +3 -3
@@ -831,7 +831,7 @@ struct xfrm_tunnel { };struct xfrm6_tunnel { -int (*handler)(struct sk_buff **pskb,unsigned int *nhoffp); +int (*handler)(struct sk_buff **pskb);void (*err_handler)(struct sk_buff *skb,struct inet6_skb_parm *opt,int type,int code,int offset,__u32 info); };
@@ -868,8 +868,8 @@ extern int xfrm4_rcv(struct sk_buff *skbextern int xfrm4_output (struct sk_buff *skb);extern int xfrm4_tunnel_register (struct xfrm_tunnel *handler);extern int xfrm4_tunnel_deregister (struct xfrm_tunnel *handler); -extern int xfrm6_rcv_spi (struct sk_buff **pskb,unsigned int *nhoffp,u32 spi); -extern int xfrm6_rcv (struct sk_buff **pskb,unsigned int *nhoffp); +extern int xfrm6_rcv_spi (struct sk_buff **pskb,u32 spi); +extern int xfrm6_rcv (struct sk_buff **pskb);extern int xfrm6_tunnel_register (struct xfrm6_tunnel *handler);extern int xfrm6_tunnel_deregister (struct xfrm6_tunnel *handler);extern u32 xfrm6_tunnel_alloc_spi (xfrm_address_t *saddr);
net/dccp/ipv6.c +1 -1
@@ -1028,7 +1028,7 @@ discard:return 0 ; } -static int dccp_v6_rcv (struct sk_buff **pskb,unsigned int *nhoffp) +static int dccp_v6_rcv (struct sk_buff **pskb) {const struct dccp_hdr *dh;struct sk_buff *skb = *pskb;
net/ipv6/exthdrs.c +12 -7
@@ -152,7 +152,7 @@ static struct tlvtype_proc tlvprocdestop {-1, NULL} }; -static int ipv6_destopt_rcv (struct sk_buff **skbp,unsigned int *nhoffp) +static int ipv6_destopt_rcv (struct sk_buff **skbp) {struct sk_buff *skb = *skbp;struct inet6_skb_parm *opt =IP6CB (skb);
@@ -169,7 +169,7 @@ static int ipv6_destopt_rcv(struct sk_buif (ip6_parse_tlv (tlvprocdestopt_lst, skb)) { skb->h.raw += ((skb->h.raw[1 ]+1 )<<3 ); - *nhoffp = opt->dst1; + opt->nhoff = opt->dst1;return 1 ; }
@@ -192,7 +192,7 @@ void __init ipv6_destopt_init(void)NONE header. No data in packet. ********************************/ -static int ipv6_nodata_rcv (struct sk_buff **skbp,unsigned int *nhoffp) +static int ipv6_nodata_rcv (struct sk_buff **skbp) {struct sk_buff *skb = *skbp;
@@ -215,7 +215,7 @@ void __init ipv6_nodata_init(void)Routing header. ********************************/ -static int ipv6_rthdr_rcv (struct sk_buff **skbp,unsigned int *nhoffp) +static int ipv6_rthdr_rcv (struct sk_buff **skbp) {struct sk_buff *skb = *skbp;struct inet6_skb_parm *opt =IP6CB (skb);
@@ -249,7 +249,7 @@ looped_back: skb->h.raw += (hdr->hdrlen +1 ) <<3 ; opt->dst0 = opt->dst1; opt->dst1 =0 ; - *nhoffp = (&hdr->nexthdr) - skb->nh.raw; + opt->nhoff = (&hdr->nexthdr) - skb->nh.raw;return 1 ; }
@@ -487,9 +487,14 @@ static struct tlvtype_proc tlvprochopoptint ipv6_parse_hopopts (struct sk_buff *skb,int nhoff) { -IP6CB (skb)->hop =sizeof (struct ipv6hdr ); -if (ip6_parse_tlv (tlvprochopopt_lst, skb)) +struct inet6_skb_parm *opt =IP6CB (skb); + + opt->hop =sizeof (struct ipv6hdr ); +if (ip6_parse_tlv (tlvprochopopt_lst, skb)) { + skb->h.raw += (skb->h.raw[1 ]+1 )<<3 ; + opt->nhoff =sizeof (struct ipv6hdr );return sizeof (struct ipv6hdr ); + }return -1 ; }
net/ipv6/icmp.c +2 -2
@@ -79,7 +79,7 @@ DEFINE_SNMP_STAT(struct icmpv6_mib, icmp static DEFINE_PER_CPU(struct socket *, __icmpv6_socket) = NULL;#define icmpv6_socket __get_cpu_var(__icmpv6_socket) -static int icmpv6_rcv (struct sk_buff **pskb,unsigned int *nhoffp); +static int icmpv6_rcv (struct sk_buff **pskb);static struct inet6_protocol icmpv6_protocol = { .handler = icmpv6_rcv,
@@ -581,7 +581,7 @@ static void icmpv6_notify(struct sk_buff* Handle icmp messages */ -static int icmpv6_rcv (struct sk_buff **pskb,unsigned int *nhoffp) +static int icmpv6_rcv (struct sk_buff **pskb) {struct sk_buff *skb = *pskb;struct net_device *dev = skb->dev;
net/ipv6/ip6_input.c +6 -15
@@ -97,6 +97,9 @@ int ipv6_rcv(struct sk_buff *skb, structif (hdr->version !=6 ) goto err; + skb->h.raw = (u8 *)(hdr +1 ); +IP6CB (skb)->nhoff = offsetof(struct ipv6hdr , nexthdr); + pkt_len =ntohs (hdr->payload_len);/* pkt_len may be zero if Jumbo payload option is present */
@@ -111,8 +114,7 @@ int ipv6_rcv(struct sk_buff *skb, struct }if (hdr->nexthdr ==NEXTHDR_HOP ) { - skb->h.raw = (u8 *)(hdr+1 ); -if (ipv6_parse_hopopts (skb, offsetof(struct ipv6hdr , nexthdr)) <0 ) { +if (ipv6_parse_hopopts (skb,IP6CB (skb)->nhoff) <0 ) {IP6_INC_STATS_BH (IPSTATS_MIB_INHDRERRORS );return 0 ; }
@@ -143,26 +145,15 @@ static inline int ip6_input_finish(strucint nexthdr;u8 hash; - skb->h.raw = skb->nh.raw +sizeof (struct ipv6hdr ); -/* * Parse extension headers */ - nexthdr = skb->nh.ipv6h->nexthdr; - nhoff = offsetof(struct ipv6hdr , nexthdr); - -/* Skip hop-by-hop options, they are already parsed. */ -if (nexthdr ==NEXTHDR_HOP ) { - nhoff =sizeof (struct ipv6hdr ); - nexthdr = skb->h.raw[0 ]; - skb->h.raw += (skb->h.raw[1 ]+1 )<<3 ; - } -rcu_read_lock (); resubmit:if (!pskb_pull (skb, skb->h.raw - skb->data)) goto discard; + nhoff =IP6CB (skb)->nhoff; nexthdr = skb->nh.raw[nhoff]; raw_sk =sk_head (&raw_v6_htable[nexthdr & (MAX_INET_PROTOS -1 )]);
@@ -194,7 +185,7 @@ resubmit: !xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) goto discard; - ret = ipprot->handler (&skb, &nhoff); + ret = ipprot->handler (&skb);if (ret >0 ) goto resubmit;else if (ret ==0 )
net/ipv6/ip6_tunnel.c +1 -1
@@ -510,7 +510,7 @@ static inline void ip6ip6_ecn_decapsulat**/ static int -ip6ip6_rcv (struct sk_buff **pskb,unsigned int *nhoffp) +ip6ip6_rcv (struct sk_buff **pskb) {struct sk_buff *skb = *pskb;struct ipv6hdr *ipv6h;
net/ipv6/reassembly.c +5 -6
@@ -581,7 +581,6 @@ err:* the last and the first frames arrived and all the bits are here. */ static int ip6_frag_reasm (struct frag_queue *fq,struct sk_buff **skb_in, -unsigned int *nhoffp,struct net_device *dev) {struct sk_buff *fp, *head = fq->fragments;
@@ -654,6 +653,7 @@ static int ip6_frag_reasm(struct frag_qu head->dev = dev;skb_set_timestamp (head, &fq->stamp); head->nh.ipv6h->payload_len =htons (payload_len); +IP6CB (head)->nhoff = nhoff; *skb_in = head;
@@ -663,7 +663,6 @@ static int ip6_frag_reasm(struct frag_quIP6_INC_STATS_BH (IPSTATS_MIB_REASMOKS ); fq->fragments =NULL ; - *nhoffp = nhoff;return 1 ; out_oversize:
@@ -678,7 +677,7 @@ out_fail:return -1 ; } -static int ipv6_frag_rcv (struct sk_buff **skbp,unsigned int *nhoffp) +static int ipv6_frag_rcv (struct sk_buff **skbp) {struct sk_buff *skb = *skbp;struct net_device *dev = skb->dev;
@@ -710,7 +709,7 @@ static int ipv6_frag_rcv(struct sk_buff skb->h.raw +=sizeof (struct frag_hdr );IP6_INC_STATS_BH (IPSTATS_MIB_REASMOKS ); - *nhoffp = (u8 *)fhdr - skb->nh.raw; +IP6CB (skb)->nhoff = (u8 *)fhdr - skb->nh.raw;return 1 ; }
@@ -722,11 +721,11 @@ static int ipv6_frag_rcv(struct sk_buffspin_lock (&fq->lock); -ip6_frag_queue (fq, skb, fhdr, *nhoffp); +ip6_frag_queue (fq, skb, fhdr,IP6CB (skb)->nhoff);if (fq->last_in == (FIRST_IN |LAST_IN ) && fq->meat == fq->len) - ret =ip6_frag_reasm (fq, skbp, nhoffp, dev); + ret =ip6_frag_reasm (fq, skbp, dev);spin_unlock (&fq->lock);fq_put (fq,NULL );
net/ipv6/tcp_ipv6.c +1 -1
@@ -1153,7 +1153,7 @@ ipv6_pktoptions:return 0 ; } -static int tcp_v6_rcv (struct sk_buff **pskb,unsigned int *nhoffp) +static int tcp_v6_rcv (struct sk_buff **pskb) {struct sk_buff *skb = *pskb;struct tcphdr *th;
net/ipv6/udp.c +1 -1
@@ -435,7 +435,7 @@ out:read_unlock (&udp_hash_lock); } -static int udpv6_rcv (struct sk_buff **pskb,unsigned int *nhoffp) +static int udpv6_rcv (struct sk_buff **pskb) {struct sk_buff *skb = *pskb;struct sock *sk;
net/ipv6/xfrm6_input.c +4 -4
@@ -26,7 +26,7 @@ static inline void ipip6_ecn_decapsulateIP6_ECN_set_ce (inner_iph); } -int xfrm6_rcv_spi (struct sk_buff **pskb,unsigned int *nhoffp,u32 spi) +int xfrm6_rcv_spi (struct sk_buff **pskb,u32 spi) {struct sk_buff *skb = *pskb;int err;
@@ -38,7 +38,7 @@ int xfrm6_rcv_spi(struct sk_buff **pskb,int nexthdr;unsigned int nhoff; - nhoff = *nhoffp; + nhoff =IP6CB (skb)->nhoff; nexthdr = skb->nh.raw[nhoff]; seq =0 ;
@@ -144,7 +144,7 @@ drop:EXPORT_SYMBOL (xfrm6_rcv_spi); -int xfrm6_rcv (struct sk_buff **pskb,unsigned int *nhoffp) +int xfrm6_rcv (struct sk_buff **pskb) { -return xfrm6_rcv_spi (pskb, nhoffp,0 ); +return xfrm6_rcv_spi (pskb,0 ); }
net/ipv6/xfrm6_tunnel.c +3 -3
@@ -397,7 +397,7 @@ int xfrm6_tunnel_deregister(struct xfrm6EXPORT_SYMBOL (xfrm6_tunnel_deregister); -static int xfrm6_tunnel_rcv (struct sk_buff **pskb,unsigned int *nhoffp) +static int xfrm6_tunnel_rcv (struct sk_buff **pskb) {struct sk_buff *skb = *pskb;struct xfrm6_tunnel *handler = xfrm6_tunnel_handler;
@@ -405,11 +405,11 @@ static int xfrm6_tunnel_rcv(struct sk_buu32 spi;/* device-like_ip6ip6_handler() */ -if (handler && handler->handler (pskb, nhoffp) ==0 ) +if (handler && handler->handler (pskb) ==0 )return 0 ; spi =xfrm6_tunnel_spi_lookup ((xfrm_address_t *)&iph->saddr); -return xfrm6_rcv_spi (pskb, nhoffp, spi); +return xfrm6_rcv_spi (pskb, spi); } static void xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
net/sctp/ipv6.c +1 -1
@@ -905,7 +905,7 @@ static struct inet_protosw sctpv6_stream .flags = SCTP_PROTOSW_FLAG, }; -static int sctp6_rcv (struct sk_buff **pskb,unsigned int *nhoffp) +static int sctp6_rcv (struct sk_buff **pskb) {return sctp_rcv (*pskb) ?-1 :0 ; }
PATCH 3/10 : Netfilter IPsec input hooks
In reply to Patrick McHardy[IPV4/6]: Netfilter IPsec input hooks
When the innermost transform uses transport mode the decapsulated packet is not visible to netfilter. Pass the packet through the PRE_ROUTING and LOCAL_IN hooks again before handing it to upper layer protocols to make netfilter-visibility symetrical to the output path.
---
commit 0ab5982305cae5d8e4fb41d8bb849ee1808946fc
tree 6fce75eaee3b9b8d4f34470d8988906e93a22b19
parent 7398b0fba81fbae5139c7a23d27477424125e8b2
author Patrick McHardy <[email protected]> Fri, 06 Jan 2006 21:47:45 +0100 committer Patrick McHardy <[email protected]> Fri, 06 Jan 2006 21:47:45 +0100
include/net/ipv6.h | 2 ++ net/ipv4/xfrm4_input.c | 31 +++++++++++++++++++++++++++++++ net/ipv6/ip6_input.c | 2 +- net/ipv6/xfrm6_input.c | 13 +++++++++++++ 4 files changed, 47 insertions(+), 1 deletions(-)
include/net/ipv6.h +2 -0
@@ -418,6 +418,8 @@ extern int ipv6_rcv(struct sk_buff *skstruct packet_type *pt, struct net_device *orig_dev); +extern int ip6_rcv_finish (struct sk_buff *skb); +/* * upper-layer output functions */
net/ipv4/xfrm4_input.c +31 -0
@@ -11,6 +11,8 @@#include <linux/module.h> #include <linux/string.h> +#include <linux/netfilter.h> +#include <linux/netfilter_ipv4.h> #include <net/inet_ecn.h> #include <net/ip.h> #include <net/xfrm.h>
@@ -45,6 +47,23 @@ static int xfrm4_parse_spi(struct sk_bufreturn xfrm_parse_spi (skb, nexthdr, spi, seq); } +#ifdef CONFIG_NETFILTER +static inline int xfrm4_rcv_encap_finish (struct sk_buff *skb) +{ +struct iphdr *iph = skb->nh.iph; + +if (skb->dst ==NULL ) { +if (ip_route_input (skb, iph->daddr, iph->saddr, iph->tos, + skb->dev)) + goto drop; + } +return dst_input (skb); +drop: +kfree_skb (skb); +return NET_RX_DROP ; +} +#endif +int xfrm4_rcv_encap (struct sk_buff *skb,__u16 encap_type) {int err;
@@ -137,6 +156,8 @@ int xfrm4_rcv_encap(struct sk_buff *skb,memcpy (skb->sp->x+skb->sp->len, xfrm_vec, xfrm_nr*sizeof (struct sec_decap_state )); skb->sp->len += xfrm_nr; +nf_reset (skb); +if (decaps) {if (!(skb->dev->flags&IFF_LOOPBACK )) {dst_release (skb->dst);
@@ -145,7 +166,17 @@ int xfrm4_rcv_encap(struct sk_buff *skb,netif_rx (skb);return 0 ; }else { +#ifdef CONFIG_NETFILTER +__skb_push (skb, skb->data - skb->nh.raw); + skb->nh.iph->tot_len =htons (skb->len); +ip_send_check (skb->nh.iph); + +NF_HOOK (PF_INET ,NF_IP_PRE_ROUTING , skb, skb->dev,NULL , + xfrm4_rcv_encap_finish); +return 0 ; +#else return -skb->nh.iph->protocol; +#endif } drop_unlock:
net/ipv6/ip6_input.c +1 -1
@@ -48,7 +48,7 @@ -static inline int ip6_rcv_finish (struct sk_buff *skb) +inline int ip6_rcv_finish (struct sk_buff *skb) {if (skb->dst ==NULL )ip6_route_input (skb);
net/ipv6/xfrm6_input.c +13 -0
@@ -11,6 +11,8 @@#include <linux/module.h> #include <linux/string.h> +#include <linux/netfilter.h> +#include <linux/netfilter_ipv6.h> #include <net/dsfield.h> #include <net/inet_ecn.h> #include <net/ip.h>
@@ -121,6 +123,8 @@ int xfrm6_rcv_spi(struct sk_buff **pskb, skb->sp->len += xfrm_nr; skb->ip_summed =CHECKSUM_NONE ; +nf_reset (skb); +if (decaps) {if (!(skb->dev->flags&IFF_LOOPBACK )) {dst_release (skb->dst);
@@ -129,7 +133,16 @@ int xfrm6_rcv_spi(struct sk_buff **pskb,netif_rx (skb);return -1 ; }else { +#ifdef CONFIG_NETFILTER + skb->nh.ipv6h->payload_len =htons (skb->len); +__skb_push (skb, skb->data - skb->nh.raw); + +NF_HOOK (PF_INET6 ,NF_IP6_PRE_ROUTING , skb, skb->dev,NULL , + ip6_rcv_finish); +return -1 ; +#else return 1 ; +#endif } drop_unlock:
PATCH 4/10 : reset IPCB flags when neccessary
In reply to Patrick McHardy[IPV4]: reset IPCB flags when neccessary
Reset IPSKB_XFRM_TUNNEL_SIZE flags in ipip and ip_gre hard_start_xmit function before the packet reenters IP. This is neccessary so the encapsulated packets are checked not to be oversized in xfrm4_output.c again. Reset all flags in sit when a packet changes its address family.
Also remove some obsolete IPSKB flags.
---
commit 0c72987fae9d4f21d80910ccca2beea4f77e1dc5
tree 71aa056fbac413a11a4d29053595e73803656974
parent 0ab5982305cae5d8e4fb41d8bb849ee1808946fc
author Patrick McHardy <[email protected]> Sat, 07 Jan 2006 00:42:27 +0100 committer Patrick McHardy <[email protected]> Sat, 07 Jan 2006 00:42:27 +0100
include/net/ip.h | 8 +++----- net/ipv4/ip_gre.c | 1 + net/ipv4/ipip.c | 1 + net/ipv6/sit.c | 2 ++ 4 files changed, 7 insertions(+), 5 deletions(-)
include/net/ip.h +3 -5
@@ -37,11 +37,9 @@ struct inet_skb_parmstruct ip_options opt;/* Compiled IP options */ unsigned char flags; -#define IPSKB_MASQUERADED 1 -#define IPSKB_TRANSLATED 2 -#define IPSKB_FORWARDED 4 -#define IPSKB_XFRM_TUNNEL_SIZE 8 -#define IPSKB_FRAG_COMPLETE 16 +#define IPSKB_FORWARDED 1 +#define IPSKB_XFRM_TUNNEL_SIZE 2 +#define IPSKB_FRAG_COMPLETE 4 };struct ipcm_cookie
net/ipv4/ip_gre.c +1 -0
@@ -832,6 +832,7 @@ static int ipgre_tunnel_xmit(struct sk_b skb->h.raw = skb->nh.raw; skb->nh.raw =skb_push (skb, gre_hlen);memset (&(IPCB (skb)->opt),0 ,sizeof (IPCB (skb)->opt)); +IPCB (skb)->flags &= ~IPSKB_XFRM_TUNNEL_SIZE ;dst_release (skb->dst); skb->dst = &rt->u.dst;
net/ipv4/ipip.c +1 -0
@@ -621,6 +621,7 @@ static int ipip_tunnel_xmit(struct sk_bu skb->h.raw = skb->nh.raw; skb->nh.raw =skb_push (skb,sizeof (struct iphdr ));memset (&(IPCB (skb)->opt),0 ,sizeof (IPCB (skb)->opt)); +IPCB (skb)->flags &= ~IPSKB_XFRM_TUNNEL_SIZE ;dst_release (skb->dst); skb->dst = &rt->u.dst;
net/ipv6/sit.c +2 -0
@@ -381,6 +381,7 @@ static int ipip6_rcv(struct sk_buff *skb skb->mac.raw = skb->nh.raw; skb->nh.raw = skb->data;memset (&(IPCB (skb)->opt),0 ,sizeof (struct ip_options )); +IPCB (skb)->flags =0 ; skb->protocol =htons (ETH_P_IPV6 ); skb->pkt_type =PACKET_HOST ; tunnel->stat.rx_packets++;
@@ -552,6 +553,7 @@ static int ipip6_tunnel_xmit(struct sk_b skb->h.raw = skb->nh.raw; skb->nh.raw =skb_push (skb,sizeof (struct iphdr ));memset (&(IPCB (skb)->opt),0 ,sizeof (IPCB (skb)->opt)); +IPCB (skb)->flags =0 ;dst_release (skb->dst); skb->dst = &rt->u.dst;
PATCH 5/10 : Fix xfrm lookup in ip_route_me_harder/ip6_route_me_harder
In reply to Patrick McHardy[NETFILTER]: Fix xfrm lookup in ip_route_me_harder/ip6_route_me_harder
ip_route_me_harder doesn't use the port numbers of the xfrm lookup and uses ip_route_input for non-local addresses which doesn't do a xfrm lookup, ip6_route_me_harder doesn't do a xfrm lookup at all.
Use xfrm_decode_session and do the lookup manually, make sure both only do the lookup if the packet hasn't been transformed already.
Makeing sure the lookup only happens once needs a new field in the IP6CB, which exceeds the size of skb->cb. The size of skb->cb is increased to 48b. Apparently the IPv6 mobile extensions need some more room anyway.
---
commit d923d6ec05f49c2b746a7e8c6d0a71f3bcc35b4e
tree 7005afd82b323b88784953b776f9d95671d4b9f3
parent 0c72987fae9d4f21d80910ccca2beea4f77e1dc5
author Patrick McHardy <[email protected]> Sat, 07 Jan 2006 00:57:41 +0100 committer Patrick McHardy <[email protected]> Sat, 07 Jan 2006 00:57:41 +0100
include/linux/ipv6.h | 3 +++ include/linux/skbuff.h | 2 +- include/net/ip.h | 3 ++- include/net/xfrm.h | 2 +- net/ipv4/ip_gre.c | 2 +- net/ipv4/ipip.c | 2 +- net/ipv4/netfilter.c | 12 ++++++++++-- net/ipv4/xfrm4_output.c | 1 + net/ipv6/netfilter.c | 9 ++++++++- net/ipv6/xfrm6_output.c | 1 + net/xfrm/xfrm_policy.c | 9 +++++---- 11 files changed, 34 insertions(+), 12 deletions(-)
include/linux/ipv6.h +3 -0
@@ -192,6 +192,9 @@ struct inet6_skb_parm {__u16 dst1;__u16 lastopt;__u32 nhoff; +__u16 flags; + +#define IP6SKB_XFRM_TRANSFORMED 1 };#define IP6CB (skb) ((struct inet6_skb_parm*)((skb)->cb))
include/linux/skbuff.h +1 -1
@@ -251,7 +251,7 @@ struct sk_buff {* want to keep them across layers you have to do a skb_clone() * first. This is owned by whoever has the skb queued ATM. */ -char cb[40 ]; +char cb[48 ];unsigned int len, data_len,
include/net/ip.h +2 -1
@@ -39,7 +39,8 @@ struct inet_skb_parm#define IPSKB_FORWARDED 1#define IPSKB_XFRM_TUNNEL_SIZE 2 -#define IPSKB_FRAG_COMPLETE 4 +#define IPSKB_XFRM_TRANSFORMED 4 +#define IPSKB_FRAG_COMPLETE 8 };struct ipcm_cookie
include/net/xfrm.h +1 -1
@@ -668,7 +668,7 @@ static inline int xfrm6_policy_check(strreturn xfrm_policy_check (sk, dir, skb,AF_INET6 ); } - +extern int xfrm_decode_session (struct sk_buff *skb,struct flowi *fl,unsigned short family);extern int __xfrm_route_forward (struct sk_buff *skb,unsigned short family);static inline int xfrm_route_forward (struct sk_buff *skb,unsigned short family)
net/ipv4/ip_gre.c +1 -1
@@ -832,7 +832,7 @@ static int ipgre_tunnel_xmit(struct sk_b skb->h.raw = skb->nh.raw; skb->nh.raw =skb_push (skb, gre_hlen);memset (&(IPCB (skb)->opt),0 ,sizeof (IPCB (skb)->opt)); -IPCB (skb)->flags &= ~IPSKB_XFRM_TUNNEL_SIZE ; +IPCB (skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE |IPSKB_XFRM_TRANSFORMED );dst_release (skb->dst); skb->dst = &rt->u.dst;
net/ipv4/ipip.c +1 -1
@@ -621,7 +621,7 @@ static int ipip_tunnel_xmit(struct sk_bu skb->h.raw = skb->nh.raw; skb->nh.raw =skb_push (skb,sizeof (struct iphdr ));memset (&(IPCB (skb)->opt),0 ,sizeof (IPCB (skb)->opt)); -IPCB (skb)->flags &= ~IPSKB_XFRM_TUNNEL_SIZE ; +IPCB (skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE |IPSKB_XFRM_TRANSFORMED );dst_release (skb->dst); skb->dst = &rt->u.dst;
net/ipv4/netfilter.c +10 -2
@@ -7,11 +7,13 @@#include <linux/netfilter.h> #include <linux/netfilter_ipv4.h> +#include <linux/ip.h> #include <linux/tcp.h> #include <linux/udp.h> #include <linux/icmp.h> #include <net/route.h> -#include <linux/ip.h> +#include <net/xfrm.h> +#include <net/ip.h> /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */ int ip_route_me_harder (struct sk_buff **pskb)
@@ -33,7 +35,6 @@ int ip_route_me_harder(struct sk_buff **#ifdef CONFIG_IP_ROUTE_FWMARK fl.nl_u.ip4_u.fwmark = (*pskb)->nfmark;#endif - fl.proto = iph->protocol;if (ip_route_output_key (&rt, &fl) !=0 )return -1 ;
@@ -60,6 +61,13 @@ int ip_route_me_harder(struct sk_buff **if ((*pskb)->dst->error)return -1 ; +#ifdef CONFIG_XFRM +if (!(IPCB (*pskb)->flags &IPSKB_XFRM_TRANSFORMED ) && +xfrm_decode_session (*pskb, &fl,AF_INET ) ==0 ) +if (xfrm_lookup (&(*pskb)->dst, &fl, (*pskb)->sk,0 )) +return -1 ; +#endif +/* Change in oif may mean change in hh_len. */ hh_len = (*pskb)->dst->dev->hard_header_len;if (skb_headroom (*pskb) < hh_len) {
net/ipv4/xfrm4_output.c +1 -0
@@ -140,6 +140,7 @@ static int xfrm4_output_one(struct sk_bu x = dst->xfrm; }while (x && !x->props.mode); +IPCB (skb)->flags |=IPSKB_XFRM_TRANSFORMED ; err =0 ; out_exit:
net/ipv6/netfilter.c +8 -1
@@ -10,6 +10,7 @@#include <net/dst.h> #include <net/ipv6.h> #include <net/ip6_route.h> +#include <net/xfrm.h> int ip6_route_me_harder (struct sk_buff *skb) {
@@ -21,11 +22,17 @@ int ip6_route_me_harder(struct sk_buff * { .ip6_u = { .daddr = iph->daddr, .saddr = iph->saddr, } }, - .proto = iph->nexthdr, }; dst =ip6_route_output (skb->sk, &fl); +#ifdef CONFIG_XFRM +if (!(IP6CB (skb)->flags &IP6SKB_XFRM_TRANSFORMED ) && +xfrm_decode_session (skb, &fl,AF_INET6 ) ==0 ) +if (xfrm_lookup (&skb->dst, &fl, skb->sk,0 )) +return -1 ; +#endif +if (dst->error) {IP6_INC_STATS (IPSTATS_MIB_OUTNOROUTES );LIMIT_NETDEBUG (KERN_DEBUG "ip6_route_me_harder: No more route.\n" );
net/ipv6/xfrm6_output.c +1 -0
@@ -139,6 +139,7 @@ static int xfrm6_output_one(struct sk_bu x = dst->xfrm; }while (x && !x->props.mode); +IP6CB (skb)->flags |=IP6SKB_XFRM_TRANSFORMED ; err =0 ; out_exit:
net/xfrm/xfrm_policy.c +5 -4
@@ -951,8 +951,8 @@ xfrm_policy_ok(struct xfrm_tmpl *tmpl, sreturn start; } -static int -_decode_session (struct sk_buff *skb,struct flowi *fl,unsigned short family) +int +xfrm_decode_session (struct sk_buff *skb,struct flowi *fl,unsigned short family) {struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family);
@@ -963,6 +963,7 @@ _decode_session(struct sk_buff *skb, strxfrm_policy_put_afinfo (afinfo);return 0 ; } +EXPORT_SYMBOL (xfrm_decode_session);static inline int secpath_has_tunnel (struct sec_path *sp,int k) {
@@ -982,7 +983,7 @@ int __xfrm_policy_check(struct sock *sk,u8 fl_dir =policy_to_flow_dir (dir);u32 sk_sid; -if (_decode_session (skb, &fl, family) <0 ) +if (xfrm_decode_session (skb, &fl, family) <0 )return 0 ; sk_sid =security_sk_sid (sk, &fl, fl_dir);
@@ -1055,7 +1056,7 @@ int __xfrm_route_forward(struct sk_buff {struct flowi fl; -if (_decode_session (skb, &fl, family) <0 ) +if (xfrm_decode_session (skb, &fl, family) <0 )return 0 ;return xfrm_lookup (&skb->dst, &fl,NULL ,0 ) ==0 ;
PATCH 6/10 : Use conntrack information to determine if packet was NATed
In reply to Patrick McHardy[NETFILTER]: Use conntrack information to determine if packet was NATed
Preparation for IPsec support for NAT:
Use conntrack information instead of saving the saving and comparing the addresses to determine if a packet was NATed and needs to be rerouted to make it easier to extend the key.
---
commit 3ba04601534a6f050fb695c92ddc62d403078cc6
tree 01e3981c9fae596a30f0ce815f9695f71417724b
parent d923d6ec05f49c2b746a7e8c6d0a71f3bcc35b4e
author Patrick McHardy <[email protected]> Sat, 07 Jan 2006 01:02:59 +0100 committer Patrick McHardy <[email protected]> Sat, 07 Jan 2006 01:02:59 +0100
net/ipv4/netfilter/ip_nat_standalone.c | 34 ++++++++++++++++++-------------- 1 files changed, 19 insertions(+), 15 deletions(-)
net/ipv4/netfilter/ip_nat_standalone.c +19 -15
@@ -162,18 +162,20 @@ ip_nat_in(unsigned int hooknum,const struct net_device *out, int (*okfn)(struct sk_buff *)) { - u_int32_t saddr, daddr; + struct ip_conntrack *ct; +enum ip_conntrack_info ctinfo;unsigned int ret; - saddr = (*pskb)->nh.iph->saddr; - daddr = (*pskb)->nh.iph->daddr; - ret =ip_nat_fn (hooknum, pskb, in, out, okfn);if (ret !=NF_DROP && ret !=NF_STOLEN - && ((*pskb)->nh.iph->saddr != saddr - || (*pskb)->nh.iph->daddr != daddr)) { -dst_release ((*pskb)->dst); - (*pskb)->dst =NULL ; + && (ct =ip_conntrack_get (*pskb, &ctinfo)) !=NULL ) { +enum ip_conntrack_dir dir =CTINFO2DIR (ctinfo); + +if (ct->tuplehash[dir].tuple.src.ip != + ct->tuplehash[!dir].tuple.dst.ip) { +dst_release ((*pskb)->dst); + (*pskb)->dst =NULL ; + } }return ret; }
@@ -200,7 +202,8 @@ ip_nat_local_fn(unsigned int hooknum,const struct net_device *out, int (*okfn)(struct sk_buff *)) { - u_int32_t saddr, daddr; + struct ip_conntrack *ct; +enum ip_conntrack_info ctinfo;unsigned int ret;/* root is playing with raw sockets. */
@@ -208,14 +211,15 @@ ip_nat_local_fn(unsigned int hooknum, || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))return NF_ACCEPT ; - saddr = (*pskb)->nh.iph->saddr; - daddr = (*pskb)->nh.iph->daddr; - ret =ip_nat_fn (hooknum, pskb, in, out, okfn);if (ret !=NF_DROP && ret !=NF_STOLEN - && ((*pskb)->nh.iph->saddr != saddr - || (*pskb)->nh.iph->daddr != daddr)) -return ip_route_me_harder (pskb) ==0 ? ret :NF_DROP ; + && (ct =ip_conntrack_get (*pskb, &ctinfo)) !=NULL ) { +enum ip_conntrack_dir dir =CTINFO2DIR (ctinfo); + +if (ct->tuplehash[dir].tuple.dst.ip != + ct->tuplehash[!dir].tuple.src.ip) +return ip_route_me_harder (pskb) ==0 ? ret :NF_DROP ; + }return ret; }
PATCH 7/10 : Redo policy lookups after NAT when neccessary
In reply to Patrick McHardy[NETFILTER]: Redo policy lookups after NAT when neccessary
When NAT changes the key used for the xfrm lookup it needs to be done again. If a new policy is returned in POST_ROUTING the packet needs to be passed to xfrm4_output_one manually after all hooks were called because POST_ROUTING is called with fixed okfn (ip_finish_output).
---
commit 00b264ce82ccc3216e3e4b0175dd1337eeb16f24
tree e774053af87fff87b29e652c6a90588c30695224
parent 3ba04601534a6f050fb695c92ddc62d403078cc6
author Patrick McHardy <[email protected]> Sat, 07 Jan 2006 01:18:04 +0100 committer Patrick McHardy <[email protected]> Sat, 07 Jan 2006 01:18:04 +0100
include/net/xfrm.h | 1 + net/ipv4/ip_output.c | 5 +++++ net/ipv4/netfilter/ip_nat_standalone.c | 27 +++++++++++++++++++++++++-- net/ipv4/xfrm4_output.c | 2 +- 4 files changed, 32 insertions(+), 3 deletions(-)
include/net/xfrm.h +1 -0
@@ -866,6 +866,7 @@ extern int xfrm_state_mtu(struct xfrm_stextern int xfrm_init_state (struct xfrm_state *x);extern int xfrm4_rcv (struct sk_buff *skb);extern int xfrm4_output (struct sk_buff *skb); +extern int xfrm4_output_finish (struct sk_buff *skb);extern int xfrm4_tunnel_register (struct xfrm_tunnel *handler);extern int xfrm4_tunnel_deregister (struct xfrm_tunnel *handler);extern int xfrm6_rcv_spi (struct sk_buff **pskb,u32 spi);
net/ipv4/ip_output.c +5 -0
@@ -202,6 +202,11 @@ static inline int ip_finish_output2(strustatic inline int ip_finish_output (struct sk_buff *skb) { +#if defined(CONFIG_NETFILTER ) && defined(CONFIG_XFRM ) +/* Policy lookup after SNAT yielded a new policy */ +if (skb->dst->xfrm !=NULL ) +return xfrm4_output_finish (skb); +#endif if (skb->len >dst_mtu (skb->dst) && !(skb_shinfo (skb)->ufo_size ||skb_shinfo (skb)->tso_size))return ip_fragment (skb, ip_finish_output2);
net/ipv4/netfilter/ip_nat_standalone.c +25 -2
@@ -187,12 +187,30 @@ ip_nat_out(unsigned int hooknum,const struct net_device *out, int (*okfn)(struct sk_buff *)) { + struct ip_conntrack *ct; +enum ip_conntrack_info ctinfo; +unsigned int ret; +/* root is playing with raw sockets. */ if ((*pskb)->len <sizeof (struct iphdr ) || (*pskb)->nh.iph->ihl *4 <sizeof (struct iphdr ))return NF_ACCEPT ; -return ip_nat_fn (hooknum, pskb, in, out, okfn); + ret =ip_nat_fn (hooknum, pskb, in, out, okfn); +if (ret !=NF_DROP && ret !=NF_STOLEN + && (ct =ip_conntrack_get (*pskb, &ctinfo)) !=NULL ) { +enum ip_conntrack_dir dir =CTINFO2DIR (ctinfo); + +if (ct->tuplehash[dir].tuple.src.ip != + ct->tuplehash[!dir].tuple.dst.ip +#ifdef CONFIG_XFRM + || ct->tuplehash[dir].tuple.src.u.all != + ct->tuplehash[!dir].tuple.dst.u.all +#endif + ) +return ip_route_me_harder (pskb) ==0 ? ret :NF_DROP ; + } +return ret; } static unsigned int
@@ -217,7 +235,12 @@ ip_nat_local_fn(unsigned int hooknum,enum ip_conntrack_dir dir =CTINFO2DIR (ctinfo);if (ct->tuplehash[dir].tuple.dst.ip != - ct->tuplehash[!dir].tuple.src.ip) + ct->tuplehash[!dir].tuple.src.ip +#ifdef CONFIG_XFRM + || ct->tuplehash[dir].tuple.dst.u.all != + ct->tuplehash[dir].tuple.src.u.all +#endif + )return ip_route_me_harder (pskb) ==0 ? ret :NF_DROP ; }return ret;
net/ipv4/xfrm4_output.c +1 -1
@@ -152,7 +152,7 @@ error_nolock: goto out_exit; } -static int xfrm4_output_finish (struct sk_buff *skb) +int xfrm4_output_finish (struct sk_buff *skb) {int err;
PATCH 8/10 : Keep conntrack reference until IPsec policy checks are done
In reply to Patrick McHardy[NETFILTER]: Keep conntrack reference until IPsec policy checks are done
Keep the conntrack reference until policy checks have been performed for IPsec NAT support. The reference needs to be dropped before a packet is queued to avoid having the conntrack module unloadable.
---
commit 7573dacaa96b4bd8ddc1be7139d17f7c95010413
tree 63a51c62e2d7a4730df5339a0f7d22f1121c8750
parent 00b264ce82ccc3216e3e4b0175dd1337eeb16f24
author Patrick McHardy <[email protected]> Sat, 07 Jan 2006 01:22:09 +0100 committer Patrick McHardy <[email protected]> Sat, 07 Jan 2006 01:22:09 +0100
net/dccp/ipv4.c | 1 + net/ipv4/ip_input.c | 15 ++++++--------- net/ipv4/raw.c | 1 + net/ipv4/tcp_ipv4.c | 1 + net/ipv4/udp.c | 2 ++ net/sctp/input.c | 1 + 6 files changed, 12 insertions(+), 9 deletions(-)
net/dccp/ipv4.c +1 -0
@@ -1099,6 +1099,7 @@ int dccp_v4_destroy_sock(struct sock *skkfree_skb (sk->sk_send_head); sk->sk_send_head =NULL ; } +nf_reset (skb);/* Clean up a referenced DCCP bind bucket. */ if (inet_csk (sk)->icsk_bind_hash !=NULL )
net/ipv4/ip_input.c +6 -9
@@ -185,7 +185,6 @@ int ip_call_ra_chain(struct sk_buff *skbraw_rcv (last, skb2); } last = sk; -nf_reset (skb); } }
@@ -204,10 +203,6 @@ static inline int ip_local_deliver_finis__skb_pull (skb, ihl); -/* Free reference early: we don't need it any more, and it may -hold ip_conntrack module loaded indefinitely. */ -nf_reset (skb); -/* Point into the IP datagram, just past the header. */ skb->h.raw = skb->data;
@@ -232,10 +227,12 @@ static inline int ip_local_deliver_finisif ((ipprot =rcu_dereference (inet_protos[hash])) !=NULL ) {int ret; -if (!ipprot->no_policy && - !xfrm4_policy_check (NULL ,XFRM_POLICY_IN , skb)) { -kfree_skb (skb); - goto out; +if (!ipprot->no_policy) { +if (!xfrm4_policy_check (NULL ,XFRM_POLICY_IN , skb)) { +kfree_skb (skb); + goto out; + } +nf_reset (skb); } ret = ipprot->handler (skb);if (ret <0 ) {
net/ipv4/raw.c +1 -0
@@ -255,6 +255,7 @@ int raw_rcv(struct sock *sk, struct sk_bkfree_skb (skb);return NET_RX_DROP ; } +nf_reset (skb);skb_push (skb, skb->data - skb->nh.raw);
net/ipv4/tcp_ipv4.c +1 -0
@@ -1080,6 +1080,7 @@ process:if (!xfrm4_policy_check (sk,XFRM_POLICY_IN , skb)) goto discard_and_relse; +nf_reset (skb);if (sk_filter (sk, skb,0 )) goto discard_and_relse;
net/ipv4/udp.c +2 -0
@@ -989,6 +989,7 @@ static int udp_queue_rcv_skb(struct sockkfree_skb (skb);return -1 ; } +nf_reset (skb);if (up->encap_type) {/*
@@ -1149,6 +1150,7 @@ int udp_rcv(struct sk_buff *skb)if (!xfrm4_policy_check (NULL ,XFRM_POLICY_IN , skb)) goto drop; +nf_reset (skb);/* No socket. Drop packet silently, if checksum is wrong */ if (udp_checksum_complete (skb))
net/sctp/input.c +1 -0
@@ -225,6 +225,7 @@ int sctp_rcv(struct sk_buff *skb)if (!xfrm_policy_check (sk,XFRM_POLICY_IN , skb, family)) goto discard_release; +nf_reset (skb); ret =sk_filter (sk, skb,1 );if (ret)
PATCH 9/10 : Handle NAT in IPsec policy checks
In reply to Patrick McHardy[NETFILTER]: Handle NAT in IPsec policy checks
Handle NAT of decapsulated IPsec packets by reconstructing the struct flowi of the original packet from the conntrack information for IPsec policy checks.
---
commit dcaa54019ab79d6db75572a239fdc0df0d8019be
tree cf231c2e385835e3c7e25772c86c480e3646fd84
parent 7573dacaa96b4bd8ddc1be7139d17f7c95010413
author Patrick McHardy <[email protected]> Sat, 07 Jan 2006 01:27:39 +0100 committer Patrick McHardy <[email protected]> Sat, 07 Jan 2006 01:27:39 +0100
include/linux/netfilter.h | 16 ++++++++++ net/dccp/ipv4.c | 2 + net/ipv4/netfilter.c | 3 ++ net/ipv4/netfilter/ip_nat_standalone.c | 50 +++++++++++++++++++++++++++++++- net/xfrm/xfrm_policy.c | 2 + 5 files changed, 70 insertions(+), 3 deletions(-)
include/linux/netfilter.h +16 -0
@@ -274,6 +274,20 @@ struct nf_queue_rerouter {extern int nf_register_queue_rerouter (int pf,struct nf_queue_rerouter *rer);extern int nf_unregister_queue_rerouter (int pf); +#include <net/flow.h> +extern void (*ip_nat_decode_session)(struct sk_buff *,struct flowi *); + +static inline void +nf_nat_decode_session (struct sk_buff *skb,struct flowi *fl,int family) +{ +#ifdef CONFIG_IP_NF_NAT_NEEDED +void (*decodefn)(struct sk_buff *,struct flowi *); + +if (family ==AF_INET && (decodefn = ip_nat_decode_session) !=NULL ) +decodefn (skb, fl); +#endif +} +#ifdef CONFIG_PROC_FS #include <linux/proc_fs.h> extern struct proc_dir_entry *proc_net_netfilter;
@@ -282,6 +296,8 @@ extern struct proc_dir_entry *proc_net_n#else /* !CONFIG_NETFILTER */ #define NF_HOOK (pf, hook, skb, indev, outdev, okfn) (okfn)(skb)static inline void nf_ct_attach (struct sk_buff *new,struct sk_buff *skb) {} +static inline void +nf_nat_decode_session (struct sk_buff *skb,struct flowi *fl,int family) {}#endif /*CONFIG_NETFILTER*/ #endif /*__KERNEL__*/
net/dccp/ipv4.c +1 -1
@@ -986,6 +986,7 @@ int dccp_v4_rcv(struct sk_buff *skb)if (!xfrm4_policy_check (sk,XFRM_POLICY_IN , skb)) goto discard_and_relse; +nf_reset (skb);return sk_receive_skb (sk, skb);
@@ -1099,7 +1100,6 @@ int dccp_v4_destroy_sock(struct sock *skkfree_skb (sk->sk_send_head); sk->sk_send_head =NULL ; } -nf_reset (skb);/* Clean up a referenced DCCP bind bucket. */ if (inet_csk (sk)->icsk_bind_hash !=NULL )
net/ipv4/netfilter.c +3 -0
@@ -86,6 +86,9 @@ int ip_route_me_harder(struct sk_buff ** }EXPORT_SYMBOL (ip_route_me_harder); +void (*ip_nat_decode_session)(struct sk_buff *,struct flowi *); +EXPORT_SYMBOL (ip_nat_decode_session); +/* * Extra routing may needed on local out, as the QUEUE target never * returns control to the table.
net/ipv4/netfilter/ip_nat_standalone.c +48 -2
71 lines not on this page · raw message
net/xfrm/xfrm_policy.c +2 -0
@@ -22,6 +22,7 @@#include <linux/workqueue.h> #include <linux/notifier.h> #include <linux/netdevice.h> +#include <linux/netfilter.h> #include <linux/module.h> #include <net/xfrm.h> #include <net/ip.h>
@@ -985,6 +986,7 @@ int __xfrm_policy_check(struct sock *sk,if (xfrm_decode_session (skb, &fl, family) <0 )return 0 ; +nf_nat_decode_session (skb, &fl, family); sk_sid =security_sk_sid (sk, &fl, fl_dir);
PATCH 10/10 : Add ipt_policy/ip6t_policy matches
In reply to Patrick McHardy[NETFILTER]: Add ipt_policy/ip6t_policy matches
---
commit 77b5a9142e3a53b375c6c21ec80fdbe890a8403c
tree e9da4deaa832b50dfa746ed1e3fa873972dcbc42
parent dcaa54019ab79d6db75572a239fdc0df0d8019be
author Patrick McHardy <[email protected]> Sat, 07 Jan 2006 01:30:29 +0100 committer Patrick McHardy <[email protected]> Sat, 07 Jan 2006 01:30:29 +0100
include/linux/netfilter_ipv4/ipt_policy.h | 52 ++++++++ include/linux/netfilter_ipv6/ip6t_policy.h | 52 ++++++++ net/ipv4/netfilter/Kconfig | 10 ++ net/ipv4/netfilter/Makefile | 1 net/ipv4/netfilter/ipt_policy.c | 170 +++++++++++++++++++++++++++ net/ipv6/netfilter/Kconfig | 10 ++ net/ipv6/netfilter/Makefile | 1 net/ipv6/netfilter/ip6t_policy.c | 175 ++++++++++++++++++++++++++++ 8 files changed, 471 insertions(+), 0 deletions(-)
include/linux/netfilter_ipv4/ipt_policy.h new file +52 -0
@@ -0,0 +1,52 @@ +#ifndef _IPT_POLICY_H +#define _IPT_POLICY_H + +#define IPT_POLICY_MAX_ELEM 4 + +enum ipt_policy_flags +{ +IPT_POLICY_MATCH_IN =0x1 , +IPT_POLICY_MATCH_OUT =0x2 , +IPT_POLICY_MATCH_NONE =0x4 , +IPT_POLICY_MATCH_STRICT =0x8 , +}; + +enum ipt_policy_modes +{ +IPT_POLICY_MODE_TRANSPORT , +IPT_POLICY_MODE_TUNNEL +}; + +struct ipt_policy_spec +{ +u_int8_t saddr:1 , + daddr:1 , + proto:1 , + mode:1 , + spi:1 , + reqid:1 ; +}; + +struct ipt_policy_elem +{ +u_int32_t saddr; +u_int32_t smask; +u_int32_t daddr; +u_int32_t dmask; +u_int32_t spi; +u_int32_t reqid; +u_int8_t proto; +u_int8_t mode; + +struct ipt_policy_spec match; +struct ipt_policy_spec invert; +}; + +struct ipt_policy_info +{ +struct ipt_policy_elem pol[IPT_POLICY_MAX_ELEM ]; +u_int16_t flags; +u_int16_t len; +}; + +#endif /* _IPT_POLICY_H */
include/linux/netfilter_ipv6/ip6t_policy.h new file +52 -0
52 lines not on this page · raw message
net/ipv4/netfilter/Kconfig +10 -0
@@ -487,6 +487,16 @@ config IP_NF_MATCH_STRING To compile it as a module, choose M here. If unsure, say N. +config IP_NF_MATCH_POLICY + tristate "IPsec policy match support" + depends on IP_NF_IPTABLES && XFRM + help + Policy matching allows you to match packets based on the + IPsec policy that was used during decapsulation/will + be used during encapsulation. + + To compile it as a module, choose M here. If unsure, say N. + # `filter', generic and specific targets config IP_NF_FILTER tristate "Packet filtering"
net/ipv4/netfilter/Makefile +1 -0
@@ -72,6 +72,7 @@ obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_ obj-$(CONFIG_IP_NF_MATCH_REALM) += ipt_realm.o obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o obj-$(CONFIG_IP_NF_MATCH_PHYSDEV) += ipt_physdev.o +obj-$(CONFIG_IP_NF_MATCH_POLICY) += ipt_policy.o obj-$(CONFIG_IP_NF_MATCH_COMMENT) += ipt_comment.o obj-$(CONFIG_IP_NF_MATCH_STRING) += ipt_string.o
net/ipv4/netfilter/ipt_policy.c new file +170 -0
170 lines not on this page · raw message
net/ipv6/netfilter/Kconfig +10 -0
@@ -179,6 +179,16 @@ config IP6_NF_MATCH_PHYSDEV To compile it as a module, choose M here. If unsure, say N. +config IP6_NF_MATCH_POLICY + tristate "IPsec policy match support" + depends on IP6_NF_IPTABLES && XFRM + help + Policy matching allows you to match packets based on the + IPsec policy that was used during decapsulation/will + be used during encapsulation. + + To compile it as a module, choose M here. If unsure, say N. + # The targets config IP6_NF_FILTER tristate "Packet filtering"
net/ipv6/netfilter/Makefile +1 -0
@@ -13,6 +13,7 @@ obj-$(CONFIG_IP6_NF_MATCH_OPTS) += ip6t_ obj-$(CONFIG_IP6_NF_MATCH_IPV6HEADER) += ip6t_ipv6header.o obj-$(CONFIG_IP6_NF_MATCH_FRAG) += ip6t_frag.o obj-$(CONFIG_IP6_NF_MATCH_AHESP) += ip6t_esp.o ip6t_ah.o +obj-$(CONFIG_IP6_NF_MATCH_POLICY) += ip6t_policy.o obj-$(CONFIG_IP6_NF_MATCH_EUI64) += ip6t_eui64.o obj-$(CONFIG_IP6_NF_MATCH_MULTIPORT) += ip6t_multiport.o obj-$(CONFIG_IP6_NF_MATCH_OWNER) += ip6t_owner.o
net/ipv6/netfilter/ip6t_policy.c new file +175 -0
175 lines not on this page · raw message
In article <[email protected]> (at Sat, 7 Jan 2006 02:09:30 +0100 (MET)), Patrick McHardy <[email protected]> says:
Quoted 4 lines
following are the remaining patches for netfilter IPsec support. They are missing the common-case optimization for inner transport mode SAs on the input path, but since its just an optimization, I think it can also be done later. One note: unfortunately I had to increase
I definitely want to do it before 2.6.16. Anyway, we'll test this series of patches. Thank you.
--yoshfuji
Quoted 10 lines · YOSHIFUJI Hideaki / $B5HF#1QL@ wrote:
In article <[email protected]> (at Sat, 7 Jan 2006 02:09:30 +0100 (MET)), Patrick McHardy <[email protected]> says:
Quoted 4 lines
following are the remaining patches for netfilter IPsec support. They are missing the common-case optimization for inner transport mode SAs on the input path, but since its just an optimization, I think it can also be done later. One note: unfortunately I had to increase
I definitely want to do it before 2.6.16.
That shouldn't be a problem, its not hard, but I think it might get a bit ugly.
Quoted 1 line
Anyway, we'll test this series of patches.
Thanks.