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

📄 pfkey_v2_build.c

📁 This a good VPN source
💻 C
📖 第 1 页 / 共 3 页
字号:
intpfkey_key_build(struct sadb_ext**	pfkey_ext,		uint16_t		exttype,		uint16_t		key_bits,		char*			key){	int error = 0;	struct sadb_key *pfkey_key = (struct sadb_key *)*pfkey_ext;	DEBUGGING(PF_KEY_DEBUG_BUILD,		"pfkey_key_build:\n");	/* sanity checks... */	if(pfkey_key) {		ERROR("pfkey_key_build: "			"why is pfkey_key already pointing to something?\n");		SENDERR(EINVAL);	}	if(!key_bits) {		ERROR("pfkey_key_build: "			"key_bits is zero, it must be non-zero.\n");		SENDERR(EINVAL);	}	if( !((exttype == SADB_EXT_KEY_AUTH) || (exttype == SADB_EXT_KEY_ENCRYPT))) {		ERROR("pfkey_key_build: "			"unsupported extension type=%d.\n",			exttype);		SENDERR(EINVAL);	}	pfkey_key = (struct sadb_key*)	  MALLOC(sizeof(struct sadb_key) +		 DIVUP(key_bits, 64) * IPSEC_PFKEYv2_ALIGN);	*pfkey_ext = (struct sadb_ext*)pfkey_key;	if(pfkey_key == NULL) {		ERROR("pfkey_key_build: "			"memory allocation failed\n");		SENDERR(ENOMEM);	}	memset(pfkey_key,	       0,	       sizeof(struct sadb_key) +	       DIVUP(key_bits, 64) * IPSEC_PFKEYv2_ALIGN);		pfkey_key->sadb_key_len = DIVUP(sizeof(struct sadb_key) * IPSEC_PFKEYv2_ALIGN +	key_bits,					64);	pfkey_key->sadb_key_exttype = exttype;	pfkey_key->sadb_key_bits = key_bits;	pfkey_key->sadb_key_reserved = 0;	memcpy((char*)pfkey_key + sizeof(struct sadb_key),	       key,	       DIVUP(key_bits, 8));errlab:	return error;}intpfkey_ident_build(struct sadb_ext**	pfkey_ext,		  uint16_t		exttype,		  uint16_t		ident_type,		  uint64_t		ident_id,		  uint8_t               ident_len,		  char*			ident_string){	int error = 0;	struct sadb_ident *pfkey_ident = (struct sadb_ident *)*pfkey_ext;	int data_len = ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);	DEBUGGING(PF_KEY_DEBUG_BUILD,		"pfkey_ident_build:\n");	/* sanity checks... */	if(pfkey_ident) {		ERROR("pfkey_ident_build: "			"why is pfkey_ident already pointing to something?\n");		SENDERR(EINVAL);	}	if( ! ((exttype == SADB_EXT_IDENTITY_SRC) ||	       (exttype == SADB_EXT_IDENTITY_DST))) {		ERROR("pfkey_ident_build: "			"unsupported extension type=%d.\n",			exttype);		SENDERR(EINVAL);	}	if((ident_type == SADB_IDENTTYPE_RESERVED)) {		ERROR("pfkey_ident_build: "			"ident_type must be non-zero.\n");		SENDERR(EINVAL);	}	if(ident_type > SADB_IDENTTYPE_MAX) {		ERROR("pfkey_ident_build: "			"identtype=%d out of range.\n",			ident_type);		SENDERR(EINVAL);	}	if(((ident_type == SADB_IDENTTYPE_PREFIX) ||	    (ident_type == SADB_IDENTTYPE_FQDN)) &&	   !ident_string) {		ERROR("pfkey_ident_build: "			"string required to allocate size of extension.\n");		SENDERR(EINVAL);	}	#if 0	if((ident_type == SADB_IDENTTYPE_USERFQDN) ) {	}#endif	    	pfkey_ident = (struct sadb_ident*)	  MALLOC(ident_len * IPSEC_PFKEYv2_ALIGN);	*pfkey_ext = (struct sadb_ext*)pfkey_ident;	if(pfkey_ident == NULL) {		ERROR("pfkey_ident_build: "			"memory allocation failed\n");		SENDERR(ENOMEM);	}	memset(pfkey_ident, 0, ident_len * IPSEC_PFKEYv2_ALIGN);		pfkey_ident->sadb_ident_len = ident_len;	pfkey_ident->sadb_ident_exttype = exttype;	pfkey_ident->sadb_ident_type = ident_type;	pfkey_ident->sadb_ident_reserved = 0;	pfkey_ident->sadb_ident_id = ident_id;	memcpy((char*)pfkey_ident + sizeof(struct sadb_ident),	       ident_string,	       data_len);errlab:	return error;}intpfkey_sens_build(struct sadb_ext**	pfkey_ext,		 uint32_t		dpd,		 uint8_t		sens_level,		 uint8_t		sens_len,		 uint64_t*		sens_bitmap,		 uint8_t		integ_level,		 uint8_t		integ_len,		 uint64_t*		integ_bitmap){	int error = 0;	struct sadb_sens *pfkey_sens = (struct sadb_sens *)*pfkey_ext;	int i;	uint64_t* bitmap;	DEBUGGING(PF_KEY_DEBUG_BUILD,		"pfkey_sens_build:\n");	/* sanity checks... */	if(pfkey_sens) {		ERROR("pfkey_sens_build: "			"why is pfkey_sens already pointing to something?\n");		SENDERR(EINVAL);	}	DEBUGGING(PF_KEY_DEBUG_BUILD,		"pfkey_sens_build: "		"Sorry, I can't build exttype=%d yet.\n",		(*pfkey_ext)->sadb_ext_type);	SENDERR(EINVAL); /* don't process these yet */	pfkey_sens = (struct sadb_sens*)	  MALLOC(sizeof(struct sadb_sens) +		 (sens_len + integ_len) * sizeof(uint64_t));	*pfkey_ext = (struct sadb_ext*)pfkey_sens;	if(pfkey_sens == NULL) {		ERROR("pfkey_sens_build: "			"memory allocation failed\n");		SENDERR(ENOMEM);	}	memset(pfkey_sens,	       0,	       sizeof(struct sadb_sens) +	       (sens_len + integ_len) * sizeof(uint64_t));		pfkey_sens->sadb_sens_len = (sizeof(struct sadb_sens) +		    (sens_len + integ_len) * sizeof(uint64_t)) / IPSEC_PFKEYv2_ALIGN;	pfkey_sens->sadb_sens_exttype = SADB_EXT_SENSITIVITY;	pfkey_sens->sadb_sens_dpd = dpd;	pfkey_sens->sadb_sens_sens_level = sens_level;	pfkey_sens->sadb_sens_sens_len = sens_len;	pfkey_sens->sadb_sens_integ_level = integ_level;	pfkey_sens->sadb_sens_integ_len = integ_len;	pfkey_sens->sadb_sens_reserved = 0;	bitmap = (uint64_t*)((char*)pfkey_ext + sizeof(struct sadb_sens));	for(i = 0; i < sens_len; i++) {		*bitmap = sens_bitmap[i];		bitmap++;	}	for(i = 0; i < integ_len; i++) {		*bitmap = integ_bitmap[i];		bitmap++;	}errlab:	return error;}intpfkey_prop_build(struct sadb_ext**	pfkey_ext,		 uint8_t		replay,		 unsigned int		comb_num,		 struct sadb_comb*	comb){	int error = 0;	int i;	struct sadb_prop *pfkey_prop = (struct sadb_prop *)*pfkey_ext;	struct sadb_comb *combp;	DEBUGGING(PF_KEY_DEBUG_BUILD,		"pfkey_prop_build:\n");	/* sanity checks... */	if(pfkey_prop) {		ERROR("pfkey_prop_build: "			"why is pfkey_prop already pointing to something?\n");		SENDERR(EINVAL);	}	pfkey_prop = (struct sadb_prop*)	  MALLOC(sizeof(struct sadb_prop) +		 comb_num * sizeof(struct sadb_comb));	*pfkey_ext = (struct sadb_ext*)pfkey_prop;	if(pfkey_prop == NULL) {		ERROR("pfkey_prop_build: "			"memory allocation failed\n");		SENDERR(ENOMEM);	}	memset(pfkey_prop,	       0,	       sizeof(struct sadb_prop) +		    comb_num * sizeof(struct sadb_comb));		pfkey_prop->sadb_prop_len = (sizeof(struct sadb_prop) +		    comb_num * sizeof(struct sadb_comb)) / IPSEC_PFKEYv2_ALIGN;	pfkey_prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;	pfkey_prop->sadb_prop_replay = replay;	for(i=0; i<3; i++) {		pfkey_prop->sadb_prop_reserved[i] = 0;	}	combp = (struct sadb_comb*)((char*)*pfkey_ext + sizeof(struct sadb_prop));	for(i = 0; i < comb_num; i++) {		memcpy (combp, &(comb[i]), sizeof(struct sadb_comb));		combp++;	}#if 0  uint8_t sadb_comb_auth;  uint8_t sadb_comb_encrypt;  uint16_t sadb_comb_flags;  uint16_t sadb_comb_auth_minbits;  uint16_t sadb_comb_auth_maxbits;  uint16_t sadb_comb_encrypt_minbits;  uint16_t sadb_comb_encrypt_maxbits;  uint32_t sadb_comb_reserved;  uint32_t sadb_comb_soft_allocations;  uint32_t sadb_comb_hard_allocations;  uint64_t sadb_comb_soft_bytes;  uint64_t sadb_comb_hard_bytes;  uint64_t sadb_comb_soft_addtime;  uint64_t sadb_comb_hard_addtime;  uint64_t sadb_comb_soft_usetime;  uint64_t sadb_comb_hard_usetime;  uint32_t sadb_comb_soft_packets;  uint32_t sadb_comb_hard_packets;#endiferrlab:	return error;}intpfkey_supported_build(struct sadb_ext**	pfkey_ext,		      uint16_t		exttype,		      unsigned int	alg_num,		      struct sadb_alg*	alg){	int error = 0;	unsigned int i;	struct sadb_supported *pfkey_supported = (struct sadb_supported *)*pfkey_ext;	struct sadb_alg *pfkey_alg;	/* sanity checks... */	if(pfkey_supported) {		DEBUGGING(PF_KEY_DEBUG_BUILD,			"pfkey_supported_build: "			"why is pfkey_supported already pointing to something?\n");		SENDERR(EINVAL);	}	if( !((exttype == SADB_EXT_SUPPORTED_AUTH) || (exttype == SADB_EXT_SUPPORTED_ENCRYPT))) {		DEBUGGING(PF_KEY_DEBUG_BUILD,			"pfkey_supported_build: "			"unsupported extension type=%d.\n",			exttype);		SENDERR(EINVAL);	}	pfkey_supported = (struct sadb_supported*)	  MALLOC(sizeof(struct sadb_supported) +		    alg_num *		    sizeof(struct sadb_alg));	*pfkey_ext = (struct sadb_ext*)pfkey_supported;	if(pfkey_supported == NULL) {		DEBUGGING(PF_KEY_DEBUG_BUILD,			"pfkey_supported_build: "			"memory allocation failed\n");		SENDERR(ENOMEM);	}	memset(pfkey_supported,	       0,	       sizeof(struct sadb_supported) +					       alg_num *					       sizeof(struct sadb_alg));		pfkey_supported->sadb_supported_len = (sizeof(struct sadb_supported) +					       alg_num *					       sizeof(struct sadb_alg)) /						IPSEC_PFKEYv2_ALIGN;	pfkey_supported->sadb_supported_exttype = exttype;	pfkey_supported->sadb_supported_reserved = 0;	pfkey_alg = (struct sadb_alg*)((char*)pfkey_supported + sizeof(struct sadb_supported));	for(i = 0; i < alg_num; i++) {		memcpy (pfkey_alg, &(alg[i]), sizeof(struct sadb_alg));		pfkey_alg->sadb_alg_reserved = 0;		pfkey_alg++;	}	#if 0	DEBUGGING(PF_KEY_DEBUG_BUILD,		"pfkey_supported_build: "		"Sorry, I can't build exttype=%d yet.\n",		(*pfkey_ext)->sadb_ext_type);	SENDERR(EINVAL); /* don't process these yet */  uint8_t sadb_alg_id;  uint8_t sadb_alg_ivlen;  uint16_t sadb_alg_minbits;  uint16_t sadb_alg_maxbits;  uint16_t sadb_alg_reserved;#endiferrlab:	return error;}intpfkey_spirange_build(struct sadb_ext**	pfkey_ext,		     uint16_t		exttype,		     uint32_t		min, /* in network order */		     uint32_t		max) /* in network order */{	int error = 0;	struct sadb_spirange *pfkey_spirange = (struct sadb_spirange *)*pfkey_ext;		/* sanity checks... */	if(pfkey_spirange) {		DEBUGGING(PF_KEY_DEBUG_BUILD,			"pfkey_spirange_build: "			"why is pfkey_spirange already pointing to something?\n");		SENDERR(EINVAL);	}	        if(ntohl(max) < ntohl(min)) {		DEBUGGING(PF_KEY_DEBUG_BUILD,			"pfkey_spirange_build: "			"minspi=%08x must be < maxspi=%08x.\n",			ntohl(min),			ntohl(max));                SENDERR(EINVAL);        }		if(ntohl(min) <= 255) {		DEBUGGING(PF_KEY_DEBUG_BUILD,			"pfkey_spirange_build: "			"minspi=%08x must be > 255.\n",			ntohl(min));		SENDERR(EEXIST);	}		pfkey_spirange = (struct sadb_spirange*)	  MALLOC(sizeof(struct sadb_spirange));	*pfkey_ext = (struct sadb_ext*)pfkey_spirange;	if(pfkey_spirange == NULL) {		DEBUGGING(PF_KEY_DEBUG_BUILD,			"pfkey_spirange_build: "			"memory allocation failed\n");		SENDERR(ENOMEM);	}	memset(pfkey_spirange,	       0,	       sizeof(struct sadb_spirange));	        pfkey_spirange->sadb_spirange_len = sizeof(struct sadb_spirange) / IPSEC_PFKEYv2_ALIGN;	pfkey_spirange->sadb_spirange_exttype = SADB_EXT_SPIRANGE;	pfkey_spirange->sadb_spirange_min = min;	pfkey_spirange->sadb_spirange_max = max;	pfkey_spirange->sadb_spirange_reserved = 0; errlab:	return error;}intpfkey_x_kmprivate_build(struct sadb_ext**	pfkey_ext){	int error = 0;	struct sadb_x_kmprivate *pfkey_x_kmprivate = (struct sadb_x_kmprivate *)*pfkey_ext;	/* sanity checks... */	if(pfkey_x_kmprivate) {		DEBUGGING(PF_KEY_DEBUG_BUILD,			"pfkey_x_kmprivate_build: "			"why is pfkey_x_kmprivate already pointing to something?\n");		SENDERR(EINVAL);	}		pfkey_x_kmprivate->sadb_x_kmprivate_reserved = 0;	DEBUGGING(PF_KEY_DEBUG_BUILD,		"pfkey_x_kmprivate_build: "		"Sorry, I can't build exttype=%d yet.\n",		(*pfkey_ext)->sadb_ext_type);	SENDERR(EINVAL); /* don't process these yet */	pfkey_x_kmprivate = (struct sadb_x_kmprivate*)	  MALLOC(sizeof(struct sadb_x_kmprivate));	*pfkey_ext = (struct sadb_ext*)pfkey_x_kmprivate;	if(pfkey_x_kmprivate == NULL) {		DEBUGGING(PF_KEY_DEBUG_BUILD,			"pfkey_x_kmprivate_build: "			"memory allocation failed\n");		SENDERR(ENOMEM);	}	memset(pfkey_x_kmprivate,	       0,	       sizeof(struct sadb_x_kmprivate));	        pfkey_x_kmprivate->sadb_x_kmprivate_len =		sizeof(struct sadb_x_kmprivate) / IPSEC_PFKEYv2_ALIGN;        pfkey_x_kmprivate->sadb_x_kmprivate_exttype = SADB_X_EXT_KMPRIVATE;        pfkey_x_kmprivate->sadb_x_kmprivate_reserved = 0;errlab:	return error;}intpfkey_x_satype_build(struct sadb_ext**	pfkey_ext,		     uint8_t		satype){	int error = 0;	int i;	struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)*pfkey_ext;	DEBUGGING(PF_KEY_DEBUG_BUILD,		"pfkey_x_satype_build:\n");	/* sanity checks... */	if(pfkey_x_satype) {		ERROR("pfkey_x_satype_build: "			"why is pfkey_x_satype already pointing to something?\n");		SENDERR(EINVAL);	}		if(!satype) {		ERROR("pfkey_x_satype_build: "			"SA type not set, must be non-zero.\n");		SENDERR(EINVAL);	}	if(satype > SADB_SATYPE_MAX) {		ERROR("pfkey_x_satype_build: "			"satype %d > max %d\n", 			satype, SADB_SATYPE_MAX);		SENDERR(EINVAL);	}	pfkey_x_satype = (struct sadb_x_satype*)	  MALLOC(sizeof(struct sadb_x_satype));	*pfkey_ext = (struct sadb_ext*)pfkey_x_satype;	if(pfkey_x_satype == NULL) {		ERROR("pfkey_x_satype_build: "			"memory allocation failed\n");		SENDERR(ENOMEM);	}	memset(pfkey_x_satype,	       0,	       sizeof(struct sadb_x_satype));	        pfkey_x_satype->sadb_x_satype_len = sizeof(struct sadb_x_satype) / IPSEC_PFKEYv2_ALIGN;	pfkey_x_satype->sadb_x_satype_exttype = SADB_X_EXT_SATYPE2;	pfkey_x_satype->sadb_x_satype_satype = satype;	for(i=0; i<3; i++) {		pfkey_x_satype->sadb_x_satype_reserved[i] = 0;	}errlab:	return error;}intpfkey_x_debug_build(struct sadb_ext**	pfkey_ext,		    uint32_t            tunnel,		    uint32_t		netlink,		    uint32_t		xform,		    uint32_t		eroute,		    uint32_t		spi,		    uint32_t		radij,		    uint32_t		esp,		    uint32_t		ah,		    uint32_t		rcv,		    uint32_t            pfkey,		    uint32_t            ipcomp,		    uint32_t            verbose)

⌨️ 快捷键说明

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