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