ipsec_netbsd.c revision 1.3 1 1.2 itojun /* $NetBSD: ipsec_netbsd.c,v 1.3 2003/10/06 22:05:15 tls Exp $ */
2 1.1 jonathan /* $KAME: esp_input.c,v 1.60 2001/09/04 08:43:19 itojun Exp $ */
3 1.1 jonathan /* $KAME: ah_input.c,v 1.64 2001/09/04 08:43:19 itojun Exp $ */
4 1.1 jonathan
5 1.1 jonathan /*
6 1.1 jonathan * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 1.1 jonathan * All rights reserved.
8 1.1 jonathan *
9 1.1 jonathan * Redistribution and use in source and binary forms, with or without
10 1.1 jonathan * modification, are permitted provided that the following conditions
11 1.1 jonathan * are met:
12 1.1 jonathan * 1. Redistributions of source code must retain the above copyright
13 1.1 jonathan * notice, this list of conditions and the following disclaimer.
14 1.1 jonathan * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jonathan * notice, this list of conditions and the following disclaimer in the
16 1.1 jonathan * documentation and/or other materials provided with the distribution.
17 1.1 jonathan * 3. Neither the name of the project nor the names of its contributors
18 1.1 jonathan * may be used to endorse or promote products derived from this software
19 1.1 jonathan * without specific prior written permission.
20 1.1 jonathan *
21 1.1 jonathan * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 1.1 jonathan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 jonathan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 jonathan * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 1.1 jonathan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 jonathan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 jonathan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 jonathan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 jonathan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 jonathan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 jonathan * SUCH DAMAGE.
32 1.1 jonathan */
33 1.1 jonathan
34 1.1 jonathan #include <sys/cdefs.h>
35 1.2 itojun __KERNEL_RCSID(0, "$NetBSD: ipsec_netbsd.c,v 1.3 2003/10/06 22:05:15 tls Exp $");
36 1.1 jonathan
37 1.1 jonathan #include "opt_inet.h"
38 1.1 jonathan #include "opt_ipsec.h"
39 1.1 jonathan
40 1.1 jonathan #include <sys/param.h>
41 1.1 jonathan #include <sys/systm.h>
42 1.1 jonathan #include <sys/malloc.h>
43 1.1 jonathan #include <sys/mbuf.h>
44 1.1 jonathan #include <sys/domain.h>
45 1.1 jonathan #include <sys/protosw.h>
46 1.1 jonathan #include <sys/socket.h>
47 1.1 jonathan #include <sys/errno.h>
48 1.1 jonathan #include <sys/time.h>
49 1.1 jonathan #include <sys/kernel.h>
50 1.1 jonathan #include <sys/sysctl.h>
51 1.1 jonathan
52 1.1 jonathan #include <net/if.h>
53 1.1 jonathan #include <net/route.h>
54 1.1 jonathan #include <net/netisr.h>
55 1.1 jonathan #include <machine/cpu.h>
56 1.1 jonathan
57 1.1 jonathan #include <netinet/in.h>
58 1.1 jonathan #include <netinet/in_systm.h>
59 1.1 jonathan #include <netinet/in_var.h>
60 1.1 jonathan #include <netinet/ip.h>
61 1.1 jonathan #include <netinet/ip_var.h>
62 1.1 jonathan #include <netinet/ip_ecn.h>
63 1.1 jonathan #include <netinet/ip_icmp.h>
64 1.1 jonathan
65 1.1 jonathan #ifdef IPSEC
66 1.1 jonathan #include <netkey/key.h>
67 1.1 jonathan #include <netkey/keydb.h>
68 1.1 jonathan #include <netkey/key_debug.h>
69 1.1 jonathan #endif
70 1.1 jonathan
71 1.1 jonathan #ifdef FAST_IPSEC
72 1.1 jonathan #include <netipsec/ipsec.h>
73 1.3 tls #include <netipsec/key.h>
74 1.3 tls #include <netipsec/keydb.h>
75 1.3 tls #include <netipsec/key_debug.h>
76 1.1 jonathan #include <netipsec/ah_var.h>
77 1.1 jonathan #endif
78 1.1 jonathan
79 1.1 jonathan
80 1.1 jonathan #include <machine/stdarg.h>
81 1.1 jonathan
82 1.1 jonathan
83 1.1 jonathan
84 1.3 tls #include <netipsec/key.h>
85 1.1 jonathan
86 1.1 jonathan /* assumes that ip header and ah header are contiguous on mbuf */
87 1.1 jonathan void *
88 1.1 jonathan ah4_ctlinput(cmd, sa, v)
89 1.1 jonathan int cmd;
90 1.1 jonathan struct sockaddr *sa;
91 1.1 jonathan void *v;
92 1.1 jonathan {
93 1.1 jonathan struct ip *ip = v;
94 1.1 jonathan struct ah *ah;
95 1.1 jonathan struct icmp *icp;
96 1.1 jonathan struct secasvar *sav;
97 1.1 jonathan
98 1.1 jonathan if (sa->sa_family != AF_INET ||
99 1.1 jonathan sa->sa_len != sizeof(struct sockaddr_in))
100 1.1 jonathan return NULL;
101 1.1 jonathan if ((unsigned)cmd >= PRC_NCMDS)
102 1.1 jonathan return NULL;
103 1.1 jonathan #ifndef notyet
104 1.1 jonathan (void) ip; (void) ah; (void) icp; (void) sav;
105 1.1 jonathan #else
106 1.1 jonathan if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
107 1.1 jonathan /*
108 1.1 jonathan * Check to see if we have a valid SA corresponding to
109 1.1 jonathan * the address in the ICMP message payload.
110 1.1 jonathan */
111 1.1 jonathan ah = (struct ah *)((caddr_t)ip + (ip->ip_hl << 2));
112 1.1 jonathan if ((sav = key_allocsa(AF_INET,
113 1.1 jonathan (caddr_t) &ip->ip_src,
114 1.1 jonathan (caddr_t) &ip->ip_dst,
115 1.1 jonathan IPPROTO_AH, ah->ah_spi)) == NULL)
116 1.1 jonathan return NULL;
117 1.1 jonathan if (sav->state != SADB_SASTATE_MATURE &&
118 1.1 jonathan sav->state != SADB_SASTATE_DYING) {
119 1.1 jonathan key_freesav(sav);
120 1.1 jonathan return NULL;
121 1.1 jonathan }
122 1.1 jonathan
123 1.1 jonathan /* XXX Further validation? */
124 1.1 jonathan
125 1.1 jonathan key_freesav(sav);
126 1.1 jonathan
127 1.1 jonathan /*
128 1.1 jonathan * Now that we've validated that we are actually communicating
129 1.1 jonathan * with the host indicated in the ICMP message, locate the
130 1.1 jonathan * ICMP header, recalculate the new MTU, and create the
131 1.1 jonathan * corresponding routing entry.
132 1.1 jonathan */
133 1.1 jonathan icp = (struct icmp *)((caddr_t)ip -
134 1.1 jonathan offsetof(struct icmp, icmp_ip));
135 1.1 jonathan icmp_mtudisc(icp, ip->ip_dst);
136 1.1 jonathan
137 1.1 jonathan return NULL;
138 1.1 jonathan }
139 1.1 jonathan #endif
140 1.1 jonathan
141 1.1 jonathan return NULL;
142 1.1 jonathan }
143 1.1 jonathan
144 1.1 jonathan /* assumes that ip header and esp header are contiguous on mbuf */
145 1.1 jonathan void *
146 1.1 jonathan esp4_ctlinput(cmd, sa, v)
147 1.1 jonathan int cmd;
148 1.1 jonathan struct sockaddr *sa;
149 1.1 jonathan void *v;
150 1.1 jonathan {
151 1.1 jonathan struct ip *ip = v;
152 1.1 jonathan struct esp *esp;
153 1.1 jonathan struct icmp *icp;
154 1.1 jonathan struct secasvar *sav;
155 1.1 jonathan
156 1.1 jonathan if (sa->sa_family != AF_INET ||
157 1.1 jonathan sa->sa_len != sizeof(struct sockaddr_in))
158 1.1 jonathan return NULL;
159 1.1 jonathan if ((unsigned)cmd >= PRC_NCMDS)
160 1.1 jonathan return NULL;
161 1.1 jonathan #ifndef notyet
162 1.1 jonathan (void) ip; (void) esp; (void) icp; (void) sav;
163 1.1 jonathan #else
164 1.1 jonathan if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
165 1.1 jonathan /*
166 1.1 jonathan * Check to see if we have a valid SA corresponding to
167 1.1 jonathan * the address in the ICMP message payload.
168 1.1 jonathan */
169 1.1 jonathan esp = (struct esp *)((caddr_t)ip + (ip->ip_hl << 2));
170 1.1 jonathan if ((sav = key_allocsa(AF_INET,
171 1.1 jonathan (caddr_t) &ip->ip_src,
172 1.1 jonathan (caddr_t) &ip->ip_dst,
173 1.1 jonathan IPPROTO_ESP, esp->esp_spi)) == NULL)
174 1.1 jonathan return NULL;
175 1.1 jonathan if (sav->state != SADB_SASTATE_MATURE &&
176 1.1 jonathan sav->state != SADB_SASTATE_DYING) {
177 1.1 jonathan key_freesav(sav);
178 1.1 jonathan return NULL;
179 1.1 jonathan }
180 1.1 jonathan
181 1.1 jonathan /* XXX Further validation? */
182 1.1 jonathan
183 1.1 jonathan key_freesav(sav);
184 1.1 jonathan
185 1.1 jonathan /*
186 1.1 jonathan * Now that we've validated that we are actually communicating
187 1.1 jonathan * with the host indicated in the ICMP message, locate the
188 1.1 jonathan * ICMP header, recalculate the new MTU, and create the
189 1.1 jonathan * corresponding routing entry.
190 1.1 jonathan */
191 1.1 jonathan icp = (struct icmp *)((caddr_t)ip -
192 1.1 jonathan offsetof(struct icmp, icmp_ip));
193 1.1 jonathan icmp_mtudisc(icp, ip->ip_dst);
194 1.1 jonathan
195 1.1 jonathan return NULL;
196 1.1 jonathan }
197 1.1 jonathan #endif
198 1.1 jonathan
199 1.1 jonathan return NULL;
200 1.1 jonathan }
201 1.1 jonathan
202 1.1 jonathan #ifdef INET6
203 1.1 jonathan void
204 1.1 jonathan esp6_ctlinput(cmd, sa, d)
205 1.1 jonathan int cmd;
206 1.1 jonathan struct sockaddr *sa;
207 1.1 jonathan void *d;
208 1.1 jonathan {
209 1.1 jonathan const struct newesp *espp;
210 1.1 jonathan struct newesp esp;
211 1.1 jonathan struct ip6ctlparam *ip6cp = NULL, ip6cp1;
212 1.1 jonathan struct secasvar *sav;
213 1.1 jonathan struct ip6_hdr *ip6;
214 1.1 jonathan struct mbuf *m;
215 1.1 jonathan int off;
216 1.1 jonathan struct sockaddr_in6 *sa6_src, *sa6_dst;
217 1.1 jonathan
218 1.1 jonathan if (sa->sa_family != AF_INET6 ||
219 1.1 jonathan sa->sa_len != sizeof(struct sockaddr_in6))
220 1.1 jonathan return;
221 1.1 jonathan if ((unsigned)cmd >= PRC_NCMDS)
222 1.1 jonathan return;
223 1.1 jonathan
224 1.1 jonathan /* if the parameter is from icmp6, decode it. */
225 1.1 jonathan if (d != NULL) {
226 1.1 jonathan ip6cp = (struct ip6ctlparam *)d;
227 1.1 jonathan m = ip6cp->ip6c_m;
228 1.1 jonathan ip6 = ip6cp->ip6c_ip6;
229 1.1 jonathan off = ip6cp->ip6c_off;
230 1.1 jonathan } else {
231 1.1 jonathan m = NULL;
232 1.1 jonathan ip6 = NULL;
233 1.1 jonathan }
234 1.1 jonathan
235 1.1 jonathan if (ip6) {
236 1.1 jonathan /*
237 1.1 jonathan * Notify the error to all possible sockets via pfctlinput2.
238 1.1 jonathan * Since the upper layer information (such as protocol type,
239 1.1 jonathan * source and destination ports) is embedded in the encrypted
240 1.1 jonathan * data and might have been cut, we can't directly call
241 1.1 jonathan * an upper layer ctlinput function. However, the pcbnotify
242 1.1 jonathan * function will consider source and destination addresses
243 1.1 jonathan * as well as the flow info value, and may be able to find
244 1.1 jonathan * some PCB that should be notified.
245 1.1 jonathan * Although pfctlinput2 will call esp6_ctlinput(), there is
246 1.1 jonathan * no possibility of an infinite loop of function calls,
247 1.1 jonathan * because we don't pass the inner IPv6 header.
248 1.1 jonathan */
249 1.1 jonathan bzero(&ip6cp1, sizeof(ip6cp1));
250 1.1 jonathan ip6cp1.ip6c_src = ip6cp->ip6c_src;
251 1.1 jonathan pfctlinput2(cmd, sa, (void *)&ip6cp1);
252 1.1 jonathan
253 1.1 jonathan /*
254 1.1 jonathan * Then go to special cases that need ESP header information.
255 1.1 jonathan * XXX: We assume that when ip6 is non NULL,
256 1.1 jonathan * M and OFF are valid.
257 1.1 jonathan */
258 1.1 jonathan
259 1.1 jonathan /* check if we can safely examine src and dst ports */
260 1.1 jonathan if (m->m_pkthdr.len < off + sizeof(esp))
261 1.1 jonathan return;
262 1.1 jonathan
263 1.1 jonathan if (m->m_len < off + sizeof(esp)) {
264 1.1 jonathan /*
265 1.1 jonathan * this should be rare case,
266 1.1 jonathan * so we compromise on this copy...
267 1.1 jonathan */
268 1.1 jonathan m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
269 1.1 jonathan espp = &esp;
270 1.1 jonathan } else
271 1.1 jonathan espp = (struct newesp*)(mtod(m, caddr_t) + off);
272 1.1 jonathan
273 1.1 jonathan if (cmd == PRC_MSGSIZE) {
274 1.1 jonathan int valid = 0;
275 1.1 jonathan
276 1.1 jonathan /*
277 1.1 jonathan * Check to see if we have a valid SA corresponding to
278 1.1 jonathan * the address in the ICMP message payload.
279 1.1 jonathan */
280 1.1 jonathan sa6_src = ip6cp->ip6c_src;
281 1.1 jonathan sa6_dst = (struct sockaddr_in6 *)sa;
282 1.1 jonathan sav = key_allocsa(AF_INET6,
283 1.1 jonathan (caddr_t)&sa6_src->sin6_addr,
284 1.1 jonathan (caddr_t)&sa6_dst->sin6_addr,
285 1.1 jonathan IPPROTO_ESP, espp->esp_spi);
286 1.1 jonathan if (sav) {
287 1.1 jonathan if (sav->state == SADB_SASTATE_MATURE ||
288 1.1 jonathan sav->state == SADB_SASTATE_DYING)
289 1.1 jonathan valid++;
290 1.1 jonathan key_freesav(sav);
291 1.1 jonathan }
292 1.1 jonathan
293 1.1 jonathan /* XXX Further validation? */
294 1.1 jonathan
295 1.1 jonathan /*
296 1.1 jonathan * Depending on the value of "valid" and routing table
297 1.1 jonathan * size (mtudisc_{hi,lo}wat), we will:
298 1.1 jonathan * - recalcurate the new MTU and create the
299 1.1 jonathan * corresponding routing entry, or
300 1.1 jonathan * - ignore the MTU change notification.
301 1.1 jonathan */
302 1.1 jonathan icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
303 1.1 jonathan }
304 1.1 jonathan } else {
305 1.1 jonathan /* we normally notify any pcb here */
306 1.1 jonathan }
307 1.1 jonathan }
308 1.1 jonathan #endif /* INET6 */
309 1.1 jonathan
310 1.1 jonathan
311 1.1 jonathan /*FIXME: placebo for invalpcbcacheall. Fast-IPsec has no pcb cache? */
312 1.1 jonathan
313 1.1 jonathan void ipsec_invalpcbcacheall(void);
314 1.1 jonathan void
315 1.1 jonathan ipsec_invalpcbcacheall(void)
316 1.1 jonathan {
317 1.1 jonathan }
318 1.1 jonathan
319 1.1 jonathan /* XXX will need a different oid at parent */
320 1.1 jonathan int
321 1.1 jonathan ipsec_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
322 1.1 jonathan int *name;
323 1.1 jonathan u_int namelen;
324 1.1 jonathan void *oldp;
325 1.1 jonathan size_t *oldlenp;
326 1.1 jonathan void *newp;
327 1.1 jonathan size_t newlen;
328 1.1 jonathan {
329 1.1 jonathan /* All sysctl names at this level are terminal. */
330 1.1 jonathan if (namelen != 1)
331 1.1 jonathan return ENOTDIR;
332 1.1 jonathan
333 1.1 jonathan /* common sanity checks */
334 1.1 jonathan switch (name[0]) {
335 1.1 jonathan case IPSECCTL_DEF_ESP_TRANSLEV:
336 1.1 jonathan case IPSECCTL_DEF_ESP_NETLEV:
337 1.1 jonathan case IPSECCTL_DEF_AH_TRANSLEV:
338 1.1 jonathan case IPSECCTL_DEF_AH_NETLEV:
339 1.1 jonathan if (newp != NULL && newlen == sizeof(int)) {
340 1.1 jonathan switch (*(int *)newp) {
341 1.1 jonathan case IPSEC_LEVEL_USE:
342 1.1 jonathan case IPSEC_LEVEL_REQUIRE:
343 1.1 jonathan break;
344 1.1 jonathan default:
345 1.1 jonathan return EINVAL;
346 1.1 jonathan }
347 1.1 jonathan }
348 1.1 jonathan break;
349 1.1 jonathan case IPSECCTL_DEF_POLICY:
350 1.1 jonathan if (newp != NULL && newlen == sizeof(int)) {
351 1.1 jonathan switch (*(int *)newp) {
352 1.1 jonathan case IPSEC_POLICY_DISCARD:
353 1.1 jonathan case IPSEC_POLICY_NONE:
354 1.1 jonathan break;
355 1.1 jonathan default:
356 1.1 jonathan return EINVAL;
357 1.1 jonathan }
358 1.1 jonathan ipsec_invalpcbcacheall();
359 1.1 jonathan }
360 1.1 jonathan break;
361 1.1 jonathan }
362 1.1 jonathan
363 1.1 jonathan switch (name[0]) {
364 1.1 jonathan case IPSECCTL_STATS:
365 1.1 jonathan return sysctl_struct(oldp, oldlenp, newp, newlen,
366 1.1 jonathan &ipsecstat, sizeof(ipsecstat));
367 1.1 jonathan case IPSECCTL_DEF_POLICY:
368 1.1 jonathan return sysctl_int(oldp, oldlenp, newp, newlen,
369 1.1 jonathan &ip4_def_policy.policy);
370 1.1 jonathan case IPSECCTL_DEF_ESP_TRANSLEV:
371 1.1 jonathan if (newp != NULL)
372 1.1 jonathan ipsec_invalpcbcacheall();
373 1.1 jonathan return sysctl_int(oldp, oldlenp, newp, newlen,
374 1.1 jonathan &ip4_esp_trans_deflev);
375 1.1 jonathan case IPSECCTL_DEF_ESP_NETLEV:
376 1.1 jonathan if (newp != NULL)
377 1.1 jonathan ipsec_invalpcbcacheall();
378 1.1 jonathan return sysctl_int(oldp, oldlenp, newp, newlen,
379 1.1 jonathan &ip4_esp_net_deflev);
380 1.1 jonathan case IPSECCTL_DEF_AH_TRANSLEV:
381 1.1 jonathan if (newp != NULL)
382 1.1 jonathan ipsec_invalpcbcacheall();
383 1.1 jonathan return sysctl_int(oldp, oldlenp, newp, newlen,
384 1.1 jonathan &ip4_ah_trans_deflev);
385 1.1 jonathan case IPSECCTL_DEF_AH_NETLEV:
386 1.1 jonathan if (newp != NULL)
387 1.1 jonathan ipsec_invalpcbcacheall();
388 1.1 jonathan return sysctl_int(oldp, oldlenp, newp, newlen,
389 1.1 jonathan &ip4_ah_net_deflev);
390 1.1 jonathan case IPSECCTL_AH_CLEARTOS:
391 1.1 jonathan return sysctl_int(oldp, oldlenp, newp, newlen,
392 1.1 jonathan &/*ip4_*/ah_cleartos);
393 1.1 jonathan case IPSECCTL_AH_OFFSETMASK:
394 1.1 jonathan return sysctl_int(oldp, oldlenp, newp, newlen,
395 1.1 jonathan &ip4_ah_offsetmask);
396 1.1 jonathan case IPSECCTL_DFBIT:
397 1.1 jonathan return sysctl_int(oldp, oldlenp, newp, newlen,
398 1.1 jonathan &ip4_ipsec_dfbit);
399 1.1 jonathan case IPSECCTL_ECN:
400 1.1 jonathan return sysctl_int(oldp, oldlenp, newp, newlen, &ip4_ipsec_ecn);
401 1.1 jonathan case IPSECCTL_DEBUG:
402 1.1 jonathan return sysctl_int(oldp, oldlenp, newp, newlen, &ipsec_debug);
403 1.1 jonathan default:
404 1.1 jonathan return EOPNOTSUPP;
405 1.1 jonathan }
406 1.1 jonathan /* NOTREACHED */
407 1.1 jonathan }
408