1/*
2 * DECnet       An implementation of the DECnet protocol suite for the LINUX
3 *              operating system.  DECnet is implemented using the  BSD Socket
4 *              interface as the means of communication with the user level.
5 *
6 *              DECnet Routing Functions (Endnode and Router)
7 *
8 * Authors:     Steve Whitehouse <SteveW@ACM.org>
9 *              Eduardo Marcelo Serrat <emserrat@geocities.com>
10 *
11 * Changes:
12 *              Steve Whitehouse : Fixes to allow "intra-ethernet" and
13 *                                 "return-to-sender" bits on outgoing
14 *                                 packets.
15 *		Steve Whitehouse : Timeouts for cached routes.
16 *              Steve Whitehouse : Use dst cache for input routes too.
17 *              Steve Whitehouse : Fixed error values in dn_send_skb.
18 *              Steve Whitehouse : Rework routing functions to better fit
19 *                                 DECnet routing design
20 *              Alexey Kuznetsov : New SMP locking
21 *              Steve Whitehouse : More SMP locking changes & dn_cache_dump()
22 *              Steve Whitehouse : Prerouting NF hook, now really is prerouting.
23 *				   Fixed possible skb leak in rtnetlink funcs.
24 *              Steve Whitehouse : Dave Miller's dynamic hash table sizing and
25 *                                 Alexey Kuznetsov's finer grained locking
26 *                                 from ipv4/route.c.
27 *              Steve Whitehouse : Routing is now starting to look like a
28 *                                 sensible set of code now, mainly due to
29 *                                 my copying the IPv4 routing code. The
30 *                                 hooks here are modified and will continue
31 *                                 to evolve for a while.
32 *              Steve Whitehouse : Real SMP at last :-) Also new netfilter
33 *                                 stuff. Look out raw sockets your days
34 *                                 are numbered!
35 *              Steve Whitehouse : Added return-to-sender functions. Added
36 *                                 backlog congestion level return codes.
37 *		Steve Whitehouse : Fixed bug where routes were set up with
38 *                                 no ref count on net devices.
39 *              Steve Whitehouse : RCU for the route cache
40 *              Steve Whitehouse : Preparations for the flow cache
41 *              Steve Whitehouse : Prepare for nonlinear skbs
42 */
43
44/******************************************************************************
45    (c) 1995-1998 E.M. Serrat		emserrat@geocities.com
46
47    This program is free software; you can redistribute it and/or modify
48    it under the terms of the GNU General Public License as published by
49    the Free Software Foundation; either version 2 of the License, or
50    any later version.
51
52    This program is distributed in the hope that it will be useful,
53    but WITHOUT ANY WARRANTY; without even the implied warranty of
54    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
55    GNU General Public License for more details.
56*******************************************************************************/
57
58#include <linux/errno.h>
59#include <linux/types.h>
60#include <linux/socket.h>
61#include <linux/in.h>
62#include <linux/kernel.h>
63#include <linux/sockios.h>
64#include <linux/net.h>
65#include <linux/netdevice.h>
66#include <linux/inet.h>
67#include <linux/route.h>
68#include <linux/in_route.h>
69#include <linux/slab.h>
70#include <net/sock.h>
71#include <linux/mm.h>
72#include <linux/proc_fs.h>
73#include <linux/seq_file.h>
74#include <linux/init.h>
75#include <linux/rtnetlink.h>
76#include <linux/string.h>
77#include <linux/netfilter_decnet.h>
78#include <linux/rcupdate.h>
79#include <linux/times.h>
80#include <linux/export.h>
81#include <asm/errno.h>
82#include <net/net_namespace.h>
83#include <net/netlink.h>
84#include <net/neighbour.h>
85#include <net/dst.h>
86#include <net/flow.h>
87#include <net/fib_rules.h>
88#include <net/dn.h>
89#include <net/dn_dev.h>
90#include <net/dn_nsp.h>
91#include <net/dn_route.h>
92#include <net/dn_neigh.h>
93#include <net/dn_fib.h>
94
95struct dn_rt_hash_bucket
96{
97	struct dn_route __rcu *chain;
98	spinlock_t lock;
99};
100
101extern struct neigh_table dn_neigh_table;
102
103
104static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};
105
106static const int dn_rt_min_delay = 2 * HZ;
107static const int dn_rt_max_delay = 10 * HZ;
108static const int dn_rt_mtu_expires = 10 * 60 * HZ;
109
110static unsigned long dn_rt_deadline;
111
112static int dn_dst_gc(struct dst_ops *ops);
113static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);
114static unsigned int dn_dst_default_advmss(const struct dst_entry *dst);
115static unsigned int dn_dst_mtu(const struct dst_entry *dst);
116static void dn_dst_destroy(struct dst_entry *);
117static void dn_dst_ifdown(struct dst_entry *, struct net_device *dev, int how);
118static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
119static void dn_dst_link_failure(struct sk_buff *);
120static void dn_dst_update_pmtu(struct dst_entry *dst, struct sock *sk,
121			       struct sk_buff *skb , u32 mtu);
122static void dn_dst_redirect(struct dst_entry *dst, struct sock *sk,
123			    struct sk_buff *skb);
124static struct neighbour *dn_dst_neigh_lookup(const struct dst_entry *dst,
125					     struct sk_buff *skb,
126					     const void *daddr);
127static int dn_route_input(struct sk_buff *);
128static void dn_run_flush(unsigned long dummy);
129
130static struct dn_rt_hash_bucket *dn_rt_hash_table;
131static unsigned int dn_rt_hash_mask;
132
133static struct timer_list dn_route_timer;
134static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
135int decnet_dst_gc_interval = 2;
136
137static struct dst_ops dn_dst_ops = {
138	.family =		PF_DECnet,
139	.protocol =		cpu_to_be16(ETH_P_DNA_RT),
140	.gc_thresh =		128,
141	.gc =			dn_dst_gc,
142	.check =		dn_dst_check,
143	.default_advmss =	dn_dst_default_advmss,
144	.mtu =			dn_dst_mtu,
145	.cow_metrics =		dst_cow_metrics_generic,
146	.destroy =		dn_dst_destroy,
147	.ifdown =		dn_dst_ifdown,
148	.negative_advice =	dn_dst_negative_advice,
149	.link_failure =		dn_dst_link_failure,
150	.update_pmtu =		dn_dst_update_pmtu,
151	.redirect =		dn_dst_redirect,
152	.neigh_lookup =		dn_dst_neigh_lookup,
153};
154
155static void dn_dst_destroy(struct dst_entry *dst)
156{
157	struct dn_route *rt = (struct dn_route *) dst;
158
159	if (rt->n)
160		neigh_release(rt->n);
161	dst_destroy_metrics_generic(dst);
162}
163
164static void dn_dst_ifdown(struct dst_entry *dst, struct net_device *dev, int how)
165{
166	if (how) {
167		struct dn_route *rt = (struct dn_route *) dst;
168		struct neighbour *n = rt->n;
169
170		if (n && n->dev == dev) {
171			n->dev = dev_net(dev)->loopback_dev;
172			dev_hold(n->dev);
173			dev_put(dev);
174		}
175	}
176}
177
178static __inline__ unsigned int dn_hash(__le16 src, __le16 dst)
179{
180	__u16 tmp = (__u16 __force)(src ^ dst);
181	tmp ^= (tmp >> 3);
182	tmp ^= (tmp >> 5);
183	tmp ^= (tmp >> 10);
184	return dn_rt_hash_mask & (unsigned int)tmp;
185}
186
187static inline void dnrt_free(struct dn_route *rt)
188{
189	call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
190}
191
192static inline void dnrt_drop(struct dn_route *rt)
193{
194	dst_release(&rt->dst);
195	call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
196}
197
198static void dn_dst_check_expire(unsigned long dummy)
199{
200	int i;
201	struct dn_route *rt;
202	struct dn_route __rcu **rtp;
203	unsigned long now = jiffies;
204	unsigned long expire = 120 * HZ;
205
206	for (i = 0; i <= dn_rt_hash_mask; i++) {
207		rtp = &dn_rt_hash_table[i].chain;
208
209		spin_lock(&dn_rt_hash_table[i].lock);
210		while ((rt = rcu_dereference_protected(*rtp,
211						lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
212			if (atomic_read(&rt->dst.__refcnt) ||
213					(now - rt->dst.lastuse) < expire) {
214				rtp = &rt->dst.dn_next;
215				continue;
216			}
217			*rtp = rt->dst.dn_next;
218			rt->dst.dn_next = NULL;
219			dnrt_free(rt);
220		}
221		spin_unlock(&dn_rt_hash_table[i].lock);
222
223		if ((jiffies - now) > 0)
224			break;
225	}
226
227	mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
228}
229
230static int dn_dst_gc(struct dst_ops *ops)
231{
232	struct dn_route *rt;
233	struct dn_route __rcu **rtp;
234	int i;
235	unsigned long now = jiffies;
236	unsigned long expire = 10 * HZ;
237
238	for (i = 0; i <= dn_rt_hash_mask; i++) {
239
240		spin_lock_bh(&dn_rt_hash_table[i].lock);
241		rtp = &dn_rt_hash_table[i].chain;
242
243		while ((rt = rcu_dereference_protected(*rtp,
244						lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
245			if (atomic_read(&rt->dst.__refcnt) ||
246					(now - rt->dst.lastuse) < expire) {
247				rtp = &rt->dst.dn_next;
248				continue;
249			}
250			*rtp = rt->dst.dn_next;
251			rt->dst.dn_next = NULL;
252			dnrt_drop(rt);
253			break;
254		}
255		spin_unlock_bh(&dn_rt_hash_table[i].lock);
256	}
257
258	return 0;
259}
260
261/*
262 * The decnet standards don't impose a particular minimum mtu, what they
263 * do insist on is that the routing layer accepts a datagram of at least
264 * 230 bytes long. Here we have to subtract the routing header length from
265 * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
266 * assume the worst and use a long header size.
267 *
268 * We update both the mtu and the advertised mss (i.e. the segment size we
269 * advertise to the other end).
270 */
271static void dn_dst_update_pmtu(struct dst_entry *dst, struct sock *sk,
272			       struct sk_buff *skb, u32 mtu)
273{
274	struct dn_route *rt = (struct dn_route *) dst;
275	struct neighbour *n = rt->n;
276	u32 min_mtu = 230;
277	struct dn_dev *dn;
278
279	dn = n ? rcu_dereference_raw(n->dev->dn_ptr) : NULL;
280
281	if (dn && dn->use_long == 0)
282		min_mtu -= 6;
283	else
284		min_mtu -= 21;
285
286	if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
287		if (!(dst_metric_locked(dst, RTAX_MTU))) {
288			dst_metric_set(dst, RTAX_MTU, mtu);
289			dst_set_expires(dst, dn_rt_mtu_expires);
290		}
291		if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
292			u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
293			u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
294			if (!existing_mss || existing_mss > mss)
295				dst_metric_set(dst, RTAX_ADVMSS, mss);
296		}
297	}
298}
299
300static void dn_dst_redirect(struct dst_entry *dst, struct sock *sk,
301			    struct sk_buff *skb)
302{
303}
304
305/*
306 * When a route has been marked obsolete. (e.g. routing cache flush)
307 */
308static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
309{
310	return NULL;
311}
312
313static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
314{
315	dst_release(dst);
316	return NULL;
317}
318
319static void dn_dst_link_failure(struct sk_buff *skb)
320{
321}
322
323static inline int compare_keys(struct flowidn *fl1, struct flowidn *fl2)
324{
325	return ((fl1->daddr ^ fl2->daddr) |
326		(fl1->saddr ^ fl2->saddr) |
327		(fl1->flowidn_mark ^ fl2->flowidn_mark) |
328		(fl1->flowidn_scope ^ fl2->flowidn_scope) |
329		(fl1->flowidn_oif ^ fl2->flowidn_oif) |
330		(fl1->flowidn_iif ^ fl2->flowidn_iif)) == 0;
331}
332
333static int dn_insert_route(struct dn_route *rt, unsigned int hash, struct dn_route **rp)
334{
335	struct dn_route *rth;
336	struct dn_route __rcu **rthp;
337	unsigned long now = jiffies;
338
339	rthp = &dn_rt_hash_table[hash].chain;
340
341	spin_lock_bh(&dn_rt_hash_table[hash].lock);
342	while ((rth = rcu_dereference_protected(*rthp,
343						lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
344		if (compare_keys(&rth->fld, &rt->fld)) {
345			/* Put it first */
346			*rthp = rth->dst.dn_next;
347			rcu_assign_pointer(rth->dst.dn_next,
348					   dn_rt_hash_table[hash].chain);
349			rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
350
351			dst_use(&rth->dst, now);
352			spin_unlock_bh(&dn_rt_hash_table[hash].lock);
353
354			dnrt_drop(rt);
355			*rp = rth;
356			return 0;
357		}
358		rthp = &rth->dst.dn_next;
359	}
360
361	rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
362	rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
363
364	dst_use(&rt->dst, now);
365	spin_unlock_bh(&dn_rt_hash_table[hash].lock);
366	*rp = rt;
367	return 0;
368}
369
370static void dn_run_flush(unsigned long dummy)
371{
372	int i;
373	struct dn_route *rt, *next;
374
375	for (i = 0; i < dn_rt_hash_mask; i++) {
376		spin_lock_bh(&dn_rt_hash_table[i].lock);
377
378		if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
379			goto nothing_to_declare;
380
381		for(; rt; rt = next) {
382			next = rcu_dereference_raw(rt->dst.dn_next);
383			RCU_INIT_POINTER(rt->dst.dn_next, NULL);
384			dst_free((struct dst_entry *)rt);
385		}
386
387nothing_to_declare:
388		spin_unlock_bh(&dn_rt_hash_table[i].lock);
389	}
390}
391
392static DEFINE_SPINLOCK(dn_rt_flush_lock);
393
394void dn_rt_cache_flush(int delay)
395{
396	unsigned long now = jiffies;
397	int user_mode = !in_interrupt();
398
399	if (delay < 0)
400		delay = dn_rt_min_delay;
401
402	spin_lock_bh(&dn_rt_flush_lock);
403
404	if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
405		long tmo = (long)(dn_rt_deadline - now);
406
407		if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
408			tmo = 0;
409
410		if (delay > tmo)
411			delay = tmo;
412	}
413
414	if (delay <= 0) {
415		spin_unlock_bh(&dn_rt_flush_lock);
416		dn_run_flush(0);
417		return;
418	}
419
420	if (dn_rt_deadline == 0)
421		dn_rt_deadline = now + dn_rt_max_delay;
422
423	dn_rt_flush_timer.expires = now + delay;
424	add_timer(&dn_rt_flush_timer);
425	spin_unlock_bh(&dn_rt_flush_lock);
426}
427
428/**
429 * dn_return_short - Return a short packet to its sender
430 * @skb: The packet to return
431 *
432 */
433static int dn_return_short(struct sk_buff *skb)
434{
435	struct dn_skb_cb *cb;
436	unsigned char *ptr;
437	__le16 *src;
438	__le16 *dst;
439
440	/* Add back headers */
441	skb_push(skb, skb->data - skb_network_header(skb));
442
443	if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
444		return NET_RX_DROP;
445
446	cb = DN_SKB_CB(skb);
447	/* Skip packet length and point to flags */
448	ptr = skb->data + 2;
449	*ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
450
451	dst = (__le16 *)ptr;
452	ptr += 2;
453	src = (__le16 *)ptr;
454	ptr += 2;
455	*ptr = 0; /* Zero hop count */
456
457	swap(*src, *dst);
458
459	skb->pkt_type = PACKET_OUTGOING;
460	dn_rt_finish_output(skb, NULL, NULL);
461	return NET_RX_SUCCESS;
462}
463
464/**
465 * dn_return_long - Return a long packet to its sender
466 * @skb: The long format packet to return
467 *
468 */
469static int dn_return_long(struct sk_buff *skb)
470{
471	struct dn_skb_cb *cb;
472	unsigned char *ptr;
473	unsigned char *src_addr, *dst_addr;
474	unsigned char tmp[ETH_ALEN];
475
476	/* Add back all headers */
477	skb_push(skb, skb->data - skb_network_header(skb));
478
479	if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
480		return NET_RX_DROP;
481
482	cb = DN_SKB_CB(skb);
483	/* Ignore packet length and point to flags */
484	ptr = skb->data + 2;
485
486	/* Skip padding */
487	if (*ptr & DN_RT_F_PF) {
488		char padlen = (*ptr & ~DN_RT_F_PF);
489		ptr += padlen;
490	}
491
492	*ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
493	ptr += 2;
494	dst_addr = ptr;
495	ptr += 8;
496	src_addr = ptr;
497	ptr += 6;
498	*ptr = 0; /* Zero hop count */
499
500	/* Swap source and destination */
501	memcpy(tmp, src_addr, ETH_ALEN);
502	memcpy(src_addr, dst_addr, ETH_ALEN);
503	memcpy(dst_addr, tmp, ETH_ALEN);
504
505	skb->pkt_type = PACKET_OUTGOING;
506	dn_rt_finish_output(skb, dst_addr, src_addr);
507	return NET_RX_SUCCESS;
508}
509
510/**
511 * dn_route_rx_packet - Try and find a route for an incoming packet
512 * @skb: The packet to find a route for
513 *
514 * Returns: result of input function if route is found, error code otherwise
515 */
516static int dn_route_rx_packet(struct sk_buff *skb)
517{
518	struct dn_skb_cb *cb;
519	int err;
520
521	if ((err = dn_route_input(skb)) == 0)
522		return dst_input(skb);
523
524	cb = DN_SKB_CB(skb);
525	if (decnet_debug_level & 4) {
526		char *devname = skb->dev ? skb->dev->name : "???";
527
528		printk(KERN_DEBUG
529			"DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%d\n",
530			(int)cb->rt_flags, devname, skb->len,
531			le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
532			err, skb->pkt_type);
533	}
534
535	if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
536		switch (cb->rt_flags & DN_RT_PKT_MSK) {
537		case DN_RT_PKT_SHORT:
538			return dn_return_short(skb);
539		case DN_RT_PKT_LONG:
540			return dn_return_long(skb);
541		}
542	}
543
544	kfree_skb(skb);
545	return NET_RX_DROP;
546}
547
548static int dn_route_rx_long(struct sk_buff *skb)
549{
550	struct dn_skb_cb *cb = DN_SKB_CB(skb);
551	unsigned char *ptr = skb->data;
552
553	if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
554		goto drop_it;
555
556	skb_pull(skb, 20);
557	skb_reset_transport_header(skb);
558
559	/* Destination info */
560	ptr += 2;
561	cb->dst = dn_eth2dn(ptr);
562	if (memcmp(ptr, dn_hiord_addr, 4) != 0)
563		goto drop_it;
564	ptr += 6;
565
566
567	/* Source info */
568	ptr += 2;
569	cb->src = dn_eth2dn(ptr);
570	if (memcmp(ptr, dn_hiord_addr, 4) != 0)
571		goto drop_it;
572	ptr += 6;
573	/* Other junk */
574	ptr++;
575	cb->hops = *ptr++; /* Visit Count */
576
577	return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
578		       dn_route_rx_packet);
579
580drop_it:
581	kfree_skb(skb);
582	return NET_RX_DROP;
583}
584
585
586
587static int dn_route_rx_short(struct sk_buff *skb)
588{
589	struct dn_skb_cb *cb = DN_SKB_CB(skb);
590	unsigned char *ptr = skb->data;
591
592	if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
593		goto drop_it;
594
595	skb_pull(skb, 5);
596	skb_reset_transport_header(skb);
597
598	cb->dst = *(__le16 *)ptr;
599	ptr += 2;
600	cb->src = *(__le16 *)ptr;
601	ptr += 2;
602	cb->hops = *ptr & 0x3f;
603
604	return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
605		       dn_route_rx_packet);
606
607drop_it:
608	kfree_skb(skb);
609	return NET_RX_DROP;
610}
611
612static int dn_route_discard(struct sk_buff *skb)
613{
614	/*
615	 * I know we drop the packet here, but thats considered success in
616	 * this case
617	 */
618	kfree_skb(skb);
619	return NET_RX_SUCCESS;
620}
621
622static int dn_route_ptp_hello(struct sk_buff *skb)
623{
624	dn_dev_hello(skb);
625	dn_neigh_pointopoint_hello(skb);
626	return NET_RX_SUCCESS;
627}
628
629int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
630{
631	struct dn_skb_cb *cb;
632	unsigned char flags = 0;
633	__u16 len = le16_to_cpu(*(__le16 *)skb->data);
634	struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
635	unsigned char padlen = 0;
636
637	if (!net_eq(dev_net(dev), &init_net))
638		goto dump_it;
639
640	if (dn == NULL)
641		goto dump_it;
642
643	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
644		goto out;
645
646	if (!pskb_may_pull(skb, 3))
647		goto dump_it;
648
649	skb_pull(skb, 2);
650
651	if (len > skb->len)
652		goto dump_it;
653
654	skb_trim(skb, len);
655
656	flags = *skb->data;
657
658	cb = DN_SKB_CB(skb);
659	cb->stamp = jiffies;
660	cb->iif = dev->ifindex;
661
662	/*
663	 * If we have padding, remove it.
664	 */
665	if (flags & DN_RT_F_PF) {
666		padlen = flags & ~DN_RT_F_PF;
667		if (!pskb_may_pull(skb, padlen + 1))
668			goto dump_it;
669		skb_pull(skb, padlen);
670		flags = *skb->data;
671	}
672
673	skb_reset_network_header(skb);
674
675	/*
676	 * Weed out future version DECnet
677	 */
678	if (flags & DN_RT_F_VER)
679		goto dump_it;
680
681	cb->rt_flags = flags;
682
683	if (decnet_debug_level & 1)
684		printk(KERN_DEBUG
685			"dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
686			(int)flags, (dev) ? dev->name : "???", len, skb->len,
687			padlen);
688
689	if (flags & DN_RT_PKT_CNTL) {
690		if (unlikely(skb_linearize(skb)))
691			goto dump_it;
692
693		switch (flags & DN_RT_CNTL_MSK) {
694		case DN_RT_PKT_INIT:
695			dn_dev_init_pkt(skb);
696			break;
697		case DN_RT_PKT_VERI:
698			dn_dev_veri_pkt(skb);
699			break;
700		}
701
702		if (dn->parms.state != DN_DEV_S_RU)
703			goto dump_it;
704
705		switch (flags & DN_RT_CNTL_MSK) {
706		case DN_RT_PKT_HELO:
707			return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
708				       skb, skb->dev, NULL,
709				       dn_route_ptp_hello);
710
711		case DN_RT_PKT_L1RT:
712		case DN_RT_PKT_L2RT:
713			return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
714				       skb, skb->dev, NULL,
715				       dn_route_discard);
716		case DN_RT_PKT_ERTH:
717			return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
718				       skb, skb->dev, NULL,
719				       dn_neigh_router_hello);
720
721		case DN_RT_PKT_EEDH:
722			return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
723				       skb, skb->dev, NULL,
724				       dn_neigh_endnode_hello);
725		}
726	} else {
727		if (dn->parms.state != DN_DEV_S_RU)
728			goto dump_it;
729
730		skb_pull(skb, 1); /* Pull flags */
731
732		switch (flags & DN_RT_PKT_MSK) {
733		case DN_RT_PKT_LONG:
734			return dn_route_rx_long(skb);
735		case DN_RT_PKT_SHORT:
736			return dn_route_rx_short(skb);
737		}
738	}
739
740dump_it:
741	kfree_skb(skb);
742out:
743	return NET_RX_DROP;
744}
745
746static int dn_to_neigh_output(struct sk_buff *skb)
747{
748	struct dst_entry *dst = skb_dst(skb);
749	struct dn_route *rt = (struct dn_route *) dst;
750	struct neighbour *n = rt->n;
751
752	return n->output(n, skb);
753}
754
755static int dn_output(struct sk_buff *skb)
756{
757	struct dst_entry *dst = skb_dst(skb);
758	struct dn_route *rt = (struct dn_route *)dst;
759	struct net_device *dev = dst->dev;
760	struct dn_skb_cb *cb = DN_SKB_CB(skb);
761
762	int err = -EINVAL;
763
764	if (rt->n == NULL)
765		goto error;
766
767	skb->dev = dev;
768
769	cb->src = rt->rt_saddr;
770	cb->dst = rt->rt_daddr;
771
772	/*
773	 * Always set the Intra-Ethernet bit on all outgoing packets
774	 * originated on this node. Only valid flag from upper layers
775	 * is return-to-sender-requested. Set hop count to 0 too.
776	 */
777	cb->rt_flags &= ~DN_RT_F_RQR;
778	cb->rt_flags |= DN_RT_F_IE;
779	cb->hops = 0;
780
781	return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
782		       dn_to_neigh_output);
783
784error:
785	net_dbg_ratelimited("dn_output: This should not happen\n");
786
787	kfree_skb(skb);
788
789	return err;
790}
791
792static int dn_forward(struct sk_buff *skb)
793{
794	struct dn_skb_cb *cb = DN_SKB_CB(skb);
795	struct dst_entry *dst = skb_dst(skb);
796	struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
797	struct dn_route *rt;
798	int header_len;
799#ifdef CONFIG_NETFILTER
800	struct net_device *dev = skb->dev;
801#endif
802
803	if (skb->pkt_type != PACKET_HOST)
804		goto drop;
805
806	/* Ensure that we have enough space for headers */
807	rt = (struct dn_route *)skb_dst(skb);
808	header_len = dn_db->use_long ? 21 : 6;
809	if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
810		goto drop;
811
812	/*
813	 * Hop count exceeded.
814	 */
815	if (++cb->hops > 30)
816		goto drop;
817
818	skb->dev = rt->dst.dev;
819
820	/*
821	 * If packet goes out same interface it came in on, then set
822	 * the Intra-Ethernet bit. This has no effect for short
823	 * packets, so we don't need to test for them here.
824	 */
825	cb->rt_flags &= ~DN_RT_F_IE;
826	if (rt->rt_flags & RTCF_DOREDIRECT)
827		cb->rt_flags |= DN_RT_F_IE;
828
829	return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
830		       dn_to_neigh_output);
831
832drop:
833	kfree_skb(skb);
834	return NET_RX_DROP;
835}
836
837/*
838 * Used to catch bugs. This should never normally get
839 * called.
840 */
841static int dn_rt_bug(struct sk_buff *skb)
842{
843	struct dn_skb_cb *cb = DN_SKB_CB(skb);
844
845	net_dbg_ratelimited("dn_rt_bug: skb from:%04x to:%04x\n",
846			    le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
847
848	kfree_skb(skb);
849
850	return NET_RX_DROP;
851}
852
853static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
854{
855	return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
856}
857
858static unsigned int dn_dst_mtu(const struct dst_entry *dst)
859{
860	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
861
862	return mtu ? : dst->dev->mtu;
863}
864
865static struct neighbour *dn_dst_neigh_lookup(const struct dst_entry *dst,
866					     struct sk_buff *skb,
867					     const void *daddr)
868{
869	return __neigh_lookup_errno(&dn_neigh_table, daddr, dst->dev);
870}
871
872static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
873{
874	struct dn_fib_info *fi = res->fi;
875	struct net_device *dev = rt->dst.dev;
876	unsigned int mss_metric;
877	struct neighbour *n;
878
879	if (fi) {
880		if (DN_FIB_RES_GW(*res) &&
881		    DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
882			rt->rt_gateway = DN_FIB_RES_GW(*res);
883		dst_init_metrics(&rt->dst, fi->fib_metrics, true);
884	}
885	rt->rt_type = res->type;
886
887	if (dev != NULL && rt->n == NULL) {
888		n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
889		if (IS_ERR(n))
890			return PTR_ERR(n);
891		rt->n = n;
892	}
893
894	if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
895		dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
896	mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
897	if (mss_metric) {
898		unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
899		if (mss_metric > mss)
900			dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
901	}
902	return 0;
903}
904
905static inline int dn_match_addr(__le16 addr1, __le16 addr2)
906{
907	__u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
908	int match = 16;
909	while(tmp) {
910		tmp >>= 1;
911		match--;
912	}
913	return match;
914}
915
916static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
917{
918	__le16 saddr = 0;
919	struct dn_dev *dn_db;
920	struct dn_ifaddr *ifa;
921	int best_match = 0;
922	int ret;
923
924	rcu_read_lock();
925	dn_db = rcu_dereference(dev->dn_ptr);
926	for (ifa = rcu_dereference(dn_db->ifa_list);
927	     ifa != NULL;
928	     ifa = rcu_dereference(ifa->ifa_next)) {
929		if (ifa->ifa_scope > scope)
930			continue;
931		if (!daddr) {
932			saddr = ifa->ifa_local;
933			break;
934		}
935		ret = dn_match_addr(daddr, ifa->ifa_local);
936		if (ret > best_match)
937			saddr = ifa->ifa_local;
938		if (best_match == 0)
939			saddr = ifa->ifa_local;
940	}
941	rcu_read_unlock();
942
943	return saddr;
944}
945
946static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
947{
948	return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
949}
950
951static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
952{
953	__le16 mask = dnet_make_mask(res->prefixlen);
954	return (daddr&~mask)|res->fi->fib_nh->nh_gw;
955}
956
957static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oldflp, int try_hard)
958{
959	struct flowidn fld = {
960		.daddr = oldflp->daddr,
961		.saddr = oldflp->saddr,
962		.flowidn_scope = RT_SCOPE_UNIVERSE,
963		.flowidn_mark = oldflp->flowidn_mark,
964		.flowidn_iif = LOOPBACK_IFINDEX,
965		.flowidn_oif = oldflp->flowidn_oif,
966	};
967	struct dn_route *rt = NULL;
968	struct net_device *dev_out = NULL, *dev;
969	struct neighbour *neigh = NULL;
970	unsigned int hash;
971	unsigned int flags = 0;
972	struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
973	int err;
974	int free_res = 0;
975	__le16 gateway = 0;
976
977	if (decnet_debug_level & 16)
978		printk(KERN_DEBUG
979		       "dn_route_output_slow: dst=%04x src=%04x mark=%d"
980		       " iif=%d oif=%d\n", le16_to_cpu(oldflp->daddr),
981		       le16_to_cpu(oldflp->saddr),
982		       oldflp->flowidn_mark, LOOPBACK_IFINDEX,
983		       oldflp->flowidn_oif);
984
985	/* If we have an output interface, verify its a DECnet device */
986	if (oldflp->flowidn_oif) {
987		dev_out = dev_get_by_index(&init_net, oldflp->flowidn_oif);
988		err = -ENODEV;
989		if (dev_out && dev_out->dn_ptr == NULL) {
990			dev_put(dev_out);
991			dev_out = NULL;
992		}
993		if (dev_out == NULL)
994			goto out;
995	}
996
997	/* If we have a source address, verify that its a local address */
998	if (oldflp->saddr) {
999		err = -EADDRNOTAVAIL;
1000
1001		if (dev_out) {
1002			if (dn_dev_islocal(dev_out, oldflp->saddr))
1003				goto source_ok;
1004			dev_put(dev_out);
1005			goto out;
1006		}
1007		rcu_read_lock();
1008		for_each_netdev_rcu(&init_net, dev) {
1009			if (!dev->dn_ptr)
1010				continue;
1011			if (!dn_dev_islocal(dev, oldflp->saddr))
1012				continue;
1013			if ((dev->flags & IFF_LOOPBACK) &&
1014			    oldflp->daddr &&
1015			    !dn_dev_islocal(dev, oldflp->daddr))
1016				continue;
1017
1018			dev_out = dev;
1019			break;
1020		}
1021		rcu_read_unlock();
1022		if (dev_out == NULL)
1023			goto out;
1024		dev_hold(dev_out);
1025source_ok:
1026		;
1027	}
1028
1029	/* No destination? Assume its local */
1030	if (!fld.daddr) {
1031		fld.daddr = fld.saddr;
1032
1033		err = -EADDRNOTAVAIL;
1034		if (dev_out)
1035			dev_put(dev_out);
1036		dev_out = init_net.loopback_dev;
1037		dev_hold(dev_out);
1038		if (!fld.daddr) {
1039			fld.daddr =
1040			fld.saddr = dnet_select_source(dev_out, 0,
1041						       RT_SCOPE_HOST);
1042			if (!fld.daddr)
1043				goto out;
1044		}
1045		fld.flowidn_oif = LOOPBACK_IFINDEX;
1046		res.type = RTN_LOCAL;
1047		goto make_route;
1048	}
1049
1050	if (decnet_debug_level & 16)
1051		printk(KERN_DEBUG
1052		       "dn_route_output_slow: initial checks complete."
1053		       " dst=%o4x src=%04x oif=%d try_hard=%d\n",
1054		       le16_to_cpu(fld.daddr), le16_to_cpu(fld.saddr),
1055		       fld.flowidn_oif, try_hard);
1056
1057	/*
1058	 * N.B. If the kernel is compiled without router support then
1059	 * dn_fib_lookup() will evaluate to non-zero so this if () block
1060	 * will always be executed.
1061	 */
1062	err = -ESRCH;
1063	if (try_hard || (err = dn_fib_lookup(&fld, &res)) != 0) {
1064		struct dn_dev *dn_db;
1065		if (err != -ESRCH)
1066			goto out;
1067		/*
1068		 * Here the fallback is basically the standard algorithm for
1069		 * routing in endnodes which is described in the DECnet routing
1070		 * docs
1071		 *
1072		 * If we are not trying hard, look in neighbour cache.
1073		 * The result is tested to ensure that if a specific output
1074		 * device/source address was requested, then we honour that
1075		 * here
1076		 */
1077		if (!try_hard) {
1078			neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fld.daddr);
1079			if (neigh) {
1080				if ((oldflp->flowidn_oif &&
1081				    (neigh->dev->ifindex != oldflp->flowidn_oif)) ||
1082				    (oldflp->saddr &&
1083				    (!dn_dev_islocal(neigh->dev,
1084						     oldflp->saddr)))) {
1085					neigh_release(neigh);
1086					neigh = NULL;
1087				} else {
1088					if (dev_out)
1089						dev_put(dev_out);
1090					if (dn_dev_islocal(neigh->dev, fld.daddr)) {
1091						dev_out = init_net.loopback_dev;
1092						res.type = RTN_LOCAL;
1093					} else {
1094						dev_out = neigh->dev;
1095					}
1096					dev_hold(dev_out);
1097					goto select_source;
1098				}
1099			}
1100		}
1101
1102		/* Not there? Perhaps its a local address */
1103		if (dev_out == NULL)
1104			dev_out = dn_dev_get_default();
1105		err = -ENODEV;
1106		if (dev_out == NULL)
1107			goto out;
1108		dn_db = rcu_dereference_raw(dev_out->dn_ptr);
1109		/* Possible improvement - check all devices for local addr */
1110		if (dn_dev_islocal(dev_out, fld.daddr)) {
1111			dev_put(dev_out);
1112			dev_out = init_net.loopback_dev;
1113			dev_hold(dev_out);
1114			res.type = RTN_LOCAL;
1115			goto select_source;
1116		}
1117		/* Not local either.... try sending it to the default router */
1118		neigh = neigh_clone(dn_db->router);
1119		BUG_ON(neigh && neigh->dev != dev_out);
1120
1121		/* Ok then, we assume its directly connected and move on */
1122select_source:
1123		if (neigh)
1124			gateway = ((struct dn_neigh *)neigh)->addr;
1125		if (gateway == 0)
1126			gateway = fld.daddr;
1127		if (fld.saddr == 0) {
1128			fld.saddr = dnet_select_source(dev_out, gateway,
1129						       res.type == RTN_LOCAL ?
1130						       RT_SCOPE_HOST :
1131						       RT_SCOPE_LINK);
1132			if (fld.saddr == 0 && res.type != RTN_LOCAL)
1133				goto e_addr;
1134		}
1135		fld.flowidn_oif = dev_out->ifindex;
1136		goto make_route;
1137	}
1138	free_res = 1;
1139
1140	if (res.type == RTN_NAT)
1141		goto e_inval;
1142
1143	if (res.type == RTN_LOCAL) {
1144		if (!fld.saddr)
1145			fld.saddr = fld.daddr;
1146		if (dev_out)
1147			dev_put(dev_out);
1148		dev_out = init_net.loopback_dev;
1149		dev_hold(dev_out);
1150		fld.flowidn_oif = dev_out->ifindex;
1151		if (res.fi)
1152			dn_fib_info_put(res.fi);
1153		res.fi = NULL;
1154		goto make_route;
1155	}
1156
1157	if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1158		dn_fib_select_multipath(&fld, &res);
1159
1160	/*
1161	 * We could add some logic to deal with default routes here and
1162	 * get rid of some of the special casing above.
1163	 */
1164
1165	if (!fld.saddr)
1166		fld.saddr = DN_FIB_RES_PREFSRC(res);
1167
1168	if (dev_out)
1169		dev_put(dev_out);
1170	dev_out = DN_FIB_RES_DEV(res);
1171	dev_hold(dev_out);
1172	fld.flowidn_oif = dev_out->ifindex;
1173	gateway = DN_FIB_RES_GW(res);
1174
1175make_route:
1176	if (dev_out->flags & IFF_LOOPBACK)
1177		flags |= RTCF_LOCAL;
1178
1179	rt = dst_alloc(&dn_dst_ops, dev_out, 1, DST_OBSOLETE_NONE, DST_HOST);
1180	if (rt == NULL)
1181		goto e_nobufs;
1182
1183	memset(&rt->fld, 0, sizeof(rt->fld));
1184	rt->fld.saddr        = oldflp->saddr;
1185	rt->fld.daddr        = oldflp->daddr;
1186	rt->fld.flowidn_oif  = oldflp->flowidn_oif;
1187	rt->fld.flowidn_iif  = 0;
1188	rt->fld.flowidn_mark = oldflp->flowidn_mark;
1189
1190	rt->rt_saddr      = fld.saddr;
1191	rt->rt_daddr      = fld.daddr;
1192	rt->rt_gateway    = gateway ? gateway : fld.daddr;
1193	rt->rt_local_src  = fld.saddr;
1194
1195	rt->rt_dst_map    = fld.daddr;
1196	rt->rt_src_map    = fld.saddr;
1197
1198	rt->n = neigh;
1199	neigh = NULL;
1200
1201	rt->dst.lastuse = jiffies;
1202	rt->dst.output  = dn_output;
1203	rt->dst.input   = dn_rt_bug;
1204	rt->rt_flags      = flags;
1205	if (flags & RTCF_LOCAL)
1206		rt->dst.input = dn_nsp_rx;
1207
1208	err = dn_rt_set_next_hop(rt, &res);
1209	if (err)
1210		goto e_neighbour;
1211
1212	hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
1213	dn_insert_route(rt, hash, (struct dn_route **)pprt);
1214
1215done:
1216	if (neigh)
1217		neigh_release(neigh);
1218	if (free_res)
1219		dn_fib_res_put(&res);
1220	if (dev_out)
1221		dev_put(dev_out);
1222out:
1223	return err;
1224
1225e_addr:
1226	err = -EADDRNOTAVAIL;
1227	goto done;
1228e_inval:
1229	err = -EINVAL;
1230	goto done;
1231e_nobufs:
1232	err = -ENOBUFS;
1233	goto done;
1234e_neighbour:
1235	dst_free(&rt->dst);
1236	goto e_nobufs;
1237}
1238
1239
1240/*
1241 * N.B. The flags may be moved into the flowi at some future stage.
1242 */
1243static int __dn_route_output_key(struct dst_entry **pprt, const struct flowidn *flp, int flags)
1244{
1245	unsigned int hash = dn_hash(flp->saddr, flp->daddr);
1246	struct dn_route *rt = NULL;
1247
1248	if (!(flags & MSG_TRYHARD)) {
1249		rcu_read_lock_bh();
1250		for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
1251			rt = rcu_dereference_bh(rt->dst.dn_next)) {
1252			if ((flp->daddr == rt->fld.daddr) &&
1253			    (flp->saddr == rt->fld.saddr) &&
1254			    (flp->flowidn_mark == rt->fld.flowidn_mark) &&
1255			    dn_is_output_route(rt) &&
1256			    (rt->fld.flowidn_oif == flp->flowidn_oif)) {
1257				dst_use(&rt->dst, jiffies);
1258				rcu_read_unlock_bh();
1259				*pprt = &rt->dst;
1260				return 0;
1261			}
1262		}
1263		rcu_read_unlock_bh();
1264	}
1265
1266	return dn_route_output_slow(pprt, flp, flags);
1267}
1268
1269static int dn_route_output_key(struct dst_entry **pprt, struct flowidn *flp, int flags)
1270{
1271	int err;
1272
1273	err = __dn_route_output_key(pprt, flp, flags);
1274	if (err == 0 && flp->flowidn_proto) {
1275		*pprt = xfrm_lookup(&init_net, *pprt,
1276				    flowidn_to_flowi(flp), NULL, 0);
1277		if (IS_ERR(*pprt)) {
1278			err = PTR_ERR(*pprt);
1279			*pprt = NULL;
1280		}
1281	}
1282	return err;
1283}
1284
1285int dn_route_output_sock(struct dst_entry __rcu **pprt, struct flowidn *fl, struct sock *sk, int flags)
1286{
1287	int err;
1288
1289	err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
1290	if (err == 0 && fl->flowidn_proto) {
1291		if (!(flags & MSG_DONTWAIT))
1292			fl->flowidn_flags |= FLOWI_FLAG_CAN_SLEEP;
1293		*pprt = xfrm_lookup(&init_net, *pprt,
1294				    flowidn_to_flowi(fl), sk, 0);
1295		if (IS_ERR(*pprt)) {
1296			err = PTR_ERR(*pprt);
1297			*pprt = NULL;
1298		}
1299	}
1300	return err;
1301}
1302
1303static int dn_route_input_slow(struct sk_buff *skb)
1304{
1305	struct dn_route *rt = NULL;
1306	struct dn_skb_cb *cb = DN_SKB_CB(skb);
1307	struct net_device *in_dev = skb->dev;
1308	struct net_device *out_dev = NULL;
1309	struct dn_dev *dn_db;
1310	struct neighbour *neigh = NULL;
1311	unsigned int hash;
1312	int flags = 0;
1313	__le16 gateway = 0;
1314	__le16 local_src = 0;
1315	struct flowidn fld = {
1316		.daddr = cb->dst,
1317		.saddr = cb->src,
1318		.flowidn_scope = RT_SCOPE_UNIVERSE,
1319		.flowidn_mark = skb->mark,
1320		.flowidn_iif = skb->dev->ifindex,
1321	};
1322	struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1323	int err = -EINVAL;
1324	int free_res = 0;
1325
1326	dev_hold(in_dev);
1327
1328	if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
1329		goto out;
1330
1331	/* Zero source addresses are not allowed */
1332	if (fld.saddr == 0)
1333		goto out;
1334
1335	/*
1336	 * In this case we've just received a packet from a source
1337	 * outside ourselves pretending to come from us. We don't
1338	 * allow it any further to prevent routing loops, spoofing and
1339	 * other nasties. Loopback packets already have the dst attached
1340	 * so this only affects packets which have originated elsewhere.
1341	 */
1342	err  = -ENOTUNIQ;
1343	if (dn_dev_islocal(in_dev, cb->src))
1344		goto out;
1345
1346	err = dn_fib_lookup(&fld, &res);
1347	if (err) {
1348		if (err != -ESRCH)
1349			goto out;
1350		/*
1351		 * Is the destination us ?
1352		 */
1353		if (!dn_dev_islocal(in_dev, cb->dst))
1354			goto e_inval;
1355
1356		res.type = RTN_LOCAL;
1357	} else {
1358		__le16 src_map = fld.saddr;
1359		free_res = 1;
1360
1361		out_dev = DN_FIB_RES_DEV(res);
1362		if (out_dev == NULL) {
1363			net_crit_ratelimited("Bug in dn_route_input_slow() No output device\n");
1364			goto e_inval;
1365		}
1366		dev_hold(out_dev);
1367
1368		if (res.r)
1369			src_map = fld.saddr; /* no NAT support for now */
1370
1371		gateway = DN_FIB_RES_GW(res);
1372		if (res.type == RTN_NAT) {
1373			fld.daddr = dn_fib_rules_map_destination(fld.daddr, &res);
1374			dn_fib_res_put(&res);
1375			free_res = 0;
1376			if (dn_fib_lookup(&fld, &res))
1377				goto e_inval;
1378			free_res = 1;
1379			if (res.type != RTN_UNICAST)
1380				goto e_inval;
1381			flags |= RTCF_DNAT;
1382			gateway = fld.daddr;
1383		}
1384		fld.saddr = src_map;
1385	}
1386
1387	switch(res.type) {
1388	case RTN_UNICAST:
1389		/*
1390		 * Forwarding check here, we only check for forwarding
1391		 * being turned off, if you want to only forward intra
1392		 * area, its up to you to set the routing tables up
1393		 * correctly.
1394		 */
1395		if (dn_db->parms.forwarding == 0)
1396			goto e_inval;
1397
1398		if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1399			dn_fib_select_multipath(&fld, &res);
1400
1401		/*
1402		 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1403		 * flag as a hint to set the intra-ethernet bit when
1404		 * forwarding. If we've got NAT in operation, we don't do
1405		 * this optimisation.
1406		 */
1407		if (out_dev == in_dev && !(flags & RTCF_NAT))
1408			flags |= RTCF_DOREDIRECT;
1409
1410		local_src = DN_FIB_RES_PREFSRC(res);
1411
1412	case RTN_BLACKHOLE:
1413	case RTN_UNREACHABLE:
1414		break;
1415	case RTN_LOCAL:
1416		flags |= RTCF_LOCAL;
1417		fld.saddr = cb->dst;
1418		fld.daddr = cb->src;
1419
1420		/* Routing tables gave us a gateway */
1421		if (gateway)
1422			goto make_route;
1423
1424		/* Packet was intra-ethernet, so we know its on-link */
1425		if (cb->rt_flags & DN_RT_F_IE) {
1426			gateway = cb->src;
1427			goto make_route;
1428		}
1429
1430		/* Use the default router if there is one */
1431		neigh = neigh_clone(dn_db->router);
1432		if (neigh) {
1433			gateway = ((struct dn_neigh *)neigh)->addr;
1434			goto make_route;
1435		}
1436
1437		/* Close eyes and pray */
1438		gateway = cb->src;
1439		goto make_route;
1440	default:
1441		goto e_inval;
1442	}
1443
1444make_route:
1445	rt = dst_alloc(&dn_dst_ops, out_dev, 0, DST_OBSOLETE_NONE, DST_HOST);
1446	if (rt == NULL)
1447		goto e_nobufs;
1448
1449	memset(&rt->fld, 0, sizeof(rt->fld));
1450	rt->rt_saddr      = fld.saddr;
1451	rt->rt_daddr      = fld.daddr;
1452	rt->rt_gateway    = fld.daddr;
1453	if (gateway)
1454		rt->rt_gateway = gateway;
1455	rt->rt_local_src  = local_src ? local_src : rt->rt_saddr;
1456
1457	rt->rt_dst_map    = fld.daddr;
1458	rt->rt_src_map    = fld.saddr;
1459
1460	rt->fld.saddr        = cb->src;
1461	rt->fld.daddr        = cb->dst;
1462	rt->fld.flowidn_oif  = 0;
1463	rt->fld.flowidn_iif  = in_dev->ifindex;
1464	rt->fld.flowidn_mark = fld.flowidn_mark;
1465
1466	rt->n = neigh;
1467	rt->dst.lastuse = jiffies;
1468	rt->dst.output = dn_rt_bug;
1469	switch (res.type) {
1470	case RTN_UNICAST:
1471		rt->dst.input = dn_forward;
1472		break;
1473	case RTN_LOCAL:
1474		rt->dst.output = dn_output;
1475		rt->dst.input = dn_nsp_rx;
1476		rt->dst.dev = in_dev;
1477		flags |= RTCF_LOCAL;
1478		break;
1479	default:
1480	case RTN_UNREACHABLE:
1481	case RTN_BLACKHOLE:
1482		rt->dst.input = dst_discard;
1483	}
1484	rt->rt_flags = flags;
1485
1486	err = dn_rt_set_next_hop(rt, &res);
1487	if (err)
1488		goto e_neighbour;
1489
1490	hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
1491	dn_insert_route(rt, hash, &rt);
1492	skb_dst_set(skb, &rt->dst);
1493
1494done:
1495	if (neigh)
1496		neigh_release(neigh);
1497	if (free_res)
1498		dn_fib_res_put(&res);
1499	dev_put(in_dev);
1500	if (out_dev)
1501		dev_put(out_dev);
1502out:
1503	return err;
1504
1505e_inval:
1506	err = -EINVAL;
1507	goto done;
1508
1509e_nobufs:
1510	err = -ENOBUFS;
1511	goto done;
1512
1513e_neighbour:
1514	dst_free(&rt->dst);
1515	goto done;
1516}
1517
1518static int dn_route_input(struct sk_buff *skb)
1519{
1520	struct dn_route *rt;
1521	struct dn_skb_cb *cb = DN_SKB_CB(skb);
1522	unsigned int hash = dn_hash(cb->src, cb->dst);
1523
1524	if (skb_dst(skb))
1525		return 0;
1526
1527	rcu_read_lock();
1528	for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
1529	    rt = rcu_dereference(rt->dst.dn_next)) {
1530		if ((rt->fld.saddr == cb->src) &&
1531		    (rt->fld.daddr == cb->dst) &&
1532		    (rt->fld.flowidn_oif == 0) &&
1533		    (rt->fld.flowidn_mark == skb->mark) &&
1534		    (rt->fld.flowidn_iif == cb->iif)) {
1535			dst_use(&rt->dst, jiffies);
1536			rcu_read_unlock();
1537			skb_dst_set(skb, (struct dst_entry *)rt);
1538			return 0;
1539		}
1540	}
1541	rcu_read_unlock();
1542
1543	return dn_route_input_slow(skb);
1544}
1545
1546static int dn_rt_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1547			   int event, int nowait, unsigned int flags)
1548{
1549	struct dn_route *rt = (struct dn_route *)skb_dst(skb);
1550	struct rtmsg *r;
1551	struct nlmsghdr *nlh;
1552	long expires;
1553
1554	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*r), flags);
1555	if (!nlh)
1556		return -EMSGSIZE;
1557
1558	r = nlmsg_data(nlh);
1559	r->rtm_family = AF_DECnet;
1560	r->rtm_dst_len = 16;
1561	r->rtm_src_len = 0;
1562	r->rtm_tos = 0;
1563	r->rtm_table = RT_TABLE_MAIN;
1564	r->rtm_type = rt->rt_type;
1565	r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1566	r->rtm_scope = RT_SCOPE_UNIVERSE;
1567	r->rtm_protocol = RTPROT_UNSPEC;
1568
1569	if (rt->rt_flags & RTCF_NOTIFY)
1570		r->rtm_flags |= RTM_F_NOTIFY;
1571
1572	if (nla_put_u32(skb, RTA_TABLE, RT_TABLE_MAIN) < 0 ||
1573	    nla_put_le16(skb, RTA_DST, rt->rt_daddr) < 0)
1574		goto errout;
1575
1576	if (rt->fld.saddr) {
1577		r->rtm_src_len = 16;
1578		if (nla_put_le16(skb, RTA_SRC, rt->fld.saddr) < 0)
1579			goto errout;
1580	}
1581	if (rt->dst.dev &&
1582	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex) < 0)
1583		goto errout;
1584
1585	/*
1586	 * Note to self - change this if input routes reverse direction when
1587	 * they deal only with inputs and not with replies like they do
1588	 * currently.
1589	 */
1590	if (nla_put_le16(skb, RTA_PREFSRC, rt->rt_local_src) < 0)
1591		goto errout;
1592
1593	if (rt->rt_daddr != rt->rt_gateway &&
1594	    nla_put_le16(skb, RTA_GATEWAY, rt->rt_gateway) < 0)
1595		goto errout;
1596
1597	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
1598		goto errout;
1599
1600	expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
1601	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires,
1602			       rt->dst.error) < 0)
1603		goto errout;
1604
1605	if (dn_is_input_route(rt) &&
1606	    nla_put_u32(skb, RTA_IIF, rt->fld.flowidn_iif) < 0)
1607		goto errout;
1608
1609	return nlmsg_end(skb, nlh);
1610
1611errout:
1612	nlmsg_cancel(skb, nlh);
1613	return -EMSGSIZE;
1614}
1615
1616const struct nla_policy rtm_dn_policy[RTA_MAX + 1] = {
1617	[RTA_DST]		= { .type = NLA_U16 },
1618	[RTA_SRC]		= { .type = NLA_U16 },
1619	[RTA_IIF]		= { .type = NLA_U32 },
1620	[RTA_OIF]		= { .type = NLA_U32 },
1621	[RTA_GATEWAY]		= { .type = NLA_U16 },
1622	[RTA_PRIORITY]		= { .type = NLA_U32 },
1623	[RTA_PREFSRC]		= { .type = NLA_U16 },
1624	[RTA_METRICS]		= { .type = NLA_NESTED },
1625	[RTA_MULTIPATH]		= { .type = NLA_NESTED },
1626	[RTA_TABLE]		= { .type = NLA_U32 },
1627	[RTA_MARK]		= { .type = NLA_U32 },
1628};
1629
1630/*
1631 * This is called by both endnodes and routers now.
1632 */
1633static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
1634{
1635	struct net *net = sock_net(in_skb->sk);
1636	struct rtmsg *rtm = nlmsg_data(nlh);
1637	struct dn_route *rt = NULL;
1638	struct dn_skb_cb *cb;
1639	int err;
1640	struct sk_buff *skb;
1641	struct flowidn fld;
1642	struct nlattr *tb[RTA_MAX+1];
1643
1644	if (!net_eq(net, &init_net))
1645		return -EINVAL;
1646
1647	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_dn_policy);
1648	if (err < 0)
1649		return err;
1650
1651	memset(&fld, 0, sizeof(fld));
1652	fld.flowidn_proto = DNPROTO_NSP;
1653
1654	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1655	if (skb == NULL)
1656		return -ENOBUFS;
1657	skb_reset_mac_header(skb);
1658	cb = DN_SKB_CB(skb);
1659
1660	if (tb[RTA_SRC])
1661		fld.saddr = nla_get_le16(tb[RTA_SRC]);
1662
1663	if (tb[RTA_DST])
1664		fld.daddr = nla_get_le16(tb[RTA_DST]);
1665
1666	if (tb[RTA_IIF])
1667		fld.flowidn_iif = nla_get_u32(tb[RTA_IIF]);
1668
1669	if (fld.flowidn_iif) {
1670		struct net_device *dev;
1671		if ((dev = dev_get_by_index(&init_net, fld.flowidn_iif)) == NULL) {
1672			kfree_skb(skb);
1673			return -ENODEV;
1674		}
1675		if (!dev->dn_ptr) {
1676			dev_put(dev);
1677			kfree_skb(skb);
1678			return -ENODEV;
1679		}
1680		skb->protocol = htons(ETH_P_DNA_RT);
1681		skb->dev = dev;
1682		cb->src = fld.saddr;
1683		cb->dst = fld.daddr;
1684		local_bh_disable();
1685		err = dn_route_input(skb);
1686		local_bh_enable();
1687		memset(cb, 0, sizeof(struct dn_skb_cb));
1688		rt = (struct dn_route *)skb_dst(skb);
1689		if (!err && -rt->dst.error)
1690			err = rt->dst.error;
1691	} else {
1692		if (tb[RTA_OIF])
1693			fld.flowidn_oif = nla_get_u32(tb[RTA_OIF]);
1694
1695		err = dn_route_output_key((struct dst_entry **)&rt, &fld, 0);
1696	}
1697
1698	if (skb->dev)
1699		dev_put(skb->dev);
1700	skb->dev = NULL;
1701	if (err)
1702		goto out_free;
1703	skb_dst_set(skb, &rt->dst);
1704	if (rtm->rtm_flags & RTM_F_NOTIFY)
1705		rt->rt_flags |= RTCF_NOTIFY;
1706
1707	err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, RTM_NEWROUTE, 0, 0);
1708
1709	if (err == 0)
1710		goto out_free;
1711	if (err < 0) {
1712		err = -EMSGSIZE;
1713		goto out_free;
1714	}
1715
1716	return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).portid);
1717
1718out_free:
1719	kfree_skb(skb);
1720	return err;
1721}
1722
1723/*
1724 * For routers, this is called from dn_fib_dump, but for endnodes its
1725 * called directly from the rtnetlink dispatch table.
1726 */
1727int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1728{
1729	struct net *net = sock_net(skb->sk);
1730	struct dn_route *rt;
1731	int h, s_h;
1732	int idx, s_idx;
1733	struct rtmsg *rtm;
1734
1735	if (!net_eq(net, &init_net))
1736		return 0;
1737
1738	if (nlmsg_len(cb->nlh) < sizeof(struct rtmsg))
1739		return -EINVAL;
1740
1741	rtm = nlmsg_data(cb->nlh);
1742	if (!(rtm->rtm_flags & RTM_F_CLONED))
1743		return 0;
1744
1745	s_h = cb->args[0];
1746	s_idx = idx = cb->args[1];
1747	for(h = 0; h <= dn_rt_hash_mask; h++) {
1748		if (h < s_h)
1749			continue;
1750		if (h > s_h)
1751			s_idx = 0;
1752		rcu_read_lock_bh();
1753		for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
1754			rt;
1755			rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
1756			if (idx < s_idx)
1757				continue;
1758			skb_dst_set(skb, dst_clone(&rt->dst));
1759			if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).portid,
1760					cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1761					1, NLM_F_MULTI) <= 0) {
1762				skb_dst_drop(skb);
1763				rcu_read_unlock_bh();
1764				goto done;
1765			}
1766			skb_dst_drop(skb);
1767		}
1768		rcu_read_unlock_bh();
1769	}
1770
1771done:
1772	cb->args[0] = h;
1773	cb->args[1] = idx;
1774	return skb->len;
1775}
1776
1777#ifdef CONFIG_PROC_FS
1778struct dn_rt_cache_iter_state {
1779	int bucket;
1780};
1781
1782static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1783{
1784	struct dn_route *rt = NULL;
1785	struct dn_rt_cache_iter_state *s = seq->private;
1786
1787	for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1788		rcu_read_lock_bh();
1789		rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
1790		if (rt)
1791			break;
1792		rcu_read_unlock_bh();
1793	}
1794	return rt;
1795}
1796
1797static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1798{
1799	struct dn_rt_cache_iter_state *s = seq->private;
1800
1801	rt = rcu_dereference_bh(rt->dst.dn_next);
1802	while (!rt) {
1803		rcu_read_unlock_bh();
1804		if (--s->bucket < 0)
1805			break;
1806		rcu_read_lock_bh();
1807		rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
1808	}
1809	return rt;
1810}
1811
1812static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1813{
1814	struct dn_route *rt = dn_rt_cache_get_first(seq);
1815
1816	if (rt) {
1817		while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1818			--*pos;
1819	}
1820	return *pos ? NULL : rt;
1821}
1822
1823static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1824{
1825	struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1826	++*pos;
1827	return rt;
1828}
1829
1830static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1831{
1832	if (v)
1833		rcu_read_unlock_bh();
1834}
1835
1836static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1837{
1838	struct dn_route *rt = v;
1839	char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1840
1841	seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
1842		   rt->dst.dev ? rt->dst.dev->name : "*",
1843		   dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1844		   dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
1845		   atomic_read(&rt->dst.__refcnt),
1846		   rt->dst.__use, 0);
1847	return 0;
1848}
1849
1850static const struct seq_operations dn_rt_cache_seq_ops = {
1851	.start	= dn_rt_cache_seq_start,
1852	.next	= dn_rt_cache_seq_next,
1853	.stop	= dn_rt_cache_seq_stop,
1854	.show	= dn_rt_cache_seq_show,
1855};
1856
1857static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1858{
1859	return seq_open_private(file, &dn_rt_cache_seq_ops,
1860			sizeof(struct dn_rt_cache_iter_state));
1861}
1862
1863static const struct file_operations dn_rt_cache_seq_fops = {
1864	.owner	 = THIS_MODULE,
1865	.open	 = dn_rt_cache_seq_open,
1866	.read	 = seq_read,
1867	.llseek	 = seq_lseek,
1868	.release = seq_release_private,
1869};
1870
1871#endif /* CONFIG_PROC_FS */
1872
1873void __init dn_route_init(void)
1874{
1875	int i, goal, order;
1876
1877	dn_dst_ops.kmem_cachep =
1878		kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
1879				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1880	dst_entries_init(&dn_dst_ops);
1881	setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
1882	dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1883	add_timer(&dn_route_timer);
1884
1885	goal = totalram_pages >> (26 - PAGE_SHIFT);
1886
1887	for(order = 0; (1UL << order) < goal; order++)
1888		/* NOTHING */;
1889
1890	/*
1891	 * Only want 1024 entries max, since the table is very, very unlikely
1892	 * to be larger than that.
1893	 */
1894	while(order && ((((1UL << order) * PAGE_SIZE) /
1895				sizeof(struct dn_rt_hash_bucket)) >= 2048))
1896		order--;
1897
1898	do {
1899		dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1900			sizeof(struct dn_rt_hash_bucket);
1901		while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1902			dn_rt_hash_mask--;
1903		dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1904			__get_free_pages(GFP_ATOMIC, order);
1905	} while (dn_rt_hash_table == NULL && --order > 0);
1906
1907	if (!dn_rt_hash_table)
1908		panic("Failed to allocate DECnet route cache hash table\n");
1909
1910	printk(KERN_INFO
1911		"DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1912		dn_rt_hash_mask,
1913		(long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1914
1915	dn_rt_hash_mask--;
1916	for(i = 0; i <= dn_rt_hash_mask; i++) {
1917		spin_lock_init(&dn_rt_hash_table[i].lock);
1918		dn_rt_hash_table[i].chain = NULL;
1919	}
1920
1921	dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
1922
1923	proc_create("decnet_cache", S_IRUGO, init_net.proc_net,
1924		    &dn_rt_cache_seq_fops);
1925
1926#ifdef CONFIG_DECNET_ROUTER
1927	rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1928		      dn_fib_dump, NULL);
1929#else
1930	rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1931		      dn_cache_dump, NULL);
1932#endif
1933}
1934
1935void __exit dn_route_cleanup(void)
1936{
1937	del_timer(&dn_route_timer);
1938	dn_run_flush(0);
1939
1940	remove_proc_entry("decnet_cache", init_net.proc_net);
1941	dst_entries_destroy(&dn_dst_ops);
1942}
1943
1944