Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30987
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 22
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-25 14:08
文章分类
文章存档

2018年(21)

我的朋友

分类: LINUX

2018-09-19 15:47:57

2、扩展的Macth


#define ipt_entry_match xt_entry_match

netfilter定义了一个通用的match数据结构struct xt_match

struct xt_match

{

    struct list_head list;
    //match名字,和iptables配置的-m参数相同

    const char name[XT_FUNCTION_MAXNAMELEN-1];

    u_int8_t revision;

    //规则匹配函数

    bool (*match)(const struct sk_buff *skb,

              const struct xt_match_param *);

 

    /* 匹配函数入参检查函数 */

    bool (*checkentry)(const struct xt_mtchk_param *);

 

    /* Called when entry of this type deleted. */

    void (*destroy)(const struct xt_mtdtor_param *);
    。。。。。。

    const char *table;
    //match的自定义数据长度

    unsigned int matchsize;

    unsigned int compatsize;

    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函数进行匹配处理。

 

 

注册match

Int xt_register_match(struct xt_match *match)

{

    u_int8_t af = match->family;

    int ret;

    ret = mutex_lock_interruptible(&xt[af].mutex);

    if (ret != 0)

    return ret;

    list_add(&match->list, &xt[af].match);//加入Netfilter管理的全局Match链表上

    mutex_unlock(&xt[af].mutex);

 

    return ret;

}

3、Target

Netfilter中对扩展target 和扩展match的管理非常相似

target分扩展target和标准target

#define ipt_entry_target xt_entry_target

 

标准target

#define ipt_standard_target  xt_standard_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,就使用targettarget函数返回值来处理报文。

 

调用函数xt_register_target()来注册扩展target

(未完待续)


阅读(984) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~