close

Linux netfilter源碼分析(1)

  • IP報文的接收到hook函數的調用

1.1  ip_input.c    ip_rcv()函數

以接收到的報文為例,類似的還有ip_forward(ip_forward.c)ip_output(ip_output.c)

int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{

    struct iphdr *iph;   //定義一個ip報文的數據報頭

    u32 len;

    if (skb->pkt_type == PACKET_OTHERHOST)
       goto drop; 
//數據包不是發給我們的

    IP_INC_STATS_BH(IPSTATS_MIB_INRECEIVES); //收到數據包統計量加1

    if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)

   {

/* 如果數據報是共享的,則複製一個出來,此時複製而出的已經和socket脫離了關係 */
      IP_INC_STATS_BH(IPSTATS_MIB_INDISCARDS);
      goto out;
   }

   if (!pskb_may_pull(skb, sizeof(struct iphdr)))
     goto inhdr_error; 
//對數據報的頭長度進行檢查,

 

   iph = skb->nh.iph;  //取得數據報的頭部位置

  if (iph->ihl < 5 || iph->version != 4)  //版本號或者頭長度不對,
    goto inhdr_error; //頭長度是以4字節為單位的,所以5表示的是20字節

  if (!pskb_may_pull(skb, iph->ihl*4))
    goto inhdr_error;

 

  if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
     goto inhdr_error;
//檢查報文的檢驗和字段

  len = ntohs(iph->tot_len);
 if (skb->len < len || len < (iph->ihl*4))
    goto inhdr_error;
//整個報文長度不可能比報頭長度小

 if (pskb_trim_rcsum(skb, len))

 { //對數據報進行裁減,這樣可以分片發送過來的數據報不會有重複數據
  IP_INC_STATS_BH(IPSTATS_MIB_INDISCARDS);
  goto drop;
 }

   return NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, dev, NULL,
         ip_rcv_finish);
//通過回調函數調用ip_rcv_finish


inhdr_error:
 IP_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
drop:
        kfree_skb(skb);
//丟掉數據報
out:
        return NET_RX_DROP;
}

 

 

 

1.2  include/linux/netfilter.h  NF_HOOK

 

#ifdef CONFIG_NETFILTER_DEBUG

#define NF_HOOK(pf, hook, skb, indev, outdev, okfn)                     \

 nf_hook_slow((pf), (hook), (skb), (indev), (outdev), (okfn), INT_MIN)

#define NF_HOOK_THRESH nf_hook_slow

#else

#define NF_HOOK(pf, hook, skb, indev, outdev, okfn)                     \

(list_empty(&nf_hooks[(pf)][(hook)])                                  \

 ? (okfn)(skb)                                                       \

 : nf_hook_slow((pf), (hook), (skb), (indev), (outdev), (okfn), INT_MIN))

#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh)       \

(list_empty(&nf_hooks[(pf)][(hook)])                                  \

 ? (okfn)(skb)                                                       \

 : nf_hook_slow((pf), (hook), (skb), (indev), (outdev), (okfn), (thresh)))

#endif

 

/*    如果nf_hooks[PF_INET][NF_IP_FORWARD]所指向的鏈表為空(即該鉤子上沒有掛處理函數),則直接調用okfn;否則,則調用net/core/netfilter.c::nf_hook_slow()轉入Netfilter的處理。  */

 

 

1.3  net/core/netfilter.c  nf_kook_slow()函數

 

int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
                 struct net_device *indev,
                 struct net_device *outdev,
                 int (*okfn)(struct sk_buff *),
                 int hook_thresh)
{
        struct list_head *elem;
        unsigned int verdict;
        int ret = 0;

        rcu_read_lock();

        /*
取得對應的鏈表首部*/
        elem = &nf_hooks[pf][hook];
next_hook:
        /*
調用對應的鉤子函數*/
        verdict = nf_iterate(&nf_hooks[pf][hook], pskb, hook, indev,
                             outdev, &elem, okfn, hook_thresh);


        /*判斷返回值,做相應的處理*/
if (verdict == NF_ACCEPT || verdict == NF_STOP) {
         ret = 1;    /*
前面提到過,返回1,則表示裝繼續調用okfn函數指針*/
         goto unlock;
    } else if (verdict == NF_DROP) {
         kfree_skb(*pskb);                /*
刪除數據包,需要釋放skb*/
          ret = -EPERM;
    } else if (verdict == NF_QUEUE) {
          NFDEBUG("nf_hook: Verdict = QUEUE.\n");
              if (!nf_queue(*pskb, elem, pf, hook, indev, outdev, okfn))
               goto next_hook;
     }
unlock:
        rcu_read_unlock();
        return ret;
}

 

1.4   net/core/netfilter.c   nf_iterate()函數

 

static unsigned int nf_iterate(struct list_head *head,

                            struct sk_buff **skb,

                            int hook,

                            const struct net_device *indev,

                            const struct net_device *outdev,

                            struct list_head **i,

                            int (*okfn)(struct sk_buff *),

                            int hook_thresh)

{

       /*

        * The caller must not block between calls to this

        * function because of risk of continuing from deleted element.

        */

/* 依次調用指定hook點下的所有nf_hook_ops->(*hook)函數,這些nf_hook_ops裡有filter表註冊的,有mangle表註冊的,等等。

list_for_each_continue_rcu函數是一個for循環的宏,當調用結點中的hook函數後,根據返回值進行相應處理。如果hook函數的返回值是NF_QUEUE,NF_STOLEN,NF_DROP時,函數返回該值;如果返回值是NF_REPEAT時,則跳到前一個結點繼續處理;如果是其他值,由下一個結點繼續處理。如果整條鏈表處理完畢,返回值不是上面四個值,則返回NF_ACCEPT*/

       list_for_each_continue_rcu(*i, head) {

              struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;

 

              if (hook_thresh > elem->priority)

                     continue;

 

              switch (elem->hook(hook, skb, indev, outdev, okfn)) {

              case NF_QUEUE:

                     return NF_QUEUE;

 

              case NF_STOLEN:

                     return NF_STOLEN;

 

              case NF_DROP:

                     return NF_DROP;

 

              case NF_REPEAT:

                     *i = (*i)->prev;

                     break;

              }

       }

       return NF_ACCEPT;

}

 

轉貼自:http://alexanderlaw.blog.hexun.com/8960896_d.html

arrow
arrow
    文章標籤
    iptable ip_rcv
    全站熱搜
    創作者介紹
    創作者 lyt0112 的頭像
    lyt0112

    小廷的部落格

    lyt0112 發表在 痞客邦 留言(0) 人氣()