blob: a03284061a3191e0019e6a32cd4c340019853c16 [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>
69#include <net/sock.h>
70#include <linux/mm.h>
71#include <linux/proc_fs.h>
72#include <linux/seq_file.h>
73#include <linux/init.h>
74#include <linux/rtnetlink.h>
75#include <linux/string.h>
76#include <linux/netfilter_decnet.h>
77#include <linux/rcupdate.h>
78#include <linux/times.h>
79#include <asm/errno.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020080#include <net/net_namespace.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070081#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <net/neighbour.h>
83#include <net/dst.h>
84#include <net/flow.h>
Steven Whitehousea8731cb2006-08-09 15:56:46 -070085#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <net/dn.h>
87#include <net/dn_dev.h>
88#include <net/dn_nsp.h>
89#include <net/dn_route.h>
90#include <net/dn_neigh.h>
91#include <net/dn_fib.h>
92
93struct dn_rt_hash_bucket
94{
95 struct dn_route *chain;
96 spinlock_t lock;
Eric Dumazetfca09fb2008-02-07 23:29:57 -080097};
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99extern struct neigh_table dn_neigh_table;
100
101
102static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};
103
104static const int dn_rt_min_delay = 2 * HZ;
105static const int dn_rt_max_delay = 10 * HZ;
106static const int dn_rt_mtu_expires = 10 * 60 * HZ;
107
108static unsigned long dn_rt_deadline;
109
Daniel Lezcano569d3642008-01-18 03:56:57 -0800110static int dn_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);
112static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
113static void dn_dst_link_failure(struct sk_buff *);
114static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu);
115static int dn_route_input(struct sk_buff *);
116static void dn_run_flush(unsigned long dummy);
117
118static struct dn_rt_hash_bucket *dn_rt_hash_table;
119static unsigned dn_rt_hash_mask;
120
121static struct timer_list dn_route_timer;
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700122static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123int decnet_dst_gc_interval = 2;
124
125static struct dst_ops dn_dst_ops = {
126 .family = PF_DECnet,
Harvey Harrison09640e62009-02-01 00:45:17 -0800127 .protocol = cpu_to_be16(ETH_P_DNA_RT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .gc_thresh = 128,
129 .gc = dn_dst_gc,
130 .check = dn_dst_check,
131 .negative_advice = dn_dst_negative_advice,
132 .link_failure = dn_dst_link_failure,
133 .update_pmtu = dn_dst_update_pmtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .entries = ATOMIC_INIT(0),
135};
136
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800137static __inline__ unsigned dn_hash(__le16 src, __le16 dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800139 __u16 tmp = (__u16 __force)(src ^ dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 tmp ^= (tmp >> 3);
141 tmp ^= (tmp >> 5);
142 tmp ^= (tmp >> 10);
143 return dn_rt_hash_mask & (unsigned)tmp;
144}
145
146static inline void dnrt_free(struct dn_route *rt)
147{
148 call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
149}
150
151static inline void dnrt_drop(struct dn_route *rt)
152{
Adrian Bunk34001122006-03-20 23:00:29 -0800153 dst_release(&rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
155}
156
157static void dn_dst_check_expire(unsigned long dummy)
158{
159 int i;
160 struct dn_route *rt, **rtp;
161 unsigned long now = jiffies;
162 unsigned long expire = 120 * HZ;
163
164 for(i = 0; i <= dn_rt_hash_mask; i++) {
165 rtp = &dn_rt_hash_table[i].chain;
166
167 spin_lock(&dn_rt_hash_table[i].lock);
168 while((rt=*rtp) != NULL) {
169 if (atomic_read(&rt->u.dst.__refcnt) ||
170 (now - rt->u.dst.lastuse) < expire) {
Eric Dumazet0c195c32007-02-09 16:25:52 -0800171 rtp = &rt->u.dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 continue;
173 }
Eric Dumazet0c195c32007-02-09 16:25:52 -0800174 *rtp = rt->u.dst.dn_next;
175 rt->u.dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 dnrt_free(rt);
177 }
178 spin_unlock(&dn_rt_hash_table[i].lock);
179
180 if ((jiffies - now) > 0)
181 break;
182 }
183
184 mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
185}
186
Daniel Lezcano569d3642008-01-18 03:56:57 -0800187static int dn_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct dn_route *rt, **rtp;
190 int i;
191 unsigned long now = jiffies;
192 unsigned long expire = 10 * HZ;
193
194 for(i = 0; i <= dn_rt_hash_mask; i++) {
195
196 spin_lock_bh(&dn_rt_hash_table[i].lock);
197 rtp = &dn_rt_hash_table[i].chain;
198
199 while((rt=*rtp) != NULL) {
200 if (atomic_read(&rt->u.dst.__refcnt) ||
201 (now - rt->u.dst.lastuse) < expire) {
Eric Dumazet0c195c32007-02-09 16:25:52 -0800202 rtp = &rt->u.dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 continue;
204 }
Eric Dumazet0c195c32007-02-09 16:25:52 -0800205 *rtp = rt->u.dst.dn_next;
206 rt->u.dst.dn_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 dnrt_drop(rt);
208 break;
209 }
210 spin_unlock_bh(&dn_rt_hash_table[i].lock);
211 }
212
213 return 0;
214}
215
216/*
217 * The decnet standards don't impose a particular minimum mtu, what they
218 * do insist on is that the routing layer accepts a datagram of at least
219 * 230 bytes long. Here we have to subtract the routing header length from
220 * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
221 * assume the worst and use a long header size.
222 *
223 * We update both the mtu and the advertised mss (i.e. the segment size we
224 * advertise to the other end).
225 */
226static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu)
227{
228 u32 min_mtu = 230;
229 struct dn_dev *dn = dst->neighbour ?
230 (struct dn_dev *)dst->neighbour->dev->dn_ptr : NULL;
231
232 if (dn && dn->use_long == 0)
233 min_mtu -= 6;
234 else
235 min_mtu -= 21;
236
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700237 if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!(dst_metric_locked(dst, RTAX_MTU))) {
239 dst->metrics[RTAX_MTU-1] = mtu;
240 dst_set_expires(dst, dn_rt_mtu_expires);
241 }
242 if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
243 u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700244 if (dst_metric(dst, RTAX_ADVMSS) > mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 dst->metrics[RTAX_ADVMSS-1] = mss;
246 }
247 }
248}
249
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900250/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * When a route has been marked obsolete. (e.g. routing cache flush)
252 */
253static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
254{
255 return NULL;
256}
257
258static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
259{
260 dst_release(dst);
261 return NULL;
262}
263
264static void dn_dst_link_failure(struct sk_buff *skb)
265{
266 return;
267}
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
520 return NF_HOOK(PF_DECnet, NF_DN_PRE_ROUTING, skb, skb->dev, NULL, dn_route_rx_packet);
521
522drop_it:
523 kfree_skb(skb);
524 return NET_RX_DROP;
525}
526
527
528
529static int dn_route_rx_short(struct sk_buff *skb)
530{
531 struct dn_skb_cb *cb = DN_SKB_CB(skb);
532 unsigned char *ptr = skb->data;
533
534 if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
535 goto drop_it;
536
537 skb_pull(skb, 5);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300538 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800540 cb->dst = *(__le16 *)ptr;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900541 ptr += 2;
542 cb->src = *(__le16 *)ptr;
543 ptr += 2;
544 cb->hops = *ptr & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 return NF_HOOK(PF_DECnet, NF_DN_PRE_ROUTING, skb, skb->dev, NULL, dn_route_rx_packet);
547
548drop_it:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900549 kfree_skb(skb);
550 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553static int dn_route_discard(struct sk_buff *skb)
554{
555 /*
556 * I know we drop the packet here, but thats considered success in
557 * this case
558 */
559 kfree_skb(skb);
560 return NET_RX_SUCCESS;
561}
562
563static int dn_route_ptp_hello(struct sk_buff *skb)
564{
565 dn_dev_hello(skb);
566 dn_neigh_pointopoint_hello(skb);
567 return NET_RX_SUCCESS;
568}
569
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700570int 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 -0700571{
572 struct dn_skb_cb *cb;
573 unsigned char flags = 0;
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800574 __u16 len = le16_to_cpu(*(__le16 *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct dn_dev *dn = (struct dn_dev *)dev->dn_ptr;
576 unsigned char padlen = 0;
577
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700578 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700579 goto dump_it;
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (dn == NULL)
582 goto dump_it;
583
584 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
585 goto out;
586
587 if (!pskb_may_pull(skb, 3))
588 goto dump_it;
589
590 skb_pull(skb, 2);
591
592 if (len > skb->len)
593 goto dump_it;
594
595 skb_trim(skb, len);
596
597 flags = *skb->data;
598
599 cb = DN_SKB_CB(skb);
600 cb->stamp = jiffies;
601 cb->iif = dev->ifindex;
602
603 /*
604 * If we have padding, remove it.
605 */
606 if (flags & DN_RT_F_PF) {
607 padlen = flags & ~DN_RT_F_PF;
608 if (!pskb_may_pull(skb, padlen + 1))
609 goto dump_it;
610 skb_pull(skb, padlen);
611 flags = *skb->data;
612 }
613
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700614 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /*
617 * Weed out future version DECnet
618 */
619 if (flags & DN_RT_F_VER)
620 goto dump_it;
621
622 cb->rt_flags = flags;
623
624 if (decnet_debug_level & 1)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900625 printk(KERN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900627 (int)flags, (dev) ? dev->name : "???", len, skb->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 padlen);
629
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900630 if (flags & DN_RT_PKT_CNTL) {
Herbert Xu364c6ba2006-06-09 16:10:40 -0700631 if (unlikely(skb_linearize(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 goto dump_it;
633
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900634 switch(flags & DN_RT_CNTL_MSK) {
635 case DN_RT_PKT_INIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 dn_dev_init_pkt(skb);
637 break;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900638 case DN_RT_PKT_VERI:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 dn_dev_veri_pkt(skb);
640 break;
641 }
642
643 if (dn->parms.state != DN_DEV_S_RU)
644 goto dump_it;
645
646 switch(flags & DN_RT_CNTL_MSK) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900647 case DN_RT_PKT_HELO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return NF_HOOK(PF_DECnet, NF_DN_HELLO, skb, skb->dev, NULL, dn_route_ptp_hello);
649
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900650 case DN_RT_PKT_L1RT:
651 case DN_RT_PKT_L2RT:
652 return NF_HOOK(PF_DECnet, NF_DN_ROUTE, skb, skb->dev, NULL, dn_route_discard);
653 case DN_RT_PKT_ERTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return NF_HOOK(PF_DECnet, NF_DN_HELLO, skb, skb->dev, NULL, dn_neigh_router_hello);
655
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900656 case DN_RT_PKT_EEDH:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return NF_HOOK(PF_DECnet, NF_DN_HELLO, skb, skb->dev, NULL, dn_neigh_endnode_hello);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900658 }
659 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (dn->parms.state != DN_DEV_S_RU)
661 goto dump_it;
662
663 skb_pull(skb, 1); /* Pull flags */
664
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900665 switch(flags & DN_RT_PKT_MSK) {
666 case DN_RT_PKT_LONG:
667 return dn_route_rx_long(skb);
668 case DN_RT_PKT_SHORT:
669 return dn_route_rx_short(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673dump_it:
674 kfree_skb(skb);
675out:
676 return NET_RX_DROP;
677}
678
679static int dn_output(struct sk_buff *skb)
680{
Eric Dumazetadf30902009-06-02 05:19:30 +0000681 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct dn_route *rt = (struct dn_route *)dst;
683 struct net_device *dev = dst->dev;
684 struct dn_skb_cb *cb = DN_SKB_CB(skb);
685 struct neighbour *neigh;
686
687 int err = -EINVAL;
688
689 if ((neigh = dst->neighbour) == NULL)
690 goto error;
691
692 skb->dev = dev;
693
694 cb->src = rt->rt_saddr;
695 cb->dst = rt->rt_daddr;
696
697 /*
698 * Always set the Intra-Ethernet bit on all outgoing packets
699 * originated on this node. Only valid flag from upper layers
700 * is return-to-sender-requested. Set hop count to 0 too.
701 */
702 cb->rt_flags &= ~DN_RT_F_RQR;
703 cb->rt_flags |= DN_RT_F_IE;
704 cb->hops = 0;
705
706 return NF_HOOK(PF_DECnet, NF_DN_LOCAL_OUT, skb, NULL, dev, neigh->output);
707
708error:
709 if (net_ratelimit())
710 printk(KERN_DEBUG "dn_output: This should not happen\n");
711
712 kfree_skb(skb);
713
714 return err;
715}
716
717static int dn_forward(struct sk_buff *skb)
718{
719 struct dn_skb_cb *cb = DN_SKB_CB(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000720 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct dn_dev *dn_db = dst->dev->dn_ptr;
722 struct dn_route *rt;
723 struct neighbour *neigh = dst->neighbour;
724 int header_len;
725#ifdef CONFIG_NETFILTER
726 struct net_device *dev = skb->dev;
727#endif
728
729 if (skb->pkt_type != PACKET_HOST)
730 goto drop;
731
732 /* Ensure that we have enough space for headers */
Eric Dumazetadf30902009-06-02 05:19:30 +0000733 rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 header_len = dn_db->use_long ? 21 : 6;
735 if (skb_cow(skb, LL_RESERVED_SPACE(rt->u.dst.dev)+header_len))
736 goto drop;
737
738 /*
739 * Hop count exceeded.
740 */
741 if (++cb->hops > 30)
742 goto drop;
743
744 skb->dev = rt->u.dst.dev;
745
746 /*
747 * If packet goes out same interface it came in on, then set
748 * the Intra-Ethernet bit. This has no effect for short
749 * packets, so we don't need to test for them here.
750 */
751 cb->rt_flags &= ~DN_RT_F_IE;
752 if (rt->rt_flags & RTCF_DOREDIRECT)
753 cb->rt_flags |= DN_RT_F_IE;
754
755 return NF_HOOK(PF_DECnet, NF_DN_FORWARD, skb, dev, skb->dev, neigh->output);
756
757drop:
758 kfree_skb(skb);
759 return NET_RX_DROP;
760}
761
762/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * Used to catch bugs. This should never normally get
764 * called.
765 */
766static int dn_rt_bug(struct sk_buff *skb)
767{
768 if (net_ratelimit()) {
769 struct dn_skb_cb *cb = DN_SKB_CB(skb);
770
771 printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800772 le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
775 kfree_skb(skb);
776
Florian Westphal0e8635a2009-06-20 00:53:25 +0000777 return NET_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
781{
782 struct dn_fib_info *fi = res->fi;
783 struct net_device *dev = rt->u.dst.dev;
784 struct neighbour *n;
785 unsigned mss;
786
787 if (fi) {
788 if (DN_FIB_RES_GW(*res) &&
789 DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
790 rt->rt_gateway = DN_FIB_RES_GW(*res);
791 memcpy(rt->u.dst.metrics, fi->fib_metrics,
792 sizeof(rt->u.dst.metrics));
793 }
794 rt->rt_type = res->type;
795
796 if (dev != NULL && rt->u.dst.neighbour == NULL) {
797 n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
798 if (IS_ERR(n))
799 return PTR_ERR(n);
800 rt->u.dst.neighbour = n;
801 }
802
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700803 if (dst_metric(&rt->u.dst, RTAX_MTU) == 0 ||
804 dst_metric(&rt->u.dst, RTAX_MTU) > rt->u.dst.dev->mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 rt->u.dst.metrics[RTAX_MTU-1] = rt->u.dst.dev->mtu;
806 mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->u.dst));
Satoru SATOH5ffc02a2008-05-04 22:14:42 -0700807 if (dst_metric(&rt->u.dst, RTAX_ADVMSS) == 0 ||
808 dst_metric(&rt->u.dst, RTAX_ADVMSS) > mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 rt->u.dst.metrics[RTAX_ADVMSS-1] = mss;
810 return 0;
811}
812
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800813static inline int dn_match_addr(__le16 addr1, __le16 addr2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800815 __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int match = 16;
817 while(tmp) {
818 tmp >>= 1;
819 match--;
820 }
821 return match;
822}
823
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800824static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800826 __le16 saddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct dn_dev *dn_db = dev->dn_ptr;
828 struct dn_ifaddr *ifa;
829 int best_match = 0;
830 int ret;
831
832 read_lock(&dev_base_lock);
833 for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
834 if (ifa->ifa_scope > scope)
835 continue;
836 if (!daddr) {
837 saddr = ifa->ifa_local;
838 break;
839 }
840 ret = dn_match_addr(daddr, ifa->ifa_local);
841 if (ret > best_match)
842 saddr = ifa->ifa_local;
843 if (best_match == 0)
844 saddr = ifa->ifa_local;
845 }
846 read_unlock(&dev_base_lock);
847
848 return saddr;
849}
850
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800851static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
854}
855
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800856static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800858 __le16 mask = dnet_make_mask(res->prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
860}
861
862static int dn_route_output_slow(struct dst_entry **pprt, const struct flowi *oldflp, int try_hard)
863{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900864 struct flowi fl = { .nl_u = { .dn_u =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 { .daddr = oldflp->fld_dst,
866 .saddr = oldflp->fld_src,
867 .scope = RT_SCOPE_UNIVERSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 } },
Thomas Graf47dcf0c2006-11-09 15:20:38 -0800869 .mark = oldflp->mark,
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700870 .iif = init_net.loopback_dev->ifindex,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 .oif = oldflp->oif };
872 struct dn_route *rt = NULL;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700873 struct net_device *dev_out = NULL, *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct neighbour *neigh = NULL;
875 unsigned hash;
876 unsigned flags = 0;
877 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
878 int err;
879 int free_res = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800880 __le16 gateway = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (decnet_debug_level & 16)
883 printk(KERN_DEBUG
884 "dn_route_output_slow: dst=%04x src=%04x mark=%d"
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800885 " iif=%d oif=%d\n", le16_to_cpu(oldflp->fld_dst),
886 le16_to_cpu(oldflp->fld_src),
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700887 oldflp->mark, init_net.loopback_dev->ifindex, oldflp->oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /* If we have an output interface, verify its a DECnet device */
890 if (oldflp->oif) {
Eric W. Biederman881d9662007-09-17 11:56:21 -0700891 dev_out = dev_get_by_index(&init_net, oldflp->oif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 err = -ENODEV;
893 if (dev_out && dev_out->dn_ptr == NULL) {
894 dev_put(dev_out);
895 dev_out = NULL;
896 }
897 if (dev_out == NULL)
898 goto out;
899 }
900
901 /* If we have a source address, verify that its a local address */
902 if (oldflp->fld_src) {
903 err = -EADDRNOTAVAIL;
904
905 if (dev_out) {
906 if (dn_dev_islocal(dev_out, oldflp->fld_src))
907 goto source_ok;
908 dev_put(dev_out);
909 goto out;
910 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800911 rcu_read_lock();
912 for_each_netdev_rcu(&init_net, dev) {
Pavel Emelianov7562f872007-05-03 15:13:45 -0700913 if (!dev->dn_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700915 if (!dn_dev_islocal(dev, oldflp->fld_src))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700916 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700917 if ((dev->flags & IFF_LOOPBACK) &&
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700918 oldflp->fld_dst &&
Pavel Emelianov7562f872007-05-03 15:13:45 -0700919 !dn_dev_islocal(dev, oldflp->fld_dst))
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700920 continue;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700921
922 dev_out = dev;
Patrick Caulfield9bbf28a12006-08-02 14:14:44 -0700923 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800925 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (dev_out == NULL)
927 goto out;
928 dev_hold(dev_out);
929source_ok:
930 ;
931 }
932
933 /* No destination? Assume its local */
934 if (!fl.fld_dst) {
935 fl.fld_dst = fl.fld_src;
936
937 err = -EADDRNOTAVAIL;
938 if (dev_out)
939 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700940 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 dev_hold(dev_out);
942 if (!fl.fld_dst) {
943 fl.fld_dst =
944 fl.fld_src = dnet_select_source(dev_out, 0,
945 RT_SCOPE_HOST);
946 if (!fl.fld_dst)
947 goto out;
948 }
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700949 fl.oif = init_net.loopback_dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 res.type = RTN_LOCAL;
951 goto make_route;
952 }
953
954 if (decnet_debug_level & 16)
955 printk(KERN_DEBUG
956 "dn_route_output_slow: initial checks complete."
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800957 " dst=%o4x src=%04x oif=%d try_hard=%d\n",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -0800958 le16_to_cpu(fl.fld_dst), le16_to_cpu(fl.fld_src),
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800959 fl.oif, try_hard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /*
962 * N.B. If the kernel is compiled without router support then
963 * dn_fib_lookup() will evaluate to non-zero so this if () block
964 * will always be executed.
965 */
966 err = -ESRCH;
967 if (try_hard || (err = dn_fib_lookup(&fl, &res)) != 0) {
968 struct dn_dev *dn_db;
969 if (err != -ESRCH)
970 goto out;
971 /*
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900972 * Here the fallback is basically the standard algorithm for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * routing in endnodes which is described in the DECnet routing
974 * docs
975 *
976 * If we are not trying hard, look in neighbour cache.
977 * The result is tested to ensure that if a specific output
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900978 * device/source address was requested, then we honour that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * here
980 */
981 if (!try_hard) {
Eric W. Biederman426b5302008-01-24 00:13:18 -0800982 neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fl.fld_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (neigh) {
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900984 if ((oldflp->oif &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 (neigh->dev->ifindex != oldflp->oif)) ||
986 (oldflp->fld_src &&
987 (!dn_dev_islocal(neigh->dev,
988 oldflp->fld_src)))) {
989 neigh_release(neigh);
990 neigh = NULL;
991 } else {
992 if (dev_out)
993 dev_put(dev_out);
994 if (dn_dev_islocal(neigh->dev, fl.fld_dst)) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700995 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 res.type = RTN_LOCAL;
997 } else {
998 dev_out = neigh->dev;
999 }
1000 dev_hold(dev_out);
1001 goto select_source;
1002 }
1003 }
1004 }
1005
1006 /* Not there? Perhaps its a local address */
1007 if (dev_out == NULL)
1008 dev_out = dn_dev_get_default();
1009 err = -ENODEV;
1010 if (dev_out == NULL)
1011 goto out;
1012 dn_db = dev_out->dn_ptr;
1013 /* Possible improvement - check all devices for local addr */
1014 if (dn_dev_islocal(dev_out, fl.fld_dst)) {
1015 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001016 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 dev_hold(dev_out);
1018 res.type = RTN_LOCAL;
1019 goto select_source;
1020 }
1021 /* Not local either.... try sending it to the default router */
1022 neigh = neigh_clone(dn_db->router);
1023 BUG_ON(neigh && neigh->dev != dev_out);
1024
1025 /* Ok then, we assume its directly connected and move on */
1026select_source:
1027 if (neigh)
1028 gateway = ((struct dn_neigh *)neigh)->addr;
1029 if (gateway == 0)
1030 gateway = fl.fld_dst;
1031 if (fl.fld_src == 0) {
1032 fl.fld_src = dnet_select_source(dev_out, gateway,
1033 res.type == RTN_LOCAL ?
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001034 RT_SCOPE_HOST :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 RT_SCOPE_LINK);
1036 if (fl.fld_src == 0 && res.type != RTN_LOCAL)
1037 goto e_addr;
1038 }
1039 fl.oif = dev_out->ifindex;
1040 goto make_route;
1041 }
1042 free_res = 1;
1043
1044 if (res.type == RTN_NAT)
1045 goto e_inval;
1046
1047 if (res.type == RTN_LOCAL) {
1048 if (!fl.fld_src)
1049 fl.fld_src = fl.fld_dst;
1050 if (dev_out)
1051 dev_put(dev_out);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07001052 dev_out = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 dev_hold(dev_out);
1054 fl.oif = dev_out->ifindex;
1055 if (res.fi)
1056 dn_fib_info_put(res.fi);
1057 res.fi = NULL;
1058 goto make_route;
1059 }
1060
1061 if (res.fi->fib_nhs > 1 && fl.oif == 0)
1062 dn_fib_select_multipath(&fl, &res);
1063
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001064 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * We could add some logic to deal with default routes here and
1066 * get rid of some of the special casing above.
1067 */
1068
1069 if (!fl.fld_src)
1070 fl.fld_src = DN_FIB_RES_PREFSRC(res);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (dev_out)
1073 dev_put(dev_out);
1074 dev_out = DN_FIB_RES_DEV(res);
1075 dev_hold(dev_out);
1076 fl.oif = dev_out->ifindex;
1077 gateway = DN_FIB_RES_GW(res);
1078
1079make_route:
1080 if (dev_out->flags & IFF_LOOPBACK)
1081 flags |= RTCF_LOCAL;
1082
1083 rt = dst_alloc(&dn_dst_ops);
1084 if (rt == NULL)
1085 goto e_nobufs;
1086
1087 atomic_set(&rt->u.dst.__refcnt, 1);
1088 rt->u.dst.flags = DST_HOST;
1089
1090 rt->fl.fld_src = oldflp->fld_src;
1091 rt->fl.fld_dst = oldflp->fld_dst;
1092 rt->fl.oif = oldflp->oif;
1093 rt->fl.iif = 0;
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001094 rt->fl.mark = oldflp->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 rt->rt_saddr = fl.fld_src;
1097 rt->rt_daddr = fl.fld_dst;
1098 rt->rt_gateway = gateway ? gateway : fl.fld_dst;
1099 rt->rt_local_src = fl.fld_src;
1100
1101 rt->rt_dst_map = fl.fld_dst;
1102 rt->rt_src_map = fl.fld_src;
1103
1104 rt->u.dst.dev = dev_out;
1105 dev_hold(dev_out);
1106 rt->u.dst.neighbour = neigh;
1107 neigh = NULL;
1108
1109 rt->u.dst.lastuse = jiffies;
1110 rt->u.dst.output = dn_output;
1111 rt->u.dst.input = dn_rt_bug;
1112 rt->rt_flags = flags;
1113 if (flags & RTCF_LOCAL)
1114 rt->u.dst.input = dn_nsp_rx;
1115
1116 err = dn_rt_set_next_hop(rt, &res);
1117 if (err)
1118 goto e_neighbour;
1119
1120 hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst);
1121 dn_insert_route(rt, hash, (struct dn_route **)pprt);
1122
1123done:
1124 if (neigh)
1125 neigh_release(neigh);
1126 if (free_res)
1127 dn_fib_res_put(&res);
1128 if (dev_out)
1129 dev_put(dev_out);
1130out:
1131 return err;
1132
1133e_addr:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001134 err = -EADDRNOTAVAIL;
1135 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136e_inval:
1137 err = -EINVAL;
1138 goto done;
1139e_nobufs:
1140 err = -ENOBUFS;
1141 goto done;
1142e_neighbour:
1143 dst_free(&rt->u.dst);
1144 goto e_nobufs;
1145}
1146
1147
1148/*
1149 * N.B. The flags may be moved into the flowi at some future stage.
1150 */
1151static int __dn_route_output_key(struct dst_entry **pprt, const struct flowi *flp, int flags)
1152{
1153 unsigned hash = dn_hash(flp->fld_src, flp->fld_dst);
1154 struct dn_route *rt = NULL;
1155
1156 if (!(flags & MSG_TRYHARD)) {
1157 rcu_read_lock_bh();
1158 for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt;
Eric Dumazet0c195c32007-02-09 16:25:52 -08001159 rt = rcu_dereference(rt->u.dst.dn_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if ((flp->fld_dst == rt->fl.fld_dst) &&
1161 (flp->fld_src == rt->fl.fld_src) &&
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001162 (flp->mark == rt->fl.mark) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 (rt->fl.iif == 0) &&
1164 (rt->fl.oif == flp->oif)) {
Pavel Emelyanov03f49f32007-11-10 21:28:34 -08001165 dst_use(&rt->u.dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 rcu_read_unlock_bh();
1167 *pprt = &rt->u.dst;
1168 return 0;
1169 }
1170 }
1171 rcu_read_unlock_bh();
1172 }
1173
1174 return dn_route_output_slow(pprt, flp, flags);
1175}
1176
1177static int dn_route_output_key(struct dst_entry **pprt, struct flowi *flp, int flags)
1178{
1179 int err;
1180
1181 err = __dn_route_output_key(pprt, flp, flags);
1182 if (err == 0 && flp->proto) {
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001183 err = xfrm_lookup(&init_net, pprt, flp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185 return err;
1186}
1187
1188int dn_route_output_sock(struct dst_entry **pprt, struct flowi *fl, struct sock *sk, int flags)
1189{
1190 int err;
1191
1192 err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
1193 if (err == 0 && fl->proto) {
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001194 err = xfrm_lookup(&init_net, pprt, fl, sk,
1195 (flags & MSG_DONTWAIT) ? 0 : XFRM_LOOKUP_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197 return err;
1198}
1199
1200static int dn_route_input_slow(struct sk_buff *skb)
1201{
1202 struct dn_route *rt = NULL;
1203 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1204 struct net_device *in_dev = skb->dev;
1205 struct net_device *out_dev = NULL;
1206 struct dn_dev *dn_db;
1207 struct neighbour *neigh = NULL;
1208 unsigned hash;
1209 int flags = 0;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001210 __le16 gateway = 0;
1211 __le16 local_src = 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001212 struct flowi fl = { .nl_u = { .dn_u =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 { .daddr = cb->dst,
1214 .saddr = cb->src,
1215 .scope = RT_SCOPE_UNIVERSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 } },
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001217 .mark = skb->mark,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 .iif = skb->dev->ifindex };
1219 struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1220 int err = -EINVAL;
1221 int free_res = 0;
1222
1223 dev_hold(in_dev);
1224
1225 if ((dn_db = in_dev->dn_ptr) == NULL)
1226 goto out;
1227
1228 /* Zero source addresses are not allowed */
1229 if (fl.fld_src == 0)
1230 goto out;
1231
1232 /*
1233 * In this case we've just received a packet from a source
1234 * outside ourselves pretending to come from us. We don't
1235 * allow it any further to prevent routing loops, spoofing and
1236 * other nasties. Loopback packets already have the dst attached
1237 * so this only affects packets which have originated elsewhere.
1238 */
1239 err = -ENOTUNIQ;
1240 if (dn_dev_islocal(in_dev, cb->src))
1241 goto out;
1242
1243 err = dn_fib_lookup(&fl, &res);
1244 if (err) {
1245 if (err != -ESRCH)
1246 goto out;
1247 /*
1248 * Is the destination us ?
1249 */
1250 if (!dn_dev_islocal(in_dev, cb->dst))
1251 goto e_inval;
1252
1253 res.type = RTN_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 } else {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001255 __le16 src_map = fl.fld_src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 free_res = 1;
1257
1258 out_dev = DN_FIB_RES_DEV(res);
1259 if (out_dev == NULL) {
1260 if (net_ratelimit())
1261 printk(KERN_CRIT "Bug in dn_route_input_slow() "
1262 "No output device\n");
1263 goto e_inval;
1264 }
1265 dev_hold(out_dev);
1266
1267 if (res.r)
Steven Whitehousea8731cb2006-08-09 15:56:46 -07001268 src_map = fl.fld_src; /* no NAT support for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 gateway = DN_FIB_RES_GW(res);
1271 if (res.type == RTN_NAT) {
1272 fl.fld_dst = dn_fib_rules_map_destination(fl.fld_dst, &res);
1273 dn_fib_res_put(&res);
1274 free_res = 0;
1275 if (dn_fib_lookup(&fl, &res))
1276 goto e_inval;
1277 free_res = 1;
1278 if (res.type != RTN_UNICAST)
1279 goto e_inval;
1280 flags |= RTCF_DNAT;
1281 gateway = fl.fld_dst;
1282 }
1283 fl.fld_src = src_map;
1284 }
1285
1286 switch(res.type) {
1287 case RTN_UNICAST:
1288 /*
1289 * Forwarding check here, we only check for forwarding
1290 * being turned off, if you want to only forward intra
1291 * area, its up to you to set the routing tables up
1292 * correctly.
1293 */
1294 if (dn_db->parms.forwarding == 0)
1295 goto e_inval;
1296
1297 if (res.fi->fib_nhs > 1 && fl.oif == 0)
1298 dn_fib_select_multipath(&fl, &res);
1299
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001300 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1302 * flag as a hint to set the intra-ethernet bit when
1303 * forwarding. If we've got NAT in operation, we don't do
1304 * this optimisation.
1305 */
1306 if (out_dev == in_dev && !(flags & RTCF_NAT))
1307 flags |= RTCF_DOREDIRECT;
1308
1309 local_src = DN_FIB_RES_PREFSRC(res);
1310
1311 case RTN_BLACKHOLE:
1312 case RTN_UNREACHABLE:
1313 break;
1314 case RTN_LOCAL:
1315 flags |= RTCF_LOCAL;
1316 fl.fld_src = cb->dst;
1317 fl.fld_dst = cb->src;
1318
1319 /* Routing tables gave us a gateway */
1320 if (gateway)
1321 goto make_route;
1322
1323 /* Packet was intra-ethernet, so we know its on-link */
Steven Whitehouse3a31b9d2006-10-18 20:45:22 -07001324 if (cb->rt_flags & DN_RT_F_IE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 gateway = cb->src;
1326 flags |= RTCF_DIRECTSRC;
1327 goto make_route;
1328 }
1329
1330 /* Use the default router if there is one */
1331 neigh = neigh_clone(dn_db->router);
1332 if (neigh) {
1333 gateway = ((struct dn_neigh *)neigh)->addr;
1334 goto make_route;
1335 }
1336
1337 /* Close eyes and pray */
1338 gateway = cb->src;
1339 flags |= RTCF_DIRECTSRC;
1340 goto make_route;
1341 default:
1342 goto e_inval;
1343 }
1344
1345make_route:
1346 rt = dst_alloc(&dn_dst_ops);
1347 if (rt == NULL)
1348 goto e_nobufs;
1349
1350 rt->rt_saddr = fl.fld_src;
1351 rt->rt_daddr = fl.fld_dst;
1352 rt->rt_gateway = fl.fld_dst;
1353 if (gateway)
1354 rt->rt_gateway = gateway;
1355 rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
1356
1357 rt->rt_dst_map = fl.fld_dst;
1358 rt->rt_src_map = fl.fld_src;
1359
1360 rt->fl.fld_src = cb->src;
1361 rt->fl.fld_dst = cb->dst;
1362 rt->fl.oif = 0;
1363 rt->fl.iif = in_dev->ifindex;
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001364 rt->fl.mark = fl.mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 rt->u.dst.flags = DST_HOST;
1367 rt->u.dst.neighbour = neigh;
1368 rt->u.dst.dev = out_dev;
1369 rt->u.dst.lastuse = jiffies;
1370 rt->u.dst.output = dn_rt_bug;
1371 switch(res.type) {
1372 case RTN_UNICAST:
1373 rt->u.dst.input = dn_forward;
1374 break;
1375 case RTN_LOCAL:
1376 rt->u.dst.output = dn_output;
1377 rt->u.dst.input = dn_nsp_rx;
1378 rt->u.dst.dev = in_dev;
1379 flags |= RTCF_LOCAL;
1380 break;
1381 default:
1382 case RTN_UNREACHABLE:
1383 case RTN_BLACKHOLE:
Herbert Xu352e5122007-11-13 21:34:06 -08001384 rt->u.dst.input = dst_discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386 rt->rt_flags = flags;
1387 if (rt->u.dst.dev)
1388 dev_hold(rt->u.dst.dev);
1389
1390 err = dn_rt_set_next_hop(rt, &res);
1391 if (err)
1392 goto e_neighbour;
1393
1394 hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst);
Eric Dumazetadf30902009-06-02 05:19:30 +00001395 dn_insert_route(rt, hash, &rt);
1396 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398done:
1399 if (neigh)
1400 neigh_release(neigh);
1401 if (free_res)
1402 dn_fib_res_put(&res);
1403 dev_put(in_dev);
1404 if (out_dev)
1405 dev_put(out_dev);
1406out:
1407 return err;
1408
1409e_inval:
1410 err = -EINVAL;
1411 goto done;
1412
1413e_nobufs:
1414 err = -ENOBUFS;
1415 goto done;
1416
1417e_neighbour:
1418 dst_free(&rt->u.dst);
1419 goto done;
1420}
1421
Roel Kluin5eaa65b2008-12-10 15:18:31 -08001422static int dn_route_input(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct dn_route *rt;
1425 struct dn_skb_cb *cb = DN_SKB_CB(skb);
1426 unsigned hash = dn_hash(cb->src, cb->dst);
1427
Eric Dumazetadf30902009-06-02 05:19:30 +00001428 if (skb_dst(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return 0;
1430
1431 rcu_read_lock();
1432 for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
Eric Dumazet0c195c32007-02-09 16:25:52 -08001433 rt = rcu_dereference(rt->u.dst.dn_next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if ((rt->fl.fld_src == cb->src) &&
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001435 (rt->fl.fld_dst == cb->dst) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 (rt->fl.oif == 0) &&
Thomas Graf47dcf0c2006-11-09 15:20:38 -08001437 (rt->fl.mark == skb->mark) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 (rt->fl.iif == cb->iif)) {
Pavel Emelyanov03f49f32007-11-10 21:28:34 -08001439 dst_use(&rt->u.dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 rcu_read_unlock();
Eric Dumazetadf30902009-06-02 05:19:30 +00001441 skb_dst_set(skb, (struct dst_entry *)rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return 0;
1443 }
1444 }
1445 rcu_read_unlock();
1446
1447 return dn_route_input_slow(skb);
1448}
1449
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001450static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1451 int event, int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Eric Dumazetadf30902009-06-02 05:19:30 +00001453 struct dn_route *rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 struct rtmsg *r;
1455 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001456 unsigned char *b = skb_tail_pointer(skb);
Thomas Grafe3703b32006-11-27 09:27:07 -08001457 long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001459 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 r = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 r->rtm_family = AF_DECnet;
1462 r->rtm_dst_len = 16;
1463 r->rtm_src_len = 0;
1464 r->rtm_tos = 0;
1465 r->rtm_table = RT_TABLE_MAIN;
Patrick McHardy9e762a42006-08-10 23:09:48 -07001466 RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 r->rtm_type = rt->rt_type;
1468 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1469 r->rtm_scope = RT_SCOPE_UNIVERSE;
1470 r->rtm_protocol = RTPROT_UNSPEC;
1471 if (rt->rt_flags & RTCF_NOTIFY)
1472 r->rtm_flags |= RTM_F_NOTIFY;
1473 RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
1474 if (rt->fl.fld_src) {
1475 r->rtm_src_len = 16;
1476 RTA_PUT(skb, RTA_SRC, 2, &rt->fl.fld_src);
1477 }
1478 if (rt->u.dst.dev)
1479 RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->u.dst.dev->ifindex);
1480 /*
1481 * Note to self - change this if input routes reverse direction when
1482 * they deal only with inputs and not with replies like they do
1483 * currently.
1484 */
1485 RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
1486 if (rt->rt_daddr != rt->rt_gateway)
1487 RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
1488 if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
1489 goto rtattr_failure;
Thomas Grafe3703b32006-11-27 09:27:07 -08001490 expires = rt->u.dst.expires ? rt->u.dst.expires - jiffies : 0;
1491 if (rtnl_put_cacheinfo(skb, &rt->u.dst, 0, 0, 0, expires,
1492 rt->u.dst.error) < 0)
1493 goto rtattr_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (rt->fl.iif)
1495 RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fl.iif);
1496
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001497 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return skb->len;
1499
1500nlmsg_failure:
1501rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001502 nlmsg_trim(skb, b);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001503 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
1506/*
1507 * This is called by both endnodes and routers now.
1508 */
Thomas Graffa34ddd2007-03-22 11:57:46 -07001509static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001511 struct net *net = sock_net(in_skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 struct rtattr **rta = arg;
1513 struct rtmsg *rtm = NLMSG_DATA(nlh);
1514 struct dn_route *rt = NULL;
1515 struct dn_skb_cb *cb;
1516 int err;
1517 struct sk_buff *skb;
1518 struct flowi fl;
1519
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001520 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001521 return -EINVAL;
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 memset(&fl, 0, sizeof(fl));
1524 fl.proto = DNPROTO_NSP;
1525
1526 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1527 if (skb == NULL)
1528 return -ENOBUFS;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001529 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 cb = DN_SKB_CB(skb);
1531
1532 if (rta[RTA_SRC-1])
1533 memcpy(&fl.fld_src, RTA_DATA(rta[RTA_SRC-1]), 2);
1534 if (rta[RTA_DST-1])
1535 memcpy(&fl.fld_dst, RTA_DATA(rta[RTA_DST-1]), 2);
1536 if (rta[RTA_IIF-1])
1537 memcpy(&fl.iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
1538
1539 if (fl.iif) {
1540 struct net_device *dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001541 if ((dev = dev_get_by_index(&init_net, fl.iif)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 kfree_skb(skb);
1543 return -ENODEV;
1544 }
1545 if (!dev->dn_ptr) {
1546 dev_put(dev);
1547 kfree_skb(skb);
1548 return -ENODEV;
1549 }
YOSHIFUJI Hideakib98999d2007-12-12 03:51:49 +09001550 skb->protocol = htons(ETH_P_DNA_RT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 skb->dev = dev;
1552 cb->src = fl.fld_src;
1553 cb->dst = fl.fld_dst;
1554 local_bh_disable();
1555 err = dn_route_input(skb);
1556 local_bh_enable();
1557 memset(cb, 0, sizeof(struct dn_skb_cb));
Eric Dumazetadf30902009-06-02 05:19:30 +00001558 rt = (struct dn_route *)skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!err && -rt->u.dst.error)
1560 err = rt->u.dst.error;
1561 } else {
1562 int oif = 0;
1563 if (rta[RTA_OIF - 1])
1564 memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
1565 fl.oif = oif;
1566 err = dn_route_output_key((struct dst_entry **)&rt, &fl, 0);
1567 }
1568
1569 if (skb->dev)
1570 dev_put(skb->dev);
1571 skb->dev = NULL;
1572 if (err)
1573 goto out_free;
Eric Dumazetadf30902009-06-02 05:19:30 +00001574 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (rtm->rtm_flags & RTM_F_NOTIFY)
1576 rt->rt_flags |= RTCF_NOTIFY;
1577
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001578 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 -07001579
1580 if (err == 0)
1581 goto out_free;
1582 if (err < 0) {
1583 err = -EMSGSIZE;
1584 goto out_free;
1585 }
1586
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001587 return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589out_free:
1590 kfree_skb(skb);
1591 return err;
1592}
1593
1594/*
1595 * For routers, this is called from dn_fib_dump, but for endnodes its
1596 * called directly from the rtnetlink dispatch table.
1597 */
1598int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1599{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001600 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 struct dn_route *rt;
1602 int h, s_h;
1603 int idx, s_idx;
1604
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001605 if (!net_eq(net, &init_net))
Denis V. Lunevb8542722007-12-01 00:21:31 +11001606 return 0;
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
1609 return -EINVAL;
1610 if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
1611 return 0;
1612
1613 s_h = cb->args[0];
1614 s_idx = idx = cb->args[1];
1615 for(h = 0; h <= dn_rt_hash_mask; h++) {
1616 if (h < s_h)
1617 continue;
1618 if (h > s_h)
1619 s_idx = 0;
1620 rcu_read_lock_bh();
1621 for(rt = rcu_dereference(dn_rt_hash_table[h].chain), idx = 0;
1622 rt;
Eric Dumazet0c195c32007-02-09 16:25:52 -08001623 rt = rcu_dereference(rt->u.dst.dn_next), idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (idx < s_idx)
1625 continue;
Eric Dumazetadf30902009-06-02 05:19:30 +00001626 skb_dst_set(skb, dst_clone(&rt->u.dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001628 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07001629 1, NLM_F_MULTI) <= 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001630 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 rcu_read_unlock_bh();
1632 goto done;
1633 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001634 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636 rcu_read_unlock_bh();
1637 }
1638
1639done:
1640 cb->args[0] = h;
1641 cb->args[1] = idx;
1642 return skb->len;
1643}
1644
1645#ifdef CONFIG_PROC_FS
1646struct dn_rt_cache_iter_state {
1647 int bucket;
1648};
1649
1650static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1651{
1652 struct dn_route *rt = NULL;
1653 struct dn_rt_cache_iter_state *s = seq->private;
1654
1655 for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1656 rcu_read_lock_bh();
1657 rt = dn_rt_hash_table[s->bucket].chain;
1658 if (rt)
1659 break;
1660 rcu_read_unlock_bh();
1661 }
Eric Dumazet0d89d792008-01-10 22:35:21 -08001662 return rcu_dereference(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
1665static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1666{
Eric Dumazet0d89d792008-01-10 22:35:21 -08001667 struct dn_rt_cache_iter_state *s = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Eric Dumazet0c195c32007-02-09 16:25:52 -08001669 rt = rt->u.dst.dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 while(!rt) {
1671 rcu_read_unlock_bh();
1672 if (--s->bucket < 0)
1673 break;
1674 rcu_read_lock_bh();
1675 rt = dn_rt_hash_table[s->bucket].chain;
1676 }
Eric Dumazet0d89d792008-01-10 22:35:21 -08001677 return rcu_dereference(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
1680static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1681{
1682 struct dn_route *rt = dn_rt_cache_get_first(seq);
1683
1684 if (rt) {
1685 while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1686 --*pos;
1687 }
1688 return *pos ? NULL : rt;
1689}
1690
1691static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1692{
1693 struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1694 ++*pos;
1695 return rt;
1696}
1697
1698static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1699{
1700 if (v)
1701 rcu_read_unlock_bh();
1702}
1703
1704static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1705{
1706 struct dn_route *rt = v;
1707 char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1708
1709 seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
1710 rt->u.dst.dev ? rt->u.dst.dev->name : "*",
Harvey Harrisonc4106aa2008-11-27 00:12:47 -08001711 dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1712 dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 atomic_read(&rt->u.dst.__refcnt),
1714 rt->u.dst.__use,
1715 (int) dst_metric(&rt->u.dst, RTAX_RTT));
1716 return 0;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001719static const struct seq_operations dn_rt_cache_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 .start = dn_rt_cache_seq_start,
1721 .next = dn_rt_cache_seq_next,
1722 .stop = dn_rt_cache_seq_stop,
1723 .show = dn_rt_cache_seq_show,
1724};
1725
1726static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1727{
Pavel Emelyanov31164082007-10-10 02:30:23 -07001728 return seq_open_private(file, &dn_rt_cache_seq_ops,
1729 sizeof(struct dn_rt_cache_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Arjan van de Ven9a321442007-02-12 00:55:35 -08001732static const struct file_operations dn_rt_cache_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 .owner = THIS_MODULE,
1734 .open = dn_rt_cache_seq_open,
1735 .read = seq_read,
1736 .llseek = seq_lseek,
1737 .release = seq_release_private,
1738};
1739
1740#endif /* CONFIG_PROC_FS */
1741
1742void __init dn_route_init(void)
1743{
1744 int i, goal, order;
1745
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07001746 dn_dst_ops.kmem_cachep =
1747 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001748 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001749 setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1751 add_timer(&dn_route_timer);
1752
Jan Beulich44813742009-09-21 17:03:05 -07001753 goal = totalram_pages >> (26 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 for(order = 0; (1UL << order) < goal; order++)
1756 /* NOTHING */;
1757
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001758 /*
1759 * Only want 1024 entries max, since the table is very, very unlikely
1760 * to be larger than that.
1761 */
1762 while(order && ((((1UL << order) * PAGE_SIZE) /
1763 sizeof(struct dn_rt_hash_bucket)) >= 2048))
1764 order--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001766 do {
1767 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1768 sizeof(struct dn_rt_hash_bucket);
1769 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1770 dn_rt_hash_mask--;
1771 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1772 __get_free_pages(GFP_ATOMIC, order);
1773 } while (dn_rt_hash_table == NULL && --order > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 if (!dn_rt_hash_table)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001776 panic("Failed to allocate DECnet route cache hash table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001778 printk(KERN_INFO
1779 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1780 dn_rt_hash_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1782
1783 dn_rt_hash_mask--;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001784 for(i = 0; i <= dn_rt_hash_mask; i++) {
1785 spin_lock_init(&dn_rt_hash_table[i].lock);
1786 dn_rt_hash_table[i].chain = NULL;
1787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001789 dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001791 proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
Thomas Graffa34ddd2007-03-22 11:57:46 -07001792
1793#ifdef CONFIG_DECNET_ROUTER
1794 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, dn_fib_dump);
1795#else
1796 rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1797 dn_cache_dump);
1798#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
1801void __exit dn_route_cleanup(void)
1802{
1803 del_timer(&dn_route_timer);
1804 dn_run_flush(0);
1805
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001806 proc_net_remove(&init_net, "decnet_cache");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808