blob: 5e636365d33cb2e99b1077b59ce9e14c4b5c7c02 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
116static void dn_dst_link_failure(struct sk_buff *);
117static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu);
118static int dn_route_input(struct sk_buff *);
119static void dn_run_flush(unsigned long dummy);
120
121static struct dn_rt_hash_bucket *dn_rt_hash_table;
122static unsigned dn_rt_hash_mask;
123
124static struct timer_list dn_route_timer;
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700125static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126int decnet_dst_gc_interval = 2;
127
128static struct dst_ops dn_dst_ops = {
129 .family = PF_DECnet,
Harvey Harrison09640e62009-02-01 00:45:17 -0800130 .protocol = cpu_to_be16(ETH_P_DNA_RT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .gc_thresh = 128,
132 .gc = dn_dst_gc,
133 .check = dn_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800134 .default_advmss = dn_dst_default_advmss,
David S. Millerd33e4552010-12-14 13:01:14 -0800135 .default_mtu = dn_dst_default_mtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .negative_advice = dn_dst_negative_advice,
137 .link_failure = dn_dst_link_failure,
138 .update_pmtu = dn_dst_update_pmtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800141static __inline__ unsigned dn_hash(__le16 src, __le16 dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800143 __u16 tmp = (__u16 __force)(src ^ dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 tmp ^= (tmp >> 3);
145 tmp ^= (tmp >> 5);
146 tmp ^= (tmp >> 10);
147 return dn_rt_hash_mask & (unsigned)tmp;
148}
149
150static inline void dnrt_free(struct dn_route *rt)
151{
Changli Gaod8d1f302010-06-10 23:31:35 -0700152 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155static inline void dnrt_drop(struct dn_route *rt)
156{
Changli Gaod8d1f302010-06-10 23:31:35 -0700157 dst_release(&rt->dst);
158 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161static void dn_dst_check_expire(unsigned long dummy)
162{
163 int i;
Eric Dumazetfc766e42010-10-29 03:09:24 +0000164 struct dn_route *rt;
165 struct dn_route __rcu **rtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 unsigned long now = jiffies;
167 unsigned long expire = 120 * HZ;
168
Eric Dumazetfc766e42010-10-29 03:09:24 +0000169 for (i = 0; i <= dn_rt_hash_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 rtp = &dn_rt_hash_table[i].chain;
171
172 spin_lock(&dn_rt_hash_table[i].lock);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000173 while ((rt = rcu_dereference_protected(*rtp,
174 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700175 if (atomic_read(&rt->dst.__refcnt) ||
176 (now - rt->dst.lastuse) < expire) {
177 rtp = &rt->dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 continue;
179 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700180 *rtp = rt->dst.dn_next;
181 rt->dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 dnrt_free(rt);
183 }
184 spin_unlock(&dn_rt_hash_table[i].lock);
185
186 if ((jiffies - now) > 0)
187 break;
188 }
189
190 mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
191}
192
Daniel Lezcano569d3642008-01-18 03:56:57 -0800193static int dn_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Eric Dumazetfc766e42010-10-29 03:09:24 +0000195 struct dn_route *rt;
196 struct dn_route __rcu **rtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int i;
198 unsigned long now = jiffies;
199 unsigned long expire = 10 * HZ;
200
Eric Dumazetfc766e42010-10-29 03:09:24 +0000201 for (i = 0; i <= dn_rt_hash_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 spin_lock_bh(&dn_rt_hash_table[i].lock);
204 rtp = &dn_rt_hash_table[i].chain;
205
Eric Dumazetfc766e42010-10-29 03:09:24 +0000206 while ((rt = rcu_dereference_protected(*rtp,
207 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700208 if (atomic_read(&rt->dst.__refcnt) ||
209 (now - rt->dst.lastuse) < expire) {
210 rtp = &rt->dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 continue;
212 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700213 *rtp = rt->dst.dn_next;
214 rt->dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dnrt_drop(rt);
216 break;
217 }
218 spin_unlock_bh(&dn_rt_hash_table[i].lock);
219 }
220
221 return 0;
222}
223
224/*
225 * The decnet standards don't impose a particular minimum mtu, what they
226 * do insist on is that the routing layer accepts a datagram of at least
227 * 230 bytes long. Here we have to subtract the routing header length from
228 * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
229 * assume the worst and use a long header size.
230 *
231 * We update both the mtu and the advertised mss (i.e. the segment size we
232 * advertise to the other end).
233 */
234static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu)
235{
236 u32 min_mtu = 230;
237 struct dn_dev *dn = dst->neighbour ?
Eric Dumazetfc766e42010-10-29 03:09:24 +0000238 rcu_dereference_raw(dst->neighbour->dev->dn_ptr) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (dn && dn->use_long == 0)
241 min_mtu -= 6;
242 else
243 min_mtu -= 21;
244
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700245 if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!(dst_metric_locked(dst, RTAX_MTU))) {
David S. Millerdefb3512010-12-08 21:16:57 -0800247 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dst_set_expires(dst, dn_rt_mtu_expires);
249 }
250 if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
251 u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
David S. Miller0dbaee32010-12-13 12:52:14 -0800252 u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
253 if (!existing_mss || existing_mss > mss)
David S. Millerdefb3512010-12-08 21:16:57 -0800254 dst_metric_set(dst, RTAX_ADVMSS, mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 }
257}
258
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900259/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * When a route has been marked obsolete. (e.g. routing cache flush)
261 */
262static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
263{
264 return NULL;
265}
266
267static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
268{
269 dst_release(dst);
270 return NULL;
271}
272
273static void dn_dst_link_failure(struct sk_buff *skb)
274{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
278{
Changli Gao58116622010-11-12 18:43:55 +0000279 return ((fl1->fld_dst ^ fl2->fld_dst) |
280 (fl1->fld_src ^ fl2->fld_src) |
Thomas Graf47dcf0c2006-11-09 15:20:38 -0800281 (fl1->mark ^ fl2->mark) |
Changli Gao58116622010-11-12 18:43:55 +0000282 (fl1->fld_scope ^ fl2->fld_scope) |
David S. Miller8238b212006-10-12 00:49:15 -0700283 (fl1->oif ^ fl2->oif) |
284 (fl1->iif ^ fl2->iif)) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287static int dn_insert_route(struct dn_route *rt, unsigned hash, struct dn_route **rp)
288{
Eric Dumazetfc766e42010-10-29 03:09:24 +0000289 struct dn_route *rth;
290 struct dn_route __rcu **rthp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 unsigned long now = jiffies;
292
293 rthp = &dn_rt_hash_table[hash].chain;
294
295 spin_lock_bh(&dn_rt_hash_table[hash].lock);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000296 while ((rth = rcu_dereference_protected(*rthp,
297 lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (compare_keys(&rth->fl, &rt->fl)) {
299 /* Put it first */
Changli Gaod8d1f302010-06-10 23:31:35 -0700300 *rthp = rth->dst.dn_next;
301 rcu_assign_pointer(rth->dst.dn_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 dn_rt_hash_table[hash].chain);
303 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
304
Changli Gaod8d1f302010-06-10 23:31:35 -0700305 dst_use(&rth->dst, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
307
308 dnrt_drop(rt);
309 *rp = rth;
310 return 0;
311 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700312 rthp = &rth->dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
Changli Gaod8d1f302010-06-10 23:31:35 -0700315 rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900317
Changli Gaod8d1f302010-06-10 23:31:35 -0700318 dst_use(&rt->dst, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
320 *rp = rt;
321 return 0;
322}
323
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800324static void dn_run_flush(unsigned long dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 int i;
327 struct dn_route *rt, *next;
328
Eric Dumazetfc766e42010-10-29 03:09:24 +0000329 for (i = 0; i < dn_rt_hash_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 spin_lock_bh(&dn_rt_hash_table[i].lock);
331
Eric Dumazetfc766e42010-10-29 03:09:24 +0000332 if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 goto nothing_to_declare;
334
Eric Dumazetfc766e42010-10-29 03:09:24 +0000335 for(; rt; rt = next) {
336 next = rcu_dereference_raw(rt->dst.dn_next);
337 RCU_INIT_POINTER(rt->dst.dn_next, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dst_free((struct dst_entry *)rt);
339 }
340
341nothing_to_declare:
342 spin_unlock_bh(&dn_rt_hash_table[i].lock);
343 }
344}
345
346static DEFINE_SPINLOCK(dn_rt_flush_lock);
347
348void dn_rt_cache_flush(int delay)
349{
350 unsigned long now = jiffies;
351 int user_mode = !in_interrupt();
352
353 if (delay < 0)
354 delay = dn_rt_min_delay;
355
356 spin_lock_bh(&dn_rt_flush_lock);
357
358 if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
359 long tmo = (long)(dn_rt_deadline - now);
360
361 if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
362 tmo = 0;
363
364 if (delay > tmo)
365 delay = tmo;
366 }
367
368 if (delay <= 0) {
369 spin_unlock_bh(&dn_rt_flush_lock);
370 dn_run_flush(0);
371 return;
372 }
373
374 if (dn_rt_deadline == 0)
375 dn_rt_deadline = now + dn_rt_max_delay;
376
377 dn_rt_flush_timer.expires = now + delay;
378 add_timer(&dn_rt_flush_timer);
379 spin_unlock_bh(&dn_rt_flush_lock);
380}
381
382/**
383 * dn_return_short - Return a short packet to its sender
384 * @skb: The packet to return
385 *
386 */
387static int dn_return_short(struct sk_buff *skb)
388{
389 struct dn_skb_cb *cb;
390 unsigned char *ptr;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800391 __le16 *src;
392 __le16 *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /* Add back headers */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700395 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
398 return NET_RX_DROP;
399
400 cb = DN_SKB_CB(skb);
401 /* Skip packet length and point to flags */
402 ptr = skb->data + 2;
403 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
404
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800405 dst = (__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800407 src = (__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 ptr += 2;
409 *ptr = 0; /* Zero hop count */
410
Ilpo Järvinena0bffff2009-03-21 13:36:17 -0700411 swap(*src, *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 skb->pkt_type = PACKET_OUTGOING;
414 dn_rt_finish_output(skb, NULL, NULL);
415 return NET_RX_SUCCESS;
416}
417
418/**
419 * dn_return_long - Return a long packet to its sender
420 * @skb: The long format packet to return
421 *
422 */
423static int dn_return_long(struct sk_buff *skb)
424{
425 struct dn_skb_cb *cb;
426 unsigned char *ptr;
427 unsigned char *src_addr, *dst_addr;
428 unsigned char tmp[ETH_ALEN];
429
430 /* Add back all headers */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700431 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
434 return NET_RX_DROP;
435
436 cb = DN_SKB_CB(skb);
437 /* Ignore packet length and point to flags */
438 ptr = skb->data + 2;
439
440 /* Skip padding */
441 if (*ptr & DN_RT_F_PF) {
442 char padlen = (*ptr & ~DN_RT_F_PF);
443 ptr += padlen;
444 }
445
446 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
447 ptr += 2;
448 dst_addr = ptr;
449 ptr += 8;
450 src_addr = ptr;
451 ptr += 6;
452 *ptr = 0; /* Zero hop count */
453
454 /* Swap source and destination */
455 memcpy(tmp, src_addr, ETH_ALEN);
456 memcpy(src_addr, dst_addr, ETH_ALEN);
457 memcpy(dst_addr, tmp, ETH_ALEN);
458
459 skb->pkt_type = PACKET_OUTGOING;
460 dn_rt_finish_output(skb, dst_addr, src_addr);
461 return NET_RX_SUCCESS;
462}
463
464/**
465 * dn_route_rx_packet - Try and find a route for an incoming packet
466 * @skb: The packet to find a route for
467 *
468 * Returns: result of input function if route is found, error code otherwise
469 */
470static int dn_route_rx_packet(struct sk_buff *skb)
471{
Eric Dumazetfc766e42010-10-29 03:09:24 +0000472 struct dn_skb_cb *cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 int err;
474
475 if ((err = dn_route_input(skb)) == 0)
476 return dst_input(skb);
477
Eric Dumazetfc766e42010-10-29 03:09:24 +0000478 cb = DN_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (decnet_debug_level & 4) {
480 char *devname = skb->dev ? skb->dev->name : "???";
Eric Dumazetfc766e42010-10-29 03:09:24 +0000481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 printk(KERN_DEBUG
483 "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 -0800484 (int)cb->rt_flags, devname, skb->len,
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800485 le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 err, skb->pkt_type);
487 }
488
489 if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
490 switch(cb->rt_flags & DN_RT_PKT_MSK) {
491 case DN_RT_PKT_SHORT:
492 return dn_return_short(skb);
493 case DN_RT_PKT_LONG:
494 return dn_return_long(skb);
495 }
496 }
497
498 kfree_skb(skb);
499 return NET_RX_DROP;
500}
501
502static int dn_route_rx_long(struct sk_buff *skb)
503{
504 struct dn_skb_cb *cb = DN_SKB_CB(skb);
505 unsigned char *ptr = skb->data;
506
507 if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
508 goto drop_it;
509
510 skb_pull(skb, 20);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300511 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900513 /* Destination info */
514 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800515 cb->dst = dn_eth2dn(ptr);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900516 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
517 goto drop_it;
518 ptr += 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900521 /* Source info */
522 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800523 cb->src = 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;
527 /* Other junk */
528 ptr++;
529 cb->hops = *ptr++; /* Visit Count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100531 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
532 dn_route_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534drop_it:
535 kfree_skb(skb);
536 return NET_RX_DROP;
537}
538
539
540
541static int dn_route_rx_short(struct sk_buff *skb)
542{
543 struct dn_skb_cb *cb = DN_SKB_CB(skb);
544 unsigned char *ptr = skb->data;
545
546 if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
547 goto drop_it;
548
549 skb_pull(skb, 5);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300550 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800552 cb->dst = *(__le16 *)ptr;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900553 ptr += 2;
554 cb->src = *(__le16 *)ptr;
555 ptr += 2;
556 cb->hops = *ptr & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100558 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
559 dn_route_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561drop_it:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900562 kfree_skb(skb);
563 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566static int dn_route_discard(struct sk_buff *skb)
567{
568 /*
569 * I know we drop the packet here, but thats considered success in
570 * this case
571 */
572 kfree_skb(skb);
573 return NET_RX_SUCCESS;
574}
575
576static int dn_route_ptp_hello(struct sk_buff *skb)
577{
578 dn_dev_hello(skb);
579 dn_neigh_pointopoint_hello(skb);
580 return NET_RX_SUCCESS;
581}
582
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700583int 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 -0700584{
585 struct dn_skb_cb *cb;
586 unsigned char flags = 0;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800587 __u16 len = le16_to_cpu(*(__le16 *)skb->data);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000588 struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 unsigned char padlen = 0;
590
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700591 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700592 goto dump_it;
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (dn == NULL)
595 goto dump_it;
596
597 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
598 goto out;
599
600 if (!pskb_may_pull(skb, 3))
601 goto dump_it;
602
603 skb_pull(skb, 2);
604
605 if (len > skb->len)
606 goto dump_it;
607
608 skb_trim(skb, len);
609
610 flags = *skb->data;
611
612 cb = DN_SKB_CB(skb);
613 cb->stamp = jiffies;
614 cb->iif = dev->ifindex;
615
616 /*
617 * If we have padding, remove it.
618 */
619 if (flags & DN_RT_F_PF) {
620 padlen = flags & ~DN_RT_F_PF;
621 if (!pskb_may_pull(skb, padlen + 1))
622 goto dump_it;
623 skb_pull(skb, padlen);
624 flags = *skb->data;
625 }
626
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700627 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /*
630 * Weed out future version DECnet
631 */
632 if (flags & DN_RT_F_VER)
633 goto dump_it;
634
635 cb->rt_flags = flags;
636
637 if (decnet_debug_level & 1)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900638 printk(KERN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900640 (int)flags, (dev) ? dev->name : "???", len, skb->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 padlen);
642
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900643 if (flags & DN_RT_PKT_CNTL) {
Herbert Xu364c6ba2006-06-09 16:10:40 -0700644 if (unlikely(skb_linearize(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 goto dump_it;
646
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900647 switch(flags & DN_RT_CNTL_MSK) {
648 case DN_RT_PKT_INIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 dn_dev_init_pkt(skb);
650 break;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900651 case DN_RT_PKT_VERI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 dn_dev_veri_pkt(skb);
653 break;
654 }
655
656 if (dn->parms.state != DN_DEV_S_RU)
657 goto dump_it;
658
659 switch(flags & DN_RT_CNTL_MSK) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900660 case DN_RT_PKT_HELO:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100661 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
662 skb, skb->dev, NULL,
663 dn_route_ptp_hello);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900665 case DN_RT_PKT_L1RT:
666 case DN_RT_PKT_L2RT:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100667 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
668 skb, skb->dev, NULL,
669 dn_route_discard);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900670 case DN_RT_PKT_ERTH:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100671 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
672 skb, skb->dev, NULL,
673 dn_neigh_router_hello);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900675 case DN_RT_PKT_EEDH:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100676 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
677 skb, skb->dev, NULL,
678 dn_neigh_endnode_hello);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900679 }
680 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (dn->parms.state != DN_DEV_S_RU)
682 goto dump_it;
683
684 skb_pull(skb, 1); /* Pull flags */
685
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900686 switch(flags & DN_RT_PKT_MSK) {
687 case DN_RT_PKT_LONG:
688 return dn_route_rx_long(skb);
689 case DN_RT_PKT_SHORT:
690 return dn_route_rx_short(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694dump_it:
695 kfree_skb(skb);
696out:
697 return NET_RX_DROP;
698}
699
700static int dn_output(struct sk_buff *skb)
701{
Eric Dumazetadf30902009-06-02 05:19:30 +0000702 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct dn_route *rt = (struct dn_route *)dst;
704 struct net_device *dev = dst->dev;
705 struct dn_skb_cb *cb = DN_SKB_CB(skb);
706 struct neighbour *neigh;
707
708 int err = -EINVAL;
709
710 if ((neigh = dst->neighbour) == NULL)
711 goto error;
712
713 skb->dev = dev;
714
715 cb->src = rt->rt_saddr;
716 cb->dst = rt->rt_daddr;
717
718 /*
719 * Always set the Intra-Ethernet bit on all outgoing packets
720 * originated on this node. Only valid flag from upper layers
721 * is return-to-sender-requested. Set hop count to 0 too.
722 */
723 cb->rt_flags &= ~DN_RT_F_RQR;
724 cb->rt_flags |= DN_RT_F_IE;
725 cb->hops = 0;
726
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100727 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
728 neigh->output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730error:
731 if (net_ratelimit())
732 printk(KERN_DEBUG "dn_output: This should not happen\n");
733
734 kfree_skb(skb);
735
736 return err;
737}
738
739static int dn_forward(struct sk_buff *skb)
740{
741 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000742 struct dst_entry *dst = skb_dst(skb);
Eric Dumazetfc766e42010-10-29 03:09:24 +0000743 struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct dn_route *rt;
745 struct neighbour *neigh = dst->neighbour;
746 int header_len;
747#ifdef CONFIG_NETFILTER
748 struct net_device *dev = skb->dev;
749#endif
750
751 if (skb->pkt_type != PACKET_HOST)
752 goto drop;
753
754 /* Ensure that we have enough space for headers */
Eric Dumazetadf30902009-06-02 05:19:30 +0000755 rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 header_len = dn_db->use_long ? 21 : 6;
Changli Gaod8d1f302010-06-10 23:31:35 -0700757 if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 goto drop;
759
760 /*
761 * Hop count exceeded.
762 */
763 if (++cb->hops > 30)
764 goto drop;
765
Changli Gaod8d1f302010-06-10 23:31:35 -0700766 skb->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /*
769 * If packet goes out same interface it came in on, then set
770 * the Intra-Ethernet bit. This has no effect for short
771 * packets, so we don't need to test for them here.
772 */
773 cb->rt_flags &= ~DN_RT_F_IE;
774 if (rt->rt_flags & RTCF_DOREDIRECT)
775 cb->rt_flags |= DN_RT_F_IE;
776
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100777 return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
778 neigh->output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780drop:
781 kfree_skb(skb);
782 return NET_RX_DROP;
783}
784
785/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * Used to catch bugs. This should never normally get
787 * called.
788 */
789static int dn_rt_bug(struct sk_buff *skb)
790{
791 if (net_ratelimit()) {
792 struct dn_skb_cb *cb = DN_SKB_CB(skb);
793
794 printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800795 le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
798 kfree_skb(skb);
799
Florian Westphal0e8635a2009-06-20 00:53:25 +0000800 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
David S. Miller0dbaee32010-12-13 12:52:14 -0800803static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
804{
805 return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
806}
807
David S. Millerd33e4552010-12-14 13:01:14 -0800808static unsigned int dn_dst_default_mtu(const struct dst_entry *dst)
809{
810 return dst->dev->mtu;
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
814{
815 struct dn_fib_info *fi = res->fi;
Changli Gaod8d1f302010-06-10 23:31:35 -0700816 struct net_device *dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct neighbour *n;
David S. Miller0dbaee32010-12-13 12:52:14 -0800818 unsigned int metric;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (fi) {
821 if (DN_FIB_RES_GW(*res) &&
822 DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
823 rt->rt_gateway = DN_FIB_RES_GW(*res);
David S. Millerdefb3512010-12-08 21:16:57 -0800824 dst_import_metrics(&rt->dst, fi->fib_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826 rt->rt_type = res->type;
827
Changli Gaod8d1f302010-06-10 23:31:35 -0700828 if (dev != NULL && rt->dst.neighbour == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
830 if (IS_ERR(n))
831 return PTR_ERR(n);
Changli Gaod8d1f302010-06-10 23:31:35 -0700832 rt->dst.neighbour = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
David S. Millerd33e4552010-12-14 13:01:14 -0800835 if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
David S. Millerdefb3512010-12-08 21:16:57 -0800836 dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
David S. Miller0dbaee32010-12-13 12:52:14 -0800837 metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
838 if (metric) {
839 unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
840 if (metric > mss)
841 dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return 0;
844}
845
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800846static inline int dn_match_addr(__le16 addr1, __le16 addr2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800848 __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 int match = 16;
850 while(tmp) {
851 tmp >>= 1;
852 match--;
853 }
854 return match;
855}
856
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800857static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800859 __le16 saddr = 0;
Eric Dumazetfc766e42010-10-29 03:09:24 +0000860 struct dn_dev *dn_db;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct dn_ifaddr *ifa;
862 int best_match = 0;
863 int ret;
864
Eric Dumazetfc766e42010-10-29 03:09:24 +0000865 rcu_read_lock();
866 dn_db = rcu_dereference(dev->dn_ptr);
867 for (ifa = rcu_dereference(dn_db->ifa_list);
868 ifa != NULL;
869 ifa = rcu_dereference(ifa->ifa_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (ifa->ifa_scope > scope)
871 continue;
872 if (!daddr) {
873 saddr = ifa->ifa_local;
874 break;
875 }
876 ret = dn_match_addr(daddr, ifa->ifa_local);
877 if (ret > best_match)
878 saddr = ifa->ifa_local;
879 if (best_match == 0)
880 saddr = ifa->ifa_local;
881 }
Eric Dumazetfc766e42010-10-29 03:09:24 +0000882 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 return saddr;
885}
886
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800887static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
890}
891
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800892static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800894 __le16 mask = dnet_make_mask(res->prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
896}
897
898static int dn_route_output_slow(struct dst_entry **pprt, const struct flowi *oldflp, int try_hard)
899{
Changli Gao58116622010-11-12 18:43:55 +0000900 struct flowi fl = { .fld_dst = oldflp->fld_dst,
901 .fld_src = oldflp->fld_src,
902 .fld_scope = RT_SCOPE_UNIVERSE,
Thomas Graf47dcf0c2006-11-09 15:20:38 -0800903 .mark = oldflp->mark,
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700904 .iif = init_net.loopback_dev->ifindex,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 .oif = oldflp->oif };
906 struct dn_route *rt = NULL;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700907 struct net_device *dev_out = NULL, *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct neighbour *neigh = NULL;
909 unsigned hash;
910 unsigned flags = 0;
911 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
912 int err;
913 int free_res = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800914 __le16 gateway = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (decnet_debug_level & 16)
917 printk(KERN_DEBUG
918 "dn_route_output_slow: dst=%04x src=%04x mark=%d"
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800919 " iif=%d oif=%d\n", le16_to_cpu(oldflp->fld_dst),
920 le16_to_cpu(oldflp->fld_src),
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700921 oldflp->mark, init_net.loopback_dev->ifindex, oldflp->oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* If we have an output interface, verify its a DECnet device */
924 if (oldflp->oif) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700925 dev_out = dev_get_by_index(&init_net, oldflp->oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 err = -ENODEV;
927 if (dev_out && dev_out->dn_ptr == NULL) {
928 dev_put(dev_out);
929 dev_out = NULL;
930 }
931 if (dev_out == NULL)
932 goto out;
933 }
934
935 /* If we have a source address, verify that its a local address */
936 if (oldflp->fld_src) {
937 err = -EADDRNOTAVAIL;
938
939 if (dev_out) {
940 if (dn_dev_islocal(dev_out, oldflp->fld_src))
941 goto source_ok;
942 dev_put(dev_out);
943 goto out;
944 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800945 rcu_read_lock();
946 for_each_netdev_rcu(&init_net, dev) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700947 if (!dev->dn_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700949 if (!dn_dev_islocal(dev, oldflp->fld_src))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700950 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700951 if ((dev->flags & IFF_LOOPBACK) &&
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700952 oldflp->fld_dst &&
Pavel Emelianov7562f872007-05-03 15:13:45 -0700953 !dn_dev_islocal(dev, oldflp->fld_dst))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700954 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700955
956 dev_out = dev;
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700957 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800959 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (dev_out == NULL)
961 goto out;
962 dev_hold(dev_out);
963source_ok:
964 ;
965 }
966
967 /* No destination? Assume its local */
968 if (!fl.fld_dst) {
969 fl.fld_dst = fl.fld_src;
970
971 err = -EADDRNOTAVAIL;
972 if (dev_out)
973 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700974 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 dev_hold(dev_out);
976 if (!fl.fld_dst) {
977 fl.fld_dst =
978 fl.fld_src = dnet_select_source(dev_out, 0,
979 RT_SCOPE_HOST);
980 if (!fl.fld_dst)
981 goto out;
982 }
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700983 fl.oif = init_net.loopback_dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 res.type = RTN_LOCAL;
985 goto make_route;
986 }
987
988 if (decnet_debug_level & 16)
989 printk(KERN_DEBUG
990 "dn_route_output_slow: initial checks complete."
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800991 " dst=%o4x src=%04x oif=%d try_hard=%d\n",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800992 le16_to_cpu(fl.fld_dst), le16_to_cpu(fl.fld_src),
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800993 fl.oif, try_hard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /*
996 * N.B. If the kernel is compiled without router support then
997 * dn_fib_lookup() will evaluate to non-zero so this if () block
998 * will always be executed.
999 */
1000 err = -ESRCH;
1001 if (try_hard || (err = dn_fib_lookup(&fl, &res)) != 0) {
1002 struct dn_dev *dn_db;
1003 if (err != -ESRCH)
1004 goto out;
1005 /*
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001006 * Here the fallback is basically the standard algorithm for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 * routing in endnodes which is described in the DECnet routing
1008 * docs
1009 *
1010 * If we are not trying hard, look in neighbour cache.
1011 * The result is tested to ensure that if a specific output
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001012 * device/source address was requested, then we honour that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * here
1014 */
1015 if (!try_hard) {
Eric W. Biederman426b5302008-01-24 00:13:18 -08001016 neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fl.fld_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (neigh) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001018 if ((oldflp->oif &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 (neigh->dev->ifindex != oldflp->oif)) ||
1020 (oldflp->fld_src &&
1021 (!dn_dev_islocal(neigh->dev,
1022 oldflp->fld_src)))) {
1023 neigh_release(neigh);
1024 neigh = NULL;
1025 } else {
1026 if (dev_out)
1027 dev_put(dev_out);
1028 if (dn_dev_islocal(neigh->dev, fl.fld_dst)) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001029 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 res.type = RTN_LOCAL;
1031 } else {
1032 dev_out = neigh->dev;
1033 }
1034 dev_hold(dev_out);
1035 goto select_source;
1036 }
1037 }
1038 }
1039
1040 /* Not there? Perhaps its a local address */
1041 if (dev_out == NULL)
1042 dev_out = dn_dev_get_default();
1043 err = -ENODEV;
1044 if (dev_out == NULL)
1045 goto out;
Eric Dumazetfc766e42010-10-29 03:09:24 +00001046 dn_db = rcu_dereference_raw(dev_out->dn_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /* Possible improvement - check all devices for local addr */
1048 if (dn_dev_islocal(dev_out, fl.fld_dst)) {
1049 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001050 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 dev_hold(dev_out);
1052 res.type = RTN_LOCAL;
1053 goto select_source;
1054 }
1055 /* Not local either.... try sending it to the default router */
1056 neigh = neigh_clone(dn_db->router);
1057 BUG_ON(neigh && neigh->dev != dev_out);
1058
1059 /* Ok then, we assume its directly connected and move on */
1060select_source:
1061 if (neigh)
1062 gateway = ((struct dn_neigh *)neigh)->addr;
1063 if (gateway == 0)
1064 gateway = fl.fld_dst;
1065 if (fl.fld_src == 0) {
1066 fl.fld_src = dnet_select_source(dev_out, gateway,
1067 res.type == RTN_LOCAL ?
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001068 RT_SCOPE_HOST :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 RT_SCOPE_LINK);
1070 if (fl.fld_src == 0 && res.type != RTN_LOCAL)
1071 goto e_addr;
1072 }
1073 fl.oif = dev_out->ifindex;
1074 goto make_route;
1075 }
1076 free_res = 1;
1077
1078 if (res.type == RTN_NAT)
1079 goto e_inval;
1080
1081 if (res.type == RTN_LOCAL) {
1082 if (!fl.fld_src)
1083 fl.fld_src = fl.fld_dst;
1084 if (dev_out)
1085 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001086 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 dev_hold(dev_out);
1088 fl.oif = dev_out->ifindex;
1089 if (res.fi)
1090 dn_fib_info_put(res.fi);
1091 res.fi = NULL;
1092 goto make_route;
1093 }
1094
1095 if (res.fi->fib_nhs > 1 && fl.oif == 0)
1096 dn_fib_select_multipath(&fl, &res);
1097
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001098 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 * We could add some logic to deal with default routes here and
1100 * get rid of some of the special casing above.
1101 */
1102
1103 if (!fl.fld_src)
1104 fl.fld_src = DN_FIB_RES_PREFSRC(res);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (dev_out)
1107 dev_put(dev_out);
1108 dev_out = DN_FIB_RES_DEV(res);
1109 dev_hold(dev_out);
1110 fl.oif = dev_out->ifindex;
1111 gateway = DN_FIB_RES_GW(res);
1112
1113make_route:
1114 if (dev_out->flags & IFF_LOOPBACK)
1115 flags |= RTCF_LOCAL;
1116
1117 rt = dst_alloc(&dn_dst_ops);
1118 if (rt == NULL)
1119 goto e_nobufs;
1120
Changli Gaod8d1f302010-06-10 23:31:35 -07001121 atomic_set(&rt->dst.__refcnt, 1);
1122 rt->dst.flags = DST_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 rt->fl.fld_src = oldflp->fld_src;
1125 rt->fl.fld_dst = oldflp->fld_dst;
1126 rt->fl.oif = oldflp->oif;
1127 rt->fl.iif = 0;
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001128 rt->fl.mark = oldflp->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 rt->rt_saddr = fl.fld_src;
1131 rt->rt_daddr = fl.fld_dst;
1132 rt->rt_gateway = gateway ? gateway : fl.fld_dst;
1133 rt->rt_local_src = fl.fld_src;
1134
1135 rt->rt_dst_map = fl.fld_dst;
1136 rt->rt_src_map = fl.fld_src;
1137
Changli Gaod8d1f302010-06-10 23:31:35 -07001138 rt->dst.dev = dev_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 dev_hold(dev_out);
Changli Gaod8d1f302010-06-10 23:31:35 -07001140 rt->dst.neighbour = neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 neigh = NULL;
1142
Changli Gaod8d1f302010-06-10 23:31:35 -07001143 rt->dst.lastuse = jiffies;
1144 rt->dst.output = dn_output;
1145 rt->dst.input = dn_rt_bug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 rt->rt_flags = flags;
1147 if (flags & RTCF_LOCAL)
Changli Gaod8d1f302010-06-10 23:31:35 -07001148 rt->dst.input = dn_nsp_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 err = dn_rt_set_next_hop(rt, &res);
1151 if (err)
1152 goto e_neighbour;
1153
1154 hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst);
1155 dn_insert_route(rt, hash, (struct dn_route **)pprt);
1156
1157done:
1158 if (neigh)
1159 neigh_release(neigh);
1160 if (free_res)
1161 dn_fib_res_put(&res);
1162 if (dev_out)
1163 dev_put(dev_out);
1164out:
1165 return err;
1166
1167e_addr:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001168 err = -EADDRNOTAVAIL;
1169 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170e_inval:
1171 err = -EINVAL;
1172 goto done;
1173e_nobufs:
1174 err = -ENOBUFS;
1175 goto done;
1176e_neighbour:
Changli Gaod8d1f302010-06-10 23:31:35 -07001177 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto e_nobufs;
1179}
1180
1181
1182/*
1183 * N.B. The flags may be moved into the flowi at some future stage.
1184 */
1185static int __dn_route_output_key(struct dst_entry **pprt, const struct flowi *flp, int flags)
1186{
1187 unsigned hash = dn_hash(flp->fld_src, flp->fld_dst);
1188 struct dn_route *rt = NULL;
1189
1190 if (!(flags & MSG_TRYHARD)) {
1191 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001192 for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07001193 rt = rcu_dereference_bh(rt->dst.dn_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if ((flp->fld_dst == rt->fl.fld_dst) &&
1195 (flp->fld_src == rt->fl.fld_src) &&
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001196 (flp->mark == rt->fl.mark) &&
David S. Millerc7537962010-11-11 17:07:48 -08001197 dn_is_output_route(rt) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 (rt->fl.oif == flp->oif)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001199 dst_use(&rt->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 rcu_read_unlock_bh();
Changli Gaod8d1f302010-06-10 23:31:35 -07001201 *pprt = &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203 }
1204 }
1205 rcu_read_unlock_bh();
1206 }
1207
1208 return dn_route_output_slow(pprt, flp, flags);
1209}
1210
1211static int dn_route_output_key(struct dst_entry **pprt, struct flowi *flp, int flags)
1212{
1213 int err;
1214
1215 err = __dn_route_output_key(pprt, flp, flags);
1216 if (err == 0 && flp->proto) {
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001217 err = xfrm_lookup(&init_net, pprt, flp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 return err;
1220}
1221
1222int dn_route_output_sock(struct dst_entry **pprt, struct flowi *fl, struct sock *sk, int flags)
1223{
1224 int err;
1225
1226 err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
1227 if (err == 0 && fl->proto) {
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001228 err = xfrm_lookup(&init_net, pprt, fl, sk,
1229 (flags & MSG_DONTWAIT) ? 0 : XFRM_LOOKUP_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231 return err;
1232}
1233
1234static int dn_route_input_slow(struct sk_buff *skb)
1235{
1236 struct dn_route *rt = NULL;
1237 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1238 struct net_device *in_dev = skb->dev;
1239 struct net_device *out_dev = NULL;
1240 struct dn_dev *dn_db;
1241 struct neighbour *neigh = NULL;
1242 unsigned hash;
1243 int flags = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001244 __le16 gateway = 0;
1245 __le16 local_src = 0;
Changli Gao58116622010-11-12 18:43:55 +00001246 struct flowi fl = { .fld_dst = cb->dst,
1247 .fld_src = cb->src,
1248 .fld_scope = RT_SCOPE_UNIVERSE,
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001249 .mark = skb->mark,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 .iif = skb->dev->ifindex };
1251 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1252 int err = -EINVAL;
1253 int free_res = 0;
1254
1255 dev_hold(in_dev);
1256
Eric Dumazetfc766e42010-10-29 03:09:24 +00001257 if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 goto out;
1259
1260 /* Zero source addresses are not allowed */
1261 if (fl.fld_src == 0)
1262 goto out;
1263
1264 /*
1265 * In this case we've just received a packet from a source
1266 * outside ourselves pretending to come from us. We don't
1267 * allow it any further to prevent routing loops, spoofing and
1268 * other nasties. Loopback packets already have the dst attached
1269 * so this only affects packets which have originated elsewhere.
1270 */
1271 err = -ENOTUNIQ;
1272 if (dn_dev_islocal(in_dev, cb->src))
1273 goto out;
1274
1275 err = dn_fib_lookup(&fl, &res);
1276 if (err) {
1277 if (err != -ESRCH)
1278 goto out;
1279 /*
1280 * Is the destination us ?
1281 */
1282 if (!dn_dev_islocal(in_dev, cb->dst))
1283 goto e_inval;
1284
1285 res.type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 } else {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001287 __le16 src_map = fl.fld_src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 free_res = 1;
1289
1290 out_dev = DN_FIB_RES_DEV(res);
1291 if (out_dev == NULL) {
1292 if (net_ratelimit())
1293 printk(KERN_CRIT "Bug in dn_route_input_slow() "
1294 "No output device\n");
1295 goto e_inval;
1296 }
1297 dev_hold(out_dev);
1298
1299 if (res.r)
Steven Whitehousea8731cb2006-08-09 15:56:46 -07001300 src_map = fl.fld_src; /* no NAT support for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 gateway = DN_FIB_RES_GW(res);
1303 if (res.type == RTN_NAT) {
1304 fl.fld_dst = dn_fib_rules_map_destination(fl.fld_dst, &res);
1305 dn_fib_res_put(&res);
1306 free_res = 0;
1307 if (dn_fib_lookup(&fl, &res))
1308 goto e_inval;
1309 free_res = 1;
1310 if (res.type != RTN_UNICAST)
1311 goto e_inval;
1312 flags |= RTCF_DNAT;
1313 gateway = fl.fld_dst;
1314 }
1315 fl.fld_src = src_map;
1316 }
1317
1318 switch(res.type) {
1319 case RTN_UNICAST:
1320 /*
1321 * Forwarding check here, we only check for forwarding
1322 * being turned off, if you want to only forward intra
1323 * area, its up to you to set the routing tables up
1324 * correctly.
1325 */
1326 if (dn_db->parms.forwarding == 0)
1327 goto e_inval;
1328
1329 if (res.fi->fib_nhs > 1 && fl.oif == 0)
1330 dn_fib_select_multipath(&fl, &res);
1331
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001332 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1334 * flag as a hint to set the intra-ethernet bit when
1335 * forwarding. If we've got NAT in operation, we don't do
1336 * this optimisation.
1337 */
1338 if (out_dev == in_dev && !(flags & RTCF_NAT))
1339 flags |= RTCF_DOREDIRECT;
1340
1341 local_src = DN_FIB_RES_PREFSRC(res);
1342
1343 case RTN_BLACKHOLE:
1344 case RTN_UNREACHABLE:
1345 break;
1346 case RTN_LOCAL:
1347 flags |= RTCF_LOCAL;
1348 fl.fld_src = cb->dst;
1349 fl.fld_dst = cb->src;
1350
1351 /* Routing tables gave us a gateway */
1352 if (gateway)
1353 goto make_route;
1354
1355 /* Packet was intra-ethernet, so we know its on-link */
Steven Whitehouse3a31b9d2006-10-18 20:45:22 -07001356 if (cb->rt_flags & DN_RT_F_IE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 gateway = cb->src;
1358 flags |= RTCF_DIRECTSRC;
1359 goto make_route;
1360 }
1361
1362 /* Use the default router if there is one */
1363 neigh = neigh_clone(dn_db->router);
1364 if (neigh) {
1365 gateway = ((struct dn_neigh *)neigh)->addr;
1366 goto make_route;
1367 }
1368
1369 /* Close eyes and pray */
1370 gateway = cb->src;
1371 flags |= RTCF_DIRECTSRC;
1372 goto make_route;
1373 default:
1374 goto e_inval;
1375 }
1376
1377make_route:
1378 rt = dst_alloc(&dn_dst_ops);
1379 if (rt == NULL)
1380 goto e_nobufs;
1381
1382 rt->rt_saddr = fl.fld_src;
1383 rt->rt_daddr = fl.fld_dst;
1384 rt->rt_gateway = fl.fld_dst;
1385 if (gateway)
1386 rt->rt_gateway = gateway;
1387 rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
1388
1389 rt->rt_dst_map = fl.fld_dst;
1390 rt->rt_src_map = fl.fld_src;
1391
1392 rt->fl.fld_src = cb->src;
1393 rt->fl.fld_dst = cb->dst;
1394 rt->fl.oif = 0;
1395 rt->fl.iif = in_dev->ifindex;
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001396 rt->fl.mark = fl.mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Changli Gaod8d1f302010-06-10 23:31:35 -07001398 rt->dst.flags = DST_HOST;
1399 rt->dst.neighbour = neigh;
1400 rt->dst.dev = out_dev;
1401 rt->dst.lastuse = jiffies;
1402 rt->dst.output = dn_rt_bug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 switch(res.type) {
1404 case RTN_UNICAST:
Changli Gaod8d1f302010-06-10 23:31:35 -07001405 rt->dst.input = dn_forward;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 break;
1407 case RTN_LOCAL:
Changli Gaod8d1f302010-06-10 23:31:35 -07001408 rt->dst.output = dn_output;
1409 rt->dst.input = dn_nsp_rx;
1410 rt->dst.dev = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 flags |= RTCF_LOCAL;
1412 break;
1413 default:
1414 case RTN_UNREACHABLE:
1415 case RTN_BLACKHOLE:
Changli Gaod8d1f302010-06-10 23:31:35 -07001416 rt->dst.input = dst_discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418 rt->rt_flags = flags;
Changli Gaod8d1f302010-06-10 23:31:35 -07001419 if (rt->dst.dev)
1420 dev_hold(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 err = dn_rt_set_next_hop(rt, &res);
1423 if (err)
1424 goto e_neighbour;
1425
1426 hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst);
Eric Dumazetadf30902009-06-02 05:19:30 +00001427 dn_insert_route(rt, hash, &rt);
Changli Gaod8d1f302010-06-10 23:31:35 -07001428 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430done:
1431 if (neigh)
1432 neigh_release(neigh);
1433 if (free_res)
1434 dn_fib_res_put(&res);
1435 dev_put(in_dev);
1436 if (out_dev)
1437 dev_put(out_dev);
1438out:
1439 return err;
1440
1441e_inval:
1442 err = -EINVAL;
1443 goto done;
1444
1445e_nobufs:
1446 err = -ENOBUFS;
1447 goto done;
1448
1449e_neighbour:
Changli Gaod8d1f302010-06-10 23:31:35 -07001450 dst_free(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 goto done;
1452}
1453
Roel Kluin5eaa65b2008-12-10 15:18:31 -08001454static int dn_route_input(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 struct dn_route *rt;
1457 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1458 unsigned hash = dn_hash(cb->src, cb->dst);
1459
Eric Dumazetadf30902009-06-02 05:19:30 +00001460 if (skb_dst(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return 0;
1462
1463 rcu_read_lock();
1464 for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
Changli Gaod8d1f302010-06-10 23:31:35 -07001465 rt = rcu_dereference(rt->dst.dn_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if ((rt->fl.fld_src == cb->src) &&
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001467 (rt->fl.fld_dst == cb->dst) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 (rt->fl.oif == 0) &&
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001469 (rt->fl.mark == skb->mark) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 (rt->fl.iif == cb->iif)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001471 dst_use(&rt->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 rcu_read_unlock();
Eric Dumazetadf30902009-06-02 05:19:30 +00001473 skb_dst_set(skb, (struct dst_entry *)rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 0;
1475 }
1476 }
1477 rcu_read_unlock();
1478
1479 return dn_route_input_slow(skb);
1480}
1481
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001482static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1483 int event, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Eric Dumazetadf30902009-06-02 05:19:30 +00001485 struct dn_route *rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 struct rtmsg *r;
1487 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001488 unsigned char *b = skb_tail_pointer(skb);
Thomas Grafe3703b32006-11-27 09:27:07 -08001489 long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001491 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 r = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 r->rtm_family = AF_DECnet;
1494 r->rtm_dst_len = 16;
1495 r->rtm_src_len = 0;
1496 r->rtm_tos = 0;
1497 r->rtm_table = RT_TABLE_MAIN;
Patrick McHardy9e762a42006-08-10 23:09:48 -07001498 RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 r->rtm_type = rt->rt_type;
1500 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1501 r->rtm_scope = RT_SCOPE_UNIVERSE;
1502 r->rtm_protocol = RTPROT_UNSPEC;
1503 if (rt->rt_flags & RTCF_NOTIFY)
1504 r->rtm_flags |= RTM_F_NOTIFY;
1505 RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
1506 if (rt->fl.fld_src) {
1507 r->rtm_src_len = 16;
1508 RTA_PUT(skb, RTA_SRC, 2, &rt->fl.fld_src);
1509 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001510 if (rt->dst.dev)
1511 RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /*
1513 * Note to self - change this if input routes reverse direction when
1514 * they deal only with inputs and not with replies like they do
1515 * currently.
1516 */
1517 RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
1518 if (rt->rt_daddr != rt->rt_gateway)
1519 RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
David S. Millerdefb3512010-12-08 21:16:57 -08001520 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 goto rtattr_failure;
Changli Gaod8d1f302010-06-10 23:31:35 -07001522 expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
1523 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
1524 rt->dst.error) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08001525 goto rtattr_failure;
David S. Millerc7537962010-11-11 17:07:48 -08001526 if (dn_is_input_route(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fl.iif);
1528
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001529 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return skb->len;
1531
1532nlmsg_failure:
1533rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001534 nlmsg_trim(skb, b);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001535 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
1538/*
1539 * This is called by both endnodes and routers now.
1540 */
Thomas Graffa34ddd2007-03-22 11:57:46 -07001541static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001543 struct net *net = sock_net(in_skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 struct rtattr **rta = arg;
1545 struct rtmsg *rtm = NLMSG_DATA(nlh);
1546 struct dn_route *rt = NULL;
1547 struct dn_skb_cb *cb;
1548 int err;
1549 struct sk_buff *skb;
1550 struct flowi fl;
1551
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001552 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001553 return -EINVAL;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 memset(&fl, 0, sizeof(fl));
1556 fl.proto = DNPROTO_NSP;
1557
1558 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1559 if (skb == NULL)
1560 return -ENOBUFS;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001561 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 cb = DN_SKB_CB(skb);
1563
1564 if (rta[RTA_SRC-1])
1565 memcpy(&fl.fld_src, RTA_DATA(rta[RTA_SRC-1]), 2);
1566 if (rta[RTA_DST-1])
1567 memcpy(&fl.fld_dst, RTA_DATA(rta[RTA_DST-1]), 2);
1568 if (rta[RTA_IIF-1])
1569 memcpy(&fl.iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
1570
1571 if (fl.iif) {
1572 struct net_device *dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001573 if ((dev = dev_get_by_index(&init_net, fl.iif)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 kfree_skb(skb);
1575 return -ENODEV;
1576 }
1577 if (!dev->dn_ptr) {
1578 dev_put(dev);
1579 kfree_skb(skb);
1580 return -ENODEV;
1581 }
YOSHIFUJI Hideakib98999d2007-12-12 03:51:49 +09001582 skb->protocol = htons(ETH_P_DNA_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 skb->dev = dev;
1584 cb->src = fl.fld_src;
1585 cb->dst = fl.fld_dst;
1586 local_bh_disable();
1587 err = dn_route_input(skb);
1588 local_bh_enable();
1589 memset(cb, 0, sizeof(struct dn_skb_cb));
Eric Dumazetadf30902009-06-02 05:19:30 +00001590 rt = (struct dn_route *)skb_dst(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001591 if (!err && -rt->dst.error)
1592 err = rt->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 } else {
1594 int oif = 0;
1595 if (rta[RTA_OIF - 1])
1596 memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
1597 fl.oif = oif;
1598 err = dn_route_output_key((struct dst_entry **)&rt, &fl, 0);
1599 }
1600
1601 if (skb->dev)
1602 dev_put(skb->dev);
1603 skb->dev = NULL;
1604 if (err)
1605 goto out_free;
Changli Gaod8d1f302010-06-10 23:31:35 -07001606 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (rtm->rtm_flags & RTM_F_NOTIFY)
1608 rt->rt_flags |= RTCF_NOTIFY;
1609
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001610 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 -07001611
1612 if (err == 0)
1613 goto out_free;
1614 if (err < 0) {
1615 err = -EMSGSIZE;
1616 goto out_free;
1617 }
1618
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001619 return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621out_free:
1622 kfree_skb(skb);
1623 return err;
1624}
1625
1626/*
1627 * For routers, this is called from dn_fib_dump, but for endnodes its
1628 * called directly from the rtnetlink dispatch table.
1629 */
1630int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1631{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001632 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 struct dn_route *rt;
1634 int h, s_h;
1635 int idx, s_idx;
1636
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001637 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001638 return 0;
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
1641 return -EINVAL;
1642 if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
1643 return 0;
1644
1645 s_h = cb->args[0];
1646 s_idx = idx = cb->args[1];
1647 for(h = 0; h <= dn_rt_hash_mask; h++) {
1648 if (h < s_h)
1649 continue;
1650 if (h > s_h)
1651 s_idx = 0;
1652 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001653 for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07001655 rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (idx < s_idx)
1657 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07001658 skb_dst_set(skb, dst_clone(&rt->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001660 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001661 1, NLM_F_MULTI) <= 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001662 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 rcu_read_unlock_bh();
1664 goto done;
1665 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001666 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668 rcu_read_unlock_bh();
1669 }
1670
1671done:
1672 cb->args[0] = h;
1673 cb->args[1] = idx;
1674 return skb->len;
1675}
1676
1677#ifdef CONFIG_PROC_FS
1678struct dn_rt_cache_iter_state {
1679 int bucket;
1680};
1681
1682static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1683{
1684 struct dn_route *rt = NULL;
1685 struct dn_rt_cache_iter_state *s = seq->private;
1686
1687 for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1688 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001689 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (rt)
1691 break;
1692 rcu_read_unlock_bh();
1693 }
Paul E. McKenneya898def2010-02-22 17:04:49 -08001694 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
1697static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1698{
Eric Dumazet0d89d792008-01-10 22:35:21 -08001699 struct dn_rt_cache_iter_state *s = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Eric Dumazetfc766e42010-10-29 03:09:24 +00001701 rt = rcu_dereference_bh(rt->dst.dn_next);
1702 while (!rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 rcu_read_unlock_bh();
1704 if (--s->bucket < 0)
1705 break;
1706 rcu_read_lock_bh();
Eric Dumazetfc766e42010-10-29 03:09:24 +00001707 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
Eric Dumazetfc766e42010-10-29 03:09:24 +00001709 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1713{
1714 struct dn_route *rt = dn_rt_cache_get_first(seq);
1715
1716 if (rt) {
1717 while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1718 --*pos;
1719 }
1720 return *pos ? NULL : rt;
1721}
1722
1723static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1724{
1725 struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1726 ++*pos;
1727 return rt;
1728}
1729
1730static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1731{
1732 if (v)
1733 rcu_read_unlock_bh();
1734}
1735
1736static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1737{
1738 struct dn_route *rt = v;
1739 char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1740
1741 seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07001742 rt->dst.dev ? rt->dst.dev->name : "*",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -08001743 dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1744 dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
Changli Gaod8d1f302010-06-10 23:31:35 -07001745 atomic_read(&rt->dst.__refcnt),
1746 rt->dst.__use,
1747 (int) dst_metric(&rt->dst, RTAX_RTT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001749}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001751static const struct seq_operations dn_rt_cache_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 .start = dn_rt_cache_seq_start,
1753 .next = dn_rt_cache_seq_next,
1754 .stop = dn_rt_cache_seq_stop,
1755 .show = dn_rt_cache_seq_show,
1756};
1757
1758static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1759{
Pavel Emelyanov31164082007-10-10 02:30:23 -07001760 return seq_open_private(file, &dn_rt_cache_seq_ops,
1761 sizeof(struct dn_rt_cache_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Arjan van de Ven9a321442007-02-12 00:55:35 -08001764static const struct file_operations dn_rt_cache_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 .owner = THIS_MODULE,
1766 .open = dn_rt_cache_seq_open,
1767 .read = seq_read,
1768 .llseek = seq_lseek,
1769 .release = seq_release_private,
1770};
1771
1772#endif /* CONFIG_PROC_FS */
1773
1774void __init dn_route_init(void)
1775{
1776 int i, goal, order;
1777
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07001778 dn_dst_ops.kmem_cachep =
1779 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001780 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001781 dst_entries_init(&dn_dst_ops);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001782 setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1784 add_timer(&dn_route_timer);
1785
Jan Beulich44813742009-09-21 17:03:05 -07001786 goal = totalram_pages >> (26 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 for(order = 0; (1UL << order) < goal; order++)
1789 /* NOTHING */;
1790
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001791 /*
1792 * Only want 1024 entries max, since the table is very, very unlikely
1793 * to be larger than that.
1794 */
1795 while(order && ((((1UL << order) * PAGE_SIZE) /
1796 sizeof(struct dn_rt_hash_bucket)) >= 2048))
1797 order--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001799 do {
1800 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1801 sizeof(struct dn_rt_hash_bucket);
1802 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1803 dn_rt_hash_mask--;
1804 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1805 __get_free_pages(GFP_ATOMIC, order);
1806 } while (dn_rt_hash_table == NULL && --order > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 if (!dn_rt_hash_table)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001809 panic("Failed to allocate DECnet route cache hash table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001811 printk(KERN_INFO
1812 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1813 dn_rt_hash_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1815
1816 dn_rt_hash_mask--;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001817 for(i = 0; i <= dn_rt_hash_mask; i++) {
1818 spin_lock_init(&dn_rt_hash_table[i].lock);
1819 dn_rt_hash_table[i].chain = NULL;
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001822 dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001824 proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001825
1826#ifdef CONFIG_DECNET_ROUTER
1827 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, dn_fib_dump);
1828#else
1829 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1830 dn_cache_dump);
1831#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
1834void __exit dn_route_cleanup(void)
1835{
1836 del_timer(&dn_route_timer);
1837 dn_run_flush(0);
1838
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001839 proc_net_remove(&init_net, "decnet_cache");
Eric Dumazetfc66f952010-10-08 06:37:34 +00001840 dst_entries_destroy(&dn_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842