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
;
51 static struct dn_fib_rule default_rule
= {
53 .refcnt
= ATOMIC_INIT(2),
55 .table
= RT_TABLE_MAIN
,
56 .action
= FR_ACT_TO_TBL
,
61 int dn_fib_lookup(struct flowi
*flp
, struct dn_fib_res
*res
)
63 struct fib_lookup_arg arg
= {
68 err
= fib_rules_lookup(&dn_fib_rules_ops
, flp
, 0, &arg
);
74 static int dn_fib_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
75 int flags
, struct fib_lookup_arg
*arg
)
78 struct dn_fib_table
*tbl
;
80 switch(rule
->action
) {
84 case FR_ACT_UNREACHABLE
:
92 case FR_ACT_BLACKHOLE
:
98 tbl
= dn_fib_get_table(rule
->table
, 0);
102 err
= tbl
->lookup(tbl
, flp
, (struct dn_fib_res
*)arg
->result
);
109 static const struct nla_policy dn_fib_rule_policy
[FRA_MAX
+1] = {
113 static int dn_fib_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
115 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
116 __le16 daddr
= fl
->fld_dst
;
117 __le16 saddr
= fl
->fld_src
;
119 if (((saddr
^ r
->src
) & r
->srcmask
) ||
120 ((daddr
^ r
->dst
) & r
->dstmask
))
126 static int dn_fib_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
127 struct nlmsghdr
*nlh
, struct fib_rule_hdr
*frh
,
131 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
136 if (rule
->table
== RT_TABLE_UNSPEC
) {
137 if (rule
->action
== FR_ACT_TO_TBL
) {
138 struct dn_fib_table
*table
;
140 table
= dn_fib_empty_table();
146 rule
->table
= table
->n
;
151 r
->src
= nla_get_le16(tb
[FRA_SRC
]);
154 r
->dst
= nla_get_le16(tb
[FRA_DST
]);
156 r
->src_len
= frh
->src_len
;
157 r
->srcmask
= dnet_make_mask(r
->src_len
);
158 r
->dst_len
= frh
->dst_len
;
159 r
->dstmask
= dnet_make_mask(r
->dst_len
);
165 static int dn_fib_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
168 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
170 if (frh
->src_len
&& (r
->src_len
!= frh
->src_len
))
173 if (frh
->dst_len
&& (r
->dst_len
!= frh
->dst_len
))
176 if (frh
->src_len
&& (r
->src
!= nla_get_le16(tb
[FRA_SRC
])))
179 if (frh
->dst_len
&& (r
->dst
!= nla_get_le16(tb
[FRA_DST
])))
185 unsigned dnet_addr_type(__le16 addr
)
187 struct flowi fl
= { .nl_u
= { .dn_u
= { .daddr
= addr
} } };
188 struct dn_fib_res res
;
189 unsigned ret
= RTN_UNICAST
;
190 struct dn_fib_table
*tb
= dn_fib_get_table(RT_TABLE_LOCAL
, 0);
195 if (!tb
->lookup(tb
, &fl
, &res
)) {
197 dn_fib_res_put(&res
);
203 static int dn_fib_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
204 struct nlmsghdr
*nlh
, struct fib_rule_hdr
*frh
)
206 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
208 frh
->family
= AF_DECnet
;
209 frh
->dst_len
= r
->dst_len
;
210 frh
->src_len
= r
->src_len
;
214 NLA_PUT_LE16(skb
, FRA_DST
, r
->dst
);
216 NLA_PUT_LE16(skb
, FRA_SRC
, r
->src
);
224 static u32
dn_fib_rule_default_pref(void)
226 struct list_head
*pos
;
227 struct fib_rule
*rule
;
229 if (!list_empty(&dn_fib_rules_ops
.rules_list
)) {
230 pos
= dn_fib_rules_ops
.rules_list
.next
;
231 if (pos
->next
!= &dn_fib_rules_ops
.rules_list
) {
232 rule
= list_entry(pos
->next
, struct fib_rule
, list
);
234 return rule
->pref
- 1;
241 static void dn_fib_rule_flush_cache(void)
243 dn_rt_cache_flush(-1);
246 static struct fib_rules_ops dn_fib_rules_ops
= {
248 .rule_size
= sizeof(struct dn_fib_rule
),
249 .addr_size
= sizeof(u16
),
250 .action
= dn_fib_rule_action
,
251 .match
= dn_fib_rule_match
,
252 .configure
= dn_fib_rule_configure
,
253 .compare
= dn_fib_rule_compare
,
254 .fill
= dn_fib_rule_fill
,
255 .default_pref
= dn_fib_rule_default_pref
,
256 .flush_cache
= dn_fib_rule_flush_cache
,
257 .nlgroup
= RTNLGRP_DECnet_RULE
,
258 .policy
= dn_fib_rule_policy
,
259 .rules_list
= LIST_HEAD_INIT(dn_fib_rules_ops
.rules_list
),
260 .owner
= THIS_MODULE
,
263 void __init
dn_fib_rules_init(void)
265 list_add_tail(&default_rule
.common
.list
,
266 &dn_fib_rules_ops
.rules_list
);
267 fib_rules_register(&dn_fib_rules_ops
);
270 void __exit
dn_fib_rules_cleanup(void)
272 fib_rules_unregister(&dn_fib_rules_ops
);