⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip_nat_core.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 2 页
字号:
/* NAT for netfilter; shared with compatibility layer. *//* (C) 1999-2001 Paul `Rusty' Russell * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */#include <linux/module.h>#include <linux/types.h>#include <linux/timer.h>#include <linux/skbuff.h>#include <linux/netfilter_ipv4.h>#include <linux/vmalloc.h>#include <net/checksum.h>#include <net/icmp.h>#include <net/ip.h>#include <net/tcp.h>  /* For tcp_prot in getorigdst */#include <linux/icmp.h>#include <linux/udp.h>#include <linux/jhash.h>#define ASSERT_READ_LOCK(x)#define ASSERT_WRITE_LOCK(x)#include <linux/netfilter_ipv4/ip_conntrack.h>#include <linux/netfilter_ipv4/ip_conntrack_core.h>#include <linux/netfilter_ipv4/ip_conntrack_protocol.h>#include <linux/netfilter_ipv4/ip_nat.h>#include <linux/netfilter_ipv4/ip_nat_protocol.h>#include <linux/netfilter_ipv4/ip_nat_core.h>#include <linux/netfilter_ipv4/ip_nat_helper.h>#include <linux/netfilter_ipv4/ip_conntrack_helper.h>#include <linux/netfilter_ipv4/listhelp.h>#if 0#define DEBUGP printk#else#define DEBUGP(format, args...)#endifDEFINE_RWLOCK(ip_nat_lock);/* Calculated at init based on memory size */static unsigned int ip_nat_htable_size;static struct list_head *bysource;#define MAX_IP_NAT_PROTO 256static struct ip_nat_protocol *ip_nat_protos[MAX_IP_NAT_PROTO];static inline struct ip_nat_protocol *__ip_nat_proto_find(u_int8_t protonum){	return ip_nat_protos[protonum];}struct ip_nat_protocol *ip_nat_proto_find_get(u_int8_t protonum){	struct ip_nat_protocol *p;	/* we need to disable preemption to make sure 'p' doesn't get	 * removed until we've grabbed the reference */	preempt_disable();	p = __ip_nat_proto_find(protonum);	if (!try_module_get(p->me))		p = &ip_nat_unknown_protocol;	preempt_enable();	return p;}EXPORT_SYMBOL_GPL(ip_nat_proto_find_get);voidip_nat_proto_put(struct ip_nat_protocol *p){	module_put(p->me);}EXPORT_SYMBOL_GPL(ip_nat_proto_put);/* We keep an extra hash for each conntrack, for fast searching. */static inline unsigned inthash_by_src(const struct ip_conntrack_tuple *tuple){	/* Original src, to ensure we map it consistently if poss. */	return jhash_3words(tuple->src.ip, tuple->src.u.all,			    tuple->dst.protonum, 0) % ip_nat_htable_size;}/* Noone using conntrack by the time this called. */static void ip_nat_cleanup_conntrack(struct ip_conntrack *conn){	if (!(conn->status & IPS_NAT_DONE_MASK))		return;	write_lock_bh(&ip_nat_lock);	list_del(&conn->nat.info.bysource);	write_unlock_bh(&ip_nat_lock);}/* We do checksum mangling, so if they were wrong before they're still * wrong.  Also works for incomplete packets (eg. ICMP dest * unreachables.) */u_int16_tip_nat_cheat_check(u_int32_t oldvalinv, u_int32_t newval, u_int16_t oldcheck){	u_int32_t diffs[] = { oldvalinv, newval };	return csum_fold(csum_partial((char *)diffs, sizeof(diffs),				      oldcheck^0xFFFF));}EXPORT_SYMBOL(ip_nat_cheat_check);/* Is this tuple already taken? (not by us) */intip_nat_used_tuple(const struct ip_conntrack_tuple *tuple,		  const struct ip_conntrack *ignored_conntrack){	/* Conntrack tracking doesn't keep track of outgoing tuples; only	   incoming ones.  NAT means they don't have a fixed mapping,	   so we invert the tuple and look for the incoming reply.	   We could keep a separate hash if this proves too slow. */	struct ip_conntrack_tuple reply;	invert_tuplepr(&reply, tuple);	return ip_conntrack_tuple_taken(&reply, ignored_conntrack);}EXPORT_SYMBOL(ip_nat_used_tuple);/* If we source map this tuple so reply looks like reply_tuple, will * that meet the constraints of range. */static intin_range(const struct ip_conntrack_tuple *tuple,	 const struct ip_nat_range *range){	struct ip_nat_protocol *proto = 				__ip_nat_proto_find(tuple->dst.protonum);	/* If we are supposed to map IPs, then we must be in the	   range specified, otherwise let this drag us onto a new src IP. */	if (range->flags & IP_NAT_RANGE_MAP_IPS) {		if (ntohl(tuple->src.ip) < ntohl(range->min_ip)		    || ntohl(tuple->src.ip) > ntohl(range->max_ip))			return 0;	}	if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)	    || proto->in_range(tuple, IP_NAT_MANIP_SRC,			       &range->min, &range->max))		return 1;	return 0;}static inline intsame_src(const struct ip_conntrack *ct,	 const struct ip_conntrack_tuple *tuple){	return (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum		== tuple->dst.protonum		&& ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip		== tuple->src.ip		&& ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all		== tuple->src.u.all);}/* Only called for SRC manip */static intfind_appropriate_src(const struct ip_conntrack_tuple *tuple,		     struct ip_conntrack_tuple *result,		     const struct ip_nat_range *range){	unsigned int h = hash_by_src(tuple);	struct ip_conntrack *ct;	read_lock_bh(&ip_nat_lock);	list_for_each_entry(ct, &bysource[h], nat.info.bysource) {		if (same_src(ct, tuple)) {			/* Copy source part from reply tuple. */			invert_tuplepr(result,				       &ct->tuplehash[IP_CT_DIR_REPLY].tuple);			result->dst = tuple->dst;			if (in_range(result, range)) {				read_unlock_bh(&ip_nat_lock);				return 1;			}		}	}	read_unlock_bh(&ip_nat_lock);	return 0;}/* For [FUTURE] fragmentation handling, we want the least-used   src-ip/dst-ip/proto triple.  Fairness doesn't come into it.  Thus   if the range specifies 1.2.3.4 ports 10000-10005 and 1.2.3.5 ports   1-65535, we don't do pro-rata allocation based on ports; we choose   the ip with the lowest src-ip/dst-ip/proto usage.*/static voidfind_best_ips_proto(struct ip_conntrack_tuple *tuple,		    const struct ip_nat_range *range,		    const struct ip_conntrack *conntrack,		    enum ip_nat_manip_type maniptype){	u_int32_t *var_ipp;	/* Host order */	u_int32_t minip, maxip, j;	/* No IP mapping?  Do nothing. */	if (!(range->flags & IP_NAT_RANGE_MAP_IPS))		return;	if (maniptype == IP_NAT_MANIP_SRC)		var_ipp = &tuple->src.ip;	else		var_ipp = &tuple->dst.ip;	/* Fast path: only one choice. */	if (range->min_ip == range->max_ip) {		*var_ipp = range->min_ip;		return;	}	/* Hashing source and destination IPs gives a fairly even	 * spread in practice (if there are a small number of IPs	 * involved, there usually aren't that many connections	 * anyway).  The consistency means that servers see the same	 * client coming from the same IP (some Internet Banking sites	 * like this), even across reboots. */	minip = ntohl(range->min_ip);	maxip = ntohl(range->max_ip);	j = jhash_2words(tuple->src.ip, tuple->dst.ip, 0);	*var_ipp = htonl(minip + j % (maxip - minip + 1));}/* Manipulate the tuple into the range given.  For NF_IP_POST_ROUTING, * we change the source to map into the range.  For NF_IP_PRE_ROUTING * and NF_IP_LOCAL_OUT, we change the destination to map into the * range.  It might not be possible to get a unique tuple, but we try. * At worst (or if we race), we will end up with a final duplicate in * __ip_conntrack_confirm and drop the packet. */static voidget_unique_tuple(struct ip_conntrack_tuple *tuple,		 const struct ip_conntrack_tuple *orig_tuple,		 const struct ip_nat_range *range,		 struct ip_conntrack *conntrack,		 enum ip_nat_manip_type maniptype){	struct ip_nat_protocol *proto;	/* 1) If this srcip/proto/src-proto-part is currently mapped,	   and that same mapping gives a unique tuple within the given	   range, use that.	   This is only required for source (ie. NAT/masq) mappings.	   So far, we don't do local source mappings, so multiple	   manips not an issue.  */	if (maniptype == IP_NAT_MANIP_SRC) {		if (find_appropriate_src(orig_tuple, tuple, range)) {			DEBUGP("get_unique_tuple: Found current src map\n");			if (!ip_nat_used_tuple(tuple, conntrack))				return;		}	}	/* 2) Select the least-used IP/proto combination in the given	   range. */	*tuple = *orig_tuple;	find_best_ips_proto(tuple, range, conntrack, maniptype);	/* 3) The per-protocol part of the manip is made to map into	   the range to make a unique tuple. */	proto = ip_nat_proto_find_get(orig_tuple->dst.protonum);	/* Only bother mapping if it's not already in range and unique */	if ((!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)	     || proto->in_range(tuple, maniptype, &range->min, &range->max))	    && !ip_nat_used_tuple(tuple, conntrack)) {		ip_nat_proto_put(proto);		return;	}	/* Last change: get protocol to try to obtain unique tuple. */	proto->unique_tuple(tuple, range, maniptype, conntrack);	ip_nat_proto_put(proto);}unsigned intip_nat_setup_info(struct ip_conntrack *conntrack,		  const struct ip_nat_range *range,		  unsigned int hooknum){	struct ip_conntrack_tuple curr_tuple, new_tuple;	struct ip_nat_info *info = &conntrack->nat.info;	int have_to_hash = !(conntrack->status & IPS_NAT_DONE_MASK);	enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);	IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING		     || hooknum == NF_IP_POST_ROUTING		     || hooknum == NF_IP_LOCAL_IN		     || hooknum == NF_IP_LOCAL_OUT);	BUG_ON(ip_nat_initialized(conntrack, maniptype));	/* What we've got will look like inverse of reply. Normally	   this is what is in the conntrack, except for prior	   manipulations (future optimization: if num_manips == 0,	   orig_tp =	   conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple) */	invert_tuplepr(&curr_tuple,		       &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple);	get_unique_tuple(&new_tuple, &curr_tuple, range, conntrack, maniptype);	if (!ip_ct_tuple_equal(&new_tuple, &curr_tuple)) {		struct ip_conntrack_tuple reply;		/* Alter conntrack table so will recognize replies. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -