Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72827
  • 博文数量: 27
  • 博客积分: 2120
  • 博客等级: 大尉
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-25 20:51
文章分类
文章存档

2011年(1)

2010年(10)

2009年(16)

我的朋友

分类: LINUX

2009-10-25 21:07:26

Linux 为每一个经过网络堆栈的数据连接生成一个连接记录项,用于记录这个连接独特的信息,例如远程IP,本地IP,通信端口等等。记录建立好后,属于这个连接的所有数据包都将只是匹配这个连接记录项,而不是再生成一个新的记录。

在内核的协议栈上设置了多个钩子点,其中
发给本机的包流经NF_IP_PRE_ROUTING、NF_IP_LOCAL_IN
本机发出的包流经NF_IP_LOCAL_OUT、NF_IP_POST_ROUTING
本机转发的包流经NF_IP_PRE_ROUTING、NF_IP_POST_ROUTING

netfileter用一个连接跟踪表来记录所有的连接(已建立的或则是即将建立的),这个表是用hash算法实现的,定义在nf_conntrack_core.c中
struct list_head * nf_conntrack_hash __read_mostly;(这个__read_mostly意义不是很清楚)
表可以容纳的表项数用一个全局变量定义
unsigned int nf_conntrack_htable_size __read_mostly;
每个表项自己又是一条链表的首部,连接跟踪表的总大小用一个全局变量定义
int nf_conntrack_max __read_mostly;
表项的节点结构为
struct nf_conntrack_tuple_hash
{
   struct list_head list;
   struct nf_conntrack_tuple tuple;
};
其中的nf_conntrack_tuple用来描述具体的连接。
/* This contains the information to distinguish a connection. */
struct nf_conntrack_tuple
{
        struct nf_conntrack_man src;

        /* These are the parts of the tuple which are fixed. */
        struct {
                union nf_conntrack_address u3;
                union {
                        /* Add other protocols here. */
                        u_int16_t all;

                        struct {
                                __be16 port;
                        } tcp;
                        struct {
                                __be16 port;
                        } udp;
                        struct {
                                u_int8_t type, code;
                        } icmp;
                        struct {
                                __be16 port;
                        } sctp;
                        struct {
                                __be16 key;
                        } gre;
                } u;

                /* The protocol. */
                u_int8_t protonum;

                /* The direction (for tuplehash) */
                u_int8_t dir;
        } dst;
};
里面第一个结构nf_conntrack_man表示的是可以定制的部分,一般来说也就是包的源部分(比如NAT用)
/* The l3 protocol-specific manipulable parts of the tuple: always in
   network order! */
union nf_conntrack_address {
        u_int32_t all[NF_CT_TUPLE_L3SIZE];
        __be32 ip;
        __be32 ip6[4];
};

/* The protocol-specific manipulable parts of the tuple: always in
   network order! */
union nf_conntrack_man_proto
{
        /* Add other protocols here. */
        u_int16_t all;

        struct {
                __be16 port;
        } tcp;
        struct {
                __be16 port;
        } udp;
        struct {
                __be16 id;
        } icmp;
        struct {
                __be16 port;
        } sctp;
        struct {
                __be16 key;     /* GRE key is 32bit, PPtP only uses 16bit */
        } gre;
};

/* The manipulable part of the tuple. */
struct nf_conntrack_man
{
        union nf_conntrack_address u3;
        union nf_conntrack_man_proto u;
        /* Layer 3 protocol */
        u_int16_t l3num;
};

注意到dst中有个变量dir,它主要用于标识一个连接的方向。

内核用一个结构struct nf_conn描述一个连接
struct nf_conn
{
        /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
           plus 1 for any connection(s) we are `master' for */
        struct nf_conntrack ct_general;

        /* XXX should I move this to the tail ? - Y.K */
        /* These are my tuples; original and reply */
        struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];

        /* Have we seen traffic both ways yet? (bitset) */
        unsigned long status;

        /* If we were expected by an expectation, this will be it */
        struct nf_conn *master;

        /* Timer function; drops refcnt when it goes off. */
        struct timer_list timeout;

#ifdef CONFIG_NF_CT_ACCT
        /* Accounting Information (same cache line as other written members) */
        struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
#endif

        /* Unique ID that identifies this conntrack*/
        unsigned int id;

        /* features - nat, helper, ... used by allocating system */
        u_int32_t features;

#if defined(CONFIG_NF_CONNTRACK_MARK)
        u_int32_t mark;
#endif

#ifdef CONFIG_NF_CONNTRACK_SECMARK
        u_int32_t secmark;
#endif

        /* Storage reserved for other modules: */
        union nf_conntrack_proto proto;

        /* features dynamically at the end: helper, nat (both optional) */
        char data[0];
};
注意里面的struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX], 它描述的是这个连接orig包与replay包的特性,每个数据包都会被转成nf_conntrack_tuple然后跟这个数据中的成员比较(一帮就 orig跟replay),以确定是否属于这个连接。每个数据报被转成tuple后,内核会根据这个tuple算出hash值,然后用这个hash值找对 应的跟踪记录nf_conntrack_hash[hash],如果没有,呢就表明这个包来自一个新连接。内核会新添加一条跟踪记录。

阅读(576) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~