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>
12 * Steve Whitehouse : Fixes to allow "intra-ethernet" and
13 * "return-to-sender" bits on outgoing
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
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
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
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>
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>
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>
86 #include <net/fib_rules.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;
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 void dn_dst_destroy(struct dst_entry *);
116 static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
117 static void dn_dst_link_failure(struct sk_buff *);
118 static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu);
119 static int dn_route_input(struct sk_buff *);
120 static void dn_run_flush(unsigned long dummy);
122 static struct dn_rt_hash_bucket *dn_rt_hash_table;
123 static unsigned dn_rt_hash_mask;
125 static struct timer_list dn_route_timer;
126 static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
127 int decnet_dst_gc_interval = 2;
129 static struct dst_ops dn_dst_ops = {
131 .protocol = cpu_to_be16(ETH_P_DNA_RT),
134 .check = dn_dst_check,
135 .default_advmss = dn_dst_default_advmss,
136 .default_mtu = dn_dst_default_mtu,
137 .cow_metrics = dst_cow_metrics_generic,
138 .destroy = dn_dst_destroy,
139 .negative_advice = dn_dst_negative_advice,
140 .link_failure = dn_dst_link_failure,
141 .update_pmtu = dn_dst_update_pmtu,
144 static void dn_dst_destroy(struct dst_entry *dst)
146 dst_destroy_metrics_generic(dst);
149 static __inline__ unsigned dn_hash(__le16 src, __le16 dst)
151 __u16 tmp = (__u16 __force)(src ^ dst);
155 return dn_rt_hash_mask & (unsigned)tmp;
158 static inline void dnrt_free(struct dn_route *rt)
160 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
163 static inline void dnrt_drop(struct dn_route *rt)
165 dst_release(&rt->dst);
166 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
169 static void dn_dst_check_expire(unsigned long dummy)
173 struct dn_route __rcu **rtp;
174 unsigned long now = jiffies;
175 unsigned long expire = 120 * HZ;
177 for (i = 0; i <= dn_rt_hash_mask; i++) {
178 rtp = &dn_rt_hash_table[i].chain;
180 spin_lock(&dn_rt_hash_table[i].lock);
181 while ((rt = rcu_dereference_protected(*rtp,
182 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
183 if (atomic_read(&rt->dst.__refcnt) ||
184 (now - rt->dst.lastuse) < expire) {
185 rtp = &rt->dst.dn_next;
188 *rtp = rt->dst.dn_next;
189 rt->dst.dn_next = NULL;
192 spin_unlock(&dn_rt_hash_table[i].lock);
194 if ((jiffies - now) > 0)
198 mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
201 static int dn_dst_gc(struct dst_ops *ops)
204 struct dn_route __rcu **rtp;
206 unsigned long now = jiffies;
207 unsigned long expire = 10 * HZ;
209 for (i = 0; i <= dn_rt_hash_mask; i++) {
211 spin_lock_bh(&dn_rt_hash_table[i].lock);
212 rtp = &dn_rt_hash_table[i].chain;
214 while ((rt = rcu_dereference_protected(*rtp,
215 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
216 if (atomic_read(&rt->dst.__refcnt) ||
217 (now - rt->dst.lastuse) < expire) {
218 rtp = &rt->dst.dn_next;
221 *rtp = rt->dst.dn_next;
222 rt->dst.dn_next = NULL;
226 spin_unlock_bh(&dn_rt_hash_table[i].lock);
233 * The decnet standards don't impose a particular minimum mtu, what they
234 * do insist on is that the routing layer accepts a datagram of at least
235 * 230 bytes long. Here we have to subtract the routing header length from
236 * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
237 * assume the worst and use a long header size.
239 * We update both the mtu and the advertised mss (i.e. the segment size we
240 * advertise to the other end).
242 static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu)
244 struct neighbour *n = dst_get_neighbour(dst);
248 dn = n ? rcu_dereference_raw(n->dev->dn_ptr) : NULL;
250 if (dn && dn->use_long == 0)
255 if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
256 if (!(dst_metric_locked(dst, RTAX_MTU))) {
257 dst_metric_set(dst, RTAX_MTU, mtu);
258 dst_set_expires(dst, dn_rt_mtu_expires);
260 if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
261 u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
262 u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
263 if (!existing_mss || existing_mss > mss)
264 dst_metric_set(dst, RTAX_ADVMSS, mss);
270 * When a route has been marked obsolete. (e.g. routing cache flush)
272 static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
277 static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
283 static void dn_dst_link_failure(struct sk_buff *skb)
287 static inline int compare_keys(struct flowidn *fl1, struct flowidn *fl2)
289 return ((fl1->daddr ^ fl2->daddr) |
290 (fl1->saddr ^ fl2->saddr) |
291 (fl1->flowidn_mark ^ fl2->flowidn_mark) |
292 (fl1->flowidn_scope ^ fl2->flowidn_scope) |
293 (fl1->flowidn_oif ^ fl2->flowidn_oif) |
294 (fl1->flowidn_iif ^ fl2->flowidn_iif)) == 0;
297 static int dn_insert_route(struct dn_route *rt, unsigned hash, struct dn_route **rp)
299 struct dn_route *rth;
300 struct dn_route __rcu **rthp;
301 unsigned long now = jiffies;
303 rthp = &dn_rt_hash_table[hash].chain;
305 spin_lock_bh(&dn_rt_hash_table[hash].lock);
306 while ((rth = rcu_dereference_protected(*rthp,
307 lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
308 if (compare_keys(&rth->fld, &rt->fld)) {
310 *rthp = rth->dst.dn_next;
311 rcu_assign_pointer(rth->dst.dn_next,
312 dn_rt_hash_table[hash].chain);
313 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
315 dst_use(&rth->dst, now);
316 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
322 rthp = &rth->dst.dn_next;
325 rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
326 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
328 dst_use(&rt->dst, now);
329 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
334 static void dn_run_flush(unsigned long dummy)
337 struct dn_route *rt, *next;
339 for (i = 0; i < dn_rt_hash_mask; i++) {
340 spin_lock_bh(&dn_rt_hash_table[i].lock);
342 if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
343 goto nothing_to_declare;
345 for(; rt; rt = next) {
346 next = rcu_dereference_raw(rt->dst.dn_next);
347 RCU_INIT_POINTER(rt->dst.dn_next, NULL);
348 dst_free((struct dst_entry *)rt);
352 spin_unlock_bh(&dn_rt_hash_table[i].lock);
356 static DEFINE_SPINLOCK(dn_rt_flush_lock);
358 void dn_rt_cache_flush(int delay)
360 unsigned long now = jiffies;
361 int user_mode = !in_interrupt();
364 delay = dn_rt_min_delay;
366 spin_lock_bh(&dn_rt_flush_lock);
368 if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
369 long tmo = (long)(dn_rt_deadline - now);
371 if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
379 spin_unlock_bh(&dn_rt_flush_lock);
384 if (dn_rt_deadline == 0)
385 dn_rt_deadline = now + dn_rt_max_delay;
387 dn_rt_flush_timer.expires = now + delay;
388 add_timer(&dn_rt_flush_timer);
389 spin_unlock_bh(&dn_rt_flush_lock);
393 * dn_return_short - Return a short packet to its sender
394 * @skb: The packet to return
397 static int dn_return_short(struct sk_buff *skb)
399 struct dn_skb_cb *cb;
404 /* Add back headers */
405 skb_push(skb, skb->data - skb_network_header(skb));
407 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
411 /* Skip packet length and point to flags */
413 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
419 *ptr = 0; /* Zero hop count */
423 skb->pkt_type = PACKET_OUTGOING;
424 dn_rt_finish_output(skb, NULL, NULL);
425 return NET_RX_SUCCESS;
429 * dn_return_long - Return a long packet to its sender
430 * @skb: The long format packet to return
433 static int dn_return_long(struct sk_buff *skb)
435 struct dn_skb_cb *cb;
437 unsigned char *src_addr, *dst_addr;
438 unsigned char tmp[ETH_ALEN];
440 /* Add back all headers */
441 skb_push(skb, skb->data - skb_network_header(skb));
443 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
447 /* Ignore packet length and point to flags */
451 if (*ptr & DN_RT_F_PF) {
452 char padlen = (*ptr & ~DN_RT_F_PF);
456 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
462 *ptr = 0; /* Zero hop count */
464 /* Swap source and destination */
465 memcpy(tmp, src_addr, ETH_ALEN);
466 memcpy(src_addr, dst_addr, ETH_ALEN);
467 memcpy(dst_addr, tmp, ETH_ALEN);
469 skb->pkt_type = PACKET_OUTGOING;
470 dn_rt_finish_output(skb, dst_addr, src_addr);
471 return NET_RX_SUCCESS;
475 * dn_route_rx_packet - Try and find a route for an incoming packet
476 * @skb: The packet to find a route for
478 * Returns: result of input function if route is found, error code otherwise
480 static int dn_route_rx_packet(struct sk_buff *skb)
482 struct dn_skb_cb *cb;
485 if ((err = dn_route_input(skb)) == 0)
486 return dst_input(skb);
489 if (decnet_debug_level & 4) {
490 char *devname = skb->dev ? skb->dev->name : "???";
493 "DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%d\n",
494 (int)cb->rt_flags, devname, skb->len,
495 le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
499 if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
500 switch (cb->rt_flags & DN_RT_PKT_MSK) {
501 case DN_RT_PKT_SHORT:
502 return dn_return_short(skb);
504 return dn_return_long(skb);
512 static int dn_route_rx_long(struct sk_buff *skb)
514 struct dn_skb_cb *cb = DN_SKB_CB(skb);
515 unsigned char *ptr = skb->data;
517 if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
521 skb_reset_transport_header(skb);
523 /* Destination info */
525 cb->dst = dn_eth2dn(ptr);
526 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
533 cb->src = dn_eth2dn(ptr);
534 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
539 cb->hops = *ptr++; /* Visit Count */
541 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
551 static int dn_route_rx_short(struct sk_buff *skb)
553 struct dn_skb_cb *cb = DN_SKB_CB(skb);
554 unsigned char *ptr = skb->data;
556 if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
560 skb_reset_transport_header(skb);
562 cb->dst = *(__le16 *)ptr;
564 cb->src = *(__le16 *)ptr;
566 cb->hops = *ptr & 0x3f;
568 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
576 static int dn_route_discard(struct sk_buff *skb)
579 * I know we drop the packet here, but thats considered success in
583 return NET_RX_SUCCESS;
586 static int dn_route_ptp_hello(struct sk_buff *skb)
589 dn_neigh_pointopoint_hello(skb);
590 return NET_RX_SUCCESS;
593 int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
595 struct dn_skb_cb *cb;
596 unsigned char flags = 0;
597 __u16 len = le16_to_cpu(*(__le16 *)skb->data);
598 struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
599 unsigned char padlen = 0;
601 if (!net_eq(dev_net(dev), &init_net))
607 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
610 if (!pskb_may_pull(skb, 3))
624 cb->iif = dev->ifindex;
627 * If we have padding, remove it.
629 if (flags & DN_RT_F_PF) {
630 padlen = flags & ~DN_RT_F_PF;
631 if (!pskb_may_pull(skb, padlen + 1))
633 skb_pull(skb, padlen);
637 skb_reset_network_header(skb);
640 * Weed out future version DECnet
642 if (flags & DN_RT_F_VER)
645 cb->rt_flags = flags;
647 if (decnet_debug_level & 1)
649 "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
650 (int)flags, (dev) ? dev->name : "???", len, skb->len,
653 if (flags & DN_RT_PKT_CNTL) {
654 if (unlikely(skb_linearize(skb)))
657 switch (flags & DN_RT_CNTL_MSK) {
659 dn_dev_init_pkt(skb);
662 dn_dev_veri_pkt(skb);
666 if (dn->parms.state != DN_DEV_S_RU)
669 switch (flags & DN_RT_CNTL_MSK) {
671 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
677 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
681 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
683 dn_neigh_router_hello);
686 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
688 dn_neigh_endnode_hello);
691 if (dn->parms.state != DN_DEV_S_RU)
694 skb_pull(skb, 1); /* Pull flags */
696 switch (flags & DN_RT_PKT_MSK) {
698 return dn_route_rx_long(skb);
699 case DN_RT_PKT_SHORT:
700 return dn_route_rx_short(skb);
710 static int dn_to_neigh_output(struct sk_buff *skb)
712 struct dst_entry *dst = skb_dst(skb);
713 struct neighbour *n = dst_get_neighbour(dst);
715 return n->output(n, skb);
718 static int dn_output(struct sk_buff *skb)
720 struct dst_entry *dst = skb_dst(skb);
721 struct dn_route *rt = (struct dn_route *)dst;
722 struct net_device *dev = dst->dev;
723 struct dn_skb_cb *cb = DN_SKB_CB(skb);
724 struct neighbour *neigh;
728 if ((neigh = dst_get_neighbour(dst)) == NULL)
733 cb->src = rt->rt_saddr;
734 cb->dst = rt->rt_daddr;
737 * Always set the Intra-Ethernet bit on all outgoing packets
738 * originated on this node. Only valid flag from upper layers
739 * is return-to-sender-requested. Set hop count to 0 too.
741 cb->rt_flags &= ~DN_RT_F_RQR;
742 cb->rt_flags |= DN_RT_F_IE;
745 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
750 printk(KERN_DEBUG "dn_output: This should not happen\n");
757 static int dn_forward(struct sk_buff *skb)
759 struct dn_skb_cb *cb = DN_SKB_CB(skb);
760 struct dst_entry *dst = skb_dst(skb);
761 struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
764 #ifdef CONFIG_NETFILTER
765 struct net_device *dev = skb->dev;
768 if (skb->pkt_type != PACKET_HOST)
771 /* Ensure that we have enough space for headers */
772 rt = (struct dn_route *)skb_dst(skb);
773 header_len = dn_db->use_long ? 21 : 6;
774 if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
778 * Hop count exceeded.
783 skb->dev = rt->dst.dev;
786 * If packet goes out same interface it came in on, then set
787 * the Intra-Ethernet bit. This has no effect for short
788 * packets, so we don't need to test for them here.
790 cb->rt_flags &= ~DN_RT_F_IE;
791 if (rt->rt_flags & RTCF_DOREDIRECT)
792 cb->rt_flags |= DN_RT_F_IE;
794 return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
803 * Used to catch bugs. This should never normally get
806 static int dn_rt_bug(struct sk_buff *skb)
808 if (net_ratelimit()) {
809 struct dn_skb_cb *cb = DN_SKB_CB(skb);
811 printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
812 le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
820 static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
822 return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
825 static unsigned int dn_dst_default_mtu(const struct dst_entry *dst)
827 return dst->dev->mtu;
830 static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
832 struct dn_fib_info *fi = res->fi;
833 struct net_device *dev = rt->dst.dev;
834 unsigned int mss_metric;
838 if (DN_FIB_RES_GW(*res) &&
839 DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
840 rt->rt_gateway = DN_FIB_RES_GW(*res);
841 dst_init_metrics(&rt->dst, fi->fib_metrics, true);
843 rt->rt_type = res->type;
845 if (dev != NULL && dst_get_neighbour(&rt->dst) == NULL) {
846 n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
849 dst_set_neighbour(&rt->dst, n);
852 if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
853 dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
854 mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
856 unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
857 if (mss_metric > mss)
858 dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
863 static inline int dn_match_addr(__le16 addr1, __le16 addr2)
865 __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
874 static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
877 struct dn_dev *dn_db;
878 struct dn_ifaddr *ifa;
883 dn_db = rcu_dereference(dev->dn_ptr);
884 for (ifa = rcu_dereference(dn_db->ifa_list);
886 ifa = rcu_dereference(ifa->ifa_next)) {
887 if (ifa->ifa_scope > scope)
890 saddr = ifa->ifa_local;
893 ret = dn_match_addr(daddr, ifa->ifa_local);
894 if (ret > best_match)
895 saddr = ifa->ifa_local;
897 saddr = ifa->ifa_local;
904 static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
906 return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
909 static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
911 __le16 mask = dnet_make_mask(res->prefixlen);
912 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
915 static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oldflp, int try_hard)
917 struct flowidn fld = {
918 .daddr = oldflp->daddr,
919 .saddr = oldflp->saddr,
920 .flowidn_scope = RT_SCOPE_UNIVERSE,
921 .flowidn_mark = oldflp->flowidn_mark,
922 .flowidn_iif = init_net.loopback_dev->ifindex,
923 .flowidn_oif = oldflp->flowidn_oif,
925 struct dn_route *rt = NULL;
926 struct net_device *dev_out = NULL, *dev;
927 struct neighbour *neigh = NULL;
930 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
935 if (decnet_debug_level & 16)
937 "dn_route_output_slow: dst=%04x src=%04x mark=%d"
938 " iif=%d oif=%d\n", le16_to_cpu(oldflp->daddr),
939 le16_to_cpu(oldflp->saddr),
940 oldflp->flowidn_mark, init_net.loopback_dev->ifindex,
941 oldflp->flowidn_oif);
943 /* If we have an output interface, verify its a DECnet device */
944 if (oldflp->flowidn_oif) {
945 dev_out = dev_get_by_index(&init_net, oldflp->flowidn_oif);
947 if (dev_out && dev_out->dn_ptr == NULL) {
955 /* If we have a source address, verify that its a local address */
957 err = -EADDRNOTAVAIL;
960 if (dn_dev_islocal(dev_out, oldflp->saddr))
966 for_each_netdev_rcu(&init_net, dev) {
969 if (!dn_dev_islocal(dev, oldflp->saddr))
971 if ((dev->flags & IFF_LOOPBACK) &&
973 !dn_dev_islocal(dev, oldflp->daddr))
987 /* No destination? Assume its local */
989 fld.daddr = fld.saddr;
991 err = -EADDRNOTAVAIL;
994 dev_out = init_net.loopback_dev;
998 fld.saddr = dnet_select_source(dev_out, 0,
1003 fld.flowidn_oif = init_net.loopback_dev->ifindex;
1004 res.type = RTN_LOCAL;
1008 if (decnet_debug_level & 16)
1010 "dn_route_output_slow: initial checks complete."
1011 " dst=%o4x src=%04x oif=%d try_hard=%d\n",
1012 le16_to_cpu(fld.daddr), le16_to_cpu(fld.saddr),
1013 fld.flowidn_oif, try_hard);
1016 * N.B. If the kernel is compiled without router support then
1017 * dn_fib_lookup() will evaluate to non-zero so this if () block
1018 * will always be executed.
1021 if (try_hard || (err = dn_fib_lookup(&fld, &res)) != 0) {
1022 struct dn_dev *dn_db;
1026 * Here the fallback is basically the standard algorithm for
1027 * routing in endnodes which is described in the DECnet routing
1030 * If we are not trying hard, look in neighbour cache.
1031 * The result is tested to ensure that if a specific output
1032 * device/source address was requested, then we honour that
1036 neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fld.daddr);
1038 if ((oldflp->flowidn_oif &&
1039 (neigh->dev->ifindex != oldflp->flowidn_oif)) ||
1041 (!dn_dev_islocal(neigh->dev,
1043 neigh_release(neigh);
1048 if (dn_dev_islocal(neigh->dev, fld.daddr)) {
1049 dev_out = init_net.loopback_dev;
1050 res.type = RTN_LOCAL;
1052 dev_out = neigh->dev;
1060 /* Not there? Perhaps its a local address */
1061 if (dev_out == NULL)
1062 dev_out = dn_dev_get_default();
1064 if (dev_out == NULL)
1066 dn_db = rcu_dereference_raw(dev_out->dn_ptr);
1067 /* Possible improvement - check all devices for local addr */
1068 if (dn_dev_islocal(dev_out, fld.daddr)) {
1070 dev_out = init_net.loopback_dev;
1072 res.type = RTN_LOCAL;
1075 /* Not local either.... try sending it to the default router */
1076 neigh = neigh_clone(dn_db->router);
1077 BUG_ON(neigh && neigh->dev != dev_out);
1079 /* Ok then, we assume its directly connected and move on */
1082 gateway = ((struct dn_neigh *)neigh)->addr;
1084 gateway = fld.daddr;
1085 if (fld.saddr == 0) {
1086 fld.saddr = dnet_select_source(dev_out, gateway,
1087 res.type == RTN_LOCAL ?
1090 if (fld.saddr == 0 && res.type != RTN_LOCAL)
1093 fld.flowidn_oif = dev_out->ifindex;
1098 if (res.type == RTN_NAT)
1101 if (res.type == RTN_LOCAL) {
1103 fld.saddr = fld.daddr;
1106 dev_out = init_net.loopback_dev;
1108 fld.flowidn_oif = dev_out->ifindex;
1110 dn_fib_info_put(res.fi);
1115 if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1116 dn_fib_select_multipath(&fld, &res);
1119 * We could add some logic to deal with default routes here and
1120 * get rid of some of the special casing above.
1124 fld.saddr = DN_FIB_RES_PREFSRC(res);
1128 dev_out = DN_FIB_RES_DEV(res);
1130 fld.flowidn_oif = dev_out->ifindex;
1131 gateway = DN_FIB_RES_GW(res);
1134 if (dev_out->flags & IFF_LOOPBACK)
1135 flags |= RTCF_LOCAL;
1137 rt = dst_alloc(&dn_dst_ops, dev_out, 1, 0, DST_HOST);
1141 memset(&rt->fld, 0, sizeof(rt->fld));
1142 rt->fld.saddr = oldflp->saddr;
1143 rt->fld.daddr = oldflp->daddr;
1144 rt->fld.flowidn_oif = oldflp->flowidn_oif;
1145 rt->fld.flowidn_iif = 0;
1146 rt->fld.flowidn_mark = oldflp->flowidn_mark;
1148 rt->rt_saddr = fld.saddr;
1149 rt->rt_daddr = fld.daddr;
1150 rt->rt_gateway = gateway ? gateway : fld.daddr;
1151 rt->rt_local_src = fld.saddr;
1153 rt->rt_dst_map = fld.daddr;
1154 rt->rt_src_map = fld.saddr;
1156 dst_set_neighbour(&rt->dst, neigh);
1159 rt->dst.lastuse = jiffies;
1160 rt->dst.output = dn_output;
1161 rt->dst.input = dn_rt_bug;
1162 rt->rt_flags = flags;
1163 if (flags & RTCF_LOCAL)
1164 rt->dst.input = dn_nsp_rx;
1166 err = dn_rt_set_next_hop(rt, &res);
1170 hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
1171 dn_insert_route(rt, hash, (struct dn_route **)pprt);
1175 neigh_release(neigh);
1177 dn_fib_res_put(&res);
1184 err = -EADDRNOTAVAIL;
1199 * N.B. The flags may be moved into the flowi at some future stage.
1201 static int __dn_route_output_key(struct dst_entry **pprt, const struct flowidn *flp, int flags)
1203 unsigned hash = dn_hash(flp->saddr, flp->daddr);
1204 struct dn_route *rt = NULL;
1206 if (!(flags & MSG_TRYHARD)) {
1208 for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
1209 rt = rcu_dereference_bh(rt->dst.dn_next)) {
1210 if ((flp->daddr == rt->fld.daddr) &&
1211 (flp->saddr == rt->fld.saddr) &&
1212 (flp->flowidn_mark == rt->fld.flowidn_mark) &&
1213 dn_is_output_route(rt) &&
1214 (rt->fld.flowidn_oif == flp->flowidn_oif)) {
1215 dst_use(&rt->dst, jiffies);
1216 rcu_read_unlock_bh();
1221 rcu_read_unlock_bh();
1224 return dn_route_output_slow(pprt, flp, flags);
1227 static int dn_route_output_key(struct dst_entry **pprt, struct flowidn *flp, int flags)
1231 err = __dn_route_output_key(pprt, flp, flags);
1232 if (err == 0 && flp->flowidn_proto) {
1233 *pprt = xfrm_lookup(&init_net, *pprt,
1234 flowidn_to_flowi(flp), NULL, 0);
1235 if (IS_ERR(*pprt)) {
1236 err = PTR_ERR(*pprt);
1243 int dn_route_output_sock(struct dst_entry **pprt, struct flowidn *fl, struct sock *sk, int flags)
1247 err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
1248 if (err == 0 && fl->flowidn_proto) {
1249 if (!(flags & MSG_DONTWAIT))
1250 fl->flowidn_flags |= FLOWI_FLAG_CAN_SLEEP;
1251 *pprt = xfrm_lookup(&init_net, *pprt,
1252 flowidn_to_flowi(fl), sk, 0);
1253 if (IS_ERR(*pprt)) {
1254 err = PTR_ERR(*pprt);
1261 static int dn_route_input_slow(struct sk_buff *skb)
1263 struct dn_route *rt = NULL;
1264 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1265 struct net_device *in_dev = skb->dev;
1266 struct net_device *out_dev = NULL;
1267 struct dn_dev *dn_db;
1268 struct neighbour *neigh = NULL;
1272 __le16 local_src = 0;
1273 struct flowidn fld = {
1276 .flowidn_scope = RT_SCOPE_UNIVERSE,
1277 .flowidn_mark = skb->mark,
1278 .flowidn_iif = skb->dev->ifindex,
1280 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1286 if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
1289 /* Zero source addresses are not allowed */
1294 * In this case we've just received a packet from a source
1295 * outside ourselves pretending to come from us. We don't
1296 * allow it any further to prevent routing loops, spoofing and
1297 * other nasties. Loopback packets already have the dst attached
1298 * so this only affects packets which have originated elsewhere.
1301 if (dn_dev_islocal(in_dev, cb->src))
1304 err = dn_fib_lookup(&fld, &res);
1309 * Is the destination us ?
1311 if (!dn_dev_islocal(in_dev, cb->dst))
1314 res.type = RTN_LOCAL;
1316 __le16 src_map = fld.saddr;
1319 out_dev = DN_FIB_RES_DEV(res);
1320 if (out_dev == NULL) {
1321 if (net_ratelimit())
1322 printk(KERN_CRIT "Bug in dn_route_input_slow() "
1323 "No output device\n");
1329 src_map = fld.saddr; /* no NAT support for now */
1331 gateway = DN_FIB_RES_GW(res);
1332 if (res.type == RTN_NAT) {
1333 fld.daddr = dn_fib_rules_map_destination(fld.daddr, &res);
1334 dn_fib_res_put(&res);
1336 if (dn_fib_lookup(&fld, &res))
1339 if (res.type != RTN_UNICAST)
1342 gateway = fld.daddr;
1344 fld.saddr = src_map;
1350 * Forwarding check here, we only check for forwarding
1351 * being turned off, if you want to only forward intra
1352 * area, its up to you to set the routing tables up
1355 if (dn_db->parms.forwarding == 0)
1358 if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1359 dn_fib_select_multipath(&fld, &res);
1362 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1363 * flag as a hint to set the intra-ethernet bit when
1364 * forwarding. If we've got NAT in operation, we don't do
1365 * this optimisation.
1367 if (out_dev == in_dev && !(flags & RTCF_NAT))
1368 flags |= RTCF_DOREDIRECT;
1370 local_src = DN_FIB_RES_PREFSRC(res);
1373 case RTN_UNREACHABLE:
1376 flags |= RTCF_LOCAL;
1377 fld.saddr = cb->dst;
1378 fld.daddr = cb->src;
1380 /* Routing tables gave us a gateway */
1384 /* Packet was intra-ethernet, so we know its on-link */
1385 if (cb->rt_flags & DN_RT_F_IE) {
1387 flags |= RTCF_DIRECTSRC;
1391 /* Use the default router if there is one */
1392 neigh = neigh_clone(dn_db->router);
1394 gateway = ((struct dn_neigh *)neigh)->addr;
1398 /* Close eyes and pray */
1400 flags |= RTCF_DIRECTSRC;
1407 rt = dst_alloc(&dn_dst_ops, out_dev, 0, 0, DST_HOST);
1411 memset(&rt->fld, 0, sizeof(rt->fld));
1412 rt->rt_saddr = fld.saddr;
1413 rt->rt_daddr = fld.daddr;
1414 rt->rt_gateway = fld.daddr;
1416 rt->rt_gateway = gateway;
1417 rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
1419 rt->rt_dst_map = fld.daddr;
1420 rt->rt_src_map = fld.saddr;
1422 rt->fld.saddr = cb->src;
1423 rt->fld.daddr = cb->dst;
1424 rt->fld.flowidn_oif = 0;
1425 rt->fld.flowidn_iif = in_dev->ifindex;
1426 rt->fld.flowidn_mark = fld.flowidn_mark;
1428 dst_set_neighbour(&rt->dst, neigh);
1429 rt->dst.lastuse = jiffies;
1430 rt->dst.output = dn_rt_bug;
1433 rt->dst.input = dn_forward;
1436 rt->dst.output = dn_output;
1437 rt->dst.input = dn_nsp_rx;
1438 rt->dst.dev = in_dev;
1439 flags |= RTCF_LOCAL;
1442 case RTN_UNREACHABLE:
1444 rt->dst.input = dst_discard;
1446 rt->rt_flags = flags;
1448 err = dn_rt_set_next_hop(rt, &res);
1452 hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
1453 dn_insert_route(rt, hash, &rt);
1454 skb_dst_set(skb, &rt->dst);
1458 neigh_release(neigh);
1460 dn_fib_res_put(&res);
1480 static int dn_route_input(struct sk_buff *skb)
1482 struct dn_route *rt;
1483 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1484 unsigned hash = dn_hash(cb->src, cb->dst);
1490 for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
1491 rt = rcu_dereference(rt->dst.dn_next)) {
1492 if ((rt->fld.saddr == cb->src) &&
1493 (rt->fld.daddr == cb->dst) &&
1494 (rt->fld.flowidn_oif == 0) &&
1495 (rt->fld.flowidn_mark == skb->mark) &&
1496 (rt->fld.flowidn_iif == cb->iif)) {
1497 dst_use(&rt->dst, jiffies);
1499 skb_dst_set(skb, (struct dst_entry *)rt);
1505 return dn_route_input_slow(skb);
1508 static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1509 int event, int nowait, unsigned int flags)
1511 struct dn_route *rt = (struct dn_route *)skb_dst(skb);
1513 struct nlmsghdr *nlh;
1514 unsigned char *b = skb_tail_pointer(skb);
1517 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
1518 r = NLMSG_DATA(nlh);
1519 r->rtm_family = AF_DECnet;
1520 r->rtm_dst_len = 16;
1523 r->rtm_table = RT_TABLE_MAIN;
1524 RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
1525 r->rtm_type = rt->rt_type;
1526 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1527 r->rtm_scope = RT_SCOPE_UNIVERSE;
1528 r->rtm_protocol = RTPROT_UNSPEC;
1529 if (rt->rt_flags & RTCF_NOTIFY)
1530 r->rtm_flags |= RTM_F_NOTIFY;
1531 RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
1532 if (rt->fld.saddr) {
1533 r->rtm_src_len = 16;
1534 RTA_PUT(skb, RTA_SRC, 2, &rt->fld.saddr);
1537 RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
1539 * Note to self - change this if input routes reverse direction when
1540 * they deal only with inputs and not with replies like they do
1543 RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
1544 if (rt->rt_daddr != rt->rt_gateway)
1545 RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
1546 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
1547 goto rtattr_failure;
1548 expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
1549 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
1551 goto rtattr_failure;
1552 if (dn_is_input_route(rt))
1553 RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fld.flowidn_iif);
1555 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1565 * This is called by both endnodes and routers now.
1567 static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
1569 struct net *net = sock_net(in_skb->sk);
1570 struct rtattr **rta = arg;
1571 struct rtmsg *rtm = NLMSG_DATA(nlh);
1572 struct dn_route *rt = NULL;
1573 struct dn_skb_cb *cb;
1575 struct sk_buff *skb;
1578 if (!net_eq(net, &init_net))
1581 memset(&fld, 0, sizeof(fld));
1582 fld.flowidn_proto = DNPROTO_NSP;
1584 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1587 skb_reset_mac_header(skb);
1588 cb = DN_SKB_CB(skb);
1591 memcpy(&fld.saddr, RTA_DATA(rta[RTA_SRC-1]), 2);
1593 memcpy(&fld.daddr, RTA_DATA(rta[RTA_DST-1]), 2);
1595 memcpy(&fld.flowidn_iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
1597 if (fld.flowidn_iif) {
1598 struct net_device *dev;
1599 if ((dev = dev_get_by_index(&init_net, fld.flowidn_iif)) == NULL) {
1608 skb->protocol = htons(ETH_P_DNA_RT);
1610 cb->src = fld.saddr;
1611 cb->dst = fld.daddr;
1613 err = dn_route_input(skb);
1615 memset(cb, 0, sizeof(struct dn_skb_cb));
1616 rt = (struct dn_route *)skb_dst(skb);
1617 if (!err && -rt->dst.error)
1618 err = rt->dst.error;
1621 if (rta[RTA_OIF - 1])
1622 memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
1623 fld.flowidn_oif = oif;
1624 err = dn_route_output_key((struct dst_entry **)&rt, &fld, 0);
1632 skb_dst_set(skb, &rt->dst);
1633 if (rtm->rtm_flags & RTM_F_NOTIFY)
1634 rt->rt_flags |= RTCF_NOTIFY;
1636 err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, RTM_NEWROUTE, 0, 0);
1645 return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
1653 * For routers, this is called from dn_fib_dump, but for endnodes its
1654 * called directly from the rtnetlink dispatch table.
1656 int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1658 struct net *net = sock_net(skb->sk);
1659 struct dn_route *rt;
1663 if (!net_eq(net, &init_net))
1666 if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
1668 if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
1672 s_idx = idx = cb->args[1];
1673 for(h = 0; h <= dn_rt_hash_mask; h++) {
1679 for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
1681 rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
1684 skb_dst_set(skb, dst_clone(&rt->dst));
1685 if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
1686 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1687 1, NLM_F_MULTI) <= 0) {
1689 rcu_read_unlock_bh();
1694 rcu_read_unlock_bh();
1703 #ifdef CONFIG_PROC_FS
1704 struct dn_rt_cache_iter_state {
1708 static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1710 struct dn_route *rt = NULL;
1711 struct dn_rt_cache_iter_state *s = seq->private;
1713 for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1715 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
1718 rcu_read_unlock_bh();
1723 static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1725 struct dn_rt_cache_iter_state *s = seq->private;
1727 rt = rcu_dereference_bh(rt->dst.dn_next);
1729 rcu_read_unlock_bh();
1730 if (--s->bucket < 0)
1733 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
1738 static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1740 struct dn_route *rt = dn_rt_cache_get_first(seq);
1743 while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1746 return *pos ? NULL : rt;
1749 static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1751 struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1756 static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1759 rcu_read_unlock_bh();
1762 static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1764 struct dn_route *rt = v;
1765 char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1767 seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
1768 rt->dst.dev ? rt->dst.dev->name : "*",
1769 dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1770 dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
1771 atomic_read(&rt->dst.__refcnt),
1773 (int) dst_metric(&rt->dst, RTAX_RTT));
1777 static const struct seq_operations dn_rt_cache_seq_ops = {
1778 .start = dn_rt_cache_seq_start,
1779 .next = dn_rt_cache_seq_next,
1780 .stop = dn_rt_cache_seq_stop,
1781 .show = dn_rt_cache_seq_show,
1784 static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1786 return seq_open_private(file, &dn_rt_cache_seq_ops,
1787 sizeof(struct dn_rt_cache_iter_state));
1790 static const struct file_operations dn_rt_cache_seq_fops = {
1791 .owner = THIS_MODULE,
1792 .open = dn_rt_cache_seq_open,
1794 .llseek = seq_lseek,
1795 .release = seq_release_private,
1798 #endif /* CONFIG_PROC_FS */
1800 void __init dn_route_init(void)
1804 dn_dst_ops.kmem_cachep =
1805 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
1806 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1807 dst_entries_init(&dn_dst_ops);
1808 setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
1809 dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1810 add_timer(&dn_route_timer);
1812 goal = totalram_pages >> (26 - PAGE_SHIFT);
1814 for(order = 0; (1UL << order) < goal; order++)
1818 * Only want 1024 entries max, since the table is very, very unlikely
1819 * to be larger than that.
1821 while(order && ((((1UL << order) * PAGE_SIZE) /
1822 sizeof(struct dn_rt_hash_bucket)) >= 2048))
1826 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1827 sizeof(struct dn_rt_hash_bucket);
1828 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1830 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1831 __get_free_pages(GFP_ATOMIC, order);
1832 } while (dn_rt_hash_table == NULL && --order > 0);
1834 if (!dn_rt_hash_table)
1835 panic("Failed to allocate DECnet route cache hash table\n");
1838 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1840 (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1843 for(i = 0; i <= dn_rt_hash_mask; i++) {
1844 spin_lock_init(&dn_rt_hash_table[i].lock);
1845 dn_rt_hash_table[i].chain = NULL;
1848 dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
1850 proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
1852 #ifdef CONFIG_DECNET_ROUTER
1853 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1856 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1857 dn_cache_dump, NULL);
1861 void __exit dn_route_cleanup(void)
1863 del_timer(&dn_route_timer);
1866 proc_net_remove(&init_net, "decnet_cache");
1867 dst_entries_destroy(&dn_dst_ops);