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