如果我写了几个内核模块,并且在所有内核模块中都指定它们应该是第一个(或最后一个)调用的netfilter挂钩,它们实际上会以什么顺序被调用?
netfilter_ops_out.hook = hook_func_out;
netfilter_ops_out.pf = PF_INET;
netfilter_ops_out.hooknum = NF_IP_LOCAL_OUT;
netfilter_ops_out.priority = NF_IP_PRI_FIRST;
ret = nf_register_hook(&netfilter_ops_out);
if (0 > ret) {
printk("Error registering netfilter hook: %d\n", ret);
return ret;
}
netfilter_ops_in.hook = hook_func_in;
netfilter_ops_in.pf = PF_INET;
netfilter_ops_in.hooknum = NF_IP_LOCAL_IN;
netfilter_ops_in.priority = NF_IP_PRI_LAST;
ret = nf_register_hook(&netfilter_ops_in);
if (0 > ret) {
printk("Error registering netfilter hook: %d\n", ret);
return ret;
}
在实验上,我制作了两个模块,insmod
用两个不同的顺序编辑它们 - 但是它们给出了相同的结果,暗示有一些亚序不仅仅是“第一个”先到先得#39; (它也不是按字母顺序......)
答案 0 :(得分:3)
从nf_register_hook()代码中,我们可以知道如果两个钩子属于相同的nf_hooks [reg-> pf] [reg-> hooknum],则钩子执行序列由优先级决定。如果优先级也相同,则序列为“先到先服务”。请参阅以下代码:
int nf_register_hook(struct nf_hook_ops *reg)
{
struct nf_hook_ops *elem;
int err;
err = mutex_lock_interruptible(&nf_hook_mutex);
if (err < 0)
return err;
list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
if (reg->priority < elem->priority)
break;
}
list_add_rcu(®->list, elem->list.prev);
mutex_unlock(&nf_hook_mutex);
#if defined(CONFIG_JUMP_LABEL)
static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
#endif
return 0;
}