blob: f489b081c25d4313ad0adbf119a55b6c28a9157a [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{
244 u32 min_mtu = 230;
245 struct dn_dev *dn = dst->neighbour ?
Eric Dumazetfc766e42010-10-29 03:09:24 +0000246 rcu_dereference_raw(dst->neighbour->dev->dn_ptr) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 if (dn && dn->use_long == 0)
249 min_mtu -= 6;
250 else
251 min_mtu -= 21;
252
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700253 if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!(dst_metric_locked(dst, RTAX_MTU))) {
David S. Millerdefb3512010-12-08 21:16:57 -0800255 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 dst_set_expires(dst, dn_rt_mtu_expires);
257 }
258 if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
259 u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
David S. Miller0dbaee32010-12-13 12:52:14 -0800260 u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
261 if (!existing_mss || existing_mss > mss)
David S. Millerdefb3512010-12-08 21:16:57 -0800262 dst_metric_set(dst, RTAX_ADVMSS, mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 }
265}
266
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900267/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * When a route has been marked obsolete. (e.g. routing cache flush)
269 */
270static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
271{
272 return NULL;
273}
274
275static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
276{
277 dst_release(dst);
278 return NULL;
279}
280
281static void dn_dst_link_failure(struct sk_buff *skb)
282{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
David S. Millerbef55ae2011-03-12 17:17:10 -0500285static inline int compare_keys(struct flowidn *fl1, struct flowidn *fl2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
David S. Millerbef55ae2011-03-12 17:17:10 -0500287 return ((fl1->daddr ^ fl2->daddr) |
288 (fl1->saddr ^ fl2->saddr) |
289 (fl1->flowidn_mark ^ fl2->flowidn_mark) |
290 (fl1->flowidn_scope ^ fl2->flowidn_scope) |
291 (fl1->flowidn_oif ^ fl2->flowidn_oif) |
292 (fl1->flowidn_iif ^ fl2->flowidn_iif)) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295static int dn_insert_route(struct dn_route *rt, unsigned hash, struct dn_route **rp)
296{
Eric Dumazetfc766e42010-10-29 03:09:24 +0000297 struct dn_route *rth;
298 struct dn_route __rcu **rthp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unsigned long now = jiffies;
300
301 rthp = &dn_rt_hash_table[hash].chain;
302
303 spin_lock_bh(&dn_rt_hash_table[hash].lock);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000304 while ((rth = rcu_dereference_protected(*rthp,
305 lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
David S. Millerbef55ae2011-03-12 17:17:10 -0500306 if (compare_keys(&rth->fld, &rt->fld)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /* Put it first */
Changli Gaod8d1f302010-06-10 23:31:35 -0700308 *rthp = rth->dst.dn_next;
309 rcu_assign_pointer(rth->dst.dn_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 dn_rt_hash_table[hash].chain);
311 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
312
Changli Gaod8d1f302010-06-10 23:31:35 -0700313 dst_use(&rth->dst, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
315
316 dnrt_drop(rt);
317 *rp = rth;
318 return 0;
319 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700320 rthp = &rth->dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
Changli Gaod8d1f302010-06-10 23:31:35 -0700323 rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900325
Changli Gaod8d1f302010-06-10 23:31:35 -0700326 dst_use(&rt->dst, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
328 *rp = rt;
329 return 0;
330}
331
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800332static void dn_run_flush(unsigned long dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 int i;
335 struct dn_route *rt, *next;
336
Eric Dumazetfc766e42010-10-29 03:09:24 +0000337 for (i = 0; i < dn_rt_hash_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 spin_lock_bh(&dn_rt_hash_table[i].lock);
339
Eric Dumazetfc766e42010-10-29 03:09:24 +0000340 if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto nothing_to_declare;
342
Eric Dumazetfc766e42010-10-29 03:09:24 +0000343 for(; rt; rt = next) {
344 next = rcu_dereference_raw(rt->dst.dn_next);
345 RCU_INIT_POINTER(rt->dst.dn_next, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dst_free((struct dst_entry *)rt);
347 }
348
349nothing_to_declare:
350 spin_unlock_bh(&dn_rt_hash_table[i].lock);
351 }
352}
353
354static DEFINE_SPINLOCK(dn_rt_flush_lock);
355
356void dn_rt_cache_flush(int delay)
357{
358 unsigned long now = jiffies;
359 int user_mode = !in_interrupt();
360
361 if (delay < 0)
362 delay = dn_rt_min_delay;
363
364 spin_lock_bh(&dn_rt_flush_lock);
365
366 if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
367 long tmo = (long)(dn_rt_deadline - now);
368
369 if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
370 tmo = 0;
371
372 if (delay > tmo)
373 delay = tmo;
374 }
375
376 if (delay <= 0) {
377 spin_unlock_bh(&dn_rt_flush_lock);
378 dn_run_flush(0);
379 return;
380 }
381
382 if (dn_rt_deadline == 0)
383 dn_rt_deadline = now + dn_rt_max_delay;
384
385 dn_rt_flush_timer.expires = now + delay;
386 add_timer(&dn_rt_flush_timer);
387 spin_unlock_bh(&dn_rt_flush_lock);
388}
389
390/**
391 * dn_return_short - Return a short packet to its sender
392 * @skb: The packet to return
393 *
394 */
395static int dn_return_short(struct sk_buff *skb)
396{
397 struct dn_skb_cb *cb;
398 unsigned char *ptr;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800399 __le16 *src;
400 __le16 *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* Add back headers */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700403 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
406 return NET_RX_DROP;
407
408 cb = DN_SKB_CB(skb);
409 /* Skip packet length and point to flags */
410 ptr = skb->data + 2;
411 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
412
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800413 dst = (__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800415 src = (__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ptr += 2;
417 *ptr = 0; /* Zero hop count */
418
Ilpo Järvinena0bffff2009-03-21 13:36:17 -0700419 swap(*src, *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 skb->pkt_type = PACKET_OUTGOING;
422 dn_rt_finish_output(skb, NULL, NULL);
423 return NET_RX_SUCCESS;
424}
425
426/**
427 * dn_return_long - Return a long packet to its sender
428 * @skb: The long format packet to return
429 *
430 */
431static int dn_return_long(struct sk_buff *skb)
432{
433 struct dn_skb_cb *cb;
434 unsigned char *ptr;
435 unsigned char *src_addr, *dst_addr;
436 unsigned char tmp[ETH_ALEN];
437
438 /* Add back all headers */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700439 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
442 return NET_RX_DROP;
443
444 cb = DN_SKB_CB(skb);
445 /* Ignore packet length and point to flags */
446 ptr = skb->data + 2;
447
448 /* Skip padding */
449 if (*ptr & DN_RT_F_PF) {
450 char padlen = (*ptr & ~DN_RT_F_PF);
451 ptr += padlen;
452 }
453
454 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
455 ptr += 2;
456 dst_addr = ptr;
457 ptr += 8;
458 src_addr = ptr;
459 ptr += 6;
460 *ptr = 0; /* Zero hop count */
461
462 /* Swap source and destination */
463 memcpy(tmp, src_addr, ETH_ALEN);
464 memcpy(src_addr, dst_addr, ETH_ALEN);
465 memcpy(dst_addr, tmp, ETH_ALEN);
466
467 skb->pkt_type = PACKET_OUTGOING;
468 dn_rt_finish_output(skb, dst_addr, src_addr);
469 return NET_RX_SUCCESS;
470}
471
472/**
473 * dn_route_rx_packet - Try and find a route for an incoming packet
474 * @skb: The packet to find a route for
475 *
476 * Returns: result of input function if route is found, error code otherwise
477 */
478static int dn_route_rx_packet(struct sk_buff *skb)
479{
Eric Dumazetfc766e42010-10-29 03:09:24 +0000480 struct dn_skb_cb *cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int err;
482
483 if ((err = dn_route_input(skb)) == 0)
484 return dst_input(skb);
485
Eric Dumazetfc766e42010-10-29 03:09:24 +0000486 cb = DN_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (decnet_debug_level & 4) {
488 char *devname = skb->dev ? skb->dev->name : "???";
Eric Dumazetfc766e42010-10-29 03:09:24 +0000489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 printk(KERN_DEBUG
491 "DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%d\n",
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800492 (int)cb->rt_flags, devname, skb->len,
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800493 le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 err, skb->pkt_type);
495 }
496
497 if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
498 switch(cb->rt_flags & DN_RT_PKT_MSK) {
499 case DN_RT_PKT_SHORT:
500 return dn_return_short(skb);
501 case DN_RT_PKT_LONG:
502 return dn_return_long(skb);
503 }
504 }
505
506 kfree_skb(skb);
507 return NET_RX_DROP;
508}
509
510static int dn_route_rx_long(struct sk_buff *skb)
511{
512 struct dn_skb_cb *cb = DN_SKB_CB(skb);
513 unsigned char *ptr = skb->data;
514
515 if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
516 goto drop_it;
517
518 skb_pull(skb, 20);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300519 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900521 /* Destination info */
522 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800523 cb->dst = dn_eth2dn(ptr);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900524 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
525 goto drop_it;
526 ptr += 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900529 /* Source info */
530 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800531 cb->src = dn_eth2dn(ptr);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900532 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
533 goto drop_it;
534 ptr += 6;
535 /* Other junk */
536 ptr++;
537 cb->hops = *ptr++; /* Visit Count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100539 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
540 dn_route_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542drop_it:
543 kfree_skb(skb);
544 return NET_RX_DROP;
545}
546
547
548
549static int dn_route_rx_short(struct sk_buff *skb)
550{
551 struct dn_skb_cb *cb = DN_SKB_CB(skb);
552 unsigned char *ptr = skb->data;
553
554 if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
555 goto drop_it;
556
557 skb_pull(skb, 5);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300558 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800560 cb->dst = *(__le16 *)ptr;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900561 ptr += 2;
562 cb->src = *(__le16 *)ptr;
563 ptr += 2;
564 cb->hops = *ptr & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100566 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
567 dn_route_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569drop_it:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900570 kfree_skb(skb);
571 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574static int dn_route_discard(struct sk_buff *skb)
575{
576 /*
577 * I know we drop the packet here, but thats considered success in
578 * this case
579 */
580 kfree_skb(skb);
581 return NET_RX_SUCCESS;
582}
583
584static int dn_route_ptp_hello(struct sk_buff *skb)
585{
586 dn_dev_hello(skb);
587 dn_neigh_pointopoint_hello(skb);
588 return NET_RX_SUCCESS;
589}
590
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700591int 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 -0700592{
593 struct dn_skb_cb *cb;
594 unsigned char flags = 0;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800595 __u16 len = le16_to_cpu(*(__le16 *)skb->data);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000596 struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned char padlen = 0;
598
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700599 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700600 goto dump_it;
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (dn == NULL)
603 goto dump_it;
604
605 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
606 goto out;
607
608 if (!pskb_may_pull(skb, 3))
609 goto dump_it;
610
611 skb_pull(skb, 2);
612
613 if (len > skb->len)
614 goto dump_it;
615
616 skb_trim(skb, len);
617
618 flags = *skb->data;
619
620 cb = DN_SKB_CB(skb);
621 cb->stamp = jiffies;
622 cb->iif = dev->ifindex;
623
624 /*
625 * If we have padding, remove it.
626 */
627 if (flags & DN_RT_F_PF) {
628 padlen = flags & ~DN_RT_F_PF;
629 if (!pskb_may_pull(skb, padlen + 1))
630 goto dump_it;
631 skb_pull(skb, padlen);
632 flags = *skb->data;
633 }
634
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700635 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /*
638 * Weed out future version DECnet
639 */
640 if (flags & DN_RT_F_VER)
641 goto dump_it;
642
643 cb->rt_flags = flags;
644
645 if (decnet_debug_level & 1)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900646 printk(KERN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900648 (int)flags, (dev) ? dev->name : "???", len, skb->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 padlen);
650
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900651 if (flags & DN_RT_PKT_CNTL) {
Herbert Xu364c6ba2006-06-09 16:10:40 -0700652 if (unlikely(skb_linearize(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 goto dump_it;
654
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900655 switch(flags & DN_RT_CNTL_MSK) {
656 case DN_RT_PKT_INIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 dn_dev_init_pkt(skb);
658 break;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900659 case DN_RT_PKT_VERI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 dn_dev_veri_pkt(skb);
661 break;
662 }
663
664 if (dn->parms.state != DN_DEV_S_RU)
665 goto dump_it;
666
667 switch(flags & DN_RT_CNTL_MSK) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900668 case DN_RT_PKT_HELO:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100669 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
670 skb, skb->dev, NULL,
671 dn_route_ptp_hello);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900673 case DN_RT_PKT_L1RT:
674 case DN_RT_PKT_L2RT:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100675 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
676 skb, skb->dev, NULL,
677 dn_route_discard);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900678 case DN_RT_PKT_ERTH:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100679 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
680 skb, skb->dev, NULL,
681 dn_neigh_router_hello);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900683 case DN_RT_PKT_EEDH:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100684 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
685 skb, skb->dev, NULL,
686 dn_neigh_endnode_hello);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900687 }
688 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (dn->parms.state != DN_DEV_S_RU)
690 goto dump_it;
691
692 skb_pull(skb, 1); /* Pull flags */
693
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900694 switch(flags & DN_RT_PKT_MSK) {
695 case DN_RT_PKT_LONG:
696 return dn_route_rx_long(skb);
697 case DN_RT_PKT_SHORT:
698 return dn_route_rx_short(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702dump_it:
703 kfree_skb(skb);
704out:
705 return NET_RX_DROP;
706}
707
708static int dn_output(struct sk_buff *skb)
709{
Eric Dumazetadf30902009-06-02 05:19:30 +0000710 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 struct dn_route *rt = (struct dn_route *)dst;
712 struct net_device *dev = dst->dev;
713 struct dn_skb_cb *cb = DN_SKB_CB(skb);
714 struct neighbour *neigh;
715
716 int err = -EINVAL;
717
718 if ((neigh = dst->neighbour) == NULL)
719 goto error;
720
721 skb->dev = dev;
722
723 cb->src = rt->rt_saddr;
724 cb->dst = rt->rt_daddr;
725
726 /*
727 * Always set the Intra-Ethernet bit on all outgoing packets
728 * originated on this node. Only valid flag from upper layers
729 * is return-to-sender-requested. Set hop count to 0 too.
730 */
731 cb->rt_flags &= ~DN_RT_F_RQR;
732 cb->rt_flags |= DN_RT_F_IE;
733 cb->hops = 0;
734
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100735 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
736 neigh->output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738error:
739 if (net_ratelimit())
740 printk(KERN_DEBUG "dn_output: This should not happen\n");
741
742 kfree_skb(skb);
743
744 return err;
745}
746
747static int dn_forward(struct sk_buff *skb)
748{
749 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000750 struct dst_entry *dst = skb_dst(skb);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000751 struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct dn_route *rt;
753 struct neighbour *neigh = dst->neighbour;
754 int header_len;
755#ifdef CONFIG_NETFILTER
756 struct net_device *dev = skb->dev;
757#endif
758
759 if (skb->pkt_type != PACKET_HOST)
760 goto drop;
761
762 /* Ensure that we have enough space for headers */
Eric Dumazetadf30902009-06-02 05:19:30 +0000763 rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 header_len = dn_db->use_long ? 21 : 6;
Changli Gaod8d1f302010-06-10 23:31:35 -0700765 if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 goto drop;
767
768 /*
769 * Hop count exceeded.
770 */
771 if (++cb->hops > 30)
772 goto drop;
773
Changli Gaod8d1f302010-06-10 23:31:35 -0700774 skb->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /*
777 * If packet goes out same interface it came in on, then set
778 * the Intra-Ethernet bit. This has no effect for short
779 * packets, so we don't need to test for them here.
780 */
781 cb->rt_flags &= ~DN_RT_F_IE;
782 if (rt->rt_flags & RTCF_DOREDIRECT)
783 cb->rt_flags |= DN_RT_F_IE;
784
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100785 return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
786 neigh->output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788drop:
789 kfree_skb(skb);
790 return NET_RX_DROP;
791}
792
793/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 * Used to catch bugs. This should never normally get
795 * called.
796 */
797static int dn_rt_bug(struct sk_buff *skb)
798{
799 if (net_ratelimit()) {
800 struct dn_skb_cb *cb = DN_SKB_CB(skb);
801
802 printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800803 le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805
806 kfree_skb(skb);
807
Florian Westphal0e8635a2009-06-20 00:53:25 +0000808 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
David S. Miller0dbaee32010-12-13 12:52:14 -0800811static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
812{
813 return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
814}
815
David S. Millerd33e4552010-12-14 13:01:14 -0800816static unsigned int dn_dst_default_mtu(const struct dst_entry *dst)
817{
818 return dst->dev->mtu;
819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
822{
823 struct dn_fib_info *fi = res->fi;
Changli Gaod8d1f302010-06-10 23:31:35 -0700824 struct net_device *dev = rt->dst.dev;
David S. Miller62fa8a82011-01-26 20:51:05 -0800825 unsigned int mss_metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct neighbour *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 if (fi) {
829 if (DN_FIB_RES_GW(*res) &&
830 DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
831 rt->rt_gateway = DN_FIB_RES_GW(*res);
David S. Miller62fa8a82011-01-26 20:51:05 -0800832 dst_init_metrics(&rt->dst, fi->fib_metrics, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834 rt->rt_type = res->type;
835
Changli Gaod8d1f302010-06-10 23:31:35 -0700836 if (dev != NULL && rt->dst.neighbour == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
838 if (IS_ERR(n))
839 return PTR_ERR(n);
Changli Gaod8d1f302010-06-10 23:31:35 -0700840 rt->dst.neighbour = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
David S. Millerd33e4552010-12-14 13:01:14 -0800843 if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
David S. Millerdefb3512010-12-08 21:16:57 -0800844 dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
David S. Miller62fa8a82011-01-26 20:51:05 -0800845 mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
846 if (mss_metric) {
David S. Miller0dbaee32010-12-13 12:52:14 -0800847 unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
David S. Miller62fa8a82011-01-26 20:51:05 -0800848 if (mss_metric > mss)
David S. Miller0dbaee32010-12-13 12:52:14 -0800849 dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 0;
852}
853
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800854static inline int dn_match_addr(__le16 addr1, __le16 addr2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800856 __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 int match = 16;
858 while(tmp) {
859 tmp >>= 1;
860 match--;
861 }
862 return match;
863}
864
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800865static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800867 __le16 saddr = 0;
Eric Dumazetfc766e42010-10-29 03:09:24 +0000868 struct dn_dev *dn_db;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct dn_ifaddr *ifa;
870 int best_match = 0;
871 int ret;
872
Eric Dumazetfc766e42010-10-29 03:09:24 +0000873 rcu_read_lock();
874 dn_db = rcu_dereference(dev->dn_ptr);
875 for (ifa = rcu_dereference(dn_db->ifa_list);
876 ifa != NULL;
877 ifa = rcu_dereference(ifa->ifa_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (ifa->ifa_scope > scope)
879 continue;
880 if (!daddr) {
881 saddr = ifa->ifa_local;
882 break;
883 }
884 ret = dn_match_addr(daddr, ifa->ifa_local);
885 if (ret > best_match)
886 saddr = ifa->ifa_local;
887 if (best_match == 0)
888 saddr = ifa->ifa_local;
889 }
Eric Dumazetfc766e42010-10-29 03:09:24 +0000890 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 return saddr;
893}
894
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800895static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
898}
899
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800900static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800902 __le16 mask = dnet_make_mask(res->prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
904}
905
David S. Millerbef55ae2011-03-12 17:17:10 -0500906static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oldflp, int try_hard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
David S. Millerbef55ae2011-03-12 17:17:10 -0500908 struct flowidn fld = {
909 .daddr = oldflp->daddr,
910 .saddr = oldflp->saddr,
911 .flowidn_scope = RT_SCOPE_UNIVERSE,
912 .flowidn_mark = oldflp->flowidn_mark,
913 .flowidn_iif = init_net.loopback_dev->ifindex,
914 .flowidn_oif = oldflp->flowidn_oif,
David S. Miller1d28f422011-03-12 00:29:39 -0500915 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct dn_route *rt = NULL;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700917 struct net_device *dev_out = NULL, *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct neighbour *neigh = NULL;
919 unsigned hash;
920 unsigned flags = 0;
921 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
922 int err;
923 int free_res = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800924 __le16 gateway = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (decnet_debug_level & 16)
927 printk(KERN_DEBUG
928 "dn_route_output_slow: dst=%04x src=%04x mark=%d"
David S. Millerbef55ae2011-03-12 17:17:10 -0500929 " iif=%d oif=%d\n", le16_to_cpu(oldflp->daddr),
930 le16_to_cpu(oldflp->saddr),
931 oldflp->flowidn_mark, init_net.loopback_dev->ifindex,
932 oldflp->flowidn_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /* If we have an output interface, verify its a DECnet device */
David S. Millerbef55ae2011-03-12 17:17:10 -0500935 if (oldflp->flowidn_oif) {
936 dev_out = dev_get_by_index(&init_net, oldflp->flowidn_oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 err = -ENODEV;
938 if (dev_out && dev_out->dn_ptr == NULL) {
939 dev_put(dev_out);
940 dev_out = NULL;
941 }
942 if (dev_out == NULL)
943 goto out;
944 }
945
946 /* If we have a source address, verify that its a local address */
David S. Millerbef55ae2011-03-12 17:17:10 -0500947 if (oldflp->saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 err = -EADDRNOTAVAIL;
949
950 if (dev_out) {
David S. Millerbef55ae2011-03-12 17:17:10 -0500951 if (dn_dev_islocal(dev_out, oldflp->saddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 goto source_ok;
953 dev_put(dev_out);
954 goto out;
955 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800956 rcu_read_lock();
957 for_each_netdev_rcu(&init_net, dev) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700958 if (!dev->dn_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 continue;
David S. Millerbef55ae2011-03-12 17:17:10 -0500960 if (!dn_dev_islocal(dev, oldflp->saddr))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700961 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700962 if ((dev->flags & IFF_LOOPBACK) &&
David S. Millerbef55ae2011-03-12 17:17:10 -0500963 oldflp->daddr &&
964 !dn_dev_islocal(dev, oldflp->daddr))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700965 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700966
967 dev_out = dev;
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800970 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (dev_out == NULL)
972 goto out;
973 dev_hold(dev_out);
974source_ok:
975 ;
976 }
977
978 /* No destination? Assume its local */
David S. Millerbef55ae2011-03-12 17:17:10 -0500979 if (!fld.daddr) {
980 fld.daddr = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 err = -EADDRNOTAVAIL;
983 if (dev_out)
984 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700985 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -0500987 if (!fld.daddr) {
988 fld.daddr =
989 fld.saddr = dnet_select_source(dev_out, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 RT_SCOPE_HOST);
David S. Millerbef55ae2011-03-12 17:17:10 -0500991 if (!fld.daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 goto out;
993 }
David S. Millerbef55ae2011-03-12 17:17:10 -0500994 fld.flowidn_oif = init_net.loopback_dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 res.type = RTN_LOCAL;
996 goto make_route;
997 }
998
999 if (decnet_debug_level & 16)
1000 printk(KERN_DEBUG
1001 "dn_route_output_slow: initial checks complete."
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001002 " dst=%o4x src=%04x oif=%d try_hard=%d\n",
David S. Millerbef55ae2011-03-12 17:17:10 -05001003 le16_to_cpu(fld.daddr), le16_to_cpu(fld.saddr),
1004 fld.flowidn_oif, try_hard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /*
1007 * N.B. If the kernel is compiled without router support then
1008 * dn_fib_lookup() will evaluate to non-zero so this if () block
1009 * will always be executed.
1010 */
1011 err = -ESRCH;
David S. Millerbef55ae2011-03-12 17:17:10 -05001012 if (try_hard || (err = dn_fib_lookup(&fld, &res)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 struct dn_dev *dn_db;
1014 if (err != -ESRCH)
1015 goto out;
1016 /*
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001017 * Here the fallback is basically the standard algorithm for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * routing in endnodes which is described in the DECnet routing
1019 * docs
1020 *
1021 * If we are not trying hard, look in neighbour cache.
1022 * The result is tested to ensure that if a specific output
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001023 * device/source address was requested, then we honour that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 * here
1025 */
1026 if (!try_hard) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001027 neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fld.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (neigh) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001029 if ((oldflp->flowidn_oif &&
1030 (neigh->dev->ifindex != oldflp->flowidn_oif)) ||
1031 (oldflp->saddr &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 (!dn_dev_islocal(neigh->dev,
David S. Millerbef55ae2011-03-12 17:17:10 -05001033 oldflp->saddr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 neigh_release(neigh);
1035 neigh = NULL;
1036 } else {
1037 if (dev_out)
1038 dev_put(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001039 if (dn_dev_islocal(neigh->dev, fld.daddr)) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001040 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 res.type = RTN_LOCAL;
1042 } else {
1043 dev_out = neigh->dev;
1044 }
1045 dev_hold(dev_out);
1046 goto select_source;
1047 }
1048 }
1049 }
1050
1051 /* Not there? Perhaps its a local address */
1052 if (dev_out == NULL)
1053 dev_out = dn_dev_get_default();
1054 err = -ENODEV;
1055 if (dev_out == NULL)
1056 goto out;
Eric Dumazetfc766e42010-10-29 03:09:24 +00001057 dn_db = rcu_dereference_raw(dev_out->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* Possible improvement - check all devices for local addr */
David S. Millerbef55ae2011-03-12 17:17:10 -05001059 if (dn_dev_islocal(dev_out, fld.daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001061 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 dev_hold(dev_out);
1063 res.type = RTN_LOCAL;
1064 goto select_source;
1065 }
1066 /* Not local either.... try sending it to the default router */
1067 neigh = neigh_clone(dn_db->router);
1068 BUG_ON(neigh && neigh->dev != dev_out);
1069
1070 /* Ok then, we assume its directly connected and move on */
1071select_source:
1072 if (neigh)
1073 gateway = ((struct dn_neigh *)neigh)->addr;
1074 if (gateway == 0)
David S. Millerbef55ae2011-03-12 17:17:10 -05001075 gateway = fld.daddr;
1076 if (fld.saddr == 0) {
1077 fld.saddr = dnet_select_source(dev_out, gateway,
1078 res.type == RTN_LOCAL ?
1079 RT_SCOPE_HOST :
1080 RT_SCOPE_LINK);
1081 if (fld.saddr == 0 && res.type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto e_addr;
1083 }
David S. Millerbef55ae2011-03-12 17:17:10 -05001084 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto make_route;
1086 }
1087 free_res = 1;
1088
1089 if (res.type == RTN_NAT)
1090 goto e_inval;
1091
1092 if (res.type == RTN_LOCAL) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001093 if (!fld.saddr)
1094 fld.saddr = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (dev_out)
1096 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001097 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001099 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (res.fi)
1101 dn_fib_info_put(res.fi);
1102 res.fi = NULL;
1103 goto make_route;
1104 }
1105
David S. Millerbef55ae2011-03-12 17:17:10 -05001106 if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1107 dn_fib_select_multipath(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001109 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * We could add some logic to deal with default routes here and
1111 * get rid of some of the special casing above.
1112 */
1113
David S. Millerbef55ae2011-03-12 17:17:10 -05001114 if (!fld.saddr)
1115 fld.saddr = DN_FIB_RES_PREFSRC(res);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (dev_out)
1118 dev_put(dev_out);
1119 dev_out = DN_FIB_RES_DEV(res);
1120 dev_hold(dev_out);
David S. Millerbef55ae2011-03-12 17:17:10 -05001121 fld.flowidn_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 gateway = DN_FIB_RES_GW(res);
1123
1124make_route:
1125 if (dev_out->flags & IFF_LOOPBACK)
1126 flags |= RTCF_LOCAL;
1127
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001128 rt = dst_alloc(&dn_dst_ops, dev_out, 1, 0, DST_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (rt == NULL)
1130 goto e_nobufs;
1131
David S. Millerbef55ae2011-03-12 17:17:10 -05001132 rt->fld.saddr = oldflp->saddr;
1133 rt->fld.daddr = oldflp->daddr;
1134 rt->fld.flowidn_oif = oldflp->flowidn_oif;
1135 rt->fld.flowidn_iif = 0;
1136 rt->fld.flowidn_mark = oldflp->flowidn_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
David S. Millerbef55ae2011-03-12 17:17:10 -05001138 rt->rt_saddr = fld.saddr;
1139 rt->rt_daddr = fld.daddr;
1140 rt->rt_gateway = gateway ? gateway : fld.daddr;
1141 rt->rt_local_src = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
David S. Millerbef55ae2011-03-12 17:17:10 -05001143 rt->rt_dst_map = fld.daddr;
1144 rt->rt_src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Changli Gaod8d1f302010-06-10 23:31:35 -07001146 rt->dst.neighbour = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 neigh = NULL;
1148
Changli Gaod8d1f302010-06-10 23:31:35 -07001149 rt->dst.lastuse = jiffies;
1150 rt->dst.output = dn_output;
1151 rt->dst.input = dn_rt_bug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 rt->rt_flags = flags;
1153 if (flags & RTCF_LOCAL)
Changli Gaod8d1f302010-06-10 23:31:35 -07001154 rt->dst.input = dn_nsp_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 err = dn_rt_set_next_hop(rt, &res);
1157 if (err)
1158 goto e_neighbour;
1159
David S. Millerbef55ae2011-03-12 17:17:10 -05001160 hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 dn_insert_route(rt, hash, (struct dn_route **)pprt);
1162
1163done:
1164 if (neigh)
1165 neigh_release(neigh);
1166 if (free_res)
1167 dn_fib_res_put(&res);
1168 if (dev_out)
1169 dev_put(dev_out);
1170out:
1171 return err;
1172
1173e_addr:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001174 err = -EADDRNOTAVAIL;
1175 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176e_inval:
1177 err = -EINVAL;
1178 goto done;
1179e_nobufs:
1180 err = -ENOBUFS;
1181 goto done;
1182e_neighbour:
Changli Gaod8d1f302010-06-10 23:31:35 -07001183 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 goto e_nobufs;
1185}
1186
1187
1188/*
1189 * N.B. The flags may be moved into the flowi at some future stage.
1190 */
David S. Millerbef55ae2011-03-12 17:17:10 -05001191static int __dn_route_output_key(struct dst_entry **pprt, const struct flowidn *flp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
David S. Millerbef55ae2011-03-12 17:17:10 -05001193 unsigned hash = dn_hash(flp->saddr, flp->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct dn_route *rt = NULL;
1195
1196 if (!(flags & MSG_TRYHARD)) {
1197 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001198 for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07001199 rt = rcu_dereference_bh(rt->dst.dn_next)) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001200 if ((flp->daddr == rt->fld.daddr) &&
1201 (flp->saddr == rt->fld.saddr) &&
1202 (flp->flowidn_mark == rt->fld.flowidn_mark) &&
David S. Millerc7537962010-11-11 17:07:48 -08001203 dn_is_output_route(rt) &&
David S. Millerbef55ae2011-03-12 17:17:10 -05001204 (rt->fld.flowidn_oif == flp->flowidn_oif)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001205 dst_use(&rt->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 rcu_read_unlock_bh();
Changli Gaod8d1f302010-06-10 23:31:35 -07001207 *pprt = &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return 0;
1209 }
1210 }
1211 rcu_read_unlock_bh();
1212 }
1213
1214 return dn_route_output_slow(pprt, flp, flags);
1215}
1216
David S. Millerbef55ae2011-03-12 17:17:10 -05001217static int dn_route_output_key(struct dst_entry **pprt, struct flowidn *flp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 int err;
1220
1221 err = __dn_route_output_key(pprt, flp, flags);
David S. Millerbef55ae2011-03-12 17:17:10 -05001222 if (err == 0 && flp->flowidn_proto) {
1223 *pprt = xfrm_lookup(&init_net, *pprt,
1224 flowidn_to_flowi(flp), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -08001225 if (IS_ERR(*pprt)) {
1226 err = PTR_ERR(*pprt);
1227 *pprt = NULL;
1228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230 return err;
1231}
1232
David S. Millerbef55ae2011-03-12 17:17:10 -05001233int dn_route_output_sock(struct dst_entry **pprt, struct flowidn *fl, struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 int err;
1236
1237 err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
David S. Millerbef55ae2011-03-12 17:17:10 -05001238 if (err == 0 && fl->flowidn_proto) {
David S. Miller80c0bc92011-03-01 14:36:37 -08001239 if (!(flags & MSG_DONTWAIT))
David S. Millerbef55ae2011-03-12 17:17:10 -05001240 fl->flowidn_flags |= FLOWI_FLAG_CAN_SLEEP;
1241 *pprt = xfrm_lookup(&init_net, *pprt,
1242 flowidn_to_flowi(fl), sk, 0);
David S. Miller452edd52011-03-02 13:27:41 -08001243 if (IS_ERR(*pprt)) {
1244 err = PTR_ERR(*pprt);
1245 *pprt = NULL;
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248 return err;
1249}
1250
1251static int dn_route_input_slow(struct sk_buff *skb)
1252{
1253 struct dn_route *rt = NULL;
1254 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1255 struct net_device *in_dev = skb->dev;
1256 struct net_device *out_dev = NULL;
1257 struct dn_dev *dn_db;
1258 struct neighbour *neigh = NULL;
1259 unsigned hash;
1260 int flags = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001261 __le16 gateway = 0;
1262 __le16 local_src = 0;
David S. Millerbef55ae2011-03-12 17:17:10 -05001263 struct flowidn fld = {
1264 .daddr = cb->dst,
1265 .saddr = cb->src,
1266 .flowidn_scope = RT_SCOPE_UNIVERSE,
1267 .flowidn_mark = skb->mark,
1268 .flowidn_iif = skb->dev->ifindex,
David S. Miller1d28f422011-03-12 00:29:39 -05001269 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1271 int err = -EINVAL;
1272 int free_res = 0;
1273
1274 dev_hold(in_dev);
1275
Eric Dumazetfc766e42010-10-29 03:09:24 +00001276 if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 goto out;
1278
1279 /* Zero source addresses are not allowed */
David S. Millerbef55ae2011-03-12 17:17:10 -05001280 if (fld.saddr == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 goto out;
1282
1283 /*
1284 * In this case we've just received a packet from a source
1285 * outside ourselves pretending to come from us. We don't
1286 * allow it any further to prevent routing loops, spoofing and
1287 * other nasties. Loopback packets already have the dst attached
1288 * so this only affects packets which have originated elsewhere.
1289 */
1290 err = -ENOTUNIQ;
1291 if (dn_dev_islocal(in_dev, cb->src))
1292 goto out;
1293
David S. Millerbef55ae2011-03-12 17:17:10 -05001294 err = dn_fib_lookup(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (err) {
1296 if (err != -ESRCH)
1297 goto out;
1298 /*
1299 * Is the destination us ?
1300 */
1301 if (!dn_dev_islocal(in_dev, cb->dst))
1302 goto e_inval;
1303
1304 res.type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 } else {
David S. Millerbef55ae2011-03-12 17:17:10 -05001306 __le16 src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 free_res = 1;
1308
1309 out_dev = DN_FIB_RES_DEV(res);
1310 if (out_dev == NULL) {
1311 if (net_ratelimit())
1312 printk(KERN_CRIT "Bug in dn_route_input_slow() "
1313 "No output device\n");
1314 goto e_inval;
1315 }
1316 dev_hold(out_dev);
1317
1318 if (res.r)
David S. Millerbef55ae2011-03-12 17:17:10 -05001319 src_map = fld.saddr; /* no NAT support for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 gateway = DN_FIB_RES_GW(res);
1322 if (res.type == RTN_NAT) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001323 fld.daddr = dn_fib_rules_map_destination(fld.daddr, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 dn_fib_res_put(&res);
1325 free_res = 0;
David S. Millerbef55ae2011-03-12 17:17:10 -05001326 if (dn_fib_lookup(&fld, &res))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 goto e_inval;
1328 free_res = 1;
1329 if (res.type != RTN_UNICAST)
1330 goto e_inval;
1331 flags |= RTCF_DNAT;
David S. Millerbef55ae2011-03-12 17:17:10 -05001332 gateway = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
David S. Millerbef55ae2011-03-12 17:17:10 -05001334 fld.saddr = src_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336
1337 switch(res.type) {
1338 case RTN_UNICAST:
1339 /*
1340 * Forwarding check here, we only check for forwarding
1341 * being turned off, if you want to only forward intra
1342 * area, its up to you to set the routing tables up
1343 * correctly.
1344 */
1345 if (dn_db->parms.forwarding == 0)
1346 goto e_inval;
1347
David S. Millerbef55ae2011-03-12 17:17:10 -05001348 if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
1349 dn_fib_select_multipath(&fld, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001351 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1353 * flag as a hint to set the intra-ethernet bit when
1354 * forwarding. If we've got NAT in operation, we don't do
1355 * this optimisation.
1356 */
1357 if (out_dev == in_dev && !(flags & RTCF_NAT))
1358 flags |= RTCF_DOREDIRECT;
1359
1360 local_src = DN_FIB_RES_PREFSRC(res);
1361
1362 case RTN_BLACKHOLE:
1363 case RTN_UNREACHABLE:
1364 break;
1365 case RTN_LOCAL:
1366 flags |= RTCF_LOCAL;
David S. Millerbef55ae2011-03-12 17:17:10 -05001367 fld.saddr = cb->dst;
1368 fld.daddr = cb->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /* Routing tables gave us a gateway */
1371 if (gateway)
1372 goto make_route;
1373
1374 /* Packet was intra-ethernet, so we know its on-link */
Steven Whitehouse3a31b9d2006-10-18 20:45:22 -07001375 if (cb->rt_flags & DN_RT_F_IE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 gateway = cb->src;
1377 flags |= RTCF_DIRECTSRC;
1378 goto make_route;
1379 }
1380
1381 /* Use the default router if there is one */
1382 neigh = neigh_clone(dn_db->router);
1383 if (neigh) {
1384 gateway = ((struct dn_neigh *)neigh)->addr;
1385 goto make_route;
1386 }
1387
1388 /* Close eyes and pray */
1389 gateway = cb->src;
1390 flags |= RTCF_DIRECTSRC;
1391 goto make_route;
1392 default:
1393 goto e_inval;
1394 }
1395
1396make_route:
David S. Miller5c1e6aa2011-04-28 14:13:38 -07001397 rt = dst_alloc(&dn_dst_ops, out_dev, 0, 0, DST_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (rt == NULL)
1399 goto e_nobufs;
1400
David S. Millerbef55ae2011-03-12 17:17:10 -05001401 rt->rt_saddr = fld.saddr;
1402 rt->rt_daddr = fld.daddr;
1403 rt->rt_gateway = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (gateway)
1405 rt->rt_gateway = gateway;
1406 rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
1407
David S. Millerbef55ae2011-03-12 17:17:10 -05001408 rt->rt_dst_map = fld.daddr;
1409 rt->rt_src_map = fld.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
David S. Millerbef55ae2011-03-12 17:17:10 -05001411 rt->fld.saddr = cb->src;
1412 rt->fld.daddr = cb->dst;
1413 rt->fld.flowidn_oif = 0;
1414 rt->fld.flowidn_iif = in_dev->ifindex;
1415 rt->fld.flowidn_mark = fld.flowidn_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Changli Gaod8d1f302010-06-10 23:31:35 -07001417 rt->dst.neighbour = neigh;
Changli Gaod8d1f302010-06-10 23:31:35 -07001418 rt->dst.lastuse = jiffies;
1419 rt->dst.output = dn_rt_bug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 switch(res.type) {
1421 case RTN_UNICAST:
Changli Gaod8d1f302010-06-10 23:31:35 -07001422 rt->dst.input = dn_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 break;
1424 case RTN_LOCAL:
Changli Gaod8d1f302010-06-10 23:31:35 -07001425 rt->dst.output = dn_output;
1426 rt->dst.input = dn_nsp_rx;
1427 rt->dst.dev = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 flags |= RTCF_LOCAL;
1429 break;
1430 default:
1431 case RTN_UNREACHABLE:
1432 case RTN_BLACKHOLE:
Changli Gaod8d1f302010-06-10 23:31:35 -07001433 rt->dst.input = dst_discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435 rt->rt_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 err = dn_rt_set_next_hop(rt, &res);
1438 if (err)
1439 goto e_neighbour;
1440
David S. Millerbef55ae2011-03-12 17:17:10 -05001441 hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
Eric Dumazetadf30902009-06-02 05:19:30 +00001442 dn_insert_route(rt, hash, &rt);
Changli Gaod8d1f302010-06-10 23:31:35 -07001443 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445done:
1446 if (neigh)
1447 neigh_release(neigh);
1448 if (free_res)
1449 dn_fib_res_put(&res);
1450 dev_put(in_dev);
1451 if (out_dev)
1452 dev_put(out_dev);
1453out:
1454 return err;
1455
1456e_inval:
1457 err = -EINVAL;
1458 goto done;
1459
1460e_nobufs:
1461 err = -ENOBUFS;
1462 goto done;
1463
1464e_neighbour:
Changli Gaod8d1f302010-06-10 23:31:35 -07001465 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 goto done;
1467}
1468
Roel Kluin5eaa65b2008-12-10 15:18:31 -08001469static int dn_route_input(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 struct dn_route *rt;
1472 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1473 unsigned hash = dn_hash(cb->src, cb->dst);
1474
Eric Dumazetadf30902009-06-02 05:19:30 +00001475 if (skb_dst(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return 0;
1477
1478 rcu_read_lock();
1479 for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
Changli Gaod8d1f302010-06-10 23:31:35 -07001480 rt = rcu_dereference(rt->dst.dn_next)) {
David S. Millerbef55ae2011-03-12 17:17:10 -05001481 if ((rt->fld.saddr == cb->src) &&
1482 (rt->fld.daddr == cb->dst) &&
1483 (rt->fld.flowidn_oif == 0) &&
1484 (rt->fld.flowidn_mark == skb->mark) &&
1485 (rt->fld.flowidn_iif == cb->iif)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001486 dst_use(&rt->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 rcu_read_unlock();
Eric Dumazetadf30902009-06-02 05:19:30 +00001488 skb_dst_set(skb, (struct dst_entry *)rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return 0;
1490 }
1491 }
1492 rcu_read_unlock();
1493
1494 return dn_route_input_slow(skb);
1495}
1496
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001497static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1498 int event, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Eric Dumazetadf30902009-06-02 05:19:30 +00001500 struct dn_route *rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct rtmsg *r;
1502 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001503 unsigned char *b = skb_tail_pointer(skb);
Thomas Grafe3703b32006-11-27 09:27:07 -08001504 long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001506 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 r = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 r->rtm_family = AF_DECnet;
1509 r->rtm_dst_len = 16;
1510 r->rtm_src_len = 0;
1511 r->rtm_tos = 0;
1512 r->rtm_table = RT_TABLE_MAIN;
Patrick McHardy9e762a42006-08-10 23:09:48 -07001513 RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 r->rtm_type = rt->rt_type;
1515 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1516 r->rtm_scope = RT_SCOPE_UNIVERSE;
1517 r->rtm_protocol = RTPROT_UNSPEC;
1518 if (rt->rt_flags & RTCF_NOTIFY)
1519 r->rtm_flags |= RTM_F_NOTIFY;
1520 RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
David S. Millerbef55ae2011-03-12 17:17:10 -05001521 if (rt->fld.saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 r->rtm_src_len = 16;
David S. Millerbef55ae2011-03-12 17:17:10 -05001523 RTA_PUT(skb, RTA_SRC, 2, &rt->fld.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001525 if (rt->dst.dev)
1526 RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /*
1528 * Note to self - change this if input routes reverse direction when
1529 * they deal only with inputs and not with replies like they do
1530 * currently.
1531 */
1532 RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
1533 if (rt->rt_daddr != rt->rt_gateway)
1534 RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
David S. Millerdefb3512010-12-08 21:16:57 -08001535 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 goto rtattr_failure;
Changli Gaod8d1f302010-06-10 23:31:35 -07001537 expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
1538 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
1539 rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08001540 goto rtattr_failure;
David S. Millerc7537962010-11-11 17:07:48 -08001541 if (dn_is_input_route(rt))
David S. Millerbef55ae2011-03-12 17:17:10 -05001542 RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fld.flowidn_iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001544 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return skb->len;
1546
1547nlmsg_failure:
1548rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001549 nlmsg_trim(skb, b);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001550 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553/*
1554 * This is called by both endnodes and routers now.
1555 */
Thomas Graffa34ddd2007-03-22 11:57:46 -07001556static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001558 struct net *net = sock_net(in_skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 struct rtattr **rta = arg;
1560 struct rtmsg *rtm = NLMSG_DATA(nlh);
1561 struct dn_route *rt = NULL;
1562 struct dn_skb_cb *cb;
1563 int err;
1564 struct sk_buff *skb;
David S. Millerbef55ae2011-03-12 17:17:10 -05001565 struct flowidn fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001567 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001568 return -EINVAL;
1569
David S. Millerbef55ae2011-03-12 17:17:10 -05001570 memset(&fld, 0, sizeof(fld));
1571 fld.flowidn_proto = DNPROTO_NSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1574 if (skb == NULL)
1575 return -ENOBUFS;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001576 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 cb = DN_SKB_CB(skb);
1578
1579 if (rta[RTA_SRC-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001580 memcpy(&fld.saddr, RTA_DATA(rta[RTA_SRC-1]), 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (rta[RTA_DST-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001582 memcpy(&fld.daddr, RTA_DATA(rta[RTA_DST-1]), 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (rta[RTA_IIF-1])
David S. Millerbef55ae2011-03-12 17:17:10 -05001584 memcpy(&fld.flowidn_iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
David S. Millerbef55ae2011-03-12 17:17:10 -05001586 if (fld.flowidn_iif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct net_device *dev;
David S. Millerbef55ae2011-03-12 17:17:10 -05001588 if ((dev = dev_get_by_index(&init_net, fld.flowidn_iif)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 kfree_skb(skb);
1590 return -ENODEV;
1591 }
1592 if (!dev->dn_ptr) {
1593 dev_put(dev);
1594 kfree_skb(skb);
1595 return -ENODEV;
1596 }
YOSHIFUJI Hideakib98999d2007-12-12 03:51:49 +09001597 skb->protocol = htons(ETH_P_DNA_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 skb->dev = dev;
David S. Millerbef55ae2011-03-12 17:17:10 -05001599 cb->src = fld.saddr;
1600 cb->dst = fld.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 local_bh_disable();
1602 err = dn_route_input(skb);
1603 local_bh_enable();
1604 memset(cb, 0, sizeof(struct dn_skb_cb));
Eric Dumazetadf30902009-06-02 05:19:30 +00001605 rt = (struct dn_route *)skb_dst(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001606 if (!err && -rt->dst.error)
1607 err = rt->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 } else {
1609 int oif = 0;
1610 if (rta[RTA_OIF - 1])
1611 memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
David S. Millerbef55ae2011-03-12 17:17:10 -05001612 fld.flowidn_oif = oif;
1613 err = dn_route_output_key((struct dst_entry **)&rt, &fld, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
1615
1616 if (skb->dev)
1617 dev_put(skb->dev);
1618 skb->dev = NULL;
1619 if (err)
1620 goto out_free;
Changli Gaod8d1f302010-06-10 23:31:35 -07001621 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (rtm->rtm_flags & RTM_F_NOTIFY)
1623 rt->rt_flags |= RTCF_NOTIFY;
1624
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001625 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 -07001626
1627 if (err == 0)
1628 goto out_free;
1629 if (err < 0) {
1630 err = -EMSGSIZE;
1631 goto out_free;
1632 }
1633
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001634 return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636out_free:
1637 kfree_skb(skb);
1638 return err;
1639}
1640
1641/*
1642 * For routers, this is called from dn_fib_dump, but for endnodes its
1643 * called directly from the rtnetlink dispatch table.
1644 */
1645int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1646{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001647 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct dn_route *rt;
1649 int h, s_h;
1650 int idx, s_idx;
1651
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001652 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001653 return 0;
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
1656 return -EINVAL;
1657 if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
1658 return 0;
1659
1660 s_h = cb->args[0];
1661 s_idx = idx = cb->args[1];
1662 for(h = 0; h <= dn_rt_hash_mask; h++) {
1663 if (h < s_h)
1664 continue;
1665 if (h > s_h)
1666 s_idx = 0;
1667 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001668 for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07001670 rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (idx < s_idx)
1672 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001673 skb_dst_set(skb, dst_clone(&rt->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001675 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001676 1, NLM_F_MULTI) <= 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001677 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 rcu_read_unlock_bh();
1679 goto done;
1680 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001681 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683 rcu_read_unlock_bh();
1684 }
1685
1686done:
1687 cb->args[0] = h;
1688 cb->args[1] = idx;
1689 return skb->len;
1690}
1691
1692#ifdef CONFIG_PROC_FS
1693struct dn_rt_cache_iter_state {
1694 int bucket;
1695};
1696
1697static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1698{
1699 struct dn_route *rt = NULL;
1700 struct dn_rt_cache_iter_state *s = seq->private;
1701
1702 for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1703 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001704 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (rt)
1706 break;
1707 rcu_read_unlock_bh();
1708 }
Paul E. McKenneya898def2010-02-22 17:04:49 -08001709 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1713{
Eric Dumazet0d89d792008-01-10 22:35:21 -08001714 struct dn_rt_cache_iter_state *s = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Eric Dumazetfc766e42010-10-29 03:09:24 +00001716 rt = rcu_dereference_bh(rt->dst.dn_next);
1717 while (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 rcu_read_unlock_bh();
1719 if (--s->bucket < 0)
1720 break;
1721 rcu_read_lock_bh();
Eric Dumazetfc766e42010-10-29 03:09:24 +00001722 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
Eric Dumazetfc766e42010-10-29 03:09:24 +00001724 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
1727static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1728{
1729 struct dn_route *rt = dn_rt_cache_get_first(seq);
1730
1731 if (rt) {
1732 while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1733 --*pos;
1734 }
1735 return *pos ? NULL : rt;
1736}
1737
1738static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1739{
1740 struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1741 ++*pos;
1742 return rt;
1743}
1744
1745static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1746{
1747 if (v)
1748 rcu_read_unlock_bh();
1749}
1750
1751static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1752{
1753 struct dn_route *rt = v;
1754 char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1755
1756 seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07001757 rt->dst.dev ? rt->dst.dev->name : "*",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -08001758 dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1759 dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
Changli Gaod8d1f302010-06-10 23:31:35 -07001760 atomic_read(&rt->dst.__refcnt),
1761 rt->dst.__use,
1762 (int) dst_metric(&rt->dst, RTAX_RTT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001764}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001766static const struct seq_operations dn_rt_cache_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 .start = dn_rt_cache_seq_start,
1768 .next = dn_rt_cache_seq_next,
1769 .stop = dn_rt_cache_seq_stop,
1770 .show = dn_rt_cache_seq_show,
1771};
1772
1773static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1774{
Pavel Emelyanov31164082007-10-10 02:30:23 -07001775 return seq_open_private(file, &dn_rt_cache_seq_ops,
1776 sizeof(struct dn_rt_cache_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Arjan van de Ven9a321442007-02-12 00:55:35 -08001779static const struct file_operations dn_rt_cache_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 .owner = THIS_MODULE,
1781 .open = dn_rt_cache_seq_open,
1782 .read = seq_read,
1783 .llseek = seq_lseek,
1784 .release = seq_release_private,
1785};
1786
1787#endif /* CONFIG_PROC_FS */
1788
1789void __init dn_route_init(void)
1790{
1791 int i, goal, order;
1792
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07001793 dn_dst_ops.kmem_cachep =
1794 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001795 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001796 dst_entries_init(&dn_dst_ops);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001797 setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1799 add_timer(&dn_route_timer);
1800
Jan Beulich44813742009-09-21 17:03:05 -07001801 goal = totalram_pages >> (26 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 for(order = 0; (1UL << order) < goal; order++)
1804 /* NOTHING */;
1805
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001806 /*
1807 * Only want 1024 entries max, since the table is very, very unlikely
1808 * to be larger than that.
1809 */
1810 while(order && ((((1UL << order) * PAGE_SIZE) /
1811 sizeof(struct dn_rt_hash_bucket)) >= 2048))
1812 order--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001814 do {
1815 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1816 sizeof(struct dn_rt_hash_bucket);
1817 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1818 dn_rt_hash_mask--;
1819 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1820 __get_free_pages(GFP_ATOMIC, order);
1821 } while (dn_rt_hash_table == NULL && --order > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 if (!dn_rt_hash_table)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001824 panic("Failed to allocate DECnet route cache hash table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001826 printk(KERN_INFO
1827 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1828 dn_rt_hash_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1830
1831 dn_rt_hash_mask--;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001832 for(i = 0; i <= dn_rt_hash_mask; i++) {
1833 spin_lock_init(&dn_rt_hash_table[i].lock);
1834 dn_rt_hash_table[i].chain = NULL;
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001837 dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001839 proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001840
1841#ifdef CONFIG_DECNET_ROUTER
1842 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, dn_fib_dump);
1843#else
1844 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1845 dn_cache_dump);
1846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
1849void __exit dn_route_cleanup(void)
1850{
1851 del_timer(&dn_route_timer);
1852 dn_run_flush(0);
1853
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001854 proc_net_remove(&init_net, "decnet_cache");
Eric Dumazetfc66f952010-10-08 06:37:34 +00001855 dst_entries_destroy(&dn_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857