绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
Netfilter 连接跟踪与状态检测的实现(2)
2020-05-22 18:03:07

5.resolve_normal_ct
        resolve_normal_ct 函数是连接跟踪中重要的函数之一,它的主要功能就是判断数据包在连接跟踪表是否存在,如果不存在,则为数据包分配相应的连接跟踪节点空间并初始化,然后设置连接状态:


  1. /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
  2. static inline struct ip_conntrack *
  3. resolve_normal_ct(struct sk_buff *skb,
  4.                   struct ip_conntrack_protocol *proto,
  5.                   int *set_reply,
  6.                   unsigned int hooknum,
  7.                   enum ip_conntrack_info *ctinfo)
  8. {
  9.         struct ip_conntrack_tuple tuple;
  10.         struct ip_conntrack_tuple_hash *h;
  11.         struct ip_conntrack *ct;

  12.         IP_NF_ASSERT((skb->nh.iph->frag_off & htons(IP_OFFSET)) == 0);

  13. /*前面提到过,需要将一个数据包转换成tuple,这个转换,就是通过ip_ct_get_tuple函数实现的*/
  14.         if (!ip_ct_get_tuple(skb->nh.iph, skb, skb->nh.iph->ihl*4, 
  15.                                 &tuple,proto))
  16.                 return NULL;

  17.         /*查看数据包对应的tuple在连接跟踪表中是否存在 */
  18.         h = ip_conntrack_find_get(&tuple, NULL);
  19.         if (!h) {
  20.                 /*如果不存在,初始化之*/
  21. h = init_conntrack(&tuple, proto, skb);
  22.                 if (!h)
  23.                         return NULL;
  24.                 if (IS_ERR(h))
  25.                         return (void *)h;
  26.         }
  27. /*根据hash表节点,取得数据包对应的连接跟踪结构*/
  28.         ct = tuplehash_to_ctrack(h);

  29.         /* 判断连接的方向 */
  30.         if (DIRECTION(h) == IP_CT_DIR_REPLY) {
  31.                 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
  32.                 /* Please set reply bit if this packet OK */
  33.                 *set_reply = 1;
  34.         } else {
  35.                 /* Once we've had two way comms, always ESTABLISHED. */
  36.                 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
  37.                         DEBUGP("ip_conntrack_in: normal packet for %p\n",
  38.                                ct);
  39.                         *ctinfo = IP_CT_ESTABLISHED;
  40.                 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
  41.                         DEBUGP("ip_conntrack_in: related packet for %p\n",
  42.                                ct);
  43.                         *ctinfo = IP_CT_RELATED;
  44.                 } else {
  45.                         DEBUGP("ip_conntrack_in: new packet for %p\n",
  46.                                ct);
  47.                         *ctinfo = IP_CT_NEW;
  48.                 }
  49.                 *set_reply = 0;
  50.         }
  51. /*设置skb的对应成员,如使用计数器、数据包状态标记*/
  52.         skb->nfct = &ct->ct_general;
  53.         skb->nfctinfo = *ctinfo;
  54.         return ct;
  55. }
复制代码




这个函数包含了连接跟踪中许多重要的步骤
n        调用ip_ct_get_tuple函数,把数据包转换为tuple;
n        ip_conntrack_find_get函数,根据tuple查找连接跟踪表;
n        init_conntrack函数,初始化一条连接;
n        判断连接方向,设置连接状态;

5.1 数据包的转换
ip_ct_get_tuple 实现数据包至tuple的转换,这个转换,主要是根据数据包的套接字对来进行转换的:


  1. int ip_ct_get_tuple(const struct iphdr *iph,
  2.                 const struct sk_buff *skb,
  3.                 unsigned int dataoff,
  4.                 struct ip_conntrack_tuple *tuple,
  5.                 const struct ip_conntrack_protocol *protocol)
  6. {
  7.                 /* Never happen */
  8.                 if (iph->frag_off & htons(IP_OFFSET)) {
  9.                         printk("ip_conntrack_core: Frag of proto %u.\n",
  10.                        iph->protocol);
  11.                         return 0;
  12.         }
  13. /*设置来源、目的地址*/
  14.                 tuple->src.ip = iph->saddr;
  15.                 tuple->dst.ip = iph->daddr;
  16.         tuple->dst.protonum = iph->protocol;
  17.                 tuple->dst.dir = IP_CT_DIR_ORIGINAL;

  18.         return protocol->pkt_to_tuple(skb, dataoff, tuple);
  19. }
复制代码



回忆一下我们前面分析协议的初始化中协议初始化的部份,pkt_to_tuple 函数指针,以每种协议的不同而不同,以TCP协议为例:


  1. static int tcp_pkt_to_tuple(const struct sk_buff *skb,
  2.                             unsigned int dataoff,
  3.                             struct ip_conntrack_tuple *tuple)
  4. {
  5.                 struct tcphdr _hdr, *hp;

  6.                 /* 获取TCP报头*/
  7. hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
  8.         if (hp == NULL)
  9.                         return 0;
  10. /*根据报头的端口信息,设置tuple对应成员*/
  11.                 tuple->src.u.tcp.port = hp->source;
  12.         tuple->dst.u.tcp.port = hp->dest;

  13.         return 1;
  14. }
复制代码


TCP协议中,根据来源和目的端口设置,其它协议类似,读者可以对比分析。

5.2 Hash 表的搜索
要对Hash表进行遍历,首要需要找到hash表的入口,然后来遍历该入口指向的链表。每个链表的节点是struct ip_conntrack_tuple_hash,它封装了tuple,所谓封装,就是把待查找的tuple与节点中已存的tuple相比较,我们来看这一过程的实现。
计算hash值,是调用hash_conntrack函数,根据数据包对应的tuple实现的:

  1. unsigned int hash = hash_conntrack(tuple);

  2.         这样,tuple对应的hash表入口即为ip_conntrack_hash[hash],也就是链表的首节点,然后调用ip_conntrack_find_get函数进行查找:
  3. struct ip_conntrack_tuple_hash *
  4. ip_conntrack_find_get(const struct ip_conntrack_tuple *tuple,
  5.                       const struct ip_conntrack *ignored_conntrack)
  6. {
  7.         struct ip_conntrack_tuple_hash *h;

  8.         READ_LOCK(&ip_conntrack_lock);
  9.         /*搜索链表*/
  10.         h = __ip_conntrack_find(tuple, ignored_conntrack);
  11.         if (h)                /*查找到了,使用计数器累加*/
  12.                 atomic_inc(&tuplehash_to_ctrack(h)->ct_general.use);
  13.         READ_UNLOCK(&ip_conntrack_lock);

  14.         return h;
  15. }
复制代码



链表是内核中一个标准的双向链表,可以调用宏list_for_each_entry 进遍历链表:

  1. static struct ip_conntrack_tuple_hash *
  2. __ip_conntrack_find(const struct ip_conntrack_tuple *tuple,
  3.                     const struct ip_conntrack *ignored_conntrack)
  4. {
  5.         struct ip_conntrack_tuple_hash *h;
  6.         unsigned int hash = hash_conntrack(tuple);

  7.         MUST_BE_READ_LOCKED(&ip_conntrack_lock);
  8.         list_for_each_entry(h, &ip_conntrack_hash[hash], list) {
  9.                 if (conntrack_tuple_cmp(h, tuple, ignored_conntrack)) {
  10.                         CONNTRACK_STAT_INC(found);
  11.                         return h;
  12.                 }
  13.                 CONNTRACK_STAT_INC(searched);
  14.         }

  15.         return NULL;
  16. }
复制代码



list_for_each_entry在以&ip_conntrack_hash[hash]为起始地址的链表中,逐个搜索其成员,比较这个节点中的tuple是否与待查找的tuple是否一致,这个比较过程,是通过conntrack_tuple_cmp 函数实现的:

  1. conntrack_tuple_cmp(const struct ip_conntrack_tuple_hash *i,
  2.                     const struct ip_conntrack_tuple *tuple,
  3.                     const struct ip_conntrack *ignored_conntrack)
  4. {
  5.         MUST_BE_READ_LOCKED(&ip_conntrack_lock);
  6.         return tuplehash_to_ctrack(i) != ignored_conntrack
  7.                 && ip_ct_tuple_equal(tuple, &i->tuple);
  8. }
复制代码



tuplehash_to_ctrack 函数主要是取连接跟踪ip_conntrack中的连接方向,判断它是否等于ignored_conntrack,对与这里的比较而言,ignored_conntrack传递过来的为NULL。
主要的比较函数是ip_ct_tuple_equal函数,函数分为“来源”和“目的”进行比较:

  1. static inline int ip_ct_tuple_src_equal(const struct ip_conntrack_tuple *t1,
  2.                                         const struct ip_conntrack_tuple *t2)
  3. {
  4.         return t1->src.ip == t2->src.ip
  5.                 && t1->src.u.all == t2->src.u.all;
  6. }

  7. static inline int ip_ct_tuple_dst_equal(const struct ip_conntrack_tuple *t1,
  8.                                         const struct ip_conntrack_tuple *t2)
  9. {
  10.         return t1->dst.ip == t2->dst.ip
  11.                 && t1->dst.u.all == t2->dst.u.all
  12.                 && t1->dst.protonum == t2->dst.protonum;
  13. }

  14. static inline int ip_ct_tuple_equal(const struct ip_conntrack_tuple *t1,
  15.                                     const struct ip_conntrack_tuple *t2)
  16. {
  17.         return ip_ct_tuple_src_equal(t1, t2) && ip_ct_tuple_dst_equal(t1, t2);
  18. }
复制代码



这里的比较,除了IP地址之外,并没有直接比较“端口”,这是因为像ICMP协议这样的并没有“端口”协议,struct ip_conntrack_tuple 结构中,与协议相关的,如端口等,都定义成union类型,这样,就可以直接使用u.all,而不用再去管TCP,UDP还是ICMP了。

5.3 连接初始化
内核使用ip_conntrack结构来描述一个数据包的连接状态,init_conntrack函数就是在连接状态表中不存在当前数据包时,初始化一个ip_conntrack结构,此结构被Netfilter用来描述一条连接,前面分析hash表时,已经分析了它的tuplehash成员:

  1. struct ip_conntrack
  2. {
  3.         /* 包含了使用计数器和指向删除连接的函数的指针 */
  4.         struct nf_conntrack ct_general;

  5.         /* 连接状态位,它通常是一个ip_conntrack_status类型的枚举变量,如IPS_SEEN_REPLY_BIT等*/
  6.         unsigned long status;

  7.         /* 内核的定时器,用于处理连接超时 */
  8.         struct timer_list timeout;

  9. #ifdef CONFIG_IP_NF_CT_ACCT
  10.         /* Accounting Information (same cache line as other written members) */
  11.         struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
  12. #endif
  13.         /* If we were expected by an expectation, this will be it */
  14.         struct ip_conntrack *master;

  15.         /* Current number of expected connections */
  16.         unsigned int expecting;

  17.         /* Helper, if any. */
  18.         struct ip_conntrack_helper *helper;

  19.         /* Storage reserved for other modules: */
  20.         union ip_conntrack_proto proto;

  21.         union ip_conntrack_help help;

  22. #ifdef CONFIG_IP_NF_NAT_NEEDED
  23.         struct {
  24.                 struct ip_nat_info info;
  25. #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
  26.         defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
  27.                 int masq_index;
  28. #endif
  29.         } nat;
  30. #endif /* CONFIG_IP_NF_NAT_NEEDED */

  31. #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
  32.         unsigned long mark;
  33. #endif

  34.         /* Traversed often, so hopefully in different cacheline to top */
  35.         /* These are my tuples; original and reply */
  36.         struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
  37. };


  38. /* Allocate a new conntrack: we return -ENOMEM if classification
  39.    failed due to stress.  Otherwise it really is unclassifiable. */
  40. static struct ip_conntrack_tuple_hash *
  41. init_conntrack(const struct ip_conntrack_tuple *tuple,
  42.                struct ip_conntrack_protocol *protocol,
  43.                struct sk_buff *skb)
  44. {
  45.         struct ip_conntrack *conntrack;
  46.         struct ip_conntrack_tuple repl_tuple;
  47.         size_t hash;
  48.         struct ip_conntrack_expect *exp;

  49.         /*如果计算hash值的随机数种子没有被初始化,则初始化之*/
  50.         if (!ip_conntrack_hash_rnd_initted) {
  51.                 get_random_bytes(&ip_conntrack_hash_rnd, 4);
  52.                 ip_conntrack_hash_rnd_initted = 1;
  53.         }

  54.         /*计算hash值*/
  55.         hash = hash_conntrack(tuple);
  56.         
  57.         /*判断连接跟踪表是否已满*/
  58.         if (ip_conntrack_max
  59.             && atomic_read(&ip_conntrack_count) >= ip_conntrack_max) {
  60.                 /* Try dropping from this hash chain. */
  61.                 if (!early_drop(&ip_conntrack_hash[hash])) {
  62.                         if (net_ratelimit())
  63.                                 printk(KERN_WARNING
  64.                                        "ip_conntrack: table full, dropping"
  65.                                        " packet.\n");
  66.                         return ERR_PTR(-ENOMEM);
  67.                 }
  68.         }

  69.         /*根据当前的tuple取反,计算该数据包的“应答”的tuple*/
  70.         if (!ip_ct_invert_tuple(&repl_tuple, tuple, protocol)) {
  71.                 DEBUGP("Can't invert tuple.\n");
  72.                 return NULL;
  73.         }
  74.         /*为数据包对应的连接分配空间*/
  75.         conntrack = kmem_cache_alloc(ip_conntrack_cachep, GFP_ATOMIC);
  76.         if (!conntrack) {
  77.                 DEBUGP("Can't allocate conntrack.\n");
  78.                 return ERR_PTR(-ENOMEM);
  79.         }
  80.         /*初始化该结构*/
  81.         memset(conntrack, 0, sizeof(*conntrack));
  82.         /*使用计数器累加*/
  83.         atomic_set(&conntrack->ct_general.use, 1);
  84.         /*设置destroy函数指针*/
  85.         conntrack->ct_general.destroy = destroy_conntrack;
  86.         /*设置正反两个方向的tuple*/
  87. conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *tuple;
  88.         conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = repl_tuple;
  89.         if (!protocol->new(conntrack, skb)) {
  90.                 kmem_cache_free(ip_conntrack_cachep, conntrack);
  91.                 return NULL;
  92.         }
  93.         /* 初始化时间计数器,并设置超时初始函数 */
  94.         init_timer(&conntrack->timeout);
  95.         conntrack->timeout.data = (unsigned long)conntrack;
  96.         conntrack->timeout.function = death_by_timeout;

  97.         WRITE_LOCK(&ip_conntrack_lock);
  98.         exp = find_expectation(tuple);

  99.         if (exp) {
  100.                 DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
  101.                         conntrack, exp);
  102.                 /* Welcome, Mr. Bond.  We've been expecting you... */
  103.                 __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
  104.                 conntrack->master = exp->master;
  105. #if CONFIG_IP_NF_CONNTRACK_MARK
  106.                 conntrack->mark = exp->master->mark;
  107. #endif
  108.                 nf_conntrack_get(&conntrack->master->ct_general);
  109.                 CONNTRACK_STAT_INC(expect_new);
  110.         } else {
  111.                 conntrack->helper = ip_ct_find_helper(&repl_tuple);

  112.                 CONNTRACK_STAT_INC(new);
  113.         }

  114.         /* 这里,并没有直接就把该连接加入hash表,而是先加入到unconfirmed链表中. */
  115.         list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed);

  116.         atomic_inc(&ip_conntrack_count);
  117.         WRITE_UNLOCK(&ip_conntrack_lock);

  118.         if (exp) {
  119.                 if (exp->expectfn)
  120.                         exp->expectfn(conntrack, exp);
  121.                 destroy_expect(exp);
  122.         }

  123.         /*返回的是初始方向的hash节点*/
  124.         return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
  125. }
复制代码




在前文中提到过,一条完整的连接,采用struct ip_conntrack 结构描述,初始化函数的主要功能,就是分配一个这样的空间,然后初始化它的一些成员。

在这个函数中,有三个重要的地方需要注意,一个是根据当前tuple,计算出应答方向的tuple,它是调用ip_ct_invert_tuple 函数实现的:

  1. int
  2. ip_ct_invert_tuple(struct ip_conntrack_tuple *inverse,
  3.                    const struct ip_conntrack_tuple *orig,
  4.                    const struct ip_conntrack_protocol *protocol)
  5. {
  6.         inverse->src.ip = orig->dst.ip;
  7.         inverse->dst.ip = orig->src.ip;
  8.         inverse->dst.protonum = orig->dst.protonum;
  9.         inverse->dst.dir = !orig->dst.dir;

  10.         return protocol->invert_tuple(inverse, orig);
  11. }
复制代码




这个函数事实上,与前面讲的tuple的转换是一样的,只是来了个乾坤大挪移,把来源和目的,以及方向对调了。

另一个重点的是函数对特殊协议的支持,我们这里暂时跳过了这部份。

第三个地方是调用协议的new函数:
        if (!protocol->new(conntrack, skb)) {
                kmem_cache_free(ip_conntrack_cachep, conntrack);
                return NULL;
        }
new 函数指定在每个封包次创建连接时被调用,它根据协议的不同,所处理的过程不同,以ICMP协议为例:

  1. /* Called when a new connection for this protocol found. */
  2. static int icmp_new(struct ip_conntrack *conntrack,
  3.                     const struct sk_buff *skb)
  4. {
  5.         static u_int8_t valid_new[]
  6.                 = { [ICMP_ECHO] = 1,
  7.                     [ICMP_TIMESTAMP] = 1,
  8.                     [ICMP_INFO_REQUEST] = 1,
  9.                     [ICMP_ADDRESS] = 1 };

  10.         if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
  11.             || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
  12.                 /* Can't create a new ICMP `conn' with this. */
  13.                 DEBUGP("icmp: can't create new conn with type %u\n",
  14.                        conntrack->tuplehash[0].tuple.dst.u.icmp.type);
  15.                 DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
  16.                 return 0;
  17.         }
  18.         atomic_set(&conntrack->proto.icmp.count, 0);
  19.         return 1;
  20. }
复制代码


对于ICMP协议而言,仅有ICMP 请求回显、时间戳请求、信息请求(已经很少用了)、地址掩码请求这四个“请求”,可能是一个“新建”的连接,所以,ICMP协议的new函数判断是否是一个全法的ICMP新建连接,如果是非法的,则返回0,否则,初始化协议使用计数器,返回1。

5.4 连接状态的判断
resolve_normal_ct 函数的后一个重要的工作是对连接状态的判断,tuple中包含一个“方向”成员dst.dir,对于一个初始连接,它是IP_CT_DIR_ORIGINAL:
tuple->dst.dir = IP_CT_DIR_ORIGINAL;
而它的应答包的tuple,则为IP_CT_DIR_REPLY:
inverse->dst.dir = !orig->dst.dir;
IP_CT_DIR_ORIGINAL 和IP_CT_DIR_REPLY都是枚举变量:

  1. enum ip_conntrack_dir
  2. {
  3.         IP_CT_DIR_ORIGINAL,
  4.         IP_CT_DIR_REPLY,
  5.         IP_CT_DIR_MAX
  6. };
复制代码



宏DIRECTION 就根据tuple中对应成员的值,判断数据包的方向,
/* If we're the first tuple, it's the original dir. */
#define DIRECTION(h) ((enum ip_conntrack_dir)(h)->tuple.dst.dir)

但是,还有一些特殊地方,比如TCP协议,它是一个面向连接的协议,所以,它的“初始”或“应答”包,并不一定就是“新建”或单纯的“应答”包,而是在一个连接过程中的“已建连接包”,另一个,如FTP等 复杂协议,它们还存在一些“关联”的连接,当然这两部份目前还没有涉及到,但并不影响我们分析如下这段代码:


  1.         /* 如果是一个应答包 ,设置状态为已建+应答*/
  2.         if (DIRECTION(h) == IP_CT_DIR_REPLY) {
  3.                 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
  4.                 /* 设置应答标志变量 */
  5.                 *set_reply = 1;
  6.         } else {
  7.                 /* 新建连接方过来的数据包,对面向连接的协议而言,可能是一个已建连接,判断其标志位*/
  8.                 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
  9.                         DEBUGP("ip_conntrack_in: normal packet for %p\n",
  10.                                ct);
  11.                         *ctinfo = IP_CT_ESTABLISHED;
  12.                 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
  13.                         DEBUGP("ip_conntrack_in: related packet for %p\n",
  14.                                ct);
  15.                         *ctinfo = IP_CT_RELATED;                        //关联连接
  16.                 } else {
  17.                         DEBUGP("ip_conntrack_in: new packet for %p\n",
  18.                                ct);
  19.                         *ctinfo = IP_CT_NEW;                                //否则,则为一个新建连接
  20.                 }
  21.                 *set_reply = 0;
  22.         }
  23.         
  24.         /*设置数据包skb与连接状态的关联*/
  25.         skb->nfct = &ct->ct_general;
  26.         /*每个sk_buff都将与ip_conntrack的一个状态关联,所以从sk_buff可以得到相应ip_conntrack的状态,即数据包的状态*/
  27.         skb->nfctinfo = *ctinfo;
  28.         return ct;
复制代码



以上的代表所表示的发送或应答的状态如下图所示:


6.        ip_confirm

当数据包要离开Linux时,它会穿过NF_IP_POST_ROUTING Hook点,状态跟踪模块在这里注册了ip_refrag函数:

  1. static unsigned int ip_refrag(unsigned int hooknum,
  2.                               struct sk_buff **pskb,
  3.                               const struct net_device *in,
  4.                               const struct net_device *out,
  5.                               int (*okfn)(struct sk_buff *))
  6. {
  7.         struct rtable *rt = (struct rtable *)(*pskb)->dst;

  8.         /* ip_confirm函数用于处理将tuple加入hash表等重要的后续处理 */
  9.         if (ip_confirm(hooknum, pskb, in, out, okfn) != NF_ACCEPT)
  10.                 return NF_DROP;

  11.         /* 在连接跟踪开始之前,对分片包进行了重组,这里判断数据包是否需要分片,如果要分片,就调用ip_fragment分片函数将数据包分片发送出去,因为数据包已经被发送走了,所以,在它之后的任何Hook函数已经没有意思了 */
  12.         if ((*pskb)->len > dst_mtu(&rt->u.dst) &&
  13.             !skb_shinfo(*pskb)->tso_size) {
  14.                 /* No hook can be after us, so this should be OK. */
  15.                 ip_fragment(*pskb, okfn);
  16.                 return NF_STOLEN;
  17.         }
  18.         return NF_ACCEPT;
  19. }
复制代码



ip_confirm 函数是状态跟踪的另一个重要的函数:

  1. static unsigned int ip_confirm(unsigned int hooknum,
  2.                                struct sk_buff **pskb,
  3.                                const struct net_device *in,
  4.                                const struct net_device *out,
  5.                                int (*okfn)(struct sk_buff *))
  6. {
  7.         /* We've seen it coming out the other side: confirm it */
  8.         return ip_conntrack_confirm(pskb);
  9. }
复制代码


        函数仅是转向,将控制权转交给ip_conntrack_confirm函数:

  1. /* Confirm a connection: returns NF_DROP if packet must be dropped. */
  2. static inline int ip_conntrack_confirm(struct sk_buff **pskb)
  3. {
  4.         if ((*pskb)->nfct
  5.             && !is_confirmed((struct ip_conntrack *)(*pskb)->nfct))
  6.                 return __ip_conntrack_confirm(pskb);
  7.         return NF_ACCEPT;
  8. }
复制代码




is_comfirmed函数用于判断数据包是否已经被__ip_conntrack_confirm函数处理过了,它是通过IPS_CONFIRMED_BIT 标志位来判断,而这个标志位当然是在__ip_conntrack_confirm函数中来设置的:

  1. /* Confirm a connection given skb; places it in hash table */
  2. int
  3. __ip_conntrack_confirm(struct sk_buff **pskb)
  4. {
  5.         unsigned int hash, repl_hash;
  6.         struct ip_conntrack *ct;
  7.         enum ip_conntrack_info ctinfo;

  8.         /*取得数据包的连接状态*/
  9.         ct = ip_conntrack_get(*pskb, &ctinfo);

  10.         /* 如果当前包不是一个初始方向的封包,则直接返回. */
  11.         if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
  12.                 return NF_ACCEPT;

  13. /*计算初始及应答两个方向tuple对应的hash值*/
  14.         hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
  15.         repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);

  16.         /* We're not in hash table, and we refuse to set up related
  17.            connections for unconfirmed conns.  But packet copies and
  18.            REJECT will give spurious warnings here. */
  19.         /* IP_NF_ASSERT(atomic_read(&ct->ct_general.use) == 1); */

  20.         /* No external references means noone else could have
  21.            confirmed us. */
  22.         IP_NF_ASSERT(!is_confirmed(ct));
  23.         DEBUGP("Confirming conntrack %p\n", ct);

  24.         WRITE_LOCK(&ip_conntrack_lock);

  25.         /* 在hash表中查找初始及应答的节点*/
  26.         if (!LIST_FIND(&ip_conntrack_hash[hash],
  27.                        conntrack_tuple_cmp,
  28.                        struct ip_conntrack_tuple_hash *,
  29.                        &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, NULL)
  30.             && !LIST_FIND(&ip_conntrack_hash[repl_hash],
  31.                           conntrack_tuple_cmp,
  32.                           struct ip_conntrack_tuple_hash *,
  33.                           &ct->tuplehash[IP_CT_DIR_REPLY].tuple, NULL)) {
  34.                 /* Remove from unconfirmed list */
  35.                 list_del(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list);

  36.                 /*将当前连接(初始和应答的tuple)添加进hash表*/
  37.                 list_prepend(&ip_conntrack_hash[hash],
  38.                              &ct->tuplehash[IP_CT_DIR_ORIGINAL]);
  39.                 list_prepend(&ip_conntrack_hash[repl_hash],
  40.                              &ct->tuplehash[IP_CT_DIR_REPLY]);
  41.                 /* Timer relative to confirmation time, not original
  42.                    setting time, otherwise we'd get timer wrap in
  43.                    weird delay cases. */
  44.                 ct->timeout.expires += jiffies;
  45.                 add_timer(&ct->timeout);
  46.                 atomic_inc(&ct->ct_general.use);
  47.                 set_bit(IPS_CONFIRMED_BIT, &ct->status);
  48.                 CONNTRACK_STAT_INC(insert);
  49.                 WRITE_UNLOCK(&ip_conntrack_lock);
  50.                 return NF_ACCEPT;
  51.         }

  52.         CONNTRACK_STAT_INC(insert_failed);
  53.         WRITE_UNLOCK(&ip_conntrack_lock);

  54.         return NF_DROP;
  55. }
分享好友

分享这个小栈给你的朋友们,一起进步吧。

内核源码
创建时间:2020-05-18 13:36:55
内核源码精华帖内容汇总
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • 飘絮絮絮丶
    专家
戳我,来吐槽~