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 <netinet/in.h>
38 #include <netinet6/ipsec.h>
40 #include <arpa/inet.h>
47 #include "ipsec_strerror.h"
49 static const char *ipsp_dir_strs
[] = {
53 static const char *ipsp_policy_strs
[] = {
54 "discard", "none", "ipsec", "entrust", "bypass",
57 static char *ipsec_dump_ipsecrequest
__P((char *, size_t,
58 struct sadb_x_ipsecrequest
*, size_t));
59 static int set_addresses
__P((char *, size_t, struct sockaddr
*,
61 static char *set_address
__P((char *, size_t, struct sockaddr
*));
64 * policy is sadb_x_policy buffer.
65 * Must call free() later.
66 * When delimiter == NULL, alternatively ' '(space) is applied.
69 ipsec_dump_policy(policy
, delimiter
)
73 struct sadb_x_policy
*xpl
= (struct sadb_x_policy
*)policy
;
74 struct sadb_x_ipsecrequest
*xisr
;
83 if (xpl
->sadb_x_policy_exttype
!= SADB_X_EXT_POLICY
) {
84 __ipsec_errcode
= EIPSEC_INVAL_EXTTYPE
;
89 if (delimiter
== NULL
)
92 switch (xpl
->sadb_x_policy_dir
) {
94 case IPSEC_DIR_INBOUND
:
95 case IPSEC_DIR_OUTBOUND
:
98 __ipsec_errcode
= EIPSEC_INVAL_DIR
;
102 switch (xpl
->sadb_x_policy_type
) {
103 case IPSEC_POLICY_DISCARD
:
104 case IPSEC_POLICY_NONE
:
105 case IPSEC_POLICY_IPSEC
:
106 case IPSEC_POLICY_BYPASS
:
107 case IPSEC_POLICY_ENTRUST
:
110 __ipsec_errcode
= EIPSEC_INVAL_POLICY
;
114 buflen
= strlen(ipsp_dir_strs
[xpl
->sadb_x_policy_dir
])
116 + strlen(ipsp_policy_strs
[xpl
->sadb_x_policy_type
])
119 if ((buf
= malloc(buflen
)) == NULL
) {
120 __ipsec_errcode
= EIPSEC_NO_BUFS
;
123 snprintf(buf
, buflen
, "%s %s", ipsp_dir_strs
[xpl
->sadb_x_policy_dir
],
124 ipsp_policy_strs
[xpl
->sadb_x_policy_type
]);
126 if (xpl
->sadb_x_policy_type
!= IPSEC_POLICY_IPSEC
) {
127 __ipsec_errcode
= EIPSEC_NO_ERROR
;
131 /* count length of buffer for use */
133 while (off
< PFKEY_EXTLEN(xpl
)) {
134 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xpl
+ off
);
135 off
+= xisr
->sadb_x_ipsecrequest_len
;
139 if (off
!= PFKEY_EXTLEN(xpl
)) {
140 __ipsec_errcode
= EIPSEC_INVAL_SADBMSG
;
146 while (off
< PFKEY_EXTLEN(xpl
)) {
147 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xpl
+ off
);
149 if (ipsec_dump_ipsecrequest(isrbuf
, sizeof(isrbuf
), xisr
,
150 PFKEY_EXTLEN(xpl
) - off
) == NULL
) {
155 buflen
= strlen(buf
) + strlen(delimiter
) + strlen(isrbuf
) + 1;
156 newbuf
= (char *)realloc(buf
, buflen
);
157 if (newbuf
== NULL
) {
158 __ipsec_errcode
= EIPSEC_NO_BUFS
;
163 snprintf(buf
, buflen
, "%s%s%s", buf
, delimiter
, isrbuf
);
165 off
+= xisr
->sadb_x_ipsecrequest_len
;
168 __ipsec_errcode
= EIPSEC_NO_ERROR
;
173 ipsec_dump_ipsecrequest(buf
, len
, xisr
, bound
)
176 struct sadb_x_ipsecrequest
*xisr
;
177 size_t bound
; /* boundary */
179 const char *proto
, *mode
, *level
;
180 char abuf
[NI_MAXHOST
* 2 + 2];
182 if (xisr
->sadb_x_ipsecrequest_len
> bound
) {
183 __ipsec_errcode
= EIPSEC_INVAL_PROTO
;
187 switch (xisr
->sadb_x_ipsecrequest_proto
) {
198 __ipsec_errcode
= EIPSEC_INVAL_PROTO
;
202 switch (xisr
->sadb_x_ipsecrequest_mode
) {
206 case IPSEC_MODE_TRANSPORT
:
209 case IPSEC_MODE_TUNNEL
:
213 __ipsec_errcode
= EIPSEC_INVAL_MODE
;
218 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
219 struct sockaddr
*sa1
, *sa2
;
222 p
= (caddr_t
)(xisr
+ 1);
223 sa1
= (struct sockaddr
*)p
;
224 sa2
= (struct sockaddr
*)(p
+ sa1
->sa_len
);
225 if (sizeof(*xisr
) + sa1
->sa_len
+ sa2
->sa_len
!=
226 xisr
->sadb_x_ipsecrequest_len
) {
227 __ipsec_errcode
= EIPSEC_INVAL_ADDRESS
;
230 if (set_addresses(abuf
, sizeof(abuf
), sa1
, sa2
) != 0) {
231 __ipsec_errcode
= EIPSEC_INVAL_ADDRESS
;
236 switch (xisr
->sadb_x_ipsecrequest_level
) {
237 case IPSEC_LEVEL_DEFAULT
:
240 case IPSEC_LEVEL_USE
:
243 case IPSEC_LEVEL_REQUIRE
:
246 case IPSEC_LEVEL_UNIQUE
:
250 __ipsec_errcode
= EIPSEC_INVAL_LEVEL
;
254 if (xisr
->sadb_x_ipsecrequest_reqid
== 0)
255 snprintf(buf
, len
, "%s/%s/%s/%s", proto
, mode
, abuf
, level
);
259 if (xisr
->sadb_x_ipsecrequest_reqid
> IPSEC_MANUAL_REQID_MAX
)
263 snprintf(buf
, len
, "%s/%s/%s/%s%c%d", proto
, mode
, abuf
, level
,
264 ch
, xisr
->sadb_x_ipsecrequest_reqid
);
271 set_addresses(buf
, len
, sa1
, sa2
)
274 struct sockaddr
*sa1
;
275 struct sockaddr
*sa2
;
277 char tmp1
[NI_MAXHOST
], tmp2
[NI_MAXHOST
];
279 if (set_address(tmp1
, sizeof(tmp1
), sa1
) == NULL
||
280 set_address(tmp2
, sizeof(tmp2
), sa2
) == NULL
)
282 if (strlen(tmp1
) + 1 + strlen(tmp2
) + 1 > len
)
284 snprintf(buf
, len
, "%s-%s", tmp1
, tmp2
);
289 set_address(buf
, len
, sa
)
294 #ifdef NI_WITHSCOPEID
295 const int niflags
= NI_NUMERICHOST
| NI_WITHSCOPEID
;
297 const int niflags
= NI_NUMERICHOST
;
303 if (getnameinfo(sa
, sa
->sa_len
, buf
, len
, NULL
, 0, niflags
) != 0)