blob: 17a1932216d63f01d8b3d4ff6e802d825eecc008 [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>
Thomas Graf73417f62007-03-27 13:56:52 -070034#include <net/dn_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Steven Whitehousea8731cb2006-08-09 15:56:46 -070036static struct fib_rules_ops dn_fib_rules_ops;
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct dn_fib_rule
39{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070040 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
51static struct dn_fib_rule default_rule = {
Steven Whitehousea8731cb2006-08-09 15:56:46 -070052 .common = {
53 .refcnt = ATOMIC_INIT(2),
54 .pref = 0x7fff,
55 .table = RT_TABLE_MAIN,
56 .action = FR_ACT_TO_TBL,
57 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
Steven Whitehousea8731cb2006-08-09 15:56:46 -070060static LIST_HEAD(dn_fib_rules);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Steven Whitehousea8731cb2006-08-09 15:56:46 -070062
63int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070065 struct fib_lookup_arg arg = {
66 .result = res,
67 };
68 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Steven Whitehousea8731cb2006-08-09 15:56:46 -070070 err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
71 res->r = arg.rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 return err;
74}
75
Adrian Bunk2aa7f362006-08-14 23:55:20 -070076static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
77 int flags, struct fib_lookup_arg *arg)
Steven Whitehouseecba3202006-03-20 22:43:28 -080078{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070079 int err = -EAGAIN;
80 struct dn_fib_table *tbl;
Steven Whitehouseecba3202006-03-20 22:43:28 -080081
Steven Whitehousea8731cb2006-08-09 15:56:46 -070082 switch(rule->action) {
83 case FR_ACT_TO_TBL:
84 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Steven Whitehousea8731cb2006-08-09 15:56:46 -070086 case FR_ACT_UNREACHABLE:
87 err = -ENETUNREACH;
88 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Steven Whitehousea8731cb2006-08-09 15:56:46 -070090 case FR_ACT_PROHIBIT:
91 err = -EACCES;
92 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Steven Whitehousea8731cb2006-08-09 15:56:46 -070094 case FR_ACT_BLACKHOLE:
95 default:
96 err = -EINVAL;
97 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700100 tbl = dn_fib_get_table(rule->table, 0);
101 if (tbl == NULL)
102 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800103
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700104 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
105 if (err > 0)
106 err = -EAGAIN;
107errout:
108 return err;
109}
110
111static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800112 FRA_GENERIC_POLICY,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700113};
114
115static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
116{
117 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800118 __le16 daddr = fl->fld_dst;
119 __le16 saddr = fl->fld_src;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700120
121 if (((saddr ^ r->src) & r->srcmask) ||
122 ((daddr ^ r->dst) & r->dstmask))
123 return 0;
124
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700125 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700128static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
129 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
130 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700132 int err = -EINVAL;
133 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Thomas Grafe1701c62007-03-24 12:46:02 -0700135 if (frh->tos)
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700136 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800137
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700138 if (rule->table == RT_TABLE_UNSPEC) {
139 if (rule->action == FR_ACT_TO_TBL) {
140 struct dn_fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700142 table = dn_fib_empty_table();
143 if (table == NULL) {
144 err = -ENOBUFS;
145 goto errout;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700148 rule->table = table->n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 }
151
Thomas Grafe1701c62007-03-24 12:46:02 -0700152 if (frh->src_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000153 r->src = nla_get_le16(tb[FRA_SRC]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700154
Thomas Grafe1701c62007-03-24 12:46:02 -0700155 if (frh->dst_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000156 r->dst = nla_get_le16(tb[FRA_DST]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700157
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700158 r->src_len = frh->src_len;
159 r->srcmask = dnet_make_mask(r->src_len);
160 r->dst_len = frh->dst_len;
161 r->dstmask = dnet_make_mask(r->dst_len);
162 err = 0;
163errout:
164 return err;
165}
166
167static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
168 struct nlattr **tb)
169{
170 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
171
172 if (frh->src_len && (r->src_len != frh->src_len))
173 return 0;
174
175 if (frh->dst_len && (r->dst_len != frh->dst_len))
176 return 0;
177
Thomas Grafe1701c62007-03-24 12:46:02 -0700178 if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700179 return 0;
180
Thomas Grafe1701c62007-03-24 12:46:02 -0700181 if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700182 return 0;
183
184 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800187unsigned dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
190 struct dn_fib_res res;
191 unsigned ret = RTN_UNICAST;
Patrick McHardyabcab262006-08-10 23:11:47 -0700192 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 res.r = NULL;
195
196 if (tb) {
197 if (!tb->lookup(tb, &fl, &res)) {
198 ret = res.type;
199 dn_fib_res_put(&res);
200 }
201 }
202 return ret;
203}
204
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700205static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
206 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700208 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700210 frh->family = AF_DECnet;
211 frh->dst_len = r->dst_len;
212 frh->src_len = r->src_len;
213 frh->tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700215 if (r->dst_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000216 NLA_PUT_LE16(skb, FRA_DST, r->dst);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700217 if (r->src_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000218 NLA_PUT_LE16(skb, FRA_SRC, r->src);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700219
220 return 0;
221
222nla_put_failure:
223 return -ENOBUFS;
224}
225
226static u32 dn_fib_rule_default_pref(void)
227{
228 struct list_head *pos;
229 struct fib_rule *rule;
230
231 if (!list_empty(&dn_fib_rules)) {
232 pos = dn_fib_rules.next;
233 if (pos->next != &dn_fib_rules) {
234 rule = list_entry(pos->next, struct fib_rule, list);
235 if (rule->pref)
236 return rule->pref - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Thomas Graf73417f62007-03-27 13:56:52 -0700243static void dn_fib_rule_flush_cache(void)
244{
Thomas Graf4b19ca42007-03-28 14:18:52 -0700245 dn_rt_cache_flush(-1);
Thomas Graf73417f62007-03-27 13:56:52 -0700246}
247
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700248static struct fib_rules_ops dn_fib_rules_ops = {
249 .family = AF_DECnet,
250 .rule_size = sizeof(struct dn_fib_rule),
Thomas Grafe1701c62007-03-24 12:46:02 -0700251 .addr_size = sizeof(u16),
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700252 .action = dn_fib_rule_action,
253 .match = dn_fib_rule_match,
254 .configure = dn_fib_rule_configure,
255 .compare = dn_fib_rule_compare,
256 .fill = dn_fib_rule_fill,
257 .default_pref = dn_fib_rule_default_pref,
Thomas Graf73417f62007-03-27 13:56:52 -0700258 .flush_cache = dn_fib_rule_flush_cache,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700259 .nlgroup = RTNLGRP_DECnet_RULE,
260 .policy = dn_fib_rule_policy,
261 .rules_list = &dn_fib_rules,
262 .owner = THIS_MODULE,
263};
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265void __init dn_fib_rules_init(void)
266{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700267 list_add_tail(&default_rule.common.list, &dn_fib_rules);
268 fib_rules_register(&dn_fib_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271void __exit dn_fib_rules_cleanup(void)
272{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700273 fib_rules_unregister(&dn_fib_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276