blob: 48fdf10be7a1634a78114cd3a4e7d05b0902f585 [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
Eric W. Biedermane9c51582009-12-03 12:22:55 -080036static struct fib_rules_ops *dn_fib_rules_ops;
Steven Whitehousea8731cb2006-08-09 15:56:46 -070037
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
Steven Whitehousea8731cb2006-08-09 15:56:46 -070051
52int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070054 struct fib_lookup_arg arg = {
55 .result = res,
56 };
57 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Eric W. Biedermane9c51582009-12-03 12:22:55 -080059 err = fib_rules_lookup(dn_fib_rules_ops, flp, 0, &arg);
Steven Whitehousea8731cb2006-08-09 15:56:46 -070060 res->r = arg.rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 return err;
63}
64
Adrian Bunk2aa7f362006-08-14 23:55:20 -070065static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
66 int flags, struct fib_lookup_arg *arg)
Steven Whitehouseecba3202006-03-20 22:43:28 -080067{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070068 int err = -EAGAIN;
69 struct dn_fib_table *tbl;
Steven Whitehouseecba3202006-03-20 22:43:28 -080070
Steven Whitehousea8731cb2006-08-09 15:56:46 -070071 switch(rule->action) {
72 case FR_ACT_TO_TBL:
73 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Steven Whitehousea8731cb2006-08-09 15:56:46 -070075 case FR_ACT_UNREACHABLE:
76 err = -ENETUNREACH;
77 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Steven Whitehousea8731cb2006-08-09 15:56:46 -070079 case FR_ACT_PROHIBIT:
80 err = -EACCES;
81 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Steven Whitehousea8731cb2006-08-09 15:56:46 -070083 case FR_ACT_BLACKHOLE:
84 default:
85 err = -EINVAL;
86 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88
Steven Whitehousea8731cb2006-08-09 15:56:46 -070089 tbl = dn_fib_get_table(rule->table, 0);
90 if (tbl == NULL)
91 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -080092
Steven Whitehousea8731cb2006-08-09 15:56:46 -070093 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
94 if (err > 0)
95 err = -EAGAIN;
96errout:
97 return err;
98}
99
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700100static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800101 FRA_GENERIC_POLICY,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700102};
103
104static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
105{
106 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800107 __le16 daddr = fl->fld_dst;
108 __le16 saddr = fl->fld_src;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700109
110 if (((saddr ^ r->src) & r->srcmask) ||
111 ((daddr ^ r->dst) & r->dstmask))
112 return 0;
113
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700114 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700117static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
Rami Rosen8b3521e2009-05-11 05:52:49 +0000118 struct fib_rule_hdr *frh,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700119 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700121 int err = -EINVAL;
122 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Thomas Grafe1701c62007-03-24 12:46:02 -0700124 if (frh->tos)
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700125 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800126
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700127 if (rule->table == RT_TABLE_UNSPEC) {
128 if (rule->action == FR_ACT_TO_TBL) {
129 struct dn_fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700131 table = dn_fib_empty_table();
132 if (table == NULL) {
133 err = -ENOBUFS;
134 goto errout;
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700137 rule->table = table->n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 }
140
Thomas Grafe1701c62007-03-24 12:46:02 -0700141 if (frh->src_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000142 r->src = nla_get_le16(tb[FRA_SRC]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700143
Thomas Grafe1701c62007-03-24 12:46:02 -0700144 if (frh->dst_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000145 r->dst = nla_get_le16(tb[FRA_DST]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700146
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700147 r->src_len = frh->src_len;
148 r->srcmask = dnet_make_mask(r->src_len);
149 r->dst_len = frh->dst_len;
150 r->dstmask = dnet_make_mask(r->dst_len);
151 err = 0;
152errout:
153 return err;
154}
155
156static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
157 struct nlattr **tb)
158{
159 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
160
161 if (frh->src_len && (r->src_len != frh->src_len))
162 return 0;
163
164 if (frh->dst_len && (r->dst_len != frh->dst_len))
165 return 0;
166
Thomas Grafe1701c62007-03-24 12:46:02 -0700167 if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700168 return 0;
169
Thomas Grafe1701c62007-03-24 12:46:02 -0700170 if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700171 return 0;
172
173 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800176unsigned dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
179 struct dn_fib_res res;
180 unsigned ret = RTN_UNICAST;
Patrick McHardyabcab262006-08-10 23:11:47 -0700181 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 res.r = NULL;
184
185 if (tb) {
186 if (!tb->lookup(tb, &fl, &res)) {
187 ret = res.type;
188 dn_fib_res_put(&res);
189 }
190 }
191 return ret;
192}
193
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700194static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
Rami Rosen04af8cf2009-05-20 17:26:23 -0700195 struct fib_rule_hdr *frh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700197 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700199 frh->dst_len = r->dst_len;
200 frh->src_len = r->src_len;
201 frh->tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700203 if (r->dst_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000204 NLA_PUT_LE16(skb, FRA_DST, r->dst);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700205 if (r->src_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000206 NLA_PUT_LE16(skb, FRA_SRC, r->src);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700207
208 return 0;
209
210nla_put_failure:
211 return -ENOBUFS;
212}
213
Denis V. Lunevae299fc2008-07-05 19:01:28 -0700214static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
Thomas Graf73417f62007-03-27 13:56:52 -0700215{
Thomas Graf4b19ca42007-03-28 14:18:52 -0700216 dn_rt_cache_flush(-1);
Thomas Graf73417f62007-03-27 13:56:52 -0700217}
218
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200219static const struct fib_rules_ops __net_initdata dn_fib_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200220 .family = AF_DECnet,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700221 .rule_size = sizeof(struct dn_fib_rule),
Thomas Grafe1701c62007-03-24 12:46:02 -0700222 .addr_size = sizeof(u16),
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700223 .action = dn_fib_rule_action,
224 .match = dn_fib_rule_match,
225 .configure = dn_fib_rule_configure,
226 .compare = dn_fib_rule_compare,
227 .fill = dn_fib_rule_fill,
Patrick McHardyd8a566b2010-04-13 05:03:15 +0000228 .default_pref = fib_default_rule_pref,
Thomas Graf73417f62007-03-27 13:56:52 -0700229 .flush_cache = dn_fib_rule_flush_cache,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700230 .nlgroup = RTNLGRP_DECnet_RULE,
231 .policy = dn_fib_rule_policy,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700232 .owner = THIS_MODULE,
Denis V. Lunev03592382008-01-20 16:46:01 -0800233 .fro_net = &init_net,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700234};
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236void __init dn_fib_rules_init(void)
237{
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800238 dn_fib_rules_ops =
239 fib_rules_register(&dn_fib_rules_ops_template, &init_net);
240 BUG_ON(IS_ERR(dn_fib_rules_ops));
241 BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff,
Denis V. Lunev2994c632007-11-10 22:12:03 -0800242 RT_TABLE_MAIN, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245void __exit dn_fib_rules_cleanup(void)
246{
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800247 fib_rules_unregister(dn_fib_rules_ops);
248 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251