dn_rules.c 5.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

/*
 * DECnet       An implementation of the DECnet protocol suite for the LINUX
 *              operating system.  DECnet is implemented using the  BSD Socket
 *              interface as the means of communication with the user level.
 *
 *              DECnet Routing Forwarding Information Base (Rules)
 *
 * Author:      Steve Whitehouse <SteveW@ACM.org>
 *              Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
 *
 *
 * Changes:
15 16
 *              Steve Whitehouse <steve@chygwyn.com>
 *              Updated for Thomas Graf's generic rules
L
Linus Torvalds 已提交
17 18 19 20 21 22 23 24
 *
 */
#include <linux/net.h>
#include <linux/init.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/netdevice.h>
#include <linux/spinlock.h>
25 26
#include <linux/list.h>
#include <linux/rcupdate.h>
27
#include <linux/export.h>
L
Linus Torvalds 已提交
28 29 30
#include <net/neighbour.h>
#include <net/dst.h>
#include <net/flow.h>
31
#include <net/fib_rules.h>
L
Linus Torvalds 已提交
32 33 34 35
#include <net/dn.h>
#include <net/dn_fib.h>
#include <net/dn_neigh.h>
#include <net/dn_dev.h>
36
#include <net/dn_route.h>
L
Linus Torvalds 已提交
37

38
static struct fib_rules_ops *dn_fib_rules_ops;
39

L
Linus Torvalds 已提交
40 41
struct dn_fib_rule
{
42 43 44 45 46 47 48 49 50
	struct fib_rule		common;
	unsigned char		dst_len;
	unsigned char		src_len;
	__le16			src;
	__le16			srcmask;
	__le16			dst;
	__le16			dstmask;
	__le16			srcmap;
	u8			flags;
L
Linus Torvalds 已提交
51 52 53
};


54
int dn_fib_lookup(struct flowidn *flp, struct dn_fib_res *res)
L
Linus Torvalds 已提交
55
{
56 57 58 59 60
	struct fib_lookup_arg arg = {
		.result = res,
	};
	int err;

61 62
	err = fib_rules_lookup(dn_fib_rules_ops,
			       flowidn_to_flowi(flp), 0, &arg);
63
	res->r = arg.rule;
L
Linus Torvalds 已提交
64 65 66 67

	return err;
}

A
Adrian Bunk 已提交
68 69
static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
			      int flags, struct fib_lookup_arg *arg)
70
{
71
	struct flowidn *fld = &flp->u.dn;
72 73
	int err = -EAGAIN;
	struct dn_fib_table *tbl;
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	switch(rule->action) {
	case FR_ACT_TO_TBL:
		break;

	case FR_ACT_UNREACHABLE:
		err = -ENETUNREACH;
		goto errout;

	case FR_ACT_PROHIBIT:
		err = -EACCES;
		goto errout;

	case FR_ACT_BLACKHOLE:
	default:
		err = -EINVAL;
		goto errout;
L
Linus Torvalds 已提交
91
	}
92 93 94 95 96

	tbl = dn_fib_get_table(rule->table, 0);
	if (tbl == NULL)
		goto errout;

97
	err = tbl->lookup(tbl, fld, (struct dn_fib_res *)arg->result);
98 99 100 101
	if (err > 0)
		err = -EAGAIN;
errout:
	return err;
L
Linus Torvalds 已提交
102 103
}

104
static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
105
	FRA_GENERIC_POLICY,
106
};
L
Linus Torvalds 已提交
107

108
static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
L
Linus Torvalds 已提交
109
{
110
	struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
111 112 113
	struct flowidn *fld = &fl->u.dn;
	__le16 daddr = fld->daddr;
	__le16 saddr = fld->saddr;
114 115 116 117

	if (((saddr ^ r->src) & r->srcmask) ||
	    ((daddr ^ r->dst) & r->dstmask))
		return 0;
L
Linus Torvalds 已提交
118

119 120 121 122
	return 1;
}

static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
123
				 struct fib_rule_hdr *frh,
124 125
				 struct nlattr **tb,
				 struct netlink_ext_ack *extack)
126 127 128 129
{
	int err = -EINVAL;
	struct dn_fib_rule *r = (struct dn_fib_rule *)rule;

130 131
	if (frh->tos) {
		NL_SET_ERR_MSG(extack, "Invalid tos value");
132
		goto  errout;
133
	}
134 135 136 137 138 139 140 141 142 143 144 145

	if (rule->table == RT_TABLE_UNSPEC) {
		if (rule->action == FR_ACT_TO_TBL) {
			struct dn_fib_table *table;

			table = dn_fib_empty_table();
			if (table == NULL) {
				err = -ENOBUFS;
				goto errout;
			}

			rule->table = table->n;
L
Linus Torvalds 已提交
146 147 148
		}
	}

149
	if (frh->src_len)
150
		r->src = nla_get_le16(tb[FRA_SRC]);
151

152
	if (frh->dst_len)
153
		r->dst = nla_get_le16(tb[FRA_DST]);
L
Linus Torvalds 已提交
154

155 156 157 158 159 160 161 162
	r->src_len = frh->src_len;
	r->srcmask = dnet_make_mask(r->src_len);
	r->dst_len = frh->dst_len;
	r->dstmask = dnet_make_mask(r->dst_len);
	err = 0;
errout:
	return err;
}
L
Linus Torvalds 已提交
163

164 165
static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
			       struct nlattr **tb)
L
Linus Torvalds 已提交
166
{
167 168 169 170
	struct dn_fib_rule *r = (struct dn_fib_rule *)rule;

	if (frh->src_len && (r->src_len != frh->src_len))
		return 0;
L
Linus Torvalds 已提交
171

172 173
	if (frh->dst_len && (r->dst_len != frh->dst_len))
		return 0;
174

175
	if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
176 177
		return 0;

178
	if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
179
		return 0;
L
Linus Torvalds 已提交
180

181
	return 1;
L
Linus Torvalds 已提交
182 183
}

184
unsigned int dnet_addr_type(__le16 addr)
L
Linus Torvalds 已提交
185
{
186
	struct flowidn fld = { .daddr = addr };
L
Linus Torvalds 已提交
187
	struct dn_fib_res res;
188
	unsigned int ret = RTN_UNICAST;
189
	struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
L
Linus Torvalds 已提交
190 191 192 193

	res.r = NULL;

	if (tb) {
194
		if (!tb->lookup(tb, &fld, &res)) {
L
Linus Torvalds 已提交
195 196 197 198 199 200 201
			ret = res.type;
			dn_fib_res_put(&res);
		}
	}
	return ret;
}

202
static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
203
			    struct fib_rule_hdr *frh)
L
Linus Torvalds 已提交
204
{
205
	struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
L
Linus Torvalds 已提交
206

207 208 209
	frh->dst_len = r->dst_len;
	frh->src_len = r->src_len;
	frh->tos = 0;
L
Linus Torvalds 已提交
210

D
David S. Miller 已提交
211 212 213 214 215
	if ((r->dst_len &&
	     nla_put_le16(skb, FRA_DST, r->dst)) ||
	    (r->src_len &&
	     nla_put_le16(skb, FRA_SRC, r->src)))
		goto nla_put_failure;
216
	return 0;
L
Linus Torvalds 已提交
217

218 219
nla_put_failure:
	return -ENOBUFS;
L
Linus Torvalds 已提交
220 221
}

222
static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
223
{
224
	dn_rt_cache_flush(-1);
225 226
}

227
static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template = {
228
	.family		= AF_DECnet,
229
	.rule_size	= sizeof(struct dn_fib_rule),
230
	.addr_size	= sizeof(u16),
231 232 233 234 235
	.action		= dn_fib_rule_action,
	.match		= dn_fib_rule_match,
	.configure	= dn_fib_rule_configure,
	.compare	= dn_fib_rule_compare,
	.fill		= dn_fib_rule_fill,
236
	.flush_cache	= dn_fib_rule_flush_cache,
237 238 239
	.nlgroup	= RTNLGRP_DECnet_RULE,
	.policy		= dn_fib_rule_policy,
	.owner		= THIS_MODULE,
240
	.fro_net	= &init_net,
241 242
};

L
Linus Torvalds 已提交
243 244
void __init dn_fib_rules_init(void)
{
245 246 247 248
	dn_fib_rules_ops =
		fib_rules_register(&dn_fib_rules_ops_template, &init_net);
	BUG_ON(IS_ERR(dn_fib_rules_ops));
	BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff,
249
			            RT_TABLE_MAIN, 0));
L
Linus Torvalds 已提交
250 251 252 253
}

void __exit dn_fib_rules_cleanup(void)
{
254
	rtnl_lock();
255
	fib_rules_unregister(dn_fib_rules_ops);
256
	rtnl_unlock();
257
	rcu_barrier();
L
Linus Torvalds 已提交
258
}