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

📄 pfkey_v2_parser.c

📁 This a good VPN source
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * @(#) RFC2367 PF_KEYv2 Key management API message parser * Copyright (C) 1999, 2000, 2001  Richard Guy Briggs <rgb@freeswan.org> *  * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * for more details. * * RCSID $Id: pfkey_v2_parser.c,v 1.130 2004/09/08 17:21:36 ken Exp $ *//* *		Template from klips/net/ipsec/ipsec/ipsec_netlink.c. */char pfkey_v2_parser_c_version[] = "$Id: pfkey_v2_parser.c,v 1.130 2004/09/08 17:21:36 ken Exp $";#include <linux/config.h>#include <linux/version.h>#include <linux/kernel.h> /* printk() */#include "openswan/ipsec_param.h"#ifdef MALLOC_SLAB# include <linux/slab.h> /* kmalloc() */#else /* MALLOC_SLAB */# include <linux/malloc.h> /* kmalloc() */#endif /* MALLOC_SLAB */#include <linux/errno.h>  /* error codes */#include <linux/types.h>  /* size_t */#include <linux/interrupt.h> /* mark_bh */#include <linux/netdevice.h>   /* struct device, and other headers */#include <linux/etherdevice.h> /* eth_type_trans */#include <linux/ip.h>          /* struct iphdr */#include <linux/skbuff.h>#include <openswan.h>#include <crypto/des.h>#ifdef SPINLOCK# ifdef SPINLOCK_23#  include <linux/spinlock.h> /* *lock* */# else /* SPINLOCK_23 */#  include <asm/spinlock.h> /* *lock* */# endif /* SPINLOCK_23 */#endif /* SPINLOCK */#ifdef NET_21# include <asm/uaccess.h># include <linux/in6.h># define ip_chk_addr inet_addr_type# define IS_MYADDR RTN_LOCAL#endif#include <asm/checksum.h>#include <net/ip.h>#ifdef NETLINK_SOCK# include <linux/netlink.h>#else# include <net/netlink.h>#endif#include <linux/random.h>	/* get_random_bytes() */#include "openswan/radij.h"#include "openswan/ipsec_encap.h"#include "openswan/ipsec_sa.h"#include "openswan/ipsec_radij.h"#include "openswan/ipsec_xform.h"#include "openswan/ipsec_ah.h"#include "openswan/ipsec_esp.h"#include "openswan/ipsec_tunnel.h"#include "openswan/ipsec_rcv.h"#include "openswan/ipcomp.h"#include <pfkeyv2.h>#include <pfkey.h>#include "openswan/ipsec_proto.h"#include "openswan/ipsec_alg.h"#include "openswan/ipsec_kern24.h"#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)struct sklist_t {	struct socket *sk;	struct sklist_t* next;} pfkey_sklist_head, *pfkey_sklist, *pfkey_sklist_prev;__u32 pfkey_msg_seq = 0;#if 0#define DUMP_SAID dump_said(&extr->ips->ips_said, __LINE__)#define DUMP_SAID2 dump_said(&extr.ips->ips_said, __LINE__)static void dump_said(ip_said *s, int line){ 	char msa[SATOT_BUF];	size_t msa_len;		msa_len = satot(s, 0, msa, sizeof(msa));		printk("line: %d msa: %s\n", line, msa);}#endifintpfkey_alloc_eroute(struct eroute** eroute){	int error = 0;	if(*eroute) {		KLIPS_PRINT(debug_pfkey,			    "klips_debug:pfkey_alloc_eroute: "			    "eroute struct already allocated\n");		SENDERR(EEXIST);	}	if((*eroute = kmalloc(sizeof(**eroute), GFP_ATOMIC) ) == NULL) {		KLIPS_PRINT(debug_pfkey,			    "klips_debug:pfkey_alloc_eroute: "			    "memory allocation error\n");		SENDERR(ENOMEM);	}	KLIPS_PRINT(debug_pfkey,		    "klips_debug:pfkey_alloc_eroute: "		    "allocating %lu bytes for an eroute at 0p%p\n",		    (unsigned long) sizeof(**eroute), *eroute);	memset((caddr_t)*eroute, 0, sizeof(**eroute));	(*eroute)->er_eaddr.sen_len =		(*eroute)->er_emask.sen_len = sizeof(struct sockaddr_encap);	(*eroute)->er_eaddr.sen_family =		(*eroute)->er_emask.sen_family = AF_ENCAP;	(*eroute)->er_eaddr.sen_type = SENT_IP4;	(*eroute)->er_emask.sen_type = 255;	(*eroute)->er_pid = 0;	(*eroute)->er_count = 0;	(*eroute)->er_lasttime = jiffies/HZ; errlab:	return(error);}DEBUG_NO_STATIC intpfkey_x_protocol_process(struct sadb_ext *pfkey_ext,			 struct pfkey_extracted_data *extr){	int error = 0;	struct sadb_protocol * p = (struct sadb_protocol *)pfkey_ext;	KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_protocol_process: %p\n", extr);	if (extr == 0) {		KLIPS_PRINT(debug_pfkey,                         "klips_debug:pfkey_x_protocol_process:"			    "extr is NULL, fatal\n");		SENDERR(EINVAL);	}	if (extr->eroute == 0) {		KLIPS_PRINT(debug_pfkey,                        "klips_debug:pfkey_x_protocol_process:"			    "extr->eroute is NULL, fatal\n");		SENDERR(EINVAL);	}	extr->eroute->er_eaddr.sen_proto = p->sadb_protocol_proto;	extr->eroute->er_emask.sen_proto = p->sadb_protocol_proto ? ~0:0;	KLIPS_PRINT(debug_pfkey,		    "klips_debug:pfkey_x_protocol_process: protocol = %d.\n",		    p->sadb_protocol_proto); errlab:	return error;}DEBUG_NO_STATIC intpfkey_ipsec_sa_init(struct ipsec_sa *ipsp, struct sadb_ext **extensions){        int i;        int error = 0;        char sa[SATOT_BUF];	size_t sa_len;	char ipaddr_txt[ADDRTOA_BUF];	char ipaddr2_txt[ADDRTOA_BUF];#if defined (CONFIG_KLIPS_AUTH_HMAC_MD5) || defined (CONFIG_KLIPS_AUTH_HMAC_SHA1)	unsigned char kb[AHMD596_BLKLEN];#endif#ifdef CONFIG_KLIPS_ALG	struct ipsec_alg_enc *ixt_e = NULL;	struct ipsec_alg_auth *ixt_a = NULL;#endif /* CONFIG_KLIPS_ALG */	if(ipsp == NULL) {		KLIPS_PRINT(debug_pfkey,			    "klips_debug:pfkey_ipsec_sa_init: "			    "ipsp is NULL, fatal\n");		SENDERR(EINVAL);	}	sa_len = satot(&ipsp->ips_said, 0, sa, sizeof(sa));        KLIPS_PRINT(debug_pfkey,		    "klips_debug:pfkey_ipsec_sa_init: "		    "(pfkey defined) called for SA:%s\n",		    sa_len ? sa : " (error)");	KLIPS_PRINT(debug_pfkey,		    "klips_debug:pfkey_ipsec_sa_init: "		    "calling init routine of %s%s%s\n",		    IPS_XFORM_NAME(ipsp));		switch(ipsp->ips_said.proto) {		#ifdef CONFIG_KLIPS_IPIP	case IPPROTO_IPIP: {		addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_s))->sin_addr,			0,			ipaddr_txt, sizeof(ipaddr_txt));		addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_d))->sin_addr,			0,			ipaddr2_txt, sizeof(ipaddr_txt));		KLIPS_PRINT(debug_pfkey,			    "klips_debug:pfkey_ipsec_sa_init: "			    "(pfkey defined) IPIP ipsec_sa set for %s->%s.\n",			    ipaddr_txt,			    ipaddr2_txt);	}	break;#endif /* !CONFIG_KLIPS_IPIP */#ifdef CONFIG_KLIPS_AH	case IPPROTO_AH:		switch(ipsp->ips_authalg) {# ifdef CONFIG_KLIPS_AUTH_HMAC_MD5		case AH_MD5: {			unsigned char *akp;			unsigned int aks;			MD5_CTX *ictx;			MD5_CTX *octx;						if(ipsp->ips_key_bits_a != (AHMD596_KLEN * 8)) {				KLIPS_PRINT(debug_pfkey,					    "klips_debug:pfkey_ipsec_sa_init: "					    "incorrect key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,					    ipsp->ips_key_bits_a, AHMD596_KLEN * 8);				SENDERR(EINVAL);			}			#  if KLIPS_DIVULGE_HMAC_KEY			KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,				    "klips_debug:pfkey_ipsec_sa_init: "				    "hmac md5-96 key is 0x%08x %08x %08x %08x\n",				    ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),				    ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),				    ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),				    ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));#  endif /* KLIPS_DIVULGE_HMAC_KEY */						ipsp->ips_auth_bits = AHMD596_ALEN * 8;						/* save the pointer to the key material */			akp = ipsp->ips_key_a;			aks = ipsp->ips_key_a_size;						KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,			           "klips_debug:pfkey_ipsec_sa_init: "			           "allocating %lu bytes for md5_ctx.\n",			           (unsigned long) sizeof(struct md5_ctx));			if((ipsp->ips_key_a = (caddr_t)			    kmalloc(sizeof(struct md5_ctx), GFP_ATOMIC)) == NULL) {				ipsp->ips_key_a = akp;				SENDERR(ENOMEM);			}			ipsp->ips_key_a_size = sizeof(struct md5_ctx);			for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {				kb[i] = akp[i] ^ HMAC_IPAD;			}			for (; i < AHMD596_BLKLEN; i++) {				kb[i] = HMAC_IPAD;			}			ictx = &(((struct md5_ctx*)(ipsp->ips_key_a))->ictx);			osMD5Init(ictx);			osMD5Update(ictx, kb, AHMD596_BLKLEN);			for (i = 0; i < AHMD596_BLKLEN; i++) {				kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);			}			octx = &(((struct md5_ctx*)(ipsp->ips_key_a))->octx);			osMD5Init(octx);			osMD5Update(octx, kb, AHMD596_BLKLEN);			#  if KLIPS_DIVULGE_HMAC_KEY			KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,				    "klips_debug:pfkey_ipsec_sa_init: "				    "MD5 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",				    ((__u32*)ictx)[0],				    ((__u32*)ictx)[1],				    ((__u32*)ictx)[2],				    ((__u32*)ictx)[3],				    ((__u32*)octx)[0],				    ((__u32*)octx)[1],				    ((__u32*)octx)[2],				    ((__u32*)octx)[3] );#  endif /* KLIPS_DIVULGE_HMAC_KEY */						/* zero key buffer -- paranoid */			memset(akp, 0, aks);			kfree(akp);		}		break;# endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */# ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1		case AH_SHA: {			unsigned char *akp;			unsigned int aks;			SHA1_CTX *ictx;			SHA1_CTX *octx;						if(ipsp->ips_key_bits_a != (AHSHA196_KLEN * 8)) {				KLIPS_PRINT(debug_pfkey,					    "klips_debug:pfkey_ipsec_sa_init: "					    "incorrect key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,					    ipsp->ips_key_bits_a, AHSHA196_KLEN * 8);				SENDERR(EINVAL);			}			#  if KLIPS_DIVULGE_HMAC_KEY			KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,				    "klips_debug:pfkey_ipsec_sa_init: "				    "hmac sha1-96 key is 0x%08x %08x %08x %08x\n",				    ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),				    ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),				    ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),				    ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));#  endif /* KLIPS_DIVULGE_HMAC_KEY */						ipsp->ips_auth_bits = AHSHA196_ALEN * 8;						/* save the pointer to the key material */			akp = ipsp->ips_key_a;			aks = ipsp->ips_key_a_size;						KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,			            "klips_debug:pfkey_ipsec_sa_init: "			            "allocating %lu bytes for sha1_ctx.\n",			            (unsigned long) sizeof(struct sha1_ctx));			if((ipsp->ips_key_a = (caddr_t)			    kmalloc(sizeof(struct sha1_ctx), GFP_ATOMIC)) == NULL) {				ipsp->ips_key_a = akp;				SENDERR(ENOMEM);			}			ipsp->ips_key_a_size = sizeof(struct sha1_ctx);			for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {				kb[i] = akp[i] ^ HMAC_IPAD;			}			for (; i < AHMD596_BLKLEN; i++) {				kb[i] = HMAC_IPAD;			}			ictx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->ictx);			SHA1Init(ictx);			SHA1Update(ictx, kb, AHSHA196_BLKLEN);			for (i = 0; i < AHSHA196_BLKLEN; i++) {				kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);			}			octx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->octx);			SHA1Init(octx);			SHA1Update(octx, kb, AHSHA196_BLKLEN);			#  if KLIPS_DIVULGE_HMAC_KEY			KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,				    "klips_debug:pfkey_ipsec_sa_init: "				    "SHA1 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n", 				    ((__u32*)ictx)[0],				    ((__u32*)ictx)[1],				    ((__u32*)ictx)[2],				    ((__u32*)ictx)[3],				    ((__u32*)octx)[0],				    ((__u32*)octx)[1],				    ((__u32*)octx)[2],				    ((__u32*)octx)[3] );#  endif /* KLIPS_DIVULGE_HMAC_KEY */			/* zero key buffer -- paranoid */			memset(akp, 0, aks);

⌨️ 快捷键说明

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