xt_register_match将某个xt_match注册的过程

=========================以下内核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;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值