Linux-Netfilter機制學習(一)

原文地址:http://blog.chinaunix.net/uid-22227409-id-2656911.html

Linux-Netfilter機制學習(一)

基於Linux2.6.30內核源代碼

鉤子函數的註冊管理

主要說明Netfilter鉤子函數的掛載點,hook函數的保存機制,註冊方式;

1)  hook的存儲機制

鉤子函數由一個全局二維鏈表數組nf_hooks保存

struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;

首先按照其協議族進行歸類存儲(第一維代表的意義),主要協議族包括IPV4IPV6等協議在內;在每個協議族中,根據鉤子點順序排列,Linux系統定義的最大掛載鉤子函數的地點爲8個,實際常用的爲5各,分別是NF_IP_PRE_ROUTING, NF_IP_LOCAL_IN, NF_IP_FORWARD, NF_IP_LOCAL_OUT, NF_IP_POST_ROUTING;在每個鉤子點內保存的則是按照鉤子函數優先級保存的鉤子節點結構nf_hook_ops,在nf_hook_ops中實際存放了鉤子函數的內容。

struct nf_hook_ops

{

struct list_head list;

 

/* User fills in from here down. */

nf_hookfn *hook;

struct module *owner;

u_int8_t pf;

unsigned int hooknum;

/* Hooks are ordered in ascending priority. */

int priority;

};

在相應的鉤子點調用鉤子函數時,則根據協議族和鉤子點找到相應的鏈表入口,然後依次調用該鏈中的每一個鉤子函數對數據包進行操作

2)  hook的管理機制

內核在啓動的時候通過netfilter_init函數完成鉤子函數的初始化工作;如果需要在相應的鉤子點掛載鉤子函數,則需要首先定義一個nf_hook_ops結構,在其中實現所需的鉤子函數,再調用nf_register_hook將該鉤子函數註冊到上述的全局二維鏈表中。

下面先來看一下netfilter_init初始化函數:

void __init netfilter_init(void)

{

int i, h;

/*首先完成全局二維鏈表的初始化工作*/

for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {

        for (h = 0; h < NF_MAX_HOOKS; h++)

               INIT_LIST_HEAD(&nf_hooks[i][h]);

}

 

#ifdef CONFIG_PROC_FS

/*proc文件系統內註冊相應的文件選項*/

proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);

if (!proc_net_netfilter)

        panic("cannot create netfilter proc entry");

#endif

/*完成proc文件夾netfilter下針對nf_queue的初始化工作,完成讀、寫等功能*/

if (netfilter_queue_init() < 0)

        panic("cannot initialize nf_queue");

/*完成proc文件夾netfilter下日誌文件的初始化工作*/

if (netfilter_log_init() < 0)

        panic("cannot initialize nf_log");

}

   鉤子註冊函數nf_registre_hook的定義:

/*nf_register_hook就是一個維護雙向鏈表的過程,將要註冊的鉤子選項hook_ops加入到全局的二維鏈表結構中(調用鉤子函數的過程,自然也就是查找nf_hooks數組的過程了)*/

int nf_register_hook(struct nf_hook_ops *reg)

{

struct nf_hook_ops *elem;

int err;

 

err = mutex_lock_interruptible(&nf_hook_mutex);

if (err < 0)

        return err;

/*根據協議族和Hook點來查找與當前待註冊節點匹配的數組元素*/

list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {

        /*在同一鏈表內部按照優先級排序*/

        if (reg->priority < elem->priority)

               break;

}

/*添加節點*/

list_add_rcu(&reg->list, elem->list.prev);

mutex_unlock(&nf_hook_mutex);

return 0;

}

而對於鉤子函數的註銷則通過nf_unregister_hook完成,在自己的鏈表結構中刪除本身所屬的節點。

void nf_unregister_hook(struct nf_hook_ops *reg)

{

mutex_lock(&nf_hook_mutex);

list_del_rcu(&reg->list);

mutex_unlock(&nf_hook_mutex);

 

synchronize_net();

}

鉤子函數的註冊及撤銷還能通過nf_register_hooks(struct nf_hook_ops *reg,unsigned int n)nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)來完成一次進行多個函數的處理,通過定義nf_hook_ops結構的數組來完成多個鉤子函數的處理,具體過程與上面兩個函數類似。

3)  hook的調用

對於各個掛載點上的鉤子函數,會在數據包經過內核的特點函數點進行調用。

比如對於NF_IP_PRE_ROUTING掛載點,在IP棧成功接收sk_buff包後處理

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

{

return NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, dev, NULL,

                     ip_rcv_finish);

}

鉤子函數會在處理函數的最後通過NF_HOOK宏完成調用過程

NF_HOOK宏定義如下:

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

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

 

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

({int __ret;                                                              \

if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\

__ret = (okfn)(skb);                                                \

__ret;})

可以看出nf_hook_thresh是真正的執行函數,只有在該函數返回爲1時,纔會執行okfn函數,okfn函數是當數據包被允許通過時所進行的處理。

/**

 *     nf_hook_thresh - call a netfilter hook

 *    

 *     Returns 1 if the hook has allowed the packet to pass.  The function

 *     okfn must be invoked by the caller in this case.  Any other return

 *     value indicates the packet has been consumed by the hook.

 */

static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,

                       struct sk_buff *skb,

                       struct net_device *indev,

                       struct net_device *outdev,

                       int (*okfn)(struct sk_buff *), int thresh,

                       int cond)

{

if (!cond)

        return 1;

#ifndef CONFIG_NETFILTER_DEBUG

/*如果鉤子函數數組爲空,直接返回1*/

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

        return 1;

#endif

return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);

}

否則通過nf_hook_slow函數完成後續的處理

/* Returns 1 if okfn() needs to be executed by the caller,

 * -EPERM for NF_DROP, 0 otherwise. */

int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,

         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;

 

/* We may already have this, but read-locks nest anyway */

rcu_read_lock();

/*elem爲對於該協議類型的鉤子掛載點的鏈表*/

elem = &nf_hooks[pf][hook];

next_hook:

/*nf_iterate函數完成掛載點上所有鉤子函數的執行工作*/

verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,

                    outdev, &elem, okfn, hook_thresh);

/*如果數據包爲接收或立即結束,則返回*/

if (verdict == NF_ACCEPT || verdict == NF_STOP) {

        ret = 1;

} else if (verdict == NF_DROP) {

        kfree_skb(skb);

        ret = -EPERM;

} else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {

        if (!nf_queue(skb, elem, pf, hook, indev, outdev, okfn,

                     verdict >> NF_VERDICT_BITS))

               goto next_hook;

}

rcu_read_unlock();

return ret;

}

NF_STOP2.6內核後新增的選項,其功能和NF_ACCEPT類似但強於NF_ACCEPT,一旦掛接鏈表中某個hook節點返回NF_STOP,skb包就立即結束檢查而接受,不再進入鏈表中後續的hook節點,NF_ACCEPT則還需要進入後續hook點檢查。

/*循環執行鏈表上所有的鉤子函數*/

unsigned int nf_iterate(struct list_head *head,

                     struct sk_buff *skb,

                     unsigned int hook,

                     const struct net_device *indev,

                     const struct net_device *outdev,

                     struct list_head **i,

                     int (*okfn)(struct sk_buff *),

                     int hook_thresh)

{

       unsigned int verdict;

 

       /*

        * The caller must not block between calls to this

        * function because of risk of continuing from deleted element.

        */

/*循環遍歷鏈表中所有的鉤子函數,並執行,如果對於數據包的處理爲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;

 

              /* Optimization: we don't need to hold module

                 reference here, since function can't sleep. --RR */

              verdict = elem->hook(hook, skb, indev, outdev, okfn);

              if (verdict != NF_ACCEPT) {

#ifdef CONFIG_NETFILTER_DEBUG

                     if (unlikely((verdict & NF_VERDICT_MASK)

                                                 > NF_MAX_VERDICT)) {

                            NFDEBUG("Evil return from %p(%u).\n",

                                   elem->hook, hook);

                            continue;

                     }

#endif

                     if (verdict != NF_REPEAT)

                            return verdict;

                     *i = (*i)->prev;

              }

       }

       /*遍歷完所有的鉤子函數後,返回*/

       return NF_ACCEPT;

}

對於一個鉤子掛載點的處理結束,對於其他鉤子函數掛載點的處理都與這個相似,只是調用的位置不同,通過這種機制,伴隨着對於一個數據包在內核中的處理流程,完成了各個鉤子掛載點上函數的執行。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章