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

📄 ip6_tunnel.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
}static inline intip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev){	struct ip6_tnl *t = netdev_priv(dev);	struct ipv6hdr *ipv6h = ipv6_hdr(skb);	int encap_limit = -1;	__u16 offset;	struct flowi fl;	__u8 dsfield;	__u32 mtu;	int err;	if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||	    !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))		return -1;	offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));	if (offset > 0) {		struct ipv6_tlv_tnl_enc_lim *tel;		tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];		if (tel->encap_limit == 0) {			icmpv6_send(skb, ICMPV6_PARAMPROB,				    ICMPV6_HDR_FIELD, offset + 2, skb->dev);			return -1;		}		encap_limit = tel->encap_limit - 1;	} else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))		encap_limit = t->parms.encap_limit;	memcpy(&fl, &t->fl, sizeof (fl));	fl.proto = IPPROTO_IPV6;	dsfield = ipv6_get_dsfield(ipv6h);	if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))		fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);	if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))		fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);	err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);	if (err != 0) {		if (err == -EMSGSIZE)			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);		return -1;	}	return 0;}static intip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev){	struct ip6_tnl *t = netdev_priv(dev);	struct net_device_stats *stats = &t->stat;	int ret;	if (t->recursion++) {		t->stat.collisions++;		goto tx_err;	}	switch (skb->protocol) {	case __constant_htons(ETH_P_IP):		ret = ip4ip6_tnl_xmit(skb, dev);		break;	case __constant_htons(ETH_P_IPV6):		ret = ip6ip6_tnl_xmit(skb, dev);		break;	default:		goto tx_err;	}	if (ret < 0)		goto tx_err;	t->recursion--;	return 0;tx_err:	stats->tx_errors++;	stats->tx_dropped++;	kfree_skb(skb);	t->recursion--;	return 0;}static void ip6_tnl_set_cap(struct ip6_tnl *t){	struct ip6_tnl_parm *p = &t->parms;	int ltype = ipv6_addr_type(&p->laddr);	int rtype = ipv6_addr_type(&p->raddr);	p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);	if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&	    rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&	    !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&	    (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {		if (ltype&IPV6_ADDR_UNICAST)			p->flags |= IP6_TNL_F_CAP_XMIT;		if (rtype&IPV6_ADDR_UNICAST)			p->flags |= IP6_TNL_F_CAP_RCV;	}}static void ip6_tnl_link_config(struct ip6_tnl *t){	struct net_device *dev = t->dev;	struct ip6_tnl_parm *p = &t->parms;	struct flowi *fl = &t->fl;	memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));	memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));	/* Set up flowi template */	ipv6_addr_copy(&fl->fl6_src, &p->laddr);	ipv6_addr_copy(&fl->fl6_dst, &p->raddr);	fl->oif = p->link;	fl->fl6_flowlabel = 0;	if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))		fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;	if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))		fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;	ip6_tnl_set_cap(t);	if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)		dev->flags |= IFF_POINTOPOINT;	else		dev->flags &= ~IFF_POINTOPOINT;	dev->iflink = p->link;	if (p->flags & IP6_TNL_F_CAP_XMIT) {		int strict = (ipv6_addr_type(&p->raddr) &			      (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));		struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,						 p->link, strict);		if (rt == NULL)			return;		if (rt->rt6i_dev) {			dev->hard_header_len = rt->rt6i_dev->hard_header_len +				sizeof (struct ipv6hdr);			dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);			if (dev->mtu < IPV6_MIN_MTU)				dev->mtu = IPV6_MIN_MTU;		}		dst_release(&rt->u.dst);	}}/** * ip6_tnl_change - update the tunnel parameters *   @t: tunnel to be changed *   @p: tunnel configuration parameters *   @active: != 0 if tunnel is ready for use * * Description: *   ip6_tnl_change() updates the tunnel parameters **/static intip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p){	ipv6_addr_copy(&t->parms.laddr, &p->laddr);	ipv6_addr_copy(&t->parms.raddr, &p->raddr);	t->parms.flags = p->flags;	t->parms.hop_limit = p->hop_limit;	t->parms.encap_limit = p->encap_limit;	t->parms.flowinfo = p->flowinfo;	t->parms.link = p->link;	t->parms.proto = p->proto;	ip6_tnl_dst_reset(t);	ip6_tnl_link_config(t);	return 0;}/** * ip6_tnl_ioctl - configure ipv6 tunnels from userspace *   @dev: virtual device associated with tunnel *   @ifr: parameters passed from userspace *   @cmd: command to be performed * * Description: *   ip6_tnl_ioctl() is used for managing IPv6 tunnels *   from userspace. * *   The possible commands are the following: *     %SIOCGETTUNNEL: get tunnel parameters for device *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters *     %SIOCCHGTUNNEL: change tunnel parameters to those given *     %SIOCDELTUNNEL: delete tunnel * *   The fallback device "ip6tnl0", created during module *   initialization, can be used for creating other tunnel devices. * * Return: *   0 on success, *   %-EFAULT if unable to copy data to or from userspace, *   %-EPERM if current process hasn't %CAP_NET_ADMIN set *   %-EINVAL if passed tunnel parameters are invalid, *   %-EEXIST if changing a tunnel's parameters would cause a conflict *   %-ENODEV if attempting to change or delete a nonexisting device **/static intip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd){	int err = 0;	struct ip6_tnl_parm p;	struct ip6_tnl *t = NULL;	switch (cmd) {	case SIOCGETTUNNEL:		if (dev == ip6_fb_tnl_dev) {			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {				err = -EFAULT;				break;			}			t = ip6_tnl_locate(&p, 0);		}		if (t == NULL)			t = netdev_priv(dev);		memcpy(&p, &t->parms, sizeof (p));		if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {			err = -EFAULT;		}		break;	case SIOCADDTUNNEL:	case SIOCCHGTUNNEL:		err = -EPERM;		if (!capable(CAP_NET_ADMIN))			break;		err = -EFAULT;		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))			break;		err = -EINVAL;		if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&		    p.proto != 0)			break;		t = ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);		if (dev != ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {			if (t != NULL) {				if (t->dev != dev) {					err = -EEXIST;					break;				}			} else				t = netdev_priv(dev);			ip6_tnl_unlink(t);			err = ip6_tnl_change(t, &p);			ip6_tnl_link(t);			netdev_state_change(dev);		}		if (t) {			err = 0;			if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))				err = -EFAULT;		} else			err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);		break;	case SIOCDELTUNNEL:		err = -EPERM;		if (!capable(CAP_NET_ADMIN))			break;		if (dev == ip6_fb_tnl_dev) {			err = -EFAULT;			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))				break;			err = -ENOENT;			if ((t = ip6_tnl_locate(&p, 0)) == NULL)				break;			err = -EPERM;			if (t->dev == ip6_fb_tnl_dev)				break;			dev = t->dev;		}		err = 0;		unregister_netdevice(dev);		break;	default:		err = -EINVAL;	}	return err;}/** * ip6_tnl_get_stats - return the stats for tunnel device *   @dev: virtual device associated with tunnel * * Return: stats for device **/static struct net_device_stats *ip6_tnl_get_stats(struct net_device *dev){	return &(((struct ip6_tnl *)netdev_priv(dev))->stat);}/** * ip6_tnl_change_mtu - change mtu manually for tunnel device *   @dev: virtual device associated with tunnel *   @new_mtu: the new mtu * * Return: *   0 on success, *   %-EINVAL if mtu too small **/static intip6_tnl_change_mtu(struct net_device *dev, int new_mtu){	if (new_mtu < IPV6_MIN_MTU) {		return -EINVAL;	}	dev->mtu = new_mtu;	return 0;}/** * ip6_tnl_dev_setup - setup virtual tunnel device *   @dev: virtual device associated with tunnel * * Description: *   Initialize function pointers and device parameters **/static void ip6_tnl_dev_setup(struct net_device *dev){	dev->uninit = ip6_tnl_dev_uninit;	dev->destructor = free_netdev;	dev->hard_start_xmit = ip6_tnl_xmit;	dev->get_stats = ip6_tnl_get_stats;	dev->do_ioctl = ip6_tnl_ioctl;	dev->change_mtu = ip6_tnl_change_mtu;	dev->type = ARPHRD_TUNNEL6;	dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);	dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);	dev->flags |= IFF_NOARP;	dev->addr_len = sizeof(struct in6_addr);}/** * ip6_tnl_dev_init_gen - general initializer for all tunnel devices *   @dev: virtual device associated with tunnel **/static inline voidip6_tnl_dev_init_gen(struct net_device *dev){	struct ip6_tnl *t = netdev_priv(dev);	t->dev = dev;	strcpy(t->parms.name, dev->name);}/** * ip6_tnl_dev_init - initializer for all non fallback tunnel devices *   @dev: virtual device associated with tunnel **/static intip6_tnl_dev_init(struct net_device *dev){	struct ip6_tnl *t = netdev_priv(dev);	ip6_tnl_dev_init_gen(dev);	ip6_tnl_link_config(t);	return 0;}/** * ip6_fb_tnl_dev_init - initializer for fallback tunnel device *   @dev: fallback device * * Return: 0 **/static intip6_fb_tnl_dev_init(struct net_device *dev){	struct ip6_tnl *t = netdev_priv(dev);	ip6_tnl_dev_init_gen(dev);	t->parms.proto = IPPROTO_IPV6;	dev_hold(dev);	tnls_wc[0] = t;	return 0;}static struct xfrm6_tunnel ip4ip6_handler = {	.handler	= ip4ip6_rcv,	.err_handler	= ip4ip6_err,	.priority	=	1,};static struct xfrm6_tunnel ip6ip6_handler = {	.handler	= ip6ip6_rcv,	.err_handler	= ip6ip6_err,	.priority	=	1,};/** * ip6_tunnel_init - register protocol and reserve needed resources * * Return: 0 on success **/static int __init ip6_tunnel_init(void){	int  err;	if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {		printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");		err = -EAGAIN;		goto out;	}	if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {		printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");		err = -EAGAIN;		goto unreg_ip4ip6;	}	ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",				      ip6_tnl_dev_setup);	if (!ip6_fb_tnl_dev) {		err = -ENOMEM;		goto fail;	}	ip6_fb_tnl_dev->init = ip6_fb_tnl_dev_init;	if ((err = register_netdev(ip6_fb_tnl_dev))) {		free_netdev(ip6_fb_tnl_dev);		goto fail;	}	return 0;fail:	xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);unreg_ip4ip6:	xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);out:	return err;}static void __exit ip6_tnl_destroy_tunnels(void){	int h;	struct ip6_tnl *t;	for (h = 0; h < HASH_SIZE; h++) {		while ((t = tnls_r_l[h]) != NULL)			unregister_netdevice(t->dev);	}	t = tnls_wc[0];	unregister_netdevice(t->dev);}/** * ip6_tunnel_cleanup - free resources and unregister protocol **/static void __exit ip6_tunnel_cleanup(void){	if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))		printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");	if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))		printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");	rtnl_lock();	ip6_tnl_destroy_tunnels();	rtnl_unlock();}module_init(ip6_tunnel_init);module_exit(ip6_tunnel_cleanup);

⌨️ 快捷键说明

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