这篇笔记记录了连接跟踪子系统中期望连接相关的内容,这部分内容比较抽象,自己目前也只是基于ftp协议的使用来理解的,对于其它应用场景尚未接触过,尚不理解。期望连接相关代码文件有:
代码路径 | 说明 |
---|---|
/net/netfilter/nf_conntrack_expect.c | 期望连接子模块实现文件 |
1. 期望连接跟踪信息块
期望连接跟踪信息块用struct nf_conntrack_expect对象表示,该结构定义如下:
struct nf_conntrack_expect
{
//同属一个master连接的期望连接被组织到一个链表中,见struct nf_conn_help定义
struct hlist_node lnode;
//用于将期望连接跟踪信息块加入到全局的哈希表中
struct hlist_node hnode;
//该期望连接跟踪信息块能够匹配的skb
struct nf_conntrack_tuple tuple;
struct nf_conntrack_tuple_mask mask;
//当期望连接到来时,会调用该函数,该回调函数是可选的
void (*expectfn)(struct nf_conn *new, struct nf_conntrack_expect *this);
//指向识别出该期望连接的helper
struct nf_conntrack_helper *helper;
//指向master连接跟踪信息块
struct nf_conn *master;
//和普通的连接类似,每个期望连接也有有效定时器,定时器超时则会从系统中删除该期望连接
struct timer_list timeout;
//引用计数
atomic_t use;
/* Flags */
unsigned int flags;
#ifdef CONFIG_NF_NAT_NEEDED
__be32 saved_ip;
/* This is the original per-proto part, used to map the
* expected connection the way the recipient expects. */
union nf_conntrack_man_proto saved_proto;
/* Direction relative to the master connection. */
enum ip_conntrack_dir dir;
#endif
struct rcu_head rcu;
};
2. 期望连接跟踪信息块的插入
当helper识别到即将要有一个期望连接到达时,就会创建一个期望连接跟踪信息块,然后将其加入到全局哈希表中,这样等真正的期望连接的skb到达时,就可以匹配该期望连接跟踪信息块,表示期望连接建立成功。
int nf_ct_expect_related(struct nf_conntrack_expect *expect)
{
struct nf_conntrack_expect *i;
struct nf_conn *master = expect->master;
//找到master连接的help扩展
struct nf_conn_help *master_help = nfct_help(master);
struct hlist_node *n;
unsigned int h;
int ret;
NF_CT_ASSERT(master_help);
spin_lock_bh(&nf_conntrack_lock);
//个人理解,这里只是一种检查,确保对应的helper存在
if (!master_h