1 // SPDX-License-Identifier: GPL-2.0
4 * DECnet An implementation of the DECnet protocol suite for the LINUX
5 * operating system. DECnet is implemented using the BSD Socket
6 * interface as the means of communication with the user level.
8 * DECnet Routing Forwarding Information Base (Rules)
10 * Author: Steve Whitehouse <SteveW@ACM.org>
11 * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
15 * Steve Whitehouse <steve@chygwyn.com>
16 * Updated for Thomas Graf's generic rules
19 #include <linux/net.h>
20 #include <linux/init.h>
21 #include <linux/netlink.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/netdevice.h>
24 #include <linux/spinlock.h>
25 #include <linux/list.h>
26 #include <linux/rcupdate.h>
27 #include <linux/export.h>
28 #include <net/neighbour.h>
31 #include <net/fib_rules.h>
33 #include <net/dn_fib.h>
34 #include <net/dn_neigh.h>
35 #include <net/dn_dev.h>
36 #include <net/dn_route.h>
38 static struct fib_rules_ops
*dn_fib_rules_ops
;
42 struct fib_rule common
;
43 unsigned char dst_len
;
44 unsigned char src_len
;
54 int dn_fib_lookup(struct flowidn
*flp
, struct dn_fib_res
*res
)
56 struct fib_lookup_arg arg
= {
61 err
= fib_rules_lookup(dn_fib_rules_ops
,
62 flowidn_to_flowi(flp
), 0, &arg
);
68 static int dn_fib_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
69 int flags
, struct fib_lookup_arg
*arg
)
71 struct flowidn
*fld
= &flp
->u
.dn
;
73 struct dn_fib_table
*tbl
;
75 switch(rule
->action
) {
79 case FR_ACT_UNREACHABLE
:
87 case FR_ACT_BLACKHOLE
:
93 tbl
= dn_fib_get_table(rule
->table
, 0);
97 err
= tbl
->lookup(tbl
, fld
, (struct dn_fib_res
*)arg
->result
);
104 static const struct nla_policy dn_fib_rule_policy
[FRA_MAX
+1] = {
108 static int dn_fib_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
110 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
111 struct flowidn
*fld
= &fl
->u
.dn
;
112 __le16 daddr
= fld
->daddr
;
113 __le16 saddr
= fld
->saddr
;
115 if (((saddr
^ r
->src
) & r
->srcmask
) ||
116 ((daddr
^ r
->dst
) & r
->dstmask
))
122 static int dn_fib_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
123 struct fib_rule_hdr
*frh
,
125 struct netlink_ext_ack
*extack
)
128 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
131 NL_SET_ERR_MSG(extack
, "Invalid tos value");
135 if (rule
->table
== RT_TABLE_UNSPEC
) {
136 if (rule
->action
== FR_ACT_TO_TBL
) {
137 struct dn_fib_table
*table
;
139 table
= dn_fib_empty_table();
145 rule
->table
= table
->n
;
150 r
->src
= nla_get_le16(tb
[FRA_SRC
]);
153 r
->dst
= nla_get_le16(tb
[FRA_DST
]);
155 r
->src_len
= frh
->src_len
;
156 r
->srcmask
= dnet_make_mask(r
->src_len
);
157 r
->dst_len
= frh
->dst_len
;
158 r
->dstmask
= dnet_make_mask(r
->dst_len
);
164 static int dn_fib_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
167 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
169 if (frh
->src_len
&& (r
->src_len
!= frh
->src_len
))
172 if (frh
->dst_len
&& (r
->dst_len
!= frh
->dst_len
))
175 if (frh
->src_len
&& (r
->src
!= nla_get_le16(tb
[FRA_SRC
])))
178 if (frh
->dst_len
&& (r
->dst
!= nla_get_le16(tb
[FRA_DST
])))
184 unsigned int dnet_addr_type(__le16 addr
)
186 struct flowidn fld
= { .daddr
= addr
};
187 struct dn_fib_res res
;
188 unsigned int ret
= RTN_UNICAST
;
189 struct dn_fib_table
*tb
= dn_fib_get_table(RT_TABLE_LOCAL
, 0);
194 if (!tb
->lookup(tb
, &fld
, &res
)) {
196 dn_fib_res_put(&res
);
202 static int dn_fib_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
203 struct fib_rule_hdr
*frh
)
205 struct dn_fib_rule
*r
= (struct dn_fib_rule
*)rule
;
207 frh
->dst_len
= r
->dst_len
;
208 frh
->src_len
= r
->src_len
;
212 nla_put_le16(skb
, FRA_DST
, r
->dst
)) ||
214 nla_put_le16(skb
, FRA_SRC
, r
->src
)))
215 goto nla_put_failure
;
222 static void dn_fib_rule_flush_cache(struct fib_rules_ops
*ops
)
224 dn_rt_cache_flush(-1);
227 static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template
= {
229 .rule_size
= sizeof(struct dn_fib_rule
),
230 .addr_size
= sizeof(u16
),
231 .action
= dn_fib_rule_action
,
232 .match
= dn_fib_rule_match
,
233 .configure
= dn_fib_rule_configure
,
234 .compare
= dn_fib_rule_compare
,
235 .fill
= dn_fib_rule_fill
,
236 .flush_cache
= dn_fib_rule_flush_cache
,
237 .nlgroup
= RTNLGRP_DECnet_RULE
,
238 .policy
= dn_fib_rule_policy
,
239 .owner
= THIS_MODULE
,
240 .fro_net
= &init_net
,
243 void __init
dn_fib_rules_init(void)
246 fib_rules_register(&dn_fib_rules_ops_template
, &init_net
);
247 BUG_ON(IS_ERR(dn_fib_rules_ops
));
248 BUG_ON(fib_default_rule_add(dn_fib_rules_ops
, 0x7fff,
252 void __exit
dn_fib_rules_cleanup(void)
255 fib_rules_unregister(dn_fib_rules_ops
);