]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/decnet/dn_route.c
Merge branch 'for-davem' of git://git.kernel.org/pub/scm/linux/kernel/git/bwh/sfc...
[mv-sheeva.git] / net / decnet / dn_route.c
1 /*
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
46
47     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
58 #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 <linux/slab.h>
70 #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>
81 #include <net/net_namespace.h>
82 #include <net/netlink.h>
83 #include <net/neighbour.h>
84 #include <net/dst.h>
85 #include <net/flow.h>
86 #include <net/fib_rules.h>
87 #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
94 struct dn_rt_hash_bucket
95 {
96         struct dn_route __rcu *chain;
97         spinlock_t lock;
98 };
99
100 extern struct neigh_table dn_neigh_table;
101
102
103 static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};
104
105 static const int dn_rt_min_delay = 2 * HZ;
106 static const int dn_rt_max_delay = 10 * HZ;
107 static const int dn_rt_mtu_expires = 10 * 60 * HZ;
108
109 static unsigned long dn_rt_deadline;
110
111 static int dn_dst_gc(struct dst_ops *ops);
112 static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);
113 static unsigned int dn_dst_default_advmss(const struct dst_entry *dst);
114 static unsigned int dn_dst_default_mtu(const struct dst_entry *dst);
115 static void dn_dst_destroy(struct dst_entry *);
116 static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
117 static void dn_dst_link_failure(struct sk_buff *);
118 static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu);
119 static int dn_route_input(struct sk_buff *);
120 static void dn_run_flush(unsigned long dummy);
121
122 static struct dn_rt_hash_bucket *dn_rt_hash_table;
123 static unsigned dn_rt_hash_mask;
124
125 static struct timer_list dn_route_timer;
126 static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
127 int decnet_dst_gc_interval = 2;
128
129 static struct dst_ops dn_dst_ops = {
130         .family =               PF_DECnet,
131         .protocol =             cpu_to_be16(ETH_P_DNA_RT),
132         .gc_thresh =            128,
133         .gc =                   dn_dst_gc,
134         .check =                dn_dst_check,
135         .default_advmss =       dn_dst_default_advmss,
136         .default_mtu =          dn_dst_default_mtu,
137         .cow_metrics =          dst_cow_metrics_generic,
138         .destroy =              dn_dst_destroy,
139         .negative_advice =      dn_dst_negative_advice,
140         .link_failure =         dn_dst_link_failure,
141         .update_pmtu =          dn_dst_update_pmtu,
142 };
143
144 static void dn_dst_destroy(struct dst_entry *dst)
145 {
146         dst_destroy_metrics_generic(dst);
147 }
148
149 static __inline__ unsigned dn_hash(__le16 src, __le16 dst)
150 {
151         __u16 tmp = (__u16 __force)(src ^ dst);
152         tmp ^= (tmp >> 3);
153         tmp ^= (tmp >> 5);
154         tmp ^= (tmp >> 10);
155         return dn_rt_hash_mask & (unsigned)tmp;
156 }
157
158 static inline void dnrt_free(struct dn_route *rt)
159 {
160         call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
161 }
162
163 static inline void dnrt_drop(struct dn_route *rt)
164 {
165         dst_release(&rt->dst);
166         call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
167 }
168
169 static void dn_dst_check_expire(unsigned long dummy)
170 {
171         int i;
172         struct dn_route *rt;
173         struct dn_route __rcu **rtp;
174         unsigned long now = jiffies;
175         unsigned long expire = 120 * HZ;
176
177         for (i = 0; i <= dn_rt_hash_mask; i++) {
178                 rtp = &dn_rt_hash_table[i].chain;
179
180                 spin_lock(&dn_rt_hash_table[i].lock);
181                 while ((rt = rcu_dereference_protected(*rtp,
182                                                 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
183                         if (atomic_read(&rt->dst.__refcnt) ||
184                                         (now - rt->dst.lastuse) < expire) {
185                                 rtp = &rt->dst.dn_next;
186                                 continue;
187                         }
188                         *rtp = rt->dst.dn_next;
189                         rt->dst.dn_next = NULL;
190                         dnrt_free(rt);
191                 }
192                 spin_unlock(&dn_rt_hash_table[i].lock);
193
194                 if ((jiffies - now) > 0)
195                         break;
196         }
197
198         mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
199 }
200
201 static int dn_dst_gc(struct dst_ops *ops)
202 {
203         struct dn_route *rt;
204         struct dn_route __rcu **rtp;
205         int i;
206         unsigned long now = jiffies;
207         unsigned long expire = 10 * HZ;
208
209         for (i = 0; i <= dn_rt_hash_mask; i++) {
210
211                 spin_lock_bh(&dn_rt_hash_table[i].lock);
212                 rtp = &dn_rt_hash_table[i].chain;
213
214                 while ((rt = rcu_dereference_protected(*rtp,
215                                                 lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
216                         if (atomic_read(&rt->dst.__refcnt) ||
217                                         (now - rt->dst.lastuse) < expire) {
218                                 rtp = &rt->dst.dn_next;
219                                 continue;
220                         }
221                         *rtp = rt->dst.dn_next;
222                         rt->dst.dn_next = NULL;
223                         dnrt_drop(rt);
224                         break;
225                 }
226                 spin_unlock_bh(&dn_rt_hash_table[i].lock);
227         }
228
229         return 0;
230 }
231
232 /*
233  * The decnet standards don't impose a particular minimum mtu, what they
234  * do insist on is that the routing layer accepts a datagram of at least
235  * 230 bytes long. Here we have to subtract the routing header length from
236  * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
237  * assume the worst and use a long header size.
238  *
239  * We update both the mtu and the advertised mss (i.e. the segment size we
240  * advertise to the other end).
241  */
242 static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu)
243 {
244         u32 min_mtu = 230;
245         struct dn_dev *dn = dst->neighbour ?
246                             rcu_dereference_raw(dst->neighbour->dev->dn_ptr) : NULL;
247
248         if (dn && dn->use_long == 0)
249                 min_mtu -= 6;
250         else
251                 min_mtu -= 21;
252
253         if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
254                 if (!(dst_metric_locked(dst, RTAX_MTU))) {
255                         dst_metric_set(dst, RTAX_MTU, mtu);
256                         dst_set_expires(dst, dn_rt_mtu_expires);
257                 }
258                 if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
259                         u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
260                         u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
261                         if (!existing_mss || existing_mss > mss)
262                                 dst_metric_set(dst, RTAX_ADVMSS, mss);
263                 }
264         }
265 }
266
267 /*
268  * When a route has been marked obsolete. (e.g. routing cache flush)
269  */
270 static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
271 {
272         return NULL;
273 }
274
275 static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
276 {
277         dst_release(dst);
278         return NULL;
279 }
280
281 static void dn_dst_link_failure(struct sk_buff *skb)
282 {
283 }
284
285 static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
286 {
287         return ((fl1->fld_dst ^ fl2->fld_dst) |
288                 (fl1->fld_src ^ fl2->fld_src) |
289                 (fl1->mark ^ fl2->mark) |
290                 (fl1->fld_scope ^ fl2->fld_scope) |
291                 (fl1->oif ^ fl2->oif) |
292                 (fl1->iif ^ fl2->iif)) == 0;
293 }
294
295 static int dn_insert_route(struct dn_route *rt, unsigned hash, struct dn_route **rp)
296 {
297         struct dn_route *rth;
298         struct dn_route __rcu **rthp;
299         unsigned long now = jiffies;
300
301         rthp = &dn_rt_hash_table[hash].chain;
302
303         spin_lock_bh(&dn_rt_hash_table[hash].lock);
304         while ((rth = rcu_dereference_protected(*rthp,
305                                                 lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
306                 if (compare_keys(&rth->fl, &rt->fl)) {
307                         /* Put it first */
308                         *rthp = rth->dst.dn_next;
309                         rcu_assign_pointer(rth->dst.dn_next,
310                                            dn_rt_hash_table[hash].chain);
311                         rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
312
313                         dst_use(&rth->dst, now);
314                         spin_unlock_bh(&dn_rt_hash_table[hash].lock);
315
316                         dnrt_drop(rt);
317                         *rp = rth;
318                         return 0;
319                 }
320                 rthp = &rth->dst.dn_next;
321         }
322
323         rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
324         rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
325
326         dst_use(&rt->dst, now);
327         spin_unlock_bh(&dn_rt_hash_table[hash].lock);
328         *rp = rt;
329         return 0;
330 }
331
332 static void dn_run_flush(unsigned long dummy)
333 {
334         int i;
335         struct dn_route *rt, *next;
336
337         for (i = 0; i < dn_rt_hash_mask; i++) {
338                 spin_lock_bh(&dn_rt_hash_table[i].lock);
339
340                 if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
341                         goto nothing_to_declare;
342
343                 for(; rt; rt = next) {
344                         next = rcu_dereference_raw(rt->dst.dn_next);
345                         RCU_INIT_POINTER(rt->dst.dn_next, NULL);
346                         dst_free((struct dst_entry *)rt);
347                 }
348
349 nothing_to_declare:
350                 spin_unlock_bh(&dn_rt_hash_table[i].lock);
351         }
352 }
353
354 static DEFINE_SPINLOCK(dn_rt_flush_lock);
355
356 void dn_rt_cache_flush(int delay)
357 {
358         unsigned long now = jiffies;
359         int user_mode = !in_interrupt();
360
361         if (delay < 0)
362                 delay = dn_rt_min_delay;
363
364         spin_lock_bh(&dn_rt_flush_lock);
365
366         if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
367                 long tmo = (long)(dn_rt_deadline - now);
368
369                 if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
370                         tmo = 0;
371
372                 if (delay > tmo)
373                         delay = tmo;
374         }
375
376         if (delay <= 0) {
377                 spin_unlock_bh(&dn_rt_flush_lock);
378                 dn_run_flush(0);
379                 return;
380         }
381
382         if (dn_rt_deadline == 0)
383                 dn_rt_deadline = now + dn_rt_max_delay;
384
385         dn_rt_flush_timer.expires = now + delay;
386         add_timer(&dn_rt_flush_timer);
387         spin_unlock_bh(&dn_rt_flush_lock);
388 }
389
390 /**
391  * dn_return_short - Return a short packet to its sender
392  * @skb: The packet to return
393  *
394  */
395 static int dn_return_short(struct sk_buff *skb)
396 {
397         struct dn_skb_cb *cb;
398         unsigned char *ptr;
399         __le16 *src;
400         __le16 *dst;
401
402         /* Add back headers */
403         skb_push(skb, skb->data - skb_network_header(skb));
404
405         if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
406                 return NET_RX_DROP;
407
408         cb = DN_SKB_CB(skb);
409         /* Skip packet length and point to flags */
410         ptr = skb->data + 2;
411         *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
412
413         dst = (__le16 *)ptr;
414         ptr += 2;
415         src = (__le16 *)ptr;
416         ptr += 2;
417         *ptr = 0; /* Zero hop count */
418
419         swap(*src, *dst);
420
421         skb->pkt_type = PACKET_OUTGOING;
422         dn_rt_finish_output(skb, NULL, NULL);
423         return NET_RX_SUCCESS;
424 }
425
426 /**
427  * dn_return_long - Return a long packet to its sender
428  * @skb: The long format packet to return
429  *
430  */
431 static int dn_return_long(struct sk_buff *skb)
432 {
433         struct dn_skb_cb *cb;
434         unsigned char *ptr;
435         unsigned char *src_addr, *dst_addr;
436         unsigned char tmp[ETH_ALEN];
437
438         /* Add back all headers */
439         skb_push(skb, skb->data - skb_network_header(skb));
440
441         if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
442                 return NET_RX_DROP;
443
444         cb = DN_SKB_CB(skb);
445         /* Ignore packet length and point to flags */
446         ptr = skb->data + 2;
447
448         /* Skip padding */
449         if (*ptr & DN_RT_F_PF) {
450                 char padlen = (*ptr & ~DN_RT_F_PF);
451                 ptr += padlen;
452         }
453
454         *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
455         ptr += 2;
456         dst_addr = ptr;
457         ptr += 8;
458         src_addr = ptr;
459         ptr += 6;
460         *ptr = 0; /* Zero hop count */
461
462         /* Swap source and destination */
463         memcpy(tmp, src_addr, ETH_ALEN);
464         memcpy(src_addr, dst_addr, ETH_ALEN);
465         memcpy(dst_addr, tmp, ETH_ALEN);
466
467         skb->pkt_type = PACKET_OUTGOING;
468         dn_rt_finish_output(skb, dst_addr, src_addr);
469         return NET_RX_SUCCESS;
470 }
471
472 /**
473  * dn_route_rx_packet - Try and find a route for an incoming packet
474  * @skb: The packet to find a route for
475  *
476  * Returns: result of input function if route is found, error code otherwise
477  */
478 static int dn_route_rx_packet(struct sk_buff *skb)
479 {
480         struct dn_skb_cb *cb;
481         int err;
482
483         if ((err = dn_route_input(skb)) == 0)
484                 return dst_input(skb);
485
486         cb = DN_SKB_CB(skb);
487         if (decnet_debug_level & 4) {
488                 char *devname = skb->dev ? skb->dev->name : "???";
489
490                 printk(KERN_DEBUG
491                         "DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%d\n",
492                         (int)cb->rt_flags, devname, skb->len,
493                         le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
494                         err, skb->pkt_type);
495         }
496
497         if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
498                 switch(cb->rt_flags & DN_RT_PKT_MSK) {
499                         case DN_RT_PKT_SHORT:
500                                 return dn_return_short(skb);
501                         case DN_RT_PKT_LONG:
502                                 return dn_return_long(skb);
503                 }
504         }
505
506         kfree_skb(skb);
507         return NET_RX_DROP;
508 }
509
510 static int dn_route_rx_long(struct sk_buff *skb)
511 {
512         struct dn_skb_cb *cb = DN_SKB_CB(skb);
513         unsigned char *ptr = skb->data;
514
515         if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
516                 goto drop_it;
517
518         skb_pull(skb, 20);
519         skb_reset_transport_header(skb);
520
521         /* Destination info */
522         ptr += 2;
523         cb->dst = dn_eth2dn(ptr);
524         if (memcmp(ptr, dn_hiord_addr, 4) != 0)
525                 goto drop_it;
526         ptr += 6;
527
528
529         /* Source info */
530         ptr += 2;
531         cb->src = dn_eth2dn(ptr);
532         if (memcmp(ptr, dn_hiord_addr, 4) != 0)
533                 goto drop_it;
534         ptr += 6;
535         /* Other junk */
536         ptr++;
537         cb->hops = *ptr++; /* Visit Count */
538
539         return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
540                        dn_route_rx_packet);
541
542 drop_it:
543         kfree_skb(skb);
544         return NET_RX_DROP;
545 }
546
547
548
549 static int dn_route_rx_short(struct sk_buff *skb)
550 {
551         struct dn_skb_cb *cb = DN_SKB_CB(skb);
552         unsigned char *ptr = skb->data;
553
554         if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
555                 goto drop_it;
556
557         skb_pull(skb, 5);
558         skb_reset_transport_header(skb);
559
560         cb->dst = *(__le16 *)ptr;
561         ptr += 2;
562         cb->src = *(__le16 *)ptr;
563         ptr += 2;
564         cb->hops = *ptr & 0x3f;
565
566         return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
567                        dn_route_rx_packet);
568
569 drop_it:
570         kfree_skb(skb);
571         return NET_RX_DROP;
572 }
573
574 static int dn_route_discard(struct sk_buff *skb)
575 {
576         /*
577          * I know we drop the packet here, but thats considered success in
578          * this case
579          */
580         kfree_skb(skb);
581         return NET_RX_SUCCESS;
582 }
583
584 static int dn_route_ptp_hello(struct sk_buff *skb)
585 {
586         dn_dev_hello(skb);
587         dn_neigh_pointopoint_hello(skb);
588         return NET_RX_SUCCESS;
589 }
590
591 int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
592 {
593         struct dn_skb_cb *cb;
594         unsigned char flags = 0;
595         __u16 len = le16_to_cpu(*(__le16 *)skb->data);
596         struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
597         unsigned char padlen = 0;
598
599         if (!net_eq(dev_net(dev), &init_net))
600                 goto dump_it;
601
602         if (dn == NULL)
603                 goto dump_it;
604
605         if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
606                 goto out;
607
608         if (!pskb_may_pull(skb, 3))
609                 goto dump_it;
610
611         skb_pull(skb, 2);
612
613         if (len > skb->len)
614                 goto dump_it;
615
616         skb_trim(skb, len);
617
618         flags = *skb->data;
619
620         cb = DN_SKB_CB(skb);
621         cb->stamp = jiffies;
622         cb->iif = dev->ifindex;
623
624         /*
625          * If we have padding, remove it.
626          */
627         if (flags & DN_RT_F_PF) {
628                 padlen = flags & ~DN_RT_F_PF;
629                 if (!pskb_may_pull(skb, padlen + 1))
630                         goto dump_it;
631                 skb_pull(skb, padlen);
632                 flags = *skb->data;
633         }
634
635         skb_reset_network_header(skb);
636
637         /*
638          * Weed out future version DECnet
639          */
640         if (flags & DN_RT_F_VER)
641                 goto dump_it;
642
643         cb->rt_flags = flags;
644
645         if (decnet_debug_level & 1)
646                 printk(KERN_DEBUG
647                         "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
648                         (int)flags, (dev) ? dev->name : "???", len, skb->len,
649                         padlen);
650
651         if (flags & DN_RT_PKT_CNTL) {
652                 if (unlikely(skb_linearize(skb)))
653                         goto dump_it;
654
655                 switch(flags & DN_RT_CNTL_MSK) {
656                         case DN_RT_PKT_INIT:
657                                 dn_dev_init_pkt(skb);
658                                 break;
659                         case DN_RT_PKT_VERI:
660                                 dn_dev_veri_pkt(skb);
661                                 break;
662                 }
663
664                 if (dn->parms.state != DN_DEV_S_RU)
665                         goto dump_it;
666
667                 switch(flags & DN_RT_CNTL_MSK) {
668                         case DN_RT_PKT_HELO:
669                                 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
670                                                skb, skb->dev, NULL,
671                                                dn_route_ptp_hello);
672
673                         case DN_RT_PKT_L1RT:
674                         case DN_RT_PKT_L2RT:
675                                 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
676                                                skb, skb->dev, NULL,
677                                                dn_route_discard);
678                         case DN_RT_PKT_ERTH:
679                                 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
680                                                skb, skb->dev, NULL,
681                                                dn_neigh_router_hello);
682
683                         case DN_RT_PKT_EEDH:
684                                 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
685                                                skb, skb->dev, NULL,
686                                                dn_neigh_endnode_hello);
687                 }
688         } else {
689                 if (dn->parms.state != DN_DEV_S_RU)
690                         goto dump_it;
691
692                 skb_pull(skb, 1); /* Pull flags */
693
694                 switch(flags & DN_RT_PKT_MSK) {
695                         case DN_RT_PKT_LONG:
696                                 return dn_route_rx_long(skb);
697                         case DN_RT_PKT_SHORT:
698                                 return dn_route_rx_short(skb);
699                 }
700         }
701
702 dump_it:
703         kfree_skb(skb);
704 out:
705         return NET_RX_DROP;
706 }
707
708 static int dn_output(struct sk_buff *skb)
709 {
710         struct dst_entry *dst = skb_dst(skb);
711         struct dn_route *rt = (struct dn_route *)dst;
712         struct net_device *dev = dst->dev;
713         struct dn_skb_cb *cb = DN_SKB_CB(skb);
714         struct neighbour *neigh;
715
716         int err = -EINVAL;
717
718         if ((neigh = dst->neighbour) == NULL)
719                 goto error;
720
721         skb->dev = dev;
722
723         cb->src = rt->rt_saddr;
724         cb->dst = rt->rt_daddr;
725
726         /*
727          * Always set the Intra-Ethernet bit on all outgoing packets
728          * originated on this node. Only valid flag from upper layers
729          * is return-to-sender-requested. Set hop count to 0 too.
730          */
731         cb->rt_flags &= ~DN_RT_F_RQR;
732         cb->rt_flags |= DN_RT_F_IE;
733         cb->hops = 0;
734
735         return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
736                        neigh->output);
737
738 error:
739         if (net_ratelimit())
740                 printk(KERN_DEBUG "dn_output: This should not happen\n");
741
742         kfree_skb(skb);
743
744         return err;
745 }
746
747 static int dn_forward(struct sk_buff *skb)
748 {
749         struct dn_skb_cb *cb = DN_SKB_CB(skb);
750         struct dst_entry *dst = skb_dst(skb);
751         struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
752         struct dn_route *rt;
753         struct neighbour *neigh = dst->neighbour;
754         int header_len;
755 #ifdef CONFIG_NETFILTER
756         struct net_device *dev = skb->dev;
757 #endif
758
759         if (skb->pkt_type != PACKET_HOST)
760                 goto drop;
761
762         /* Ensure that we have enough space for headers */
763         rt = (struct dn_route *)skb_dst(skb);
764         header_len = dn_db->use_long ? 21 : 6;
765         if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
766                 goto drop;
767
768         /*
769          * Hop count exceeded.
770          */
771         if (++cb->hops > 30)
772                 goto drop;
773
774         skb->dev = rt->dst.dev;
775
776         /*
777          * If packet goes out same interface it came in on, then set
778          * the Intra-Ethernet bit. This has no effect for short
779          * packets, so we don't need to test for them here.
780          */
781         cb->rt_flags &= ~DN_RT_F_IE;
782         if (rt->rt_flags & RTCF_DOREDIRECT)
783                 cb->rt_flags |= DN_RT_F_IE;
784
785         return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
786                        neigh->output);
787
788 drop:
789         kfree_skb(skb);
790         return NET_RX_DROP;
791 }
792
793 /*
794  * Used to catch bugs. This should never normally get
795  * called.
796  */
797 static int dn_rt_bug(struct sk_buff *skb)
798 {
799         if (net_ratelimit()) {
800                 struct dn_skb_cb *cb = DN_SKB_CB(skb);
801
802                 printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
803                                 le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
804         }
805
806         kfree_skb(skb);
807
808         return NET_RX_DROP;
809 }
810
811 static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
812 {
813         return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
814 }
815
816 static unsigned int dn_dst_default_mtu(const struct dst_entry *dst)
817 {
818         return dst->dev->mtu;
819 }
820
821 static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
822 {
823         struct dn_fib_info *fi = res->fi;
824         struct net_device *dev = rt->dst.dev;
825         unsigned int mss_metric;
826         struct neighbour *n;
827
828         if (fi) {
829                 if (DN_FIB_RES_GW(*res) &&
830                     DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
831                         rt->rt_gateway = DN_FIB_RES_GW(*res);
832                 dst_init_metrics(&rt->dst, fi->fib_metrics, true);
833         }
834         rt->rt_type = res->type;
835
836         if (dev != NULL && rt->dst.neighbour == NULL) {
837                 n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
838                 if (IS_ERR(n))
839                         return PTR_ERR(n);
840                 rt->dst.neighbour = n;
841         }
842
843         if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
844                 dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
845         mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
846         if (mss_metric) {
847                 unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
848                 if (mss_metric > mss)
849                         dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
850         }
851         return 0;
852 }
853
854 static inline int dn_match_addr(__le16 addr1, __le16 addr2)
855 {
856         __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
857         int match = 16;
858         while(tmp) {
859                 tmp >>= 1;
860                 match--;
861         }
862         return match;
863 }
864
865 static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
866 {
867         __le16 saddr = 0;
868         struct dn_dev *dn_db;
869         struct dn_ifaddr *ifa;
870         int best_match = 0;
871         int ret;
872
873         rcu_read_lock();
874         dn_db = rcu_dereference(dev->dn_ptr);
875         for (ifa = rcu_dereference(dn_db->ifa_list);
876              ifa != NULL;
877              ifa = rcu_dereference(ifa->ifa_next)) {
878                 if (ifa->ifa_scope > scope)
879                         continue;
880                 if (!daddr) {
881                         saddr = ifa->ifa_local;
882                         break;
883                 }
884                 ret = dn_match_addr(daddr, ifa->ifa_local);
885                 if (ret > best_match)
886                         saddr = ifa->ifa_local;
887                 if (best_match == 0)
888                         saddr = ifa->ifa_local;
889         }
890         rcu_read_unlock();
891
892         return saddr;
893 }
894
895 static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
896 {
897         return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
898 }
899
900 static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
901 {
902         __le16 mask = dnet_make_mask(res->prefixlen);
903         return (daddr&~mask)|res->fi->fib_nh->nh_gw;
904 }
905
906 static int dn_route_output_slow(struct dst_entry **pprt, const struct flowi *oldflp, int try_hard)
907 {
908         struct flowi fl = { .fld_dst = oldflp->fld_dst,
909                             .fld_src = oldflp->fld_src,
910                             .fld_scope = RT_SCOPE_UNIVERSE,
911                             .mark = oldflp->mark,
912                             .iif = init_net.loopback_dev->ifindex,
913                             .oif = oldflp->oif };
914         struct dn_route *rt = NULL;
915         struct net_device *dev_out = NULL, *dev;
916         struct neighbour *neigh = NULL;
917         unsigned hash;
918         unsigned flags = 0;
919         struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
920         int err;
921         int free_res = 0;
922         __le16 gateway = 0;
923
924         if (decnet_debug_level & 16)
925                 printk(KERN_DEBUG
926                        "dn_route_output_slow: dst=%04x src=%04x mark=%d"
927                        " iif=%d oif=%d\n", le16_to_cpu(oldflp->fld_dst),
928                        le16_to_cpu(oldflp->fld_src),
929                        oldflp->mark, init_net.loopback_dev->ifindex, oldflp->oif);
930
931         /* If we have an output interface, verify its a DECnet device */
932         if (oldflp->oif) {
933                 dev_out = dev_get_by_index(&init_net, oldflp->oif);
934                 err = -ENODEV;
935                 if (dev_out && dev_out->dn_ptr == NULL) {
936                         dev_put(dev_out);
937                         dev_out = NULL;
938                 }
939                 if (dev_out == NULL)
940                         goto out;
941         }
942
943         /* If we have a source address, verify that its a local address */
944         if (oldflp->fld_src) {
945                 err = -EADDRNOTAVAIL;
946
947                 if (dev_out) {
948                         if (dn_dev_islocal(dev_out, oldflp->fld_src))
949                                 goto source_ok;
950                         dev_put(dev_out);
951                         goto out;
952                 }
953                 rcu_read_lock();
954                 for_each_netdev_rcu(&init_net, dev) {
955                         if (!dev->dn_ptr)
956                                 continue;
957                         if (!dn_dev_islocal(dev, oldflp->fld_src))
958                                 continue;
959                         if ((dev->flags & IFF_LOOPBACK) &&
960                             oldflp->fld_dst &&
961                             !dn_dev_islocal(dev, oldflp->fld_dst))
962                                 continue;
963
964                         dev_out = dev;
965                         break;
966                 }
967                 rcu_read_unlock();
968                 if (dev_out == NULL)
969                         goto out;
970                 dev_hold(dev_out);
971 source_ok:
972                 ;
973         }
974
975         /* No destination? Assume its local */
976         if (!fl.fld_dst) {
977                 fl.fld_dst = fl.fld_src;
978
979                 err = -EADDRNOTAVAIL;
980                 if (dev_out)
981                         dev_put(dev_out);
982                 dev_out = init_net.loopback_dev;
983                 dev_hold(dev_out);
984                 if (!fl.fld_dst) {
985                         fl.fld_dst =
986                         fl.fld_src = dnet_select_source(dev_out, 0,
987                                                        RT_SCOPE_HOST);
988                         if (!fl.fld_dst)
989                                 goto out;
990                 }
991                 fl.oif = init_net.loopback_dev->ifindex;
992                 res.type = RTN_LOCAL;
993                 goto make_route;
994         }
995
996         if (decnet_debug_level & 16)
997                 printk(KERN_DEBUG
998                        "dn_route_output_slow: initial checks complete."
999                        " dst=%o4x src=%04x oif=%d try_hard=%d\n",
1000                        le16_to_cpu(fl.fld_dst), le16_to_cpu(fl.fld_src),
1001                        fl.oif, try_hard);
1002
1003         /*
1004          * N.B. If the kernel is compiled without router support then
1005          * dn_fib_lookup() will evaluate to non-zero so this if () block
1006          * will always be executed.
1007          */
1008         err = -ESRCH;
1009         if (try_hard || (err = dn_fib_lookup(&fl, &res)) != 0) {
1010                 struct dn_dev *dn_db;
1011                 if (err != -ESRCH)
1012                         goto out;
1013                 /*
1014                  * Here the fallback is basically the standard algorithm for
1015                  * routing in endnodes which is described in the DECnet routing
1016                  * docs
1017                  *
1018                  * If we are not trying hard, look in neighbour cache.
1019                  * The result is tested to ensure that if a specific output
1020                  * device/source address was requested, then we honour that
1021                  * here
1022                  */
1023                 if (!try_hard) {
1024                         neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fl.fld_dst);
1025                         if (neigh) {
1026                                 if ((oldflp->oif &&
1027                                     (neigh->dev->ifindex != oldflp->oif)) ||
1028                                     (oldflp->fld_src &&
1029                                     (!dn_dev_islocal(neigh->dev,
1030                                                       oldflp->fld_src)))) {
1031                                         neigh_release(neigh);
1032                                         neigh = NULL;
1033                                 } else {
1034                                         if (dev_out)
1035                                                 dev_put(dev_out);
1036                                         if (dn_dev_islocal(neigh->dev, fl.fld_dst)) {
1037                                                 dev_out = init_net.loopback_dev;
1038                                                 res.type = RTN_LOCAL;
1039                                         } else {
1040                                                 dev_out = neigh->dev;
1041                                         }
1042                                         dev_hold(dev_out);
1043                                         goto select_source;
1044                                 }
1045                         }
1046                 }
1047
1048                 /* Not there? Perhaps its a local address */
1049                 if (dev_out == NULL)
1050                         dev_out = dn_dev_get_default();
1051                 err = -ENODEV;
1052                 if (dev_out == NULL)
1053                         goto out;
1054                 dn_db = rcu_dereference_raw(dev_out->dn_ptr);
1055                 /* Possible improvement - check all devices for local addr */
1056                 if (dn_dev_islocal(dev_out, fl.fld_dst)) {
1057                         dev_put(dev_out);
1058                         dev_out = init_net.loopback_dev;
1059                         dev_hold(dev_out);
1060                         res.type = RTN_LOCAL;
1061                         goto select_source;
1062                 }
1063                 /* Not local either.... try sending it to the default router */
1064                 neigh = neigh_clone(dn_db->router);
1065                 BUG_ON(neigh && neigh->dev != dev_out);
1066
1067                 /* Ok then, we assume its directly connected and move on */
1068 select_source:
1069                 if (neigh)
1070                         gateway = ((struct dn_neigh *)neigh)->addr;
1071                 if (gateway == 0)
1072                         gateway = fl.fld_dst;
1073                 if (fl.fld_src == 0) {
1074                         fl.fld_src = dnet_select_source(dev_out, gateway,
1075                                                          res.type == RTN_LOCAL ?
1076                                                          RT_SCOPE_HOST :
1077                                                          RT_SCOPE_LINK);
1078                         if (fl.fld_src == 0 && res.type != RTN_LOCAL)
1079                                 goto e_addr;
1080                 }
1081                 fl.oif = dev_out->ifindex;
1082                 goto make_route;
1083         }
1084         free_res = 1;
1085
1086         if (res.type == RTN_NAT)
1087                 goto e_inval;
1088
1089         if (res.type == RTN_LOCAL) {
1090                 if (!fl.fld_src)
1091                         fl.fld_src = fl.fld_dst;
1092                 if (dev_out)
1093                         dev_put(dev_out);
1094                 dev_out = init_net.loopback_dev;
1095                 dev_hold(dev_out);
1096                 fl.oif = dev_out->ifindex;
1097                 if (res.fi)
1098                         dn_fib_info_put(res.fi);
1099                 res.fi = NULL;
1100                 goto make_route;
1101         }
1102
1103         if (res.fi->fib_nhs > 1 && fl.oif == 0)
1104                 dn_fib_select_multipath(&fl, &res);
1105
1106         /*
1107          * We could add some logic to deal with default routes here and
1108          * get rid of some of the special casing above.
1109          */
1110
1111         if (!fl.fld_src)
1112                 fl.fld_src = DN_FIB_RES_PREFSRC(res);
1113
1114         if (dev_out)
1115                 dev_put(dev_out);
1116         dev_out = DN_FIB_RES_DEV(res);
1117         dev_hold(dev_out);
1118         fl.oif = dev_out->ifindex;
1119         gateway = DN_FIB_RES_GW(res);
1120
1121 make_route:
1122         if (dev_out->flags & IFF_LOOPBACK)
1123                 flags |= RTCF_LOCAL;
1124
1125         rt = dst_alloc(&dn_dst_ops, 0);
1126         if (rt == NULL)
1127                 goto e_nobufs;
1128
1129         atomic_set(&rt->dst.__refcnt, 1);
1130         rt->dst.flags   = DST_HOST;
1131
1132         rt->fl.fld_src    = oldflp->fld_src;
1133         rt->fl.fld_dst    = oldflp->fld_dst;
1134         rt->fl.oif        = oldflp->oif;
1135         rt->fl.iif        = 0;
1136         rt->fl.mark       = oldflp->mark;
1137
1138         rt->rt_saddr      = fl.fld_src;
1139         rt->rt_daddr      = fl.fld_dst;
1140         rt->rt_gateway    = gateway ? gateway : fl.fld_dst;
1141         rt->rt_local_src  = fl.fld_src;
1142
1143         rt->rt_dst_map    = fl.fld_dst;
1144         rt->rt_src_map    = fl.fld_src;
1145
1146         rt->dst.dev = dev_out;
1147         dev_hold(dev_out);
1148         rt->dst.neighbour = neigh;
1149         neigh = NULL;
1150
1151         rt->dst.lastuse = jiffies;
1152         rt->dst.output  = dn_output;
1153         rt->dst.input   = dn_rt_bug;
1154         rt->rt_flags      = flags;
1155         if (flags & RTCF_LOCAL)
1156                 rt->dst.input = dn_nsp_rx;
1157
1158         err = dn_rt_set_next_hop(rt, &res);
1159         if (err)
1160                 goto e_neighbour;
1161
1162         hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst);
1163         dn_insert_route(rt, hash, (struct dn_route **)pprt);
1164
1165 done:
1166         if (neigh)
1167                 neigh_release(neigh);
1168         if (free_res)
1169                 dn_fib_res_put(&res);
1170         if (dev_out)
1171                 dev_put(dev_out);
1172 out:
1173         return err;
1174
1175 e_addr:
1176         err = -EADDRNOTAVAIL;
1177         goto done;
1178 e_inval:
1179         err = -EINVAL;
1180         goto done;
1181 e_nobufs:
1182         err = -ENOBUFS;
1183         goto done;
1184 e_neighbour:
1185         dst_free(&rt->dst);
1186         goto e_nobufs;
1187 }
1188
1189
1190 /*
1191  * N.B. The flags may be moved into the flowi at some future stage.
1192  */
1193 static int __dn_route_output_key(struct dst_entry **pprt, const struct flowi *flp, int flags)
1194 {
1195         unsigned hash = dn_hash(flp->fld_src, flp->fld_dst);
1196         struct dn_route *rt = NULL;
1197
1198         if (!(flags & MSG_TRYHARD)) {
1199                 rcu_read_lock_bh();
1200                 for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
1201                         rt = rcu_dereference_bh(rt->dst.dn_next)) {
1202                         if ((flp->fld_dst == rt->fl.fld_dst) &&
1203                             (flp->fld_src == rt->fl.fld_src) &&
1204                             (flp->mark == rt->fl.mark) &&
1205                             dn_is_output_route(rt) &&
1206                             (rt->fl.oif == flp->oif)) {
1207                                 dst_use(&rt->dst, jiffies);
1208                                 rcu_read_unlock_bh();
1209                                 *pprt = &rt->dst;
1210                                 return 0;
1211                         }
1212                 }
1213                 rcu_read_unlock_bh();
1214         }
1215
1216         return dn_route_output_slow(pprt, flp, flags);
1217 }
1218
1219 static int dn_route_output_key(struct dst_entry **pprt, struct flowi *flp, int flags)
1220 {
1221         int err;
1222
1223         err = __dn_route_output_key(pprt, flp, flags);
1224         if (err == 0 && flp->proto) {
1225                 err = xfrm_lookup(&init_net, pprt, flp, NULL, 0);
1226         }
1227         return err;
1228 }
1229
1230 int dn_route_output_sock(struct dst_entry **pprt, struct flowi *fl, struct sock *sk, int flags)
1231 {
1232         int err;
1233
1234         err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
1235         if (err == 0 && fl->proto) {
1236                 err = xfrm_lookup(&init_net, pprt, fl, sk,
1237                                  (flags & MSG_DONTWAIT) ? 0 : XFRM_LOOKUP_WAIT);
1238         }
1239         return err;
1240 }
1241
1242 static int dn_route_input_slow(struct sk_buff *skb)
1243 {
1244         struct dn_route *rt = NULL;
1245         struct dn_skb_cb *cb = DN_SKB_CB(skb);
1246         struct net_device *in_dev = skb->dev;
1247         struct net_device *out_dev = NULL;
1248         struct dn_dev *dn_db;
1249         struct neighbour *neigh = NULL;
1250         unsigned hash;
1251         int flags = 0;
1252         __le16 gateway = 0;
1253         __le16 local_src = 0;
1254         struct flowi fl = { .fld_dst = cb->dst,
1255                             .fld_src = cb->src,
1256                             .fld_scope = RT_SCOPE_UNIVERSE,
1257                             .mark = skb->mark,
1258                             .iif = skb->dev->ifindex };
1259         struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
1260         int err = -EINVAL;
1261         int free_res = 0;
1262
1263         dev_hold(in_dev);
1264
1265         if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
1266                 goto out;
1267
1268         /* Zero source addresses are not allowed */
1269         if (fl.fld_src == 0)
1270                 goto out;
1271
1272         /*
1273          * In this case we've just received a packet from a source
1274          * outside ourselves pretending to come from us. We don't
1275          * allow it any further to prevent routing loops, spoofing and
1276          * other nasties. Loopback packets already have the dst attached
1277          * so this only affects packets which have originated elsewhere.
1278          */
1279         err  = -ENOTUNIQ;
1280         if (dn_dev_islocal(in_dev, cb->src))
1281                 goto out;
1282
1283         err = dn_fib_lookup(&fl, &res);
1284         if (err) {
1285                 if (err != -ESRCH)
1286                         goto out;
1287                 /*
1288                  * Is the destination us ?
1289                  */
1290                 if (!dn_dev_islocal(in_dev, cb->dst))
1291                         goto e_inval;
1292
1293                 res.type = RTN_LOCAL;
1294         } else {
1295                 __le16 src_map = fl.fld_src;
1296                 free_res = 1;
1297
1298                 out_dev = DN_FIB_RES_DEV(res);
1299                 if (out_dev == NULL) {
1300                         if (net_ratelimit())
1301                                 printk(KERN_CRIT "Bug in dn_route_input_slow() "
1302                                                  "No output device\n");
1303                         goto e_inval;
1304                 }
1305                 dev_hold(out_dev);
1306
1307                 if (res.r)
1308                         src_map = fl.fld_src; /* no NAT support for now */
1309
1310                 gateway = DN_FIB_RES_GW(res);
1311                 if (res.type == RTN_NAT) {
1312                         fl.fld_dst = dn_fib_rules_map_destination(fl.fld_dst, &res);
1313                         dn_fib_res_put(&res);
1314                         free_res = 0;
1315                         if (dn_fib_lookup(&fl, &res))
1316                                 goto e_inval;
1317                         free_res = 1;
1318                         if (res.type != RTN_UNICAST)
1319                                 goto e_inval;
1320                         flags |= RTCF_DNAT;
1321                         gateway = fl.fld_dst;
1322                 }
1323                 fl.fld_src = src_map;
1324         }
1325
1326         switch(res.type) {
1327         case RTN_UNICAST:
1328                 /*
1329                  * Forwarding check here, we only check for forwarding
1330                  * being turned off, if you want to only forward intra
1331                  * area, its up to you to set the routing tables up
1332                  * correctly.
1333                  */
1334                 if (dn_db->parms.forwarding == 0)
1335                         goto e_inval;
1336
1337                 if (res.fi->fib_nhs > 1 && fl.oif == 0)
1338                         dn_fib_select_multipath(&fl, &res);
1339
1340                 /*
1341                  * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
1342                  * flag as a hint to set the intra-ethernet bit when
1343                  * forwarding. If we've got NAT in operation, we don't do
1344                  * this optimisation.
1345                  */
1346                 if (out_dev == in_dev && !(flags & RTCF_NAT))
1347                         flags |= RTCF_DOREDIRECT;
1348
1349                 local_src = DN_FIB_RES_PREFSRC(res);
1350
1351         case RTN_BLACKHOLE:
1352         case RTN_UNREACHABLE:
1353                 break;
1354         case RTN_LOCAL:
1355                 flags |= RTCF_LOCAL;
1356                 fl.fld_src = cb->dst;
1357                 fl.fld_dst = cb->src;
1358
1359                 /* Routing tables gave us a gateway */
1360                 if (gateway)
1361                         goto make_route;
1362
1363                 /* Packet was intra-ethernet, so we know its on-link */
1364                 if (cb->rt_flags & DN_RT_F_IE) {
1365                         gateway = cb->src;
1366                         flags |= RTCF_DIRECTSRC;
1367                         goto make_route;
1368                 }
1369
1370                 /* Use the default router if there is one */
1371                 neigh = neigh_clone(dn_db->router);
1372                 if (neigh) {
1373                         gateway = ((struct dn_neigh *)neigh)->addr;
1374                         goto make_route;
1375                 }
1376
1377                 /* Close eyes and pray */
1378                 gateway = cb->src;
1379                 flags |= RTCF_DIRECTSRC;
1380                 goto make_route;
1381         default:
1382                 goto e_inval;
1383         }
1384
1385 make_route:
1386         rt = dst_alloc(&dn_dst_ops, 0);
1387         if (rt == NULL)
1388                 goto e_nobufs;
1389
1390         rt->rt_saddr      = fl.fld_src;
1391         rt->rt_daddr      = fl.fld_dst;
1392         rt->rt_gateway    = fl.fld_dst;
1393         if (gateway)
1394                 rt->rt_gateway = gateway;
1395         rt->rt_local_src  = local_src ? local_src : rt->rt_saddr;
1396
1397         rt->rt_dst_map    = fl.fld_dst;
1398         rt->rt_src_map    = fl.fld_src;
1399
1400         rt->fl.fld_src    = cb->src;
1401         rt->fl.fld_dst    = cb->dst;
1402         rt->fl.oif        = 0;
1403         rt->fl.iif        = in_dev->ifindex;
1404         rt->fl.mark       = fl.mark;
1405
1406         rt->dst.flags = DST_HOST;
1407         rt->dst.neighbour = neigh;
1408         rt->dst.dev = out_dev;
1409         rt->dst.lastuse = jiffies;
1410         rt->dst.output = dn_rt_bug;
1411         switch(res.type) {
1412                 case RTN_UNICAST:
1413                         rt->dst.input = dn_forward;
1414                         break;
1415                 case RTN_LOCAL:
1416                         rt->dst.output = dn_output;
1417                         rt->dst.input = dn_nsp_rx;
1418                         rt->dst.dev = in_dev;
1419                         flags |= RTCF_LOCAL;
1420                         break;
1421                 default:
1422                 case RTN_UNREACHABLE:
1423                 case RTN_BLACKHOLE:
1424                         rt->dst.input = dst_discard;
1425         }
1426         rt->rt_flags = flags;
1427         if (rt->dst.dev)
1428                 dev_hold(rt->dst.dev);
1429
1430         err = dn_rt_set_next_hop(rt, &res);
1431         if (err)
1432                 goto e_neighbour;
1433
1434         hash = dn_hash(rt->fl.fld_src, rt->fl.fld_dst);
1435         dn_insert_route(rt, hash, &rt);
1436         skb_dst_set(skb, &rt->dst);
1437
1438 done:
1439         if (neigh)
1440                 neigh_release(neigh);
1441         if (free_res)
1442                 dn_fib_res_put(&res);
1443         dev_put(in_dev);
1444         if (out_dev)
1445                 dev_put(out_dev);
1446 out:
1447         return err;
1448
1449 e_inval:
1450         err = -EINVAL;
1451         goto done;
1452
1453 e_nobufs:
1454         err = -ENOBUFS;
1455         goto done;
1456
1457 e_neighbour:
1458         dst_free(&rt->dst);
1459         goto done;
1460 }
1461
1462 static int dn_route_input(struct sk_buff *skb)
1463 {
1464         struct dn_route *rt;
1465         struct dn_skb_cb *cb = DN_SKB_CB(skb);
1466         unsigned hash = dn_hash(cb->src, cb->dst);
1467
1468         if (skb_dst(skb))
1469                 return 0;
1470
1471         rcu_read_lock();
1472         for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
1473             rt = rcu_dereference(rt->dst.dn_next)) {
1474                 if ((rt->fl.fld_src == cb->src) &&
1475                     (rt->fl.fld_dst == cb->dst) &&
1476                     (rt->fl.oif == 0) &&
1477                     (rt->fl.mark == skb->mark) &&
1478                     (rt->fl.iif == cb->iif)) {
1479                         dst_use(&rt->dst, jiffies);
1480                         rcu_read_unlock();
1481                         skb_dst_set(skb, (struct dst_entry *)rt);
1482                         return 0;
1483                 }
1484         }
1485         rcu_read_unlock();
1486
1487         return dn_route_input_slow(skb);
1488 }
1489
1490 static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1491                            int event, int nowait, unsigned int flags)
1492 {
1493         struct dn_route *rt = (struct dn_route *)skb_dst(skb);
1494         struct rtmsg *r;
1495         struct nlmsghdr *nlh;
1496         unsigned char *b = skb_tail_pointer(skb);
1497         long expires;
1498
1499         nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
1500         r = NLMSG_DATA(nlh);
1501         r->rtm_family = AF_DECnet;
1502         r->rtm_dst_len = 16;
1503         r->rtm_src_len = 0;
1504         r->rtm_tos = 0;
1505         r->rtm_table = RT_TABLE_MAIN;
1506         RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
1507         r->rtm_type = rt->rt_type;
1508         r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
1509         r->rtm_scope = RT_SCOPE_UNIVERSE;
1510         r->rtm_protocol = RTPROT_UNSPEC;
1511         if (rt->rt_flags & RTCF_NOTIFY)
1512                 r->rtm_flags |= RTM_F_NOTIFY;
1513         RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
1514         if (rt->fl.fld_src) {
1515                 r->rtm_src_len = 16;
1516                 RTA_PUT(skb, RTA_SRC, 2, &rt->fl.fld_src);
1517         }
1518         if (rt->dst.dev)
1519                 RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
1520         /*
1521          * Note to self - change this if input routes reverse direction when
1522          * they deal only with inputs and not with replies like they do
1523          * currently.
1524          */
1525         RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
1526         if (rt->rt_daddr != rt->rt_gateway)
1527                 RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
1528         if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
1529                 goto rtattr_failure;
1530         expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
1531         if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
1532                                rt->dst.error) < 0)
1533                 goto rtattr_failure;
1534         if (dn_is_input_route(rt))
1535                 RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fl.iif);
1536
1537         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1538         return skb->len;
1539
1540 nlmsg_failure:
1541 rtattr_failure:
1542         nlmsg_trim(skb, b);
1543         return -1;
1544 }
1545
1546 /*
1547  * This is called by both endnodes and routers now.
1548  */
1549 static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
1550 {
1551         struct net *net = sock_net(in_skb->sk);
1552         struct rtattr **rta = arg;
1553         struct rtmsg *rtm = NLMSG_DATA(nlh);
1554         struct dn_route *rt = NULL;
1555         struct dn_skb_cb *cb;
1556         int err;
1557         struct sk_buff *skb;
1558         struct flowi fl;
1559
1560         if (!net_eq(net, &init_net))
1561                 return -EINVAL;
1562
1563         memset(&fl, 0, sizeof(fl));
1564         fl.proto = DNPROTO_NSP;
1565
1566         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1567         if (skb == NULL)
1568                 return -ENOBUFS;
1569         skb_reset_mac_header(skb);
1570         cb = DN_SKB_CB(skb);
1571
1572         if (rta[RTA_SRC-1])
1573                 memcpy(&fl.fld_src, RTA_DATA(rta[RTA_SRC-1]), 2);
1574         if (rta[RTA_DST-1])
1575                 memcpy(&fl.fld_dst, RTA_DATA(rta[RTA_DST-1]), 2);
1576         if (rta[RTA_IIF-1])
1577                 memcpy(&fl.iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
1578
1579         if (fl.iif) {
1580                 struct net_device *dev;
1581                 if ((dev = dev_get_by_index(&init_net, fl.iif)) == NULL) {
1582                         kfree_skb(skb);
1583                         return -ENODEV;
1584                 }
1585                 if (!dev->dn_ptr) {
1586                         dev_put(dev);
1587                         kfree_skb(skb);
1588                         return -ENODEV;
1589                 }
1590                 skb->protocol = htons(ETH_P_DNA_RT);
1591                 skb->dev = dev;
1592                 cb->src = fl.fld_src;
1593                 cb->dst = fl.fld_dst;
1594                 local_bh_disable();
1595                 err = dn_route_input(skb);
1596                 local_bh_enable();
1597                 memset(cb, 0, sizeof(struct dn_skb_cb));
1598                 rt = (struct dn_route *)skb_dst(skb);
1599                 if (!err && -rt->dst.error)
1600                         err = rt->dst.error;
1601         } else {
1602                 int oif = 0;
1603                 if (rta[RTA_OIF - 1])
1604                         memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
1605                 fl.oif = oif;
1606                 err = dn_route_output_key((struct dst_entry **)&rt, &fl, 0);
1607         }
1608
1609         if (skb->dev)
1610                 dev_put(skb->dev);
1611         skb->dev = NULL;
1612         if (err)
1613                 goto out_free;
1614         skb_dst_set(skb, &rt->dst);
1615         if (rtm->rtm_flags & RTM_F_NOTIFY)
1616                 rt->rt_flags |= RTCF_NOTIFY;
1617
1618         err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, RTM_NEWROUTE, 0, 0);
1619
1620         if (err == 0)
1621                 goto out_free;
1622         if (err < 0) {
1623                 err = -EMSGSIZE;
1624                 goto out_free;
1625         }
1626
1627         return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
1628
1629 out_free:
1630         kfree_skb(skb);
1631         return err;
1632 }
1633
1634 /*
1635  * For routers, this is called from dn_fib_dump, but for endnodes its
1636  * called directly from the rtnetlink dispatch table.
1637  */
1638 int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
1639 {
1640         struct net *net = sock_net(skb->sk);
1641         struct dn_route *rt;
1642         int h, s_h;
1643         int idx, s_idx;
1644
1645         if (!net_eq(net, &init_net))
1646                 return 0;
1647
1648         if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
1649                 return -EINVAL;
1650         if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
1651                 return 0;
1652
1653         s_h = cb->args[0];
1654         s_idx = idx = cb->args[1];
1655         for(h = 0; h <= dn_rt_hash_mask; h++) {
1656                 if (h < s_h)
1657                         continue;
1658                 if (h > s_h)
1659                         s_idx = 0;
1660                 rcu_read_lock_bh();
1661                 for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
1662                         rt;
1663                         rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
1664                         if (idx < s_idx)
1665                                 continue;
1666                         skb_dst_set(skb, dst_clone(&rt->dst));
1667                         if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
1668                                         cb->nlh->nlmsg_seq, RTM_NEWROUTE,
1669                                         1, NLM_F_MULTI) <= 0) {
1670                                 skb_dst_drop(skb);
1671                                 rcu_read_unlock_bh();
1672                                 goto done;
1673                         }
1674                         skb_dst_drop(skb);
1675                 }
1676                 rcu_read_unlock_bh();
1677         }
1678
1679 done:
1680         cb->args[0] = h;
1681         cb->args[1] = idx;
1682         return skb->len;
1683 }
1684
1685 #ifdef CONFIG_PROC_FS
1686 struct dn_rt_cache_iter_state {
1687         int bucket;
1688 };
1689
1690 static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
1691 {
1692         struct dn_route *rt = NULL;
1693         struct dn_rt_cache_iter_state *s = seq->private;
1694
1695         for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
1696                 rcu_read_lock_bh();
1697                 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
1698                 if (rt)
1699                         break;
1700                 rcu_read_unlock_bh();
1701         }
1702         return rt;
1703 }
1704
1705 static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
1706 {
1707         struct dn_rt_cache_iter_state *s = seq->private;
1708
1709         rt = rcu_dereference_bh(rt->dst.dn_next);
1710         while (!rt) {
1711                 rcu_read_unlock_bh();
1712                 if (--s->bucket < 0)
1713                         break;
1714                 rcu_read_lock_bh();
1715                 rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
1716         }
1717         return rt;
1718 }
1719
1720 static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
1721 {
1722         struct dn_route *rt = dn_rt_cache_get_first(seq);
1723
1724         if (rt) {
1725                 while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
1726                         --*pos;
1727         }
1728         return *pos ? NULL : rt;
1729 }
1730
1731 static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1732 {
1733         struct dn_route *rt = dn_rt_cache_get_next(seq, v);
1734         ++*pos;
1735         return rt;
1736 }
1737
1738 static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
1739 {
1740         if (v)
1741                 rcu_read_unlock_bh();
1742 }
1743
1744 static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
1745 {
1746         struct dn_route *rt = v;
1747         char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
1748
1749         seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
1750                         rt->dst.dev ? rt->dst.dev->name : "*",
1751                         dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
1752                         dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
1753                         atomic_read(&rt->dst.__refcnt),
1754                         rt->dst.__use,
1755                         (int) dst_metric(&rt->dst, RTAX_RTT));
1756         return 0;
1757 }
1758
1759 static const struct seq_operations dn_rt_cache_seq_ops = {
1760         .start  = dn_rt_cache_seq_start,
1761         .next   = dn_rt_cache_seq_next,
1762         .stop   = dn_rt_cache_seq_stop,
1763         .show   = dn_rt_cache_seq_show,
1764 };
1765
1766 static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
1767 {
1768         return seq_open_private(file, &dn_rt_cache_seq_ops,
1769                         sizeof(struct dn_rt_cache_iter_state));
1770 }
1771
1772 static const struct file_operations dn_rt_cache_seq_fops = {
1773         .owner   = THIS_MODULE,
1774         .open    = dn_rt_cache_seq_open,
1775         .read    = seq_read,
1776         .llseek  = seq_lseek,
1777         .release = seq_release_private,
1778 };
1779
1780 #endif /* CONFIG_PROC_FS */
1781
1782 void __init dn_route_init(void)
1783 {
1784         int i, goal, order;
1785
1786         dn_dst_ops.kmem_cachep =
1787                 kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
1788                                   SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1789         dst_entries_init(&dn_dst_ops);
1790         setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
1791         dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
1792         add_timer(&dn_route_timer);
1793
1794         goal = totalram_pages >> (26 - PAGE_SHIFT);
1795
1796         for(order = 0; (1UL << order) < goal; order++)
1797                 /* NOTHING */;
1798
1799         /*
1800          * Only want 1024 entries max, since the table is very, very unlikely
1801          * to be larger than that.
1802          */
1803         while(order && ((((1UL << order) * PAGE_SIZE) /
1804                                 sizeof(struct dn_rt_hash_bucket)) >= 2048))
1805                 order--;
1806
1807         do {
1808                 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
1809                         sizeof(struct dn_rt_hash_bucket);
1810                 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
1811                         dn_rt_hash_mask--;
1812                 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
1813                         __get_free_pages(GFP_ATOMIC, order);
1814         } while (dn_rt_hash_table == NULL && --order > 0);
1815
1816         if (!dn_rt_hash_table)
1817                 panic("Failed to allocate DECnet route cache hash table\n");
1818
1819         printk(KERN_INFO
1820                 "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
1821                 dn_rt_hash_mask,
1822                 (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
1823
1824         dn_rt_hash_mask--;
1825         for(i = 0; i <= dn_rt_hash_mask; i++) {
1826                 spin_lock_init(&dn_rt_hash_table[i].lock);
1827                 dn_rt_hash_table[i].chain = NULL;
1828         }
1829
1830         dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
1831
1832         proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
1833
1834 #ifdef CONFIG_DECNET_ROUTER
1835         rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, dn_fib_dump);
1836 #else
1837         rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
1838                       dn_cache_dump);
1839 #endif
1840 }
1841
1842 void __exit dn_route_cleanup(void)
1843 {
1844         del_timer(&dn_route_timer);
1845         dn_run_flush(0);
1846
1847         proc_net_remove(&init_net, "decnet_cache");
1848         dst_entries_destroy(&dn_dst_ops);
1849 }
1850