2007-09-27 23:48:05 +04:00
/*
* Stateless NAT actions
*
* Copyright ( c ) 2007 Herbert Xu < herbert @ gondor . apana . org . au >
*
* This program is free software ; you can redistribute it and / or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation ; either version 2 of the License , or ( at your option )
* any later version .
*/
# include <linux/errno.h>
# include <linux/init.h>
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/netfilter.h>
# include <linux/rtnetlink.h>
# include <linux/skbuff.h>
# include <linux/slab.h>
# include <linux/spinlock.h>
# include <linux/string.h>
# include <linux/tc_act/tc_nat.h>
# include <net/act_api.h>
# include <net/icmp.h>
# include <net/ip.h>
# include <net/netlink.h>
# include <net/tc_act/tc_nat.h>
# include <net/tcp.h>
# include <net/udp.h>
# define NAT_TAB_MASK 15
2013-12-16 08:15:08 +04:00
static struct tcf_hashinfo nat_hash_info ;
2007-09-27 23:48:05 +04:00
2008-01-24 07:36:30 +03:00
static const struct nla_policy nat_policy [ TCA_NAT_MAX + 1 ] = {
[ TCA_NAT_PARMS ] = { . len = sizeof ( struct tc_nat ) } ,
} ;
2013-01-14 09:15:39 +04:00
static int tcf_nat_init ( struct net * net , struct nlattr * nla , struct nlattr * est ,
2007-09-27 23:48:05 +04:00
struct tc_action * a , int ovr , int bind )
{
2008-01-23 09:11:50 +03:00
struct nlattr * tb [ TCA_NAT_MAX + 1 ] ;
2007-09-27 23:48:05 +04:00
struct tc_nat * parm ;
2008-01-24 07:33:32 +03:00
int ret = 0 , err ;
2007-09-27 23:48:05 +04:00
struct tcf_nat * p ;
struct tcf_common * pc ;
2008-01-24 07:33:32 +03:00
if ( nla = = NULL )
2007-09-27 23:48:05 +04:00
return - EINVAL ;
2008-01-24 07:36:30 +03:00
err = nla_parse_nested ( tb , TCA_NAT_MAX , nla , nat_policy ) ;
2008-01-24 07:33:32 +03:00
if ( err < 0 )
return err ;
2008-01-24 07:36:30 +03:00
if ( tb [ TCA_NAT_PARMS ] = = NULL )
2007-09-27 23:48:05 +04:00
return - EINVAL ;
2008-01-23 09:11:50 +03:00
parm = nla_data ( tb [ TCA_NAT_PARMS ] ) ;
2007-09-27 23:48:05 +04:00
2014-01-17 23:37:02 +04:00
pc = tcf_hash_check ( parm - > index , a , bind ) ;
2007-09-27 23:48:05 +04:00
if ( ! pc ) {
2014-01-17 23:37:02 +04:00
pc = tcf_hash_create ( parm - > index , est , a , sizeof ( * p ) , bind ) ;
2008-11-26 08:12:32 +03:00
if ( IS_ERR ( pc ) )
2011-01-19 22:26:56 +03:00
return PTR_ERR ( pc ) ;
2007-09-27 23:48:05 +04:00
ret = ACT_P_CREATED ;
} else {
2013-12-23 17:02:11 +04:00
if ( bind )
return 0 ;
2014-01-17 23:37:02 +04:00
tcf_hash_release ( pc , bind , a - > ops - > hinfo ) ;
2013-12-23 17:02:11 +04:00
if ( ! ovr )
2007-09-27 23:48:05 +04:00
return - EEXIST ;
}
2013-12-23 17:02:11 +04:00
p = to_tcf_nat ( pc ) ;
2007-09-27 23:48:05 +04:00
spin_lock_bh ( & p - > tcf_lock ) ;
p - > old_addr = parm - > old_addr ;
p - > new_addr = parm - > new_addr ;
p - > mask = parm - > mask ;
p - > flags = parm - > flags ;
p - > tcf_action = parm - > action ;
spin_unlock_bh ( & p - > tcf_lock ) ;
if ( ret = = ACT_P_CREATED )
2014-01-17 23:37:02 +04:00
tcf_hash_insert ( pc , a - > ops - > hinfo ) ;
2007-09-27 23:48:05 +04:00
return ret ;
}
static int tcf_nat_cleanup ( struct tc_action * a , int bind )
{
struct tcf_nat * p = a - > priv ;
return tcf_hash_release ( & p - > common , bind , & nat_hash_info ) ;
}
2011-07-06 03:25:42 +04:00
static int tcf_nat ( struct sk_buff * skb , const struct tc_action * a ,
2007-09-27 23:48:05 +04:00
struct tcf_result * res )
{
struct tcf_nat * p = a - > priv ;
struct iphdr * iph ;
__be32 old_addr ;
__be32 new_addr ;
__be32 mask ;
__be32 addr ;
int egress ;
int action ;
int ihl ;
2010-08-03 21:39:18 +04:00
int noff ;
2007-09-27 23:48:05 +04:00
spin_lock ( & p - > tcf_lock ) ;
p - > tcf_tm . lastuse = jiffies ;
old_addr = p - > old_addr ;
new_addr = p - > new_addr ;
mask = p - > mask ;
egress = p - > flags & TCA_NAT_FLAG_EGRESS ;
action = p - > tcf_action ;
2011-01-09 11:30:54 +03:00
bstats_update ( & p - > tcf_bstats , skb ) ;
2007-09-27 23:48:05 +04:00
spin_unlock ( & p - > tcf_lock ) ;
if ( unlikely ( action = = TC_ACT_SHOT ) )
goto drop ;
2010-08-03 21:39:18 +04:00
noff = skb_network_offset ( skb ) ;
if ( ! pskb_may_pull ( skb , sizeof ( * iph ) + noff ) )
2007-09-27 23:48:05 +04:00
goto drop ;
iph = ip_hdr ( skb ) ;
if ( egress )
addr = iph - > saddr ;
else
addr = iph - > daddr ;
if ( ! ( ( old_addr ^ addr ) & mask ) ) {
if ( skb_cloned ( skb ) & &
2010-08-03 21:39:18 +04:00
! skb_clone_writable ( skb , sizeof ( * iph ) + noff ) & &
2007-09-27 23:48:05 +04:00
pskb_expand_head ( skb , 0 , 0 , GFP_ATOMIC ) )
goto drop ;
new_addr & = mask ;
new_addr | = addr & ~ mask ;
/* Rewrite IP header */
iph = ip_hdr ( skb ) ;
if ( egress )
iph - > saddr = new_addr ;
else
iph - > daddr = new_addr ;
2007-11-29 17:17:11 +03:00
csum_replace4 ( & iph - > check , addr , new_addr ) ;
2010-05-29 18:26:59 +04:00
} else if ( ( iph - > frag_off & htons ( IP_OFFSET ) ) | |
iph - > protocol ! = IPPROTO_ICMP ) {
goto out ;
2007-09-27 23:48:05 +04:00
}
ihl = iph - > ihl * 4 ;
/* It would be nice to share code with stateful NAT. */
switch ( iph - > frag_off & htons ( IP_OFFSET ) ? 0 : iph - > protocol ) {
case IPPROTO_TCP :
{
struct tcphdr * tcph ;
2010-08-03 21:39:18 +04:00
if ( ! pskb_may_pull ( skb , ihl + sizeof ( * tcph ) + noff ) | |
2007-09-27 23:48:05 +04:00
( skb_cloned ( skb ) & &
2010-08-03 21:39:18 +04:00
! skb_clone_writable ( skb , ihl + sizeof ( * tcph ) + noff ) & &
2007-09-27 23:48:05 +04:00
pskb_expand_head ( skb , 0 , 0 , GFP_ATOMIC ) ) )
goto drop ;
tcph = ( void * ) ( skb_network_header ( skb ) + ihl ) ;
2007-11-29 17:17:11 +03:00
inet_proto_csum_replace4 ( & tcph - > check , skb , addr , new_addr , 1 ) ;
2007-09-27 23:48:05 +04:00
break ;
}
case IPPROTO_UDP :
{
struct udphdr * udph ;
2010-08-03 21:39:18 +04:00
if ( ! pskb_may_pull ( skb , ihl + sizeof ( * udph ) + noff ) | |
2007-09-27 23:48:05 +04:00
( skb_cloned ( skb ) & &
2010-08-03 21:39:18 +04:00
! skb_clone_writable ( skb , ihl + sizeof ( * udph ) + noff ) & &
2007-09-27 23:48:05 +04:00
pskb_expand_head ( skb , 0 , 0 , GFP_ATOMIC ) ) )
goto drop ;
udph = ( void * ) ( skb_network_header ( skb ) + ihl ) ;
if ( udph - > check | | skb - > ip_summed = = CHECKSUM_PARTIAL ) {
2007-11-29 17:17:11 +03:00
inet_proto_csum_replace4 ( & udph - > check , skb , addr ,
new_addr , 1 ) ;
2007-09-27 23:48:05 +04:00
if ( ! udph - > check )
udph - > check = CSUM_MANGLED_0 ;
}
break ;
}
case IPPROTO_ICMP :
{
struct icmphdr * icmph ;
2010-08-03 21:39:18 +04:00
if ( ! pskb_may_pull ( skb , ihl + sizeof ( * icmph ) + noff ) )
2007-09-27 23:48:05 +04:00
goto drop ;
icmph = ( void * ) ( skb_network_header ( skb ) + ihl ) ;
if ( ( icmph - > type ! = ICMP_DEST_UNREACH ) & &
( icmph - > type ! = ICMP_TIME_EXCEEDED ) & &
( icmph - > type ! = ICMP_PARAMETERPROB ) )
break ;
2010-08-03 21:39:18 +04:00
if ( ! pskb_may_pull ( skb , ihl + sizeof ( * icmph ) + sizeof ( * iph ) +
noff ) )
2010-07-09 19:33:25 +04:00
goto drop ;
2010-07-29 17:41:46 +04:00
icmph = ( void * ) ( skb_network_header ( skb ) + ihl ) ;
2007-09-27 23:48:05 +04:00
iph = ( void * ) ( icmph + 1 ) ;
if ( egress )
addr = iph - > daddr ;
else
addr = iph - > saddr ;
if ( ( old_addr ^ addr ) & mask )
break ;
if ( skb_cloned ( skb ) & &
2010-08-03 21:39:18 +04:00
! skb_clone_writable ( skb , ihl + sizeof ( * icmph ) +
sizeof ( * iph ) + noff ) & &
2007-09-27 23:48:05 +04:00
pskb_expand_head ( skb , 0 , 0 , GFP_ATOMIC ) )
goto drop ;
icmph = ( void * ) ( skb_network_header ( skb ) + ihl ) ;
iph = ( void * ) ( icmph + 1 ) ;
new_addr & = mask ;
new_addr | = addr & ~ mask ;
/* XXX Fix up the inner checksums. */
if ( egress )
iph - > daddr = new_addr ;
else
iph - > saddr = new_addr ;
2007-11-29 17:17:11 +03:00
inet_proto_csum_replace4 ( & icmph - > checksum , skb , addr , new_addr ,
2010-07-29 18:04:18 +04:00
0 ) ;
2007-09-27 23:48:05 +04:00
break ;
}
default :
break ;
}
2010-05-29 18:26:59 +04:00
out :
2007-09-27 23:48:05 +04:00
return action ;
drop :
spin_lock ( & p - > tcf_lock ) ;
p - > tcf_qstats . drops + + ;
spin_unlock ( & p - > tcf_lock ) ;
return TC_ACT_SHOT ;
}
static int tcf_nat_dump ( struct sk_buff * skb , struct tc_action * a ,
int bind , int ref )
{
unsigned char * b = skb_tail_pointer ( skb ) ;
struct tcf_nat * p = a - > priv ;
2010-08-17 00:04:22 +04:00
struct tc_nat opt = {
. old_addr = p - > old_addr ,
. new_addr = p - > new_addr ,
. mask = p - > mask ,
. flags = p - > flags ,
. index = p - > tcf_index ,
. action = p - > tcf_action ,
. refcnt = p - > tcf_refcnt - ref ,
. bindcnt = p - > tcf_bindcnt - bind ,
} ;
2007-09-27 23:48:05 +04:00
struct tcf_t t ;
2012-03-29 13:11:39 +04:00
if ( nla_put ( skb , TCA_NAT_PARMS , sizeof ( opt ) , & opt ) )
goto nla_put_failure ;
2007-09-27 23:48:05 +04:00
t . install = jiffies_to_clock_t ( jiffies - p - > tcf_tm . install ) ;
t . lastuse = jiffies_to_clock_t ( jiffies - p - > tcf_tm . lastuse ) ;
t . expires = jiffies_to_clock_t ( p - > tcf_tm . expires ) ;
2012-03-29 13:11:39 +04:00
if ( nla_put ( skb , TCA_NAT_TM , sizeof ( t ) , & t ) )
goto nla_put_failure ;
2007-09-27 23:48:05 +04:00
return skb - > len ;
2008-01-23 09:11:50 +03:00
nla_put_failure :
2007-09-27 23:48:05 +04:00
nlmsg_trim ( skb , b ) ;
return - 1 ;
}
static struct tc_action_ops act_nat_ops = {
. kind = " nat " ,
. hinfo = & nat_hash_info ,
. type = TCA_ACT_NAT ,
. owner = THIS_MODULE ,
. act = tcf_nat ,
. dump = tcf_nat_dump ,
. cleanup = tcf_nat_cleanup ,
. init = tcf_nat_init ,
} ;
MODULE_DESCRIPTION ( " Stateless NAT actions " ) ;
MODULE_LICENSE ( " GPL " ) ;
static int __init nat_init_module ( void )
{
2013-12-20 12:08:51 +04:00
int err = tcf_hashinfo_init ( & nat_hash_info , NAT_TAB_MASK ) ;
2013-12-16 08:15:08 +04:00
if ( err )
return err ;
2007-09-27 23:48:05 +04:00
return tcf_register_action ( & act_nat_ops ) ;
}
static void __exit nat_cleanup_module ( void )
{
tcf_unregister_action ( & act_nat_ops ) ;
2013-12-16 08:15:08 +04:00
tcf_hashinfo_destroy ( & nat_hash_info ) ;
2007-09-27 23:48:05 +04:00
}
module_init ( nat_init_module ) ;
module_exit ( nat_cleanup_module ) ;