941 lines
30 KiB
C
941 lines
30 KiB
C
/*
|
|
******************************************************************************
|
|
* INTERPEAK SOURCE FILE
|
|
*
|
|
* Document no: @(#) $Name: VXWORKS_ITER40_RCPL8_2024030303 $ $RCSfile: ipike_cmd.c,v $ $Revision: 1.43.10.4 $
|
|
* $Source: /home/interpeak/CVSRoot/ipike/src/ipike_cmd.c,v $
|
|
* $Author: jren $
|
|
* $State: Exp $ $Locker: $
|
|
*
|
|
* INTERPEAK_COPYRIGHT_STRING
|
|
* Design and implementation by Kenneth Jonsson <kenneth@interpeak.se>
|
|
******************************************************************************
|
|
*/
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 1 DESCRIPTION
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 2 CONFIGURATION
|
|
****************************************************************************
|
|
*/
|
|
|
|
#include "ipike_config.h"
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 3 INCLUDE FILES
|
|
****************************************************************************
|
|
*/
|
|
|
|
#include "ipike_h.h"
|
|
#include "ipike.h"
|
|
#include "ipike_configuration_api.h"
|
|
#include "ipike_ipc.h"
|
|
#include "ipike_map.h"
|
|
|
|
#include <ipcom_type.h>
|
|
#include <ipcom_cstyle.h>
|
|
#include <ipcom_os.h>
|
|
#include <ipcom_getopt.h>
|
|
|
|
|
|
#ifdef IPIKE_USE_EVENT_LOG_TEST
|
|
#include <ipike_event_receiver.h>
|
|
#endif
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 4 DEFINES
|
|
****************************************************************************
|
|
*/
|
|
|
|
#define IPIKE_CMD_PRINTF(statement, silent) do { if (!silent) statement; } while (0)
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 5 TYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
typedef struct Ipike_cmd_param_struct
|
|
{
|
|
Ip_bool silent;
|
|
}
|
|
Ipike_cmd_param;
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 6 EXTERN PROTOTYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 7 LOCAL PROTOTYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 8 DATA
|
|
****************************************************************************
|
|
*/
|
|
#ifdef IPIKE_USE_EVENT_LOG_TEST
|
|
IP_STATIC Ip_u16 event_log_stats[IPIKE_EVENT_MAX] = {0};
|
|
#endif
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 9 STATIC FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
#ifdef IPIKE_USE_PERF_EVAL
|
|
|
|
IP_STATIC void
|
|
ipike_print_perf_stats(Ip_u32 uptime, Ip_u32 exchanges, Ip_u32 entries, Ipike_perf *stat_ptr);
|
|
#endif
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_create_socket
|
|
*===========================================================================
|
|
* Description: Create cmd socket.
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_STATIC Ip_fd
|
|
ipike_cmd_create_socket(void)
|
|
{
|
|
return ipike_dispath_open_cmd_socket(IP_FALSE);
|
|
}
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_readn
|
|
*===========================================================================
|
|
* Description: Read 'len' bytes from a blocking socket.
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_STATIC Ip_ssize_t
|
|
ipike_cmd_readn(Ip_fd fd, void *buf, Ip_size_t len)
|
|
{
|
|
Ip_ssize_t ret;
|
|
Ip_size_t read = 0;
|
|
|
|
while (read < len)
|
|
{
|
|
ret = ipcom_socketread(fd, (Ip_u8 *)buf + read, len - read);
|
|
if (ret <= 0)
|
|
/* Socket error or closed. */
|
|
return -1;
|
|
read += (Ip_size_t)ret;
|
|
}
|
|
ip_assert(read == len);
|
|
return (Ip_ssize_t)len;
|
|
}
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_send
|
|
*===========================================================================
|
|
* Description: Send a command to IPIKE
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_send(Ip_u8 cmd, Ip_u8* buf, Ip_u32 len, Ip_fd* fd)
|
|
{
|
|
Ip_err err = 0;
|
|
Ip_u32 n = 0;
|
|
|
|
if (ipcom_ipd_send(IPIKE_IPD_NAME, IPCOM_IPD_MSGTYPE_PING) != IPCOM_SUCCESS)
|
|
{
|
|
ipcom_printf("The IPIKE daemon is not started" IP_LF);
|
|
return IP_ERRNO_ESRCH;
|
|
}
|
|
|
|
if (IP_INVALID_SOCKET == (*fd = ipike_cmd_create_socket()))
|
|
{
|
|
ipcom_printf("Failed to create shell command socket"IP_LF);
|
|
err = ipcom_errno;
|
|
}
|
|
else
|
|
{
|
|
n = ip_htonl(len);
|
|
if (ipcom_socketwrite(*fd, &cmd, sizeof(cmd)) < 0)
|
|
{
|
|
ipcom_printf("Failed to write command"IP_LF);
|
|
err = ipcom_errno;
|
|
}
|
|
else if (ipcom_socketwrite(*fd, &n, sizeof(n)) < 0)
|
|
{
|
|
ipcom_printf("Failed to write command data length"IP_LF);
|
|
err = ipcom_errno;
|
|
}
|
|
else if ((len > 0) &&
|
|
(ipcom_socketwrite(*fd, buf, len) < 0))
|
|
{
|
|
ipcom_printf("Failed to write command data length"IP_LF);
|
|
err = ipcom_errno;
|
|
}
|
|
if (IPCOM_SUCCESS != err)
|
|
{
|
|
ipcom_socketclose(*fd);
|
|
*fd = IP_INVALID_SOCKET;
|
|
}
|
|
}
|
|
|
|
return err;
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_send_string
|
|
*===========================================================================
|
|
* Description: Send a command with a string as only argument
|
|
* Parameters: arg - the argument.
|
|
* Returns:
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_send_string(Ip_u8 type, const char* arg)
|
|
{
|
|
Ip_fd fd = IP_INVALID_SOCKET;
|
|
Ip_u8* buf = IP_NULL;
|
|
Ip_u32 len = 0;
|
|
|
|
buf = (Ip_u8*)arg;
|
|
len = (Ip_u32) ipcom_strlen(arg) + 1;
|
|
|
|
if (ipike_cmd_send(type, buf, len, &fd) != IPCOM_SUCCESS)
|
|
{
|
|
ipcom_printf("Send string command failed"IP_LF);
|
|
}
|
|
else
|
|
{
|
|
buf = IP_NULL;
|
|
if ((ipike_cmd_readn(fd, &len, sizeof(len)) < 0) ||
|
|
((len = ip_ntohl(len)) == 0) ||
|
|
((buf = ipcom_malloc(len)) == IP_NULL) ||
|
|
(ipike_cmd_readn(fd, buf, len) < 0) ||
|
|
(buf[len-1] != 0))
|
|
{
|
|
ipcom_printf("Receive string command failed"IP_LF);
|
|
}
|
|
else
|
|
{
|
|
ipcom_printf("%s"IP_LF, buf);
|
|
}
|
|
ipcom_free(buf);
|
|
ipcom_socketclose(fd);
|
|
}
|
|
|
|
return IPIKE_SUCCESS;
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_send_string
|
|
*===========================================================================
|
|
* Description: Send a command with a string as only argument
|
|
* Parameters: arg1 - the first argument.
|
|
* arg2 - the second argument.
|
|
* Returns:
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_send_string2(Ip_u8 type, const char* arg1, const char* arg2)
|
|
{
|
|
int err = IPIKE_SUCCESS;
|
|
char* arg = IP_NULL;
|
|
|
|
arg = ipcom_malloc(ipcom_strlen(arg1) + ipcom_strlen(arg2) + 2);
|
|
if (IP_NULL == arg)
|
|
{
|
|
IP_PANIC();
|
|
err = IPIKE_OUT_OF_MEMORY;
|
|
}
|
|
else
|
|
{
|
|
ipcom_sprintf(arg, "%s %s", arg1, arg2);
|
|
err = ipike_cmd_send_string(type, arg);
|
|
ipcom_free(arg);
|
|
}
|
|
|
|
return err;
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_list
|
|
*===========================================================================
|
|
* Description: Lists all IKE SA to the console.
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_list(void)
|
|
{
|
|
Ip_err err = 0;
|
|
int i;
|
|
char str_addr[IPIKE_ADDRSTRLEN];
|
|
int cmd_sock = IP_INVALID_SOCKET;
|
|
int num_sas;
|
|
struct Ipike_ipd_sa_info sa;
|
|
const char* str = IP_NULL;
|
|
|
|
if (ipike_cmd_send(IPIKE_CMD_LIST, IP_NULL, 0, &cmd_sock) != IPCOM_SUCCESS)
|
|
return IP_ERRNO_ESRCH;
|
|
|
|
if (ipike_cmd_readn(cmd_sock, &num_sas, sizeof(num_sas)) < 0)
|
|
{
|
|
ipcom_printf("failed to recv shell command socket: %s"IP_LF,
|
|
ipcom_strerror(ipcom_errno));
|
|
err = ipcom_errno;
|
|
goto cleanup;
|
|
}
|
|
|
|
for (i = 0; i < num_sas; i++)
|
|
{
|
|
if (ipike_cmd_readn(cmd_sock, &sa, sizeof(sa)) < 0)
|
|
{
|
|
ipcom_printf("failed to recv shell command socket: %s"IP_LF,
|
|
ipcom_strerror(ipcom_errno));
|
|
err = ipcom_errno;
|
|
goto cleanup;
|
|
}
|
|
|
|
ipcom_printf("[%d] initiator cookie: %s ",
|
|
i, ipike_cookie_to_string(sa.initiator_cookie));
|
|
ipcom_printf("responder cookie: %s" IP_LF,
|
|
ipike_cookie_to_string(sa.responder_cookie));
|
|
#ifdef _WRS_CONFIG_TIME_T_LONG_LONG
|
|
ipcom_printf("\tcreated %lld seconds ago as %s, ref.count: %d, state: ",
|
|
ipike_time() - sa.created,
|
|
sa.is_initiator ? "initiator" : "responder",
|
|
sa.ref_count);
|
|
#else
|
|
ipcom_printf("\tcreated %ld seconds ago as %s, ref.count: %d, state: ",
|
|
ipike_time() - sa.created,
|
|
sa.is_initiator ? "initiator" : "responder",
|
|
sa.ref_count);
|
|
#endif
|
|
switch (sa.state)
|
|
{
|
|
case IPIKE_SA_CONSTRUCTING:
|
|
ipcom_printf("CONSTRUCTING");
|
|
break;
|
|
case IPIKE_SA_ACTIVE:
|
|
ipcom_printf("ACTIVE");
|
|
break;
|
|
case IPIKE_SA_DYING:
|
|
ipcom_printf("DYING");
|
|
break;
|
|
default:
|
|
ipcom_printf("* UNKNOWN STATE *");
|
|
break;
|
|
}
|
|
ipcom_printf(IP_LF "\tpeer addr: %s",
|
|
ipcom_inet_ntop(sa.peer_addr.sa.sa_family,
|
|
ipike_addr_get_ptr(&sa.peer_addr),
|
|
str_addr,
|
|
sizeof(str_addr)));
|
|
ipcom_printf(", local addr: %s" IP_LF,
|
|
ipcom_inet_ntop(sa.local_addr.sa.sa_family,
|
|
ipike_addr_get_ptr(&sa.local_addr),
|
|
str_addr,
|
|
sizeof(str_addr)));
|
|
ipcom_printf("\tsent bytes: %d, received bytes: %d" IP_LF,
|
|
sa.bytes_sent, sa.bytes_received);
|
|
|
|
if (sa.state > IPIKE_SA_CONSTRUCTING)
|
|
{
|
|
#if defined(IPIKE_USE_VERSION2)
|
|
if (sa.version >= 0x20)
|
|
str = ipike_attribute_encryption_alg_to_string_v2(sa.encryption_algorithm);
|
|
#endif
|
|
#if defined(IPIKE_USE_VERSION1)
|
|
if (sa.version < 0x20)
|
|
str = ipike_attribute_encryption_alg_to_string(sa.encryption_algorithm);
|
|
#endif
|
|
ipcom_printf("\tencryption algorithm: %s", str);
|
|
|
|
#if defined(IPIKE_USE_VERSION2)
|
|
if (sa.version >= 0x20)
|
|
str = ipike_attribute_prf_alg_to_string_v2(sa.hash_algorithm);
|
|
else
|
|
#endif
|
|
str = ipike_attribute_hash_alg_to_string(sa.hash_algorithm);
|
|
|
|
ipcom_printf(", hash algorithm: %s" IP_LF, str);
|
|
if (sa.version >= 0x20)
|
|
str = ipike_attribute_auth_method_to_string_v2(sa.authentication_method);
|
|
else
|
|
str = ipike_attribute_auth_method_to_string(sa.authentication_method);
|
|
ipcom_printf("\tauthentication method: %s" IP_LF, str);
|
|
str = ipike_attribute_life_type_to_string(sa.life_type);
|
|
ipcom_printf("\tlife type: %s", str);
|
|
|
|
/* Make some special treatment when printing u64 */
|
|
if ( sizeof(long) == sizeof(sa.life_duration) )
|
|
ipcom_printf(", life duration: %lu" IP_LF, sa.life_duration);
|
|
#ifdef IPCOM_USE_LONGLONG_TYPE
|
|
else
|
|
ipcom_printf(", life duration: %llu" IP_LF, sa.life_duration);
|
|
#else
|
|
else /* Just print the lower 32bit if all other fails */
|
|
ipcom_printf(", life duration: %u" IP_LF, IP_U64_GETLO(sa.life_duration));
|
|
#endif
|
|
|
|
ipcom_printf("\tdh group: %s",
|
|
ipike_attribute_dh_group_to_string(sa.dh_group));
|
|
ipcom_printf(", generator: %d" IP_LF, sa.dh_generator);
|
|
ipcom_printf("\tprotocol version %d.%d" IP_LF,
|
|
((sa.version >> 4) & 0x0f),
|
|
(sa.version & 0x0f));
|
|
}
|
|
}
|
|
|
|
cleanup:
|
|
if (cmd_sock != IP_INVALID_SOCKET)
|
|
ipcom_socketclose(cmd_sock);
|
|
return err;
|
|
}
|
|
|
|
#ifdef IPIKE_USE_PERF_EVAL
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_new_stat
|
|
*===========================================================================
|
|
* Description: Print usage statistics to console.
|
|
* Parameters:
|
|
* Returns:
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_stat(void)
|
|
{
|
|
Ip_fd fd;
|
|
Ipike_perf stats;
|
|
Ip_u32 uptime;
|
|
Ip_u32 exchanges;
|
|
Ip_u32 entries;
|
|
|
|
if (ipike_cmd_send(IPIKE_CMD_STAT, IP_NULL, 0, &fd) != IPCOM_SUCCESS)
|
|
{
|
|
ipcom_printf("Send stat command failed"IP_LF);
|
|
}
|
|
else
|
|
{
|
|
if ((ipike_cmd_readn(fd, &uptime, sizeof(uptime)) < 0)
|
|
|| (ipike_cmd_readn(fd, &exchanges, sizeof(exchanges)) < 0)
|
|
|| (ipike_cmd_readn(fd, &entries, sizeof(entries)) < 0)
|
|
|| (ipike_cmd_readn(fd, &stats, sizeof(Ipike_perf)) < 0))
|
|
{
|
|
ipcom_printf("Receive stat command failed"IP_LF);
|
|
}
|
|
else
|
|
{
|
|
ipike_print_perf_stats(uptime, exchanges, entries, &stats);
|
|
}
|
|
ipcom_socketclose(fd);
|
|
}
|
|
|
|
return IPIKE_SUCCESS;
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
*
|
|
*===========================================================================
|
|
* Description: Prints performance stats to stdout
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_STATIC void
|
|
ipike_print_perf_stats(Ip_u32 uptime, Ip_u32 exchanges, Ip_u32 entries, Ipike_perf *stat_ptr)
|
|
{
|
|
int i,j;
|
|
Ipike_exchange_stats *ptr;
|
|
Ip_double rate = 0.0f;
|
|
|
|
char *str_a[2][5] = { { "ID PROT","AGGRESSIVE","QUICK MODE","TRANSACTION","INFORMATIONAL"},
|
|
{ "SA INIT/AUTH", "CHILD SA","INFORMATIONAL","NA","NA"} };
|
|
ipcom_printf("---------------------------------------------------------------------------------------------\n");
|
|
|
|
for (i = 0; i < 2; i++)
|
|
{
|
|
ipcom_printf("IKE version %d"IP_LF, i+1);
|
|
for (j = 0; j < 5; j++)
|
|
{
|
|
if (i > 0 && j > 2)
|
|
continue;
|
|
ptr = (i==0) ? &stat_ptr->v1_stat[j] : &stat_ptr->v2_stat[j];
|
|
|
|
if (ptr->ok_count > 0)
|
|
rate = (( (Ip_double) ptr->ok_time.tv_sec * 1000.0) + ( (Ip_double) ptr->ok_time.tv_usec / 1000.0)) / ptr->ok_count;
|
|
else
|
|
rate = 0.0f;
|
|
#ifdef _WRS_CONFIG_TIME_T_LONG_LONG
|
|
ipcom_printf("%15s -----------> OK secs: %5lld usecs: %10d count: %5d rate(ms): %3.2f\n"
|
|
,str_a[i][j],ptr->ok_time.tv_sec, ptr->ok_time.tv_usec, ptr->ok_count, rate);
|
|
#else
|
|
ipcom_printf("%15s -----------> OK secs: %5d usecs: %10d count: %5d rate(ms): %3.2f\n"
|
|
,str_a[i][j],ptr->ok_time.tv_sec, ptr->ok_time.tv_usec, ptr->ok_count, rate);
|
|
#endif
|
|
|
|
if (ptr->nok_count > 0)
|
|
rate = (( (Ip_double) ptr->nok_time.tv_sec * 1000.0) + ( (Ip_double) ptr->nok_time.tv_usec / 1000.0)) / ptr->nok_count;
|
|
else
|
|
rate = 0.0f;
|
|
#ifdef _WRS_CONFIG_TIME_T_LONG_LONG
|
|
ipcom_printf("%15s -----------> NOK secs: %5lld usecs: %10d count: %5d rate(ms): %3.2f\n"
|
|
, str_a[i][j],ptr->nok_time.tv_sec, ptr->nok_time.tv_usec, ptr->nok_count, rate);
|
|
#else
|
|
ipcom_printf("%15s -----------> NOK secs: %5d usecs: %10d count: %5d rate(ms): %3.2f\n"
|
|
, str_a[i][j],ptr->nok_time.tv_sec, ptr->nok_time.tv_usec, ptr->nok_count, rate);
|
|
#endif
|
|
}
|
|
|
|
ptr = (i==0) ? &stat_ptr->v1_rekey[IPIKE_PHASE2] : &stat_ptr->v2_rekey[IPIKE_PHASE2];
|
|
|
|
if (ptr->ok_count > 0)
|
|
rate = (( (Ip_double) ptr->ok_time.tv_sec * 1000.0) + ( (Ip_double)ptr->ok_time.tv_usec / 1000.0)) / ptr->ok_count;
|
|
else
|
|
rate = 0.0f;
|
|
|
|
#ifdef _WRS_CONFIG_TIME_T_LONG_LONG
|
|
ipcom_printf("%15s -----------> OK secs: %5lld usecs: %10d count: %5d rate(ms): %3.2f\n"
|
|
,"REKEY",ptr->ok_time.tv_sec, ptr->ok_time.tv_usec, ptr->ok_count, rate);
|
|
#else
|
|
ipcom_printf("%15s -----------> OK secs: %5d usecs: %10d count: %5d rate(ms): %3.2f\n"
|
|
,"REKEY",ptr->ok_time.tv_sec, ptr->ok_time.tv_usec, ptr->ok_count, rate);
|
|
#endif
|
|
|
|
if (ptr->nok_count > 0)
|
|
rate = (( (Ip_double) ptr->nok_time.tv_sec * 1000.0) + ( (Ip_double)ptr->nok_time.tv_usec / 1000.0)) / ptr->nok_count;
|
|
else
|
|
rate = 0.0f;
|
|
|
|
#ifdef _WRS_CONFIG_TIME_T_LONG_LONG
|
|
ipcom_printf("%15s -----------> NOK secs: %5lld usecs: %10d count: %5d rate(ms): %3.2f\n"
|
|
,"REKEY",ptr->nok_time.tv_sec, ptr->nok_time.tv_usec, ptr->nok_count, rate);
|
|
#else
|
|
ipcom_printf("%15s -----------> NOK secs: %5d usecs: %10d count: %5d rate(ms): %3.2f\n"
|
|
,"REKEY",ptr->nok_time.tv_sec, ptr->nok_time.tv_usec, ptr->nok_count, rate);
|
|
#endif
|
|
ipcom_printf("---------------------------------------------------------------------------------------------\n");
|
|
}
|
|
|
|
ipcom_printf("Uptime %d seconds"IP_LF, uptime);
|
|
ipcom_printf("Number of active exchanges: %d"IP_LF, exchanges);
|
|
ipcom_printf("Number of active IKE SA: %d"IP_LF, entries);
|
|
|
|
ipcom_printf("---------------------------------------------------------------------------------------------\n");
|
|
}
|
|
#endif
|
|
|
|
|
|
#ifdef IPIKE_USE_EVENT_LOG_TEST
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_event_cb
|
|
*===========================================================================
|
|
* Description: Log event callback
|
|
* Parameters: event - Event
|
|
* Returns: 0 - Continue to to receive events
|
|
* < 0 - Stop receiving events (unregister)
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_event_cb(
|
|
Ipike_event *event)
|
|
{
|
|
ip_assert(event->type < IPIKE_EVENT_MAX);
|
|
|
|
event_log_stats[event->type]++;
|
|
|
|
/* Keep us registered */
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_event_log_start
|
|
*===========================================================================
|
|
* Description: Starts the log event receiver and reset log statistics.
|
|
* Parameters:
|
|
* Returns:
|
|
*/
|
|
IP_STATIC void
|
|
ipike_cmd_event_log_start(void)
|
|
{
|
|
/* This will spawn a "receiver task" which will listen for events
|
|
* triggered inside IPsec and IKE module and then dispatch them
|
|
* to the 'event_cb' function. The receiver task will continuously try to
|
|
* keep its channel open to the IKE daemon (ipiked) until it is successful
|
|
* or until ipike_event_receiver_destroy() is called.
|
|
*/
|
|
ipcom_memset(event_log_stats, 0 , sizeof(event_log_stats));
|
|
|
|
ipike_event_receiver_init(ipike_cmd_event_cb);
|
|
}
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_event_log_stop
|
|
*===========================================================================
|
|
* Description: Stops the log event receiver.
|
|
* Parameters:
|
|
* Returns:
|
|
*/
|
|
IP_STATIC void
|
|
ipike_cmd_event_log_stop(void)
|
|
{
|
|
/* This will signal the "receiver task" to stop receiving events.
|
|
* The function will block until the "receiver task" has
|
|
* terminated properly */
|
|
ipike_event_receiver_destroy();
|
|
}
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_event_log_stat
|
|
*===========================================================================
|
|
* Description: Prints out log events statistics
|
|
* Parameters:
|
|
* Returns:
|
|
*/
|
|
IP_STATIC void
|
|
ipike_cmd_event_log_stat(void)
|
|
{
|
|
int i;
|
|
|
|
/* Print out the log event counters */
|
|
for (i = 0; i < IPIKE_EVENT_MAX; i++)
|
|
{
|
|
ipcom_printf("%-32s %d"IP_LF,
|
|
ipike_event_type_str((Ip_u16)i), event_log_stats[i]);
|
|
}
|
|
}
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_log
|
|
*===========================================================================
|
|
* Description: Controls an IKE/IPsec event receiver
|
|
* Parameters:
|
|
* Returns:
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_log(const char *cmd)
|
|
{
|
|
if (cmd == IP_NULL)
|
|
return IP_ERRNO_EINVAL;
|
|
|
|
if ( ipcom_strcmp("start", cmd) == 0 )
|
|
ipike_cmd_event_log_start();
|
|
else if ( ipcom_strcmp("stop", cmd) == 0 )
|
|
ipike_cmd_event_log_stop();
|
|
else if ( ipcom_strcmp("stat", cmd) == 0 )
|
|
ipike_cmd_event_log_stat();
|
|
else
|
|
return IP_ERRNO_EINVAL;
|
|
|
|
return IPIKE_SUCCESS;
|
|
}
|
|
#endif /* IPIKE_USE_EVENT_LOG_TEST */
|
|
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_config_dump
|
|
*===========================================================================
|
|
* Description: Dump current configuration to specified file
|
|
* Parameters: filename - file to dump cinfiguration.
|
|
* Returns:
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_config_dump(const char* filename)
|
|
{
|
|
return ipike_cmd_send_string(IPIKE_CMD_CONFIG_DUMP, filename);
|
|
}
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_config_load
|
|
*===========================================================================
|
|
* Description: Load configuration from the specified file
|
|
* Parameters: filename - file to load configuration from.
|
|
* Returns:
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_config_load(const char* filename)
|
|
{
|
|
return ipike_cmd_send_string(IPIKE_CMD_CONFIG_LOAD, filename);
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_config_add
|
|
*===========================================================================
|
|
* Description: Add/Update configuration from the specified content
|
|
* Parameters: content - content to load configuration from.
|
|
* Returns:
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_config_add(const char* content)
|
|
{
|
|
return ipike_cmd_send_string(IPIKE_CMD_CONFIG_ADD, content);
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_config_delete
|
|
*===========================================================================
|
|
* Description: Delete configuration from the specified content
|
|
* Parameters: content - content to load configuration from.
|
|
* Returns:
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_config_delete(const char* content)
|
|
{
|
|
return ipike_cmd_send_string(IPIKE_CMD_CONFIG_DELETE, content);
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_mobike
|
|
*===========================================================================
|
|
* Description: Update ip address for an IKE SA (MOBIKE)
|
|
* Parameters: old_addr - the old ip address
|
|
* new_addr - the new ip address
|
|
* Returns:
|
|
*/
|
|
IP_STATIC int
|
|
ipike_cmd_mobike(const char* old_addr, const char* new_addr)
|
|
{
|
|
return ipike_cmd_send_string2(IPIKE_CMD_MOBIKE, old_addr, new_addr);
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd_print_usage
|
|
*===========================================================================
|
|
* Description:
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_STATIC void
|
|
ipike_cmd_print_usage(void)
|
|
{
|
|
ipcom_printf("NAME"IP_LF);
|
|
ipcom_printf(" " IPIKE_CMD_NAME " - control function for IPIKE"IP_LF);
|
|
ipcom_printf(IP_LF);
|
|
ipcom_printf("VERSION"IP_LF);
|
|
ipcom_printf(" %s"IP_LF, ipike_version_number());
|
|
ipcom_printf(IP_LF);
|
|
ipcom_printf("SYNOPSIS"IP_LF);
|
|
ipcom_printf(" " IPIKE_CMD_NAME " list"IP_LF);
|
|
#ifdef IPIKE_USE_PERF_EVAL
|
|
ipcom_printf(" " IPIKE_CMD_NAME " stat"IP_LF);
|
|
#endif
|
|
ipcom_printf(" " IPIKE_CMD_NAME " config dump|load <file name>"IP_LF);
|
|
ipcom_printf(" " IPIKE_CMD_NAME " config add|delete \"<content>\""IP_LF);
|
|
ipcom_printf(" " IPIKE_CMD_NAME " mobike <old address> <new address>"IP_LF);
|
|
#ifdef IPIKE_USE_EVENT_LOG_TEST
|
|
ipcom_printf(" " IPIKE_CMD_NAME " log start|stop|stat"IP_LF);
|
|
#endif /* IPIKE_USE_EVENT_LOG_TEST */
|
|
ipcom_printf(IP_LF);
|
|
ipcom_printf("DESCRIPTION"IP_LF);
|
|
ipcom_printf(" The " IPIKE_CMD_NAME " command controls the Internet Key Exchange (IKE) daemon."IP_LF);
|
|
ipcom_printf(IP_LF);
|
|
ipcom_printf(" Commands:"IP_LF);
|
|
ipcom_printf(" list Prints all IKE SA(s)."IP_LF);
|
|
#ifdef IPIKE_USE_PERF_EVAL
|
|
ipcom_printf(" stat Prints statistics."IP_LF);
|
|
#endif
|
|
ipcom_printf(" config Load/dump configuration from/to file."IP_LF);
|
|
ipcom_printf(" config Add/delete configuration with command content."IP_LF);
|
|
ipcom_printf(" mobike Update local address for active IKE SA."IP_LF);
|
|
|
|
#ifdef IPIKE_USE_EVENT_LOG_TEST
|
|
ipcom_printf(" log Start/stop IKE/IPsec event logging."IP_LF);
|
|
#endif /* IPIKE_USE_EVENT_LOG_TEST */
|
|
}
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 10 GLOBAL FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 11 PUBLIC FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipike_cmd
|
|
*===========================================================================
|
|
* Description: CLI control command for IPIKE.
|
|
* Parameters:
|
|
* Returns:
|
|
*/
|
|
IP_GLOBAL int
|
|
ipike_cmd(int argc, char** argv)
|
|
{
|
|
Ipcom_getopt opt;
|
|
Ipike_cmd_param param;
|
|
const char* cmd;
|
|
int tkn;
|
|
|
|
ipcom_memset(¶m, 0, sizeof(param));
|
|
|
|
ipcom_getopt_clear_r(&opt);
|
|
while ((tkn = ipcom_getopt_r(argc, argv, "f:hp:s", &opt)) != -1)
|
|
{
|
|
switch(tkn)
|
|
{
|
|
case 'h':
|
|
ipike_cmd_print_usage();
|
|
return 0;
|
|
case 's':
|
|
param.silent = IP_TRUE;
|
|
break;
|
|
default:
|
|
IPIKE_CMD_PRINTF(ipcom_printf("-%c is an unknown option" IP_LF, (char) tkn),
|
|
param.silent);
|
|
return IP_ERRNO_EINVAL;
|
|
}
|
|
}
|
|
|
|
if ((argc - opt.optind) == 0)
|
|
{
|
|
IPIKE_CMD_PRINTF(ipcom_printf("No command specified" IP_LF), param.silent);
|
|
ipike_cmd_print_usage();
|
|
return IP_ERRNO_EINVAL;
|
|
}
|
|
|
|
if (IPIKE_SUCCESS != ipike_map_startup())
|
|
{
|
|
IPIKE_CMD_PRINTF(ipcom_printf("Failed to initialize string map" IP_LF), param.silent);
|
|
return IP_ERRNO_ENOMEM;
|
|
}
|
|
|
|
cmd = argv[opt.optind];
|
|
if (ipcom_strcmp("list", cmd) == 0)
|
|
{
|
|
if (argc - opt.optind > 1)
|
|
IPIKE_CMD_PRINTF(ipcom_printf("Only one command can be specified" IP_LF), param.silent);
|
|
if (ipcom_strcmp("list", cmd) == 0)
|
|
return ipike_cmd_list();
|
|
}
|
|
else if (ipcom_strcmp("stat", cmd) == 0)
|
|
{
|
|
if (argc - opt.optind > 1)
|
|
IPIKE_CMD_PRINTF(ipcom_printf("Only one command can be specified" IP_LF), param.silent);
|
|
#ifdef IPIKE_USE_PERF_EVAL
|
|
return ipike_cmd_stat();
|
|
|
|
#else
|
|
IPIKE_CMD_PRINTF(ipcom_printf("'%s' is not a valid config command" IP_LF, cmd), param.silent);
|
|
#endif
|
|
}
|
|
else if (ipcom_strcmp("config", cmd) == 0)
|
|
{
|
|
if ((argc - opt.optind) != 3)
|
|
{
|
|
IPIKE_CMD_PRINTF(ipcom_printf("Must specify dump/load followed by file name" IP_LF), param.silent);
|
|
}
|
|
else
|
|
{
|
|
cmd = argv[opt.optind + 1];
|
|
if (ipcom_strcmp("dump", cmd) == 0)
|
|
{
|
|
return ipike_cmd_config_dump(argv[opt.optind + 2]);
|
|
}
|
|
else if (ipcom_strcmp("load", cmd) == 0)
|
|
{
|
|
return ipike_cmd_config_load(argv[opt.optind + 2]);
|
|
}
|
|
else if (ipcom_strcmp("add", cmd) == 0)
|
|
{
|
|
return ipike_cmd_config_add(argv[opt.optind + 2]);
|
|
}
|
|
else if (ipcom_strcmp("delete", cmd) == 0)
|
|
{
|
|
return ipike_cmd_config_delete(argv[opt.optind + 2]);
|
|
}
|
|
else
|
|
{
|
|
IPIKE_CMD_PRINTF(ipcom_printf("'%s' is not a valid config command" IP_LF, cmd), param.silent);
|
|
}
|
|
}
|
|
}
|
|
else if (ipcom_strcmp("mobike", cmd) == 0)
|
|
{
|
|
if ((argc - opt.optind) == 3)
|
|
{
|
|
return ipike_cmd_mobike(argv[opt.optind + 1], argv[opt.optind + 2]);
|
|
}
|
|
else
|
|
{
|
|
IPIKE_CMD_PRINTF(ipcom_printf("Must specify old address followed by new address" IP_LF), param.silent);
|
|
}
|
|
}
|
|
#ifdef IPIKE_USE_EVENT_LOG_TEST
|
|
else if (ipcom_strcmp("log", cmd) == 0)
|
|
{
|
|
int err = IP_ERRNO_EINVAL;
|
|
if ( (argc - opt.optind) == 2)
|
|
{
|
|
cmd = argv[opt.optind + 1];
|
|
err = ipike_cmd_log(cmd);
|
|
}
|
|
|
|
if ( err != IPIKE_SUCCESS)
|
|
{
|
|
IPIKE_CMD_PRINTF(ipcom_printf("Must specify 'start', 'stop' or stat" IP_LF), param.silent);
|
|
}
|
|
return err;
|
|
}
|
|
#endif /* IPIKE_USE_EVENT_LOG_TEST */
|
|
else
|
|
{
|
|
IPIKE_CMD_PRINTF(ipcom_printf("'%s' is not a valid command" IP_LF, cmd), param.silent);
|
|
}
|
|
|
|
return IP_ERRNO_EINVAL;
|
|
}
|
|
|
|
/*
|
|
****************************************************************************
|
|
* END OF FILE
|
|
****************************************************************************
|
|
*/
|
|
|