blob: 295bbd6a56f2e690435148cd557b39eb4c05fed7 [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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/neighbour.h>
28#include <net/dst.h>
29#include <net/flow.h>
Steven Whitehousea8731cb2006-08-09 15:56:46 -070030#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/dn.h>
32#include <net/dn_fib.h>
33#include <net/dn_neigh.h>
34#include <net/dn_dev.h>
Thomas Graf73417f62007-03-27 13:56:52 -070035#include <net/dn_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Eric W. Biedermane9c51582009-12-03 12:22:55 -080037static struct fib_rules_ops *dn_fib_rules_ops;
Steven Whitehousea8731cb2006-08-09 15:56:46 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039struct dn_fib_rule
40{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070041 struct fib_rule common;
42 unsigned char dst_len;
43 unsigned char src_len;
44 __le16 src;
45 __le16 srcmask;
46 __le16 dst;
47 __le16 dstmask;
48 __le16 srcmap;
49 u8 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Steven Whitehousea8731cb2006-08-09 15:56:46 -070052
David S. Millerbef55ae2011-03-12 17:17:10 -050053int dn_fib_lookup(struct flowidn *flp, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070055 struct fib_lookup_arg arg = {
56 .result = res,
57 };
58 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
David S. Millerbef55ae2011-03-12 17:17:10 -050060 err = fib_rules_lookup(dn_fib_rules_ops,
61 flowidn_to_flowi(flp), 0, &arg);
Steven Whitehousea8731cb2006-08-09 15:56:46 -070062 res->r = arg.rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 return err;
65}
66
Adrian Bunk2aa7f362006-08-14 23:55:20 -070067static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
68 int flags, struct fib_lookup_arg *arg)
Steven Whitehouseecba3202006-03-20 22:43:28 -080069{
David S. Millerbef55ae2011-03-12 17:17:10 -050070 struct flowidn *fld = &flp->u.dn;
Steven Whitehousea8731cb2006-08-09 15:56:46 -070071 int err = -EAGAIN;
72 struct dn_fib_table *tbl;
Steven Whitehouseecba3202006-03-20 22:43:28 -080073
Steven Whitehousea8731cb2006-08-09 15:56:46 -070074 switch(rule->action) {
75 case FR_ACT_TO_TBL:
76 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Steven Whitehousea8731cb2006-08-09 15:56:46 -070078 case FR_ACT_UNREACHABLE:
79 err = -ENETUNREACH;
80 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Steven Whitehousea8731cb2006-08-09 15:56:46 -070082 case FR_ACT_PROHIBIT:
83 err = -EACCES;
84 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Steven Whitehousea8731cb2006-08-09 15:56:46 -070086 case FR_ACT_BLACKHOLE:
87 default:
88 err = -EINVAL;
89 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
Steven Whitehousea8731cb2006-08-09 15:56:46 -070092 tbl = dn_fib_get_table(rule->table, 0);
93 if (tbl == NULL)
94 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -080095
David S. Millerbef55ae2011-03-12 17:17:10 -050096 err = tbl->lookup(tbl, fld, (struct dn_fib_res *)arg->result);
Steven Whitehousea8731cb2006-08-09 15:56:46 -070097 if (err > 0)
98 err = -EAGAIN;
99errout:
100 return err;
101}
102
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700103static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800104 FRA_GENERIC_POLICY,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700105};
106
107static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
108{
109 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
David S. Millerbef55ae2011-03-12 17:17:10 -0500110 struct flowidn *fld = &fl->u.dn;
111 __le16 daddr = fld->daddr;
112 __le16 saddr = fld->saddr;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700113
114 if (((saddr ^ r->src) & r->srcmask) ||
115 ((daddr ^ r->dst) & r->dstmask))
116 return 0;
117
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700118 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700121static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
Rami Rosen8b3521e2009-05-11 05:52:49 +0000122 struct fib_rule_hdr *frh,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700123 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700125 int err = -EINVAL;
126 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Thomas Grafe1701c62007-03-24 12:46:02 -0700128 if (frh->tos)
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700129 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800130
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700131 if (rule->table == RT_TABLE_UNSPEC) {
132 if (rule->action == FR_ACT_TO_TBL) {
133 struct dn_fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700135 table = dn_fib_empty_table();
136 if (table == NULL) {
137 err = -ENOBUFS;
138 goto errout;
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700141 rule->table = table->n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 }
144
Thomas Grafe1701c62007-03-24 12:46:02 -0700145 if (frh->src_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000146 r->src = nla_get_le16(tb[FRA_SRC]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700147
Thomas Grafe1701c62007-03-24 12:46:02 -0700148 if (frh->dst_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000149 r->dst = nla_get_le16(tb[FRA_DST]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700150
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700151 r->src_len = frh->src_len;
152 r->srcmask = dnet_make_mask(r->src_len);
153 r->dst_len = frh->dst_len;
154 r->dstmask = dnet_make_mask(r->dst_len);
155 err = 0;
156errout:
157 return err;
158}
159
160static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
161 struct nlattr **tb)
162{
163 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
164
165 if (frh->src_len && (r->src_len != frh->src_len))
166 return 0;
167
168 if (frh->dst_len && (r->dst_len != frh->dst_len))
169 return 0;
170
Thomas Grafe1701c62007-03-24 12:46:02 -0700171 if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700172 return 0;
173
Thomas Grafe1701c62007-03-24 12:46:02 -0700174 if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700175 return 0;
176
177 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Eric Dumazet95c96172012-04-15 05:58:06 +0000180unsigned int dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
David S. Millerbef55ae2011-03-12 17:17:10 -0500182 struct flowidn fld = { .daddr = addr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct dn_fib_res res;
Eric Dumazet95c96172012-04-15 05:58:06 +0000184 unsigned int ret = RTN_UNICAST;
Patrick McHardyabcab262006-08-10 23:11:47 -0700185 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 res.r = NULL;
188
189 if (tb) {
David S. Millerbef55ae2011-03-12 17:17:10 -0500190 if (!tb->lookup(tb, &fld, &res)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 ret = res.type;
192 dn_fib_res_put(&res);
193 }
194 }
195 return ret;
196}
197
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700198static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
Rami Rosen04af8cf2009-05-20 17:26:23 -0700199 struct fib_rule_hdr *frh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700201 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700203 frh->dst_len = r->dst_len;
204 frh->src_len = r->src_len;
205 frh->tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
David S. Millerb21dddb2012-04-01 20:15:14 -0400207 if ((r->dst_len &&
208 nla_put_le16(skb, FRA_DST, r->dst)) ||
209 (r->src_len &&
210 nla_put_le16(skb, FRA_SRC, r->src)))
211 goto nla_put_failure;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700212 return 0;
213
214nla_put_failure:
215 return -ENOBUFS;
216}
217
Denis V. Lunevae299fc2008-07-05 19:01:28 -0700218static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
Thomas Graf73417f62007-03-27 13:56:52 -0700219{
Thomas Graf4b19ca42007-03-28 14:18:52 -0700220 dn_rt_cache_flush(-1);
Thomas Graf73417f62007-03-27 13:56:52 -0700221}
222
Andi Kleen04a6f822012-10-04 17:12:11 -0700223static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200224 .family = AF_DECnet,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700225 .rule_size = sizeof(struct dn_fib_rule),
Thomas Grafe1701c62007-03-24 12:46:02 -0700226 .addr_size = sizeof(u16),
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700227 .action = dn_fib_rule_action,
228 .match = dn_fib_rule_match,
229 .configure = dn_fib_rule_configure,
230 .compare = dn_fib_rule_compare,
231 .fill = dn_fib_rule_fill,
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{
WANG Cong419df122015-03-31 11:01:46 -0700250 rtnl_lock();
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800251 fib_rules_unregister(dn_fib_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700252 rtnl_unlock();
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800253 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256