開局一張圖,內容全靠編,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 ! "); 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 doesnt 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 ! "); 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 ! "); 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 ! "); 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 ", (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. "); 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 doesnt seem to say what to do in the case that the * frame + options doesnt fit the mtu. As it used to fall down dead * in this case we were fortunate it didnt happen */ // mac頭的mtu小於8則直接返回,因為報文數據部分至少8個位元組 if(mtu<8) { /* Its wrong but its 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 doesnt 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! "); 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 "); 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函數進行分片,逐個分片發送出去。
TAG:Linux | Linux內核 |