admin.c revision 6b112236f31ae21c11e416d3dd5d265a5cc5177a
10a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang/*	$NetBSD: admin.c,v 1.17.6.2 2008/06/18 07:30:19 mgrooms Exp $	*/
20a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
30a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang/* Id: admin.c,v 1.25 2006/04/06 14:31:04 manubsd Exp */
40a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
50a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang/*
60a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
70a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * All rights reserved.
86b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang *
90a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * Redistribution and use in source and binary forms, with or without
100a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * modification, are permitted provided that the following conditions
110a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * are met:
120a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * 1. Redistributions of source code must retain the above copyright
130a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang *    notice, this list of conditions and the following disclaimer.
140a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * 2. Redistributions in binary form must reproduce the above copyright
150a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang *    notice, this list of conditions and the following disclaimer in the
160a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang *    documentation and/or other materials provided with the distribution.
170a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * 3. Neither the name of the project nor the names of its contributors
180a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang *    may be used to endorse or promote products derived from this software
190a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang *    without specific prior written permission.
206b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang *
210a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
220a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
230a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
250a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
270a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
280a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
300a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
310a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * SUCH DAMAGE.
320a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang */
330a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
340a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "config.h"
350a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
360a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <sys/types.h>
370a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <sys/param.h>
380a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <sys/socket.h>
390a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#ifndef ANDROID_CHANGES
400a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <sys/signal.h>
410a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#else
426b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#include <cutils/sockets.h>
436b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#include <arpa/inet.h>
446b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#include <ctype.h>
456b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#include "cfparse_proto.h"
460a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#define SIGHUP	1
470a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#endif
480a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <sys/stat.h>
490a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <sys/un.h>
500a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
510a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <net/pfkeyv2.h>
520a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
530a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <netinet/in.h>
540a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include PATH_IPSEC_H
550a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
560a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
570a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <stdlib.h>
580a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <stdio.h>
590a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <string.h>
600a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <errno.h>
610a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <netdb.h>
620a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#ifdef HAVE_UNISTD_H
630a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <unistd.h>
640a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#endif
650a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#ifdef ENABLE_HYBRID
660a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include <resolv.h>
670a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#endif
680a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
690a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "var.h"
700a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "misc.h"
710a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "vmbuf.h"
720a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "plog.h"
730a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "sockmisc.h"
740a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "debug.h"
750a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
760a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "schedule.h"
770a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "localconf.h"
780a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "remoteconf.h"
790a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "grabmyaddr.h"
800a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "isakmp_var.h"
810a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "isakmp.h"
820a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "oakley.h"
830a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "handler.h"
840a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "evt.h"
850a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "pfkey.h"
860a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "ipsec_doi.h"
870a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "admin.h"
880a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "admin_var.h"
890a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "isakmp_inf.h"
900a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#ifdef ENABLE_HYBRID
910a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "isakmp_cfg.h"
920a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#endif
930a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "session.h"
940a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#include "gcmalloc.h"
950a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
960a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#ifdef ENABLE_ADMINPORT
970a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangchar *adminsock_path = ADMINSOCK_PATH;
980a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wanguid_t adminsock_owner = 0;
990a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wanggid_t adminsock_group = 0;
1000a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangmode_t adminsock_mode = 0600;
1010a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1020a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangstatic struct sockaddr_un sunaddr;
1030a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangstatic int admin_process __P((int, char *));
1040a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangstatic int admin_reply __P((int, struct admin_com *, vchar_t *));
1050a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1060a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangint
1070a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangadmin_handler()
1080a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang{
1090a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	int so2;
1100a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	struct sockaddr_storage from;
1110a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	socklen_t fromlen = sizeof(from);
1120a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	struct admin_com com;
1130a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	char *combuf = NULL;
1140a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	int len, error = -1;
1150a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1160a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	so2 = accept(lcconf->sock_admin, (struct sockaddr *)&from, &fromlen);
1170a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (so2 < 0) {
1180a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
1190a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"failed to accept admin command: %s\n",
1200a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			strerror(errno));
1210a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return -1;
1220a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
1230a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1240a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	/* get buffer length */
1250a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	while ((len = recv(so2, (char *)&com, sizeof(com), MSG_PEEK)) < 0) {
1260a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		if (errno == EINTR)
1270a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			continue;
1280a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
1290a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"failed to recv admin command: %s\n",
1300a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			strerror(errno));
1310a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		goto end;
1320a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
1330a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1340a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	/* sanity check */
1350a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (len < sizeof(com)) {
1360a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
1370a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"invalid header length of admin command\n");
1380a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		goto end;
1390a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
1400a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1410a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	/* get buffer to receive */
1420a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if ((combuf = racoon_malloc(com.ac_len)) == 0) {
1430a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
1440a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"failed to alloc buffer for admin command\n");
1450a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		goto end;
1460a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
1470a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1480a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	/* get real data */
1490a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	while ((len = recv(so2, combuf, com.ac_len, 0)) < 0) {
1500a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		if (errno == EINTR)
1510a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			continue;
1520a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
1530a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"failed to recv admin command: %s\n",
1540a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			strerror(errno));
1550a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		goto end;
1560a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
1570a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1580a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (com.ac_cmd == ADMIN_RELOAD_CONF) {
1590a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		/* reload does not work at all! */
1600a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		signal_handler(SIGHUP);
1610a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		goto end;
1620a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
1630a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1640a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	error = admin_process(so2, combuf);
1650a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1660a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang    end:
1670a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	(void)close(so2);
1680a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (combuf)
1690a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		racoon_free(combuf);
1700a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1710a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	return error;
1720a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang}
1730a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1740a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang/*
1750a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang * main child's process.
1760a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang */
1770a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangstatic int
1780a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangadmin_process(so2, combuf)
1790a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	int so2;
1800a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	char *combuf;
1810a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang{
1820a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	struct admin_com *com = (struct admin_com *)combuf;
1830a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	vchar_t *buf = NULL;
1840a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	vchar_t *id = NULL;
1850a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	vchar_t *key = NULL;
1860a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	int idtype = 0;
1870a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	int error = -1;
1880a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1890a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	com->ac_errno = 0;
1900a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1910a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	switch (com->ac_cmd) {
1920a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_RELOAD_CONF:
1930a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		/* don't entered because of proccessing it in other place. */
1940a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL, "should never reach here\n");
1950a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		goto out;
1960a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
1970a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_SHOW_SCHED:
1980a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	{
1990a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		caddr_t p = NULL;
2000a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		int len;
2010a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2020a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		com->ac_errno = -1;
2030a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2040a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		if (sched_dump(&p, &len) == -1)
2050a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			goto out2;
2060a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2070a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		if ((buf = vmalloc(len)) == NULL)
2080a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			goto out2;
2090a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2100a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		memcpy(buf->v, p, len);
2110a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2120a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		com->ac_errno = 0;
2130a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangout2:
2140a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		racoon_free(p);
2150a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		break;
2160a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
2170a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2180a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_SHOW_EVT:
2190a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		/* It's not really an error, don't force racoonctl to quit */
2200a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		if ((buf = evt_dump()) == NULL)
2216b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang			com->ac_errno = 0;
2220a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		break;
2230a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2240a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_SHOW_SA:
2250a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_FLUSH_SA:
2260a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	    {
2270a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		switch (com->ac_proto) {
2280a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		case ADMIN_PROTO_ISAKMP:
2290a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			switch (com->ac_cmd) {
2300a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			case ADMIN_SHOW_SA:
2310a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				buf = dumpph1();
2320a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				if (buf == NULL)
2330a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					com->ac_errno = -1;
2340a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				break;
2350a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			case ADMIN_FLUSH_SA:
2360a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				flushph1();
2370a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				break;
2380a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			}
2390a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			break;
2400a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		case ADMIN_PROTO_IPSEC:
2410a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		case ADMIN_PROTO_AH:
2420a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		case ADMIN_PROTO_ESP:
2430a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			switch (com->ac_cmd) {
2440a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			case ADMIN_SHOW_SA:
2450a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    {
2460a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				u_int p;
2470a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				p = admin2pfkey_proto(com->ac_proto);
2480a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				if (p == -1)
2490a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					goto out;
2500a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				buf = pfkey_dump_sadb(p);
2510a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				if (buf == NULL)
2520a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					com->ac_errno = -1;
2530a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    }
2540a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				break;
2550a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			case ADMIN_FLUSH_SA:
2560a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				pfkey_flush_sadb(com->ac_proto);
2570a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				break;
2580a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			}
2590a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			break;
2600a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2610a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		case ADMIN_PROTO_INTERNAL:
2620a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			switch (com->ac_cmd) {
2630a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			case ADMIN_SHOW_SA:
2640a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				buf = NULL; /*XXX dumpph2(&error);*/
2650a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				if (buf == NULL)
2660a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					com->ac_errno = error;
2670a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				break;
2680a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			case ADMIN_FLUSH_SA:
2690a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				/*XXX flushph2();*/
2700a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				com->ac_errno = 0;
2710a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				break;
2720a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			}
2730a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			break;
2740a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2750a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		default:
2760a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			/* ignore */
2770a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			com->ac_errno = -1;
2780a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		}
2790a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	    }
2800a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		break;
2810a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2820a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_DELETE_SA: {
2830a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		struct ph1handle *iph1;
2840a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		struct sockaddr *dst;
2850a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		struct sockaddr *src;
2860a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		char *loc, *rem;
2870a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2880a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		src = (struct sockaddr *)
2890a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			&((struct admin_com_indexes *)
2900a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    ((caddr_t)com + sizeof(*com)))->src;
2910a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		dst = (struct sockaddr *)
2920a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			&((struct admin_com_indexes *)
2930a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    ((caddr_t)com + sizeof(*com)))->dst;
2940a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
2950a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		loc = racoon_strdup(saddrwop2str(src));
2960a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		rem = racoon_strdup(saddrwop2str(dst));
2970a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		STRDUP_FATAL(loc);
2980a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		STRDUP_FATAL(rem);
2990a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3000a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		if ((iph1 = getph1byaddrwop(src, dst)) == NULL) {
3016b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang			plog(LLV_ERROR, LOCATION, NULL,
3020a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    "phase 1 for %s -> %s not found\n", loc, rem);
3030a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		} else {
3040a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if (iph1->status == PHASE1ST_ESTABLISHED)
3050a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				isakmp_info_send_d1(iph1);
3060a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			purge_remote(iph1);
3070a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		}
3080a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3090a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		racoon_free(loc);
3100a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		racoon_free(rem);
3110a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3120a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		break;
3130a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
3140a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3150a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#ifdef ENABLE_HYBRID
3160a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_LOGOUT_USER: {
3170a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		struct ph1handle *iph1;
3180a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		char *user;
3190a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		int found = 0;
3200a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3210a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		if (com->ac_len > sizeof(com) + LOGINLEN + 1) {
3220a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			plog(LLV_ERROR, LOCATION, NULL,
3230a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    "malformed message (login too long)\n");
3240a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			break;
3250a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		}
3260a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3270a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		user = (char *)(com + 1);
3280a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		found = purgeph1bylogin(user);
3296b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang		plog(LLV_INFO, LOCATION, NULL,
3300a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		    "deleted %d SA for user \"%s\"\n", found, user);
3310a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3320a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		break;
3330a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
3340a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#endif
3350a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3360a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_DELETE_ALL_SA_DST: {
3370a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		struct ph1handle *iph1;
3380a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		struct sockaddr *dst;
3390a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		char *loc, *rem;
3400a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3410a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		dst = (struct sockaddr *)
3420a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			&((struct admin_com_indexes *)
3430a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    ((caddr_t)com + sizeof(*com)))->dst;
3440a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3450a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		rem = racoon_strdup(saddrwop2str(dst));
3460a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		STRDUP_FATAL(rem);
3470a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3486b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang		plog(LLV_INFO, LOCATION, NULL,
3490a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		    "Flushing all SAs for peer %s\n", rem);
3500a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3510a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		while ((iph1 = getph1bydstaddrwop(dst)) != NULL) {
3520a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			loc = racoon_strdup(saddrwop2str(iph1->local));
3530a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			STRDUP_FATAL(loc);
3540a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3550a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if (iph1->status == PHASE1ST_ESTABLISHED)
3560a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				isakmp_info_send_d1(iph1);
3570a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			purge_remote(iph1);
3580a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3590a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			racoon_free(loc);
3600a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		}
3616b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
3620a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		racoon_free(rem);
3630a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3640a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		break;
3650a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
3660a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3670a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_ESTABLISH_SA_PSK: {
3680a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		struct admin_com_psk *acp;
3690a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		char *data;
3700a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3710a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		com->ac_cmd = ADMIN_ESTABLISH_SA;
3720a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3730a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		acp = (struct admin_com_psk *)
3746b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang		    ((char *)com + sizeof(*com) +
3750a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		    sizeof(struct admin_com_indexes));
3760a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3770a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		idtype = acp->id_type;
3780a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3790a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		if ((id = vmalloc(acp->id_len)) == NULL) {
3800a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			plog(LLV_ERROR, LOCATION, NULL,
3816b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang			    "cannot allocate memory: %s\n",
3820a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    strerror(errno));
3830a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			break;
3840a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		}
3850a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		data = (char *)(acp + 1);
3860a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		memcpy(id->v, data, id->l);
3870a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
3880a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		if ((key = vmalloc(acp->key_len)) == NULL) {
3890a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			plog(LLV_ERROR, LOCATION, NULL,
3906b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang			    "cannot allocate memory: %s\n",
3910a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    strerror(errno));
3920a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			vfree(id);
3930a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			id = NULL;
3940a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			break;
3950a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		}
3960a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		data = (char *)(data + acp->id_len);
3970a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		memcpy(key->v, data, key->l);
3980a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
3990a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	/* FALLTHROUGH */
4000a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_ESTABLISH_SA:
4010a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	    {
4020a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		struct sockaddr *dst;
4030a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		struct sockaddr *src;
4040a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		src = (struct sockaddr *)
4050a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			&((struct admin_com_indexes *)
4060a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    ((caddr_t)com + sizeof(*com)))->src;
4070a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		dst = (struct sockaddr *)
4080a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			&((struct admin_com_indexes *)
4090a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			    ((caddr_t)com + sizeof(*com)))->dst;
4100a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4110a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		switch (com->ac_proto) {
4120a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		case ADMIN_PROTO_ISAKMP: {
4130a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			struct remoteconf *rmconf;
4140a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			struct sockaddr *remote = NULL;
4150a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			struct sockaddr *local = NULL;
4160a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			u_int16_t port;
4170a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4180a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			com->ac_errno = -1;
4190a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4200a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			/* search appropreate configuration */
4210a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			rmconf = getrmconf(dst);
4220a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if (rmconf == NULL) {
4230a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				plog(LLV_ERROR, LOCATION, NULL,
4240a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					"no configuration found "
4250a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					"for %s\n", saddrwop2str(dst));
4260a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				goto out1;
4270a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			}
4280a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4290a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			/* get remote IP address and port number. */
4300a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if ((remote = dupsaddr(dst)) == NULL)
4310a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				goto out1;
4320a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4330a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			port = extract_port(rmconf->remote);
4340a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if (set_port(remote, port) == NULL)
4350a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				goto out1;
4360a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4370a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			/* get local address */
4380a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if ((local = dupsaddr(src)) == NULL)
4390a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				goto out1;
4400a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4410a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			port = getmyaddrsport(local);
4420a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if (set_port(local, port) == NULL)
4430a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				goto out1;
4440a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4450a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#ifdef ENABLE_HYBRID
4460a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			/* Set the id and key */
4470a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if (id && key) {
4480a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				if (xauth_rmconf_used(&rmconf->xauth) == -1)
4490a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					goto out1;
4500a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4510a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				if (rmconf->xauth->login != NULL) {
4520a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					vfree(rmconf->xauth->login);
4530a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					rmconf->xauth->login = NULL;
4540a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				}
4550a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				if (rmconf->xauth->pass != NULL) {
4560a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					vfree(rmconf->xauth->pass);
4570a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang					rmconf->xauth->pass = NULL;
4580a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				}
4590a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4600a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				rmconf->xauth->login = id;
4610a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				rmconf->xauth->pass = key;
4620a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			}
4630a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#endif
4646b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
4650a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			plog(LLV_INFO, LOCATION, NULL,
4660a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				"accept a request to establish IKE-SA: "
4670a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				"%s\n", saddrwop2str(remote));
4680a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4690a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			/* begin ident mode */
4700a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if (isakmp_ph1begin_i(rmconf, remote, local) < 0)
4710a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				goto out1;
4720a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4730a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			com->ac_errno = 0;
4740a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangout1:
4750a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if (local != NULL)
4760a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				racoon_free(local);
4770a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			if (remote != NULL)
4780a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang				racoon_free(remote);
4790a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			break;
4800a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		}
4810a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		case ADMIN_PROTO_AH:
4820a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		case ADMIN_PROTO_ESP:
4830a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			break;
4840a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		default:
4850a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			/* ignore */
4860a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			com->ac_errno = -1;
4870a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		}
4880a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	    }
4890a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		break;
4900a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4910a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	default:
4920a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
4930a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"invalid command: %d\n", com->ac_cmd);
4940a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		com->ac_errno = -1;
4950a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
4960a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
4970a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if ((error = admin_reply(so2, com, buf)) != 0)
4980a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		goto out;
4990a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5000a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	error = 0;
5010a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangout:
5020a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (buf != NULL)
5030a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		vfree(buf);
5040a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5050a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	return error;
5060a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang}
5070a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5080a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangstatic int
5090a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangadmin_reply(so, combuf, buf)
5100a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	int so;
5110a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	struct admin_com *combuf;
5120a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	vchar_t *buf;
5130a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang{
5140a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	int tlen;
5150a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	char *retbuf = NULL;
5160a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5170a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (buf != NULL)
5180a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		tlen = sizeof(*combuf) + buf->l;
5190a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	else
5200a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		tlen = sizeof(*combuf);
5210a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5220a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	retbuf = racoon_calloc(1, tlen);
5230a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (retbuf == NULL) {
5240a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
5250a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"failed to allocate admin buffer\n");
5260a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return -1;
5270a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
5280a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5290a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	memcpy(retbuf, combuf, sizeof(*combuf));
5300a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	((struct admin_com *)retbuf)->ac_len = tlen;
5310a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5320a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (buf != NULL)
5330a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		memcpy(retbuf + sizeof(*combuf), buf->v, buf->l);
5340a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5350a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	tlen = send(so, retbuf, tlen, 0);
5360a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	racoon_free(retbuf);
5370a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (tlen < 0) {
5380a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
5390a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"failed to send admin command: %s\n",
5400a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			strerror(errno));
5410a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return -1;
5420a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
5430a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5440a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	return 0;
5450a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang}
5460a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5470a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang/* ADMIN_PROTO -> SADB_SATYPE */
5480a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangint
5490a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangadmin2pfkey_proto(proto)
5500a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	u_int proto;
5510a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang{
5520a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	switch (proto) {
5530a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_PROTO_IPSEC:
5540a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return SADB_SATYPE_UNSPEC;
5550a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_PROTO_AH:
5560a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return SADB_SATYPE_AH;
5570a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	case ADMIN_PROTO_ESP:
5580a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return SADB_SATYPE_ESP;
5590a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	default:
5600a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
5610a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"unsupported proto for admin: %d\n", proto);
5620a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return -1;
5630a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
5640a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	/*NOTREACHED*/
5650a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang}
5660a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5670a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangint
5680a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangadmin_init()
5690a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang{
5700a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (adminsock_path == NULL) {
5710a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		lcconf->sock_admin = -1;
5720a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return 0;
5730a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
5740a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5750a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	memset(&sunaddr, 0, sizeof(sunaddr));
5760a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	sunaddr.sun_family = AF_UNIX;
5770a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	snprintf(sunaddr.sun_path, sizeof(sunaddr.sun_path),
5780a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		"%s", adminsock_path);
5790a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5800a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	lcconf->sock_admin = socket(AF_UNIX, SOCK_STREAM, 0);
5810a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (lcconf->sock_admin == -1) {
5820a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
5830a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"socket: %s\n", strerror(errno));
5840a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return -1;
5850a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
5860a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5870a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	unlink(sunaddr.sun_path);
5880a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (bind(lcconf->sock_admin, (struct sockaddr *)&sunaddr,
5890a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			sizeof(sunaddr)) != 0) {
5900a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
5910a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"bind(sockname:%s): %s\n",
5920a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			sunaddr.sun_path, strerror(errno));
5930a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		(void)close(lcconf->sock_admin);
5940a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return -1;
5950a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
5960a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
5970a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (chown(sunaddr.sun_path, adminsock_owner, adminsock_group) != 0) {
5986b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
5996b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang		    "chown(%s, %d, %d): %s\n",
6006b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang		    sunaddr.sun_path, adminsock_owner,
6010a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		    adminsock_group, strerror(errno));
6020a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		(void)close(lcconf->sock_admin);
6030a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return -1;
6040a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
6050a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
6060a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (chmod(sunaddr.sun_path, adminsock_mode) != 0) {
6076b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
6086b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang		    "chmod(%s, 0%03o): %s\n",
6090a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		    sunaddr.sun_path, adminsock_mode, strerror(errno));
6100a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		(void)close(lcconf->sock_admin);
6110a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return -1;
6120a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
6130a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
6140a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	if (listen(lcconf->sock_admin, 5) != 0) {
6150a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		plog(LLV_ERROR, LOCATION, NULL,
6160a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			"listen(sockname:%s): %s\n",
6170a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang			sunaddr.sun_path, strerror(errno));
6180a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		(void)close(lcconf->sock_admin);
6190a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		return -1;
6200a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	}
6210a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	plog(LLV_DEBUG, LOCATION, NULL,
6220a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang		"open %s as racoon management.\n", sunaddr.sun_path);
6230a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
6240a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	return 0;
6250a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang}
6260a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang
6270a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangint
6280a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wangadmin_close()
6290a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang{
6300a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	close(lcconf->sock_admin);
6310a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang	return 0;
6320a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang}
6330a1907d434839af6a9cb6329bbde60b237bf53dcChung-yih Wang#endif
6346b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
6356b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#ifdef ANDROID_CHANGES
6366b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang// Add the android specific control commands from VPN settings.
6376b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#define CMD_LOAD_CONFIG "LOAD_CONFIG "
6386b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#define CMD_SETKEY "SETKEY "
6396b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#define CMD_SET_CERTS "SET_CERTS "
6406b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#define RACOON_SOCKET "racoon"
6416b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#define PORT_L2TP 1701
6426b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
6436b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang// The following policy is supported for now.
6446b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#define INCOMING_POLICY "in ipsec esp/transport//require"
6456b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#define OUTGOING_POLICY "out ipsec esp/transport//require"
6466b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
6476b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangstatic inline int get_sockaddr_in(addr, port, sin)
6486b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    const char *addr;
6496b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    int port;
6506b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    struct sockaddr_in *sin;
6516b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang{
6526b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    struct hostent *entry;
6536b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
6546b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    sin->sin_family = AF_INET;
6556b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    sin->sin_port = htons(port);
6566b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    sin->sin_addr.s_addr = inet_addr(addr);
6576b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
6586b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if ((int)sin->sin_addr.s_addr != -1) {
6596b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        return 0;
6606b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
6616b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if ((entry = gethostbyname(addr)) != NULL) {
6626b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        memcpy(&sin->sin_addr, *entry->h_addr_list, sizeof(struct in_addr));
6636b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        if ((int)sin->sin_addr.s_addr != -1) {
6646b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang            return 0;
6656b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        }
6666b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
6676b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    plog(LLV_ERROR, LOCATION, NULL,
6686b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang         "ERROR: incorrect src or dst address(%s)", addr);
6696b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    return -1;
6706b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang}
6716b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
6726b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangstatic int parse_addresses(buf, src, dst)
6736b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    char *buf;
6746b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    struct sockaddr_in *src, *dst;
6756b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang{
6766b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    char *tokens[2], *argv = buf;
6776b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    int i = 0;
6786b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    while(i < 2) {
6796b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        if ((tokens[i++] = strtok(argv, " ")) == NULL) {
6806b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang            return -1;
6816b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        }
6826b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        argv = NULL;
6836b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
6846b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if ((get_sockaddr_in(tokens[0], 0, src) != 0) ||
6856b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        (get_sockaddr_in(tokens[1], PORT_L2TP, dst) != 0)) {
6866b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        return -1;
6876b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
6886b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    return 0;
6896b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang}
6906b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
6916b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangstatic int spdadd(addresses)
6926b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    const char *addresses;
6936b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang{
6946b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    struct sockaddr src, dst;
6956b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    char *outpolicy, *inpolicy;
6966b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    int inlen, outlen, plen;
6976b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    int so = -1;
6986b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
6996b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    /* SETKEY src_ip dst_ip */
7006b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if (parse_addresses(addresses, (struct sockaddr_in *)&src,
7016b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        (struct sockaddr_in *)&dst) < 0) {
7026b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        exit(1);
7036b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
7046b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
7056b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    outpolicy = ipsec_set_policy(OUTGOING_POLICY, strlen(OUTGOING_POLICY));
7066b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    outlen = ipsec_get_policylen(outpolicy);
7076b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    inpolicy = ipsec_set_policy(INCOMING_POLICY, strlen(INCOMING_POLICY));
7086b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    inlen = ipsec_get_policylen(inpolicy);
7096b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
7106b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if ((so = pfkey_open()) < 0) {
7116b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        plog(LLV_ERROR, LOCATION, NULL, "ERROR: %s", ipsec_strerror());
7126b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        exit(1);
7136b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
7146b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    // spdflush()
7156b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if (pfkey_send_spdflush(so) < 0) {
7166b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        plog(LLV_ERROR, LOCATION, NULL, "ERROR: %s", ipsec_strerror());
7176b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        exit(1);
7186b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
7196b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    plen = sizeof(struct in_addr) << 3;
7206b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    // add outgoing policy
7216b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if (pfkey_send_spdadd(so, &src, plen, &dst, plen,
7226b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang                17, outpolicy, outlen, 0) < 0) {
7236b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        plog(LLV_ERROR, LOCATION, NULL, "ERROR: %s", ipsec_strerror());
7246b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        exit(1);
7256b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
7266b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    // add incoming policy
7276b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if (pfkey_send_spdadd(so, &dst, plen, &src, plen,
7286b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang                17, inpolicy, inlen, 0) < 0) {
7296b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        plog(LLV_ERROR, LOCATION, NULL, "ERROR: %s", ipsec_strerror());
7306b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        exit(1);
7316b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
7326b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
7336b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    pfkey_close(so);
7346b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    return 0;
7356b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang}
7366b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
7376b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangstatic int setcerts(cmd)
7386b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    const char *cmd;
7396b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang{
7406b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    /*
7416b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang     * SET_CERTS has 4 arguments:
7426b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang     * destip cacert_path usercert_path userkey_path
7436b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang     */
7446b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    struct remoteconf *tplrmconf;
7456b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    struct sockaddr dst, anonymous;
7466b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    char *tokens[4], *buf = (char*)cmd;
7476b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    int i = 0;
7486b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    while(i < 4) {
7496b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        if ((tokens[i++] = strtok(buf, " ")) == NULL) {
7506b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang            plog(LLV_ERROR, LOCATION, NULL,
7516b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang                 "incorrect command SET_CERTS %s", cmd);
7526b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang            return -1;
7536b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        }
7546b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        buf = NULL;
7556b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
7566b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if (get_sockaddr_in((struct sockaddr_in *)&dst, tokens[0], 0) != 0) {
7576b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        plog(LLV_ERROR, LOCATION, NULL, "incorrect dest address %s", tokens[0]);
7586b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        return -1;
7596b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
7606b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    anonymous.sa_family = AF_UNSPEC;
7616b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if((tplrmconf = getrmconf(&anonymous)) == NULL) {
7626b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        plog(LLV_ERROR, LOCATION, NULL, "Can not find the remtoe template");
7636b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        return -1;
7646b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
7656b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    memcpy(tplrmconf->remote, &dst, sizeof(dst));
7666b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    tplrmconf->cacertfile = strdup(tokens[1]);
7676b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    tplrmconf->mycertfile = strdup(tokens[2]);
7686b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    tplrmconf->myprivfile = strdup(tokens[3]);
7696b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    return 0;
7706b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang}
7716b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
7726b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangstatic int
7736b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangcontrol_process(buf)
7746b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    char *buf;
7756b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang{
7766b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    plog(LLV_ERROR, LOCATION, NULL, "control command %s", buf);
7776b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if(strncmp(buf, CMD_LOAD_CONFIG, strlen(CMD_LOAD_CONFIG)) == 0) {
7786b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        /* LOAD_CONFIG /data/misc/vpn/xxx/racoon.conf */
7796b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        lcconf->racoon_conf = strdup(buf + strlen(CMD_LOAD_CONFIG));
7806b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        return cfreparse();
7816b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    } else if (strncmp(buf, CMD_SETKEY, strlen(CMD_SETKEY)) == 0) {
7826b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        return spdadd(buf + strlen(CMD_SETKEY));
7836b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    } else if (strncmp(buf, CMD_SET_CERTS, strlen(CMD_SET_CERTS)) == 0) {
7846b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        return setcerts(buf + strlen(CMD_SET_CERTS));
7856b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
7866b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    plog(LLV_ERROR, LOCATION, NULL, "Unsupported command '%s'", buf);
7876b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    return -1;
7886b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang}
7896b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
7906b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangint
7916b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangcontrol_init()
7926b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang{
7936b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    lcconf->control_client = -1;
7946b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    lcconf->sock_control = android_get_control_socket(RACOON_SOCKET);
7956b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if (lcconf->sock_control < 0) {
7966b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        plog(LLV_ERROR, LOCATION, NULL,
7976b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang             "Obtaining file descriptor socket '%s' failed: %s",
7986b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang             RACOON_SOCKET, strerror(errno));
7996b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        return -1;
8006b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
8016b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if (listen(lcconf->sock_control, 5) < 0) {
8026b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        plog(LLV_ERROR, LOCATION, NULL,
8036b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang             "Unable to listen on fd '%d' for socket '%s': %s",
8046b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang             lcconf->sock_control, RACOON_SOCKET, strerror(errno));
8056b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        close(lcconf->sock_control);
8066b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        lcconf->sock_control = -1;
8076b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        return -1;
8086b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
8096b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    return 0;
8106b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang}
8116b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
8126b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangint
8136b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangcontrol_newclient()
8146b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang{
8156b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    struct sockaddr_storage from;
8166b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    socklen_t fromlen = sizeof(from);
8176b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
8186b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    lcconf->control_client =
8196b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        accept(lcconf->sock_control, (struct sockaddr *)&from, &fromlen);
8206b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if (lcconf->control_client < 0) {
8216b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        plog(LLV_ERROR, LOCATION, NULL,
8226b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang            "failed to accept control command: %s\n",
8236b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang            strerror(errno));
8246b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        return -1;
8256b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
8266b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    return 0;
8276b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang}
8286b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
8296b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangint
8306b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangcontrol_handler()
8316b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang{
8326b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    char buf[512], reply;
8336b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    int i, n, len, error = -1;
8346b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
8356b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    /* get command */
8366b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    i = 0;
8376b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    len = sizeof(buf);
8386b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    while ((n = recv(lcconf->control_client, buf + i, len - i, 0)) > 0) {
8396b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        i += n;
8406b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        if (i >= len) {
8416b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang            plog(LLV_ERROR, LOCATION, NULL,
8426b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang                "command is too long: %s\n", buf);
8436b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang            goto end;
8446b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        } else if (buf[i - 1] == 0) {
8456b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang            error = control_process(buf);
8466b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang            goto end;
8476b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        }
8486b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
8496b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    plog(LLV_ERROR, LOCATION, NULL,
8506b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        "failed to recv control command: %s\n", strerror(errno));
8516b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
8526b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangend:
8536b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    reply = error;
8546b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    if (send(lcconf->control_client, &reply, 1, 0) != 1) {
8556b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang        plog(LLV_ERROR, LOCATION, NULL,
8566b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang             "failed to send the reply(%d) back\n", reply);
8576b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    }
8586b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    (void)close(lcconf->control_client);
8596b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    lcconf->control_client = -1;
8606b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    return error;
8616b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang}
8626b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
8636b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangint
8646b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangcontrol_close()
8656b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang{
8666b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    close(lcconf->sock_control);
8676b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang    return 0;
8686b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang}
8696b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang
8706b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wangvoid test_commands(char *cmd)
8716b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang{
8726b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang  control_process(0, cmd);
8736b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang}
8746b112236f31ae21c11e416d3dd5d265a5cc5177aChung-yih Wang#endif
875