2005-04-17 02:20:36 +04:00
/*
* DECnet An implementation of the DECnet protocol suite for the LINUX
* operating system . DECnet is implemented using the BSD Socket
* interface as the means of communication with the user level .
*
* DECnet Routing Forwarding Information Base ( Rules )
*
* Author : Steve Whitehouse < SteveW @ ACM . org >
* Mostly copied from Alexey Kuznetsov ' s ipv4 / fib_rules . c
*
*
* Changes :
2006-08-10 02:56:46 +04:00
* Steve Whitehouse < steve @ chygwyn . com >
* Updated for Thomas Graf ' s generic rules
2005-04-17 02:20:36 +04:00
*
*/
# include <linux/net.h>
# include <linux/init.h>
# include <linux/netlink.h>
# include <linux/rtnetlink.h>
# include <linux/netdevice.h>
# include <linux/spinlock.h>
2006-03-21 09:43:28 +03:00
# include <linux/list.h>
# include <linux/rcupdate.h>
2011-07-15 19:47:34 +04:00
# include <linux/export.h>
2005-04-17 02:20:36 +04:00
# include <net/neighbour.h>
# include <net/dst.h>
# include <net/flow.h>
2006-08-10 02:56:46 +04:00
# include <net/fib_rules.h>
2005-04-17 02:20:36 +04:00
# include <net/dn.h>
# include <net/dn_fib.h>
# include <net/dn_neigh.h>
# include <net/dn_dev.h>
2007-03-28 00:56:52 +04:00
# include <net/dn_route.h>
2005-04-17 02:20:36 +04:00
2009-12-03 23:22:55 +03:00
static struct fib_rules_ops * dn_fib_rules_ops ;
2006-08-10 02:56:46 +04:00
2005-04-17 02:20:36 +04:00
struct dn_fib_rule
{
2006-08-10 02:56:46 +04:00
struct fib_rule common ;
unsigned char dst_len ;
unsigned char src_len ;
__le16 src ;
__le16 srcmask ;
__le16 dst ;
__le16 dstmask ;
__le16 srcmap ;
u8 flags ;
2005-04-17 02:20:36 +04:00
} ;
2011-03-13 01:17:10 +03:00
int dn_fib_lookup ( struct flowidn * flp , struct dn_fib_res * res )
2005-04-17 02:20:36 +04:00
{
2006-08-10 02:56:46 +04:00
struct fib_lookup_arg arg = {
. result = res ,
} ;
int err ;
2011-03-13 01:17:10 +03:00
err = fib_rules_lookup ( dn_fib_rules_ops ,
flowidn_to_flowi ( flp ) , 0 , & arg ) ;
2006-08-10 02:56:46 +04:00
res - > r = arg . rule ;
2005-04-17 02:20:36 +04:00
return err ;
}
2006-08-15 10:55:20 +04:00
static int dn_fib_rule_action ( struct fib_rule * rule , struct flowi * flp ,
int flags , struct fib_lookup_arg * arg )
2006-03-21 09:43:28 +03:00
{
2011-03-13 01:17:10 +03:00
struct flowidn * fld = & flp - > u . dn ;
2006-08-10 02:56:46 +04:00
int err = - EAGAIN ;
struct dn_fib_table * tbl ;
2006-03-21 09:43:28 +03:00
2006-08-10 02:56:46 +04:00
switch ( rule - > action ) {
case FR_ACT_TO_TBL :
break ;
case FR_ACT_UNREACHABLE :
err = - ENETUNREACH ;
goto errout ;
case FR_ACT_PROHIBIT :
err = - EACCES ;
goto errout ;
case FR_ACT_BLACKHOLE :
default :
err = - EINVAL ;
goto errout ;
2005-04-17 02:20:36 +04:00
}
2006-08-10 02:56:46 +04:00
tbl = dn_fib_get_table ( rule - > table , 0 ) ;
if ( tbl = = NULL )
goto errout ;
2011-03-13 01:17:10 +03:00
err = tbl - > lookup ( tbl , fld , ( struct dn_fib_res * ) arg - > result ) ;
2006-08-10 02:56:46 +04:00
if ( err > 0 )
err = - EAGAIN ;
errout :
return err ;
2005-04-17 02:20:36 +04:00
}
2007-06-05 23:38:30 +04:00
static const struct nla_policy dn_fib_rule_policy [ FRA_MAX + 1 ] = {
2006-11-10 02:22:48 +03:00
FRA_GENERIC_POLICY ,
2006-08-10 02:56:46 +04:00
} ;
2005-04-17 02:20:36 +04:00
2006-08-10 02:56:46 +04:00
static int dn_fib_rule_match ( struct fib_rule * rule , struct flowi * fl , int flags )
2005-04-17 02:20:36 +04:00
{
2006-08-10 02:56:46 +04:00
struct dn_fib_rule * r = ( struct dn_fib_rule * ) rule ;
2011-03-13 01:17:10 +03:00
struct flowidn * fld = & fl - > u . dn ;
__le16 daddr = fld - > daddr ;
__le16 saddr = fld - > saddr ;
2006-08-10 02:56:46 +04:00
if ( ( ( saddr ^ r - > src ) & r - > srcmask ) | |
( ( daddr ^ r - > dst ) & r - > dstmask ) )
return 0 ;
2005-04-17 02:20:36 +04:00
2006-08-10 02:56:46 +04:00
return 1 ;
}
static int dn_fib_rule_configure ( struct fib_rule * rule , struct sk_buff * skb ,
2009-05-11 09:52:49 +04:00
struct fib_rule_hdr * frh ,
2006-08-10 02:56:46 +04:00
struct nlattr * * tb )
{
int err = - EINVAL ;
struct dn_fib_rule * r = ( struct dn_fib_rule * ) rule ;
2007-03-24 22:46:02 +03:00
if ( frh - > tos )
2006-08-10 02:56:46 +04:00
goto errout ;
if ( rule - > table = = RT_TABLE_UNSPEC ) {
if ( rule - > action = = FR_ACT_TO_TBL ) {
struct dn_fib_table * table ;
table = dn_fib_empty_table ( ) ;
if ( table = = NULL ) {
err = - ENOBUFS ;
goto errout ;
}
rule - > table = table - > n ;
2005-04-17 02:20:36 +04:00
}
}
2007-03-24 22:46:02 +03:00
if ( frh - > src_len )
2007-02-09 21:13:37 +03:00
r - > src = nla_get_le16 ( tb [ FRA_SRC ] ) ;
2006-03-21 09:43:28 +03:00
2007-03-24 22:46:02 +03:00
if ( frh - > dst_len )
2007-02-09 21:13:37 +03:00
r - > dst = nla_get_le16 ( tb [ FRA_DST ] ) ;
2005-04-17 02:20:36 +04:00
2006-08-10 02:56:46 +04:00
r - > src_len = frh - > src_len ;
r - > srcmask = dnet_make_mask ( r - > src_len ) ;
r - > dst_len = frh - > dst_len ;
r - > dstmask = dnet_make_mask ( r - > dst_len ) ;
err = 0 ;
errout :
return err ;
}
2005-04-17 02:20:36 +04:00
2006-08-10 02:56:46 +04:00
static int dn_fib_rule_compare ( struct fib_rule * rule , struct fib_rule_hdr * frh ,
struct nlattr * * tb )
2005-04-17 02:20:36 +04:00
{
2006-08-10 02:56:46 +04:00
struct dn_fib_rule * r = ( struct dn_fib_rule * ) rule ;
if ( frh - > src_len & & ( r - > src_len ! = frh - > src_len ) )
return 0 ;
2005-04-17 02:20:36 +04:00
2006-08-10 02:56:46 +04:00
if ( frh - > dst_len & & ( r - > dst_len ! = frh - > dst_len ) )
return 0 ;
2006-03-21 09:43:28 +03:00
2007-03-24 22:46:02 +03:00
if ( frh - > src_len & & ( r - > src ! = nla_get_le16 ( tb [ FRA_SRC ] ) ) )
2006-08-10 02:56:46 +04:00
return 0 ;
2007-03-24 22:46:02 +03:00
if ( frh - > dst_len & & ( r - > dst ! = nla_get_le16 ( tb [ FRA_DST ] ) ) )
2006-08-10 02:56:46 +04:00
return 0 ;
2005-04-17 02:20:36 +04:00
2006-08-10 02:56:46 +04:00
return 1 ;
2005-04-17 02:20:36 +04:00
}
2012-04-15 09:58:06 +04:00
unsigned int dnet_addr_type ( __le16 addr )
2005-04-17 02:20:36 +04:00
{
2011-03-13 01:17:10 +03:00
struct flowidn fld = { . daddr = addr } ;
2005-04-17 02:20:36 +04:00
struct dn_fib_res res ;
2012-04-15 09:58:06 +04:00
unsigned int ret = RTN_UNICAST ;
2006-08-11 10:11:47 +04:00
struct dn_fib_table * tb = dn_fib_get_table ( RT_TABLE_LOCAL , 0 ) ;
2005-04-17 02:20:36 +04:00
res . r = NULL ;
if ( tb ) {
2011-03-13 01:17:10 +03:00
if ( ! tb - > lookup ( tb , & fld , & res ) ) {
2005-04-17 02:20:36 +04:00
ret = res . type ;
dn_fib_res_put ( & res ) ;
}
}
return ret ;
}
2006-08-10 02:56:46 +04:00
static int dn_fib_rule_fill ( struct fib_rule * rule , struct sk_buff * skb ,
2009-05-21 04:26:23 +04:00
struct fib_rule_hdr * frh )
2005-04-17 02:20:36 +04:00
{
2006-08-10 02:56:46 +04:00
struct dn_fib_rule * r = ( struct dn_fib_rule * ) rule ;
2005-04-17 02:20:36 +04:00
2006-08-10 02:56:46 +04:00
frh - > dst_len = r - > dst_len ;
frh - > src_len = r - > src_len ;
frh - > tos = 0 ;
2005-04-17 02:20:36 +04:00
2012-04-02 04:15:14 +04:00
if ( ( r - > dst_len & &
nla_put_le16 ( skb , FRA_DST , r - > dst ) ) | |
( r - > src_len & &
nla_put_le16 ( skb , FRA_SRC , r - > src ) ) )
goto nla_put_failure ;
2006-08-10 02:56:46 +04:00
return 0 ;
2005-04-17 02:20:36 +04:00
2006-08-10 02:56:46 +04:00
nla_put_failure :
return - ENOBUFS ;
2005-04-17 02:20:36 +04:00
}
2008-07-06 06:01:28 +04:00
static void dn_fib_rule_flush_cache ( struct fib_rules_ops * ops )
2007-03-28 00:56:52 +04:00
{
2007-03-29 01:18:52 +04:00
dn_rt_cache_flush ( - 1 ) ;
2007-03-28 00:56:52 +04:00
}
2012-10-05 04:12:11 +04:00
static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template = {
2010-04-26 18:02:05 +04:00
. family = AF_DECnet ,
2006-08-10 02:56:46 +04:00
. rule_size = sizeof ( struct dn_fib_rule ) ,
2007-03-24 22:46:02 +03:00
. addr_size = sizeof ( u16 ) ,
2006-08-10 02:56:46 +04:00
. action = dn_fib_rule_action ,
. match = dn_fib_rule_match ,
. configure = dn_fib_rule_configure ,
. compare = dn_fib_rule_compare ,
. fill = dn_fib_rule_fill ,
2010-04-13 09:03:15 +04:00
. default_pref = fib_default_rule_pref ,
2007-03-28 00:56:52 +04:00
. flush_cache = dn_fib_rule_flush_cache ,
2006-08-10 02:56:46 +04:00
. nlgroup = RTNLGRP_DECnet_RULE ,
. policy = dn_fib_rule_policy ,
. owner = THIS_MODULE ,
2008-01-21 03:46:01 +03:00
. fro_net = & init_net ,
2006-08-10 02:56:46 +04:00
} ;
2005-04-17 02:20:36 +04:00
void __init dn_fib_rules_init ( void )
{
2009-12-03 23:22:55 +03:00
dn_fib_rules_ops =
fib_rules_register ( & dn_fib_rules_ops_template , & init_net ) ;
BUG_ON ( IS_ERR ( dn_fib_rules_ops ) ) ;
BUG_ON ( fib_default_rule_add ( dn_fib_rules_ops , 0x7fff ,
2007-11-11 09:12:03 +03:00
RT_TABLE_MAIN , 0 ) ) ;
2005-04-17 02:20:36 +04:00
}
void __exit dn_fib_rules_cleanup ( void )
{
2009-12-03 23:22:55 +03:00
fib_rules_unregister ( dn_fib_rules_ops ) ;
rcu_barrier ( ) ;
2005-04-17 02:20:36 +04:00
}