dn_route.c 47.4 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * 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 Functions (Endnode and Router)
 *
 * Authors:     Steve Whitehouse <SteveW@ACM.org>
 *              Eduardo Marcelo Serrat <emserrat@geocities.com>
 *
 * Changes:
 *              Steve Whitehouse : Fixes to allow "intra-ethernet" and
 *                                 "return-to-sender" bits on outgoing
 *                                 packets.
 *		Steve Whitehouse : Timeouts for cached routes.
 *              Steve Whitehouse : Use dst cache for input routes too.
 *              Steve Whitehouse : Fixed error values in dn_send_skb.
 *              Steve Whitehouse : Rework routing functions to better fit
 *                                 DECnet routing design
 *              Alexey Kuznetsov : New SMP locking
 *              Steve Whitehouse : More SMP locking changes & dn_cache_dump()
 *              Steve Whitehouse : Prerouting NF hook, now really is prerouting.
 *				   Fixed possible skb leak in rtnetlink funcs.
 *              Steve Whitehouse : Dave Miller's dynamic hash table sizing and
 *                                 Alexey Kuznetsov's finer grained locking
 *                                 from ipv4/route.c.
 *              Steve Whitehouse : Routing is now starting to look like a
 *                                 sensible set of code now, mainly due to
 *                                 my copying the IPv4 routing code. The
 *                                 hooks here are modified and will continue
 *                                 to evolve for a while.
 *              Steve Whitehouse : Real SMP at last :-) Also new netfilter
 *                                 stuff. Look out raw sockets your days
 *                                 are numbered!
 *              Steve Whitehouse : Added return-to-sender functions. Added
 *                                 backlog congestion level return codes.
 *		Steve Whitehouse : Fixed bug where routes were set up with
 *                                 no ref count on net devices.
 *              Steve Whitehouse : RCU for the route cache
 *              Steve Whitehouse : Preparations for the flow cache
 *              Steve Whitehouse : Prepare for nonlinear skbs
 */

/******************************************************************************
    (c) 1995-1998 E.M. Serrat		emserrat@geocities.com
46

L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*******************************************************************************/

#include <linux/errno.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/kernel.h>
#include <linux/sockios.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/inet.h>
#include <linux/route.h>
#include <linux/in_route.h>
69
#include <linux/slab.h>
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79
#include <net/sock.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/init.h>
#include <linux/rtnetlink.h>
#include <linux/string.h>
#include <linux/netfilter_decnet.h>
#include <linux/rcupdate.h>
#include <linux/times.h>
80
#include <linux/export.h>
L
Linus Torvalds 已提交
81
#include <asm/errno.h>
82
#include <net/net_namespace.h>
83
#include <net/netlink.h>
L
Linus Torvalds 已提交
84 85 86
#include <net/neighbour.h>
#include <net/dst.h>
#include <net/flow.h>
87
#include <net/fib_rules.h>
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96
#include <net/dn.h>
#include <net/dn_dev.h>
#include <net/dn_nsp.h>
#include <net/dn_route.h>
#include <net/dn_neigh.h>
#include <net/dn_fib.h>

struct dn_rt_hash_bucket
{
97
	struct dn_route __rcu *chain;
L
Linus Torvalds 已提交
98
	spinlock_t lock;
99
};
L
Linus Torvalds 已提交
100 101 102 103 104 105 106 107 108 109 110 111

extern struct neigh_table dn_neigh_table;


static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};

static const int dn_rt_min_delay = 2 * HZ;
static const int dn_rt_max_delay = 10 * HZ;
static const int dn_rt_mtu_expires = 10 * 60 * HZ;

static unsigned long dn_rt_deadline;

112
static int dn_dst_gc(struct dst_ops *ops);
L
Linus Torvalds 已提交
113
static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);
114
static unsigned int dn_dst_default_advmss(const struct dst_entry *dst);
115
static unsigned int dn_dst_mtu(const struct dst_entry *dst);
116
static void dn_dst_destroy(struct dst_entry *);
117
static void dn_dst_ifdown(struct dst_entry *, struct net_device *dev, int how);
L
Linus Torvalds 已提交
118 119
static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
static void dn_dst_link_failure(struct sk_buff *);
120 121 122 123
static void dn_dst_update_pmtu(struct dst_entry *dst, struct sock *sk,
			       struct sk_buff *skb , u32 mtu);
static void dn_dst_redirect(struct dst_entry *dst, struct sock *sk,
			    struct sk_buff *skb);
124 125 126
static struct neighbour *dn_dst_neigh_lookup(const struct dst_entry *dst,
					     struct sk_buff *skb,
					     const void *daddr);
L
Linus Torvalds 已提交
127 128 129 130
static int dn_route_input(struct sk_buff *);
static void dn_run_flush(unsigned long dummy);

static struct dn_rt_hash_bucket *dn_rt_hash_table;
131
static unsigned int dn_rt_hash_mask;
L
Linus Torvalds 已提交
132 133

static struct timer_list dn_route_timer;
134
static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
L
Linus Torvalds 已提交
135 136 137 138 139 140 141
int decnet_dst_gc_interval = 2;

static struct dst_ops dn_dst_ops = {
	.family =		PF_DECnet,
	.gc_thresh =		128,
	.gc =			dn_dst_gc,
	.check =		dn_dst_check,
142
	.default_advmss =	dn_dst_default_advmss,
143
	.mtu =			dn_dst_mtu,
144 145
	.cow_metrics =		dst_cow_metrics_generic,
	.destroy =		dn_dst_destroy,
146
	.ifdown =		dn_dst_ifdown,
L
Linus Torvalds 已提交
147 148 149
	.negative_advice =	dn_dst_negative_advice,
	.link_failure =		dn_dst_link_failure,
	.update_pmtu =		dn_dst_update_pmtu,
150
	.redirect =		dn_dst_redirect,
151
	.neigh_lookup =		dn_dst_neigh_lookup,
L
Linus Torvalds 已提交
152 153
};

154 155
static void dn_dst_destroy(struct dst_entry *dst)
{
156 157 158 159
	struct dn_route *rt = (struct dn_route *) dst;

	if (rt->n)
		neigh_release(rt->n);
160 161 162
	dst_destroy_metrics_generic(dst);
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176
static void dn_dst_ifdown(struct dst_entry *dst, struct net_device *dev, int how)
{
	if (how) {
		struct dn_route *rt = (struct dn_route *) dst;
		struct neighbour *n = rt->n;

		if (n && n->dev == dev) {
			n->dev = dev_net(dev)->loopback_dev;
			dev_hold(n->dev);
			dev_put(dev);
		}
	}
}

177
static __inline__ unsigned int dn_hash(__le16 src, __le16 dst)
L
Linus Torvalds 已提交
178
{
179
	__u16 tmp = (__u16 __force)(src ^ dst);
L
Linus Torvalds 已提交
180 181 182
	tmp ^= (tmp >> 3);
	tmp ^= (tmp >> 5);
	tmp ^= (tmp >> 10);
183
	return dn_rt_hash_mask & (unsigned int)tmp;
L
Linus Torvalds 已提交
184 185 186 187 188
}

static void dn_dst_check_expire(unsigned long dummy)
{
	int i;
189 190
	struct dn_route *rt;
	struct dn_route __rcu **rtp;
L
Linus Torvalds 已提交
191 192 193
	unsigned long now = jiffies;
	unsigned long expire = 120 * HZ;

194
	for (i = 0; i <= dn_rt_hash_mask; i++) {
L
Linus Torvalds 已提交
195 196 197
		rtp = &dn_rt_hash_table[i].chain;

		spin_lock(&dn_rt_hash_table[i].lock);
198 199
		while ((rt = rcu_dereference_protected(*rtp,
						lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
200 201
			if (atomic_read(&rt->dst.__refcnt) > 1 ||
			    (now - rt->dst.lastuse) < expire) {
202
				rtp = &rt->dst.dn_next;
L
Linus Torvalds 已提交
203 204
				continue;
			}
205 206
			*rtp = rt->dst.dn_next;
			rt->dst.dn_next = NULL;
207 208
			dst_dev_put(&rt->dst);
			dst_release(&rt->dst);
L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217 218
		}
		spin_unlock(&dn_rt_hash_table[i].lock);

		if ((jiffies - now) > 0)
			break;
	}

	mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
}

219
static int dn_dst_gc(struct dst_ops *ops)
L
Linus Torvalds 已提交
220
{
221 222
	struct dn_route *rt;
	struct dn_route __rcu **rtp;
L
Linus Torvalds 已提交
223 224 225 226
	int i;
	unsigned long now = jiffies;
	unsigned long expire = 10 * HZ;

227
	for (i = 0; i <= dn_rt_hash_mask; i++) {
L
Linus Torvalds 已提交
228 229 230 231

		spin_lock_bh(&dn_rt_hash_table[i].lock);
		rtp = &dn_rt_hash_table[i].chain;

232 233
		while ((rt = rcu_dereference_protected(*rtp,
						lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
234 235
			if (atomic_read(&rt->dst.__refcnt) > 1 ||
			    (now - rt->dst.lastuse) < expire) {
236
				rtp = &rt->dst.dn_next;
L
Linus Torvalds 已提交
237 238
				continue;
			}
239 240
			*rtp = rt->dst.dn_next;
			rt->dst.dn_next = NULL;
241 242
			dst_dev_put(&rt->dst);
			dst_release(&rt->dst);
L
Linus Torvalds 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
			break;
		}
		spin_unlock_bh(&dn_rt_hash_table[i].lock);
	}

	return 0;
}

/*
 * The decnet standards don't impose a particular minimum mtu, what they
 * do insist on is that the routing layer accepts a datagram of at least
 * 230 bytes long. Here we have to subtract the routing header length from
 * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
 * assume the worst and use a long header size.
 *
 * We update both the mtu and the advertised mss (i.e. the segment size we
 * advertise to the other end).
 */
261 262
static void dn_dst_update_pmtu(struct dst_entry *dst, struct sock *sk,
			       struct sk_buff *skb, u32 mtu)
L
Linus Torvalds 已提交
263
{
264 265
	struct dn_route *rt = (struct dn_route *) dst;
	struct neighbour *n = rt->n;
L
Linus Torvalds 已提交
266
	u32 min_mtu = 230;
267 268 269
	struct dn_dev *dn;

	dn = n ? rcu_dereference_raw(n->dev->dn_ptr) : NULL;
L
Linus Torvalds 已提交
270 271 272 273 274 275

	if (dn && dn->use_long == 0)
		min_mtu -= 6;
	else
		min_mtu -= 21;

276
	if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
L
Linus Torvalds 已提交
277
		if (!(dst_metric_locked(dst, RTAX_MTU))) {
278
			dst_metric_set(dst, RTAX_MTU, mtu);
L
Linus Torvalds 已提交
279 280 281 282
			dst_set_expires(dst, dn_rt_mtu_expires);
		}
		if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
			u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
283 284
			u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
			if (!existing_mss || existing_mss > mss)
285
				dst_metric_set(dst, RTAX_ADVMSS, mss);
L
Linus Torvalds 已提交
286 287 288 289
		}
	}
}

290 291
static void dn_dst_redirect(struct dst_entry *dst, struct sock *sk,
			    struct sk_buff *skb)
292 293 294
{
}

295
/*
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
 * When a route has been marked obsolete. (e.g. routing cache flush)
 */
static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
{
	return NULL;
}

static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
{
	dst_release(dst);
	return NULL;
}

static void dn_dst_link_failure(struct sk_buff *skb)
{
}

313
static inline int compare_keys(struct flowidn *fl1, struct flowidn *fl2)
L
Linus Torvalds 已提交
314
{
315 316 317 318 319 320
	return ((fl1->daddr ^ fl2->daddr) |
		(fl1->saddr ^ fl2->saddr) |
		(fl1->flowidn_mark ^ fl2->flowidn_mark) |
		(fl1->flowidn_scope ^ fl2->flowidn_scope) |
		(fl1->flowidn_oif ^ fl2->flowidn_oif) |
		(fl1->flowidn_iif ^ fl2->flowidn_iif)) == 0;
L
Linus Torvalds 已提交
321 322
}

323
static int dn_insert_route(struct dn_route *rt, unsigned int hash, struct dn_route **rp)
L
Linus Torvalds 已提交
324
{
325 326
	struct dn_route *rth;
	struct dn_route __rcu **rthp;
L
Linus Torvalds 已提交
327 328 329 330 331
	unsigned long now = jiffies;

	rthp = &dn_rt_hash_table[hash].chain;

	spin_lock_bh(&dn_rt_hash_table[hash].lock);
332 333
	while ((rth = rcu_dereference_protected(*rthp,
						lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
334
		if (compare_keys(&rth->fld, &rt->fld)) {
L
Linus Torvalds 已提交
335
			/* Put it first */
336 337
			*rthp = rth->dst.dn_next;
			rcu_assign_pointer(rth->dst.dn_next,
L
Linus Torvalds 已提交
338 339 340
					   dn_rt_hash_table[hash].chain);
			rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);

341
			dst_use(&rth->dst, now);
L
Linus Torvalds 已提交
342 343
			spin_unlock_bh(&dn_rt_hash_table[hash].lock);

344
			dst_release_immediate(&rt->dst);
L
Linus Torvalds 已提交
345 346 347
			*rp = rth;
			return 0;
		}
348
		rthp = &rth->dst.dn_next;
L
Linus Torvalds 已提交
349 350
	}

351
	rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
L
Linus Torvalds 已提交
352
	rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
353

354
	dst_use(&rt->dst, now);
L
Linus Torvalds 已提交
355 356 357 358 359
	spin_unlock_bh(&dn_rt_hash_table[hash].lock);
	*rp = rt;
	return 0;
}

R
Roel Kluin 已提交
360
static void dn_run_flush(unsigned long dummy)
L
Linus Torvalds 已提交
361 362 363 364
{
	int i;
	struct dn_route *rt, *next;

365
	for (i = 0; i < dn_rt_hash_mask; i++) {
L
Linus Torvalds 已提交
366 367
		spin_lock_bh(&dn_rt_hash_table[i].lock);

368
		if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
L
Linus Torvalds 已提交
369 370
			goto nothing_to_declare;

371 372 373
		for(; rt; rt = next) {
			next = rcu_dereference_raw(rt->dst.dn_next);
			RCU_INIT_POINTER(rt->dst.dn_next, NULL);
374 375
			dst_dev_put(&rt->dst);
			dst_release(&rt->dst);
L
Linus Torvalds 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
		}

nothing_to_declare:
		spin_unlock_bh(&dn_rt_hash_table[i].lock);
	}
}

static DEFINE_SPINLOCK(dn_rt_flush_lock);

void dn_rt_cache_flush(int delay)
{
	unsigned long now = jiffies;
	int user_mode = !in_interrupt();

	if (delay < 0)
		delay = dn_rt_min_delay;

	spin_lock_bh(&dn_rt_flush_lock);

	if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
		long tmo = (long)(dn_rt_deadline - now);

		if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
			tmo = 0;

		if (delay > tmo)
			delay = tmo;
	}

	if (delay <= 0) {
		spin_unlock_bh(&dn_rt_flush_lock);
		dn_run_flush(0);
		return;
	}

	if (dn_rt_deadline == 0)
		dn_rt_deadline = now + dn_rt_max_delay;

	dn_rt_flush_timer.expires = now + delay;
	add_timer(&dn_rt_flush_timer);
	spin_unlock_bh(&dn_rt_flush_lock);
}

/**
 * dn_return_short - Return a short packet to its sender
 * @skb: The packet to return
 *
 */
static int dn_return_short(struct sk_buff *skb)
{
	struct dn_skb_cb *cb;
	unsigned char *ptr;
428 429
	__le16 *src;
	__le16 *dst;
L
Linus Torvalds 已提交
430 431

	/* Add back headers */
432
	skb_push(skb, skb->data - skb_network_header(skb));
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440 441

	if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
		return NET_RX_DROP;

	cb = DN_SKB_CB(skb);
	/* Skip packet length and point to flags */
	ptr = skb->data + 2;
	*ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;

442
	dst = (__le16 *)ptr;
L
Linus Torvalds 已提交
443
	ptr += 2;
444
	src = (__le16 *)ptr;
L
Linus Torvalds 已提交
445 446 447
	ptr += 2;
	*ptr = 0; /* Zero hop count */

448
	swap(*src, *dst);
L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

	skb->pkt_type = PACKET_OUTGOING;
	dn_rt_finish_output(skb, NULL, NULL);
	return NET_RX_SUCCESS;
}

/**
 * dn_return_long - Return a long packet to its sender
 * @skb: The long format packet to return
 *
 */
static int dn_return_long(struct sk_buff *skb)
{
	struct dn_skb_cb *cb;
	unsigned char *ptr;
	unsigned char *src_addr, *dst_addr;
	unsigned char tmp[ETH_ALEN];

	/* Add back all headers */
468
	skb_push(skb, skb->data - skb_network_header(skb));
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

	if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
		return NET_RX_DROP;

	cb = DN_SKB_CB(skb);
	/* Ignore packet length and point to flags */
	ptr = skb->data + 2;

	/* Skip padding */
	if (*ptr & DN_RT_F_PF) {
		char padlen = (*ptr & ~DN_RT_F_PF);
		ptr += padlen;
	}

	*ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
	ptr += 2;
	dst_addr = ptr;
	ptr += 8;
	src_addr = ptr;
	ptr += 6;
	*ptr = 0; /* Zero hop count */

	/* Swap source and destination */
	memcpy(tmp, src_addr, ETH_ALEN);
	memcpy(src_addr, dst_addr, ETH_ALEN);
	memcpy(dst_addr, tmp, ETH_ALEN);

	skb->pkt_type = PACKET_OUTGOING;
	dn_rt_finish_output(skb, dst_addr, src_addr);
	return NET_RX_SUCCESS;
}

/**
 * dn_route_rx_packet - Try and find a route for an incoming packet
 * @skb: The packet to find a route for
 *
 * Returns: result of input function if route is found, error code otherwise
 */
507
static int dn_route_rx_packet(struct net *net, struct sock *sk, struct sk_buff *skb)
L
Linus Torvalds 已提交
508
{
509
	struct dn_skb_cb *cb;
L
Linus Torvalds 已提交
510 511 512 513 514
	int err;

	if ((err = dn_route_input(skb)) == 0)
		return dst_input(skb);

515
	cb = DN_SKB_CB(skb);
L
Linus Torvalds 已提交
516 517
	if (decnet_debug_level & 4) {
		char *devname = skb->dev ? skb->dev->name : "???";
518

L
Linus Torvalds 已提交
519 520
		printk(KERN_DEBUG
			"DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%d\n",
521
			(int)cb->rt_flags, devname, skb->len,
522
			le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
L
Linus Torvalds 已提交
523 524 525 526
			err, skb->pkt_type);
	}

	if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
J
Joe Perches 已提交
527 528 529 530 531
		switch (cb->rt_flags & DN_RT_PKT_MSK) {
		case DN_RT_PKT_SHORT:
			return dn_return_short(skb);
		case DN_RT_PKT_LONG:
			return dn_return_long(skb);
L
Linus Torvalds 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
		}
	}

	kfree_skb(skb);
	return NET_RX_DROP;
}

static int dn_route_rx_long(struct sk_buff *skb)
{
	struct dn_skb_cb *cb = DN_SKB_CB(skb);
	unsigned char *ptr = skb->data;

	if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
		goto drop_it;

	skb_pull(skb, 20);
548
	skb_reset_transport_header(skb);
L
Linus Torvalds 已提交
549

550 551
	/* Destination info */
	ptr += 2;
552
	cb->dst = dn_eth2dn(ptr);
553 554 555
	if (memcmp(ptr, dn_hiord_addr, 4) != 0)
		goto drop_it;
	ptr += 6;
L
Linus Torvalds 已提交
556 557


558 559
	/* Source info */
	ptr += 2;
560
	cb->src = dn_eth2dn(ptr);
561 562 563 564 565 566
	if (memcmp(ptr, dn_hiord_addr, 4) != 0)
		goto drop_it;
	ptr += 6;
	/* Other junk */
	ptr++;
	cb->hops = *ptr++; /* Visit Count */
L
Linus Torvalds 已提交
567

568 569
	return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING,
		       &init_net, NULL, skb, skb->dev, NULL,
570
		       dn_route_rx_packet);
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

drop_it:
	kfree_skb(skb);
	return NET_RX_DROP;
}



static int dn_route_rx_short(struct sk_buff *skb)
{
	struct dn_skb_cb *cb = DN_SKB_CB(skb);
	unsigned char *ptr = skb->data;

	if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
		goto drop_it;

	skb_pull(skb, 5);
588
	skb_reset_transport_header(skb);
L
Linus Torvalds 已提交
589

590
	cb->dst = *(__le16 *)ptr;
591 592 593 594
	ptr += 2;
	cb->src = *(__le16 *)ptr;
	ptr += 2;
	cb->hops = *ptr & 0x3f;
L
Linus Torvalds 已提交
595

596 597
	return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING,
		       &init_net, NULL, skb, skb->dev, NULL,
598
		       dn_route_rx_packet);
L
Linus Torvalds 已提交
599 600

drop_it:
601 602
	kfree_skb(skb);
	return NET_RX_DROP;
L
Linus Torvalds 已提交
603 604
}

605
static int dn_route_discard(struct net *net, struct sock *sk, struct sk_buff *skb)
L
Linus Torvalds 已提交
606 607 608 609 610 611 612 613 614
{
	/*
	 * I know we drop the packet here, but thats considered success in
	 * this case
	 */
	kfree_skb(skb);
	return NET_RX_SUCCESS;
}

615
static int dn_route_ptp_hello(struct net *net, struct sock *sk, struct sk_buff *skb)
L
Linus Torvalds 已提交
616 617 618 619 620 621
{
	dn_dev_hello(skb);
	dn_neigh_pointopoint_hello(skb);
	return NET_RX_SUCCESS;
}

D
David S. Miller 已提交
622
int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
L
Linus Torvalds 已提交
623 624 625
{
	struct dn_skb_cb *cb;
	unsigned char flags = 0;
626
	__u16 len = le16_to_cpu(*(__le16 *)skb->data);
627
	struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
L
Linus Torvalds 已提交
628 629
	unsigned char padlen = 0;

630
	if (!net_eq(dev_net(dev), &init_net))
631 632
		goto dump_it;

L
Linus Torvalds 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
	if (dn == NULL)
		goto dump_it;

	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
		goto out;

	if (!pskb_may_pull(skb, 3))
		goto dump_it;

	skb_pull(skb, 2);

	if (len > skb->len)
		goto dump_it;

	skb_trim(skb, len);

	flags = *skb->data;

	cb = DN_SKB_CB(skb);
	cb->stamp = jiffies;
	cb->iif = dev->ifindex;

	/*
	 * If we have padding, remove it.
	 */
	if (flags & DN_RT_F_PF) {
		padlen = flags & ~DN_RT_F_PF;
		if (!pskb_may_pull(skb, padlen + 1))
			goto dump_it;
		skb_pull(skb, padlen);
		flags = *skb->data;
	}

666
	skb_reset_network_header(skb);
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675 676

	/*
	 * Weed out future version DECnet
	 */
	if (flags & DN_RT_F_VER)
		goto dump_it;

	cb->rt_flags = flags;

	if (decnet_debug_level & 1)
677
		printk(KERN_DEBUG
L
Linus Torvalds 已提交
678
			"dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
679
			(int)flags, (dev) ? dev->name : "???", len, skb->len,
L
Linus Torvalds 已提交
680 681
			padlen);

682
	if (flags & DN_RT_PKT_CNTL) {
H
Herbert Xu 已提交
683
		if (unlikely(skb_linearize(skb)))
L
Linus Torvalds 已提交
684 685
			goto dump_it;

J
Joe Perches 已提交
686 687 688 689 690 691 692
		switch (flags & DN_RT_CNTL_MSK) {
		case DN_RT_PKT_INIT:
			dn_dev_init_pkt(skb);
			break;
		case DN_RT_PKT_VERI:
			dn_dev_veri_pkt(skb);
			break;
L
Linus Torvalds 已提交
693 694 695 696 697
		}

		if (dn->parms.state != DN_DEV_S_RU)
			goto dump_it;

J
Joe Perches 已提交
698 699 700
		switch (flags & DN_RT_CNTL_MSK) {
		case DN_RT_PKT_HELO:
			return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
701
				       &init_net, NULL, skb, skb->dev, NULL,
J
Joe Perches 已提交
702 703 704 705 706
				       dn_route_ptp_hello);

		case DN_RT_PKT_L1RT:
		case DN_RT_PKT_L2RT:
			return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
707
				       &init_net, NULL, skb, skb->dev, NULL,
J
Joe Perches 已提交
708 709 710
				       dn_route_discard);
		case DN_RT_PKT_ERTH:
			return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
711
				       &init_net, NULL, skb, skb->dev, NULL,
J
Joe Perches 已提交
712 713 714 715
				       dn_neigh_router_hello);

		case DN_RT_PKT_EEDH:
			return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
716
				       &init_net, NULL, skb, skb->dev, NULL,
J
Joe Perches 已提交
717
				       dn_neigh_endnode_hello);
718 719
		}
	} else {
L
Linus Torvalds 已提交
720 721 722 723 724
		if (dn->parms.state != DN_DEV_S_RU)
			goto dump_it;

		skb_pull(skb, 1); /* Pull flags */

J
Joe Perches 已提交
725 726 727 728 729
		switch (flags & DN_RT_PKT_MSK) {
		case DN_RT_PKT_LONG:
			return dn_route_rx_long(skb);
		case DN_RT_PKT_SHORT:
			return dn_route_rx_short(skb);
L
Linus Torvalds 已提交
730
		}
731
	}
L
Linus Torvalds 已提交
732 733 734 735 736 737 738

dump_it:
	kfree_skb(skb);
out:
	return NET_RX_DROP;
}

E
Eric W. Biederman 已提交
739
static int dn_output(struct net *net, struct sock *sk, struct sk_buff *skb)
L
Linus Torvalds 已提交
740
{
E
Eric Dumazet 已提交
741
	struct dst_entry *dst = skb_dst(skb);
L
Linus Torvalds 已提交
742 743 744 745 746 747
	struct dn_route *rt = (struct dn_route *)dst;
	struct net_device *dev = dst->dev;
	struct dn_skb_cb *cb = DN_SKB_CB(skb);

	int err = -EINVAL;

748
	if (rt->n == NULL)
L
Linus Torvalds 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
		goto error;

	skb->dev = dev;

	cb->src = rt->rt_saddr;
	cb->dst = rt->rt_daddr;

	/*
	 * Always set the Intra-Ethernet bit on all outgoing packets
	 * originated on this node. Only valid flag from upper layers
	 * is return-to-sender-requested. Set hop count to 0 too.
	 */
	cb->rt_flags &= ~DN_RT_F_RQR;
	cb->rt_flags |= DN_RT_F_IE;
	cb->hops = 0;

765 766
	return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT,
		       &init_net, sk, skb, NULL, dev,
767
		       dn_to_neigh_output);
L
Linus Torvalds 已提交
768 769

error:
770
	net_dbg_ratelimited("dn_output: This should not happen\n");
L
Linus Torvalds 已提交
771 772 773 774 775 776 777 778 779

	kfree_skb(skb);

	return err;
}

static int dn_forward(struct sk_buff *skb)
{
	struct dn_skb_cb *cb = DN_SKB_CB(skb);
E
Eric Dumazet 已提交
780
	struct dst_entry *dst = skb_dst(skb);
781
	struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
L
Linus Torvalds 已提交
782 783 784 785 786 787 788 789
	struct dn_route *rt;
	int header_len;
	struct net_device *dev = skb->dev;

	if (skb->pkt_type != PACKET_HOST)
		goto drop;

	/* Ensure that we have enough space for headers */
E
Eric Dumazet 已提交
790
	rt = (struct dn_route *)skb_dst(skb);
L
Linus Torvalds 已提交
791
	header_len = dn_db->use_long ? 21 : 6;
792
	if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
L
Linus Torvalds 已提交
793 794 795 796 797 798 799 800
		goto drop;

	/*
	 * Hop count exceeded.
	 */
	if (++cb->hops > 30)
		goto drop;

801
	skb->dev = rt->dst.dev;
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811

	/*
	 * If packet goes out same interface it came in on, then set
	 * the Intra-Ethernet bit. This has no effect for short
	 * packets, so we don't need to test for them here.
	 */
	cb->rt_flags &= ~DN_RT_F_IE;
	if (rt->rt_flags & RTCF_DOREDIRECT)
		cb->rt_flags |= DN_RT_F_IE;

812 813
	return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD,
		       &init_net, NULL, skb, dev, skb->dev,
814
		       dn_to_neigh_output);
L
Linus Torvalds 已提交
815 816 817 818 819 820 821 822 823 824

drop:
	kfree_skb(skb);
	return NET_RX_DROP;
}

/*
 * Used to catch bugs. This should never normally get
 * called.
 */
E
Eric W. Biederman 已提交
825
static int dn_rt_bug_out(struct net *net, struct sock *sk, struct sk_buff *skb)
826 827 828 829 830 831 832 833 834 835 836
{
	struct dn_skb_cb *cb = DN_SKB_CB(skb);

	net_dbg_ratelimited("dn_rt_bug: skb from:%04x to:%04x\n",
			    le16_to_cpu(cb->src), le16_to_cpu(cb->dst));

	kfree_skb(skb);

	return NET_RX_DROP;
}

L
Linus Torvalds 已提交
837 838
static int dn_rt_bug(struct sk_buff *skb)
{
839
	struct dn_skb_cb *cb = DN_SKB_CB(skb);
L
Linus Torvalds 已提交
840

841 842
	net_dbg_ratelimited("dn_rt_bug: skb from:%04x to:%04x\n",
			    le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
L
Linus Torvalds 已提交
843 844 845

	kfree_skb(skb);

846
	return NET_RX_DROP;
L
Linus Torvalds 已提交
847 848
}

849 850 851 852 853
static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
{
	return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
}

854
static unsigned int dn_dst_mtu(const struct dst_entry *dst)
855
{
856 857 858
	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);

	return mtu ? : dst->dev->mtu;
859 860
}

861 862 863
static struct neighbour *dn_dst_neigh_lookup(const struct dst_entry *dst,
					     struct sk_buff *skb,
					     const void *daddr)
864 865 866 867
{
	return __neigh_lookup_errno(&dn_neigh_table, daddr, dst->dev);
}

L
Linus Torvalds 已提交
868 869 870
static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
{
	struct dn_fib_info *fi = res->fi;
871
	struct net_device *dev = rt->dst.dev;
872
	unsigned int mss_metric;
L
Linus Torvalds 已提交
873 874 875 876 877 878
	struct neighbour *n;

	if (fi) {
		if (DN_FIB_RES_GW(*res) &&
		    DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
			rt->rt_gateway = DN_FIB_RES_GW(*res);
879
		dst_init_metrics(&rt->dst, fi->fib_metrics, true);
L
Linus Torvalds 已提交
880 881 882
	}
	rt->rt_type = res->type;

883
	if (dev != NULL && rt->n == NULL) {
L
Linus Torvalds 已提交
884 885 886
		n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
		if (IS_ERR(n))
			return PTR_ERR(n);
887
		rt->n = n;
L
Linus Torvalds 已提交
888 889
	}

890
	if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
891
		dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
892 893
	mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
	if (mss_metric) {
894
		unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
895
		if (mss_metric > mss)
896 897
			dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
	}
L
Linus Torvalds 已提交
898 899 900
	return 0;
}

901
static inline int dn_match_addr(__le16 addr1, __le16 addr2)
L
Linus Torvalds 已提交
902
{
903
	__u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
L
Linus Torvalds 已提交
904 905 906 907 908 909 910 911
	int match = 16;
	while(tmp) {
		tmp >>= 1;
		match--;
	}
	return match;
}

912
static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
L
Linus Torvalds 已提交
913
{
914
	__le16 saddr = 0;
915
	struct dn_dev *dn_db;
L
Linus Torvalds 已提交
916 917 918 919
	struct dn_ifaddr *ifa;
	int best_match = 0;
	int ret;

920 921 922 923 924
	rcu_read_lock();
	dn_db = rcu_dereference(dev->dn_ptr);
	for (ifa = rcu_dereference(dn_db->ifa_list);
	     ifa != NULL;
	     ifa = rcu_dereference(ifa->ifa_next)) {
L
Linus Torvalds 已提交
925 926 927 928 929 930 931 932 933 934 935 936
		if (ifa->ifa_scope > scope)
			continue;
		if (!daddr) {
			saddr = ifa->ifa_local;
			break;
		}
		ret = dn_match_addr(daddr, ifa->ifa_local);
		if (ret > best_match)
			saddr = ifa->ifa_local;
		if (best_match == 0)
			saddr = ifa->ifa_local;
	}
937
	rcu_read_unlock();
L
Linus Torvalds 已提交
938 939 940 941

	return saddr;
}

942
static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
L
Linus Torvalds 已提交
943 944 945 946
{
	return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
}

947
static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
L
Linus Torvalds 已提交
948
{
949
	__le16 mask = dnet_make_mask(res->prefixlen);
L
Linus Torvalds 已提交
950 951 952
	return (daddr&~mask)|res->fi->fib_nh->nh_gw;
}

953
static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oldflp, int try_hard)
L
Linus Torvalds 已提交
954
{
955 956 957 958 959
	struct flowidn fld = {
		.daddr = oldflp->daddr,
		.saddr = oldflp->saddr,
		.flowidn_scope = RT_SCOPE_UNIVERSE,
		.flowidn_mark = oldflp->flowidn_mark,
960
		.flowidn_iif = LOOPBACK_IFINDEX,
961
		.flowidn_oif = oldflp->flowidn_oif,
962
	};
L
Linus Torvalds 已提交
963
	struct dn_route *rt = NULL;
964
	struct net_device *dev_out = NULL, *dev;
L
Linus Torvalds 已提交
965
	struct neighbour *neigh = NULL;
966 967
	unsigned int hash;
	unsigned int flags = 0;
L
Linus Torvalds 已提交
968 969 970
	struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
	int err;
	int free_res = 0;
971
	__le16 gateway = 0;
L
Linus Torvalds 已提交
972 973 974 975

	if (decnet_debug_level & 16)
		printk(KERN_DEBUG
		       "dn_route_output_slow: dst=%04x src=%04x mark=%d"
976 977
		       " iif=%d oif=%d\n", le16_to_cpu(oldflp->daddr),
		       le16_to_cpu(oldflp->saddr),
978
		       oldflp->flowidn_mark, LOOPBACK_IFINDEX,
979
		       oldflp->flowidn_oif);
L
Linus Torvalds 已提交
980 981

	/* If we have an output interface, verify its a DECnet device */
982 983
	if (oldflp->flowidn_oif) {
		dev_out = dev_get_by_index(&init_net, oldflp->flowidn_oif);
L
Linus Torvalds 已提交
984 985 986 987 988 989 990 991 992 993
		err = -ENODEV;
		if (dev_out && dev_out->dn_ptr == NULL) {
			dev_put(dev_out);
			dev_out = NULL;
		}
		if (dev_out == NULL)
			goto out;
	}

	/* If we have a source address, verify that its a local address */
994
	if (oldflp->saddr) {
L
Linus Torvalds 已提交
995 996 997
		err = -EADDRNOTAVAIL;

		if (dev_out) {
998
			if (dn_dev_islocal(dev_out, oldflp->saddr))
L
Linus Torvalds 已提交
999 1000 1001 1002
				goto source_ok;
			dev_put(dev_out);
			goto out;
		}
1003 1004
		rcu_read_lock();
		for_each_netdev_rcu(&init_net, dev) {
1005
			if (!dev->dn_ptr)
L
Linus Torvalds 已提交
1006
				continue;
1007
			if (!dn_dev_islocal(dev, oldflp->saddr))
1008
				continue;
1009
			if ((dev->flags & IFF_LOOPBACK) &&
1010 1011
			    oldflp->daddr &&
			    !dn_dev_islocal(dev, oldflp->daddr))
1012
				continue;
1013 1014

			dev_out = dev;
1015
			break;
L
Linus Torvalds 已提交
1016
		}
1017
		rcu_read_unlock();
L
Linus Torvalds 已提交
1018 1019 1020 1021 1022 1023 1024 1025
		if (dev_out == NULL)
			goto out;
		dev_hold(dev_out);
source_ok:
		;
	}

	/* No destination? Assume its local */
1026 1027
	if (!fld.daddr) {
		fld.daddr = fld.saddr;
L
Linus Torvalds 已提交
1028 1029 1030

		if (dev_out)
			dev_put(dev_out);
1031
		err = -EINVAL;
1032
		dev_out = init_net.loopback_dev;
1033 1034 1035
		if (!dev_out->dn_ptr)
			goto out;
		err = -EADDRNOTAVAIL;
L
Linus Torvalds 已提交
1036
		dev_hold(dev_out);
1037 1038 1039
		if (!fld.daddr) {
			fld.daddr =
			fld.saddr = dnet_select_source(dev_out, 0,
L
Linus Torvalds 已提交
1040
						       RT_SCOPE_HOST);
1041
			if (!fld.daddr)
L
Linus Torvalds 已提交
1042 1043
				goto out;
		}
1044
		fld.flowidn_oif = LOOPBACK_IFINDEX;
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051
		res.type = RTN_LOCAL;
		goto make_route;
	}

	if (decnet_debug_level & 16)
		printk(KERN_DEBUG
		       "dn_route_output_slow: initial checks complete."
R
Rasmus Villemoes 已提交
1052
		       " dst=%04x src=%04x oif=%d try_hard=%d\n",
1053 1054
		       le16_to_cpu(fld.daddr), le16_to_cpu(fld.saddr),
		       fld.flowidn_oif, try_hard);
L
Linus Torvalds 已提交
1055 1056 1057 1058 1059 1060 1061

	/*
	 * N.B. If the kernel is compiled without router support then
	 * dn_fib_lookup() will evaluate to non-zero so this if () block
	 * will always be executed.
	 */
	err = -ESRCH;
1062
	if (try_hard || (err = dn_fib_lookup(&fld, &res)) != 0) {
L
Linus Torvalds 已提交
1063 1064 1065 1066
		struct dn_dev *dn_db;
		if (err != -ESRCH)
			goto out;
		/*
1067
		 * Here the fallback is basically the standard algorithm for
L
Linus Torvalds 已提交
1068 1069 1070 1071 1072
		 * routing in endnodes which is described in the DECnet routing
		 * docs
		 *
		 * If we are not trying hard, look in neighbour cache.
		 * The result is tested to ensure that if a specific output
1073
		 * device/source address was requested, then we honour that
L
Linus Torvalds 已提交
1074 1075 1076
		 * here
		 */
		if (!try_hard) {
1077
			neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fld.daddr);
L
Linus Torvalds 已提交
1078
			if (neigh) {
1079 1080 1081
				if ((oldflp->flowidn_oif &&
				    (neigh->dev->ifindex != oldflp->flowidn_oif)) ||
				    (oldflp->saddr &&
L
Linus Torvalds 已提交
1082
				    (!dn_dev_islocal(neigh->dev,
1083
						     oldflp->saddr)))) {
L
Linus Torvalds 已提交
1084 1085 1086 1087 1088
					neigh_release(neigh);
					neigh = NULL;
				} else {
					if (dev_out)
						dev_put(dev_out);
1089
					if (dn_dev_islocal(neigh->dev, fld.daddr)) {
1090
						dev_out = init_net.loopback_dev;
L
Linus Torvalds 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
						res.type = RTN_LOCAL;
					} else {
						dev_out = neigh->dev;
					}
					dev_hold(dev_out);
					goto select_source;
				}
			}
		}

		/* Not there? Perhaps its a local address */
		if (dev_out == NULL)
			dev_out = dn_dev_get_default();
		err = -ENODEV;
		if (dev_out == NULL)
			goto out;
1107
		dn_db = rcu_dereference_raw(dev_out->dn_ptr);
1108 1109
		if (!dn_db)
			goto e_inval;
L
Linus Torvalds 已提交
1110
		/* Possible improvement - check all devices for local addr */
1111
		if (dn_dev_islocal(dev_out, fld.daddr)) {
L
Linus Torvalds 已提交
1112
			dev_put(dev_out);
1113
			dev_out = init_net.loopback_dev;
L
Linus Torvalds 已提交
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
			dev_hold(dev_out);
			res.type = RTN_LOCAL;
			goto select_source;
		}
		/* Not local either.... try sending it to the default router */
		neigh = neigh_clone(dn_db->router);
		BUG_ON(neigh && neigh->dev != dev_out);

		/* Ok then, we assume its directly connected and move on */
select_source:
		if (neigh)
			gateway = ((struct dn_neigh *)neigh)->addr;
		if (gateway == 0)
1127 1128 1129 1130 1131 1132 1133
			gateway = fld.daddr;
		if (fld.saddr == 0) {
			fld.saddr = dnet_select_source(dev_out, gateway,
						       res.type == RTN_LOCAL ?
						       RT_SCOPE_HOST :
						       RT_SCOPE_LINK);
			if (fld.saddr == 0 && res.type != RTN_LOCAL)
L
Linus Torvalds 已提交
1134 1135
				goto e_addr;
		}
1136
		fld.flowidn_oif = dev_out->ifindex;
L
Linus Torvalds 已提交
1137 1138 1139 1140 1141 1142 1143 1144
		goto make_route;
	}
	free_res = 1;

	if (res.type == RTN_NAT)
		goto e_inval;

	if (res.type == RTN_LOCAL) {
1145 1146
		if (!fld.saddr)
			fld.saddr = fld.daddr;
L
Linus Torvalds 已提交
1147 1148
		if (dev_out)
			dev_put(dev_out);
1149
		dev_out = init_net.loopback_dev;
L
Linus Torvalds 已提交
1150
		dev_hold(dev_out);
1151 1152
		if (!dev_out->dn_ptr)
			goto e_inval;
1153
		fld.flowidn_oif = dev_out->ifindex;
L
Linus Torvalds 已提交
1154 1155 1156 1157 1158 1159
		if (res.fi)
			dn_fib_info_put(res.fi);
		res.fi = NULL;
		goto make_route;
	}

1160 1161
	if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
		dn_fib_select_multipath(&fld, &res);
L
Linus Torvalds 已提交
1162

1163
	/*
L
Linus Torvalds 已提交
1164 1165 1166 1167
	 * We could add some logic to deal with default routes here and
	 * get rid of some of the special casing above.
	 */

1168 1169
	if (!fld.saddr)
		fld.saddr = DN_FIB_RES_PREFSRC(res);
1170

L
Linus Torvalds 已提交
1171 1172 1173 1174
	if (dev_out)
		dev_put(dev_out);
	dev_out = DN_FIB_RES_DEV(res);
	dev_hold(dev_out);
1175
	fld.flowidn_oif = dev_out->ifindex;
L
Linus Torvalds 已提交
1176 1177 1178 1179 1180 1181
	gateway = DN_FIB_RES_GW(res);

make_route:
	if (dev_out->flags & IFF_LOOPBACK)
		flags |= RTCF_LOCAL;

1182 1183
	rt = dst_alloc(&dn_dst_ops, dev_out, 1, DST_OBSOLETE_NONE,
		       DST_HOST | DST_NOGC);
L
Linus Torvalds 已提交
1184 1185 1186
	if (rt == NULL)
		goto e_nobufs;

1187
	memset(&rt->fld, 0, sizeof(rt->fld));
1188 1189 1190 1191 1192
	rt->fld.saddr        = oldflp->saddr;
	rt->fld.daddr        = oldflp->daddr;
	rt->fld.flowidn_oif  = oldflp->flowidn_oif;
	rt->fld.flowidn_iif  = 0;
	rt->fld.flowidn_mark = oldflp->flowidn_mark;
L
Linus Torvalds 已提交
1193

1194 1195 1196 1197
	rt->rt_saddr      = fld.saddr;
	rt->rt_daddr      = fld.daddr;
	rt->rt_gateway    = gateway ? gateway : fld.daddr;
	rt->rt_local_src  = fld.saddr;
L
Linus Torvalds 已提交
1198

1199 1200
	rt->rt_dst_map    = fld.daddr;
	rt->rt_src_map    = fld.saddr;
L
Linus Torvalds 已提交
1201

1202
	rt->n = neigh;
L
Linus Torvalds 已提交
1203 1204
	neigh = NULL;

1205 1206 1207
	rt->dst.lastuse = jiffies;
	rt->dst.output  = dn_output;
	rt->dst.input   = dn_rt_bug;
L
Linus Torvalds 已提交
1208 1209
	rt->rt_flags      = flags;
	if (flags & RTCF_LOCAL)
1210
		rt->dst.input = dn_nsp_rx;
L
Linus Torvalds 已提交
1211 1212 1213 1214 1215

	err = dn_rt_set_next_hop(rt, &res);
	if (err)
		goto e_neighbour;

1216
	hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
1217
	/* dn_insert_route() increments dst->__refcnt */
L
Linus Torvalds 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
	dn_insert_route(rt, hash, (struct dn_route **)pprt);

done:
	if (neigh)
		neigh_release(neigh);
	if (free_res)
		dn_fib_res_put(&res);
	if (dev_out)
		dev_put(dev_out);
out:
	return err;

e_addr:
1231 1232
	err = -EADDRNOTAVAIL;
	goto done;
L
Linus Torvalds 已提交
1233 1234 1235 1236 1237 1238 1239
e_inval:
	err = -EINVAL;
	goto done;
e_nobufs:
	err = -ENOBUFS;
	goto done;
e_neighbour:
1240
	dst_release_immediate(&rt->dst);
L
Linus Torvalds 已提交
1241 1242 1243 1244 1245 1246 1247
	goto e_nobufs;
}


/*
 * N.B. The flags may be moved into the flowi at some future stage.
 */
1248
static int __dn_route_output_key(struct dst_entry **pprt, const struct flowidn *flp, int flags)
L
Linus Torvalds 已提交
1249
{
1250
	unsigned int hash = dn_hash(flp->saddr, flp->daddr);
L
Linus Torvalds 已提交
1251 1252 1253 1254
	struct dn_route *rt = NULL;

	if (!(flags & MSG_TRYHARD)) {
		rcu_read_lock_bh();
1255
		for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
1256
			rt = rcu_dereference_bh(rt->dst.dn_next)) {
1257 1258 1259
			if ((flp->daddr == rt->fld.daddr) &&
			    (flp->saddr == rt->fld.saddr) &&
			    (flp->flowidn_mark == rt->fld.flowidn_mark) &&
1260
			    dn_is_output_route(rt) &&
1261
			    (rt->fld.flowidn_oif == flp->flowidn_oif)) {
1262
				dst_use(&rt->dst, jiffies);
L
Linus Torvalds 已提交
1263
				rcu_read_unlock_bh();
1264
				*pprt = &rt->dst;
L
Linus Torvalds 已提交
1265 1266 1267 1268 1269 1270 1271 1272 1273
				return 0;
			}
		}
		rcu_read_unlock_bh();
	}

	return dn_route_output_slow(pprt, flp, flags);
}

1274
static int dn_route_output_key(struct dst_entry **pprt, struct flowidn *flp, int flags)
L
Linus Torvalds 已提交
1275 1276 1277 1278
{
	int err;

	err = __dn_route_output_key(pprt, flp, flags);
1279 1280 1281
	if (err == 0 && flp->flowidn_proto) {
		*pprt = xfrm_lookup(&init_net, *pprt,
				    flowidn_to_flowi(flp), NULL, 0);
1282 1283 1284 1285
		if (IS_ERR(*pprt)) {
			err = PTR_ERR(*pprt);
			*pprt = NULL;
		}
L
Linus Torvalds 已提交
1286 1287 1288 1289
	}
	return err;
}

1290
int dn_route_output_sock(struct dst_entry __rcu **pprt, struct flowidn *fl, struct sock *sk, int flags)
L
Linus Torvalds 已提交
1291 1292 1293 1294
{
	int err;

	err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
1295 1296 1297
	if (err == 0 && fl->flowidn_proto) {
		*pprt = xfrm_lookup(&init_net, *pprt,
				    flowidn_to_flowi(fl), sk, 0);
1298 1299 1300 1301
		if (IS_ERR(*pprt)) {
			err = PTR_ERR(*pprt);
			*pprt = NULL;
		}
L
Linus Torvalds 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
	}
	return err;
}

static int dn_route_input_slow(struct sk_buff *skb)
{
	struct dn_route *rt = NULL;
	struct dn_skb_cb *cb = DN_SKB_CB(skb);
	struct net_device *in_dev = skb->dev;
	struct net_device *out_dev = NULL;
	struct dn_dev *dn_db;
	struct neighbour *neigh = NULL;
1314
	unsigned int hash;
L
Linus Torvalds 已提交
1315
	int flags = 0;
1316 1317
	__le16 gateway = 0;
	__le16 local_src = 0;
1318 1319 1320 1321 1322 1323
	struct flowidn fld = {
		.daddr = cb->dst,
		.saddr = cb->src,
		.flowidn_scope = RT_SCOPE_UNIVERSE,
		.flowidn_mark = skb->mark,
		.flowidn_iif = skb->dev->ifindex,
1324
	};
L
Linus Torvalds 已提交
1325 1326 1327 1328 1329 1330
	struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
	int err = -EINVAL;
	int free_res = 0;

	dev_hold(in_dev);

1331
	if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
L
Linus Torvalds 已提交
1332 1333 1334
		goto out;

	/* Zero source addresses are not allowed */
1335
	if (fld.saddr == 0)
L
Linus Torvalds 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
		goto out;

	/*
	 * In this case we've just received a packet from a source
	 * outside ourselves pretending to come from us. We don't
	 * allow it any further to prevent routing loops, spoofing and
	 * other nasties. Loopback packets already have the dst attached
	 * so this only affects packets which have originated elsewhere.
	 */
	err  = -ENOTUNIQ;
	if (dn_dev_islocal(in_dev, cb->src))
		goto out;

1349
	err = dn_fib_lookup(&fld, &res);
L
Linus Torvalds 已提交
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
	if (err) {
		if (err != -ESRCH)
			goto out;
		/*
		 * Is the destination us ?
		 */
		if (!dn_dev_islocal(in_dev, cb->dst))
			goto e_inval;

		res.type = RTN_LOCAL;
	} else {
1361
		__le16 src_map = fld.saddr;
L
Linus Torvalds 已提交
1362 1363 1364 1365
		free_res = 1;

		out_dev = DN_FIB_RES_DEV(res);
		if (out_dev == NULL) {
1366
			net_crit_ratelimited("Bug in dn_route_input_slow() No output device\n");
L
Linus Torvalds 已提交
1367 1368 1369 1370 1371
			goto e_inval;
		}
		dev_hold(out_dev);

		if (res.r)
1372
			src_map = fld.saddr; /* no NAT support for now */
L
Linus Torvalds 已提交
1373 1374 1375

		gateway = DN_FIB_RES_GW(res);
		if (res.type == RTN_NAT) {
1376
			fld.daddr = dn_fib_rules_map_destination(fld.daddr, &res);
L
Linus Torvalds 已提交
1377 1378
			dn_fib_res_put(&res);
			free_res = 0;
1379
			if (dn_fib_lookup(&fld, &res))
L
Linus Torvalds 已提交
1380 1381 1382 1383 1384
				goto e_inval;
			free_res = 1;
			if (res.type != RTN_UNICAST)
				goto e_inval;
			flags |= RTCF_DNAT;
1385
			gateway = fld.daddr;
L
Linus Torvalds 已提交
1386
		}
1387
		fld.saddr = src_map;
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
	}

	switch(res.type) {
	case RTN_UNICAST:
		/*
		 * Forwarding check here, we only check for forwarding
		 * being turned off, if you want to only forward intra
		 * area, its up to you to set the routing tables up
		 * correctly.
		 */
		if (dn_db->parms.forwarding == 0)
			goto e_inval;

1401 1402
		if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
			dn_fib_select_multipath(&fld, &res);
L
Linus Torvalds 已提交
1403

1404
		/*
L
Linus Torvalds 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
		 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
		 * flag as a hint to set the intra-ethernet bit when
		 * forwarding. If we've got NAT in operation, we don't do
		 * this optimisation.
		 */
		if (out_dev == in_dev && !(flags & RTCF_NAT))
			flags |= RTCF_DOREDIRECT;

		local_src = DN_FIB_RES_PREFSRC(res);

	case RTN_BLACKHOLE:
	case RTN_UNREACHABLE:
		break;
	case RTN_LOCAL:
		flags |= RTCF_LOCAL;
1420 1421
		fld.saddr = cb->dst;
		fld.daddr = cb->src;
L
Linus Torvalds 已提交
1422 1423 1424 1425 1426 1427

		/* Routing tables gave us a gateway */
		if (gateway)
			goto make_route;

		/* Packet was intra-ethernet, so we know its on-link */
1428
		if (cb->rt_flags & DN_RT_F_IE) {
L
Linus Torvalds 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
			gateway = cb->src;
			goto make_route;
		}

		/* Use the default router if there is one */
		neigh = neigh_clone(dn_db->router);
		if (neigh) {
			gateway = ((struct dn_neigh *)neigh)->addr;
			goto make_route;
		}

		/* Close eyes and pray */
		gateway = cb->src;
		goto make_route;
	default:
		goto e_inval;
	}

make_route:
1448 1449
	rt = dst_alloc(&dn_dst_ops, out_dev, 1, DST_OBSOLETE_NONE,
		       DST_HOST | DST_NOGC);
L
Linus Torvalds 已提交
1450 1451 1452
	if (rt == NULL)
		goto e_nobufs;

1453
	memset(&rt->fld, 0, sizeof(rt->fld));
1454 1455 1456
	rt->rt_saddr      = fld.saddr;
	rt->rt_daddr      = fld.daddr;
	rt->rt_gateway    = fld.daddr;
L
Linus Torvalds 已提交
1457 1458 1459 1460
	if (gateway)
		rt->rt_gateway = gateway;
	rt->rt_local_src  = local_src ? local_src : rt->rt_saddr;

1461 1462
	rt->rt_dst_map    = fld.daddr;
	rt->rt_src_map    = fld.saddr;
L
Linus Torvalds 已提交
1463

1464 1465 1466 1467 1468
	rt->fld.saddr        = cb->src;
	rt->fld.daddr        = cb->dst;
	rt->fld.flowidn_oif  = 0;
	rt->fld.flowidn_iif  = in_dev->ifindex;
	rt->fld.flowidn_mark = fld.flowidn_mark;
L
Linus Torvalds 已提交
1469

1470
	rt->n = neigh;
1471
	rt->dst.lastuse = jiffies;
E
Eric W. Biederman 已提交
1472
	rt->dst.output = dn_rt_bug_out;
J
Joe Perches 已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
	switch (res.type) {
	case RTN_UNICAST:
		rt->dst.input = dn_forward;
		break;
	case RTN_LOCAL:
		rt->dst.output = dn_output;
		rt->dst.input = dn_nsp_rx;
		rt->dst.dev = in_dev;
		flags |= RTCF_LOCAL;
		break;
	default:
	case RTN_UNREACHABLE:
	case RTN_BLACKHOLE:
		rt->dst.input = dst_discard;
L
Linus Torvalds 已提交
1487 1488 1489 1490 1491 1492 1493
	}
	rt->rt_flags = flags;

	err = dn_rt_set_next_hop(rt, &res);
	if (err)
		goto e_neighbour;

1494
	hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
1495
	/* dn_insert_route() increments dst->__refcnt */
E
Eric Dumazet 已提交
1496
	dn_insert_route(rt, hash, &rt);
1497
	skb_dst_set(skb, &rt->dst);
L
Linus Torvalds 已提交
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518

done:
	if (neigh)
		neigh_release(neigh);
	if (free_res)
		dn_fib_res_put(&res);
	dev_put(in_dev);
	if (out_dev)
		dev_put(out_dev);
out:
	return err;

e_inval:
	err = -EINVAL;
	goto done;

e_nobufs:
	err = -ENOBUFS;
	goto done;

e_neighbour:
1519
	dst_release_immediate(&rt->dst);
L
Linus Torvalds 已提交
1520 1521 1522
	goto done;
}

R
Roel Kluin 已提交
1523
static int dn_route_input(struct sk_buff *skb)
L
Linus Torvalds 已提交
1524 1525 1526
{
	struct dn_route *rt;
	struct dn_skb_cb *cb = DN_SKB_CB(skb);
1527
	unsigned int hash = dn_hash(cb->src, cb->dst);
L
Linus Torvalds 已提交
1528

E
Eric Dumazet 已提交
1529
	if (skb_dst(skb))
L
Linus Torvalds 已提交
1530 1531 1532 1533
		return 0;

	rcu_read_lock();
	for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
1534
	    rt = rcu_dereference(rt->dst.dn_next)) {
1535 1536 1537 1538 1539
		if ((rt->fld.saddr == cb->src) &&
		    (rt->fld.daddr == cb->dst) &&
		    (rt->fld.flowidn_oif == 0) &&
		    (rt->fld.flowidn_mark == skb->mark) &&
		    (rt->fld.flowidn_iif == cb->iif)) {
1540
			dst_use(&rt->dst, jiffies);
L
Linus Torvalds 已提交
1541
			rcu_read_unlock();
E
Eric Dumazet 已提交
1542
			skb_dst_set(skb, (struct dst_entry *)rt);
L
Linus Torvalds 已提交
1543 1544 1545 1546 1547 1548 1549 1550
			return 0;
		}
	}
	rcu_read_unlock();

	return dn_route_input_slow(skb);
}

1551
static int dn_rt_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1552
			   int event, int nowait, unsigned int flags)
L
Linus Torvalds 已提交
1553
{
E
Eric Dumazet 已提交
1554
	struct dn_route *rt = (struct dn_route *)skb_dst(skb);
L
Linus Torvalds 已提交
1555 1556
	struct rtmsg *r;
	struct nlmsghdr *nlh;
1557
	long expires;
L
Linus Torvalds 已提交
1558

1559
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*r), flags);
1560
	if (!nlh)
1561 1562
		return -EMSGSIZE;

1563
	r = nlmsg_data(nlh);
L
Linus Torvalds 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572
	r->rtm_family = AF_DECnet;
	r->rtm_dst_len = 16;
	r->rtm_src_len = 0;
	r->rtm_tos = 0;
	r->rtm_table = RT_TABLE_MAIN;
	r->rtm_type = rt->rt_type;
	r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
	r->rtm_scope = RT_SCOPE_UNIVERSE;
	r->rtm_protocol = RTPROT_UNSPEC;
1573

L
Linus Torvalds 已提交
1574 1575
	if (rt->rt_flags & RTCF_NOTIFY)
		r->rtm_flags |= RTM_F_NOTIFY;
1576 1577 1578 1579 1580

	if (nla_put_u32(skb, RTA_TABLE, RT_TABLE_MAIN) < 0 ||
	    nla_put_le16(skb, RTA_DST, rt->rt_daddr) < 0)
		goto errout;

1581
	if (rt->fld.saddr) {
L
Linus Torvalds 已提交
1582
		r->rtm_src_len = 16;
1583 1584
		if (nla_put_le16(skb, RTA_SRC, rt->fld.saddr) < 0)
			goto errout;
L
Linus Torvalds 已提交
1585
	}
1586 1587 1588 1589
	if (rt->dst.dev &&
	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex) < 0)
		goto errout;

L
Linus Torvalds 已提交
1590 1591 1592 1593 1594
	/*
	 * Note to self - change this if input routes reverse direction when
	 * they deal only with inputs and not with replies like they do
	 * currently.
	 */
1595 1596 1597 1598 1599 1600 1601
	if (nla_put_le16(skb, RTA_PREFSRC, rt->rt_local_src) < 0)
		goto errout;

	if (rt->rt_daddr != rt->rt_gateway &&
	    nla_put_le16(skb, RTA_GATEWAY, rt->rt_gateway) < 0)
		goto errout;

1602
	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
1603 1604
		goto errout;

1605
	expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
1606
	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires,
1607
			       rt->dst.error) < 0)
1608
		goto errout;
L
Linus Torvalds 已提交
1609

1610 1611 1612
	if (dn_is_input_route(rt) &&
	    nla_put_u32(skb, RTA_IIF, rt->fld.flowidn_iif) < 0)
		goto errout;
L
Linus Torvalds 已提交
1613

1614 1615
	nlmsg_end(skb, nlh);
	return 0;
1616 1617 1618 1619

errout:
	nlmsg_cancel(skb, nlh);
	return -EMSGSIZE;
L
Linus Torvalds 已提交
1620 1621
}

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
const struct nla_policy rtm_dn_policy[RTA_MAX + 1] = {
	[RTA_DST]		= { .type = NLA_U16 },
	[RTA_SRC]		= { .type = NLA_U16 },
	[RTA_IIF]		= { .type = NLA_U32 },
	[RTA_OIF]		= { .type = NLA_U32 },
	[RTA_GATEWAY]		= { .type = NLA_U16 },
	[RTA_PRIORITY]		= { .type = NLA_U32 },
	[RTA_PREFSRC]		= { .type = NLA_U16 },
	[RTA_METRICS]		= { .type = NLA_NESTED },
	[RTA_MULTIPATH]		= { .type = NLA_NESTED },
	[RTA_TABLE]		= { .type = NLA_U32 },
	[RTA_MARK]		= { .type = NLA_U32 },
};

L
Linus Torvalds 已提交
1636 1637 1638
/*
 * This is called by both endnodes and routers now.
 */
1639 1640
static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
			     struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
1641
{
1642
	struct net *net = sock_net(in_skb->sk);
1643
	struct rtmsg *rtm = nlmsg_data(nlh);
L
Linus Torvalds 已提交
1644 1645 1646 1647
	struct dn_route *rt = NULL;
	struct dn_skb_cb *cb;
	int err;
	struct sk_buff *skb;
1648
	struct flowidn fld;
1649
	struct nlattr *tb[RTA_MAX+1];
L
Linus Torvalds 已提交
1650

O
Octavian Purdila 已提交
1651
	if (!net_eq(net, &init_net))
1652 1653
		return -EINVAL;

1654 1655
	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_dn_policy,
			  extack);
1656 1657 1658
	if (err < 0)
		return err;

1659 1660
	memset(&fld, 0, sizeof(fld));
	fld.flowidn_proto = DNPROTO_NSP;
L
Linus Torvalds 已提交
1661

1662
	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
1663 1664
	if (skb == NULL)
		return -ENOBUFS;
1665
	skb_reset_mac_header(skb);
L
Linus Torvalds 已提交
1666 1667
	cb = DN_SKB_CB(skb);

1668 1669 1670 1671 1672 1673 1674 1675
	if (tb[RTA_SRC])
		fld.saddr = nla_get_le16(tb[RTA_SRC]);

	if (tb[RTA_DST])
		fld.daddr = nla_get_le16(tb[RTA_DST]);

	if (tb[RTA_IIF])
		fld.flowidn_iif = nla_get_u32(tb[RTA_IIF]);
L
Linus Torvalds 已提交
1676

1677
	if (fld.flowidn_iif) {
L
Linus Torvalds 已提交
1678
		struct net_device *dev;
1679 1680
		dev = __dev_get_by_index(&init_net, fld.flowidn_iif);
		if (!dev || !dev->dn_ptr) {
L
Linus Torvalds 已提交
1681 1682 1683
			kfree_skb(skb);
			return -ENODEV;
		}
1684
		skb->protocol = htons(ETH_P_DNA_RT);
L
Linus Torvalds 已提交
1685
		skb->dev = dev;
1686 1687
		cb->src = fld.saddr;
		cb->dst = fld.daddr;
L
Linus Torvalds 已提交
1688 1689 1690 1691
		local_bh_disable();
		err = dn_route_input(skb);
		local_bh_enable();
		memset(cb, 0, sizeof(struct dn_skb_cb));
E
Eric Dumazet 已提交
1692
		rt = (struct dn_route *)skb_dst(skb);
1693 1694
		if (!err && -rt->dst.error)
			err = rt->dst.error;
L
Linus Torvalds 已提交
1695
	} else {
1696 1697 1698
		if (tb[RTA_OIF])
			fld.flowidn_oif = nla_get_u32(tb[RTA_OIF]);

1699
		err = dn_route_output_key((struct dst_entry **)&rt, &fld, 0);
L
Linus Torvalds 已提交
1700 1701 1702 1703 1704
	}

	skb->dev = NULL;
	if (err)
		goto out_free;
1705
	skb_dst_set(skb, &rt->dst);
L
Linus Torvalds 已提交
1706 1707 1708
	if (rtm->rtm_flags & RTM_F_NOTIFY)
		rt->rt_flags |= RTCF_NOTIFY;

1709
	err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, RTM_NEWROUTE, 0, 0);
L
Linus Torvalds 已提交
1710 1711 1712 1713 1714
	if (err < 0) {
		err = -EMSGSIZE;
		goto out_free;
	}

1715
	return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).portid);
L
Linus Torvalds 已提交
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727

out_free:
	kfree_skb(skb);
	return err;
}

/*
 * For routers, this is called from dn_fib_dump, but for endnodes its
 * called directly from the rtnetlink dispatch table.
 */
int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
1728
	struct net *net = sock_net(skb->sk);
L
Linus Torvalds 已提交
1729 1730 1731
	struct dn_route *rt;
	int h, s_h;
	int idx, s_idx;
1732
	struct rtmsg *rtm;
L
Linus Torvalds 已提交
1733

O
Octavian Purdila 已提交
1734
	if (!net_eq(net, &init_net))
1735 1736
		return 0;

1737
	if (nlmsg_len(cb->nlh) < sizeof(struct rtmsg))
L
Linus Torvalds 已提交
1738
		return -EINVAL;
1739 1740 1741

	rtm = nlmsg_data(cb->nlh);
	if (!(rtm->rtm_flags & RTM_F_CLONED))
L
Linus Torvalds 已提交
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
		return 0;

	s_h = cb->args[0];
	s_idx = idx = cb->args[1];
	for(h = 0; h <= dn_rt_hash_mask; h++) {
		if (h < s_h)
			continue;
		if (h > s_h)
			s_idx = 0;
		rcu_read_lock_bh();
1752
		for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
L
Linus Torvalds 已提交
1753
			rt;
1754
			rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
L
Linus Torvalds 已提交
1755 1756
			if (idx < s_idx)
				continue;
1757
			skb_dst_set(skb, dst_clone(&rt->dst));
1758
			if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).portid,
1759
					cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1760
					1, NLM_F_MULTI) < 0) {
E
Eric Dumazet 已提交
1761
				skb_dst_drop(skb);
L
Linus Torvalds 已提交
1762 1763 1764
				rcu_read_unlock_bh();
				goto done;
			}
E
Eric Dumazet 已提交
1765
			skb_dst_drop(skb);
L
Linus Torvalds 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
		}
		rcu_read_unlock_bh();
	}

done:
	cb->args[0] = h;
	cb->args[1] = idx;
	return skb->len;
}

#ifdef CONFIG_PROC_FS
struct dn_rt_cache_iter_state {
	int bucket;
};

static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
{
	struct dn_route *rt = NULL;
	struct dn_rt_cache_iter_state *s = seq->private;

	for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
		rcu_read_lock_bh();
1788
		rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
L
Linus Torvalds 已提交
1789 1790 1791 1792
		if (rt)
			break;
		rcu_read_unlock_bh();
	}
1793
	return rt;
L
Linus Torvalds 已提交
1794 1795 1796 1797
}

static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
{
1798
	struct dn_rt_cache_iter_state *s = seq->private;
L
Linus Torvalds 已提交
1799

1800 1801
	rt = rcu_dereference_bh(rt->dst.dn_next);
	while (!rt) {
L
Linus Torvalds 已提交
1802 1803 1804 1805
		rcu_read_unlock_bh();
		if (--s->bucket < 0)
			break;
		rcu_read_lock_bh();
1806
		rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
L
Linus Torvalds 已提交
1807
	}
1808
	return rt;
L
Linus Torvalds 已提交
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
}

static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
{
	struct dn_route *rt = dn_rt_cache_get_first(seq);

	if (rt) {
		while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
			--*pos;
	}
	return *pos ? NULL : rt;
}

static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	struct dn_route *rt = dn_rt_cache_get_next(seq, v);
	++*pos;
	return rt;
}

static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
{
	if (v)
		rcu_read_unlock_bh();
}

static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
{
	struct dn_route *rt = v;
	char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];

	seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
1841 1842 1843 1844 1845
		   rt->dst.dev ? rt->dst.dev->name : "*",
		   dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
		   dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
		   atomic_read(&rt->dst.__refcnt),
		   rt->dst.__use, 0);
L
Linus Torvalds 已提交
1846
	return 0;
1847
}
L
Linus Torvalds 已提交
1848

1849
static const struct seq_operations dn_rt_cache_seq_ops = {
L
Linus Torvalds 已提交
1850 1851 1852 1853 1854 1855 1856 1857
	.start	= dn_rt_cache_seq_start,
	.next	= dn_rt_cache_seq_next,
	.stop	= dn_rt_cache_seq_stop,
	.show	= dn_rt_cache_seq_show,
};

static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
{
1858 1859
	return seq_open_private(file, &dn_rt_cache_seq_ops,
			sizeof(struct dn_rt_cache_iter_state));
L
Linus Torvalds 已提交
1860 1861
}

1862
static const struct file_operations dn_rt_cache_seq_fops = {
L
Linus Torvalds 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
	.owner	 = THIS_MODULE,
	.open	 = dn_rt_cache_seq_open,
	.read	 = seq_read,
	.llseek	 = seq_lseek,
	.release = seq_release_private,
};

#endif /* CONFIG_PROC_FS */

void __init dn_route_init(void)
{
	int i, goal, order;

A
Alexey Dobriyan 已提交
1876 1877
	dn_dst_ops.kmem_cachep =
		kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
1878
				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1879
	dst_entries_init(&dn_dst_ops);
1880
	setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
L
Linus Torvalds 已提交
1881 1882 1883
	dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
	add_timer(&dn_route_timer);

1884
	goal = totalram_pages >> (26 - PAGE_SHIFT);
L
Linus Torvalds 已提交
1885 1886 1887 1888

	for(order = 0; (1UL << order) < goal; order++)
		/* NOTHING */;

1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
	/*
	 * Only want 1024 entries max, since the table is very, very unlikely
	 * to be larger than that.
	 */
	while(order && ((((1UL << order) * PAGE_SIZE) /
				sizeof(struct dn_rt_hash_bucket)) >= 2048))
		order--;

	do {
		dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
			sizeof(struct dn_rt_hash_bucket);
		while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
			dn_rt_hash_mask--;
		dn_rt_hash_table = (struct dn_rt_hash_bucket *)
			__get_free_pages(GFP_ATOMIC, order);
	} while (dn_rt_hash_table == NULL && --order > 0);
L
Linus Torvalds 已提交
1905 1906

	if (!dn_rt_hash_table)
1907
		panic("Failed to allocate DECnet route cache hash table\n");
L
Linus Torvalds 已提交
1908

1909 1910 1911
	printk(KERN_INFO
		"DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
		dn_rt_hash_mask,
L
Linus Torvalds 已提交
1912 1913 1914
		(long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);

	dn_rt_hash_mask--;
1915 1916 1917 1918
	for(i = 0; i <= dn_rt_hash_mask; i++) {
		spin_lock_init(&dn_rt_hash_table[i].lock);
		dn_rt_hash_table[i].chain = NULL;
	}
L
Linus Torvalds 已提交
1919

1920
	dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
L
Linus Torvalds 已提交
1921

1922 1923
	proc_create("decnet_cache", S_IRUGO, init_net.proc_net,
		    &dn_rt_cache_seq_fops);
1924 1925

#ifdef CONFIG_DECNET_ROUTER
1926 1927
	rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
		      dn_fib_dump, NULL);
1928 1929
#else
	rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1930
		      dn_cache_dump, NULL);
1931
#endif
L
Linus Torvalds 已提交
1932 1933 1934 1935 1936 1937 1938
}

void __exit dn_route_cleanup(void)
{
	del_timer(&dn_route_timer);
	dn_run_flush(0);

1939
	remove_proc_entry("decnet_cache", init_net.proc_net);
1940
	dst_entries_destroy(&dn_dst_ops);
L
Linus Torvalds 已提交
1941 1942
}