1 /* $FreeBSD: src/lib/libipsec/test-policy.c,v 1.2.2.2 2001/07/03 11:01:15 ume Exp $ */
2 /* $KAME: test-policy.c,v 1.14 2000/12/27 11:38:11 sakane 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 <net/pfkeyv2.h>
39 #include <netkey/key_debug.h>
40 #include <netinet6/ipsec.h>
52 int result
; /* expected result; 0:ok 1:ng */
57 { 1, "in ipsec must_error" },
58 { 1, "out ipsec esp/must_error" },
63 { 1, "out ipsec esp" },
64 { 0, "in ipsec ah/transport" },
65 { 1, "in ipsec ah/tunnel" },
66 { 0, "out ipsec ah/transport/" },
67 { 1, "out ipsec ah/tunnel/" },
68 { 0, "in ipsec esp / transport / 10.0.0.1-10.0.0.2" },
69 { 0, "in ipsec esp/tunnel/::1-::2" },
70 { 1, "in ipsec esp/tunnel/10.0.0.1-::2" },
71 { 0, "in ipsec esp/tunnel/::1-::2/require" },
72 { 0, "out ipsec ah/transport//use" },
73 { 1, "out ipsec ah/transport esp/use" },
74 { 1, "in ipsec ah/transport esp/tunnel" },
75 { 0, "in ipsec ah/transport esp/tunnel/::1-::1" },
78 esp / tunnel / ::1-::2" },
80 ah/transport/::1-::2 esp/tunnel/::3-::4/use ah/transport/::5-::6/require
81 ah/transport/::1-::2 esp/tunnel/::3-::4/use ah/transport/::5-::6/require
82 ah/transport/::1-::2 esp/tunnel/::3-::4/use ah/transport/::5-::6/require
84 { 0, "out ipsec esp/transport/fec0::10-fec0::11/use" },
88 int test1sub1 (struct req_t
*);
89 int test1sub2 (char *, int);
111 for (i
= 0; i
< NELEM(reqs
); i
++) {
112 printf("#%d [%s]\n", i
+ 1, reqs
[i
].str
);
114 result
= test1sub1(&reqs
[i
]);
115 if (result
== 0 && reqs
[i
].result
== 1) {
116 warnx("ERROR: expecting failure.\n");
117 } else if (result
== 1 && reqs
[i
].result
== 0) {
118 warnx("ERROR: expecting success.\n");
131 buf
= ipsec_set_policy(req
->str
, strlen(req
->str
));
133 printf("ipsec_set_policy: %s\n", ipsec_strerror());
137 if (test1sub2(buf
, PF_INET
) != 0
138 || test1sub2(buf
, PF_INET6
) != 0) {
143 kdebug_sadb_x_policy((struct sadb_ext
*)buf
);
151 test1sub2(policy
, family
)
156 int proto
= 0, optname
= 0;
163 optname
= IP_IPSEC_POLICY
;
166 proto
= IPPROTO_IPV6
;
167 optname
= IPV6_IPSEC_POLICY
;
171 if ((so
= socket(family
, SOCK_DGRAM
, 0)) < 0)
174 len
= ipsec_get_policylen(policy
);
176 printf("\tsetlen:%d\n", len
);
179 if (setsockopt(so
, proto
, optname
, policy
, len
) < 0) {
180 printf("fail to set sockopt; %s\n", strerror(errno
));
185 memset(getbuf
, 0, sizeof(getbuf
));
186 memcpy(getbuf
, policy
, sizeof(struct sadb_x_policy
));
187 if (getsockopt(so
, proto
, optname
, getbuf
, &len
) < 0) {
188 printf("fail to get sockopt; %s\n", strerror(errno
));
197 printf("\tgetlen:%d\n", len
);
200 if ((buf
= ipsec_dump_policy(getbuf
, NULL
)) == NULL
) {
201 printf("%s\n", ipsec_strerror());
206 printf("\t[%s]\n", buf
);
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
226 char *pol1
= "out ipsec";
227 char *pol2
= "out ipsec ah/transport//use";
235 errx(1, "root privilege required.\n");
237 sp1
= ipsec_set_policy(pol1
, strlen(pol1
));
238 splen1
= ipsec_get_policylen(sp1
);
239 sp2
= ipsec_set_policy(pol2
, strlen(pol2
));
240 splen2
= ipsec_get_policylen(sp2
);
242 if ((so
= pfkey_open()) < 0)
243 errx(1, "ERROR: %s\n", ipsec_strerror());
245 printf("spdflush()\n");
246 if (pfkey_send_spdflush(so
) < 0)
247 errx(1, "ERROR: %s\n", ipsec_strerror());
252 printf("spdsetidx()\n");
253 if (pfkey_send_spdsetidx(so
, (struct sockaddr
*)addr
, 128,
254 (struct sockaddr
*)addr
, 128,
255 255, sp1
, splen1
, 0) < 0)
256 errx(1, "ERROR: %s\n", ipsec_strerror());
260 printf("spdupdate()\n");
261 if (pfkey_send_spdupdate(so
, (struct sockaddr
*)addr
, 128,
262 (struct sockaddr
*)addr
, 128,
263 255, sp2
, splen2
, 0) < 0)
264 errx(1, "ERROR: %s\n", ipsec_strerror());
270 printf("spddelete()\n");
271 if (pfkey_send_spddelete(so
, (struct sockaddr
*)addr
, 128,
272 (struct sockaddr
*)addr
, 128,
273 255, sp1
, splen1
, 0) < 0)
274 errx(1, "ERROR: %s\n", ipsec_strerror());
278 printf("spdadd()\n");
279 if (pfkey_send_spdadd(so
, (struct sockaddr
*)addr
, 128,
280 (struct sockaddr
*)addr
, 128,
281 255, sp2
, splen2
, 0) < 0)
282 errx(1, "ERROR: %s\n", ipsec_strerror());
285 printf("spdget(%u)\n", spid
);
286 if (pfkey_send_spdget(so
, spid
) < 0)
287 errx(1, "ERROR: %s\n", ipsec_strerror());
293 printf("spddelete2()\n");
294 if (pfkey_send_spddelete2(so
, spid
) < 0)
295 errx(1, "ERROR: %s\n", ipsec_strerror());
300 printf("spdadd() with lifetime's 10(s)\n");
301 if (pfkey_send_spdadd2(so
, (struct sockaddr
*)addr
, 128,
302 (struct sockaddr
*)addr
, 128,
303 255, 0, 10, sp2
, splen2
, 0) < 0)
304 errx(1, "ERROR: %s\n", ipsec_strerror());
308 /* expecting failure */
309 printf("spdupdate()\n");
310 if (pfkey_send_spdupdate(so
, (struct sockaddr
*)addr
, 128,
311 (struct sockaddr
*)addr
, 128,
312 255, sp2
, splen2
, 0) == 0) {
313 warnx("ERROR: expecting failure.\n");
324 struct sadb_msg
*msg
;
325 caddr_t mhp
[SADB_EXT_MAX
+ 1];
327 if ((msg
= pfkey_recv(so
)) == NULL
)
328 errx(1, "ERROR: pfkey_recv failure.\n");
329 if (pfkey_align(msg
, mhp
) < 0)
330 errx(1, "ERROR: pfkey_align failure.\n");
332 return ((struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
])->sadb_x_policy_id
;