连接跟踪之期望连接

1 概述

​ 。。。

2 helper功能

2.1 概述

​ helper功能是连接跟踪的扩展功能之一,给不同应用层协议来对连接跟踪模块进行功能上的扩展。比如ftp、tftp等利用helper功能来实现期望连接的建立和关联。

2.2 代码结构

​ 如图:

在这里插入图片描述

helper功能主代码文件主要提供API:helper扩展添加,helper实例查找,helper实例注册,helper实例初始化等

期望连接的主代码文件主要提供API:期望连接新建,期望连接初始化、期望连接查找、期望连接插入等

各协议对应的主代码文件主要功能是初始化helper实例、注册helper实例、实现各自的help回调函数

例如ftp的help回调函数主要功能是根据控制通道的报文负载获取数据通道的端口号等信息,根据这些信息新建期望连接并初始化

2.3 helper核心数据结构

​ 数据结构体总体关系图:

在这里插入图片描述

各个协议模块初始化其对应的struct nf_conntrack_helper实例,注册到nf_ct_helper_hash哈希表里,

报文进入连接跟踪模块在新建连接跟踪时挂载help扩展,根据报文元组信息从nf_ct_helper_hash哈希表查找helper实例

把找到的helper赋值给help扩展的成员helper。在出连接跟踪模块前执行该helper指定的回调函数help。

2.3.1 struct nf_conn_help help扩展结构体
/* nf_conn feature for connections that have a helper */
struct nf_conn_help {
        /* Helper. if any */
        struct nf_conntrack_helper __rcu *helper;      //对应的helper实例

        struct hlist_head expectations;       //存该连接的期望连接

        /* Current number of expected connections */
        u8 expecting[NF_CT_MAX_EXPECT_CLASSES];      //该连接的期望连接的数量

        /* private helper information. */
        char data[32] __aligned(8);
};
2.3.2 struct nf_conntrack_helper helper结构体
struct nf_conntrack_helper {
        struct hlist_node hnode;        /* Internal use. */
		//helper名字,iptables添加规则匹配helper时使用
        char name[NF_CT_HELPER_NAME_LEN]; /* name of the module */
        refcount_t refcnt;
        struct module *me;              /* pointer to self */          
        const struct nf_conntrack_expect_policy *expect_policy;//各协议的期望连接的策略参数:超时时间和最大期望连接数

        /* Tuple of things we will help (compared against server response) */
        struct nf_conntrack_tuple tuple; //注册helper时会设置tuple,根据报文匹配tulpe查找到该helper

        /* Function to call when data passes; return verdict, or -1 to
           invalidate. */     
        int (*help)(struct sk_buff *skb,
                    unsigned int protoff,          
                    struct nf_conn *ct,            
                   enum ip_conntrack_info conntrackinfo); //不同协议都有自己定义的help回调函数,在ipv4_helper()执行

        void (*destroy)(struct nf_conn *ct);

        int (*from_nlattr)(struct nlattr *attr, struct nf_conn *ct);
        int (*to_nlattr)(struct sk_buff *skb, const struct nf_conn *ct);
        unsigned int expect_class_max; 

        unsigned int flags;

        /* For user-space helpers: */  
        unsigned int queue_num;        
        /* length of userspace private data stored in nf_conn_help->data */
        u16 data_len;
};
2.4 helper核心函数
2.4.1 nf_conntrack_helper_register

​ 流程图:
在这里插入图片描述

2.4.2 __nf_ct_try_assign_helper

2.4.3 nf_ct_lookup_helper
static struct nf_conntrack_helper *
nf_ct_lookup_helper(struct nf_conn *ct, struct net *net)
{
        if (!net->ct.sysctl_auto_assign_helper) {//默认false,加载helper模块时可以通过参数指定为true或者通过sysctl -w net.netfilter.nf_conntrack_helper修改为1,为false的时候该接口必定返回NULL,不会查找helper
                if (net->ct.auto_assign_helper_warned)//警告信息只打印一次
                        return NULL;                   
                if (!__nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple))
                        return NULL;                   
                pr_info("nf_conntrack: default automatic helper assignment "
                        "has been turned off for security reasons and CT-based "
                        " firewall rule not found. Use the iptables CT target "
                        "to attach helpers instead.\n");//出于安全原因默认自动指定helper已关闭,并且未发现基于CT的防火墙规则。替代方法:使用iptables CT target来关联helper
                net->ct.auto_assign_helper_warned = 1;
                return NULL;  
        }

        return __nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
}  

nf_ct_lookup_helper这个接口更像是为了兼容而在使用,并通过这个接口告诉用户,内核默认已关闭该种关联helper的方法。用另一个关联helper的方法替代

2.4.4 __nf_ct_helper_find
2.4.5 __nf_conntrack_helper_find

3 期望连接的实现

3.1 整体实现框架

​ 以ftp协议为例:

在这里插入图片描述

3.2 核心数据结构
3.3 核心函数

4 期望连接的作用

ftp在active模式的报文交互

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值