ipsec_rcv.c

来自「FREESWAN VPN源代码包」· C语言 代码 · 共 2,240 行 · 第 1/5 页

C
2,240
字号
	*/	/*	 * First things first -- look us up in the erouting tables.	 */	matcher.sen_len = sizeof (struct sockaddr_encap);	matcher.sen_family = AF_ENCAP;	matcher.sen_type = SENT_IP4;	if(ipp->protocol == IPPROTO_IPIP) {		struct iphdr *ipp2;		ipp2 = (struct iphdr*) (((char*)ipp) + (ipp->ihl << 2));		matcher.sen_ip_src.s_addr = ipp2->saddr;		matcher.sen_ip_dst.s_addr = ipp2->daddr;	} else {		matcher.sen_ip_src.s_addr = ipp->saddr;		matcher.sen_ip_dst.s_addr = ipp->daddr;	}		/*	 * The spinlock is to prevent any other process from accessing or	 * deleting the eroute while we are using and updating it.	 */	spin_lock(&eroute_lock);		er = ipsec_findroute(&matcher);	if(er) {		policy_said = er->er_said;		policy_eaddr = er->er_eaddr;		policy_emask = er->er_emask;		er->er_count++;		er->er_lasttime = jiffies/HZ;	}		spin_unlock(&eroute_lock);	if(er) {		/*		 * The spinlock is to prevent any other process from		 * accessing or deleting the tdb while we are using and		 * updating it.		 */		spin_lock(&tdb_lock);		policy_tdb = gettdb(&policy_said);		if (policy_tdb == NULL) {			spin_unlock(&tdb_lock);			KLIPS_PRINT(debug_rcv,				    "klips_debug:ipsec_rcv: "				    "no Tunnel Descriptor Block for SA%s: incoming packet with no policy SA, dropped.\n",				    sa_len ? sa : " (error)");			goto rcvleave;		}				sa_len = satoa(policy_said, 0, sa, SATOA_BUF);		KLIPS_PRINT(debug_rcv,			    "klips_debug:ipsec_rcv: "			    "found policy Tunnel Descriptor Block -- SA:%s\n",			    sa_len ? sa : " (error)");		while(1) {			if(policy_tdb->tdb_inext) {				policy_tdb = policy_tdb->tdb_inext;			} else {				break;			}		}		if(policy_tdb != tdbp) {			spin_unlock(&tdb_lock);			KLIPS_PRINT(debug_rcv,				    "klips_debug:ipsec_rcv: "				    "Tunnel Descriptor Block for SA%s: incoming packet with different policy SA, dropped.\n",				    sa_len ? sa : " (error)");			goto rcvleave;		}		/* spin_unlock(&tdb_lock); */	}#endif /* INBOUND_POLICY_CHECK_eroute */	spin_unlock(&tdb_lock);#ifdef NET_21	if(stats) {		stats->rx_bytes += skb->len;	}	if(skb->dst) {		dst_release(skb->dst);		skb->dst = NULL;	}	skb->pkt_type = PACKET_HOST;	if(hard_header_len &&	   (skb->mac.raw != (skb->data - hard_header_len)) &&	   (hard_header_len <= skb_headroom(skb))) {		/* copy back original MAC header */		memmove(skb->data - hard_header_len, skb->mac.raw, hard_header_len);		skb->mac.raw = skb->data - hard_header_len;	}#endif /* NET_21 */#ifdef CONFIG_IPSEC_IPCOMP	if(ipp->protocol == IPPROTO_COMP) {		unsigned int flags = 0;		if(sysctl_ipsec_inbound_policy_check) {			KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,				"klips_debug:ipsec_rcv: "				"inbound policy checking enabled, IPCOMP follows IPIP, dropped.\n");			if (stats) {				stats->rx_errors++;			}			goto rcvleave;		}		/*		  XXX need a TDB for updating ratio counters but it is not		  following policy anyways so it is not a priority		*/		skb = skb_decompress(skb, NULL, &flags);		if (!skb || flags) {			KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,				"klips_debug:ipsec_rcv: "				"skb_decompress() returned error flags: %d, dropped.\n",			       flags);			if (stats) {				stats->rx_errors++;			}			goto rcvleave;		}	}#endif /* CONFIG_IPSEC_IPCOMP */#ifdef SKB_RESET_NFCT        nf_conntrack_put(skb->nfct);        skb->nfct = NULL;#ifdef CONFIG_NETFILTER_DEBUG	skb->nf_debug = 0;#endif /* CONFIG_NETFILTER_DEBUG */#endif /* SKB_RESET_NFCT */	KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,		    "klips_debug:ipsec_rcv: "		    "netif_rx() called.\n");	netif_rx(skb);	MOD_DEC_USE_COUNT;	return(0);	 rcvleave: 	if(skb) {#ifdef NET_21                kfree_skb(skb);#else /* NET_21 */                kfree_skb(skb, FREE_WRITE);#endif /* NET_21 */	}	MOD_DEC_USE_COUNT;	return(0);}struct inet_protocol ah_protocol ={	ipsec_rcv,				/* AH handler */	NULL,				/* TUNNEL error control */	0,				/* next */	IPPROTO_AH,			/* protocol ID */	0,				/* copy */	NULL,				/* data */	"AH"				/* name */};struct inet_protocol esp_protocol = {	ipsec_rcv,			/* ESP handler          */	NULL,				/* TUNNEL error control */	0,				/* next */	IPPROTO_ESP,			/* protocol ID */	0,				/* copy */	NULL,				/* data */	"ESP"				/* name */};#if 0/* We probably don't want to install a pure IPCOMP protocol handler, but   only want to handle IPCOMP if it is encapsulated inside an ESP payload   (which is already handled) */#ifdef CONFIG_IPSEC_IPCOMPstruct inet_protocol comp_protocol ={	ipsec_rcv,			/* COMP handler		*/	NULL,				/* COMP error control	*/	0,				/* next */	IPPROTO_COMP,			/* protocol ID */	0,				/* copy */	NULL,				/* data */	"COMP"				/* name */};#endif /* CONFIG_IPSEC_IPCOMP */#endif/* * $Log: ipsec_rcv.c,v $ * Revision 1.15  2003/07/04 19:07:34  ken * Added NAT-T 0.6 diff/patch from Tuomo * * Revision 1.14  2003/02/07 13:14:24  ken * Pullin jjo's ALG 0.8.1rc branch * * Revision 1.13.2.1  2003/02/06 22:09:49  jjo * sync to alg-0.8.1-rc4 * * Revision 1.13  2003/01/19 23:06:27  ken * Added MTS Patch (fix broken AT&T VPN System for IBM Users) - amcedwards * * Revision 1.12  2003/01/02 21:47:45  ken * NAT-T 0.5 * * Revision 1.11  2002/11/05 01:41:46  ken * Pulled  from 1.99 * * Revision 1.102.6.6  2002/10/30 17:39:07  rgb * Fixed cut-and-paste error mis-identifying comp runt as ah. * * Revision 1.102.6.5  2002/10/30 05:45:19  rgb * Make sure partial esp/ah/comp headers don't cause oops. * * Revision 1.102.6.4  2002/10/28 19:54:56  sam * Fix scope of shift in AH header length check. * * Revision 1.10  2002/11/05 01:35:58  ken * 1.99cand -> 1.99 Final Code changes * * Revision 1.9  2002/10/26 19:18:48  ken * fixed bad patch of ipsec_rcv.c * * Revision 1.102.6.3  2002/10/25 17:26:40  sam * runt patch applied for DOS * * Revision 1.102.6.2  2002/10/18 05:20:36  sam * Patched with RGB's fix from ipsec_rcv.c v1.109: * * Fixed ESP short packet DOS (and AH and IPCOMP) * * Revision 1.8  2002/10/26 14:32:44  ken * Patch in code from 1.99 for ESP DOS Fix, and RPM build fixes. * patch -p1 -F 3 < ../_kb9-DOSFIX.diff * * Revision 1.7  2002/10/16 00:21:46  ken * Applied JuanJo's NULL Patch * * Revision 1.6  2002/10/16 00:16:50  ken * Backed out David patch in prep for JuanJo's * * Revision 1.5  2002/10/15 18:35:24  ken * Updated README/CHANGES for _kb8 * * Revision 1.4  2002/10/15 17:52:47  ken * Added David De Reu's NULL ALG Bugfix * * Revision 1.3  2002/09/05 03:53:52  ken * Added NAT-T Patch * * Revision 1.2  2002/09/05 03:27:08  ken * Applied freeswan-alg-0.8.0-BASE-klips.diff * * Revision 1.1.1.1  2002/09/05 03:13:17  ken * 1.98b * * Revision 1.102  2002/01/29 17:17:56  mcr * 	moved include of ipsec_param.h to after include of linux/kernel.h * 	otherwise, it seems that some option that is set in ipsec_param.h * 	screws up something subtle in the include path to kernel.h, and * 	it complains on the snprintf() prototype. * * Revision 1.101  2002/01/29 04:00:52  mcr * 	more excise of kversions.h header. * * Revision 1.100  2002/01/29 02:13:17  mcr * 	introduction of ipsec_kversion.h means that include of * 	ipsec_param.h must preceed any decisions about what files to * 	include to deal with differences in kernel source. * * Revision 1.99  2002/01/28 21:40:59  mcr * 	should use #if to test boolean option rather than #ifdef. * * Revision 1.98  2002/01/20 20:19:36  mcr * 	renamed option to IP_FRAGMENT_LINEARIZE. * * Revision 1.97  2002/01/12 02:55:36  mcr * 	fix for post-2.4.4 to linearize skb's when ESP packet * 	was assembled from fragments. * * Revision 1.96  2001/11/26 09:23:49  rgb * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes. * * Revision 1.93.2.2  2001/10/22 20:54:07  mcr * 	include des.h, removed phony prototypes and fixed calling * 	conventions to match real prototypes. * * Revision 1.93.2.1  2001/09/25 02:22:22  mcr * 	struct tdb -> struct ipsec_sa. * 	lifetime checks moved to ipsec_life.c * 	some sa(tdb) manipulation functions renamed. * * Revision 1.95  2001/11/06 19:49:07  rgb * Added variable descriptions. * Removed unauthenticated sequence==0 check to prevent DoS. * * Revision 1.94  2001/10/18 04:45:20  rgb * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h, * lib/freeswan.h version macros moved to lib/kversions.h. * Other compiler directive cleanups. * * Revision 1.93  2001/09/07 22:17:24  rgb * Fix for removal of transport layer protocol handler arg in 2.4.4. * Fix to accomodate peer non-conformance to IPCOMP rfc2393. * * Revision 1.92  2001/08/27 19:44:41  rgb * Fix error in comment. * * Revision 1.91  2001/07/20 19:31:48  dhr * [DHR] fix source and destination subnets of policy in diagnostic * * Revision 1.90  2001/07/06 19:51:09  rgb * Added inbound policy checking code for IPIP SAs. * Renamed unused function argument for ease and intuitive naming. * * Revision 1.89  2001/06/22 19:35:23  rgb * Disable ipcomp processing if we are handed a ipcomp packet with no esp * or ah header. * Print protocol if we are handed a non-ipsec packet. * * Revision 1.88  2001/06/20 06:30:47  rgb * Fixed transport mode IPCOMP policy check bug. * * Revision 1.87  2001/06/13 20:58:40  rgb * Added parentheses around assignment used as truth value to silence * compiler. * * Revision 1.86  2001/06/07 22:25:23  rgb * Added a source address policy check for tunnel mode.  It still does * not check client addresses and masks. * Only decapsulate IPIP if it is expected. * * Revision 1.85  2001/05/30 08:14:02  rgb * Removed vestiges of esp-null transforms. * * Revision 1.84  2001/05/27 06:12:11  rgb * Added structures for pid, packet count and last access time to eroute. * Added packet count to beginning of /proc/net/ipsec_eroute. * * Revision 1.83  2001/05/04 16:45:47  rgb * Remove unneeded code.  ipp is not used after this point. * * Revision 1.82  2001/05/04 16:36:00  rgb * Fix skb_cow() call for 2.4.4. (SS) * * Revision 1.81  2001/05/02 14:46:53  rgb * Fix typo for compiler directive to pull IPH back. * * Revision 1.80  2001/04/30 19:46:34  rgb * Update for 2.4.4.  We now receive the skb with skb->data pointing to * h.raw. * * Revision 1.79  2001/04/23 15:01:15  rgb * Added spin_lock() check to prevent double-locking for multiple * transforms and hence kernel lock-ups with SMP kernels. * Minor spin_unlock() adjustments to unlock before non-dependant prints * and IPSEC device stats updates. * * Revision 1.78  2001/04/21 23:04:24  rgb * Check if soft expire has already been sent before sending another to * prevent ACQUIRE flooding. * * Revision 1.77  2001/03/16 07:35:20  rgb * Ditch extra #if 1 around now permanent policy checking code. * * Revision 1.76  2001/02/27 22:24:54  rgb * Re-formatting debug output (line-splitting, joining, 1arg/line). * Check for satoa() return codes. * * Revision 1.75  2001/02/19 22:28:30  rgb * Minor change to virtual device discovery code to assert which I/F has * been found. * * Revision 1.74  2000/11/25 03:50:36  rgb * Oops fix by minor re-arrangement of code to avoid accessing a freed tdb. * * Revision 1.73  2000/11/09 20:52:15  rgb * More spinlock shuffling, locking earlier and unlocking later in rcv to * include ipcomp and prevent races, renaming some tdb variables that got * forgotten, moving some unlocks to include tdbs and adding a missing * unlock.  Thanks to Svenning for some of these. * * Revision 1.72  2000/11/09 20:11:22  rgb * Minor shuffles to fix non-standard kernel config option selection. * * Revision 1.71  2000/11/06 04:36:18  rgb * Ditched spin_lock_irqsave in favour of spin_lock. * Minor initial protocol check rewrite. * Clean up debug printing. * Clean up tdb handling on ipcomp. * Fixed transport mode null pointer de-reference without ipcomp. * Add Svenning's adaptive content compression. * Disabled registration of ipcomp handler. * * Revision 1.70  2000/10/30 23:41:43  henry * Hans-Joerg Hoexer's null-pointer fix * * Revision 1.69  2000/10/10 18:54:16  rgb * Added a fix for incoming policy check with ipcomp enabled but * uncompressible. * * Revision 1.68  2000/09/22 17:53:12  rgb * Fixed ipcomp tdb pointers update for policy checking. * * Revision 1.67  2000/09/21 03:40:58  rgb * Added more debugging to try and track down the cpi outward copy problem. * * Revision 1.66  2000/09/20 04:00:10  rgb * Changed static functions to DEBUG_NO_STATIC to reveal function names for * debugging oopsen. * * Revision 1.65  2000/09/19 07:07:16  rgb * Added debugging to inbound policy check for ipcomp. * Added missing spin_unlocks (thanks Svenning!). * Fixed misplaced tdbnext pointers causing mismatched ipip policy check. * Protect ipcomp policy check following ipip decap with sysctl switch. * * Revision 1.64  2000/09/18 21:27:29  rgb * 2.0 fixes. * * Revision 1.63  2000/09/18 02:35:50  rgb * Added policy checking to ipcomp and re-enabled policy checking by * default. * Optimised satoa calls. * * Revision 1.62  2000/09/17 21:02:32  rgb * Clean up debugging, removing slow timestamp debug code. * * Revision 1.61  2000/09/16 01:07:55  rgb * Fixed erroneous ref from struct ipcomp to struct ipcomphdr. * * Revision 1.60  2000/09/15 11:37:01  rgb * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk> * IPCOMP zlib deflate code. * * Revision 1.59  2000/09/15 04:56:20  rgb * Remove redundant satoa() call, reformat commen

⌨️ 快捷键说明

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