Merge branch 'v6v7' into devel
[linux/fpc-iii.git] / net / decnet / dn_route.c
blob5e636365d33cb2e99b1077b59ce9e14c4b5c7c02
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.
6 * DECnet Routing Functions (Endnode and Router)
8 * Authors: Steve Whitehouse <SteveW@ACM.org>
9 * Eduardo Marcelo Serrat <emserrat@geocities.com>
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
44 /******************************************************************************
45 (c) 1995-1998 E.M. Serrat emserrat@geocities.com
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.
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 *******************************************************************************/
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 <asm/errno.h>
81 #include <net/net_namespace.h>
82 #include <net/netlink.h>
83 #include <net/neighbour.h>
84 #include <net/dst.h>
85 #include <net/flow.h>
86 #include <net/fib_rules.h>
87 #include <net/dn.h>
88 #include <net/dn_dev.h>
89 #include <net/dn_nsp.h>
90 #include <net/dn_route.h>
91 #include <net/dn_neigh.h>
92 #include <net/dn_fib.h>
94 struct dn_rt_hash_bucket
96 struct dn_route __rcu *chain;
97 spinlock_t lock;
100 extern struct neigh_table dn_neigh_table;
103 static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};
105 static const int dn_rt_min_delay = 2 * HZ;
106 static const int dn_rt_max_delay = 10 * HZ;
107 static const int dn_rt_mtu_expires = 10 * 60 * HZ;
109 static unsigned long dn_rt_deadline;
111 static int dn_dst_gc(struct dst_ops *ops);
112 static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);
113 static unsigned int dn_dst_default_advmss(const struct dst_entry *dst);
114 static unsigned int dn_dst_default_mtu(const struct dst_entry *dst);
115 static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
116 static void dn_dst_link_failure(struct sk_buff *);
117 static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu);
118 static int dn_route_input(struct sk_buff *);
119 static void dn_run_flush(unsigned long dummy);
121 static struct dn_rt_hash_bucket *dn_rt_hash_table;
122 static unsigned dn_rt_hash_mask;
124 static struct timer_list dn_route_timer;
125 static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
126 int decnet_dst_gc_interval = 2;
128 static struct dst_ops dn_dst_ops = {
129 .family = PF_DECnet,
130 .protocol = cpu_to_be16(ETH_P_DNA_RT),
131 .gc_thresh = 128,
132 .gc = dn_dst_gc,
133 .check = dn_dst_check,
134 .default_advmss = dn_dst_default_advmss,
135 .default_mtu = dn_dst_default_mtu,
136 .negative_advice = dn_dst_negative_advice,
137 .link_failure = dn_dst_link_failure,
138 .update_pmtu = dn_dst_update_pmtu,
141 static __inline__ unsigned dn_hash(__le16 src, __le16 dst)
143 __u16 tmp = (__u16 __force)(src ^ dst);
144 tmp ^= (tmp >> 3);
145 tmp ^= (tmp >> 5);
146 tmp ^= (tmp >> 10);
147 return dn_rt_hash_mask & (unsigned)tmp;
150 static inline void dnrt_free(struct dn_route *rt)
152 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
155 static inline void dnrt_drop(struct dn_route *rt)
157 dst_release(&rt->dst);
158 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
161 static void dn_dst_check_expire(unsigned long dummy)
163 int i;
164 struct dn_route *rt;
165 struct dn_route __rcu **rtp;
166 unsigned long now = jiffies;
167 unsigned long expire = 120 * HZ;
169 for (i = 0; i <= dn_rt_hash_mask; i++) {
170 rtp = &dn_rt_hash_table[i].chain;
172 spin_lock(&dn_rt_hash_table[i].lock);
173 while ((rt = rcu_dereference_protected(*rtp,
174 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
175 if (atomic_read(&rt->dst.__refcnt) ||
176 (now - rt->dst.lastuse) < expire) {
177 rtp = &rt->dst.dn_next;
178 continue;
180 *rtp = rt->dst.dn_next;
181 rt->dst.dn_next = NULL;
182 dnrt_free(rt);
184 spin_unlock(&dn_rt_hash_table[i].lock);
186 if ((jiffies - now) > 0)
187 break;
190 mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
193 static int dn_dst_gc(struct dst_ops *ops)
195 struct dn_route *rt;
196 struct dn_route __rcu **rtp;
197 int i;
198 unsigned long now = jiffies;
199 unsigned long expire = 10 * HZ;
201 for (i = 0; i <= dn_rt_hash_mask; i++) {
203 spin_lock_bh(&dn_rt_hash_table[i].lock);
204 rtp = &dn_rt_hash_table[i].chain;
206 while ((rt = rcu_dereference_protected(*rtp,
207 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
208 if (atomic_read(&rt->dst.__refcnt) ||
209 (now - rt->dst.lastuse) < expire) {
210 rtp = &rt->dst.dn_next;
211 continue;
213 *rtp = rt->dst.dn_next;
214 rt->dst.dn_next = NULL;
215 dnrt_drop(rt);
216 break;
218 spin_unlock_bh(&dn_rt_hash_table[i].lock);
221 return 0;
225 * The decnet standards don't impose a particular minimum mtu, what they
226 * do insist on is that the routing layer accepts a datagram of at least
227 * 230 bytes long. Here we have to subtract the routing header length from
228 * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
229 * assume the worst and use a long header size.
231 * We update both the mtu and the advertised mss (i.e. the segment size we
232 * advertise to the other end).
234 static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu)
236 u32 min_mtu = 230;
237 struct dn_dev *dn = dst->neighbour ?
238 rcu_dereference_raw(dst->neighbour->dev->dn_ptr) : NULL;
240 if (dn && dn->use_long == 0)
241 min_mtu -= 6;
242 else
243 min_mtu -= 21;
245 if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
246 if (!(dst_metric_locked(dst, RTAX_MTU))) {
247 dst_metric_set(dst, RTAX_MTU, mtu);
248 dst_set_expires(dst, dn_rt_mtu_expires);
250 if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
251 u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
252 u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
253 if (!existing_mss || existing_mss > mss)
254 dst_metric_set(dst, RTAX_ADVMSS, mss);
260 * When a route has been marked obsolete. (e.g. routing cache flush)
262 static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
264 return NULL;
267 static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
269 dst_release(dst);
270 return NULL;
273 static void dn_dst_link_failure(struct sk_buff *skb)
277 static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
279 return ((fl1->fld_dst ^ fl2->fld_dst) |
280 (fl1->fld_src ^ fl2->fld_src) |
281 (fl1->mark ^ fl2->mark) |
282 (fl1->fld_scope ^ fl2->fld_scope) |
283 (fl1->oif ^ fl2->oif) |
284 (fl1->iif ^ fl2->iif)) == 0;
287 static int dn_insert_route(struct dn_route *rt, unsigned hash, struct dn_route **rp)
289 struct dn_route *rth;
290 struct dn_route __rcu **rthp;
291 unsigned long now = jiffies;
293 rthp = &dn_rt_hash_table[hash].chain;
295 spin_lock_bh(&dn_rt_hash_table[hash].lock);
296 while ((rth = rcu_dereference_protected(*rthp,
297 lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
298 if (compare_keys(&rth->fl, &rt->fl)) {
299 /* Put it first */
300 *rthp = rth->dst.dn_next;
301 rcu_assign_pointer(rth->dst.dn_next,
302 dn_rt_hash_table[hash].chain);
303 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
305 dst_use(&rth->dst, now);
306 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
308 dnrt_drop(rt);
309 *rp = rth;
310 return 0;
312 rthp = &rth->dst.dn_next;
315 rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
316 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
318 dst_use(&rt->dst, now);
319 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
320 *rp = rt;
321 return 0;
324 static void dn_run_flush(unsigned long dummy)
326 int i;
327 struct dn_route *rt, *next;
329 for (i = 0; i < dn_rt_hash_mask; i++) {
330 spin_lock_bh(&dn_rt_hash_table[i].lock);
332 if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
333 goto nothing_to_declare;
335 for(; rt; rt = next) {
336 next = rcu_dereference_raw(rt->dst.dn_next);
337 RCU_INIT_POINTER(rt->dst.dn_next, NULL);
338 dst_free((struct dst_entry *)rt);
341 nothing_to_declare:
342 spin_unlock_bh(&dn_rt_hash_table[i].lock);
346 static DEFINE_SPINLOCK(dn_rt_flush_lock);
348 void dn_rt_cache_flush(int delay)
350 unsigned long now = jiffies;
351 int user_mode = !in_interrupt();
353 if (delay < 0)
354 delay = dn_rt_min_delay;
356 spin_lock_bh(&dn_rt_flush_lock);
358 if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
359 long tmo = (long)(dn_rt_deadline - now);
361 if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
362 tmo = 0;
364 if (delay > tmo)
365 delay = tmo;
368 if (delay <= 0) {
369 spin_unlock_bh(&dn_rt_flush_lock);
370 dn_run_flush(0);
371 return;
374 if (dn_rt_deadline == 0)
375 dn_rt_deadline = now + dn_rt_max_delay;
377 dn_rt_flush_timer.expires = now + delay;
378 add_timer(&dn_rt_flush_timer);
379 spin_unlock_bh(&dn_rt_flush_lock);
383 * dn_return_short - Return a short packet to its sender
384 * @skb: The packet to return
387 static int dn_return_short(struct sk_buff *skb)
389 struct dn_skb_cb *cb;
390 unsigned char *ptr;
391 __le16 *src;
392 __le16 *dst;
394 /* Add back headers */
395 skb_push(skb, skb->data - skb_network_header(skb));
397 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
398 return NET_RX_DROP;
400 cb = DN_SKB_CB(skb);
401 /* Skip packet length and point to flags */
402 ptr = skb->data + 2;
403 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
405 dst = (__le16 *)ptr;
406 ptr += 2;
407 src = (__le16 *)ptr;
408 ptr += 2;
409 *ptr = 0; /* Zero hop count */
411 swap(*src, *dst);
413 skb->pkt_type = PACKET_OUTGOING;
414 dn_rt_finish_output(skb, NULL, NULL);
415 return NET_RX_SUCCESS;
419 * dn_return_long - Return a long packet to its sender
420 * @skb: The long format packet to return
423 static int dn_return_long(struct sk_buff *skb)
425 struct dn_skb_cb *cb;
426 unsigned char *ptr;
427 unsigned char *src_addr, *dst_addr;
428 unsigned char tmp[ETH_ALEN];
430 /* Add back all headers */
431 skb_push(skb, skb->data - skb_network_header(skb));
433 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
434 return NET_RX_DROP;
436 cb = DN_SKB_CB(skb);
437 /* Ignore packet length and point to flags */
438 ptr = skb->data + 2;
440 /* Skip padding */
441 if (*ptr & DN_RT_F_PF) {
442 char padlen = (*ptr & ~DN_RT_F_PF);
443 ptr += padlen;
446 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
447 ptr += 2;
448 dst_addr = ptr;
449 ptr += 8;
450 src_addr = ptr;
451 ptr += 6;
452 *ptr = 0; /* Zero hop count */
454 /* Swap source and destination */
455 memcpy(tmp, src_addr, ETH_ALEN);
456 memcpy(src_addr, dst_addr, ETH_ALEN);
457 memcpy(dst_addr, tmp, ETH_ALEN);
459 skb->pkt_type = PACKET_OUTGOING;
460 dn_rt_finish_output(skb, dst_addr, src_addr);
461 return NET_RX_SUCCESS;
465 * dn_route_rx_packet - Try and find a route for an incoming packet
466 * @skb: The packet to find a route for
468 * Returns: result of input function if route is found, error code otherwise
470 static int dn_route_rx_packet(struct sk_buff *skb)
472 struct dn_skb_cb *cb;
473 int err;
475 if ((err = dn_route_input(skb)) == 0)
476 return dst_input(skb);
478 cb = DN_SKB_CB(skb);
479 if (decnet_debug_level & 4) {
480 char *devname = skb->dev ? skb->dev->name : "???";
482 printk(KERN_DEBUG
483 "DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%d\n",
484 (int)cb->rt_flags, devname, skb->len,
485 le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
486 err, skb->pkt_type);
489 if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
490 switch(cb->rt_flags & DN_RT_PKT_MSK) {
491 case DN_RT_PKT_SHORT:
492 return dn_return_short(skb);
493 case DN_RT_PKT_LONG:
494 return dn_return_long(skb);
498 kfree_skb(skb);
499 return NET_RX_DROP;
502 static int dn_route_rx_long(struct sk_buff *skb)
504 struct dn_skb_cb *cb = DN_SKB_CB(skb);
505 unsigned char *ptr = skb->data;
507 if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
508 goto drop_it;
510 skb_pull(skb, 20);
511 skb_reset_transport_header(skb);
513 /* Destination info */
514 ptr += 2;
515 cb->dst = dn_eth2dn(ptr);
516 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
517 goto drop_it;
518 ptr += 6;
521 /* Source info */
522 ptr += 2;
523 cb->src = dn_eth2dn(ptr);
524 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
525 goto drop_it;
526 ptr += 6;
527 /* Other junk */
528 ptr++;
529 cb->hops = *ptr++; /* Visit Count */
531 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
532 dn_route_rx_packet);
534 drop_it:
535 kfree_skb(skb);
536 return NET_RX_DROP;
541 static int dn_route_rx_short(struct sk_buff *skb)
543 struct dn_skb_cb *cb = DN_SKB_CB(skb);
544 unsigned char *ptr = skb->data;
546 if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
547 goto drop_it;
549 skb_pull(skb, 5);
550 skb_reset_transport_header(skb);
552 cb->dst = *(__le16 *)ptr;
553 ptr += 2;
554 cb->src = *(__le16 *)ptr;
555 ptr += 2;
556 cb->hops = *ptr & 0x3f;
558 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
559 dn_route_rx_packet);
561 drop_it:
562 kfree_skb(skb);
563 return NET_RX_DROP;
566 static int dn_route_discard(struct sk_buff *skb)
569 * I know we drop the packet here, but thats considered success in
570 * this case
572 kfree_skb(skb);
573 return NET_RX_SUCCESS;
576 static int dn_route_ptp_hello(struct sk_buff *skb)
578 dn_dev_hello(skb);
579 dn_neigh_pointopoint_hello(skb);
580 return NET_RX_SUCCESS;
583 int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
585 struct dn_skb_cb *cb;
586 unsigned char flags = 0;
587 __u16 len = le16_to_cpu(*(__le16 *)skb->data);
588 struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
589 unsigned char padlen = 0;
591 if (!net_eq(dev_net(dev), &init_net))
592 goto dump_it;
594 if (dn == NULL)
595 goto dump_it;
597 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
598 goto out;
600 if (!pskb_may_pull(skb, 3))
601 goto dump_it;
603 skb_pull(skb, 2);
605 if (len > skb->len)
606 goto dump_it;
608 skb_trim(skb, len);
610 flags = *skb->data;
612 cb = DN_SKB_CB(skb);
613 cb->stamp = jiffies;
614 cb->iif = dev->ifindex;
617 * If we have padding, remove it.
619 if (flags & DN_RT_F_PF) {
620 padlen = flags & ~DN_RT_F_PF;
621 if (!pskb_may_pull(skb, padlen + 1))
622 goto dump_it;
623 skb_pull(skb, padlen);
624 flags = *skb->data;
627 skb_reset_network_header(skb);
630 * Weed out future version DECnet
632 if (flags & DN_RT_F_VER)
633 goto dump_it;
635 cb->rt_flags = flags;
637 if (decnet_debug_level & 1)
638 printk(KERN_DEBUG
639 "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
640 (int)flags, (dev) ? dev->name : "???", len, skb->len,
641 padlen);
643 if (flags & DN_RT_PKT_CNTL) {
644 if (unlikely(skb_linearize(skb)))
645 goto dump_it;
647 switch(flags & DN_RT_CNTL_MSK) {
648 case DN_RT_PKT_INIT:
649 dn_dev_init_pkt(skb);
650 break;
651 case DN_RT_PKT_VERI:
652 dn_dev_veri_pkt(skb);
653 break;
656 if (dn->parms.state != DN_DEV_S_RU)
657 goto dump_it;
659 switch(flags & DN_RT_CNTL_MSK) {
660 case DN_RT_PKT_HELO:
661 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
662 skb, skb->dev, NULL,
663 dn_route_ptp_hello);
665 case DN_RT_PKT_L1RT:
666 case DN_RT_PKT_L2RT:
667 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
668 skb, skb->dev, NULL,
669 dn_route_discard);
670 case DN_RT_PKT_ERTH:
671 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
672 skb, skb->dev, NULL,
673 dn_neigh_router_hello);
675 case DN_RT_PKT_EEDH:
676 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
677 skb, skb->dev, NULL,
678 dn_neigh_endnode_hello);
680 } else {
681 if (dn->parms.state != DN_DEV_S_RU)
682 goto dump_it;
684 skb_pull(skb, 1); /* Pull flags */
686 switch(flags & DN_RT_PKT_MSK) {
687 case DN_RT_PKT_LONG:
688 return dn_route_rx_long(skb);
689 case DN_RT_PKT_SHORT:
690 return dn_route_rx_short(skb);
694 dump_it:
695 kfree_skb(skb);
696 out:
697 return NET_RX_DROP;
700 static int dn_output(struct sk_buff *skb)
702 struct dst_entry *dst = skb_dst(skb);
703 struct dn_route *rt = (struct dn_route *)dst;
704 struct net_device *dev = dst->dev;
705 struct dn_skb_cb *cb = DN_SKB_CB(skb);
706 struct neighbour *neigh;
708 int err = -EINVAL;
710 if ((neigh = dst->neighbour) == NULL)
711 goto error;
713 skb->dev = dev;
715 cb->src = rt->rt_saddr;
716 cb->dst = rt->rt_daddr;
719 * Always set the Intra-Ethernet bit on all outgoing packets
720 * originated on this node. Only valid flag from upper layers
721 * is return-to-sender-requested. Set hop count to 0 too.
723 cb->rt_flags &= ~DN_RT_F_RQR;
724 cb->rt_flags |= DN_RT_F_IE;
725 cb->hops = 0;
727 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
728 neigh->output);
730 error:
731 if (net_ratelimit())
732 printk(KERN_DEBUG "dn_output: This should not happen\n");
734 kfree_skb(skb);
736 return err;
739 static int dn_forward(struct sk_buff *skb)
741 struct dn_skb_cb *cb = DN_SKB_CB(skb);
742 struct dst_entry *dst = skb_dst(skb);
743 struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
744 struct dn_route *rt;
745 struct neighbour *neigh = dst->neighbour;
746 int header_len;
747 #ifdef CONFIG_NETFILTER
748 struct net_device *dev = skb->dev;
749 #endif
751 if (skb->pkt_type != PACKET_HOST)
752 goto drop;
754 /* Ensure that we have enough space for headers */
755 rt = (struct dn_route *)skb_dst(skb);
756 header_len = dn_db->use_long ? 21 : 6;
757 if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
758 goto drop;
761 * Hop count exceeded.
763 if (++cb->hops > 30)
764 goto drop;
766 skb->dev = rt->dst.dev;
769 * If packet goes out same interface it came in on, then set
770 * the Intra-Ethernet bit. This has no effect for short
771 * packets, so we don't need to test for them here.
773 cb->rt_flags &= ~DN_RT_F_IE;
774 if (rt->rt_flags & RTCF_DOREDIRECT)
775 cb->rt_flags |= DN_RT_F_IE;
777 return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
778 neigh->output);
780 drop:
781 kfree_skb(skb);
782 return NET_RX_DROP;
786 * Used to catch bugs. This should never normally get
787 * called.
789 static int dn_rt_bug(struct sk_buff *skb)
791 if (net_ratelimit()) {
792 struct dn_skb_cb *cb = DN_SKB_CB(skb);
794 printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
795 le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
798 kfree_skb(skb);
800 return NET_RX_DROP;
803 static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
805 return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
808 static unsigned int dn_dst_default_mtu(const struct dst_entry *dst)
810 return dst->dev->mtu;
813 static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
815 struct dn_fib_info *fi = res->fi;
816 struct net_device *dev = rt->dst.dev;
817 struct neighbour *n;
818 unsigned int metric;
820 if (fi) {
821 if (DN_FIB_RES_GW(*res) &&
822 DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
823 rt->rt_gateway = DN_FIB_RES_GW(*res);
824 dst_import_metrics(&rt->dst, fi->fib_metrics);
826 rt->rt_type = res->type;
828 if (dev != NULL && rt->dst.neighbour == NULL) {
829 n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
830 if (IS_ERR(n))
831 return PTR_ERR(n);
832 rt->dst.neighbour = n;
835 if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
836 dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
837 metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
838 if (metric) {
839 unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
840 if (metric > mss)
841 dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
843 return 0;
846 static inline int dn_match_addr(__le16 addr1, __le16 addr2)
848 __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
849 int match = 16;
850 while(tmp) {
851 tmp >>= 1;
852 match--;
854 return match;
857 static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
859 __le16 saddr = 0;
860 struct dn_dev *dn_db;
861 struct dn_ifaddr *ifa;
862 int best_match = 0;
863 int ret;
865 rcu_read_lock();
866 dn_db = rcu_dereference(dev->dn_ptr);
867 for (ifa = rcu_dereference(dn_db->ifa_list);
868 ifa != NULL;
869 ifa = rcu_dereference(ifa->ifa_next)) {
870 if (ifa->ifa_scope > scope)
871 continue;
872 if (!daddr) {
873 saddr = ifa->ifa_local;
874 break;
876 ret = dn_match_addr(daddr, ifa->ifa_local);
877 if (ret > best_match)
878 saddr = ifa->ifa_local;
879 if (best_match == 0)
880 saddr = ifa->ifa_local;
882 rcu_read_unlock();
884 return saddr;
887 static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
889 return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
892 static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
894 __le16 mask = dnet_make_mask(res->prefixlen);
895 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
898 static int dn_route_output_slow(struct dst_entry **pprt, const struct flowi *oldflp, int try_hard)
900 struct flowi fl = { .fld_dst = oldflp->fld_dst,
901 .fld_src = oldflp->fld_src,
902 .fld_scope = RT_SCOPE_UNIVERSE,
903 .mark = oldflp->mark,
904 .iif = init_net.loopback_dev->ifindex,
905 .oif = oldflp->oif };
906 struct dn_route *rt = NULL;
907 struct net_device *dev_out = NULL, *dev;
908 struct neighbour *neigh = NULL;
909 unsigned hash;
910 unsigned flags = 0;
911 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
912 int err;
913 int free_res = 0;
914 __le16 gateway = 0;
916 if (decnet_debug_level & 16)
917 printk(KERN_DEBUG
918 "dn_route_output_slow: dst=%04x src=%04x mark=%d"
919 " iif=%d oif=%d\n", le16_to_cpu(oldflp->fld_dst),
920 le16_to_cpu(oldflp->fld_src),
921 oldflp->mark, init_net.loopback_dev->ifindex, oldflp->oif);
923 /* If we have an output interface, verify its a DECnet device */
924 if (oldflp->oif) {
925 dev_out = dev_get_by_index(&init_net, oldflp->oif);
926 err = -ENODEV;
927 if (dev_out && dev_out->dn_ptr == NULL) {
928 dev_put(dev_out);
929 dev_out = NULL;
931 if (dev_out == NULL)
932 goto out;
935 /* If we have a source address, verify that its a local address */
936 if (oldflp->fld_src) {
937 err = -EADDRNOTAVAIL;
939 if (dev_out) {
940 if (dn_dev_islocal(dev_out, oldflp->fld_src))
941 goto source_ok;
942 dev_put(dev_out);
943 goto out;
945 rcu_read_lock();
946 for_each_netdev_rcu(&init_net, dev) {
947 if (!dev->dn_ptr)
948 continue;
949 if (!dn_dev_islocal(dev, oldflp->fld_src))
950 continue;
951 if ((dev->flags & IFF_LOOPBACK) &&
952 oldflp->fld_dst &&
953 !dn_dev_islocal(dev, oldflp->fld_dst))
954 continue;
956 dev_out = dev;
957 break;
959 rcu_read_unlock();
960 if (dev_out == NULL)
961 goto out;
962 dev_hold(dev_out);
963 source_ok:
967 /* No destination? Assume its local */
968 if (!fl.fld_dst) {
969 fl.fld_dst = fl.fld_src;
971 err = -EADDRNOTAVAIL;
972 if (dev_out)
973 dev_put(dev_out);
974 dev_out = init_net.loopback_dev;
975 dev_hold(dev_out);
976 if (!fl.fld_dst) {
977 fl.fld_dst =
978 fl.fld_src = dnet_select_source(dev_out, 0,
979 RT_SCOPE_HOST);
980 if (!fl.fld_dst)
981 goto out;
983 fl.oif = init_net.loopback_dev->ifindex;
984 res.type = RTN_LOCAL;
985 goto make_route;
988 if (decnet_debug_level & 16)
989 printk(KERN_DEBUG
990 "dn_route_output_slow: initial checks complete."
991 " dst=%o4x src=%04x oif=%d try_hard=%d\n",
992 le16_to_cpu(fl.fld_dst), le16_to_cpu(fl.fld_src),
993 fl.oif, try_hard);
996 * N.B. If the kernel is compiled without router support then
997 * dn_fib_lookup() will evaluate to non-zero so this if () block
998 * will always be executed.
1000 err = -ESRCH;
1001 if (try_hard || (err = dn_fib_lookup(&fl, &res)) != 0) {
1002 struct dn_dev *dn_db;
1003 if (err != -ESRCH)
1004 goto out;
1006 * Here the fallback is basically the standard algorithm for
1007 * routing in endnodes which is described in the DECnet routing
1008 * docs
1010 * If we are not trying hard, look in neighbour cache.
1011 * The result is tested to ensure that if a specific output
1012 * device/source address was requested, then we honour that
1013 * here
1015 if (!try_hard) {
1016 neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fl.fld_dst);
1017 if (neigh) {
1018 if ((oldflp->oif &&
1019 (neigh->dev->ifindex != oldflp->oif)) ||
1020 (oldflp->fld_src &&
1021 (!dn_dev_islocal(neigh->dev,
1022 oldflp->fld_src)))) {
1023 neigh_release(neigh);
1024 neigh = NULL;
1025 } else {
1026 if (dev_out)
1027 dev_put(dev_out);
1028 if (dn_dev_islocal(neigh->dev, fl.fld_dst)) {
1029 dev_out = init_net.loopback_dev;
1030 res.type = RTN_LOCAL;
1031 } else {
1032 dev_out = neigh->dev;
1034 dev_hold(dev_out);
1035 goto select_source;
1040 /* Not there? Perhaps its a local address */
1041 if (dev_out == NULL)
1042 dev_out = dn_dev_get_default();
1043 err = -ENODEV;
1044 if (dev_out == NULL)
1045 goto out;
1046 dn_db = rcu_dereference_raw(dev_out->dn_ptr);
1047 /* Possible improvement - check all devices for local addr */
1048 if (dn_dev_islocal(dev_out, fl.fld_dst)) {
1049 dev_put(dev_out);
1050 dev_out = init_net.loopback_dev;
1051 dev_hold(dev_out);
1052 res.type = RTN_LOCAL;
1053 goto select_source;
1055 /* Not local either.... try sending it to the default router */
1056 neigh = neigh_clone(dn_db->router);
1057 BUG_ON(neigh && neigh->dev != dev_out);
1059 /* Ok then, we assume its directly connected and move on */
1060 select_source:
1061 if (neigh)
1062 gateway = ((struct dn_neigh *)neigh)->addr;
1063 if (gateway == 0)
1064 gateway = fl.fld_dst;
1065 if (fl.fld_src == 0) {
1066 fl.fld_src = dnet_select_source(dev_out, gateway,
1067 res.type == RTN_LOCAL ?
1068 RT_SCOPE_HOST :
1069 RT_SCOPE_LINK);
1070 if (fl.fld_src == 0 && res.type != RTN_LOCAL)
1071 goto e_addr;
1073 fl.oif = dev_out->ifindex;
1074 goto make_route;
1076 free_res = 1;
1078 if (res.type == RTN_NAT)
1079 goto e_inval;
1081 if (res.type == RTN_LOCAL) {
1082 if (!fl.fld_src)
1083 fl.fld_src = fl.fld_dst;
1084 if (dev_out)
1085 dev_put(dev_out);
1086 dev_out = init_net.loopback_dev;
1087 dev_hold(dev_out);
1088 fl.oif = dev_out->ifindex;
1089 if (res.fi)
1090 dn_fib_info_put(res.fi);
1091 res.fi = NULL;
1092 goto make_route;
1095 if (res.fi->fib_nhs > 1 && fl.oif == 0)
1096 dn_fib_select_multipath(&fl, &res);
1099 * We could add some logic to deal with default routes here and
1100 * get rid of some of the special casing above.
1103 if (!fl.fld_src)
1104 fl.fld_src = DN_FIB_RES_PREFSRC(res);
1106 if (dev_out)
1107 dev_put(dev_out);
1108 dev_out = DN_FIB_RES_DEV(res);
1109 dev_hold(dev_out);
1110 fl.oif = dev_out->ifindex;
1111 gateway = DN_FIB_RES_GW(res);
1113 make_route:
1114 if (dev_out->flags & IFF_LOOPBACK)
1115 flags |= RTCF_LOCAL;
1117 rt = dst_alloc(&dn_dst_ops);
1118 if (rt == NULL)
1119 goto e_nobufs;
1121 atomic_set(&rt->dst.__refcnt, 1);
1122 rt->dst.flags = DST_HOST;
1124 rt->fl.fld_src = oldflp->fld_src;
1125 rt->fl.fld_dst = oldflp->fld_dst;
1126 rt->fl.oif = oldflp->oif;
1127 rt->fl.iif = 0;
1128 rt->fl.mark = oldflp->mark;
1130 rt->rt_saddr = fl.fld_src;
1131 rt->rt_daddr = fl.fld_dst;
1132 rt->rt_gateway = gateway ? gateway : fl.fld_dst;
1133 rt->rt_local_src = fl.fld_src;
1135 rt->rt_dst_map = fl.fld_dst;
1136 rt->rt_src_map = fl.fld_src;
1138 rt->dst.dev = dev_out;
1139 dev_hold(dev_out);
1140 rt->dst.neighbour = neigh;
1141 neigh = NULL;
1143 rt->dst.lastuse = jiffies;
1144 rt->dst.output = dn_output;
1145 rt->dst.input = dn_rt_bug;
1146 rt->rt_flags = flags;
1147 if (flags & RTCF_LOCAL)
1148 rt->dst.input = dn_nsp_rx;
1150 err = dn_rt_set_next_hop(rt, &res);
1151 if (err)
1152 goto e_neighbour;
1154 hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst);
1155 dn_insert_route(rt, hash, (struct dn_route **)pprt);
1157 done:
1158 if (neigh)
1159 neigh_release(neigh);
1160 if (free_res)
1161 dn_fib_res_put(&res);
1162 if (dev_out)
1163 dev_put(dev_out);
1164 out:
1165 return err;
1167 e_addr:
1168 err = -EADDRNOTAVAIL;
1169 goto done;
1170 e_inval:
1171 err = -EINVAL;
1172 goto done;
1173 e_nobufs:
1174 err = -ENOBUFS;
1175 goto done;
1176 e_neighbour:
1177 dst_free(&rt->dst);
1178 goto e_nobufs;
1183 * N.B. The flags may be moved into the flowi at some future stage.
1185 static int __dn_route_output_key(struct dst_entry **pprt, const struct flowi *flp, int flags)
1187 unsigned hash = dn_hash(flp->fld_src, flp->fld_dst);
1188 struct dn_route *rt = NULL;
1190 if (!(flags & MSG_TRYHARD)) {
1191 rcu_read_lock_bh();
1192 for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
1193 rt = rcu_dereference_bh(rt->dst.dn_next)) {
1194 if ((flp->fld_dst == rt->fl.fld_dst) &&
1195 (flp->fld_src == rt->fl.fld_src) &&
1196 (flp->mark == rt->fl.mark) &&
1197 dn_is_output_route(rt) &&
1198 (rt->fl.oif == flp->oif)) {
1199 dst_use(&rt->dst, jiffies);
1200 rcu_read_unlock_bh();
1201 *pprt = &rt->dst;
1202 return 0;
1205 rcu_read_unlock_bh();
1208 return dn_route_output_slow(pprt, flp, flags);
1211 static int dn_route_output_key(struct dst_entry **pprt, struct flowi *flp, int flags)
1213 int err;
1215 err = __dn_route_output_key(pprt, flp, flags);
1216 if (err == 0 && flp->proto) {
1217 err = xfrm_lookup(&init_net, pprt, flp, NULL, 0);
1219 return err;
1222 int dn_route_output_sock(struct dst_entry **pprt, struct flowi *fl, struct sock *sk, int flags)
1224 int err;
1226 err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
1227 if (err == 0 && fl->proto) {
1228 err = xfrm_lookup(&init_net, pprt, fl, sk,
1229 (flags & MSG_DONTWAIT) ? 0 : XFRM_LOOKUP_WAIT);
1231 return err;
1234 static int dn_route_input_slow(struct sk_buff *skb)
1236 struct dn_route *rt = NULL;
1237 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1238 struct net_device *in_dev = skb->dev;
1239 struct net_device *out_dev = NULL;
1240 struct dn_dev *dn_db;
1241 struct neighbour *neigh = NULL;
1242 unsigned hash;
1243 int flags = 0;
1244 __le16 gateway = 0;
1245 __le16 local_src = 0;
1246 struct flowi fl = { .fld_dst = cb->dst,
1247 .fld_src = cb->src,
1248 .fld_scope = RT_SCOPE_UNIVERSE,
1249 .mark = skb->mark,
1250 .iif = skb->dev->ifindex };
1251 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1252 int err = -EINVAL;
1253 int free_res = 0;
1255 dev_hold(in_dev);
1257 if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
1258 goto out;
1260 /* Zero source addresses are not allowed */
1261 if (fl.fld_src == 0)
1262 goto out;
1265 * In this case we've just received a packet from a source
1266 * outside ourselves pretending to come from us. We don't
1267 * allow it any further to prevent routing loops, spoofing and
1268 * other nasties. Loopback packets already have the dst attached
1269 * so this only affects packets which have originated elsewhere.
1271 err = -ENOTUNIQ;
1272 if (dn_dev_islocal(in_dev, cb->src))
1273 goto out;
1275 err = dn_fib_lookup(&fl, &res);
1276 if (err) {
1277 if (err != -ESRCH)
1278 goto out;
1280 * Is the destination us ?
1282 if (!dn_dev_islocal(in_dev, cb->dst))
1283 goto e_inval;
1285 res.type = RTN_LOCAL;
1286 } else {
1287 __le16 src_map = fl.fld_src;
1288 free_res = 1;
1290 out_dev = DN_FIB_RES_DEV(res);
1291 if (out_dev == NULL) {
1292 if (net_ratelimit())
1293 printk(KERN_CRIT "Bug in dn_route_input_slow() "
1294 "No output device\n");
1295 goto e_inval;
1297 dev_hold(out_dev);
1299 if (res.r)
1300 src_map = fl.fld_src; /* no NAT support for now */
1302 gateway = DN_FIB_RES_GW(res);
1303 if (res.type == RTN_NAT) {
1304 fl.fld_dst = dn_fib_rules_map_destination(fl.fld_dst, &res);
1305 dn_fib_res_put(&res);
1306 free_res = 0;
1307 if (dn_fib_lookup(&fl, &res))
1308 goto e_inval;
1309 free_res = 1;
1310 if (res.type != RTN_UNICAST)
1311 goto e_inval;
1312 flags |= RTCF_DNAT;
1313 gateway = fl.fld_dst;
1315 fl.fld_src = src_map;
1318 switch(res.type) {
1319 case RTN_UNICAST:
1321 * Forwarding check here, we only check for forwarding
1322 * being turned off, if you want to only forward intra
1323 * area, its up to you to set the routing tables up
1324 * correctly.
1326 if (dn_db->parms.forwarding == 0)
1327 goto e_inval;
1329 if (res.fi->fib_nhs > 1 && fl.oif == 0)
1330 dn_fib_select_multipath(&fl, &res);
1333 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1334 * flag as a hint to set the intra-ethernet bit when
1335 * forwarding. If we've got NAT in operation, we don't do
1336 * this optimisation.
1338 if (out_dev == in_dev && !(flags & RTCF_NAT))
1339 flags |= RTCF_DOREDIRECT;
1341 local_src = DN_FIB_RES_PREFSRC(res);
1343 case RTN_BLACKHOLE:
1344 case RTN_UNREACHABLE:
1345 break;
1346 case RTN_LOCAL:
1347 flags |= RTCF_LOCAL;
1348 fl.fld_src = cb->dst;
1349 fl.fld_dst = cb->src;
1351 /* Routing tables gave us a gateway */
1352 if (gateway)
1353 goto make_route;
1355 /* Packet was intra-ethernet, so we know its on-link */
1356 if (cb->rt_flags & DN_RT_F_IE) {
1357 gateway = cb->src;
1358 flags |= RTCF_DIRECTSRC;
1359 goto make_route;
1362 /* Use the default router if there is one */
1363 neigh = neigh_clone(dn_db->router);
1364 if (neigh) {
1365 gateway = ((struct dn_neigh *)neigh)->addr;
1366 goto make_route;
1369 /* Close eyes and pray */
1370 gateway = cb->src;
1371 flags |= RTCF_DIRECTSRC;
1372 goto make_route;
1373 default:
1374 goto e_inval;
1377 make_route:
1378 rt = dst_alloc(&dn_dst_ops);
1379 if (rt == NULL)
1380 goto e_nobufs;
1382 rt->rt_saddr = fl.fld_src;
1383 rt->rt_daddr = fl.fld_dst;
1384 rt->rt_gateway = fl.fld_dst;
1385 if (gateway)
1386 rt->rt_gateway = gateway;
1387 rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
1389 rt->rt_dst_map = fl.fld_dst;
1390 rt->rt_src_map = fl.fld_src;
1392 rt->fl.fld_src = cb->src;
1393 rt->fl.fld_dst = cb->dst;
1394 rt->fl.oif = 0;
1395 rt->fl.iif = in_dev->ifindex;
1396 rt->fl.mark = fl.mark;
1398 rt->dst.flags = DST_HOST;
1399 rt->dst.neighbour = neigh;
1400 rt->dst.dev = out_dev;
1401 rt->dst.lastuse = jiffies;
1402 rt->dst.output = dn_rt_bug;
1403 switch(res.type) {
1404 case RTN_UNICAST:
1405 rt->dst.input = dn_forward;
1406 break;
1407 case RTN_LOCAL:
1408 rt->dst.output = dn_output;
1409 rt->dst.input = dn_nsp_rx;
1410 rt->dst.dev = in_dev;
1411 flags |= RTCF_LOCAL;
1412 break;
1413 default:
1414 case RTN_UNREACHABLE:
1415 case RTN_BLACKHOLE:
1416 rt->dst.input = dst_discard;
1418 rt->rt_flags = flags;
1419 if (rt->dst.dev)
1420 dev_hold(rt->dst.dev);
1422 err = dn_rt_set_next_hop(rt, &res);
1423 if (err)
1424 goto e_neighbour;
1426 hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst);
1427 dn_insert_route(rt, hash, &rt);
1428 skb_dst_set(skb, &rt->dst);
1430 done:
1431 if (neigh)
1432 neigh_release(neigh);
1433 if (free_res)
1434 dn_fib_res_put(&res);
1435 dev_put(in_dev);
1436 if (out_dev)
1437 dev_put(out_dev);
1438 out:
1439 return err;
1441 e_inval:
1442 err = -EINVAL;
1443 goto done;
1445 e_nobufs:
1446 err = -ENOBUFS;
1447 goto done;
1449 e_neighbour:
1450 dst_free(&rt->dst);
1451 goto done;
1454 static int dn_route_input(struct sk_buff *skb)
1456 struct dn_route *rt;
1457 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1458 unsigned hash = dn_hash(cb->src, cb->dst);
1460 if (skb_dst(skb))
1461 return 0;
1463 rcu_read_lock();
1464 for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
1465 rt = rcu_dereference(rt->dst.dn_next)) {
1466 if ((rt->fl.fld_src == cb->src) &&
1467 (rt->fl.fld_dst == cb->dst) &&
1468 (rt->fl.oif == 0) &&
1469 (rt->fl.mark == skb->mark) &&
1470 (rt->fl.iif == cb->iif)) {
1471 dst_use(&rt->dst, jiffies);
1472 rcu_read_unlock();
1473 skb_dst_set(skb, (struct dst_entry *)rt);
1474 return 0;
1477 rcu_read_unlock();
1479 return dn_route_input_slow(skb);
1482 static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1483 int event, int nowait, unsigned int flags)
1485 struct dn_route *rt = (struct dn_route *)skb_dst(skb);
1486 struct rtmsg *r;
1487 struct nlmsghdr *nlh;
1488 unsigned char *b = skb_tail_pointer(skb);
1489 long expires;
1491 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
1492 r = NLMSG_DATA(nlh);
1493 r->rtm_family = AF_DECnet;
1494 r->rtm_dst_len = 16;
1495 r->rtm_src_len = 0;
1496 r->rtm_tos = 0;
1497 r->rtm_table = RT_TABLE_MAIN;
1498 RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
1499 r->rtm_type = rt->rt_type;
1500 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1501 r->rtm_scope = RT_SCOPE_UNIVERSE;
1502 r->rtm_protocol = RTPROT_UNSPEC;
1503 if (rt->rt_flags & RTCF_NOTIFY)
1504 r->rtm_flags |= RTM_F_NOTIFY;
1505 RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
1506 if (rt->fl.fld_src) {
1507 r->rtm_src_len = 16;
1508 RTA_PUT(skb, RTA_SRC, 2, &rt->fl.fld_src);
1510 if (rt->dst.dev)
1511 RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
1513 * Note to self - change this if input routes reverse direction when
1514 * they deal only with inputs and not with replies like they do
1515 * currently.
1517 RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
1518 if (rt->rt_daddr != rt->rt_gateway)
1519 RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
1520 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
1521 goto rtattr_failure;
1522 expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
1523 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
1524 rt->dst.error) < 0)
1525 goto rtattr_failure;
1526 if (dn_is_input_route(rt))
1527 RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fl.iif);
1529 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1530 return skb->len;
1532 nlmsg_failure:
1533 rtattr_failure:
1534 nlmsg_trim(skb, b);
1535 return -1;
1539 * This is called by both endnodes and routers now.
1541 static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
1543 struct net *net = sock_net(in_skb->sk);
1544 struct rtattr **rta = arg;
1545 struct rtmsg *rtm = NLMSG_DATA(nlh);
1546 struct dn_route *rt = NULL;
1547 struct dn_skb_cb *cb;
1548 int err;
1549 struct sk_buff *skb;
1550 struct flowi fl;
1552 if (!net_eq(net, &init_net))
1553 return -EINVAL;
1555 memset(&fl, 0, sizeof(fl));
1556 fl.proto = DNPROTO_NSP;
1558 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1559 if (skb == NULL)
1560 return -ENOBUFS;
1561 skb_reset_mac_header(skb);
1562 cb = DN_SKB_CB(skb);
1564 if (rta[RTA_SRC-1])
1565 memcpy(&fl.fld_src, RTA_DATA(rta[RTA_SRC-1]), 2);
1566 if (rta[RTA_DST-1])
1567 memcpy(&fl.fld_dst, RTA_DATA(rta[RTA_DST-1]), 2);
1568 if (rta[RTA_IIF-1])
1569 memcpy(&fl.iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
1571 if (fl.iif) {
1572 struct net_device *dev;
1573 if ((dev = dev_get_by_index(&init_net, fl.iif)) == NULL) {
1574 kfree_skb(skb);
1575 return -ENODEV;
1577 if (!dev->dn_ptr) {
1578 dev_put(dev);
1579 kfree_skb(skb);
1580 return -ENODEV;
1582 skb->protocol = htons(ETH_P_DNA_RT);
1583 skb->dev = dev;
1584 cb->src = fl.fld_src;
1585 cb->dst = fl.fld_dst;
1586 local_bh_disable();
1587 err = dn_route_input(skb);
1588 local_bh_enable();
1589 memset(cb, 0, sizeof(struct dn_skb_cb));
1590 rt = (struct dn_route *)skb_dst(skb);
1591 if (!err && -rt->dst.error)
1592 err = rt->dst.error;
1593 } else {
1594 int oif = 0;
1595 if (rta[RTA_OIF - 1])
1596 memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
1597 fl.oif = oif;
1598 err = dn_route_output_key((struct dst_entry **)&rt, &fl, 0);
1601 if (skb->dev)
1602 dev_put(skb->dev);
1603 skb->dev = NULL;
1604 if (err)
1605 goto out_free;
1606 skb_dst_set(skb, &rt->dst);
1607 if (rtm->rtm_flags & RTM_F_NOTIFY)
1608 rt->rt_flags |= RTCF_NOTIFY;
1610 err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, RTM_NEWROUTE, 0, 0);
1612 if (err == 0)
1613 goto out_free;
1614 if (err < 0) {
1615 err = -EMSGSIZE;
1616 goto out_free;
1619 return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
1621 out_free:
1622 kfree_skb(skb);
1623 return err;
1627 * For routers, this is called from dn_fib_dump, but for endnodes its
1628 * called directly from the rtnetlink dispatch table.
1630 int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1632 struct net *net = sock_net(skb->sk);
1633 struct dn_route *rt;
1634 int h, s_h;
1635 int idx, s_idx;
1637 if (!net_eq(net, &init_net))
1638 return 0;
1640 if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
1641 return -EINVAL;
1642 if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
1643 return 0;
1645 s_h = cb->args[0];
1646 s_idx = idx = cb->args[1];
1647 for(h = 0; h <= dn_rt_hash_mask; h++) {
1648 if (h < s_h)
1649 continue;
1650 if (h > s_h)
1651 s_idx = 0;
1652 rcu_read_lock_bh();
1653 for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
1655 rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
1656 if (idx < s_idx)
1657 continue;
1658 skb_dst_set(skb, dst_clone(&rt->dst));
1659 if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
1660 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1661 1, NLM_F_MULTI) <= 0) {
1662 skb_dst_drop(skb);
1663 rcu_read_unlock_bh();
1664 goto done;
1666 skb_dst_drop(skb);
1668 rcu_read_unlock_bh();
1671 done:
1672 cb->args[0] = h;
1673 cb->args[1] = idx;
1674 return skb->len;
1677 #ifdef CONFIG_PROC_FS
1678 struct dn_rt_cache_iter_state {
1679 int bucket;
1682 static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1684 struct dn_route *rt = NULL;
1685 struct dn_rt_cache_iter_state *s = seq->private;
1687 for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1688 rcu_read_lock_bh();
1689 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
1690 if (rt)
1691 break;
1692 rcu_read_unlock_bh();
1694 return rt;
1697 static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1699 struct dn_rt_cache_iter_state *s = seq->private;
1701 rt = rcu_dereference_bh(rt->dst.dn_next);
1702 while (!rt) {
1703 rcu_read_unlock_bh();
1704 if (--s->bucket < 0)
1705 break;
1706 rcu_read_lock_bh();
1707 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
1709 return rt;
1712 static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1714 struct dn_route *rt = dn_rt_cache_get_first(seq);
1716 if (rt) {
1717 while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1718 --*pos;
1720 return *pos ? NULL : rt;
1723 static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1725 struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1726 ++*pos;
1727 return rt;
1730 static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1732 if (v)
1733 rcu_read_unlock_bh();
1736 static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1738 struct dn_route *rt = v;
1739 char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1741 seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
1742 rt->dst.dev ? rt->dst.dev->name : "*",
1743 dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1744 dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
1745 atomic_read(&rt->dst.__refcnt),
1746 rt->dst.__use,
1747 (int) dst_metric(&rt->dst, RTAX_RTT));
1748 return 0;
1751 static const struct seq_operations dn_rt_cache_seq_ops = {
1752 .start = dn_rt_cache_seq_start,
1753 .next = dn_rt_cache_seq_next,
1754 .stop = dn_rt_cache_seq_stop,
1755 .show = dn_rt_cache_seq_show,
1758 static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1760 return seq_open_private(file, &dn_rt_cache_seq_ops,
1761 sizeof(struct dn_rt_cache_iter_state));
1764 static const struct file_operations dn_rt_cache_seq_fops = {
1765 .owner = THIS_MODULE,
1766 .open = dn_rt_cache_seq_open,
1767 .read = seq_read,
1768 .llseek = seq_lseek,
1769 .release = seq_release_private,
1772 #endif /* CONFIG_PROC_FS */
1774 void __init dn_route_init(void)
1776 int i, goal, order;
1778 dn_dst_ops.kmem_cachep =
1779 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
1780 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1781 dst_entries_init(&dn_dst_ops);
1782 setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
1783 dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1784 add_timer(&dn_route_timer);
1786 goal = totalram_pages >> (26 - PAGE_SHIFT);
1788 for(order = 0; (1UL << order) < goal; order++)
1789 /* NOTHING */;
1792 * Only want 1024 entries max, since the table is very, very unlikely
1793 * to be larger than that.
1795 while(order && ((((1UL << order) * PAGE_SIZE) /
1796 sizeof(struct dn_rt_hash_bucket)) >= 2048))
1797 order--;
1799 do {
1800 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1801 sizeof(struct dn_rt_hash_bucket);
1802 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1803 dn_rt_hash_mask--;
1804 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1805 __get_free_pages(GFP_ATOMIC, order);
1806 } while (dn_rt_hash_table == NULL && --order > 0);
1808 if (!dn_rt_hash_table)
1809 panic("Failed to allocate DECnet route cache hash table\n");
1811 printk(KERN_INFO
1812 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1813 dn_rt_hash_mask,
1814 (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1816 dn_rt_hash_mask--;
1817 for(i = 0; i <= dn_rt_hash_mask; i++) {
1818 spin_lock_init(&dn_rt_hash_table[i].lock);
1819 dn_rt_hash_table[i].chain = NULL;
1822 dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
1824 proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
1826 #ifdef CONFIG_DECNET_ROUTER
1827 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, dn_fib_dump);
1828 #else
1829 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1830 dn_cache_dump);
1831 #endif
1834 void __exit dn_route_cleanup(void)
1836 del_timer(&dn_route_timer);
1837 dn_run_flush(0);
1839 proc_net_remove(&init_net, "decnet_cache");
1840 dst_entries_destroy(&dn_dst_ops);