blob: 6e74b3f110bcdd7980aeda4387f4b51894449c5e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hideaki429eb0f2007-02-09 23:24:40 +090046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 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 Torvalds1da177e2005-04-16 15:20:36 -070058#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 Heo5a0e3ad2010-03-24 17:04:11 +090069#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040080#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <asm/errno.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020082#include <net/net_namespace.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070083#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <net/neighbour.h>
85#include <net/dst.h>
86#include <net/flow.h>
Steven Whitehousea8731cb2006-08-09 15:56:46 -070087#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <net/dn.h>
89#include <net/dn_dev.h>
90#include <net/dn_nsp.h>
91#include <net/dn_route.h>
92#include <net/dn_neigh.h>
93#include <net/dn_fib.h>
94
95struct dn_rt_hash_bucket
96{
Eric Dumazetfc766e4c2010-10-29 03:09:24 +000097 struct dn_route __rcu *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 spinlock_t lock;
Eric Dumazetfca09fb2008-02-07 23:29:57 -080099};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101extern struct neigh_table dn_neigh_table;
102
103
104static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};
105
106static const int dn_rt_min_delay = 2 * HZ;
107static const int dn_rt_max_delay = 10 * HZ;
108static const int dn_rt_mtu_expires = 10 * 60 * HZ;
109
110static unsigned long dn_rt_deadline;
111
Daniel Lezcano569d3642008-01-18 03:56:57 -0800112static int dn_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);
David S. Miller0dbaee32010-12-13 12:52:14 -0800114static unsigned int dn_dst_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +0000115static unsigned int dn_dst_mtu(const struct dst_entry *dst);
David S. Miller62fa8a82011-01-26 20:51:05 -0800116static void dn_dst_destroy(struct dst_entry *);
David S. Millerfccd7d52012-07-02 22:22:18 -0700117static void dn_dst_ifdown(struct dst_entry *, struct net_device *dev, int how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
119static void dn_dst_link_failure(struct sk_buff *);
120static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu);
David S. Millerf894cbf2012-07-02 21:52:24 -0700121static struct neighbour *dn_dst_neigh_lookup(const struct dst_entry *dst,
122 struct sk_buff *skb,
123 const void *daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static int dn_route_input(struct sk_buff *);
125static void dn_run_flush(unsigned long dummy);
126
127static struct dn_rt_hash_bucket *dn_rt_hash_table;
Eric Dumazet95c96172012-04-15 05:58:06 +0000128static unsigned int dn_rt_hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130static struct timer_list dn_route_timer;
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700131static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132int decnet_dst_gc_interval = 2;
133
134static struct dst_ops dn_dst_ops = {
135 .family = PF_DECnet,
Harvey Harrison09640e62009-02-01 00:45:17 -0800136 .protocol = cpu_to_be16(ETH_P_DNA_RT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .gc_thresh = 128,
138 .gc = dn_dst_gc,
139 .check = dn_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800140 .default_advmss = dn_dst_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000141 .mtu = dn_dst_mtu,
David S. Miller62fa8a82011-01-26 20:51:05 -0800142 .cow_metrics = dst_cow_metrics_generic,
143 .destroy = dn_dst_destroy,
David S. Millerfccd7d52012-07-02 22:22:18 -0700144 .ifdown = dn_dst_ifdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .negative_advice = dn_dst_negative_advice,
146 .link_failure = dn_dst_link_failure,
147 .update_pmtu = dn_dst_update_pmtu,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700148 .neigh_lookup = dn_dst_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
David S. Miller62fa8a82011-01-26 20:51:05 -0800151static void dn_dst_destroy(struct dst_entry *dst)
152{
David S. Millerfccd7d52012-07-02 22:22:18 -0700153 struct dn_route *rt = (struct dn_route *) dst;
154
155 if (rt->n)
156 neigh_release(rt->n);
David S. Miller62fa8a82011-01-26 20:51:05 -0800157 dst_destroy_metrics_generic(dst);
158}
159
David S. Millerfccd7d52012-07-02 22:22:18 -0700160static void dn_dst_ifdown(struct dst_entry *dst, struct net_device *dev, int how)
161{
162 if (how) {
163 struct dn_route *rt = (struct dn_route *) dst;
164 struct neighbour *n = rt->n;
165
166 if (n && n->dev == dev) {
167 n->dev = dev_net(dev)->loopback_dev;
168 dev_hold(n->dev);
169 dev_put(dev);
170 }
171 }
172}
173
Eric Dumazet95c96172012-04-15 05:58:06 +0000174static __inline__ unsigned int dn_hash(__le16 src, __le16 dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800176 __u16 tmp = (__u16 __force)(src ^ dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 tmp ^= (tmp >> 3);
178 tmp ^= (tmp >> 5);
179 tmp ^= (tmp >> 10);
Eric Dumazet95c96172012-04-15 05:58:06 +0000180 return dn_rt_hash_mask & (unsigned int)tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183static inline void dnrt_free(struct dn_route *rt)
184{
Changli Gaod8d1f302010-06-10 23:31:35 -0700185 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188static inline void dnrt_drop(struct dn_route *rt)
189{
Changli Gaod8d1f302010-06-10 23:31:35 -0700190 dst_release(&rt->dst);
191 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194static void dn_dst_check_expire(unsigned long dummy)
195{
196 int i;
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000197 struct dn_route *rt;
198 struct dn_route __rcu **rtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned long now = jiffies;
200 unsigned long expire = 120 * HZ;
201
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000202 for (i = 0; i <= dn_rt_hash_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 rtp = &dn_rt_hash_table[i].chain;
204
205 spin_lock(&dn_rt_hash_table[i].lock);
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000206 while ((rt = rcu_dereference_protected(*rtp,
207 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700208 if (atomic_read(&rt->dst.__refcnt) ||
209 (now - rt->dst.lastuse) < expire) {
210 rtp = &rt->dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 continue;
212 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700213 *rtp = rt->dst.dn_next;
214 rt->dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dnrt_free(rt);
216 }
217 spin_unlock(&dn_rt_hash_table[i].lock);
218
219 if ((jiffies - now) > 0)
220 break;
221 }
222
223 mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
224}
225
Daniel Lezcano569d3642008-01-18 03:56:57 -0800226static int dn_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000228 struct dn_route *rt;
229 struct dn_route __rcu **rtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int i;
231 unsigned long now = jiffies;
232 unsigned long expire = 10 * HZ;
233
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000234 for (i = 0; i <= dn_rt_hash_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 spin_lock_bh(&dn_rt_hash_table[i].lock);
237 rtp = &dn_rt_hash_table[i].chain;
238
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000239 while ((rt = rcu_dereference_protected(*rtp,
240 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700241 if (atomic_read(&rt->dst.__refcnt) ||
242 (now - rt->dst.lastuse) < expire) {
243 rtp = &rt->dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 continue;
245 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700246 *rtp = rt->dst.dn_next;
247 rt->dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dnrt_drop(rt);
249 break;
250 }
251 spin_unlock_bh(&dn_rt_hash_table[i].lock);
252 }
253
254 return 0;
255}
256
257/*
258 * The decnet standards don't impose a particular minimum mtu, what they
259 * do insist on is that the routing layer accepts a datagram of at least
260 * 230 bytes long. Here we have to subtract the routing header length from
261 * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
262 * assume the worst and use a long header size.
263 *
264 * We update both the mtu and the advertised mss (i.e. the segment size we
265 * advertise to the other end).
266 */
267static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu)
268{
David S. Millerfccd7d52012-07-02 22:22:18 -0700269 struct dn_route *rt = (struct dn_route *) dst;
270 struct neighbour *n = rt->n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 u32 min_mtu = 230;
David S. Miller69cce1d2011-07-17 23:09:49 -0700272 struct dn_dev *dn;
273
274 dn = n ? rcu_dereference_raw(n->dev->dn_ptr) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 if (dn && dn->use_long == 0)
277 min_mtu -= 6;
278 else
279 min_mtu -= 21;
280
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700281 if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!(dst_metric_locked(dst, RTAX_MTU))) {
David S. Millerdefb3512010-12-08 21:16:57 -0800283 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 dst_set_expires(dst, dn_rt_mtu_expires);
285 }
286 if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
287 u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
David S. Miller0dbaee32010-12-13 12:52:14 -0800288 u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
289 if (!existing_mss || existing_mss > mss)
David S. Millerdefb3512010-12-08 21:16:57 -0800290 dst_metric_set(dst, RTAX_ADVMSS, mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 }
293}
294
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900295/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * When a route has been marked obsolete. (e.g. routing cache flush)
297 */
298static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
299{
300 return NULL;
301}
302
303static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
304{
305 dst_release(dst);
306 return NULL;
307}
308
309static void dn_dst_link_failure(struct sk_buff *skb)
310{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
David S. Millerbef55ae2011-03-12 17:17:10 -0500313static inline int compare_keys(struct flowidn *fl1, struct flowidn *fl2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
David S. Millerbef55ae2011-03-12 17:17:10 -0500315 return ((fl1->daddr ^ fl2->daddr) |
316 (fl1->saddr ^ fl2->saddr) |
317 (fl1->flowidn_mark ^ fl2->flowidn_mark) |
318 (fl1->flowidn_scope ^ fl2->flowidn_scope) |
319 (fl1->flowidn_oif ^ fl2->flowidn_oif) |
320 (fl1->flowidn_iif ^ fl2->flowidn_iif)) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Eric Dumazet95c96172012-04-15 05:58:06 +0000323static int dn_insert_route(struct dn_route *rt, unsigned int hash, struct dn_route **rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000325 struct dn_route *rth;
326 struct dn_route __rcu **rthp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned long now = jiffies;
328
329 rthp = &dn_rt_hash_table[hash].chain;
330
331 spin_lock_bh(&dn_rt_hash_table[hash].lock);
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000332 while ((rth = rcu_dereference_protected(*rthp,
333 lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
David S. Millerbef55ae2011-03-12 17:17:10 -0500334 if (compare_keys(&rth->fld, &rt->fld)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* Put it first */
Changli Gaod8d1f302010-06-10 23:31:35 -0700336 *rthp = rth->dst.dn_next;
337 rcu_assign_pointer(rth->dst.dn_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dn_rt_hash_table[hash].chain);
339 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
340
Changli Gaod8d1f302010-06-10 23:31:35 -0700341 dst_use(&rth->dst, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
343
344 dnrt_drop(rt);
345 *rp = rth;
346 return 0;
347 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700348 rthp = &rth->dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
Changli Gaod8d1f302010-06-10 23:31:35 -0700351 rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900353
Changli Gaod8d1f302010-06-10 23:31:35 -0700354 dst_use(&rt->dst, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
356 *rp = rt;
357 return 0;
358}
359
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800360static void dn_run_flush(unsigned long dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 int i;
363 struct dn_route *rt, *next;
364
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000365 for (i = 0; i < dn_rt_hash_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 spin_lock_bh(&dn_rt_hash_table[i].lock);
367
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000368 if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto nothing_to_declare;
370
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000371 for(; rt; rt = next) {
372 next = rcu_dereference_raw(rt->dst.dn_next);
373 RCU_INIT_POINTER(rt->dst.dn_next, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 dst_free((struct dst_entry *)rt);
375 }
376
377nothing_to_declare:
378 spin_unlock_bh(&dn_rt_hash_table[i].lock);
379 }
380}
381
382static DEFINE_SPINLOCK(dn_rt_flush_lock);
383
384void dn_rt_cache_flush(int delay)
385{
386 unsigned long now = jiffies;
387 int user_mode = !in_interrupt();
388
389 if (delay < 0)
390 delay = dn_rt_min_delay;
391
392 spin_lock_bh(&dn_rt_flush_lock);
393
394 if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
395 long tmo = (long)(dn_rt_deadline - now);
396
397 if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
398 tmo = 0;
399
400 if (delay > tmo)
401 delay = tmo;
402 }
403
404 if (delay <= 0) {
405 spin_unlock_bh(&dn_rt_flush_lock);
406 dn_run_flush(0);
407 return;
408 }
409
410 if (dn_rt_deadline == 0)
411 dn_rt_deadline = now + dn_rt_max_delay;
412
413 dn_rt_flush_timer.expires = now + delay;
414 add_timer(&dn_rt_flush_timer);
415 spin_unlock_bh(&dn_rt_flush_lock);
416}
417
418/**
419 * dn_return_short - Return a short packet to its sender
420 * @skb: The packet to return
421 *
422 */
423static int dn_return_short(struct sk_buff *skb)
424{
425 struct dn_skb_cb *cb;
426 unsigned char *ptr;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800427 __le16 *src;
428 __le16 *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* Add back headers */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700431 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
434 return NET_RX_DROP;
435
436 cb = DN_SKB_CB(skb);
437 /* Skip packet length and point to flags */
438 ptr = skb->data + 2;
439 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
440
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800441 dst = (__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800443 src = (__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 ptr += 2;
445 *ptr = 0; /* Zero hop count */
446
Ilpo Järvinena0bffff2009-03-21 13:36:17 -0700447 swap(*src, *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 skb->pkt_type = PACKET_OUTGOING;
450 dn_rt_finish_output(skb, NULL, NULL);
451 return NET_RX_SUCCESS;
452}
453
454/**
455 * dn_return_long - Return a long packet to its sender
456 * @skb: The long format packet to return
457 *
458 */
459static int dn_return_long(struct sk_buff *skb)
460{
461 struct dn_skb_cb *cb;
462 unsigned char *ptr;
463 unsigned char *src_addr, *dst_addr;
464 unsigned char tmp[ETH_ALEN];
465
466 /* Add back all headers */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700467 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
470 return NET_RX_DROP;
471
472 cb = DN_SKB_CB(skb);
473 /* Ignore packet length and point to flags */
474 ptr = skb->data + 2;
475
476 /* Skip padding */
477 if (*ptr & DN_RT_F_PF) {
478 char padlen = (*ptr & ~DN_RT_F_PF);
479 ptr += padlen;
480 }
481
482 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
483 ptr += 2;
484 dst_addr = ptr;
485 ptr += 8;
486 src_addr = ptr;
487 ptr += 6;
488 *ptr = 0; /* Zero hop count */
489
490 /* Swap source and destination */
491 memcpy(tmp, src_addr, ETH_ALEN);
492 memcpy(src_addr, dst_addr, ETH_ALEN);
493 memcpy(dst_addr, tmp, ETH_ALEN);
494
495 skb->pkt_type = PACKET_OUTGOING;
496 dn_rt_finish_output(skb, dst_addr, src_addr);
497 return NET_RX_SUCCESS;
498}
499
500/**
501 * dn_route_rx_packet - Try and find a route for an incoming packet
502 * @skb: The packet to find a route for
503 *
504 * Returns: result of input function if route is found, error code otherwise
505 */
506static int dn_route_rx_packet(struct sk_buff *skb)
507{
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000508 struct dn_skb_cb *cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int err;
510
511 if ((err = dn_route_input(skb)) == 0)
512 return dst_input(skb);
513
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000514 cb = DN_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (decnet_debug_level & 4) {
516 char *devname = skb->dev ? skb->dev->name : "???";
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 printk(KERN_DEBUG
519 "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 Whitehousec4ea94a2006-03-20 22:42:39 -0800520 (int)cb->rt_flags, devname, skb->len,
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800521 le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 err, skb->pkt_type);
523 }
524
525 if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
Joe Perches06f8fe12011-07-01 09:43:03 +0000526 switch (cb->rt_flags & DN_RT_PKT_MSK) {
527 case DN_RT_PKT_SHORT:
528 return dn_return_short(skb);
529 case DN_RT_PKT_LONG:
530 return dn_return_long(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 }
533
534 kfree_skb(skb);
535 return NET_RX_DROP;
536}
537
538static int dn_route_rx_long(struct sk_buff *skb)
539{
540 struct dn_skb_cb *cb = DN_SKB_CB(skb);
541 unsigned char *ptr = skb->data;
542
543 if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
544 goto drop_it;
545
546 skb_pull(skb, 20);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300547 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900549 /* Destination info */
550 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800551 cb->dst = dn_eth2dn(ptr);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900552 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
553 goto drop_it;
554 ptr += 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900557 /* Source info */
558 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800559 cb->src = dn_eth2dn(ptr);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900560 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
561 goto drop_it;
562 ptr += 6;
563 /* Other junk */
564 ptr++;
565 cb->hops = *ptr++; /* Visit Count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100567 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
568 dn_route_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570drop_it:
571 kfree_skb(skb);
572 return NET_RX_DROP;
573}
574
575
576
577static int dn_route_rx_short(struct sk_buff *skb)
578{
579 struct dn_skb_cb *cb = DN_SKB_CB(skb);
580 unsigned char *ptr = skb->data;
581
582 if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
583 goto drop_it;
584
585 skb_pull(skb, 5);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300586 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800588 cb->dst = *(__le16 *)ptr;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900589 ptr += 2;
590 cb->src = *(__le16 *)ptr;
591 ptr += 2;
592 cb->hops = *ptr & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100594 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
595 dn_route_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597drop_it:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900598 kfree_skb(skb);
599 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602static int dn_route_discard(struct sk_buff *skb)
603{
604 /*
605 * I know we drop the packet here, but thats considered success in
606 * this case
607 */
608 kfree_skb(skb);
609 return NET_RX_SUCCESS;
610}
611
612static int dn_route_ptp_hello(struct sk_buff *skb)
613{
614 dn_dev_hello(skb);
615 dn_neigh_pointopoint_hello(skb);
616 return NET_RX_SUCCESS;
617}
618
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700619int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct dn_skb_cb *cb;
622 unsigned char flags = 0;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800623 __u16 len = le16_to_cpu(*(__le16 *)skb->data);
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000624 struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned char padlen = 0;
626
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700627 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700628 goto dump_it;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (dn == NULL)
631 goto dump_it;
632
633 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
634 goto out;
635
636 if (!pskb_may_pull(skb, 3))
637 goto dump_it;
638
639 skb_pull(skb, 2);
640
641 if (len > skb->len)
642 goto dump_it;
643
644 skb_trim(skb, len);
645
646 flags = *skb->data;
647
648 cb = DN_SKB_CB(skb);
649 cb->stamp = jiffies;
650 cb->iif = dev->ifindex;
651
652 /*
653 * If we have padding, remove it.
654 */
655 if (flags & DN_RT_F_PF) {
656 padlen = flags & ~DN_RT_F_PF;
657 if (!pskb_may_pull(skb, padlen + 1))
658 goto dump_it;
659 skb_pull(skb, padlen);
660 flags = *skb->data;
661 }
662
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700663 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /*
666 * Weed out future version DECnet
667 */
668 if (flags & DN_RT_F_VER)
669 goto dump_it;
670
671 cb->rt_flags = flags;
672
673 if (decnet_debug_level & 1)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900674 printk(KERN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900676 (int)flags, (dev) ? dev->name : "???", len, skb->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 padlen);
678
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900679 if (flags & DN_RT_PKT_CNTL) {
Herbert Xu364c6ba2006-06-09 16:10:40 -0700680 if (unlikely(skb_linearize(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto dump_it;
682
Joe Perches06f8fe12011-07-01 09:43:03 +0000683 switch (flags & DN_RT_CNTL_MSK) {
684 case DN_RT_PKT_INIT:
685 dn_dev_init_pkt(skb);
686 break;
687 case DN_RT_PKT_VERI:
688 dn_dev_veri_pkt(skb);
689 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
692 if (dn->parms.state != DN_DEV_S_RU)
693 goto dump_it;
694
Joe Perches06f8fe12011-07-01 09:43:03 +0000695 switch (flags & DN_RT_CNTL_MSK) {
696 case DN_RT_PKT_HELO:
697 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
698 skb, skb->dev, NULL,
699 dn_route_ptp_hello);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Joe Perches06f8fe12011-07-01 09:43:03 +0000701 case DN_RT_PKT_L1RT:
702 case DN_RT_PKT_L2RT:
703 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
704 skb, skb->dev, NULL,
705 dn_route_discard);
706 case DN_RT_PKT_ERTH:
707 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
708 skb, skb->dev, NULL,
709 dn_neigh_router_hello);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Joe Perches06f8fe12011-07-01 09:43:03 +0000711 case DN_RT_PKT_EEDH:
712 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
713 skb, skb->dev, NULL,
714 dn_neigh_endnode_hello);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900715 }
716 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (dn->parms.state != DN_DEV_S_RU)
718 goto dump_it;
719
720 skb_pull(skb, 1); /* Pull flags */
721
Joe Perches06f8fe12011-07-01 09:43:03 +0000722 switch (flags & DN_RT_PKT_MSK) {
723 case DN_RT_PKT_LONG:
724 return dn_route_rx_long(skb);
725 case DN_RT_PKT_SHORT:
726 return dn_route_rx_short(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730dump_it:
731 kfree_skb(skb);
732out:
733 return NET_RX_DROP;
734}
735
David S. Miller8f40b162011-07-17 13:34:11 -0700736static int dn_to_neigh_output(struct sk_buff *skb)
737{
738 struct dst_entry *dst = skb_dst(skb);
David S. Millerfccd7d52012-07-02 22:22:18 -0700739 struct dn_route *rt = (struct dn_route *) dst;
740 struct neighbour *n = rt->n;
David S. Miller8f40b162011-07-17 13:34:11 -0700741
742 return n->output(n, skb);
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745static int dn_output(struct sk_buff *skb)
746{
Eric Dumazetadf30902009-06-02 05:19:30 +0000747 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct dn_route *rt = (struct dn_route *)dst;
749 struct net_device *dev = dst->dev;
750 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 int err = -EINVAL;
753
David S. Millerfccd7d52012-07-02 22:22:18 -0700754 if (rt->n == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 goto error;
756
757 skb->dev = dev;
758
759 cb->src = rt->rt_saddr;
760 cb->dst = rt->rt_daddr;
761
762 /*
763 * Always set the Intra-Ethernet bit on all outgoing packets
764 * originated on this node. Only valid flag from upper layers
765 * is return-to-sender-requested. Set hop count to 0 too.
766 */
767 cb->rt_flags &= ~DN_RT_F_RQR;
768 cb->rt_flags |= DN_RT_F_IE;
769 cb->hops = 0;
770
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100771 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
David S. Miller8f40b162011-07-17 13:34:11 -0700772 dn_to_neigh_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774error:
Joe Perchese87cc472012-05-13 21:56:26 +0000775 net_dbg_ratelimited("dn_output: This should not happen\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 kfree_skb(skb);
778
779 return err;
780}
781
782static int dn_forward(struct sk_buff *skb)
783{
784 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000785 struct dst_entry *dst = skb_dst(skb);
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000786 struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct dn_route *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int header_len;
789#ifdef CONFIG_NETFILTER
790 struct net_device *dev = skb->dev;
791#endif
792
793 if (skb->pkt_type != PACKET_HOST)
794 goto drop;
795
796 /* Ensure that we have enough space for headers */
Eric Dumazetadf30902009-06-02 05:19:30 +0000797 rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 header_len = dn_db->use_long ? 21 : 6;
Changli Gaod8d1f302010-06-10 23:31:35 -0700799 if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto drop;
801
802 /*
803 * Hop count exceeded.
804 */
805 if (++cb->hops > 30)
806 goto drop;
807
Changli Gaod8d1f302010-06-10 23:31:35 -0700808 skb->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /*
811 * If packet goes out same interface it came in on, then set
812 * the Intra-Ethernet bit. This has no effect for short
813 * packets, so we don't need to test for them here.
814 */
815 cb->rt_flags &= ~DN_RT_F_IE;
816 if (rt->rt_flags & RTCF_DOREDIRECT)
817 cb->rt_flags |= DN_RT_F_IE;
818
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100819 return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
David S. Miller8f40b162011-07-17 13:34:11 -0700820 dn_to_neigh_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822drop:
823 kfree_skb(skb);
824 return NET_RX_DROP;
825}
826
827/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * Used to catch bugs. This should never normally get
829 * called.
830 */
831static int dn_rt_bug(struct sk_buff *skb)
832{
Joe Perchese87cc472012-05-13 21:56:26 +0000833 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Joe Perchese87cc472012-05-13 21:56:26 +0000835 net_dbg_ratelimited("dn_rt_bug: skb from:%04x to:%04x\n",
836 le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 kfree_skb(skb);
839
Florian Westphal0e8635a2009-06-20 00:53:25 +0000840 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
David S. Miller0dbaee32010-12-13 12:52:14 -0800843static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
844{
845 return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
846}
847
Steffen Klassertebb762f2011-11-23 02:12:51 +0000848static unsigned int dn_dst_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -0800849{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000850 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
851
852 return mtu ? : dst->dev->mtu;
David S. Millerd33e4552010-12-14 13:01:14 -0800853}
854
David S. Millerf894cbf2012-07-02 21:52:24 -0700855static struct neighbour *dn_dst_neigh_lookup(const struct dst_entry *dst,
856 struct sk_buff *skb,
857 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700858{
859 return __neigh_lookup_errno(&dn_neigh_table, daddr, dst->dev);
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
863{
864 struct dn_fib_info *fi = res->fi;
Changli Gaod8d1f302010-06-10 23:31:35 -0700865 struct net_device *dev = rt->dst.dev;
David S. Miller62fa8a82011-01-26 20:51:05 -0800866 unsigned int mss_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 struct neighbour *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 if (fi) {
870 if (DN_FIB_RES_GW(*res) &&
871 DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
872 rt->rt_gateway = DN_FIB_RES_GW(*res);
David S. Miller62fa8a82011-01-26 20:51:05 -0800873 dst_init_metrics(&rt->dst, fi->fib_metrics, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 rt->rt_type = res->type;
876
David S. Millerfccd7d52012-07-02 22:22:18 -0700877 if (dev != NULL && rt->n == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
879 if (IS_ERR(n))
880 return PTR_ERR(n);
David S. Millerfccd7d52012-07-02 22:22:18 -0700881 rt->n = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
David S. Millerd33e4552010-12-14 13:01:14 -0800884 if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
David S. Millerdefb3512010-12-08 21:16:57 -0800885 dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
David S. Miller62fa8a82011-01-26 20:51:05 -0800886 mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
887 if (mss_metric) {
David S. Miller0dbaee32010-12-13 12:52:14 -0800888 unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
David S. Miller62fa8a82011-01-26 20:51:05 -0800889 if (mss_metric > mss)
David S. Miller0dbaee32010-12-13 12:52:14 -0800890 dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return 0;
893}
894
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800895static inline int dn_match_addr(__le16 addr1, __le16 addr2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800897 __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int match = 16;
899 while(tmp) {
900 tmp >>= 1;
901 match--;
902 }
903 return match;
904}
905
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800906static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800908 __le16 saddr = 0;
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000909 struct dn_dev *dn_db;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct dn_ifaddr *ifa;
911 int best_match = 0;
912 int ret;
913
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000914 rcu_read_lock();
915 dn_db = rcu_dereference(dev->dn_ptr);
916 for (ifa = rcu_dereference(dn_db->ifa_list);
917 ifa != NULL;
918 ifa = rcu_dereference(ifa->ifa_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (ifa->ifa_scope > scope)
920 continue;
921 if (!daddr) {
922 saddr = ifa->ifa_local;
923 break;
924 }
925 ret = dn_match_addr(daddr, ifa->ifa_local);
926 if (ret > best_match)
927 saddr = ifa->ifa_local;
928 if (best_match == 0)
929 saddr = ifa->ifa_local;
930 }
Eric Dumazetfc766e4c2010-10-29 03:09:24 +0000931 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 return saddr;
934}
935
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800936static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
939}
940
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800941static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800943 __le16 mask = dnet_make_mask(res->prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
945}
946
David S. Millerbef55ae2011-03-12 17:17:10 -0500947static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oldflp, int try_hard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
David S. Millerbef55ae2011-03-12 17:17:10 -0500949 struct flowidn fld = {
950 .daddr = oldflp->daddr,
951 .saddr = oldflp->saddr,
952 .flowidn_scope = RT_SCOPE_UNIVERSE,
953 .flowidn_mark = oldflp->flowidn_mark,
954 .flowidn_iif = init_net.loopback_dev->ifindex,
955 .flowidn_oif = oldflp->flowidn_oif,
David S. Miller1d28f422011-03-12 00:29:39 -0500956 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 struct dn_route *rt = NULL;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700958 struct net_device *dev_out = NULL, *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 struct neighbour *neigh = NULL;
Eric Dumazet95c96172012-04-15 05:58:06 +0000960 unsigned int hash;
961 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
963 int err;
964 int free_res = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800965 __le16 gateway = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (decnet_debug_level & 16)
968 printk(KERN_DEBUG
969 "dn_route_output_slow: dst=%04x src=%04x mark=%d"
David S. Millerbef55ae2011-03-12 17:17:10 -0500970 " iif=%d oif=%d\n", le16_to_cpu(oldflp->daddr),
971 le16_to_cpu(oldflp->saddr),
972 oldflp->flowidn_mark, init_net.loopback_dev->ifindex,
973 oldflp->flowidn_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 /* If we have an output interface, verify its a DECnet device */
David S. Millerbef55ae2011-03-12 17:17:10 -0500976 if (oldflp->flowidn_oif) {
977 dev_out = dev_get_by_index(&init_net, oldflp->flowidn_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 err = -ENODEV;
979 if (dev_out && dev_out->dn_ptr == NULL) {
980 dev_put(dev_out);
981 dev_out = NULL;
982 }
983 if (dev_out == NULL)
984 goto out;
985 }
986
987 /* If we have a source address, verify that its a local address */
David S. Millerbef55ae2011-03-12 17:17:10 -0500988 if (oldflp->saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 err = -EADDRNOTAVAIL;
990
991 if (dev_out) {
David S. Millerbef55ae2011-03-12 17:17:10 -0500992 if (dn_dev_islocal(dev_out, oldflp->saddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 goto source_ok;
994 dev_put(dev_out);
995 goto out;
996 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800997 rcu_read_lock();
998 for_each_netdev_rcu(&init_net, dev) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700999 if (!dev->dn_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 continue;
David S. Millerbef55ae2011-03-12 17:17:10 -05001001 if (!dn_dev_islocal(dev, oldflp->saddr))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -07001002 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -07001003 if ((dev->flags & IFF_LOOPBACK) &&
David S. Millerbef55ae2011-03-12 17:17:10 -05001004 oldflp->daddr &&
1005 !dn_dev_islocal(dev, oldflp->daddr))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -07001006 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -07001007
1008 dev_out = dev;
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -07001009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001011 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (dev_out == NULL)
1013 goto out;
1014 dev_hold(dev_out);
1015source_ok:
1016 ;
1017 }
1018
1019 /* No destination? Assume its local */
David S. Millerbef55ae2011-03-12 17:17:10 -05001020 if (!fld.daddr) {
1021 fld.daddr = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 err = -EADDRNOTAVAIL;
1024 if (dev_out)
1025 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001026 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001028 if (!fld.daddr) {
1029 fld.daddr =
1030 fld.saddr = dnet_select_source(dev_out, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 RT_SCOPE_HOST);
David S. Millerbef55ae2011-03-12 17:17:10 -05001032 if (!fld.daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 goto out;
1034 }
David S. Millerbef55ae2011-03-12 17:17:10 -05001035 fld.flowidn_oif = init_net.loopback_dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 res.type = RTN_LOCAL;
1037 goto make_route;
1038 }
1039
1040 if (decnet_debug_level & 16)
1041 printk(KERN_DEBUG
1042 "dn_route_output_slow: initial checks complete."
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001043 " dst=%o4x src=%04x oif=%d try_hard=%d\n",
David S. Millerbef55ae2011-03-12 17:17:10 -05001044 le16_to_cpu(fld.daddr), le16_to_cpu(fld.saddr),
1045 fld.flowidn_oif, try_hard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /*
1048 * N.B. If the kernel is compiled without router support then
1049 * dn_fib_lookup() will evaluate to non-zero so this if () block
1050 * will always be executed.
1051 */
1052 err = -ESRCH;
David S. Millerbef55ae2011-03-12 17:17:10 -05001053 if (try_hard || (err = dn_fib_lookup(&fld, &res)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct dn_dev *dn_db;
1055 if (err != -ESRCH)
1056 goto out;
1057 /*
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001058 * Here the fallback is basically the standard algorithm for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 * routing in endnodes which is described in the DECnet routing
1060 * docs
1061 *
1062 * If we are not trying hard, look in neighbour cache.
1063 * The result is tested to ensure that if a specific output
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001064 * device/source address was requested, then we honour that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * here
1066 */
1067 if (!try_hard) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001068 neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fld.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (neigh) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001070 if ((oldflp->flowidn_oif &&
1071 (neigh->dev->ifindex != oldflp->flowidn_oif)) ||
1072 (oldflp->saddr &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 (!dn_dev_islocal(neigh->dev,
David S. Millerbef55ae2011-03-12 17:17:10 -05001074 oldflp->saddr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 neigh_release(neigh);
1076 neigh = NULL;
1077 } else {
1078 if (dev_out)
1079 dev_put(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001080 if (dn_dev_islocal(neigh->dev, fld.daddr)) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001081 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 res.type = RTN_LOCAL;
1083 } else {
1084 dev_out = neigh->dev;
1085 }
1086 dev_hold(dev_out);
1087 goto select_source;
1088 }
1089 }
1090 }
1091
1092 /* Not there? Perhaps its a local address */
1093 if (dev_out == NULL)
1094 dev_out = dn_dev_get_default();
1095 err = -ENODEV;
1096 if (dev_out == NULL)
1097 goto out;
Eric Dumazetfc766e4c2010-10-29 03:09:24 +00001098 dn_db = rcu_dereference_raw(dev_out->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* Possible improvement - check all devices for local addr */
David S. Millerbef55ae2011-03-12 17:17:10 -05001100 if (dn_dev_islocal(dev_out, fld.daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001102 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 dev_hold(dev_out);
1104 res.type = RTN_LOCAL;
1105 goto select_source;
1106 }
1107 /* Not local either.... try sending it to the default router */
1108 neigh = neigh_clone(dn_db->router);
1109 BUG_ON(neigh && neigh->dev != dev_out);
1110
1111 /* Ok then, we assume its directly connected and move on */
1112select_source:
1113 if (neigh)
1114 gateway = ((struct dn_neigh *)neigh)->addr;
1115 if (gateway == 0)
David S. Millerbef55ae2011-03-12 17:17:10 -05001116 gateway = fld.daddr;
1117 if (fld.saddr == 0) {
1118 fld.saddr = dnet_select_source(dev_out, gateway,
1119 res.type == RTN_LOCAL ?
1120 RT_SCOPE_HOST :
1121 RT_SCOPE_LINK);
1122 if (fld.saddr == 0 && res.type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 goto e_addr;
1124 }
David S. Millerbef55ae2011-03-12 17:17:10 -05001125 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 goto make_route;
1127 }
1128 free_res = 1;
1129
1130 if (res.type == RTN_NAT)
1131 goto e_inval;
1132
1133 if (res.type == RTN_LOCAL) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001134 if (!fld.saddr)
1135 fld.saddr = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (dev_out)
1137 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001138 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001140 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (res.fi)
1142 dn_fib_info_put(res.fi);
1143 res.fi = NULL;
1144 goto make_route;
1145 }
1146
David S. Millerbef55ae2011-03-12 17:17:10 -05001147 if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1148 dn_fib_select_multipath(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001150 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 * We could add some logic to deal with default routes here and
1152 * get rid of some of the special casing above.
1153 */
1154
David S. Millerbef55ae2011-03-12 17:17:10 -05001155 if (!fld.saddr)
1156 fld.saddr = DN_FIB_RES_PREFSRC(res);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (dev_out)
1159 dev_put(dev_out);
1160 dev_out = DN_FIB_RES_DEV(res);
1161 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001162 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 gateway = DN_FIB_RES_GW(res);
1164
1165make_route:
1166 if (dev_out->flags & IFF_LOOPBACK)
1167 flags |= RTCF_LOCAL;
1168
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001169 rt = dst_alloc(&dn_dst_ops, dev_out, 1, 0, DST_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (rt == NULL)
1171 goto e_nobufs;
1172
David S. Millercf911662011-04-28 14:31:47 -07001173 memset(&rt->fld, 0, sizeof(rt->fld));
David S. Millerbef55ae2011-03-12 17:17:10 -05001174 rt->fld.saddr = oldflp->saddr;
1175 rt->fld.daddr = oldflp->daddr;
1176 rt->fld.flowidn_oif = oldflp->flowidn_oif;
1177 rt->fld.flowidn_iif = 0;
1178 rt->fld.flowidn_mark = oldflp->flowidn_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
David S. Millerbef55ae2011-03-12 17:17:10 -05001180 rt->rt_saddr = fld.saddr;
1181 rt->rt_daddr = fld.daddr;
1182 rt->rt_gateway = gateway ? gateway : fld.daddr;
1183 rt->rt_local_src = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
David S. Millerbef55ae2011-03-12 17:17:10 -05001185 rt->rt_dst_map = fld.daddr;
1186 rt->rt_src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
David S. Millerfccd7d52012-07-02 22:22:18 -07001188 rt->n = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 neigh = NULL;
1190
Changli Gaod8d1f302010-06-10 23:31:35 -07001191 rt->dst.lastuse = jiffies;
1192 rt->dst.output = dn_output;
1193 rt->dst.input = dn_rt_bug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 rt->rt_flags = flags;
1195 if (flags & RTCF_LOCAL)
Changli Gaod8d1f302010-06-10 23:31:35 -07001196 rt->dst.input = dn_nsp_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 err = dn_rt_set_next_hop(rt, &res);
1199 if (err)
1200 goto e_neighbour;
1201
David S. Millerbef55ae2011-03-12 17:17:10 -05001202 hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 dn_insert_route(rt, hash, (struct dn_route **)pprt);
1204
1205done:
1206 if (neigh)
1207 neigh_release(neigh);
1208 if (free_res)
1209 dn_fib_res_put(&res);
1210 if (dev_out)
1211 dev_put(dev_out);
1212out:
1213 return err;
1214
1215e_addr:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001216 err = -EADDRNOTAVAIL;
1217 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218e_inval:
1219 err = -EINVAL;
1220 goto done;
1221e_nobufs:
1222 err = -ENOBUFS;
1223 goto done;
1224e_neighbour:
Changli Gaod8d1f302010-06-10 23:31:35 -07001225 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 goto e_nobufs;
1227}
1228
1229
1230/*
1231 * N.B. The flags may be moved into the flowi at some future stage.
1232 */
David S. Millerbef55ae2011-03-12 17:17:10 -05001233static int __dn_route_output_key(struct dst_entry **pprt, const struct flowidn *flp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Eric Dumazet95c96172012-04-15 05:58:06 +00001235 unsigned int hash = dn_hash(flp->saddr, flp->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 struct dn_route *rt = NULL;
1237
1238 if (!(flags & MSG_TRYHARD)) {
1239 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001240 for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07001241 rt = rcu_dereference_bh(rt->dst.dn_next)) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001242 if ((flp->daddr == rt->fld.daddr) &&
1243 (flp->saddr == rt->fld.saddr) &&
1244 (flp->flowidn_mark == rt->fld.flowidn_mark) &&
David S. Millerc7537962010-11-11 17:07:48 -08001245 dn_is_output_route(rt) &&
David S. Millerbef55ae2011-03-12 17:17:10 -05001246 (rt->fld.flowidn_oif == flp->flowidn_oif)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001247 dst_use(&rt->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 rcu_read_unlock_bh();
Changli Gaod8d1f302010-06-10 23:31:35 -07001249 *pprt = &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251 }
1252 }
1253 rcu_read_unlock_bh();
1254 }
1255
1256 return dn_route_output_slow(pprt, flp, flags);
1257}
1258
David S. Millerbef55ae2011-03-12 17:17:10 -05001259static int dn_route_output_key(struct dst_entry **pprt, struct flowidn *flp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 int err;
1262
1263 err = __dn_route_output_key(pprt, flp, flags);
David S. Millerbef55ae2011-03-12 17:17:10 -05001264 if (err == 0 && flp->flowidn_proto) {
1265 *pprt = xfrm_lookup(&init_net, *pprt,
1266 flowidn_to_flowi(flp), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -08001267 if (IS_ERR(*pprt)) {
1268 err = PTR_ERR(*pprt);
1269 *pprt = NULL;
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272 return err;
1273}
1274
David S. Millerbef55ae2011-03-12 17:17:10 -05001275int dn_route_output_sock(struct dst_entry **pprt, struct flowidn *fl, struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 int err;
1278
1279 err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
David S. Millerbef55ae2011-03-12 17:17:10 -05001280 if (err == 0 && fl->flowidn_proto) {
David S. Miller80c0bc92011-03-01 14:36:37 -08001281 if (!(flags & MSG_DONTWAIT))
David S. Millerbef55ae2011-03-12 17:17:10 -05001282 fl->flowidn_flags |= FLOWI_FLAG_CAN_SLEEP;
1283 *pprt = xfrm_lookup(&init_net, *pprt,
1284 flowidn_to_flowi(fl), sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -08001285 if (IS_ERR(*pprt)) {
1286 err = PTR_ERR(*pprt);
1287 *pprt = NULL;
1288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 return err;
1291}
1292
1293static int dn_route_input_slow(struct sk_buff *skb)
1294{
1295 struct dn_route *rt = NULL;
1296 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1297 struct net_device *in_dev = skb->dev;
1298 struct net_device *out_dev = NULL;
1299 struct dn_dev *dn_db;
1300 struct neighbour *neigh = NULL;
Eric Dumazet95c96172012-04-15 05:58:06 +00001301 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int flags = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001303 __le16 gateway = 0;
1304 __le16 local_src = 0;
David S. Millerbef55ae2011-03-12 17:17:10 -05001305 struct flowidn fld = {
1306 .daddr = cb->dst,
1307 .saddr = cb->src,
1308 .flowidn_scope = RT_SCOPE_UNIVERSE,
1309 .flowidn_mark = skb->mark,
1310 .flowidn_iif = skb->dev->ifindex,
David S. Miller1d28f422011-03-12 00:29:39 -05001311 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1313 int err = -EINVAL;
1314 int free_res = 0;
1315
1316 dev_hold(in_dev);
1317
Eric Dumazetfc766e4c2010-10-29 03:09:24 +00001318 if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 goto out;
1320
1321 /* Zero source addresses are not allowed */
David S. Millerbef55ae2011-03-12 17:17:10 -05001322 if (fld.saddr == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 goto out;
1324
1325 /*
1326 * In this case we've just received a packet from a source
1327 * outside ourselves pretending to come from us. We don't
1328 * allow it any further to prevent routing loops, spoofing and
1329 * other nasties. Loopback packets already have the dst attached
1330 * so this only affects packets which have originated elsewhere.
1331 */
1332 err = -ENOTUNIQ;
1333 if (dn_dev_islocal(in_dev, cb->src))
1334 goto out;
1335
David S. Millerbef55ae2011-03-12 17:17:10 -05001336 err = dn_fib_lookup(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (err) {
1338 if (err != -ESRCH)
1339 goto out;
1340 /*
1341 * Is the destination us ?
1342 */
1343 if (!dn_dev_islocal(in_dev, cb->dst))
1344 goto e_inval;
1345
1346 res.type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 } else {
David S. Millerbef55ae2011-03-12 17:17:10 -05001348 __le16 src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 free_res = 1;
1350
1351 out_dev = DN_FIB_RES_DEV(res);
1352 if (out_dev == NULL) {
Joe Perchese87cc472012-05-13 21:56:26 +00001353 net_crit_ratelimited("Bug in dn_route_input_slow() No output device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 goto e_inval;
1355 }
1356 dev_hold(out_dev);
1357
1358 if (res.r)
David S. Millerbef55ae2011-03-12 17:17:10 -05001359 src_map = fld.saddr; /* no NAT support for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 gateway = DN_FIB_RES_GW(res);
1362 if (res.type == RTN_NAT) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001363 fld.daddr = dn_fib_rules_map_destination(fld.daddr, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 dn_fib_res_put(&res);
1365 free_res = 0;
David S. Millerbef55ae2011-03-12 17:17:10 -05001366 if (dn_fib_lookup(&fld, &res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 goto e_inval;
1368 free_res = 1;
1369 if (res.type != RTN_UNICAST)
1370 goto e_inval;
1371 flags |= RTCF_DNAT;
David S. Millerbef55ae2011-03-12 17:17:10 -05001372 gateway = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
David S. Millerbef55ae2011-03-12 17:17:10 -05001374 fld.saddr = src_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
1377 switch(res.type) {
1378 case RTN_UNICAST:
1379 /*
1380 * Forwarding check here, we only check for forwarding
1381 * being turned off, if you want to only forward intra
1382 * area, its up to you to set the routing tables up
1383 * correctly.
1384 */
1385 if (dn_db->parms.forwarding == 0)
1386 goto e_inval;
1387
David S. Millerbef55ae2011-03-12 17:17:10 -05001388 if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1389 dn_fib_select_multipath(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001391 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1393 * flag as a hint to set the intra-ethernet bit when
1394 * forwarding. If we've got NAT in operation, we don't do
1395 * this optimisation.
1396 */
1397 if (out_dev == in_dev && !(flags & RTCF_NAT))
1398 flags |= RTCF_DOREDIRECT;
1399
1400 local_src = DN_FIB_RES_PREFSRC(res);
1401
1402 case RTN_BLACKHOLE:
1403 case RTN_UNREACHABLE:
1404 break;
1405 case RTN_LOCAL:
1406 flags |= RTCF_LOCAL;
David S. Millerbef55ae2011-03-12 17:17:10 -05001407 fld.saddr = cb->dst;
1408 fld.daddr = cb->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* Routing tables gave us a gateway */
1411 if (gateway)
1412 goto make_route;
1413
1414 /* Packet was intra-ethernet, so we know its on-link */
Steven Whitehouse3a31b9d2006-10-18 20:45:22 -07001415 if (cb->rt_flags & DN_RT_F_IE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 gateway = cb->src;
1417 flags |= RTCF_DIRECTSRC;
1418 goto make_route;
1419 }
1420
1421 /* Use the default router if there is one */
1422 neigh = neigh_clone(dn_db->router);
1423 if (neigh) {
1424 gateway = ((struct dn_neigh *)neigh)->addr;
1425 goto make_route;
1426 }
1427
1428 /* Close eyes and pray */
1429 gateway = cb->src;
1430 flags |= RTCF_DIRECTSRC;
1431 goto make_route;
1432 default:
1433 goto e_inval;
1434 }
1435
1436make_route:
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001437 rt = dst_alloc(&dn_dst_ops, out_dev, 0, 0, DST_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (rt == NULL)
1439 goto e_nobufs;
1440
David S. Millercf911662011-04-28 14:31:47 -07001441 memset(&rt->fld, 0, sizeof(rt->fld));
David S. Millerbef55ae2011-03-12 17:17:10 -05001442 rt->rt_saddr = fld.saddr;
1443 rt->rt_daddr = fld.daddr;
1444 rt->rt_gateway = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (gateway)
1446 rt->rt_gateway = gateway;
1447 rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
1448
David S. Millerbef55ae2011-03-12 17:17:10 -05001449 rt->rt_dst_map = fld.daddr;
1450 rt->rt_src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
David S. Millerbef55ae2011-03-12 17:17:10 -05001452 rt->fld.saddr = cb->src;
1453 rt->fld.daddr = cb->dst;
1454 rt->fld.flowidn_oif = 0;
1455 rt->fld.flowidn_iif = in_dev->ifindex;
1456 rt->fld.flowidn_mark = fld.flowidn_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
David S. Millerfccd7d52012-07-02 22:22:18 -07001458 rt->n = neigh;
Changli Gaod8d1f302010-06-10 23:31:35 -07001459 rt->dst.lastuse = jiffies;
1460 rt->dst.output = dn_rt_bug;
Joe Perches06f8fe12011-07-01 09:43:03 +00001461 switch (res.type) {
1462 case RTN_UNICAST:
1463 rt->dst.input = dn_forward;
1464 break;
1465 case RTN_LOCAL:
1466 rt->dst.output = dn_output;
1467 rt->dst.input = dn_nsp_rx;
1468 rt->dst.dev = in_dev;
1469 flags |= RTCF_LOCAL;
1470 break;
1471 default:
1472 case RTN_UNREACHABLE:
1473 case RTN_BLACKHOLE:
1474 rt->dst.input = dst_discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476 rt->rt_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 err = dn_rt_set_next_hop(rt, &res);
1479 if (err)
1480 goto e_neighbour;
1481
David S. Millerbef55ae2011-03-12 17:17:10 -05001482 hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
Eric Dumazetadf30902009-06-02 05:19:30 +00001483 dn_insert_route(rt, hash, &rt);
Changli Gaod8d1f302010-06-10 23:31:35 -07001484 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486done:
1487 if (neigh)
1488 neigh_release(neigh);
1489 if (free_res)
1490 dn_fib_res_put(&res);
1491 dev_put(in_dev);
1492 if (out_dev)
1493 dev_put(out_dev);
1494out:
1495 return err;
1496
1497e_inval:
1498 err = -EINVAL;
1499 goto done;
1500
1501e_nobufs:
1502 err = -ENOBUFS;
1503 goto done;
1504
1505e_neighbour:
Changli Gaod8d1f302010-06-10 23:31:35 -07001506 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 goto done;
1508}
1509
Roel Kluin5eaa65b2008-12-10 15:18:31 -08001510static int dn_route_input(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 struct dn_route *rt;
1513 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Eric Dumazet95c96172012-04-15 05:58:06 +00001514 unsigned int hash = dn_hash(cb->src, cb->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Eric Dumazetadf30902009-06-02 05:19:30 +00001516 if (skb_dst(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return 0;
1518
1519 rcu_read_lock();
1520 for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
Changli Gaod8d1f302010-06-10 23:31:35 -07001521 rt = rcu_dereference(rt->dst.dn_next)) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001522 if ((rt->fld.saddr == cb->src) &&
1523 (rt->fld.daddr == cb->dst) &&
1524 (rt->fld.flowidn_oif == 0) &&
1525 (rt->fld.flowidn_mark == skb->mark) &&
1526 (rt->fld.flowidn_iif == cb->iif)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001527 dst_use(&rt->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 rcu_read_unlock();
Eric Dumazetadf30902009-06-02 05:19:30 +00001529 skb_dst_set(skb, (struct dst_entry *)rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return 0;
1531 }
1532 }
1533 rcu_read_unlock();
1534
1535 return dn_route_input_slow(skb);
1536}
1537
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001538static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1539 int event, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Eric Dumazetadf30902009-06-02 05:19:30 +00001541 struct dn_route *rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 struct rtmsg *r;
1543 struct nlmsghdr *nlh;
Thomas Grafe3703b32006-11-27 09:27:07 -08001544 long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
David S. Miller737100e2012-06-26 21:46:19 -07001546 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
1547 if (!nlh)
Thomas Graf6b609782012-06-26 23:36:15 +00001548 return -EMSGSIZE;
1549
David S. Miller737100e2012-06-26 21:46:19 -07001550 r = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 r->rtm_family = AF_DECnet;
1552 r->rtm_dst_len = 16;
1553 r->rtm_src_len = 0;
1554 r->rtm_tos = 0;
1555 r->rtm_table = RT_TABLE_MAIN;
1556 r->rtm_type = rt->rt_type;
1557 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1558 r->rtm_scope = RT_SCOPE_UNIVERSE;
1559 r->rtm_protocol = RTPROT_UNSPEC;
Thomas Graf6b609782012-06-26 23:36:15 +00001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (rt->rt_flags & RTCF_NOTIFY)
1562 r->rtm_flags |= RTM_F_NOTIFY;
Thomas Graf6b609782012-06-26 23:36:15 +00001563
1564 if (nla_put_u32(skb, RTA_TABLE, RT_TABLE_MAIN) < 0 ||
1565 nla_put_le16(skb, RTA_DST, rt->rt_daddr) < 0)
1566 goto errout;
1567
David S. Millerbef55ae2011-03-12 17:17:10 -05001568 if (rt->fld.saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 r->rtm_src_len = 16;
Thomas Graf6b609782012-06-26 23:36:15 +00001570 if (nla_put_le16(skb, RTA_SRC, rt->fld.saddr) < 0)
1571 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
Thomas Graf6b609782012-06-26 23:36:15 +00001573 if (rt->dst.dev &&
1574 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex) < 0)
1575 goto errout;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /*
1578 * Note to self - change this if input routes reverse direction when
1579 * they deal only with inputs and not with replies like they do
1580 * currently.
1581 */
Thomas Graf6b609782012-06-26 23:36:15 +00001582 if (nla_put_le16(skb, RTA_PREFSRC, rt->rt_local_src) < 0)
1583 goto errout;
1584
1585 if (rt->rt_daddr != rt->rt_gateway &&
1586 nla_put_le16(skb, RTA_GATEWAY, rt->rt_gateway) < 0)
1587 goto errout;
1588
David S. Millerdefb3512010-12-08 21:16:57 -08001589 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Graf6b609782012-06-26 23:36:15 +00001590 goto errout;
1591
Changli Gaod8d1f302010-06-10 23:31:35 -07001592 expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
1593 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
1594 rt->dst.error) < 0)
Thomas Graf6b609782012-06-26 23:36:15 +00001595 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Thomas Graf6b609782012-06-26 23:36:15 +00001597 if (dn_is_input_route(rt) &&
1598 nla_put_u32(skb, RTA_IIF, rt->fld.flowidn_iif) < 0)
1599 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Thomas Graf6b609782012-06-26 23:36:15 +00001601 return nlmsg_end(skb, nlh);
1602
1603errout:
1604 nlmsg_cancel(skb, nlh);
1605 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608/*
1609 * This is called by both endnodes and routers now.
1610 */
Thomas Graffa34ddd2007-03-22 11:57:46 -07001611static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001613 struct net *net = sock_net(in_skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 struct rtattr **rta = arg;
David S. Miller737100e2012-06-26 21:46:19 -07001615 struct rtmsg *rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 struct dn_route *rt = NULL;
1617 struct dn_skb_cb *cb;
1618 int err;
1619 struct sk_buff *skb;
David S. Millerbef55ae2011-03-12 17:17:10 -05001620 struct flowidn fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001622 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001623 return -EINVAL;
1624
David S. Millerbef55ae2011-03-12 17:17:10 -05001625 memset(&fld, 0, sizeof(fld));
1626 fld.flowidn_proto = DNPROTO_NSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Thomas Graf6b609782012-06-26 23:36:15 +00001628 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (skb == NULL)
1630 return -ENOBUFS;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001631 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 cb = DN_SKB_CB(skb);
1633
1634 if (rta[RTA_SRC-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001635 memcpy(&fld.saddr, RTA_DATA(rta[RTA_SRC-1]), 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (rta[RTA_DST-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001637 memcpy(&fld.daddr, RTA_DATA(rta[RTA_DST-1]), 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (rta[RTA_IIF-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001639 memcpy(&fld.flowidn_iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
David S. Millerbef55ae2011-03-12 17:17:10 -05001641 if (fld.flowidn_iif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 struct net_device *dev;
David S. Millerbef55ae2011-03-12 17:17:10 -05001643 if ((dev = dev_get_by_index(&init_net, fld.flowidn_iif)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 kfree_skb(skb);
1645 return -ENODEV;
1646 }
1647 if (!dev->dn_ptr) {
1648 dev_put(dev);
1649 kfree_skb(skb);
1650 return -ENODEV;
1651 }
YOSHIFUJI Hideakib98999d2007-12-12 03:51:49 +09001652 skb->protocol = htons(ETH_P_DNA_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 skb->dev = dev;
David S. Millerbef55ae2011-03-12 17:17:10 -05001654 cb->src = fld.saddr;
1655 cb->dst = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 local_bh_disable();
1657 err = dn_route_input(skb);
1658 local_bh_enable();
1659 memset(cb, 0, sizeof(struct dn_skb_cb));
Eric Dumazetadf30902009-06-02 05:19:30 +00001660 rt = (struct dn_route *)skb_dst(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001661 if (!err && -rt->dst.error)
1662 err = rt->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 } else {
1664 int oif = 0;
1665 if (rta[RTA_OIF - 1])
1666 memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
David S. Millerbef55ae2011-03-12 17:17:10 -05001667 fld.flowidn_oif = oif;
1668 err = dn_route_output_key((struct dst_entry **)&rt, &fld, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670
1671 if (skb->dev)
1672 dev_put(skb->dev);
1673 skb->dev = NULL;
1674 if (err)
1675 goto out_free;
Changli Gaod8d1f302010-06-10 23:31:35 -07001676 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (rtm->rtm_flags & RTM_F_NOTIFY)
1678 rt->rt_flags |= RTCF_NOTIFY;
1679
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001680 err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, RTM_NEWROUTE, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (err == 0)
1683 goto out_free;
1684 if (err < 0) {
1685 err = -EMSGSIZE;
1686 goto out_free;
1687 }
1688
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001689 return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691out_free:
1692 kfree_skb(skb);
1693 return err;
1694}
1695
1696/*
1697 * For routers, this is called from dn_fib_dump, but for endnodes its
1698 * called directly from the rtnetlink dispatch table.
1699 */
1700int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1701{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001702 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 struct dn_route *rt;
1704 int h, s_h;
1705 int idx, s_idx;
Thomas Graf6b609782012-06-26 23:36:15 +00001706 struct rtmsg *rtm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001708 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001709 return 0;
1710
Thomas Graf6b609782012-06-26 23:36:15 +00001711 if (nlmsg_len(cb->nlh) < sizeof(struct rtmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return -EINVAL;
Thomas Graf6b609782012-06-26 23:36:15 +00001713
1714 rtm = nlmsg_data(cb->nlh);
1715 if (!(rtm->rtm_flags & RTM_F_CLONED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return 0;
1717
1718 s_h = cb->args[0];
1719 s_idx = idx = cb->args[1];
1720 for(h = 0; h <= dn_rt_hash_mask; h++) {
1721 if (h < s_h)
1722 continue;
1723 if (h > s_h)
1724 s_idx = 0;
1725 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001726 for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07001728 rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (idx < s_idx)
1730 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001731 skb_dst_set(skb, dst_clone(&rt->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001733 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001734 1, NLM_F_MULTI) <= 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001735 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 rcu_read_unlock_bh();
1737 goto done;
1738 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001739 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741 rcu_read_unlock_bh();
1742 }
1743
1744done:
1745 cb->args[0] = h;
1746 cb->args[1] = idx;
1747 return skb->len;
1748}
1749
1750#ifdef CONFIG_PROC_FS
1751struct dn_rt_cache_iter_state {
1752 int bucket;
1753};
1754
1755static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1756{
1757 struct dn_route *rt = NULL;
1758 struct dn_rt_cache_iter_state *s = seq->private;
1759
1760 for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1761 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001762 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (rt)
1764 break;
1765 rcu_read_unlock_bh();
1766 }
Paul E. McKenneya898def2010-02-22 17:04:49 -08001767 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
1770static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1771{
Eric Dumazet0d89d792008-01-10 22:35:21 -08001772 struct dn_rt_cache_iter_state *s = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Eric Dumazetfc766e4c2010-10-29 03:09:24 +00001774 rt = rcu_dereference_bh(rt->dst.dn_next);
1775 while (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 rcu_read_unlock_bh();
1777 if (--s->bucket < 0)
1778 break;
1779 rcu_read_lock_bh();
Eric Dumazetfc766e4c2010-10-29 03:09:24 +00001780 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
Eric Dumazetfc766e4c2010-10-29 03:09:24 +00001782 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
1785static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1786{
1787 struct dn_route *rt = dn_rt_cache_get_first(seq);
1788
1789 if (rt) {
1790 while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1791 --*pos;
1792 }
1793 return *pos ? NULL : rt;
1794}
1795
1796static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1797{
1798 struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1799 ++*pos;
1800 return rt;
1801}
1802
1803static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1804{
1805 if (v)
1806 rcu_read_unlock_bh();
1807}
1808
1809static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1810{
1811 struct dn_route *rt = v;
1812 char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1813
1814 seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07001815 rt->dst.dev ? rt->dst.dev->name : "*",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -08001816 dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1817 dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
Changli Gaod8d1f302010-06-10 23:31:35 -07001818 atomic_read(&rt->dst.__refcnt),
1819 rt->dst.__use,
1820 (int) dst_metric(&rt->dst, RTAX_RTT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001824static const struct seq_operations dn_rt_cache_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 .start = dn_rt_cache_seq_start,
1826 .next = dn_rt_cache_seq_next,
1827 .stop = dn_rt_cache_seq_stop,
1828 .show = dn_rt_cache_seq_show,
1829};
1830
1831static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1832{
Pavel Emelyanov31164082007-10-10 02:30:23 -07001833 return seq_open_private(file, &dn_rt_cache_seq_ops,
1834 sizeof(struct dn_rt_cache_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Arjan van de Ven9a321442007-02-12 00:55:35 -08001837static const struct file_operations dn_rt_cache_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 .owner = THIS_MODULE,
1839 .open = dn_rt_cache_seq_open,
1840 .read = seq_read,
1841 .llseek = seq_lseek,
1842 .release = seq_release_private,
1843};
1844
1845#endif /* CONFIG_PROC_FS */
1846
1847void __init dn_route_init(void)
1848{
1849 int i, goal, order;
1850
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07001851 dn_dst_ops.kmem_cachep =
1852 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001853 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001854 dst_entries_init(&dn_dst_ops);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001855 setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1857 add_timer(&dn_route_timer);
1858
Jan Beulich44813742009-09-21 17:03:05 -07001859 goal = totalram_pages >> (26 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 for(order = 0; (1UL << order) < goal; order++)
1862 /* NOTHING */;
1863
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001864 /*
1865 * Only want 1024 entries max, since the table is very, very unlikely
1866 * to be larger than that.
1867 */
1868 while(order && ((((1UL << order) * PAGE_SIZE) /
1869 sizeof(struct dn_rt_hash_bucket)) >= 2048))
1870 order--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001872 do {
1873 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1874 sizeof(struct dn_rt_hash_bucket);
1875 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1876 dn_rt_hash_mask--;
1877 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1878 __get_free_pages(GFP_ATOMIC, order);
1879 } while (dn_rt_hash_table == NULL && --order > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 if (!dn_rt_hash_table)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001882 panic("Failed to allocate DECnet route cache hash table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001884 printk(KERN_INFO
1885 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1886 dn_rt_hash_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1888
1889 dn_rt_hash_mask--;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001890 for(i = 0; i <= dn_rt_hash_mask; i++) {
1891 spin_lock_init(&dn_rt_hash_table[i].lock);
1892 dn_rt_hash_table[i].chain = NULL;
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001895 dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001897 proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001898
1899#ifdef CONFIG_DECNET_ROUTER
Greg Rosec7ac8672011-06-10 01:27:09 +00001900 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1901 dn_fib_dump, NULL);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001902#else
1903 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
Greg Rosec7ac8672011-06-10 01:27:09 +00001904 dn_cache_dump, NULL);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001905#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
1908void __exit dn_route_cleanup(void)
1909{
1910 del_timer(&dn_route_timer);
1911 dn_run_flush(0);
1912
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001913 proc_net_remove(&init_net, "decnet_cache");
Eric Dumazetfc66f952010-10-08 06:37:34 +00001914 dst_entries_destroy(&dn_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916