ipsec.c revision 1.2 1 1.2 dsl /* $NetBSD: ipsec.c,v 1.2 2004/10/29 21:27:34 dsl Exp $ */
2 1.1 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (C) 1999 WIDE Project.
5 1.1 itojun * All rights reserved.
6 1.1 itojun *
7 1.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.1 itojun * modification, are permitted provided that the following conditions
9 1.1 itojun * are met:
10 1.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.1 itojun * documentation and/or other materials provided with the distribution.
15 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.1 itojun * may be used to endorse or promote products derived from this software
17 1.1 itojun * without specific prior written permission.
18 1.1 itojun *
19 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 itojun * SUCH DAMAGE.
30 1.1 itojun */
31 1.1 itojun
32 1.1 itojun #include <sys/param.h>
33 1.1 itojun #include <sys/stat.h>
34 1.1 itojun #include <sys/socket.h>
35 1.1 itojun
36 1.1 itojun #include <netinet/in.h>
37 1.1 itojun #include <arpa/inet.h>
38 1.1 itojun
39 1.1 itojun #include <stdio.h>
40 1.1 itojun #include <stdlib.h>
41 1.1 itojun #include <string.h>
42 1.1 itojun #include <unistd.h>
43 1.1 itojun #include <ctype.h>
44 1.1 itojun
45 1.1 itojun #ifdef IPSEC
46 1.1 itojun #include <netinet6/ipsec.h>
47 1.1 itojun #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
48 1.1 itojun #undef IPSEC
49 1.1 itojun #endif
50 1.1 itojun #endif
51 1.1 itojun
52 1.1 itojun #include "ipsec.h"
53 1.1 itojun
54 1.1 itojun #ifdef IPSEC
55 1.1 itojun int
56 1.1 itojun ipsecsetup(af, fd, policy)
57 1.1 itojun int af;
58 1.1 itojun int fd;
59 1.1 itojun const char *policy;
60 1.1 itojun {
61 1.1 itojun char *p0, *p;
62 1.1 itojun int error;
63 1.1 itojun
64 1.1 itojun if (!policy || policy == '\0')
65 1.1 itojun p0 = p = strdup("in entrust; out entrust");
66 1.1 itojun else
67 1.1 itojun p0 = p = strdup(policy);
68 1.1 itojun
69 1.1 itojun error = 0;
70 1.1 itojun while (1) {
71 1.1 itojun p = strtok(p, ";");
72 1.1 itojun if (p == NULL)
73 1.1 itojun break;
74 1.2 dsl while (*p && isspace((unsigned char)*p))
75 1.1 itojun p++;
76 1.1 itojun if (!*p) {
77 1.1 itojun p = NULL;
78 1.1 itojun continue;
79 1.1 itojun }
80 1.1 itojun error = ipsecsetup0(af, fd, p, 1);
81 1.1 itojun if (error < 0)
82 1.1 itojun break;
83 1.1 itojun p = NULL;
84 1.1 itojun }
85 1.1 itojun
86 1.1 itojun free(p0);
87 1.1 itojun return error;
88 1.1 itojun }
89 1.1 itojun
90 1.1 itojun int
91 1.1 itojun ipsecsetup_test(policy)
92 1.1 itojun const char *policy;
93 1.1 itojun {
94 1.1 itojun char *p0, *p;
95 1.1 itojun char *buf;
96 1.1 itojun int error;
97 1.1 itojun
98 1.1 itojun if (!policy)
99 1.1 itojun return -1;
100 1.1 itojun p0 = p = strdup((char *)policy);
101 1.1 itojun
102 1.1 itojun error = 0;
103 1.1 itojun while (1) {
104 1.1 itojun p = strtok(p, ";");
105 1.1 itojun if (p == NULL)
106 1.1 itojun break;
107 1.2 dsl while (*p && isspace((unsigned char)*p))
108 1.1 itojun p++;
109 1.1 itojun if (!*p) {
110 1.1 itojun p = NULL;
111 1.1 itojun continue;
112 1.1 itojun }
113 1.1 itojun buf = ipsec_set_policy((char *)p, strlen(p));
114 1.1 itojun if (buf == NULL) {
115 1.1 itojun error = -1;
116 1.1 itojun break;
117 1.1 itojun }
118 1.1 itojun free(buf);
119 1.1 itojun p = NULL;
120 1.1 itojun }
121 1.1 itojun
122 1.1 itojun free(p0);
123 1.1 itojun return error;
124 1.1 itojun }
125 1.1 itojun
126 1.1 itojun int
127 1.1 itojun ipsecsetup0(af, fd, policy, commit)
128 1.1 itojun int af;
129 1.1 itojun int fd;
130 1.1 itojun const char *policy;
131 1.1 itojun int commit;
132 1.1 itojun {
133 1.1 itojun int level;
134 1.1 itojun int opt;
135 1.1 itojun char *buf;
136 1.1 itojun int error;
137 1.1 itojun
138 1.1 itojun switch (af) {
139 1.1 itojun case AF_INET:
140 1.1 itojun level = IPPROTO_IP;
141 1.1 itojun opt = IP_IPSEC_POLICY;
142 1.1 itojun break;
143 1.1 itojun #ifdef INET6
144 1.1 itojun case AF_INET6:
145 1.1 itojun level = IPPROTO_IPV6;
146 1.1 itojun opt = IPV6_IPSEC_POLICY;
147 1.1 itojun break;
148 1.1 itojun #endif
149 1.1 itojun default:
150 1.1 itojun return -1;
151 1.1 itojun }
152 1.1 itojun
153 1.1 itojun buf = ipsec_set_policy((char *)policy, strlen(policy));
154 1.1 itojun if (buf != NULL) {
155 1.1 itojun error = 0;
156 1.1 itojun if (commit && setsockopt(fd, level, opt,
157 1.1 itojun buf, ipsec_get_policylen(buf)) < 0) {
158 1.1 itojun error = -1;
159 1.1 itojun }
160 1.1 itojun free(buf);
161 1.1 itojun } else
162 1.1 itojun error = -1;
163 1.1 itojun return error;
164 1.1 itojun }
165 1.1 itojun #endif
166