Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DECnet An implementation of the DECnet protocol suite for the LINUX |
| 3 | * operating system. DECnet is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * DECnet Routing Functions (Endnode and Router) |
| 7 | * |
| 8 | * Authors: Steve Whitehouse <SteveW@ACM.org> |
| 9 | * Eduardo Marcelo Serrat <emserrat@geocities.com> |
| 10 | * |
| 11 | * Changes: |
| 12 | * Steve Whitehouse : Fixes to allow "intra-ethernet" and |
| 13 | * "return-to-sender" bits on outgoing |
| 14 | * packets. |
| 15 | * Steve Whitehouse : Timeouts for cached routes. |
| 16 | * Steve Whitehouse : Use dst cache for input routes too. |
| 17 | * Steve Whitehouse : Fixed error values in dn_send_skb. |
| 18 | * Steve Whitehouse : Rework routing functions to better fit |
| 19 | * DECnet routing design |
| 20 | * Alexey Kuznetsov : New SMP locking |
| 21 | * Steve Whitehouse : More SMP locking changes & dn_cache_dump() |
| 22 | * Steve Whitehouse : Prerouting NF hook, now really is prerouting. |
| 23 | * Fixed possible skb leak in rtnetlink funcs. |
| 24 | * Steve Whitehouse : Dave Miller's dynamic hash table sizing and |
| 25 | * Alexey Kuznetsov's finer grained locking |
| 26 | * from ipv4/route.c. |
| 27 | * Steve Whitehouse : Routing is now starting to look like a |
| 28 | * sensible set of code now, mainly due to |
| 29 | * my copying the IPv4 routing code. The |
| 30 | * hooks here are modified and will continue |
| 31 | * to evolve for a while. |
| 32 | * Steve Whitehouse : Real SMP at last :-) Also new netfilter |
| 33 | * stuff. Look out raw sockets your days |
| 34 | * are numbered! |
| 35 | * Steve Whitehouse : Added return-to-sender functions. Added |
| 36 | * backlog congestion level return codes. |
| 37 | * Steve Whitehouse : Fixed bug where routes were set up with |
| 38 | * no ref count on net devices. |
| 39 | * Steve Whitehouse : RCU for the route cache |
| 40 | * Steve Whitehouse : Preparations for the flow cache |
| 41 | * Steve Whitehouse : Prepare for nonlinear skbs |
| 42 | */ |
| 43 | |
| 44 | /****************************************************************************** |
| 45 | (c) 1995-1998 E.M. Serrat emserrat@geocities.com |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | This program is free software; you can redistribute it and/or modify |
| 48 | it under the terms of the GNU General Public License as published by |
| 49 | the Free Software Foundation; either version 2 of the License, or |
| 50 | any later version. |
| 51 | |
| 52 | This program is distributed in the hope that it will be useful, |
| 53 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 54 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 55 | GNU General Public License for more details. |
| 56 | *******************************************************************************/ |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <linux/errno.h> |
| 59 | #include <linux/types.h> |
| 60 | #include <linux/socket.h> |
| 61 | #include <linux/in.h> |
| 62 | #include <linux/kernel.h> |
| 63 | #include <linux/sockios.h> |
| 64 | #include <linux/net.h> |
| 65 | #include <linux/netdevice.h> |
| 66 | #include <linux/inet.h> |
| 67 | #include <linux/route.h> |
| 68 | #include <linux/in_route.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 69 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #include <net/sock.h> |
| 71 | #include <linux/mm.h> |
| 72 | #include <linux/proc_fs.h> |
| 73 | #include <linux/seq_file.h> |
| 74 | #include <linux/init.h> |
| 75 | #include <linux/rtnetlink.h> |
| 76 | #include <linux/string.h> |
| 77 | #include <linux/netfilter_decnet.h> |
| 78 | #include <linux/rcupdate.h> |
| 79 | #include <linux/times.h> |
| 80 | #include <asm/errno.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 81 | #include <net/net_namespace.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 82 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #include <net/neighbour.h> |
| 84 | #include <net/dst.h> |
| 85 | #include <net/flow.h> |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 86 | #include <net/fib_rules.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | #include <net/dn.h> |
| 88 | #include <net/dn_dev.h> |
| 89 | #include <net/dn_nsp.h> |
| 90 | #include <net/dn_route.h> |
| 91 | #include <net/dn_neigh.h> |
| 92 | #include <net/dn_fib.h> |
| 93 | |
| 94 | struct dn_rt_hash_bucket |
| 95 | { |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 96 | struct dn_route __rcu *chain; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | spinlock_t lock; |
Eric Dumazet | fca09fb | 2008-02-07 23:29:57 -0800 | [diff] [blame] | 98 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | extern struct neigh_table dn_neigh_table; |
| 101 | |
| 102 | |
| 103 | static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00}; |
| 104 | |
| 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; |
| 108 | |
| 109 | static unsigned long dn_rt_deadline; |
| 110 | |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 111 | static int dn_dst_gc(struct dst_ops *ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | static struct dst_entry *dn_dst_check(struct dst_entry *, __u32); |
| 113 | static struct dst_entry *dn_dst_negative_advice(struct dst_entry *); |
| 114 | static void dn_dst_link_failure(struct sk_buff *); |
| 115 | static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu); |
| 116 | static int dn_route_input(struct sk_buff *); |
| 117 | static void dn_run_flush(unsigned long dummy); |
| 118 | |
| 119 | static struct dn_rt_hash_bucket *dn_rt_hash_table; |
| 120 | static unsigned dn_rt_hash_mask; |
| 121 | |
| 122 | static struct timer_list dn_route_timer; |
Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 123 | static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | int decnet_dst_gc_interval = 2; |
| 125 | |
| 126 | static struct dst_ops dn_dst_ops = { |
| 127 | .family = PF_DECnet, |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 128 | .protocol = cpu_to_be16(ETH_P_DNA_RT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | .gc_thresh = 128, |
| 130 | .gc = dn_dst_gc, |
| 131 | .check = dn_dst_check, |
| 132 | .negative_advice = dn_dst_negative_advice, |
| 133 | .link_failure = dn_dst_link_failure, |
| 134 | .update_pmtu = dn_dst_update_pmtu, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 137 | static __inline__ unsigned dn_hash(__le16 src, __le16 dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 139 | __u16 tmp = (__u16 __force)(src ^ dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | tmp ^= (tmp >> 3); |
| 141 | tmp ^= (tmp >> 5); |
| 142 | tmp ^= (tmp >> 10); |
| 143 | return dn_rt_hash_mask & (unsigned)tmp; |
| 144 | } |
| 145 | |
| 146 | static inline void dnrt_free(struct dn_route *rt) |
| 147 | { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 148 | call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static inline void dnrt_drop(struct dn_route *rt) |
| 152 | { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 153 | dst_release(&rt->dst); |
| 154 | call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void dn_dst_check_expire(unsigned long dummy) |
| 158 | { |
| 159 | int i; |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 160 | struct dn_route *rt; |
| 161 | struct dn_route __rcu **rtp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | unsigned long now = jiffies; |
| 163 | unsigned long expire = 120 * HZ; |
| 164 | |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 165 | for (i = 0; i <= dn_rt_hash_mask; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | rtp = &dn_rt_hash_table[i].chain; |
| 167 | |
| 168 | spin_lock(&dn_rt_hash_table[i].lock); |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 169 | while ((rt = rcu_dereference_protected(*rtp, |
| 170 | lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 171 | if (atomic_read(&rt->dst.__refcnt) || |
| 172 | (now - rt->dst.lastuse) < expire) { |
| 173 | rtp = &rt->dst.dn_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | continue; |
| 175 | } |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 176 | *rtp = rt->dst.dn_next; |
| 177 | rt->dst.dn_next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | dnrt_free(rt); |
| 179 | } |
| 180 | spin_unlock(&dn_rt_hash_table[i].lock); |
| 181 | |
| 182 | if ((jiffies - now) > 0) |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ); |
| 187 | } |
| 188 | |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 189 | static int dn_dst_gc(struct dst_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 191 | struct dn_route *rt; |
| 192 | struct dn_route __rcu **rtp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | int i; |
| 194 | unsigned long now = jiffies; |
| 195 | unsigned long expire = 10 * HZ; |
| 196 | |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 197 | for (i = 0; i <= dn_rt_hash_mask; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | spin_lock_bh(&dn_rt_hash_table[i].lock); |
| 200 | rtp = &dn_rt_hash_table[i].chain; |
| 201 | |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 202 | while ((rt = rcu_dereference_protected(*rtp, |
| 203 | lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 204 | if (atomic_read(&rt->dst.__refcnt) || |
| 205 | (now - rt->dst.lastuse) < expire) { |
| 206 | rtp = &rt->dst.dn_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | continue; |
| 208 | } |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 209 | *rtp = rt->dst.dn_next; |
| 210 | rt->dst.dn_next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | dnrt_drop(rt); |
| 212 | break; |
| 213 | } |
| 214 | spin_unlock_bh(&dn_rt_hash_table[i].lock); |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * The decnet standards don't impose a particular minimum mtu, what they |
| 222 | * do insist on is that the routing layer accepts a datagram of at least |
| 223 | * 230 bytes long. Here we have to subtract the routing header length from |
| 224 | * 230 to get the minimum acceptable mtu. If there is no neighbour, then we |
| 225 | * assume the worst and use a long header size. |
| 226 | * |
| 227 | * We update both the mtu and the advertised mss (i.e. the segment size we |
| 228 | * advertise to the other end). |
| 229 | */ |
| 230 | static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu) |
| 231 | { |
| 232 | u32 min_mtu = 230; |
| 233 | struct dn_dev *dn = dst->neighbour ? |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 234 | rcu_dereference_raw(dst->neighbour->dev->dn_ptr) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | if (dn && dn->use_long == 0) |
| 237 | min_mtu -= 6; |
| 238 | else |
| 239 | min_mtu -= 21; |
| 240 | |
Satoru SATOH | 5ffc02a | 2008-05-04 22:14:42 -0700 | [diff] [blame] | 241 | if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if (!(dst_metric_locked(dst, RTAX_MTU))) { |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame^] | 243 | dst_metric_set(dst, RTAX_MTU, mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | dst_set_expires(dst, dn_rt_mtu_expires); |
| 245 | } |
| 246 | if (!(dst_metric_locked(dst, RTAX_ADVMSS))) { |
| 247 | u32 mss = mtu - DN_MAX_NSP_DATA_HEADER; |
Satoru SATOH | 5ffc02a | 2008-05-04 22:14:42 -0700 | [diff] [blame] | 248 | if (dst_metric(dst, RTAX_ADVMSS) > mss) |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame^] | 249 | dst_metric_set(dst, RTAX_ADVMSS, mss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 254 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | * When a route has been marked obsolete. (e.g. routing cache flush) |
| 256 | */ |
| 257 | static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie) |
| 258 | { |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst) |
| 263 | { |
| 264 | dst_release(dst); |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | static void dn_dst_link_failure(struct sk_buff *skb) |
| 269 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static inline int compare_keys(struct flowi *fl1, struct flowi *fl2) |
| 273 | { |
Changli Gao | 5811662 | 2010-11-12 18:43:55 +0000 | [diff] [blame] | 274 | return ((fl1->fld_dst ^ fl2->fld_dst) | |
| 275 | (fl1->fld_src ^ fl2->fld_src) | |
Thomas Graf | 47dcf0c | 2006-11-09 15:20:38 -0800 | [diff] [blame] | 276 | (fl1->mark ^ fl2->mark) | |
Changli Gao | 5811662 | 2010-11-12 18:43:55 +0000 | [diff] [blame] | 277 | (fl1->fld_scope ^ fl2->fld_scope) | |
David S. Miller | 8238b21 | 2006-10-12 00:49:15 -0700 | [diff] [blame] | 278 | (fl1->oif ^ fl2->oif) | |
| 279 | (fl1->iif ^ fl2->iif)) == 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static int dn_insert_route(struct dn_route *rt, unsigned hash, struct dn_route **rp) |
| 283 | { |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 284 | struct dn_route *rth; |
| 285 | struct dn_route __rcu **rthp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | unsigned long now = jiffies; |
| 287 | |
| 288 | rthp = &dn_rt_hash_table[hash].chain; |
| 289 | |
| 290 | spin_lock_bh(&dn_rt_hash_table[hash].lock); |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 291 | while ((rth = rcu_dereference_protected(*rthp, |
| 292 | lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | if (compare_keys(&rth->fl, &rt->fl)) { |
| 294 | /* Put it first */ |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 295 | *rthp = rth->dst.dn_next; |
| 296 | rcu_assign_pointer(rth->dst.dn_next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | dn_rt_hash_table[hash].chain); |
| 298 | rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth); |
| 299 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 300 | dst_use(&rth->dst, now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | spin_unlock_bh(&dn_rt_hash_table[hash].lock); |
| 302 | |
| 303 | dnrt_drop(rt); |
| 304 | *rp = rth; |
| 305 | return 0; |
| 306 | } |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 307 | rthp = &rth->dst.dn_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 310 | rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 312 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 313 | dst_use(&rt->dst, now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | spin_unlock_bh(&dn_rt_hash_table[hash].lock); |
| 315 | *rp = rt; |
| 316 | return 0; |
| 317 | } |
| 318 | |
Roel Kluin | 5eaa65b | 2008-12-10 15:18:31 -0800 | [diff] [blame] | 319 | static void dn_run_flush(unsigned long dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
| 321 | int i; |
| 322 | struct dn_route *rt, *next; |
| 323 | |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 324 | for (i = 0; i < dn_rt_hash_mask; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | spin_lock_bh(&dn_rt_hash_table[i].lock); |
| 326 | |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 327 | if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | goto nothing_to_declare; |
| 329 | |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 330 | for(; rt; rt = next) { |
| 331 | next = rcu_dereference_raw(rt->dst.dn_next); |
| 332 | RCU_INIT_POINTER(rt->dst.dn_next, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | dst_free((struct dst_entry *)rt); |
| 334 | } |
| 335 | |
| 336 | nothing_to_declare: |
| 337 | spin_unlock_bh(&dn_rt_hash_table[i].lock); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static DEFINE_SPINLOCK(dn_rt_flush_lock); |
| 342 | |
| 343 | void dn_rt_cache_flush(int delay) |
| 344 | { |
| 345 | unsigned long now = jiffies; |
| 346 | int user_mode = !in_interrupt(); |
| 347 | |
| 348 | if (delay < 0) |
| 349 | delay = dn_rt_min_delay; |
| 350 | |
| 351 | spin_lock_bh(&dn_rt_flush_lock); |
| 352 | |
| 353 | if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) { |
| 354 | long tmo = (long)(dn_rt_deadline - now); |
| 355 | |
| 356 | if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay) |
| 357 | tmo = 0; |
| 358 | |
| 359 | if (delay > tmo) |
| 360 | delay = tmo; |
| 361 | } |
| 362 | |
| 363 | if (delay <= 0) { |
| 364 | spin_unlock_bh(&dn_rt_flush_lock); |
| 365 | dn_run_flush(0); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | if (dn_rt_deadline == 0) |
| 370 | dn_rt_deadline = now + dn_rt_max_delay; |
| 371 | |
| 372 | dn_rt_flush_timer.expires = now + delay; |
| 373 | add_timer(&dn_rt_flush_timer); |
| 374 | spin_unlock_bh(&dn_rt_flush_lock); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * dn_return_short - Return a short packet to its sender |
| 379 | * @skb: The packet to return |
| 380 | * |
| 381 | */ |
| 382 | static int dn_return_short(struct sk_buff *skb) |
| 383 | { |
| 384 | struct dn_skb_cb *cb; |
| 385 | unsigned char *ptr; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 386 | __le16 *src; |
| 387 | __le16 *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | /* Add back headers */ |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 390 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL) |
| 393 | return NET_RX_DROP; |
| 394 | |
| 395 | cb = DN_SKB_CB(skb); |
| 396 | /* Skip packet length and point to flags */ |
| 397 | ptr = skb->data + 2; |
| 398 | *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS; |
| 399 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 400 | dst = (__le16 *)ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | ptr += 2; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 402 | src = (__le16 *)ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | ptr += 2; |
| 404 | *ptr = 0; /* Zero hop count */ |
| 405 | |
Ilpo Järvinen | a0bffff | 2009-03-21 13:36:17 -0700 | [diff] [blame] | 406 | swap(*src, *dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
| 408 | skb->pkt_type = PACKET_OUTGOING; |
| 409 | dn_rt_finish_output(skb, NULL, NULL); |
| 410 | return NET_RX_SUCCESS; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * dn_return_long - Return a long packet to its sender |
| 415 | * @skb: The long format packet to return |
| 416 | * |
| 417 | */ |
| 418 | static int dn_return_long(struct sk_buff *skb) |
| 419 | { |
| 420 | struct dn_skb_cb *cb; |
| 421 | unsigned char *ptr; |
| 422 | unsigned char *src_addr, *dst_addr; |
| 423 | unsigned char tmp[ETH_ALEN]; |
| 424 | |
| 425 | /* Add back all headers */ |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 426 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL) |
| 429 | return NET_RX_DROP; |
| 430 | |
| 431 | cb = DN_SKB_CB(skb); |
| 432 | /* Ignore packet length and point to flags */ |
| 433 | ptr = skb->data + 2; |
| 434 | |
| 435 | /* Skip padding */ |
| 436 | if (*ptr & DN_RT_F_PF) { |
| 437 | char padlen = (*ptr & ~DN_RT_F_PF); |
| 438 | ptr += padlen; |
| 439 | } |
| 440 | |
| 441 | *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS; |
| 442 | ptr += 2; |
| 443 | dst_addr = ptr; |
| 444 | ptr += 8; |
| 445 | src_addr = ptr; |
| 446 | ptr += 6; |
| 447 | *ptr = 0; /* Zero hop count */ |
| 448 | |
| 449 | /* Swap source and destination */ |
| 450 | memcpy(tmp, src_addr, ETH_ALEN); |
| 451 | memcpy(src_addr, dst_addr, ETH_ALEN); |
| 452 | memcpy(dst_addr, tmp, ETH_ALEN); |
| 453 | |
| 454 | skb->pkt_type = PACKET_OUTGOING; |
| 455 | dn_rt_finish_output(skb, dst_addr, src_addr); |
| 456 | return NET_RX_SUCCESS; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * dn_route_rx_packet - Try and find a route for an incoming packet |
| 461 | * @skb: The packet to find a route for |
| 462 | * |
| 463 | * Returns: result of input function if route is found, error code otherwise |
| 464 | */ |
| 465 | static int dn_route_rx_packet(struct sk_buff *skb) |
| 466 | { |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 467 | struct dn_skb_cb *cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | int err; |
| 469 | |
| 470 | if ((err = dn_route_input(skb)) == 0) |
| 471 | return dst_input(skb); |
| 472 | |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 473 | cb = DN_SKB_CB(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | if (decnet_debug_level & 4) { |
| 475 | char *devname = skb->dev ? skb->dev->name : "???"; |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | printk(KERN_DEBUG |
| 478 | "DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%d\n", |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 479 | (int)cb->rt_flags, devname, skb->len, |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 480 | le16_to_cpu(cb->src), le16_to_cpu(cb->dst), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | err, skb->pkt_type); |
| 482 | } |
| 483 | |
| 484 | if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) { |
| 485 | switch(cb->rt_flags & DN_RT_PKT_MSK) { |
| 486 | case DN_RT_PKT_SHORT: |
| 487 | return dn_return_short(skb); |
| 488 | case DN_RT_PKT_LONG: |
| 489 | return dn_return_long(skb); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | kfree_skb(skb); |
| 494 | return NET_RX_DROP; |
| 495 | } |
| 496 | |
| 497 | static int dn_route_rx_long(struct sk_buff *skb) |
| 498 | { |
| 499 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 500 | unsigned char *ptr = skb->data; |
| 501 | |
| 502 | if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */ |
| 503 | goto drop_it; |
| 504 | |
| 505 | skb_pull(skb, 20); |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 506 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 508 | /* Destination info */ |
| 509 | ptr += 2; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 510 | cb->dst = dn_eth2dn(ptr); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 511 | if (memcmp(ptr, dn_hiord_addr, 4) != 0) |
| 512 | goto drop_it; |
| 513 | ptr += 6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
| 515 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 516 | /* Source info */ |
| 517 | ptr += 2; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 518 | cb->src = dn_eth2dn(ptr); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 519 | if (memcmp(ptr, dn_hiord_addr, 4) != 0) |
| 520 | goto drop_it; |
| 521 | ptr += 6; |
| 522 | /* Other junk */ |
| 523 | ptr++; |
| 524 | cb->hops = *ptr++; /* Visit Count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Jan Engelhardt | 5d877d8 | 2010-03-23 04:09:14 +0100 | [diff] [blame] | 526 | return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL, |
| 527 | dn_route_rx_packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
| 529 | drop_it: |
| 530 | kfree_skb(skb); |
| 531 | return NET_RX_DROP; |
| 532 | } |
| 533 | |
| 534 | |
| 535 | |
| 536 | static int dn_route_rx_short(struct sk_buff *skb) |
| 537 | { |
| 538 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 539 | unsigned char *ptr = skb->data; |
| 540 | |
| 541 | if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */ |
| 542 | goto drop_it; |
| 543 | |
| 544 | skb_pull(skb, 5); |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 545 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 547 | cb->dst = *(__le16 *)ptr; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 548 | ptr += 2; |
| 549 | cb->src = *(__le16 *)ptr; |
| 550 | ptr += 2; |
| 551 | cb->hops = *ptr & 0x3f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Jan Engelhardt | 5d877d8 | 2010-03-23 04:09:14 +0100 | [diff] [blame] | 553 | return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL, |
| 554 | dn_route_rx_packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | drop_it: |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 557 | kfree_skb(skb); |
| 558 | return NET_RX_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static int dn_route_discard(struct sk_buff *skb) |
| 562 | { |
| 563 | /* |
| 564 | * I know we drop the packet here, but thats considered success in |
| 565 | * this case |
| 566 | */ |
| 567 | kfree_skb(skb); |
| 568 | return NET_RX_SUCCESS; |
| 569 | } |
| 570 | |
| 571 | static int dn_route_ptp_hello(struct sk_buff *skb) |
| 572 | { |
| 573 | dn_dev_hello(skb); |
| 574 | dn_neigh_pointopoint_hello(skb); |
| 575 | return NET_RX_SUCCESS; |
| 576 | } |
| 577 | |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 578 | int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
| 580 | struct dn_skb_cb *cb; |
| 581 | unsigned char flags = 0; |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 582 | __u16 len = le16_to_cpu(*(__le16 *)skb->data); |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 583 | struct dn_dev *dn = rcu_dereference(dev->dn_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | unsigned char padlen = 0; |
| 585 | |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 586 | if (!net_eq(dev_net(dev), &init_net)) |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 587 | goto dump_it; |
| 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | if (dn == NULL) |
| 590 | goto dump_it; |
| 591 | |
| 592 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) |
| 593 | goto out; |
| 594 | |
| 595 | if (!pskb_may_pull(skb, 3)) |
| 596 | goto dump_it; |
| 597 | |
| 598 | skb_pull(skb, 2); |
| 599 | |
| 600 | if (len > skb->len) |
| 601 | goto dump_it; |
| 602 | |
| 603 | skb_trim(skb, len); |
| 604 | |
| 605 | flags = *skb->data; |
| 606 | |
| 607 | cb = DN_SKB_CB(skb); |
| 608 | cb->stamp = jiffies; |
| 609 | cb->iif = dev->ifindex; |
| 610 | |
| 611 | /* |
| 612 | * If we have padding, remove it. |
| 613 | */ |
| 614 | if (flags & DN_RT_F_PF) { |
| 615 | padlen = flags & ~DN_RT_F_PF; |
| 616 | if (!pskb_may_pull(skb, padlen + 1)) |
| 617 | goto dump_it; |
| 618 | skb_pull(skb, padlen); |
| 619 | flags = *skb->data; |
| 620 | } |
| 621 | |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 622 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * Weed out future version DECnet |
| 626 | */ |
| 627 | if (flags & DN_RT_F_VER) |
| 628 | goto dump_it; |
| 629 | |
| 630 | cb->rt_flags = flags; |
| 631 | |
| 632 | if (decnet_debug_level & 1) |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 633 | printk(KERN_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n", |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 635 | (int)flags, (dev) ? dev->name : "???", len, skb->len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | padlen); |
| 637 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 638 | if (flags & DN_RT_PKT_CNTL) { |
Herbert Xu | 364c6ba | 2006-06-09 16:10:40 -0700 | [diff] [blame] | 639 | if (unlikely(skb_linearize(skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | goto dump_it; |
| 641 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 642 | switch(flags & DN_RT_CNTL_MSK) { |
| 643 | case DN_RT_PKT_INIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | dn_dev_init_pkt(skb); |
| 645 | break; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 646 | case DN_RT_PKT_VERI: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | dn_dev_veri_pkt(skb); |
| 648 | break; |
| 649 | } |
| 650 | |
| 651 | if (dn->parms.state != DN_DEV_S_RU) |
| 652 | goto dump_it; |
| 653 | |
| 654 | switch(flags & DN_RT_CNTL_MSK) { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 655 | case DN_RT_PKT_HELO: |
Jan Engelhardt | 5d877d8 | 2010-03-23 04:09:14 +0100 | [diff] [blame] | 656 | return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO, |
| 657 | skb, skb->dev, NULL, |
| 658 | dn_route_ptp_hello); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 660 | case DN_RT_PKT_L1RT: |
| 661 | case DN_RT_PKT_L2RT: |
Jan Engelhardt | 5d877d8 | 2010-03-23 04:09:14 +0100 | [diff] [blame] | 662 | return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE, |
| 663 | skb, skb->dev, NULL, |
| 664 | dn_route_discard); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 665 | case DN_RT_PKT_ERTH: |
Jan Engelhardt | 5d877d8 | 2010-03-23 04:09:14 +0100 | [diff] [blame] | 666 | return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO, |
| 667 | skb, skb->dev, NULL, |
| 668 | dn_neigh_router_hello); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 670 | case DN_RT_PKT_EEDH: |
Jan Engelhardt | 5d877d8 | 2010-03-23 04:09:14 +0100 | [diff] [blame] | 671 | return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO, |
| 672 | skb, skb->dev, NULL, |
| 673 | dn_neigh_endnode_hello); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 674 | } |
| 675 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (dn->parms.state != DN_DEV_S_RU) |
| 677 | goto dump_it; |
| 678 | |
| 679 | skb_pull(skb, 1); /* Pull flags */ |
| 680 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 681 | switch(flags & DN_RT_PKT_MSK) { |
| 682 | case DN_RT_PKT_LONG: |
| 683 | return dn_route_rx_long(skb); |
| 684 | case DN_RT_PKT_SHORT: |
| 685 | return dn_route_rx_short(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 687 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
| 689 | dump_it: |
| 690 | kfree_skb(skb); |
| 691 | out: |
| 692 | return NET_RX_DROP; |
| 693 | } |
| 694 | |
| 695 | static int dn_output(struct sk_buff *skb) |
| 696 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 697 | struct dst_entry *dst = skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | struct dn_route *rt = (struct dn_route *)dst; |
| 699 | struct net_device *dev = dst->dev; |
| 700 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 701 | struct neighbour *neigh; |
| 702 | |
| 703 | int err = -EINVAL; |
| 704 | |
| 705 | if ((neigh = dst->neighbour) == NULL) |
| 706 | goto error; |
| 707 | |
| 708 | skb->dev = dev; |
| 709 | |
| 710 | cb->src = rt->rt_saddr; |
| 711 | cb->dst = rt->rt_daddr; |
| 712 | |
| 713 | /* |
| 714 | * Always set the Intra-Ethernet bit on all outgoing packets |
| 715 | * originated on this node. Only valid flag from upper layers |
| 716 | * is return-to-sender-requested. Set hop count to 0 too. |
| 717 | */ |
| 718 | cb->rt_flags &= ~DN_RT_F_RQR; |
| 719 | cb->rt_flags |= DN_RT_F_IE; |
| 720 | cb->hops = 0; |
| 721 | |
Jan Engelhardt | 5d877d8 | 2010-03-23 04:09:14 +0100 | [diff] [blame] | 722 | return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev, |
| 723 | neigh->output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | error: |
| 726 | if (net_ratelimit()) |
| 727 | printk(KERN_DEBUG "dn_output: This should not happen\n"); |
| 728 | |
| 729 | kfree_skb(skb); |
| 730 | |
| 731 | return err; |
| 732 | } |
| 733 | |
| 734 | static int dn_forward(struct sk_buff *skb) |
| 735 | { |
| 736 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 737 | struct dst_entry *dst = skb_dst(skb); |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 738 | struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | struct dn_route *rt; |
| 740 | struct neighbour *neigh = dst->neighbour; |
| 741 | int header_len; |
| 742 | #ifdef CONFIG_NETFILTER |
| 743 | struct net_device *dev = skb->dev; |
| 744 | #endif |
| 745 | |
| 746 | if (skb->pkt_type != PACKET_HOST) |
| 747 | goto drop; |
| 748 | |
| 749 | /* Ensure that we have enough space for headers */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 750 | rt = (struct dn_route *)skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | header_len = dn_db->use_long ? 21 : 6; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 752 | if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | goto drop; |
| 754 | |
| 755 | /* |
| 756 | * Hop count exceeded. |
| 757 | */ |
| 758 | if (++cb->hops > 30) |
| 759 | goto drop; |
| 760 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 761 | skb->dev = rt->dst.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
| 763 | /* |
| 764 | * If packet goes out same interface it came in on, then set |
| 765 | * the Intra-Ethernet bit. This has no effect for short |
| 766 | * packets, so we don't need to test for them here. |
| 767 | */ |
| 768 | cb->rt_flags &= ~DN_RT_F_IE; |
| 769 | if (rt->rt_flags & RTCF_DOREDIRECT) |
| 770 | cb->rt_flags |= DN_RT_F_IE; |
| 771 | |
Jan Engelhardt | 5d877d8 | 2010-03-23 04:09:14 +0100 | [diff] [blame] | 772 | return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev, |
| 773 | neigh->output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | drop: |
| 776 | kfree_skb(skb); |
| 777 | return NET_RX_DROP; |
| 778 | } |
| 779 | |
| 780 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | * Used to catch bugs. This should never normally get |
| 782 | * called. |
| 783 | */ |
| 784 | static int dn_rt_bug(struct sk_buff *skb) |
| 785 | { |
| 786 | if (net_ratelimit()) { |
| 787 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 788 | |
| 789 | printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n", |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 790 | le16_to_cpu(cb->src), le16_to_cpu(cb->dst)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | kfree_skb(skb); |
| 794 | |
Florian Westphal | 0e8635a | 2009-06-20 00:53:25 +0000 | [diff] [blame] | 795 | return NET_RX_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res) |
| 799 | { |
| 800 | struct dn_fib_info *fi = res->fi; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 801 | struct net_device *dev = rt->dst.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | struct neighbour *n; |
| 803 | unsigned mss; |
| 804 | |
| 805 | if (fi) { |
| 806 | if (DN_FIB_RES_GW(*res) && |
| 807 | DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) |
| 808 | rt->rt_gateway = DN_FIB_RES_GW(*res); |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame^] | 809 | dst_import_metrics(&rt->dst, fi->fib_metrics); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | rt->rt_type = res->type; |
| 812 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 813 | if (dev != NULL && rt->dst.neighbour == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev); |
| 815 | if (IS_ERR(n)) |
| 816 | return PTR_ERR(n); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 817 | rt->dst.neighbour = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 820 | if (dst_metric(&rt->dst, RTAX_MTU) == 0 || |
| 821 | dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu) |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame^] | 822 | dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 823 | mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst)); |
| 824 | if (dst_metric(&rt->dst, RTAX_ADVMSS) == 0 || |
| 825 | dst_metric(&rt->dst, RTAX_ADVMSS) > mss) |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame^] | 826 | dst_metric_set(&rt->dst, RTAX_ADVMSS, mss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 830 | static inline int dn_match_addr(__le16 addr1, __le16 addr2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | { |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 832 | __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | int match = 16; |
| 834 | while(tmp) { |
| 835 | tmp >>= 1; |
| 836 | match--; |
| 837 | } |
| 838 | return match; |
| 839 | } |
| 840 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 841 | static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | { |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 843 | __le16 saddr = 0; |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 844 | struct dn_dev *dn_db; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | struct dn_ifaddr *ifa; |
| 846 | int best_match = 0; |
| 847 | int ret; |
| 848 | |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 849 | rcu_read_lock(); |
| 850 | dn_db = rcu_dereference(dev->dn_ptr); |
| 851 | for (ifa = rcu_dereference(dn_db->ifa_list); |
| 852 | ifa != NULL; |
| 853 | ifa = rcu_dereference(ifa->ifa_next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | if (ifa->ifa_scope > scope) |
| 855 | continue; |
| 856 | if (!daddr) { |
| 857 | saddr = ifa->ifa_local; |
| 858 | break; |
| 859 | } |
| 860 | ret = dn_match_addr(daddr, ifa->ifa_local); |
| 861 | if (ret > best_match) |
| 862 | saddr = ifa->ifa_local; |
| 863 | if (best_match == 0) |
| 864 | saddr = ifa->ifa_local; |
| 865 | } |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 866 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | return saddr; |
| 869 | } |
| 870 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 871 | static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
| 873 | return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope); |
| 874 | } |
| 875 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 876 | static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 878 | __le16 mask = dnet_make_mask(res->prefixlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return (daddr&~mask)|res->fi->fib_nh->nh_gw; |
| 880 | } |
| 881 | |
| 882 | static int dn_route_output_slow(struct dst_entry **pprt, const struct flowi *oldflp, int try_hard) |
| 883 | { |
Changli Gao | 5811662 | 2010-11-12 18:43:55 +0000 | [diff] [blame] | 884 | struct flowi fl = { .fld_dst = oldflp->fld_dst, |
| 885 | .fld_src = oldflp->fld_src, |
| 886 | .fld_scope = RT_SCOPE_UNIVERSE, |
Thomas Graf | 47dcf0c | 2006-11-09 15:20:38 -0800 | [diff] [blame] | 887 | .mark = oldflp->mark, |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 888 | .iif = init_net.loopback_dev->ifindex, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | .oif = oldflp->oif }; |
| 890 | struct dn_route *rt = NULL; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 891 | struct net_device *dev_out = NULL, *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | struct neighbour *neigh = NULL; |
| 893 | unsigned hash; |
| 894 | unsigned flags = 0; |
| 895 | struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST }; |
| 896 | int err; |
| 897 | int free_res = 0; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 898 | __le16 gateway = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
| 900 | if (decnet_debug_level & 16) |
| 901 | printk(KERN_DEBUG |
| 902 | "dn_route_output_slow: dst=%04x src=%04x mark=%d" |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 903 | " iif=%d oif=%d\n", le16_to_cpu(oldflp->fld_dst), |
| 904 | le16_to_cpu(oldflp->fld_src), |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 905 | oldflp->mark, init_net.loopback_dev->ifindex, oldflp->oif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
| 907 | /* If we have an output interface, verify its a DECnet device */ |
| 908 | if (oldflp->oif) { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 909 | dev_out = dev_get_by_index(&init_net, oldflp->oif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | err = -ENODEV; |
| 911 | if (dev_out && dev_out->dn_ptr == NULL) { |
| 912 | dev_put(dev_out); |
| 913 | dev_out = NULL; |
| 914 | } |
| 915 | if (dev_out == NULL) |
| 916 | goto out; |
| 917 | } |
| 918 | |
| 919 | /* If we have a source address, verify that its a local address */ |
| 920 | if (oldflp->fld_src) { |
| 921 | err = -EADDRNOTAVAIL; |
| 922 | |
| 923 | if (dev_out) { |
| 924 | if (dn_dev_islocal(dev_out, oldflp->fld_src)) |
| 925 | goto source_ok; |
| 926 | dev_put(dev_out); |
| 927 | goto out; |
| 928 | } |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 929 | rcu_read_lock(); |
| 930 | for_each_netdev_rcu(&init_net, dev) { |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 931 | if (!dev->dn_ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | continue; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 933 | if (!dn_dev_islocal(dev, oldflp->fld_src)) |
Patrick Caulfield | 9bbf28a1 | 2006-08-02 14:14:44 -0700 | [diff] [blame] | 934 | continue; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 935 | if ((dev->flags & IFF_LOOPBACK) && |
Patrick Caulfield | 9bbf28a1 | 2006-08-02 14:14:44 -0700 | [diff] [blame] | 936 | oldflp->fld_dst && |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 937 | !dn_dev_islocal(dev, oldflp->fld_dst)) |
Patrick Caulfield | 9bbf28a1 | 2006-08-02 14:14:44 -0700 | [diff] [blame] | 938 | continue; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 939 | |
| 940 | dev_out = dev; |
Patrick Caulfield | 9bbf28a1 | 2006-08-02 14:14:44 -0700 | [diff] [blame] | 941 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | } |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 943 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | if (dev_out == NULL) |
| 945 | goto out; |
| 946 | dev_hold(dev_out); |
| 947 | source_ok: |
| 948 | ; |
| 949 | } |
| 950 | |
| 951 | /* No destination? Assume its local */ |
| 952 | if (!fl.fld_dst) { |
| 953 | fl.fld_dst = fl.fld_src; |
| 954 | |
| 955 | err = -EADDRNOTAVAIL; |
| 956 | if (dev_out) |
| 957 | dev_put(dev_out); |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 958 | dev_out = init_net.loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | dev_hold(dev_out); |
| 960 | if (!fl.fld_dst) { |
| 961 | fl.fld_dst = |
| 962 | fl.fld_src = dnet_select_source(dev_out, 0, |
| 963 | RT_SCOPE_HOST); |
| 964 | if (!fl.fld_dst) |
| 965 | goto out; |
| 966 | } |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 967 | fl.oif = init_net.loopback_dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | res.type = RTN_LOCAL; |
| 969 | goto make_route; |
| 970 | } |
| 971 | |
| 972 | if (decnet_debug_level & 16) |
| 973 | printk(KERN_DEBUG |
| 974 | "dn_route_output_slow: initial checks complete." |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 975 | " dst=%o4x src=%04x oif=%d try_hard=%d\n", |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 976 | le16_to_cpu(fl.fld_dst), le16_to_cpu(fl.fld_src), |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 977 | fl.oif, try_hard); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | |
| 979 | /* |
| 980 | * N.B. If the kernel is compiled without router support then |
| 981 | * dn_fib_lookup() will evaluate to non-zero so this if () block |
| 982 | * will always be executed. |
| 983 | */ |
| 984 | err = -ESRCH; |
| 985 | if (try_hard || (err = dn_fib_lookup(&fl, &res)) != 0) { |
| 986 | struct dn_dev *dn_db; |
| 987 | if (err != -ESRCH) |
| 988 | goto out; |
| 989 | /* |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 990 | * Here the fallback is basically the standard algorithm for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | * routing in endnodes which is described in the DECnet routing |
| 992 | * docs |
| 993 | * |
| 994 | * If we are not trying hard, look in neighbour cache. |
| 995 | * The result is tested to ensure that if a specific output |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 996 | * device/source address was requested, then we honour that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | * here |
| 998 | */ |
| 999 | if (!try_hard) { |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 1000 | neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fl.fld_dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | if (neigh) { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1002 | if ((oldflp->oif && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | (neigh->dev->ifindex != oldflp->oif)) || |
| 1004 | (oldflp->fld_src && |
| 1005 | (!dn_dev_islocal(neigh->dev, |
| 1006 | oldflp->fld_src)))) { |
| 1007 | neigh_release(neigh); |
| 1008 | neigh = NULL; |
| 1009 | } else { |
| 1010 | if (dev_out) |
| 1011 | dev_put(dev_out); |
| 1012 | if (dn_dev_islocal(neigh->dev, fl.fld_dst)) { |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 1013 | dev_out = init_net.loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | res.type = RTN_LOCAL; |
| 1015 | } else { |
| 1016 | dev_out = neigh->dev; |
| 1017 | } |
| 1018 | dev_hold(dev_out); |
| 1019 | goto select_source; |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /* Not there? Perhaps its a local address */ |
| 1025 | if (dev_out == NULL) |
| 1026 | dev_out = dn_dev_get_default(); |
| 1027 | err = -ENODEV; |
| 1028 | if (dev_out == NULL) |
| 1029 | goto out; |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 1030 | dn_db = rcu_dereference_raw(dev_out->dn_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | /* Possible improvement - check all devices for local addr */ |
| 1032 | if (dn_dev_islocal(dev_out, fl.fld_dst)) { |
| 1033 | dev_put(dev_out); |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 1034 | dev_out = init_net.loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | dev_hold(dev_out); |
| 1036 | res.type = RTN_LOCAL; |
| 1037 | goto select_source; |
| 1038 | } |
| 1039 | /* Not local either.... try sending it to the default router */ |
| 1040 | neigh = neigh_clone(dn_db->router); |
| 1041 | BUG_ON(neigh && neigh->dev != dev_out); |
| 1042 | |
| 1043 | /* Ok then, we assume its directly connected and move on */ |
| 1044 | select_source: |
| 1045 | if (neigh) |
| 1046 | gateway = ((struct dn_neigh *)neigh)->addr; |
| 1047 | if (gateway == 0) |
| 1048 | gateway = fl.fld_dst; |
| 1049 | if (fl.fld_src == 0) { |
| 1050 | fl.fld_src = dnet_select_source(dev_out, gateway, |
| 1051 | res.type == RTN_LOCAL ? |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1052 | RT_SCOPE_HOST : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | RT_SCOPE_LINK); |
| 1054 | if (fl.fld_src == 0 && res.type != RTN_LOCAL) |
| 1055 | goto e_addr; |
| 1056 | } |
| 1057 | fl.oif = dev_out->ifindex; |
| 1058 | goto make_route; |
| 1059 | } |
| 1060 | free_res = 1; |
| 1061 | |
| 1062 | if (res.type == RTN_NAT) |
| 1063 | goto e_inval; |
| 1064 | |
| 1065 | if (res.type == RTN_LOCAL) { |
| 1066 | if (!fl.fld_src) |
| 1067 | fl.fld_src = fl.fld_dst; |
| 1068 | if (dev_out) |
| 1069 | dev_put(dev_out); |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 1070 | dev_out = init_net.loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | dev_hold(dev_out); |
| 1072 | fl.oif = dev_out->ifindex; |
| 1073 | if (res.fi) |
| 1074 | dn_fib_info_put(res.fi); |
| 1075 | res.fi = NULL; |
| 1076 | goto make_route; |
| 1077 | } |
| 1078 | |
| 1079 | if (res.fi->fib_nhs > 1 && fl.oif == 0) |
| 1080 | dn_fib_select_multipath(&fl, &res); |
| 1081 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1082 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | * We could add some logic to deal with default routes here and |
| 1084 | * get rid of some of the special casing above. |
| 1085 | */ |
| 1086 | |
| 1087 | if (!fl.fld_src) |
| 1088 | fl.fld_src = DN_FIB_RES_PREFSRC(res); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | if (dev_out) |
| 1091 | dev_put(dev_out); |
| 1092 | dev_out = DN_FIB_RES_DEV(res); |
| 1093 | dev_hold(dev_out); |
| 1094 | fl.oif = dev_out->ifindex; |
| 1095 | gateway = DN_FIB_RES_GW(res); |
| 1096 | |
| 1097 | make_route: |
| 1098 | if (dev_out->flags & IFF_LOOPBACK) |
| 1099 | flags |= RTCF_LOCAL; |
| 1100 | |
| 1101 | rt = dst_alloc(&dn_dst_ops); |
| 1102 | if (rt == NULL) |
| 1103 | goto e_nobufs; |
| 1104 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1105 | atomic_set(&rt->dst.__refcnt, 1); |
| 1106 | rt->dst.flags = DST_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | |
| 1108 | rt->fl.fld_src = oldflp->fld_src; |
| 1109 | rt->fl.fld_dst = oldflp->fld_dst; |
| 1110 | rt->fl.oif = oldflp->oif; |
| 1111 | rt->fl.iif = 0; |
Thomas Graf | 47dcf0c | 2006-11-09 15:20:38 -0800 | [diff] [blame] | 1112 | rt->fl.mark = oldflp->mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | |
| 1114 | rt->rt_saddr = fl.fld_src; |
| 1115 | rt->rt_daddr = fl.fld_dst; |
| 1116 | rt->rt_gateway = gateway ? gateway : fl.fld_dst; |
| 1117 | rt->rt_local_src = fl.fld_src; |
| 1118 | |
| 1119 | rt->rt_dst_map = fl.fld_dst; |
| 1120 | rt->rt_src_map = fl.fld_src; |
| 1121 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1122 | rt->dst.dev = dev_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | dev_hold(dev_out); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1124 | rt->dst.neighbour = neigh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | neigh = NULL; |
| 1126 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1127 | rt->dst.lastuse = jiffies; |
| 1128 | rt->dst.output = dn_output; |
| 1129 | rt->dst.input = dn_rt_bug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | rt->rt_flags = flags; |
| 1131 | if (flags & RTCF_LOCAL) |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1132 | rt->dst.input = dn_nsp_rx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
| 1134 | err = dn_rt_set_next_hop(rt, &res); |
| 1135 | if (err) |
| 1136 | goto e_neighbour; |
| 1137 | |
| 1138 | hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst); |
| 1139 | dn_insert_route(rt, hash, (struct dn_route **)pprt); |
| 1140 | |
| 1141 | done: |
| 1142 | if (neigh) |
| 1143 | neigh_release(neigh); |
| 1144 | if (free_res) |
| 1145 | dn_fib_res_put(&res); |
| 1146 | if (dev_out) |
| 1147 | dev_put(dev_out); |
| 1148 | out: |
| 1149 | return err; |
| 1150 | |
| 1151 | e_addr: |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1152 | err = -EADDRNOTAVAIL; |
| 1153 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | e_inval: |
| 1155 | err = -EINVAL; |
| 1156 | goto done; |
| 1157 | e_nobufs: |
| 1158 | err = -ENOBUFS; |
| 1159 | goto done; |
| 1160 | e_neighbour: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1161 | dst_free(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | goto e_nobufs; |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | /* |
| 1167 | * N.B. The flags may be moved into the flowi at some future stage. |
| 1168 | */ |
| 1169 | static int __dn_route_output_key(struct dst_entry **pprt, const struct flowi *flp, int flags) |
| 1170 | { |
| 1171 | unsigned hash = dn_hash(flp->fld_src, flp->fld_dst); |
| 1172 | struct dn_route *rt = NULL; |
| 1173 | |
| 1174 | if (!(flags & MSG_TRYHARD)) { |
| 1175 | rcu_read_lock_bh(); |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 1176 | for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1177 | rt = rcu_dereference_bh(rt->dst.dn_next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | if ((flp->fld_dst == rt->fl.fld_dst) && |
| 1179 | (flp->fld_src == rt->fl.fld_src) && |
Thomas Graf | 47dcf0c | 2006-11-09 15:20:38 -0800 | [diff] [blame] | 1180 | (flp->mark == rt->fl.mark) && |
David S. Miller | c753796 | 2010-11-11 17:07:48 -0800 | [diff] [blame] | 1181 | dn_is_output_route(rt) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | (rt->fl.oif == flp->oif)) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1183 | dst_use(&rt->dst, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | rcu_read_unlock_bh(); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1185 | *pprt = &rt->dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
| 1188 | } |
| 1189 | rcu_read_unlock_bh(); |
| 1190 | } |
| 1191 | |
| 1192 | return dn_route_output_slow(pprt, flp, flags); |
| 1193 | } |
| 1194 | |
| 1195 | static int dn_route_output_key(struct dst_entry **pprt, struct flowi *flp, int flags) |
| 1196 | { |
| 1197 | int err; |
| 1198 | |
| 1199 | err = __dn_route_output_key(pprt, flp, flags); |
| 1200 | if (err == 0 && flp->proto) { |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 1201 | err = xfrm_lookup(&init_net, pprt, flp, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } |
| 1203 | return err; |
| 1204 | } |
| 1205 | |
| 1206 | int dn_route_output_sock(struct dst_entry **pprt, struct flowi *fl, struct sock *sk, int flags) |
| 1207 | { |
| 1208 | int err; |
| 1209 | |
| 1210 | err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD); |
| 1211 | if (err == 0 && fl->proto) { |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 1212 | err = xfrm_lookup(&init_net, pprt, fl, sk, |
| 1213 | (flags & MSG_DONTWAIT) ? 0 : XFRM_LOOKUP_WAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
| 1215 | return err; |
| 1216 | } |
| 1217 | |
| 1218 | static int dn_route_input_slow(struct sk_buff *skb) |
| 1219 | { |
| 1220 | struct dn_route *rt = NULL; |
| 1221 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 1222 | struct net_device *in_dev = skb->dev; |
| 1223 | struct net_device *out_dev = NULL; |
| 1224 | struct dn_dev *dn_db; |
| 1225 | struct neighbour *neigh = NULL; |
| 1226 | unsigned hash; |
| 1227 | int flags = 0; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 1228 | __le16 gateway = 0; |
| 1229 | __le16 local_src = 0; |
Changli Gao | 5811662 | 2010-11-12 18:43:55 +0000 | [diff] [blame] | 1230 | struct flowi fl = { .fld_dst = cb->dst, |
| 1231 | .fld_src = cb->src, |
| 1232 | .fld_scope = RT_SCOPE_UNIVERSE, |
Thomas Graf | 47dcf0c | 2006-11-09 15:20:38 -0800 | [diff] [blame] | 1233 | .mark = skb->mark, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | .iif = skb->dev->ifindex }; |
| 1235 | struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE }; |
| 1236 | int err = -EINVAL; |
| 1237 | int free_res = 0; |
| 1238 | |
| 1239 | dev_hold(in_dev); |
| 1240 | |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 1241 | if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | goto out; |
| 1243 | |
| 1244 | /* Zero source addresses are not allowed */ |
| 1245 | if (fl.fld_src == 0) |
| 1246 | goto out; |
| 1247 | |
| 1248 | /* |
| 1249 | * In this case we've just received a packet from a source |
| 1250 | * outside ourselves pretending to come from us. We don't |
| 1251 | * allow it any further to prevent routing loops, spoofing and |
| 1252 | * other nasties. Loopback packets already have the dst attached |
| 1253 | * so this only affects packets which have originated elsewhere. |
| 1254 | */ |
| 1255 | err = -ENOTUNIQ; |
| 1256 | if (dn_dev_islocal(in_dev, cb->src)) |
| 1257 | goto out; |
| 1258 | |
| 1259 | err = dn_fib_lookup(&fl, &res); |
| 1260 | if (err) { |
| 1261 | if (err != -ESRCH) |
| 1262 | goto out; |
| 1263 | /* |
| 1264 | * Is the destination us ? |
| 1265 | */ |
| 1266 | if (!dn_dev_islocal(in_dev, cb->dst)) |
| 1267 | goto e_inval; |
| 1268 | |
| 1269 | res.type = RTN_LOCAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | } else { |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 1271 | __le16 src_map = fl.fld_src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | free_res = 1; |
| 1273 | |
| 1274 | out_dev = DN_FIB_RES_DEV(res); |
| 1275 | if (out_dev == NULL) { |
| 1276 | if (net_ratelimit()) |
| 1277 | printk(KERN_CRIT "Bug in dn_route_input_slow() " |
| 1278 | "No output device\n"); |
| 1279 | goto e_inval; |
| 1280 | } |
| 1281 | dev_hold(out_dev); |
| 1282 | |
| 1283 | if (res.r) |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 1284 | src_map = fl.fld_src; /* no NAT support for now */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
| 1286 | gateway = DN_FIB_RES_GW(res); |
| 1287 | if (res.type == RTN_NAT) { |
| 1288 | fl.fld_dst = dn_fib_rules_map_destination(fl.fld_dst, &res); |
| 1289 | dn_fib_res_put(&res); |
| 1290 | free_res = 0; |
| 1291 | if (dn_fib_lookup(&fl, &res)) |
| 1292 | goto e_inval; |
| 1293 | free_res = 1; |
| 1294 | if (res.type != RTN_UNICAST) |
| 1295 | goto e_inval; |
| 1296 | flags |= RTCF_DNAT; |
| 1297 | gateway = fl.fld_dst; |
| 1298 | } |
| 1299 | fl.fld_src = src_map; |
| 1300 | } |
| 1301 | |
| 1302 | switch(res.type) { |
| 1303 | case RTN_UNICAST: |
| 1304 | /* |
| 1305 | * Forwarding check here, we only check for forwarding |
| 1306 | * being turned off, if you want to only forward intra |
| 1307 | * area, its up to you to set the routing tables up |
| 1308 | * correctly. |
| 1309 | */ |
| 1310 | if (dn_db->parms.forwarding == 0) |
| 1311 | goto e_inval; |
| 1312 | |
| 1313 | if (res.fi->fib_nhs > 1 && fl.oif == 0) |
| 1314 | dn_fib_select_multipath(&fl, &res); |
| 1315 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1316 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT |
| 1318 | * flag as a hint to set the intra-ethernet bit when |
| 1319 | * forwarding. If we've got NAT in operation, we don't do |
| 1320 | * this optimisation. |
| 1321 | */ |
| 1322 | if (out_dev == in_dev && !(flags & RTCF_NAT)) |
| 1323 | flags |= RTCF_DOREDIRECT; |
| 1324 | |
| 1325 | local_src = DN_FIB_RES_PREFSRC(res); |
| 1326 | |
| 1327 | case RTN_BLACKHOLE: |
| 1328 | case RTN_UNREACHABLE: |
| 1329 | break; |
| 1330 | case RTN_LOCAL: |
| 1331 | flags |= RTCF_LOCAL; |
| 1332 | fl.fld_src = cb->dst; |
| 1333 | fl.fld_dst = cb->src; |
| 1334 | |
| 1335 | /* Routing tables gave us a gateway */ |
| 1336 | if (gateway) |
| 1337 | goto make_route; |
| 1338 | |
| 1339 | /* Packet was intra-ethernet, so we know its on-link */ |
Steven Whitehouse | 3a31b9d | 2006-10-18 20:45:22 -0700 | [diff] [blame] | 1340 | if (cb->rt_flags & DN_RT_F_IE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | gateway = cb->src; |
| 1342 | flags |= RTCF_DIRECTSRC; |
| 1343 | goto make_route; |
| 1344 | } |
| 1345 | |
| 1346 | /* Use the default router if there is one */ |
| 1347 | neigh = neigh_clone(dn_db->router); |
| 1348 | if (neigh) { |
| 1349 | gateway = ((struct dn_neigh *)neigh)->addr; |
| 1350 | goto make_route; |
| 1351 | } |
| 1352 | |
| 1353 | /* Close eyes and pray */ |
| 1354 | gateway = cb->src; |
| 1355 | flags |= RTCF_DIRECTSRC; |
| 1356 | goto make_route; |
| 1357 | default: |
| 1358 | goto e_inval; |
| 1359 | } |
| 1360 | |
| 1361 | make_route: |
| 1362 | rt = dst_alloc(&dn_dst_ops); |
| 1363 | if (rt == NULL) |
| 1364 | goto e_nobufs; |
| 1365 | |
| 1366 | rt->rt_saddr = fl.fld_src; |
| 1367 | rt->rt_daddr = fl.fld_dst; |
| 1368 | rt->rt_gateway = fl.fld_dst; |
| 1369 | if (gateway) |
| 1370 | rt->rt_gateway = gateway; |
| 1371 | rt->rt_local_src = local_src ? local_src : rt->rt_saddr; |
| 1372 | |
| 1373 | rt->rt_dst_map = fl.fld_dst; |
| 1374 | rt->rt_src_map = fl.fld_src; |
| 1375 | |
| 1376 | rt->fl.fld_src = cb->src; |
| 1377 | rt->fl.fld_dst = cb->dst; |
| 1378 | rt->fl.oif = 0; |
| 1379 | rt->fl.iif = in_dev->ifindex; |
Thomas Graf | 47dcf0c | 2006-11-09 15:20:38 -0800 | [diff] [blame] | 1380 | rt->fl.mark = fl.mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1382 | rt->dst.flags = DST_HOST; |
| 1383 | rt->dst.neighbour = neigh; |
| 1384 | rt->dst.dev = out_dev; |
| 1385 | rt->dst.lastuse = jiffies; |
| 1386 | rt->dst.output = dn_rt_bug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | switch(res.type) { |
| 1388 | case RTN_UNICAST: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1389 | rt->dst.input = dn_forward; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | break; |
| 1391 | case RTN_LOCAL: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1392 | rt->dst.output = dn_output; |
| 1393 | rt->dst.input = dn_nsp_rx; |
| 1394 | rt->dst.dev = in_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | flags |= RTCF_LOCAL; |
| 1396 | break; |
| 1397 | default: |
| 1398 | case RTN_UNREACHABLE: |
| 1399 | case RTN_BLACKHOLE: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1400 | rt->dst.input = dst_discard; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } |
| 1402 | rt->rt_flags = flags; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1403 | if (rt->dst.dev) |
| 1404 | dev_hold(rt->dst.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
| 1406 | err = dn_rt_set_next_hop(rt, &res); |
| 1407 | if (err) |
| 1408 | goto e_neighbour; |
| 1409 | |
| 1410 | hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1411 | dn_insert_route(rt, hash, &rt); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1412 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | |
| 1414 | done: |
| 1415 | if (neigh) |
| 1416 | neigh_release(neigh); |
| 1417 | if (free_res) |
| 1418 | dn_fib_res_put(&res); |
| 1419 | dev_put(in_dev); |
| 1420 | if (out_dev) |
| 1421 | dev_put(out_dev); |
| 1422 | out: |
| 1423 | return err; |
| 1424 | |
| 1425 | e_inval: |
| 1426 | err = -EINVAL; |
| 1427 | goto done; |
| 1428 | |
| 1429 | e_nobufs: |
| 1430 | err = -ENOBUFS; |
| 1431 | goto done; |
| 1432 | |
| 1433 | e_neighbour: |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1434 | dst_free(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | goto done; |
| 1436 | } |
| 1437 | |
Roel Kluin | 5eaa65b | 2008-12-10 15:18:31 -0800 | [diff] [blame] | 1438 | static int dn_route_input(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | { |
| 1440 | struct dn_route *rt; |
| 1441 | struct dn_skb_cb *cb = DN_SKB_CB(skb); |
| 1442 | unsigned hash = dn_hash(cb->src, cb->dst); |
| 1443 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1444 | if (skb_dst(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | return 0; |
| 1446 | |
| 1447 | rcu_read_lock(); |
| 1448 | for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1449 | rt = rcu_dereference(rt->dst.dn_next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | if ((rt->fl.fld_src == cb->src) && |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1451 | (rt->fl.fld_dst == cb->dst) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | (rt->fl.oif == 0) && |
Thomas Graf | 47dcf0c | 2006-11-09 15:20:38 -0800 | [diff] [blame] | 1453 | (rt->fl.mark == skb->mark) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | (rt->fl.iif == cb->iif)) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1455 | dst_use(&rt->dst, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | rcu_read_unlock(); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1457 | skb_dst_set(skb, (struct dst_entry *)rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | return 0; |
| 1459 | } |
| 1460 | } |
| 1461 | rcu_read_unlock(); |
| 1462 | |
| 1463 | return dn_route_input_slow(skb); |
| 1464 | } |
| 1465 | |
Jamal Hadi Salim | b6544c0 | 2005-06-18 22:54:12 -0700 | [diff] [blame] | 1466 | static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq, |
| 1467 | int event, int nowait, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1469 | struct dn_route *rt = (struct dn_route *)skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | struct rtmsg *r; |
| 1471 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1472 | unsigned char *b = skb_tail_pointer(skb); |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 1473 | long expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | |
Jamal Hadi Salim | b6544c0 | 2005-06-18 22:54:12 -0700 | [diff] [blame] | 1475 | nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | r = NLMSG_DATA(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | r->rtm_family = AF_DECnet; |
| 1478 | r->rtm_dst_len = 16; |
| 1479 | r->rtm_src_len = 0; |
| 1480 | r->rtm_tos = 0; |
| 1481 | r->rtm_table = RT_TABLE_MAIN; |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 1482 | RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | r->rtm_type = rt->rt_type; |
| 1484 | r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED; |
| 1485 | r->rtm_scope = RT_SCOPE_UNIVERSE; |
| 1486 | r->rtm_protocol = RTPROT_UNSPEC; |
| 1487 | if (rt->rt_flags & RTCF_NOTIFY) |
| 1488 | r->rtm_flags |= RTM_F_NOTIFY; |
| 1489 | RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr); |
| 1490 | if (rt->fl.fld_src) { |
| 1491 | r->rtm_src_len = 16; |
| 1492 | RTA_PUT(skb, RTA_SRC, 2, &rt->fl.fld_src); |
| 1493 | } |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1494 | if (rt->dst.dev) |
| 1495 | RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | /* |
| 1497 | * Note to self - change this if input routes reverse direction when |
| 1498 | * they deal only with inputs and not with replies like they do |
| 1499 | * currently. |
| 1500 | */ |
| 1501 | RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src); |
| 1502 | if (rt->rt_daddr != rt->rt_gateway) |
| 1503 | RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway); |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame^] | 1504 | if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | goto rtattr_failure; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1506 | expires = rt->dst.expires ? rt->dst.expires - jiffies : 0; |
| 1507 | if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires, |
| 1508 | rt->dst.error) < 0) |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 1509 | goto rtattr_failure; |
David S. Miller | c753796 | 2010-11-11 17:07:48 -0800 | [diff] [blame] | 1510 | if (dn_is_input_route(rt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fl.iif); |
| 1512 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1513 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | return skb->len; |
| 1515 | |
| 1516 | nlmsg_failure: |
| 1517 | rtattr_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 1518 | nlmsg_trim(skb, b); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1519 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /* |
| 1523 | * This is called by both endnodes and routers now. |
| 1524 | */ |
Thomas Graf | fa34ddd | 2007-03-22 11:57:46 -0700 | [diff] [blame] | 1525 | static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1527 | struct net *net = sock_net(in_skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | struct rtattr **rta = arg; |
| 1529 | struct rtmsg *rtm = NLMSG_DATA(nlh); |
| 1530 | struct dn_route *rt = NULL; |
| 1531 | struct dn_skb_cb *cb; |
| 1532 | int err; |
| 1533 | struct sk_buff *skb; |
| 1534 | struct flowi fl; |
| 1535 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 1536 | if (!net_eq(net, &init_net)) |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 1537 | return -EINVAL; |
| 1538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | memset(&fl, 0, sizeof(fl)); |
| 1540 | fl.proto = DNPROTO_NSP; |
| 1541 | |
| 1542 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1543 | if (skb == NULL) |
| 1544 | return -ENOBUFS; |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 1545 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | cb = DN_SKB_CB(skb); |
| 1547 | |
| 1548 | if (rta[RTA_SRC-1]) |
| 1549 | memcpy(&fl.fld_src, RTA_DATA(rta[RTA_SRC-1]), 2); |
| 1550 | if (rta[RTA_DST-1]) |
| 1551 | memcpy(&fl.fld_dst, RTA_DATA(rta[RTA_DST-1]), 2); |
| 1552 | if (rta[RTA_IIF-1]) |
| 1553 | memcpy(&fl.iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int)); |
| 1554 | |
| 1555 | if (fl.iif) { |
| 1556 | struct net_device *dev; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1557 | if ((dev = dev_get_by_index(&init_net, fl.iif)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | kfree_skb(skb); |
| 1559 | return -ENODEV; |
| 1560 | } |
| 1561 | if (!dev->dn_ptr) { |
| 1562 | dev_put(dev); |
| 1563 | kfree_skb(skb); |
| 1564 | return -ENODEV; |
| 1565 | } |
YOSHIFUJI Hideaki | b98999d | 2007-12-12 03:51:49 +0900 | [diff] [blame] | 1566 | skb->protocol = htons(ETH_P_DNA_RT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | skb->dev = dev; |
| 1568 | cb->src = fl.fld_src; |
| 1569 | cb->dst = fl.fld_dst; |
| 1570 | local_bh_disable(); |
| 1571 | err = dn_route_input(skb); |
| 1572 | local_bh_enable(); |
| 1573 | memset(cb, 0, sizeof(struct dn_skb_cb)); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1574 | rt = (struct dn_route *)skb_dst(skb); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1575 | if (!err && -rt->dst.error) |
| 1576 | err = rt->dst.error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | } else { |
| 1578 | int oif = 0; |
| 1579 | if (rta[RTA_OIF - 1]) |
| 1580 | memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int)); |
| 1581 | fl.oif = oif; |
| 1582 | err = dn_route_output_key((struct dst_entry **)&rt, &fl, 0); |
| 1583 | } |
| 1584 | |
| 1585 | if (skb->dev) |
| 1586 | dev_put(skb->dev); |
| 1587 | skb->dev = NULL; |
| 1588 | if (err) |
| 1589 | goto out_free; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1590 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | if (rtm->rtm_flags & RTM_F_NOTIFY) |
| 1592 | rt->rt_flags |= RTCF_NOTIFY; |
| 1593 | |
Jamal Hadi Salim | b6544c0 | 2005-06-18 22:54:12 -0700 | [diff] [blame] | 1594 | err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, RTM_NEWROUTE, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | |
| 1596 | if (err == 0) |
| 1597 | goto out_free; |
| 1598 | if (err < 0) { |
| 1599 | err = -EMSGSIZE; |
| 1600 | goto out_free; |
| 1601 | } |
| 1602 | |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 1603 | return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
| 1605 | out_free: |
| 1606 | kfree_skb(skb); |
| 1607 | return err; |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | * For routers, this is called from dn_fib_dump, but for endnodes its |
| 1612 | * called directly from the rtnetlink dispatch table. |
| 1613 | */ |
| 1614 | int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 1615 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1616 | struct net *net = sock_net(skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | struct dn_route *rt; |
| 1618 | int h, s_h; |
| 1619 | int idx, s_idx; |
| 1620 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 1621 | if (!net_eq(net, &init_net)) |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 1622 | return 0; |
| 1623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg)) |
| 1625 | return -EINVAL; |
| 1626 | if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)) |
| 1627 | return 0; |
| 1628 | |
| 1629 | s_h = cb->args[0]; |
| 1630 | s_idx = idx = cb->args[1]; |
| 1631 | for(h = 0; h <= dn_rt_hash_mask; h++) { |
| 1632 | if (h < s_h) |
| 1633 | continue; |
| 1634 | if (h > s_h) |
| 1635 | s_idx = 0; |
| 1636 | rcu_read_lock_bh(); |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 1637 | for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | rt; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1639 | rt = rcu_dereference_bh(rt->dst.dn_next), idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | if (idx < s_idx) |
| 1641 | continue; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1642 | skb_dst_set(skb, dst_clone(&rt->dst)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid, |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1644 | cb->nlh->nlmsg_seq, RTM_NEWROUTE, |
Jamal Hadi Salim | b6544c0 | 2005-06-18 22:54:12 -0700 | [diff] [blame] | 1645 | 1, NLM_F_MULTI) <= 0) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1646 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | rcu_read_unlock_bh(); |
| 1648 | goto done; |
| 1649 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1650 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | } |
| 1652 | rcu_read_unlock_bh(); |
| 1653 | } |
| 1654 | |
| 1655 | done: |
| 1656 | cb->args[0] = h; |
| 1657 | cb->args[1] = idx; |
| 1658 | return skb->len; |
| 1659 | } |
| 1660 | |
| 1661 | #ifdef CONFIG_PROC_FS |
| 1662 | struct dn_rt_cache_iter_state { |
| 1663 | int bucket; |
| 1664 | }; |
| 1665 | |
| 1666 | static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq) |
| 1667 | { |
| 1668 | struct dn_route *rt = NULL; |
| 1669 | struct dn_rt_cache_iter_state *s = seq->private; |
| 1670 | |
| 1671 | for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) { |
| 1672 | rcu_read_lock_bh(); |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 1673 | rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | if (rt) |
| 1675 | break; |
| 1676 | rcu_read_unlock_bh(); |
| 1677 | } |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 1678 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt) |
| 1682 | { |
Eric Dumazet | 0d89d79 | 2008-01-10 22:35:21 -0800 | [diff] [blame] | 1683 | struct dn_rt_cache_iter_state *s = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 1685 | rt = rcu_dereference_bh(rt->dst.dn_next); |
| 1686 | while (!rt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | rcu_read_unlock_bh(); |
| 1688 | if (--s->bucket < 0) |
| 1689 | break; |
| 1690 | rcu_read_lock_bh(); |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 1691 | rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | } |
Eric Dumazet | fc766e4c | 2010-10-29 03:09:24 +0000 | [diff] [blame] | 1693 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos) |
| 1697 | { |
| 1698 | struct dn_route *rt = dn_rt_cache_get_first(seq); |
| 1699 | |
| 1700 | if (rt) { |
| 1701 | while(*pos && (rt = dn_rt_cache_get_next(seq, rt))) |
| 1702 | --*pos; |
| 1703 | } |
| 1704 | return *pos ? NULL : rt; |
| 1705 | } |
| 1706 | |
| 1707 | static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1708 | { |
| 1709 | struct dn_route *rt = dn_rt_cache_get_next(seq, v); |
| 1710 | ++*pos; |
| 1711 | return rt; |
| 1712 | } |
| 1713 | |
| 1714 | static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v) |
| 1715 | { |
| 1716 | if (v) |
| 1717 | rcu_read_unlock_bh(); |
| 1718 | } |
| 1719 | |
| 1720 | static int dn_rt_cache_seq_show(struct seq_file *seq, void *v) |
| 1721 | { |
| 1722 | struct dn_route *rt = v; |
| 1723 | char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN]; |
| 1724 | |
| 1725 | seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n", |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1726 | rt->dst.dev ? rt->dst.dev->name : "*", |
Harvey Harrison | c4106aa | 2008-11-27 00:12:47 -0800 | [diff] [blame] | 1727 | dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1), |
| 1728 | dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2), |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1729 | atomic_read(&rt->dst.__refcnt), |
| 1730 | rt->dst.__use, |
| 1731 | (int) dst_metric(&rt->dst, RTAX_RTT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | return 0; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 1735 | static const struct seq_operations dn_rt_cache_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | .start = dn_rt_cache_seq_start, |
| 1737 | .next = dn_rt_cache_seq_next, |
| 1738 | .stop = dn_rt_cache_seq_stop, |
| 1739 | .show = dn_rt_cache_seq_show, |
| 1740 | }; |
| 1741 | |
| 1742 | static int dn_rt_cache_seq_open(struct inode *inode, struct file *file) |
| 1743 | { |
Pavel Emelyanov | 3116408 | 2007-10-10 02:30:23 -0700 | [diff] [blame] | 1744 | return seq_open_private(file, &dn_rt_cache_seq_ops, |
| 1745 | sizeof(struct dn_rt_cache_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1748 | static const struct file_operations dn_rt_cache_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | .owner = THIS_MODULE, |
| 1750 | .open = dn_rt_cache_seq_open, |
| 1751 | .read = seq_read, |
| 1752 | .llseek = seq_lseek, |
| 1753 | .release = seq_release_private, |
| 1754 | }; |
| 1755 | |
| 1756 | #endif /* CONFIG_PROC_FS */ |
| 1757 | |
| 1758 | void __init dn_route_init(void) |
| 1759 | { |
| 1760 | int i, goal, order; |
| 1761 | |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 1762 | dn_dst_ops.kmem_cachep = |
| 1763 | kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1764 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1765 | dst_entries_init(&dn_dst_ops); |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1766 | setup_timer(&dn_route_timer, dn_dst_check_expire, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ; |
| 1768 | add_timer(&dn_route_timer); |
| 1769 | |
Jan Beulich | 4481374 | 2009-09-21 17:03:05 -0700 | [diff] [blame] | 1770 | goal = totalram_pages >> (26 - PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
| 1772 | for(order = 0; (1UL << order) < goal; order++) |
| 1773 | /* NOTHING */; |
| 1774 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1775 | /* |
| 1776 | * Only want 1024 entries max, since the table is very, very unlikely |
| 1777 | * to be larger than that. |
| 1778 | */ |
| 1779 | while(order && ((((1UL << order) * PAGE_SIZE) / |
| 1780 | sizeof(struct dn_rt_hash_bucket)) >= 2048)) |
| 1781 | order--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1783 | do { |
| 1784 | dn_rt_hash_mask = (1UL << order) * PAGE_SIZE / |
| 1785 | sizeof(struct dn_rt_hash_bucket); |
| 1786 | while(dn_rt_hash_mask & (dn_rt_hash_mask - 1)) |
| 1787 | dn_rt_hash_mask--; |
| 1788 | dn_rt_hash_table = (struct dn_rt_hash_bucket *) |
| 1789 | __get_free_pages(GFP_ATOMIC, order); |
| 1790 | } while (dn_rt_hash_table == NULL && --order > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | |
| 1792 | if (!dn_rt_hash_table) |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1793 | panic("Failed to allocate DECnet route cache hash table\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1795 | printk(KERN_INFO |
| 1796 | "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n", |
| 1797 | dn_rt_hash_mask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024); |
| 1799 | |
| 1800 | dn_rt_hash_mask--; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1801 | for(i = 0; i <= dn_rt_hash_mask; i++) { |
| 1802 | spin_lock_init(&dn_rt_hash_table[i].lock); |
| 1803 | dn_rt_hash_table[i].chain = NULL; |
| 1804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1806 | dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1808 | proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops); |
Thomas Graf | fa34ddd | 2007-03-22 11:57:46 -0700 | [diff] [blame] | 1809 | |
| 1810 | #ifdef CONFIG_DECNET_ROUTER |
| 1811 | rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, dn_fib_dump); |
| 1812 | #else |
| 1813 | rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, |
| 1814 | dn_cache_dump); |
| 1815 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | void __exit dn_route_cleanup(void) |
| 1819 | { |
| 1820 | del_timer(&dn_route_timer); |
| 1821 | dn_run_flush(0); |
| 1822 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1823 | proc_net_remove(&init_net, "decnet_cache"); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1824 | dst_entries_destroy(&dn_dst_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |