]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/decnet/dn_rules.c
[NET] rules: Protocol independant mark selector
[net-next-2.6.git] / net / decnet / dn_rules.c
CommitLineData
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>
34
a8731cbf
SW
35static struct fib_rules_ops dn_fib_rules_ops;
36
1da177e4
LT
37struct dn_fib_rule
38{
a8731cbf
SW
39 struct fib_rule common;
40 unsigned char dst_len;
41 unsigned char src_len;
42 __le16 src;
43 __le16 srcmask;
44 __le16 dst;
45 __le16 dstmask;
46 __le16 srcmap;
47 u8 flags;
1da177e4
LT
48};
49
50static struct dn_fib_rule default_rule = {
a8731cbf
SW
51 .common = {
52 .refcnt = ATOMIC_INIT(2),
53 .pref = 0x7fff,
54 .table = RT_TABLE_MAIN,
55 .action = FR_ACT_TO_TBL,
56 },
1da177e4
LT
57};
58
a8731cbf
SW
59static LIST_HEAD(dn_fib_rules);
60
1da177e4 61
a8731cbf 62int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
1da177e4 63{
a8731cbf
SW
64 struct fib_lookup_arg arg = {
65 .result = res,
66 };
67 int err;
68
69 err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
70 res->r = arg.rule;
1da177e4
LT
71
72 return err;
73}
74
2aa7f36c
AB
75static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
76 int flags, struct fib_lookup_arg *arg)
ecba320f 77{
a8731cbf
SW
78 int err = -EAGAIN;
79 struct dn_fib_table *tbl;
ecba320f 80
a8731cbf
SW
81 switch(rule->action) {
82 case FR_ACT_TO_TBL:
83 break;
84
85 case FR_ACT_UNREACHABLE:
86 err = -ENETUNREACH;
87 goto errout;
88
89 case FR_ACT_PROHIBIT:
90 err = -EACCES;
91 goto errout;
92
93 case FR_ACT_BLACKHOLE:
94 default:
95 err = -EINVAL;
96 goto errout;
1da177e4 97 }
a8731cbf
SW
98
99 tbl = dn_fib_get_table(rule->table, 0);
100 if (tbl == NULL)
101 goto errout;
102
103 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
104 if (err > 0)
105 err = -EAGAIN;
106errout:
107 return err;
1da177e4
LT
108}
109
a8731cbf 110static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
5176f91e 111 [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
a8731cbf
SW
112 [FRA_PRIORITY] = { .type = NLA_U32 },
113 [FRA_SRC] = { .type = NLA_U16 },
114 [FRA_DST] = { .type = NLA_U16 },
115 [FRA_FWMARK] = { .type = NLA_U32 },
88e91f29 116 [FRA_FWMASK] = { .type = NLA_U32 },
d880309a 117 [FRA_TABLE] = { .type = NLA_U32 },
a8731cbf 118};
1da177e4 119
a8731cbf 120static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
1da177e4 121{
a8731cbf 122 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
375d9d71
SW
123 __le16 daddr = fl->fld_dst;
124 __le16 saddr = fl->fld_src;
a8731cbf
SW
125
126 if (((saddr ^ r->src) & r->srcmask) ||
127 ((daddr ^ r->dst) & r->dstmask))
128 return 0;
1da177e4 129
a8731cbf
SW
130 return 1;
131}
132
133static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
134 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
135 struct nlattr **tb)
136{
137 int err = -EINVAL;
138 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
139
140 if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
141 goto errout;
142
143 if (rule->table == RT_TABLE_UNSPEC) {
144 if (rule->action == FR_ACT_TO_TBL) {
145 struct dn_fib_table *table;
146
147 table = dn_fib_empty_table();
148 if (table == NULL) {
149 err = -ENOBUFS;
150 goto errout;
151 }
152
153 rule->table = table->n;
1da177e4
LT
154 }
155 }
156
a8731cbf
SW
157 if (tb[FRA_SRC])
158 r->src = nla_get_u16(tb[FRA_SRC]);
ecba320f 159
a8731cbf
SW
160 if (tb[FRA_DST])
161 r->dst = nla_get_u16(tb[FRA_DST]);
1da177e4 162
a8731cbf
SW
163 r->src_len = frh->src_len;
164 r->srcmask = dnet_make_mask(r->src_len);
165 r->dst_len = frh->dst_len;
166 r->dstmask = dnet_make_mask(r->dst_len);
167 err = 0;
168errout:
169 return err;
170}
1da177e4 171
a8731cbf
SW
172static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
173 struct nlattr **tb)
1da177e4 174{
a8731cbf
SW
175 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
176
177 if (frh->src_len && (r->src_len != frh->src_len))
178 return 0;
1da177e4 179
a8731cbf
SW
180 if (frh->dst_len && (r->dst_len != frh->dst_len))
181 return 0;
ecba320f 182
d1aa62f1 183 if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC])))
a8731cbf
SW
184 return 0;
185
d1aa62f1 186 if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST])))
a8731cbf 187 return 0;
1da177e4 188
a8731cbf 189 return 1;
1da177e4
LT
190}
191
c4ea94ab 192unsigned dnet_addr_type(__le16 addr)
1da177e4
LT
193{
194 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
195 struct dn_fib_res res;
196 unsigned ret = RTN_UNICAST;
abcab268 197 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
1da177e4
LT
198
199 res.r = NULL;
200
201 if (tb) {
202 if (!tb->lookup(tb, &fl, &res)) {
203 ret = res.type;
204 dn_fib_res_put(&res);
205 }
206 }
207 return ret;
208}
209
a8731cbf
SW
210static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
211 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
1da177e4 212{
a8731cbf 213 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
1da177e4 214
a8731cbf
SW
215 frh->family = AF_DECnet;
216 frh->dst_len = r->dst_len;
217 frh->src_len = r->src_len;
218 frh->tos = 0;
1da177e4 219
a8731cbf
SW
220 if (r->dst_len)
221 NLA_PUT_U16(skb, FRA_DST, r->dst);
222 if (r->src_len)
223 NLA_PUT_U16(skb, FRA_SRC, r->src);
1da177e4 224
a8731cbf 225 return 0;
1da177e4 226
a8731cbf
SW
227nla_put_failure:
228 return -ENOBUFS;
1da177e4
LT
229}
230
a8731cbf 231static u32 dn_fib_rule_default_pref(void)
1da177e4 232{
a8731cbf
SW
233 struct list_head *pos;
234 struct fib_rule *rule;
235
236 if (!list_empty(&dn_fib_rules)) {
237 pos = dn_fib_rules.next;
238 if (pos->next != &dn_fib_rules) {
239 rule = list_entry(pos->next, struct fib_rule, list);
240 if (rule->pref)
241 return rule->pref - 1;
242 }
1da177e4
LT
243 }
244
a8731cbf 245 return 0;
1da177e4
LT
246}
247
248int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
249{
a8731cbf 250 return fib_rules_dump(skb, cb, AF_DECnet);
1da177e4
LT
251}
252
a8731cbf
SW
253static struct fib_rules_ops dn_fib_rules_ops = {
254 .family = AF_DECnet,
255 .rule_size = sizeof(struct dn_fib_rule),
256 .action = dn_fib_rule_action,
257 .match = dn_fib_rule_match,
258 .configure = dn_fib_rule_configure,
259 .compare = dn_fib_rule_compare,
260 .fill = dn_fib_rule_fill,
261 .default_pref = dn_fib_rule_default_pref,
262 .nlgroup = RTNLGRP_DECnet_RULE,
263 .policy = dn_fib_rule_policy,
264 .rules_list = &dn_fib_rules,
265 .owner = THIS_MODULE,
266};
267
1da177e4
LT
268void __init dn_fib_rules_init(void)
269{
a8731cbf
SW
270 list_add_tail(&default_rule.common.list, &dn_fib_rules);
271 fib_rules_register(&dn_fib_rules_ops);
1da177e4
LT
272}
273
274void __exit dn_fib_rules_cleanup(void)
275{
a8731cbf 276 fib_rules_unregister(&dn_fib_rules_ops);
1da177e4
LT
277}
278
279