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