blob: b91b60363c3926ddf7ac3d4626b5f52ce77d0802 [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>
80#include <asm/errno.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020081#include <net/net_namespace.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070082#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <net/neighbour.h>
84#include <net/dst.h>
85#include <net/flow.h>
Steven Whitehousea8731cb2006-08-09 15:56:46 -070086#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#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
94struct dn_rt_hash_bucket
95{
Eric Dumazetfc766e42010-10-29 03:09:24 +000096 struct dn_route __rcu *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 spinlock_t lock;
Eric Dumazetfca09fb2008-02-07 23:29:57 -080098};
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100extern struct neigh_table dn_neigh_table;
101
102
103static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};
104
105static const int dn_rt_min_delay = 2 * HZ;
106static const int dn_rt_max_delay = 10 * HZ;
107static const int dn_rt_mtu_expires = 10 * 60 * HZ;
108
109static unsigned long dn_rt_deadline;
110
Daniel Lezcano569d3642008-01-18 03:56:57 -0800111static int dn_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);
David S. Miller0dbaee32010-12-13 12:52:14 -0800113static unsigned int dn_dst_default_advmss(const struct dst_entry *dst);
David S. Millerd33e4552010-12-14 13:01:14 -0800114static unsigned int dn_dst_default_mtu(const struct dst_entry *dst);
David S. Miller62fa8a82011-01-26 20:51:05 -0800115static void dn_dst_destroy(struct dst_entry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
117static void dn_dst_link_failure(struct sk_buff *);
118static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu);
119static int dn_route_input(struct sk_buff *);
120static void dn_run_flush(unsigned long dummy);
121
122static struct dn_rt_hash_bucket *dn_rt_hash_table;
123static unsigned dn_rt_hash_mask;
124
125static struct timer_list dn_route_timer;
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700126static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127int decnet_dst_gc_interval = 2;
128
129static struct dst_ops dn_dst_ops = {
130 .family = PF_DECnet,
Harvey Harrison09640e62009-02-01 00:45:17 -0800131 .protocol = cpu_to_be16(ETH_P_DNA_RT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .gc_thresh = 128,
133 .gc = dn_dst_gc,
134 .check = dn_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800135 .default_advmss = dn_dst_default_advmss,
David S. Millerd33e4552010-12-14 13:01:14 -0800136 .default_mtu = dn_dst_default_mtu,
David S. Miller62fa8a82011-01-26 20:51:05 -0800137 .cow_metrics = dst_cow_metrics_generic,
138 .destroy = dn_dst_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .negative_advice = dn_dst_negative_advice,
140 .link_failure = dn_dst_link_failure,
141 .update_pmtu = dn_dst_update_pmtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
David S. Miller62fa8a82011-01-26 20:51:05 -0800144static void dn_dst_destroy(struct dst_entry *dst)
145{
146 dst_destroy_metrics_generic(dst);
147}
148
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800149static __inline__ unsigned dn_hash(__le16 src, __le16 dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800151 __u16 tmp = (__u16 __force)(src ^ dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 tmp ^= (tmp >> 3);
153 tmp ^= (tmp >> 5);
154 tmp ^= (tmp >> 10);
155 return dn_rt_hash_mask & (unsigned)tmp;
156}
157
158static inline void dnrt_free(struct dn_route *rt)
159{
Changli Gaod8d1f302010-06-10 23:31:35 -0700160 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163static inline void dnrt_drop(struct dn_route *rt)
164{
Changli Gaod8d1f302010-06-10 23:31:35 -0700165 dst_release(&rt->dst);
166 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169static void dn_dst_check_expire(unsigned long dummy)
170{
171 int i;
Eric Dumazetfc766e42010-10-29 03:09:24 +0000172 struct dn_route *rt;
173 struct dn_route __rcu **rtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned long now = jiffies;
175 unsigned long expire = 120 * HZ;
176
Eric Dumazetfc766e42010-10-29 03:09:24 +0000177 for (i = 0; i <= dn_rt_hash_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 rtp = &dn_rt_hash_table[i].chain;
179
180 spin_lock(&dn_rt_hash_table[i].lock);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000181 while ((rt = rcu_dereference_protected(*rtp,
182 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700183 if (atomic_read(&rt->dst.__refcnt) ||
184 (now - rt->dst.lastuse) < expire) {
185 rtp = &rt->dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 continue;
187 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700188 *rtp = rt->dst.dn_next;
189 rt->dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 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 Lezcano569d3642008-01-18 03:56:57 -0800201static int dn_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Eric Dumazetfc766e42010-10-29 03:09:24 +0000203 struct dn_route *rt;
204 struct dn_route __rcu **rtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int i;
206 unsigned long now = jiffies;
207 unsigned long expire = 10 * HZ;
208
Eric Dumazetfc766e42010-10-29 03:09:24 +0000209 for (i = 0; i <= dn_rt_hash_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 spin_lock_bh(&dn_rt_hash_table[i].lock);
212 rtp = &dn_rt_hash_table[i].chain;
213
Eric Dumazetfc766e42010-10-29 03:09:24 +0000214 while ((rt = rcu_dereference_protected(*rtp,
215 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700216 if (atomic_read(&rt->dst.__refcnt) ||
217 (now - rt->dst.lastuse) < expire) {
218 rtp = &rt->dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 continue;
220 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700221 *rtp = rt->dst.dn_next;
222 rt->dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 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 */
242static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu)
243{
Eric Dumazet8a533662012-02-09 16:13:19 -0500244 struct neighbour *n = dst_get_neighbour(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 u32 min_mtu = 230;
Eric Dumazet8a533662012-02-09 16:13:19 -0500246 struct dn_dev *dn;
247
248 dn = n ? rcu_dereference_raw(n->dev->dn_ptr) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (dn && dn->use_long == 0)
251 min_mtu -= 6;
252 else
253 min_mtu -= 21;
254
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700255 if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!(dst_metric_locked(dst, RTAX_MTU))) {
David S. Millerdefb3512010-12-08 21:16:57 -0800257 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 dst_set_expires(dst, dn_rt_mtu_expires);
259 }
260 if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
261 u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
David S. Miller0dbaee32010-12-13 12:52:14 -0800262 u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
263 if (!existing_mss || existing_mss > mss)
David S. Millerdefb3512010-12-08 21:16:57 -0800264 dst_metric_set(dst, RTAX_ADVMSS, mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 }
267}
268
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900269/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * When a route has been marked obsolete. (e.g. routing cache flush)
271 */
272static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
273{
274 return NULL;
275}
276
277static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
278{
279 dst_release(dst);
280 return NULL;
281}
282
283static void dn_dst_link_failure(struct sk_buff *skb)
284{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
David S. Millerbef55ae2011-03-12 17:17:10 -0500287static inline int compare_keys(struct flowidn *fl1, struct flowidn *fl2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
David S. Millerbef55ae2011-03-12 17:17:10 -0500289 return ((fl1->daddr ^ fl2->daddr) |
290 (fl1->saddr ^ fl2->saddr) |
291 (fl1->flowidn_mark ^ fl2->flowidn_mark) |
292 (fl1->flowidn_scope ^ fl2->flowidn_scope) |
293 (fl1->flowidn_oif ^ fl2->flowidn_oif) |
294 (fl1->flowidn_iif ^ fl2->flowidn_iif)) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297static int dn_insert_route(struct dn_route *rt, unsigned hash, struct dn_route **rp)
298{
Eric Dumazetfc766e42010-10-29 03:09:24 +0000299 struct dn_route *rth;
300 struct dn_route __rcu **rthp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned long now = jiffies;
302
303 rthp = &dn_rt_hash_table[hash].chain;
304
305 spin_lock_bh(&dn_rt_hash_table[hash].lock);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000306 while ((rth = rcu_dereference_protected(*rthp,
307 lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
David S. Millerbef55ae2011-03-12 17:17:10 -0500308 if (compare_keys(&rth->fld, &rt->fld)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* Put it first */
Changli Gaod8d1f302010-06-10 23:31:35 -0700310 *rthp = rth->dst.dn_next;
311 rcu_assign_pointer(rth->dst.dn_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 dn_rt_hash_table[hash].chain);
313 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
314
Changli Gaod8d1f302010-06-10 23:31:35 -0700315 dst_use(&rth->dst, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
317
318 dnrt_drop(rt);
319 *rp = rth;
320 return 0;
321 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700322 rthp = &rth->dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
Changli Gaod8d1f302010-06-10 23:31:35 -0700325 rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900327
Changli Gaod8d1f302010-06-10 23:31:35 -0700328 dst_use(&rt->dst, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
330 *rp = rt;
331 return 0;
332}
333
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800334static void dn_run_flush(unsigned long dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 int i;
337 struct dn_route *rt, *next;
338
Eric Dumazetfc766e42010-10-29 03:09:24 +0000339 for (i = 0; i < dn_rt_hash_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 spin_lock_bh(&dn_rt_hash_table[i].lock);
341
Eric Dumazetfc766e42010-10-29 03:09:24 +0000342 if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto nothing_to_declare;
344
Eric Dumazetfc766e42010-10-29 03:09:24 +0000345 for(; rt; rt = next) {
346 next = rcu_dereference_raw(rt->dst.dn_next);
347 RCU_INIT_POINTER(rt->dst.dn_next, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 dst_free((struct dst_entry *)rt);
349 }
350
351nothing_to_declare:
352 spin_unlock_bh(&dn_rt_hash_table[i].lock);
353 }
354}
355
356static DEFINE_SPINLOCK(dn_rt_flush_lock);
357
358void dn_rt_cache_flush(int delay)
359{
360 unsigned long now = jiffies;
361 int user_mode = !in_interrupt();
362
363 if (delay < 0)
364 delay = dn_rt_min_delay;
365
366 spin_lock_bh(&dn_rt_flush_lock);
367
368 if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
369 long tmo = (long)(dn_rt_deadline - now);
370
371 if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
372 tmo = 0;
373
374 if (delay > tmo)
375 delay = tmo;
376 }
377
378 if (delay <= 0) {
379 spin_unlock_bh(&dn_rt_flush_lock);
380 dn_run_flush(0);
381 return;
382 }
383
384 if (dn_rt_deadline == 0)
385 dn_rt_deadline = now + dn_rt_max_delay;
386
387 dn_rt_flush_timer.expires = now + delay;
388 add_timer(&dn_rt_flush_timer);
389 spin_unlock_bh(&dn_rt_flush_lock);
390}
391
392/**
393 * dn_return_short - Return a short packet to its sender
394 * @skb: The packet to return
395 *
396 */
397static int dn_return_short(struct sk_buff *skb)
398{
399 struct dn_skb_cb *cb;
400 unsigned char *ptr;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800401 __le16 *src;
402 __le16 *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* Add back headers */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700405 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
408 return NET_RX_DROP;
409
410 cb = DN_SKB_CB(skb);
411 /* Skip packet length and point to flags */
412 ptr = skb->data + 2;
413 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
414
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800415 dst = (__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800417 src = (__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ptr += 2;
419 *ptr = 0; /* Zero hop count */
420
Ilpo Järvinena0bffff2009-03-21 13:36:17 -0700421 swap(*src, *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 skb->pkt_type = PACKET_OUTGOING;
424 dn_rt_finish_output(skb, NULL, NULL);
425 return NET_RX_SUCCESS;
426}
427
428/**
429 * dn_return_long - Return a long packet to its sender
430 * @skb: The long format packet to return
431 *
432 */
433static int dn_return_long(struct sk_buff *skb)
434{
435 struct dn_skb_cb *cb;
436 unsigned char *ptr;
437 unsigned char *src_addr, *dst_addr;
438 unsigned char tmp[ETH_ALEN];
439
440 /* Add back all headers */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700441 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
444 return NET_RX_DROP;
445
446 cb = DN_SKB_CB(skb);
447 /* Ignore packet length and point to flags */
448 ptr = skb->data + 2;
449
450 /* Skip padding */
451 if (*ptr & DN_RT_F_PF) {
452 char padlen = (*ptr & ~DN_RT_F_PF);
453 ptr += padlen;
454 }
455
456 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
457 ptr += 2;
458 dst_addr = ptr;
459 ptr += 8;
460 src_addr = ptr;
461 ptr += 6;
462 *ptr = 0; /* Zero hop count */
463
464 /* Swap source and destination */
465 memcpy(tmp, src_addr, ETH_ALEN);
466 memcpy(src_addr, dst_addr, ETH_ALEN);
467 memcpy(dst_addr, tmp, ETH_ALEN);
468
469 skb->pkt_type = PACKET_OUTGOING;
470 dn_rt_finish_output(skb, dst_addr, src_addr);
471 return NET_RX_SUCCESS;
472}
473
474/**
475 * dn_route_rx_packet - Try and find a route for an incoming packet
476 * @skb: The packet to find a route for
477 *
478 * Returns: result of input function if route is found, error code otherwise
479 */
480static int dn_route_rx_packet(struct sk_buff *skb)
481{
Eric Dumazetfc766e42010-10-29 03:09:24 +0000482 struct dn_skb_cb *cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int err;
484
485 if ((err = dn_route_input(skb)) == 0)
486 return dst_input(skb);
487
Eric Dumazetfc766e42010-10-29 03:09:24 +0000488 cb = DN_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (decnet_debug_level & 4) {
490 char *devname = skb->dev ? skb->dev->name : "???";
Eric Dumazetfc766e42010-10-29 03:09:24 +0000491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 printk(KERN_DEBUG
493 "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 -0800494 (int)cb->rt_flags, devname, skb->len,
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800495 le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 err, skb->pkt_type);
497 }
498
499 if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
500 switch(cb->rt_flags & DN_RT_PKT_MSK) {
501 case DN_RT_PKT_SHORT:
502 return dn_return_short(skb);
503 case DN_RT_PKT_LONG:
504 return dn_return_long(skb);
505 }
506 }
507
508 kfree_skb(skb);
509 return NET_RX_DROP;
510}
511
512static int dn_route_rx_long(struct sk_buff *skb)
513{
514 struct dn_skb_cb *cb = DN_SKB_CB(skb);
515 unsigned char *ptr = skb->data;
516
517 if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
518 goto drop_it;
519
520 skb_pull(skb, 20);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300521 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900523 /* Destination info */
524 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800525 cb->dst = dn_eth2dn(ptr);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900526 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
527 goto drop_it;
528 ptr += 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900531 /* Source info */
532 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800533 cb->src = dn_eth2dn(ptr);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900534 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
535 goto drop_it;
536 ptr += 6;
537 /* Other junk */
538 ptr++;
539 cb->hops = *ptr++; /* Visit Count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100541 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
542 dn_route_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544drop_it:
545 kfree_skb(skb);
546 return NET_RX_DROP;
547}
548
549
550
551static int dn_route_rx_short(struct sk_buff *skb)
552{
553 struct dn_skb_cb *cb = DN_SKB_CB(skb);
554 unsigned char *ptr = skb->data;
555
556 if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
557 goto drop_it;
558
559 skb_pull(skb, 5);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300560 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800562 cb->dst = *(__le16 *)ptr;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900563 ptr += 2;
564 cb->src = *(__le16 *)ptr;
565 ptr += 2;
566 cb->hops = *ptr & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100568 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
569 dn_route_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571drop_it:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900572 kfree_skb(skb);
573 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576static int dn_route_discard(struct sk_buff *skb)
577{
578 /*
579 * I know we drop the packet here, but thats considered success in
580 * this case
581 */
582 kfree_skb(skb);
583 return NET_RX_SUCCESS;
584}
585
586static int dn_route_ptp_hello(struct sk_buff *skb)
587{
588 dn_dev_hello(skb);
589 dn_neigh_pointopoint_hello(skb);
590 return NET_RX_SUCCESS;
591}
592
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700593int 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 -0700594{
595 struct dn_skb_cb *cb;
596 unsigned char flags = 0;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800597 __u16 len = le16_to_cpu(*(__le16 *)skb->data);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000598 struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 unsigned char padlen = 0;
600
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700601 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700602 goto dump_it;
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (dn == NULL)
605 goto dump_it;
606
607 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
608 goto out;
609
610 if (!pskb_may_pull(skb, 3))
611 goto dump_it;
612
613 skb_pull(skb, 2);
614
615 if (len > skb->len)
616 goto dump_it;
617
618 skb_trim(skb, len);
619
620 flags = *skb->data;
621
622 cb = DN_SKB_CB(skb);
623 cb->stamp = jiffies;
624 cb->iif = dev->ifindex;
625
626 /*
627 * If we have padding, remove it.
628 */
629 if (flags & DN_RT_F_PF) {
630 padlen = flags & ~DN_RT_F_PF;
631 if (!pskb_may_pull(skb, padlen + 1))
632 goto dump_it;
633 skb_pull(skb, padlen);
634 flags = *skb->data;
635 }
636
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700637 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /*
640 * Weed out future version DECnet
641 */
642 if (flags & DN_RT_F_VER)
643 goto dump_it;
644
645 cb->rt_flags = flags;
646
647 if (decnet_debug_level & 1)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900648 printk(KERN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900650 (int)flags, (dev) ? dev->name : "???", len, skb->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 padlen);
652
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900653 if (flags & DN_RT_PKT_CNTL) {
Herbert Xu364c6ba2006-06-09 16:10:40 -0700654 if (unlikely(skb_linearize(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto dump_it;
656
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900657 switch(flags & DN_RT_CNTL_MSK) {
658 case DN_RT_PKT_INIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 dn_dev_init_pkt(skb);
660 break;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900661 case DN_RT_PKT_VERI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 dn_dev_veri_pkt(skb);
663 break;
664 }
665
666 if (dn->parms.state != DN_DEV_S_RU)
667 goto dump_it;
668
669 switch(flags & DN_RT_CNTL_MSK) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900670 case DN_RT_PKT_HELO:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100671 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
672 skb, skb->dev, NULL,
673 dn_route_ptp_hello);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900675 case DN_RT_PKT_L1RT:
676 case DN_RT_PKT_L2RT:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100677 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
678 skb, skb->dev, NULL,
679 dn_route_discard);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900680 case DN_RT_PKT_ERTH:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100681 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
682 skb, skb->dev, NULL,
683 dn_neigh_router_hello);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900685 case DN_RT_PKT_EEDH:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100686 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
687 skb, skb->dev, NULL,
688 dn_neigh_endnode_hello);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900689 }
690 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (dn->parms.state != DN_DEV_S_RU)
692 goto dump_it;
693
694 skb_pull(skb, 1); /* Pull flags */
695
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900696 switch(flags & DN_RT_PKT_MSK) {
697 case DN_RT_PKT_LONG:
698 return dn_route_rx_long(skb);
699 case DN_RT_PKT_SHORT:
700 return dn_route_rx_short(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704dump_it:
705 kfree_skb(skb);
706out:
707 return NET_RX_DROP;
708}
709
710static int dn_output(struct sk_buff *skb)
711{
Eric Dumazetadf30902009-06-02 05:19:30 +0000712 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct dn_route *rt = (struct dn_route *)dst;
714 struct net_device *dev = dst->dev;
715 struct dn_skb_cb *cb = DN_SKB_CB(skb);
716 struct neighbour *neigh;
717
718 int err = -EINVAL;
719
Eric Dumazet8a533662012-02-09 16:13:19 -0500720 if ((neigh = dst_get_neighbour(dst)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goto error;
722
723 skb->dev = dev;
724
725 cb->src = rt->rt_saddr;
726 cb->dst = rt->rt_daddr;
727
728 /*
729 * Always set the Intra-Ethernet bit on all outgoing packets
730 * originated on this node. Only valid flag from upper layers
731 * is return-to-sender-requested. Set hop count to 0 too.
732 */
733 cb->rt_flags &= ~DN_RT_F_RQR;
734 cb->rt_flags |= DN_RT_F_IE;
735 cb->hops = 0;
736
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100737 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
738 neigh->output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740error:
741 if (net_ratelimit())
742 printk(KERN_DEBUG "dn_output: This should not happen\n");
743
744 kfree_skb(skb);
745
746 return err;
747}
748
749static int dn_forward(struct sk_buff *skb)
750{
751 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000752 struct dst_entry *dst = skb_dst(skb);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000753 struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 struct dn_route *rt;
Eric Dumazet8a533662012-02-09 16:13:19 -0500755 struct neighbour *neigh = dst_get_neighbour(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 int header_len;
757#ifdef CONFIG_NETFILTER
758 struct net_device *dev = skb->dev;
759#endif
760
761 if (skb->pkt_type != PACKET_HOST)
762 goto drop;
763
764 /* Ensure that we have enough space for headers */
Eric Dumazetadf30902009-06-02 05:19:30 +0000765 rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 header_len = dn_db->use_long ? 21 : 6;
Changli Gaod8d1f302010-06-10 23:31:35 -0700767 if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 goto drop;
769
770 /*
771 * Hop count exceeded.
772 */
773 if (++cb->hops > 30)
774 goto drop;
775
Changli Gaod8d1f302010-06-10 23:31:35 -0700776 skb->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /*
779 * If packet goes out same interface it came in on, then set
780 * the Intra-Ethernet bit. This has no effect for short
781 * packets, so we don't need to test for them here.
782 */
783 cb->rt_flags &= ~DN_RT_F_IE;
784 if (rt->rt_flags & RTCF_DOREDIRECT)
785 cb->rt_flags |= DN_RT_F_IE;
786
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100787 return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
788 neigh->output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790drop:
791 kfree_skb(skb);
792 return NET_RX_DROP;
793}
794
795/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 * Used to catch bugs. This should never normally get
797 * called.
798 */
799static int dn_rt_bug(struct sk_buff *skb)
800{
801 if (net_ratelimit()) {
802 struct dn_skb_cb *cb = DN_SKB_CB(skb);
803
804 printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800805 le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808 kfree_skb(skb);
809
Florian Westphal0e8635a2009-06-20 00:53:25 +0000810 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
David S. Miller0dbaee32010-12-13 12:52:14 -0800813static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
814{
815 return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
816}
817
David S. Millerd33e4552010-12-14 13:01:14 -0800818static unsigned int dn_dst_default_mtu(const struct dst_entry *dst)
819{
820 return dst->dev->mtu;
821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
824{
825 struct dn_fib_info *fi = res->fi;
Changli Gaod8d1f302010-06-10 23:31:35 -0700826 struct net_device *dev = rt->dst.dev;
David S. Miller62fa8a82011-01-26 20:51:05 -0800827 unsigned int mss_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 struct neighbour *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 if (fi) {
831 if (DN_FIB_RES_GW(*res) &&
832 DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
833 rt->rt_gateway = DN_FIB_RES_GW(*res);
David S. Miller62fa8a82011-01-26 20:51:05 -0800834 dst_init_metrics(&rt->dst, fi->fib_metrics, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 rt->rt_type = res->type;
837
Eric Dumazet8a533662012-02-09 16:13:19 -0500838 if (dev != NULL && dst_get_neighbour(&rt->dst) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
840 if (IS_ERR(n))
841 return PTR_ERR(n);
Eric Dumazet8a533662012-02-09 16:13:19 -0500842 dst_set_neighbour(&rt->dst, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
David S. Millerd33e4552010-12-14 13:01:14 -0800845 if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
David S. Millerdefb3512010-12-08 21:16:57 -0800846 dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
David S. Miller62fa8a82011-01-26 20:51:05 -0800847 mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
848 if (mss_metric) {
David S. Miller0dbaee32010-12-13 12:52:14 -0800849 unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
David S. Miller62fa8a82011-01-26 20:51:05 -0800850 if (mss_metric > mss)
David S. Miller0dbaee32010-12-13 12:52:14 -0800851 dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return 0;
854}
855
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800856static inline int dn_match_addr(__le16 addr1, __le16 addr2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800858 __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 int match = 16;
860 while(tmp) {
861 tmp >>= 1;
862 match--;
863 }
864 return match;
865}
866
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800867static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800869 __le16 saddr = 0;
Eric Dumazetfc766e42010-10-29 03:09:24 +0000870 struct dn_dev *dn_db;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct dn_ifaddr *ifa;
872 int best_match = 0;
873 int ret;
874
Eric Dumazetfc766e42010-10-29 03:09:24 +0000875 rcu_read_lock();
876 dn_db = rcu_dereference(dev->dn_ptr);
877 for (ifa = rcu_dereference(dn_db->ifa_list);
878 ifa != NULL;
879 ifa = rcu_dereference(ifa->ifa_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (ifa->ifa_scope > scope)
881 continue;
882 if (!daddr) {
883 saddr = ifa->ifa_local;
884 break;
885 }
886 ret = dn_match_addr(daddr, ifa->ifa_local);
887 if (ret > best_match)
888 saddr = ifa->ifa_local;
889 if (best_match == 0)
890 saddr = ifa->ifa_local;
891 }
Eric Dumazetfc766e42010-10-29 03:09:24 +0000892 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 return saddr;
895}
896
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800897static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
900}
901
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800902static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800904 __le16 mask = dnet_make_mask(res->prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
906}
907
David S. Millerbef55ae2011-03-12 17:17:10 -0500908static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oldflp, int try_hard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
David S. Millerbef55ae2011-03-12 17:17:10 -0500910 struct flowidn fld = {
911 .daddr = oldflp->daddr,
912 .saddr = oldflp->saddr,
913 .flowidn_scope = RT_SCOPE_UNIVERSE,
914 .flowidn_mark = oldflp->flowidn_mark,
915 .flowidn_iif = init_net.loopback_dev->ifindex,
916 .flowidn_oif = oldflp->flowidn_oif,
David S. Miller1d28f422011-03-12 00:29:39 -0500917 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct dn_route *rt = NULL;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700919 struct net_device *dev_out = NULL, *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct neighbour *neigh = NULL;
921 unsigned hash;
922 unsigned flags = 0;
923 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
924 int err;
925 int free_res = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800926 __le16 gateway = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (decnet_debug_level & 16)
929 printk(KERN_DEBUG
930 "dn_route_output_slow: dst=%04x src=%04x mark=%d"
David S. Millerbef55ae2011-03-12 17:17:10 -0500931 " iif=%d oif=%d\n", le16_to_cpu(oldflp->daddr),
932 le16_to_cpu(oldflp->saddr),
933 oldflp->flowidn_mark, init_net.loopback_dev->ifindex,
934 oldflp->flowidn_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /* If we have an output interface, verify its a DECnet device */
David S. Millerbef55ae2011-03-12 17:17:10 -0500937 if (oldflp->flowidn_oif) {
938 dev_out = dev_get_by_index(&init_net, oldflp->flowidn_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 err = -ENODEV;
940 if (dev_out && dev_out->dn_ptr == NULL) {
941 dev_put(dev_out);
942 dev_out = NULL;
943 }
944 if (dev_out == NULL)
945 goto out;
946 }
947
948 /* If we have a source address, verify that its a local address */
David S. Millerbef55ae2011-03-12 17:17:10 -0500949 if (oldflp->saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 err = -EADDRNOTAVAIL;
951
952 if (dev_out) {
David S. Millerbef55ae2011-03-12 17:17:10 -0500953 if (dn_dev_islocal(dev_out, oldflp->saddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 goto source_ok;
955 dev_put(dev_out);
956 goto out;
957 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800958 rcu_read_lock();
959 for_each_netdev_rcu(&init_net, dev) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700960 if (!dev->dn_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 continue;
David S. Millerbef55ae2011-03-12 17:17:10 -0500962 if (!dn_dev_islocal(dev, oldflp->saddr))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700963 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700964 if ((dev->flags & IFF_LOOPBACK) &&
David S. Millerbef55ae2011-03-12 17:17:10 -0500965 oldflp->daddr &&
966 !dn_dev_islocal(dev, oldflp->daddr))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700967 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700968
969 dev_out = dev;
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700970 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800972 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (dev_out == NULL)
974 goto out;
975 dev_hold(dev_out);
976source_ok:
977 ;
978 }
979
980 /* No destination? Assume its local */
David S. Millerbef55ae2011-03-12 17:17:10 -0500981 if (!fld.daddr) {
982 fld.daddr = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 err = -EADDRNOTAVAIL;
985 if (dev_out)
986 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700987 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -0500989 if (!fld.daddr) {
990 fld.daddr =
991 fld.saddr = dnet_select_source(dev_out, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 RT_SCOPE_HOST);
David S. Millerbef55ae2011-03-12 17:17:10 -0500993 if (!fld.daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto out;
995 }
David S. Millerbef55ae2011-03-12 17:17:10 -0500996 fld.flowidn_oif = init_net.loopback_dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 res.type = RTN_LOCAL;
998 goto make_route;
999 }
1000
1001 if (decnet_debug_level & 16)
1002 printk(KERN_DEBUG
1003 "dn_route_output_slow: initial checks complete."
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001004 " dst=%o4x src=%04x oif=%d try_hard=%d\n",
David S. Millerbef55ae2011-03-12 17:17:10 -05001005 le16_to_cpu(fld.daddr), le16_to_cpu(fld.saddr),
1006 fld.flowidn_oif, try_hard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /*
1009 * N.B. If the kernel is compiled without router support then
1010 * dn_fib_lookup() will evaluate to non-zero so this if () block
1011 * will always be executed.
1012 */
1013 err = -ESRCH;
David S. Millerbef55ae2011-03-12 17:17:10 -05001014 if (try_hard || (err = dn_fib_lookup(&fld, &res)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 struct dn_dev *dn_db;
1016 if (err != -ESRCH)
1017 goto out;
1018 /*
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001019 * Here the fallback is basically the standard algorithm for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * routing in endnodes which is described in the DECnet routing
1021 * docs
1022 *
1023 * If we are not trying hard, look in neighbour cache.
1024 * The result is tested to ensure that if a specific output
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001025 * device/source address was requested, then we honour that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 * here
1027 */
1028 if (!try_hard) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001029 neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fld.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (neigh) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001031 if ((oldflp->flowidn_oif &&
1032 (neigh->dev->ifindex != oldflp->flowidn_oif)) ||
1033 (oldflp->saddr &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 (!dn_dev_islocal(neigh->dev,
David S. Millerbef55ae2011-03-12 17:17:10 -05001035 oldflp->saddr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 neigh_release(neigh);
1037 neigh = NULL;
1038 } else {
1039 if (dev_out)
1040 dev_put(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001041 if (dn_dev_islocal(neigh->dev, fld.daddr)) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001042 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 res.type = RTN_LOCAL;
1044 } else {
1045 dev_out = neigh->dev;
1046 }
1047 dev_hold(dev_out);
1048 goto select_source;
1049 }
1050 }
1051 }
1052
1053 /* Not there? Perhaps its a local address */
1054 if (dev_out == NULL)
1055 dev_out = dn_dev_get_default();
1056 err = -ENODEV;
1057 if (dev_out == NULL)
1058 goto out;
Eric Dumazetfc766e42010-10-29 03:09:24 +00001059 dn_db = rcu_dereference_raw(dev_out->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* Possible improvement - check all devices for local addr */
David S. Millerbef55ae2011-03-12 17:17:10 -05001061 if (dn_dev_islocal(dev_out, fld.daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001063 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 dev_hold(dev_out);
1065 res.type = RTN_LOCAL;
1066 goto select_source;
1067 }
1068 /* Not local either.... try sending it to the default router */
1069 neigh = neigh_clone(dn_db->router);
1070 BUG_ON(neigh && neigh->dev != dev_out);
1071
1072 /* Ok then, we assume its directly connected and move on */
1073select_source:
1074 if (neigh)
1075 gateway = ((struct dn_neigh *)neigh)->addr;
1076 if (gateway == 0)
David S. Millerbef55ae2011-03-12 17:17:10 -05001077 gateway = fld.daddr;
1078 if (fld.saddr == 0) {
1079 fld.saddr = dnet_select_source(dev_out, gateway,
1080 res.type == RTN_LOCAL ?
1081 RT_SCOPE_HOST :
1082 RT_SCOPE_LINK);
1083 if (fld.saddr == 0 && res.type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto e_addr;
1085 }
David S. Millerbef55ae2011-03-12 17:17:10 -05001086 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 goto make_route;
1088 }
1089 free_res = 1;
1090
1091 if (res.type == RTN_NAT)
1092 goto e_inval;
1093
1094 if (res.type == RTN_LOCAL) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001095 if (!fld.saddr)
1096 fld.saddr = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (dev_out)
1098 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001099 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001101 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (res.fi)
1103 dn_fib_info_put(res.fi);
1104 res.fi = NULL;
1105 goto make_route;
1106 }
1107
David S. Millerbef55ae2011-03-12 17:17:10 -05001108 if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1109 dn_fib_select_multipath(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001111 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * We could add some logic to deal with default routes here and
1113 * get rid of some of the special casing above.
1114 */
1115
David S. Millerbef55ae2011-03-12 17:17:10 -05001116 if (!fld.saddr)
1117 fld.saddr = DN_FIB_RES_PREFSRC(res);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (dev_out)
1120 dev_put(dev_out);
1121 dev_out = DN_FIB_RES_DEV(res);
1122 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001123 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 gateway = DN_FIB_RES_GW(res);
1125
1126make_route:
1127 if (dev_out->flags & IFF_LOOPBACK)
1128 flags |= RTCF_LOCAL;
1129
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001130 rt = dst_alloc(&dn_dst_ops, dev_out, 1, 0, DST_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (rt == NULL)
1132 goto e_nobufs;
1133
David S. Millercf911662011-04-28 14:31:47 -07001134 memset(&rt->fld, 0, sizeof(rt->fld));
David S. Millerbef55ae2011-03-12 17:17:10 -05001135 rt->fld.saddr = oldflp->saddr;
1136 rt->fld.daddr = oldflp->daddr;
1137 rt->fld.flowidn_oif = oldflp->flowidn_oif;
1138 rt->fld.flowidn_iif = 0;
1139 rt->fld.flowidn_mark = oldflp->flowidn_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
David S. Millerbef55ae2011-03-12 17:17:10 -05001141 rt->rt_saddr = fld.saddr;
1142 rt->rt_daddr = fld.daddr;
1143 rt->rt_gateway = gateway ? gateway : fld.daddr;
1144 rt->rt_local_src = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
David S. Millerbef55ae2011-03-12 17:17:10 -05001146 rt->rt_dst_map = fld.daddr;
1147 rt->rt_src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Eric Dumazet8a533662012-02-09 16:13:19 -05001149 dst_set_neighbour(&rt->dst, neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 neigh = NULL;
1151
Changli Gaod8d1f302010-06-10 23:31:35 -07001152 rt->dst.lastuse = jiffies;
1153 rt->dst.output = dn_output;
1154 rt->dst.input = dn_rt_bug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 rt->rt_flags = flags;
1156 if (flags & RTCF_LOCAL)
Changli Gaod8d1f302010-06-10 23:31:35 -07001157 rt->dst.input = dn_nsp_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 err = dn_rt_set_next_hop(rt, &res);
1160 if (err)
1161 goto e_neighbour;
1162
David S. Millerbef55ae2011-03-12 17:17:10 -05001163 hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 dn_insert_route(rt, hash, (struct dn_route **)pprt);
1165
1166done:
1167 if (neigh)
1168 neigh_release(neigh);
1169 if (free_res)
1170 dn_fib_res_put(&res);
1171 if (dev_out)
1172 dev_put(dev_out);
1173out:
1174 return err;
1175
1176e_addr:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001177 err = -EADDRNOTAVAIL;
1178 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179e_inval:
1180 err = -EINVAL;
1181 goto done;
1182e_nobufs:
1183 err = -ENOBUFS;
1184 goto done;
1185e_neighbour:
Changli Gaod8d1f302010-06-10 23:31:35 -07001186 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto e_nobufs;
1188}
1189
1190
1191/*
1192 * N.B. The flags may be moved into the flowi at some future stage.
1193 */
David S. Millerbef55ae2011-03-12 17:17:10 -05001194static int __dn_route_output_key(struct dst_entry **pprt, const struct flowidn *flp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
David S. Millerbef55ae2011-03-12 17:17:10 -05001196 unsigned hash = dn_hash(flp->saddr, flp->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 struct dn_route *rt = NULL;
1198
1199 if (!(flags & MSG_TRYHARD)) {
1200 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001201 for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07001202 rt = rcu_dereference_bh(rt->dst.dn_next)) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001203 if ((flp->daddr == rt->fld.daddr) &&
1204 (flp->saddr == rt->fld.saddr) &&
1205 (flp->flowidn_mark == rt->fld.flowidn_mark) &&
David S. Millerc7537962010-11-11 17:07:48 -08001206 dn_is_output_route(rt) &&
David S. Millerbef55ae2011-03-12 17:17:10 -05001207 (rt->fld.flowidn_oif == flp->flowidn_oif)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001208 dst_use(&rt->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 rcu_read_unlock_bh();
Changli Gaod8d1f302010-06-10 23:31:35 -07001210 *pprt = &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return 0;
1212 }
1213 }
1214 rcu_read_unlock_bh();
1215 }
1216
1217 return dn_route_output_slow(pprt, flp, flags);
1218}
1219
David S. Millerbef55ae2011-03-12 17:17:10 -05001220static int dn_route_output_key(struct dst_entry **pprt, struct flowidn *flp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 int err;
1223
1224 err = __dn_route_output_key(pprt, flp, flags);
David S. Millerbef55ae2011-03-12 17:17:10 -05001225 if (err == 0 && flp->flowidn_proto) {
1226 *pprt = xfrm_lookup(&init_net, *pprt,
1227 flowidn_to_flowi(flp), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -08001228 if (IS_ERR(*pprt)) {
1229 err = PTR_ERR(*pprt);
1230 *pprt = NULL;
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233 return err;
1234}
1235
David S. Millerbef55ae2011-03-12 17:17:10 -05001236int dn_route_output_sock(struct dst_entry **pprt, struct flowidn *fl, struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 int err;
1239
1240 err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
David S. Millerbef55ae2011-03-12 17:17:10 -05001241 if (err == 0 && fl->flowidn_proto) {
David S. Miller80c0bc92011-03-01 14:36:37 -08001242 if (!(flags & MSG_DONTWAIT))
David S. Millerbef55ae2011-03-12 17:17:10 -05001243 fl->flowidn_flags |= FLOWI_FLAG_CAN_SLEEP;
1244 *pprt = xfrm_lookup(&init_net, *pprt,
1245 flowidn_to_flowi(fl), sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -08001246 if (IS_ERR(*pprt)) {
1247 err = PTR_ERR(*pprt);
1248 *pprt = NULL;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 return err;
1252}
1253
1254static int dn_route_input_slow(struct sk_buff *skb)
1255{
1256 struct dn_route *rt = NULL;
1257 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1258 struct net_device *in_dev = skb->dev;
1259 struct net_device *out_dev = NULL;
1260 struct dn_dev *dn_db;
1261 struct neighbour *neigh = NULL;
1262 unsigned hash;
1263 int flags = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001264 __le16 gateway = 0;
1265 __le16 local_src = 0;
David S. Millerbef55ae2011-03-12 17:17:10 -05001266 struct flowidn fld = {
1267 .daddr = cb->dst,
1268 .saddr = cb->src,
1269 .flowidn_scope = RT_SCOPE_UNIVERSE,
1270 .flowidn_mark = skb->mark,
1271 .flowidn_iif = skb->dev->ifindex,
David S. Miller1d28f422011-03-12 00:29:39 -05001272 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1274 int err = -EINVAL;
1275 int free_res = 0;
1276
1277 dev_hold(in_dev);
1278
Eric Dumazetfc766e42010-10-29 03:09:24 +00001279 if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 goto out;
1281
1282 /* Zero source addresses are not allowed */
David S. Millerbef55ae2011-03-12 17:17:10 -05001283 if (fld.saddr == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto out;
1285
1286 /*
1287 * In this case we've just received a packet from a source
1288 * outside ourselves pretending to come from us. We don't
1289 * allow it any further to prevent routing loops, spoofing and
1290 * other nasties. Loopback packets already have the dst attached
1291 * so this only affects packets which have originated elsewhere.
1292 */
1293 err = -ENOTUNIQ;
1294 if (dn_dev_islocal(in_dev, cb->src))
1295 goto out;
1296
David S. Millerbef55ae2011-03-12 17:17:10 -05001297 err = dn_fib_lookup(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (err) {
1299 if (err != -ESRCH)
1300 goto out;
1301 /*
1302 * Is the destination us ?
1303 */
1304 if (!dn_dev_islocal(in_dev, cb->dst))
1305 goto e_inval;
1306
1307 res.type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 } else {
David S. Millerbef55ae2011-03-12 17:17:10 -05001309 __le16 src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 free_res = 1;
1311
1312 out_dev = DN_FIB_RES_DEV(res);
1313 if (out_dev == NULL) {
1314 if (net_ratelimit())
1315 printk(KERN_CRIT "Bug in dn_route_input_slow() "
1316 "No output device\n");
1317 goto e_inval;
1318 }
1319 dev_hold(out_dev);
1320
1321 if (res.r)
David S. Millerbef55ae2011-03-12 17:17:10 -05001322 src_map = fld.saddr; /* no NAT support for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 gateway = DN_FIB_RES_GW(res);
1325 if (res.type == RTN_NAT) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001326 fld.daddr = dn_fib_rules_map_destination(fld.daddr, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 dn_fib_res_put(&res);
1328 free_res = 0;
David S. Millerbef55ae2011-03-12 17:17:10 -05001329 if (dn_fib_lookup(&fld, &res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 goto e_inval;
1331 free_res = 1;
1332 if (res.type != RTN_UNICAST)
1333 goto e_inval;
1334 flags |= RTCF_DNAT;
David S. Millerbef55ae2011-03-12 17:17:10 -05001335 gateway = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
David S. Millerbef55ae2011-03-12 17:17:10 -05001337 fld.saddr = src_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
1340 switch(res.type) {
1341 case RTN_UNICAST:
1342 /*
1343 * Forwarding check here, we only check for forwarding
1344 * being turned off, if you want to only forward intra
1345 * area, its up to you to set the routing tables up
1346 * correctly.
1347 */
1348 if (dn_db->parms.forwarding == 0)
1349 goto e_inval;
1350
David S. Millerbef55ae2011-03-12 17:17:10 -05001351 if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1352 dn_fib_select_multipath(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001354 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1356 * flag as a hint to set the intra-ethernet bit when
1357 * forwarding. If we've got NAT in operation, we don't do
1358 * this optimisation.
1359 */
1360 if (out_dev == in_dev && !(flags & RTCF_NAT))
1361 flags |= RTCF_DOREDIRECT;
1362
1363 local_src = DN_FIB_RES_PREFSRC(res);
1364
1365 case RTN_BLACKHOLE:
1366 case RTN_UNREACHABLE:
1367 break;
1368 case RTN_LOCAL:
1369 flags |= RTCF_LOCAL;
David S. Millerbef55ae2011-03-12 17:17:10 -05001370 fld.saddr = cb->dst;
1371 fld.daddr = cb->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /* Routing tables gave us a gateway */
1374 if (gateway)
1375 goto make_route;
1376
1377 /* Packet was intra-ethernet, so we know its on-link */
Steven Whitehouse3a31b9d2006-10-18 20:45:22 -07001378 if (cb->rt_flags & DN_RT_F_IE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 gateway = cb->src;
1380 flags |= RTCF_DIRECTSRC;
1381 goto make_route;
1382 }
1383
1384 /* Use the default router if there is one */
1385 neigh = neigh_clone(dn_db->router);
1386 if (neigh) {
1387 gateway = ((struct dn_neigh *)neigh)->addr;
1388 goto make_route;
1389 }
1390
1391 /* Close eyes and pray */
1392 gateway = cb->src;
1393 flags |= RTCF_DIRECTSRC;
1394 goto make_route;
1395 default:
1396 goto e_inval;
1397 }
1398
1399make_route:
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001400 rt = dst_alloc(&dn_dst_ops, out_dev, 0, 0, DST_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (rt == NULL)
1402 goto e_nobufs;
1403
David S. Millercf911662011-04-28 14:31:47 -07001404 memset(&rt->fld, 0, sizeof(rt->fld));
David S. Millerbef55ae2011-03-12 17:17:10 -05001405 rt->rt_saddr = fld.saddr;
1406 rt->rt_daddr = fld.daddr;
1407 rt->rt_gateway = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (gateway)
1409 rt->rt_gateway = gateway;
1410 rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
1411
David S. Millerbef55ae2011-03-12 17:17:10 -05001412 rt->rt_dst_map = fld.daddr;
1413 rt->rt_src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
David S. Millerbef55ae2011-03-12 17:17:10 -05001415 rt->fld.saddr = cb->src;
1416 rt->fld.daddr = cb->dst;
1417 rt->fld.flowidn_oif = 0;
1418 rt->fld.flowidn_iif = in_dev->ifindex;
1419 rt->fld.flowidn_mark = fld.flowidn_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Eric Dumazet8a533662012-02-09 16:13:19 -05001421 dst_set_neighbour(&rt->dst, neigh);
Changli Gaod8d1f302010-06-10 23:31:35 -07001422 rt->dst.lastuse = jiffies;
1423 rt->dst.output = dn_rt_bug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 switch(res.type) {
1425 case RTN_UNICAST:
Changli Gaod8d1f302010-06-10 23:31:35 -07001426 rt->dst.input = dn_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 break;
1428 case RTN_LOCAL:
Changli Gaod8d1f302010-06-10 23:31:35 -07001429 rt->dst.output = dn_output;
1430 rt->dst.input = dn_nsp_rx;
1431 rt->dst.dev = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 flags |= RTCF_LOCAL;
1433 break;
1434 default:
1435 case RTN_UNREACHABLE:
1436 case RTN_BLACKHOLE:
Changli Gaod8d1f302010-06-10 23:31:35 -07001437 rt->dst.input = dst_discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439 rt->rt_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 err = dn_rt_set_next_hop(rt, &res);
1442 if (err)
1443 goto e_neighbour;
1444
David S. Millerbef55ae2011-03-12 17:17:10 -05001445 hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
Eric Dumazetadf30902009-06-02 05:19:30 +00001446 dn_insert_route(rt, hash, &rt);
Changli Gaod8d1f302010-06-10 23:31:35 -07001447 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449done:
1450 if (neigh)
1451 neigh_release(neigh);
1452 if (free_res)
1453 dn_fib_res_put(&res);
1454 dev_put(in_dev);
1455 if (out_dev)
1456 dev_put(out_dev);
1457out:
1458 return err;
1459
1460e_inval:
1461 err = -EINVAL;
1462 goto done;
1463
1464e_nobufs:
1465 err = -ENOBUFS;
1466 goto done;
1467
1468e_neighbour:
Changli Gaod8d1f302010-06-10 23:31:35 -07001469 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 goto done;
1471}
1472
Roel Kluin5eaa65b2008-12-10 15:18:31 -08001473static int dn_route_input(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct dn_route *rt;
1476 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1477 unsigned hash = dn_hash(cb->src, cb->dst);
1478
Eric Dumazetadf30902009-06-02 05:19:30 +00001479 if (skb_dst(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
1481
1482 rcu_read_lock();
1483 for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
Changli Gaod8d1f302010-06-10 23:31:35 -07001484 rt = rcu_dereference(rt->dst.dn_next)) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001485 if ((rt->fld.saddr == cb->src) &&
1486 (rt->fld.daddr == cb->dst) &&
1487 (rt->fld.flowidn_oif == 0) &&
1488 (rt->fld.flowidn_mark == skb->mark) &&
1489 (rt->fld.flowidn_iif == cb->iif)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001490 dst_use(&rt->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 rcu_read_unlock();
Eric Dumazetadf30902009-06-02 05:19:30 +00001492 skb_dst_set(skb, (struct dst_entry *)rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return 0;
1494 }
1495 }
1496 rcu_read_unlock();
1497
1498 return dn_route_input_slow(skb);
1499}
1500
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001501static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1502 int event, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Eric Dumazetadf30902009-06-02 05:19:30 +00001504 struct dn_route *rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 struct rtmsg *r;
1506 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001507 unsigned char *b = skb_tail_pointer(skb);
Thomas Grafe3703b32006-11-27 09:27:07 -08001508 long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001510 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 r = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 r->rtm_family = AF_DECnet;
1513 r->rtm_dst_len = 16;
1514 r->rtm_src_len = 0;
1515 r->rtm_tos = 0;
1516 r->rtm_table = RT_TABLE_MAIN;
Patrick McHardy9e762a42006-08-10 23:09:48 -07001517 RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 r->rtm_type = rt->rt_type;
1519 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1520 r->rtm_scope = RT_SCOPE_UNIVERSE;
1521 r->rtm_protocol = RTPROT_UNSPEC;
1522 if (rt->rt_flags & RTCF_NOTIFY)
1523 r->rtm_flags |= RTM_F_NOTIFY;
1524 RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
David S. Millerbef55ae2011-03-12 17:17:10 -05001525 if (rt->fld.saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 r->rtm_src_len = 16;
David S. Millerbef55ae2011-03-12 17:17:10 -05001527 RTA_PUT(skb, RTA_SRC, 2, &rt->fld.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001529 if (rt->dst.dev)
1530 RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /*
1532 * Note to self - change this if input routes reverse direction when
1533 * they deal only with inputs and not with replies like they do
1534 * currently.
1535 */
1536 RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
1537 if (rt->rt_daddr != rt->rt_gateway)
1538 RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
David S. Millerdefb3512010-12-08 21:16:57 -08001539 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 goto rtattr_failure;
Changli Gaod8d1f302010-06-10 23:31:35 -07001541 expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
1542 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
1543 rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08001544 goto rtattr_failure;
David S. Millerc7537962010-11-11 17:07:48 -08001545 if (dn_is_input_route(rt))
David S. Millerbef55ae2011-03-12 17:17:10 -05001546 RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fld.flowidn_iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001548 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return skb->len;
1550
1551nlmsg_failure:
1552rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001553 nlmsg_trim(skb, b);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001554 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557/*
1558 * This is called by both endnodes and routers now.
1559 */
Thomas Graffa34ddd2007-03-22 11:57:46 -07001560static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001562 struct net *net = sock_net(in_skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 struct rtattr **rta = arg;
1564 struct rtmsg *rtm = NLMSG_DATA(nlh);
1565 struct dn_route *rt = NULL;
1566 struct dn_skb_cb *cb;
1567 int err;
1568 struct sk_buff *skb;
David S. Millerbef55ae2011-03-12 17:17:10 -05001569 struct flowidn fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001571 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001572 return -EINVAL;
1573
David S. Millerbef55ae2011-03-12 17:17:10 -05001574 memset(&fld, 0, sizeof(fld));
1575 fld.flowidn_proto = DNPROTO_NSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1578 if (skb == NULL)
1579 return -ENOBUFS;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001580 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 cb = DN_SKB_CB(skb);
1582
1583 if (rta[RTA_SRC-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001584 memcpy(&fld.saddr, RTA_DATA(rta[RTA_SRC-1]), 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (rta[RTA_DST-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001586 memcpy(&fld.daddr, RTA_DATA(rta[RTA_DST-1]), 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (rta[RTA_IIF-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001588 memcpy(&fld.flowidn_iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
David S. Millerbef55ae2011-03-12 17:17:10 -05001590 if (fld.flowidn_iif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 struct net_device *dev;
David S. Millerbef55ae2011-03-12 17:17:10 -05001592 if ((dev = dev_get_by_index(&init_net, fld.flowidn_iif)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 kfree_skb(skb);
1594 return -ENODEV;
1595 }
1596 if (!dev->dn_ptr) {
1597 dev_put(dev);
1598 kfree_skb(skb);
1599 return -ENODEV;
1600 }
YOSHIFUJI Hideakib98999d2007-12-12 03:51:49 +09001601 skb->protocol = htons(ETH_P_DNA_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 skb->dev = dev;
David S. Millerbef55ae2011-03-12 17:17:10 -05001603 cb->src = fld.saddr;
1604 cb->dst = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 local_bh_disable();
1606 err = dn_route_input(skb);
1607 local_bh_enable();
1608 memset(cb, 0, sizeof(struct dn_skb_cb));
Eric Dumazetadf30902009-06-02 05:19:30 +00001609 rt = (struct dn_route *)skb_dst(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001610 if (!err && -rt->dst.error)
1611 err = rt->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 } else {
1613 int oif = 0;
1614 if (rta[RTA_OIF - 1])
1615 memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
David S. Millerbef55ae2011-03-12 17:17:10 -05001616 fld.flowidn_oif = oif;
1617 err = dn_route_output_key((struct dst_entry **)&rt, &fld, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619
1620 if (skb->dev)
1621 dev_put(skb->dev);
1622 skb->dev = NULL;
1623 if (err)
1624 goto out_free;
Changli Gaod8d1f302010-06-10 23:31:35 -07001625 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (rtm->rtm_flags & RTM_F_NOTIFY)
1627 rt->rt_flags |= RTCF_NOTIFY;
1628
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001629 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 -07001630
1631 if (err == 0)
1632 goto out_free;
1633 if (err < 0) {
1634 err = -EMSGSIZE;
1635 goto out_free;
1636 }
1637
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001638 return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640out_free:
1641 kfree_skb(skb);
1642 return err;
1643}
1644
1645/*
1646 * For routers, this is called from dn_fib_dump, but for endnodes its
1647 * called directly from the rtnetlink dispatch table.
1648 */
1649int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1650{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001651 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct dn_route *rt;
1653 int h, s_h;
1654 int idx, s_idx;
1655
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001656 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001657 return 0;
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
1660 return -EINVAL;
1661 if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
1662 return 0;
1663
1664 s_h = cb->args[0];
1665 s_idx = idx = cb->args[1];
1666 for(h = 0; h <= dn_rt_hash_mask; h++) {
1667 if (h < s_h)
1668 continue;
1669 if (h > s_h)
1670 s_idx = 0;
1671 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001672 for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07001674 rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (idx < s_idx)
1676 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001677 skb_dst_set(skb, dst_clone(&rt->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001679 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001680 1, NLM_F_MULTI) <= 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001681 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 rcu_read_unlock_bh();
1683 goto done;
1684 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001685 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
1687 rcu_read_unlock_bh();
1688 }
1689
1690done:
1691 cb->args[0] = h;
1692 cb->args[1] = idx;
1693 return skb->len;
1694}
1695
1696#ifdef CONFIG_PROC_FS
1697struct dn_rt_cache_iter_state {
1698 int bucket;
1699};
1700
1701static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1702{
1703 struct dn_route *rt = NULL;
1704 struct dn_rt_cache_iter_state *s = seq->private;
1705
1706 for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1707 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001708 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (rt)
1710 break;
1711 rcu_read_unlock_bh();
1712 }
Paul E. McKenneya898def2010-02-22 17:04:49 -08001713 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
1716static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1717{
Eric Dumazet0d89d792008-01-10 22:35:21 -08001718 struct dn_rt_cache_iter_state *s = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Eric Dumazetfc766e42010-10-29 03:09:24 +00001720 rt = rcu_dereference_bh(rt->dst.dn_next);
1721 while (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 rcu_read_unlock_bh();
1723 if (--s->bucket < 0)
1724 break;
1725 rcu_read_lock_bh();
Eric Dumazetfc766e42010-10-29 03:09:24 +00001726 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
Eric Dumazetfc766e42010-10-29 03:09:24 +00001728 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
1731static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1732{
1733 struct dn_route *rt = dn_rt_cache_get_first(seq);
1734
1735 if (rt) {
1736 while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1737 --*pos;
1738 }
1739 return *pos ? NULL : rt;
1740}
1741
1742static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1743{
1744 struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1745 ++*pos;
1746 return rt;
1747}
1748
1749static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1750{
1751 if (v)
1752 rcu_read_unlock_bh();
1753}
1754
1755static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1756{
1757 struct dn_route *rt = v;
1758 char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1759
1760 seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07001761 rt->dst.dev ? rt->dst.dev->name : "*",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -08001762 dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1763 dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
Changli Gaod8d1f302010-06-10 23:31:35 -07001764 atomic_read(&rt->dst.__refcnt),
1765 rt->dst.__use,
1766 (int) dst_metric(&rt->dst, RTAX_RTT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001770static const struct seq_operations dn_rt_cache_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 .start = dn_rt_cache_seq_start,
1772 .next = dn_rt_cache_seq_next,
1773 .stop = dn_rt_cache_seq_stop,
1774 .show = dn_rt_cache_seq_show,
1775};
1776
1777static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1778{
Pavel Emelyanov31164082007-10-10 02:30:23 -07001779 return seq_open_private(file, &dn_rt_cache_seq_ops,
1780 sizeof(struct dn_rt_cache_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Arjan van de Ven9a321442007-02-12 00:55:35 -08001783static const struct file_operations dn_rt_cache_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 .owner = THIS_MODULE,
1785 .open = dn_rt_cache_seq_open,
1786 .read = seq_read,
1787 .llseek = seq_lseek,
1788 .release = seq_release_private,
1789};
1790
1791#endif /* CONFIG_PROC_FS */
1792
1793void __init dn_route_init(void)
1794{
1795 int i, goal, order;
1796
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07001797 dn_dst_ops.kmem_cachep =
1798 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001799 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001800 dst_entries_init(&dn_dst_ops);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001801 setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1803 add_timer(&dn_route_timer);
1804
Jan Beulich44813742009-09-21 17:03:05 -07001805 goal = totalram_pages >> (26 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 for(order = 0; (1UL << order) < goal; order++)
1808 /* NOTHING */;
1809
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001810 /*
1811 * Only want 1024 entries max, since the table is very, very unlikely
1812 * to be larger than that.
1813 */
1814 while(order && ((((1UL << order) * PAGE_SIZE) /
1815 sizeof(struct dn_rt_hash_bucket)) >= 2048))
1816 order--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001818 do {
1819 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1820 sizeof(struct dn_rt_hash_bucket);
1821 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1822 dn_rt_hash_mask--;
1823 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1824 __get_free_pages(GFP_ATOMIC, order);
1825 } while (dn_rt_hash_table == NULL && --order > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 if (!dn_rt_hash_table)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001828 panic("Failed to allocate DECnet route cache hash table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001830 printk(KERN_INFO
1831 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1832 dn_rt_hash_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1834
1835 dn_rt_hash_mask--;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001836 for(i = 0; i <= dn_rt_hash_mask; i++) {
1837 spin_lock_init(&dn_rt_hash_table[i].lock);
1838 dn_rt_hash_table[i].chain = NULL;
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001841 dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001843 proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001844
1845#ifdef CONFIG_DECNET_ROUTER
1846 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, dn_fib_dump);
1847#else
1848 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1849 dn_cache_dump);
1850#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853void __exit dn_route_cleanup(void)
1854{
1855 del_timer(&dn_route_timer);
1856 dn_run_flush(0);
1857
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001858 proc_net_remove(&init_net, "decnet_cache");
Eric Dumazetfc66f952010-10-08 06:37:34 +00001859 dst_entries_destroy(&dn_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861