📄 ip_conntrack_core.c
字号:
/* Traverse backwards: gives us oldest, which is roughly LRU */ struct ip_conntrack_tuple_hash *h; struct ip_conntrack *ct = NULL; int dropped = 0; READ_LOCK(&ip_conntrack_lock); h = LIST_FIND_B(chain, unreplied, struct ip_conntrack_tuple_hash *); if (h) { ct = tuplehash_to_ctrack(h); atomic_inc(&ct->ct_general.use); } READ_UNLOCK(&ip_conntrack_lock); if (!ct) return dropped; if (del_timer(&ct->timeout)) { death_by_timeout((unsigned long)ct); dropped = 1; CONNTRACK_STAT_INC(early_drop); } ip_conntrack_put(ct); return dropped;}static inline int helper_cmp(const struct ip_conntrack_helper *i, const struct ip_conntrack_tuple *rtuple){ return ip_ct_tuple_mask_cmp(rtuple, &i->tuple, &i->mask);}static struct ip_conntrack_helper *ip_ct_find_helper(const struct ip_conntrack_tuple *tuple){ return LIST_FIND(&helpers, helper_cmp, struct ip_conntrack_helper *, tuple);}/* Allocate a new conntrack: we return -ENOMEM if classification failed due to stress. Otherwise it really is unclassifiable. */static struct ip_conntrack_tuple_hash *init_conntrack(const struct ip_conntrack_tuple *tuple, struct ip_conntrack_protocol *protocol, struct sk_buff *skb){ struct ip_conntrack *conntrack; struct ip_conntrack_tuple repl_tuple; size_t hash; struct ip_conntrack_expect *exp; if (!ip_conntrack_hash_rnd_initted) { get_random_bytes(&ip_conntrack_hash_rnd, 4); ip_conntrack_hash_rnd_initted = 1; } hash = hash_conntrack(tuple); if (ip_conntrack_max && atomic_read(&ip_conntrack_count) >= ip_conntrack_max) { /* Try dropping from this hash chain. */ if (!early_drop(&ip_conntrack_hash[hash])) { if (net_ratelimit()) printk(KERN_WARNING "ip_conntrack: table full, dropping" " packet.\n"); return ERR_PTR(-ENOMEM); } } if (!ip_ct_invert_tuple(&repl_tuple, tuple, protocol)) { DEBUGP("Can't invert tuple.\n"); return NULL; } conntrack = kmem_cache_alloc(ip_conntrack_cachep, GFP_ATOMIC); if (!conntrack) { DEBUGP("Can't allocate conntrack.\n"); return ERR_PTR(-ENOMEM); } memset(conntrack, 0, sizeof(*conntrack)); atomic_set(&conntrack->ct_general.use, 1); conntrack->ct_general.destroy = destroy_conntrack; conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *tuple; conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = repl_tuple; if (!protocol->new(conntrack, skb)) { kmem_cache_free(ip_conntrack_cachep, conntrack); return NULL; } /* Don't set timer yet: wait for confirmation */ init_timer(&conntrack->timeout); conntrack->timeout.data = (unsigned long)conntrack; conntrack->timeout.function = death_by_timeout; WRITE_LOCK(&ip_conntrack_lock); exp = find_expectation(tuple); if (exp) { DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n", conntrack, exp); /* Welcome, Mr. Bond. We've been expecting you... */ __set_bit(IPS_EXPECTED_BIT, &conntrack->status); conntrack->master = exp->master;#if CONFIG_IP_NF_CONNTRACK_MARK conntrack->mark = exp->master->mark;#endif nf_conntrack_get(&conntrack->master->ct_general); CONNTRACK_STAT_INC(expect_new); } else { conntrack->helper = ip_ct_find_helper(&repl_tuple); CONNTRACK_STAT_INC(new); } /* Overload tuple linked list to put us in unconfirmed list. */ list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed); atomic_inc(&ip_conntrack_count); WRITE_UNLOCK(&ip_conntrack_lock); if (exp) { if (exp->expectfn) exp->expectfn(conntrack, exp); destroy_expect(exp); } return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];}/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */static inline struct ip_conntrack *resolve_normal_ct(struct sk_buff *skb, struct ip_conntrack_protocol *proto, int *set_reply, unsigned int hooknum, enum ip_conntrack_info *ctinfo){ struct ip_conntrack_tuple tuple; struct ip_conntrack_tuple_hash *h; struct ip_conntrack *ct; IP_NF_ASSERT((skb->nh.iph->frag_off & htons(IP_OFFSET)) == 0); if (!ip_ct_get_tuple(skb->nh.iph, skb, skb->nh.iph->ihl*4, &tuple,proto)) return NULL; /* look for tuple match */ h = ip_conntrack_find_get(&tuple, NULL); if (!h) { h = init_conntrack(&tuple, proto, skb); if (!h) return NULL; if (IS_ERR(h)) return (void *)h; } ct = tuplehash_to_ctrack(h); /* It exists; we have (non-exclusive) reference. */ if (DIRECTION(h) == IP_CT_DIR_REPLY) { *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY; /* Please set reply bit if this packet OK */ *set_reply = 1; } else { /* Once we've had two way comms, always ESTABLISHED. */ if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { DEBUGP("ip_conntrack_in: normal packet for %p\n", ct); *ctinfo = IP_CT_ESTABLISHED; } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) { DEBUGP("ip_conntrack_in: related packet for %p\n", ct); *ctinfo = IP_CT_RELATED; } else { DEBUGP("ip_conntrack_in: new packet for %p\n", ct); *ctinfo = IP_CT_NEW; } *set_reply = 0; } skb->nfct = &ct->ct_general; skb->nfctinfo = *ctinfo; return ct;}/* Netfilter hook itself. */unsigned int ip_conntrack_in(unsigned int hooknum, struct sk_buff **pskb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)){ struct ip_conntrack *ct; enum ip_conntrack_info ctinfo; struct ip_conntrack_protocol *proto; int set_reply; int ret; /* Previously seen (loopback or untracked)? Ignore. */ if ((*pskb)->nfct) { CONNTRACK_STAT_INC(ignore); return NF_ACCEPT; } /* Never happen */ if ((*pskb)->nh.iph->frag_off & htons(IP_OFFSET)) { if (net_ratelimit()) { printk(KERN_ERR "ip_conntrack_in: Frag of proto %u (hook=%u)\n", (*pskb)->nh.iph->protocol, hooknum); } return NF_DROP; } /* FIXME: Do this right please. --RR */ (*pskb)->nfcache |= NFC_UNKNOWN;/* Doesn't cover locally-generated broadcast, so not worth it. */#if 0 /* Ignore broadcast: no `connection'. */ if ((*pskb)->pkt_type == PACKET_BROADCAST) { printk("Broadcast packet!\n"); return NF_ACCEPT; } else if (((*pskb)->nh.iph->daddr & htonl(0x000000FF)) == htonl(0x000000FF)) { printk("Should bcast: %u.%u.%u.%u->%u.%u.%u.%u (sk=%p, ptype=%u)\n", NIPQUAD((*pskb)->nh.iph->saddr), NIPQUAD((*pskb)->nh.iph->daddr), (*pskb)->sk, (*pskb)->pkt_type); }#endif proto = ip_ct_find_proto((*pskb)->nh.iph->protocol); /* It may be an special packet, error, unclean... * inverse of the return code tells to the netfilter * core what to do with the packet. */ if (proto->error != NULL && (ret = proto->error(*pskb, &ctinfo, hooknum)) <= 0) { CONNTRACK_STAT_INC(error); CONNTRACK_STAT_INC(invalid); return -ret; } if (!(ct = resolve_normal_ct(*pskb, proto,&set_reply,hooknum,&ctinfo))) { /* Not valid part of a connection */ CONNTRACK_STAT_INC(invalid); return NF_ACCEPT; } if (IS_ERR(ct)) { /* Too stressed to deal. */ CONNTRACK_STAT_INC(drop); return NF_DROP; } IP_NF_ASSERT((*pskb)->nfct); ret = proto->packet(ct, *pskb, ctinfo); if (ret < 0) { /* Invalid: inverse of the return code tells * the netfilter core what to do*/ nf_conntrack_put((*pskb)->nfct); (*pskb)->nfct = NULL; CONNTRACK_STAT_INC(invalid); return -ret; } if (set_reply) set_bit(IPS_SEEN_REPLY_BIT, &ct->status); return ret;}int invert_tuplepr(struct ip_conntrack_tuple *inverse, const struct ip_conntrack_tuple *orig){ return ip_ct_invert_tuple(inverse, orig, ip_ct_find_proto(orig->dst.protonum));}/* Would two expected things clash? */static inline int expect_clash(const struct ip_conntrack_expect *a, const struct ip_conntrack_expect *b){ /* Part covered by intersection of masks must be unequal, otherwise they clash */ struct ip_conntrack_tuple intersect_mask = { { a->mask.src.ip & b->mask.src.ip, { a->mask.src.u.all & b->mask.src.u.all } }, { a->mask.dst.ip & b->mask.dst.ip, { a->mask.dst.u.all & b->mask.dst.u.all }, a->mask.dst.protonum & b->mask.dst.protonum } }; return ip_ct_tuple_mask_cmp(&a->tuple, &b->tuple, &intersect_mask);}static inline int expect_matches(const struct ip_conntrack_expect *a, const struct ip_conntrack_expect *b){ return a->master == b->master && ip_ct_tuple_equal(&a->tuple, &b->tuple) && ip_ct_tuple_equal(&a->mask, &b->mask);}/* Generally a bad idea to call this: could have matched already. */void ip_conntrack_unexpect_related(struct ip_conntrack_expect *exp){ struct ip_conntrack_expect *i; WRITE_LOCK(&ip_conntrack_lock); /* choose the the oldest expectation to evict */ list_for_each_entry_reverse(i, &ip_conntrack_expect_list, list) { if (expect_matches(i, exp) && del_timer(&i->timeout)) { unlink_expect(i); WRITE_UNLOCK(&ip_conntrack_lock); destroy_expect(i); return; } } WRITE_UNLOCK(&ip_conntrack_lock);}struct ip_conntrack_expect *ip_conntrack_expect_alloc(void){ struct ip_conntrack_expect *new; new = kmem_cache_alloc(ip_conntrack_expect_cachep, GFP_ATOMIC); if (!new) { DEBUGP("expect_related: OOM allocating expect\n"); return NULL; } new->master = NULL; return new;}void ip_conntrack_expect_free(struct ip_conntrack_expect *expect){ kmem_cache_free(ip_conntrack_expect_cachep, expect);}static void ip_conntrack_expect_insert(struct ip_conntrack_expect *exp){ atomic_inc(&exp->master->ct_general.use); exp->master->expecting++; list_add(&exp->list, &ip_conntrack_expect_list); if (exp->master->helper->timeout) { init_timer(&exp->timeout); exp->timeout.data = (unsigned long)exp; exp->timeout.function = expectation_timed_out; exp->timeout.expires = jiffies + exp->master->helper->timeout * HZ; add_timer(&exp->timeout); } else exp->timeout.function = NULL; CONNTRACK_STAT_INC(expect_create);}/* Race with expectations being used means we could have none to find; OK. */static void evict_oldest_expect(struct ip_conntrack *master){ struct ip_conntrack_expect *i; list_for_each_entry_reverse(i, &ip_conntrack_expect_list, list) { if (i->master == master) { if (del_timer(&i->timeout)) { unlink_expect(i); destroy_expect(i); } break; } }}static inline int refresh_timer(struct ip_conntrack_expect *i){ if (!del_timer(&i->timeout)) return 0; i->timeout.expires = jiffies + i->master->helper->timeout*HZ; add_timer(&i->timeout); return 1;}int ip_conntrack_expect_related(struct ip_conntrack_expect *expect){ struct ip_conntrack_expect *i; int ret; DEBUGP("ip_conntrack_expect_related %p\n", related_to); DEBUGP("tuple: "); DUMP_TUPLE(&expect->tuple); DEBUGP("mask: "); DUMP_TUPLE(&expect->mask); WRITE_LOCK(&ip_conntrack_lock); list_for_each_entry(i, &ip_conntrack_expect_list, list) { if (expect_matches(i, expect)) { /* Refresh timer: if it's dying, ignore.. */ if (refresh_timer(i)) { ret = 0; /* We don't need the one they've given us. */ ip_conntrack_expect_free(expect); goto out; } } else if (expect_clash(i, expect)) { ret = -EBUSY; goto out; } } /* Will be over limit? */ if (expect->master->helper->max_expected && expect->master->expecting >= expect->master->helper->max_expected) evict_oldest_expect(expect->master); ip_conntrack_expect_insert(expect);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -