ip分片源码解析(基于linux1.2.13)

ip分片源码解析(基于linux1.2.13)_第1张图片
开局一张图,内容全靠编,ip分片的处理过程使用的数据结构如上图所示。每各ipq结构体负责一个ip数据包的分片处理,每个ipfrag结构体代表一个ip数据包中的一个分片。全局指针ipqueue管理所有ip数据包的所有分片。

// 创建一个表示ip分片的结构体
static struct ipfrag *ip_frag_create(int offset, int end, struct sk_buff *skb, unsigned char *ptr)
{
	struct ipfrag *fp;

	fp = (struct ipfrag *) kmalloc(sizeof(struct ipfrag), GFP_ATOMIC);
	if (fp == NULL)
	{
		printk("IP: frag_create: no memory left !\n");
		return(NULL);
	}
	memset(fp, 0, sizeof(struct ipfrag));

	/* Fill in the structure. */
	fp->offset = offset; // ip分配的首字节在未分片数据中的偏移
	fp->end = end; // 最后一个字节的偏移 + 1,即下一个分片的首字节偏移
	fp->len = end - offset; // 分片长度
	fp->skb = skb;
	fp->ptr = ptr; // 指向分片的数据首地址

	return(fp);
}
// 根据ip头找到分片队列的头指针
static struct ipq *ip_find(struct iphdr *iph)
{
	struct ipq *qp;
	struct ipq *qplast;

	cli();
	qplast = NULL;
	for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next)
	{	// 对比ip头里的几个字段
		if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
			iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol)
		{	// 找到后重置计时器,在这删除,在ip_find外面新增一个计时
			del_timer(&qp->timer);	/* So it doesn't vanish on us. The timer will be reset anyway */
			sti();
			return(qp);
		}
	}
	sti();
	return(NULL);
}
// 释放ip分片队列
static void ip_free(struct ipq *qp)
{
	struct ipfrag *fp;
	struct ipfrag *xp;

	/*
	 * Stop the timer for this entry.
	 */
	// 删除定时器
	del_timer(&qp->timer);

	/* Remove this entry from the "incomplete datagrams" queue. */
	cli();
	/* 
		被删除的节点前面没有节点说明他是第一个节点,因为不是循环链表,
		修改首指针ipqueue指向被删除节点的下一个,如果下一个不为空,下一个节点的prev节点指向空,
		因为这时候他为第一个节点。
	*/
	if (qp->prev == NULL)
	{
		ipqueue = qp->next;
		if (ipqueue != NULL)
			ipqueue->prev = NULL;
	}
	else
	{	
		/*
			被删除节点不是第一个节点,但可能是最后一个,
			被删除节点的前一个节点的next指针指向被删除节点的下一个节点,
			如果如果被删除节点的下一个节点不为空则他的prev指针执行被删除节点
			前面的节点
		*/
		qp->prev->next = qp->next;
		if (qp->next != NULL)
			qp->next->prev = qp->prev;
	}

	/* Release all fragment data. */

	fp = qp->fragments;
	// 删除所有分片节点
	while (fp != NULL)
	{
		xp = fp->next;
		IS_SKB(fp->skb);
		kfree_skb(fp->skb,FREE_READ);
		kfree_s(fp, sizeof(struct ipfrag));
		fp = xp;
	}
	// 删除mac头和ip头,8字节是icmp用的,存放传输层的前8个字节
	/* Release the MAC header. */
	kfree_s(qp->mac, qp->maclen);

	/* Release the IP header. */
	kfree_s(qp->iph, qp->ihlen + 8);

	/* Finally, release the queue descriptor itself. */
	kfree_s(qp, sizeof(struct ipq));
	sti();
}
// 分片重组超时处理函数
static void ip_expire(unsigned long arg)
{
	struct ipq *qp;

	qp = (struct ipq *)arg;

	/*
	 *	Send an ICMP "Fragment Reassembly Timeout" message.
	 */

	ip_statistics.IpReasmTimeout++;
	ip_statistics.IpReasmFails++;   
	/* This if is always true... shrug */
	// 发送icmp超时报文
	if(qp->fragments!=NULL)
		icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
				ICMP_EXC_FRAGTIME, 0, qp->dev);

	/*
	 *	Nuke the fragment queue.
	 */
	// 释放分片队列
	ip_free(qp);
}
// 创建一个队列用于重组分片
static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev)
{
	struct ipq *qp;
	int maclen;
	int ihlen;
	// 分片一个新的表示分片队列的节点
	qp = (struct ipq *) kmalloc(sizeof(struct ipq), GFP_ATOMIC);
	if (qp == NULL)
	{
		printk("IP: create: no memory left !\n");
		return(NULL);
		skb->dev = qp->dev;
	}
	memset(qp, 0, sizeof(struct ipq));

	/*
	 *	Allocate memory for the MAC header.
	 *
	 *	FIXME: We have a maximum MAC address size limit and define
	 *	elsewhere. We should use it here and avoid the 3 kmalloc() calls
	 */
	// mac头长度等于ip头减去mac头首地址
	maclen = ((unsigned long) iph) - ((unsigned long) skb->data);
	qp->mac = (unsigned char *) kmalloc(maclen, GFP_ATOMIC);
	if (qp->mac == NULL)
	{
		printk("IP: create: no memory left !\n");
		kfree_s(qp, sizeof(struct ipq));
		return(NULL);
	}

	/*
	 *	Allocate memory for the IP header (plus 8 octets for ICMP).
	 */
	// ip头长度由ip头字段得出,多分配8个字节给icmp
	ihlen = (iph->ihl * sizeof(unsigned long));
	qp->iph = (struct iphdr *) kmalloc(ihlen + 8, GFP_ATOMIC);
	if (qp->iph == NULL)
	{
		printk("IP: create: no memory left !\n");
		kfree_s(qp->mac, maclen);
		kfree_s(qp, sizeof(struct ipq));
		return(NULL);
	}

	/* Fill in the structure. */
	// 把mac头内容复制到mac字段
	memcpy(qp->mac, skb->data, maclen);
	// 把ip头和传输层的8个字节复制到iph字段,8个字段的内容用于发送icmp报文时
	memcpy(qp->iph, iph, ihlen + 8);
	// 未分片的ip报文的总长度,未知,收到所有分片后重新赋值
	qp->len = 0;
	// 当前分片的ip头和mac头长度
	qp->ihlen = ihlen;
	qp->maclen = maclen;
	qp->fragments = NULL;
	qp->dev = dev;

	/* Start a timer for this entry. */
	// 开始计时,一定时间内还没收到所有分片则重组失败,发送icmp报文
	qp->timer.expires = IP_FRAG_TIME;		/* about 30 seconds	*/
	qp->timer.data = (unsigned long) qp;		/* pointer to queue	*/
	qp->timer.function = ip_expire;			/* expire function	*/
	add_timer(&qp->timer);

	/* Add this entry to the queue. */
	qp->prev = NULL;
	cli();
	// 头插法插入分片重组的队列
	qp->next = ipqueue;
	// 如果当前新增的节点不是第一个节点则把当前第一个节点的prev指针指向新增的节点
	if (qp->next != NULL)
		qp->next->prev = qp;
	//更新ipqueue指向新增的节点,新增节点是首节点 
	ipqueue = qp;
	sti();
	return(qp);
}
// 判断分片是否全部到达
static int ip_done(struct ipq *qp)
{
	struct ipfrag *fp;
	int offset;

	/* Only possible if we received the final fragment. */
	// 收到最后分片的时候会更新len字段,如果没有收到他就是初始化0,所以为0说明最后一个分片还没到达,直接返回未完成
	if (qp->len == 0)
		return(0);
	// 走到这里说明全部分片已经到达
	/* Check all fragment offsets to see if they connect. */
	fp = qp->fragments;
	offset = 0;
	// 检查所有分片,每个分片时按照偏移从小到大排序的链表,因为每次分片节点到达时会插入相应的位置
	while (fp != NULL)
	{	/*
			如果当前节点的偏移大于期待的偏移(即上一个节点的最后一个字节的偏移+1,由end字段表示),
			说明有中间节点没到达,直接返回未完成
		*/
		if (fp->offset > offset)
			return(0);	/* fragment(s) missing */
		offset = fp->end;
		fp = fp->next;
	}

	/* All fragments are present. */
	// 分片全部到达并且每个分片的字节连续则重组完成
	return(1);
}
// 重组成功后构造完整的ip报文
static struct sk_buff *ip_glue(struct ipq *qp)
{
	struct sk_buff *skb;
	struct iphdr *iph;
	struct ipfrag *fp;
	unsigned char *ptr;
	int count, len;

	/*
	 *	Allocate a new buffer for the datagram.
	 */
	// 整个包的长度等于mac头长度+ip头长度+数据长度
	len = qp->maclen + qp->ihlen + qp->len;
	// 分配新的skb	
	if ((skb = alloc_skb(len,GFP_ATOMIC)) == NULL)
	{
		ip_statistics.IpReasmFails++;
		printk("IP: queue_glue: no memory for gluing queue 0x%X\n", (int) qp);
		ip_free(qp);
		return(NULL);
	}

	/* Fill in the basic details. */
	// 这里应该是等于qp->len?
	skb->len = (len - qp->maclen);
	skb->h.raw = skb->data; // data字段指向新分配的内存首地址
	skb->free = 1;

	/* Copy the original MAC and IP headers into the new buffer. */
	ptr = (unsigned char *) skb->h.raw;
	memcpy(ptr, ((unsigned char *) qp->mac), qp->maclen); // 把mac头复制到新的内存
	ptr += qp->maclen;
	memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen); // 把ip头复制到新的内存
	ptr += qp->ihlen; // 指向数据部分的首地址
	skb->h.raw += qp->maclen;// 指向ip头首地址

	count = 0;

	/* Copy the data portions of all fragments into the new buffer. */
	fp = qp->fragments;
	// 开始复制数据部分
	while(fp != NULL)
	{	// 如果当前节点的数据长度+已经复制的内容长度大于skb->len则说明内容溢出了,丢弃该数据包
		if(count+fp->len > skb->len)
		{
			printk("Invalid fragment list: Fragment over size.\n");
			ip_free(qp);
			kfree_skb(skb,FREE_WRITE);
			ip_statistics.IpReasmFails++;
			return NULL;
		}
		// 把分片中的数据复制到对应偏移的位置 
		memcpy((ptr + fp->offset), fp->ptr, fp->len);
		// 已复制的数据长度
		count += fp->len;
		fp = fp->next;
	}

	/* We glued together all fragments, so remove the queue entry. */
	ip_free(qp);// 数据复制完后可以释放分片队列了

	/* Done with all fragments. Fixup the new IP header. */
	iph = skb->h.iph; // 上面的raw字段指向了ip头首地址,skb->h.iph等价于raw字段的值
	iph->frag_off = 0; // 清除分片字段
	// 更新总长度为ip头+数据的长度
	iph->tot_len = htons((iph->ihl * sizeof(unsigned long)) + count);
	skb->ip_hdr = iph;

	ip_statistics.IpReasmOKs++;
	return(skb);
}
// 处理分片报文
static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
{
	struct ipfrag *prev, *next;
	struct ipfrag *tfp;
	struct ipq *qp;
	struct sk_buff *skb2;
	unsigned char *ptr;
	int flags, offset;
	int i, ihl, end;

	ip_statistics.IpReasmReqds++;

	/* Find the entry of this IP datagram in the "incomplete datagrams" queue. */
	qp = ip_find(iph); // 根据ip头找是否已经存在分片队列

	/* Is this a non-fragmented datagram? */
	offset = ntohs(iph->frag_off);
	flags = offset & ~IP_OFFSET; // 取得三个分片标记位
	offset &= IP_OFFSET; // 取得分片偏移
	// 如果没有更多分片了,并且offset=0(第一个分片),则属于出错,第一个分片后面肯定还有分片,否则干嘛要分片
	if (((flags & IP_MF) == 0) && (offset == 0))
	{
		if (qp != NULL)
			ip_free(qp);	/* Huh? How could this exist?? */
		return(skb);
	}
	// 偏移乘以8得到数据的真实偏移
	offset <<= 3;		/* offset is in 8-byte chunks */

	/*
	 * If the queue already existed, keep restarting its timer as long
	 * as we still are receiving fragments.  Otherwise, create a fresh
	 * queue entry.
	 */
	/*
		如果已经存在分片队列,说明之前已经有分片到达,重置计时器,所以超时的逻辑是,
		如果IP_FRAG_TIME时间内没有分片到达,则认为重组超时,这里没有以总时间来判断。
	*/
	if (qp != NULL)
	{
		del_timer(&qp->timer);
		qp->timer.expires = IP_FRAG_TIME;	/* about 30 seconds */
		qp->timer.data = (unsigned long) qp;	/* pointer to queue */
		qp->timer.function = ip_expire;		/* expire function */
		add_timer(&qp->timer);
	}
	else
	{
		/*
		 *	If we failed to create it, then discard the frame
		 */
		// 新建一个管理分片队列的节点
		if ((qp = ip_create(skb, iph, dev)) == NULL)
		{
			skb->sk = NULL;
			kfree_skb(skb, FREE_READ);
			ip_statistics.IpReasmFails++;
			return NULL;
		}
	}

	/*
	 *	Determine the position of this fragment.
	 */
	// ip头长度
	ihl = (iph->ihl * sizeof(unsigned long));
	// 偏移+数据部分长度等于end,end的值是最后一个字节+1
	end = offset + ntohs(iph->tot_len) - ihl;

	/*
	 *	Point into the IP datagram 'data' part.
	 */
	// data指向整个报文首地址,即mac头首地址,ptr指向ip报文的数据部分
	ptr = skb->data + dev->hard_header_len + ihl;

	/*
	 *	Is this the final fragment?
	 */
	// 是否是最后一个分片,是的话,未分片的ip报文长度为end,即最后一个报文的最后一个字节的偏移+1,因为偏移从0算起
	if ((flags & IP_MF) == 0)
		qp->len = end;

	/*
	 * 	Find out which fragments are in front and at the back of us
	 * 	in the chain of fragments so far.  We must know where to put
	 * 	this fragment, right?
	 */

	prev = NULL;
	// 插入分片队列相应的位置,保证分片的有序
	for(next = qp->fragments; next != NULL; next = next->next)
	{	// 找出第一个比当前分片偏移大的节点
		if (next->offset > offset)
			break;	/* bingo! */
		prev = next;
	}

	/*
	 * 	We found where to put this one.
	 * 	Check for overlap with preceding fragment, and, if needed,
	 * 	align things so that any overlaps are eliminated.
	 */
	// 处理分片重叠问题
	/*
		处理当前节点和前面节点的重叠问题,因为上面保证了offset >= prev->offset,
		所以只需要比较当前节点的偏移和prev节点的end字段
	*/
	if (prev != NULL && offset < prev->end)
	{	
		// 说明存在重叠,算出重叠的大小,把当前节点的重叠部分丢弃,更新offset和ptr指针往前走,没处理完全重叠的情况
		i = prev->end - offset;
		offset += i;	/* ptr into datagram */
		ptr += i;	/* ptr into fragment data */
	}

	/*
	 * Look for overlap with succeeding segments.
	 * If we can merge fragments, do it.
	 */
	// 处理当前节点和后面节点的重叠问题
	for(; next != NULL; next = tfp)
	{
		tfp = next->next;
		// 当前节点及其后面的节点都不会发生重叠了
		if (next->offset >= end)
			break;		/* no overlaps at all */
		// 反之发生了重叠,算出重叠大小
		i = end - next->offset;			/* overlap is 'i' bytes */
		// 更新和当前节点重叠的节点的字段,往后挪
		next->len -= i;				/* so reduce size of	*/
		next->offset += i;			/* next fragment	*/
		next->ptr += i;

		/*
		 *	If we get a frag size of <= 0, remove it and the packet
		 *	that it goes with.
		 */
		// 发生了完全重叠,则删除旧的节点
		if (next->len <= 0)
		{
			if (next->prev != NULL)
				next->prev->next = next->next;// 说明旧节点不是第一个节点
			else
				qp->fragments = next->next;//  说明旧节点是第一个节点
			// 这里应该是tfp !=NULL ?
			if (tfp->next != NULL)
				next->next->prev = next->prev;

			kfree_skb(next->skb,FREE_READ);
			kfree_s(next, sizeof(struct ipfrag));
		}
	}

	/*
	 *	Insert this fragment in the chain of fragments.
	 */

	tfp = NULL;
	// 创建一个分片节点
	tfp = ip_frag_create(offset, end, skb, ptr);

	/*
	 *	No memory to save the fragment - so throw the lot
	 */

	if (!tfp)
	{
		skb->sk = NULL;
		kfree_skb(skb, FREE_READ);
		return NULL;
	}
	// 插入分片队列
	tfp->prev = prev;
	tfp->next = next;
	if (prev != NULL)
		prev->next = tfp;
	else
		qp->fragments = tfp;

	if (next != NULL)
		next->prev = tfp;

	/*
	 * 	OK, so we inserted this new fragment into the chain.
	 * 	Check if we now have a full IP datagram which we can
	 * 	bump up to the IP layer...
	 */
	// 判断全部分片是否到达,是的话重组
	if (ip_done(qp))
	{
		skb2 = ip_glue(qp);		/* glue together the fragments */
		return(skb2);
	}
	return(NULL);
}
// ip分片处理,即发出去的ip包太大需要分片
void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
{
	struct iphdr *iph;
	unsigned char *raw;
	unsigned char *ptr;
	struct sk_buff *skb2;
	int left, mtu, hlen, len;
	int offset;
	unsigned long flags;

	/*
	 *	Point into the IP datagram header.
	 */
	// mac首地址
	raw = skb->data;
	// ip头首地址
	iph = (struct iphdr *) (raw + dev->hard_header_len);

	skb->ip_hdr = iph;

	/*
	 *	Setup starting values.
	 */

	hlen = (iph->ihl * sizeof(unsigned long));
	// 算出ip报文的数据长度
	left = ntohs(iph->tot_len) - hlen;	/* Space per frame */
	hlen += dev->hard_header_len;		/* Total header size */
	// 每个分片的数据部分长度等于mac层的mtu减去mac头和ip头
	mtu = (dev->mtu - hlen);		/* Size of data space */
	// 数据部分首地址
	ptr = (raw + hlen);			/* Where to start from */

	/*
	 *	Check for any "DF" flag. [DF means do not fragment]
	 */
	// 设置了不能分片则发送icmp报文
	if (ntohs(iph->frag_off) & IP_DF)
	{
		/*
		 *	Reply giving the MTU of the failed hop.
		 */
		ip_statistics.IpFragFails++;
		icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev->mtu, dev);
		return;
	}

	/*
	 *	The protocol doesn't seem to say what to do in the case that the
	 *	frame + options doesn't fit the mtu. As it used to fall down dead
	 *	in this case we were fortunate it didn't happen
	 */
	// mac头的mtu小于8则直接返回,因为报文数据部分至少8个字节
	if(mtu<8)
	{
		/* It's wrong but it's better than nothing */
		icmp_send(skb,ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED,dev->mtu, dev);
		ip_statistics.IpFragFails++;
		return;
	}

	// 该ip报文本身就是一个分片,现在需要进行再次分片,偏移的首地址是该报文的首地址乘以8
	if (is_frag & 2)
		offset = (ntohs(iph->frag_off) & 0x1fff) << 3;
	else
		offset = 0;


	/*
	 *	Keep copying data until we run out.
	 */
	// 开始分片
	while(left > 0)
	{
		len = left;
		/* IF: it doesn't fit, use 'mtu' - the data space left */
		// 大于mtu则继续分片,否则就是最后一个分片
		if (len > mtu)
			len = mtu;
		/* IF: we are not sending upto and including the packet end
		   then align the next start on an eight byte boundary */
		// 剩下的字节比mtu大的时候下面的判断会成立,则取8的倍数大小,不一定等于mtu 
		if (len < left)
		{
			len/=8;
			len*=8;
		}
		/*
		 *	Allocate buffer.
		 */
		// 分片新的skb,大小为mac头+ip头+数据部分长度
		if ((skb2 = alloc_skb(len + hlen,GFP_ATOMIC)) == NULL)
		{
			printk("IP: frag: no memory for new fragment!\n");
			ip_statistics.IpFragFails++;
			return;
		}

		/*
		 *	Set up data on packet
		 */

		skb2->arp = skb->arp;
		if(skb->free==0)
			printk("IP fragmenter: BUG free!=1 in fragmenter\n");
		skb2->free = 1;
		skb2->len = len + hlen;
		skb2->h.raw=(char *) skb2->data;
		/*
		 *	Charge the memory for the fragment to any owner
		 *	it might possess
		 */

		save_flags(flags);
		if (sk)
		{
			cli();
			sk->wmem_alloc += skb2->mem_len;
			skb2->sk=sk;
		}
		restore_flags(flags);
		skb2->raddr = skb->raddr;	/* For rebuild_header - must be here */

		/*
		 *	Copy the packet header into the new buffer.
		 */
		// 把mac报头和ip报头+选项都复制到skb中,选项应该只复制到第一个分片
		memcpy(skb2->h.raw, raw, hlen);

		/*
		 *	Copy a block of the IP datagram.
		 */
		// 复制数据部分,长度为len,ptr指向原ip报文的首地址,
		memcpy(skb2->h.raw + hlen, ptr, len);
		left -= len;
		// 指向ip头首地址
		skb2->h.raw+=dev->hard_header_len;

		/*
		 *	Fill in the new header fields.
		 */
		iph = (struct iphdr *)(skb2->h.raw/*+dev->hard_header_len*/);
		// 设置该分配的偏移
		iph->frag_off = htons((offset >> 3));
		/*
		 *	Added AC : If we are fragmenting a fragment thats not the
		 *		   last fragment then keep MF on each bit
		 */
		/*
			1. 还有数据
			2. 再分片的时候,该分片本身设置了分片flag,如果left大于MF置1,
			如果left=0,需要看原报文是否设置了MF,如果有,说明原报文后面还有报文,
			所以原报文下的所有分片MF都是1,如果原报文是最后一个报文,则MF=0,那对原报文分片的时候,
			最后一个分片的MF=0,其他的为1
		*/
		if (left > 0 || (is_frag & 1))
			iph->frag_off |= htons(IP_MF);
		// 更新数据指针和偏移
		ptr += len;
		offset += len;

		/*
		 *	Put this fragment into the sending queue.
		 */

		ip_statistics.IpFragCreates++;
		// 发送分片
		ip_queue_xmit(sk, dev, skb2, 2);
	}
	ip_statistics.IpFragOKs++;
}

ip层接收到链路层的数据包后,根据ip头的字段判断是否是分片或者是不是第一个分片,然后使用上面的几个函数和定时器的功能完成分配重组。ip层往外发送数据的时候,如果数据包太大则使用ip_fragment函数进行分片,逐个分片发送出去。

你可能感兴趣的:(linux1.2.13源码分析)