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