📄 dn_route.c
字号:
/* * DECnet An implementation of the DECnet protocol suite for the LINUX * operating system. DECnet is implemented using the BSD Socket * interface as the means of communication with the user level. * * DECnet Routing Functions (Endnode and Router) * * Authors: Steve Whitehouse <SteveW@ACM.org> * Eduardo Marcelo Serrat <emserrat@geocities.com> * * Changes: * Steve Whitehouse : Fixes to allow "intra-ethernet" and * "return-to-sender" bits on outgoing * packets. * Steve Whitehouse : Timeouts for cached routes. * Steve Whitehouse : Use dst cache for input routes too. * Steve Whitehouse : Fixed error values in dn_send_skb. * Steve Whitehouse : Rework routing functions to better fit * DECnet routing design * Alexey Kuznetsov : New SMP locking * Steve Whitehouse : More SMP locking changes & dn_cache_dump() * Steve Whitehouse : Prerouting NF hook, now really is prerouting. * Fixed possible skb leak in rtnetlink funcs. * Steve Whitehouse : Dave Miller's dynamic hash table sizing and * Alexey Kuznetsov's finer grained locking * from ipv4/route.c. * Steve Whitehouse : Routing is now starting to look like a * sensible set of code now, mainly due to * my copying the IPv4 routing code. The * hooks here are modified and will continue * to evolve for a while. * Steve Whitehouse : Real SMP at last :-) Also new netfilter * stuff. Look out raw sockets your days * are numbered! *//****************************************************************************** (c) 1995-1998 E.M. Serrat emserrat@geocities.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.*******************************************************************************/#include <linux/config.h>#include <linux/errno.h>#include <linux/types.h>#include <linux/socket.h>#include <linux/in.h>#include <linux/kernel.h>#include <linux/sockios.h>#include <linux/net.h>#include <linux/netdevice.h>#include <linux/inet.h>#include <linux/route.h>#include <net/sock.h>#include <linux/fcntl.h>#include <linux/mm.h>#include <linux/proc_fs.h>#include <linux/init.h>#include <linux/rtnetlink.h>#include <linux/string.h>#include <linux/netfilter_decnet.h>#include <asm/errno.h>#include <net/neighbour.h>#include <net/dst.h>#include <net/dn.h>#include <net/dn_dev.h>#include <net/dn_nsp.h>#include <net/dn_route.h>#include <net/dn_neigh.h>#include <net/dn_fib.h>struct dn_rt_hash_bucket{ struct dn_route *chain; rwlock_t lock;} __attribute__((__aligned__(8)));extern struct neigh_table dn_neigh_table;static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};int dn_rt_min_delay = 2*HZ;int dn_rt_max_delay = 10*HZ;static unsigned long dn_rt_deadline = 0;static int dn_dst_gc(void);static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);static struct dst_entry *dn_dst_reroute(struct dst_entry *, struct sk_buff *skb);static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);static void dn_dst_link_failure(struct sk_buff *);static int dn_route_input(struct sk_buff *);static void dn_run_flush(unsigned long dummy);static struct dn_rt_hash_bucket *dn_rt_hash_table;static unsigned dn_rt_hash_mask;static struct timer_list dn_route_timer;static struct timer_list dn_rt_flush_timer = { function: dn_run_flush };int decnet_dst_gc_interval = 2;static struct dst_ops dn_dst_ops = { PF_DECnet, __constant_htons(ETH_P_DNA_RT), 128, dn_dst_gc, dn_dst_check, dn_dst_reroute, NULL, dn_dst_negative_advice, dn_dst_link_failure, sizeof(struct dn_route), ATOMIC_INIT(0)};static __inline__ unsigned dn_hash(unsigned short src, unsigned short dst){ unsigned short tmp = src ^ dst; tmp ^= (tmp >> 3); tmp ^= (tmp >> 5); tmp ^= (tmp >> 10); return dn_rt_hash_mask & (unsigned)tmp;}static void SMP_TIMER_NAME(dn_dst_check_expire)(unsigned long dummy){ int i; struct dn_route *rt, **rtp; unsigned long now = jiffies; unsigned long expire = 120 * HZ; for(i = 0; i <= dn_rt_hash_mask; i++) { rtp = &dn_rt_hash_table[i].chain; write_lock(&dn_rt_hash_table[i].lock); while((rt=*rtp) != NULL) { if (atomic_read(&rt->u.dst.__refcnt) || (now - rt->u.dst.lastuse) < expire) { rtp = &rt->u.rt_next; continue; } *rtp = rt->u.rt_next; rt->u.rt_next = NULL; dst_free(&rt->u.dst); } write_unlock(&dn_rt_hash_table[i].lock); if ((jiffies - now) > 0) break; } mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);}SMP_TIMER_DEFINE(dn_dst_check_expire, dn_dst_task);static int dn_dst_gc(void){ struct dn_route *rt, **rtp; int i; unsigned long now = jiffies; unsigned long expire = 10 * HZ; for(i = 0; i <= dn_rt_hash_mask; i++) { write_lock_bh(&dn_rt_hash_table[i].lock); rtp = &dn_rt_hash_table[i].chain; while((rt=*rtp) != NULL) { if (atomic_read(&rt->u.dst.__refcnt) || (now - rt->u.dst.lastuse) < expire) { rtp = &rt->u.rt_next; continue; } *rtp = rt->u.rt_next; rt->u.rt_next = NULL; dst_free(&rt->u.dst); break; } write_unlock_bh(&dn_rt_hash_table[i].lock); } return 0;}static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie){ dst_release(dst); return NULL;}static struct dst_entry *dn_dst_reroute(struct dst_entry *dst, struct sk_buff *skb){ return NULL;}/* * This is called through sendmsg() when you specify MSG_TRYHARD * and there is already a route in cache. */static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst){ dst_release(dst); return NULL;}static void dn_dst_link_failure(struct sk_buff *skb){ return;}static void dn_insert_route(struct dn_route *rt, unsigned hash){ unsigned long now = jiffies; write_lock_bh(&dn_rt_hash_table[hash].lock); rt->u.rt_next = dn_rt_hash_table[hash].chain; dn_rt_hash_table[hash].chain = rt; dst_hold(&rt->u.dst); rt->u.dst.__use++; rt->u.dst.lastuse = now; write_unlock_bh(&dn_rt_hash_table[hash].lock);}void SMP_TIMER_NAME(dn_run_flush)(unsigned long dummy){ int i; struct dn_route *rt, *next; for(i = 0; i < dn_rt_hash_mask; i++) { write_lock_bh(&dn_rt_hash_table[i].lock); if ((rt = xchg(&dn_rt_hash_table[i].chain, NULL)) == NULL) goto nothing_to_declare; for(; rt; rt=next) { next = rt->u.rt_next; rt->u.rt_next = NULL; dst_free((struct dst_entry *)rt); }nothing_to_declare: write_unlock_bh(&dn_rt_hash_table[i].lock); }}SMP_TIMER_DEFINE(dn_run_flush, dn_flush_task);static spinlock_t dn_rt_flush_lock = SPIN_LOCK_UNLOCKED;void dn_rt_cache_flush(int delay){ unsigned long now = jiffies; int user_mode = !in_interrupt(); if (delay < 0) delay = dn_rt_min_delay; spin_lock_bh(&dn_rt_flush_lock); if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) { long tmo = (long)(dn_rt_deadline - now); if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay) tmo = 0; if (delay > tmo) delay = tmo; } if (delay <= 0) { spin_unlock_bh(&dn_rt_flush_lock); dn_run_flush(0); return; } if (dn_rt_deadline == 0) dn_rt_deadline = now + dn_rt_max_delay; dn_rt_flush_timer.expires = now + delay; add_timer(&dn_rt_flush_timer); spin_unlock_bh(&dn_rt_flush_lock);}static int dn_route_rx_packet(struct sk_buff *skb){ int err; if ((err = dn_route_input(skb)) == 0) return skb->dst->input(skb); kfree_skb(skb); return err;}static int dn_route_rx_long(struct sk_buff *skb){ struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb; unsigned char *ptr = skb->data; if (skb->len < 21) /* 20 for long header, 1 for shortest nsp */ goto drop_it; skb_pull(skb, 20); skb->h.raw = skb->data; /* Destination info */ ptr += 2; cb->dst = dn_htons(dn_eth2dn(ptr)); if (memcmp(ptr, dn_hiord_addr, 4) != 0) goto drop_it; ptr += 6; /* Source info */ ptr += 2; cb->src = dn_htons(dn_eth2dn(ptr)); if (memcmp(ptr, dn_hiord_addr, 4) != 0) goto drop_it; ptr += 6; /* Other junk */ ptr++; cb->hops = *ptr++; /* Visit Count */ return NF_HOOK(PF_DECnet, NF_DN_PRE_ROUTING, skb, skb->dev, NULL, dn_route_rx_packet);drop_it: kfree_skb(skb); return 0;}static int dn_route_rx_short(struct sk_buff *skb){ struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb; unsigned char *ptr = skb->data; if (skb->len < 6) /* 5 for short header + 1 for shortest nsp */ goto drop_it; skb_pull(skb, 5); skb->h.raw = skb->data; cb->dst = *(dn_address *)ptr; ptr += 2; cb->src = *(dn_address *)ptr; ptr += 2; cb->hops = *ptr & 0x3f; return NF_HOOK(PF_DECnet, NF_DN_PRE_ROUTING, skb, skb->dev, NULL, dn_route_rx_packet);drop_it: kfree_skb(skb); return 0;}static int dn_route_discard(struct sk_buff *skb){ kfree_skb(skb); return 0;}static int dn_route_ptp_hello(struct sk_buff *skb){ dn_dev_hello(skb); dn_neigh_pointopoint_hello(skb); return 0;}int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt){ struct dn_skb_cb *cb; unsigned char flags = 0; int padlen = 0; __u16 len = dn_ntohs(*(__u16 *)skb->data); struct dn_dev *dn = (struct dn_dev *)dev->dn_ptr; if (dn == NULL) goto dump_it; if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) goto out; skb_pull(skb, 2); if (len > skb->len) goto dump_it; skb_trim(skb, len); flags = *skb->data; cb = (struct dn_skb_cb *)skb->cb; cb->stamp = jiffies; cb->iif = dev->ifindex; /* * If we have padding, remove it. */ if (flags & DN_RT_F_PF) { padlen = flags & ~DN_RT_F_PF; skb_pull(skb, padlen); flags = *skb->data; } skb->nh.raw = skb->data; /* * Weed out future version DECnet */ if (flags & DN_RT_F_VER) goto dump_it; cb->rt_flags = flags; if (decnet_debug_level & 1) printk(KERN_DEBUG "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n", (int)flags, (dev) ? dev->name : "???", len, skb->len, padlen); if (flags & DN_RT_PKT_CNTL) { switch(flags & DN_RT_CNTL_MSK) { case DN_RT_PKT_INIT: dn_dev_init_pkt(skb); break; case DN_RT_PKT_VERI: dn_dev_veri_pkt(skb); break; } if (dn->parms.state != DN_DEV_S_RU) goto dump_it; switch(flags & DN_RT_CNTL_MSK) { case DN_RT_PKT_HELO: NF_HOOK(PF_DECnet, NF_DN_HELLO, skb, skb->dev, NULL, dn_route_ptp_hello); goto out; case DN_RT_PKT_L1RT: case DN_RT_PKT_L2RT: NF_HOOK(PF_DECnet, NF_DN_ROUTE, skb, skb->dev, NULL, dn_route_discard); goto out; case DN_RT_PKT_ERTH: NF_HOOK(PF_DECnet, NF_DN_HELLO, skb, skb->dev, NULL, dn_neigh_router_hello); goto out; case DN_RT_PKT_EEDH: NF_HOOK(PF_DECnet, NF_DN_HELLO, skb, skb->dev, NULL, dn_neigh_endnode_hello); goto out; } } else { if (dn->parms.state != DN_DEV_S_RU) goto dump_it; skb_pull(skb, 1); /* Pull flags */ switch(flags & DN_RT_PKT_MSK) { case DN_RT_PKT_LONG: return dn_route_rx_long(skb); case DN_RT_PKT_SHORT: return dn_route_rx_short(skb); } }dump_it: kfree_skb(skb);out: return 0;}static int dn_output(struct sk_buff *skb){ struct dst_entry *dst = skb->dst; struct dn_route *rt = (struct dn_route *)dst; struct net_device *dev = dst->dev; struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb; struct neighbour *neigh; int err = -EINVAL; if ((neigh = dst->neighbour) == NULL) goto error; skb->dev = dev; cb->src = rt->rt_saddr; cb->dst = rt->rt_daddr; /* * Always set the Intra-Ethernet bit on all outgoing packets * originated on this node. Only valid flag from upper layers * is return-to-sender-requested. Set hop count to 0 too. */ cb->rt_flags &= ~DN_RT_F_RQR; cb->rt_flags |= DN_RT_F_IE; cb->hops = 0; return NF_HOOK(PF_DECnet, NF_DN_LOCAL_OUT, skb, NULL, dev, neigh->output);error: if (net_ratelimit()) printk(KERN_DEBUG "dn_output: This should not happen\n"); kfree_skb(skb); return err;}#ifdef CONFIG_DECNET_ROUTERstatic int dn_forward(struct sk_buff *skb){ struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb; struct dst_entry *dst = skb->dst; struct net_device *dev = skb->dev; struct neighbour *neigh; int err = -EINVAL; if ((neigh = dst->neighbour) == NULL) goto error; /* * Hop count exceeded. */ err = 0; if (++cb->hops > 30) goto drop; skb->dev = dst->dev; /* * If packet goes out same interface it came in on, then set * the Intra-Ethernet bit. This has no effect for short * packets, so we don't need to test for them here. */ if (cb->iif == dst->dev->ifindex) cb->rt_flags |= DN_RT_F_IE; else cb->rt_flags &= ~DN_RT_F_IE; return NF_HOOK(PF_DECnet, NF_DN_FORWARD, skb, dev, skb->dev, neigh->output);error: if (net_ratelimit()) printk(KERN_DEBUG "dn_forward: This should not happen\n");drop: kfree_skb(skb); return err;}#endif/* * Drop packet. This is used for endnodes and for * when we should not be forwarding packets from * this dest. */static int dn_blackhole(struct sk_buff *skb){ kfree_skb(skb); return 0;}/* * Used to catch bugs. This should never normally get * called. */static int dn_rt_bug(struct sk_buff *skb){ if (net_ratelimit()) { struct dn_skb_cb *cb = (struct dn_skb_cb *)skb->cb; printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n", cb->src, cb->dst); } kfree_skb(skb); return -EINVAL;}static int dn_route_output_slow(struct dst_entry **pprt, dn_address dst, dn_address src, int flags)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -