• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-23  評論-73  文章-3  trackbacks-0

            IP碎片重組過程分析
             
            本文檔的Copyleft歸yfydz所有,使用GPL發布,可以自由拷貝,轉載,轉載時請保持文檔的完整性,嚴禁用于任何商業用途。
            msn: yfydz_no1@hotmail.com
            來源:http://yfydz.cublog.cn

            1. 前言
             
            對IP碎片的重組是防火墻提高安全性的一個重要手段,通過提前進行碎片重組,可以有效防御各種碎片攻擊,Linux內核的防火墻netfilter就自動對IP碎片包進行了重組,本文介紹Linux內核中的IP重組過程,內核代碼版本2.4.26。

            2. 處理流程
            實現IP重組的基本函數為ip_defrag(),在net/ipv4/ip_fragment.c中實現,基本過程是建立碎片處理隊列,隊列中每個節點是一個鏈表,這個鏈表保存同一個連接的碎片,當碎片都到達之后進行數據包重組,或者在一定時間(缺省30秒)內所有碎片包不能到達而釋放掉。

            2.1 數據結構
             
            在處理分片包時,將skb包的cb字段保存碎片控制信息struct ipfrag_skb_cb。
             
            #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
             
            struct ipfrag_skb_cb
            {
             struct inet_skb_parm h;
             int   offset;
            };
             
            ipq隊列節點結構:
             
            /* Describe an entry in the "incomplete datagrams" queue. */
            struct ipq {
            // 下一個
             struct ipq *next;  /* linked list pointers   */
            // 最新使用鏈表
             struct list_head lru_list; /* lru list member    */
            // 以下4項用來匹配一組IP分配
             u32  saddr;
             u32  daddr;
             u16  id;
             u8  protocol;
            // 狀態標志
             u8  last_in;
            #define COMPLETE  4   // 數據已經完整
            #define FIRST_IN  2   // 第一個包到達
            #define LAST_IN   1   // 最后一個包到達
            //  接收到的IP碎片鏈表
             struct sk_buff *fragments; /* linked list of received fragments */
            // len是根據最新IP碎片中的偏移信息得出的數據總長
             int  len;  /* total length of original datagram */
            // meat是所有碎片實際長度的累加
             int  meat;
             spinlock_t lock;
             atomic_t refcnt;
            // 超時
             struct timer_list timer; /* when will this queue expire?  */
            // 前一項隊列地址
             struct ipq **pprev;
            // 數據進入網卡的索引號
             int  iif;
            // 最新一個碎片的時間戳
             struct timeval stamp;
            };

            2.2 ip_defrag()函數:
             
            這是進行碎片重組的基本函數,返回重組后的skb包,或者返回NULL。

            struct sk_buff *ip_defrag(struct sk_buff *skb)
            {
             struct iphdr *iph = skb->nh.iph;
             struct ipq *qp;
             struct net_device *dev;
             
            // 統計信息
             IP_INC_STATS_BH(IpReasmReqds);
             /* Start by cleaning up the memory. */

            // 檢查已經分配的碎片內存是否超過所設置的上限
             if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh)
            // ip_evictor()函數釋放當前緩沖區中未能重組的數據包,使ip_frag_mem)小于
            // sysctl_ipfrag_low_thresh(緩沖低限)
              ip_evictor();
             dev = skb->dev;
             
             /* Lookup (or create) queue header */
            // 根據IP頭信息查找隊列節點
             if ((qp = ip_find(iph)) != NULL) {
              struct sk_buff *ret = NULL;
              spin_lock(&qp->lock);

            // skb數據包進入隊列節點鏈表
              ip_frag_queue(qp, skb);
              if (qp->last_in == (FIRST_IN|LAST_IN) &&
                  qp->meat == qp->len)

            // 滿足重組條件,對數據包進行重組,返回重組后的數據包
               ret = ip_frag_reasm(qp, dev);
              spin_unlock(&qp->lock);

            // 如果隊列節點使用數為0,釋放隊列節點
              ipq_put(qp);
              return ret;
             }
             
            // 找不到相關節點,丟棄該數據包
             IP_INC_STATS_BH(IpReasmFails);
             kfree_skb(skb);
             return NULL;
            }
             
            2.3 ip_find()函數
             
            ip_find()函數用于查找符合數據包的源、目的地址、協議和ID的隊列節點,找到后返回,如果找不到,則新建一個節點:
             
            static inline struct ipq *ip_find(struct iphdr *iph)
            {
             __u16 id = iph->id;
             __u32 saddr = iph->saddr;
             __u32 daddr = iph->daddr;
             __u8 protocol = iph->protocol;

            // 碎片隊列是以HASH表形式實現的
            // HASH函數使用源、目的地址、協議和ID四個IP頭參數進行
             unsigned int hash = ipqhashfn(id, saddr, daddr, protocol);
             struct ipq *qp;
             read_lock(&ipfrag_lock);
             for(qp = ipq_hash[hash]; qp; qp = qp->next) {
              if(qp->id == id  &&
                 qp->saddr == saddr &&
                 qp->daddr == daddr &&
                 qp->protocol == protocol) {
               atomic_inc(&qp->refcnt);
               read_unlock(&ipfrag_lock);
               return qp;
              }
             }
             read_unlock(&ipfrag_lock);
            // 如果不存在,新建隊列節點
             return ip_frag_create(hash, iph);
            }
             
            ip_frag_create()函數,返回一個碎片隊列節點
             
            static struct ipq *ip_frag_create(unsigned hash, struct iphdr *iph)
            {
             struct ipq *qp;
            // 分配一個新的碎片隊列節點
             if ((qp = frag_alloc_queue()) == NULL)
              goto out_nomem;
             qp->protocol = iph->protocol;
             qp->last_in = 0;
             qp->id = iph->id;
             qp->saddr = iph->saddr;
             qp->daddr = iph->daddr;
             qp->len = 0;
            // meat是當前隊列中所有碎片的長度總和
             qp->meat = 0;
             qp->fragments = NULL;
             qp->iif = 0;
             /* Initialize a timer for this entry. */

            // 隊列節點的定時器設置
             init_timer(&qp->timer);
             qp->timer.data = (unsigned long) qp; /* pointer to queue */

            // 超時處理,釋放內存,發送ICMP碎片超時錯誤
             qp->timer.function = ip_expire;  /* expire function */
             qp->lock = SPIN_LOCK_UNLOCKED;

            // 初始化隊列節點的使用數為1,注意不能是0
             atomic_set(&qp->refcnt, 1);
            // 將碎片節點放入隊列HASH表
             return ip_frag_intern(hash, qp);
             
            out_nomem:
             NETDEBUG(if (net_ratelimit()) printk(KERN_ERR "ip_frag_create: no memory left
            !\n"));
             return NULL;
            }
             
            2.4 ip_frag_queue()函數
             
            ip_frag_queue()函數將新來的skb包插入隊列節點中,這個函數是防御各種碎片攻擊的關鍵,要能處理各種異常的重組過程:
             
            // ping of death, teardrop等就是靠異常的碎片偏移來進行攻擊,因此需要仔細檢查
            // 是否碎片偏移是否異常

            static void ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
            {
             struct sk_buff *prev, *next;
             int flags, offset;
             int ihl, end;
             
            // 對已經有COMPLETE標志的隊列節點再來新數據包表示錯誤
             if (qp->last_in & COMPLETE)
              goto err;
             
            // 計算當前包的偏移值,IP頭中的偏移值只有13位,但表示的是8字節的倍數
              offset = ntohs(skb->nh.iph->frag_off);
             flags = offset & ~IP_OFFSET;
             offset &= IP_OFFSET;
             offset <<= 3;  /* offset is in 8-byte chunks */
              ihl = skb->nh.iph->ihl * 4;
             
             /* Determine the position of this fragment. */
            // end是當前包尾在完整包中的位置
              end = offset + skb->len - ihl;
             
             /* Is this the final fragment? */
             if ((flags & IP_MF) == 0) {
            // 已經沒有后續分片包了
              /* If we already have some bits beyond end
               * or have different end, the segment is corrrupted.
               */
              if (end < qp->len ||
                  ((qp->last_in & LAST_IN) && end != qp->len))
               goto err;
              qp->last_in |= LAST_IN;
              qp->len = end;
             } else {
            // 仍然存在后續的分片包,檢查數據長度是否是8字節對齊的
              if (end&7) {
               end &= ~7;
               if (skb->ip_summed != CHECKSUM_UNNECESSARY)
                skb->ip_summed = CHECKSUM_NONE;
              }

              if (end > qp->len) {
            // 長度超過當前記錄的長度
               /* Some bits beyond end -> corruption. */
               if (qp->last_in & LAST_IN)
                goto err;
               qp->len = end;
              }
             }

             if (end == offset)
              goto err;
             
            // 去掉IP頭部分,只保留數據部分
             if (pskb_pull(skb, ihl) == NULL)
              goto err;

            // 將skb包長度調整為end-offset, 該值為該skb包中的實際有效數據長度
             if (pskb_trim(skb, end-offset))
              goto err;
             
             /* 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 (FRAG_CB(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.
              */
            // 檢查偏移是否有重疊,重疊是允許的,只要是正確的
             if (prev) {
              int i = (FRAG_CB(prev)->offset + prev->len) - offset;
              if (i > 0) {
               offset += i;
               if (end <= offset)
                goto err;
               if (!pskb_pull(skb, i))
                goto err;
               if (skb->ip_summed != CHECKSUM_UNNECESSARY)
                skb->ip_summed = CHECKSUM_NONE;
              }
             }
             
            // 如果重疊,則隊列后面的所有包的偏移值都要調整,數據包長度的累加值也要相應減小
             while (next && FRAG_CB(next)->offset < end) {
              int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
              if (i < next->len) {
               /* Eat head of the next overlapped fragment
                * and leave the loop. The next ones cannot overlap.
                */
               if (!pskb_pull(next, i))
                goto err;
               FRAG_CB(next)->offset += i;
               qp->meat -= i;
               if (next->ip_summed != CHECKSUM_UNNECESSARY)
                next->ip_summed = CHECKSUM_NONE;
               break;
              } else {
               struct sk_buff *free_it = next;
               /* Old fragmnet is completely overridden with
                * new one drop it.
                */
               next = next->next;
               if (prev)
                prev->next = next;
               else
                qp->fragments = next;
               qp->meat -= free_it->len;
               frag_kfree_skb(free_it);
              }
             }
             
            // skb記錄自己的偏移值
             FRAG_CB(skb)->offset = offset;
            // 將當前的skb插入隊列
             /* Insert this fragment in the chain of fragments. */
             skb->next = next;
             if (prev)
              prev->next = skb;
             else
              qp->fragments = skb;
              if (skb->dev)
               qp->iif = skb->dev->ifindex;
             skb->dev = NULL;
            // 時間更新
             qp->stamp = skb->stamp;
            // 當前數據包總長累加
             qp->meat += skb->len;
            // 將skb大小加到碎片內存中
             atomic_add(skb->truesize, &ip_frag_mem);
             if (offset == 0)
              qp->last_in |= FIRST_IN;
             write_lock(&ipfrag_lock);

            // 調整碎片節點在最近使用隊列中的位置,在存儲區超過限值時先釋放的是最老的未用的
            // 那些碎片
             list_move_tail(&qp->lru_list, &ipq_lru_list);
             write_unlock(&ipfrag_lock);
             return;
             
            err:
            // 出錯時直接丟棄數據包,但隊列中已有的不釋放,如果重組失敗是等超時或
            // 超過碎片內存限值上限時釋放
             kfree_skb(skb);
            }

            2.5 ip_frag_reasm()函數
             
            ip_frag_reasm()函數實現最終的數據重組過程,是在所有數據都正確接收后進行
             
            static struct sk_buff *ip_frag_reasm(struct ipq *qp, struct net_device *dev)
            {
             struct iphdr *iph;
             struct sk_buff *fp, *head = qp->fragments;
             int len;
             int ihlen;
             
            // 將節點從鏈表中斷開,刪除定時器
             ipq_kill(qp);
             BUG_TRAP(head != NULL);
             BUG_TRAP(FRAG_CB(head)->offset == 0);
             /* Allocate a new buffer for the datagram. */
             ihlen = head->nh.iph->ihl*4;
             len = ihlen + qp->len;
             
            // IP總長過了限值丟棄
             if(len > 65535)
              goto out_oversize;
             /* Head of list must not be cloned. */
             if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
              goto out_nomem;
             
             /* If the first fragment is fragmented itself, we split
              * it to two chunks: the first with data and paged part
              * and the second, holding only fragments. */
             if (skb_shinfo(head)->frag_list) {
            // 隊列第一個skb不能是分片的,分片的話重新分配一個skb,自身數據長度為0,
            // 最終head的效果是這樣一個skb,自身不包括數據,但其end指針,也就是
            // struct skb_shared_info結構中的frag_list包含所有碎片skb,這也是skb
            // 的一種表現形式,不一定是一個連續的數據塊,但最終通過skb_linearize()
            // 函數將這些鏈表節點中的數據都復制到連續數據塊中
              struct sk_buff *clone;
              int i, plen = 0;
              if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
               goto out_nomem;
              clone->next = head->next;
              head->next = clone;
              skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
              skb_shinfo(head)->frag_list = NULL;
              for (i=0; i<skb_shinfo(head)->nr_frags; i++)
               plen += skb_shinfo(head)->frags[i].size;
              clone->len = clone->data_len = head->data_len - plen;
              head->data_len -= clone->len;
              head->len -= clone->len;
              clone->csum = 0;
              clone->ip_summed = head->ip_summed;
              atomic_add(clone->truesize, &ip_frag_mem);
             }
             
             skb_shinfo(head)->frag_list = head->next;
             skb_push(head, head->data - head->nh.raw);
             atomic_sub(head->truesize, &ip_frag_mem);
             
            // 依次將所有后續包數據長度累加,并將其長度從分配的內存計數中刪除
             for (fp=head->next; fp; fp = fp->next) {
              head->data_len += fp->len;
              head->len += fp->len;
              if (head->ip_summed != fp->ip_summed)
               head->ip_summed = CHECKSUM_NONE;
              else if (head->ip_summed == CHECKSUM_HW)
               head->csum = csum_add(head->csum, fp->csum);
              head->truesize += fp->truesize;
              atomic_sub(fp->truesize, &ip_frag_mem);
             }
             head->next = NULL;
             head->dev = dev;
             head->stamp = qp->stamp;
             
            // 對IP頭中的長度和偏移標志進行重置
             iph = head->nh.iph;
             iph->frag_off = 0;
             iph->tot_len = htons(len);
             IP_INC_STATS_BH(IpReasmOKs);

            // 各碎片skb已經得到處理,在釋放qp時將不再重新釋放了
             qp->fragments = NULL;
             return head;
            out_nomem:
              NETDEBUG(if (net_ratelimit())
                      printk(KERN_ERR
               "IP: queue_glue: no memory for gluing queue %p\n",
               qp));
             goto out_fail;
            out_oversize:
             if (net_ratelimit())
              printk(KERN_INFO
               "Oversized IP packet from %d.%d.%d.%d.\n",
               NIPQUAD(qp->saddr));
            out_fail:
             IP_INC_STATS_BH(IpReasmFails);
             return NULL;
            }
             
            2.6 ipq的釋放
             
            重組完成后就要將碎片隊列釋放掉:
             
            static __inline__ void ipq_put(struct ipq *ipq)
            {
             if (atomic_dec_and_test(&ipq->refcnt))
              ip_frag_destroy(ipq);
            }

            /* Complete destruction of ipq. */
            static void ip_frag_destroy(struct ipq *qp)
            {
             struct sk_buff *fp;
             BUG_TRAP(qp->last_in&COMPLETE);
             BUG_TRAP(del_timer(&qp->timer) == 0);
             /* Release all fragment data. */
             fp = qp->fragments;
             while (fp) {
              struct sk_buff *xp = fp->next;
            // 釋放每個碎片skb
              frag_kfree_skb(fp);
              fp = xp;
             }
             /* Finally, release the queue descriptor itself. */
            // 釋放碎片節點本身
             frag_free_queue(qp);
            }
             
            3. 結論
             
            linux的IP碎片重組過程中考慮了多種可能的異常,具有較大的安全性,因此在數據包進入netfilter架構前進行數據包的重組就可以防御各類碎片攻擊。

            posted on 2008-04-19 14:11 ViskerWong 閱讀(907) 評論(0)  編輯 收藏 引用
            大伊人青草狠狠久久| 亚洲国产二区三区久久| 亚洲国产成人久久一区WWW| 久久综合九色综合久99| 伊人久久大香线蕉亚洲五月天| 热re99久久6国产精品免费| 狠狠色丁香婷婷久久综合不卡| 久久久久国产一区二区三区| 亚洲国产精品一区二区久久hs| 精品综合久久久久久97超人| 久久www免费人成看国产片| 四虎久久影院| 国产精品激情综合久久| 亚洲欧美日韩久久精品第一区| 51久久夜色精品国产| 久久综合色之久久综合| av午夜福利一片免费看久久| 亚洲国产高清精品线久久| 久久国产精品99久久久久久老狼| 无码人妻久久一区二区三区蜜桃| 国产激情久久久久影院| 久久久久人妻一区二区三区vr| 老司机午夜网站国内精品久久久久久久久 | 一本久久a久久精品综合夜夜| 久久精品国产亚洲av麻豆蜜芽| 久久se精品一区精品二区国产| 91精品国产综合久久婷婷 | 久久久久人妻精品一区二区三区| 久久伊人中文无码| 日韩久久久久中文字幕人妻| 丰满少妇人妻久久久久久4| 国产成年无码久久久久毛片| 蜜臀av性久久久久蜜臀aⅴ麻豆| 久久久久久国产精品无码下载| 色8激情欧美成人久久综合电| 中文字幕一区二区三区久久网站 | 亚洲&#228;v永久无码精品天堂久久| 夜夜亚洲天天久久| 久久久久国产精品三级网| 色欲综合久久躁天天躁| 久久国产AVJUST麻豆|