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