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

📄 spi.c

📁 FREESWAN VPN源代码包
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * All-in-one program to set Security Association parameters * Copyright (C) 1996  John Ioannidis. * Copyright (C) 1997, 1998, 1999, 2000, 2001  Richard Guy Briggs. *  * 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. */char spi_c_version[] = "RCSID $Id: spi.c,v 1.5 2003/05/26 12:47:12 ken Exp $";#include <asm/types.h>#include <sys/types.h>#include <sys/ioctl.h>/* #include <linux/netdevice.h> */#include <net/if.h>/* #include <linux/types.h> */ /* new */#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <errno.h>/* #include <sys/socket.h> */#include <netinet/in.h>#include <arpa/inet.h>/* #include <linux/ip.h> */#include <netdb.h>#include <unistd.h>#include <getopt.h>#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <freeswan.h>#include <signal.h>#include <sys/socket.h>#include <pfkeyv2.h>#include <pfkey.h>#include "ipsec_xform.h"/* 	 * 	Manual conn support for ipsec_alg (modular algos). * 	Rather ugly to include from pluto dir but avoids * 	code duplication. */#ifndef NO_KERNEL_ALG#include "../../pluto/alg_info.h"#include "../../pluto/constants.h"struct connection;#include "../../pluto/kernel_alg.h"#endif /* NO_KERNEL_ALG */char *program_name;char me[] = "ipsec spi";int debug = 0;char *command;extern char *optarg;extern int optind, opterr, optopt;char scratch[2];char *iv = NULL, *enckey = NULL, *authkey = NULL;size_t ivlen = 0, enckeylen = 0, authkeylen = 0;ip_address edst, dst, src;int address_family = 0;unsigned char proto = 0;int alg = 0;#ifndef NO_KERNEL_ALG/*  * 	Manual connection support for modular algos (ipsec_alg) --Juanjo. */#define XF_OTHER_ALG (XF_CLR-1)	/* define magic XF_ symbol for alg_info's */#include <assert.h>const char *alg_string = NULL;	/* algorithm string */struct alg_info_esp *alg_info = NULL;	/* algorithm info got from string */struct esp_info *esp_info = NULL;	/* esp info from 1st (only) element */const char *alg_err;		/* auxiliar for parsing errors */int proc_read_ok = 0;		/* /proc/net/pf_key_support read ok */#endif /* NO_KERNEL_ALG */int replay_window = 0;char sa[SATOT_BUF];extern unsigned int pfkey_lib_debug; /* used by libfreeswan/pfkey_v2_build */int pfkey_sock;fd_set pfkey_socks;uint32_t pfkey_seq = 0;enum life_severity {	life_soft = 0,	life_hard = 1,	life_maxsever = 2};enum life_type {	life_alloc = 0,	life_bytes = 1,	life_addtime = 2,	life_usetime = 3,	life_packets = 4,	life_maxtype = 5};#define streql(_a,_b) (!strcmp((_a),(_b)))static const char *usage_string = "\Usage:\n\	in the following, <SA> is: --af <inet | inet6> --edst <dstaddr> --spi <spi> --proto <proto>\n\                               OR: --said <proto><.|:><spi>@<dstaddr>\n\	                  <life> is: --life <soft|hard>-<allocations|bytes|addtime|usetime|packets>=<value>[,...]\n\spi --clear\n\spi --help\n\spi --version\n\spi\n\spi --del <SA>\n\spi --ip4 <SA> --src <encap-src> --dst <encap-dst>\n\spi --ip6 <SA> --src <encap-src> --dst <encap-dst>\n\spi --ah <algo> <SA> [<life> ][ --replay_window <replay_window> ] --authkey <key>\n\	where <algo> is one of:	hmac-md5-96 | hmac-sha1-96\n\spi --esp <algo> <SA> [<life> ][ --replay_window <replay-window> ] --enckey <ekey> --authkey <akey>\n\	where <algo> is one of:	3des-md5-96 | 3des-sha1-96\n\spi --esp <algo> <SA> [<life> ][ --replay_window <replay-window> ] --enckey <ekey>\n\	where <algo> is:	3des\n\spi --comp <algo> <SA>\n\	where <algo> is:	deflate\n\[ --debug ] is optional to any spi command.\n\[ --label <label> ] is optional to any spi command.";static voidusage(char *s, FILE *f){	/* s argument is actually ignored, at present */	fprintf(f, "%s:%s", s, usage_string);	exit(-1);}#ifdef SIGS_FROM_BELOWstatic voidpfkey_sig_handler(int x){	int len;	int error = 0;#if 0	int i;	fd_set l_pfkey_socks;#endif	unsigned char buffer[PFKEYv2_MAX_MSGSIZE];	struct sadb_ext *extensions_parse[SADB_EXT_MAX + 1];	struct sadb_msg *pfkey_msg;		/* if(signal.type == SIGIO) } */	while(1) {#if 0		memcpy(&l_pfkey_socks, &pfkey_socks, sizeof(pfkey_socks));				fprintf(stdout, "%s:pfkey_sig_handler: "			"entering select for pfkey_sock=%d.\n",			program_name,			pfkey_sock);		if((i = select(pfkey_sock + 1, &l_pfkey_socks, NULL, NULL, NULL)) < 0) {			fprintf(stderr, "%s:system error:pfkey_sig_handler: "				"select returned errno:%d.\n",				program_name,				errno);			break;		}		if(!i) {			fprintf(stdout, "%s:pfkey_sig_handler: "				"select returned %d.\n",				program_name,				i);			return;		}		ret = recvmsg(pfkey_sock,			      /* struct msghdr * */msg,			      /* unsigned int */flags);		if(ret == -1) {			fprintf(stderr, "%s: pfkey recvmsg failed.\n",				program_name);			switch(errno) {			case EBADF:			case ENOTCONN:			case ENOTSOCK:			case EWOULDBLOCK:			case EINTR:			case EFAULT:				fprintf(stderr, "system error:%d\n",					errno);				exit(1);			default:				fprintf(stderr, "unknown error:%d\n",					errno);				exit(1);			}		}#endif		fprintf(stdout, "%s:pfkey_sig_handler: "			"entering read for pfkey_sock=%d.\n",			program_name,			pfkey_sock);		if((len = read(pfkey_sock, buffer, sizeof(buffer))) < 0) {			fprintf(stderr, "%s: pfkey read failed.\n",				program_name);			switch(errno) {			case EBADF:			case ENOTCONN:			case ENOTSOCK:			case EWOULDBLOCK:			case EINTR:			case EFAULT:				fprintf(stderr, "%s:system error:%d\n",					program_name,					errno);				exit(1);			default:				fprintf(stderr, "%s:unknown error:%d\n",					program_name,					errno);				exit(1);			}			break;		}				if(len < sizeof(struct sadb_msg)) {			fprintf(stderr, "%s:system error:pfkey_sig_handler: "				"read returned only %d octets of a minimum of %d octets for the message header.\n",				program_name,				len, sizeof(struct sadb_msg));			break;		}				fprintf(stdout, "%s:pfkey_sig_handler: "			"read %d octets from pfkey_sock=%d.\n",			program_name,			len, pfkey_sock);				pfkey_msg = (struct sadb_msg*)buffer;		if(pfkey_msg->sadb_msg_version != PF_KEY_V2) {			fprintf(stderr, "system error:pfkey_sig_handler: not PF_KEY_V2 msg.\n");			break;		}				if(len != pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN) {			fprintf(stderr, "system error:pfkey_sig_handler: bogus msg len of %d, not %d byte aligned.\n",				len, IPSEC_PFKEYv2_ALIGN);			break;		}				/* XXX when this becomes a lib, keying daemons must be able to receive errors */		if(pfkey_msg->sadb_msg_errno) {			fprintf(stderr, "system error:pfkey_sig_handler: errno set to %d.\n",				pfkey_msg->sadb_msg_errno);			break;		}				/* check PID */		if(pfkey_msg->sadb_msg_pid != getpid()) {			fprintf(stderr, "system error:pfkey_sig_handler: pid (%d) does not equal originating process pid (%d).\n",				pfkey_msg->sadb_msg_pid, getpid());			break;		}				if(pfkey_msg->sadb_msg_seq != pfkey_seq) {			fprintf(stderr, "system error:pfkey_sig_handler: seq (%d) does not equal original message seq (%d).\n",				pfkey_msg->sadb_msg_seq, pfkey_seq);			break;		}				if(pfkey_msg->sadb_msg_reserved) {			fprintf(stderr, "system error:pfkey_sig_handler: reserved field must be zero, set to %d.\n",				pfkey_msg->sadb_msg_reserved);			break;		}				if((pfkey_msg->sadb_msg_type > SADB_MAX) || (!pfkey_msg->sadb_msg_type)){			fprintf(stderr, "system error:pfkey_sig_handler: msg type too large or small:%d.\n",				pfkey_msg->sadb_msg_type);			break;		}				if((error = pfkey_msg_parse(pfkey_msg, NULL, extensions_parse, EXT_BITS_OUT))) {			fprintf(stderr, "system error:pfkey_sig_handler: pfkey_msg_parse returns %d.\n",				error);		} else {			fprintf(stdout, "%s:pfkey_sig_handler: return (msg would normally be sent for parsing).\n",				program_name);		}		break;	}	return;}#endif /* SIGS_FROM_BELOW */intparse_life_options(uint32_t life[life_maxsever][life_maxtype],		   char *life_opt[life_maxsever][life_maxtype],		   char *optarg){	char *optargp = optarg;	char *endptr;		do {		int life_severity, life_type;		char *optargt = optargp;				if(strncmp(optargp, "soft", sizeof("soft")-1) == 0) {			life_severity = life_soft;			optargp += sizeof("soft")-1;		} else if(strncmp(optargp, "hard", sizeof("hard")-1) == 0) {			life_severity = life_hard;			optargp += sizeof("hard")-1;		} else {			fprintf(stderr, "%s: missing lifetime severity in %s, optargt=%p, optargp=%p, sizeof(\"soft\")=%d\n",				program_name, optargt, optargt, optargp, sizeof("soft"));			usage(program_name, stderr);			return(1);		}		if(debug) {			fprintf(stdout, "%s: debug: life_severity=%d, optargt=%p=\"%s\", optargp=%p=\"%s\", sizeof(\"soft\")=%d\n",				program_name, life_severity, optargt, optargt, optargp, optargp, sizeof("soft"));		}		if(*(optargp++) != '-') {			fprintf(stderr, "%s: expected '-' after severity of lifetime parameter to --life option.\n",				program_name);			usage(program_name, stderr);			return(1);		}		if(debug) {			fprintf(stdout, "%s: debug: optargt=%p=\"%s\", optargp=%p=\"%s\", strlen(optargt)=%d, strlen(optargp)=%d, strncmp(optargp, \"addtime\", sizeof(\"addtime\")-1)=%d\n",				program_name, optargt, optargt, optargp, optargp, strlen(optargt), strlen(optargp), strncmp(optargp, "addtime", sizeof("addtime")-1));		}		if(strncmp(optargp, "allocations", sizeof("allocations")-1) == 0) {			life_type = life_alloc;			optargp += sizeof("allocations")-1;		} else if(strncmp(optargp, "bytes", sizeof("bytes")-1) == 0) {			life_type = life_bytes;			optargp += sizeof("bytes")-1;		} else if(strncmp(optargp, "addtime", sizeof("addtime")-1) == 0) {			life_type = life_addtime;			optargp += sizeof("addtime")-1;		} else if(strncmp(optargp, "usetime", sizeof("usetime")-1) == 0) {			life_type = life_usetime;			optargp += sizeof("usetime")-1;		} else if(strncmp(optargp, "packets", sizeof("packets")-1) == 0) {			life_type = life_packets;			optargp += sizeof("packets")-1;		} else {			fprintf(stderr, "%s: missing lifetime type after '-' in %s\n",				program_name, optargt);			usage(program_name, stderr);			return(1);		}		if(debug) {			fprintf(stdout, "%s: debug: life_type=%d\n",				program_name, life_type);		}		if(life_opt[life_severity][life_type] != NULL) {			fprintf(stderr, "%s: Error, lifetime parameter redefined:%s, already defined as:%p\n",				program_name, optargt, life_opt[life_severity][life_type]);			return(1);		}		if(*(optargp++) != '=') {			fprintf(stderr, "%s: expected '=' after type of lifetime parameter to --life option.\n",				program_name);			usage(program_name, stderr);			return(1);		}		if(debug) {			fprintf(stdout, "%s: debug: optargt=%p, optargt+strlen(optargt)=%p, optargp=%p, strlen(optargp)=%d\n",				program_name, optargt, optargt+strlen(optargt), optargp, strlen(optargp));		}		if(strlen(optargp) == 0) {			fprintf(stderr, "%s: expected value after '=' in --life option. optargt=%p, optargt+strlen(optargt)=%p, optargp=%p\n",				program_name, optargt, optargt+strlen(optargt), optargp);			usage(program_name, stderr);			return(1);		}		life[life_severity][life_type] = strtoul(optargp, &endptr, 0);		if(!((endptr == optargp + strlen(optargp)) || (endptr == optargp + strcspn(optargp, ", ")))) {			fprintf(stderr, "%s: Invalid character='%c' at offset %d in lifetime option parameter: '%s', parameter string is %d characters long, %d valid value characters found.\n",				program_name,				*endptr, endptr - optarg, optarg, strlen(optarg), strcspn(optargp, ", ") - 1);			return(1);		}		life_opt[life_severity][life_type] = optargt;		if(debug) {			fprintf(stdout, "%s lifetime %s set to %d.\n",				program_name, optargt, life[life_severity][life_type]);		}		optargp=endptr+1;	} while(*endptr==',' || isspace(*endptr));		return(0);}intpfkey_register(uint8_t satype) {	/* for registering SA types that can be negotiated */	int error = 0;	struct sadb_ext *extensions[SADB_EXT_MAX + 1];	struct sadb_msg *pfkey_msg;	pfkey_extensions_init(extensions);	if((error = pfkey_msg_hdr_build(&extensions[0],					SADB_REGISTER,					satype,					0,

⌨️ 快捷键说明

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