blob: 50e819edf8c72086b66b8a34167717c8779d1e3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
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:
Steven Whitehousea8731cb2006-08-09 15:56:46 -070014 * Steve Whitehouse <steve@chygwyn.com>
15 * Updated for Thomas Graf's generic rules
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/netlink.h>
21#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/spinlock.h>
Steven Whitehouseecba3202006-03-20 22:43:28 -080024#include <linux/list.h>
25#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/neighbour.h>
27#include <net/dst.h>
28#include <net/flow.h>
Steven Whitehousea8731cb2006-08-09 15:56:46 -070029#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/dn.h>
31#include <net/dn_fib.h>
32#include <net/dn_neigh.h>
33#include <net/dn_dev.h>
34
Steven Whitehousea8731cb2006-08-09 15:56:46 -070035static struct fib_rules_ops dn_fib_rules_ops;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct dn_fib_rule
38{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070039 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#ifdef CONFIG_DECNET_ROUTE_FWMARK
Steven Whitehousea8731cb2006-08-09 15:56:46 -070049 u32 fwmark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53static struct dn_fib_rule default_rule = {
Steven Whitehousea8731cb2006-08-09 15:56:46 -070054 .common = {
55 .refcnt = ATOMIC_INIT(2),
56 .pref = 0x7fff,
57 .table = RT_TABLE_MAIN,
58 .action = FR_ACT_TO_TBL,
59 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Steven Whitehousea8731cb2006-08-09 15:56:46 -070062static LIST_HEAD(dn_fib_rules);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Steven Whitehousea8731cb2006-08-09 15:56:46 -070064
65int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070067 struct fib_lookup_arg arg = {
68 .result = res,
69 };
70 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Steven Whitehousea8731cb2006-08-09 15:56:46 -070072 err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
73 res->r = arg.rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 return err;
76}
77
Adrian Bunk2aa7f362006-08-14 23:55:20 -070078static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
79 int flags, struct fib_lookup_arg *arg)
Steven Whitehouseecba3202006-03-20 22:43:28 -080080{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070081 int err = -EAGAIN;
82 struct dn_fib_table *tbl;
Steven Whitehouseecba3202006-03-20 22:43:28 -080083
Steven Whitehousea8731cb2006-08-09 15:56:46 -070084 switch(rule->action) {
85 case FR_ACT_TO_TBL:
86 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Steven Whitehousea8731cb2006-08-09 15:56:46 -070088 case FR_ACT_UNREACHABLE:
89 err = -ENETUNREACH;
90 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Steven Whitehousea8731cb2006-08-09 15:56:46 -070092 case FR_ACT_PROHIBIT:
93 err = -EACCES;
94 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Steven Whitehousea8731cb2006-08-09 15:56:46 -070096 case FR_ACT_BLACKHOLE:
97 default:
98 err = -EINVAL;
99 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700102 tbl = dn_fib_get_table(rule->table, 0);
103 if (tbl == NULL)
104 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800105
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700106 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
107 if (err > 0)
108 err = -EAGAIN;
109errout:
110 return err;
111}
112
113static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
114 [FRA_IFNAME] = { .type = NLA_STRING },
115 [FRA_PRIORITY] = { .type = NLA_U32 },
116 [FRA_SRC] = { .type = NLA_U16 },
117 [FRA_DST] = { .type = NLA_U16 },
118 [FRA_FWMARK] = { .type = NLA_U32 },
Steven Whitehoused8803092006-08-11 16:43:41 -0700119 [FRA_TABLE] = { .type = NLA_U32 },
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700120};
121
122static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
123{
124 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
125 u16 daddr = fl->fld_dst;
126 u16 saddr = fl->fld_src;
127
128 if (((saddr ^ r->src) & r->srcmask) ||
129 ((daddr ^ r->dst) & r->dstmask))
130 return 0;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#ifdef CONFIG_DECNET_ROUTE_FWMARK
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700133 if (r->fwmark && (r->fwmark != fl->fld_fwmark))
134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700137 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700140static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
141 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
142 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700144 int err = -EINVAL;
145 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700147 if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
148 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800149
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700150 if (rule->table == RT_TABLE_UNSPEC) {
151 if (rule->action == FR_ACT_TO_TBL) {
152 struct dn_fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700154 table = dn_fib_empty_table();
155 if (table == NULL) {
156 err = -ENOBUFS;
157 goto errout;
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700160 rule->table = table->n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 }
163
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700164 if (tb[FRA_SRC])
165 r->src = nla_get_u16(tb[FRA_SRC]);
166
167 if (tb[FRA_DST])
168 r->dst = nla_get_u16(tb[FRA_DST]);
169
170#ifdef CONFIG_DECNET_ROUTE_FWMARK
171 if (tb[FRA_FWMARK])
172 r->fwmark = nla_get_u32(tb[FRA_FWMARK]);
173#endif
174
175 r->src_len = frh->src_len;
176 r->srcmask = dnet_make_mask(r->src_len);
177 r->dst_len = frh->dst_len;
178 r->dstmask = dnet_make_mask(r->dst_len);
179 err = 0;
180errout:
181 return err;
182}
183
184static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
185 struct nlattr **tb)
186{
187 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
188
189 if (frh->src_len && (r->src_len != frh->src_len))
190 return 0;
191
192 if (frh->dst_len && (r->dst_len != frh->dst_len))
193 return 0;
194
195#ifdef CONFIG_DECNET_ROUTE_FWMARK
196 if (tb[FRA_FWMARK] && (r->fwmark != nla_get_u32(tb[FRA_FWMARK])))
197 return 0;
198#endif
199
Steven Whitehoused1aa62f2006-08-11 16:44:18 -0700200 if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700201 return 0;
202
Steven Whitehoused1aa62f2006-08-11 16:44:18 -0700203 if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700204 return 0;
205
206 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800209unsigned dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
212 struct dn_fib_res res;
213 unsigned ret = RTN_UNICAST;
Patrick McHardyabcab262006-08-10 23:11:47 -0700214 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 res.r = NULL;
217
218 if (tb) {
219 if (!tb->lookup(tb, &fl, &res)) {
220 ret = res.type;
221 dn_fib_res_put(&res);
222 }
223 }
224 return ret;
225}
226
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700227static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
228 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700230 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700232 frh->family = AF_DECnet;
233 frh->dst_len = r->dst_len;
234 frh->src_len = r->src_len;
235 frh->tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700237#ifdef CONFIG_DECNET_ROUTE_FWMARK
238 if (r->fwmark)
239 NLA_PUT_U32(skb, FRA_FWMARK, r->fwmark);
240#endif
241 if (r->dst_len)
242 NLA_PUT_U16(skb, FRA_DST, r->dst);
243 if (r->src_len)
244 NLA_PUT_U16(skb, FRA_SRC, r->src);
245
246 return 0;
247
248nla_put_failure:
249 return -ENOBUFS;
250}
251
252static u32 dn_fib_rule_default_pref(void)
253{
254 struct list_head *pos;
255 struct fib_rule *rule;
256
257 if (!list_empty(&dn_fib_rules)) {
258 pos = dn_fib_rules.next;
259 if (pos->next != &dn_fib_rules) {
260 rule = list_entry(pos->next, struct fib_rule, list);
261 if (rule->pref)
262 return rule->pref - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
270{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700271 return fib_rules_dump(skb, cb, AF_DECnet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700274static struct fib_rules_ops dn_fib_rules_ops = {
275 .family = AF_DECnet,
276 .rule_size = sizeof(struct dn_fib_rule),
277 .action = dn_fib_rule_action,
278 .match = dn_fib_rule_match,
279 .configure = dn_fib_rule_configure,
280 .compare = dn_fib_rule_compare,
281 .fill = dn_fib_rule_fill,
282 .default_pref = dn_fib_rule_default_pref,
283 .nlgroup = RTNLGRP_DECnet_RULE,
284 .policy = dn_fib_rule_policy,
285 .rules_list = &dn_fib_rules,
286 .owner = THIS_MODULE,
287};
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289void __init dn_fib_rules_init(void)
290{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700291 list_add_tail(&default_rule.common.list, &dn_fib_rules);
292 fib_rules_register(&dn_fib_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295void __exit dn_fib_rules_cleanup(void)
296{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700297 fib_rules_unregister(&dn_fib_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300