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