随笔-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 阅读(807) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理