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

📄 ip_gre.c

📁 GNU Hurd 源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	int    mtu;	if (tunnel->recursion++) {		tunnel->stat.collisions++;		goto tx_error;	}	if (dev->hard_header) {		gre_hlen = 0;		tiph = (struct iphdr*)skb->data;	} else {		gre_hlen = tunnel->hlen;		tiph = &tunnel->parms.iph;	}	if ((dst = tiph->daddr) == 0) {		/* NBMA tunnel */		if (skb->dst == NULL) {			tunnel->stat.tx_fifo_errors++;			goto tx_error;		}		if (skb->protocol == __constant_htons(ETH_P_IP)) {			rt = (struct rtable*)skb->dst;			if ((dst = rt->rt_gateway) == 0)				goto tx_error_icmp;		}#ifdef CONFIG_IPV6		else if (skb->protocol == __constant_htons(ETH_P_IPV6)) {			struct in6_addr *addr6;			int addr_type;			struct neighbour *neigh = skb->dst->neighbour;			if (neigh == NULL)				goto tx_error;			addr6 = (struct in6_addr*)&neigh->primary_key;			addr_type = ipv6_addr_type(addr6);			if (addr_type == IPV6_ADDR_ANY) {				addr6 = &skb->nh.ipv6h->daddr;				addr_type = ipv6_addr_type(addr6);			}			if ((addr_type & IPV6_ADDR_COMPATv4) == 0)				goto tx_error_icmp;			dst = addr6->s6_addr32[3];		}#endif		else			goto tx_error;	}	tos = tiph->tos;	if (tos&1) {		if (skb->protocol == __constant_htons(ETH_P_IP))			tos = old_iph->tos;		tos &= ~1;	}	if (ip_route_output(&rt, dst, tiph->saddr, RT_TOS(tos), tunnel->parms.link)) {		tunnel->stat.tx_carrier_errors++;		goto tx_error;	}	tdev = rt->u.dst.dev;	if (tdev == dev) {		ip_rt_put(rt);		tunnel->stat.collisions++;		goto tx_error;	}	df = tiph->frag_off;	mtu = rt->u.dst.pmtu - tunnel->hlen;	if (skb->protocol == __constant_htons(ETH_P_IP)) {		if (skb->dst && mtu < skb->dst->pmtu && mtu >= 68)			skb->dst->pmtu = mtu;		df |= (old_iph->frag_off&__constant_htons(IP_DF));		if ((old_iph->frag_off&__constant_htons(IP_DF)) &&		    mtu < ntohs(old_iph->tot_len)) {			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));			ip_rt_put(rt);			goto tx_error;		}	}#ifdef CONFIG_IPV6	else if (skb->protocol == __constant_htons(ETH_P_IPV6)) {		struct rt6_info *rt6 = (struct rt6_info*)skb->dst;		if (rt6 && mtu < rt6->u.dst.pmtu && mtu >= IPV6_MIN_MTU) {			if ((tunnel->parms.iph.daddr && !MULTICAST(tunnel->parms.iph.daddr)) ||			    rt6->rt6i_dst.plen == 128) {				rt6->rt6i_flags |= RTF_MODIFIED;				skb->dst->pmtu = mtu;			}		}		if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);			ip_rt_put(rt);			goto tx_error;		}	}#endif	if (tunnel->err_count > 0) {		if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {			tunnel->err_count--;			dst_link_failure(skb);		} else			tunnel->err_count = 0;	}	skb->h.raw = skb->nh.raw;	max_headroom = ((tdev->hard_header_len+15)&~15)+ gre_hlen;	if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {		struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);		if (!new_skb) {			ip_rt_put(rt);  			stats->tx_dropped++;			dev_kfree_skb(skb);			tunnel->recursion--;			return 0;		}		if (skb->sk)			skb_set_owner_w(new_skb, skb->sk);		dev_kfree_skb(skb);		skb = new_skb;	}	skb->nh.raw = skb_push(skb, gre_hlen);	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));	dst_release(skb->dst);	skb->dst = &rt->u.dst;	/*	 *	Push down and install the IPIP header.	 */	iph 			=	skb->nh.iph;	iph->version		=	4;	iph->ihl		=	sizeof(struct iphdr) >> 2;	iph->frag_off		=	df;	iph->protocol		=	IPPROTO_GRE;	iph->tos		=	tos;	iph->daddr		=	rt->rt_dst;	iph->saddr		=	rt->rt_src;	if ((iph->ttl = tiph->ttl) == 0) {		if (skb->protocol == __constant_htons(ETH_P_IP))			iph->ttl = old_iph->ttl;#ifdef CONFIG_IPV6		else if (skb->protocol == __constant_htons(ETH_P_IPV6))			iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;#endif		else			iph->ttl = ip_statistics.IpDefaultTTL;	}	((u16*)(iph+1))[0] = tunnel->parms.o_flags;	((u16*)(iph+1))[1] = skb->protocol;	if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {		u32 *ptr = (u32*)(((u8*)iph) + tunnel->hlen - 4);		if (tunnel->parms.o_flags&GRE_SEQ) {			++tunnel->o_seqno;			*ptr = htonl(tunnel->o_seqno);			ptr--;		}		if (tunnel->parms.o_flags&GRE_KEY) {			*ptr = tunnel->parms.o_key;			ptr--;		}		if (tunnel->parms.o_flags&GRE_CSUM) {			*ptr = 0;			*(__u16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));		}	}	iph->tot_len		=	htons(skb->len);	iph->id			=	htons(ip_id_count++);	ip_send_check(iph);	stats->tx_bytes += skb->len;	stats->tx_packets++;	ip_send(skb);	tunnel->recursion--;	return 0;tx_error_icmp:	dst_link_failure(skb);tx_error:	stats->tx_errors++;	dev_kfree_skb(skb);	tunnel->recursion--;	return 0;}static intipgre_tunnel_ioctl (struct device *dev, struct ifreq *ifr, int cmd){	int err = 0;	struct ip_tunnel_parm p;	struct ip_tunnel *t;	MOD_INC_USE_COUNT;	switch (cmd) {	case SIOCGETTUNNEL:		t = NULL;		if (dev == &ipgre_fb_tunnel_dev) {			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {				err = -EFAULT;				break;			}			t = ipgre_tunnel_locate(&p, 0);		}		if (t == NULL)			t = (struct ip_tunnel*)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;		if (!capable(CAP_NET_ADMIN))			goto done;		err = -EFAULT;		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))			goto done;		err = -EINVAL;		if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||		    p.iph.ihl != 5 || (p.iph.frag_off&__constant_htons(~IP_DF)) ||		    ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))			goto done;		if (p.iph.ttl)			p.iph.frag_off |= __constant_htons(IP_DF);		if (!(p.i_flags&GRE_KEY))			p.i_key = 0;		if (!(p.o_flags&GRE_KEY))			p.o_key = 0;		t = ipgre_tunnel_locate(&p, cmd == SIOCADDTUNNEL);		if (dev != &ipgre_fb_tunnel_dev && cmd == SIOCCHGTUNNEL &&		    t != &ipgre_fb_tunnel) {			if (t != NULL) {				if (t->dev != dev) {					err = -EEXIST;					break;				}			} else {				unsigned nflags=0;				t = (struct ip_tunnel*)dev->priv;				if (MULTICAST(p.iph.daddr))					nflags = IFF_BROADCAST;				else if (p.iph.daddr)					nflags = IFF_POINTOPOINT;				if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {					err = -EINVAL;					break;				}				start_bh_atomic();				ipgre_tunnel_unlink(t);				t->parms.iph.saddr = p.iph.saddr;				t->parms.iph.daddr = p.iph.daddr;				t->parms.i_key = p.i_key;				t->parms.o_key = p.o_key;				memcpy(dev->dev_addr, &p.iph.saddr, 4);				memcpy(dev->broadcast, &p.iph.daddr, 4);				ipgre_tunnel_link(t);				end_bh_atomic();				netdev_state_change(dev);			}		}		if (t) {			err = 0;			if (cmd == SIOCCHGTUNNEL) {				t->parms.iph.ttl = p.iph.ttl;				t->parms.iph.tos = p.iph.tos;				t->parms.iph.frag_off = p.iph.frag_off;			}			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))			goto done;		if (dev == &ipgre_fb_tunnel_dev) {			err = -EFAULT;			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))				goto done;			err = -ENOENT;			if ((t = ipgre_tunnel_locate(&p, 0)) == NULL)				goto done;			err = -EPERM;			if (t == &ipgre_fb_tunnel)				goto done;		}		err = unregister_netdevice(dev);		break;	default:		err = -EINVAL;	}done:	MOD_DEC_USE_COUNT;	return err;}static struct net_device_stats *ipgre_tunnel_get_stats(struct device *dev){	return &(((struct ip_tunnel*)dev->priv)->stat);}static int ipgre_tunnel_change_mtu(struct device *dev, int new_mtu){	struct ip_tunnel *tunnel = (struct ip_tunnel*)dev->priv;	if (new_mtu < 68 || new_mtu > 0xFFF8 - tunnel->hlen)		return -EINVAL;	dev->mtu = new_mtu;	return 0;}#ifdef CONFIG_NET_IPGRE_BROADCAST/* Nice toy. Unfortunately, useless in real life :-)   It allows to construct virtual multiprotocol broadcast "LAN"   over the Internet, provided multicast routing is tuned.   I have no idea was this bicycle invented before me,   so that I had to set ARPHRD_IPGRE to a random value.   I have an impression, that Cisco could make something similar,   but this feature is apparently missing in IOS<=11.2(8).      I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks   with broadcast 224.66.66.66. If you have access to mbone, play with me :-)   ping -t 255 224.66.66.66   If nobody answers, mbone does not work.   ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255   ip addr add 10.66.66.<somewhat>/24 dev Universe   ifconfig Universe up   ifconfig Universe add fe80::<Your_real_addr>/10   ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96   ftp 10.66.66.66   ...   ftp fec0:6666:6666::193.233.7.65   ... */static int ipgre_header(struct sk_buff *skb, struct device *dev, unsigned short type,			void *daddr, void *saddr, unsigned len){	struct ip_tunnel *t = (struct ip_tunnel*)dev->priv;	struct iphdr *iph = (struct iphdr *)skb_push(skb, t->hlen);	u16 *p = (u16*)(iph+1);	memcpy(iph, &t->parms.iph, sizeof(struct iphdr));	p[0]		= t->parms.o_flags;	p[1]		= htons(type);	/*	 *	Set the source hardware address. 	 */	 	if (saddr)		memcpy(&iph->saddr, saddr, 4);	if (daddr) {		memcpy(&iph->daddr, daddr, 4);		return t->hlen;	}	if (iph->daddr && !MULTICAST(iph->daddr))		return t->hlen;		return -t->hlen;}static int ipgre_open(struct device *dev){	struct ip_tunnel *t = (struct ip_tunnel*)dev->priv;	MOD_INC_USE_COUNT;	if (MULTICAST(t->parms.iph.daddr)) {		struct rtable *rt;		if (ip_route_output(&rt, t->parms.iph.daddr,				    t->parms.iph.saddr, RT_TOS(t->parms.iph.tos), 				    t->parms.link)) {			MOD_DEC_USE_COUNT;			return -EADDRNOTAVAIL;		}		dev = rt->u.dst.dev;		ip_rt_put(rt);		if (dev->ip_ptr == NULL) {			MOD_DEC_USE_COUNT;			return -EADDRNOTAVAIL;		}		t->mlink = dev->ifindex;		ip_mc_inc_group(dev->ip_ptr, t->parms.iph.daddr);	}	return 0;}static int ipgre_close(struct device *dev){	struct ip_tunnel *t = (struct ip_tunnel*)dev->priv;	if (MULTICAST(t->parms.iph.daddr) && t->mlink) {		dev = dev_get_by_index(t->mlink);		if (dev && dev->ip_ptr)			ip_mc_dec_group(dev->ip_ptr, t->parms.iph.daddr);	}	MOD_DEC_USE_COUNT;	return 0;}#endifstatic void ipgre_tunnel_init_gen(struct device *dev){	struct ip_tunnel *t = (struct ip_tunnel*)dev->priv;	dev->destructor		= ipgre_tunnel_destroy;	dev->hard_start_xmit	= ipgre_tunnel_xmit;	dev->get_stats		= ipgre_tunnel_get_stats;	dev->do_ioctl		= ipgre_tunnel_ioctl;	dev->change_mtu		= ipgre_tunnel_change_mtu;	dev_init_buffers(dev);	dev->type		= ARPHRD_IPGRE;	dev->hard_header_len 	= LL_MAX_HEADER + sizeof(struct iphdr) + 4;	dev->mtu		= 1500 - sizeof(struct iphdr) - 4;	dev->flags		= IFF_NOARP;	dev->iflink		= 0;	dev->addr_len		= 4;	memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);	memcpy(dev->broadcast, &t->parms.iph.daddr, 4);}static int ipgre_tunnel_init(struct device *dev){	struct device *tdev = NULL;	struct ip_tunnel *tunnel;	struct iphdr *iph;	int hlen = LL_MAX_HEADER;	int mtu = 1500;	int addend = sizeof(struct iphdr) + 4;	tunnel = (struct ip_tunnel*)dev->priv;	iph = &tunnel->parms.iph;	ipgre_tunnel_init_gen(dev);	/* Guess output device to choose reasonable mtu and hard_header_len */	if (iph->daddr) {		struct rtable *rt;		if (!ip_route_output(&rt, iph->daddr, iph->saddr, RT_TOS(iph->tos), tunnel->parms.link)) {			tdev = rt->u.dst.dev;			ip_rt_put(rt);		}		dev->flags |= IFF_POINTOPOINT;#ifdef CONFIG_NET_IPGRE_BROADCAST		if (MULTICAST(iph->daddr)) {			if (!iph->saddr)				return -EINVAL;			dev->flags = IFF_BROADCAST;			dev->hard_header = ipgre_header;			dev->open = ipgre_open;			dev->stop = ipgre_close;		}#endif	}	if (!tdev && tunnel->parms.link)		tdev = dev_get_by_index(tunnel->parms.link);	if (tdev) {		hlen = tdev->hard_header_len;		mtu = tdev->mtu;	}	dev->iflink = tunnel->parms.link;	/* Precalculate GRE options length */	if (tunnel->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {		if (tunnel->parms.o_flags&GRE_CSUM)			addend += 4;		if (tunnel->parms.o_flags&GRE_KEY)			addend += 4;		if (tunnel->parms.o_flags&GRE_SEQ)			addend += 4;	}	dev->hard_header_len = hlen + addend;	dev->mtu = mtu - addend;	tunnel->hlen = addend;	return 0;}#ifdef MODULEstatic int ipgre_fb_tunnel_open(struct device *dev){	MOD_INC_USE_COUNT;	return 0;}static int ipgre_fb_tunnel_close(struct device *dev){	MOD_DEC_USE_COUNT;	return 0;}#endif__initfunc(int ipgre_fb_tunnel_init(struct device *dev)){	struct ip_tunnel *tunnel = (struct ip_tunnel*)dev->priv;	struct iphdr *iph;	ipgre_tunnel_init_gen(dev);#ifdef MODULE	dev->open		= ipgre_fb_tunnel_open;	dev->stop		= ipgre_fb_tunnel_close;#endif	iph = &ipgre_fb_tunnel.parms.iph;	iph->version		= 4;	iph->protocol		= IPPROTO_GRE;	iph->ihl		= 5;	tunnel->hlen		= sizeof(struct iphdr) + 4;	tunnels_wc[0]		= &ipgre_fb_tunnel;	return 0;}static struct inet_protocol ipgre_protocol = {  ipgre_rcv,             /* GRE handler          */  ipgre_err,             /* TUNNEL error control */  0,                    /* next                 */  IPPROTO_GRE,          /* protocol ID          */  0,                    /* copy                 */  NULL,                 /* data                 */  "GRE"                 /* name                 */};/* *	And now the modules code and kernel interface. */#ifdef MODULEint init_module(void) #else__initfunc(int ipgre_init(void))#endif{	printk(KERN_INFO "GRE over IPv4 tunneling driver\n");	ipgre_fb_tunnel_dev.priv = (void*)&ipgre_fb_tunnel;	ipgre_fb_tunnel_dev.name = ipgre_fb_tunnel.parms.name;#ifdef MODULE	register_netdev(&ipgre_fb_tunnel_dev);#else	register_netdevice(&ipgre_fb_tunnel_dev);#endif	inet_add_protocol(&ipgre_protocol);	return 0;}#ifdef MODULEvoid cleanup_module(void){	if ( inet_del_protocol(&ipgre_protocol) < 0 )		printk(KERN_INFO "ipgre close: can't remove protocol\n");	unregister_netdev(&ipgre_fb_tunnel_dev);}#endif

⌨️ 快捷键说明

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