blob: 9bd45fcb3b8e4b17514226dccaa4a09d415b5fbc [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{
David S. Miller69cce1d2011-07-17 23:09:49 -0700244 struct neighbour *n = dst_get_neighbour(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 u32 min_mtu = 230;
David S. Miller69cce1d2011-07-17 23:09:49 -0700246 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)) {
Joe Perches06f8fe12011-07-01 09:43:03 +0000500 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
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
Joe Perches06f8fe12011-07-01 09:43:03 +0000657 switch (flags & DN_RT_CNTL_MSK) {
658 case DN_RT_PKT_INIT:
659 dn_dev_init_pkt(skb);
660 break;
661 case DN_RT_PKT_VERI:
662 dn_dev_veri_pkt(skb);
663 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
666 if (dn->parms.state != DN_DEV_S_RU)
667 goto dump_it;
668
Joe Perches06f8fe12011-07-01 09:43:03 +0000669 switch (flags & DN_RT_CNTL_MSK) {
670 case DN_RT_PKT_HELO:
671 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
Joe Perches06f8fe12011-07-01 09:43:03 +0000675 case DN_RT_PKT_L1RT:
676 case DN_RT_PKT_L2RT:
677 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
678 skb, skb->dev, NULL,
679 dn_route_discard);
680 case DN_RT_PKT_ERTH:
681 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
Joe Perches06f8fe12011-07-01 09:43:03 +0000685 case DN_RT_PKT_EEDH:
686 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
Joe Perches06f8fe12011-07-01 09:43:03 +0000696 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
David S. Miller8f40b162011-07-17 13:34:11 -0700710static int dn_to_neigh_output(struct sk_buff *skb)
711{
712 struct dst_entry *dst = skb_dst(skb);
David S. Miller69cce1d2011-07-17 23:09:49 -0700713 struct neighbour *n = dst_get_neighbour(dst);
David S. Miller8f40b162011-07-17 13:34:11 -0700714
715 return n->output(n, skb);
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static int dn_output(struct sk_buff *skb)
719{
Eric Dumazetadf30902009-06-02 05:19:30 +0000720 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct dn_route *rt = (struct dn_route *)dst;
722 struct net_device *dev = dst->dev;
723 struct dn_skb_cb *cb = DN_SKB_CB(skb);
724 struct neighbour *neigh;
725
726 int err = -EINVAL;
727
David S. Miller69cce1d2011-07-17 23:09:49 -0700728 if ((neigh = dst_get_neighbour(dst)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 goto error;
730
731 skb->dev = dev;
732
733 cb->src = rt->rt_saddr;
734 cb->dst = rt->rt_daddr;
735
736 /*
737 * Always set the Intra-Ethernet bit on all outgoing packets
738 * originated on this node. Only valid flag from upper layers
739 * is return-to-sender-requested. Set hop count to 0 too.
740 */
741 cb->rt_flags &= ~DN_RT_F_RQR;
742 cb->rt_flags |= DN_RT_F_IE;
743 cb->hops = 0;
744
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100745 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
David S. Miller8f40b162011-07-17 13:34:11 -0700746 dn_to_neigh_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748error:
749 if (net_ratelimit())
750 printk(KERN_DEBUG "dn_output: This should not happen\n");
751
752 kfree_skb(skb);
753
754 return err;
755}
756
757static int dn_forward(struct sk_buff *skb)
758{
759 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000760 struct dst_entry *dst = skb_dst(skb);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000761 struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct dn_route *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int header_len;
764#ifdef CONFIG_NETFILTER
765 struct net_device *dev = skb->dev;
766#endif
767
768 if (skb->pkt_type != PACKET_HOST)
769 goto drop;
770
771 /* Ensure that we have enough space for headers */
Eric Dumazetadf30902009-06-02 05:19:30 +0000772 rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 header_len = dn_db->use_long ? 21 : 6;
Changli Gaod8d1f302010-06-10 23:31:35 -0700774 if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 goto drop;
776
777 /*
778 * Hop count exceeded.
779 */
780 if (++cb->hops > 30)
781 goto drop;
782
Changli Gaod8d1f302010-06-10 23:31:35 -0700783 skb->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /*
786 * If packet goes out same interface it came in on, then set
787 * the Intra-Ethernet bit. This has no effect for short
788 * packets, so we don't need to test for them here.
789 */
790 cb->rt_flags &= ~DN_RT_F_IE;
791 if (rt->rt_flags & RTCF_DOREDIRECT)
792 cb->rt_flags |= DN_RT_F_IE;
793
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100794 return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
David S. Miller8f40b162011-07-17 13:34:11 -0700795 dn_to_neigh_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797drop:
798 kfree_skb(skb);
799 return NET_RX_DROP;
800}
801
802/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * Used to catch bugs. This should never normally get
804 * called.
805 */
806static int dn_rt_bug(struct sk_buff *skb)
807{
808 if (net_ratelimit()) {
809 struct dn_skb_cb *cb = DN_SKB_CB(skb);
810
811 printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800812 le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814
815 kfree_skb(skb);
816
Florian Westphal0e8635a2009-06-20 00:53:25 +0000817 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
David S. Miller0dbaee32010-12-13 12:52:14 -0800820static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
821{
822 return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
823}
824
David S. Millerd33e4552010-12-14 13:01:14 -0800825static unsigned int dn_dst_default_mtu(const struct dst_entry *dst)
826{
827 return dst->dev->mtu;
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
831{
832 struct dn_fib_info *fi = res->fi;
Changli Gaod8d1f302010-06-10 23:31:35 -0700833 struct net_device *dev = rt->dst.dev;
David S. Miller62fa8a82011-01-26 20:51:05 -0800834 unsigned int mss_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct neighbour *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (fi) {
838 if (DN_FIB_RES_GW(*res) &&
839 DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
840 rt->rt_gateway = DN_FIB_RES_GW(*res);
David S. Miller62fa8a82011-01-26 20:51:05 -0800841 dst_init_metrics(&rt->dst, fi->fib_metrics, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 rt->rt_type = res->type;
844
David S. Miller69cce1d2011-07-17 23:09:49 -0700845 if (dev != NULL && dst_get_neighbour(&rt->dst) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
847 if (IS_ERR(n))
848 return PTR_ERR(n);
David S. Miller69cce1d2011-07-17 23:09:49 -0700849 dst_set_neighbour(&rt->dst, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
David S. Millerd33e4552010-12-14 13:01:14 -0800852 if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
David S. Millerdefb3512010-12-08 21:16:57 -0800853 dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
David S. Miller62fa8a82011-01-26 20:51:05 -0800854 mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
855 if (mss_metric) {
David S. Miller0dbaee32010-12-13 12:52:14 -0800856 unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
David S. Miller62fa8a82011-01-26 20:51:05 -0800857 if (mss_metric > mss)
David S. Miller0dbaee32010-12-13 12:52:14 -0800858 dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 0;
861}
862
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800863static inline int dn_match_addr(__le16 addr1, __le16 addr2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800865 __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 int match = 16;
867 while(tmp) {
868 tmp >>= 1;
869 match--;
870 }
871 return match;
872}
873
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800874static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800876 __le16 saddr = 0;
Eric Dumazetfc766e42010-10-29 03:09:24 +0000877 struct dn_dev *dn_db;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct dn_ifaddr *ifa;
879 int best_match = 0;
880 int ret;
881
Eric Dumazetfc766e42010-10-29 03:09:24 +0000882 rcu_read_lock();
883 dn_db = rcu_dereference(dev->dn_ptr);
884 for (ifa = rcu_dereference(dn_db->ifa_list);
885 ifa != NULL;
886 ifa = rcu_dereference(ifa->ifa_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (ifa->ifa_scope > scope)
888 continue;
889 if (!daddr) {
890 saddr = ifa->ifa_local;
891 break;
892 }
893 ret = dn_match_addr(daddr, ifa->ifa_local);
894 if (ret > best_match)
895 saddr = ifa->ifa_local;
896 if (best_match == 0)
897 saddr = ifa->ifa_local;
898 }
Eric Dumazetfc766e42010-10-29 03:09:24 +0000899 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 return saddr;
902}
903
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800904static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
907}
908
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800909static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800911 __le16 mask = dnet_make_mask(res->prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
913}
914
David S. Millerbef55ae2011-03-12 17:17:10 -0500915static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oldflp, int try_hard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
David S. Millerbef55ae2011-03-12 17:17:10 -0500917 struct flowidn fld = {
918 .daddr = oldflp->daddr,
919 .saddr = oldflp->saddr,
920 .flowidn_scope = RT_SCOPE_UNIVERSE,
921 .flowidn_mark = oldflp->flowidn_mark,
922 .flowidn_iif = init_net.loopback_dev->ifindex,
923 .flowidn_oif = oldflp->flowidn_oif,
David S. Miller1d28f422011-03-12 00:29:39 -0500924 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct dn_route *rt = NULL;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700926 struct net_device *dev_out = NULL, *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct neighbour *neigh = NULL;
928 unsigned hash;
929 unsigned flags = 0;
930 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
931 int err;
932 int free_res = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800933 __le16 gateway = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 if (decnet_debug_level & 16)
936 printk(KERN_DEBUG
937 "dn_route_output_slow: dst=%04x src=%04x mark=%d"
David S. Millerbef55ae2011-03-12 17:17:10 -0500938 " iif=%d oif=%d\n", le16_to_cpu(oldflp->daddr),
939 le16_to_cpu(oldflp->saddr),
940 oldflp->flowidn_mark, init_net.loopback_dev->ifindex,
941 oldflp->flowidn_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* If we have an output interface, verify its a DECnet device */
David S. Millerbef55ae2011-03-12 17:17:10 -0500944 if (oldflp->flowidn_oif) {
945 dev_out = dev_get_by_index(&init_net, oldflp->flowidn_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 err = -ENODEV;
947 if (dev_out && dev_out->dn_ptr == NULL) {
948 dev_put(dev_out);
949 dev_out = NULL;
950 }
951 if (dev_out == NULL)
952 goto out;
953 }
954
955 /* If we have a source address, verify that its a local address */
David S. Millerbef55ae2011-03-12 17:17:10 -0500956 if (oldflp->saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 err = -EADDRNOTAVAIL;
958
959 if (dev_out) {
David S. Millerbef55ae2011-03-12 17:17:10 -0500960 if (dn_dev_islocal(dev_out, oldflp->saddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 goto source_ok;
962 dev_put(dev_out);
963 goto out;
964 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800965 rcu_read_lock();
966 for_each_netdev_rcu(&init_net, dev) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700967 if (!dev->dn_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 continue;
David S. Millerbef55ae2011-03-12 17:17:10 -0500969 if (!dn_dev_islocal(dev, oldflp->saddr))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700970 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700971 if ((dev->flags & IFF_LOOPBACK) &&
David S. Millerbef55ae2011-03-12 17:17:10 -0500972 oldflp->daddr &&
973 !dn_dev_islocal(dev, oldflp->daddr))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700974 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700975
976 dev_out = dev;
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800979 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (dev_out == NULL)
981 goto out;
982 dev_hold(dev_out);
983source_ok:
984 ;
985 }
986
987 /* No destination? Assume its local */
David S. Millerbef55ae2011-03-12 17:17:10 -0500988 if (!fld.daddr) {
989 fld.daddr = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 err = -EADDRNOTAVAIL;
992 if (dev_out)
993 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700994 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -0500996 if (!fld.daddr) {
997 fld.daddr =
998 fld.saddr = dnet_select_source(dev_out, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 RT_SCOPE_HOST);
David S. Millerbef55ae2011-03-12 17:17:10 -05001000 if (!fld.daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto out;
1002 }
David S. Millerbef55ae2011-03-12 17:17:10 -05001003 fld.flowidn_oif = init_net.loopback_dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 res.type = RTN_LOCAL;
1005 goto make_route;
1006 }
1007
1008 if (decnet_debug_level & 16)
1009 printk(KERN_DEBUG
1010 "dn_route_output_slow: initial checks complete."
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001011 " dst=%o4x src=%04x oif=%d try_hard=%d\n",
David S. Millerbef55ae2011-03-12 17:17:10 -05001012 le16_to_cpu(fld.daddr), le16_to_cpu(fld.saddr),
1013 fld.flowidn_oif, try_hard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /*
1016 * N.B. If the kernel is compiled without router support then
1017 * dn_fib_lookup() will evaluate to non-zero so this if () block
1018 * will always be executed.
1019 */
1020 err = -ESRCH;
David S. Millerbef55ae2011-03-12 17:17:10 -05001021 if (try_hard || (err = dn_fib_lookup(&fld, &res)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct dn_dev *dn_db;
1023 if (err != -ESRCH)
1024 goto out;
1025 /*
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001026 * Here the fallback is basically the standard algorithm for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * routing in endnodes which is described in the DECnet routing
1028 * docs
1029 *
1030 * If we are not trying hard, look in neighbour cache.
1031 * The result is tested to ensure that if a specific output
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001032 * device/source address was requested, then we honour that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 * here
1034 */
1035 if (!try_hard) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001036 neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fld.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (neigh) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001038 if ((oldflp->flowidn_oif &&
1039 (neigh->dev->ifindex != oldflp->flowidn_oif)) ||
1040 (oldflp->saddr &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 (!dn_dev_islocal(neigh->dev,
David S. Millerbef55ae2011-03-12 17:17:10 -05001042 oldflp->saddr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 neigh_release(neigh);
1044 neigh = NULL;
1045 } else {
1046 if (dev_out)
1047 dev_put(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001048 if (dn_dev_islocal(neigh->dev, fld.daddr)) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001049 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 res.type = RTN_LOCAL;
1051 } else {
1052 dev_out = neigh->dev;
1053 }
1054 dev_hold(dev_out);
1055 goto select_source;
1056 }
1057 }
1058 }
1059
1060 /* Not there? Perhaps its a local address */
1061 if (dev_out == NULL)
1062 dev_out = dn_dev_get_default();
1063 err = -ENODEV;
1064 if (dev_out == NULL)
1065 goto out;
Eric Dumazetfc766e42010-10-29 03:09:24 +00001066 dn_db = rcu_dereference_raw(dev_out->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* Possible improvement - check all devices for local addr */
David S. Millerbef55ae2011-03-12 17:17:10 -05001068 if (dn_dev_islocal(dev_out, fld.daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001070 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 dev_hold(dev_out);
1072 res.type = RTN_LOCAL;
1073 goto select_source;
1074 }
1075 /* Not local either.... try sending it to the default router */
1076 neigh = neigh_clone(dn_db->router);
1077 BUG_ON(neigh && neigh->dev != dev_out);
1078
1079 /* Ok then, we assume its directly connected and move on */
1080select_source:
1081 if (neigh)
1082 gateway = ((struct dn_neigh *)neigh)->addr;
1083 if (gateway == 0)
David S. Millerbef55ae2011-03-12 17:17:10 -05001084 gateway = fld.daddr;
1085 if (fld.saddr == 0) {
1086 fld.saddr = dnet_select_source(dev_out, gateway,
1087 res.type == RTN_LOCAL ?
1088 RT_SCOPE_HOST :
1089 RT_SCOPE_LINK);
1090 if (fld.saddr == 0 && res.type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 goto e_addr;
1092 }
David S. Millerbef55ae2011-03-12 17:17:10 -05001093 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto make_route;
1095 }
1096 free_res = 1;
1097
1098 if (res.type == RTN_NAT)
1099 goto e_inval;
1100
1101 if (res.type == RTN_LOCAL) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001102 if (!fld.saddr)
1103 fld.saddr = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (dev_out)
1105 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001106 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001108 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (res.fi)
1110 dn_fib_info_put(res.fi);
1111 res.fi = NULL;
1112 goto make_route;
1113 }
1114
David S. Millerbef55ae2011-03-12 17:17:10 -05001115 if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1116 dn_fib_select_multipath(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001118 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 * We could add some logic to deal with default routes here and
1120 * get rid of some of the special casing above.
1121 */
1122
David S. Millerbef55ae2011-03-12 17:17:10 -05001123 if (!fld.saddr)
1124 fld.saddr = DN_FIB_RES_PREFSRC(res);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (dev_out)
1127 dev_put(dev_out);
1128 dev_out = DN_FIB_RES_DEV(res);
1129 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001130 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 gateway = DN_FIB_RES_GW(res);
1132
1133make_route:
1134 if (dev_out->flags & IFF_LOOPBACK)
1135 flags |= RTCF_LOCAL;
1136
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001137 rt = dst_alloc(&dn_dst_ops, dev_out, 1, 0, DST_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (rt == NULL)
1139 goto e_nobufs;
1140
David S. Millercf911662011-04-28 14:31:47 -07001141 memset(&rt->fld, 0, sizeof(rt->fld));
David S. Millerbef55ae2011-03-12 17:17:10 -05001142 rt->fld.saddr = oldflp->saddr;
1143 rt->fld.daddr = oldflp->daddr;
1144 rt->fld.flowidn_oif = oldflp->flowidn_oif;
1145 rt->fld.flowidn_iif = 0;
1146 rt->fld.flowidn_mark = oldflp->flowidn_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
David S. Millerbef55ae2011-03-12 17:17:10 -05001148 rt->rt_saddr = fld.saddr;
1149 rt->rt_daddr = fld.daddr;
1150 rt->rt_gateway = gateway ? gateway : fld.daddr;
1151 rt->rt_local_src = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
David S. Millerbef55ae2011-03-12 17:17:10 -05001153 rt->rt_dst_map = fld.daddr;
1154 rt->rt_src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
David S. Miller69cce1d2011-07-17 23:09:49 -07001156 dst_set_neighbour(&rt->dst, neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 neigh = NULL;
1158
Changli Gaod8d1f302010-06-10 23:31:35 -07001159 rt->dst.lastuse = jiffies;
1160 rt->dst.output = dn_output;
1161 rt->dst.input = dn_rt_bug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 rt->rt_flags = flags;
1163 if (flags & RTCF_LOCAL)
Changli Gaod8d1f302010-06-10 23:31:35 -07001164 rt->dst.input = dn_nsp_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 err = dn_rt_set_next_hop(rt, &res);
1167 if (err)
1168 goto e_neighbour;
1169
David S. Millerbef55ae2011-03-12 17:17:10 -05001170 hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 dn_insert_route(rt, hash, (struct dn_route **)pprt);
1172
1173done:
1174 if (neigh)
1175 neigh_release(neigh);
1176 if (free_res)
1177 dn_fib_res_put(&res);
1178 if (dev_out)
1179 dev_put(dev_out);
1180out:
1181 return err;
1182
1183e_addr:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001184 err = -EADDRNOTAVAIL;
1185 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186e_inval:
1187 err = -EINVAL;
1188 goto done;
1189e_nobufs:
1190 err = -ENOBUFS;
1191 goto done;
1192e_neighbour:
Changli Gaod8d1f302010-06-10 23:31:35 -07001193 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto e_nobufs;
1195}
1196
1197
1198/*
1199 * N.B. The flags may be moved into the flowi at some future stage.
1200 */
David S. Millerbef55ae2011-03-12 17:17:10 -05001201static int __dn_route_output_key(struct dst_entry **pprt, const struct flowidn *flp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
David S. Millerbef55ae2011-03-12 17:17:10 -05001203 unsigned hash = dn_hash(flp->saddr, flp->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct dn_route *rt = NULL;
1205
1206 if (!(flags & MSG_TRYHARD)) {
1207 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001208 for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07001209 rt = rcu_dereference_bh(rt->dst.dn_next)) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001210 if ((flp->daddr == rt->fld.daddr) &&
1211 (flp->saddr == rt->fld.saddr) &&
1212 (flp->flowidn_mark == rt->fld.flowidn_mark) &&
David S. Millerc7537962010-11-11 17:07:48 -08001213 dn_is_output_route(rt) &&
David S. Millerbef55ae2011-03-12 17:17:10 -05001214 (rt->fld.flowidn_oif == flp->flowidn_oif)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001215 dst_use(&rt->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 rcu_read_unlock_bh();
Changli Gaod8d1f302010-06-10 23:31:35 -07001217 *pprt = &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return 0;
1219 }
1220 }
1221 rcu_read_unlock_bh();
1222 }
1223
1224 return dn_route_output_slow(pprt, flp, flags);
1225}
1226
David S. Millerbef55ae2011-03-12 17:17:10 -05001227static int dn_route_output_key(struct dst_entry **pprt, struct flowidn *flp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 int err;
1230
1231 err = __dn_route_output_key(pprt, flp, flags);
David S. Millerbef55ae2011-03-12 17:17:10 -05001232 if (err == 0 && flp->flowidn_proto) {
1233 *pprt = xfrm_lookup(&init_net, *pprt,
1234 flowidn_to_flowi(flp), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -08001235 if (IS_ERR(*pprt)) {
1236 err = PTR_ERR(*pprt);
1237 *pprt = NULL;
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 return err;
1241}
1242
David S. Millerbef55ae2011-03-12 17:17:10 -05001243int dn_route_output_sock(struct dst_entry **pprt, struct flowidn *fl, struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 int err;
1246
1247 err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
David S. Millerbef55ae2011-03-12 17:17:10 -05001248 if (err == 0 && fl->flowidn_proto) {
David S. Miller80c0bc92011-03-01 14:36:37 -08001249 if (!(flags & MSG_DONTWAIT))
David S. Millerbef55ae2011-03-12 17:17:10 -05001250 fl->flowidn_flags |= FLOWI_FLAG_CAN_SLEEP;
1251 *pprt = xfrm_lookup(&init_net, *pprt,
1252 flowidn_to_flowi(fl), sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -08001253 if (IS_ERR(*pprt)) {
1254 err = PTR_ERR(*pprt);
1255 *pprt = NULL;
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 return err;
1259}
1260
1261static int dn_route_input_slow(struct sk_buff *skb)
1262{
1263 struct dn_route *rt = NULL;
1264 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1265 struct net_device *in_dev = skb->dev;
1266 struct net_device *out_dev = NULL;
1267 struct dn_dev *dn_db;
1268 struct neighbour *neigh = NULL;
1269 unsigned hash;
1270 int flags = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001271 __le16 gateway = 0;
1272 __le16 local_src = 0;
David S. Millerbef55ae2011-03-12 17:17:10 -05001273 struct flowidn fld = {
1274 .daddr = cb->dst,
1275 .saddr = cb->src,
1276 .flowidn_scope = RT_SCOPE_UNIVERSE,
1277 .flowidn_mark = skb->mark,
1278 .flowidn_iif = skb->dev->ifindex,
David S. Miller1d28f422011-03-12 00:29:39 -05001279 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1281 int err = -EINVAL;
1282 int free_res = 0;
1283
1284 dev_hold(in_dev);
1285
Eric Dumazetfc766e42010-10-29 03:09:24 +00001286 if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 goto out;
1288
1289 /* Zero source addresses are not allowed */
David S. Millerbef55ae2011-03-12 17:17:10 -05001290 if (fld.saddr == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto out;
1292
1293 /*
1294 * In this case we've just received a packet from a source
1295 * outside ourselves pretending to come from us. We don't
1296 * allow it any further to prevent routing loops, spoofing and
1297 * other nasties. Loopback packets already have the dst attached
1298 * so this only affects packets which have originated elsewhere.
1299 */
1300 err = -ENOTUNIQ;
1301 if (dn_dev_islocal(in_dev, cb->src))
1302 goto out;
1303
David S. Millerbef55ae2011-03-12 17:17:10 -05001304 err = dn_fib_lookup(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (err) {
1306 if (err != -ESRCH)
1307 goto out;
1308 /*
1309 * Is the destination us ?
1310 */
1311 if (!dn_dev_islocal(in_dev, cb->dst))
1312 goto e_inval;
1313
1314 res.type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 } else {
David S. Millerbef55ae2011-03-12 17:17:10 -05001316 __le16 src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 free_res = 1;
1318
1319 out_dev = DN_FIB_RES_DEV(res);
1320 if (out_dev == NULL) {
1321 if (net_ratelimit())
1322 printk(KERN_CRIT "Bug in dn_route_input_slow() "
1323 "No output device\n");
1324 goto e_inval;
1325 }
1326 dev_hold(out_dev);
1327
1328 if (res.r)
David S. Millerbef55ae2011-03-12 17:17:10 -05001329 src_map = fld.saddr; /* no NAT support for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 gateway = DN_FIB_RES_GW(res);
1332 if (res.type == RTN_NAT) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001333 fld.daddr = dn_fib_rules_map_destination(fld.daddr, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 dn_fib_res_put(&res);
1335 free_res = 0;
David S. Millerbef55ae2011-03-12 17:17:10 -05001336 if (dn_fib_lookup(&fld, &res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 goto e_inval;
1338 free_res = 1;
1339 if (res.type != RTN_UNICAST)
1340 goto e_inval;
1341 flags |= RTCF_DNAT;
David S. Millerbef55ae2011-03-12 17:17:10 -05001342 gateway = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
David S. Millerbef55ae2011-03-12 17:17:10 -05001344 fld.saddr = src_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
1347 switch(res.type) {
1348 case RTN_UNICAST:
1349 /*
1350 * Forwarding check here, we only check for forwarding
1351 * being turned off, if you want to only forward intra
1352 * area, its up to you to set the routing tables up
1353 * correctly.
1354 */
1355 if (dn_db->parms.forwarding == 0)
1356 goto e_inval;
1357
David S. Millerbef55ae2011-03-12 17:17:10 -05001358 if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1359 dn_fib_select_multipath(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001361 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1363 * flag as a hint to set the intra-ethernet bit when
1364 * forwarding. If we've got NAT in operation, we don't do
1365 * this optimisation.
1366 */
1367 if (out_dev == in_dev && !(flags & RTCF_NAT))
1368 flags |= RTCF_DOREDIRECT;
1369
1370 local_src = DN_FIB_RES_PREFSRC(res);
1371
1372 case RTN_BLACKHOLE:
1373 case RTN_UNREACHABLE:
1374 break;
1375 case RTN_LOCAL:
1376 flags |= RTCF_LOCAL;
David S. Millerbef55ae2011-03-12 17:17:10 -05001377 fld.saddr = cb->dst;
1378 fld.daddr = cb->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* Routing tables gave us a gateway */
1381 if (gateway)
1382 goto make_route;
1383
1384 /* Packet was intra-ethernet, so we know its on-link */
Steven Whitehouse3a31b9d2006-10-18 20:45:22 -07001385 if (cb->rt_flags & DN_RT_F_IE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 gateway = cb->src;
1387 flags |= RTCF_DIRECTSRC;
1388 goto make_route;
1389 }
1390
1391 /* Use the default router if there is one */
1392 neigh = neigh_clone(dn_db->router);
1393 if (neigh) {
1394 gateway = ((struct dn_neigh *)neigh)->addr;
1395 goto make_route;
1396 }
1397
1398 /* Close eyes and pray */
1399 gateway = cb->src;
1400 flags |= RTCF_DIRECTSRC;
1401 goto make_route;
1402 default:
1403 goto e_inval;
1404 }
1405
1406make_route:
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001407 rt = dst_alloc(&dn_dst_ops, out_dev, 0, 0, DST_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (rt == NULL)
1409 goto e_nobufs;
1410
David S. Millercf911662011-04-28 14:31:47 -07001411 memset(&rt->fld, 0, sizeof(rt->fld));
David S. Millerbef55ae2011-03-12 17:17:10 -05001412 rt->rt_saddr = fld.saddr;
1413 rt->rt_daddr = fld.daddr;
1414 rt->rt_gateway = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (gateway)
1416 rt->rt_gateway = gateway;
1417 rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
1418
David S. Millerbef55ae2011-03-12 17:17:10 -05001419 rt->rt_dst_map = fld.daddr;
1420 rt->rt_src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
David S. Millerbef55ae2011-03-12 17:17:10 -05001422 rt->fld.saddr = cb->src;
1423 rt->fld.daddr = cb->dst;
1424 rt->fld.flowidn_oif = 0;
1425 rt->fld.flowidn_iif = in_dev->ifindex;
1426 rt->fld.flowidn_mark = fld.flowidn_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
David S. Miller69cce1d2011-07-17 23:09:49 -07001428 dst_set_neighbour(&rt->dst, neigh);
Changli Gaod8d1f302010-06-10 23:31:35 -07001429 rt->dst.lastuse = jiffies;
1430 rt->dst.output = dn_rt_bug;
Joe Perches06f8fe12011-07-01 09:43:03 +00001431 switch (res.type) {
1432 case RTN_UNICAST:
1433 rt->dst.input = dn_forward;
1434 break;
1435 case RTN_LOCAL:
1436 rt->dst.output = dn_output;
1437 rt->dst.input = dn_nsp_rx;
1438 rt->dst.dev = in_dev;
1439 flags |= RTCF_LOCAL;
1440 break;
1441 default:
1442 case RTN_UNREACHABLE:
1443 case RTN_BLACKHOLE:
1444 rt->dst.input = dst_discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446 rt->rt_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 err = dn_rt_set_next_hop(rt, &res);
1449 if (err)
1450 goto e_neighbour;
1451
David S. Millerbef55ae2011-03-12 17:17:10 -05001452 hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
Eric Dumazetadf30902009-06-02 05:19:30 +00001453 dn_insert_route(rt, hash, &rt);
Changli Gaod8d1f302010-06-10 23:31:35 -07001454 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456done:
1457 if (neigh)
1458 neigh_release(neigh);
1459 if (free_res)
1460 dn_fib_res_put(&res);
1461 dev_put(in_dev);
1462 if (out_dev)
1463 dev_put(out_dev);
1464out:
1465 return err;
1466
1467e_inval:
1468 err = -EINVAL;
1469 goto done;
1470
1471e_nobufs:
1472 err = -ENOBUFS;
1473 goto done;
1474
1475e_neighbour:
Changli Gaod8d1f302010-06-10 23:31:35 -07001476 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 goto done;
1478}
1479
Roel Kluin5eaa65b2008-12-10 15:18:31 -08001480static int dn_route_input(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct dn_route *rt;
1483 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1484 unsigned hash = dn_hash(cb->src, cb->dst);
1485
Eric Dumazetadf30902009-06-02 05:19:30 +00001486 if (skb_dst(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return 0;
1488
1489 rcu_read_lock();
1490 for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
Changli Gaod8d1f302010-06-10 23:31:35 -07001491 rt = rcu_dereference(rt->dst.dn_next)) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001492 if ((rt->fld.saddr == cb->src) &&
1493 (rt->fld.daddr == cb->dst) &&
1494 (rt->fld.flowidn_oif == 0) &&
1495 (rt->fld.flowidn_mark == skb->mark) &&
1496 (rt->fld.flowidn_iif == cb->iif)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001497 dst_use(&rt->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 rcu_read_unlock();
Eric Dumazetadf30902009-06-02 05:19:30 +00001499 skb_dst_set(skb, (struct dst_entry *)rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 0;
1501 }
1502 }
1503 rcu_read_unlock();
1504
1505 return dn_route_input_slow(skb);
1506}
1507
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001508static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1509 int event, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Eric Dumazetadf30902009-06-02 05:19:30 +00001511 struct dn_route *rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 struct rtmsg *r;
1513 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001514 unsigned char *b = skb_tail_pointer(skb);
Thomas Grafe3703b32006-11-27 09:27:07 -08001515 long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001517 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 r = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 r->rtm_family = AF_DECnet;
1520 r->rtm_dst_len = 16;
1521 r->rtm_src_len = 0;
1522 r->rtm_tos = 0;
1523 r->rtm_table = RT_TABLE_MAIN;
Patrick McHardy9e762a42006-08-10 23:09:48 -07001524 RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 r->rtm_type = rt->rt_type;
1526 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1527 r->rtm_scope = RT_SCOPE_UNIVERSE;
1528 r->rtm_protocol = RTPROT_UNSPEC;
1529 if (rt->rt_flags & RTCF_NOTIFY)
1530 r->rtm_flags |= RTM_F_NOTIFY;
1531 RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
David S. Millerbef55ae2011-03-12 17:17:10 -05001532 if (rt->fld.saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 r->rtm_src_len = 16;
David S. Millerbef55ae2011-03-12 17:17:10 -05001534 RTA_PUT(skb, RTA_SRC, 2, &rt->fld.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001536 if (rt->dst.dev)
1537 RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /*
1539 * Note to self - change this if input routes reverse direction when
1540 * they deal only with inputs and not with replies like they do
1541 * currently.
1542 */
1543 RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
1544 if (rt->rt_daddr != rt->rt_gateway)
1545 RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
David S. Millerdefb3512010-12-08 21:16:57 -08001546 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 goto rtattr_failure;
Changli Gaod8d1f302010-06-10 23:31:35 -07001548 expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
1549 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
1550 rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08001551 goto rtattr_failure;
David S. Millerc7537962010-11-11 17:07:48 -08001552 if (dn_is_input_route(rt))
David S. Millerbef55ae2011-03-12 17:17:10 -05001553 RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fld.flowidn_iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001555 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return skb->len;
1557
1558nlmsg_failure:
1559rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001560 nlmsg_trim(skb, b);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001561 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564/*
1565 * This is called by both endnodes and routers now.
1566 */
Thomas Graffa34ddd2007-03-22 11:57:46 -07001567static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001569 struct net *net = sock_net(in_skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 struct rtattr **rta = arg;
1571 struct rtmsg *rtm = NLMSG_DATA(nlh);
1572 struct dn_route *rt = NULL;
1573 struct dn_skb_cb *cb;
1574 int err;
1575 struct sk_buff *skb;
David S. Millerbef55ae2011-03-12 17:17:10 -05001576 struct flowidn fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001578 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001579 return -EINVAL;
1580
David S. Millerbef55ae2011-03-12 17:17:10 -05001581 memset(&fld, 0, sizeof(fld));
1582 fld.flowidn_proto = DNPROTO_NSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1585 if (skb == NULL)
1586 return -ENOBUFS;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001587 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 cb = DN_SKB_CB(skb);
1589
1590 if (rta[RTA_SRC-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001591 memcpy(&fld.saddr, RTA_DATA(rta[RTA_SRC-1]), 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (rta[RTA_DST-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001593 memcpy(&fld.daddr, RTA_DATA(rta[RTA_DST-1]), 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (rta[RTA_IIF-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001595 memcpy(&fld.flowidn_iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
David S. Millerbef55ae2011-03-12 17:17:10 -05001597 if (fld.flowidn_iif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 struct net_device *dev;
David S. Millerbef55ae2011-03-12 17:17:10 -05001599 if ((dev = dev_get_by_index(&init_net, fld.flowidn_iif)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 kfree_skb(skb);
1601 return -ENODEV;
1602 }
1603 if (!dev->dn_ptr) {
1604 dev_put(dev);
1605 kfree_skb(skb);
1606 return -ENODEV;
1607 }
YOSHIFUJI Hideakib98999d2007-12-12 03:51:49 +09001608 skb->protocol = htons(ETH_P_DNA_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 skb->dev = dev;
David S. Millerbef55ae2011-03-12 17:17:10 -05001610 cb->src = fld.saddr;
1611 cb->dst = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 local_bh_disable();
1613 err = dn_route_input(skb);
1614 local_bh_enable();
1615 memset(cb, 0, sizeof(struct dn_skb_cb));
Eric Dumazetadf30902009-06-02 05:19:30 +00001616 rt = (struct dn_route *)skb_dst(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001617 if (!err && -rt->dst.error)
1618 err = rt->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 } else {
1620 int oif = 0;
1621 if (rta[RTA_OIF - 1])
1622 memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
David S. Millerbef55ae2011-03-12 17:17:10 -05001623 fld.flowidn_oif = oif;
1624 err = dn_route_output_key((struct dst_entry **)&rt, &fld, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626
1627 if (skb->dev)
1628 dev_put(skb->dev);
1629 skb->dev = NULL;
1630 if (err)
1631 goto out_free;
Changli Gaod8d1f302010-06-10 23:31:35 -07001632 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (rtm->rtm_flags & RTM_F_NOTIFY)
1634 rt->rt_flags |= RTCF_NOTIFY;
1635
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001636 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 -07001637
1638 if (err == 0)
1639 goto out_free;
1640 if (err < 0) {
1641 err = -EMSGSIZE;
1642 goto out_free;
1643 }
1644
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001645 return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647out_free:
1648 kfree_skb(skb);
1649 return err;
1650}
1651
1652/*
1653 * For routers, this is called from dn_fib_dump, but for endnodes its
1654 * called directly from the rtnetlink dispatch table.
1655 */
1656int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1657{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001658 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct dn_route *rt;
1660 int h, s_h;
1661 int idx, s_idx;
1662
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001663 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001664 return 0;
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
1667 return -EINVAL;
1668 if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
1669 return 0;
1670
1671 s_h = cb->args[0];
1672 s_idx = idx = cb->args[1];
1673 for(h = 0; h <= dn_rt_hash_mask; h++) {
1674 if (h < s_h)
1675 continue;
1676 if (h > s_h)
1677 s_idx = 0;
1678 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001679 for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07001681 rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (idx < s_idx)
1683 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001684 skb_dst_set(skb, dst_clone(&rt->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001686 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001687 1, NLM_F_MULTI) <= 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001688 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 rcu_read_unlock_bh();
1690 goto done;
1691 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001692 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694 rcu_read_unlock_bh();
1695 }
1696
1697done:
1698 cb->args[0] = h;
1699 cb->args[1] = idx;
1700 return skb->len;
1701}
1702
1703#ifdef CONFIG_PROC_FS
1704struct dn_rt_cache_iter_state {
1705 int bucket;
1706};
1707
1708static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1709{
1710 struct dn_route *rt = NULL;
1711 struct dn_rt_cache_iter_state *s = seq->private;
1712
1713 for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1714 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001715 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (rt)
1717 break;
1718 rcu_read_unlock_bh();
1719 }
Paul E. McKenneya898def2010-02-22 17:04:49 -08001720 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
1723static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1724{
Eric Dumazet0d89d792008-01-10 22:35:21 -08001725 struct dn_rt_cache_iter_state *s = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Eric Dumazetfc766e42010-10-29 03:09:24 +00001727 rt = rcu_dereference_bh(rt->dst.dn_next);
1728 while (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 rcu_read_unlock_bh();
1730 if (--s->bucket < 0)
1731 break;
1732 rcu_read_lock_bh();
Eric Dumazetfc766e42010-10-29 03:09:24 +00001733 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
Eric Dumazetfc766e42010-10-29 03:09:24 +00001735 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
1738static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1739{
1740 struct dn_route *rt = dn_rt_cache_get_first(seq);
1741
1742 if (rt) {
1743 while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1744 --*pos;
1745 }
1746 return *pos ? NULL : rt;
1747}
1748
1749static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1750{
1751 struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1752 ++*pos;
1753 return rt;
1754}
1755
1756static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1757{
1758 if (v)
1759 rcu_read_unlock_bh();
1760}
1761
1762static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1763{
1764 struct dn_route *rt = v;
1765 char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1766
1767 seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07001768 rt->dst.dev ? rt->dst.dev->name : "*",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -08001769 dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1770 dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
Changli Gaod8d1f302010-06-10 23:31:35 -07001771 atomic_read(&rt->dst.__refcnt),
1772 rt->dst.__use,
1773 (int) dst_metric(&rt->dst, RTAX_RTT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001775}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001777static const struct seq_operations dn_rt_cache_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 .start = dn_rt_cache_seq_start,
1779 .next = dn_rt_cache_seq_next,
1780 .stop = dn_rt_cache_seq_stop,
1781 .show = dn_rt_cache_seq_show,
1782};
1783
1784static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1785{
Pavel Emelyanov31164082007-10-10 02:30:23 -07001786 return seq_open_private(file, &dn_rt_cache_seq_ops,
1787 sizeof(struct dn_rt_cache_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Arjan van de Ven9a321442007-02-12 00:55:35 -08001790static const struct file_operations dn_rt_cache_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 .owner = THIS_MODULE,
1792 .open = dn_rt_cache_seq_open,
1793 .read = seq_read,
1794 .llseek = seq_lseek,
1795 .release = seq_release_private,
1796};
1797
1798#endif /* CONFIG_PROC_FS */
1799
1800void __init dn_route_init(void)
1801{
1802 int i, goal, order;
1803
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07001804 dn_dst_ops.kmem_cachep =
1805 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001806 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001807 dst_entries_init(&dn_dst_ops);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001808 setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1810 add_timer(&dn_route_timer);
1811
Jan Beulich44813742009-09-21 17:03:05 -07001812 goal = totalram_pages >> (26 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 for(order = 0; (1UL << order) < goal; order++)
1815 /* NOTHING */;
1816
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001817 /*
1818 * Only want 1024 entries max, since the table is very, very unlikely
1819 * to be larger than that.
1820 */
1821 while(order && ((((1UL << order) * PAGE_SIZE) /
1822 sizeof(struct dn_rt_hash_bucket)) >= 2048))
1823 order--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001825 do {
1826 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1827 sizeof(struct dn_rt_hash_bucket);
1828 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1829 dn_rt_hash_mask--;
1830 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1831 __get_free_pages(GFP_ATOMIC, order);
1832 } while (dn_rt_hash_table == NULL && --order > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 if (!dn_rt_hash_table)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001835 panic("Failed to allocate DECnet route cache hash table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001837 printk(KERN_INFO
1838 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1839 dn_rt_hash_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1841
1842 dn_rt_hash_mask--;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001843 for(i = 0; i <= dn_rt_hash_mask; i++) {
1844 spin_lock_init(&dn_rt_hash_table[i].lock);
1845 dn_rt_hash_table[i].chain = NULL;
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001848 dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001850 proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001851
1852#ifdef CONFIG_DECNET_ROUTER
Greg Rosec7ac8672011-06-10 01:27:09 +00001853 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1854 dn_fib_dump, NULL);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001855#else
1856 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
Greg Rosec7ac8672011-06-10 01:27:09 +00001857 dn_cache_dump, NULL);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001858#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
1861void __exit dn_route_cleanup(void)
1862{
1863 del_timer(&dn_route_timer);
1864 dn_run_flush(0);
1865
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001866 proc_net_remove(&init_net, "decnet_cache");
Eric Dumazetfc66f952010-10-08 06:37:34 +00001867 dst_entries_destroy(&dn_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869