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 struct dst_entry
*dn_dst_negative_advice(struct dst_entry
*);
115 static void dn_dst_link_failure(struct sk_buff
*);
116 static void dn_dst_update_pmtu(struct dst_entry
*dst
, u32 mtu
);
117 static int dn_route_input(struct sk_buff
*);
118 static void dn_run_flush(unsigned long dummy
);
120 static struct dn_rt_hash_bucket
*dn_rt_hash_table
;
121 static unsigned dn_rt_hash_mask
;
123 static struct timer_list dn_route_timer
;
124 static DEFINE_TIMER(dn_rt_flush_timer
, dn_run_flush
, 0, 0);
125 int decnet_dst_gc_interval
= 2;
127 static struct dst_ops dn_dst_ops
= {
129 .protocol
= cpu_to_be16(ETH_P_DNA_RT
),
132 .check
= dn_dst_check
,
133 .default_advmss
= dn_dst_default_advmss
,
134 .negative_advice
= dn_dst_negative_advice
,
135 .link_failure
= dn_dst_link_failure
,
136 .update_pmtu
= dn_dst_update_pmtu
,
139 static __inline__
unsigned dn_hash(__le16 src
, __le16 dst
)
141 __u16 tmp
= (__u16 __force
)(src
^ dst
);
145 return dn_rt_hash_mask
& (unsigned)tmp
;
148 static inline void dnrt_free(struct dn_route
*rt
)
150 call_rcu_bh(&rt
->dst
.rcu_head
, dst_rcu_free
);
153 static inline void dnrt_drop(struct dn_route
*rt
)
155 dst_release(&rt
->dst
);
156 call_rcu_bh(&rt
->dst
.rcu_head
, dst_rcu_free
);
159 static void dn_dst_check_expire(unsigned long dummy
)
163 struct dn_route __rcu
**rtp
;
164 unsigned long now
= jiffies
;
165 unsigned long expire
= 120 * HZ
;
167 for (i
= 0; i
<= dn_rt_hash_mask
; i
++) {
168 rtp
= &dn_rt_hash_table
[i
].chain
;
170 spin_lock(&dn_rt_hash_table
[i
].lock
);
171 while ((rt
= rcu_dereference_protected(*rtp
,
172 lockdep_is_held(&dn_rt_hash_table
[i
].lock
))) != NULL
) {
173 if (atomic_read(&rt
->dst
.__refcnt
) ||
174 (now
- rt
->dst
.lastuse
) < expire
) {
175 rtp
= &rt
->dst
.dn_next
;
178 *rtp
= rt
->dst
.dn_next
;
179 rt
->dst
.dn_next
= NULL
;
182 spin_unlock(&dn_rt_hash_table
[i
].lock
);
184 if ((jiffies
- now
) > 0)
188 mod_timer(&dn_route_timer
, now
+ decnet_dst_gc_interval
* HZ
);
191 static int dn_dst_gc(struct dst_ops
*ops
)
194 struct dn_route __rcu
**rtp
;
196 unsigned long now
= jiffies
;
197 unsigned long expire
= 10 * HZ
;
199 for (i
= 0; i
<= dn_rt_hash_mask
; i
++) {
201 spin_lock_bh(&dn_rt_hash_table
[i
].lock
);
202 rtp
= &dn_rt_hash_table
[i
].chain
;
204 while ((rt
= rcu_dereference_protected(*rtp
,
205 lockdep_is_held(&dn_rt_hash_table
[i
].lock
))) != NULL
) {
206 if (atomic_read(&rt
->dst
.__refcnt
) ||
207 (now
- rt
->dst
.lastuse
) < expire
) {
208 rtp
= &rt
->dst
.dn_next
;
211 *rtp
= rt
->dst
.dn_next
;
212 rt
->dst
.dn_next
= NULL
;
216 spin_unlock_bh(&dn_rt_hash_table
[i
].lock
);
223 * The decnet standards don't impose a particular minimum mtu, what they
224 * do insist on is that the routing layer accepts a datagram of at least
225 * 230 bytes long. Here we have to subtract the routing header length from
226 * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
227 * assume the worst and use a long header size.
229 * We update both the mtu and the advertised mss (i.e. the segment size we
230 * advertise to the other end).
232 static void dn_dst_update_pmtu(struct dst_entry
*dst
, u32 mtu
)
235 struct dn_dev
*dn
= dst
->neighbour
?
236 rcu_dereference_raw(dst
->neighbour
->dev
->dn_ptr
) : NULL
;
238 if (dn
&& dn
->use_long
== 0)
243 if (dst_metric(dst
, RTAX_MTU
) > mtu
&& mtu
>= min_mtu
) {
244 if (!(dst_metric_locked(dst
, RTAX_MTU
))) {
245 dst_metric_set(dst
, RTAX_MTU
, mtu
);
246 dst_set_expires(dst
, dn_rt_mtu_expires
);
248 if (!(dst_metric_locked(dst
, RTAX_ADVMSS
))) {
249 u32 mss
= mtu
- DN_MAX_NSP_DATA_HEADER
;
250 u32 existing_mss
= dst_metric_raw(dst
, RTAX_ADVMSS
);
251 if (!existing_mss
|| existing_mss
> mss
)
252 dst_metric_set(dst
, RTAX_ADVMSS
, mss
);
258 * When a route has been marked obsolete. (e.g. routing cache flush)
260 static struct dst_entry
*dn_dst_check(struct dst_entry
*dst
, __u32 cookie
)
265 static struct dst_entry
*dn_dst_negative_advice(struct dst_entry
*dst
)
271 static void dn_dst_link_failure(struct sk_buff
*skb
)
275 static inline int compare_keys(struct flowi
*fl1
, struct flowi
*fl2
)
277 return ((fl1
->fld_dst
^ fl2
->fld_dst
) |
278 (fl1
->fld_src
^ fl2
->fld_src
) |
279 (fl1
->mark
^ fl2
->mark
) |
280 (fl1
->fld_scope
^ fl2
->fld_scope
) |
281 (fl1
->oif
^ fl2
->oif
) |
282 (fl1
->iif
^ fl2
->iif
)) == 0;
285 static int dn_insert_route(struct dn_route
*rt
, unsigned hash
, struct dn_route
**rp
)
287 struct dn_route
*rth
;
288 struct dn_route __rcu
**rthp
;
289 unsigned long now
= jiffies
;
291 rthp
= &dn_rt_hash_table
[hash
].chain
;
293 spin_lock_bh(&dn_rt_hash_table
[hash
].lock
);
294 while ((rth
= rcu_dereference_protected(*rthp
,
295 lockdep_is_held(&dn_rt_hash_table
[hash
].lock
))) != NULL
) {
296 if (compare_keys(&rth
->fl
, &rt
->fl
)) {
298 *rthp
= rth
->dst
.dn_next
;
299 rcu_assign_pointer(rth
->dst
.dn_next
,
300 dn_rt_hash_table
[hash
].chain
);
301 rcu_assign_pointer(dn_rt_hash_table
[hash
].chain
, rth
);
303 dst_use(&rth
->dst
, now
);
304 spin_unlock_bh(&dn_rt_hash_table
[hash
].lock
);
310 rthp
= &rth
->dst
.dn_next
;
313 rcu_assign_pointer(rt
->dst
.dn_next
, dn_rt_hash_table
[hash
].chain
);
314 rcu_assign_pointer(dn_rt_hash_table
[hash
].chain
, rt
);
316 dst_use(&rt
->dst
, now
);
317 spin_unlock_bh(&dn_rt_hash_table
[hash
].lock
);
322 static void dn_run_flush(unsigned long dummy
)
325 struct dn_route
*rt
, *next
;
327 for (i
= 0; i
< dn_rt_hash_mask
; i
++) {
328 spin_lock_bh(&dn_rt_hash_table
[i
].lock
);
330 if ((rt
= xchg((struct dn_route
**)&dn_rt_hash_table
[i
].chain
, NULL
)) == NULL
)
331 goto nothing_to_declare
;
333 for(; rt
; rt
= next
) {
334 next
= rcu_dereference_raw(rt
->dst
.dn_next
);
335 RCU_INIT_POINTER(rt
->dst
.dn_next
, NULL
);
336 dst_free((struct dst_entry
*)rt
);
340 spin_unlock_bh(&dn_rt_hash_table
[i
].lock
);
344 static DEFINE_SPINLOCK(dn_rt_flush_lock
);
346 void dn_rt_cache_flush(int delay
)
348 unsigned long now
= jiffies
;
349 int user_mode
= !in_interrupt();
352 delay
= dn_rt_min_delay
;
354 spin_lock_bh(&dn_rt_flush_lock
);
356 if (del_timer(&dn_rt_flush_timer
) && delay
> 0 && dn_rt_deadline
) {
357 long tmo
= (long)(dn_rt_deadline
- now
);
359 if (user_mode
&& tmo
< dn_rt_max_delay
- dn_rt_min_delay
)
367 spin_unlock_bh(&dn_rt_flush_lock
);
372 if (dn_rt_deadline
== 0)
373 dn_rt_deadline
= now
+ dn_rt_max_delay
;
375 dn_rt_flush_timer
.expires
= now
+ delay
;
376 add_timer(&dn_rt_flush_timer
);
377 spin_unlock_bh(&dn_rt_flush_lock
);
381 * dn_return_short - Return a short packet to its sender
382 * @skb: The packet to return
385 static int dn_return_short(struct sk_buff
*skb
)
387 struct dn_skb_cb
*cb
;
392 /* Add back headers */
393 skb_push(skb
, skb
->data
- skb_network_header(skb
));
395 if ((skb
= skb_unshare(skb
, GFP_ATOMIC
)) == NULL
)
399 /* Skip packet length and point to flags */
401 *ptr
++ = (cb
->rt_flags
& ~DN_RT_F_RQR
) | DN_RT_F_RTS
;
407 *ptr
= 0; /* Zero hop count */
411 skb
->pkt_type
= PACKET_OUTGOING
;
412 dn_rt_finish_output(skb
, NULL
, NULL
);
413 return NET_RX_SUCCESS
;
417 * dn_return_long - Return a long packet to its sender
418 * @skb: The long format packet to return
421 static int dn_return_long(struct sk_buff
*skb
)
423 struct dn_skb_cb
*cb
;
425 unsigned char *src_addr
, *dst_addr
;
426 unsigned char tmp
[ETH_ALEN
];
428 /* Add back all headers */
429 skb_push(skb
, skb
->data
- skb_network_header(skb
));
431 if ((skb
= skb_unshare(skb
, GFP_ATOMIC
)) == NULL
)
435 /* Ignore packet length and point to flags */
439 if (*ptr
& DN_RT_F_PF
) {
440 char padlen
= (*ptr
& ~DN_RT_F_PF
);
444 *ptr
++ = (cb
->rt_flags
& ~DN_RT_F_RQR
) | DN_RT_F_RTS
;
450 *ptr
= 0; /* Zero hop count */
452 /* Swap source and destination */
453 memcpy(tmp
, src_addr
, ETH_ALEN
);
454 memcpy(src_addr
, dst_addr
, ETH_ALEN
);
455 memcpy(dst_addr
, tmp
, ETH_ALEN
);
457 skb
->pkt_type
= PACKET_OUTGOING
;
458 dn_rt_finish_output(skb
, dst_addr
, src_addr
);
459 return NET_RX_SUCCESS
;
463 * dn_route_rx_packet - Try and find a route for an incoming packet
464 * @skb: The packet to find a route for
466 * Returns: result of input function if route is found, error code otherwise
468 static int dn_route_rx_packet(struct sk_buff
*skb
)
470 struct dn_skb_cb
*cb
;
473 if ((err
= dn_route_input(skb
)) == 0)
474 return dst_input(skb
);
477 if (decnet_debug_level
& 4) {
478 char *devname
= skb
->dev
? skb
->dev
->name
: "???";
481 "DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%d\n",
482 (int)cb
->rt_flags
, devname
, skb
->len
,
483 le16_to_cpu(cb
->src
), le16_to_cpu(cb
->dst
),
487 if ((skb
->pkt_type
== PACKET_HOST
) && (cb
->rt_flags
& DN_RT_F_RQR
)) {
488 switch(cb
->rt_flags
& DN_RT_PKT_MSK
) {
489 case DN_RT_PKT_SHORT
:
490 return dn_return_short(skb
);
492 return dn_return_long(skb
);
500 static int dn_route_rx_long(struct sk_buff
*skb
)
502 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
503 unsigned char *ptr
= skb
->data
;
505 if (!pskb_may_pull(skb
, 21)) /* 20 for long header, 1 for shortest nsp */
509 skb_reset_transport_header(skb
);
511 /* Destination info */
513 cb
->dst
= dn_eth2dn(ptr
);
514 if (memcmp(ptr
, dn_hiord_addr
, 4) != 0)
521 cb
->src
= dn_eth2dn(ptr
);
522 if (memcmp(ptr
, dn_hiord_addr
, 4) != 0)
527 cb
->hops
= *ptr
++; /* Visit Count */
529 return NF_HOOK(NFPROTO_DECNET
, NF_DN_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
539 static int dn_route_rx_short(struct sk_buff
*skb
)
541 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
542 unsigned char *ptr
= skb
->data
;
544 if (!pskb_may_pull(skb
, 6)) /* 5 for short header + 1 for shortest nsp */
548 skb_reset_transport_header(skb
);
550 cb
->dst
= *(__le16
*)ptr
;
552 cb
->src
= *(__le16
*)ptr
;
554 cb
->hops
= *ptr
& 0x3f;
556 return NF_HOOK(NFPROTO_DECNET
, NF_DN_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
564 static int dn_route_discard(struct sk_buff
*skb
)
567 * I know we drop the packet here, but thats considered success in
571 return NET_RX_SUCCESS
;
574 static int dn_route_ptp_hello(struct sk_buff
*skb
)
577 dn_neigh_pointopoint_hello(skb
);
578 return NET_RX_SUCCESS
;
581 int dn_route_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
, struct net_device
*orig_dev
)
583 struct dn_skb_cb
*cb
;
584 unsigned char flags
= 0;
585 __u16 len
= le16_to_cpu(*(__le16
*)skb
->data
);
586 struct dn_dev
*dn
= rcu_dereference(dev
->dn_ptr
);
587 unsigned char padlen
= 0;
589 if (!net_eq(dev_net(dev
), &init_net
))
595 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
598 if (!pskb_may_pull(skb
, 3))
612 cb
->iif
= dev
->ifindex
;
615 * If we have padding, remove it.
617 if (flags
& DN_RT_F_PF
) {
618 padlen
= flags
& ~DN_RT_F_PF
;
619 if (!pskb_may_pull(skb
, padlen
+ 1))
621 skb_pull(skb
, padlen
);
625 skb_reset_network_header(skb
);
628 * Weed out future version DECnet
630 if (flags
& DN_RT_F_VER
)
633 cb
->rt_flags
= flags
;
635 if (decnet_debug_level
& 1)
637 "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
638 (int)flags
, (dev
) ? dev
->name
: "???", len
, skb
->len
,
641 if (flags
& DN_RT_PKT_CNTL
) {
642 if (unlikely(skb_linearize(skb
)))
645 switch(flags
& DN_RT_CNTL_MSK
) {
647 dn_dev_init_pkt(skb
);
650 dn_dev_veri_pkt(skb
);
654 if (dn
->parms
.state
!= DN_DEV_S_RU
)
657 switch(flags
& DN_RT_CNTL_MSK
) {
659 return NF_HOOK(NFPROTO_DECNET
, NF_DN_HELLO
,
665 return NF_HOOK(NFPROTO_DECNET
, NF_DN_ROUTE
,
669 return NF_HOOK(NFPROTO_DECNET
, NF_DN_HELLO
,
671 dn_neigh_router_hello
);
674 return NF_HOOK(NFPROTO_DECNET
, NF_DN_HELLO
,
676 dn_neigh_endnode_hello
);
679 if (dn
->parms
.state
!= DN_DEV_S_RU
)
682 skb_pull(skb
, 1); /* Pull flags */
684 switch(flags
& DN_RT_PKT_MSK
) {
686 return dn_route_rx_long(skb
);
687 case DN_RT_PKT_SHORT
:
688 return dn_route_rx_short(skb
);
698 static int dn_output(struct sk_buff
*skb
)
700 struct dst_entry
*dst
= skb_dst(skb
);
701 struct dn_route
*rt
= (struct dn_route
*)dst
;
702 struct net_device
*dev
= dst
->dev
;
703 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
704 struct neighbour
*neigh
;
708 if ((neigh
= dst
->neighbour
) == NULL
)
713 cb
->src
= rt
->rt_saddr
;
714 cb
->dst
= rt
->rt_daddr
;
717 * Always set the Intra-Ethernet bit on all outgoing packets
718 * originated on this node. Only valid flag from upper layers
719 * is return-to-sender-requested. Set hop count to 0 too.
721 cb
->rt_flags
&= ~DN_RT_F_RQR
;
722 cb
->rt_flags
|= DN_RT_F_IE
;
725 return NF_HOOK(NFPROTO_DECNET
, NF_DN_LOCAL_OUT
, skb
, NULL
, dev
,
730 printk(KERN_DEBUG
"dn_output: This should not happen\n");
737 static int dn_forward(struct sk_buff
*skb
)
739 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
740 struct dst_entry
*dst
= skb_dst(skb
);
741 struct dn_dev
*dn_db
= rcu_dereference(dst
->dev
->dn_ptr
);
743 struct neighbour
*neigh
= dst
->neighbour
;
745 #ifdef CONFIG_NETFILTER
746 struct net_device
*dev
= skb
->dev
;
749 if (skb
->pkt_type
!= PACKET_HOST
)
752 /* Ensure that we have enough space for headers */
753 rt
= (struct dn_route
*)skb_dst(skb
);
754 header_len
= dn_db
->use_long
? 21 : 6;
755 if (skb_cow(skb
, LL_RESERVED_SPACE(rt
->dst
.dev
)+header_len
))
759 * Hop count exceeded.
764 skb
->dev
= rt
->dst
.dev
;
767 * If packet goes out same interface it came in on, then set
768 * the Intra-Ethernet bit. This has no effect for short
769 * packets, so we don't need to test for them here.
771 cb
->rt_flags
&= ~DN_RT_F_IE
;
772 if (rt
->rt_flags
& RTCF_DOREDIRECT
)
773 cb
->rt_flags
|= DN_RT_F_IE
;
775 return NF_HOOK(NFPROTO_DECNET
, NF_DN_FORWARD
, skb
, dev
, skb
->dev
,
784 * Used to catch bugs. This should never normally get
787 static int dn_rt_bug(struct sk_buff
*skb
)
789 if (net_ratelimit()) {
790 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
792 printk(KERN_DEBUG
"dn_rt_bug: skb from:%04x to:%04x\n",
793 le16_to_cpu(cb
->src
), le16_to_cpu(cb
->dst
));
801 static unsigned int dn_dst_default_advmss(const struct dst_entry
*dst
)
803 return dn_mss_from_pmtu(dst
->dev
, dst_mtu(dst
));
806 static int dn_rt_set_next_hop(struct dn_route
*rt
, struct dn_fib_res
*res
)
808 struct dn_fib_info
*fi
= res
->fi
;
809 struct net_device
*dev
= rt
->dst
.dev
;
814 if (DN_FIB_RES_GW(*res
) &&
815 DN_FIB_RES_NH(*res
).nh_scope
== RT_SCOPE_LINK
)
816 rt
->rt_gateway
= DN_FIB_RES_GW(*res
);
817 dst_import_metrics(&rt
->dst
, fi
->fib_metrics
);
819 rt
->rt_type
= res
->type
;
821 if (dev
!= NULL
&& rt
->dst
.neighbour
== NULL
) {
822 n
= __neigh_lookup_errno(&dn_neigh_table
, &rt
->rt_gateway
, dev
);
825 rt
->dst
.neighbour
= n
;
828 if (dst_metric(&rt
->dst
, RTAX_MTU
) == 0 ||
829 dst_metric(&rt
->dst
, RTAX_MTU
) > rt
->dst
.dev
->mtu
)
830 dst_metric_set(&rt
->dst
, RTAX_MTU
, rt
->dst
.dev
->mtu
);
831 metric
= dst_metric_raw(&rt
->dst
, RTAX_ADVMSS
);
833 unsigned int mss
= dn_mss_from_pmtu(dev
, dst_mtu(&rt
->dst
));
835 dst_metric_set(&rt
->dst
, RTAX_ADVMSS
, mss
);
840 static inline int dn_match_addr(__le16 addr1
, __le16 addr2
)
842 __u16 tmp
= le16_to_cpu(addr1
) ^ le16_to_cpu(addr2
);
851 static __le16
dnet_select_source(const struct net_device
*dev
, __le16 daddr
, int scope
)
854 struct dn_dev
*dn_db
;
855 struct dn_ifaddr
*ifa
;
860 dn_db
= rcu_dereference(dev
->dn_ptr
);
861 for (ifa
= rcu_dereference(dn_db
->ifa_list
);
863 ifa
= rcu_dereference(ifa
->ifa_next
)) {
864 if (ifa
->ifa_scope
> scope
)
867 saddr
= ifa
->ifa_local
;
870 ret
= dn_match_addr(daddr
, ifa
->ifa_local
);
871 if (ret
> best_match
)
872 saddr
= ifa
->ifa_local
;
874 saddr
= ifa
->ifa_local
;
881 static inline __le16
__dn_fib_res_prefsrc(struct dn_fib_res
*res
)
883 return dnet_select_source(DN_FIB_RES_DEV(*res
), DN_FIB_RES_GW(*res
), res
->scope
);
886 static inline __le16
dn_fib_rules_map_destination(__le16 daddr
, struct dn_fib_res
*res
)
888 __le16 mask
= dnet_make_mask(res
->prefixlen
);
889 return (daddr
&~mask
)|res
->fi
->fib_nh
->nh_gw
;
892 static int dn_route_output_slow(struct dst_entry
**pprt
, const struct flowi
*oldflp
, int try_hard
)
894 struct flowi fl
= { .fld_dst
= oldflp
->fld_dst
,
895 .fld_src
= oldflp
->fld_src
,
896 .fld_scope
= RT_SCOPE_UNIVERSE
,
897 .mark
= oldflp
->mark
,
898 .iif
= init_net
.loopback_dev
->ifindex
,
899 .oif
= oldflp
->oif
};
900 struct dn_route
*rt
= NULL
;
901 struct net_device
*dev_out
= NULL
, *dev
;
902 struct neighbour
*neigh
= NULL
;
905 struct dn_fib_res res
= { .fi
= NULL
, .type
= RTN_UNICAST
};
910 if (decnet_debug_level
& 16)
912 "dn_route_output_slow: dst=%04x src=%04x mark=%d"
913 " iif=%d oif=%d\n", le16_to_cpu(oldflp
->fld_dst
),
914 le16_to_cpu(oldflp
->fld_src
),
915 oldflp
->mark
, init_net
.loopback_dev
->ifindex
, oldflp
->oif
);
917 /* If we have an output interface, verify its a DECnet device */
919 dev_out
= dev_get_by_index(&init_net
, oldflp
->oif
);
921 if (dev_out
&& dev_out
->dn_ptr
== NULL
) {
929 /* If we have a source address, verify that its a local address */
930 if (oldflp
->fld_src
) {
931 err
= -EADDRNOTAVAIL
;
934 if (dn_dev_islocal(dev_out
, oldflp
->fld_src
))
940 for_each_netdev_rcu(&init_net
, dev
) {
943 if (!dn_dev_islocal(dev
, oldflp
->fld_src
))
945 if ((dev
->flags
& IFF_LOOPBACK
) &&
947 !dn_dev_islocal(dev
, oldflp
->fld_dst
))
961 /* No destination? Assume its local */
963 fl
.fld_dst
= fl
.fld_src
;
965 err
= -EADDRNOTAVAIL
;
968 dev_out
= init_net
.loopback_dev
;
972 fl
.fld_src
= dnet_select_source(dev_out
, 0,
977 fl
.oif
= init_net
.loopback_dev
->ifindex
;
978 res
.type
= RTN_LOCAL
;
982 if (decnet_debug_level
& 16)
984 "dn_route_output_slow: initial checks complete."
985 " dst=%o4x src=%04x oif=%d try_hard=%d\n",
986 le16_to_cpu(fl
.fld_dst
), le16_to_cpu(fl
.fld_src
),
990 * N.B. If the kernel is compiled without router support then
991 * dn_fib_lookup() will evaluate to non-zero so this if () block
992 * will always be executed.
995 if (try_hard
|| (err
= dn_fib_lookup(&fl
, &res
)) != 0) {
996 struct dn_dev
*dn_db
;
1000 * Here the fallback is basically the standard algorithm for
1001 * routing in endnodes which is described in the DECnet routing
1004 * If we are not trying hard, look in neighbour cache.
1005 * The result is tested to ensure that if a specific output
1006 * device/source address was requested, then we honour that
1010 neigh
= neigh_lookup_nodev(&dn_neigh_table
, &init_net
, &fl
.fld_dst
);
1013 (neigh
->dev
->ifindex
!= oldflp
->oif
)) ||
1015 (!dn_dev_islocal(neigh
->dev
,
1016 oldflp
->fld_src
)))) {
1017 neigh_release(neigh
);
1022 if (dn_dev_islocal(neigh
->dev
, fl
.fld_dst
)) {
1023 dev_out
= init_net
.loopback_dev
;
1024 res
.type
= RTN_LOCAL
;
1026 dev_out
= neigh
->dev
;
1034 /* Not there? Perhaps its a local address */
1035 if (dev_out
== NULL
)
1036 dev_out
= dn_dev_get_default();
1038 if (dev_out
== NULL
)
1040 dn_db
= rcu_dereference_raw(dev_out
->dn_ptr
);
1041 /* Possible improvement - check all devices for local addr */
1042 if (dn_dev_islocal(dev_out
, fl
.fld_dst
)) {
1044 dev_out
= init_net
.loopback_dev
;
1046 res
.type
= RTN_LOCAL
;
1049 /* Not local either.... try sending it to the default router */
1050 neigh
= neigh_clone(dn_db
->router
);
1051 BUG_ON(neigh
&& neigh
->dev
!= dev_out
);
1053 /* Ok then, we assume its directly connected and move on */
1056 gateway
= ((struct dn_neigh
*)neigh
)->addr
;
1058 gateway
= fl
.fld_dst
;
1059 if (fl
.fld_src
== 0) {
1060 fl
.fld_src
= dnet_select_source(dev_out
, gateway
,
1061 res
.type
== RTN_LOCAL
?
1064 if (fl
.fld_src
== 0 && res
.type
!= RTN_LOCAL
)
1067 fl
.oif
= dev_out
->ifindex
;
1072 if (res
.type
== RTN_NAT
)
1075 if (res
.type
== RTN_LOCAL
) {
1077 fl
.fld_src
= fl
.fld_dst
;
1080 dev_out
= init_net
.loopback_dev
;
1082 fl
.oif
= dev_out
->ifindex
;
1084 dn_fib_info_put(res
.fi
);
1089 if (res
.fi
->fib_nhs
> 1 && fl
.oif
== 0)
1090 dn_fib_select_multipath(&fl
, &res
);
1093 * We could add some logic to deal with default routes here and
1094 * get rid of some of the special casing above.
1098 fl
.fld_src
= DN_FIB_RES_PREFSRC(res
);
1102 dev_out
= DN_FIB_RES_DEV(res
);
1104 fl
.oif
= dev_out
->ifindex
;
1105 gateway
= DN_FIB_RES_GW(res
);
1108 if (dev_out
->flags
& IFF_LOOPBACK
)
1109 flags
|= RTCF_LOCAL
;
1111 rt
= dst_alloc(&dn_dst_ops
);
1115 atomic_set(&rt
->dst
.__refcnt
, 1);
1116 rt
->dst
.flags
= DST_HOST
;
1118 rt
->fl
.fld_src
= oldflp
->fld_src
;
1119 rt
->fl
.fld_dst
= oldflp
->fld_dst
;
1120 rt
->fl
.oif
= oldflp
->oif
;
1122 rt
->fl
.mark
= oldflp
->mark
;
1124 rt
->rt_saddr
= fl
.fld_src
;
1125 rt
->rt_daddr
= fl
.fld_dst
;
1126 rt
->rt_gateway
= gateway
? gateway
: fl
.fld_dst
;
1127 rt
->rt_local_src
= fl
.fld_src
;
1129 rt
->rt_dst_map
= fl
.fld_dst
;
1130 rt
->rt_src_map
= fl
.fld_src
;
1132 rt
->dst
.dev
= dev_out
;
1134 rt
->dst
.neighbour
= neigh
;
1137 rt
->dst
.lastuse
= jiffies
;
1138 rt
->dst
.output
= dn_output
;
1139 rt
->dst
.input
= dn_rt_bug
;
1140 rt
->rt_flags
= flags
;
1141 if (flags
& RTCF_LOCAL
)
1142 rt
->dst
.input
= dn_nsp_rx
;
1144 err
= dn_rt_set_next_hop(rt
, &res
);
1148 hash
= dn_hash(rt
->fl
.fld_src
, rt
->fl
.fld_dst
);
1149 dn_insert_route(rt
, hash
, (struct dn_route
**)pprt
);
1153 neigh_release(neigh
);
1155 dn_fib_res_put(&res
);
1162 err
= -EADDRNOTAVAIL
;
1177 * N.B. The flags may be moved into the flowi at some future stage.
1179 static int __dn_route_output_key(struct dst_entry
**pprt
, const struct flowi
*flp
, int flags
)
1181 unsigned hash
= dn_hash(flp
->fld_src
, flp
->fld_dst
);
1182 struct dn_route
*rt
= NULL
;
1184 if (!(flags
& MSG_TRYHARD
)) {
1186 for (rt
= rcu_dereference_bh(dn_rt_hash_table
[hash
].chain
); rt
;
1187 rt
= rcu_dereference_bh(rt
->dst
.dn_next
)) {
1188 if ((flp
->fld_dst
== rt
->fl
.fld_dst
) &&
1189 (flp
->fld_src
== rt
->fl
.fld_src
) &&
1190 (flp
->mark
== rt
->fl
.mark
) &&
1191 dn_is_output_route(rt
) &&
1192 (rt
->fl
.oif
== flp
->oif
)) {
1193 dst_use(&rt
->dst
, jiffies
);
1194 rcu_read_unlock_bh();
1199 rcu_read_unlock_bh();
1202 return dn_route_output_slow(pprt
, flp
, flags
);
1205 static int dn_route_output_key(struct dst_entry
**pprt
, struct flowi
*flp
, int flags
)
1209 err
= __dn_route_output_key(pprt
, flp
, flags
);
1210 if (err
== 0 && flp
->proto
) {
1211 err
= xfrm_lookup(&init_net
, pprt
, flp
, NULL
, 0);
1216 int dn_route_output_sock(struct dst_entry
**pprt
, struct flowi
*fl
, struct sock
*sk
, int flags
)
1220 err
= __dn_route_output_key(pprt
, fl
, flags
& MSG_TRYHARD
);
1221 if (err
== 0 && fl
->proto
) {
1222 err
= xfrm_lookup(&init_net
, pprt
, fl
, sk
,
1223 (flags
& MSG_DONTWAIT
) ? 0 : XFRM_LOOKUP_WAIT
);
1228 static int dn_route_input_slow(struct sk_buff
*skb
)
1230 struct dn_route
*rt
= NULL
;
1231 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
1232 struct net_device
*in_dev
= skb
->dev
;
1233 struct net_device
*out_dev
= NULL
;
1234 struct dn_dev
*dn_db
;
1235 struct neighbour
*neigh
= NULL
;
1239 __le16 local_src
= 0;
1240 struct flowi fl
= { .fld_dst
= cb
->dst
,
1242 .fld_scope
= RT_SCOPE_UNIVERSE
,
1244 .iif
= skb
->dev
->ifindex
};
1245 struct dn_fib_res res
= { .fi
= NULL
, .type
= RTN_UNREACHABLE
};
1251 if ((dn_db
= rcu_dereference(in_dev
->dn_ptr
)) == NULL
)
1254 /* Zero source addresses are not allowed */
1255 if (fl
.fld_src
== 0)
1259 * In this case we've just received a packet from a source
1260 * outside ourselves pretending to come from us. We don't
1261 * allow it any further to prevent routing loops, spoofing and
1262 * other nasties. Loopback packets already have the dst attached
1263 * so this only affects packets which have originated elsewhere.
1266 if (dn_dev_islocal(in_dev
, cb
->src
))
1269 err
= dn_fib_lookup(&fl
, &res
);
1274 * Is the destination us ?
1276 if (!dn_dev_islocal(in_dev
, cb
->dst
))
1279 res
.type
= RTN_LOCAL
;
1281 __le16 src_map
= fl
.fld_src
;
1284 out_dev
= DN_FIB_RES_DEV(res
);
1285 if (out_dev
== NULL
) {
1286 if (net_ratelimit())
1287 printk(KERN_CRIT
"Bug in dn_route_input_slow() "
1288 "No output device\n");
1294 src_map
= fl
.fld_src
; /* no NAT support for now */
1296 gateway
= DN_FIB_RES_GW(res
);
1297 if (res
.type
== RTN_NAT
) {
1298 fl
.fld_dst
= dn_fib_rules_map_destination(fl
.fld_dst
, &res
);
1299 dn_fib_res_put(&res
);
1301 if (dn_fib_lookup(&fl
, &res
))
1304 if (res
.type
!= RTN_UNICAST
)
1307 gateway
= fl
.fld_dst
;
1309 fl
.fld_src
= src_map
;
1315 * Forwarding check here, we only check for forwarding
1316 * being turned off, if you want to only forward intra
1317 * area, its up to you to set the routing tables up
1320 if (dn_db
->parms
.forwarding
== 0)
1323 if (res
.fi
->fib_nhs
> 1 && fl
.oif
== 0)
1324 dn_fib_select_multipath(&fl
, &res
);
1327 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1328 * flag as a hint to set the intra-ethernet bit when
1329 * forwarding. If we've got NAT in operation, we don't do
1330 * this optimisation.
1332 if (out_dev
== in_dev
&& !(flags
& RTCF_NAT
))
1333 flags
|= RTCF_DOREDIRECT
;
1335 local_src
= DN_FIB_RES_PREFSRC(res
);
1338 case RTN_UNREACHABLE
:
1341 flags
|= RTCF_LOCAL
;
1342 fl
.fld_src
= cb
->dst
;
1343 fl
.fld_dst
= cb
->src
;
1345 /* Routing tables gave us a gateway */
1349 /* Packet was intra-ethernet, so we know its on-link */
1350 if (cb
->rt_flags
& DN_RT_F_IE
) {
1352 flags
|= RTCF_DIRECTSRC
;
1356 /* Use the default router if there is one */
1357 neigh
= neigh_clone(dn_db
->router
);
1359 gateway
= ((struct dn_neigh
*)neigh
)->addr
;
1363 /* Close eyes and pray */
1365 flags
|= RTCF_DIRECTSRC
;
1372 rt
= dst_alloc(&dn_dst_ops
);
1376 rt
->rt_saddr
= fl
.fld_src
;
1377 rt
->rt_daddr
= fl
.fld_dst
;
1378 rt
->rt_gateway
= fl
.fld_dst
;
1380 rt
->rt_gateway
= gateway
;
1381 rt
->rt_local_src
= local_src
? local_src
: rt
->rt_saddr
;
1383 rt
->rt_dst_map
= fl
.fld_dst
;
1384 rt
->rt_src_map
= fl
.fld_src
;
1386 rt
->fl
.fld_src
= cb
->src
;
1387 rt
->fl
.fld_dst
= cb
->dst
;
1389 rt
->fl
.iif
= in_dev
->ifindex
;
1390 rt
->fl
.mark
= fl
.mark
;
1392 rt
->dst
.flags
= DST_HOST
;
1393 rt
->dst
.neighbour
= neigh
;
1394 rt
->dst
.dev
= out_dev
;
1395 rt
->dst
.lastuse
= jiffies
;
1396 rt
->dst
.output
= dn_rt_bug
;
1399 rt
->dst
.input
= dn_forward
;
1402 rt
->dst
.output
= dn_output
;
1403 rt
->dst
.input
= dn_nsp_rx
;
1404 rt
->dst
.dev
= in_dev
;
1405 flags
|= RTCF_LOCAL
;
1408 case RTN_UNREACHABLE
:
1410 rt
->dst
.input
= dst_discard
;
1412 rt
->rt_flags
= flags
;
1414 dev_hold(rt
->dst
.dev
);
1416 err
= dn_rt_set_next_hop(rt
, &res
);
1420 hash
= dn_hash(rt
->fl
.fld_src
, rt
->fl
.fld_dst
);
1421 dn_insert_route(rt
, hash
, &rt
);
1422 skb_dst_set(skb
, &rt
->dst
);
1426 neigh_release(neigh
);
1428 dn_fib_res_put(&res
);
1448 static int dn_route_input(struct sk_buff
*skb
)
1450 struct dn_route
*rt
;
1451 struct dn_skb_cb
*cb
= DN_SKB_CB(skb
);
1452 unsigned hash
= dn_hash(cb
->src
, cb
->dst
);
1458 for(rt
= rcu_dereference(dn_rt_hash_table
[hash
].chain
); rt
!= NULL
;
1459 rt
= rcu_dereference(rt
->dst
.dn_next
)) {
1460 if ((rt
->fl
.fld_src
== cb
->src
) &&
1461 (rt
->fl
.fld_dst
== cb
->dst
) &&
1462 (rt
->fl
.oif
== 0) &&
1463 (rt
->fl
.mark
== skb
->mark
) &&
1464 (rt
->fl
.iif
== cb
->iif
)) {
1465 dst_use(&rt
->dst
, jiffies
);
1467 skb_dst_set(skb
, (struct dst_entry
*)rt
);
1473 return dn_route_input_slow(skb
);
1476 static int dn_rt_fill_info(struct sk_buff
*skb
, u32 pid
, u32 seq
,
1477 int event
, int nowait
, unsigned int flags
)
1479 struct dn_route
*rt
= (struct dn_route
*)skb_dst(skb
);
1481 struct nlmsghdr
*nlh
;
1482 unsigned char *b
= skb_tail_pointer(skb
);
1485 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*r
), flags
);
1486 r
= NLMSG_DATA(nlh
);
1487 r
->rtm_family
= AF_DECnet
;
1488 r
->rtm_dst_len
= 16;
1491 r
->rtm_table
= RT_TABLE_MAIN
;
1492 RTA_PUT_U32(skb
, RTA_TABLE
, RT_TABLE_MAIN
);
1493 r
->rtm_type
= rt
->rt_type
;
1494 r
->rtm_flags
= (rt
->rt_flags
& ~0xFFFF) | RTM_F_CLONED
;
1495 r
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1496 r
->rtm_protocol
= RTPROT_UNSPEC
;
1497 if (rt
->rt_flags
& RTCF_NOTIFY
)
1498 r
->rtm_flags
|= RTM_F_NOTIFY
;
1499 RTA_PUT(skb
, RTA_DST
, 2, &rt
->rt_daddr
);
1500 if (rt
->fl
.fld_src
) {
1501 r
->rtm_src_len
= 16;
1502 RTA_PUT(skb
, RTA_SRC
, 2, &rt
->fl
.fld_src
);
1505 RTA_PUT(skb
, RTA_OIF
, sizeof(int), &rt
->dst
.dev
->ifindex
);
1507 * Note to self - change this if input routes reverse direction when
1508 * they deal only with inputs and not with replies like they do
1511 RTA_PUT(skb
, RTA_PREFSRC
, 2, &rt
->rt_local_src
);
1512 if (rt
->rt_daddr
!= rt
->rt_gateway
)
1513 RTA_PUT(skb
, RTA_GATEWAY
, 2, &rt
->rt_gateway
);
1514 if (rtnetlink_put_metrics(skb
, dst_metrics_ptr(&rt
->dst
)) < 0)
1515 goto rtattr_failure
;
1516 expires
= rt
->dst
.expires
? rt
->dst
.expires
- jiffies
: 0;
1517 if (rtnl_put_cacheinfo(skb
, &rt
->dst
, 0, 0, 0, expires
,
1519 goto rtattr_failure
;
1520 if (dn_is_input_route(rt
))
1521 RTA_PUT(skb
, RTA_IIF
, sizeof(int), &rt
->fl
.iif
);
1523 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1533 * This is called by both endnodes and routers now.
1535 static int dn_cache_getroute(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
, void *arg
)
1537 struct net
*net
= sock_net(in_skb
->sk
);
1538 struct rtattr
**rta
= arg
;
1539 struct rtmsg
*rtm
= NLMSG_DATA(nlh
);
1540 struct dn_route
*rt
= NULL
;
1541 struct dn_skb_cb
*cb
;
1543 struct sk_buff
*skb
;
1546 if (!net_eq(net
, &init_net
))
1549 memset(&fl
, 0, sizeof(fl
));
1550 fl
.proto
= DNPROTO_NSP
;
1552 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1555 skb_reset_mac_header(skb
);
1556 cb
= DN_SKB_CB(skb
);
1559 memcpy(&fl
.fld_src
, RTA_DATA(rta
[RTA_SRC
-1]), 2);
1561 memcpy(&fl
.fld_dst
, RTA_DATA(rta
[RTA_DST
-1]), 2);
1563 memcpy(&fl
.iif
, RTA_DATA(rta
[RTA_IIF
-1]), sizeof(int));
1566 struct net_device
*dev
;
1567 if ((dev
= dev_get_by_index(&init_net
, fl
.iif
)) == NULL
) {
1576 skb
->protocol
= htons(ETH_P_DNA_RT
);
1578 cb
->src
= fl
.fld_src
;
1579 cb
->dst
= fl
.fld_dst
;
1581 err
= dn_route_input(skb
);
1583 memset(cb
, 0, sizeof(struct dn_skb_cb
));
1584 rt
= (struct dn_route
*)skb_dst(skb
);
1585 if (!err
&& -rt
->dst
.error
)
1586 err
= rt
->dst
.error
;
1589 if (rta
[RTA_OIF
- 1])
1590 memcpy(&oif
, RTA_DATA(rta
[RTA_OIF
- 1]), sizeof(int));
1592 err
= dn_route_output_key((struct dst_entry
**)&rt
, &fl
, 0);
1600 skb_dst_set(skb
, &rt
->dst
);
1601 if (rtm
->rtm_flags
& RTM_F_NOTIFY
)
1602 rt
->rt_flags
|= RTCF_NOTIFY
;
1604 err
= dn_rt_fill_info(skb
, NETLINK_CB(in_skb
).pid
, nlh
->nlmsg_seq
, RTM_NEWROUTE
, 0, 0);
1613 return rtnl_unicast(skb
, &init_net
, NETLINK_CB(in_skb
).pid
);
1621 * For routers, this is called from dn_fib_dump, but for endnodes its
1622 * called directly from the rtnetlink dispatch table.
1624 int dn_cache_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1626 struct net
*net
= sock_net(skb
->sk
);
1627 struct dn_route
*rt
;
1631 if (!net_eq(net
, &init_net
))
1634 if (NLMSG_PAYLOAD(cb
->nlh
, 0) < sizeof(struct rtmsg
))
1636 if (!(((struct rtmsg
*)NLMSG_DATA(cb
->nlh
))->rtm_flags
&RTM_F_CLONED
))
1640 s_idx
= idx
= cb
->args
[1];
1641 for(h
= 0; h
<= dn_rt_hash_mask
; h
++) {
1647 for(rt
= rcu_dereference_bh(dn_rt_hash_table
[h
].chain
), idx
= 0;
1649 rt
= rcu_dereference_bh(rt
->dst
.dn_next
), idx
++) {
1652 skb_dst_set(skb
, dst_clone(&rt
->dst
));
1653 if (dn_rt_fill_info(skb
, NETLINK_CB(cb
->skb
).pid
,
1654 cb
->nlh
->nlmsg_seq
, RTM_NEWROUTE
,
1655 1, NLM_F_MULTI
) <= 0) {
1657 rcu_read_unlock_bh();
1662 rcu_read_unlock_bh();
1671 #ifdef CONFIG_PROC_FS
1672 struct dn_rt_cache_iter_state
{
1676 static struct dn_route
*dn_rt_cache_get_first(struct seq_file
*seq
)
1678 struct dn_route
*rt
= NULL
;
1679 struct dn_rt_cache_iter_state
*s
= seq
->private;
1681 for(s
->bucket
= dn_rt_hash_mask
; s
->bucket
>= 0; --s
->bucket
) {
1683 rt
= rcu_dereference_bh(dn_rt_hash_table
[s
->bucket
].chain
);
1686 rcu_read_unlock_bh();
1691 static struct dn_route
*dn_rt_cache_get_next(struct seq_file
*seq
, struct dn_route
*rt
)
1693 struct dn_rt_cache_iter_state
*s
= seq
->private;
1695 rt
= rcu_dereference_bh(rt
->dst
.dn_next
);
1697 rcu_read_unlock_bh();
1698 if (--s
->bucket
< 0)
1701 rt
= rcu_dereference_bh(dn_rt_hash_table
[s
->bucket
].chain
);
1706 static void *dn_rt_cache_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1708 struct dn_route
*rt
= dn_rt_cache_get_first(seq
);
1711 while(*pos
&& (rt
= dn_rt_cache_get_next(seq
, rt
)))
1714 return *pos
? NULL
: rt
;
1717 static void *dn_rt_cache_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1719 struct dn_route
*rt
= dn_rt_cache_get_next(seq
, v
);
1724 static void dn_rt_cache_seq_stop(struct seq_file
*seq
, void *v
)
1727 rcu_read_unlock_bh();
1730 static int dn_rt_cache_seq_show(struct seq_file
*seq
, void *v
)
1732 struct dn_route
*rt
= v
;
1733 char buf1
[DN_ASCBUF_LEN
], buf2
[DN_ASCBUF_LEN
];
1735 seq_printf(seq
, "%-8s %-7s %-7s %04d %04d %04d\n",
1736 rt
->dst
.dev
? rt
->dst
.dev
->name
: "*",
1737 dn_addr2asc(le16_to_cpu(rt
->rt_daddr
), buf1
),
1738 dn_addr2asc(le16_to_cpu(rt
->rt_saddr
), buf2
),
1739 atomic_read(&rt
->dst
.__refcnt
),
1741 (int) dst_metric(&rt
->dst
, RTAX_RTT
));
1745 static const struct seq_operations dn_rt_cache_seq_ops
= {
1746 .start
= dn_rt_cache_seq_start
,
1747 .next
= dn_rt_cache_seq_next
,
1748 .stop
= dn_rt_cache_seq_stop
,
1749 .show
= dn_rt_cache_seq_show
,
1752 static int dn_rt_cache_seq_open(struct inode
*inode
, struct file
*file
)
1754 return seq_open_private(file
, &dn_rt_cache_seq_ops
,
1755 sizeof(struct dn_rt_cache_iter_state
));
1758 static const struct file_operations dn_rt_cache_seq_fops
= {
1759 .owner
= THIS_MODULE
,
1760 .open
= dn_rt_cache_seq_open
,
1762 .llseek
= seq_lseek
,
1763 .release
= seq_release_private
,
1766 #endif /* CONFIG_PROC_FS */
1768 void __init
dn_route_init(void)
1772 dn_dst_ops
.kmem_cachep
=
1773 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route
), 0,
1774 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
1775 dst_entries_init(&dn_dst_ops
);
1776 setup_timer(&dn_route_timer
, dn_dst_check_expire
, 0);
1777 dn_route_timer
.expires
= jiffies
+ decnet_dst_gc_interval
* HZ
;
1778 add_timer(&dn_route_timer
);
1780 goal
= totalram_pages
>> (26 - PAGE_SHIFT
);
1782 for(order
= 0; (1UL << order
) < goal
; order
++)
1786 * Only want 1024 entries max, since the table is very, very unlikely
1787 * to be larger than that.
1789 while(order
&& ((((1UL << order
) * PAGE_SIZE
) /
1790 sizeof(struct dn_rt_hash_bucket
)) >= 2048))
1794 dn_rt_hash_mask
= (1UL << order
) * PAGE_SIZE
/
1795 sizeof(struct dn_rt_hash_bucket
);
1796 while(dn_rt_hash_mask
& (dn_rt_hash_mask
- 1))
1798 dn_rt_hash_table
= (struct dn_rt_hash_bucket
*)
1799 __get_free_pages(GFP_ATOMIC
, order
);
1800 } while (dn_rt_hash_table
== NULL
&& --order
> 0);
1802 if (!dn_rt_hash_table
)
1803 panic("Failed to allocate DECnet route cache hash table\n");
1806 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1808 (long)(dn_rt_hash_mask
*sizeof(struct dn_rt_hash_bucket
))/1024);
1811 for(i
= 0; i
<= dn_rt_hash_mask
; i
++) {
1812 spin_lock_init(&dn_rt_hash_table
[i
].lock
);
1813 dn_rt_hash_table
[i
].chain
= NULL
;
1816 dn_dst_ops
.gc_thresh
= (dn_rt_hash_mask
+ 1);
1818 proc_net_fops_create(&init_net
, "decnet_cache", S_IRUGO
, &dn_rt_cache_seq_fops
);
1820 #ifdef CONFIG_DECNET_ROUTER
1821 rtnl_register(PF_DECnet
, RTM_GETROUTE
, dn_cache_getroute
, dn_fib_dump
);
1823 rtnl_register(PF_DECnet
, RTM_GETROUTE
, dn_cache_getroute
,
1828 void __exit
dn_route_cleanup(void)
1830 del_timer(&dn_route_timer
);
1833 proc_net_remove(&init_net
, "decnet_cache");
1834 dst_entries_destroy(&dn_dst_ops
);