2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $FreeBSD: src/sys/netinet6/ipsec.c,v 1.3.2.7 2001/07/19 06:37:23 kris Exp $ */
30 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * IPsec controller part.
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/malloc.h>
69 #include <sys/domain.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/errno.h>
75 #include <sys/kernel.h>
76 #include <sys/syslog.h>
77 #include <sys/sysctl.h>
78 #include <kern/locks.h>
79 #include <sys/kauth.h>
80 #include <libkern/OSAtomic.h>
83 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/in_var.h>
90 #include <netinet/udp.h>
91 #include <netinet/udp_var.h>
92 #include <netinet/ip_ecn.h>
94 #include <netinet6/ip6_ecn.h>
96 #include <netinet/tcp.h>
97 #include <netinet/udp.h>
99 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
103 #include <netinet/in_pcb.h>
105 #include <netinet/icmp6.h>
108 #include <netinet6/ipsec.h>
110 #include <netinet6/ipsec6.h>
112 #include <netinet6/ah.h>
114 #include <netinet6/ah6.h>
117 #include <netinet6/esp.h>
119 #include <netinet6/esp6.h>
122 #include <netinet6/ipcomp.h>
124 #include <netinet6/ipcomp6.h>
126 #include <netkey/key.h>
127 #include <netkey/keydb.h>
128 #include <netkey/key_debug.h>
130 #include <net/net_osdep.h>
138 #include <sys/kdebug.h>
139 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
140 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
141 #define DBG_FNC_GETPOL_SOCK NETDBG_CODE(DBG_NETIPSEC, (1 << 8))
142 #define DBG_FNC_GETPOL_ADDR NETDBG_CODE(DBG_NETIPSEC, (2 << 8))
143 #define DBG_FNC_IPSEC_OUT NETDBG_CODE(DBG_NETIPSEC, (3 << 8))
145 extern lck_mtx_t
*sadb_mutex
;
147 struct ipsecstat ipsecstat
;
148 int ip4_ah_cleartos
= 1;
149 int ip4_ah_offsetmask
= 0; /* maybe IP_DF? */
150 int ip4_ipsec_dfbit
= 0; /* DF bit on encap. 0: clear 1: set 2: copy */
151 int ip4_esp_trans_deflev
= IPSEC_LEVEL_USE
;
152 int ip4_esp_net_deflev
= IPSEC_LEVEL_USE
;
153 int ip4_ah_trans_deflev
= IPSEC_LEVEL_USE
;
154 int ip4_ah_net_deflev
= IPSEC_LEVEL_USE
;
155 struct secpolicy ip4_def_policy
;
156 int ip4_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
157 int ip4_esp_randpad
= -1;
158 int esp_udp_encap_port
= 0;
159 static int sysctl_def_policy SYSCTL_HANDLER_ARGS
;
160 extern int natt_keepalive_interval
;
161 extern u_int32_t natt_now
;
165 SYSCTL_DECL(_net_inet_ipsec
);
167 SYSCTL_DECL(_net_inet6_ipsec6
);
170 SYSCTL_STRUCT(_net_inet_ipsec
, IPSECCTL_STATS
,
171 stats
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &ipsecstat
, ipsecstat
, "");
172 SYSCTL_PROC(_net_inet_ipsec
, IPSECCTL_DEF_POLICY
, def_policy
, CTLTYPE_INT
|CTLFLAG_RW
| CTLFLAG_LOCKED
,
173 &ip4_def_policy
.policy
, 0, &sysctl_def_policy
, "I", "");
174 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
175 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip4_esp_trans_deflev
, 0, "");
176 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
177 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip4_esp_net_deflev
, 0, "");
178 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
179 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip4_ah_trans_deflev
, 0, "");
180 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
181 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip4_ah_net_deflev
, 0, "");
182 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_CLEARTOS
,
183 ah_cleartos
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip4_ah_cleartos
, 0, "");
184 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_OFFSETMASK
,
185 ah_offsetmask
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip4_ah_offsetmask
, 0, "");
186 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DFBIT
,
187 dfbit
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip4_ipsec_dfbit
, 0, "");
188 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ECN
,
189 ecn
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip4_ipsec_ecn
, 0, "");
190 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEBUG
,
191 debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &ipsec_debug
, 0, "");
192 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ESP_RANDPAD
,
193 esp_randpad
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip4_esp_randpad
, 0, "");
195 /* for performance, we bypass ipsec until a security policy is set */
196 int ipsec_bypass
= 1;
197 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, bypass
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &ipsec_bypass
,0, "");
200 * NAT Traversal requires a UDP port for encapsulation,
201 * esp_udp_encap_port controls which port is used. Racoon
202 * must set this port to the port racoon is using locally
205 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, esp_port
,
206 CTLFLAG_RW
| CTLFLAG_LOCKED
, &esp_udp_encap_port
, 0, "");
209 struct ipsecstat ipsec6stat
;
210 int ip6_esp_trans_deflev
= IPSEC_LEVEL_USE
;
211 int ip6_esp_net_deflev
= IPSEC_LEVEL_USE
;
212 int ip6_ah_trans_deflev
= IPSEC_LEVEL_USE
;
213 int ip6_ah_net_deflev
= IPSEC_LEVEL_USE
;
214 struct secpolicy ip6_def_policy
;
215 int ip6_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
216 int ip6_esp_randpad
= -1;
218 /* net.inet6.ipsec6 */
219 SYSCTL_STRUCT(_net_inet6_ipsec6
, IPSECCTL_STATS
,
220 stats
, CTLFLAG_RD
| CTLFLAG_LOCKED
, &ipsec6stat
, ipsecstat
, "");
221 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_POLICY
,
222 def_policy
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_def_policy
.policy
, 0, "");
223 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
224 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_esp_trans_deflev
, 0, "");
225 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
226 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_esp_net_deflev
, 0, "");
227 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
228 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_ah_trans_deflev
, 0, "");
229 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
230 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_ah_net_deflev
, 0, "");
231 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ECN
,
232 ecn
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_ipsec_ecn
, 0, "");
233 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEBUG
,
234 debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &ipsec_debug
, 0, "");
235 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ESP_RANDPAD
,
236 esp_randpad
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &ip6_esp_randpad
, 0, "");
239 static int ipsec_setspidx_mbuf(struct secpolicyindex
*, u_int
, u_int
,
241 static int ipsec4_setspidx_inpcb(struct mbuf
*, struct inpcb
*pcb
);
243 static int ipsec6_setspidx_in6pcb(struct mbuf
*, struct in6pcb
*pcb
);
245 static int ipsec_setspidx(struct mbuf
*, struct secpolicyindex
*, int);
246 static void ipsec4_get_ulp(struct mbuf
*m
, struct secpolicyindex
*, int);
247 static int ipsec4_setspidx_ipaddr(struct mbuf
*, struct secpolicyindex
*);
249 static void ipsec6_get_ulp(struct mbuf
*m
, struct secpolicyindex
*, int);
250 static int ipsec6_setspidx_ipaddr(struct mbuf
*, struct secpolicyindex
*);
252 static struct inpcbpolicy
*ipsec_newpcbpolicy(void);
253 static void ipsec_delpcbpolicy(struct inpcbpolicy
*);
254 static struct secpolicy
*ipsec_deepcopy_policy(struct secpolicy
*src
);
255 static int ipsec_set_policy(struct secpolicy
**pcb_sp
,
256 int optname
, caddr_t request
, size_t len
, int priv
);
257 static int ipsec_get_policy(struct secpolicy
*pcb_sp
, struct mbuf
**mp
);
258 static void vshiftl(unsigned char *, int, int);
259 static int ipsec_in_reject(struct secpolicy
*, struct mbuf
*);
261 static struct mbuf
*ipsec4_splithdr(struct mbuf
*);
264 static struct mbuf
*ipsec6_splithdr(struct mbuf
*);
267 static int ipsec4_encapsulate(struct mbuf
*, struct secasvar
*);
270 static int ipsec6_encapsulate(struct mbuf
*, struct secasvar
*);
271 static int ipsec64_encapsulate(struct mbuf
*, struct secasvar
*);
273 static struct ipsec_tag
*ipsec_addaux(struct mbuf
*);
274 static struct ipsec_tag
*ipsec_findaux(struct mbuf
*);
275 static void ipsec_optaux(struct mbuf
*, struct ipsec_tag
*);
276 int ipsec_send_natt_keepalive(struct secasvar
*sav
);
279 sysctl_def_policy SYSCTL_HANDLER_ARGS
281 int old_policy
= ip4_def_policy
.policy
;
282 int error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
284 #pragma unused(arg1, arg2)
286 if (ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
&&
287 ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
) {
288 ip4_def_policy
.policy
= old_policy
;
292 /* Turn off the bypass if the default security policy changes */
293 if (ipsec_bypass
!= 0 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
)
300 * For OUTBOUND packet having a socket. Searching SPD for packet,
301 * and return a pointer to SP.
302 * OUT: NULL: no apropreate SP found, the following value is set to error.
304 * EACCES : discard packet.
305 * ENOENT : ipsec_acquire() in progress, maybe.
306 * others : error occurred.
307 * others: a pointer to SP
309 * NOTE: IPv6 mapped adddress concern is implemented here.
312 ipsec4_getpolicybysock(m
, dir
, so
, error
)
318 struct inpcbpolicy
*pcbsp
= NULL
;
319 struct secpolicy
*currsp
= NULL
; /* policy on socket */
320 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
322 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
324 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
325 panic("ipsec4_getpolicybysock: NULL pointer was passed.\n");
327 if (so
->so_pcb
== NULL
) {
328 printf("ipsec4_getpolicybysock: so->so_pcb == NULL\n");
329 return ipsec4_getpolicybyaddr(m
, dir
, 0, error
);
332 switch (so
->so_proto
->pr_domain
->dom_family
) {
334 pcbsp
= sotoinpcb(so
)->inp_sp
;
338 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
344 /* Socket has not specified an IPSEC policy */
345 return ipsec4_getpolicybyaddr(m
, dir
, 0, error
);
348 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_START
, 0,0,0,0,0);
350 switch (so
->so_proto
->pr_domain
->dom_family
) {
352 /* set spidx in pcb */
353 *error
= ipsec4_setspidx_inpcb(m
, sotoinpcb(so
));
357 /* set spidx in pcb */
358 *error
= ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
362 panic("ipsec4_getpolicybysock: unsupported address family\n");
365 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 1,*error
,0,0,0);
371 panic("ipsec4_getpolicybysock: pcbsp is NULL.\n");
374 case IPSEC_DIR_INBOUND
:
375 currsp
= pcbsp
->sp_in
;
377 case IPSEC_DIR_OUTBOUND
:
378 currsp
= pcbsp
->sp_out
;
381 panic("ipsec4_getpolicybysock: illegal direction.\n");
386 panic("ipsec4_getpolicybysock: currsp is NULL.\n");
388 /* when privilieged socket */
390 switch (currsp
->policy
) {
391 case IPSEC_POLICY_BYPASS
:
392 lck_mtx_lock(sadb_mutex
);
394 lck_mtx_unlock(sadb_mutex
);
396 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 2,*error
,0,0,0);
399 case IPSEC_POLICY_ENTRUST
:
400 /* look for a policy in SPD */
401 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
404 if (kernsp
!= NULL
) {
405 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
406 printf("DP ipsec4_getpolicybysock called "
407 "to allocate SP:%p\n", kernsp
));
409 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 3,*error
,0,0,0);
414 lck_mtx_lock(sadb_mutex
);
415 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
416 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
418 "fixed system default policy: %d->%d\n",
419 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
420 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
422 ip4_def_policy
.refcnt
++;
423 lck_mtx_unlock(sadb_mutex
);
425 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 4,*error
,0,0,0);
426 return &ip4_def_policy
;
428 case IPSEC_POLICY_IPSEC
:
429 lck_mtx_lock(sadb_mutex
);
431 lck_mtx_unlock(sadb_mutex
);
433 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 5,*error
,0,0,0);
437 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
438 "Invalid policy for PCB %d\n", currsp
->policy
));
440 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 6,*error
,0,0,0);
446 /* when non-privilieged socket */
447 /* look for a policy in SPD */
448 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
451 if (kernsp
!= NULL
) {
452 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
453 printf("DP ipsec4_getpolicybysock called "
454 "to allocate SP:%p\n", kernsp
));
456 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 7,*error
,0,0,0);
461 switch (currsp
->policy
) {
462 case IPSEC_POLICY_BYPASS
:
463 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
464 "Illegal policy for non-priviliged defined %d\n",
467 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 8,*error
,0,0,0);
470 case IPSEC_POLICY_ENTRUST
:
471 lck_mtx_lock(sadb_mutex
);
472 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
473 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
475 "fixed system default policy: %d->%d\n",
476 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
477 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
479 ip4_def_policy
.refcnt
++;
480 lck_mtx_unlock(sadb_mutex
);
482 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 9,*error
,0,0,0);
483 return &ip4_def_policy
;
485 case IPSEC_POLICY_IPSEC
:
486 lck_mtx_lock(sadb_mutex
);
488 lck_mtx_unlock(sadb_mutex
);
490 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 10,*error
,0,0,0);
494 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
495 "Invalid policy for PCB %d\n", currsp
->policy
));
497 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 11,*error
,0,0,0);
504 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
505 * and return a pointer to SP.
506 * OUT: positive: a pointer to the entry for security policy leaf matched.
507 * NULL: no apropreate SP found, the following value is set to error.
509 * EACCES : discard packet.
510 * ENOENT : ipsec_acquire() in progress, maybe.
511 * others : error occurred.
514 ipsec4_getpolicybyaddr(m
, dir
, flag
, error
)
520 struct secpolicy
*sp
= NULL
;
522 if (ipsec_bypass
!= 0)
525 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
528 if (m
== NULL
|| error
== NULL
)
529 panic("ipsec4_getpolicybyaddr: NULL pointer was passed.\n");
532 struct secpolicyindex spidx
;
534 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_START
, 0,0,0,0,0);
535 bzero(&spidx
, sizeof(spidx
));
537 /* make a index to look for a policy */
538 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET
, m
,
539 (flag
& IP_FORWARDING
) ? 0 : 1);
542 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 1,*error
,0,0,0);
546 sp
= key_allocsp(&spidx
, dir
);
551 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
552 printf("DP ipsec4_getpolicybyaddr called "
553 "to allocate SP:%p\n", sp
));
555 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 2,*error
,0,0,0);
560 lck_mtx_lock(sadb_mutex
);
561 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
562 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
563 ipseclog((LOG_INFO
, "fixed system default policy:%d->%d\n",
564 ip4_def_policy
.policy
,
566 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
568 ip4_def_policy
.refcnt
++;
569 lck_mtx_unlock(sadb_mutex
);
571 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 3,*error
,0,0,0);
572 return &ip4_def_policy
;
577 * For OUTBOUND packet having a socket. Searching SPD for packet,
578 * and return a pointer to SP.
579 * OUT: NULL: no apropreate SP found, the following value is set to error.
581 * EACCES : discard packet.
582 * ENOENT : ipsec_acquire() in progress, maybe.
583 * others : error occurred.
584 * others: a pointer to SP
587 ipsec6_getpolicybysock(m
, dir
, so
, error
)
593 struct inpcbpolicy
*pcbsp
= NULL
;
594 struct secpolicy
*currsp
= NULL
; /* policy on socket */
595 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
597 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
600 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
601 panic("ipsec6_getpolicybysock: NULL pointer was passed.\n");
604 if (so
->so_proto
->pr_domain
->dom_family
!= AF_INET6
)
605 panic("ipsec6_getpolicybysock: socket domain != inet6\n");
608 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
611 return ipsec6_getpolicybyaddr(m
, dir
, 0, error
);
614 /* set spidx in pcb */
615 ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
619 panic("ipsec6_getpolicybysock: pcbsp is NULL.\n");
622 case IPSEC_DIR_INBOUND
:
623 currsp
= pcbsp
->sp_in
;
625 case IPSEC_DIR_OUTBOUND
:
626 currsp
= pcbsp
->sp_out
;
629 panic("ipsec6_getpolicybysock: illegal direction.\n");
634 panic("ipsec6_getpolicybysock: currsp is NULL.\n");
636 /* when privilieged socket */
638 switch (currsp
->policy
) {
639 case IPSEC_POLICY_BYPASS
:
640 lck_mtx_lock(sadb_mutex
);
642 lck_mtx_unlock(sadb_mutex
);
646 case IPSEC_POLICY_ENTRUST
:
647 /* look for a policy in SPD */
648 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
651 if (kernsp
!= NULL
) {
652 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
653 printf("DP ipsec6_getpolicybysock called "
654 "to allocate SP:%p\n", kernsp
));
660 lck_mtx_lock(sadb_mutex
);
661 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
662 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
664 "fixed system default policy: %d->%d\n",
665 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
666 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
668 ip6_def_policy
.refcnt
++;
669 lck_mtx_unlock(sadb_mutex
);
671 return &ip6_def_policy
;
673 case IPSEC_POLICY_IPSEC
:
674 lck_mtx_lock(sadb_mutex
);
676 lck_mtx_unlock(sadb_mutex
);
681 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
682 "Invalid policy for PCB %d\n", currsp
->policy
));
689 /* when non-privilieged socket */
690 /* look for a policy in SPD */
691 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
694 if (kernsp
!= NULL
) {
695 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
696 printf("DP ipsec6_getpolicybysock called "
697 "to allocate SP:%p\n", kernsp
));
703 switch (currsp
->policy
) {
704 case IPSEC_POLICY_BYPASS
:
705 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
706 "Illegal policy for non-priviliged defined %d\n",
711 case IPSEC_POLICY_ENTRUST
:
712 lck_mtx_lock(sadb_mutex
);
713 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
714 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
716 "fixed system default policy: %d->%d\n",
717 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
718 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
720 ip6_def_policy
.refcnt
++;
721 lck_mtx_unlock(sadb_mutex
);
723 return &ip6_def_policy
;
725 case IPSEC_POLICY_IPSEC
:
726 lck_mtx_lock(sadb_mutex
);
728 lck_mtx_unlock(sadb_mutex
);
734 "ipsec6_policybysock: Invalid policy for PCB %d\n",
743 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
744 * and return a pointer to SP.
745 * `flag' means that packet is to be forwarded whether or not.
747 * OUT: positive: a pointer to the entry for security policy leaf matched.
748 * NULL: no apropreate SP found, the following value is set to error.
750 * EACCES : discard packet.
751 * ENOENT : ipsec_acquire() in progress, maybe.
752 * others : error occurred.
754 #ifndef IP_FORWARDING
755 #define IP_FORWARDING 1
759 ipsec6_getpolicybyaddr(m
, dir
, flag
, error
)
765 struct secpolicy
*sp
= NULL
;
767 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
770 if (m
== NULL
|| error
== NULL
)
771 panic("ipsec6_getpolicybyaddr: NULL pointer was passed.\n");
774 struct secpolicyindex spidx
;
776 bzero(&spidx
, sizeof(spidx
));
778 /* make a index to look for a policy */
779 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET6
, m
,
780 (flag
& IP_FORWARDING
) ? 0 : 1);
785 sp
= key_allocsp(&spidx
, dir
);
790 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
791 printf("DP ipsec6_getpolicybyaddr called "
792 "to allocate SP:%p\n", sp
));
798 lck_mtx_lock(sadb_mutex
);
799 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
800 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
801 ipseclog((LOG_INFO
, "fixed system default policy: %d->%d\n",
802 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
803 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
805 ip6_def_policy
.refcnt
++;
806 lck_mtx_unlock(sadb_mutex
);
808 return &ip6_def_policy
;
813 * set IP address into spidx from mbuf.
814 * When Forwarding packet and ICMP echo reply, this function is used.
816 * IN: get the followings from mbuf.
817 * protocol family, src, dst, next protocol
820 * other: failure, and set errno.
824 struct secpolicyindex
*spidx
,
826 __unused u_int family
,
833 if (spidx
== NULL
|| m
== NULL
)
834 panic("ipsec_setspidx_mbuf: NULL pointer was passed.\n");
836 bzero(spidx
, sizeof(*spidx
));
838 error
= ipsec_setspidx(m
, spidx
, needport
);
847 bzero(spidx
, sizeof(*spidx
));
852 ipsec4_setspidx_inpcb(m
, pcb
)
856 struct secpolicyindex
*spidx
;
859 if (ipsec_bypass
!= 0)
864 panic("ipsec4_setspidx_inpcb: no PCB found.\n");
865 if (pcb
->inp_sp
== NULL
)
866 panic("ipsec4_setspidx_inpcb: no inp_sp found.\n");
867 if (pcb
->inp_sp
->sp_out
== NULL
|| pcb
->inp_sp
->sp_in
== NULL
)
868 panic("ipsec4_setspidx_inpcb: no sp_in/out found.\n");
870 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
871 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
873 spidx
= &pcb
->inp_sp
->sp_in
->spidx
;
874 error
= ipsec_setspidx(m
, spidx
, 1);
877 spidx
->dir
= IPSEC_DIR_INBOUND
;
879 spidx
= &pcb
->inp_sp
->sp_out
->spidx
;
880 error
= ipsec_setspidx(m
, spidx
, 1);
883 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
888 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
889 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
895 ipsec6_setspidx_in6pcb(m
, pcb
)
899 struct secpolicyindex
*spidx
;
904 panic("ipsec6_setspidx_in6pcb: no PCB found.\n");
905 if (pcb
->in6p_sp
== NULL
)
906 panic("ipsec6_setspidx_in6pcb: no in6p_sp found.\n");
907 if (pcb
->in6p_sp
->sp_out
== NULL
|| pcb
->in6p_sp
->sp_in
== NULL
)
908 panic("ipsec6_setspidx_in6pcb: no sp_in/out found.\n");
910 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
911 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
913 spidx
= &pcb
->in6p_sp
->sp_in
->spidx
;
914 error
= ipsec_setspidx(m
, spidx
, 1);
917 spidx
->dir
= IPSEC_DIR_INBOUND
;
919 spidx
= &pcb
->in6p_sp
->sp_out
->spidx
;
920 error
= ipsec_setspidx(m
, spidx
, 1);
923 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
928 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
929 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
935 * configure security policy index (src/dst/proto/sport/dport)
936 * by looking at the content of mbuf.
937 * the caller is responsible for error recovery (like clearing up spidx).
940 ipsec_setspidx(m
, spidx
, needport
)
942 struct secpolicyindex
*spidx
;
945 struct ip
*ip
= NULL
;
953 panic("ipsec_setspidx: m == 0 passed.\n");
956 * validate m->m_pkthdr.len. we see incorrect length if we
957 * mistakenly call this function with inconsistent mbuf chain
958 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
961 for (n
= m
; n
; n
= n
->m_next
)
963 if (m
->m_pkthdr
.len
!= len
) {
964 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
965 printf("ipsec_setspidx: "
966 "total of m_len(%d) != pkthdr.len(%d), "
968 len
, m
->m_pkthdr
.len
));
972 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
973 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
974 printf("ipsec_setspidx: "
975 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
980 if (m
->m_len
>= sizeof(*ip
))
981 ip
= mtod(m
, struct ip
*);
983 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
987 v
= _IP_VHL_V(ip
->ip_vhl
);
993 error
= ipsec4_setspidx_ipaddr(m
, spidx
);
996 ipsec4_get_ulp(m
, spidx
, needport
);
1000 if (m
->m_pkthdr
.len
< sizeof(struct ip6_hdr
)) {
1001 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1002 printf("ipsec_setspidx: "
1003 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
1004 "ignored.\n", m
->m_pkthdr
.len
));
1007 error
= ipsec6_setspidx_ipaddr(m
, spidx
);
1010 ipsec6_get_ulp(m
, spidx
, needport
);
1014 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1015 printf("ipsec_setspidx: "
1016 "unknown IP version %u, ignored.\n", v
));
1022 ipsec4_get_ulp(m
, spidx
, needport
)
1024 struct secpolicyindex
*spidx
;
1028 struct ip6_ext ip6e
;
1036 panic("ipsec4_get_ulp: NULL pointer was passed.\n");
1037 if (m
->m_pkthdr
.len
< sizeof(ip
))
1038 panic("ipsec4_get_ulp: too short\n");
1041 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
1042 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
= IPSEC_PORT_ANY
;
1043 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
= IPSEC_PORT_ANY
;
1045 m_copydata(m
, 0, sizeof(ip
), (caddr_t
)&ip
);
1046 /* ip_input() flips it into host endian XXX need more checking */
1047 if (ip
.ip_off
& (IP_MF
| IP_OFFMASK
))
1052 off
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
1054 off
= ip
.ip_hl
<< 2;
1056 while (off
< m
->m_pkthdr
.len
) {
1059 spidx
->ul_proto
= nxt
;
1062 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
1064 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
1065 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
1067 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
1071 spidx
->ul_proto
= nxt
;
1074 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1076 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1077 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
1079 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
1083 if (off
+ sizeof(ip6e
) > m
->m_pkthdr
.len
)
1085 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1086 off
+= (ip6e
.ip6e_len
+ 2) << 2;
1087 nxt
= ip6e
.ip6e_nxt
;
1091 /* XXX intermediate headers??? */
1092 spidx
->ul_proto
= nxt
;
1098 /* assumes that m is sane */
1100 ipsec4_setspidx_ipaddr(m
, spidx
)
1102 struct secpolicyindex
*spidx
;
1104 struct ip
*ip
= NULL
;
1106 struct sockaddr_in
*sin
;
1108 if (m
->m_len
>= sizeof(*ip
))
1109 ip
= mtod(m
, struct ip
*);
1111 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
1115 sin
= (struct sockaddr_in
*)&spidx
->src
;
1116 bzero(sin
, sizeof(*sin
));
1117 sin
->sin_family
= AF_INET
;
1118 sin
->sin_len
= sizeof(struct sockaddr_in
);
1119 bcopy(&ip
->ip_src
, &sin
->sin_addr
, sizeof(ip
->ip_src
));
1120 spidx
->prefs
= sizeof(struct in_addr
) << 3;
1122 sin
= (struct sockaddr_in
*)&spidx
->dst
;
1123 bzero(sin
, sizeof(*sin
));
1124 sin
->sin_family
= AF_INET
;
1125 sin
->sin_len
= sizeof(struct sockaddr_in
);
1126 bcopy(&ip
->ip_dst
, &sin
->sin_addr
, sizeof(ip
->ip_dst
));
1127 spidx
->prefd
= sizeof(struct in_addr
) << 3;
1133 ipsec6_get_ulp(m
, spidx
, needport
)
1135 struct secpolicyindex
*spidx
;
1144 panic("ipsec6_get_ulp: NULL pointer was passed.\n");
1146 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1147 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m
));
1150 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
1151 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= IPSEC_PORT_ANY
;
1152 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= IPSEC_PORT_ANY
;
1155 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
1156 if (off
< 0 || m
->m_pkthdr
.len
< off
)
1161 spidx
->ul_proto
= nxt
;
1164 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
1166 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
1167 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= th
.th_sport
;
1168 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= th
.th_dport
;
1171 spidx
->ul_proto
= nxt
;
1174 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1176 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1177 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= uh
.uh_sport
;
1178 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= uh
.uh_dport
;
1180 case IPPROTO_ICMPV6
:
1182 /* XXX intermediate headers??? */
1183 spidx
->ul_proto
= nxt
;
1188 /* assumes that m is sane */
1190 ipsec6_setspidx_ipaddr(m
, spidx
)
1192 struct secpolicyindex
*spidx
;
1194 struct ip6_hdr
*ip6
= NULL
;
1195 struct ip6_hdr ip6buf
;
1196 struct sockaddr_in6
*sin6
;
1198 if (m
->m_len
>= sizeof(*ip6
))
1199 ip6
= mtod(m
, struct ip6_hdr
*);
1201 m_copydata(m
, 0, sizeof(ip6buf
), (caddr_t
)&ip6buf
);
1205 sin6
= (struct sockaddr_in6
*)&spidx
->src
;
1206 bzero(sin6
, sizeof(*sin6
));
1207 sin6
->sin6_family
= AF_INET6
;
1208 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1209 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_src
));
1210 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1211 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1212 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
1214 spidx
->prefs
= sizeof(struct in6_addr
) << 3;
1216 sin6
= (struct sockaddr_in6
*)&spidx
->dst
;
1217 bzero(sin6
, sizeof(*sin6
));
1218 sin6
->sin6_family
= AF_INET6
;
1219 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1220 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_dst
));
1221 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
1222 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1223 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
1225 spidx
->prefd
= sizeof(struct in6_addr
) << 3;
1231 static struct inpcbpolicy
*
1232 ipsec_newpcbpolicy()
1234 struct inpcbpolicy
*p
;
1236 p
= (struct inpcbpolicy
*)_MALLOC(sizeof(*p
), M_SECA
, M_WAITOK
);
1241 ipsec_delpcbpolicy(p
)
1242 struct inpcbpolicy
*p
;
1247 /* initialize policy in PCB */
1249 ipsec_init_policy(so
, pcb_sp
)
1251 struct inpcbpolicy
**pcb_sp
;
1253 struct inpcbpolicy
*new;
1256 if (so
== NULL
|| pcb_sp
== NULL
)
1257 panic("ipsec_init_policy: NULL pointer was passed.\n");
1259 new = ipsec_newpcbpolicy();
1261 ipseclog((LOG_DEBUG
, "ipsec_init_policy: No more memory.\n"));
1264 bzero(new, sizeof(*new));
1267 if (so
->so_uid
== 0)
1269 if (so
->so_cred
!= 0 && !suser(so
->so_cred
->pc_ucred
, NULL
))
1275 if ((new->sp_in
= key_newsp()) == NULL
) {
1276 ipsec_delpcbpolicy(new);
1279 new->sp_in
->state
= IPSEC_SPSTATE_ALIVE
;
1280 new->sp_in
->policy
= IPSEC_POLICY_ENTRUST
;
1282 if ((new->sp_out
= key_newsp()) == NULL
) {
1283 key_freesp(new->sp_in
, KEY_SADB_UNLOCKED
);
1284 ipsec_delpcbpolicy(new);
1287 new->sp_out
->state
= IPSEC_SPSTATE_ALIVE
;
1288 new->sp_out
->policy
= IPSEC_POLICY_ENTRUST
;
1295 /* copy old ipsec policy into new */
1297 ipsec_copy_policy(old
, new)
1298 struct inpcbpolicy
*old
, *new;
1300 struct secpolicy
*sp
;
1302 if (ipsec_bypass
!= 0)
1305 sp
= ipsec_deepcopy_policy(old
->sp_in
);
1307 key_freesp(new->sp_in
, KEY_SADB_UNLOCKED
);
1312 sp
= ipsec_deepcopy_policy(old
->sp_out
);
1314 key_freesp(new->sp_out
, KEY_SADB_UNLOCKED
);
1319 new->priv
= old
->priv
;
1324 /* deep-copy a policy in PCB */
1325 static struct secpolicy
*
1326 ipsec_deepcopy_policy(src
)
1327 struct secpolicy
*src
;
1329 struct ipsecrequest
*newchain
= NULL
;
1330 struct ipsecrequest
*p
;
1331 struct ipsecrequest
**q
;
1332 struct ipsecrequest
*r
;
1333 struct secpolicy
*dst
;
1342 * deep-copy IPsec request chain. This is required since struct
1343 * ipsecrequest is not reference counted.
1346 for (p
= src
->req
; p
; p
= p
->next
) {
1347 *q
= (struct ipsecrequest
*)_MALLOC(sizeof(struct ipsecrequest
),
1351 bzero(*q
, sizeof(**q
));
1354 (*q
)->saidx
.proto
= p
->saidx
.proto
;
1355 (*q
)->saidx
.mode
= p
->saidx
.mode
;
1356 (*q
)->level
= p
->level
;
1357 (*q
)->saidx
.reqid
= p
->saidx
.reqid
;
1359 bcopy(&p
->saidx
.src
, &(*q
)->saidx
.src
, sizeof((*q
)->saidx
.src
));
1360 bcopy(&p
->saidx
.dst
, &(*q
)->saidx
.dst
, sizeof((*q
)->saidx
.dst
));
1367 dst
->req
= newchain
;
1368 dst
->state
= src
->state
;
1369 dst
->policy
= src
->policy
;
1370 /* do not touch the refcnt fields */
1375 for (p
= newchain
; p
; p
= r
) {
1380 key_freesp(dst
, KEY_SADB_UNLOCKED
);
1384 /* set policy and ipsec request if present. */
1387 struct secpolicy
**pcb_sp
,
1388 __unused
int optname
,
1393 struct sadb_x_policy
*xpl
;
1394 struct secpolicy
*newsp
= NULL
;
1398 if (pcb_sp
== NULL
|| *pcb_sp
== NULL
|| request
== NULL
)
1400 if (len
< sizeof(*xpl
))
1402 xpl
= (struct sadb_x_policy
*)request
;
1404 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1405 printf("ipsec_set_policy: passed policy\n");
1406 kdebug_sadb_x_policy((struct sadb_ext
*)xpl
));
1408 /* check policy type */
1409 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1410 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
1411 || xpl
->sadb_x_policy_type
== IPSEC_POLICY_NONE
)
1414 /* check privileged socket */
1415 if (priv
== 0 && xpl
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
)
1418 /* allocation new SP entry */
1419 if ((newsp
= key_msg2sp(xpl
, len
, &error
)) == NULL
)
1422 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1424 /* clear old SP and set new SP */
1425 key_freesp(*pcb_sp
, KEY_SADB_UNLOCKED
);
1427 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1428 printf("ipsec_set_policy: new policy\n");
1429 kdebug_secpolicy(newsp
));
1435 ipsec_get_policy(pcb_sp
, mp
)
1436 struct secpolicy
*pcb_sp
;
1442 if (pcb_sp
== NULL
|| mp
== NULL
)
1445 *mp
= key_sp2msg(pcb_sp
);
1447 ipseclog((LOG_DEBUG
, "ipsec_get_policy: No more memory.\n"));
1451 m_mchtype(*mp
, MT_DATA
);
1452 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1453 printf("ipsec_get_policy:\n");
1460 ipsec4_set_policy(inp
, optname
, request
, len
, priv
)
1467 struct sadb_x_policy
*xpl
;
1468 struct secpolicy
**pcb_sp
;
1472 if (inp
== NULL
|| request
== NULL
)
1474 if (len
< sizeof(*xpl
))
1476 xpl
= (struct sadb_x_policy
*)request
;
1478 if (inp
->inp_sp
== NULL
) {
1479 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1484 /* select direction */
1485 switch (xpl
->sadb_x_policy_dir
) {
1486 case IPSEC_DIR_INBOUND
:
1487 pcb_sp
= &inp
->inp_sp
->sp_in
;
1489 case IPSEC_DIR_OUTBOUND
:
1490 pcb_sp
= &inp
->inp_sp
->sp_out
;
1493 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1494 xpl
->sadb_x_policy_dir
));
1498 /* turn bypass off */
1499 if (ipsec_bypass
!= 0)
1502 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1506 ipsec4_get_policy(inp
, request
, len
, mp
)
1512 struct sadb_x_policy
*xpl
;
1513 struct secpolicy
*pcb_sp
;
1516 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1519 if (inp
== NULL
|| request
== NULL
|| mp
== NULL
)
1521 if (len
< sizeof(*xpl
))
1523 xpl
= (struct sadb_x_policy
*)request
;
1525 if (inp
->inp_sp
== NULL
) {
1526 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1531 /* select direction */
1532 switch (xpl
->sadb_x_policy_dir
) {
1533 case IPSEC_DIR_INBOUND
:
1534 pcb_sp
= inp
->inp_sp
->sp_in
;
1536 case IPSEC_DIR_OUTBOUND
:
1537 pcb_sp
= inp
->inp_sp
->sp_out
;
1540 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1541 xpl
->sadb_x_policy_dir
));
1545 return ipsec_get_policy(pcb_sp
, mp
);
1548 /* delete policy in PCB */
1550 ipsec4_delete_pcbpolicy(inp
)
1556 panic("ipsec4_delete_pcbpolicy: NULL pointer was passed.\n");
1558 if (inp
->inp_sp
== NULL
)
1561 if (inp
->inp_sp
->sp_in
!= NULL
) {
1562 key_freesp(inp
->inp_sp
->sp_in
, KEY_SADB_UNLOCKED
);
1563 inp
->inp_sp
->sp_in
= NULL
;
1566 if (inp
->inp_sp
->sp_out
!= NULL
) {
1567 key_freesp(inp
->inp_sp
->sp_out
, KEY_SADB_UNLOCKED
);
1568 inp
->inp_sp
->sp_out
= NULL
;
1571 ipsec_delpcbpolicy(inp
->inp_sp
);
1579 ipsec6_set_policy(in6p
, optname
, request
, len
, priv
)
1580 struct in6pcb
*in6p
;
1586 struct sadb_x_policy
*xpl
;
1587 struct secpolicy
**pcb_sp
;
1591 if (in6p
== NULL
|| request
== NULL
)
1593 if (len
< sizeof(*xpl
))
1595 xpl
= (struct sadb_x_policy
*)request
;
1597 if (in6p
->in6p_sp
== NULL
) {
1598 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1603 /* select direction */
1604 switch (xpl
->sadb_x_policy_dir
) {
1605 case IPSEC_DIR_INBOUND
:
1606 pcb_sp
= &in6p
->in6p_sp
->sp_in
;
1608 case IPSEC_DIR_OUTBOUND
:
1609 pcb_sp
= &in6p
->in6p_sp
->sp_out
;
1612 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1613 xpl
->sadb_x_policy_dir
));
1617 /* turn bypass off */
1618 if (ipsec_bypass
!= 0)
1621 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1625 ipsec6_get_policy(in6p
, request
, len
, mp
)
1626 struct in6pcb
*in6p
;
1631 struct sadb_x_policy
*xpl
;
1632 struct secpolicy
*pcb_sp
;
1636 if (in6p
== NULL
|| request
== NULL
|| mp
== NULL
)
1638 if (len
< sizeof(*xpl
))
1640 xpl
= (struct sadb_x_policy
*)request
;
1642 if (in6p
->in6p_sp
== NULL
) {
1643 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1648 /* select direction */
1649 switch (xpl
->sadb_x_policy_dir
) {
1650 case IPSEC_DIR_INBOUND
:
1651 pcb_sp
= in6p
->in6p_sp
->sp_in
;
1653 case IPSEC_DIR_OUTBOUND
:
1654 pcb_sp
= in6p
->in6p_sp
->sp_out
;
1657 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1658 xpl
->sadb_x_policy_dir
));
1662 return ipsec_get_policy(pcb_sp
, mp
);
1666 ipsec6_delete_pcbpolicy(in6p
)
1667 struct in6pcb
*in6p
;
1672 panic("ipsec6_delete_pcbpolicy: NULL pointer was passed.\n");
1674 if (in6p
->in6p_sp
== NULL
)
1677 if (in6p
->in6p_sp
->sp_in
!= NULL
) {
1678 key_freesp(in6p
->in6p_sp
->sp_in
, KEY_SADB_UNLOCKED
);
1679 in6p
->in6p_sp
->sp_in
= NULL
;
1682 if (in6p
->in6p_sp
->sp_out
!= NULL
) {
1683 key_freesp(in6p
->in6p_sp
->sp_out
, KEY_SADB_UNLOCKED
);
1684 in6p
->in6p_sp
->sp_out
= NULL
;
1687 ipsec_delpcbpolicy(in6p
->in6p_sp
);
1688 in6p
->in6p_sp
= NULL
;
1695 * return current level.
1696 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1699 ipsec_get_reqlevel(isr
)
1700 struct ipsecrequest
*isr
;
1703 u_int esp_trans_deflev
= 0, esp_net_deflev
= 0, ah_trans_deflev
= 0, ah_net_deflev
= 0;
1706 if (isr
== NULL
|| isr
->sp
== NULL
)
1707 panic("ipsec_get_reqlevel: NULL pointer is passed.\n");
1708 if (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
1709 != ((struct sockaddr
*)&isr
->sp
->spidx
.dst
)->sa_family
)
1710 panic("ipsec_get_reqlevel: family mismatched.\n");
1712 /* XXX note that we have ipseclog() expanded here - code sync issue */
1713 #define IPSEC_CHECK_DEFAULT(lev) \
1714 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1715 && (lev) != IPSEC_LEVEL_UNIQUE) \
1717 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1718 (lev), IPSEC_LEVEL_REQUIRE) \
1720 (lev) = IPSEC_LEVEL_REQUIRE, \
1724 /* set default level */
1725 switch (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
) {
1728 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev
);
1729 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev
);
1730 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev
);
1731 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev
);
1736 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev
);
1737 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev
);
1738 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev
);
1739 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev
);
1743 panic("key_get_reqlevel: Unknown family. %d\n",
1744 ((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
);
1747 #undef IPSEC_CHECK_DEFAULT
1750 switch (isr
->level
) {
1751 case IPSEC_LEVEL_DEFAULT
:
1752 switch (isr
->saidx
.proto
) {
1754 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1755 level
= esp_net_deflev
;
1757 level
= esp_trans_deflev
;
1760 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1761 level
= ah_net_deflev
;
1763 level
= ah_trans_deflev
;
1765 case IPPROTO_IPCOMP
:
1767 * we don't really care, as IPcomp document says that
1768 * we shouldn't compress small packets
1770 level
= IPSEC_LEVEL_USE
;
1773 panic("ipsec_get_reqlevel: "
1774 "Illegal protocol defined %u\n",
1779 case IPSEC_LEVEL_USE
:
1780 case IPSEC_LEVEL_REQUIRE
:
1783 case IPSEC_LEVEL_UNIQUE
:
1784 level
= IPSEC_LEVEL_REQUIRE
;
1788 panic("ipsec_get_reqlevel: Illegal IPsec level %u\n",
1796 * Check AH/ESP integrity.
1802 ipsec_in_reject(sp
, m
)
1803 struct secpolicy
*sp
;
1806 struct ipsecrequest
*isr
;
1808 int need_auth
, need_conf
, need_icv
;
1810 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1811 printf("ipsec_in_reject: using SP\n");
1812 kdebug_secpolicy(sp
));
1815 switch (sp
->policy
) {
1816 case IPSEC_POLICY_DISCARD
:
1817 case IPSEC_POLICY_GENERATE
:
1819 case IPSEC_POLICY_BYPASS
:
1820 case IPSEC_POLICY_NONE
:
1823 case IPSEC_POLICY_IPSEC
:
1826 case IPSEC_POLICY_ENTRUST
:
1828 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1835 /* XXX should compare policy against ipsec header history */
1837 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1839 /* get current level */
1840 level
= ipsec_get_reqlevel(isr
);
1842 switch (isr
->saidx
.proto
) {
1844 if (level
== IPSEC_LEVEL_REQUIRE
) {
1848 /* this won't work with multiple input threads - isr->sav would change
1849 * with every packet and is not necessarily related to the current packet
1850 * being processed. If ESP processing is required - the esp code should
1851 * make sure that the integrity check is present and correct. I don't see
1852 * why it would be necessary to check for the presence of the integrity
1853 * check value here. I think this is just wrong.
1854 * isr->sav has been removed.
1855 * %%%%%% this needs to be re-worked at some point but I think the code below can
1856 * be ignored for now.
1858 if (isr
->sav
!= NULL
1859 && isr
->sav
->flags
== SADB_X_EXT_NONE
1860 && isr
->sav
->alg_auth
!= SADB_AALG_NONE
)
1866 if (level
== IPSEC_LEVEL_REQUIRE
) {
1871 case IPPROTO_IPCOMP
:
1873 * we don't really care, as IPcomp document says that
1874 * we shouldn't compress small packets, IPComp policy
1875 * should always be treated as being in "use" level.
1881 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1882 printf("ipsec_in_reject: auth:%d conf:%d icv:%d m_flags:%x\n",
1883 need_auth
, need_conf
, need_icv
, m
->m_flags
));
1885 if ((need_conf
&& !(m
->m_flags
& M_DECRYPTED
))
1886 || (!need_auth
&& need_icv
&& !(m
->m_flags
& M_AUTHIPDGM
))
1887 || (need_auth
&& !(m
->m_flags
& M_AUTHIPHDR
)))
1894 * Check AH/ESP integrity.
1895 * This function is called from tcp_input(), udp_input(),
1896 * and {ah,esp}4_input for tunnel mode
1899 ipsec4_in_reject_so(m
, so
)
1903 struct secpolicy
*sp
= NULL
;
1907 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1910 return 0; /* XXX should be panic ? */
1912 /* get SP for this packet.
1913 * When we are called from ip_forward(), we call
1914 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1917 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1919 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1922 return 0; /* XXX should be panic ?
1923 * -> No, there may be error. */
1925 result
= ipsec_in_reject(sp
, m
);
1926 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1927 printf("DP ipsec4_in_reject_so call free SP:%p\n", sp
));
1928 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1934 ipsec4_in_reject(m
, inp
)
1939 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1941 return ipsec4_in_reject_so(m
, NULL
);
1942 if (inp
->inp_socket
)
1943 return ipsec4_in_reject_so(m
, inp
->inp_socket
);
1945 panic("ipsec4_in_reject: invalid inpcb/socket");
1953 * Check AH/ESP integrity.
1954 * This function is called from tcp6_input(), udp6_input(),
1955 * and {ah,esp}6_input for tunnel mode
1958 ipsec6_in_reject_so(m
, so
)
1962 struct secpolicy
*sp
= NULL
;
1966 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1969 return 0; /* XXX should be panic ? */
1971 /* get SP for this packet.
1972 * When we are called from ip_forward(), we call
1973 * ipsec6_getpolicybyaddr() with IP_FORWARDING flag.
1976 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1978 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1981 return 0; /* XXX should be panic ? */
1983 result
= ipsec_in_reject(sp
, m
);
1984 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1985 printf("DP ipsec6_in_reject_so call free SP:%p\n", sp
));
1986 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1992 ipsec6_in_reject(m
, in6p
)
1994 struct in6pcb
*in6p
;
1997 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1999 return ipsec6_in_reject_so(m
, NULL
);
2000 if (in6p
->in6p_socket
)
2001 return ipsec6_in_reject_so(m
, in6p
->in6p_socket
);
2003 panic("ipsec6_in_reject: invalid in6p/socket");
2011 * compute the byte size to be occupied by IPsec header.
2012 * in case it is tunneled, it includes the size of outer IP header.
2013 * NOTE: SP passed is free in this function.
2017 struct secpolicy
*sp
;
2019 struct ipsecrequest
*isr
;
2022 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2023 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2024 printf("ipsec_hdrsiz: using SP\n");
2025 kdebug_secpolicy(sp
));
2028 switch (sp
->policy
) {
2029 case IPSEC_POLICY_DISCARD
:
2030 case IPSEC_POLICY_GENERATE
:
2031 case IPSEC_POLICY_BYPASS
:
2032 case IPSEC_POLICY_NONE
:
2035 case IPSEC_POLICY_IPSEC
:
2038 case IPSEC_POLICY_ENTRUST
:
2040 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
2045 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2049 switch (isr
->saidx
.proto
) {
2052 clen
= esp_hdrsiz(isr
);
2058 clen
= ah_hdrsiz(isr
);
2060 case IPPROTO_IPCOMP
:
2061 clen
= sizeof(struct ipcomp
);
2065 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2066 switch (((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
) {
2068 clen
+= sizeof(struct ip
);
2072 clen
+= sizeof(struct ip6_hdr
);
2076 ipseclog((LOG_ERR
, "ipsec_hdrsiz: "
2077 "unknown AF %d in IPsec tunnel SA\n",
2078 ((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
));
2088 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
2090 ipsec4_hdrsiz(m
, dir
, inp
)
2095 struct secpolicy
*sp
= NULL
;
2099 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2102 return 0; /* XXX should be panic ? */
2103 if (inp
!= NULL
&& inp
->inp_socket
== NULL
)
2104 panic("ipsec4_hdrsize: why is socket NULL but there is PCB.");
2106 /* get SP for this packet.
2107 * When we are called from ip_forward(), we call
2108 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
2111 sp
= ipsec4_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2113 sp
= ipsec4_getpolicybysock(m
, dir
, inp
->inp_socket
, &error
);
2116 return 0; /* XXX should be panic ? */
2118 size
= ipsec_hdrsiz(sp
);
2119 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2120 printf("DP ipsec4_hdrsiz call free SP:%p\n", sp
));
2121 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2122 printf("ipsec4_hdrsiz: size:%lu.\n", (u_int32_t
)size
));
2123 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2129 /* This function is called from ipsec6_hdrsize_tcp(),
2130 * and maybe from ip6_forward.()
2133 ipsec6_hdrsiz(m
, dir
, in6p
)
2136 struct in6pcb
*in6p
;
2138 struct secpolicy
*sp
= NULL
;
2142 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2145 return 0; /* XXX shoud be panic ? */
2146 if (in6p
!= NULL
&& in6p
->in6p_socket
== NULL
)
2147 panic("ipsec6_hdrsize: why is socket NULL but there is PCB.");
2149 /* get SP for this packet */
2150 /* XXX Is it right to call with IP_FORWARDING. */
2152 sp
= ipsec6_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2154 sp
= ipsec6_getpolicybysock(m
, dir
, in6p
->in6p_socket
, &error
);
2158 size
= ipsec_hdrsiz(sp
);
2159 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2160 printf("DP ipsec6_hdrsiz call free SP:%p\n", sp
));
2161 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2162 printf("ipsec6_hdrsiz: size:%lu.\n", (u_int32_t
)size
));
2163 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2171 * encapsulate for ipsec tunnel.
2172 * ip->ip_src must be fixed later on.
2175 ipsec4_encapsulate(m
, sav
)
2177 struct secasvar
*sav
;
2184 /* can't tunnel between different AFs */
2185 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2186 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2187 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2192 /* XXX if the dst is myself, perform nothing. */
2193 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2199 if (m
->m_len
< sizeof(*ip
))
2200 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2202 ip
= mtod(m
, struct ip
*);
2204 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
2206 hlen
= ip
->ip_hl
<< 2;
2209 if (m
->m_len
!= hlen
)
2210 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2212 /* generate header checksum */
2215 ip
->ip_sum
= in_cksum(m
, hlen
);
2217 ip
->ip_sum
= in_cksum(m
, hlen
);
2220 plen
= m
->m_pkthdr
.len
;
2223 * grow the mbuf to accomodate the new IPv4 header.
2224 * NOTE: IPv4 options will never be copied.
2226 if (M_LEADINGSPACE(m
->m_next
) < hlen
) {
2228 MGET(n
, M_DONTWAIT
, MT_DATA
);
2234 n
->m_next
= m
->m_next
;
2236 m
->m_pkthdr
.len
+= hlen
;
2237 oip
= mtod(n
, struct ip
*);
2239 m
->m_next
->m_len
+= hlen
;
2240 m
->m_next
->m_data
-= hlen
;
2241 m
->m_pkthdr
.len
+= hlen
;
2242 oip
= mtod(m
->m_next
, struct ip
*);
2244 ip
= mtod(m
, struct ip
*);
2245 ovbcopy((caddr_t
)ip
, (caddr_t
)oip
, hlen
);
2246 m
->m_len
= sizeof(struct ip
);
2247 m
->m_pkthdr
.len
-= (hlen
- sizeof(struct ip
));
2249 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
2250 /* ECN consideration. */
2251 ip_ecn_ingress(ip4_ipsec_ecn
, &ip
->ip_tos
, &oip
->ip_tos
);
2253 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2255 ip
->ip_hl
= sizeof(struct ip
) >> 2;
2257 ip
->ip_off
&= htons(~IP_OFFMASK
);
2258 ip
->ip_off
&= htons(~IP_MF
);
2259 switch (ip4_ipsec_dfbit
) {
2260 case 0: /* clear DF bit */
2261 ip
->ip_off
&= htons(~IP_DF
);
2263 case 1: /* set DF bit */
2264 ip
->ip_off
|= htons(IP_DF
);
2266 default: /* copy DF bit */
2269 ip
->ip_p
= IPPROTO_IPIP
;
2270 if (plen
+ sizeof(struct ip
) < IP_MAXPACKET
)
2271 ip
->ip_len
= htons(plen
+ sizeof(struct ip
));
2273 ipseclog((LOG_ERR
, "IPv4 ipsec: size exceeds limit: "
2274 "leave ip_len as is (invalid packet)\n"));
2277 ip
->ip_id
= ip_randomid();
2279 ip
->ip_id
= htons(ip_id
++);
2281 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
,
2282 &ip
->ip_src
, sizeof(ip
->ip_src
));
2283 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
,
2284 &ip
->ip_dst
, sizeof(ip
->ip_dst
));
2285 ip
->ip_ttl
= IPDEFTTL
;
2287 /* XXX Should ip_src be updated later ? */
2295 ipsec6_encapsulate(m
, sav
)
2297 struct secasvar
*sav
;
2299 struct ip6_hdr
*oip6
;
2300 struct ip6_hdr
*ip6
;
2303 /* can't tunnel between different AFs */
2304 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2305 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2306 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
2311 /* XXX if the dst is myself, perform nothing. */
2312 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2318 plen
= m
->m_pkthdr
.len
;
2321 * grow the mbuf to accomodate the new IPv6 header.
2323 if (m
->m_len
!= sizeof(struct ip6_hdr
))
2324 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2325 if (M_LEADINGSPACE(m
->m_next
) < sizeof(struct ip6_hdr
)) {
2327 MGET(n
, M_DONTWAIT
, MT_DATA
);
2332 n
->m_len
= sizeof(struct ip6_hdr
);
2333 n
->m_next
= m
->m_next
;
2335 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2336 oip6
= mtod(n
, struct ip6_hdr
*);
2338 m
->m_next
->m_len
+= sizeof(struct ip6_hdr
);
2339 m
->m_next
->m_data
-= sizeof(struct ip6_hdr
);
2340 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2341 oip6
= mtod(m
->m_next
, struct ip6_hdr
*);
2343 ip6
= mtod(m
, struct ip6_hdr
*);
2344 ovbcopy((caddr_t
)ip6
, (caddr_t
)oip6
, sizeof(struct ip6_hdr
));
2346 /* Fake link-local scope-class addresses */
2347 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
2348 oip6
->ip6_src
.s6_addr16
[1] = 0;
2349 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
2350 oip6
->ip6_dst
.s6_addr16
[1] = 0;
2352 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
2353 /* ECN consideration. */
2354 ip6_ecn_ingress(ip6_ipsec_ecn
, &ip6
->ip6_flow
, &oip6
->ip6_flow
);
2355 if (plen
< IPV6_MAXPACKET
- sizeof(struct ip6_hdr
))
2356 ip6
->ip6_plen
= htons(plen
);
2358 /* ip6->ip6_plen will be updated in ip6_output() */
2360 ip6
->ip6_nxt
= IPPROTO_IPV6
;
2361 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.src
)->sin6_addr
,
2362 &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
2363 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
)->sin6_addr
,
2364 &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
2365 ip6
->ip6_hlim
= IPV6_DEFHLIM
;
2367 /* XXX Should ip6_src be updated later ? */
2373 ipsec64_encapsulate(m
, sav
)
2375 struct secasvar
*sav
;
2377 struct ip6_hdr
*ip6
, *ip6i
;
2382 /* tunneling over IPv4 */
2383 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2384 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2385 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2390 /* XXX if the dst is myself, perform nothing. */
2391 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2397 plen
= m
->m_pkthdr
.len
;
2398 ip6
= mtod(m
, struct ip6_hdr
*);
2399 hlim
= ip6
->ip6_hlim
;
2401 * grow the mbuf to accomodate the new IPv4 header.
2403 if (m
->m_len
!= sizeof(struct ip6_hdr
))
2404 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2405 if (M_LEADINGSPACE(m
->m_next
) < sizeof(struct ip6_hdr
)) {
2407 MGET(n
, M_DONTWAIT
, MT_DATA
);
2412 n
->m_len
= sizeof(struct ip6_hdr
);
2413 n
->m_next
= m
->m_next
;
2415 m
->m_pkthdr
.len
+= sizeof(struct ip
);
2416 ip6i
= mtod(n
, struct ip6_hdr
*);
2418 m
->m_next
->m_len
+= sizeof(struct ip6_hdr
);
2419 m
->m_next
->m_data
-= sizeof(struct ip6_hdr
);
2420 m
->m_pkthdr
.len
+= sizeof(struct ip
);
2421 ip6i
= mtod(m
->m_next
, struct ip6_hdr
*);
2423 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
2424 /* ECN consideration. */
2425 /* XXX To be fixed later if needed */
2426 // ip_ecn_ingress(ip4_ipsec_ecn, &ip->ip_tos, &oip->ip_tos);
2428 bcopy(ip6
, ip6i
, sizeof(struct ip6_hdr
));
2429 ip
= mtod(m
, struct ip
*);
2430 m
->m_len
= sizeof(struct ip
);
2432 * Fill in some of the IPv4 fields - we don't need all of them
2433 * because the rest will be filled in by ip_output
2435 ip
->ip_v
= IPVERSION
;
2436 ip
->ip_hl
= sizeof(struct ip
) >> 2;
2442 ip
->ip_p
= IPPROTO_IPV6
;
2443 if (plen
+ sizeof(struct ip
) < IP_MAXPACKET
)
2444 ip
->ip_len
= htons(plen
+ sizeof(struct ip
));
2446 ip
->ip_len
= htons(plen
);
2447 ipseclog((LOG_ERR
, "IPv4 ipsec: size exceeds limit: "
2448 "leave ip_len as is (invalid packet)\n"));
2450 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
,
2451 &ip
->ip_src
, sizeof(ip
->ip_src
));
2452 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
,
2453 &ip
->ip_dst
, sizeof(ip
->ip_dst
));
2460 * Check the variable replay window.
2461 * ipsec_chkreplay() performs replay check before ICV verification.
2462 * ipsec_updatereplay() updates replay bitmap. This must be called after
2463 * ICV verification (it also performs replay check, which is usually done
2465 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2467 * based on RFC 2401.
2470 ipsec_chkreplay(seq
, sav
)
2472 struct secasvar
*sav
;
2474 const struct secreplay
*replay
;
2477 u_int32_t wsizeb
; /* constant: bits of window size */
2478 int frlast
; /* constant: last frame */
2483 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2485 lck_mtx_lock(sadb_mutex
);
2486 replay
= sav
->replay
;
2488 if (replay
->wsize
== 0) {
2489 lck_mtx_unlock(sadb_mutex
);
2490 return 1; /* no need to check replay. */
2494 frlast
= replay
->wsize
- 1;
2495 wsizeb
= replay
->wsize
<< 3;
2497 /* sequence number of 0 is invalid */
2499 lck_mtx_unlock(sadb_mutex
);
2503 /* first time is always okay */
2504 if (replay
->count
== 0) {
2505 lck_mtx_unlock(sadb_mutex
);
2509 if (seq
> replay
->lastseq
) {
2510 /* larger sequences are okay */
2511 lck_mtx_unlock(sadb_mutex
);
2514 /* seq is equal or less than lastseq. */
2515 diff
= replay
->lastseq
- seq
;
2517 /* over range to check, i.e. too old or wrapped */
2518 if (diff
>= wsizeb
) {
2519 lck_mtx_unlock(sadb_mutex
);
2523 fr
= frlast
- diff
/ 8;
2525 /* this packet already seen ? */
2526 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8))) {
2527 lck_mtx_unlock(sadb_mutex
);
2531 /* out of order but good */
2532 lck_mtx_unlock(sadb_mutex
);
2538 * check replay counter whether to update or not.
2543 ipsec_updatereplay(seq
, sav
)
2545 struct secasvar
*sav
;
2547 struct secreplay
*replay
;
2550 u_int32_t wsizeb
; /* constant: bits of window size */
2551 int frlast
; /* constant: last frame */
2555 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2557 lck_mtx_lock(sadb_mutex
);
2558 replay
= sav
->replay
;
2560 if (replay
->wsize
== 0)
2561 goto ok
; /* no need to check replay. */
2564 frlast
= replay
->wsize
- 1;
2565 wsizeb
= replay
->wsize
<< 3;
2567 /* sequence number of 0 is invalid */
2572 if (replay
->count
== 0) {
2573 replay
->lastseq
= seq
;
2574 bzero(replay
->bitmap
, replay
->wsize
);
2575 (replay
->bitmap
)[frlast
] = 1;
2579 if (seq
> replay
->lastseq
) {
2580 /* seq is larger than lastseq. */
2581 diff
= seq
- replay
->lastseq
;
2583 /* new larger sequence number */
2584 if (diff
< wsizeb
) {
2586 /* set bit for this packet */
2587 vshiftl((unsigned char *) replay
->bitmap
, diff
, replay
->wsize
);
2588 (replay
->bitmap
)[frlast
] |= 1;
2590 /* this packet has a "way larger" */
2591 bzero(replay
->bitmap
, replay
->wsize
);
2592 (replay
->bitmap
)[frlast
] = 1;
2594 replay
->lastseq
= seq
;
2596 /* larger is good */
2598 /* seq is equal or less than lastseq. */
2599 diff
= replay
->lastseq
- seq
;
2601 /* over range to check, i.e. too old or wrapped */
2602 if (diff
>= wsizeb
) {
2603 lck_mtx_unlock(sadb_mutex
);
2607 fr
= frlast
- diff
/ 8;
2609 /* this packet already seen ? */
2610 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8))) {
2611 lck_mtx_unlock(sadb_mutex
);
2616 (replay
->bitmap
)[fr
] |= (1 << (diff
% 8));
2618 /* out of order but good */
2622 if (replay
->count
== ~0) {
2624 /* set overflow flag */
2627 /* don't increment, no more packets accepted */
2628 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
2629 lck_mtx_unlock(sadb_mutex
);
2633 ipseclog((LOG_WARNING
, "replay counter made %d cycle. %s\n",
2634 replay
->overflow
, ipsec_logsastr(sav
)));
2639 lck_mtx_unlock(sadb_mutex
);
2644 * shift variable length buffer to left.
2645 * IN: bitmap: pointer to the buffer
2646 * nbit: the number of to shift.
2647 * wsize: buffer size (bytes).
2650 vshiftl(bitmap
, nbit
, wsize
)
2651 unsigned char *bitmap
;
2657 for (j
= 0; j
< nbit
; j
+= 8) {
2658 s
= (nbit
- j
< 8) ? (nbit
- j
): 8;
2660 for (i
= 1; i
< wsize
; i
++) {
2661 over
= (bitmap
[i
] >> (8 - s
));
2663 bitmap
[i
-1] |= over
;
2671 ipsec4_logpacketstr(ip
, spi
)
2675 static char buf
[256];
2679 s
= (u_int8_t
*)(&ip
->ip_src
);
2680 d
= (u_int8_t
*)(&ip
->ip_dst
);
2683 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2686 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%u.%u.%u.%u",
2687 s
[0], s
[1], s
[2], s
[3]);
2690 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%u.%u.%u.%u",
2691 d
[0], d
[1], d
[2], d
[3]);
2694 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2701 ipsec6_logpacketstr(ip6
, spi
)
2702 struct ip6_hdr
*ip6
;
2705 static char buf
[256];
2709 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2712 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%s",
2713 ip6_sprintf(&ip6
->ip6_src
));
2716 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%s",
2717 ip6_sprintf(&ip6
->ip6_dst
));
2720 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2728 struct secasvar
*sav
;
2730 static char buf
[256];
2732 struct secasindex
*saidx
= &sav
->sah
->saidx
;
2734 /* validity check */
2735 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2736 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
)
2737 panic("ipsec_logsastr: family mismatched.\n");
2740 snprintf(buf
, sizeof(buf
), "SA(SPI=%u ", (u_int32_t
)ntohl(sav
->spi
));
2743 if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET
) {
2745 s
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->src
)->sin_addr
;
2746 d
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->dst
)->sin_addr
;
2747 snprintf(p
, sizeof(buf
) - (p
- buf
),
2748 "src=%d.%d.%d.%d dst=%d.%d.%d.%d",
2749 s
[0], s
[1], s
[2], s
[3], d
[0], d
[1], d
[2], d
[3]);
2752 else if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET6
) {
2753 snprintf(p
, sizeof(buf
) - (p
- buf
),
2755 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->src
)->sin6_addr
));
2758 snprintf(p
, sizeof(buf
) - (p
- buf
),
2760 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->dst
)->sin6_addr
));
2765 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2781 p
= mtod(m
, u_char
*);
2782 for (i
= 0; i
< m
->m_len
; i
++) {
2783 printf("%02x ", p
[i
]);
2785 if (totlen
% 16 == 0)
2790 if (totlen
% 16 != 0)
2797 * IPsec output logic for IPv4.
2801 struct ipsec_output_state
*state
,
2802 struct secpolicy
*sp
,
2805 struct ip
*ip
= NULL
;
2806 struct ipsecrequest
*isr
= NULL
;
2807 struct secasindex saidx
;
2808 struct secasvar
*sav
= NULL
;
2810 struct sockaddr_in
*dst4
;
2811 struct sockaddr_in
*sin
;
2813 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2816 panic("state == NULL in ipsec4_output");
2818 panic("state->m == NULL in ipsec4_output");
2820 panic("state->ro == NULL in ipsec4_output");
2822 panic("state->dst == NULL in ipsec4_output");
2824 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_START
, 0,0,0,0,0);
2826 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2827 printf("ipsec4_output: applyed SP\n");
2828 kdebug_secpolicy(sp
));
2830 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2832 #if 0 /* give up to check restriction of transport mode */
2833 /* XXX but should be checked somewhere */
2835 * some of the IPsec operation must be performed only in
2838 if (isr
->saidx
.mode
== IPSEC_MODE_TRANSPORT
2839 && (flags
& IP_FORWARDING
))
2843 /* make SA index for search proper SA */
2844 ip
= mtod(state
->m
, struct ip
*);
2845 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2846 saidx
.mode
= isr
->saidx
.mode
;
2847 saidx
.reqid
= isr
->saidx
.reqid
;
2848 sin
= (struct sockaddr_in
*)&saidx
.src
;
2849 if (sin
->sin_len
== 0) {
2850 sin
->sin_len
= sizeof(*sin
);
2851 sin
->sin_family
= AF_INET
;
2852 sin
->sin_port
= IPSEC_PORT_ANY
;
2853 bcopy(&ip
->ip_src
, &sin
->sin_addr
,
2854 sizeof(sin
->sin_addr
));
2856 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2857 if (sin
->sin_len
== 0) {
2858 sin
->sin_len
= sizeof(*sin
);
2859 sin
->sin_family
= AF_INET
;
2860 sin
->sin_port
= IPSEC_PORT_ANY
;
2862 * Get port from packet if upper layer is UDP and nat traversal
2863 * is enabled and transport mode.
2866 if ((esp_udp_encap_port
& 0xFFFF) != 0 &&
2867 isr
->saidx
.mode
== IPSEC_MODE_TRANSPORT
) {
2869 if (ip
->ip_p
== IPPROTO_UDP
) {
2873 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
2875 hlen
= ip
->ip_hl
<< 2;
2877 if (state
->m
->m_len
< hlen
+ sizeof(struct udphdr
)) {
2878 state
->m
= m_pullup(state
->m
, hlen
+ sizeof(struct udphdr
));
2880 ipseclog((LOG_DEBUG
,
2881 "IPv4 output: can't pullup UDP header\n"));
2882 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
2885 ip
= mtod(state
->m
, struct ip
*);
2887 udp
= (struct udphdr
*)(((u_int8_t
*)ip
) + hlen
);
2888 sin
->sin_port
= udp
->uh_dport
;
2892 bcopy(&ip
->ip_dst
, &sin
->sin_addr
,
2893 sizeof(sin
->sin_addr
));
2896 if ((error
= key_checkrequest(isr
, &saidx
, &sav
)) != 0) {
2898 * IPsec processing is required, but no SA found.
2899 * I assume that key_acquire() had been called
2900 * to get/establish the SA. Here I discard
2901 * this packet because it is responsibility for
2902 * upper layer to retransmit the packet.
2904 IPSEC_STAT_INCREMENT(ipsecstat
.out_nosa
);
2908 /* validity check */
2910 switch (ipsec_get_reqlevel(isr
)) {
2911 case IPSEC_LEVEL_USE
:
2913 case IPSEC_LEVEL_REQUIRE
:
2914 /* must be not reached here. */
2915 panic("ipsec4_output: no SA found, but required.");
2920 * If there is no valid SA, we give up to process any
2921 * more. In such a case, the SA's status is changed
2922 * from DYING to DEAD after allocating. If a packet
2923 * send to the receiver by dead SA, the receiver can
2924 * not decode a packet because SA has been dead.
2926 if (sav
->state
!= SADB_SASTATE_MATURE
2927 && sav
->state
!= SADB_SASTATE_DYING
) {
2928 IPSEC_STAT_INCREMENT(ipsecstat
.out_nosa
);
2934 * There may be the case that SA status will be changed when
2935 * we are refering to one. So calling splsoftnet().
2938 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2940 * build IPsec tunnel.
2942 /* XXX should be processed with other familiy */
2943 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2944 ipseclog((LOG_ERR
, "ipsec4_output: "
2945 "family mismatched between inner and outer spi=%u\n",
2946 (u_int32_t
)ntohl(sav
->spi
)));
2947 error
= EAFNOSUPPORT
;
2951 state
->m
= ipsec4_splithdr(state
->m
);
2956 error
= ipsec4_encapsulate(state
->m
, sav
);
2961 ip
= mtod(state
->m
, struct ip
*);
2963 // grab sadb_mutex, before updating sah's route cache
2964 lck_mtx_lock(sadb_mutex
);
2965 state
->ro
= &sav
->sah
->sa_route
;
2966 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
2967 dst4
= (struct sockaddr_in
*)state
->dst
;
2968 if (state
->ro
->ro_rt
!= NULL
) {
2969 RT_LOCK(state
->ro
->ro_rt
);
2971 if (state
->ro
->ro_rt
!= NULL
&&
2972 (state
->ro
->ro_rt
->generation_id
!= route_generation
||
2973 !(state
->ro
->ro_rt
->rt_flags
& RTF_UP
) ||
2974 dst4
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
2975 RT_UNLOCK(state
->ro
->ro_rt
);
2976 rtfree(state
->ro
->ro_rt
);
2977 state
->ro
->ro_rt
= NULL
;
2979 if (state
->ro
->ro_rt
== 0) {
2980 dst4
->sin_family
= AF_INET
;
2981 dst4
->sin_len
= sizeof(*dst4
);
2982 dst4
->sin_addr
= ip
->ip_dst
;
2984 if (state
->ro
->ro_rt
== 0) {
2985 OSAddAtomic(1, &ipstat
.ips_noroute
);
2986 error
= EHOSTUNREACH
;
2987 // release sadb_mutex, after updating sah's route cache
2988 lck_mtx_unlock(sadb_mutex
);
2991 RT_LOCK(state
->ro
->ro_rt
);
2995 * adjust state->dst if tunnel endpoint is offlink
2997 * XXX: caching rt_gateway value in the state is
2998 * not really good, since it may point elsewhere
2999 * when the gateway gets modified to a larger
3000 * sockaddr via rt_setgate(). This is currently
3001 * addressed by SA_SIZE roundup in that routine.
3003 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
3004 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
3005 dst4
= (struct sockaddr_in
*)state
->dst
;
3007 RT_UNLOCK(state
->ro
->ro_rt
);
3008 // release sadb_mutex, after updating sah's route cache
3009 lck_mtx_unlock(sadb_mutex
);
3012 state
->m
= ipsec4_splithdr(state
->m
);
3017 switch (isr
->saidx
.proto
) {
3020 if ((error
= esp4_output(state
->m
, sav
)) != 0) {
3032 if ((error
= ah4_output(state
->m
, sav
)) != 0) {
3037 case IPPROTO_IPCOMP
:
3038 if ((error
= ipcomp4_output(state
->m
, sav
)) != 0) {
3045 "ipsec4_output: unknown ipsec protocol %d\n",
3053 if (state
->m
== 0) {
3057 ip
= mtod(state
->m
, struct ip
*);
3060 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_END
, 0,0,0,0,0);
3062 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3067 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3070 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_END
, error
,0,0,0,0);
3077 * IPsec output logic for IPv6, transport mode.
3080 ipsec6_output_trans(
3081 struct ipsec_output_state
*state
,
3084 struct secpolicy
*sp
,
3088 struct ip6_hdr
*ip6
;
3089 struct ipsecrequest
*isr
= NULL
;
3090 struct secasindex saidx
;
3093 struct sockaddr_in6
*sin6
;
3094 struct secasvar
*sav
= NULL
;
3096 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3099 panic("state == NULL in ipsec6_output_trans");
3101 panic("state->m == NULL in ipsec6_output_trans");
3103 panic("nexthdrp == NULL in ipsec6_output_trans");
3105 panic("mprev == NULL in ipsec6_output_trans");
3107 panic("sp == NULL in ipsec6_output_trans");
3109 panic("tun == NULL in ipsec6_output_trans");
3111 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
3112 printf("ipsec6_output_trans: applyed SP\n");
3113 kdebug_secpolicy(sp
));
3116 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
3117 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3118 /* the rest will be handled by ipsec6_output_tunnel() */
3122 /* make SA index for search proper SA */
3123 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3124 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
3125 saidx
.mode
= isr
->saidx
.mode
;
3126 saidx
.reqid
= isr
->saidx
.reqid
;
3127 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
3128 if (sin6
->sin6_len
== 0) {
3129 sin6
->sin6_len
= sizeof(*sin6
);
3130 sin6
->sin6_family
= AF_INET6
;
3131 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3132 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
3133 sizeof(ip6
->ip6_src
));
3134 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
3135 /* fix scope id for comparing SPD */
3136 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3137 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
3140 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
3141 if (sin6
->sin6_len
== 0) {
3142 sin6
->sin6_len
= sizeof(*sin6
);
3143 sin6
->sin6_family
= AF_INET6
;
3144 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3145 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
3146 sizeof(ip6
->ip6_dst
));
3147 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
3148 /* fix scope id for comparing SPD */
3149 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3150 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
3154 if (key_checkrequest(isr
, &saidx
, &sav
) == ENOENT
) {
3156 * IPsec processing is required, but no SA found.
3157 * I assume that key_acquire() had been called
3158 * to get/establish the SA. Here I discard
3159 * this packet because it is responsibility for
3160 * upper layer to retransmit the packet.
3162 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nosa
);
3166 * Notify the fact that the packet is discarded
3167 * to ourselves. I believe this is better than
3168 * just silently discarding. (jinmei@kame.net)
3169 * XXX: should we restrict the error to TCP packets?
3170 * XXX: should we directly notify sockets via
3173 icmp6_error(state
->m
, ICMP6_DST_UNREACH
,
3174 ICMP6_DST_UNREACH_ADMIN
, 0);
3175 state
->m
= NULL
; /* icmp6_error freed the mbuf */
3179 /* validity check */
3181 switch (ipsec_get_reqlevel(isr
)) {
3182 case IPSEC_LEVEL_USE
:
3184 case IPSEC_LEVEL_REQUIRE
:
3185 /* must be not reached here. */
3186 panic("ipsec6_output_trans: no SA found, but required.");
3191 * If there is no valid SA, we give up to process.
3192 * see same place at ipsec4_output().
3194 if (sav
->state
!= SADB_SASTATE_MATURE
3195 && sav
->state
!= SADB_SASTATE_DYING
) {
3196 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nosa
);
3201 switch (isr
->saidx
.proto
) {
3204 error
= esp6_output(state
->m
, nexthdrp
, mprev
->m_next
, sav
);
3211 error
= ah6_output(state
->m
, nexthdrp
, mprev
->m_next
, sav
);
3213 case IPPROTO_IPCOMP
:
3214 error
= ipcomp6_output(state
->m
, nexthdrp
, mprev
->m_next
, sav
);
3217 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
3218 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
3220 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
3228 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
3229 if (plen
> IPV6_MAXPACKET
) {
3230 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
3231 "IPsec with IPv6 jumbogram is not supported\n"));
3232 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
3233 error
= EINVAL
; /*XXX*/
3236 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3237 ip6
->ip6_plen
= htons(plen
);
3240 /* if we have more to go, we need a tunnel mode processing */
3245 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3250 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3257 * IPsec output logic for IPv6, tunnel mode.
3260 ipsec6_output_tunnel(
3261 struct ipsec_output_state
*state
,
3262 struct secpolicy
*sp
,
3266 struct ip6_hdr
*ip6
;
3267 struct ipsecrequest
*isr
= NULL
;
3268 struct secasindex saidx
;
3269 struct secasvar
*sav
= NULL
;
3272 struct sockaddr_in6
* dst6
;
3274 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3279 panic("state == NULL in ipsec6_output_tunnel");
3281 panic("state->m == NULL in ipsec6_output_tunnel");
3283 panic("sp == NULL in ipsec6_output_tunnel");
3285 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
3286 printf("ipsec6_output_tunnel: applyed SP\n");
3287 kdebug_secpolicy(sp
));
3290 * transport mode ipsec (before the 1st tunnel mode) is already
3291 * processed by ipsec6_output_trans().
3293 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
3294 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
3298 for (/* already initialized */; isr
; isr
= isr
->next
) {
3299 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3300 /* When tunnel mode, SA peers must be specified. */
3301 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
3303 /* make SA index to look for a proper SA */
3304 struct sockaddr_in6
*sin6
;
3306 bzero(&saidx
, sizeof(saidx
));
3307 saidx
.proto
= isr
->saidx
.proto
;
3308 saidx
.mode
= isr
->saidx
.mode
;
3309 saidx
.reqid
= isr
->saidx
.reqid
;
3311 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3312 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
3313 if (sin6
->sin6_len
== 0) {
3314 sin6
->sin6_len
= sizeof(*sin6
);
3315 sin6
->sin6_family
= AF_INET6
;
3316 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3317 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
3318 sizeof(ip6
->ip6_src
));
3319 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
3320 /* fix scope id for comparing SPD */
3321 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3322 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
3325 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
3326 if (sin6
->sin6_len
== 0) {
3327 sin6
->sin6_len
= sizeof(*sin6
);
3328 sin6
->sin6_family
= AF_INET6
;
3329 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3330 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
3331 sizeof(ip6
->ip6_dst
));
3332 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
3333 /* fix scope id for comparing SPD */
3334 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3335 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
3340 if (key_checkrequest(isr
, &saidx
, &sav
) == ENOENT
) {
3342 * IPsec processing is required, but no SA found.
3343 * I assume that key_acquire() had been called
3344 * to get/establish the SA. Here I discard
3345 * this packet because it is responsibility for
3346 * upper layer to retransmit the packet.
3348 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nosa
);
3353 /* validity check */
3355 switch (ipsec_get_reqlevel(isr
)) {
3356 case IPSEC_LEVEL_USE
:
3358 case IPSEC_LEVEL_REQUIRE
:
3359 /* must be not reached here. */
3360 panic("ipsec6_output_tunnel: no SA found, but required.");
3365 * If there is no valid SA, we give up to process.
3366 * see same place at ipsec4_output().
3368 if (sav
->state
!= SADB_SASTATE_MATURE
3369 && sav
->state
!= SADB_SASTATE_DYING
) {
3370 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nosa
);
3375 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3377 * build IPsec tunnel.
3379 state
->m
= ipsec6_splithdr(state
->m
);
3381 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nomem
);
3386 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
== AF_INET6
) {
3387 error
= ipsec6_encapsulate(state
->m
, sav
);
3392 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3393 } else if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
== AF_INET
) {
3396 struct sockaddr_in
* dst4
;
3397 struct route
*ro4
= NULL
;
3398 struct route ro4_copy
;
3399 struct ip_out_args ipoa
= { IFSCOPE_NONE
, 0 };
3402 * must be last isr because encapsulated IPv6 packet
3403 * will be sent by calling ip_output
3406 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3407 "IPv4 must be outer layer, spi=%u\n",
3408 (u_int32_t
)ntohl(sav
->spi
)));
3412 *tunneledv4
= 1; /* must not process any further in ip6_output */
3413 error
= ipsec64_encapsulate(state
->m
, sav
);
3418 /* Now we have an IPv4 packet */
3419 ip
= mtod(state
->m
, struct ip
*);
3421 // grab sadb_mutex, to update sah's route cache and get a local copy of it
3422 lck_mtx_lock(sadb_mutex
);
3423 ro4
= &sav
->sah
->sa_route
;
3424 dst4
= (struct sockaddr_in
*)&ro4
->ro_dst
;
3426 RT_LOCK(ro4
->ro_rt
);
3428 if (ro4
->ro_rt
!= NULL
&&
3429 (ro4
->ro_rt
->generation_id
!= route_generation
||
3430 !(ro4
->ro_rt
->rt_flags
& RTF_UP
) ||
3431 dst4
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
3432 RT_UNLOCK(ro4
->ro_rt
);
3436 if (ro4
->ro_rt
== NULL
) {
3437 dst4
->sin_family
= AF_INET
;
3438 dst4
->sin_len
= sizeof(*dst4
);
3439 dst4
->sin_addr
= ip
->ip_dst
;
3441 RT_UNLOCK(ro4
->ro_rt
);
3443 route_copyout(&ro4_copy
, ro4
, sizeof(ro4_copy
));
3444 // release sadb_mutex, after updating sah's route cache and getting a local copy
3445 lck_mtx_unlock(sadb_mutex
);
3446 state
->m
= ipsec4_splithdr(state
->m
);
3449 if (ro4_copy
.ro_rt
!= NULL
) {
3450 rtfree(ro4_copy
.ro_rt
);
3454 switch (isr
->saidx
.proto
) {
3457 if ((error
= esp4_output(state
->m
, sav
)) != 0) {
3459 if (ro4_copy
.ro_rt
!= NULL
) {
3460 rtfree(ro4_copy
.ro_rt
);
3470 if (ro4_copy
.ro_rt
!= NULL
) {
3471 rtfree(ro4_copy
.ro_rt
);
3476 if ((error
= ah4_output(state
->m
, sav
)) != 0) {
3478 if (ro4_copy
.ro_rt
!= NULL
) {
3479 rtfree(ro4_copy
.ro_rt
);
3484 case IPPROTO_IPCOMP
:
3485 if ((error
= ipcomp4_output(state
->m
, sav
)) != 0) {
3487 if (ro4_copy
.ro_rt
!= NULL
) {
3488 rtfree(ro4_copy
.ro_rt
);
3495 "ipsec4_output: unknown ipsec protocol %d\n",
3500 if (ro4_copy
.ro_rt
!= NULL
) {
3501 rtfree(ro4_copy
.ro_rt
);
3506 if (state
->m
== 0) {
3508 if (ro4_copy
.ro_rt
!= NULL
) {
3509 rtfree(ro4_copy
.ro_rt
);
3513 ip
= mtod(state
->m
, struct ip
*);
3514 ip
->ip_len
= ntohs(ip
->ip_len
); /* flip len field before calling ip_output */
3515 error
= ip_output(state
->m
, NULL
, &ro4_copy
, IP_OUTARGS
, NULL
, &ipoa
);
3517 // grab sadb_mutex, to synchronize the sah's route cache with the local copy
3518 lck_mtx_lock(sadb_mutex
);
3519 route_copyin(&ro4_copy
, ro4
, sizeof(ro4_copy
));
3520 lck_mtx_unlock(sadb_mutex
);
3525 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3526 "unsupported inner family, spi=%u\n",
3527 (u_int32_t
)ntohl(sav
->spi
)));
3528 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
3529 error
= EAFNOSUPPORT
;
3533 // grab sadb_mutex, before updating sah's route cache
3534 lck_mtx_lock(sadb_mutex
);
3535 state
->ro
= &sav
->sah
->sa_route
;
3536 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
3537 dst6
= (struct sockaddr_in6
*)state
->dst
;
3538 if (state
->ro
->ro_rt
) {
3539 RT_LOCK(state
->ro
->ro_rt
);
3541 if (state
->ro
->ro_rt
!= NULL
&&
3542 (state
->ro
->ro_rt
->generation_id
!= route_generation
||
3543 !(state
->ro
->ro_rt
->rt_flags
& RTF_UP
) ||
3544 !IN6_ARE_ADDR_EQUAL(&dst6
->sin6_addr
, &ip6
->ip6_dst
))) {
3545 RT_UNLOCK(state
->ro
->ro_rt
);
3546 rtfree(state
->ro
->ro_rt
);
3547 state
->ro
->ro_rt
= NULL
;
3549 if (state
->ro
->ro_rt
== 0) {
3550 bzero(dst6
, sizeof(*dst6
));
3551 dst6
->sin6_family
= AF_INET6
;
3552 dst6
->sin6_len
= sizeof(*dst6
);
3553 dst6
->sin6_addr
= ip6
->ip6_dst
;
3555 if (state
->ro
->ro_rt
) {
3556 RT_LOCK(state
->ro
->ro_rt
);
3559 if (state
->ro
->ro_rt
== 0) {
3560 ip6stat
.ip6s_noroute
++;
3561 IPSEC_STAT_INCREMENT(ipsec6stat
.out_noroute
);
3562 error
= EHOSTUNREACH
;
3563 // release sadb_mutex, after updating sah's route cache
3564 lck_mtx_unlock(sadb_mutex
);
3569 * adjust state->dst if tunnel endpoint is offlink
3571 * XXX: caching rt_gateway value in the state is
3572 * not really good, since it may point elsewhere
3573 * when the gateway gets modified to a larger
3574 * sockaddr via rt_setgate(). This is currently
3575 * addressed by SA_SIZE roundup in that routine.
3577 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
3578 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
3579 dst6
= (struct sockaddr_in6
*)state
->dst
;
3581 RT_UNLOCK(state
->ro
->ro_rt
);
3582 // release sadb_mutex, after updating sah's route cache
3583 lck_mtx_unlock(sadb_mutex
);
3586 state
->m
= ipsec6_splithdr(state
->m
);
3588 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nomem
);
3592 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3593 switch (isr
->saidx
.proto
) {
3596 error
= esp6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, sav
);
3603 error
= ah6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, sav
);
3605 case IPPROTO_IPCOMP
:
3606 /* XXX code should be here */
3609 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3610 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
3612 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
3620 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
3621 if (plen
> IPV6_MAXPACKET
) {
3622 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3623 "IPsec with IPv6 jumbogram is not supported\n"));
3624 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
3625 error
= EINVAL
; /*XXX*/
3628 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3629 ip6
->ip6_plen
= htons(plen
);
3633 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3638 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3648 * Chop IP header and option off from the payload.
3650 static struct mbuf
*
3658 if (m
->m_len
< sizeof(struct ip
))
3659 panic("ipsec4_splithdr: first mbuf too short");
3660 ip
= mtod(m
, struct ip
*);
3662 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
3664 hlen
= ip
->ip_hl
<< 2;
3666 if (m
->m_len
> hlen
) {
3667 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
3672 M_COPY_PKTHDR(mh
, m
);
3674 m
->m_flags
&= ~M_PKTHDR
;
3675 m_mchtype(m
, MT_DATA
);
3681 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), hlen
);
3682 } else if (m
->m_len
< hlen
) {
3683 m
= m_pullup(m
, hlen
);
3692 static struct mbuf
*
3697 struct ip6_hdr
*ip6
;
3700 if (m
->m_len
< sizeof(struct ip6_hdr
))
3701 panic("ipsec6_splithdr: first mbuf too short");
3702 ip6
= mtod(m
, struct ip6_hdr
*);
3703 hlen
= sizeof(struct ip6_hdr
);
3704 if (m
->m_len
> hlen
) {
3705 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
3710 M_COPY_PKTHDR(mh
, m
);
3712 m
->m_flags
&= ~M_PKTHDR
;
3713 m_mchtype(m
, MT_DATA
);
3719 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), hlen
);
3720 } else if (m
->m_len
< hlen
) {
3721 m
= m_pullup(m
, hlen
);
3729 /* validate inbound IPsec tunnel packet. */
3731 ipsec4_tunnel_validate(m
, off
, nxt0
, sav
, ifamily
)
3732 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3735 struct secasvar
*sav
;
3736 sa_family_t
*ifamily
;
3738 u_int8_t nxt
= nxt0
& 0xff;
3739 struct sockaddr_in
*sin
;
3740 struct sockaddr_in osrc
, odst
, i4src
, i4dst
;
3741 struct sockaddr_in6 i6src
, i6dst
;
3743 struct secpolicy
*sp
;
3746 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3749 if (m
->m_len
< sizeof(struct ip
))
3750 panic("too short mbuf on ipsec4_tunnel_validate");
3752 if (nxt
!= IPPROTO_IPV4
&& nxt
!= IPPROTO_IPV6
)
3754 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip
))
3756 /* do not decapsulate if the SA is for transport mode only */
3757 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3760 oip
= mtod(m
, struct ip
*);
3762 hlen
= _IP_VHL_HL(oip
->ip_vhl
) << 2;
3764 hlen
= oip
->ip_hl
<< 2;
3766 if (hlen
!= sizeof(struct ip
))
3769 sin
= (struct sockaddr_in
*)&sav
->sah
->saidx
.dst
;
3770 if (sin
->sin_family
!= AF_INET
)
3772 if (bcmp(&oip
->ip_dst
, &sin
->sin_addr
, sizeof(oip
->ip_dst
)) != 0)
3776 bzero(&osrc
, sizeof(osrc
));
3777 bzero(&odst
, sizeof(odst
));
3778 osrc
.sin_family
= odst
.sin_family
= AF_INET
;
3779 osrc
.sin_len
= odst
.sin_len
= sizeof(struct sockaddr_in
);
3780 osrc
.sin_addr
= oip
->ip_src
;
3781 odst
.sin_addr
= oip
->ip_dst
;
3783 * RFC2401 5.2.1 (b): (assume that we are using tunnel mode)
3784 * - if the inner destination is multicast address, there can be
3785 * multiple permissible inner source address. implementation
3786 * may want to skip verification of inner source address against
3788 * - if the inner protocol is ICMP, the packet may be an error report
3789 * from routers on the other side of the VPN cloud (R in the
3790 * following diagram). in this case, we cannot verify inner source
3791 * address against SPD selector.
3792 * me -- gw === gw -- R -- you
3794 * we consider the first bullet to be users responsibility on SPD entry
3795 * configuration (if you need to encrypt multicast traffic, set
3796 * the source range of SPD selector to 0.0.0.0/0, or have explicit
3797 * address ranges for possible senders).
3798 * the second bullet is not taken care of (yet).
3800 * therefore, we do not do anything special about inner source.
3802 if (nxt
== IPPROTO_IPV4
) {
3803 bzero(&i4src
, sizeof(struct sockaddr_in
));
3804 bzero(&i4dst
, sizeof(struct sockaddr_in
));
3805 i4src
.sin_family
= i4dst
.sin_family
= *ifamily
= AF_INET
;
3806 i4src
.sin_len
= i4dst
.sin_len
= sizeof(struct sockaddr_in
);
3807 m_copydata(m
, off
+ offsetof(struct ip
, ip_src
), sizeof(i4src
.sin_addr
),
3808 (caddr_t
)&i4src
.sin_addr
);
3809 m_copydata(m
, off
+ offsetof(struct ip
, ip_dst
), sizeof(i4dst
.sin_addr
),
3810 (caddr_t
)&i4dst
.sin_addr
);
3811 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3812 (struct sockaddr
*)&i4src
, (struct sockaddr
*)&i4dst
);
3813 } else if (nxt
== IPPROTO_IPV6
) {
3814 bzero(&i6src
, sizeof(struct sockaddr_in6
));
3815 bzero(&i6dst
, sizeof(struct sockaddr_in6
));
3816 i6src
.sin6_family
= i6dst
.sin6_family
= *ifamily
= AF_INET6
;
3817 i6src
.sin6_len
= i6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
3818 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_src
), sizeof(i6src
.sin6_addr
),
3819 (caddr_t
)&i6src
.sin6_addr
);
3820 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_dst
), sizeof(i6dst
.sin6_addr
),
3821 (caddr_t
)&i6dst
.sin6_addr
);
3822 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3823 (struct sockaddr
*)&i6src
, (struct sockaddr
*)&i6dst
);
3825 return 0; /* unsupported family */
3830 key_freesp(sp
, KEY_SADB_UNLOCKED
);
3836 /* validate inbound IPsec tunnel packet. */
3838 ipsec6_tunnel_validate(m
, off
, nxt0
, sav
)
3839 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3842 struct secasvar
*sav
;
3844 u_int8_t nxt
= nxt0
& 0xff;
3845 struct sockaddr_in6
*sin6
;
3846 struct sockaddr_in6 osrc
, odst
, isrc
, idst
;
3847 struct secpolicy
*sp
;
3848 struct ip6_hdr
*oip6
;
3850 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3853 if (m
->m_len
< sizeof(struct ip6_hdr
))
3854 panic("too short mbuf on ipsec6_tunnel_validate");
3856 if (nxt
!= IPPROTO_IPV6
)
3858 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip6_hdr
))
3860 /* do not decapsulate if the SA is for transport mode only */
3861 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3864 oip6
= mtod(m
, struct ip6_hdr
*);
3865 /* AF_INET should be supported, but at this moment we don't. */
3866 sin6
= (struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
;
3867 if (sin6
->sin6_family
!= AF_INET6
)
3869 if (!IN6_ARE_ADDR_EQUAL(&oip6
->ip6_dst
, &sin6
->sin6_addr
))
3873 bzero(&osrc
, sizeof(osrc
));
3874 bzero(&odst
, sizeof(odst
));
3875 bzero(&isrc
, sizeof(isrc
));
3876 bzero(&idst
, sizeof(idst
));
3877 osrc
.sin6_family
= odst
.sin6_family
= isrc
.sin6_family
=
3878 idst
.sin6_family
= AF_INET6
;
3879 osrc
.sin6_len
= odst
.sin6_len
= isrc
.sin6_len
= idst
.sin6_len
=
3880 sizeof(struct sockaddr_in6
);
3881 osrc
.sin6_addr
= oip6
->ip6_src
;
3882 odst
.sin6_addr
= oip6
->ip6_dst
;
3883 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_src
),
3884 sizeof(isrc
.sin6_addr
), (caddr_t
)&isrc
.sin6_addr
);
3885 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_dst
),
3886 sizeof(idst
.sin6_addr
), (caddr_t
)&idst
.sin6_addr
);
3889 * regarding to inner source address validation, see a long comment
3890 * in ipsec4_tunnel_validate.
3893 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3894 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3896 * when there is no suitable inbound policy for the packet of the ipsec
3897 * tunnel mode, the kernel never decapsulate the tunneled packet
3898 * as the ipsec tunnel mode even when the system wide policy is "none".
3899 * then the kernel leaves the generic tunnel module to process this
3900 * packet. if there is no rule of the generic tunnel, the packet
3901 * is rejected and the statistics will be counted up.
3905 key_freesp(sp
, KEY_SADB_UNLOCKED
);
3912 * Make a mbuf chain for encryption.
3913 * If the original mbuf chain contains a mbuf with a cluster,
3914 * allocate a new cluster and copy the data to the new cluster.
3915 * XXX: this hack is inefficient, but is necessary to handle cases
3916 * of TCP retransmission...
3922 struct mbuf
*n
, **mpp
, *mnew
;
3924 for (n
= m
, mpp
= &m
; n
; n
= n
->m_next
) {
3925 if (n
->m_flags
& M_EXT
) {
3927 * Make a copy only if there are more than one references
3929 * XXX: is this approach effective?
3932 n
->m_ext
.ext_free
||
3933 m_mclhasreference(n
)
3939 if (n
->m_flags
& M_PKTHDR
) {
3940 MGETHDR(mnew
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
3943 M_COPY_PKTHDR(mnew
, n
);
3946 MGET(mnew
, M_DONTWAIT
, MT_DATA
);
3954 * Copy data. If we don't have enough space to
3955 * store the whole data, allocate a cluster
3956 * or additional mbufs.
3957 * XXX: we don't use m_copyback(), since the
3958 * function does not use clusters and thus is
3967 if (remain
<= (mm
->m_flags
& M_PKTHDR
? MHLEN
: MLEN
))
3969 else { /* allocate a cluster */
3970 MCLGET(mm
, M_DONTWAIT
);
3971 if (!(mm
->m_flags
& M_EXT
)) {
3975 len
= remain
< MCLBYTES
?
3979 bcopy(n
->m_data
+ copied
, mm
->m_data
,
3986 if (remain
<= 0) /* completed? */
3989 /* need another mbuf */
3990 MGETHDR(mn
, M_DONTWAIT
, MT_HEADER
); /* XXXMAC: tags copied next time in loop? */
3993 mn
->m_pkthdr
.rcvif
= NULL
;
3999 mm
->m_next
= m_free(n
);
4018 * Tags are allocated as mbufs for now, since our minimum size is MLEN, we
4019 * should make use of up to that much space.
4021 #define IPSEC_TAG_HEADER \
4024 struct socket
*socket
;
4025 u_int32_t history_count
;
4026 struct ipsec_history history
[];
4029 #define IPSEC_TAG_SIZE (MLEN - sizeof(struct m_tag))
4030 #define IPSEC_TAG_HDR_SIZE (offsetof(struct ipsec_tag, history[0]))
4031 #define IPSEC_HISTORY_MAX ((IPSEC_TAG_SIZE - IPSEC_TAG_HDR_SIZE) / \
4032 sizeof(struct ipsec_history))
4034 static struct ipsec_tag
*
4040 /* Check if the tag already exists */
4041 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPSEC
, NULL
);
4044 struct ipsec_tag
*itag
;
4046 /* Allocate a tag */
4047 tag
= m_tag_create(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPSEC
,
4048 IPSEC_TAG_SIZE
, M_DONTWAIT
, m
);
4051 itag
= (struct ipsec_tag
*)(tag
+ 1);
4053 itag
->history_count
= 0;
4055 m_tag_prepend(m
, tag
);
4059 return tag
? (struct ipsec_tag
*)(tag
+ 1) : NULL
;
4062 static struct ipsec_tag
*
4068 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPSEC
, NULL
);
4070 return tag
? (struct ipsec_tag
*)(tag
+ 1) : NULL
;
4079 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPSEC
, NULL
);
4082 m_tag_delete(m
, tag
);
4086 /* if the aux buffer is unnecessary, nuke it. */
4090 struct ipsec_tag
*itag
)
4092 if (itag
&& itag
->socket
== NULL
&& itag
->history_count
== 0) {
4093 m_tag_delete(m
, ((struct m_tag
*)itag
) - 1);
4102 struct ipsec_tag
*tag
;
4104 /* if so == NULL, don't insist on getting the aux mbuf */
4106 tag
= ipsec_addaux(m
);
4110 tag
= ipsec_findaux(m
);
4113 ipsec_optaux(m
, tag
);
4122 struct ipsec_tag
*itag
;
4124 itag
= ipsec_findaux(m
);
4126 return itag
->socket
;
4137 struct ipsec_tag
*itag
;
4138 struct ipsec_history
*p
;
4139 itag
= ipsec_addaux(m
);
4142 if (itag
->history_count
== IPSEC_HISTORY_MAX
)
4143 return ENOSPC
; /* XXX */
4145 p
= &itag
->history
[itag
->history_count
];
4146 itag
->history_count
++;
4148 bzero(p
, sizeof(*p
));
4149 p
->ih_proto
= proto
;
4155 struct ipsec_history
*
4160 struct ipsec_tag
*itag
;
4162 itag
= ipsec_findaux(m
);
4165 if (itag
->history_count
== 0)
4168 *lenp
= (int)(itag
->history_count
* sizeof(struct ipsec_history
));
4169 return itag
->history
;
4176 struct ipsec_tag
*itag
;
4178 itag
= ipsec_findaux(m
);
4180 itag
->history_count
= 0;
4182 ipsec_optaux(m
, itag
);
4185 __private_extern__
int
4186 ipsec_send_natt_keepalive(
4187 struct secasvar
*sav
)
4193 struct ip_out_args ipoa
= { IFSCOPE_NONE
, 0 };
4196 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
4198 if ((esp_udp_encap_port
& 0xFFFF) == 0 || sav
->remote_ike_port
== 0) return FALSE
;
4200 // natt timestamp may have changed... reverify
4201 if ((natt_now
- sav
->natt_last_activity
) < natt_keepalive_interval
) return FALSE
;
4203 m
= m_gethdr(M_NOWAIT
, MT_DATA
);
4204 if (m
== NULL
) return FALSE
;
4207 * Create a UDP packet complete with IP header.
4208 * We must do this because UDP output requires
4209 * an inpcb which we don't have. UDP packet
4210 * contains one byte payload. The byte is set
4213 ip
= (struct ip
*)m_mtod(m
);
4214 uh
= (struct udphdr
*)((char*)m_mtod(m
) + sizeof(struct ip
));
4215 m
->m_len
= sizeof(struct udpiphdr
) + 1;
4216 bzero(m_mtod(m
), m
->m_len
);
4217 m
->m_pkthdr
.len
= m
->m_len
;
4219 ip
->ip_len
= m
->m_len
;
4220 ip
->ip_ttl
= ip_defttl
;
4221 ip
->ip_p
= IPPROTO_UDP
;
4222 if (sav
->sah
->dir
!= IPSEC_DIR_INBOUND
) {
4223 ip
->ip_src
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
;
4224 ip
->ip_dst
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
;
4226 ip
->ip_src
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
;
4227 ip
->ip_dst
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
;
4229 uh
->uh_sport
= htons((u_short
)esp_udp_encap_port
);
4230 uh
->uh_dport
= htons(sav
->remote_ike_port
);
4231 uh
->uh_ulen
= htons(1 + sizeof(struct udphdr
));
4233 *(u_int8_t
*)((char*)m_mtod(m
) + sizeof(struct ip
) + sizeof(struct udphdr
)) = 0xFF;
4235 // grab sadb_mutex, to get a local copy of sah's route cache
4236 lck_mtx_lock(sadb_mutex
);
4237 if (sav
->sah
->sa_route
.ro_rt
!= NULL
&&
4238 rt_key(sav
->sah
->sa_route
.ro_rt
)->sa_family
!= AF_INET
) {
4239 rtfree(sav
->sah
->sa_route
.ro_rt
);
4240 sav
->sah
->sa_route
.ro_rt
= NULL
;
4242 route_copyout(&ro
, &sav
->sah
->sa_route
, sizeof(ro
));
4243 lck_mtx_unlock(sadb_mutex
);
4245 error
= ip_output(m
, NULL
, &ro
, IP_OUTARGS
| IP_NOIPSEC
, NULL
, &ipoa
);
4247 // grab sadb_mutex, to synchronize the sah's route cache with the local copy
4248 lck_mtx_lock(sadb_mutex
);
4249 route_copyin(&ro
, &sav
->sah
->sa_route
, sizeof(ro
));
4250 lck_mtx_unlock(sadb_mutex
);
4252 sav
->natt_last_activity
= natt_now
;