⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip6_tunnel.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *	IPv6 tunneling device *	Linux INET6 implementation * *	Authors: *	Ville Nuorvala		<vnuorval@tcs.hut.fi> *	Yasuyuki Kozakai	<kozakai@linux-ipv6.org> * *	$Id$ * *      Based on: *      linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c * *      RFC 2473 * *	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 (at your option) any later version. * */#include <linux/module.h>#include <linux/capability.h>#include <linux/errno.h>#include <linux/types.h>#include <linux/sockios.h>#include <linux/icmp.h>#include <linux/if.h>#include <linux/in.h>#include <linux/ip.h>#include <linux/if_tunnel.h>#include <linux/net.h>#include <linux/in6.h>#include <linux/netdevice.h>#include <linux/if_arp.h>#include <linux/icmpv6.h>#include <linux/init.h>#include <linux/route.h>#include <linux/rtnetlink.h>#include <linux/netfilter_ipv6.h>#include <asm/uaccess.h>#include <asm/atomic.h>#include <net/icmp.h>#include <net/ip.h>#include <net/ipv6.h>#include <net/ip6_route.h>#include <net/addrconf.h>#include <net/ip6_tunnel.h>#include <net/xfrm.h>#include <net/dsfield.h>#include <net/inet_ecn.h>MODULE_AUTHOR("Ville Nuorvala");MODULE_DESCRIPTION("IPv6 tunneling device");MODULE_LICENSE("GPL");#define IPV6_TLV_TEL_DST_SIZE 8#ifdef IP6_TNL_DEBUG#define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)#else#define IP6_TNL_TRACE(x...) do {;} while(0)#endif#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)#define IPV6_TCLASS_SHIFT 20#define HASH_SIZE  32#define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \		     (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \		    (HASH_SIZE - 1))static int ip6_fb_tnl_dev_init(struct net_device *dev);static int ip6_tnl_dev_init(struct net_device *dev);static void ip6_tnl_dev_setup(struct net_device *dev);/* the IPv6 tunnel fallback device */static struct net_device *ip6_fb_tnl_dev;/* lists for storing tunnels in use */static struct ip6_tnl *tnls_r_l[HASH_SIZE];static struct ip6_tnl *tnls_wc[1];static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };/* lock for the tunnel lists */static DEFINE_RWLOCK(ip6_tnl_lock);static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t){	struct dst_entry *dst = t->dst_cache;	if (dst && dst->obsolete &&	    dst->ops->check(dst, t->dst_cookie) == NULL) {		t->dst_cache = NULL;		dst_release(dst);		return NULL;	}	return dst;}static inline void ip6_tnl_dst_reset(struct ip6_tnl *t){	dst_release(t->dst_cache);	t->dst_cache = NULL;}static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst){	struct rt6_info *rt = (struct rt6_info *) dst;	t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;	dst_release(t->dst_cache);	t->dst_cache = dst;}/** * ip6_tnl_lookup - fetch tunnel matching the end-point addresses *   @remote: the address of the tunnel exit-point *   @local: the address of the tunnel entry-point * * Return: *   tunnel matching given end-points if found, *   else fallback tunnel if its device is up, *   else %NULL **/static struct ip6_tnl *ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local){	unsigned h0 = HASH(remote);	unsigned h1 = HASH(local);	struct ip6_tnl *t;	for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {		if (ipv6_addr_equal(local, &t->parms.laddr) &&		    ipv6_addr_equal(remote, &t->parms.raddr) &&		    (t->dev->flags & IFF_UP))			return t;	}	if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))		return t;	return NULL;}/** * ip6_tnl_bucket - get head of list matching given tunnel parameters *   @p: parameters containing tunnel end-points * * Description: *   ip6_tnl_bucket() returns the head of the list matching the *   &struct in6_addr entries laddr and raddr in @p. * * Return: head of IPv6 tunnel list **/static struct ip6_tnl **ip6_tnl_bucket(struct ip6_tnl_parm *p){	struct in6_addr *remote = &p->raddr;	struct in6_addr *local = &p->laddr;	unsigned h = 0;	int prio = 0;	if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {		prio = 1;		h = HASH(remote) ^ HASH(local);	}	return &tnls[prio][h];}/** * ip6_tnl_link - add tunnel to hash table *   @t: tunnel to be added **/static voidip6_tnl_link(struct ip6_tnl *t){	struct ip6_tnl **tp = ip6_tnl_bucket(&t->parms);	t->next = *tp;	write_lock_bh(&ip6_tnl_lock);	*tp = t;	write_unlock_bh(&ip6_tnl_lock);}/** * ip6_tnl_unlink - remove tunnel from hash table *   @t: tunnel to be removed **/static voidip6_tnl_unlink(struct ip6_tnl *t){	struct ip6_tnl **tp;	for (tp = ip6_tnl_bucket(&t->parms); *tp; tp = &(*tp)->next) {		if (t == *tp) {			write_lock_bh(&ip6_tnl_lock);			*tp = t->next;			write_unlock_bh(&ip6_tnl_lock);			break;		}	}}/** * ip6_tnl_create() - create a new tunnel *   @p: tunnel parameters *   @pt: pointer to new tunnel * * Description: *   Create tunnel matching given parameters. * * Return: *   created tunnel or NULL **/static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p){	struct net_device *dev;	struct ip6_tnl *t;	char name[IFNAMSIZ];	int err;	if (p->name[0]) {		strlcpy(name, p->name, IFNAMSIZ);	} else {		int i;		for (i = 1; i < IP6_TNL_MAX; i++) {			sprintf(name, "ip6tnl%d", i);			if (__dev_get_by_name(&init_net, name) == NULL)				break;		}		if (i == IP6_TNL_MAX)			goto failed;	}	dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);	if (dev == NULL)		goto failed;	t = netdev_priv(dev);	dev->init = ip6_tnl_dev_init;	t->parms = *p;	if ((err = register_netdevice(dev)) < 0) {		free_netdev(dev);		goto failed;	}	dev_hold(dev);	ip6_tnl_link(t);	return t;failed:	return NULL;}/** * ip6_tnl_locate - find or create tunnel matching given parameters *   @p: tunnel parameters *   @create: != 0 if allowed to create new tunnel if no match found * * Description: *   ip6_tnl_locate() first tries to locate an existing tunnel *   based on @parms. If this is unsuccessful, but @create is set a new *   tunnel device is created and registered for use. * * Return: *   matching tunnel or NULL **/static struct ip6_tnl *ip6_tnl_locate(struct ip6_tnl_parm *p, int create){	struct in6_addr *remote = &p->raddr;	struct in6_addr *local = &p->laddr;	struct ip6_tnl *t;	for (t = *ip6_tnl_bucket(p); t; t = t->next) {		if (ipv6_addr_equal(local, &t->parms.laddr) &&		    ipv6_addr_equal(remote, &t->parms.raddr))			return t;	}	if (!create)		return NULL;	return ip6_tnl_create(p);}/** * ip6_tnl_dev_uninit - tunnel device uninitializer *   @dev: the device to be destroyed * * Description: *   ip6_tnl_dev_uninit() removes tunnel from its list **/static voidip6_tnl_dev_uninit(struct net_device *dev){	struct ip6_tnl *t = netdev_priv(dev);	if (dev == ip6_fb_tnl_dev) {		write_lock_bh(&ip6_tnl_lock);		tnls_wc[0] = NULL;		write_unlock_bh(&ip6_tnl_lock);	} else {		ip6_tnl_unlink(t);	}	ip6_tnl_dst_reset(t);	dev_put(dev);}/** * parse_tvl_tnl_enc_lim - handle encapsulation limit option *   @skb: received socket buffer * * Return: *   0 if none was found, *   else index to encapsulation limit **/static __u16parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw){	struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;	__u8 nexthdr = ipv6h->nexthdr;	__u16 off = sizeof (*ipv6h);	while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {		__u16 optlen = 0;		struct ipv6_opt_hdr *hdr;		if (raw + off + sizeof (*hdr) > skb->data &&		    !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))			break;		hdr = (struct ipv6_opt_hdr *) (raw + off);		if (nexthdr == NEXTHDR_FRAGMENT) {			struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;			if (frag_hdr->frag_off)				break;			optlen = 8;		} else if (nexthdr == NEXTHDR_AUTH) {			optlen = (hdr->hdrlen + 2) << 2;		} else {			optlen = ipv6_optlen(hdr);		}		if (nexthdr == NEXTHDR_DEST) {			__u16 i = off + 2;			while (1) {				struct ipv6_tlv_tnl_enc_lim *tel;				/* No more room for encapsulation limit */				if (i + sizeof (*tel) > off + optlen)					break;				tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];				/* return index of option if found and valid */				if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&				    tel->length == 1)					return i;				/* else jump to next option */				if (tel->type)					i += tel->length + 2;				else					i++;			}		}		nexthdr = hdr->nexthdr;		off += optlen;	}	return 0;}/** * ip6_tnl_err - tunnel error handler * * Description: *   ip6_tnl_err() should handle errors in the tunnel according *   to the specifications in RFC 2473. **/static intip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,	    int *type, int *code, int *msg, __u32 *info, int offset){	struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;	struct ip6_tnl *t;	int rel_msg = 0;	int rel_type = ICMPV6_DEST_UNREACH;	int rel_code = ICMPV6_ADDR_UNREACH;	__u32 rel_info = 0;	__u16 len;	int err = -ENOENT;	/* If the packet doesn't contain the original IPv6 header we are	   in trouble since we might need the source address for further	   processing of the error. */	read_lock(&ip6_tnl_lock);	if ((t = ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)		goto out;	if (t->parms.proto != ipproto && t->parms.proto != 0)		goto out;	err = 0;	switch (*type) {		__u32 teli;		struct ipv6_tlv_tnl_enc_lim *tel;		__u32 mtu;	case ICMPV6_DEST_UNREACH:		if (net_ratelimit())			printk(KERN_WARNING			       "%s: Path to destination invalid "			       "or inactive!\n", t->parms.name);		rel_msg = 1;		break;	case ICMPV6_TIME_EXCEED:		if ((*code) == ICMPV6_EXC_HOPLIMIT) {			if (net_ratelimit())				printk(KERN_WARNING				       "%s: Too small hop limit or "				       "routing loop in tunnel!\n",				       t->parms.name);			rel_msg = 1;		}		break;	case ICMPV6_PARAMPROB:		teli = 0;		if ((*code) == ICMPV6_HDR_FIELD)			teli = parse_tlv_tnl_enc_lim(skb, skb->data);		if (teli && teli == *info - 2) {			tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];			if (tel->encap_limit == 0) {				if (net_ratelimit())					printk(KERN_WARNING					       "%s: Too small encapsulation "					       "limit or routing loop in "					       "tunnel!\n", t->parms.name);				rel_msg = 1;			}		} else if (net_ratelimit()) {			printk(KERN_WARNING			       "%s: Recipient unable to parse tunneled "			       "packet!\n ", t->parms.name);		}		break;	case ICMPV6_PKT_TOOBIG:		mtu = *info - offset;		if (mtu < IPV6_MIN_MTU)			mtu = IPV6_MIN_MTU;		t->dev->mtu = mtu;		if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {			rel_type = ICMPV6_PKT_TOOBIG;			rel_code = 0;			rel_info = mtu;			rel_msg = 1;		}		break;	}	*type = rel_type;	*code = rel_code;	*info = rel_info;	*msg = rel_msg;out:	read_unlock(&ip6_tnl_lock);	return err;}static intip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,	   int type, int code, int offset, __be32 info){	int rel_msg = 0;	int rel_type = type;	int rel_code = code;	__u32 rel_info = ntohl(info);	int err;	struct sk_buff *skb2;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -