1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
49a4365d0SYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
59a4365d0SYoshinobu Inoue * All rights reserved.
69a4365d0SYoshinobu Inoue *
79a4365d0SYoshinobu Inoue * Redistribution and use in source and binary forms, with or without
89a4365d0SYoshinobu Inoue * modification, are permitted provided that the following conditions
99a4365d0SYoshinobu Inoue * are met:
109a4365d0SYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright
119a4365d0SYoshinobu Inoue * notice, this list of conditions and the following disclaimer.
129a4365d0SYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright
139a4365d0SYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the
149a4365d0SYoshinobu Inoue * documentation and/or other materials provided with the distribution.
159a4365d0SYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors
169a4365d0SYoshinobu Inoue * may be used to endorse or promote products derived from this software
179a4365d0SYoshinobu Inoue * without specific prior written permission.
189a4365d0SYoshinobu Inoue *
199a4365d0SYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
209a4365d0SYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219a4365d0SYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229a4365d0SYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
239a4365d0SYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249a4365d0SYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259a4365d0SYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269a4365d0SYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279a4365d0SYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289a4365d0SYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299a4365d0SYoshinobu Inoue * SUCH DAMAGE.
309a4365d0SYoshinobu Inoue */
319a4365d0SYoshinobu Inoue
329a4365d0SYoshinobu Inoue #include <sys/types.h>
339a4365d0SYoshinobu Inoue #include <sys/param.h>
349a4365d0SYoshinobu Inoue #include <sys/socket.h>
359a4365d0SYoshinobu Inoue #include <netinet/in.h>
369a4365d0SYoshinobu Inoue #include <netinet6/in6.h>
378409aedfSGeorge V. Neville-Neil #include <netipsec/ipsec.h>
389a4365d0SYoshinobu Inoue #include <stdlib.h>
399a4365d0SYoshinobu Inoue #include <string.h>
409a4365d0SYoshinobu Inoue
419a4365d0SYoshinobu Inoue
429a4365d0SYoshinobu Inoue char *requests[] = {
439a4365d0SYoshinobu Inoue "must_error", /* must be error */
449a4365d0SYoshinobu Inoue "ipsec must_error", /* must be error */
459a4365d0SYoshinobu Inoue "ipsec esp/must_error", /* must be error */
469a4365d0SYoshinobu Inoue "discard",
479a4365d0SYoshinobu Inoue "none",
489a4365d0SYoshinobu Inoue "entrust",
499a4365d0SYoshinobu Inoue "bypass", /* may be error */
509a4365d0SYoshinobu Inoue "ipsec esp", /* must be error */
519a4365d0SYoshinobu Inoue "ipsec ah/require",
529a4365d0SYoshinobu Inoue "ipsec ah/use/",
539a4365d0SYoshinobu Inoue "ipsec esp/require ah/default/203.178.141.194",
549a4365d0SYoshinobu Inoue "ipsec ah/use/203.178.141.195 esp/use/203.178.141.194",
559a4365d0SYoshinobu Inoue "ipsec esp/elf.wide.ydc.co.jp esp/www.wide.ydc.co.jp"
569a4365d0SYoshinobu Inoue "
579a4365d0SYoshinobu Inoue ipsec esp/require ah/use esp/require/10.0.0.1
589a4365d0SYoshinobu Inoue ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
599a4365d0SYoshinobu Inoue ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
609a4365d0SYoshinobu Inoue ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
619a4365d0SYoshinobu Inoue ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
629a4365d0SYoshinobu Inoue ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
639a4365d0SYoshinobu Inoue ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
649a4365d0SYoshinobu Inoue ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
659a4365d0SYoshinobu Inoue ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1ah/use/3ffe:501:481d::1
669a4365d0SYoshinobu Inoue ",
679a4365d0SYoshinobu Inoue };
689a4365d0SYoshinobu Inoue
699a4365d0SYoshinobu Inoue u_char *p_secpolicy;
709a4365d0SYoshinobu Inoue
719a4365d0SYoshinobu Inoue int test(char *buf, int family);
729a4365d0SYoshinobu Inoue char *setpolicy(char *req);
739a4365d0SYoshinobu Inoue
main()749a4365d0SYoshinobu Inoue main()
759a4365d0SYoshinobu Inoue {
769a4365d0SYoshinobu Inoue int i;
779a4365d0SYoshinobu Inoue char *buf;
789a4365d0SYoshinobu Inoue
7921eacf98SMarcelo Araujo for (i = 0; i < nitems(requests); i++) {
809a4365d0SYoshinobu Inoue printf("* requests:[%s]\n", requests[i]);
819a4365d0SYoshinobu Inoue if ((buf = setpolicy(requests[i])) == NULL)
829a4365d0SYoshinobu Inoue continue;
839a4365d0SYoshinobu Inoue printf("\tsetlen:%d\n", PFKEY_EXTLEN(buf));
849a4365d0SYoshinobu Inoue
859a4365d0SYoshinobu Inoue printf("\tPF_INET:\n");
869a4365d0SYoshinobu Inoue test(buf, PF_INET);
879a4365d0SYoshinobu Inoue
889a4365d0SYoshinobu Inoue printf("\tPF_INET6:\n");
899a4365d0SYoshinobu Inoue test(buf, PF_INET6);
909a4365d0SYoshinobu Inoue free(buf);
919a4365d0SYoshinobu Inoue }
929a4365d0SYoshinobu Inoue }
939a4365d0SYoshinobu Inoue
test(char * policy,int family)949a4365d0SYoshinobu Inoue int test(char *policy, int family)
959a4365d0SYoshinobu Inoue {
969a4365d0SYoshinobu Inoue int so, proto, optname;
979a4365d0SYoshinobu Inoue int len;
989a4365d0SYoshinobu Inoue char getbuf[1024];
999a4365d0SYoshinobu Inoue
1009a4365d0SYoshinobu Inoue switch (family) {
1019a4365d0SYoshinobu Inoue case PF_INET:
1029a4365d0SYoshinobu Inoue proto = IPPROTO_IP;
1039a4365d0SYoshinobu Inoue optname = IP_IPSEC_POLICY;
1049a4365d0SYoshinobu Inoue break;
1059a4365d0SYoshinobu Inoue case PF_INET6:
1069a4365d0SYoshinobu Inoue proto = IPPROTO_IPV6;
1079a4365d0SYoshinobu Inoue optname = IPV6_IPSEC_POLICY;
1089a4365d0SYoshinobu Inoue break;
1099a4365d0SYoshinobu Inoue }
1109a4365d0SYoshinobu Inoue
1119a4365d0SYoshinobu Inoue if ((so = socket(family, SOCK_DGRAM, 0)) < 0)
1129a4365d0SYoshinobu Inoue perror("socket");
1139a4365d0SYoshinobu Inoue
1149a4365d0SYoshinobu Inoue if (setsockopt(so, proto, optname, policy, PFKEY_EXTLEN(policy)) < 0)
1159a4365d0SYoshinobu Inoue perror("setsockopt");
1169a4365d0SYoshinobu Inoue
1179a4365d0SYoshinobu Inoue len = sizeof(getbuf);
1189a4365d0SYoshinobu Inoue memset(getbuf, 0, sizeof(getbuf));
1199a4365d0SYoshinobu Inoue if (getsockopt(so, proto, optname, getbuf, &len) < 0)
1209a4365d0SYoshinobu Inoue perror("getsockopt");
1219a4365d0SYoshinobu Inoue
1229a4365d0SYoshinobu Inoue {
1239a4365d0SYoshinobu Inoue char *buf = NULL;
1249a4365d0SYoshinobu Inoue
1259a4365d0SYoshinobu Inoue printf("\tgetlen:%d\n", len);
1269a4365d0SYoshinobu Inoue
1279a4365d0SYoshinobu Inoue if ((buf = ipsec_dump_policy(getbuf, NULL)) == NULL)
1289a4365d0SYoshinobu Inoue ipsec_strerror();
1299a4365d0SYoshinobu Inoue else
1309a4365d0SYoshinobu Inoue printf("\t[%s]\n", buf);
1319a4365d0SYoshinobu Inoue
1329a4365d0SYoshinobu Inoue free(buf);
1339a4365d0SYoshinobu Inoue }
1349a4365d0SYoshinobu Inoue
1359a4365d0SYoshinobu Inoue close (so);
1369a4365d0SYoshinobu Inoue }
1379a4365d0SYoshinobu Inoue
setpolicy(char * req)1389a4365d0SYoshinobu Inoue char *setpolicy(char *req)
1399a4365d0SYoshinobu Inoue {
1409a4365d0SYoshinobu Inoue int len;
1419a4365d0SYoshinobu Inoue char *buf;
1429a4365d0SYoshinobu Inoue
1439a4365d0SYoshinobu Inoue if ((len = ipsec_get_policylen(req)) < 0) {
1449a4365d0SYoshinobu Inoue printf("ipsec_get_policylen: %s\n", ipsec_strerror());
1459a4365d0SYoshinobu Inoue return NULL;
1469a4365d0SYoshinobu Inoue }
1479a4365d0SYoshinobu Inoue
1489a4365d0SYoshinobu Inoue if ((buf = malloc(len)) == NULL) {
1499a4365d0SYoshinobu Inoue perror("malloc");
1509a4365d0SYoshinobu Inoue return NULL;
1519a4365d0SYoshinobu Inoue }
1529a4365d0SYoshinobu Inoue
1539a4365d0SYoshinobu Inoue if ((len = ipsec_set_policy(buf, len, req)) < 0) {
1549a4365d0SYoshinobu Inoue printf("ipsec_set_policy: %s\n", ipsec_strerror());
1559a4365d0SYoshinobu Inoue free(buf);
1569a4365d0SYoshinobu Inoue return NULL;
1579a4365d0SYoshinobu Inoue }
1589a4365d0SYoshinobu Inoue
1599a4365d0SYoshinobu Inoue return buf;
1609a4365d0SYoshinobu Inoue }
161