ip6_tunnel.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,163 行 · 第 1/2 页

C
1,163
字号
	raw[6] = 1;	return opt;}/** * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own *   @t: the outgoing tunnel device *   @hdr: IPv6 header from the incoming packet  * * Description: *   Avoid trivial tunneling loop by checking that tunnel exit-point  *   doesn't match source of incoming packet. * * Return:  *   1 if conflict, *   0 else **/static inline intip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr){	return !ipv6_addr_cmp(&t->parms.raddr, &hdr->saddr);}/** * ip6ip6_tnl_xmit - encapsulate packet and send  *   @skb: the outgoing socket buffer *   @dev: the outgoing tunnel device  * * Description: *   Build new header and do some sanity checks on the packet before sending *   it. * * Return:  *   0 **/static int ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev){	struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;	struct net_device_stats *stats = &t->stat;	struct ipv6hdr *ipv6h = skb->nh.ipv6h;	struct ipv6_txoptions *opt = NULL;	int encap_limit = -1;	__u16 offset;	struct flowi fl;	struct dst_entry *dst;	struct net_device *tdev;	int mtu;	int max_headroom = sizeof(struct ipv6hdr);	u8 proto;	int err;	int pkt_len;	int dsfield;	if (t->recursion++) {		stats->collisions++;		goto tx_err;	}	if (skb->protocol != htons(ETH_P_IPV6) ||	    !(t->parms.flags & IP6_TNL_F_CAP_XMIT) ||	    ip6ip6_tnl_addr_conflict(t, ipv6h)) {		goto tx_err;	}	if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {		struct ipv6_tlv_tnl_enc_lim *tel;		tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];		if (tel->encap_limit == 0) {			icmpv6_send(skb, ICMPV6_PARAMPROB,				    ICMPV6_HDR_FIELD, offset + 2, skb->dev);			goto tx_err;		}		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));	proto = fl.proto;	dsfield = ipv6_get_dsfield(ipv6h);	if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))		fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_TCLASS_MASK);	if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))		fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_FLOWLABEL_MASK);	if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL)		goto tx_err;	if ((dst = ip6_tnl_dst_check(t)) != NULL)		dst_hold(dst);	else		dst = ip6_route_output(NULL, &fl);	if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)		goto tx_err_link_failure;	tdev = dst->dev;	if (tdev == dev) {		stats->collisions++;		if (net_ratelimit())			printk(KERN_WARNING 			       "%s: Local routing loop detected!\n",			       t->parms.name);		goto tx_err_dst_release;	}	mtu = dst_pmtu(dst) - sizeof (*ipv6h);	if (opt) {		max_headroom += 8;		mtu -= 8;	}	if (mtu < IPV6_MIN_MTU)		mtu = IPV6_MIN_MTU;	if (skb->dst && mtu < dst_pmtu(skb->dst)) {		struct rt6_info *rt = (struct rt6_info *) skb->dst;		rt->rt6i_flags |= RTF_MODIFIED;		rt->u.dst.metrics[RTAX_MTU-1] = mtu;	}	if (skb->len > mtu) {		icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);		goto tx_err_dst_release;	}	/*	 * Okay, now see if we can stuff it in the buffer as-is.	 */	max_headroom += LL_RESERVED_SPACE(tdev);		if (skb_headroom(skb) < max_headroom || 	    skb_cloned(skb) || skb_shared(skb)) {		struct sk_buff *new_skb;				if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))			goto tx_err_dst_release;		if (skb->sk)			skb_set_owner_w(new_skb, skb->sk);		kfree_skb(skb);		skb = new_skb;	}	dst_release(skb->dst);	skb->dst = dst_clone(dst);	skb->h.raw = skb->nh.raw;	if (opt)		ipv6_push_nfrag_opts(skb, opt, &proto, NULL);	skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));	ipv6h = skb->nh.ipv6h;	*(u32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);	dsfield = INET_ECN_encapsulate(0, dsfield);	ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);	ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));	ipv6h->hop_limit = t->parms.hop_limit;	ipv6h->nexthdr = proto;	ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);	ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);	nf_reset(skb);	pkt_len = skb->len;	err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, 		      skb->dst->dev, dst_output);	if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) {		stats->tx_bytes += pkt_len;		stats->tx_packets++;	} else {		stats->tx_errors++;		stats->tx_aborted_errors++;	}	ip6_tnl_dst_store(t, dst);	if (opt)		kfree(opt);	t->recursion--;	return 0;tx_err_link_failure:	stats->tx_carrier_errors++;	dst_link_failure(skb);tx_err_dst_release:	dst_release(dst);	if (opt)		kfree(opt);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;	struct in6_addr *laddr = &p->laddr;	struct in6_addr *raddr = &p->raddr;	int ltype = ipv6_addr_type(laddr);	int rtype = ipv6_addr_type(raddr);	p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);	if (ltype != IPV6_ADDR_ANY && rtype != IPV6_ADDR_ANY &&	    ((ltype|rtype) &	     (IPV6_ADDR_UNICAST|	      IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL|	      IPV6_ADDR_MAPPED|IPV6_ADDR_RESERVED)) == IPV6_ADDR_UNICAST) {		struct net_device *ldev = NULL;		int l_ok = 1;		int r_ok = 1;		if (p->link)			ldev = dev_get_by_index(p->link);				if (ltype&IPV6_ADDR_UNICAST && !ipv6_chk_addr(laddr, ldev, 0))			l_ok = 0;				if (rtype&IPV6_ADDR_UNICAST && ipv6_chk_addr(raddr, NULL, 0))			r_ok = 0;				if (l_ok && r_ok) {			if (ltype&IPV6_ADDR_UNICAST)				p->flags |= IP6_TNL_F_CAP_XMIT;			if (rtype&IPV6_ADDR_UNICAST)				p->flags |= IP6_TNL_F_CAP_RCV;		}		if (ldev)			dev_put(ldev);	}}static void ip6ip6_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) {		struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,						 p->link, 0);		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);	}}/** * ip6ip6_tnl_change - update the tunnel parameters *   @t: tunnel to be changed *   @p: tunnel configuration parameters *   @active: != 0 if tunnel is ready for use * * Description: *   ip6ip6_tnl_change() updates the tunnel parameters **/static intip6ip6_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;	ip6ip6_tnl_link_config(t);	return 0;}/** * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace  *   @dev: virtual device associated with tunnel *   @ifr: parameters passed from userspace *   @cmd: command to be performed * * Description: *   ip6ip6_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 intip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd){	int err = 0;	int create;	struct ip6_tnl_parm p;	struct ip6_tnl *t = NULL;	switch (cmd) {	case SIOCGETTUNNEL:		if (dev == ip6ip6_fb_tnl_dev) {			if (copy_from_user(&p,					   ifr->ifr_ifru.ifru_data,					   sizeof (p))) {				err = -EFAULT;				break;			}			if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV)				t = (struct ip6_tnl *) dev->priv;			else if (err)				break;		} else			t = (struct ip6_tnl *) dev->priv;		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;		create = (cmd == SIOCADDTUNNEL);		if (!capable(CAP_NET_ADMIN))			break;		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {			err = -EFAULT;			break;		}		if (!create && dev != ip6ip6_fb_tnl_dev) {			t = (struct ip6_tnl *) dev->priv;		}		if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) {			break;		}		if (cmd == SIOCCHGTUNNEL) {			if (t->dev != dev) {				err = -EEXIST;				break;			}			ip6ip6_tnl_unlink(t);			err = ip6ip6_tnl_change(t, &p);			ip6ip6_tnl_link(t);			netdev_state_change(dev);		}		if (copy_to_user(ifr->ifr_ifru.ifru_data,				 &t->parms, sizeof (p))) {			err = -EFAULT;		} else {			err = 0;		}		break;	case SIOCDELTUNNEL:		err = -EPERM;		if (!capable(CAP_NET_ADMIN))			break;		if (dev == ip6ip6_fb_tnl_dev) {			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,					   sizeof (p))) {				err = -EFAULT;				break;			}			err = ip6ip6_tnl_locate(&p, &t, 0);			if (err)				break;			if (t == ip6ip6_fb_tnl_dev->priv) {				err = -EPERM;				break;			}		} else {			t = (struct ip6_tnl *) dev->priv;		}		err = unregister_netdevice(t->dev);		break;	default:		err = -EINVAL;	}	return err;}/** * ip6ip6_tnl_get_stats - return the stats for tunnel device  *   @dev: virtual device associated with tunnel * * Return: stats for device **/static struct net_device_stats *ip6ip6_tnl_get_stats(struct net_device *dev){	return &(((struct ip6_tnl *) dev->priv)->stat);}/** * ip6ip6_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 intip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu){	if (new_mtu < IPV6_MIN_MTU) {		return -EINVAL;	}	dev->mtu = new_mtu;	return 0;}/** * ip6ip6_tnl_dev_setup - setup virtual tunnel device *   @dev: virtual device associated with tunnel * * Description: *   Initialize function pointers and device parameters **/static void ip6ip6_tnl_dev_setup(struct net_device *dev){	SET_MODULE_OWNER(dev);	dev->uninit = ip6ip6_tnl_dev_uninit;	dev->destructor = free_netdev;	dev->hard_start_xmit = ip6ip6_tnl_xmit;	dev->get_stats = ip6ip6_tnl_get_stats;	dev->do_ioctl = ip6ip6_tnl_ioctl;	dev->change_mtu = ip6ip6_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);}/** * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices *   @dev: virtual device associated with tunnel **/static inline voidip6ip6_tnl_dev_init_gen(struct net_device *dev){	struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;	t->fl.proto = IPPROTO_IPV6;	t->dev = dev;	strcpy(t->parms.name, dev->name);}/** * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices *   @dev: virtual device associated with tunnel **/static intip6ip6_tnl_dev_init(struct net_device *dev){	struct ip6_tnl *t = (struct ip6_tnl *) dev->priv;	ip6ip6_tnl_dev_init_gen(dev);	ip6ip6_tnl_link_config(t);	return 0;}/** * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device *   @dev: fallback device * * Return: 0 **/static int ip6ip6_fb_tnl_dev_init(struct net_device *dev){	struct ip6_tnl *t = dev->priv;	ip6ip6_tnl_dev_init_gen(dev);	dev_hold(dev);	tnls_wc[0] = t;	return 0;}static struct xfrm6_tunnel ip6ip6_handler = {	.handler = ip6ip6_rcv,	.err_handler = ip6ip6_err,};/** * 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(&ip6ip6_handler) < 0) {		printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");		return -EAGAIN;	}	ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",					 ip6ip6_tnl_dev_setup);	if (!ip6ip6_fb_tnl_dev) {		err = -ENOMEM;		goto fail;	}	ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;	if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {		free_netdev(ip6ip6_fb_tnl_dev);		goto fail;	}	return 0;fail:	xfrm6_tunnel_deregister(&ip6ip6_handler);	return err;}/** * ip6_tunnel_cleanup - free resources and unregister protocol **/static void __exit ip6_tunnel_cleanup(void){	if (xfrm6_tunnel_deregister(&ip6ip6_handler) < 0)		printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");	unregister_netdev(ip6ip6_fb_tnl_dev);}module_init(ip6_tunnel_init);module_exit(ip6_tunnel_cleanup);

⌨️ 快捷键说明

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