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