Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | |
3 | /* | |
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. | |
7 | * | |
8 | * DECnet Routing Forwarding Information Base (Rules) | |
9 | * | |
10 | * Author: Steve Whitehouse <SteveW@ACM.org> | |
11 | * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c | |
12 | * | |
13 | * | |
14 | * Changes: | |
a8731cbf SW |
15 | * Steve Whitehouse <steve@chygwyn.com> |
16 | * Updated for Thomas Graf's generic rules | |
1da177e4 LT |
17 | * |
18 | */ | |
1da177e4 | 19 | #include <linux/net.h> |
1da177e4 | 20 | #include <linux/init.h> |
1da177e4 LT |
21 | #include <linux/netlink.h> |
22 | #include <linux/rtnetlink.h> | |
1da177e4 | 23 | #include <linux/netdevice.h> |
1da177e4 | 24 | #include <linux/spinlock.h> |
ecba320f SW |
25 | #include <linux/list.h> |
26 | #include <linux/rcupdate.h> | |
bc3b2d7f | 27 | #include <linux/export.h> |
1da177e4 LT |
28 | #include <net/neighbour.h> |
29 | #include <net/dst.h> | |
30 | #include <net/flow.h> | |
a8731cbf | 31 | #include <net/fib_rules.h> |
1da177e4 LT |
32 | #include <net/dn.h> |
33 | #include <net/dn_fib.h> | |
34 | #include <net/dn_neigh.h> | |
35 | #include <net/dn_dev.h> | |
73417f61 | 36 | #include <net/dn_route.h> |
1da177e4 | 37 | |
e9c5158a | 38 | static struct fib_rules_ops *dn_fib_rules_ops; |
a8731cbf | 39 | |
1da177e4 LT |
40 | struct dn_fib_rule |
41 | { | |
a8731cbf SW |
42 | struct fib_rule common; |
43 | unsigned char dst_len; | |
44 | unsigned char src_len; | |
45 | __le16 src; | |
46 | __le16 srcmask; | |
47 | __le16 dst; | |
48 | __le16 dstmask; | |
49 | __le16 srcmap; | |
50 | u8 flags; | |
1da177e4 LT |
51 | }; |
52 | ||
1da177e4 | 53 | |
bef55aeb | 54 | int dn_fib_lookup(struct flowidn *flp, struct dn_fib_res *res) |
1da177e4 | 55 | { |
a8731cbf SW |
56 | struct fib_lookup_arg arg = { |
57 | .result = res, | |
58 | }; | |
59 | int err; | |
60 | ||
bef55aeb DM |
61 | err = fib_rules_lookup(dn_fib_rules_ops, |
62 | flowidn_to_flowi(flp), 0, &arg); | |
a8731cbf | 63 | res->r = arg.rule; |
1da177e4 LT |
64 | |
65 | return err; | |
66 | } | |
67 | ||
2aa7f36c AB |
68 | static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp, |
69 | int flags, struct fib_lookup_arg *arg) | |
ecba320f | 70 | { |
bef55aeb | 71 | struct flowidn *fld = &flp->u.dn; |
a8731cbf SW |
72 | int err = -EAGAIN; |
73 | struct dn_fib_table *tbl; | |
ecba320f | 74 | |
a8731cbf SW |
75 | switch(rule->action) { |
76 | case FR_ACT_TO_TBL: | |
77 | break; | |
78 | ||
79 | case FR_ACT_UNREACHABLE: | |
80 | err = -ENETUNREACH; | |
81 | goto errout; | |
82 | ||
83 | case FR_ACT_PROHIBIT: | |
84 | err = -EACCES; | |
85 | goto errout; | |
86 | ||
87 | case FR_ACT_BLACKHOLE: | |
88 | default: | |
89 | err = -EINVAL; | |
90 | goto errout; | |
1da177e4 | 91 | } |
a8731cbf SW |
92 | |
93 | tbl = dn_fib_get_table(rule->table, 0); | |
94 | if (tbl == NULL) | |
95 | goto errout; | |
96 | ||
bef55aeb | 97 | err = tbl->lookup(tbl, fld, (struct dn_fib_res *)arg->result); |
a8731cbf SW |
98 | if (err > 0) |
99 | err = -EAGAIN; | |
100 | errout: | |
101 | return err; | |
1da177e4 LT |
102 | } |
103 | ||
ef7c79ed | 104 | static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = { |
1f6c9557 | 105 | FRA_GENERIC_POLICY, |
a8731cbf | 106 | }; |
1da177e4 | 107 | |
a8731cbf | 108 | static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
1da177e4 | 109 | { |
a8731cbf | 110 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
bef55aeb DM |
111 | struct flowidn *fld = &fl->u.dn; |
112 | __le16 daddr = fld->daddr; | |
113 | __le16 saddr = fld->saddr; | |
a8731cbf SW |
114 | |
115 | if (((saddr ^ r->src) & r->srcmask) || | |
116 | ((daddr ^ r->dst) & r->dstmask)) | |
117 | return 0; | |
1da177e4 | 118 | |
a8731cbf SW |
119 | return 1; |
120 | } | |
121 | ||
122 | static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb, | |
8b3521ee | 123 | struct fib_rule_hdr *frh, |
a8731cbf SW |
124 | struct nlattr **tb) |
125 | { | |
126 | int err = -EINVAL; | |
127 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; | |
128 | ||
e1701c68 | 129 | if (frh->tos) |
a8731cbf SW |
130 | goto errout; |
131 | ||
132 | if (rule->table == RT_TABLE_UNSPEC) { | |
133 | if (rule->action == FR_ACT_TO_TBL) { | |
134 | struct dn_fib_table *table; | |
135 | ||
136 | table = dn_fib_empty_table(); | |
137 | if (table == NULL) { | |
138 | err = -ENOBUFS; | |
139 | goto errout; | |
140 | } | |
141 | ||
142 | rule->table = table->n; | |
1da177e4 LT |
143 | } |
144 | } | |
145 | ||
e1701c68 | 146 | if (frh->src_len) |
2835fdfa | 147 | r->src = nla_get_le16(tb[FRA_SRC]); |
ecba320f | 148 | |
e1701c68 | 149 | if (frh->dst_len) |
2835fdfa | 150 | r->dst = nla_get_le16(tb[FRA_DST]); |
1da177e4 | 151 | |
a8731cbf SW |
152 | r->src_len = frh->src_len; |
153 | r->srcmask = dnet_make_mask(r->src_len); | |
154 | r->dst_len = frh->dst_len; | |
155 | r->dstmask = dnet_make_mask(r->dst_len); | |
156 | err = 0; | |
157 | errout: | |
158 | return err; | |
159 | } | |
1da177e4 | 160 | |
a8731cbf SW |
161 | static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
162 | struct nlattr **tb) | |
1da177e4 | 163 | { |
a8731cbf SW |
164 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
165 | ||
166 | if (frh->src_len && (r->src_len != frh->src_len)) | |
167 | return 0; | |
1da177e4 | 168 | |
a8731cbf SW |
169 | if (frh->dst_len && (r->dst_len != frh->dst_len)) |
170 | return 0; | |
ecba320f | 171 | |
e1701c68 | 172 | if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC]))) |
a8731cbf SW |
173 | return 0; |
174 | ||
e1701c68 | 175 | if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST]))) |
a8731cbf | 176 | return 0; |
1da177e4 | 177 | |
a8731cbf | 178 | return 1; |
1da177e4 LT |
179 | } |
180 | ||
95c96174 | 181 | unsigned int dnet_addr_type(__le16 addr) |
1da177e4 | 182 | { |
bef55aeb | 183 | struct flowidn fld = { .daddr = addr }; |
1da177e4 | 184 | struct dn_fib_res res; |
95c96174 | 185 | unsigned int ret = RTN_UNICAST; |
abcab268 | 186 | struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0); |
1da177e4 LT |
187 | |
188 | res.r = NULL; | |
189 | ||
190 | if (tb) { | |
bef55aeb | 191 | if (!tb->lookup(tb, &fld, &res)) { |
1da177e4 LT |
192 | ret = res.type; |
193 | dn_fib_res_put(&res); | |
194 | } | |
195 | } | |
196 | return ret; | |
197 | } | |
198 | ||
a8731cbf | 199 | static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
04af8cf6 | 200 | struct fib_rule_hdr *frh) |
1da177e4 | 201 | { |
a8731cbf | 202 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
1da177e4 | 203 | |
a8731cbf SW |
204 | frh->dst_len = r->dst_len; |
205 | frh->src_len = r->src_len; | |
206 | frh->tos = 0; | |
1da177e4 | 207 | |
b21dddb9 DM |
208 | if ((r->dst_len && |
209 | nla_put_le16(skb, FRA_DST, r->dst)) || | |
210 | (r->src_len && | |
211 | nla_put_le16(skb, FRA_SRC, r->src))) | |
212 | goto nla_put_failure; | |
a8731cbf | 213 | return 0; |
1da177e4 | 214 | |
a8731cbf SW |
215 | nla_put_failure: |
216 | return -ENOBUFS; | |
1da177e4 LT |
217 | } |
218 | ||
ae299fc0 | 219 | static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops) |
73417f61 | 220 | { |
4b19ca44 | 221 | dn_rt_cache_flush(-1); |
73417f61 TG |
222 | } |
223 | ||
04a6f82c | 224 | static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template = { |
25239cee | 225 | .family = AF_DECnet, |
a8731cbf | 226 | .rule_size = sizeof(struct dn_fib_rule), |
e1701c68 | 227 | .addr_size = sizeof(u16), |
a8731cbf SW |
228 | .action = dn_fib_rule_action, |
229 | .match = dn_fib_rule_match, | |
230 | .configure = dn_fib_rule_configure, | |
231 | .compare = dn_fib_rule_compare, | |
232 | .fill = dn_fib_rule_fill, | |
73417f61 | 233 | .flush_cache = dn_fib_rule_flush_cache, |
a8731cbf SW |
234 | .nlgroup = RTNLGRP_DECnet_RULE, |
235 | .policy = dn_fib_rule_policy, | |
a8731cbf | 236 | .owner = THIS_MODULE, |
03592383 | 237 | .fro_net = &init_net, |
a8731cbf SW |
238 | }; |
239 | ||
1da177e4 LT |
240 | void __init dn_fib_rules_init(void) |
241 | { | |
e9c5158a EB |
242 | dn_fib_rules_ops = |
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, | |
2994c638 | 246 | RT_TABLE_MAIN, 0)); |
1da177e4 LT |
247 | } |
248 | ||
249 | void __exit dn_fib_rules_cleanup(void) | |
250 | { | |
419df12f | 251 | rtnl_lock(); |
e9c5158a | 252 | fib_rules_unregister(dn_fib_rules_ops); |
419df12f | 253 | rtnl_unlock(); |
e9c5158a | 254 | rcu_barrier(); |
1da177e4 LT |
255 | } |
256 | ||
257 |