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