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 <net/neighbour.h>
29 #include <net/fib_rules.h>
31 #include <net/dn_fib.h>
32 #include <net/dn_neigh.h>
33 #include <net/dn_dev.h>
34 #include <net/dn_route.h>
36 static struct fib_rules_ops
*dn_fib_rules_ops
;
40 struct fib_rule common
;
41 unsigned char dst_len
;
42 unsigned char src_len
;
52 int dn_fib_lookup(struct flowidn
*flp
, struct dn_fib_res
*res
)
54 struct fib_lookup_arg arg
= {
59 err
= fib_rules_lookup(dn_fib_rules_ops
,
60 flowidn_to_flowi(flp
), 0, &arg
);
66 static int dn_fib_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
67 int flags
, struct fib_lookup_arg
*arg
)
69 struct flowidn
*fld
= &flp
->u
.dn
;
71 struct dn_fib_table
*tbl
;
73 switch(rule
->action
) {
77 case FR_ACT_UNREACHABLE
:
85 case FR_ACT_BLACKHOLE
:
91 tbl
= dn_fib_get_table(rule
->table
, 0);
95 err
= tbl
->lookup(tbl
, fld
, (struct dn_fib_res
*)arg
->result
);
102 static const struct nla_policy dn_fib_rule_policy
[FRA_MAX
+1] = {
106 static int dn_fib_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
108 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
109 struct flowidn
*fld
= &fl
->u
.dn
;
110 __le16 daddr
= fld
->daddr
;
111 __le16 saddr
= fld
->saddr
;
113 if (((saddr
^ r
->src
) & r
->srcmask
) ||
114 ((daddr
^ r
->dst
) & r
->dstmask
))
120 static int dn_fib_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
121 struct fib_rule_hdr
*frh
,
125 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
130 if (rule
->table
== RT_TABLE_UNSPEC
) {
131 if (rule
->action
== FR_ACT_TO_TBL
) {
132 struct dn_fib_table
*table
;
134 table
= dn_fib_empty_table();
140 rule
->table
= table
->n
;
145 r
->src
= nla_get_le16(tb
[FRA_SRC
]);
148 r
->dst
= nla_get_le16(tb
[FRA_DST
]);
150 r
->src_len
= frh
->src_len
;
151 r
->srcmask
= dnet_make_mask(r
->src_len
);
152 r
->dst_len
= frh
->dst_len
;
153 r
->dstmask
= dnet_make_mask(r
->dst_len
);
159 static int dn_fib_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
162 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
164 if (frh
->src_len
&& (r
->src_len
!= frh
->src_len
))
167 if (frh
->dst_len
&& (r
->dst_len
!= frh
->dst_len
))
170 if (frh
->src_len
&& (r
->src
!= nla_get_le16(tb
[FRA_SRC
])))
173 if (frh
->dst_len
&& (r
->dst
!= nla_get_le16(tb
[FRA_DST
])))
179 unsigned dnet_addr_type(__le16 addr
)
181 struct flowidn fld
= { .daddr
= addr
};
182 struct dn_fib_res res
;
183 unsigned ret
= RTN_UNICAST
;
184 struct dn_fib_table
*tb
= dn_fib_get_table(RT_TABLE_LOCAL
, 0);
189 if (!tb
->lookup(tb
, &fld
, &res
)) {
191 dn_fib_res_put(&res
);
197 static int dn_fib_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
198 struct fib_rule_hdr
*frh
)
200 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
202 frh
->dst_len
= r
->dst_len
;
203 frh
->src_len
= r
->src_len
;
207 NLA_PUT_LE16(skb
, FRA_DST
, r
->dst
);
209 NLA_PUT_LE16(skb
, FRA_SRC
, r
->src
);
217 static void dn_fib_rule_flush_cache(struct fib_rules_ops
*ops
)
219 dn_rt_cache_flush(-1);
222 static const struct fib_rules_ops __net_initdata dn_fib_rules_ops_template
= {
224 .rule_size
= sizeof(struct dn_fib_rule
),
225 .addr_size
= sizeof(u16
),
226 .action
= dn_fib_rule_action
,
227 .match
= dn_fib_rule_match
,
228 .configure
= dn_fib_rule_configure
,
229 .compare
= dn_fib_rule_compare
,
230 .fill
= dn_fib_rule_fill
,
231 .default_pref
= fib_default_rule_pref
,
232 .flush_cache
= dn_fib_rule_flush_cache
,
233 .nlgroup
= RTNLGRP_DECnet_RULE
,
234 .policy
= dn_fib_rule_policy
,
235 .owner
= THIS_MODULE
,
236 .fro_net
= &init_net
,
239 void __init
dn_fib_rules_init(void)
242 fib_rules_register(&dn_fib_rules_ops_template
, &init_net
);
243 BUG_ON(IS_ERR(dn_fib_rules_ops
));
244 BUG_ON(fib_default_rule_add(dn_fib_rules_ops
, 0x7fff,
248 void __exit
dn_fib_rules_cleanup(void)
250 fib_rules_unregister(dn_fib_rules_ops
);