1 /* $FreeBSD: src/sys/netinet6/ipsec.c,v 1.3.2.7 2001/07/19 06:37:23 kris Exp $ */
2 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * IPsec controller part.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/syslog.h>
49 #include <sys/sysctl.h>
50 #include <kern/locks.h>
51 #include <sys/kauth.h>
54 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/in_var.h>
61 #include <netinet/udp.h>
62 #include <netinet/udp_var.h>
63 #include <netinet/ip_ecn.h>
65 #include <netinet6/ip6_ecn.h>
67 #include <netinet/tcp.h>
68 #include <netinet/udp.h>
70 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
74 #include <netinet/in_pcb.h>
76 #include <netinet/icmp6.h>
79 #include <netinet6/ipsec.h>
81 #include <netinet6/ipsec6.h>
83 #include <netinet6/ah.h>
85 #include <netinet6/ah6.h>
88 #include <netinet6/esp.h>
90 #include <netinet6/esp6.h>
93 #include <netinet6/ipcomp.h>
95 #include <netinet6/ipcomp6.h>
97 #include <netkey/key.h>
98 #include <netkey/keydb.h>
99 #include <netkey/key_debug.h>
101 #include <net/net_osdep.h>
109 #include <sys/kdebug.h>
110 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
111 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
112 #define DBG_FNC_GETPOL_SOCK NETDBG_CODE(DBG_NETIPSEC, (1 << 8))
113 #define DBG_FNC_GETPOL_ADDR NETDBG_CODE(DBG_NETIPSEC, (2 << 8))
114 #define DBG_FNC_IPSEC_OUT NETDBG_CODE(DBG_NETIPSEC, (3 << 8))
116 extern lck_mtx_t
*sadb_mutex
;
117 extern lck_mtx_t
*ip6_mutex
;
119 struct ipsecstat ipsecstat
;
120 int ip4_ah_cleartos
= 1;
121 int ip4_ah_offsetmask
= 0; /* maybe IP_DF? */
122 int ip4_ipsec_dfbit
= 0; /* DF bit on encap. 0: clear 1: set 2: copy */
123 int ip4_esp_trans_deflev
= IPSEC_LEVEL_USE
;
124 int ip4_esp_net_deflev
= IPSEC_LEVEL_USE
;
125 int ip4_ah_trans_deflev
= IPSEC_LEVEL_USE
;
126 int ip4_ah_net_deflev
= IPSEC_LEVEL_USE
;
127 struct secpolicy ip4_def_policy
;
128 int ip4_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
129 int ip4_esp_randpad
= -1;
130 int esp_udp_encap_port
= 0;
131 static int sysctl_def_policy SYSCTL_HANDLER_ARGS
;
132 extern u_int32_t natt_now
;
134 SYSCTL_DECL(_net_inet_ipsec
);
136 SYSCTL_DECL(_net_inet6_ipsec6
);
139 SYSCTL_STRUCT(_net_inet_ipsec
, IPSECCTL_STATS
,
140 stats
, CTLFLAG_RD
, &ipsecstat
, ipsecstat
, "");
141 SYSCTL_PROC(_net_inet_ipsec
, IPSECCTL_DEF_POLICY
, def_policy
, CTLTYPE_INT
|CTLFLAG_RW
,
142 &ip4_def_policy
.policy
, 0, &sysctl_def_policy
, "I", "");
143 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
144 CTLFLAG_RW
, &ip4_esp_trans_deflev
, 0, "");
145 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
146 CTLFLAG_RW
, &ip4_esp_net_deflev
, 0, "");
147 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
148 CTLFLAG_RW
, &ip4_ah_trans_deflev
, 0, "");
149 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
150 CTLFLAG_RW
, &ip4_ah_net_deflev
, 0, "");
151 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_CLEARTOS
,
152 ah_cleartos
, CTLFLAG_RW
, &ip4_ah_cleartos
, 0, "");
153 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_OFFSETMASK
,
154 ah_offsetmask
, CTLFLAG_RW
, &ip4_ah_offsetmask
, 0, "");
155 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DFBIT
,
156 dfbit
, CTLFLAG_RW
, &ip4_ipsec_dfbit
, 0, "");
157 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ECN
,
158 ecn
, CTLFLAG_RW
, &ip4_ipsec_ecn
, 0, "");
159 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEBUG
,
160 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
161 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ESP_RANDPAD
,
162 esp_randpad
, CTLFLAG_RW
, &ip4_esp_randpad
, 0, "");
164 /* for performance, we bypass ipsec until a security policy is set */
165 int ipsec_bypass
= 1;
166 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, bypass
, CTLFLAG_RD
, &ipsec_bypass
,0, "");
169 * NAT Traversal requires a UDP port for encapsulation,
170 * esp_udp_encap_port controls which port is used. Racoon
171 * must set this port to the port racoon is using locally
174 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, esp_port
,
175 CTLFLAG_RW
, &esp_udp_encap_port
, 0, "");
178 struct ipsecstat ipsec6stat
;
179 int ip6_esp_trans_deflev
= IPSEC_LEVEL_USE
;
180 int ip6_esp_net_deflev
= IPSEC_LEVEL_USE
;
181 int ip6_ah_trans_deflev
= IPSEC_LEVEL_USE
;
182 int ip6_ah_net_deflev
= IPSEC_LEVEL_USE
;
183 struct secpolicy ip6_def_policy
;
184 int ip6_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
185 int ip6_esp_randpad
= -1;
187 /* net.inet6.ipsec6 */
188 SYSCTL_STRUCT(_net_inet6_ipsec6
, IPSECCTL_STATS
,
189 stats
, CTLFLAG_RD
, &ipsec6stat
, ipsecstat
, "");
190 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_POLICY
,
191 def_policy
, CTLFLAG_RW
, &ip6_def_policy
.policy
, 0, "");
192 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
193 CTLFLAG_RW
, &ip6_esp_trans_deflev
, 0, "");
194 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
195 CTLFLAG_RW
, &ip6_esp_net_deflev
, 0, "");
196 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
197 CTLFLAG_RW
, &ip6_ah_trans_deflev
, 0, "");
198 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
199 CTLFLAG_RW
, &ip6_ah_net_deflev
, 0, "");
200 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ECN
,
201 ecn
, CTLFLAG_RW
, &ip6_ipsec_ecn
, 0, "");
202 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEBUG
,
203 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
204 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ESP_RANDPAD
,
205 esp_randpad
, CTLFLAG_RW
, &ip6_esp_randpad
, 0, "");
208 static int ipsec_setspidx_mbuf(struct secpolicyindex
*, u_int
, u_int
,
210 static int ipsec4_setspidx_inpcb(struct mbuf
*, struct inpcb
*pcb
);
212 static int ipsec6_setspidx_in6pcb(struct mbuf
*, struct in6pcb
*pcb
);
214 static int ipsec_setspidx(struct mbuf
*, struct secpolicyindex
*, int);
215 static void ipsec4_get_ulp(struct mbuf
*m
, struct secpolicyindex
*, int);
216 static int ipsec4_setspidx_ipaddr(struct mbuf
*, struct secpolicyindex
*);
218 static void ipsec6_get_ulp(struct mbuf
*m
, struct secpolicyindex
*, int);
219 static int ipsec6_setspidx_ipaddr(struct mbuf
*, struct secpolicyindex
*);
221 static struct inpcbpolicy
*ipsec_newpcbpolicy(void);
222 static void ipsec_delpcbpolicy(struct inpcbpolicy
*);
223 static struct secpolicy
*ipsec_deepcopy_policy(struct secpolicy
*src
);
224 static int ipsec_set_policy(struct secpolicy
**pcb_sp
,
225 int optname
, caddr_t request
, size_t len
, int priv
);
226 static int ipsec_get_policy(struct secpolicy
*pcb_sp
, struct mbuf
**mp
);
227 static void vshiftl(unsigned char *, int, int);
228 static int ipsec_in_reject(struct secpolicy
*, struct mbuf
*);
229 static size_t ipsec_hdrsiz(struct secpolicy
*);
231 static struct mbuf
*ipsec4_splithdr(struct mbuf
*);
234 static struct mbuf
*ipsec6_splithdr(struct mbuf
*);
237 static int ipsec4_encapsulate(struct mbuf
*, struct secasvar
*);
240 static int ipsec6_encapsulate(struct mbuf
*, struct secasvar
*);
242 static struct mbuf
*ipsec_addaux(struct mbuf
*);
243 static struct mbuf
*ipsec_findaux(struct mbuf
*);
244 static void ipsec_optaux(struct mbuf
*, struct mbuf
*);
245 void ipsec_send_natt_keepalive(struct secasvar
*sav
);
248 sysctl_def_policy SYSCTL_HANDLER_ARGS
250 int old_policy
= ip4_def_policy
.policy
;
251 int error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
253 if (ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
&&
254 ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
) {
255 ip4_def_policy
.policy
= old_policy
;
259 /* Turn off the bypass if the default security policy changes */
260 if (ipsec_bypass
!= 0 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
)
267 * For OUTBOUND packet having a socket. Searching SPD for packet,
268 * and return a pointer to SP.
269 * OUT: NULL: no apropreate SP found, the following value is set to error.
271 * EACCES : discard packet.
272 * ENOENT : ipsec_acquire() in progress, maybe.
273 * others : error occurred.
274 * others: a pointer to SP
276 * NOTE: IPv6 mapped adddress concern is implemented here.
279 ipsec4_getpolicybysock(m
, dir
, so
, error
)
285 struct inpcbpolicy
*pcbsp
= NULL
;
286 struct secpolicy
*currsp
= NULL
; /* policy on socket */
287 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
289 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
291 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
292 panic("ipsec4_getpolicybysock: NULL pointer was passed.\n");
294 if (so
->so_pcb
== NULL
) {
295 printf("ipsec4_getpolicybysock: so->so_pcb == NULL\n");
296 return ipsec4_getpolicybyaddr(m
, dir
, 0, error
);
299 switch (so
->so_proto
->pr_domain
->dom_family
) {
301 pcbsp
= sotoinpcb(so
)->inp_sp
;
305 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
311 /* Socket has not specified an IPSEC policy */
312 return ipsec4_getpolicybyaddr(m
, dir
, 0, error
);
315 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_START
, 0,0,0,0,0);
317 switch (so
->so_proto
->pr_domain
->dom_family
) {
319 /* set spidx in pcb */
320 *error
= ipsec4_setspidx_inpcb(m
, sotoinpcb(so
));
324 /* set spidx in pcb */
325 *error
= ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
329 panic("ipsec4_getpolicybysock: unsupported address family\n");
332 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 1,*error
,0,0,0);
338 panic("ipsec4_getpolicybysock: pcbsp is NULL.\n");
341 case IPSEC_DIR_INBOUND
:
342 currsp
= pcbsp
->sp_in
;
344 case IPSEC_DIR_OUTBOUND
:
345 currsp
= pcbsp
->sp_out
;
348 panic("ipsec4_getpolicybysock: illegal direction.\n");
353 panic("ipsec4_getpolicybysock: currsp is NULL.\n");
355 /* when privilieged socket */
357 switch (currsp
->policy
) {
358 case IPSEC_POLICY_BYPASS
:
361 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 2,*error
,0,0,0);
364 case IPSEC_POLICY_ENTRUST
:
365 /* look for a policy in SPD */
366 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
369 if (kernsp
!= NULL
) {
370 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
371 printf("DP ipsec4_getpolicybysock called "
372 "to allocate SP:%p\n", kernsp
));
374 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 3,*error
,0,0,0);
379 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
380 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
382 "fixed system default policy: %d->%d\n",
383 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
384 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
386 ip4_def_policy
.refcnt
++;
388 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 4,*error
,0,0,0);
389 return &ip4_def_policy
;
391 case IPSEC_POLICY_IPSEC
:
394 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 5,*error
,0,0,0);
398 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
399 "Invalid policy for PCB %d\n", currsp
->policy
));
401 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 6,*error
,0,0,0);
407 /* when non-privilieged socket */
408 /* look for a policy in SPD */
409 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
412 if (kernsp
!= NULL
) {
413 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
414 printf("DP ipsec4_getpolicybysock called "
415 "to allocate SP:%p\n", kernsp
));
417 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 7,*error
,0,0,0);
422 switch (currsp
->policy
) {
423 case IPSEC_POLICY_BYPASS
:
424 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
425 "Illegal policy for non-priviliged defined %d\n",
428 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 8,*error
,0,0,0);
431 case IPSEC_POLICY_ENTRUST
:
432 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
433 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
435 "fixed system default policy: %d->%d\n",
436 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
437 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
439 ip4_def_policy
.refcnt
++;
441 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 9,*error
,0,0,0);
442 return &ip4_def_policy
;
444 case IPSEC_POLICY_IPSEC
:
447 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 10,*error
,0,0,0);
451 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
452 "Invalid policy for PCB %d\n", currsp
->policy
));
454 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 11,*error
,0,0,0);
461 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
462 * and return a pointer to SP.
463 * OUT: positive: a pointer to the entry for security policy leaf matched.
464 * NULL: no apropreate SP found, the following value is set to error.
466 * EACCES : discard packet.
467 * ENOENT : ipsec_acquire() in progress, maybe.
468 * others : error occurred.
471 ipsec4_getpolicybyaddr(m
, dir
, flag
, error
)
477 struct secpolicy
*sp
= NULL
;
479 if (ipsec_bypass
!= 0)
482 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
485 if (m
== NULL
|| error
== NULL
)
486 panic("ipsec4_getpolicybyaddr: NULL pointer was passed.\n");
489 struct secpolicyindex spidx
;
491 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_START
, 0,0,0,0,0);
492 bzero(&spidx
, sizeof(spidx
));
494 /* make a index to look for a policy */
495 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET
, m
,
496 (flag
& IP_FORWARDING
) ? 0 : 1);
499 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 1,*error
,0,0,0);
503 sp
= key_allocsp(&spidx
, dir
);
508 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
509 printf("DP ipsec4_getpolicybyaddr called "
510 "to allocate SP:%p\n", sp
));
512 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 2,*error
,0,0,0);
517 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
518 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
519 ipseclog((LOG_INFO
, "fixed system default policy:%d->%d\n",
520 ip4_def_policy
.policy
,
522 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
524 ip4_def_policy
.refcnt
++;
526 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 3,*error
,0,0,0);
527 return &ip4_def_policy
;
532 * For OUTBOUND packet having a socket. Searching SPD for packet,
533 * and return a pointer to SP.
534 * OUT: NULL: no apropreate SP found, the following value is set to error.
536 * EACCES : discard packet.
537 * ENOENT : ipsec_acquire() in progress, maybe.
538 * others : error occurred.
539 * others: a pointer to SP
542 ipsec6_getpolicybysock(m
, dir
, so
, error
)
548 struct inpcbpolicy
*pcbsp
= NULL
;
549 struct secpolicy
*currsp
= NULL
; /* policy on socket */
550 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
552 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
555 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
556 panic("ipsec6_getpolicybysock: NULL pointer was passed.\n");
559 if (so
->so_proto
->pr_domain
->dom_family
!= AF_INET6
)
560 panic("ipsec6_getpolicybysock: socket domain != inet6\n");
563 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
566 return ipsec6_getpolicybyaddr(m
, dir
, 0, error
);
569 /* set spidx in pcb */
570 ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
574 panic("ipsec6_getpolicybysock: pcbsp is NULL.\n");
577 case IPSEC_DIR_INBOUND
:
578 currsp
= pcbsp
->sp_in
;
580 case IPSEC_DIR_OUTBOUND
:
581 currsp
= pcbsp
->sp_out
;
584 panic("ipsec6_getpolicybysock: illegal direction.\n");
589 panic("ipsec6_getpolicybysock: currsp is NULL.\n");
591 /* when privilieged socket */
593 switch (currsp
->policy
) {
594 case IPSEC_POLICY_BYPASS
:
599 case IPSEC_POLICY_ENTRUST
:
600 /* look for a policy in SPD */
601 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
604 if (kernsp
!= NULL
) {
605 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
606 printf("DP ipsec6_getpolicybysock called "
607 "to allocate SP:%p\n", kernsp
));
613 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
614 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
616 "fixed system default policy: %d->%d\n",
617 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
618 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
620 ip6_def_policy
.refcnt
++;
622 return &ip6_def_policy
;
624 case IPSEC_POLICY_IPSEC
:
630 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
631 "Invalid policy for PCB %d\n", currsp
->policy
));
638 /* when non-privilieged socket */
639 /* look for a policy in SPD */
640 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
643 if (kernsp
!= NULL
) {
644 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
645 printf("DP ipsec6_getpolicybysock called "
646 "to allocate SP:%p\n", kernsp
));
652 switch (currsp
->policy
) {
653 case IPSEC_POLICY_BYPASS
:
654 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
655 "Illegal policy for non-priviliged defined %d\n",
660 case IPSEC_POLICY_ENTRUST
:
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
++;
670 return &ip6_def_policy
;
672 case IPSEC_POLICY_IPSEC
:
679 "ipsec6_policybysock: Invalid policy for PCB %d\n",
688 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
689 * and return a pointer to SP.
690 * `flag' means that packet is to be forwarded whether or not.
692 * OUT: positive: a pointer to the entry for security policy leaf matched.
693 * NULL: no apropreate SP found, the following value is set to error.
695 * EACCES : discard packet.
696 * ENOENT : ipsec_acquire() in progress, maybe.
697 * others : error occurred.
699 #ifndef IP_FORWARDING
700 #define IP_FORWARDING 1
704 ipsec6_getpolicybyaddr(m
, dir
, flag
, error
)
710 struct secpolicy
*sp
= NULL
;
712 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
715 if (m
== NULL
|| error
== NULL
)
716 panic("ipsec6_getpolicybyaddr: NULL pointer was passed.\n");
719 struct secpolicyindex spidx
;
721 bzero(&spidx
, sizeof(spidx
));
723 /* make a index to look for a policy */
724 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET6
, m
,
725 (flag
& IP_FORWARDING
) ? 0 : 1);
730 sp
= key_allocsp(&spidx
, dir
);
735 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
736 printf("DP ipsec6_getpolicybyaddr called "
737 "to allocate SP:%p\n", sp
));
743 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
744 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
745 ipseclog((LOG_INFO
, "fixed system default policy: %d->%d\n",
746 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
747 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
749 ip6_def_policy
.refcnt
++;
751 return &ip6_def_policy
;
756 * set IP address into spidx from mbuf.
757 * When Forwarding packet and ICMP echo reply, this function is used.
759 * IN: get the followings from mbuf.
760 * protocol family, src, dst, next protocol
763 * other: failure, and set errno.
766 ipsec_setspidx_mbuf(spidx
, dir
, family
, m
, needport
)
767 struct secpolicyindex
*spidx
;
775 if (spidx
== NULL
|| m
== NULL
)
776 panic("ipsec_setspidx_mbuf: NULL pointer was passed.\n");
778 bzero(spidx
, sizeof(*spidx
));
780 error
= ipsec_setspidx(m
, spidx
, needport
);
789 bzero(spidx
, sizeof(*spidx
));
794 ipsec4_setspidx_inpcb(m
, pcb
)
798 struct secpolicyindex
*spidx
;
801 if (ipsec_bypass
!= 0)
806 panic("ipsec4_setspidx_inpcb: no PCB found.\n");
807 if (pcb
->inp_sp
== NULL
)
808 panic("ipsec4_setspidx_inpcb: no inp_sp found.\n");
809 if (pcb
->inp_sp
->sp_out
== NULL
|| pcb
->inp_sp
->sp_in
== NULL
)
810 panic("ipsec4_setspidx_inpcb: no sp_in/out found.\n");
812 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
813 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
815 spidx
= &pcb
->inp_sp
->sp_in
->spidx
;
816 error
= ipsec_setspidx(m
, spidx
, 1);
819 spidx
->dir
= IPSEC_DIR_INBOUND
;
821 spidx
= &pcb
->inp_sp
->sp_out
->spidx
;
822 error
= ipsec_setspidx(m
, spidx
, 1);
825 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
830 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
831 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
837 ipsec6_setspidx_in6pcb(m
, pcb
)
841 struct secpolicyindex
*spidx
;
846 panic("ipsec6_setspidx_in6pcb: no PCB found.\n");
847 if (pcb
->in6p_sp
== NULL
)
848 panic("ipsec6_setspidx_in6pcb: no in6p_sp found.\n");
849 if (pcb
->in6p_sp
->sp_out
== NULL
|| pcb
->in6p_sp
->sp_in
== NULL
)
850 panic("ipsec6_setspidx_in6pcb: no sp_in/out found.\n");
852 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
853 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
855 spidx
= &pcb
->in6p_sp
->sp_in
->spidx
;
856 error
= ipsec_setspidx(m
, spidx
, 1);
859 spidx
->dir
= IPSEC_DIR_INBOUND
;
861 spidx
= &pcb
->in6p_sp
->sp_out
->spidx
;
862 error
= ipsec_setspidx(m
, spidx
, 1);
865 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
870 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
871 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
877 * configure security policy index (src/dst/proto/sport/dport)
878 * by looking at the content of mbuf.
879 * the caller is responsible for error recovery (like clearing up spidx).
882 ipsec_setspidx(m
, spidx
, needport
)
884 struct secpolicyindex
*spidx
;
887 struct ip
*ip
= NULL
;
895 panic("ipsec_setspidx: m == 0 passed.\n");
898 * validate m->m_pkthdr.len. we see incorrect length if we
899 * mistakenly call this function with inconsistent mbuf chain
900 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
903 for (n
= m
; n
; n
= n
->m_next
)
905 if (m
->m_pkthdr
.len
!= len
) {
906 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
907 printf("ipsec_setspidx: "
908 "total of m_len(%d) != pkthdr.len(%d), "
910 len
, m
->m_pkthdr
.len
));
914 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
915 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
916 printf("ipsec_setspidx: "
917 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
922 if (m
->m_len
>= sizeof(*ip
))
923 ip
= mtod(m
, struct ip
*);
925 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
929 v
= _IP_VHL_V(ip
->ip_vhl
);
935 error
= ipsec4_setspidx_ipaddr(m
, spidx
);
938 ipsec4_get_ulp(m
, spidx
, needport
);
942 if (m
->m_pkthdr
.len
< sizeof(struct ip6_hdr
)) {
943 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
944 printf("ipsec_setspidx: "
945 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
946 "ignored.\n", m
->m_pkthdr
.len
));
949 error
= ipsec6_setspidx_ipaddr(m
, spidx
);
952 ipsec6_get_ulp(m
, spidx
, needport
);
956 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
957 printf("ipsec_setspidx: "
958 "unknown IP version %u, ignored.\n", v
));
964 ipsec4_get_ulp(m
, spidx
, needport
)
966 struct secpolicyindex
*spidx
;
978 panic("ipsec4_get_ulp: NULL pointer was passed.\n");
979 if (m
->m_pkthdr
.len
< sizeof(ip
))
980 panic("ipsec4_get_ulp: too short\n");
983 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
984 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
= IPSEC_PORT_ANY
;
985 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
= IPSEC_PORT_ANY
;
987 m_copydata(m
, 0, sizeof(ip
), (caddr_t
)&ip
);
988 /* ip_input() flips it into host endian XXX need more checking */
989 if (ip
.ip_off
& (IP_MF
| IP_OFFMASK
))
994 off
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
998 while (off
< m
->m_pkthdr
.len
) {
1001 spidx
->ul_proto
= nxt
;
1004 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
1006 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
1007 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
1009 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
1013 spidx
->ul_proto
= nxt
;
1016 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1018 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1019 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
1021 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
1025 if (off
+ sizeof(ip6e
) > m
->m_pkthdr
.len
)
1027 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1028 off
+= (ip6e
.ip6e_len
+ 2) << 2;
1029 nxt
= ip6e
.ip6e_nxt
;
1033 /* XXX intermediate headers??? */
1034 spidx
->ul_proto
= nxt
;
1040 /* assumes that m is sane */
1042 ipsec4_setspidx_ipaddr(m
, spidx
)
1044 struct secpolicyindex
*spidx
;
1046 struct ip
*ip
= NULL
;
1048 struct sockaddr_in
*sin
;
1050 if (m
->m_len
>= sizeof(*ip
))
1051 ip
= mtod(m
, struct ip
*);
1053 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
1057 sin
= (struct sockaddr_in
*)&spidx
->src
;
1058 bzero(sin
, sizeof(*sin
));
1059 sin
->sin_family
= AF_INET
;
1060 sin
->sin_len
= sizeof(struct sockaddr_in
);
1061 bcopy(&ip
->ip_src
, &sin
->sin_addr
, sizeof(ip
->ip_src
));
1062 spidx
->prefs
= sizeof(struct in_addr
) << 3;
1064 sin
= (struct sockaddr_in
*)&spidx
->dst
;
1065 bzero(sin
, sizeof(*sin
));
1066 sin
->sin_family
= AF_INET
;
1067 sin
->sin_len
= sizeof(struct sockaddr_in
);
1068 bcopy(&ip
->ip_dst
, &sin
->sin_addr
, sizeof(ip
->ip_dst
));
1069 spidx
->prefd
= sizeof(struct in_addr
) << 3;
1075 ipsec6_get_ulp(m
, spidx
, needport
)
1077 struct secpolicyindex
*spidx
;
1086 panic("ipsec6_get_ulp: NULL pointer was passed.\n");
1088 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1089 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m
));
1092 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
1093 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= IPSEC_PORT_ANY
;
1094 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= IPSEC_PORT_ANY
;
1097 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
1098 if (off
< 0 || m
->m_pkthdr
.len
< off
)
1103 spidx
->ul_proto
= nxt
;
1106 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
1108 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
1109 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= th
.th_sport
;
1110 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= th
.th_dport
;
1113 spidx
->ul_proto
= nxt
;
1116 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1118 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1119 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= uh
.uh_sport
;
1120 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= uh
.uh_dport
;
1122 case IPPROTO_ICMPV6
:
1124 /* XXX intermediate headers??? */
1125 spidx
->ul_proto
= nxt
;
1130 /* assumes that m is sane */
1132 ipsec6_setspidx_ipaddr(m
, spidx
)
1134 struct secpolicyindex
*spidx
;
1136 struct ip6_hdr
*ip6
= NULL
;
1137 struct ip6_hdr ip6buf
;
1138 struct sockaddr_in6
*sin6
;
1140 if (m
->m_len
>= sizeof(*ip6
))
1141 ip6
= mtod(m
, struct ip6_hdr
*);
1143 m_copydata(m
, 0, sizeof(ip6buf
), (caddr_t
)&ip6buf
);
1147 sin6
= (struct sockaddr_in6
*)&spidx
->src
;
1148 bzero(sin6
, sizeof(*sin6
));
1149 sin6
->sin6_family
= AF_INET6
;
1150 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1151 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_src
));
1152 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1153 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1154 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
1156 spidx
->prefs
= sizeof(struct in6_addr
) << 3;
1158 sin6
= (struct sockaddr_in6
*)&spidx
->dst
;
1159 bzero(sin6
, sizeof(*sin6
));
1160 sin6
->sin6_family
= AF_INET6
;
1161 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1162 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_dst
));
1163 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
1164 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1165 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
1167 spidx
->prefd
= sizeof(struct in6_addr
) << 3;
1173 static struct inpcbpolicy
*
1174 ipsec_newpcbpolicy()
1176 struct inpcbpolicy
*p
;
1178 p
= (struct inpcbpolicy
*)_MALLOC(sizeof(*p
), M_SECA
, M_WAITOK
);
1183 ipsec_delpcbpolicy(p
)
1184 struct inpcbpolicy
*p
;
1189 /* initialize policy in PCB */
1191 ipsec_init_policy(so
, pcb_sp
)
1193 struct inpcbpolicy
**pcb_sp
;
1195 struct inpcbpolicy
*new;
1197 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1200 if (so
== NULL
|| pcb_sp
== NULL
)
1201 panic("ipsec_init_policy: NULL pointer was passed.\n");
1203 new = ipsec_newpcbpolicy();
1205 ipseclog((LOG_DEBUG
, "ipsec_init_policy: No more memory.\n"));
1208 bzero(new, sizeof(*new));
1211 if (so
->so_uid
== 0)
1213 if (so
->so_cred
!= 0 && !suser(so
->so_cred
->pc_ucred
, NULL
))
1219 if ((new->sp_in
= key_newsp()) == NULL
) {
1220 ipsec_delpcbpolicy(new);
1223 new->sp_in
->state
= IPSEC_SPSTATE_ALIVE
;
1224 new->sp_in
->policy
= IPSEC_POLICY_ENTRUST
;
1226 if ((new->sp_out
= key_newsp()) == NULL
) {
1227 key_freesp(new->sp_in
);
1228 ipsec_delpcbpolicy(new);
1231 new->sp_out
->state
= IPSEC_SPSTATE_ALIVE
;
1232 new->sp_out
->policy
= IPSEC_POLICY_ENTRUST
;
1239 /* copy old ipsec policy into new */
1241 ipsec_copy_policy(old
, new)
1242 struct inpcbpolicy
*old
, *new;
1244 struct secpolicy
*sp
;
1246 if (ipsec_bypass
!= 0)
1249 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1251 sp
= ipsec_deepcopy_policy(old
->sp_in
);
1253 key_freesp(new->sp_in
);
1258 sp
= ipsec_deepcopy_policy(old
->sp_out
);
1260 key_freesp(new->sp_out
);
1265 new->priv
= old
->priv
;
1270 /* deep-copy a policy in PCB */
1271 static struct secpolicy
*
1272 ipsec_deepcopy_policy(src
)
1273 struct secpolicy
*src
;
1275 struct ipsecrequest
*newchain
= NULL
;
1276 struct ipsecrequest
*p
;
1277 struct ipsecrequest
**q
;
1278 struct ipsecrequest
*r
;
1279 struct secpolicy
*dst
;
1282 if (src
== NULL
|| dst
== NULL
)
1286 * deep-copy IPsec request chain. This is required since struct
1287 * ipsecrequest is not reference counted.
1290 for (p
= src
->req
; p
; p
= p
->next
) {
1291 *q
= (struct ipsecrequest
*)_MALLOC(sizeof(struct ipsecrequest
),
1295 bzero(*q
, sizeof(**q
));
1298 (*q
)->saidx
.proto
= p
->saidx
.proto
;
1299 (*q
)->saidx
.mode
= p
->saidx
.mode
;
1300 (*q
)->level
= p
->level
;
1301 (*q
)->saidx
.reqid
= p
->saidx
.reqid
;
1303 bcopy(&p
->saidx
.src
, &(*q
)->saidx
.src
, sizeof((*q
)->saidx
.src
));
1304 bcopy(&p
->saidx
.dst
, &(*q
)->saidx
.dst
, sizeof((*q
)->saidx
.dst
));
1312 dst
->req
= newchain
;
1313 dst
->state
= src
->state
;
1314 dst
->policy
= src
->policy
;
1315 /* do not touch the refcnt fields */
1320 for (p
= newchain
; p
; p
= r
) {
1328 /* set policy and ipsec request if present. */
1330 ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
)
1331 struct secpolicy
**pcb_sp
;
1337 struct sadb_x_policy
*xpl
;
1338 struct secpolicy
*newsp
= NULL
;
1341 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1344 if (pcb_sp
== NULL
|| *pcb_sp
== NULL
|| request
== NULL
)
1346 if (len
< sizeof(*xpl
))
1348 xpl
= (struct sadb_x_policy
*)request
;
1350 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1351 printf("ipsec_set_policy: passed policy\n");
1352 kdebug_sadb_x_policy((struct sadb_ext
*)xpl
));
1354 /* check policy type */
1355 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1356 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
1357 || xpl
->sadb_x_policy_type
== IPSEC_POLICY_NONE
)
1360 /* check privileged socket */
1361 if (priv
== 0 && xpl
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
)
1364 /* allocation new SP entry */
1365 if ((newsp
= key_msg2sp(xpl
, len
, &error
)) == NULL
)
1368 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1370 /* clear old SP and set new SP */
1371 key_freesp(*pcb_sp
);
1373 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1374 printf("ipsec_set_policy: new policy\n");
1375 kdebug_secpolicy(newsp
));
1381 ipsec_get_policy(pcb_sp
, mp
)
1382 struct secpolicy
*pcb_sp
;
1386 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1389 if (pcb_sp
== NULL
|| mp
== NULL
)
1392 *mp
= key_sp2msg(pcb_sp
);
1394 ipseclog((LOG_DEBUG
, "ipsec_get_policy: No more memory.\n"));
1398 (*mp
)->m_type
= MT_DATA
;
1399 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1400 printf("ipsec_get_policy:\n");
1407 ipsec4_set_policy(inp
, optname
, request
, len
, priv
)
1414 struct sadb_x_policy
*xpl
;
1415 struct secpolicy
**pcb_sp
;
1418 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1421 if (inp
== NULL
|| request
== NULL
)
1423 if (len
< sizeof(*xpl
))
1425 xpl
= (struct sadb_x_policy
*)request
;
1427 if (inp
->inp_sp
== NULL
) {
1428 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1433 /* select direction */
1434 switch (xpl
->sadb_x_policy_dir
) {
1435 case IPSEC_DIR_INBOUND
:
1436 pcb_sp
= &inp
->inp_sp
->sp_in
;
1438 case IPSEC_DIR_OUTBOUND
:
1439 pcb_sp
= &inp
->inp_sp
->sp_out
;
1442 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1443 xpl
->sadb_x_policy_dir
));
1447 /* turn bypass off */
1448 if (ipsec_bypass
!= 0)
1451 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1455 ipsec4_get_policy(inp
, request
, len
, mp
)
1461 struct sadb_x_policy
*xpl
;
1462 struct secpolicy
*pcb_sp
;
1465 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1468 if (inp
== NULL
|| request
== NULL
|| mp
== NULL
)
1470 if (len
< sizeof(*xpl
))
1472 xpl
= (struct sadb_x_policy
*)request
;
1474 if (inp
->inp_sp
== NULL
) {
1475 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1480 /* select direction */
1481 switch (xpl
->sadb_x_policy_dir
) {
1482 case IPSEC_DIR_INBOUND
:
1483 pcb_sp
= inp
->inp_sp
->sp_in
;
1485 case IPSEC_DIR_OUTBOUND
:
1486 pcb_sp
= inp
->inp_sp
->sp_out
;
1489 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1490 xpl
->sadb_x_policy_dir
));
1494 return ipsec_get_policy(pcb_sp
, mp
);
1497 /* delete policy in PCB */
1499 ipsec4_delete_pcbpolicy(inp
)
1502 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1506 panic("ipsec4_delete_pcbpolicy: NULL pointer was passed.\n");
1508 if (inp
->inp_sp
== NULL
)
1511 if (inp
->inp_sp
->sp_in
!= NULL
) {
1512 key_freesp(inp
->inp_sp
->sp_in
);
1513 inp
->inp_sp
->sp_in
= NULL
;
1516 if (inp
->inp_sp
->sp_out
!= NULL
) {
1517 key_freesp(inp
->inp_sp
->sp_out
);
1518 inp
->inp_sp
->sp_out
= NULL
;
1521 ipsec_delpcbpolicy(inp
->inp_sp
);
1529 ipsec6_set_policy(in6p
, optname
, request
, len
, priv
)
1530 struct in6pcb
*in6p
;
1536 struct sadb_x_policy
*xpl
;
1537 struct secpolicy
**pcb_sp
;
1540 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1543 if (in6p
== NULL
|| request
== NULL
)
1545 if (len
< sizeof(*xpl
))
1547 xpl
= (struct sadb_x_policy
*)request
;
1549 if (in6p
->in6p_sp
== NULL
) {
1550 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1555 /* select direction */
1556 switch (xpl
->sadb_x_policy_dir
) {
1557 case IPSEC_DIR_INBOUND
:
1558 pcb_sp
= &in6p
->in6p_sp
->sp_in
;
1560 case IPSEC_DIR_OUTBOUND
:
1561 pcb_sp
= &in6p
->in6p_sp
->sp_out
;
1564 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1565 xpl
->sadb_x_policy_dir
));
1569 /* turn bypass off */
1570 if (ipsec_bypass
!= 0)
1573 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1577 ipsec6_get_policy(in6p
, request
, len
, mp
)
1578 struct in6pcb
*in6p
;
1583 struct sadb_x_policy
*xpl
;
1584 struct secpolicy
*pcb_sp
;
1587 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1590 if (in6p
== NULL
|| request
== NULL
|| mp
== NULL
)
1592 if (len
< sizeof(*xpl
))
1594 xpl
= (struct sadb_x_policy
*)request
;
1596 if (in6p
->in6p_sp
== NULL
) {
1597 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1602 /* select direction */
1603 switch (xpl
->sadb_x_policy_dir
) {
1604 case IPSEC_DIR_INBOUND
:
1605 pcb_sp
= in6p
->in6p_sp
->sp_in
;
1607 case IPSEC_DIR_OUTBOUND
:
1608 pcb_sp
= in6p
->in6p_sp
->sp_out
;
1611 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1612 xpl
->sadb_x_policy_dir
));
1616 return ipsec_get_policy(pcb_sp
, mp
);
1620 ipsec6_delete_pcbpolicy(in6p
)
1621 struct in6pcb
*in6p
;
1623 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1627 panic("ipsec6_delete_pcbpolicy: NULL pointer was passed.\n");
1629 if (in6p
->in6p_sp
== NULL
)
1632 if (in6p
->in6p_sp
->sp_in
!= NULL
) {
1633 key_freesp(in6p
->in6p_sp
->sp_in
);
1634 in6p
->in6p_sp
->sp_in
= NULL
;
1637 if (in6p
->in6p_sp
->sp_out
!= NULL
) {
1638 key_freesp(in6p
->in6p_sp
->sp_out
);
1639 in6p
->in6p_sp
->sp_out
= NULL
;
1642 ipsec_delpcbpolicy(in6p
->in6p_sp
);
1643 in6p
->in6p_sp
= NULL
;
1650 * return current level.
1651 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1654 ipsec_get_reqlevel(isr
)
1655 struct ipsecrequest
*isr
;
1658 u_int esp_trans_deflev
, esp_net_deflev
, ah_trans_deflev
, ah_net_deflev
;
1660 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1663 if (isr
== NULL
|| isr
->sp
== NULL
)
1664 panic("ipsec_get_reqlevel: NULL pointer is passed.\n");
1665 if (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
1666 != ((struct sockaddr
*)&isr
->sp
->spidx
.dst
)->sa_family
)
1667 panic("ipsec_get_reqlevel: family mismatched.\n");
1669 /* XXX note that we have ipseclog() expanded here - code sync issue */
1670 #define IPSEC_CHECK_DEFAULT(lev) \
1671 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1672 && (lev) != IPSEC_LEVEL_UNIQUE) \
1674 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1675 (lev), IPSEC_LEVEL_REQUIRE) \
1677 (lev) = IPSEC_LEVEL_REQUIRE, \
1681 /* set default level */
1682 switch (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
) {
1685 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev
);
1686 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev
);
1687 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev
);
1688 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev
);
1693 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev
);
1694 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev
);
1695 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev
);
1696 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev
);
1700 panic("key_get_reqlevel: Unknown family. %d\n",
1701 ((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
);
1704 #undef IPSEC_CHECK_DEFAULT
1707 switch (isr
->level
) {
1708 case IPSEC_LEVEL_DEFAULT
:
1709 switch (isr
->saidx
.proto
) {
1711 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1712 level
= esp_net_deflev
;
1714 level
= esp_trans_deflev
;
1717 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1718 level
= ah_net_deflev
;
1720 level
= ah_trans_deflev
;
1722 case IPPROTO_IPCOMP
:
1724 * we don't really care, as IPcomp document says that
1725 * we shouldn't compress small packets
1727 level
= IPSEC_LEVEL_USE
;
1730 panic("ipsec_get_reqlevel: "
1731 "Illegal protocol defined %u\n",
1736 case IPSEC_LEVEL_USE
:
1737 case IPSEC_LEVEL_REQUIRE
:
1740 case IPSEC_LEVEL_UNIQUE
:
1741 level
= IPSEC_LEVEL_REQUIRE
;
1745 panic("ipsec_get_reqlevel: Illegal IPsec level %u\n",
1753 * Check AH/ESP integrity.
1759 ipsec_in_reject(sp
, m
)
1760 struct secpolicy
*sp
;
1763 struct ipsecrequest
*isr
;
1765 int need_auth
, need_conf
, need_icv
;
1767 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1768 printf("ipsec_in_reject: using SP\n");
1769 kdebug_secpolicy(sp
));
1771 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1774 switch (sp
->policy
) {
1775 case IPSEC_POLICY_DISCARD
:
1777 case IPSEC_POLICY_BYPASS
:
1778 case IPSEC_POLICY_NONE
:
1781 case IPSEC_POLICY_IPSEC
:
1784 case IPSEC_POLICY_ENTRUST
:
1786 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1793 /* XXX should compare policy against ipsec header history */
1795 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1797 /* get current level */
1798 level
= ipsec_get_reqlevel(isr
);
1800 switch (isr
->saidx
.proto
) {
1802 if (level
== IPSEC_LEVEL_REQUIRE
) {
1805 if (isr
->sav
!= NULL
1806 && isr
->sav
->flags
== SADB_X_EXT_NONE
1807 && isr
->sav
->alg_auth
!= SADB_AALG_NONE
)
1812 if (level
== IPSEC_LEVEL_REQUIRE
) {
1817 case IPPROTO_IPCOMP
:
1819 * we don't really care, as IPcomp document says that
1820 * we shouldn't compress small packets, IPComp policy
1821 * should always be treated as being in "use" level.
1827 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1828 printf("ipsec_in_reject: auth:%d conf:%d icv:%d m_flags:%x\n",
1829 need_auth
, need_conf
, need_icv
, m
->m_flags
));
1831 if ((need_conf
&& !(m
->m_flags
& M_DECRYPTED
))
1832 || (!need_auth
&& need_icv
&& !(m
->m_flags
& M_AUTHIPDGM
))
1833 || (need_auth
&& !(m
->m_flags
& M_AUTHIPHDR
)))
1840 * Check AH/ESP integrity.
1841 * This function is called from tcp_input(), udp_input(),
1842 * and {ah,esp}4_input for tunnel mode
1845 ipsec4_in_reject_so(m
, so
)
1849 struct secpolicy
*sp
= NULL
;
1853 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1857 return 0; /* XXX should be panic ? */
1859 /* get SP for this packet.
1860 * When we are called from ip_forward(), we call
1861 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1864 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1866 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1869 return 0; /* XXX should be panic ?
1870 * -> No, there may be error. */
1872 result
= ipsec_in_reject(sp
, m
);
1873 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1874 printf("DP ipsec4_in_reject_so call free SP:%p\n", sp
));
1881 ipsec4_in_reject(m
, inp
)
1885 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1888 return ipsec4_in_reject_so(m
, NULL
);
1889 if (inp
->inp_socket
)
1890 return ipsec4_in_reject_so(m
, inp
->inp_socket
);
1892 panic("ipsec4_in_reject: invalid inpcb/socket");
1900 * Check AH/ESP integrity.
1901 * This function is called from tcp6_input(), udp6_input(),
1902 * and {ah,esp}6_input for tunnel mode
1905 ipsec6_in_reject_so(m
, so
)
1909 struct secpolicy
*sp
= NULL
;
1913 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1917 return 0; /* XXX should be panic ? */
1919 /* get SP for this packet.
1920 * When we are called from ip_forward(), we call
1921 * ipsec6_getpolicybyaddr() with IP_FORWARDING flag.
1924 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1926 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1929 return 0; /* XXX should be panic ? */
1931 result
= ipsec_in_reject(sp
, m
);
1932 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1933 printf("DP ipsec6_in_reject_so call free SP:%p\n", sp
));
1940 ipsec6_in_reject(m
, in6p
)
1942 struct in6pcb
*in6p
;
1944 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1947 return ipsec6_in_reject_so(m
, NULL
);
1948 if (in6p
->in6p_socket
)
1949 return ipsec6_in_reject_so(m
, in6p
->in6p_socket
);
1951 panic("ipsec6_in_reject: invalid in6p/socket");
1959 * compute the byte size to be occupied by IPsec header.
1960 * in case it is tunneled, it includes the size of outer IP header.
1961 * NOTE: SP passed is free in this function.
1965 struct secpolicy
*sp
;
1967 struct ipsecrequest
*isr
;
1970 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1971 printf("ipsec_hdrsiz: using SP\n");
1972 kdebug_secpolicy(sp
));
1974 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
1977 switch (sp
->policy
) {
1978 case IPSEC_POLICY_DISCARD
:
1979 case IPSEC_POLICY_BYPASS
:
1980 case IPSEC_POLICY_NONE
:
1983 case IPSEC_POLICY_IPSEC
:
1986 case IPSEC_POLICY_ENTRUST
:
1988 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1993 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1997 switch (isr
->saidx
.proto
) {
2000 clen
= esp_hdrsiz(isr
);
2006 clen
= ah_hdrsiz(isr
);
2008 case IPPROTO_IPCOMP
:
2009 clen
= sizeof(struct ipcomp
);
2013 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2014 switch (((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
) {
2016 clen
+= sizeof(struct ip
);
2020 clen
+= sizeof(struct ip6_hdr
);
2024 ipseclog((LOG_ERR
, "ipsec_hdrsiz: "
2025 "unknown AF %d in IPsec tunnel SA\n",
2026 ((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
));
2036 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
2038 ipsec4_hdrsiz(m
, dir
, inp
)
2043 struct secpolicy
*sp
= NULL
;
2047 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2051 return 0; /* XXX should be panic ? */
2052 if (inp
!= NULL
&& inp
->inp_socket
== NULL
)
2053 panic("ipsec4_hdrsize: why is socket NULL but there is PCB.");
2055 /* get SP for this packet.
2056 * When we are called from ip_forward(), we call
2057 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
2060 sp
= ipsec4_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2062 sp
= ipsec4_getpolicybysock(m
, dir
, inp
->inp_socket
, &error
);
2065 return 0; /* XXX should be panic ? */
2067 size
= ipsec_hdrsiz(sp
);
2068 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2069 printf("DP ipsec4_hdrsiz call free SP:%p\n", sp
));
2070 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2071 printf("ipsec4_hdrsiz: size:%lu.\n", (unsigned long)size
));
2078 /* This function is called from ipsec6_hdrsize_tcp(),
2079 * and maybe from ip6_forward.()
2082 ipsec6_hdrsiz(m
, dir
, in6p
)
2085 struct in6pcb
*in6p
;
2087 struct secpolicy
*sp
= NULL
;
2091 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2095 return 0; /* XXX shoud be panic ? */
2096 if (in6p
!= NULL
&& in6p
->in6p_socket
== NULL
)
2097 panic("ipsec6_hdrsize: why is socket NULL but there is PCB.");
2099 /* get SP for this packet */
2100 /* XXX Is it right to call with IP_FORWARDING. */
2102 sp
= ipsec6_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2104 sp
= ipsec6_getpolicybysock(m
, dir
, in6p
->in6p_socket
, &error
);
2108 size
= ipsec_hdrsiz(sp
);
2109 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2110 printf("DP ipsec6_hdrsiz call free SP:%p\n", sp
));
2111 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2112 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size
));
2121 * encapsulate for ipsec tunnel.
2122 * ip->ip_src must be fixed later on.
2125 ipsec4_encapsulate(m
, sav
)
2127 struct secasvar
*sav
;
2134 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2136 /* can't tunnel between different AFs */
2137 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2138 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2139 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2144 /* XXX if the dst is myself, perform nothing. */
2145 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2151 if (m
->m_len
< sizeof(*ip
))
2152 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2154 ip
= mtod(m
, struct ip
*);
2156 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
2158 hlen
= ip
->ip_hl
<< 2;
2161 if (m
->m_len
!= hlen
)
2162 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2164 /* generate header checksum */
2167 ip
->ip_sum
= in_cksum(m
, hlen
);
2169 ip
->ip_sum
= in_cksum(m
, hlen
);
2172 plen
= m
->m_pkthdr
.len
;
2175 * grow the mbuf to accomodate the new IPv4 header.
2176 * NOTE: IPv4 options will never be copied.
2178 if (M_LEADINGSPACE(m
->m_next
) < hlen
) {
2180 MGET(n
, M_DONTWAIT
, MT_DATA
);
2186 n
->m_next
= m
->m_next
;
2188 m
->m_pkthdr
.len
+= hlen
;
2189 oip
= mtod(n
, struct ip
*);
2191 m
->m_next
->m_len
+= hlen
;
2192 m
->m_next
->m_data
-= hlen
;
2193 m
->m_pkthdr
.len
+= hlen
;
2194 oip
= mtod(m
->m_next
, struct ip
*);
2196 ip
= mtod(m
, struct ip
*);
2197 ovbcopy((caddr_t
)ip
, (caddr_t
)oip
, hlen
);
2198 m
->m_len
= sizeof(struct ip
);
2199 m
->m_pkthdr
.len
-= (hlen
- sizeof(struct ip
));
2201 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
2202 /* ECN consideration. */
2203 ip_ecn_ingress(ip4_ipsec_ecn
, &ip
->ip_tos
, &oip
->ip_tos
);
2205 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2207 ip
->ip_hl
= sizeof(struct ip
) >> 2;
2209 ip
->ip_off
&= htons(~IP_OFFMASK
);
2210 ip
->ip_off
&= htons(~IP_MF
);
2211 switch (ip4_ipsec_dfbit
) {
2212 case 0: /* clear DF bit */
2213 ip
->ip_off
&= htons(~IP_DF
);
2215 case 1: /* set DF bit */
2216 ip
->ip_off
|= htons(IP_DF
);
2218 default: /* copy DF bit */
2221 ip
->ip_p
= IPPROTO_IPIP
;
2222 if (plen
+ sizeof(struct ip
) < IP_MAXPACKET
)
2223 ip
->ip_len
= htons(plen
+ sizeof(struct ip
));
2225 ipseclog((LOG_ERR
, "IPv4 ipsec: size exceeds limit: "
2226 "leave ip_len as is (invalid packet)\n"));
2229 ip
->ip_id
= ip_randomid();
2231 ip
->ip_id
= htons(ip_id
++);
2233 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
,
2234 &ip
->ip_src
, sizeof(ip
->ip_src
));
2235 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
,
2236 &ip
->ip_dst
, sizeof(ip
->ip_dst
));
2237 ip
->ip_ttl
= IPDEFTTL
;
2239 /* XXX Should ip_src be updated later ? */
2247 ipsec6_encapsulate(m
, sav
)
2249 struct secasvar
*sav
;
2251 struct ip6_hdr
*oip6
;
2252 struct ip6_hdr
*ip6
;
2255 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2257 /* can't tunnel between different AFs */
2258 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2259 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2260 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
2265 /* XXX if the dst is myself, perform nothing. */
2266 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2272 plen
= m
->m_pkthdr
.len
;
2275 * grow the mbuf to accomodate the new IPv6 header.
2277 if (m
->m_len
!= sizeof(struct ip6_hdr
))
2278 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2279 if (M_LEADINGSPACE(m
->m_next
) < sizeof(struct ip6_hdr
)) {
2281 MGET(n
, M_DONTWAIT
, MT_DATA
);
2286 n
->m_len
= sizeof(struct ip6_hdr
);
2287 n
->m_next
= m
->m_next
;
2289 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2290 oip6
= mtod(n
, struct ip6_hdr
*);
2292 m
->m_next
->m_len
+= sizeof(struct ip6_hdr
);
2293 m
->m_next
->m_data
-= sizeof(struct ip6_hdr
);
2294 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2295 oip6
= mtod(m
->m_next
, struct ip6_hdr
*);
2297 ip6
= mtod(m
, struct ip6_hdr
*);
2298 ovbcopy((caddr_t
)ip6
, (caddr_t
)oip6
, sizeof(struct ip6_hdr
));
2300 /* Fake link-local scope-class addresses */
2301 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
2302 oip6
->ip6_src
.s6_addr16
[1] = 0;
2303 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
2304 oip6
->ip6_dst
.s6_addr16
[1] = 0;
2306 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
2307 /* ECN consideration. */
2308 ip6_ecn_ingress(ip6_ipsec_ecn
, &ip6
->ip6_flow
, &oip6
->ip6_flow
);
2309 if (plen
< IPV6_MAXPACKET
- sizeof(struct ip6_hdr
))
2310 ip6
->ip6_plen
= htons(plen
);
2312 /* ip6->ip6_plen will be updated in ip6_output() */
2314 ip6
->ip6_nxt
= IPPROTO_IPV6
;
2315 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.src
)->sin6_addr
,
2316 &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
2317 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
)->sin6_addr
,
2318 &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
2319 ip6
->ip6_hlim
= IPV6_DEFHLIM
;
2321 /* XXX Should ip6_src be updated later ? */
2328 * Check the variable replay window.
2329 * ipsec_chkreplay() performs replay check before ICV verification.
2330 * ipsec_updatereplay() updates replay bitmap. This must be called after
2331 * ICV verification (it also performs replay check, which is usually done
2333 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2335 * based on RFC 2401.
2338 ipsec_chkreplay(seq
, sav
)
2340 struct secasvar
*sav
;
2342 const struct secreplay
*replay
;
2345 u_int32_t wsizeb
; /* constant: bits of window size */
2346 int frlast
; /* constant: last frame */
2348 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2352 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2354 replay
= sav
->replay
;
2356 if (replay
->wsize
== 0)
2357 return 1; /* no need to check replay. */
2360 frlast
= replay
->wsize
- 1;
2361 wsizeb
= replay
->wsize
<< 3;
2363 /* sequence number of 0 is invalid */
2367 /* first time is always okay */
2368 if (replay
->count
== 0)
2371 if (seq
> replay
->lastseq
) {
2372 /* larger sequences are okay */
2375 /* seq is equal or less than lastseq. */
2376 diff
= replay
->lastseq
- seq
;
2378 /* over range to check, i.e. too old or wrapped */
2382 fr
= frlast
- diff
/ 8;
2384 /* this packet already seen ? */
2385 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2388 /* out of order but good */
2394 * check replay counter whether to update or not.
2399 ipsec_updatereplay(seq
, sav
)
2401 struct secasvar
*sav
;
2403 struct secreplay
*replay
;
2406 u_int32_t wsizeb
; /* constant: bits of window size */
2407 int frlast
; /* constant: last frame */
2409 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2413 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2415 replay
= sav
->replay
;
2417 if (replay
->wsize
== 0)
2418 goto ok
; /* no need to check replay. */
2421 frlast
= replay
->wsize
- 1;
2422 wsizeb
= replay
->wsize
<< 3;
2424 /* sequence number of 0 is invalid */
2429 if (replay
->count
== 0) {
2430 replay
->lastseq
= seq
;
2431 bzero(replay
->bitmap
, replay
->wsize
);
2432 (replay
->bitmap
)[frlast
] = 1;
2436 if (seq
> replay
->lastseq
) {
2437 /* seq is larger than lastseq. */
2438 diff
= seq
- replay
->lastseq
;
2440 /* new larger sequence number */
2441 if (diff
< wsizeb
) {
2443 /* set bit for this packet */
2444 vshiftl(replay
->bitmap
, diff
, replay
->wsize
);
2445 (replay
->bitmap
)[frlast
] |= 1;
2447 /* this packet has a "way larger" */
2448 bzero(replay
->bitmap
, replay
->wsize
);
2449 (replay
->bitmap
)[frlast
] = 1;
2451 replay
->lastseq
= seq
;
2453 /* larger is good */
2455 /* seq is equal or less than lastseq. */
2456 diff
= replay
->lastseq
- seq
;
2458 /* over range to check, i.e. too old or wrapped */
2462 fr
= frlast
- diff
/ 8;
2464 /* this packet already seen ? */
2465 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2469 (replay
->bitmap
)[fr
] |= (1 << (diff
% 8));
2471 /* out of order but good */
2475 if (replay
->count
== ~0) {
2477 /* set overflow flag */
2480 /* don't increment, no more packets accepted */
2481 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0)
2484 ipseclog((LOG_WARNING
, "replay counter made %d cycle. %s\n",
2485 replay
->overflow
, ipsec_logsastr(sav
)));
2494 * shift variable length buffer to left.
2495 * IN: bitmap: pointer to the buffer
2496 * nbit: the number of to shift.
2497 * wsize: buffer size (bytes).
2500 vshiftl(bitmap
, nbit
, wsize
)
2501 unsigned char *bitmap
;
2507 for (j
= 0; j
< nbit
; j
+= 8) {
2508 s
= (nbit
- j
< 8) ? (nbit
- j
): 8;
2510 for (i
= 1; i
< wsize
; i
++) {
2511 over
= (bitmap
[i
] >> (8 - s
));
2513 bitmap
[i
-1] |= over
;
2521 ipsec4_logpacketstr(ip
, spi
)
2525 static char buf
[256];
2529 s
= (u_int8_t
*)(&ip
->ip_src
);
2530 d
= (u_int8_t
*)(&ip
->ip_dst
);
2533 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2536 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%u.%u.%u.%u",
2537 s
[0], s
[1], s
[2], s
[3]);
2540 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%u.%u.%u.%u",
2541 d
[0], d
[1], d
[2], d
[3]);
2544 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2551 ipsec6_logpacketstr(ip6
, spi
)
2552 struct ip6_hdr
*ip6
;
2555 static char buf
[256];
2559 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2562 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%s",
2563 ip6_sprintf(&ip6
->ip6_src
));
2566 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%s",
2567 ip6_sprintf(&ip6
->ip6_dst
));
2570 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2578 struct secasvar
*sav
;
2580 static char buf
[256];
2582 struct secasindex
*saidx
= &sav
->sah
->saidx
;
2584 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2586 /* validity check */
2587 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2588 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
)
2589 panic("ipsec_logsastr: family mismatched.\n");
2592 snprintf(buf
, sizeof(buf
), "SA(SPI=%u ", (u_int32_t
)ntohl(sav
->spi
));
2595 if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET
) {
2597 s
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->src
)->sin_addr
;
2598 d
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->dst
)->sin_addr
;
2599 snprintf(p
, sizeof(buf
) - (p
- buf
),
2600 "src=%d.%d.%d.%d dst=%d.%d.%d.%d",
2601 s
[0], s
[1], s
[2], s
[3], d
[0], d
[1], d
[2], d
[3]);
2604 else if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET6
) {
2605 snprintf(p
, sizeof(buf
) - (p
- buf
),
2607 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->src
)->sin6_addr
));
2610 snprintf(p
, sizeof(buf
) - (p
- buf
),
2612 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->dst
)->sin6_addr
));
2617 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2633 p
= mtod(m
, u_char
*);
2634 for (i
= 0; i
< m
->m_len
; i
++) {
2635 printf("%02x ", p
[i
]);
2637 if (totlen
% 16 == 0)
2642 if (totlen
% 16 != 0)
2649 * IPsec output logic for IPv4.
2652 ipsec4_output(state
, sp
, flags
)
2653 struct ipsec_output_state
*state
;
2654 struct secpolicy
*sp
;
2657 struct ip
*ip
= NULL
;
2658 struct ipsecrequest
*isr
= NULL
;
2659 struct secasindex saidx
;
2661 struct sockaddr_in
*dst4
;
2662 struct sockaddr_in
*sin
;
2665 panic("state == NULL in ipsec4_output");
2667 panic("state->m == NULL in ipsec4_output");
2669 panic("state->ro == NULL in ipsec4_output");
2671 panic("state->dst == NULL in ipsec4_output");
2673 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2675 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_START
, 0,0,0,0,0);
2677 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2678 printf("ipsec4_output: applyed SP\n");
2679 kdebug_secpolicy(sp
));
2681 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2683 #if 0 /* give up to check restriction of transport mode */
2684 /* XXX but should be checked somewhere */
2686 * some of the IPsec operation must be performed only in
2689 if (isr
->saidx
.mode
== IPSEC_MODE_TRANSPORT
2690 && (flags
& IP_FORWARDING
))
2694 /* make SA index for search proper SA */
2695 ip
= mtod(state
->m
, struct ip
*);
2696 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2697 saidx
.mode
= isr
->saidx
.mode
;
2698 saidx
.reqid
= isr
->saidx
.reqid
;
2699 sin
= (struct sockaddr_in
*)&saidx
.src
;
2700 if (sin
->sin_len
== 0) {
2701 sin
->sin_len
= sizeof(*sin
);
2702 sin
->sin_family
= AF_INET
;
2703 sin
->sin_port
= IPSEC_PORT_ANY
;
2704 bcopy(&ip
->ip_src
, &sin
->sin_addr
,
2705 sizeof(sin
->sin_addr
));
2707 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2708 if (sin
->sin_len
== 0) {
2709 sin
->sin_len
= sizeof(*sin
);
2710 sin
->sin_family
= AF_INET
;
2711 sin
->sin_port
= IPSEC_PORT_ANY
;
2712 bcopy(&ip
->ip_dst
, &sin
->sin_addr
,
2713 sizeof(sin
->sin_addr
));
2716 if ((error
= key_checkrequest(isr
, &saidx
)) != 0) {
2718 * IPsec processing is required, but no SA found.
2719 * I assume that key_acquire() had been called
2720 * to get/establish the SA. Here I discard
2721 * this packet because it is responsibility for
2722 * upper layer to retransmit the packet.
2724 ipsecstat
.out_nosa
++;
2728 /* validity check */
2729 if (isr
->sav
== NULL
) {
2730 switch (ipsec_get_reqlevel(isr
)) {
2731 case IPSEC_LEVEL_USE
:
2733 case IPSEC_LEVEL_REQUIRE
:
2734 /* must be not reached here. */
2735 panic("ipsec4_output: no SA found, but required.");
2740 * If there is no valid SA, we give up to process any
2741 * more. In such a case, the SA's status is changed
2742 * from DYING to DEAD after allocating. If a packet
2743 * send to the receiver by dead SA, the receiver can
2744 * not decode a packet because SA has been dead.
2746 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2747 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2748 ipsecstat
.out_nosa
++;
2754 * There may be the case that SA status will be changed when
2755 * we are refering to one. So calling splsoftnet().
2758 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2760 * build IPsec tunnel.
2762 /* XXX should be processed with other familiy */
2763 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2764 ipseclog((LOG_ERR
, "ipsec4_output: "
2765 "family mismatched between inner and outer spi=%u\n",
2766 (u_int32_t
)ntohl(isr
->sav
->spi
)));
2767 error
= EAFNOSUPPORT
;
2771 state
->m
= ipsec4_splithdr(state
->m
);
2776 error
= ipsec4_encapsulate(state
->m
, isr
->sav
);
2781 ip
= mtod(state
->m
, struct ip
*);
2783 state
->ro
= &isr
->sav
->sah
->sa_route
;
2784 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
2785 dst4
= (struct sockaddr_in
*)state
->dst
;
2786 if (state
->ro
->ro_rt
2787 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
2788 || dst4
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
2789 rtfree(state
->ro
->ro_rt
);
2790 state
->ro
->ro_rt
= NULL
;
2792 if (state
->ro
->ro_rt
== 0) {
2793 dst4
->sin_family
= AF_INET
;
2794 dst4
->sin_len
= sizeof(*dst4
);
2795 dst4
->sin_addr
= ip
->ip_dst
;
2798 if (state
->ro
->ro_rt
== 0) {
2799 ipstat
.ips_noroute
++;
2800 error
= EHOSTUNREACH
;
2804 /* adjust state->dst if tunnel endpoint is offlink */
2805 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
2806 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
2807 dst4
= (struct sockaddr_in
*)state
->dst
;
2811 state
->m
= ipsec4_splithdr(state
->m
);
2816 switch (isr
->saidx
.proto
) {
2819 if ((error
= esp4_output(state
->m
, isr
)) != 0) {
2831 if ((error
= ah4_output(state
->m
, isr
)) != 0) {
2836 case IPPROTO_IPCOMP
:
2837 if ((error
= ipcomp4_output(state
->m
, isr
)) != 0) {
2844 "ipsec4_output: unknown ipsec protocol %d\n",
2852 if (state
->m
== 0) {
2856 ip
= mtod(state
->m
, struct ip
*);
2859 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_END
, 0,0,0,0,0);
2865 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_END
, error
,0,0,0,0);
2872 * IPsec output logic for IPv6, transport mode.
2875 ipsec6_output_trans(state
, nexthdrp
, mprev
, sp
, flags
, tun
)
2876 struct ipsec_output_state
*state
;
2879 struct secpolicy
*sp
;
2883 struct ip6_hdr
*ip6
;
2884 struct ipsecrequest
*isr
= NULL
;
2885 struct secasindex saidx
;
2888 struct sockaddr_in6
*sin6
;
2891 panic("state == NULL in ipsec6_output_trans");
2893 panic("state->m == NULL in ipsec6_output_trans");
2895 panic("nexthdrp == NULL in ipsec6_output_trans");
2897 panic("mprev == NULL in ipsec6_output_trans");
2899 panic("sp == NULL in ipsec6_output_trans");
2901 panic("tun == NULL in ipsec6_output_trans");
2903 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2904 printf("ipsec6_output_trans: applyed SP\n");
2905 kdebug_secpolicy(sp
));
2907 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
2909 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
2910 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2911 /* the rest will be handled by ipsec6_output_tunnel() */
2915 /* make SA index for search proper SA */
2916 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2917 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2918 saidx
.mode
= isr
->saidx
.mode
;
2919 saidx
.reqid
= isr
->saidx
.reqid
;
2920 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
2921 if (sin6
->sin6_len
== 0) {
2922 sin6
->sin6_len
= sizeof(*sin6
);
2923 sin6
->sin6_family
= AF_INET6
;
2924 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2925 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
2926 sizeof(ip6
->ip6_src
));
2927 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
2928 /* fix scope id for comparing SPD */
2929 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2930 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
2933 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
2934 if (sin6
->sin6_len
== 0) {
2935 sin6
->sin6_len
= sizeof(*sin6
);
2936 sin6
->sin6_family
= AF_INET6
;
2937 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2938 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
2939 sizeof(ip6
->ip6_dst
));
2940 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
2941 /* fix scope id for comparing SPD */
2942 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2943 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
2947 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
2949 * IPsec processing is required, but no SA found.
2950 * I assume that key_acquire() had been called
2951 * to get/establish the SA. Here I discard
2952 * this packet because it is responsibility for
2953 * upper layer to retransmit the packet.
2955 ipsec6stat
.out_nosa
++;
2959 * Notify the fact that the packet is discarded
2960 * to ourselves. I believe this is better than
2961 * just silently discarding. (jinmei@kame.net)
2962 * XXX: should we restrict the error to TCP packets?
2963 * XXX: should we directly notify sockets via
2966 lck_mtx_unlock(sadb_mutex
);
2967 icmp6_error(state
->m
, ICMP6_DST_UNREACH
,
2968 ICMP6_DST_UNREACH_ADMIN
, 0);
2969 lck_mtx_lock(sadb_mutex
);
2970 state
->m
= NULL
; /* icmp6_error freed the mbuf */
2974 /* validity check */
2975 if (isr
->sav
== NULL
) {
2976 switch (ipsec_get_reqlevel(isr
)) {
2977 case IPSEC_LEVEL_USE
:
2979 case IPSEC_LEVEL_REQUIRE
:
2980 /* must be not reached here. */
2981 panic("ipsec6_output_trans: no SA found, but required.");
2986 * If there is no valid SA, we give up to process.
2987 * see same place at ipsec4_output().
2989 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2990 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2991 ipsec6stat
.out_nosa
++;
2996 switch (isr
->saidx
.proto
) {
2999 error
= esp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
3006 error
= ah6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
3008 case IPPROTO_IPCOMP
:
3009 error
= ipcomp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
3012 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
3013 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
3015 ipsec6stat
.out_inval
++;
3023 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
3024 if (plen
> IPV6_MAXPACKET
) {
3025 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
3026 "IPsec with IPv6 jumbogram is not supported\n"));
3027 ipsec6stat
.out_inval
++;
3028 error
= EINVAL
; /*XXX*/
3031 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3032 ip6
->ip6_plen
= htons(plen
);
3035 /* if we have more to go, we need a tunnel mode processing */
3048 * IPsec output logic for IPv6, tunnel mode.
3051 ipsec6_output_tunnel(state
, sp
, flags
)
3052 struct ipsec_output_state
*state
;
3053 struct secpolicy
*sp
;
3056 struct ip6_hdr
*ip6
;
3057 struct ipsecrequest
*isr
= NULL
;
3058 struct secasindex saidx
;
3061 struct sockaddr_in6
* dst6
;
3064 panic("state == NULL in ipsec6_output_tunnel");
3066 panic("state->m == NULL in ipsec6_output_tunnel");
3068 panic("sp == NULL in ipsec6_output_tunnel");
3070 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3072 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
3073 printf("ipsec6_output_tunnel: applyed SP\n");
3074 kdebug_secpolicy(sp
));
3077 * transport mode ipsec (before the 1st tunnel mode) is already
3078 * processed by ipsec6_output_trans().
3080 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
3081 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
3085 for (/* already initialized */; isr
; isr
= isr
->next
) {
3086 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3087 /* When tunnel mode, SA peers must be specified. */
3088 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
3090 /* make SA index to look for a proper SA */
3091 struct sockaddr_in6
*sin6
;
3093 bzero(&saidx
, sizeof(saidx
));
3094 saidx
.proto
= isr
->saidx
.proto
;
3095 saidx
.mode
= isr
->saidx
.mode
;
3096 saidx
.reqid
= isr
->saidx
.reqid
;
3098 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3099 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
3100 if (sin6
->sin6_len
== 0) {
3101 sin6
->sin6_len
= sizeof(*sin6
);
3102 sin6
->sin6_family
= AF_INET6
;
3103 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3104 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
3105 sizeof(ip6
->ip6_src
));
3106 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
3107 /* fix scope id for comparing SPD */
3108 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3109 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
3112 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
3113 if (sin6
->sin6_len
== 0) {
3114 sin6
->sin6_len
= sizeof(*sin6
);
3115 sin6
->sin6_family
= AF_INET6
;
3116 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3117 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
3118 sizeof(ip6
->ip6_dst
));
3119 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
3120 /* fix scope id for comparing SPD */
3121 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3122 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
3127 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
3129 * IPsec processing is required, but no SA found.
3130 * I assume that key_acquire() had been called
3131 * to get/establish the SA. Here I discard
3132 * this packet because it is responsibility for
3133 * upper layer to retransmit the packet.
3135 ipsec6stat
.out_nosa
++;
3140 /* validity check */
3141 if (isr
->sav
== NULL
) {
3142 switch (ipsec_get_reqlevel(isr
)) {
3143 case IPSEC_LEVEL_USE
:
3145 case IPSEC_LEVEL_REQUIRE
:
3146 /* must be not reached here. */
3147 panic("ipsec6_output_tunnel: no SA found, but required.");
3152 * If there is no valid SA, we give up to process.
3153 * see same place at ipsec4_output().
3155 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
3156 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
3157 ipsec6stat
.out_nosa
++;
3163 * There may be the case that SA status will be changed when
3164 * we are refering to one. So calling splsoftnet().
3167 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3169 * build IPsec tunnel.
3171 /* XXX should be processed with other familiy */
3172 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
3173 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3174 "family mismatched between inner and outer, spi=%u\n",
3175 (u_int32_t
)ntohl(isr
->sav
->spi
)));
3176 ipsec6stat
.out_inval
++;
3177 error
= EAFNOSUPPORT
;
3181 state
->m
= ipsec6_splithdr(state
->m
);
3183 ipsec6stat
.out_nomem
++;
3187 error
= ipsec6_encapsulate(state
->m
, isr
->sav
);
3192 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3194 state
->ro
= &isr
->sav
->sah
->sa_route
;
3195 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
3196 dst6
= (struct sockaddr_in6
*)state
->dst
;
3197 if (state
->ro
->ro_rt
3198 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
3199 || !IN6_ARE_ADDR_EQUAL(&dst6
->sin6_addr
, &ip6
->ip6_dst
))) {
3200 rtfree(state
->ro
->ro_rt
);
3201 state
->ro
->ro_rt
= NULL
;
3203 if (state
->ro
->ro_rt
== 0) {
3204 bzero(dst6
, sizeof(*dst6
));
3205 dst6
->sin6_family
= AF_INET6
;
3206 dst6
->sin6_len
= sizeof(*dst6
);
3207 dst6
->sin6_addr
= ip6
->ip6_dst
;
3210 if (state
->ro
->ro_rt
== 0) {
3211 ip6stat
.ip6s_noroute
++;
3212 ipsec6stat
.out_noroute
++;
3213 error
= EHOSTUNREACH
;
3217 /* adjust state->dst if tunnel endpoint is offlink */
3218 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
3219 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
3220 dst6
= (struct sockaddr_in6
*)state
->dst
;
3224 state
->m
= ipsec6_splithdr(state
->m
);
3226 ipsec6stat
.out_nomem
++;
3230 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3231 switch (isr
->saidx
.proto
) {
3234 error
= esp6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
3241 error
= ah6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
3243 case IPPROTO_IPCOMP
:
3244 /* XXX code should be here */
3247 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3248 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
3250 ipsec6stat
.out_inval
++;
3258 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
3259 if (plen
> IPV6_MAXPACKET
) {
3260 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3261 "IPsec with IPv6 jumbogram is not supported\n"));
3262 ipsec6stat
.out_inval
++;
3263 error
= EINVAL
; /*XXX*/
3266 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3267 ip6
->ip6_plen
= htons(plen
);
3281 * Chop IP header and option off from the payload.
3283 static struct mbuf
*
3291 if (m
->m_len
< sizeof(struct ip
))
3292 panic("ipsec4_splithdr: first mbuf too short");
3293 ip
= mtod(m
, struct ip
*);
3295 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
3297 hlen
= ip
->ip_hl
<< 2;
3299 if (m
->m_len
> hlen
) {
3300 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3305 M_COPY_PKTHDR(mh
, m
);
3307 m
->m_flags
&= ~M_PKTHDR
;
3313 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), hlen
);
3314 } else if (m
->m_len
< hlen
) {
3315 m
= m_pullup(m
, hlen
);
3324 static struct mbuf
*
3329 struct ip6_hdr
*ip6
;
3332 if (m
->m_len
< sizeof(struct ip6_hdr
))
3333 panic("ipsec6_splithdr: first mbuf too short");
3334 ip6
= mtod(m
, struct ip6_hdr
*);
3335 hlen
= sizeof(struct ip6_hdr
);
3336 if (m
->m_len
> hlen
) {
3337 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3342 M_COPY_PKTHDR(mh
, m
);
3344 m
->m_flags
&= ~M_PKTHDR
;
3350 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), hlen
);
3351 } else if (m
->m_len
< hlen
) {
3352 m
= m_pullup(m
, hlen
);
3360 /* validate inbound IPsec tunnel packet. */
3362 ipsec4_tunnel_validate(m
, off
, nxt0
, sav
)
3363 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3366 struct secasvar
*sav
;
3368 u_int8_t nxt
= nxt0
& 0xff;
3369 struct sockaddr_in
*sin
;
3370 struct sockaddr_in osrc
, odst
, isrc
, idst
;
3372 struct secpolicy
*sp
;
3375 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3378 if (m
->m_len
< sizeof(struct ip
))
3379 panic("too short mbuf on ipsec4_tunnel_validate");
3381 if (nxt
!= IPPROTO_IPV4
)
3383 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip
))
3385 /* do not decapsulate if the SA is for transport mode only */
3386 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3389 oip
= mtod(m
, struct ip
*);
3391 hlen
= _IP_VHL_HL(oip
->ip_vhl
) << 2;
3393 hlen
= oip
->ip_hl
<< 2;
3395 if (hlen
!= sizeof(struct ip
))
3398 /* AF_INET6 should be supported, but at this moment we don't. */
3399 sin
= (struct sockaddr_in
*)&sav
->sah
->saidx
.dst
;
3400 if (sin
->sin_family
!= AF_INET
)
3402 if (bcmp(&oip
->ip_dst
, &sin
->sin_addr
, sizeof(oip
->ip_dst
)) != 0)
3406 bzero(&osrc
, sizeof(osrc
));
3407 bzero(&odst
, sizeof(odst
));
3408 bzero(&isrc
, sizeof(isrc
));
3409 bzero(&idst
, sizeof(idst
));
3410 osrc
.sin_family
= odst
.sin_family
= isrc
.sin_family
= idst
.sin_family
=
3412 osrc
.sin_len
= odst
.sin_len
= isrc
.sin_len
= idst
.sin_len
=
3413 sizeof(struct sockaddr_in
);
3414 osrc
.sin_addr
= oip
->ip_src
;
3415 odst
.sin_addr
= oip
->ip_dst
;
3416 m_copydata(m
, off
+ offsetof(struct ip
, ip_src
), sizeof(isrc
.sin_addr
),
3417 (caddr_t
)&isrc
.sin_addr
);
3418 m_copydata(m
, off
+ offsetof(struct ip
, ip_dst
), sizeof(idst
.sin_addr
),
3419 (caddr_t
)&idst
.sin_addr
);
3422 * RFC2401 5.2.1 (b): (assume that we are using tunnel mode)
3423 * - if the inner destination is multicast address, there can be
3424 * multiple permissible inner source address. implementation
3425 * may want to skip verification of inner source address against
3427 * - if the inner protocol is ICMP, the packet may be an error report
3428 * from routers on the other side of the VPN cloud (R in the
3429 * following diagram). in this case, we cannot verify inner source
3430 * address against SPD selector.
3431 * me -- gw === gw -- R -- you
3433 * we consider the first bullet to be users responsibility on SPD entry
3434 * configuration (if you need to encrypt multicast traffic, set
3435 * the source range of SPD selector to 0.0.0.0/0, or have explicit
3436 * address ranges for possible senders).
3437 * the second bullet is not taken care of (yet).
3439 * therefore, we do not do anything special about inner source.
3442 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3443 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3453 /* validate inbound IPsec tunnel packet. */
3455 ipsec6_tunnel_validate(m
, off
, nxt0
, sav
)
3456 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3459 struct secasvar
*sav
;
3461 u_int8_t nxt
= nxt0
& 0xff;
3462 struct sockaddr_in6
*sin6
;
3463 struct sockaddr_in6 osrc
, odst
, isrc
, idst
;
3464 struct secpolicy
*sp
;
3465 struct ip6_hdr
*oip6
;
3467 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3470 if (m
->m_len
< sizeof(struct ip6_hdr
))
3471 panic("too short mbuf on ipsec6_tunnel_validate");
3473 if (nxt
!= IPPROTO_IPV6
)
3475 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip6_hdr
))
3477 /* do not decapsulate if the SA is for transport mode only */
3478 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3481 oip6
= mtod(m
, struct ip6_hdr
*);
3482 /* AF_INET should be supported, but at this moment we don't. */
3483 sin6
= (struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
;
3484 if (sin6
->sin6_family
!= AF_INET6
)
3486 if (!IN6_ARE_ADDR_EQUAL(&oip6
->ip6_dst
, &sin6
->sin6_addr
))
3490 bzero(&osrc
, sizeof(osrc
));
3491 bzero(&odst
, sizeof(odst
));
3492 bzero(&isrc
, sizeof(isrc
));
3493 bzero(&idst
, sizeof(idst
));
3494 osrc
.sin6_family
= odst
.sin6_family
= isrc
.sin6_family
=
3495 idst
.sin6_family
= AF_INET6
;
3496 osrc
.sin6_len
= odst
.sin6_len
= isrc
.sin6_len
= idst
.sin6_len
=
3497 sizeof(struct sockaddr_in6
);
3498 osrc
.sin6_addr
= oip6
->ip6_src
;
3499 odst
.sin6_addr
= oip6
->ip6_dst
;
3500 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_src
),
3501 sizeof(isrc
.sin6_addr
), (caddr_t
)&isrc
.sin6_addr
);
3502 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_dst
),
3503 sizeof(idst
.sin6_addr
), (caddr_t
)&idst
.sin6_addr
);
3506 * regarding to inner source address validation, see a long comment
3507 * in ipsec4_tunnel_validate.
3510 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3511 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3513 * when there is no suitable inbound policy for the packet of the ipsec
3514 * tunnel mode, the kernel never decapsulate the tunneled packet
3515 * as the ipsec tunnel mode even when the system wide policy is "none".
3516 * then the kernel leaves the generic tunnel module to process this
3517 * packet. if there is no rule of the generic tunnel, the packet
3518 * is rejected and the statistics will be counted up.
3529 * Make a mbuf chain for encryption.
3530 * If the original mbuf chain contains a mbuf with a cluster,
3531 * allocate a new cluster and copy the data to the new cluster.
3532 * XXX: this hack is inefficient, but is necessary to handle cases
3533 * of TCP retransmission...
3539 struct mbuf
*n
, **mpp
, *mnew
;
3541 for (n
= m
, mpp
= &m
; n
; n
= n
->m_next
) {
3542 if (n
->m_flags
& M_EXT
) {
3544 * Make a copy only if there are more than one references
3546 * XXX: is this approach effective?
3549 n
->m_ext
.ext_free
||
3550 m_mclhasreference(n
)
3556 if (n
->m_flags
& M_PKTHDR
) {
3557 MGETHDR(mnew
, M_DONTWAIT
, MT_HEADER
);
3560 mnew
->m_pkthdr
= n
->m_pkthdr
;
3562 if (n
->m_pkthdr
.aux
) {
3563 mnew
->m_pkthdr
.aux
=
3564 m_copym(n
->m_pkthdr
.aux
,
3565 0, M_COPYALL
, M_DONTWAIT
);
3568 M_COPY_PKTHDR(mnew
, n
);
3569 mnew
->m_flags
= n
->m_flags
& M_COPYFLAGS
;
3572 MGET(mnew
, M_DONTWAIT
, MT_DATA
);
3580 * Copy data. If we don't have enough space to
3581 * store the whole data, allocate a cluster
3582 * or additional mbufs.
3583 * XXX: we don't use m_copyback(), since the
3584 * function does not use clusters and thus is
3593 if (remain
<= (mm
->m_flags
& M_PKTHDR
? MHLEN
: MLEN
))
3595 else { /* allocate a cluster */
3596 MCLGET(mm
, M_DONTWAIT
);
3597 if (!(mm
->m_flags
& M_EXT
)) {
3601 len
= remain
< MCLBYTES
?
3605 bcopy(n
->m_data
+ copied
, mm
->m_data
,
3612 if (remain
<= 0) /* completed? */
3615 /* need another mbuf */
3616 MGETHDR(mn
, M_DONTWAIT
, MT_HEADER
);
3619 mn
->m_pkthdr
.rcvif
= NULL
;
3625 mm
->m_next
= m_free(n
);
3643 static struct mbuf
*
3649 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3651 n
= m_aux_add(m
, AF_INET
, IPPROTO_ESP
);
3653 return n
; /* ENOBUFS */
3654 n
->m_len
= sizeof(struct socket
*);
3655 bzero(mtod(n
, void *), n
->m_len
);
3659 static struct mbuf
*
3665 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3667 if (n
&& n
->m_len
< sizeof(struct socket
*))
3668 panic("invalid ipsec m_aux");
3679 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3684 /* if the aux buffer is unnecessary, nuke it. */
3693 if (n
->m_len
== sizeof(struct socket
*) && !*mtod(n
, struct socket
**))
3698 ipsec_setsocket(m
, so
)
3704 /* if so == NULL, don't insist on getting the aux mbuf */
3706 n
= ipsec_addaux(m
);
3710 n
= ipsec_findaux(m
);
3711 if (n
&& n
->m_len
>= sizeof(struct socket
*))
3712 *mtod(n
, struct socket
**) = so
;
3723 n
= ipsec_findaux(m
);
3724 if (n
&& n
->m_len
>= sizeof(struct socket
*))
3725 return *mtod(n
, struct socket
**);
3731 ipsec_addhist(m
, proto
, spi
)
3737 struct ipsec_history
*p
;
3739 n
= ipsec_addaux(m
);
3742 if (M_TRAILINGSPACE(n
) < sizeof(*p
))
3743 return ENOSPC
; /* XXX */
3744 p
= (struct ipsec_history
*)(mtod(n
, caddr_t
) + n
->m_len
);
3745 n
->m_len
+= sizeof(*p
);
3746 bzero(p
, sizeof(*p
));
3747 p
->ih_proto
= proto
;
3752 struct ipsec_history
*
3753 ipsec_gethist(m
, lenp
)
3760 n
= ipsec_findaux(m
);
3764 if (sizeof(struct socket
*) > l
)
3766 if ((l
- sizeof(struct socket
*)) % sizeof(struct ipsec_history
))
3768 /* XXX does it make more sense to divide by sizeof(ipsec_history)? */
3770 *lenp
= l
- sizeof(struct socket
*);
3771 return (struct ipsec_history
*)
3772 (mtod(n
, caddr_t
) + sizeof(struct socket
*));
3781 n
= ipsec_findaux(m
);
3782 if ((n
) && n
->m_len
> sizeof(struct socket
*))
3783 n
->m_len
= sizeof(struct socket
*);
3787 __private_extern__
void
3788 ipsec_send_natt_keepalive(
3789 struct secasvar
*sav
)
3796 if ((esp_udp_encap_port
& 0xFFFF) == 0 || sav
->remote_ike_port
== 0) return;
3798 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
3800 m
= m_gethdr(M_NOWAIT
, MT_DATA
);
3801 if (m
== NULL
) return;
3804 * Create a UDP packet complete with IP header.
3805 * We must do this because UDP output requires
3806 * an inpcb which we don't have. UDP packet
3807 * contains one byte payload. The byte is set
3810 ip
= (struct ip
*)m_mtod(m
);
3811 uh
= (struct udphdr
*)((char*)m_mtod(m
) + sizeof(struct ip
));
3812 m
->m_len
= sizeof(struct udpiphdr
) + 1;
3813 bzero(m_mtod(m
), m
->m_len
);
3814 m
->m_pkthdr
.len
= m
->m_len
;
3816 ip
->ip_len
= m
->m_len
;
3817 ip
->ip_ttl
= ip_defttl
;
3818 ip
->ip_p
= IPPROTO_UDP
;
3819 ip
->ip_src
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
;
3820 ip
->ip_dst
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
;
3821 uh
->uh_sport
= htons((u_short
)esp_udp_encap_port
);
3822 uh
->uh_dport
= htons(sav
->remote_ike_port
);
3823 uh
->uh_ulen
= htons(1 + sizeof(struct udphdr
));
3825 *(u_int8_t
*)((char*)m_mtod(m
) + sizeof(struct ip
) + sizeof(struct udphdr
)) = 0xFF;
3827 lck_mtx_unlock(sadb_mutex
);
3828 error
= ip_output(m
, NULL
, &sav
->sah
->sa_route
, IP_NOIPSEC
, NULL
);
3829 lck_mtx_lock(sadb_mutex
);
3831 sav
->natt_last_activity
= natt_now
;