blob: f0efb0ccfeca30cd668a4e32b5aaf2059ab1f844 [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
David S. Millerbef55ae2011-03-12 17:17:10 -050052int dn_fib_lookup(struct flowidn *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
David S. Millerbef55ae2011-03-12 17:17:10 -050059 err = fib_rules_lookup(dn_fib_rules_ops,
60 flowidn_to_flowi(flp), 0, &arg);
Steven Whitehousea8731cb2006-08-09 15:56:46 -070061 res->r = arg.rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 return err;
64}
65
Adrian Bunk2aa7f362006-08-14 23:55:20 -070066static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
67 int flags, struct fib_lookup_arg *arg)
Steven Whitehouseecba3202006-03-20 22:43:28 -080068{
David S. Millerbef55ae2011-03-12 17:17:10 -050069 struct flowidn *fld = &flp->u.dn;
Steven Whitehousea8731cb2006-08-09 15:56:46 -070070 int err = -EAGAIN;
71 struct dn_fib_table *tbl;
Steven Whitehouseecba3202006-03-20 22:43:28 -080072
Steven Whitehousea8731cb2006-08-09 15:56:46 -070073 switch(rule->action) {
74 case FR_ACT_TO_TBL:
75 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Steven Whitehousea8731cb2006-08-09 15:56:46 -070077 case FR_ACT_UNREACHABLE:
78 err = -ENETUNREACH;
79 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Steven Whitehousea8731cb2006-08-09 15:56:46 -070081 case FR_ACT_PROHIBIT:
82 err = -EACCES;
83 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Steven Whitehousea8731cb2006-08-09 15:56:46 -070085 case FR_ACT_BLACKHOLE:
86 default:
87 err = -EINVAL;
88 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90
Steven Whitehousea8731cb2006-08-09 15:56:46 -070091 tbl = dn_fib_get_table(rule->table, 0);
92 if (tbl == NULL)
93 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -080094
David S. Millerbef55ae2011-03-12 17:17:10 -050095 err = tbl->lookup(tbl, fld, (struct dn_fib_res *)arg->result);
Steven Whitehousea8731cb2006-08-09 15:56:46 -070096 if (err > 0)
97 err = -EAGAIN;
98errout:
99 return err;
100}
101
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700102static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800103 FRA_GENERIC_POLICY,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700104};
105
106static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
107{
108 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
David S. Millerbef55ae2011-03-12 17:17:10 -0500109 struct flowidn *fld = &fl->u.dn;
110 __le16 daddr = fld->daddr;
111 __le16 saddr = fld->saddr;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700112
113 if (((saddr ^ r->src) & r->srcmask) ||
114 ((daddr ^ r->dst) & r->dstmask))
115 return 0;
116
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700117 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700120static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
Rami Rosen8b3521e2009-05-11 05:52:49 +0000121 struct fib_rule_hdr *frh,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700122 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700124 int err = -EINVAL;
125 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Thomas Grafe1701c62007-03-24 12:46:02 -0700127 if (frh->tos)
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700128 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800129
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700130 if (rule->table == RT_TABLE_UNSPEC) {
131 if (rule->action == FR_ACT_TO_TBL) {
132 struct dn_fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700134 table = dn_fib_empty_table();
135 if (table == NULL) {
136 err = -ENOBUFS;
137 goto errout;
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700140 rule->table = table->n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142 }
143
Thomas Grafe1701c62007-03-24 12:46:02 -0700144 if (frh->src_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000145 r->src = nla_get_le16(tb[FRA_SRC]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700146
Thomas Grafe1701c62007-03-24 12:46:02 -0700147 if (frh->dst_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000148 r->dst = nla_get_le16(tb[FRA_DST]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700149
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700150 r->src_len = frh->src_len;
151 r->srcmask = dnet_make_mask(r->src_len);
152 r->dst_len = frh->dst_len;
153 r->dstmask = dnet_make_mask(r->dst_len);
154 err = 0;
155errout:
156 return err;
157}
158
159static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
160 struct nlattr **tb)
161{
162 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
163
164 if (frh->src_len && (r->src_len != frh->src_len))
165 return 0;
166
167 if (frh->dst_len && (r->dst_len != frh->dst_len))
168 return 0;
169
Thomas Grafe1701c62007-03-24 12:46:02 -0700170 if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700171 return 0;
172
Thomas Grafe1701c62007-03-24 12:46:02 -0700173 if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700174 return 0;
175
176 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800179unsigned dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
David S. Millerbef55ae2011-03-12 17:17:10 -0500181 struct flowidn fld = { .daddr = addr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct dn_fib_res res;
183 unsigned ret = RTN_UNICAST;
Patrick McHardyabcab262006-08-10 23:11:47 -0700184 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 res.r = NULL;
187
188 if (tb) {
David S. Millerbef55ae2011-03-12 17:17:10 -0500189 if (!tb->lookup(tb, &fld, &res)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ret = res.type;
191 dn_fib_res_put(&res);
192 }
193 }
194 return ret;
195}
196
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700197static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
Rami Rosen04af8cf2009-05-20 17:26:23 -0700198 struct fib_rule_hdr *frh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700200 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700202 frh->dst_len = r->dst_len;
203 frh->src_len = r->src_len;
204 frh->tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700206 if (r->dst_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000207 NLA_PUT_LE16(skb, FRA_DST, r->dst);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700208 if (r->src_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000209 NLA_PUT_LE16(skb, FRA_SRC, r->src);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700210
211 return 0;
212
213nla_put_failure:
214 return -ENOBUFS;
215}
216
Denis V. Lunevae299fc2008-07-05 19:01:28 -0700217static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
Thomas Graf73417f62007-03-27 13:56:52 -0700218{
Thomas Graf4b19ca42007-03-28 14:18:52 -0700219 dn_rt_cache_flush(-1);
Thomas Graf73417f62007-03-27 13:56:52 -0700220}
221
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200222static const struct fib_rules_ops __net_initdata dn_fib_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200223 .family = AF_DECnet,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700224 .rule_size = sizeof(struct dn_fib_rule),
Thomas Grafe1701c62007-03-24 12:46:02 -0700225 .addr_size = sizeof(u16),
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700226 .action = dn_fib_rule_action,
227 .match = dn_fib_rule_match,
228 .configure = dn_fib_rule_configure,
229 .compare = dn_fib_rule_compare,
230 .fill = dn_fib_rule_fill,
Patrick McHardyd8a566b2010-04-13 05:03:15 +0000231 .default_pref = fib_default_rule_pref,
Thomas Graf73417f62007-03-27 13:56:52 -0700232 .flush_cache = dn_fib_rule_flush_cache,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700233 .nlgroup = RTNLGRP_DECnet_RULE,
234 .policy = dn_fib_rule_policy,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700235 .owner = THIS_MODULE,
Denis V. Lunev035923832008-01-20 16:46:01 -0800236 .fro_net = &init_net,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700237};
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239void __init dn_fib_rules_init(void)
240{
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800241 dn_fib_rules_ops =
242 fib_rules_register(&dn_fib_rules_ops_template, &init_net);
243 BUG_ON(IS_ERR(dn_fib_rules_ops));
244 BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff,
Denis V. Lunev2994c632007-11-10 22:12:03 -0800245 RT_TABLE_MAIN, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248void __exit dn_fib_rules_cleanup(void)
249{
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800250 fib_rules_unregister(dn_fib_rules_ops);
251 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254