=========================以下内核tcp match module=================================
static struct xt_match tcpudp_mt_reg[] __read_mostly = {
{
.name = "tcp",
.family = NFPROTO_IPV4,
.checkentry = tcp_mt_check,
.match = tcp_mt,
.matchsize = sizeof(struct xt_tcp);
.proto = IPPROTO_TCP,
.me = THIS_MODULE,
},
...
};
static int __init tcpupd_mt_init(void)
{
return xt_register_matches(tcpupd_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
}
========================以下展示的将tcp match注册的过程 ===================================
int xt_register_matches(struct xt_match *match, unsigned int n)
{
unsigned int i;
int err = 0;
for(i = 0; i < n; i++) {
err = xt_register_match(&match[i]);
if (err)
goto err;
}
return err;
err:
if (i > 0)
xt_unregister_matches(match, i);
return err;
}
void xt_unregister_matches(struct xt_match *match, unsigned int n)
{
while(n-- > 0)
xt_unregsiter_match(&match[n]);
}
int xt_register_match(struct xt_match *match)
{
u_int8_t af = match->family;
mutex_lock(&xt[af].mutex);
// 将相应的xt_match添加到xt对应协议的match列表中
list_add(&match->list, &xt[af].match);
mutex_unlock(&xt[af].mutex);
}
///< 以上是将tcp match注册到xt中
接下来看一下xt,其中的xt内容为各个相关的模块,在相关模块的初始化函数中,调用register函数添加进入的。
// net/netfilter/x_tables.c
struct xt_af {
struct mutex mutex;
struct list_head match;
struct list_head target;
#ifdef CONFIG_COMPAT
struct mutex compat_mutex;
struct compat_delta *compat_tab;
unsigned int number;
unsigned int cur;
#endif
};
static struct xt_af *xt;
static int __init xt_init(void)
{
unsigned int i;
int rv;
for_each_possible_cpu(i) {
seqcount_init(&per_cpu(xt_recseq, i));
}
xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL);
if (!xt)
return -ENOMEM;
for (i = 0; i < NFPROTO_NUMPROTO; I++) {
mutex_init(&xt[i].mutex);
#ifdef CONFIG_COMPAT
mutex_init(&xt[i].compat_mutex);
xt[i].compat_tab = NULL;
#endif
INIT_LIST_HEAD(&xt[i].target);
INIT_LIST_HEAD(&xt[i].match);
}
rv = register_prenet_subsys(&xt_net_ops);
if (rv < 0)
kfree(xt);
return rv;
}