blob: 590e0a72495cf8194a92781e13052f736307130c [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;
Patrick McHardy88e91f22006-08-25 16:11:08 -070050 u32 fwmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54static struct dn_fib_rule default_rule = {
Steven Whitehousea8731cb2006-08-09 15:56:46 -070055 .common = {
56 .refcnt = ATOMIC_INIT(2),
57 .pref = 0x7fff,
58 .table = RT_TABLE_MAIN,
59 .action = FR_ACT_TO_TBL,
60 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Steven Whitehousea8731cb2006-08-09 15:56:46 -070063static LIST_HEAD(dn_fib_rules);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Steven Whitehousea8731cb2006-08-09 15:56:46 -070065
66int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070068 struct fib_lookup_arg arg = {
69 .result = res,
70 };
71 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Steven Whitehousea8731cb2006-08-09 15:56:46 -070073 err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
74 res->r = arg.rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 return err;
77}
78
Adrian Bunk2aa7f362006-08-14 23:55:20 -070079static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
80 int flags, struct fib_lookup_arg *arg)
Steven Whitehouseecba3202006-03-20 22:43:28 -080081{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070082 int err = -EAGAIN;
83 struct dn_fib_table *tbl;
Steven Whitehouseecba3202006-03-20 22:43:28 -080084
Steven Whitehousea8731cb2006-08-09 15:56:46 -070085 switch(rule->action) {
86 case FR_ACT_TO_TBL:
87 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Steven Whitehousea8731cb2006-08-09 15:56:46 -070089 case FR_ACT_UNREACHABLE:
90 err = -ENETUNREACH;
91 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Steven Whitehousea8731cb2006-08-09 15:56:46 -070093 case FR_ACT_PROHIBIT:
94 err = -EACCES;
95 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Steven Whitehousea8731cb2006-08-09 15:56:46 -070097 case FR_ACT_BLACKHOLE:
98 default:
99 err = -EINVAL;
100 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700103 tbl = dn_fib_get_table(rule->table, 0);
104 if (tbl == NULL)
105 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800106
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700107 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
108 if (err > 0)
109 err = -EAGAIN;
110errout:
111 return err;
112}
113
114static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
Thomas Graf5176f912006-08-26 20:13:18 -0700115 [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700116 [FRA_PRIORITY] = { .type = NLA_U32 },
117 [FRA_SRC] = { .type = NLA_U16 },
118 [FRA_DST] = { .type = NLA_U16 },
119 [FRA_FWMARK] = { .type = NLA_U32 },
Patrick McHardy88e91f22006-08-25 16:11:08 -0700120 [FRA_FWMASK] = { .type = NLA_U32 },
Steven Whitehoused8803092006-08-11 16:43:41 -0700121 [FRA_TABLE] = { .type = NLA_U32 },
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700122};
123
124static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
125{
126 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800127 __le16 daddr = fl->fld_dst;
128 __le16 saddr = fl->fld_src;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700129
130 if (((saddr ^ r->src) & r->srcmask) ||
131 ((daddr ^ r->dst) & r->dstmask))
132 return 0;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#ifdef CONFIG_DECNET_ROUTE_FWMARK
Patrick McHardy88e91f22006-08-25 16:11:08 -0700135 if ((r->fwmark ^ fl->fld_fwmark) & r->fwmask)
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700139 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700142static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
143 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
144 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700146 int err = -EINVAL;
147 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700149 if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
150 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800151
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700152 if (rule->table == RT_TABLE_UNSPEC) {
153 if (rule->action == FR_ACT_TO_TBL) {
154 struct dn_fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700156 table = dn_fib_empty_table();
157 if (table == NULL) {
158 err = -ENOBUFS;
159 goto errout;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700162 rule->table = table->n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164 }
165
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700166 if (tb[FRA_SRC])
167 r->src = nla_get_u16(tb[FRA_SRC]);
168
169 if (tb[FRA_DST])
170 r->dst = nla_get_u16(tb[FRA_DST]);
171
172#ifdef CONFIG_DECNET_ROUTE_FWMARK
Patrick McHardy88e91f22006-08-25 16:11:08 -0700173 if (tb[FRA_FWMARK]) {
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700174 r->fwmark = nla_get_u32(tb[FRA_FWMARK]);
Patrick McHardy88e91f22006-08-25 16:11:08 -0700175 if (r->fwmark)
176 /* compatibility: if the mark value is non-zero all bits
177 * are compared unless a mask is explicitly specified.
178 */
179 r->fwmask = 0xFFFFFFFF;
180 }
181
182 if (tb[FRA_FWMASK])
183 r->fwmask = nla_get_u32(tb[FRA_FWMASK]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700184#endif
185
186 r->src_len = frh->src_len;
187 r->srcmask = dnet_make_mask(r->src_len);
188 r->dst_len = frh->dst_len;
189 r->dstmask = dnet_make_mask(r->dst_len);
190 err = 0;
191errout:
192 return err;
193}
194
195static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
196 struct nlattr **tb)
197{
198 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
199
200 if (frh->src_len && (r->src_len != frh->src_len))
201 return 0;
202
203 if (frh->dst_len && (r->dst_len != frh->dst_len))
204 return 0;
205
206#ifdef CONFIG_DECNET_ROUTE_FWMARK
207 if (tb[FRA_FWMARK] && (r->fwmark != nla_get_u32(tb[FRA_FWMARK])))
208 return 0;
Patrick McHardy88e91f22006-08-25 16:11:08 -0700209
210 if (tb[FRA_FWMASK] && (r->fwmask != nla_get_u32(tb[FRA_FWMASK])))
211 return 0;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700212#endif
213
Steven Whitehoused1aa62f2006-08-11 16:44:18 -0700214 if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700215 return 0;
216
Steven Whitehoused1aa62f2006-08-11 16:44:18 -0700217 if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700218 return 0;
219
220 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800223unsigned dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
226 struct dn_fib_res res;
227 unsigned ret = RTN_UNICAST;
Patrick McHardyabcab262006-08-10 23:11:47 -0700228 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 res.r = NULL;
231
232 if (tb) {
233 if (!tb->lookup(tb, &fl, &res)) {
234 ret = res.type;
235 dn_fib_res_put(&res);
236 }
237 }
238 return ret;
239}
240
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700241static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
242 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700244 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700246 frh->family = AF_DECnet;
247 frh->dst_len = r->dst_len;
248 frh->src_len = r->src_len;
249 frh->tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700251#ifdef CONFIG_DECNET_ROUTE_FWMARK
252 if (r->fwmark)
253 NLA_PUT_U32(skb, FRA_FWMARK, r->fwmark);
Patrick McHardy88e91f22006-08-25 16:11:08 -0700254 if (r->fwmask || r->fwmark)
255 NLA_PUT_U32(skb, FRA_FWMASK, r->fwmask);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700256#endif
257 if (r->dst_len)
258 NLA_PUT_U16(skb, FRA_DST, r->dst);
259 if (r->src_len)
260 NLA_PUT_U16(skb, FRA_SRC, r->src);
261
262 return 0;
263
264nla_put_failure:
265 return -ENOBUFS;
266}
267
268static u32 dn_fib_rule_default_pref(void)
269{
270 struct list_head *pos;
271 struct fib_rule *rule;
272
273 if (!list_empty(&dn_fib_rules)) {
274 pos = dn_fib_rules.next;
275 if (pos->next != &dn_fib_rules) {
276 rule = list_entry(pos->next, struct fib_rule, list);
277 if (rule->pref)
278 return rule->pref - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
286{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700287 return fib_rules_dump(skb, cb, AF_DECnet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700290static struct fib_rules_ops dn_fib_rules_ops = {
291 .family = AF_DECnet,
292 .rule_size = sizeof(struct dn_fib_rule),
293 .action = dn_fib_rule_action,
294 .match = dn_fib_rule_match,
295 .configure = dn_fib_rule_configure,
296 .compare = dn_fib_rule_compare,
297 .fill = dn_fib_rule_fill,
298 .default_pref = dn_fib_rule_default_pref,
299 .nlgroup = RTNLGRP_DECnet_RULE,
300 .policy = dn_fib_rule_policy,
301 .rules_list = &dn_fib_rules,
302 .owner = THIS_MODULE,
303};
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305void __init dn_fib_rules_init(void)
306{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700307 list_add_tail(&default_rule.common.list, &dn_fib_rules);
308 fib_rules_register(&dn_fib_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311void __exit dn_fib_rules_cleanup(void)
312{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700313 fib_rules_unregister(&dn_fib_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316