linux Netfilterr中擴展match target


Match:

netfilter定義了一個通用的match數據結構struct xt_match



/*
每個struct xt_match代表一個擴展match,netfilter中各個擴展match自己定義自己的匹配參數數據結構,自己實現匹配函數 */ struct xt_match { struct list_head list; //match名字,和iptables配置的-m參數相同 const char name[XT_EXTENSION_MAXNAMELEN]; u_int8_t revision; /* Return true or false: return FALSE and set *hotdrop = 1 to force immediate packet drop. */ /* Arguments changed since 2.6.9, as this must now handle non-linear skb, using skb_header_pointer and skb_ip_make_writable. *///規則匹配函數 bool (*match)(const struct sk_buff *skb, struct xt_action_param *); /* Called when user tries to insert an entry of this type. */ int (*checkentry)(const struct xt_mtchk_param *);/* 匹配函數入參檢查函數 */ /* Called when entry of this type deleted. */ void (*destroy)(const struct xt_mtdtor_param *); #ifdef CONFIG_COMPAT /* Called when userspace align differs from kernel space one */ void (*compat_from_user)(void *dst, const void *src); int (*compat_to_user)(void __user *dst, const void *src); #endif /* Set this to THIS_MODULE if you are a module, otherwise NULL */ struct module *me; const char *table; unsigned int matchsize;//match的自定義數據長度 #ifdef CONFIG_COMPAT unsigned int compatsize; #endif unsigned int hooks; unsigned short proto; unsigned short family; };

每個struct xt_match代表一個擴展matchnetfilter中各個擴展match自己定義自己的匹配參數數據結構,自己實現匹配函數。

netfilter中,每個擴展match 被注冊到全局變量xt管理的match鏈表上,可根據match的名字來找到相應的struct xt_match

用戶使用-m 來配置的擴展match下發給內核,在內核中以struct xt_entry_match數據結構來存儲,該結構后緊跟着存儲着擴展match自定義的匹配參數數據。

Match的基本處理流程

1、取到rule中存儲的xt_entry_match,這里記錄着iptables下發給內核的值,同時找到內核中注冊的xt_match,從xt_match里找到相應的match函數.

2、根據xt_entry_match中記錄的配置值初始化xt_match_param,

3、把報文和xt_match_param傳給match函數進行匹配處理。

 

int xt_register_match(struct xt_match *match)
{
    u_int8_t af = match->family;

    mutex_lock(&xt[af].mutex);
//加入Netfilter管理的全局Match鏈表上
    list_add(&match->list, &xt[af].match);
    mutex_unlock(&xt[af].mutex);
    return 0;
}

 

Target:

 

 

 
struct xt_standard_target {
    struct xt_entry_target target;
/*verdict 可以是指定返回值,也可以是goto到下一個rule的偏移量,
也可以是queue的隊列號。
*/
    int verdict;
};
/*
 如果struct xt_entry->target 值為空,表示是標准target,根據verdict值來處理報文。
如果struct xt_entry->target不為空,表示不是標准target,就使用target的target函數返回值來處理報文
*/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM