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