Blame view
net/decnet/dn_rules.c
5.28 KB
1da177e4c
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/* * DECnet An implementation of the DECnet protocol suite for the LINUX * operating system. DECnet is implemented using the BSD Socket * interface as the means of communication with the user level. * * DECnet Routing Forwarding Information Base (Rules) * * Author: Steve Whitehouse <SteveW@ACM.org> * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c * * * Changes: |
a8731cbf6
|
14 15 |
* Steve Whitehouse <steve@chygwyn.com> * Updated for Thomas Graf's generic rules |
1da177e4c
|
16 17 |
* */ |
1da177e4c
|
18 |
#include <linux/net.h> |
1da177e4c
|
19 |
#include <linux/init.h> |
1da177e4c
|
20 21 |
#include <linux/netlink.h> #include <linux/rtnetlink.h> |
1da177e4c
|
22 |
#include <linux/netdevice.h> |
1da177e4c
|
23 |
#include <linux/spinlock.h> |
ecba320f2
|
24 25 |
#include <linux/list.h> #include <linux/rcupdate.h> |
1da177e4c
|
26 27 28 |
#include <net/neighbour.h> #include <net/dst.h> #include <net/flow.h> |
a8731cbf6
|
29 |
#include <net/fib_rules.h> |
1da177e4c
|
30 31 32 33 |
#include <net/dn.h> #include <net/dn_fib.h> #include <net/dn_neigh.h> #include <net/dn_dev.h> |
73417f617
|
34 |
#include <net/dn_route.h> |
1da177e4c
|
35 |
|
e9c5158ac
|
36 |
static struct fib_rules_ops *dn_fib_rules_ops; |
a8731cbf6
|
37 |
|
1da177e4c
|
38 39 |
struct dn_fib_rule { |
a8731cbf6
|
40 41 42 43 44 45 46 47 48 |
struct fib_rule common; unsigned char dst_len; unsigned char src_len; __le16 src; __le16 srcmask; __le16 dst; __le16 dstmask; __le16 srcmap; u8 flags; |
1da177e4c
|
49 |
}; |
1da177e4c
|
50 |
|
a8731cbf6
|
51 |
int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res) |
1da177e4c
|
52 |
{ |
a8731cbf6
|
53 54 55 56 |
struct fib_lookup_arg arg = { .result = res, }; int err; |
e9c5158ac
|
57 |
err = fib_rules_lookup(dn_fib_rules_ops, flp, 0, &arg); |
a8731cbf6
|
58 |
res->r = arg.rule; |
1da177e4c
|
59 60 61 |
return err; } |
2aa7f36cd
|
62 63 |
static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp, int flags, struct fib_lookup_arg *arg) |
ecba320f2
|
64 |
{ |
a8731cbf6
|
65 66 |
int err = -EAGAIN; struct dn_fib_table *tbl; |
ecba320f2
|
67 |
|
a8731cbf6
|
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
switch(rule->action) { case FR_ACT_TO_TBL: break; case FR_ACT_UNREACHABLE: err = -ENETUNREACH; goto errout; case FR_ACT_PROHIBIT: err = -EACCES; goto errout; case FR_ACT_BLACKHOLE: default: err = -EINVAL; goto errout; |
1da177e4c
|
84 |
} |
a8731cbf6
|
85 86 87 88 89 90 91 92 93 94 |
tbl = dn_fib_get_table(rule->table, 0); if (tbl == NULL) goto errout; err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result); if (err > 0) err = -EAGAIN; errout: return err; |
1da177e4c
|
95 |
} |
ef7c79ed6
|
96 |
static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = { |
1f6c9557e
|
97 |
FRA_GENERIC_POLICY, |
a8731cbf6
|
98 |
}; |
1da177e4c
|
99 |
|
a8731cbf6
|
100 |
static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
1da177e4c
|
101 |
{ |
a8731cbf6
|
102 |
struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
375d9d718
|
103 104 |
__le16 daddr = fl->fld_dst; __le16 saddr = fl->fld_src; |
a8731cbf6
|
105 106 107 108 |
if (((saddr ^ r->src) & r->srcmask) || ((daddr ^ r->dst) & r->dstmask)) return 0; |
1da177e4c
|
109 |
|
a8731cbf6
|
110 111 112 113 |
return 1; } static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
8b3521eeb
|
114 |
struct fib_rule_hdr *frh, |
a8731cbf6
|
115 116 117 118 |
struct nlattr **tb) { int err = -EINVAL; struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
e1701c68c
|
119 |
if (frh->tos) |
a8731cbf6
|
120 121 122 123 124 125 126 127 128 129 130 131 132 |
goto errout; if (rule->table == RT_TABLE_UNSPEC) { if (rule->action == FR_ACT_TO_TBL) { struct dn_fib_table *table; table = dn_fib_empty_table(); if (table == NULL) { err = -ENOBUFS; goto errout; } rule->table = table->n; |
1da177e4c
|
133 134 |
} } |
e1701c68c
|
135 |
if (frh->src_len) |
2835fdfa4
|
136 |
r->src = nla_get_le16(tb[FRA_SRC]); |
ecba320f2
|
137 |
|
e1701c68c
|
138 |
if (frh->dst_len) |
2835fdfa4
|
139 |
r->dst = nla_get_le16(tb[FRA_DST]); |
1da177e4c
|
140 |
|
a8731cbf6
|
141 142 143 144 145 146 147 148 |
r->src_len = frh->src_len; r->srcmask = dnet_make_mask(r->src_len); r->dst_len = frh->dst_len; r->dstmask = dnet_make_mask(r->dst_len); err = 0; errout: return err; } |
1da177e4c
|
149 |
|
a8731cbf6
|
150 151 |
static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, struct nlattr **tb) |
1da177e4c
|
152 |
{ |
a8731cbf6
|
153 154 155 156 |
struct dn_fib_rule *r = (struct dn_fib_rule *)rule; if (frh->src_len && (r->src_len != frh->src_len)) return 0; |
1da177e4c
|
157 |
|
a8731cbf6
|
158 159 |
if (frh->dst_len && (r->dst_len != frh->dst_len)) return 0; |
ecba320f2
|
160 |
|
e1701c68c
|
161 |
if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC]))) |
a8731cbf6
|
162 |
return 0; |
e1701c68c
|
163 |
if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST]))) |
a8731cbf6
|
164 |
return 0; |
1da177e4c
|
165 |
|
a8731cbf6
|
166 |
return 1; |
1da177e4c
|
167 |
} |
c4ea94ab3
|
168 |
unsigned dnet_addr_type(__le16 addr) |
1da177e4c
|
169 170 171 172 |
{ struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } }; struct dn_fib_res res; unsigned ret = RTN_UNICAST; |
abcab2683
|
173 |
struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0); |
1da177e4c
|
174 175 176 177 178 179 180 181 182 183 184 |
res.r = NULL; if (tb) { if (!tb->lookup(tb, &fl, &res)) { ret = res.type; dn_fib_res_put(&res); } } return ret; } |
a8731cbf6
|
185 |
static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
04af8cf6f
|
186 |
struct fib_rule_hdr *frh) |
1da177e4c
|
187 |
{ |
a8731cbf6
|
188 |
struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
1da177e4c
|
189 |
|
a8731cbf6
|
190 191 192 |
frh->dst_len = r->dst_len; frh->src_len = r->src_len; frh->tos = 0; |
1da177e4c
|
193 |
|
a8731cbf6
|
194 |
if (r->dst_len) |
2835fdfa4
|
195 |
NLA_PUT_LE16(skb, FRA_DST, r->dst); |
a8731cbf6
|
196 |
if (r->src_len) |
2835fdfa4
|
197 |
NLA_PUT_LE16(skb, FRA_SRC, r->src); |
1da177e4c
|
198 |
|
a8731cbf6
|
199 |
return 0; |
1da177e4c
|
200 |
|
a8731cbf6
|
201 202 |
nla_put_failure: return -ENOBUFS; |
1da177e4c
|
203 |
} |
ae299fc05
|
204 |
static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops) |
73417f617
|
205 |
{ |
4b19ca44c
|
206 |
dn_rt_cache_flush(-1); |
73417f617
|
207 |
} |
3d0c9c4eb
|
208 |
static const struct fib_rules_ops __net_initdata dn_fib_rules_ops_template = { |
25239cee7
|
209 |
.family = AF_DECnet, |
a8731cbf6
|
210 |
.rule_size = sizeof(struct dn_fib_rule), |
e1701c68c
|
211 |
.addr_size = sizeof(u16), |
a8731cbf6
|
212 213 214 215 216 |
.action = dn_fib_rule_action, .match = dn_fib_rule_match, .configure = dn_fib_rule_configure, .compare = dn_fib_rule_compare, .fill = dn_fib_rule_fill, |
d8a566bea
|
217 |
.default_pref = fib_default_rule_pref, |
73417f617
|
218 |
.flush_cache = dn_fib_rule_flush_cache, |
a8731cbf6
|
219 220 |
.nlgroup = RTNLGRP_DECnet_RULE, .policy = dn_fib_rule_policy, |
a8731cbf6
|
221 |
.owner = THIS_MODULE, |
035923833
|
222 |
.fro_net = &init_net, |
a8731cbf6
|
223 |
}; |
1da177e4c
|
224 225 |
void __init dn_fib_rules_init(void) { |
e9c5158ac
|
226 227 228 229 |
dn_fib_rules_ops = fib_rules_register(&dn_fib_rules_ops_template, &init_net); BUG_ON(IS_ERR(dn_fib_rules_ops)); BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff, |
2994c6386
|
230 |
RT_TABLE_MAIN, 0)); |
1da177e4c
|
231 232 233 234 |
} void __exit dn_fib_rules_cleanup(void) { |
e9c5158ac
|
235 236 |
fib_rules_unregister(dn_fib_rules_ops); rcu_barrier(); |
1da177e4c
|
237 |
} |