2003-10-26 13:00:27 +01:00
|
|
|
/* $KAME: ipsec_dump_policy.c,v 1.13 2002/06/27 14:35:11 itojun Exp $ */
|
2000-07-04 18:22:05 +02:00
|
|
|
|
2000-01-06 13:40:54 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-09-30 23:43:45 +02:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-01-06 13:40:54 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2007-07-01 14:08:08 +02:00
|
|
|
#include <netipsec/key_var.h>
|
2000-01-06 13:40:54 +01:00
|
|
|
#include <netinet/in.h>
|
2007-07-01 14:08:08 +02:00
|
|
|
#include <netipsec/ipsec.h>
|
2000-01-06 13:40:54 +01:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2000-07-04 18:22:05 +02:00
|
|
|
#include <stdio.h>
|
2000-01-06 13:40:54 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include "ipsec_strerror.h"
|
|
|
|
|
|
|
|
static const char *ipsp_dir_strs[] = {
|
|
|
|
"any", "in", "out",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *ipsp_policy_strs[] = {
|
|
|
|
"discard", "none", "ipsec", "entrust", "bypass",
|
|
|
|
};
|
|
|
|
|
2002-03-22 10:22:15 +01:00
|
|
|
static char *ipsec_dump_ipsecrequest(char *, size_t,
|
|
|
|
struct sadb_x_ipsecrequest *, size_t);
|
|
|
|
static int set_addresses(char *, size_t, struct sockaddr *, struct sockaddr *);
|
2002-03-22 00:54:04 +01:00
|
|
|
static char *set_address(char *, size_t, struct sockaddr *);
|
2000-01-06 13:40:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* policy is sadb_x_policy buffer.
|
|
|
|
* Must call free() later.
|
|
|
|
* When delimiter == NULL, alternatively ' '(space) is applied.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
ipsec_dump_policy(policy, delimiter)
|
|
|
|
caddr_t policy;
|
|
|
|
char *delimiter;
|
|
|
|
{
|
|
|
|
struct sadb_x_policy *xpl = (struct sadb_x_policy *)policy;
|
|
|
|
struct sadb_x_ipsecrequest *xisr;
|
2000-07-04 18:22:05 +02:00
|
|
|
size_t off, buflen;
|
2000-01-06 13:40:54 +01:00
|
|
|
char *buf;
|
2000-07-04 18:22:05 +02:00
|
|
|
char isrbuf[1024];
|
|
|
|
char *newbuf;
|
2000-01-06 13:40:54 +01:00
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (policy == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
|
2000-07-04 18:22:05 +02:00
|
|
|
__ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
|
2000-01-06 13:40:54 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set delimiter */
|
|
|
|
if (delimiter == NULL)
|
|
|
|
delimiter = " ";
|
|
|
|
|
|
|
|
switch (xpl->sadb_x_policy_dir) {
|
|
|
|
case IPSEC_DIR_ANY:
|
|
|
|
case IPSEC_DIR_INBOUND:
|
|
|
|
case IPSEC_DIR_OUTBOUND:
|
|
|
|
break;
|
|
|
|
default:
|
2000-07-04 18:22:05 +02:00
|
|
|
__ipsec_errcode = EIPSEC_INVAL_DIR;
|
2000-01-06 13:40:54 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (xpl->sadb_x_policy_type) {
|
|
|
|
case IPSEC_POLICY_DISCARD:
|
|
|
|
case IPSEC_POLICY_NONE:
|
|
|
|
case IPSEC_POLICY_IPSEC:
|
|
|
|
case IPSEC_POLICY_BYPASS:
|
|
|
|
case IPSEC_POLICY_ENTRUST:
|
|
|
|
break;
|
|
|
|
default:
|
2000-07-04 18:22:05 +02:00
|
|
|
__ipsec_errcode = EIPSEC_INVAL_POLICY;
|
2000-01-06 13:40:54 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buflen = strlen(ipsp_dir_strs[xpl->sadb_x_policy_dir])
|
|
|
|
+ 1 /* space */
|
|
|
|
+ strlen(ipsp_policy_strs[xpl->sadb_x_policy_type])
|
|
|
|
+ 1; /* NUL */
|
|
|
|
|
|
|
|
if ((buf = malloc(buflen)) == NULL) {
|
2000-07-04 18:22:05 +02:00
|
|
|
__ipsec_errcode = EIPSEC_NO_BUFS;
|
2000-01-06 13:40:54 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-07-04 18:22:05 +02:00
|
|
|
snprintf(buf, buflen, "%s %s", ipsp_dir_strs[xpl->sadb_x_policy_dir],
|
|
|
|
ipsp_policy_strs[xpl->sadb_x_policy_type]);
|
2000-01-06 13:40:54 +01:00
|
|
|
|
|
|
|
if (xpl->sadb_x_policy_type != IPSEC_POLICY_IPSEC) {
|
2000-07-04 18:22:05 +02:00
|
|
|
__ipsec_errcode = EIPSEC_NO_ERROR;
|
2000-01-06 13:40:54 +01:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* count length of buffer for use */
|
2000-07-04 18:22:05 +02:00
|
|
|
off = sizeof(*xpl);
|
|
|
|
while (off < PFKEY_EXTLEN(xpl)) {
|
|
|
|
xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xpl + off);
|
|
|
|
off += xisr->sadb_x_ipsecrequest_len;
|
2000-01-06 13:40:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* validity check */
|
2000-07-04 18:22:05 +02:00
|
|
|
if (off != PFKEY_EXTLEN(xpl)) {
|
|
|
|
__ipsec_errcode = EIPSEC_INVAL_SADBMSG;
|
2000-01-06 13:40:54 +01:00
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-07-04 18:22:05 +02:00
|
|
|
off = sizeof(*xpl);
|
|
|
|
while (off < PFKEY_EXTLEN(xpl)) {
|
|
|
|
xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xpl + off);
|
2000-01-06 13:40:54 +01:00
|
|
|
|
2000-07-04 18:22:05 +02:00
|
|
|
if (ipsec_dump_ipsecrequest(isrbuf, sizeof(isrbuf), xisr,
|
|
|
|
PFKEY_EXTLEN(xpl) - off) == NULL) {
|
2000-01-06 13:40:54 +01:00
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-07-04 18:22:05 +02:00
|
|
|
buflen = strlen(buf) + strlen(delimiter) + strlen(isrbuf) + 1;
|
|
|
|
newbuf = (char *)realloc(buf, buflen);
|
|
|
|
if (newbuf == NULL) {
|
|
|
|
__ipsec_errcode = EIPSEC_NO_BUFS;
|
2000-01-06 13:40:54 +01:00
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-07-04 18:22:05 +02:00
|
|
|
buf = newbuf;
|
2010-12-11 09:07:21 +01:00
|
|
|
snprintf(buf + strlen(buf), buflen - strlen(buf),
|
|
|
|
"%s%s", delimiter, isrbuf);
|
2000-01-06 13:40:54 +01:00
|
|
|
|
2000-07-04 18:22:05 +02:00
|
|
|
off += xisr->sadb_x_ipsecrequest_len;
|
|
|
|
}
|
2000-01-06 13:40:54 +01:00
|
|
|
|
2000-07-04 18:22:05 +02:00
|
|
|
__ipsec_errcode = EIPSEC_NO_ERROR;
|
|
|
|
return buf;
|
|
|
|
}
|
2000-01-06 13:40:54 +01:00
|
|
|
|
2000-07-04 18:22:05 +02:00
|
|
|
static char *
|
|
|
|
ipsec_dump_ipsecrequest(buf, len, xisr, bound)
|
|
|
|
char *buf;
|
|
|
|
size_t len;
|
|
|
|
struct sadb_x_ipsecrequest *xisr;
|
|
|
|
size_t bound; /* boundary */
|
|
|
|
{
|
|
|
|
const char *proto, *mode, *level;
|
|
|
|
char abuf[NI_MAXHOST * 2 + 2];
|
|
|
|
|
|
|
|
if (xisr->sadb_x_ipsecrequest_len > bound) {
|
|
|
|
__ipsec_errcode = EIPSEC_INVAL_PROTO;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (xisr->sadb_x_ipsecrequest_proto) {
|
|
|
|
case IPPROTO_ESP:
|
|
|
|
proto = "esp";
|
|
|
|
break;
|
|
|
|
case IPPROTO_AH:
|
|
|
|
proto = "ah";
|
|
|
|
break;
|
|
|
|
case IPPROTO_IPCOMP:
|
|
|
|
proto = "ipcomp";
|
|
|
|
break;
|
2004-02-11 05:34:34 +01:00
|
|
|
case IPPROTO_TCP:
|
|
|
|
proto = "tcp";
|
2014-04-17 12:27:18 +02:00
|
|
|
break;
|
2000-07-04 18:22:05 +02:00
|
|
|
default:
|
|
|
|
__ipsec_errcode = EIPSEC_INVAL_PROTO;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (xisr->sadb_x_ipsecrequest_mode) {
|
|
|
|
case IPSEC_MODE_ANY:
|
|
|
|
mode = "any";
|
|
|
|
break;
|
|
|
|
case IPSEC_MODE_TRANSPORT:
|
|
|
|
mode = "transport";
|
|
|
|
break;
|
|
|
|
case IPSEC_MODE_TUNNEL:
|
|
|
|
mode = "tunnel";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
__ipsec_errcode = EIPSEC_INVAL_MODE;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
abuf[0] = '\0';
|
|
|
|
if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
|
|
|
|
struct sockaddr *sa1, *sa2;
|
|
|
|
caddr_t p;
|
|
|
|
|
|
|
|
p = (caddr_t)(xisr + 1);
|
|
|
|
sa1 = (struct sockaddr *)p;
|
|
|
|
sa2 = (struct sockaddr *)(p + sa1->sa_len);
|
|
|
|
if (sizeof(*xisr) + sa1->sa_len + sa2->sa_len !=
|
|
|
|
xisr->sadb_x_ipsecrequest_len) {
|
|
|
|
__ipsec_errcode = EIPSEC_INVAL_ADDRESS;
|
2000-01-06 13:40:54 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-07-04 18:22:05 +02:00
|
|
|
if (set_addresses(abuf, sizeof(abuf), sa1, sa2) != 0) {
|
|
|
|
__ipsec_errcode = EIPSEC_INVAL_ADDRESS;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2000-01-06 13:40:54 +01:00
|
|
|
|
2000-07-04 18:22:05 +02:00
|
|
|
switch (xisr->sadb_x_ipsecrequest_level) {
|
|
|
|
case IPSEC_LEVEL_DEFAULT:
|
|
|
|
level = "default";
|
|
|
|
break;
|
|
|
|
case IPSEC_LEVEL_USE:
|
|
|
|
level = "use";
|
|
|
|
break;
|
|
|
|
case IPSEC_LEVEL_REQUIRE:
|
|
|
|
level = "require";
|
|
|
|
break;
|
|
|
|
case IPSEC_LEVEL_UNIQUE:
|
|
|
|
level = "unique";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
__ipsec_errcode = EIPSEC_INVAL_LEVEL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xisr->sadb_x_ipsecrequest_reqid == 0)
|
|
|
|
snprintf(buf, len, "%s/%s/%s/%s", proto, mode, abuf, level);
|
|
|
|
else {
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
if (xisr->sadb_x_ipsecrequest_reqid > IPSEC_MANUAL_REQID_MAX)
|
|
|
|
ch = '#';
|
|
|
|
else
|
|
|
|
ch = ':';
|
2003-10-26 13:00:27 +01:00
|
|
|
snprintf(buf, len, "%s/%s/%s/%s%c%u", proto, mode, abuf, level,
|
2000-07-04 18:22:05 +02:00
|
|
|
ch, xisr->sadb_x_ipsecrequest_reqid);
|
2000-01-06 13:40:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-04 18:22:05 +02:00
|
|
|
set_addresses(buf, len, sa1, sa2)
|
2000-01-06 13:40:54 +01:00
|
|
|
char *buf;
|
2000-07-04 18:22:05 +02:00
|
|
|
size_t len;
|
|
|
|
struct sockaddr *sa1;
|
|
|
|
struct sockaddr *sa2;
|
2000-01-06 13:40:54 +01:00
|
|
|
{
|
2000-07-04 18:22:05 +02:00
|
|
|
char tmp1[NI_MAXHOST], tmp2[NI_MAXHOST];
|
|
|
|
|
|
|
|
if (set_address(tmp1, sizeof(tmp1), sa1) == NULL ||
|
|
|
|
set_address(tmp2, sizeof(tmp2), sa2) == NULL)
|
|
|
|
return -1;
|
|
|
|
if (strlen(tmp1) + 1 + strlen(tmp2) + 1 > len)
|
|
|
|
return -1;
|
|
|
|
snprintf(buf, len, "%s-%s", tmp1, tmp2);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-01-06 13:40:54 +01:00
|
|
|
|
2000-07-04 18:22:05 +02:00
|
|
|
static char *
|
|
|
|
set_address(buf, len, sa)
|
|
|
|
char *buf;
|
|
|
|
size_t len;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
{
|
|
|
|
const int niflags = NI_NUMERICHOST;
|
2000-01-06 13:40:54 +01:00
|
|
|
|
2000-07-04 18:22:05 +02:00
|
|
|
if (len < 1)
|
|
|
|
return NULL;
|
|
|
|
buf[0] = '\0';
|
|
|
|
if (getnameinfo(sa, sa->sa_len, buf, len, NULL, 0, niflags) != 0)
|
|
|
|
return NULL;
|
|
|
|
return buf;
|
2000-01-06 13:40:54 +01:00
|
|
|
}
|