3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * DECnet Routing Forwarding Information Base (Rules)
9 * Author: Steve Whitehouse <SteveW@ACM.org>
10 * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
14 * Steve Whitehouse <steve@chygwyn.com>
15 * Updated for Thomas Graf's generic rules
18 #include <linux/net.h>
19 #include <linux/init.h>
20 #include <linux/netlink.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/netdevice.h>
23 #include <linux/spinlock.h>
24 #include <linux/list.h>
25 #include <linux/rcupdate.h>
26 #include <linux/export.h>
27 #include <net/neighbour.h>
30 #include <net/fib_rules.h>
32 #include <net/dn_fib.h>
33 #include <net/dn_neigh.h>
34 #include <net/dn_dev.h>
35 #include <net/dn_route.h>
37 static struct fib_rules_ops
*dn_fib_rules_ops
;
41 struct fib_rule common
;
42 unsigned char dst_len
;
43 unsigned char src_len
;
53 int dn_fib_lookup(struct flowidn
*flp
, struct dn_fib_res
*res
)
55 struct fib_lookup_arg arg
= {
60 err
= fib_rules_lookup(dn_fib_rules_ops
,
61 flowidn_to_flowi(flp
), 0, &arg
);
67 static int dn_fib_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
68 int flags
, struct fib_lookup_arg
*arg
)
70 struct flowidn
*fld
= &flp
->u
.dn
;
72 struct dn_fib_table
*tbl
;
74 switch(rule
->action
) {
78 case FR_ACT_UNREACHABLE
:
86 case FR_ACT_BLACKHOLE
:
92 tbl
= dn_fib_get_table(rule
->table
, 0);
96 err
= tbl
->lookup(tbl
, fld
, (struct dn_fib_res
*)arg
->result
);
103 static const struct nla_policy dn_fib_rule_policy
[FRA_MAX
+1] = {
107 static int dn_fib_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
109 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
110 struct flowidn
*fld
= &fl
->u
.dn
;
111 __le16 daddr
= fld
->daddr
;
112 __le16 saddr
= fld
->saddr
;
114 if (((saddr
^ r
->src
) & r
->srcmask
) ||
115 ((daddr
^ r
->dst
) & r
->dstmask
))
121 static int dn_fib_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
122 struct fib_rule_hdr
*frh
,
126 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
131 if (rule
->table
== RT_TABLE_UNSPEC
) {
132 if (rule
->action
== FR_ACT_TO_TBL
) {
133 struct dn_fib_table
*table
;
135 table
= dn_fib_empty_table();
141 rule
->table
= table
->n
;
146 r
->src
= nla_get_le16(tb
[FRA_SRC
]);
149 r
->dst
= nla_get_le16(tb
[FRA_DST
]);
151 r
->src_len
= frh
->src_len
;
152 r
->srcmask
= dnet_make_mask(r
->src_len
);
153 r
->dst_len
= frh
->dst_len
;
154 r
->dstmask
= dnet_make_mask(r
->dst_len
);
160 static int dn_fib_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
163 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
165 if (frh
->src_len
&& (r
->src_len
!= frh
->src_len
))
168 if (frh
->dst_len
&& (r
->dst_len
!= frh
->dst_len
))
171 if (frh
->src_len
&& (r
->src
!= nla_get_le16(tb
[FRA_SRC
])))
174 if (frh
->dst_len
&& (r
->dst
!= nla_get_le16(tb
[FRA_DST
])))
180 unsigned int dnet_addr_type(__le16 addr
)
182 struct flowidn fld
= { .daddr
= addr
};
183 struct dn_fib_res res
;
184 unsigned int ret
= RTN_UNICAST
;
185 struct dn_fib_table
*tb
= dn_fib_get_table(RT_TABLE_LOCAL
, 0);
190 if (!tb
->lookup(tb
, &fld
, &res
)) {
192 dn_fib_res_put(&res
);
198 static int dn_fib_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
199 struct fib_rule_hdr
*frh
)
201 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
203 frh
->dst_len
= r
->dst_len
;
204 frh
->src_len
= r
->src_len
;
208 nla_put_le16(skb
, FRA_DST
, r
->dst
)) ||
210 nla_put_le16(skb
, FRA_SRC
, r
->src
)))
211 goto nla_put_failure
;
218 static void dn_fib_rule_flush_cache(struct fib_rules_ops
*ops
)
220 dn_rt_cache_flush(-1);
223 static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template
= {
225 .rule_size
= sizeof(struct dn_fib_rule
),
226 .addr_size
= sizeof(u16
),
227 .action
= dn_fib_rule_action
,
228 .match
= dn_fib_rule_match
,
229 .configure
= dn_fib_rule_configure
,
230 .compare
= dn_fib_rule_compare
,
231 .fill
= dn_fib_rule_fill
,
232 .default_pref
= fib_default_rule_pref
,
233 .flush_cache
= dn_fib_rule_flush_cache
,
234 .nlgroup
= RTNLGRP_DECnet_RULE
,
235 .policy
= dn_fib_rule_policy
,
236 .owner
= THIS_MODULE
,
237 .fro_net
= &init_net
,
240 void __init
dn_fib_rules_init(void)
243 fib_rules_register(&dn_fib_rules_ops_template
, &init_net
);
244 BUG_ON(IS_ERR(dn_fib_rules_ops
));
245 BUG_ON(fib_default_rule_add(dn_fib_rules_ops
, 0x7fff,
249 void __exit
dn_fib_rules_cleanup(void)
251 fib_rules_unregister(dn_fib_rules_ops
);