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 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
= {
130 .protocol
= cpu_to_be16(ETH_P_DNA_RT
),
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
);
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
)
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
;
180 *rtp
= rt
->dst
.dn_next
;
181 rt
->dst
.dn_next
= NULL
;
184 spin_unlock(&dn_rt_hash_table
[i
].lock
);
186 if ((jiffies
- now
) > 0)
190 mod_timer(&dn_route_timer
, now
+ decnet_dst_gc_interval
* HZ
);
193 static int dn_dst_gc(struct dst_ops
*ops
)
196 struct dn_route __rcu
**rtp
;
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
;
213 *rtp
= rt
->dst
.dn_next
;
214 rt
->dst
.dn_next
= NULL
;
218 spin_unlock_bh(&dn_rt_hash_table
[i
].lock
);
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
)
237 struct dn_dev
*dn
= dst
->neighbour
?
238 rcu_dereference_raw(dst
->neighbour
->dev
->dn_ptr
) : NULL
;
240 if (dn
&& dn
->use_long
== 0)
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
)
267 static struct dst_entry
*dn_dst_negative_advice(struct dst_entry
*dst
)
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
)) {
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
);
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
);
324 static void dn_run_flush(unsigned long dummy
)
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
);
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();
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
)
369 spin_unlock_bh(&dn_rt_flush_lock
);
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
;
394 /* Add back headers */
395 skb_push(skb
, skb
->data
- skb_network_header(skb
));
397 if ((skb
= skb_unshare(skb
, GFP_ATOMIC
)) == NULL
)
401 /* Skip packet length and point to flags */
403 *ptr
++ = (cb
->rt_flags
& ~DN_RT_F_RQR
) | DN_RT_F_RTS
;
409 *ptr
= 0; /* Zero hop count */
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
;
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
)
437 /* Ignore packet length and point to flags */
441 if (*ptr
& DN_RT_F_PF
) {
442 char padlen
= (*ptr
& ~DN_RT_F_PF
);
446 *ptr
++ = (cb
->rt_flags
& ~DN_RT_F_RQR
) | DN_RT_F_RTS
;
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
;
475 if ((err
= dn_route_input(skb
)) == 0)
476 return dst_input(skb
);
479 if (decnet_debug_level
& 4) {
480 char *devname
= skb
->dev
? skb
->dev
->name
: "???";
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
),
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
);
494 return dn_return_long(skb
);
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 */
511 skb_reset_transport_header(skb
);
513 /* Destination info */
515 cb
->dst
= dn_eth2dn(ptr
);
516 if (memcmp(ptr
, dn_hiord_addr
, 4) != 0)
523 cb
->src
= dn_eth2dn(ptr
);
524 if (memcmp(ptr
, dn_hiord_addr
, 4) != 0)
529 cb
->hops
= *ptr
++; /* Visit Count */
531 return NF_HOOK(NFPROTO_DECNET
, NF_DN_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
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 */
550 skb_reset_transport_header(skb
);
552 cb
->dst
= *(__le16
*)ptr
;
554 cb
->src
= *(__le16
*)ptr
;
556 cb
->hops
= *ptr
& 0x3f;
558 return NF_HOOK(NFPROTO_DECNET
, NF_DN_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
566 static int dn_route_discard(struct sk_buff
*skb
)
569 * I know we drop the packet here, but thats considered success in
573 return NET_RX_SUCCESS
;
576 static int dn_route_ptp_hello(struct sk_buff
*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
))
597 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
600 if (!pskb_may_pull(skb
, 3))
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))
623 skb_pull(skb
, padlen
);
627 skb_reset_network_header(skb
);
630 * Weed out future version DECnet
632 if (flags
& DN_RT_F_VER
)
635 cb
->rt_flags
= flags
;
637 if (decnet_debug_level
& 1)
639 "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
640 (int)flags
, (dev
) ? dev
->name
: "???", len
, skb
->len
,
643 if (flags
& DN_RT_PKT_CNTL
) {
644 if (unlikely(skb_linearize(skb
)))
647 switch(flags
& DN_RT_CNTL_MSK
) {
649 dn_dev_init_pkt(skb
);
652 dn_dev_veri_pkt(skb
);
656 if (dn
->parms
.state
!= DN_DEV_S_RU
)
659 switch(flags
& DN_RT_CNTL_MSK
) {
661 return NF_HOOK(NFPROTO_DECNET
, NF_DN_HELLO
,
667 return NF_HOOK(NFPROTO_DECNET
, NF_DN_ROUTE
,
671 return NF_HOOK(NFPROTO_DECNET
, NF_DN_HELLO
,
673 dn_neigh_router_hello
);
676 return NF_HOOK(NFPROTO_DECNET
, NF_DN_HELLO
,
678 dn_neigh_endnode_hello
);
681 if (dn
->parms
.state
!= DN_DEV_S_RU
)
684 skb_pull(skb
, 1); /* Pull flags */
686 switch(flags
& DN_RT_PKT_MSK
) {
688 return dn_route_rx_long(skb
);
689 case DN_RT_PKT_SHORT
:
690 return dn_route_rx_short(skb
);
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
;
710 if ((neigh
= dst
->neighbour
) == NULL
)
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
;
727 return NF_HOOK(NFPROTO_DECNET
, NF_DN_LOCAL_OUT
, skb
, NULL
, dev
,
732 printk(KERN_DEBUG
"dn_output: This should not happen\n");
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
);
745 struct neighbour
*neigh
= dst
->neighbour
;
747 #ifdef CONFIG_NETFILTER
748 struct net_device
*dev
= skb
->dev
;
751 if (skb
->pkt_type
!= PACKET_HOST
)
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
))
761 * Hop count exceeded.
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
,
786 * Used to catch bugs. This should never normally get
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
));
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
;
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
);
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
);
839 unsigned int mss
= dn_mss_from_pmtu(dev
, dst_mtu(&rt
->dst
));
841 dst_metric_set(&rt
->dst
, RTAX_ADVMSS
, mss
);
846 static inline int dn_match_addr(__le16 addr1
, __le16 addr2
)
848 __u16 tmp
= le16_to_cpu(addr1
) ^ le16_to_cpu(addr2
);
857 static __le16
dnet_select_source(const struct net_device
*dev
, __le16 daddr
, int scope
)
860 struct dn_dev
*dn_db
;
861 struct dn_ifaddr
*ifa
;
866 dn_db
= rcu_dereference(dev
->dn_ptr
);
867 for (ifa
= rcu_dereference(dn_db
->ifa_list
);
869 ifa
= rcu_dereference(ifa
->ifa_next
)) {
870 if (ifa
->ifa_scope
> scope
)
873 saddr
= ifa
->ifa_local
;
876 ret
= dn_match_addr(daddr
, ifa
->ifa_local
);
877 if (ret
> best_match
)
878 saddr
= ifa
->ifa_local
;
880 saddr
= ifa
->ifa_local
;
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
;
911 struct dn_fib_res res
= { .fi
= NULL
, .type
= RTN_UNICAST
};
916 if (decnet_debug_level
& 16)
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 */
925 dev_out
= dev_get_by_index(&init_net
, oldflp
->oif
);
927 if (dev_out
&& dev_out
->dn_ptr
== NULL
) {
935 /* If we have a source address, verify that its a local address */
936 if (oldflp
->fld_src
) {
937 err
= -EADDRNOTAVAIL
;
940 if (dn_dev_islocal(dev_out
, oldflp
->fld_src
))
946 for_each_netdev_rcu(&init_net
, dev
) {
949 if (!dn_dev_islocal(dev
, oldflp
->fld_src
))
951 if ((dev
->flags
& IFF_LOOPBACK
) &&
953 !dn_dev_islocal(dev
, oldflp
->fld_dst
))
967 /* No destination? Assume its local */
969 fl
.fld_dst
= fl
.fld_src
;
971 err
= -EADDRNOTAVAIL
;
974 dev_out
= init_net
.loopback_dev
;
978 fl
.fld_src
= dnet_select_source(dev_out
, 0,
983 fl
.oif
= init_net
.loopback_dev
->ifindex
;
984 res
.type
= RTN_LOCAL
;
988 if (decnet_debug_level
& 16)
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
),
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.
1001 if (try_hard
|| (err
= dn_fib_lookup(&fl
, &res
)) != 0) {
1002 struct dn_dev
*dn_db
;
1006 * Here the fallback is basically the standard algorithm for
1007 * routing in endnodes which is described in the DECnet routing
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
1016 neigh
= neigh_lookup_nodev(&dn_neigh_table
, &init_net
, &fl
.fld_dst
);
1019 (neigh
->dev
->ifindex
!= oldflp
->oif
)) ||
1021 (!dn_dev_islocal(neigh
->dev
,
1022 oldflp
->fld_src
)))) {
1023 neigh_release(neigh
);
1028 if (dn_dev_islocal(neigh
->dev
, fl
.fld_dst
)) {
1029 dev_out
= init_net
.loopback_dev
;
1030 res
.type
= RTN_LOCAL
;
1032 dev_out
= neigh
->dev
;
1040 /* Not there? Perhaps its a local address */
1041 if (dev_out
== NULL
)
1042 dev_out
= dn_dev_get_default();
1044 if (dev_out
== NULL
)
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
)) {
1050 dev_out
= init_net
.loopback_dev
;
1052 res
.type
= RTN_LOCAL
;
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 */
1062 gateway
= ((struct dn_neigh
*)neigh
)->addr
;
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
?
1070 if (fl
.fld_src
== 0 && res
.type
!= RTN_LOCAL
)
1073 fl
.oif
= dev_out
->ifindex
;
1078 if (res
.type
== RTN_NAT
)
1081 if (res
.type
== RTN_LOCAL
) {
1083 fl
.fld_src
= fl
.fld_dst
;
1086 dev_out
= init_net
.loopback_dev
;
1088 fl
.oif
= dev_out
->ifindex
;
1090 dn_fib_info_put(res
.fi
);
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.
1104 fl
.fld_src
= DN_FIB_RES_PREFSRC(res
);
1108 dev_out
= DN_FIB_RES_DEV(res
);
1110 fl
.oif
= dev_out
->ifindex
;
1111 gateway
= DN_FIB_RES_GW(res
);
1114 if (dev_out
->flags
& IFF_LOOPBACK
)
1115 flags
|= RTCF_LOCAL
;
1117 rt
= dst_alloc(&dn_dst_ops
);
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
;
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
;
1140 rt
->dst
.neighbour
= neigh
;
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
);
1154 hash
= dn_hash(rt
->fl
.fld_src
, rt
->fl
.fld_dst
);
1155 dn_insert_route(rt
, hash
, (struct dn_route
**)pprt
);
1159 neigh_release(neigh
);
1161 dn_fib_res_put(&res
);
1168 err
= -EADDRNOTAVAIL
;
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
)) {
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();
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
)
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);
1222 int dn_route_output_sock(struct dst_entry
**pprt
, struct flowi
*fl
, struct sock
*sk
, int flags
)
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
);
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
;
1245 __le16 local_src
= 0;
1246 struct flowi fl
= { .fld_dst
= cb
->dst
,
1248 .fld_scope
= RT_SCOPE_UNIVERSE
,
1250 .iif
= skb
->dev
->ifindex
};
1251 struct dn_fib_res res
= { .fi
= NULL
, .type
= RTN_UNREACHABLE
};
1257 if ((dn_db
= rcu_dereference(in_dev
->dn_ptr
)) == NULL
)
1260 /* Zero source addresses are not allowed */
1261 if (fl
.fld_src
== 0)
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.
1272 if (dn_dev_islocal(in_dev
, cb
->src
))
1275 err
= dn_fib_lookup(&fl
, &res
);
1280 * Is the destination us ?
1282 if (!dn_dev_islocal(in_dev
, cb
->dst
))
1285 res
.type
= RTN_LOCAL
;
1287 __le16 src_map
= fl
.fld_src
;
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");
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
);
1307 if (dn_fib_lookup(&fl
, &res
))
1310 if (res
.type
!= RTN_UNICAST
)
1313 gateway
= fl
.fld_dst
;
1315 fl
.fld_src
= src_map
;
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
1326 if (dn_db
->parms
.forwarding
== 0)
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
);
1344 case RTN_UNREACHABLE
:
1347 flags
|= RTCF_LOCAL
;
1348 fl
.fld_src
= cb
->dst
;
1349 fl
.fld_dst
= cb
->src
;
1351 /* Routing tables gave us a gateway */
1355 /* Packet was intra-ethernet, so we know its on-link */
1356 if (cb
->rt_flags
& DN_RT_F_IE
) {
1358 flags
|= RTCF_DIRECTSRC
;
1362 /* Use the default router if there is one */
1363 neigh
= neigh_clone(dn_db
->router
);
1365 gateway
= ((struct dn_neigh
*)neigh
)->addr
;
1369 /* Close eyes and pray */
1371 flags
|= RTCF_DIRECTSRC
;
1378 rt
= dst_alloc(&dn_dst_ops
);
1382 rt
->rt_saddr
= fl
.fld_src
;
1383 rt
->rt_daddr
= fl
.fld_dst
;
1384 rt
->rt_gateway
= fl
.fld_dst
;
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
;
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
;
1405 rt
->dst
.input
= dn_forward
;
1408 rt
->dst
.output
= dn_output
;
1409 rt
->dst
.input
= dn_nsp_rx
;
1410 rt
->dst
.dev
= in_dev
;
1411 flags
|= RTCF_LOCAL
;
1414 case RTN_UNREACHABLE
:
1416 rt
->dst
.input
= dst_discard
;
1418 rt
->rt_flags
= flags
;
1420 dev_hold(rt
->dst
.dev
);
1422 err
= dn_rt_set_next_hop(rt
, &res
);
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
);
1432 neigh_release(neigh
);
1434 dn_fib_res_put(&res
);
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
);
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
);
1473 skb_dst_set(skb
, (struct dst_entry
*)rt
);
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
);
1487 struct nlmsghdr
*nlh
;
1488 unsigned char *b
= skb_tail_pointer(skb
);
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;
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
);
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
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
,
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
;
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
;
1549 struct sk_buff
*skb
;
1552 if (!net_eq(net
, &init_net
))
1555 memset(&fl
, 0, sizeof(fl
));
1556 fl
.proto
= DNPROTO_NSP
;
1558 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1561 skb_reset_mac_header(skb
);
1562 cb
= DN_SKB_CB(skb
);
1565 memcpy(&fl
.fld_src
, RTA_DATA(rta
[RTA_SRC
-1]), 2);
1567 memcpy(&fl
.fld_dst
, RTA_DATA(rta
[RTA_DST
-1]), 2);
1569 memcpy(&fl
.iif
, RTA_DATA(rta
[RTA_IIF
-1]), sizeof(int));
1572 struct net_device
*dev
;
1573 if ((dev
= dev_get_by_index(&init_net
, fl
.iif
)) == NULL
) {
1582 skb
->protocol
= htons(ETH_P_DNA_RT
);
1584 cb
->src
= fl
.fld_src
;
1585 cb
->dst
= fl
.fld_dst
;
1587 err
= dn_route_input(skb
);
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
;
1595 if (rta
[RTA_OIF
- 1])
1596 memcpy(&oif
, RTA_DATA(rta
[RTA_OIF
- 1]), sizeof(int));
1598 err
= dn_route_output_key((struct dst_entry
**)&rt
, &fl
, 0);
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);
1619 return rtnl_unicast(skb
, &init_net
, NETLINK_CB(in_skb
).pid
);
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
;
1637 if (!net_eq(net
, &init_net
))
1640 if (NLMSG_PAYLOAD(cb
->nlh
, 0) < sizeof(struct rtmsg
))
1642 if (!(((struct rtmsg
*)NLMSG_DATA(cb
->nlh
))->rtm_flags
&RTM_F_CLONED
))
1646 s_idx
= idx
= cb
->args
[1];
1647 for(h
= 0; h
<= dn_rt_hash_mask
; h
++) {
1653 for(rt
= rcu_dereference_bh(dn_rt_hash_table
[h
].chain
), idx
= 0;
1655 rt
= rcu_dereference_bh(rt
->dst
.dn_next
), idx
++) {
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) {
1663 rcu_read_unlock_bh();
1668 rcu_read_unlock_bh();
1677 #ifdef CONFIG_PROC_FS
1678 struct dn_rt_cache_iter_state
{
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
) {
1689 rt
= rcu_dereference_bh(dn_rt_hash_table
[s
->bucket
].chain
);
1692 rcu_read_unlock_bh();
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
);
1703 rcu_read_unlock_bh();
1704 if (--s
->bucket
< 0)
1707 rt
= rcu_dereference_bh(dn_rt_hash_table
[s
->bucket
].chain
);
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
);
1717 while(*pos
&& (rt
= dn_rt_cache_get_next(seq
, rt
)))
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
);
1730 static void dn_rt_cache_seq_stop(struct seq_file
*seq
, void *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
),
1747 (int) dst_metric(&rt
->dst
, RTAX_RTT
));
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
,
1768 .llseek
= seq_lseek
,
1769 .release
= seq_release_private
,
1772 #endif /* CONFIG_PROC_FS */
1774 void __init
dn_route_init(void)
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
++)
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))
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))
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");
1812 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1814 (long)(dn_rt_hash_mask
*sizeof(struct dn_rt_hash_bucket
))/1024);
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
);
1829 rtnl_register(PF_DECnet
, RTM_GETROUTE
, dn_cache_getroute
,
1834 void __exit
dn_route_cleanup(void)
1836 del_timer(&dn_route_timer
);
1839 proc_net_remove(&init_net
, "decnet_cache");
1840 dst_entries_destroy(&dn_dst_ops
);