blob: b6c98ac93dc8737d96e54ac7f00dacacf90ae6f4 [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};
49
50static struct dn_fib_rule default_rule = {
Steven Whitehousea8731cb2006-08-09 15:56:46 -070051 .common = {
52 .refcnt = ATOMIC_INIT(2),
53 .pref = 0x7fff,
54 .table = RT_TABLE_MAIN,
55 .action = FR_ACT_TO_TBL,
56 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
Steven Whitehousea8731cb2006-08-09 15:56:46 -070059static LIST_HEAD(dn_fib_rules);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Steven Whitehousea8731cb2006-08-09 15:56:46 -070061
62int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070064 struct fib_lookup_arg arg = {
65 .result = res,
66 };
67 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Steven Whitehousea8731cb2006-08-09 15:56:46 -070069 err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
70 res->r = arg.rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 return err;
73}
74
Adrian Bunk2aa7f362006-08-14 23:55:20 -070075static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
76 int flags, struct fib_lookup_arg *arg)
Steven Whitehouseecba3202006-03-20 22:43:28 -080077{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070078 int err = -EAGAIN;
79 struct dn_fib_table *tbl;
Steven Whitehouseecba3202006-03-20 22:43:28 -080080
Steven Whitehousea8731cb2006-08-09 15:56:46 -070081 switch(rule->action) {
82 case FR_ACT_TO_TBL:
83 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Steven Whitehousea8731cb2006-08-09 15:56:46 -070085 case FR_ACT_UNREACHABLE:
86 err = -ENETUNREACH;
87 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Steven Whitehousea8731cb2006-08-09 15:56:46 -070089 case FR_ACT_PROHIBIT:
90 err = -EACCES;
91 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Steven Whitehousea8731cb2006-08-09 15:56:46 -070093 case FR_ACT_BLACKHOLE:
94 default:
95 err = -EINVAL;
96 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
Steven Whitehousea8731cb2006-08-09 15:56:46 -070099 tbl = dn_fib_get_table(rule->table, 0);
100 if (tbl == NULL)
101 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800102
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700103 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
104 if (err > 0)
105 err = -EAGAIN;
106errout:
107 return err;
108}
109
110static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800111 FRA_GENERIC_POLICY,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700112 [FRA_SRC] = { .type = NLA_U16 },
113 [FRA_DST] = { .type = NLA_U16 },
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700114};
115
116static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
117{
118 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800119 __le16 daddr = fl->fld_dst;
120 __le16 saddr = fl->fld_src;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700121
122 if (((saddr ^ r->src) & r->srcmask) ||
123 ((daddr ^ r->dst) & r->dstmask))
124 return 0;
125
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700126 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700129static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
130 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
131 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700133 int err = -EINVAL;
134 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700136 if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
137 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800138
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700139 if (rule->table == RT_TABLE_UNSPEC) {
140 if (rule->action == FR_ACT_TO_TBL) {
141 struct dn_fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700143 table = dn_fib_empty_table();
144 if (table == NULL) {
145 err = -ENOBUFS;
146 goto errout;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700149 rule->table = table->n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 }
152
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700153 if (tb[FRA_SRC])
Al Viro2835fdf2007-02-09 18:13:37 +0000154 r->src = nla_get_le16(tb[FRA_SRC]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700155
156 if (tb[FRA_DST])
Al Viro2835fdf2007-02-09 18:13:37 +0000157 r->dst = nla_get_le16(tb[FRA_DST]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700158
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700159 r->src_len = frh->src_len;
160 r->srcmask = dnet_make_mask(r->src_len);
161 r->dst_len = frh->dst_len;
162 r->dstmask = dnet_make_mask(r->dst_len);
163 err = 0;
164errout:
165 return err;
166}
167
168static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
169 struct nlattr **tb)
170{
171 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
172
173 if (frh->src_len && (r->src_len != frh->src_len))
174 return 0;
175
176 if (frh->dst_len && (r->dst_len != frh->dst_len))
177 return 0;
178
Al Viro2835fdf2007-02-09 18:13:37 +0000179 if (tb[FRA_SRC] && (r->src != nla_get_le16(tb[FRA_SRC])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700180 return 0;
181
Al Viro2835fdf2007-02-09 18:13:37 +0000182 if (tb[FRA_DST] && (r->dst != nla_get_le16(tb[FRA_DST])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700183 return 0;
184
185 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800188unsigned dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
191 struct dn_fib_res res;
192 unsigned ret = RTN_UNICAST;
Patrick McHardyabcab262006-08-10 23:11:47 -0700193 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 res.r = NULL;
196
197 if (tb) {
198 if (!tb->lookup(tb, &fl, &res)) {
199 ret = res.type;
200 dn_fib_res_put(&res);
201 }
202 }
203 return ret;
204}
205
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700206static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
207 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700209 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700211 frh->family = AF_DECnet;
212 frh->dst_len = r->dst_len;
213 frh->src_len = r->src_len;
214 frh->tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700216 if (r->dst_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000217 NLA_PUT_LE16(skb, FRA_DST, r->dst);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700218 if (r->src_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000219 NLA_PUT_LE16(skb, FRA_SRC, r->src);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700220
221 return 0;
222
223nla_put_failure:
224 return -ENOBUFS;
225}
226
227static u32 dn_fib_rule_default_pref(void)
228{
229 struct list_head *pos;
230 struct fib_rule *rule;
231
232 if (!list_empty(&dn_fib_rules)) {
233 pos = dn_fib_rules.next;
234 if (pos->next != &dn_fib_rules) {
235 rule = list_entry(pos->next, struct fib_rule, list);
236 if (rule->pref)
237 return rule->pref - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
245{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700246 return fib_rules_dump(skb, cb, AF_DECnet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700249static struct fib_rules_ops dn_fib_rules_ops = {
250 .family = AF_DECnet,
251 .rule_size = sizeof(struct dn_fib_rule),
252 .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,
258 .nlgroup = RTNLGRP_DECnet_RULE,
259 .policy = dn_fib_rule_policy,
260 .rules_list = &dn_fib_rules,
261 .owner = THIS_MODULE,
262};
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264void __init dn_fib_rules_init(void)
265{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700266 list_add_tail(&default_rule.common.list, &dn_fib_rules);
267 fib_rules_register(&dn_fib_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270void __exit dn_fib_rules_cleanup(void)
271{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700272 fib_rules_unregister(&dn_fib_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275