blob: c795c3f509c9494486463f1839c4c9dcadff797c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3/*
4 * DECnet An implementation of the DECnet protocol suite for the LINUX
5 * operating system. DECnet is implemented using the BSD Socket
6 * interface as the means of communication with the user level.
7 *
8 * DECnet Routing Forwarding Information Base (Rules)
9 *
10 * Author: Steve Whitehouse <SteveW@ACM.org>
11 * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
12 *
13 *
14 * Changes:
Steven Whitehousea8731cb2006-08-09 15:56:46 -070015 * Steve Whitehouse <steve@chygwyn.com>
16 * Updated for Thomas Graf's generic rules
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/netlink.h>
22#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/spinlock.h>
Steven Whitehouseecba3202006-03-20 22:43:28 -080025#include <linux/list.h>
26#include <linux/rcupdate.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/neighbour.h>
29#include <net/dst.h>
30#include <net/flow.h>
Steven Whitehousea8731cb2006-08-09 15:56:46 -070031#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/dn.h>
33#include <net/dn_fib.h>
34#include <net/dn_neigh.h>
35#include <net/dn_dev.h>
Thomas Graf73417f62007-03-27 13:56:52 -070036#include <net/dn_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Eric W. Biedermane9c51582009-12-03 12:22:55 -080038static struct fib_rules_ops *dn_fib_rules_ops;
Steven Whitehousea8731cb2006-08-09 15:56:46 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040struct dn_fib_rule
41{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070042 struct fib_rule common;
43 unsigned char dst_len;
44 unsigned char src_len;
45 __le16 src;
46 __le16 srcmask;
47 __le16 dst;
48 __le16 dstmask;
49 __le16 srcmap;
50 u8 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Steven Whitehousea8731cb2006-08-09 15:56:46 -070053
David S. Millerbef55ae2011-03-12 17:17:10 -050054int dn_fib_lookup(struct flowidn *flp, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070056 struct fib_lookup_arg arg = {
57 .result = res,
58 };
59 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
David S. Millerbef55ae2011-03-12 17:17:10 -050061 err = fib_rules_lookup(dn_fib_rules_ops,
62 flowidn_to_flowi(flp), 0, &arg);
Steven Whitehousea8731cb2006-08-09 15:56:46 -070063 res->r = arg.rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 return err;
66}
67
Adrian Bunk2aa7f362006-08-14 23:55:20 -070068static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
69 int flags, struct fib_lookup_arg *arg)
Steven Whitehouseecba3202006-03-20 22:43:28 -080070{
David S. Millerbef55ae2011-03-12 17:17:10 -050071 struct flowidn *fld = &flp->u.dn;
Steven Whitehousea8731cb2006-08-09 15:56:46 -070072 int err = -EAGAIN;
73 struct dn_fib_table *tbl;
Steven Whitehouseecba3202006-03-20 22:43:28 -080074
Steven Whitehousea8731cb2006-08-09 15:56:46 -070075 switch(rule->action) {
76 case FR_ACT_TO_TBL:
77 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Steven Whitehousea8731cb2006-08-09 15:56:46 -070079 case FR_ACT_UNREACHABLE:
80 err = -ENETUNREACH;
81 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Steven Whitehousea8731cb2006-08-09 15:56:46 -070083 case FR_ACT_PROHIBIT:
84 err = -EACCES;
85 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Steven Whitehousea8731cb2006-08-09 15:56:46 -070087 case FR_ACT_BLACKHOLE:
88 default:
89 err = -EINVAL;
90 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
Steven Whitehousea8731cb2006-08-09 15:56:46 -070093 tbl = dn_fib_get_table(rule->table, 0);
94 if (tbl == NULL)
95 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -080096
David S. Millerbef55ae2011-03-12 17:17:10 -050097 err = tbl->lookup(tbl, fld, (struct dn_fib_res *)arg->result);
Steven Whitehousea8731cb2006-08-09 15:56:46 -070098 if (err > 0)
99 err = -EAGAIN;
100errout:
101 return err;
102}
103
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700104static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800105 FRA_GENERIC_POLICY,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700106};
107
108static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
109{
110 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
David S. Millerbef55ae2011-03-12 17:17:10 -0500111 struct flowidn *fld = &fl->u.dn;
112 __le16 daddr = fld->daddr;
113 __le16 saddr = fld->saddr;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700114
115 if (((saddr ^ r->src) & r->srcmask) ||
116 ((daddr ^ r->dst) & r->dstmask))
117 return 0;
118
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700119 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700122static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
Rami Rosen8b3521e2009-05-11 05:52:49 +0000123 struct fib_rule_hdr *frh,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700124 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700126 int err = -EINVAL;
127 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Thomas Grafe1701c62007-03-24 12:46:02 -0700129 if (frh->tos)
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700130 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800131
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700132 if (rule->table == RT_TABLE_UNSPEC) {
133 if (rule->action == FR_ACT_TO_TBL) {
134 struct dn_fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700136 table = dn_fib_empty_table();
137 if (table == NULL) {
138 err = -ENOBUFS;
139 goto errout;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700142 rule->table = table->n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 }
145
Thomas Grafe1701c62007-03-24 12:46:02 -0700146 if (frh->src_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000147 r->src = nla_get_le16(tb[FRA_SRC]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700148
Thomas Grafe1701c62007-03-24 12:46:02 -0700149 if (frh->dst_len)
Al Viro2835fdf2007-02-09 18:13:37 +0000150 r->dst = nla_get_le16(tb[FRA_DST]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700151
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700152 r->src_len = frh->src_len;
153 r->srcmask = dnet_make_mask(r->src_len);
154 r->dst_len = frh->dst_len;
155 r->dstmask = dnet_make_mask(r->dst_len);
156 err = 0;
157errout:
158 return err;
159}
160
161static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
162 struct nlattr **tb)
163{
164 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
165
166 if (frh->src_len && (r->src_len != frh->src_len))
167 return 0;
168
169 if (frh->dst_len && (r->dst_len != frh->dst_len))
170 return 0;
171
Thomas Grafe1701c62007-03-24 12:46:02 -0700172 if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700173 return 0;
174
Thomas Grafe1701c62007-03-24 12:46:02 -0700175 if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700176 return 0;
177
178 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Eric Dumazet95c96172012-04-15 05:58:06 +0000181unsigned int dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
David S. Millerbef55ae2011-03-12 17:17:10 -0500183 struct flowidn fld = { .daddr = addr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct dn_fib_res res;
Eric Dumazet95c96172012-04-15 05:58:06 +0000185 unsigned int ret = RTN_UNICAST;
Patrick McHardyabcab262006-08-10 23:11:47 -0700186 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 res.r = NULL;
189
190 if (tb) {
David S. Millerbef55ae2011-03-12 17:17:10 -0500191 if (!tb->lookup(tb, &fld, &res)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 ret = res.type;
193 dn_fib_res_put(&res);
194 }
195 }
196 return ret;
197}
198
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700199static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
Rami Rosen04af8cf2009-05-20 17:26:23 -0700200 struct fib_rule_hdr *frh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700202 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700204 frh->dst_len = r->dst_len;
205 frh->src_len = r->src_len;
206 frh->tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
David S. Millerb21dddb2012-04-01 20:15:14 -0400208 if ((r->dst_len &&
209 nla_put_le16(skb, FRA_DST, r->dst)) ||
210 (r->src_len &&
211 nla_put_le16(skb, FRA_SRC, r->src)))
212 goto nla_put_failure;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700213 return 0;
214
215nla_put_failure:
216 return -ENOBUFS;
217}
218
Denis V. Lunevae299fc2008-07-05 19:01:28 -0700219static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
Thomas Graf73417f62007-03-27 13:56:52 -0700220{
Thomas Graf4b19ca42007-03-28 14:18:52 -0700221 dn_rt_cache_flush(-1);
Thomas Graf73417f62007-03-27 13:56:52 -0700222}
223
Andi Kleen04a6f822012-10-04 17:12:11 -0700224static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200225 .family = AF_DECnet,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700226 .rule_size = sizeof(struct dn_fib_rule),
Thomas Grafe1701c62007-03-24 12:46:02 -0700227 .addr_size = sizeof(u16),
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700228 .action = dn_fib_rule_action,
229 .match = dn_fib_rule_match,
230 .configure = dn_fib_rule_configure,
231 .compare = dn_fib_rule_compare,
232 .fill = dn_fib_rule_fill,
Thomas Graf73417f62007-03-27 13:56:52 -0700233 .flush_cache = dn_fib_rule_flush_cache,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700234 .nlgroup = RTNLGRP_DECnet_RULE,
235 .policy = dn_fib_rule_policy,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700236 .owner = THIS_MODULE,
Denis V. Lunev03592382008-01-20 16:46:01 -0800237 .fro_net = &init_net,
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700238};
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240void __init dn_fib_rules_init(void)
241{
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800242 dn_fib_rules_ops =
243 fib_rules_register(&dn_fib_rules_ops_template, &init_net);
244 BUG_ON(IS_ERR(dn_fib_rules_ops));
245 BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff,
Denis V. Lunev2994c6382007-11-10 22:12:03 -0800246 RT_TABLE_MAIN, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249void __exit dn_fib_rules_cleanup(void)
250{
WANG Cong419df122015-03-31 11:01:46 -0700251 rtnl_lock();
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800252 fib_rules_unregister(dn_fib_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700253 rtnl_unlock();
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800254 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257