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函数返回值来处理报文
*/

 

转载于:https://www.cnblogs.com/codestack/p/10850665.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值