dn_rules.c 10.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

/*
 * 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:
 *
 */
#include <linux/string.h>
#include <linux/net.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/proc_fs.h>
#include <linux/netdevice.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
#include <linux/in_route.h>
29 30
#include <linux/list.h>
#include <linux/rcupdate.h>
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42
#include <asm/atomic.h>
#include <asm/uaccess.h>
#include <net/neighbour.h>
#include <net/dst.h>
#include <net/flow.h>
#include <net/dn.h>
#include <net/dn_fib.h>
#include <net/dn_neigh.h>
#include <net/dn_dev.h>

struct dn_fib_rule
{
43
	struct hlist_node	r_hlist;
L
Linus Torvalds 已提交
44 45 46 47 48 49
	atomic_t		r_clntref;
	u32			r_preference;
	unsigned char		r_table;
	unsigned char		r_action;
	unsigned char		r_dst_len;
	unsigned char		r_src_len;
50 51 52 53 54
	__le16			r_src;
	__le16			r_srcmask;
	__le16			r_dst;
	__le16			r_dstmask;
	__le16			r_srcmap;
L
Linus Torvalds 已提交
55 56 57 58 59 60 61
	u8			r_flags;
#ifdef CONFIG_DECNET_ROUTE_FWMARK
	u32			r_fwmark;
#endif
	int			r_ifindex;
	char			r_ifname[IFNAMSIZ];
	int			r_dead;
62
	struct rcu_head		rcu;
L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71
};

static struct dn_fib_rule default_rule = {
	.r_clntref =		ATOMIC_INIT(2),
	.r_preference =		0x7fff,
	.r_table =		RT_TABLE_MAIN,
	.r_action =		RTN_UNICAST
};

72
static struct hlist_head dn_fib_rules;
L
Linus Torvalds 已提交
73 74 75 76 77

int dn_fib_rtm_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
{
	struct rtattr **rta = arg;
	struct rtmsg *rtm = NLMSG_DATA(nlh);
78 79
	struct dn_fib_rule *r;
	struct hlist_node *node;
L
Linus Torvalds 已提交
80 81
	int err = -ESRCH;

82
	hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 2) == 0) &&
			rtm->rtm_src_len == r->r_src_len &&
			rtm->rtm_dst_len == r->r_dst_len &&
			(!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 2) == 0) &&
#ifdef CONFIG_DECNET_ROUTE_FWMARK
			(!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
#endif
			(!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
			(!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
			(!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) &&
			(!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {

			err = -EPERM;
			if (r == &default_rule)
				break;

99
			hlist_del_rcu(&r->r_hlist);
L
Linus Torvalds 已提交
100 101 102 103 104 105 106 107 108 109
			r->r_dead = 1;
			dn_fib_rule_put(r);
			err = 0;
			break;
		}
	}

	return err;
}

110 111 112 113 114 115
static inline void dn_fib_rule_put_rcu(struct rcu_head *head)
{
	struct dn_fib_rule *r = container_of(head, struct dn_fib_rule, rcu);
	kfree(r);
}

L
Linus Torvalds 已提交
116 117 118 119
void dn_fib_rule_put(struct dn_fib_rule *r)
{
	if (atomic_dec_and_test(&r->r_clntref)) {
		if (r->r_dead)
120
			call_rcu(&r->rcu, dn_fib_rule_put_rcu);
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129 130
		else
			printk(KERN_DEBUG "Attempt to free alive dn_fib_rule\n");
	}
}


int dn_fib_rtm_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
{
	struct rtattr **rta = arg;
	struct rtmsg *rtm = NLMSG_DATA(nlh);
131 132
	struct dn_fib_rule *r, *new_r, *last = NULL;
	struct hlist_node *node = NULL;
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	unsigned char table_id;

	if (rtm->rtm_src_len > 16 || rtm->rtm_dst_len > 16)
		return -EINVAL;

	if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
		return -EINVAL;

	if (rtm->rtm_type == RTN_NAT)
		return -EINVAL;

	table_id = rtm->rtm_table;
	if (table_id == RT_TABLE_UNSPEC) {
		struct dn_fib_table *tb;
		if (rtm->rtm_type == RTN_UNICAST) {
			if ((tb = dn_fib_empty_table()) == NULL)
				return -ENOBUFS;
			table_id = tb->n;
		}
	}

154
	new_r = kzalloc(sizeof(*new_r), GFP_KERNEL);
L
Linus Torvalds 已提交
155 156
	if (!new_r)
		return -ENOMEM;
157

L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	if (rta[RTA_SRC-1])
		memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 2);
	if (rta[RTA_DST-1])
		memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 2);
	if (rta[RTA_GATEWAY-1])
		memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 2);
	new_r->r_src_len = rtm->rtm_src_len;
	new_r->r_dst_len = rtm->rtm_dst_len;
	new_r->r_srcmask = dnet_make_mask(rtm->rtm_src_len);
	new_r->r_dstmask = dnet_make_mask(rtm->rtm_dst_len);
#ifdef CONFIG_DECNET_ROUTE_FWMARK
	if (rta[RTA_PROTOINFO-1])
		memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
#endif
	new_r->r_action = rtm->rtm_type;
	new_r->r_flags = rtm->rtm_flags;
	if (rta[RTA_PRIORITY-1])
		memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
	new_r->r_table = table_id;
	if (rta[RTA_IIF-1]) {
		struct net_device *dev;
		rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ);
		new_r->r_ifindex = -1;
		dev = dev_get_by_name(new_r->r_ifname);
		if (dev) {
			new_r->r_ifindex = dev->ifindex;
			dev_put(dev);
		}
	}

188
	r = container_of(dn_fib_rules.first, struct dn_fib_rule, r_hlist);
L
Linus Torvalds 已提交
189
	if (!new_r->r_preference) {
190 191
		if (r && r->r_hlist.next != NULL) {
			r = container_of(r->r_hlist.next, struct dn_fib_rule, r_hlist);
L
Linus Torvalds 已提交
192 193 194 195 196
			if (r->r_preference)
				new_r->r_preference = r->r_preference - 1;
		}
	}

197
	hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
L
Linus Torvalds 已提交
198 199
		if (r->r_preference > new_r->r_preference)
			break;
200
		last = r;
L
Linus Torvalds 已提交
201 202
	}
	atomic_inc(&new_r->r_clntref);
203 204 205 206 207

	if (last)
		hlist_add_after_rcu(&last->r_hlist, &new_r->r_hlist);
	else
		hlist_add_before_rcu(&new_r->r_hlist, &r->r_hlist);
L
Linus Torvalds 已提交
208 209 210 211 212 213 214 215
	return 0;
}


int dn_fib_lookup(const struct flowi *flp, struct dn_fib_res *res)
{
	struct dn_fib_rule *r, *policy;
	struct dn_fib_table *tb;
216 217
	__le16 saddr = flp->fld_src;
	__le16 daddr = flp->fld_dst;
218
	struct hlist_node *node;
L
Linus Torvalds 已提交
219 220
	int err;

221 222 223
	rcu_read_lock();

	hlist_for_each_entry_rcu(r, node, &dn_fib_rules, r_hlist) {
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237
		if (((saddr^r->r_src) & r->r_srcmask) ||
		    ((daddr^r->r_dst) & r->r_dstmask) ||
#ifdef CONFIG_DECNET_ROUTE_FWMARK
		    (r->r_fwmark && r->r_fwmark != flp->fld_fwmark) ||
#endif
		    (r->r_ifindex && r->r_ifindex != flp->iif))
			continue;

		switch(r->r_action) {
			case RTN_UNICAST:
			case RTN_NAT:
				policy = r;
				break;
			case RTN_UNREACHABLE:
238
				rcu_read_unlock();
L
Linus Torvalds 已提交
239 240 241
				return -ENETUNREACH;
			default:
			case RTN_BLACKHOLE:
242
				rcu_read_unlock();
L
Linus Torvalds 已提交
243 244
				return -EINVAL;
			case RTN_PROHIBIT:
245
				rcu_read_unlock();
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254 255
				return -EACCES;
		}

		if ((tb = dn_fib_get_table(r->r_table, 0)) == NULL)
			continue;
		err = tb->lookup(tb, flp, res);
		if (err == 0) {
			res->r = policy;
			if (policy)
				atomic_inc(&policy->r_clntref);
256
			rcu_read_unlock();
L
Linus Torvalds 已提交
257 258 259
			return 0;
		}
		if (err < 0 && err != -EAGAIN) {
260
			rcu_read_unlock();
L
Linus Torvalds 已提交
261 262 263 264
			return err;
		}
	}

265
	rcu_read_unlock();
L
Linus Torvalds 已提交
266 267 268
	return -ESRCH;
}

269
unsigned dnet_addr_type(__le16 addr)
L
Linus Torvalds 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
{
	struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
	struct dn_fib_res res;
	unsigned ret = RTN_UNICAST;
	struct dn_fib_table *tb = dn_fib_tables[RT_TABLE_LOCAL];

	res.r = NULL;

	if (tb) {
		if (!tb->lookup(tb, &fl, &res)) {
			ret = res.type;
			dn_fib_res_put(&res);
		}
	}
	return ret;
}

287
__le16 dn_fib_rules_policy(__le16 saddr, struct dn_fib_res *res, unsigned *flags)
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
{
	struct dn_fib_rule *r = res->r;

	if (r->r_action == RTN_NAT) {
		int addrtype = dnet_addr_type(r->r_srcmap);

		if (addrtype == RTN_NAT) {
			saddr = (saddr&~r->r_srcmask)|r->r_srcmap;
			*flags |= RTCF_SNAT;
		} else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) {
			saddr = r->r_srcmap;
			*flags |= RTCF_MASQ;
		}
	}
	return saddr;
}

static void dn_fib_rules_detach(struct net_device *dev)
{
307
	struct hlist_node *node;
L
Linus Torvalds 已提交
308 309
	struct dn_fib_rule *r;

310 311
	hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
		if (r->r_ifindex == dev->ifindex)
L
Linus Torvalds 已提交
312 313 314 315 316 317
			r->r_ifindex = -1;
	}
}

static void dn_fib_rules_attach(struct net_device *dev)
{
318
	struct hlist_node *node;
L
Linus Torvalds 已提交
319 320
	struct dn_fib_rule *r;

321 322
	hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
		if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0)
L
Linus Torvalds 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
			r->r_ifindex = dev->ifindex;
	}
}

static int dn_fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
{
	struct net_device *dev = ptr;

	switch(event) {
		case NETDEV_UNREGISTER:
			dn_fib_rules_detach(dev);
			dn_fib_sync_down(0, dev, 1);
		case NETDEV_REGISTER:
			dn_fib_rules_attach(dev);
			dn_fib_sync_up(dev);
	}

	return NOTIFY_DONE;
}


static struct notifier_block dn_fib_rules_notifier = {
	.notifier_call =	dn_fib_rules_event,
};

348 349
static int dn_fib_fill_rule(struct sk_buff *skb, struct dn_fib_rule *r,
			    struct netlink_callback *cb, unsigned int flags)
L
Linus Torvalds 已提交
350 351 352 353 354 355
{
	struct rtmsg *rtm;
	struct nlmsghdr *nlh;
	unsigned char *b = skb->tail;


356
	nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags);
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	rtm = NLMSG_DATA(nlh);
	rtm->rtm_family = AF_DECnet;
	rtm->rtm_dst_len = r->r_dst_len;
	rtm->rtm_src_len = r->r_src_len;
	rtm->rtm_tos = 0;
#ifdef CONFIG_DECNET_ROUTE_FWMARK
	if (r->r_fwmark)
		RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
#endif
	rtm->rtm_table = r->r_table;
	rtm->rtm_protocol = 0;
	rtm->rtm_scope = 0;
	rtm->rtm_type = r->r_action;
	rtm->rtm_flags = r->r_flags;

	if (r->r_dst_len)
		RTA_PUT(skb, RTA_DST, 2, &r->r_dst);
	if (r->r_src_len)
		RTA_PUT(skb, RTA_SRC, 2, &r->r_src);
	if (r->r_ifname[0])
		RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
	if (r->r_preference)
		RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
	if (r->r_srcmap)
		RTA_PUT(skb, RTA_GATEWAY, 2, &r->r_srcmap);
	nlh->nlmsg_len = skb->tail - b;
	return skb->len;

nlmsg_failure:
rtattr_failure:
	skb_trim(skb, b - skb->data);
	return -1;
}

int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
{
393
	int idx = 0;
L
Linus Torvalds 已提交
394 395
	int s_idx = cb->args[0];
	struct dn_fib_rule *r;
396
	struct hlist_node *node;
L
Linus Torvalds 已提交
397

398 399
	rcu_read_lock();
	hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
L
Linus Torvalds 已提交
400
		if (idx < s_idx)
401
			goto next;
402
		if (dn_fib_fill_rule(skb, r, cb, NLM_F_MULTI) < 0)
L
Linus Torvalds 已提交
403
			break;
404
next:
405
		idx++;
L
Linus Torvalds 已提交
406
	}
407
	rcu_read_unlock();
L
Linus Torvalds 已提交
408 409 410 411 412 413 414
	cb->args[0] = idx;

	return skb->len;
}

void __init dn_fib_rules_init(void)
{
415 416
	INIT_HLIST_HEAD(&dn_fib_rules);
	hlist_add_head(&default_rule.r_hlist, &dn_fib_rules);
L
Linus Torvalds 已提交
417 418 419 420 421 422 423 424 425
	register_netdevice_notifier(&dn_fib_rules_notifier);
}

void __exit dn_fib_rules_cleanup(void)
{
	unregister_netdevice_notifier(&dn_fib_rules_notifier);
}