HOOK函数注册

上图是netfilter中注册的部分hook函数。这些hook函数是通过 nf_register_hook注册到二维数组nf_hooks中的。
enum nf_inet_hooks {
NF_INET_PRE_ROUTING,
NF_INET_LOCAL_IN,
NF_INET_FORWARD,
NF_INET_LOCAL_OUT,
NF_INET_POST_ROUTING,
NF_INET_NUMHOOKS
};
enum {
NFPROTO_UNSPEC = 0,
NFPROTO_INET = 1,
NFPROTO_IPV4 = 2,
NFPROTO_ARP = 3,
NFPROTO_BRIDGE = 7,
NFPROTO_IPV6 = 10,
NFPROTO_DECNET = 12,
NFPROTO_NUMPROTO,
};
extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
数组的第一维即上图的第一列,表示不同的地址族,上图只画出了 IPV4,ARP和BRIDGE相关的。数组的第二维即上图的第一排,表示不同模块的hook函数,对于不同的地址族,使用的宏定义不同,但都是从0开始。拿最常见的IPV4地址族来说,五个hook函数安置在数据包的必经之路,不管数据是到本机的,还是需要本机转发的,还是从本机发出去的,都会覆盖到。
NF_INET_PRE_ROUTING和NF_INET_LOCAL_OUT相当于netfilter的入口点,即不管哪种情况,数据包肯定会从这俩hook点之一进入。
NF_INET_LOCAL_IN和NF_INET_POST_ROUTING相当于netfilter的出口点,即不管哪种情况,数据包肯定会从这俩hook点之一出去。
hook函数的执行顺序: 同一个hook点上的从优先级值最小的开始执行,即上图从上往下执行,不同的hook点互不影响。
对于IPV4地址族来说,包含了多个模块的hook函数:
filter,raw,mangle,security: 这四个模块相对独立,不依赖其他模块。
conntrack: 连接跟踪模块是状态防火墙和nat的基础。
nat: 基于conntrack对于数据包的跟踪,对数据流的收包查找nat规则进行nat转换,此流的后续数据包直接查找conntrack的ct信息做nat转换。
ipvs: 四层负载均衡模块
HOOK函数执行
hook函数的执行是通过调用函数NF_HOOK来实现的,
其第一个参数pf指定了地址族,第二个参数指定了在哪个hook点,第三个参数skb是数据包,第六个参数是数据包通过hook点上hook函数的检查后,如果结果是accept而执行的函数。
static inline int
NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
struct net_device *in, struct net_device *out,
int (*okfn)(struct sk_buff *))
{
return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
}
static inline int
NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
struct net_device *in, struct net_device *out,
int (*okfn)(struct sk_buff *), int thresh)
{
int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
//执行完hook函数后,返回值为1才会执行okfn。
if (ret == 1)
ret = okfn(skb);
return ret;
}
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
struct sk_buff *skb,
struct net_device *indev,
struct net_device *outdev,
int (*okfn)(struct sk_buff *), int thresh)
{
//地址族pf对应的hook点上注册了hook函数,如果没注册直接返回1
if (nf_hooks_active(pf, hook))
return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
return 1;
}
/* Returns 1 if okfn() needs to be executed by the caller,
* -EPERM for NF_DROP, 0 otherwise. */
int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
struct net_device *indev,
struct net_device *outdev,
int (*okfn)(struct sk_buff *),
int hook_thresh)
{
struct nf_hook_ops *elem;
unsigned int verdict;
int ret = 0;
/* We may already have this, but read-locks nest anyway */
rcu_read_lock();
//取出hook点的hook函数的链表头
elem = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list);
next_hook:
//遍历执行此hook点上所有的hook函数
verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
outdev, &elem, okfn, hook_thresh);
//如果返回NF_ACCEPT或者NF_STOP说明通过hook函数的检查,则返回1
//如果返回NF_DROP,说明数据包在此hook点丢了,返回0
if (verdict == NF_ACCEPT || verdict == NF_STOP) {
ret = 1;
} else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
kfree_skb(skb);
ret = NF_DROP_GETERR(verdict);
if (ret == 0)
ret = -EPERM;
} else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
verdict >> NF_VERDICT_QBITS);
if (err < 0) {
if (err == -ECANCELED)
goto next_hook;
if (err == -ESRCH &&
(verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
goto next_hook;
kfree_skb(skb);
}
}
rcu_read_unlock();
return ret;
}
遍历执行hook点上所有的hook函数。
unsigned int nf_iterate(struct list_head *head,
struct sk_buff *skb,
unsigned int hook,
const struct net_device *indev,
const struct net_device *outdev,
struct nf_hook_ops **elemp,
int (*okfn)(struct sk_buff *),
int hook_thresh)
{
unsigned int verdict;
/*
* The caller must not block between calls to this
* function because of risk of continuing from deleted element.
*/
list_for_each_entry_continue_rcu((*elemp), head, list) {
//只执行优先级比hook_thresh大的hook函数
if (hook_thresh > (*elemp)->priority)
continue;
/* Optimization: we don't need to hold module
reference here, since function can't sleep. --RR */
repeat:
verdict = (*elemp)->hook(*elemp, skb, indev, outdev, okfn);
//hook函数的返回值不为accept,并且不为repeat才返回
if (verdict != NF_ACCEPT) {
if (verdict != NF_REPEAT)
return verdict;
goto repeat;
}
}
//走到这里说明所有的hook函数都返回accept
return NF_ACCEPT;
}
网友评论