blob: 812e6dff60675d5971b03d2d4b834ac530121e82 [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{
96 struct dn_route *chain;
97 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);
113static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
114static void dn_dst_link_failure(struct sk_buff *);
115static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu);
116static int dn_route_input(struct sk_buff *);
117static void dn_run_flush(unsigned long dummy);
118
119static struct dn_rt_hash_bucket *dn_rt_hash_table;
120static unsigned dn_rt_hash_mask;
121
122static struct timer_list dn_route_timer;
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700123static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124int decnet_dst_gc_interval = 2;
125
126static struct dst_ops dn_dst_ops = {
127 .family = PF_DECnet,
Harvey Harrison09640e62009-02-01 00:45:17 -0800128 .protocol = cpu_to_be16(ETH_P_DNA_RT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .gc_thresh = 128,
130 .gc = dn_dst_gc,
131 .check = dn_dst_check,
132 .negative_advice = dn_dst_negative_advice,
133 .link_failure = dn_dst_link_failure,
134 .update_pmtu = dn_dst_update_pmtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .entries = ATOMIC_INIT(0),
136};
137
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800138static __inline__ unsigned dn_hash(__le16 src, __le16 dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800140 __u16 tmp = (__u16 __force)(src ^ dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 tmp ^= (tmp >> 3);
142 tmp ^= (tmp >> 5);
143 tmp ^= (tmp >> 10);
144 return dn_rt_hash_mask & (unsigned)tmp;
145}
146
147static inline void dnrt_free(struct dn_route *rt)
148{
149 call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
150}
151
152static inline void dnrt_drop(struct dn_route *rt)
153{
Adrian Bunk34001122006-03-20 23:00:29 -0800154 dst_release(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
156}
157
158static void dn_dst_check_expire(unsigned long dummy)
159{
160 int i;
161 struct dn_route *rt, **rtp;
162 unsigned long now = jiffies;
163 unsigned long expire = 120 * HZ;
164
165 for(i = 0; i <= dn_rt_hash_mask; i++) {
166 rtp = &dn_rt_hash_table[i].chain;
167
168 spin_lock(&dn_rt_hash_table[i].lock);
169 while((rt=*rtp) != NULL) {
170 if (atomic_read(&rt->u.dst.__refcnt) ||
171 (now - rt->u.dst.lastuse) < expire) {
Eric Dumazet0c195c32007-02-09 16:25:52 -0800172 rtp = &rt->u.dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 continue;
174 }
Eric Dumazet0c195c32007-02-09 16:25:52 -0800175 *rtp = rt->u.dst.dn_next;
176 rt->u.dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 dnrt_free(rt);
178 }
179 spin_unlock(&dn_rt_hash_table[i].lock);
180
181 if ((jiffies - now) > 0)
182 break;
183 }
184
185 mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
186}
187
Daniel Lezcano569d3642008-01-18 03:56:57 -0800188static int dn_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct dn_route *rt, **rtp;
191 int i;
192 unsigned long now = jiffies;
193 unsigned long expire = 10 * HZ;
194
195 for(i = 0; i <= dn_rt_hash_mask; i++) {
196
197 spin_lock_bh(&dn_rt_hash_table[i].lock);
198 rtp = &dn_rt_hash_table[i].chain;
199
200 while((rt=*rtp) != NULL) {
201 if (atomic_read(&rt->u.dst.__refcnt) ||
202 (now - rt->u.dst.lastuse) < expire) {
Eric Dumazet0c195c32007-02-09 16:25:52 -0800203 rtp = &rt->u.dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 continue;
205 }
Eric Dumazet0c195c32007-02-09 16:25:52 -0800206 *rtp = rt->u.dst.dn_next;
207 rt->u.dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 dnrt_drop(rt);
209 break;
210 }
211 spin_unlock_bh(&dn_rt_hash_table[i].lock);
212 }
213
214 return 0;
215}
216
217/*
218 * The decnet standards don't impose a particular minimum mtu, what they
219 * do insist on is that the routing layer accepts a datagram of at least
220 * 230 bytes long. Here we have to subtract the routing header length from
221 * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
222 * assume the worst and use a long header size.
223 *
224 * We update both the mtu and the advertised mss (i.e. the segment size we
225 * advertise to the other end).
226 */
227static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu)
228{
229 u32 min_mtu = 230;
230 struct dn_dev *dn = dst->neighbour ?
231 (struct dn_dev *)dst->neighbour->dev->dn_ptr : NULL;
232
233 if (dn && dn->use_long == 0)
234 min_mtu -= 6;
235 else
236 min_mtu -= 21;
237
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700238 if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!(dst_metric_locked(dst, RTAX_MTU))) {
240 dst->metrics[RTAX_MTU-1] = mtu;
241 dst_set_expires(dst, dn_rt_mtu_expires);
242 }
243 if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
244 u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700245 if (dst_metric(dst, RTAX_ADVMSS) > mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 dst->metrics[RTAX_ADVMSS-1] = mss;
247 }
248 }
249}
250
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900251/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * When a route has been marked obsolete. (e.g. routing cache flush)
253 */
254static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
255{
256 return NULL;
257}
258
259static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
260{
261 dst_release(dst);
262 return NULL;
263}
264
265static void dn_dst_link_failure(struct sk_buff *skb)
266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
270{
David S. Miller8238b212006-10-12 00:49:15 -0700271 return ((fl1->nl_u.dn_u.daddr ^ fl2->nl_u.dn_u.daddr) |
272 (fl1->nl_u.dn_u.saddr ^ fl2->nl_u.dn_u.saddr) |
Thomas Graf47dcf0c2006-11-09 15:20:38 -0800273 (fl1->mark ^ fl2->mark) |
David S. Miller8238b212006-10-12 00:49:15 -0700274 (fl1->nl_u.dn_u.scope ^ fl2->nl_u.dn_u.scope) |
275 (fl1->oif ^ fl2->oif) |
276 (fl1->iif ^ fl2->iif)) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279static int dn_insert_route(struct dn_route *rt, unsigned hash, struct dn_route **rp)
280{
281 struct dn_route *rth, **rthp;
282 unsigned long now = jiffies;
283
284 rthp = &dn_rt_hash_table[hash].chain;
285
286 spin_lock_bh(&dn_rt_hash_table[hash].lock);
287 while((rth = *rthp) != NULL) {
288 if (compare_keys(&rth->fl, &rt->fl)) {
289 /* Put it first */
Eric Dumazet0c195c32007-02-09 16:25:52 -0800290 *rthp = rth->u.dst.dn_next;
291 rcu_assign_pointer(rth->u.dst.dn_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 dn_rt_hash_table[hash].chain);
293 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
294
Pavel Emelyanov03f49f32007-11-10 21:28:34 -0800295 dst_use(&rth->u.dst, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
297
298 dnrt_drop(rt);
299 *rp = rth;
300 return 0;
301 }
Eric Dumazet0c195c32007-02-09 16:25:52 -0800302 rthp = &rth->u.dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
Eric Dumazet0c195c32007-02-09 16:25:52 -0800305 rcu_assign_pointer(rt->u.dst.dn_next, dn_rt_hash_table[hash].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900307
Pavel Emelyanov03f49f32007-11-10 21:28:34 -0800308 dst_use(&rt->u.dst, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_unlock_bh(&dn_rt_hash_table[hash].lock);
310 *rp = rt;
311 return 0;
312}
313
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800314static void dn_run_flush(unsigned long dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 int i;
317 struct dn_route *rt, *next;
318
319 for(i = 0; i < dn_rt_hash_mask; i++) {
320 spin_lock_bh(&dn_rt_hash_table[i].lock);
321
322 if ((rt = xchg(&dn_rt_hash_table[i].chain, NULL)) == NULL)
323 goto nothing_to_declare;
324
325 for(; rt; rt=next) {
Eric Dumazet0c195c32007-02-09 16:25:52 -0800326 next = rt->u.dst.dn_next;
327 rt->u.dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 dst_free((struct dst_entry *)rt);
329 }
330
331nothing_to_declare:
332 spin_unlock_bh(&dn_rt_hash_table[i].lock);
333 }
334}
335
336static DEFINE_SPINLOCK(dn_rt_flush_lock);
337
338void dn_rt_cache_flush(int delay)
339{
340 unsigned long now = jiffies;
341 int user_mode = !in_interrupt();
342
343 if (delay < 0)
344 delay = dn_rt_min_delay;
345
346 spin_lock_bh(&dn_rt_flush_lock);
347
348 if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
349 long tmo = (long)(dn_rt_deadline - now);
350
351 if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
352 tmo = 0;
353
354 if (delay > tmo)
355 delay = tmo;
356 }
357
358 if (delay <= 0) {
359 spin_unlock_bh(&dn_rt_flush_lock);
360 dn_run_flush(0);
361 return;
362 }
363
364 if (dn_rt_deadline == 0)
365 dn_rt_deadline = now + dn_rt_max_delay;
366
367 dn_rt_flush_timer.expires = now + delay;
368 add_timer(&dn_rt_flush_timer);
369 spin_unlock_bh(&dn_rt_flush_lock);
370}
371
372/**
373 * dn_return_short - Return a short packet to its sender
374 * @skb: The packet to return
375 *
376 */
377static int dn_return_short(struct sk_buff *skb)
378{
379 struct dn_skb_cb *cb;
380 unsigned char *ptr;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800381 __le16 *src;
382 __le16 *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /* Add back headers */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700385 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
388 return NET_RX_DROP;
389
390 cb = DN_SKB_CB(skb);
391 /* Skip packet length and point to flags */
392 ptr = skb->data + 2;
393 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
394
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800395 dst = (__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800397 src = (__le16 *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 ptr += 2;
399 *ptr = 0; /* Zero hop count */
400
Ilpo Järvinena0bffff2009-03-21 13:36:17 -0700401 swap(*src, *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 skb->pkt_type = PACKET_OUTGOING;
404 dn_rt_finish_output(skb, NULL, NULL);
405 return NET_RX_SUCCESS;
406}
407
408/**
409 * dn_return_long - Return a long packet to its sender
410 * @skb: The long format packet to return
411 *
412 */
413static int dn_return_long(struct sk_buff *skb)
414{
415 struct dn_skb_cb *cb;
416 unsigned char *ptr;
417 unsigned char *src_addr, *dst_addr;
418 unsigned char tmp[ETH_ALEN];
419
420 /* Add back all headers */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700421 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
424 return NET_RX_DROP;
425
426 cb = DN_SKB_CB(skb);
427 /* Ignore packet length and point to flags */
428 ptr = skb->data + 2;
429
430 /* Skip padding */
431 if (*ptr & DN_RT_F_PF) {
432 char padlen = (*ptr & ~DN_RT_F_PF);
433 ptr += padlen;
434 }
435
436 *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
437 ptr += 2;
438 dst_addr = ptr;
439 ptr += 8;
440 src_addr = ptr;
441 ptr += 6;
442 *ptr = 0; /* Zero hop count */
443
444 /* Swap source and destination */
445 memcpy(tmp, src_addr, ETH_ALEN);
446 memcpy(src_addr, dst_addr, ETH_ALEN);
447 memcpy(dst_addr, tmp, ETH_ALEN);
448
449 skb->pkt_type = PACKET_OUTGOING;
450 dn_rt_finish_output(skb, dst_addr, src_addr);
451 return NET_RX_SUCCESS;
452}
453
454/**
455 * dn_route_rx_packet - Try and find a route for an incoming packet
456 * @skb: The packet to find a route for
457 *
458 * Returns: result of input function if route is found, error code otherwise
459 */
460static int dn_route_rx_packet(struct sk_buff *skb)
461{
462 struct dn_skb_cb *cb = DN_SKB_CB(skb);
463 int err;
464
465 if ((err = dn_route_input(skb)) == 0)
466 return dst_input(skb);
467
468 if (decnet_debug_level & 4) {
469 char *devname = skb->dev ? skb->dev->name : "???";
470 struct dn_skb_cb *cb = DN_SKB_CB(skb);
471 printk(KERN_DEBUG
472 "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 -0800473 (int)cb->rt_flags, devname, skb->len,
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800474 le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 err, skb->pkt_type);
476 }
477
478 if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
479 switch(cb->rt_flags & DN_RT_PKT_MSK) {
480 case DN_RT_PKT_SHORT:
481 return dn_return_short(skb);
482 case DN_RT_PKT_LONG:
483 return dn_return_long(skb);
484 }
485 }
486
487 kfree_skb(skb);
488 return NET_RX_DROP;
489}
490
491static int dn_route_rx_long(struct sk_buff *skb)
492{
493 struct dn_skb_cb *cb = DN_SKB_CB(skb);
494 unsigned char *ptr = skb->data;
495
496 if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
497 goto drop_it;
498
499 skb_pull(skb, 20);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300500 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900502 /* Destination info */
503 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800504 cb->dst = dn_eth2dn(ptr);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900505 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
506 goto drop_it;
507 ptr += 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900510 /* Source info */
511 ptr += 2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800512 cb->src = dn_eth2dn(ptr);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900513 if (memcmp(ptr, dn_hiord_addr, 4) != 0)
514 goto drop_it;
515 ptr += 6;
516 /* Other junk */
517 ptr++;
518 cb->hops = *ptr++; /* Visit Count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100520 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
521 dn_route_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523drop_it:
524 kfree_skb(skb);
525 return NET_RX_DROP;
526}
527
528
529
530static int dn_route_rx_short(struct sk_buff *skb)
531{
532 struct dn_skb_cb *cb = DN_SKB_CB(skb);
533 unsigned char *ptr = skb->data;
534
535 if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
536 goto drop_it;
537
538 skb_pull(skb, 5);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300539 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800541 cb->dst = *(__le16 *)ptr;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900542 ptr += 2;
543 cb->src = *(__le16 *)ptr;
544 ptr += 2;
545 cb->hops = *ptr & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100547 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
548 dn_route_rx_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550drop_it:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900551 kfree_skb(skb);
552 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555static int dn_route_discard(struct sk_buff *skb)
556{
557 /*
558 * I know we drop the packet here, but thats considered success in
559 * this case
560 */
561 kfree_skb(skb);
562 return NET_RX_SUCCESS;
563}
564
565static int dn_route_ptp_hello(struct sk_buff *skb)
566{
567 dn_dev_hello(skb);
568 dn_neigh_pointopoint_hello(skb);
569 return NET_RX_SUCCESS;
570}
571
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700572int 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 -0700573{
574 struct dn_skb_cb *cb;
575 unsigned char flags = 0;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800576 __u16 len = le16_to_cpu(*(__le16 *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct dn_dev *dn = (struct dn_dev *)dev->dn_ptr;
578 unsigned char padlen = 0;
579
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700580 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700581 goto dump_it;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (dn == NULL)
584 goto dump_it;
585
586 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
587 goto out;
588
589 if (!pskb_may_pull(skb, 3))
590 goto dump_it;
591
592 skb_pull(skb, 2);
593
594 if (len > skb->len)
595 goto dump_it;
596
597 skb_trim(skb, len);
598
599 flags = *skb->data;
600
601 cb = DN_SKB_CB(skb);
602 cb->stamp = jiffies;
603 cb->iif = dev->ifindex;
604
605 /*
606 * If we have padding, remove it.
607 */
608 if (flags & DN_RT_F_PF) {
609 padlen = flags & ~DN_RT_F_PF;
610 if (!pskb_may_pull(skb, padlen + 1))
611 goto dump_it;
612 skb_pull(skb, padlen);
613 flags = *skb->data;
614 }
615
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700616 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /*
619 * Weed out future version DECnet
620 */
621 if (flags & DN_RT_F_VER)
622 goto dump_it;
623
624 cb->rt_flags = flags;
625
626 if (decnet_debug_level & 1)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900627 printk(KERN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900629 (int)flags, (dev) ? dev->name : "???", len, skb->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 padlen);
631
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900632 if (flags & DN_RT_PKT_CNTL) {
Herbert Xu364c6ba2006-06-09 16:10:40 -0700633 if (unlikely(skb_linearize(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto dump_it;
635
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900636 switch(flags & DN_RT_CNTL_MSK) {
637 case DN_RT_PKT_INIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 dn_dev_init_pkt(skb);
639 break;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900640 case DN_RT_PKT_VERI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 dn_dev_veri_pkt(skb);
642 break;
643 }
644
645 if (dn->parms.state != DN_DEV_S_RU)
646 goto dump_it;
647
648 switch(flags & DN_RT_CNTL_MSK) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900649 case DN_RT_PKT_HELO:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100650 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
651 skb, skb->dev, NULL,
652 dn_route_ptp_hello);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900654 case DN_RT_PKT_L1RT:
655 case DN_RT_PKT_L2RT:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100656 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
657 skb, skb->dev, NULL,
658 dn_route_discard);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900659 case DN_RT_PKT_ERTH:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100660 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
661 skb, skb->dev, NULL,
662 dn_neigh_router_hello);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900664 case DN_RT_PKT_EEDH:
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100665 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
666 skb, skb->dev, NULL,
667 dn_neigh_endnode_hello);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900668 }
669 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (dn->parms.state != DN_DEV_S_RU)
671 goto dump_it;
672
673 skb_pull(skb, 1); /* Pull flags */
674
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900675 switch(flags & DN_RT_PKT_MSK) {
676 case DN_RT_PKT_LONG:
677 return dn_route_rx_long(skb);
678 case DN_RT_PKT_SHORT:
679 return dn_route_rx_short(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683dump_it:
684 kfree_skb(skb);
685out:
686 return NET_RX_DROP;
687}
688
689static int dn_output(struct sk_buff *skb)
690{
Eric Dumazetadf30902009-06-02 05:19:30 +0000691 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct dn_route *rt = (struct dn_route *)dst;
693 struct net_device *dev = dst->dev;
694 struct dn_skb_cb *cb = DN_SKB_CB(skb);
695 struct neighbour *neigh;
696
697 int err = -EINVAL;
698
699 if ((neigh = dst->neighbour) == NULL)
700 goto error;
701
702 skb->dev = dev;
703
704 cb->src = rt->rt_saddr;
705 cb->dst = rt->rt_daddr;
706
707 /*
708 * Always set the Intra-Ethernet bit on all outgoing packets
709 * originated on this node. Only valid flag from upper layers
710 * is return-to-sender-requested. Set hop count to 0 too.
711 */
712 cb->rt_flags &= ~DN_RT_F_RQR;
713 cb->rt_flags |= DN_RT_F_IE;
714 cb->hops = 0;
715
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100716 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
717 neigh->output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719error:
720 if (net_ratelimit())
721 printk(KERN_DEBUG "dn_output: This should not happen\n");
722
723 kfree_skb(skb);
724
725 return err;
726}
727
728static int dn_forward(struct sk_buff *skb)
729{
730 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000731 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct dn_dev *dn_db = dst->dev->dn_ptr;
733 struct dn_route *rt;
734 struct neighbour *neigh = dst->neighbour;
735 int header_len;
736#ifdef CONFIG_NETFILTER
737 struct net_device *dev = skb->dev;
738#endif
739
740 if (skb->pkt_type != PACKET_HOST)
741 goto drop;
742
743 /* Ensure that we have enough space for headers */
Eric Dumazetadf30902009-06-02 05:19:30 +0000744 rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 header_len = dn_db->use_long ? 21 : 6;
746 if (skb_cow(skb, LL_RESERVED_SPACE(rt->u.dst.dev)+header_len))
747 goto drop;
748
749 /*
750 * Hop count exceeded.
751 */
752 if (++cb->hops > 30)
753 goto drop;
754
755 skb->dev = rt->u.dst.dev;
756
757 /*
758 * If packet goes out same interface it came in on, then set
759 * the Intra-Ethernet bit. This has no effect for short
760 * packets, so we don't need to test for them here.
761 */
762 cb->rt_flags &= ~DN_RT_F_IE;
763 if (rt->rt_flags & RTCF_DOREDIRECT)
764 cb->rt_flags |= DN_RT_F_IE;
765
Jan Engelhardt5d877d82010-03-23 04:09:14 +0100766 return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
767 neigh->output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769drop:
770 kfree_skb(skb);
771 return NET_RX_DROP;
772}
773
774/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * Used to catch bugs. This should never normally get
776 * called.
777 */
778static int dn_rt_bug(struct sk_buff *skb)
779{
780 if (net_ratelimit()) {
781 struct dn_skb_cb *cb = DN_SKB_CB(skb);
782
783 printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800784 le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
787 kfree_skb(skb);
788
Florian Westphal0e8635a2009-06-20 00:53:25 +0000789 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
793{
794 struct dn_fib_info *fi = res->fi;
795 struct net_device *dev = rt->u.dst.dev;
796 struct neighbour *n;
797 unsigned mss;
798
799 if (fi) {
800 if (DN_FIB_RES_GW(*res) &&
801 DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
802 rt->rt_gateway = DN_FIB_RES_GW(*res);
803 memcpy(rt->u.dst.metrics, fi->fib_metrics,
804 sizeof(rt->u.dst.metrics));
805 }
806 rt->rt_type = res->type;
807
808 if (dev != NULL && rt->u.dst.neighbour == NULL) {
809 n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
810 if (IS_ERR(n))
811 return PTR_ERR(n);
812 rt->u.dst.neighbour = n;
813 }
814
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700815 if (dst_metric(&rt->u.dst, RTAX_MTU) == 0 ||
816 dst_metric(&rt->u.dst, RTAX_MTU) > rt->u.dst.dev->mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 rt->u.dst.metrics[RTAX_MTU-1] = rt->u.dst.dev->mtu;
818 mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->u.dst));
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700819 if (dst_metric(&rt->u.dst, RTAX_ADVMSS) == 0 ||
820 dst_metric(&rt->u.dst, RTAX_ADVMSS) > mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 rt->u.dst.metrics[RTAX_ADVMSS-1] = mss;
822 return 0;
823}
824
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800825static inline int dn_match_addr(__le16 addr1, __le16 addr2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800827 __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int match = 16;
829 while(tmp) {
830 tmp >>= 1;
831 match--;
832 }
833 return match;
834}
835
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800836static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800838 __le16 saddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct dn_dev *dn_db = dev->dn_ptr;
840 struct dn_ifaddr *ifa;
841 int best_match = 0;
842 int ret;
843
844 read_lock(&dev_base_lock);
845 for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
846 if (ifa->ifa_scope > scope)
847 continue;
848 if (!daddr) {
849 saddr = ifa->ifa_local;
850 break;
851 }
852 ret = dn_match_addr(daddr, ifa->ifa_local);
853 if (ret > best_match)
854 saddr = ifa->ifa_local;
855 if (best_match == 0)
856 saddr = ifa->ifa_local;
857 }
858 read_unlock(&dev_base_lock);
859
860 return saddr;
861}
862
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800863static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
866}
867
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800868static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800870 __le16 mask = dnet_make_mask(res->prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
872}
873
874static int dn_route_output_slow(struct dst_entry **pprt, const struct flowi *oldflp, int try_hard)
875{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900876 struct flowi fl = { .nl_u = { .dn_u =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 { .daddr = oldflp->fld_dst,
878 .saddr = oldflp->fld_src,
879 .scope = RT_SCOPE_UNIVERSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 } },
Thomas Graf47dcf0c2006-11-09 15:20:38 -0800881 .mark = oldflp->mark,
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700882 .iif = init_net.loopback_dev->ifindex,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 .oif = oldflp->oif };
884 struct dn_route *rt = NULL;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700885 struct net_device *dev_out = NULL, *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 struct neighbour *neigh = NULL;
887 unsigned hash;
888 unsigned flags = 0;
889 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
890 int err;
891 int free_res = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800892 __le16 gateway = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (decnet_debug_level & 16)
895 printk(KERN_DEBUG
896 "dn_route_output_slow: dst=%04x src=%04x mark=%d"
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800897 " iif=%d oif=%d\n", le16_to_cpu(oldflp->fld_dst),
898 le16_to_cpu(oldflp->fld_src),
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700899 oldflp->mark, init_net.loopback_dev->ifindex, oldflp->oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* If we have an output interface, verify its a DECnet device */
902 if (oldflp->oif) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700903 dev_out = dev_get_by_index(&init_net, oldflp->oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 err = -ENODEV;
905 if (dev_out && dev_out->dn_ptr == NULL) {
906 dev_put(dev_out);
907 dev_out = NULL;
908 }
909 if (dev_out == NULL)
910 goto out;
911 }
912
913 /* If we have a source address, verify that its a local address */
914 if (oldflp->fld_src) {
915 err = -EADDRNOTAVAIL;
916
917 if (dev_out) {
918 if (dn_dev_islocal(dev_out, oldflp->fld_src))
919 goto source_ok;
920 dev_put(dev_out);
921 goto out;
922 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800923 rcu_read_lock();
924 for_each_netdev_rcu(&init_net, dev) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700925 if (!dev->dn_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700927 if (!dn_dev_islocal(dev, oldflp->fld_src))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700928 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700929 if ((dev->flags & IFF_LOOPBACK) &&
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700930 oldflp->fld_dst &&
Pavel Emelianov7562f872007-05-03 15:13:45 -0700931 !dn_dev_islocal(dev, oldflp->fld_dst))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700932 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700933
934 dev_out = dev;
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800937 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (dev_out == NULL)
939 goto out;
940 dev_hold(dev_out);
941source_ok:
942 ;
943 }
944
945 /* No destination? Assume its local */
946 if (!fl.fld_dst) {
947 fl.fld_dst = fl.fld_src;
948
949 err = -EADDRNOTAVAIL;
950 if (dev_out)
951 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700952 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 dev_hold(dev_out);
954 if (!fl.fld_dst) {
955 fl.fld_dst =
956 fl.fld_src = dnet_select_source(dev_out, 0,
957 RT_SCOPE_HOST);
958 if (!fl.fld_dst)
959 goto out;
960 }
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700961 fl.oif = init_net.loopback_dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 res.type = RTN_LOCAL;
963 goto make_route;
964 }
965
966 if (decnet_debug_level & 16)
967 printk(KERN_DEBUG
968 "dn_route_output_slow: initial checks complete."
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800969 " dst=%o4x src=%04x oif=%d try_hard=%d\n",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800970 le16_to_cpu(fl.fld_dst), le16_to_cpu(fl.fld_src),
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800971 fl.oif, try_hard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /*
974 * N.B. If the kernel is compiled without router support then
975 * dn_fib_lookup() will evaluate to non-zero so this if () block
976 * will always be executed.
977 */
978 err = -ESRCH;
979 if (try_hard || (err = dn_fib_lookup(&fl, &res)) != 0) {
980 struct dn_dev *dn_db;
981 if (err != -ESRCH)
982 goto out;
983 /*
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900984 * Here the fallback is basically the standard algorithm for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 * routing in endnodes which is described in the DECnet routing
986 * docs
987 *
988 * If we are not trying hard, look in neighbour cache.
989 * The result is tested to ensure that if a specific output
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900990 * device/source address was requested, then we honour that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * here
992 */
993 if (!try_hard) {
Eric W. Biederman426b5302008-01-24 00:13:18 -0800994 neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fl.fld_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (neigh) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900996 if ((oldflp->oif &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 (neigh->dev->ifindex != oldflp->oif)) ||
998 (oldflp->fld_src &&
999 (!dn_dev_islocal(neigh->dev,
1000 oldflp->fld_src)))) {
1001 neigh_release(neigh);
1002 neigh = NULL;
1003 } else {
1004 if (dev_out)
1005 dev_put(dev_out);
1006 if (dn_dev_islocal(neigh->dev, fl.fld_dst)) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001007 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 res.type = RTN_LOCAL;
1009 } else {
1010 dev_out = neigh->dev;
1011 }
1012 dev_hold(dev_out);
1013 goto select_source;
1014 }
1015 }
1016 }
1017
1018 /* Not there? Perhaps its a local address */
1019 if (dev_out == NULL)
1020 dev_out = dn_dev_get_default();
1021 err = -ENODEV;
1022 if (dev_out == NULL)
1023 goto out;
1024 dn_db = dev_out->dn_ptr;
1025 /* Possible improvement - check all devices for local addr */
1026 if (dn_dev_islocal(dev_out, fl.fld_dst)) {
1027 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001028 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 dev_hold(dev_out);
1030 res.type = RTN_LOCAL;
1031 goto select_source;
1032 }
1033 /* Not local either.... try sending it to the default router */
1034 neigh = neigh_clone(dn_db->router);
1035 BUG_ON(neigh && neigh->dev != dev_out);
1036
1037 /* Ok then, we assume its directly connected and move on */
1038select_source:
1039 if (neigh)
1040 gateway = ((struct dn_neigh *)neigh)->addr;
1041 if (gateway == 0)
1042 gateway = fl.fld_dst;
1043 if (fl.fld_src == 0) {
1044 fl.fld_src = dnet_select_source(dev_out, gateway,
1045 res.type == RTN_LOCAL ?
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001046 RT_SCOPE_HOST :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 RT_SCOPE_LINK);
1048 if (fl.fld_src == 0 && res.type != RTN_LOCAL)
1049 goto e_addr;
1050 }
1051 fl.oif = dev_out->ifindex;
1052 goto make_route;
1053 }
1054 free_res = 1;
1055
1056 if (res.type == RTN_NAT)
1057 goto e_inval;
1058
1059 if (res.type == RTN_LOCAL) {
1060 if (!fl.fld_src)
1061 fl.fld_src = fl.fld_dst;
1062 if (dev_out)
1063 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001064 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 dev_hold(dev_out);
1066 fl.oif = dev_out->ifindex;
1067 if (res.fi)
1068 dn_fib_info_put(res.fi);
1069 res.fi = NULL;
1070 goto make_route;
1071 }
1072
1073 if (res.fi->fib_nhs > 1 && fl.oif == 0)
1074 dn_fib_select_multipath(&fl, &res);
1075
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001076 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * We could add some logic to deal with default routes here and
1078 * get rid of some of the special casing above.
1079 */
1080
1081 if (!fl.fld_src)
1082 fl.fld_src = DN_FIB_RES_PREFSRC(res);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (dev_out)
1085 dev_put(dev_out);
1086 dev_out = DN_FIB_RES_DEV(res);
1087 dev_hold(dev_out);
1088 fl.oif = dev_out->ifindex;
1089 gateway = DN_FIB_RES_GW(res);
1090
1091make_route:
1092 if (dev_out->flags & IFF_LOOPBACK)
1093 flags |= RTCF_LOCAL;
1094
1095 rt = dst_alloc(&dn_dst_ops);
1096 if (rt == NULL)
1097 goto e_nobufs;
1098
1099 atomic_set(&rt->u.dst.__refcnt, 1);
1100 rt->u.dst.flags = DST_HOST;
1101
1102 rt->fl.fld_src = oldflp->fld_src;
1103 rt->fl.fld_dst = oldflp->fld_dst;
1104 rt->fl.oif = oldflp->oif;
1105 rt->fl.iif = 0;
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001106 rt->fl.mark = oldflp->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 rt->rt_saddr = fl.fld_src;
1109 rt->rt_daddr = fl.fld_dst;
1110 rt->rt_gateway = gateway ? gateway : fl.fld_dst;
1111 rt->rt_local_src = fl.fld_src;
1112
1113 rt->rt_dst_map = fl.fld_dst;
1114 rt->rt_src_map = fl.fld_src;
1115
1116 rt->u.dst.dev = dev_out;
1117 dev_hold(dev_out);
1118 rt->u.dst.neighbour = neigh;
1119 neigh = NULL;
1120
1121 rt->u.dst.lastuse = jiffies;
1122 rt->u.dst.output = dn_output;
1123 rt->u.dst.input = dn_rt_bug;
1124 rt->rt_flags = flags;
1125 if (flags & RTCF_LOCAL)
1126 rt->u.dst.input = dn_nsp_rx;
1127
1128 err = dn_rt_set_next_hop(rt, &res);
1129 if (err)
1130 goto e_neighbour;
1131
1132 hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst);
1133 dn_insert_route(rt, hash, (struct dn_route **)pprt);
1134
1135done:
1136 if (neigh)
1137 neigh_release(neigh);
1138 if (free_res)
1139 dn_fib_res_put(&res);
1140 if (dev_out)
1141 dev_put(dev_out);
1142out:
1143 return err;
1144
1145e_addr:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001146 err = -EADDRNOTAVAIL;
1147 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148e_inval:
1149 err = -EINVAL;
1150 goto done;
1151e_nobufs:
1152 err = -ENOBUFS;
1153 goto done;
1154e_neighbour:
1155 dst_free(&rt->u.dst);
1156 goto e_nobufs;
1157}
1158
1159
1160/*
1161 * N.B. The flags may be moved into the flowi at some future stage.
1162 */
1163static int __dn_route_output_key(struct dst_entry **pprt, const struct flowi *flp, int flags)
1164{
1165 unsigned hash = dn_hash(flp->fld_src, flp->fld_dst);
1166 struct dn_route *rt = NULL;
1167
1168 if (!(flags & MSG_TRYHARD)) {
1169 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001170 for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
1171 rt = rcu_dereference_bh(rt->u.dst.dn_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if ((flp->fld_dst == rt->fl.fld_dst) &&
1173 (flp->fld_src == rt->fl.fld_src) &&
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001174 (flp->mark == rt->fl.mark) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 (rt->fl.iif == 0) &&
1176 (rt->fl.oif == flp->oif)) {
Pavel Emelyanov03f49f32007-11-10 21:28:34 -08001177 dst_use(&rt->u.dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 rcu_read_unlock_bh();
1179 *pprt = &rt->u.dst;
1180 return 0;
1181 }
1182 }
1183 rcu_read_unlock_bh();
1184 }
1185
1186 return dn_route_output_slow(pprt, flp, flags);
1187}
1188
1189static int dn_route_output_key(struct dst_entry **pprt, struct flowi *flp, int flags)
1190{
1191 int err;
1192
1193 err = __dn_route_output_key(pprt, flp, flags);
1194 if (err == 0 && flp->proto) {
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001195 err = xfrm_lookup(&init_net, pprt, flp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197 return err;
1198}
1199
1200int dn_route_output_sock(struct dst_entry **pprt, struct flowi *fl, struct sock *sk, int flags)
1201{
1202 int err;
1203
1204 err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
1205 if (err == 0 && fl->proto) {
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001206 err = xfrm_lookup(&init_net, pprt, fl, sk,
1207 (flags & MSG_DONTWAIT) ? 0 : XFRM_LOOKUP_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 return err;
1210}
1211
1212static int dn_route_input_slow(struct sk_buff *skb)
1213{
1214 struct dn_route *rt = NULL;
1215 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1216 struct net_device *in_dev = skb->dev;
1217 struct net_device *out_dev = NULL;
1218 struct dn_dev *dn_db;
1219 struct neighbour *neigh = NULL;
1220 unsigned hash;
1221 int flags = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001222 __le16 gateway = 0;
1223 __le16 local_src = 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001224 struct flowi fl = { .nl_u = { .dn_u =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 { .daddr = cb->dst,
1226 .saddr = cb->src,
1227 .scope = RT_SCOPE_UNIVERSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 } },
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001229 .mark = skb->mark,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 .iif = skb->dev->ifindex };
1231 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1232 int err = -EINVAL;
1233 int free_res = 0;
1234
1235 dev_hold(in_dev);
1236
1237 if ((dn_db = in_dev->dn_ptr) == NULL)
1238 goto out;
1239
1240 /* Zero source addresses are not allowed */
1241 if (fl.fld_src == 0)
1242 goto out;
1243
1244 /*
1245 * In this case we've just received a packet from a source
1246 * outside ourselves pretending to come from us. We don't
1247 * allow it any further to prevent routing loops, spoofing and
1248 * other nasties. Loopback packets already have the dst attached
1249 * so this only affects packets which have originated elsewhere.
1250 */
1251 err = -ENOTUNIQ;
1252 if (dn_dev_islocal(in_dev, cb->src))
1253 goto out;
1254
1255 err = dn_fib_lookup(&fl, &res);
1256 if (err) {
1257 if (err != -ESRCH)
1258 goto out;
1259 /*
1260 * Is the destination us ?
1261 */
1262 if (!dn_dev_islocal(in_dev, cb->dst))
1263 goto e_inval;
1264
1265 res.type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 } else {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001267 __le16 src_map = fl.fld_src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 free_res = 1;
1269
1270 out_dev = DN_FIB_RES_DEV(res);
1271 if (out_dev == NULL) {
1272 if (net_ratelimit())
1273 printk(KERN_CRIT "Bug in dn_route_input_slow() "
1274 "No output device\n");
1275 goto e_inval;
1276 }
1277 dev_hold(out_dev);
1278
1279 if (res.r)
Steven Whitehousea8731cb2006-08-09 15:56:46 -07001280 src_map = fl.fld_src; /* no NAT support for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 gateway = DN_FIB_RES_GW(res);
1283 if (res.type == RTN_NAT) {
1284 fl.fld_dst = dn_fib_rules_map_destination(fl.fld_dst, &res);
1285 dn_fib_res_put(&res);
1286 free_res = 0;
1287 if (dn_fib_lookup(&fl, &res))
1288 goto e_inval;
1289 free_res = 1;
1290 if (res.type != RTN_UNICAST)
1291 goto e_inval;
1292 flags |= RTCF_DNAT;
1293 gateway = fl.fld_dst;
1294 }
1295 fl.fld_src = src_map;
1296 }
1297
1298 switch(res.type) {
1299 case RTN_UNICAST:
1300 /*
1301 * Forwarding check here, we only check for forwarding
1302 * being turned off, if you want to only forward intra
1303 * area, its up to you to set the routing tables up
1304 * correctly.
1305 */
1306 if (dn_db->parms.forwarding == 0)
1307 goto e_inval;
1308
1309 if (res.fi->fib_nhs > 1 && fl.oif == 0)
1310 dn_fib_select_multipath(&fl, &res);
1311
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001312 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1314 * flag as a hint to set the intra-ethernet bit when
1315 * forwarding. If we've got NAT in operation, we don't do
1316 * this optimisation.
1317 */
1318 if (out_dev == in_dev && !(flags & RTCF_NAT))
1319 flags |= RTCF_DOREDIRECT;
1320
1321 local_src = DN_FIB_RES_PREFSRC(res);
1322
1323 case RTN_BLACKHOLE:
1324 case RTN_UNREACHABLE:
1325 break;
1326 case RTN_LOCAL:
1327 flags |= RTCF_LOCAL;
1328 fl.fld_src = cb->dst;
1329 fl.fld_dst = cb->src;
1330
1331 /* Routing tables gave us a gateway */
1332 if (gateway)
1333 goto make_route;
1334
1335 /* Packet was intra-ethernet, so we know its on-link */
Steven Whitehouse3a31b9d2006-10-18 20:45:22 -07001336 if (cb->rt_flags & DN_RT_F_IE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 gateway = cb->src;
1338 flags |= RTCF_DIRECTSRC;
1339 goto make_route;
1340 }
1341
1342 /* Use the default router if there is one */
1343 neigh = neigh_clone(dn_db->router);
1344 if (neigh) {
1345 gateway = ((struct dn_neigh *)neigh)->addr;
1346 goto make_route;
1347 }
1348
1349 /* Close eyes and pray */
1350 gateway = cb->src;
1351 flags |= RTCF_DIRECTSRC;
1352 goto make_route;
1353 default:
1354 goto e_inval;
1355 }
1356
1357make_route:
1358 rt = dst_alloc(&dn_dst_ops);
1359 if (rt == NULL)
1360 goto e_nobufs;
1361
1362 rt->rt_saddr = fl.fld_src;
1363 rt->rt_daddr = fl.fld_dst;
1364 rt->rt_gateway = fl.fld_dst;
1365 if (gateway)
1366 rt->rt_gateway = gateway;
1367 rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
1368
1369 rt->rt_dst_map = fl.fld_dst;
1370 rt->rt_src_map = fl.fld_src;
1371
1372 rt->fl.fld_src = cb->src;
1373 rt->fl.fld_dst = cb->dst;
1374 rt->fl.oif = 0;
1375 rt->fl.iif = in_dev->ifindex;
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001376 rt->fl.mark = fl.mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 rt->u.dst.flags = DST_HOST;
1379 rt->u.dst.neighbour = neigh;
1380 rt->u.dst.dev = out_dev;
1381 rt->u.dst.lastuse = jiffies;
1382 rt->u.dst.output = dn_rt_bug;
1383 switch(res.type) {
1384 case RTN_UNICAST:
1385 rt->u.dst.input = dn_forward;
1386 break;
1387 case RTN_LOCAL:
1388 rt->u.dst.output = dn_output;
1389 rt->u.dst.input = dn_nsp_rx;
1390 rt->u.dst.dev = in_dev;
1391 flags |= RTCF_LOCAL;
1392 break;
1393 default:
1394 case RTN_UNREACHABLE:
1395 case RTN_BLACKHOLE:
Herbert Xu352e5122007-11-13 21:34:06 -08001396 rt->u.dst.input = dst_discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 rt->rt_flags = flags;
1399 if (rt->u.dst.dev)
1400 dev_hold(rt->u.dst.dev);
1401
1402 err = dn_rt_set_next_hop(rt, &res);
1403 if (err)
1404 goto e_neighbour;
1405
1406 hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst);
Eric Dumazetadf30902009-06-02 05:19:30 +00001407 dn_insert_route(rt, hash, &rt);
1408 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410done:
1411 if (neigh)
1412 neigh_release(neigh);
1413 if (free_res)
1414 dn_fib_res_put(&res);
1415 dev_put(in_dev);
1416 if (out_dev)
1417 dev_put(out_dev);
1418out:
1419 return err;
1420
1421e_inval:
1422 err = -EINVAL;
1423 goto done;
1424
1425e_nobufs:
1426 err = -ENOBUFS;
1427 goto done;
1428
1429e_neighbour:
1430 dst_free(&rt->u.dst);
1431 goto done;
1432}
1433
Roel Kluin5eaa65b2008-12-10 15:18:31 -08001434static int dn_route_input(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 struct dn_route *rt;
1437 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1438 unsigned hash = dn_hash(cb->src, cb->dst);
1439
Eric Dumazetadf30902009-06-02 05:19:30 +00001440 if (skb_dst(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return 0;
1442
1443 rcu_read_lock();
1444 for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
Eric Dumazet0c195c32007-02-09 16:25:52 -08001445 rt = rcu_dereference(rt->u.dst.dn_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if ((rt->fl.fld_src == cb->src) &&
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001447 (rt->fl.fld_dst == cb->dst) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 (rt->fl.oif == 0) &&
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001449 (rt->fl.mark == skb->mark) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 (rt->fl.iif == cb->iif)) {
Pavel Emelyanov03f49f32007-11-10 21:28:34 -08001451 dst_use(&rt->u.dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 rcu_read_unlock();
Eric Dumazetadf30902009-06-02 05:19:30 +00001453 skb_dst_set(skb, (struct dst_entry *)rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 0;
1455 }
1456 }
1457 rcu_read_unlock();
1458
1459 return dn_route_input_slow(skb);
1460}
1461
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001462static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1463 int event, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Eric Dumazetadf30902009-06-02 05:19:30 +00001465 struct dn_route *rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct rtmsg *r;
1467 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001468 unsigned char *b = skb_tail_pointer(skb);
Thomas Grafe3703b32006-11-27 09:27:07 -08001469 long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001471 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 r = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 r->rtm_family = AF_DECnet;
1474 r->rtm_dst_len = 16;
1475 r->rtm_src_len = 0;
1476 r->rtm_tos = 0;
1477 r->rtm_table = RT_TABLE_MAIN;
Patrick McHardy9e762a42006-08-10 23:09:48 -07001478 RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 r->rtm_type = rt->rt_type;
1480 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1481 r->rtm_scope = RT_SCOPE_UNIVERSE;
1482 r->rtm_protocol = RTPROT_UNSPEC;
1483 if (rt->rt_flags & RTCF_NOTIFY)
1484 r->rtm_flags |= RTM_F_NOTIFY;
1485 RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
1486 if (rt->fl.fld_src) {
1487 r->rtm_src_len = 16;
1488 RTA_PUT(skb, RTA_SRC, 2, &rt->fl.fld_src);
1489 }
1490 if (rt->u.dst.dev)
1491 RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->u.dst.dev->ifindex);
1492 /*
1493 * Note to self - change this if input routes reverse direction when
1494 * they deal only with inputs and not with replies like they do
1495 * currently.
1496 */
1497 RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
1498 if (rt->rt_daddr != rt->rt_gateway)
1499 RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
1500 if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
1501 goto rtattr_failure;
Thomas Grafe3703b32006-11-27 09:27:07 -08001502 expires = rt->u.dst.expires ? rt->u.dst.expires - jiffies : 0;
1503 if (rtnl_put_cacheinfo(skb, &rt->u.dst, 0, 0, 0, expires,
1504 rt->u.dst.error) < 0)
1505 goto rtattr_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (rt->fl.iif)
1507 RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fl.iif);
1508
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001509 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return skb->len;
1511
1512nlmsg_failure:
1513rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001514 nlmsg_trim(skb, b);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001515 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
1518/*
1519 * This is called by both endnodes and routers now.
1520 */
Thomas Graffa34ddd2007-03-22 11:57:46 -07001521static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001523 struct net *net = sock_net(in_skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct rtattr **rta = arg;
1525 struct rtmsg *rtm = NLMSG_DATA(nlh);
1526 struct dn_route *rt = NULL;
1527 struct dn_skb_cb *cb;
1528 int err;
1529 struct sk_buff *skb;
1530 struct flowi fl;
1531
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001532 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001533 return -EINVAL;
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 memset(&fl, 0, sizeof(fl));
1536 fl.proto = DNPROTO_NSP;
1537
1538 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1539 if (skb == NULL)
1540 return -ENOBUFS;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001541 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 cb = DN_SKB_CB(skb);
1543
1544 if (rta[RTA_SRC-1])
1545 memcpy(&fl.fld_src, RTA_DATA(rta[RTA_SRC-1]), 2);
1546 if (rta[RTA_DST-1])
1547 memcpy(&fl.fld_dst, RTA_DATA(rta[RTA_DST-1]), 2);
1548 if (rta[RTA_IIF-1])
1549 memcpy(&fl.iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
1550
1551 if (fl.iif) {
1552 struct net_device *dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001553 if ((dev = dev_get_by_index(&init_net, fl.iif)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 kfree_skb(skb);
1555 return -ENODEV;
1556 }
1557 if (!dev->dn_ptr) {
1558 dev_put(dev);
1559 kfree_skb(skb);
1560 return -ENODEV;
1561 }
YOSHIFUJI Hideakib98999d2007-12-12 03:51:49 +09001562 skb->protocol = htons(ETH_P_DNA_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 skb->dev = dev;
1564 cb->src = fl.fld_src;
1565 cb->dst = fl.fld_dst;
1566 local_bh_disable();
1567 err = dn_route_input(skb);
1568 local_bh_enable();
1569 memset(cb, 0, sizeof(struct dn_skb_cb));
Eric Dumazetadf30902009-06-02 05:19:30 +00001570 rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (!err && -rt->u.dst.error)
1572 err = rt->u.dst.error;
1573 } else {
1574 int oif = 0;
1575 if (rta[RTA_OIF - 1])
1576 memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
1577 fl.oif = oif;
1578 err = dn_route_output_key((struct dst_entry **)&rt, &fl, 0);
1579 }
1580
1581 if (skb->dev)
1582 dev_put(skb->dev);
1583 skb->dev = NULL;
1584 if (err)
1585 goto out_free;
Eric Dumazetadf30902009-06-02 05:19:30 +00001586 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (rtm->rtm_flags & RTM_F_NOTIFY)
1588 rt->rt_flags |= RTCF_NOTIFY;
1589
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001590 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 -07001591
1592 if (err == 0)
1593 goto out_free;
1594 if (err < 0) {
1595 err = -EMSGSIZE;
1596 goto out_free;
1597 }
1598
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001599 return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601out_free:
1602 kfree_skb(skb);
1603 return err;
1604}
1605
1606/*
1607 * For routers, this is called from dn_fib_dump, but for endnodes its
1608 * called directly from the rtnetlink dispatch table.
1609 */
1610int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1611{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001612 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 struct dn_route *rt;
1614 int h, s_h;
1615 int idx, s_idx;
1616
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001617 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001618 return 0;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
1621 return -EINVAL;
1622 if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
1623 return 0;
1624
1625 s_h = cb->args[0];
1626 s_idx = idx = cb->args[1];
1627 for(h = 0; h <= dn_rt_hash_mask; h++) {
1628 if (h < s_h)
1629 continue;
1630 if (h > s_h)
1631 s_idx = 0;
1632 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001633 for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 rt;
Paul E. McKenneya898def2010-02-22 17:04:49 -08001635 rt = rcu_dereference_bh(rt->u.dst.dn_next), idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (idx < s_idx)
1637 continue;
Eric Dumazetadf30902009-06-02 05:19:30 +00001638 skb_dst_set(skb, dst_clone(&rt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001640 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001641 1, NLM_F_MULTI) <= 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001642 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 rcu_read_unlock_bh();
1644 goto done;
1645 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001646 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648 rcu_read_unlock_bh();
1649 }
1650
1651done:
1652 cb->args[0] = h;
1653 cb->args[1] = idx;
1654 return skb->len;
1655}
1656
1657#ifdef CONFIG_PROC_FS
1658struct dn_rt_cache_iter_state {
1659 int bucket;
1660};
1661
1662static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1663{
1664 struct dn_route *rt = NULL;
1665 struct dn_rt_cache_iter_state *s = seq->private;
1666
1667 for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1668 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08001669 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (rt)
1671 break;
1672 rcu_read_unlock_bh();
1673 }
Paul E. McKenneya898def2010-02-22 17:04:49 -08001674 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
1677static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1678{
Eric Dumazet0d89d792008-01-10 22:35:21 -08001679 struct dn_rt_cache_iter_state *s = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Eric Dumazet0c195c32007-02-09 16:25:52 -08001681 rt = rt->u.dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 while(!rt) {
1683 rcu_read_unlock_bh();
1684 if (--s->bucket < 0)
1685 break;
1686 rcu_read_lock_bh();
1687 rt = dn_rt_hash_table[s->bucket].chain;
1688 }
Paul E. McKenneya898def2010-02-22 17:04:49 -08001689 return rcu_dereference_bh(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1693{
1694 struct dn_route *rt = dn_rt_cache_get_first(seq);
1695
1696 if (rt) {
1697 while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1698 --*pos;
1699 }
1700 return *pos ? NULL : rt;
1701}
1702
1703static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1704{
1705 struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1706 ++*pos;
1707 return rt;
1708}
1709
1710static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1711{
1712 if (v)
1713 rcu_read_unlock_bh();
1714}
1715
1716static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1717{
1718 struct dn_route *rt = v;
1719 char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1720
1721 seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
1722 rt->u.dst.dev ? rt->u.dst.dev->name : "*",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -08001723 dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1724 dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 atomic_read(&rt->u.dst.__refcnt),
1726 rt->u.dst.__use,
1727 (int) dst_metric(&rt->u.dst, RTAX_RTT));
1728 return 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001729}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001731static const struct seq_operations dn_rt_cache_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 .start = dn_rt_cache_seq_start,
1733 .next = dn_rt_cache_seq_next,
1734 .stop = dn_rt_cache_seq_stop,
1735 .show = dn_rt_cache_seq_show,
1736};
1737
1738static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1739{
Pavel Emelyanov31164082007-10-10 02:30:23 -07001740 return seq_open_private(file, &dn_rt_cache_seq_ops,
1741 sizeof(struct dn_rt_cache_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
Arjan van de Ven9a321442007-02-12 00:55:35 -08001744static const struct file_operations dn_rt_cache_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .owner = THIS_MODULE,
1746 .open = dn_rt_cache_seq_open,
1747 .read = seq_read,
1748 .llseek = seq_lseek,
1749 .release = seq_release_private,
1750};
1751
1752#endif /* CONFIG_PROC_FS */
1753
1754void __init dn_route_init(void)
1755{
1756 int i, goal, order;
1757
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07001758 dn_dst_ops.kmem_cachep =
1759 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001760 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001761 setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1763 add_timer(&dn_route_timer);
1764
Jan Beulich44813742009-09-21 17:03:05 -07001765 goal = totalram_pages >> (26 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 for(order = 0; (1UL << order) < goal; order++)
1768 /* NOTHING */;
1769
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001770 /*
1771 * Only want 1024 entries max, since the table is very, very unlikely
1772 * to be larger than that.
1773 */
1774 while(order && ((((1UL << order) * PAGE_SIZE) /
1775 sizeof(struct dn_rt_hash_bucket)) >= 2048))
1776 order--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001778 do {
1779 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1780 sizeof(struct dn_rt_hash_bucket);
1781 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1782 dn_rt_hash_mask--;
1783 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1784 __get_free_pages(GFP_ATOMIC, order);
1785 } while (dn_rt_hash_table == NULL && --order > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 if (!dn_rt_hash_table)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001788 panic("Failed to allocate DECnet route cache hash table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001790 printk(KERN_INFO
1791 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1792 dn_rt_hash_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1794
1795 dn_rt_hash_mask--;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001796 for(i = 0; i <= dn_rt_hash_mask; i++) {
1797 spin_lock_init(&dn_rt_hash_table[i].lock);
1798 dn_rt_hash_table[i].chain = NULL;
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001801 dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001803 proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001804
1805#ifdef CONFIG_DECNET_ROUTER
1806 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, dn_fib_dump);
1807#else
1808 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1809 dn_cache_dump);
1810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
1813void __exit dn_route_cleanup(void)
1814{
1815 del_timer(&dn_route_timer);
1816 dn_run_flush(0);
1817
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001818 proc_net_remove(&init_net, "decnet_cache");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820