1 /* $FreeBSD: src/lib/libipsec/ipsec_dump_policy.c,v 1.1.2.1 2000/07/15 07:24:04 kris Exp $ */
2 /* $KAME: ipsec_dump_policy.c,v 1.11 2000/05/07 05:29:47 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/socket.h>
37 #include <netkey/key_var.h>
38 #include <netinet/in.h>
39 #include <netinet6/ipsec.h>
41 #include <arpa/inet.h>
48 #include "ipsec_strerror.h"
50 static const char *ipsp_dir_strs[] = {
54 static const char *ipsp_policy_strs[] = {
55 "discard", "none", "ipsec", "entrust", "bypass",
58 static char *ipsec_dump_ipsecrequest __P((char *, size_t,
59 struct sadb_x_ipsecrequest *, size_t));
60 static int set_addresses __P((char *, size_t, struct sockaddr *,
62 static char *set_address __P((char *, size_t, struct sockaddr *));
65 * policy is sadb_x_policy buffer.
66 * Must call free() later.
67 * When delimiter == NULL, alternatively ' '(space) is applied.
70 ipsec_dump_policy(policy, delimiter)
74 struct sadb_x_policy *xpl = (struct sadb_x_policy *)policy;
75 struct sadb_x_ipsecrequest *xisr;
84 if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
85 __ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
90 if (delimiter == NULL)
93 switch (xpl->sadb_x_policy_dir) {
95 case IPSEC_DIR_INBOUND:
96 case IPSEC_DIR_OUTBOUND:
99 __ipsec_errcode = EIPSEC_INVAL_DIR;
103 switch (xpl->sadb_x_policy_type) {
104 case IPSEC_POLICY_DISCARD:
105 case IPSEC_POLICY_NONE:
106 case IPSEC_POLICY_IPSEC:
107 case IPSEC_POLICY_BYPASS:
108 case IPSEC_POLICY_ENTRUST:
111 __ipsec_errcode = EIPSEC_INVAL_POLICY;
115 buflen = strlen(ipsp_dir_strs[xpl->sadb_x_policy_dir])
117 + strlen(ipsp_policy_strs[xpl->sadb_x_policy_type])
120 if ((buf = malloc(buflen)) == NULL) {
121 __ipsec_errcode = EIPSEC_NO_BUFS;
124 snprintf(buf, buflen, "%s %s", ipsp_dir_strs[xpl->sadb_x_policy_dir],
125 ipsp_policy_strs[xpl->sadb_x_policy_type]);
127 if (xpl->sadb_x_policy_type != IPSEC_POLICY_IPSEC) {
128 __ipsec_errcode = EIPSEC_NO_ERROR;
132 /* count length of buffer for use */
134 while (off < PFKEY_EXTLEN(xpl)) {
135 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xpl + off);
136 off += xisr->sadb_x_ipsecrequest_len;
140 if (off != PFKEY_EXTLEN(xpl)) {
141 __ipsec_errcode = EIPSEC_INVAL_SADBMSG;
147 while (off < PFKEY_EXTLEN(xpl)) {
148 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xpl + off);
150 if (ipsec_dump_ipsecrequest(isrbuf, sizeof(isrbuf), xisr,
151 PFKEY_EXTLEN(xpl) - off) == NULL) {
156 buflen = strlen(buf) + strlen(delimiter) + strlen(isrbuf) + 1;
157 newbuf = (char *)realloc(buf, buflen);
158 if (newbuf == NULL) {
159 __ipsec_errcode = EIPSEC_NO_BUFS;
164 snprintf(buf, buflen, "%s%s%s", buf, delimiter, isrbuf);
166 off += xisr->sadb_x_ipsecrequest_len;
169 __ipsec_errcode = EIPSEC_NO_ERROR;
174 ipsec_dump_ipsecrequest(buf, len, xisr, bound)
177 struct sadb_x_ipsecrequest *xisr;
178 size_t bound; /* boundary */
180 const char *proto, *mode, *level;
181 char abuf[NI_MAXHOST * 2 + 2];
183 if (xisr->sadb_x_ipsecrequest_len > bound) {
184 __ipsec_errcode = EIPSEC_INVAL_PROTO;
188 switch (xisr->sadb_x_ipsecrequest_proto) {
199 __ipsec_errcode = EIPSEC_INVAL_PROTO;
203 switch (xisr->sadb_x_ipsecrequest_mode) {
207 case IPSEC_MODE_TRANSPORT:
210 case IPSEC_MODE_TUNNEL:
214 __ipsec_errcode = EIPSEC_INVAL_MODE;
219 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
220 struct sockaddr *sa1, *sa2;
223 p = (caddr_t)(xisr + 1);
224 sa1 = (struct sockaddr *)p;
225 sa2 = (struct sockaddr *)(p + sa1->sa_len);
226 if (sizeof(*xisr) + sa1->sa_len + sa2->sa_len !=
227 xisr->sadb_x_ipsecrequest_len) {
228 __ipsec_errcode = EIPSEC_INVAL_ADDRESS;
231 if (set_addresses(abuf, sizeof(abuf), sa1, sa2) != 0) {
232 __ipsec_errcode = EIPSEC_INVAL_ADDRESS;
237 switch (xisr->sadb_x_ipsecrequest_level) {
238 case IPSEC_LEVEL_DEFAULT:
241 case IPSEC_LEVEL_USE:
244 case IPSEC_LEVEL_REQUIRE:
247 case IPSEC_LEVEL_UNIQUE:
251 __ipsec_errcode = EIPSEC_INVAL_LEVEL;
255 if (xisr->sadb_x_ipsecrequest_reqid == 0)
256 snprintf(buf, len, "%s/%s/%s/%s", proto, mode, abuf, level);
260 if (xisr->sadb_x_ipsecrequest_reqid > IPSEC_MANUAL_REQID_MAX)
264 snprintf(buf, len, "%s/%s/%s/%s%c%d", proto, mode, abuf, level,
265 ch, xisr->sadb_x_ipsecrequest_reqid);
272 set_addresses(buf, len, sa1, sa2)
275 struct sockaddr *sa1;
276 struct sockaddr *sa2;
278 char tmp1[NI_MAXHOST], tmp2[NI_MAXHOST];
280 if (set_address(tmp1, sizeof(tmp1), sa1) == NULL ||
281 set_address(tmp2, sizeof(tmp2), sa2) == NULL)
283 if (strlen(tmp1) + 1 + strlen(tmp2) + 1 > len)
285 snprintf(buf, len, "%s-%s", tmp1, tmp2);
290 set_address(buf, len, sa)
295 #ifdef NI_WITHSCOPEID
296 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
298 const int niflags = NI_NUMERICHOST;
304 if (getnameinfo(sa, sa->sa_len, buf, len, NULL, 0, niflags) != 0)