1*11067b05Srillig /* $NetBSD: ipsec.c,v 1.8 2021/09/03 21:02:04 rillig Exp $ */
24b061adfSitojun
34b061adfSitojun /*
44b061adfSitojun * Copyright (C) 1999 WIDE Project.
54b061adfSitojun * All rights reserved.
64b061adfSitojun *
74b061adfSitojun * Redistribution and use in source and binary forms, with or without
84b061adfSitojun * modification, are permitted provided that the following conditions
94b061adfSitojun * are met:
104b061adfSitojun * 1. Redistributions of source code must retain the above copyright
114b061adfSitojun * notice, this list of conditions and the following disclaimer.
124b061adfSitojun * 2. Redistributions in binary form must reproduce the above copyright
134b061adfSitojun * notice, this list of conditions and the following disclaimer in the
144b061adfSitojun * documentation and/or other materials provided with the distribution.
154b061adfSitojun * 3. Neither the name of the project nor the names of its contributors
164b061adfSitojun * may be used to endorse or promote products derived from this software
174b061adfSitojun * without specific prior written permission.
184b061adfSitojun *
194b061adfSitojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
204b061adfSitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214b061adfSitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224b061adfSitojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
234b061adfSitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244b061adfSitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254b061adfSitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264b061adfSitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274b061adfSitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284b061adfSitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294b061adfSitojun * SUCH DAMAGE.
304b061adfSitojun */
314b061adfSitojun
324b061adfSitojun #include <sys/param.h>
334b061adfSitojun #include <sys/stat.h>
344b061adfSitojun #include <sys/socket.h>
354b061adfSitojun
364b061adfSitojun #include <netinet/in.h>
374b061adfSitojun #include <arpa/inet.h>
384b061adfSitojun
39adeed07fSrillig #include <stdbool.h>
404b061adfSitojun #include <stdio.h>
414b061adfSitojun #include <stdlib.h>
424b061adfSitojun #include <string.h>
434b061adfSitojun #include <unistd.h>
444b061adfSitojun #include <ctype.h>
454b061adfSitojun
464b061adfSitojun #ifdef IPSEC
478fd6dadaSdrochner #include <netipsec/ipsec.h>
484b061adfSitojun #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
494b061adfSitojun #undef IPSEC
504b061adfSitojun #endif
514b061adfSitojun #endif
524b061adfSitojun
534b061adfSitojun #include "ipsec.h"
544b061adfSitojun
554b061adfSitojun #ifdef IPSEC
564b061adfSitojun int
ipsecsetup(int af,int fd,const char * policy)578b39f71dSchristos ipsecsetup(int af, int fd, const char *policy)
584b061adfSitojun {
594b061adfSitojun char *p0, *p;
604b061adfSitojun int error;
614b061adfSitojun
62adeed07fSrillig if (policy == NULL || *policy == '\0')
634b061adfSitojun p0 = p = strdup("in entrust; out entrust");
644b061adfSitojun else
654b061adfSitojun p0 = p = strdup(policy);
664b061adfSitojun
674b061adfSitojun error = 0;
688b39f71dSchristos for (;;) {
694b061adfSitojun p = strtok(p, ";");
704b061adfSitojun if (p == NULL)
714b061adfSitojun break;
72*11067b05Srillig while (isspace((unsigned char)*p))
734b061adfSitojun p++;
74adeed07fSrillig if (*p == '\0') {
754b061adfSitojun p = NULL;
764b061adfSitojun continue;
774b061adfSitojun }
78adeed07fSrillig error = ipsecsetup0(af, fd, p, true);
794b061adfSitojun if (error < 0)
804b061adfSitojun break;
814b061adfSitojun p = NULL;
824b061adfSitojun }
834b061adfSitojun
844b061adfSitojun free(p0);
854b061adfSitojun return error;
864b061adfSitojun }
874b061adfSitojun
884b061adfSitojun int
ipsecsetup_test(const char * policy)898b39f71dSchristos ipsecsetup_test(const char *policy)
904b061adfSitojun {
914b061adfSitojun char *p0, *p;
924b061adfSitojun char *buf;
934b061adfSitojun int error;
944b061adfSitojun
95adeed07fSrillig if (policy == NULL)
964b061adfSitojun return -1;
978b39f71dSchristos p0 = p = strdup(policy);
988b39f71dSchristos if (p == NULL)
998b39f71dSchristos return -1;
1004b061adfSitojun
1014b061adfSitojun error = 0;
1028b39f71dSchristos for (;;) {
1034b061adfSitojun p = strtok(p, ";");
1044b061adfSitojun if (p == NULL)
1054b061adfSitojun break;
106*11067b05Srillig while (isspace((unsigned char)*p))
1074b061adfSitojun p++;
108adeed07fSrillig if (*p == '\0') {
1094b061adfSitojun p = NULL;
1104b061adfSitojun continue;
1114b061adfSitojun }
1128b39f71dSchristos buf = ipsec_set_policy(p, (int)strlen(p));
1134b061adfSitojun if (buf == NULL) {
1144b061adfSitojun error = -1;
1154b061adfSitojun break;
1164b061adfSitojun }
1174b061adfSitojun free(buf);
1184b061adfSitojun p = NULL;
1194b061adfSitojun }
1204b061adfSitojun
1214b061adfSitojun free(p0);
1224b061adfSitojun return error;
1234b061adfSitojun }
1244b061adfSitojun
1254b061adfSitojun int
ipsecsetup0(int af,int fd,const char * policy,int commit)1268b39f71dSchristos ipsecsetup0(int af, int fd, const char *policy, int commit)
1274b061adfSitojun {
1284b061adfSitojun int level;
1294b061adfSitojun int opt;
1304b061adfSitojun char *buf;
1314b061adfSitojun int error;
1324b061adfSitojun
1334b061adfSitojun switch (af) {
1344b061adfSitojun case AF_INET:
1354b061adfSitojun level = IPPROTO_IP;
1364b061adfSitojun opt = IP_IPSEC_POLICY;
1374b061adfSitojun break;
1384b061adfSitojun #ifdef INET6
1394b061adfSitojun case AF_INET6:
1404b061adfSitojun level = IPPROTO_IPV6;
1414b061adfSitojun opt = IPV6_IPSEC_POLICY;
1424b061adfSitojun break;
1434b061adfSitojun #endif
1444b061adfSitojun default:
1454b061adfSitojun return -1;
1464b061adfSitojun }
1474b061adfSitojun
1488b39f71dSchristos buf = ipsec_set_policy(policy, (int)strlen(policy));
1494b061adfSitojun if (buf != NULL) {
1504b061adfSitojun error = 0;
1514b061adfSitojun if (commit && setsockopt(fd, level, opt,
1528b39f71dSchristos buf, (socklen_t)ipsec_get_policylen(buf)) < 0) {
1534b061adfSitojun error = -1;
1544b061adfSitojun }
1554b061adfSitojun free(buf);
1564b061adfSitojun } else
1574b061adfSitojun error = -1;
1584b061adfSitojun return error;
1594b061adfSitojun }
1604b061adfSitojun #endif
161