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>
52 #include <libkern/OSAtomic.h>
55 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/in_var.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
64 #include <netinet/ip_ecn.h>
66 #include <netinet6/ip6_ecn.h>
68 #include <netinet/tcp.h>
69 #include <netinet/udp.h>
71 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
75 #include <netinet/in_pcb.h>
77 #include <netinet/icmp6.h>
80 #include <netinet6/ipsec.h>
82 #include <netinet6/ipsec6.h>
84 #include <netinet6/ah.h>
86 #include <netinet6/ah6.h>
89 #include <netinet6/esp.h>
91 #include <netinet6/esp6.h>
94 #include <netinet6/ipcomp.h>
96 #include <netinet6/ipcomp6.h>
98 #include <netkey/key.h>
99 #include <netkey/keydb.h>
100 #include <netkey/key_debug.h>
102 #include <net/net_osdep.h>
110 #include <sys/kdebug.h>
111 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
112 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
113 #define DBG_FNC_GETPOL_SOCK NETDBG_CODE(DBG_NETIPSEC, (1 << 8))
114 #define DBG_FNC_GETPOL_ADDR NETDBG_CODE(DBG_NETIPSEC, (2 << 8))
115 #define DBG_FNC_IPSEC_OUT NETDBG_CODE(DBG_NETIPSEC, (3 << 8))
117 extern lck_mtx_t
*sadb_mutex
;
118 extern lck_mtx_t
*ip6_mutex
;
120 struct ipsecstat ipsecstat
;
121 int ip4_ah_cleartos
= 1;
122 int ip4_ah_offsetmask
= 0; /* maybe IP_DF? */
123 int ip4_ipsec_dfbit
= 0; /* DF bit on encap. 0: clear 1: set 2: copy */
124 int ip4_esp_trans_deflev
= IPSEC_LEVEL_USE
;
125 int ip4_esp_net_deflev
= IPSEC_LEVEL_USE
;
126 int ip4_ah_trans_deflev
= IPSEC_LEVEL_USE
;
127 int ip4_ah_net_deflev
= IPSEC_LEVEL_USE
;
128 struct secpolicy ip4_def_policy
;
129 int ip4_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
130 int ip4_esp_randpad
= -1;
131 int esp_udp_encap_port
= 0;
132 static int sysctl_def_policy SYSCTL_HANDLER_ARGS
;
133 extern u_int32_t natt_now
;
137 SYSCTL_DECL(_net_inet_ipsec
);
139 SYSCTL_DECL(_net_inet6_ipsec6
);
142 SYSCTL_STRUCT(_net_inet_ipsec
, IPSECCTL_STATS
,
143 stats
, CTLFLAG_RD
, &ipsecstat
, ipsecstat
, "");
144 SYSCTL_PROC(_net_inet_ipsec
, IPSECCTL_DEF_POLICY
, def_policy
, CTLTYPE_INT
|CTLFLAG_RW
,
145 &ip4_def_policy
.policy
, 0, &sysctl_def_policy
, "I", "");
146 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
147 CTLFLAG_RW
, &ip4_esp_trans_deflev
, 0, "");
148 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
149 CTLFLAG_RW
, &ip4_esp_net_deflev
, 0, "");
150 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
151 CTLFLAG_RW
, &ip4_ah_trans_deflev
, 0, "");
152 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
153 CTLFLAG_RW
, &ip4_ah_net_deflev
, 0, "");
154 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_CLEARTOS
,
155 ah_cleartos
, CTLFLAG_RW
, &ip4_ah_cleartos
, 0, "");
156 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_OFFSETMASK
,
157 ah_offsetmask
, CTLFLAG_RW
, &ip4_ah_offsetmask
, 0, "");
158 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DFBIT
,
159 dfbit
, CTLFLAG_RW
, &ip4_ipsec_dfbit
, 0, "");
160 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ECN
,
161 ecn
, CTLFLAG_RW
, &ip4_ipsec_ecn
, 0, "");
162 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEBUG
,
163 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
164 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ESP_RANDPAD
,
165 esp_randpad
, CTLFLAG_RW
, &ip4_esp_randpad
, 0, "");
167 /* for performance, we bypass ipsec until a security policy is set */
168 int ipsec_bypass
= 1;
169 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, bypass
, CTLFLAG_RD
, &ipsec_bypass
,0, "");
172 * NAT Traversal requires a UDP port for encapsulation,
173 * esp_udp_encap_port controls which port is used. Racoon
174 * must set this port to the port racoon is using locally
177 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, esp_port
,
178 CTLFLAG_RW
, &esp_udp_encap_port
, 0, "");
181 struct ipsecstat ipsec6stat
;
182 int ip6_esp_trans_deflev
= IPSEC_LEVEL_USE
;
183 int ip6_esp_net_deflev
= IPSEC_LEVEL_USE
;
184 int ip6_ah_trans_deflev
= IPSEC_LEVEL_USE
;
185 int ip6_ah_net_deflev
= IPSEC_LEVEL_USE
;
186 struct secpolicy ip6_def_policy
;
187 int ip6_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
188 int ip6_esp_randpad
= -1;
190 /* net.inet6.ipsec6 */
191 SYSCTL_STRUCT(_net_inet6_ipsec6
, IPSECCTL_STATS
,
192 stats
, CTLFLAG_RD
, &ipsec6stat
, ipsecstat
, "");
193 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_POLICY
,
194 def_policy
, CTLFLAG_RW
, &ip6_def_policy
.policy
, 0, "");
195 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
196 CTLFLAG_RW
, &ip6_esp_trans_deflev
, 0, "");
197 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
198 CTLFLAG_RW
, &ip6_esp_net_deflev
, 0, "");
199 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
200 CTLFLAG_RW
, &ip6_ah_trans_deflev
, 0, "");
201 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
202 CTLFLAG_RW
, &ip6_ah_net_deflev
, 0, "");
203 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ECN
,
204 ecn
, CTLFLAG_RW
, &ip6_ipsec_ecn
, 0, "");
205 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEBUG
,
206 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
207 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ESP_RANDPAD
,
208 esp_randpad
, CTLFLAG_RW
, &ip6_esp_randpad
, 0, "");
211 static int ipsec_setspidx_mbuf(struct secpolicyindex
*, u_int
, u_int
,
213 static int ipsec4_setspidx_inpcb(struct mbuf
*, struct inpcb
*pcb
);
215 static int ipsec6_setspidx_in6pcb(struct mbuf
*, struct in6pcb
*pcb
);
217 static int ipsec_setspidx(struct mbuf
*, struct secpolicyindex
*, int);
218 static void ipsec4_get_ulp(struct mbuf
*m
, struct secpolicyindex
*, int);
219 static int ipsec4_setspidx_ipaddr(struct mbuf
*, struct secpolicyindex
*);
221 static void ipsec6_get_ulp(struct mbuf
*m
, struct secpolicyindex
*, int);
222 static int ipsec6_setspidx_ipaddr(struct mbuf
*, struct secpolicyindex
*);
224 static struct inpcbpolicy
*ipsec_newpcbpolicy(void);
225 static void ipsec_delpcbpolicy(struct inpcbpolicy
*);
226 static struct secpolicy
*ipsec_deepcopy_policy(struct secpolicy
*src
);
227 static int ipsec_set_policy(struct secpolicy
**pcb_sp
,
228 int optname
, caddr_t request
, size_t len
, int priv
);
229 static int ipsec_get_policy(struct secpolicy
*pcb_sp
, struct mbuf
**mp
);
230 static void vshiftl(unsigned char *, int, int);
231 static int ipsec_in_reject(struct secpolicy
*, struct mbuf
*);
233 static struct mbuf
*ipsec4_splithdr(struct mbuf
*);
236 static struct mbuf
*ipsec6_splithdr(struct mbuf
*);
239 static int ipsec4_encapsulate(struct mbuf
*, struct secasvar
*);
242 static int ipsec6_encapsulate(struct mbuf
*, struct secasvar
*);
243 static int ipsec64_encapsulate(struct mbuf
*, struct secasvar
*);
245 static struct ipsec_tag
*ipsec_addaux(struct mbuf
*);
246 static struct ipsec_tag
*ipsec_findaux(struct mbuf
*);
247 static void ipsec_optaux(struct mbuf
*, struct ipsec_tag
*);
248 void ipsec_send_natt_keepalive(struct secasvar
*sav
);
251 sysctl_def_policy SYSCTL_HANDLER_ARGS
253 int old_policy
= ip4_def_policy
.policy
;
254 int error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
256 if (ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
&&
257 ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
) {
258 ip4_def_policy
.policy
= old_policy
;
262 /* Turn off the bypass if the default security policy changes */
263 if (ipsec_bypass
!= 0 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
)
270 * For OUTBOUND packet having a socket. Searching SPD for packet,
271 * and return a pointer to SP.
272 * OUT: NULL: no apropreate SP found, the following value is set to error.
274 * EACCES : discard packet.
275 * ENOENT : ipsec_acquire() in progress, maybe.
276 * others : error occurred.
277 * others: a pointer to SP
279 * NOTE: IPv6 mapped adddress concern is implemented here.
282 ipsec4_getpolicybysock(m
, dir
, so
, error
)
288 struct inpcbpolicy
*pcbsp
= NULL
;
289 struct secpolicy
*currsp
= NULL
; /* policy on socket */
290 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
292 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
294 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
295 panic("ipsec4_getpolicybysock: NULL pointer was passed.\n");
297 if (so
->so_pcb
== NULL
) {
298 printf("ipsec4_getpolicybysock: so->so_pcb == NULL\n");
299 return ipsec4_getpolicybyaddr(m
, dir
, 0, error
);
302 switch (so
->so_proto
->pr_domain
->dom_family
) {
304 pcbsp
= sotoinpcb(so
)->inp_sp
;
308 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
314 /* Socket has not specified an IPSEC policy */
315 return ipsec4_getpolicybyaddr(m
, dir
, 0, error
);
318 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_START
, 0,0,0,0,0);
320 switch (so
->so_proto
->pr_domain
->dom_family
) {
322 /* set spidx in pcb */
323 *error
= ipsec4_setspidx_inpcb(m
, sotoinpcb(so
));
327 /* set spidx in pcb */
328 *error
= ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
332 panic("ipsec4_getpolicybysock: unsupported address family\n");
335 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 1,*error
,0,0,0);
341 panic("ipsec4_getpolicybysock: pcbsp is NULL.\n");
344 case IPSEC_DIR_INBOUND
:
345 currsp
= pcbsp
->sp_in
;
347 case IPSEC_DIR_OUTBOUND
:
348 currsp
= pcbsp
->sp_out
;
351 panic("ipsec4_getpolicybysock: illegal direction.\n");
356 panic("ipsec4_getpolicybysock: currsp is NULL.\n");
358 /* when privilieged socket */
360 switch (currsp
->policy
) {
361 case IPSEC_POLICY_BYPASS
:
362 lck_mtx_lock(sadb_mutex
);
364 lck_mtx_unlock(sadb_mutex
);
366 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 2,*error
,0,0,0);
369 case IPSEC_POLICY_ENTRUST
:
370 /* look for a policy in SPD */
371 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
374 if (kernsp
!= NULL
) {
375 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
376 printf("DP ipsec4_getpolicybysock called "
377 "to allocate SP:%p\n", kernsp
));
379 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 3,*error
,0,0,0);
384 lck_mtx_lock(sadb_mutex
);
385 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
386 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
388 "fixed system default policy: %d->%d\n",
389 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
390 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
392 ip4_def_policy
.refcnt
++;
393 lck_mtx_unlock(sadb_mutex
);
395 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 4,*error
,0,0,0);
396 return &ip4_def_policy
;
398 case IPSEC_POLICY_IPSEC
:
399 lck_mtx_lock(sadb_mutex
);
401 lck_mtx_unlock(sadb_mutex
);
403 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 5,*error
,0,0,0);
407 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
408 "Invalid policy for PCB %d\n", currsp
->policy
));
410 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 6,*error
,0,0,0);
416 /* when non-privilieged socket */
417 /* look for a policy in SPD */
418 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
421 if (kernsp
!= NULL
) {
422 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
423 printf("DP ipsec4_getpolicybysock called "
424 "to allocate SP:%p\n", kernsp
));
426 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 7,*error
,0,0,0);
431 switch (currsp
->policy
) {
432 case IPSEC_POLICY_BYPASS
:
433 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
434 "Illegal policy for non-priviliged defined %d\n",
437 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 8,*error
,0,0,0);
440 case IPSEC_POLICY_ENTRUST
:
441 lck_mtx_lock(sadb_mutex
);
442 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
443 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
445 "fixed system default policy: %d->%d\n",
446 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
447 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
449 ip4_def_policy
.refcnt
++;
450 lck_mtx_unlock(sadb_mutex
);
452 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 9,*error
,0,0,0);
453 return &ip4_def_policy
;
455 case IPSEC_POLICY_IPSEC
:
456 lck_mtx_lock(sadb_mutex
);
458 lck_mtx_unlock(sadb_mutex
);
460 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 10,*error
,0,0,0);
464 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
465 "Invalid policy for PCB %d\n", currsp
->policy
));
467 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 11,*error
,0,0,0);
474 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
475 * and return a pointer to SP.
476 * OUT: positive: a pointer to the entry for security policy leaf matched.
477 * NULL: no apropreate SP found, the following value is set to error.
479 * EACCES : discard packet.
480 * ENOENT : ipsec_acquire() in progress, maybe.
481 * others : error occurred.
484 ipsec4_getpolicybyaddr(m
, dir
, flag
, error
)
490 struct secpolicy
*sp
= NULL
;
492 if (ipsec_bypass
!= 0)
495 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
498 if (m
== NULL
|| error
== NULL
)
499 panic("ipsec4_getpolicybyaddr: NULL pointer was passed.\n");
502 struct secpolicyindex spidx
;
504 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_START
, 0,0,0,0,0);
505 bzero(&spidx
, sizeof(spidx
));
507 /* make a index to look for a policy */
508 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET
, m
,
509 (flag
& IP_FORWARDING
) ? 0 : 1);
512 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 1,*error
,0,0,0);
516 sp
= key_allocsp(&spidx
, dir
);
521 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
522 printf("DP ipsec4_getpolicybyaddr called "
523 "to allocate SP:%p\n", sp
));
525 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 2,*error
,0,0,0);
530 lck_mtx_lock(sadb_mutex
);
531 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
532 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
533 ipseclog((LOG_INFO
, "fixed system default policy:%d->%d\n",
534 ip4_def_policy
.policy
,
536 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
538 ip4_def_policy
.refcnt
++;
539 lck_mtx_unlock(sadb_mutex
);
541 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 3,*error
,0,0,0);
542 return &ip4_def_policy
;
547 * For OUTBOUND packet having a socket. Searching SPD for packet,
548 * and return a pointer to SP.
549 * OUT: NULL: no apropreate SP found, the following value is set to error.
551 * EACCES : discard packet.
552 * ENOENT : ipsec_acquire() in progress, maybe.
553 * others : error occurred.
554 * others: a pointer to SP
557 ipsec6_getpolicybysock(m
, dir
, so
, error
)
563 struct inpcbpolicy
*pcbsp
= NULL
;
564 struct secpolicy
*currsp
= NULL
; /* policy on socket */
565 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
567 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
570 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
571 panic("ipsec6_getpolicybysock: NULL pointer was passed.\n");
574 if (so
->so_proto
->pr_domain
->dom_family
!= AF_INET6
)
575 panic("ipsec6_getpolicybysock: socket domain != inet6\n");
578 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
581 return ipsec6_getpolicybyaddr(m
, dir
, 0, error
);
584 /* set spidx in pcb */
585 ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
589 panic("ipsec6_getpolicybysock: pcbsp is NULL.\n");
592 case IPSEC_DIR_INBOUND
:
593 currsp
= pcbsp
->sp_in
;
595 case IPSEC_DIR_OUTBOUND
:
596 currsp
= pcbsp
->sp_out
;
599 panic("ipsec6_getpolicybysock: illegal direction.\n");
604 panic("ipsec6_getpolicybysock: currsp is NULL.\n");
606 /* when privilieged socket */
608 switch (currsp
->policy
) {
609 case IPSEC_POLICY_BYPASS
:
610 lck_mtx_lock(sadb_mutex
);
612 lck_mtx_unlock(sadb_mutex
);
616 case IPSEC_POLICY_ENTRUST
:
617 /* look for a policy in SPD */
618 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
621 if (kernsp
!= NULL
) {
622 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
623 printf("DP ipsec6_getpolicybysock called "
624 "to allocate SP:%p\n", kernsp
));
630 lck_mtx_lock(sadb_mutex
);
631 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
632 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
634 "fixed system default policy: %d->%d\n",
635 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
636 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
638 ip6_def_policy
.refcnt
++;
639 lck_mtx_unlock(sadb_mutex
);
641 return &ip6_def_policy
;
643 case IPSEC_POLICY_IPSEC
:
644 lck_mtx_lock(sadb_mutex
);
646 lck_mtx_unlock(sadb_mutex
);
651 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
652 "Invalid policy for PCB %d\n", currsp
->policy
));
659 /* when non-privilieged socket */
660 /* look for a policy in SPD */
661 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
664 if (kernsp
!= NULL
) {
665 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
666 printf("DP ipsec6_getpolicybysock called "
667 "to allocate SP:%p\n", kernsp
));
673 switch (currsp
->policy
) {
674 case IPSEC_POLICY_BYPASS
:
675 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
676 "Illegal policy for non-priviliged defined %d\n",
681 case IPSEC_POLICY_ENTRUST
:
682 lck_mtx_lock(sadb_mutex
);
683 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
684 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
686 "fixed system default policy: %d->%d\n",
687 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
688 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
690 ip6_def_policy
.refcnt
++;
691 lck_mtx_unlock(sadb_mutex
);
693 return &ip6_def_policy
;
695 case IPSEC_POLICY_IPSEC
:
696 lck_mtx_lock(sadb_mutex
);
698 lck_mtx_unlock(sadb_mutex
);
704 "ipsec6_policybysock: Invalid policy for PCB %d\n",
713 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
714 * and return a pointer to SP.
715 * `flag' means that packet is to be forwarded whether or not.
717 * OUT: positive: a pointer to the entry for security policy leaf matched.
718 * NULL: no apropreate SP found, the following value is set to error.
720 * EACCES : discard packet.
721 * ENOENT : ipsec_acquire() in progress, maybe.
722 * others : error occurred.
724 #ifndef IP_FORWARDING
725 #define IP_FORWARDING 1
729 ipsec6_getpolicybyaddr(m
, dir
, flag
, error
)
735 struct secpolicy
*sp
= NULL
;
737 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
740 if (m
== NULL
|| error
== NULL
)
741 panic("ipsec6_getpolicybyaddr: NULL pointer was passed.\n");
744 struct secpolicyindex spidx
;
746 bzero(&spidx
, sizeof(spidx
));
748 /* make a index to look for a policy */
749 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET6
, m
,
750 (flag
& IP_FORWARDING
) ? 0 : 1);
755 sp
= key_allocsp(&spidx
, dir
);
760 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
761 printf("DP ipsec6_getpolicybyaddr called "
762 "to allocate SP:%p\n", sp
));
768 lck_mtx_lock(sadb_mutex
);
769 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
770 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
771 ipseclog((LOG_INFO
, "fixed system default policy: %d->%d\n",
772 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
773 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
775 ip6_def_policy
.refcnt
++;
776 lck_mtx_unlock(sadb_mutex
);
778 return &ip6_def_policy
;
783 * set IP address into spidx from mbuf.
784 * When Forwarding packet and ICMP echo reply, this function is used.
786 * IN: get the followings from mbuf.
787 * protocol family, src, dst, next protocol
790 * other: failure, and set errno.
794 struct secpolicyindex
*spidx
,
796 __unused u_int family
,
803 if (spidx
== NULL
|| m
== NULL
)
804 panic("ipsec_setspidx_mbuf: NULL pointer was passed.\n");
806 bzero(spidx
, sizeof(*spidx
));
808 error
= ipsec_setspidx(m
, spidx
, needport
);
817 bzero(spidx
, sizeof(*spidx
));
822 ipsec4_setspidx_inpcb(m
, pcb
)
826 struct secpolicyindex
*spidx
;
829 if (ipsec_bypass
!= 0)
834 panic("ipsec4_setspidx_inpcb: no PCB found.\n");
835 if (pcb
->inp_sp
== NULL
)
836 panic("ipsec4_setspidx_inpcb: no inp_sp found.\n");
837 if (pcb
->inp_sp
->sp_out
== NULL
|| pcb
->inp_sp
->sp_in
== NULL
)
838 panic("ipsec4_setspidx_inpcb: no sp_in/out found.\n");
840 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
841 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
843 spidx
= &pcb
->inp_sp
->sp_in
->spidx
;
844 error
= ipsec_setspidx(m
, spidx
, 1);
847 spidx
->dir
= IPSEC_DIR_INBOUND
;
849 spidx
= &pcb
->inp_sp
->sp_out
->spidx
;
850 error
= ipsec_setspidx(m
, spidx
, 1);
853 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
858 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
859 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
865 ipsec6_setspidx_in6pcb(m
, pcb
)
869 struct secpolicyindex
*spidx
;
874 panic("ipsec6_setspidx_in6pcb: no PCB found.\n");
875 if (pcb
->in6p_sp
== NULL
)
876 panic("ipsec6_setspidx_in6pcb: no in6p_sp found.\n");
877 if (pcb
->in6p_sp
->sp_out
== NULL
|| pcb
->in6p_sp
->sp_in
== NULL
)
878 panic("ipsec6_setspidx_in6pcb: no sp_in/out found.\n");
880 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
881 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
883 spidx
= &pcb
->in6p_sp
->sp_in
->spidx
;
884 error
= ipsec_setspidx(m
, spidx
, 1);
887 spidx
->dir
= IPSEC_DIR_INBOUND
;
889 spidx
= &pcb
->in6p_sp
->sp_out
->spidx
;
890 error
= ipsec_setspidx(m
, spidx
, 1);
893 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
898 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
899 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
905 * configure security policy index (src/dst/proto/sport/dport)
906 * by looking at the content of mbuf.
907 * the caller is responsible for error recovery (like clearing up spidx).
910 ipsec_setspidx(m
, spidx
, needport
)
912 struct secpolicyindex
*spidx
;
915 struct ip
*ip
= NULL
;
923 panic("ipsec_setspidx: m == 0 passed.\n");
926 * validate m->m_pkthdr.len. we see incorrect length if we
927 * mistakenly call this function with inconsistent mbuf chain
928 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
931 for (n
= m
; n
; n
= n
->m_next
)
933 if (m
->m_pkthdr
.len
!= len
) {
934 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
935 printf("ipsec_setspidx: "
936 "total of m_len(%d) != pkthdr.len(%d), "
938 len
, m
->m_pkthdr
.len
));
942 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
943 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
944 printf("ipsec_setspidx: "
945 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
950 if (m
->m_len
>= sizeof(*ip
))
951 ip
= mtod(m
, struct ip
*);
953 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
957 v
= _IP_VHL_V(ip
->ip_vhl
);
963 error
= ipsec4_setspidx_ipaddr(m
, spidx
);
966 ipsec4_get_ulp(m
, spidx
, needport
);
970 if (m
->m_pkthdr
.len
< sizeof(struct ip6_hdr
)) {
971 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
972 printf("ipsec_setspidx: "
973 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
974 "ignored.\n", m
->m_pkthdr
.len
));
977 error
= ipsec6_setspidx_ipaddr(m
, spidx
);
980 ipsec6_get_ulp(m
, spidx
, needport
);
984 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
985 printf("ipsec_setspidx: "
986 "unknown IP version %u, ignored.\n", v
));
992 ipsec4_get_ulp(m
, spidx
, needport
)
994 struct secpolicyindex
*spidx
;
1006 panic("ipsec4_get_ulp: NULL pointer was passed.\n");
1007 if (m
->m_pkthdr
.len
< sizeof(ip
))
1008 panic("ipsec4_get_ulp: too short\n");
1011 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
1012 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
= IPSEC_PORT_ANY
;
1013 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
= IPSEC_PORT_ANY
;
1015 m_copydata(m
, 0, sizeof(ip
), (caddr_t
)&ip
);
1016 /* ip_input() flips it into host endian XXX need more checking */
1017 if (ip
.ip_off
& (IP_MF
| IP_OFFMASK
))
1022 off
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
1024 off
= ip
.ip_hl
<< 2;
1026 while (off
< m
->m_pkthdr
.len
) {
1029 spidx
->ul_proto
= nxt
;
1032 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
1034 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
1035 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
1037 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
1041 spidx
->ul_proto
= nxt
;
1044 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1046 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1047 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
1049 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
1053 if (off
+ sizeof(ip6e
) > m
->m_pkthdr
.len
)
1055 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1056 off
+= (ip6e
.ip6e_len
+ 2) << 2;
1057 nxt
= ip6e
.ip6e_nxt
;
1061 /* XXX intermediate headers??? */
1062 spidx
->ul_proto
= nxt
;
1068 /* assumes that m is sane */
1070 ipsec4_setspidx_ipaddr(m
, spidx
)
1072 struct secpolicyindex
*spidx
;
1074 struct ip
*ip
= NULL
;
1076 struct sockaddr_in
*sin
;
1078 if (m
->m_len
>= sizeof(*ip
))
1079 ip
= mtod(m
, struct ip
*);
1081 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
1085 sin
= (struct sockaddr_in
*)&spidx
->src
;
1086 bzero(sin
, sizeof(*sin
));
1087 sin
->sin_family
= AF_INET
;
1088 sin
->sin_len
= sizeof(struct sockaddr_in
);
1089 bcopy(&ip
->ip_src
, &sin
->sin_addr
, sizeof(ip
->ip_src
));
1090 spidx
->prefs
= sizeof(struct in_addr
) << 3;
1092 sin
= (struct sockaddr_in
*)&spidx
->dst
;
1093 bzero(sin
, sizeof(*sin
));
1094 sin
->sin_family
= AF_INET
;
1095 sin
->sin_len
= sizeof(struct sockaddr_in
);
1096 bcopy(&ip
->ip_dst
, &sin
->sin_addr
, sizeof(ip
->ip_dst
));
1097 spidx
->prefd
= sizeof(struct in_addr
) << 3;
1103 ipsec6_get_ulp(m
, spidx
, needport
)
1105 struct secpolicyindex
*spidx
;
1114 panic("ipsec6_get_ulp: NULL pointer was passed.\n");
1116 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1117 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m
));
1120 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
1121 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= IPSEC_PORT_ANY
;
1122 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= IPSEC_PORT_ANY
;
1125 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
1126 if (off
< 0 || m
->m_pkthdr
.len
< off
)
1131 spidx
->ul_proto
= nxt
;
1134 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
1136 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
1137 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= th
.th_sport
;
1138 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= th
.th_dport
;
1141 spidx
->ul_proto
= nxt
;
1144 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1146 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1147 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= uh
.uh_sport
;
1148 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= uh
.uh_dport
;
1150 case IPPROTO_ICMPV6
:
1152 /* XXX intermediate headers??? */
1153 spidx
->ul_proto
= nxt
;
1158 /* assumes that m is sane */
1160 ipsec6_setspidx_ipaddr(m
, spidx
)
1162 struct secpolicyindex
*spidx
;
1164 struct ip6_hdr
*ip6
= NULL
;
1165 struct ip6_hdr ip6buf
;
1166 struct sockaddr_in6
*sin6
;
1168 if (m
->m_len
>= sizeof(*ip6
))
1169 ip6
= mtod(m
, struct ip6_hdr
*);
1171 m_copydata(m
, 0, sizeof(ip6buf
), (caddr_t
)&ip6buf
);
1175 sin6
= (struct sockaddr_in6
*)&spidx
->src
;
1176 bzero(sin6
, sizeof(*sin6
));
1177 sin6
->sin6_family
= AF_INET6
;
1178 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1179 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_src
));
1180 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1181 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1182 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
1184 spidx
->prefs
= sizeof(struct in6_addr
) << 3;
1186 sin6
= (struct sockaddr_in6
*)&spidx
->dst
;
1187 bzero(sin6
, sizeof(*sin6
));
1188 sin6
->sin6_family
= AF_INET6
;
1189 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1190 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_dst
));
1191 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
1192 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1193 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
1195 spidx
->prefd
= sizeof(struct in6_addr
) << 3;
1201 static struct inpcbpolicy
*
1202 ipsec_newpcbpolicy()
1204 struct inpcbpolicy
*p
;
1206 p
= (struct inpcbpolicy
*)_MALLOC(sizeof(*p
), M_SECA
, M_WAITOK
);
1211 ipsec_delpcbpolicy(p
)
1212 struct inpcbpolicy
*p
;
1217 /* initialize policy in PCB */
1219 ipsec_init_policy(so
, pcb_sp
)
1221 struct inpcbpolicy
**pcb_sp
;
1223 struct inpcbpolicy
*new;
1226 if (so
== NULL
|| pcb_sp
== NULL
)
1227 panic("ipsec_init_policy: NULL pointer was passed.\n");
1229 new = ipsec_newpcbpolicy();
1231 ipseclog((LOG_DEBUG
, "ipsec_init_policy: No more memory.\n"));
1234 bzero(new, sizeof(*new));
1237 if (so
->so_uid
== 0)
1239 if (so
->so_cred
!= 0 && !suser(so
->so_cred
->pc_ucred
, NULL
))
1245 if ((new->sp_in
= key_newsp()) == NULL
) {
1246 ipsec_delpcbpolicy(new);
1249 new->sp_in
->state
= IPSEC_SPSTATE_ALIVE
;
1250 new->sp_in
->policy
= IPSEC_POLICY_ENTRUST
;
1252 if ((new->sp_out
= key_newsp()) == NULL
) {
1253 key_freesp(new->sp_in
, KEY_SADB_UNLOCKED
);
1254 ipsec_delpcbpolicy(new);
1257 new->sp_out
->state
= IPSEC_SPSTATE_ALIVE
;
1258 new->sp_out
->policy
= IPSEC_POLICY_ENTRUST
;
1265 /* copy old ipsec policy into new */
1267 ipsec_copy_policy(old
, new)
1268 struct inpcbpolicy
*old
, *new;
1270 struct secpolicy
*sp
;
1272 if (ipsec_bypass
!= 0)
1275 sp
= ipsec_deepcopy_policy(old
->sp_in
);
1277 key_freesp(new->sp_in
, KEY_SADB_UNLOCKED
);
1282 sp
= ipsec_deepcopy_policy(old
->sp_out
);
1284 key_freesp(new->sp_out
, KEY_SADB_UNLOCKED
);
1289 new->priv
= old
->priv
;
1294 /* deep-copy a policy in PCB */
1295 static struct secpolicy
*
1296 ipsec_deepcopy_policy(src
)
1297 struct secpolicy
*src
;
1299 struct ipsecrequest
*newchain
= NULL
;
1300 struct ipsecrequest
*p
;
1301 struct ipsecrequest
**q
;
1302 struct ipsecrequest
*r
;
1303 struct secpolicy
*dst
;
1312 * deep-copy IPsec request chain. This is required since struct
1313 * ipsecrequest is not reference counted.
1316 for (p
= src
->req
; p
; p
= p
->next
) {
1317 *q
= (struct ipsecrequest
*)_MALLOC(sizeof(struct ipsecrequest
),
1321 bzero(*q
, sizeof(**q
));
1324 (*q
)->saidx
.proto
= p
->saidx
.proto
;
1325 (*q
)->saidx
.mode
= p
->saidx
.mode
;
1326 (*q
)->level
= p
->level
;
1327 (*q
)->saidx
.reqid
= p
->saidx
.reqid
;
1329 bcopy(&p
->saidx
.src
, &(*q
)->saidx
.src
, sizeof((*q
)->saidx
.src
));
1330 bcopy(&p
->saidx
.dst
, &(*q
)->saidx
.dst
, sizeof((*q
)->saidx
.dst
));
1337 dst
->req
= newchain
;
1338 dst
->state
= src
->state
;
1339 dst
->policy
= src
->policy
;
1340 /* do not touch the refcnt fields */
1345 for (p
= newchain
; p
; p
= r
) {
1350 key_freesp(dst
, KEY_SADB_UNLOCKED
);
1354 /* set policy and ipsec request if present. */
1357 struct secpolicy
**pcb_sp
,
1358 __unused
int optname
,
1363 struct sadb_x_policy
*xpl
;
1364 struct secpolicy
*newsp
= NULL
;
1368 if (pcb_sp
== NULL
|| *pcb_sp
== NULL
|| request
== NULL
)
1370 if (len
< sizeof(*xpl
))
1372 xpl
= (struct sadb_x_policy
*)request
;
1374 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1375 printf("ipsec_set_policy: passed policy\n");
1376 kdebug_sadb_x_policy((struct sadb_ext
*)xpl
));
1378 /* check policy type */
1379 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1380 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
1381 || xpl
->sadb_x_policy_type
== IPSEC_POLICY_NONE
)
1384 /* check privileged socket */
1385 if (priv
== 0 && xpl
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
)
1388 /* allocation new SP entry */
1389 if ((newsp
= key_msg2sp(xpl
, len
, &error
)) == NULL
)
1392 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1394 /* clear old SP and set new SP */
1395 key_freesp(*pcb_sp
, KEY_SADB_UNLOCKED
);
1397 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1398 printf("ipsec_set_policy: new policy\n");
1399 kdebug_secpolicy(newsp
));
1405 ipsec_get_policy(pcb_sp
, mp
)
1406 struct secpolicy
*pcb_sp
;
1412 if (pcb_sp
== NULL
|| mp
== NULL
)
1415 *mp
= key_sp2msg(pcb_sp
);
1417 ipseclog((LOG_DEBUG
, "ipsec_get_policy: No more memory.\n"));
1421 m_mchtype(*mp
, MT_DATA
);
1422 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1423 printf("ipsec_get_policy:\n");
1430 ipsec4_set_policy(inp
, optname
, request
, len
, priv
)
1437 struct sadb_x_policy
*xpl
;
1438 struct secpolicy
**pcb_sp
;
1442 if (inp
== NULL
|| request
== NULL
)
1444 if (len
< sizeof(*xpl
))
1446 xpl
= (struct sadb_x_policy
*)request
;
1448 if (inp
->inp_sp
== NULL
) {
1449 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1454 /* select direction */
1455 switch (xpl
->sadb_x_policy_dir
) {
1456 case IPSEC_DIR_INBOUND
:
1457 pcb_sp
= &inp
->inp_sp
->sp_in
;
1459 case IPSEC_DIR_OUTBOUND
:
1460 pcb_sp
= &inp
->inp_sp
->sp_out
;
1463 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1464 xpl
->sadb_x_policy_dir
));
1468 /* turn bypass off */
1469 if (ipsec_bypass
!= 0)
1472 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1476 ipsec4_get_policy(inp
, request
, len
, mp
)
1482 struct sadb_x_policy
*xpl
;
1483 struct secpolicy
*pcb_sp
;
1486 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1489 if (inp
== NULL
|| request
== NULL
|| mp
== NULL
)
1491 if (len
< sizeof(*xpl
))
1493 xpl
= (struct sadb_x_policy
*)request
;
1495 if (inp
->inp_sp
== NULL
) {
1496 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1501 /* select direction */
1502 switch (xpl
->sadb_x_policy_dir
) {
1503 case IPSEC_DIR_INBOUND
:
1504 pcb_sp
= inp
->inp_sp
->sp_in
;
1506 case IPSEC_DIR_OUTBOUND
:
1507 pcb_sp
= inp
->inp_sp
->sp_out
;
1510 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1511 xpl
->sadb_x_policy_dir
));
1515 return ipsec_get_policy(pcb_sp
, mp
);
1518 /* delete policy in PCB */
1520 ipsec4_delete_pcbpolicy(inp
)
1526 panic("ipsec4_delete_pcbpolicy: NULL pointer was passed.\n");
1528 if (inp
->inp_sp
== NULL
)
1531 if (inp
->inp_sp
->sp_in
!= NULL
) {
1532 key_freesp(inp
->inp_sp
->sp_in
, KEY_SADB_UNLOCKED
);
1533 inp
->inp_sp
->sp_in
= NULL
;
1536 if (inp
->inp_sp
->sp_out
!= NULL
) {
1537 key_freesp(inp
->inp_sp
->sp_out
, KEY_SADB_UNLOCKED
);
1538 inp
->inp_sp
->sp_out
= NULL
;
1541 ipsec_delpcbpolicy(inp
->inp_sp
);
1549 ipsec6_set_policy(in6p
, optname
, request
, len
, priv
)
1550 struct in6pcb
*in6p
;
1556 struct sadb_x_policy
*xpl
;
1557 struct secpolicy
**pcb_sp
;
1561 if (in6p
== NULL
|| request
== NULL
)
1563 if (len
< sizeof(*xpl
))
1565 xpl
= (struct sadb_x_policy
*)request
;
1567 if (in6p
->in6p_sp
== NULL
) {
1568 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1573 /* select direction */
1574 switch (xpl
->sadb_x_policy_dir
) {
1575 case IPSEC_DIR_INBOUND
:
1576 pcb_sp
= &in6p
->in6p_sp
->sp_in
;
1578 case IPSEC_DIR_OUTBOUND
:
1579 pcb_sp
= &in6p
->in6p_sp
->sp_out
;
1582 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1583 xpl
->sadb_x_policy_dir
));
1587 /* turn bypass off */
1588 if (ipsec_bypass
!= 0)
1591 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1595 ipsec6_get_policy(in6p
, request
, len
, mp
)
1596 struct in6pcb
*in6p
;
1601 struct sadb_x_policy
*xpl
;
1602 struct secpolicy
*pcb_sp
;
1606 if (in6p
== NULL
|| request
== NULL
|| mp
== NULL
)
1608 if (len
< sizeof(*xpl
))
1610 xpl
= (struct sadb_x_policy
*)request
;
1612 if (in6p
->in6p_sp
== NULL
) {
1613 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1618 /* select direction */
1619 switch (xpl
->sadb_x_policy_dir
) {
1620 case IPSEC_DIR_INBOUND
:
1621 pcb_sp
= in6p
->in6p_sp
->sp_in
;
1623 case IPSEC_DIR_OUTBOUND
:
1624 pcb_sp
= in6p
->in6p_sp
->sp_out
;
1627 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1628 xpl
->sadb_x_policy_dir
));
1632 return ipsec_get_policy(pcb_sp
, mp
);
1636 ipsec6_delete_pcbpolicy(in6p
)
1637 struct in6pcb
*in6p
;
1642 panic("ipsec6_delete_pcbpolicy: NULL pointer was passed.\n");
1644 if (in6p
->in6p_sp
== NULL
)
1647 if (in6p
->in6p_sp
->sp_in
!= NULL
) {
1648 key_freesp(in6p
->in6p_sp
->sp_in
, KEY_SADB_UNLOCKED
);
1649 in6p
->in6p_sp
->sp_in
= NULL
;
1652 if (in6p
->in6p_sp
->sp_out
!= NULL
) {
1653 key_freesp(in6p
->in6p_sp
->sp_out
, KEY_SADB_UNLOCKED
);
1654 in6p
->in6p_sp
->sp_out
= NULL
;
1657 ipsec_delpcbpolicy(in6p
->in6p_sp
);
1658 in6p
->in6p_sp
= NULL
;
1665 * return current level.
1666 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1669 ipsec_get_reqlevel(isr
)
1670 struct ipsecrequest
*isr
;
1673 u_int esp_trans_deflev
= 0, esp_net_deflev
= 0, ah_trans_deflev
= 0, ah_net_deflev
= 0;
1676 if (isr
== NULL
|| isr
->sp
== NULL
)
1677 panic("ipsec_get_reqlevel: NULL pointer is passed.\n");
1678 if (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
1679 != ((struct sockaddr
*)&isr
->sp
->spidx
.dst
)->sa_family
)
1680 panic("ipsec_get_reqlevel: family mismatched.\n");
1682 /* XXX note that we have ipseclog() expanded here - code sync issue */
1683 #define IPSEC_CHECK_DEFAULT(lev) \
1684 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1685 && (lev) != IPSEC_LEVEL_UNIQUE) \
1687 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1688 (lev), IPSEC_LEVEL_REQUIRE) \
1690 (lev) = IPSEC_LEVEL_REQUIRE, \
1694 /* set default level */
1695 switch (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
) {
1698 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev
);
1699 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev
);
1700 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev
);
1701 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev
);
1706 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev
);
1707 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev
);
1708 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev
);
1709 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev
);
1713 panic("key_get_reqlevel: Unknown family. %d\n",
1714 ((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
);
1717 #undef IPSEC_CHECK_DEFAULT
1720 switch (isr
->level
) {
1721 case IPSEC_LEVEL_DEFAULT
:
1722 switch (isr
->saidx
.proto
) {
1724 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1725 level
= esp_net_deflev
;
1727 level
= esp_trans_deflev
;
1730 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1731 level
= ah_net_deflev
;
1733 level
= ah_trans_deflev
;
1735 case IPPROTO_IPCOMP
:
1737 * we don't really care, as IPcomp document says that
1738 * we shouldn't compress small packets
1740 level
= IPSEC_LEVEL_USE
;
1743 panic("ipsec_get_reqlevel: "
1744 "Illegal protocol defined %u\n",
1749 case IPSEC_LEVEL_USE
:
1750 case IPSEC_LEVEL_REQUIRE
:
1753 case IPSEC_LEVEL_UNIQUE
:
1754 level
= IPSEC_LEVEL_REQUIRE
;
1758 panic("ipsec_get_reqlevel: Illegal IPsec level %u\n",
1766 * Check AH/ESP integrity.
1772 ipsec_in_reject(sp
, m
)
1773 struct secpolicy
*sp
;
1776 struct ipsecrequest
*isr
;
1778 int need_auth
, need_conf
, need_icv
;
1780 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1781 printf("ipsec_in_reject: using SP\n");
1782 kdebug_secpolicy(sp
));
1785 switch (sp
->policy
) {
1786 case IPSEC_POLICY_DISCARD
:
1787 case IPSEC_POLICY_GENERATE
:
1789 case IPSEC_POLICY_BYPASS
:
1790 case IPSEC_POLICY_NONE
:
1793 case IPSEC_POLICY_IPSEC
:
1796 case IPSEC_POLICY_ENTRUST
:
1798 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1805 /* XXX should compare policy against ipsec header history */
1807 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1809 /* get current level */
1810 level
= ipsec_get_reqlevel(isr
);
1812 switch (isr
->saidx
.proto
) {
1814 if (level
== IPSEC_LEVEL_REQUIRE
) {
1818 /* this won't work with multiple input threads - isr->sav would change
1819 * with every packet and is not necessarily related to the current packet
1820 * being processed. If ESP processing is required - the esp code should
1821 * make sure that the integrity check is present and correct. I don't see
1822 * why it would be necessary to check for the presence of the integrity
1823 * check value here. I think this is just wrong.
1824 * isr->sav has been removed.
1825 * %%%%%% this needs to be re-worked at some point but I think the code below can
1826 * be ignored for now.
1828 if (isr
->sav
!= NULL
1829 && isr
->sav
->flags
== SADB_X_EXT_NONE
1830 && isr
->sav
->alg_auth
!= SADB_AALG_NONE
)
1836 if (level
== IPSEC_LEVEL_REQUIRE
) {
1841 case IPPROTO_IPCOMP
:
1843 * we don't really care, as IPcomp document says that
1844 * we shouldn't compress small packets, IPComp policy
1845 * should always be treated as being in "use" level.
1851 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1852 printf("ipsec_in_reject: auth:%d conf:%d icv:%d m_flags:%x\n",
1853 need_auth
, need_conf
, need_icv
, m
->m_flags
));
1855 if ((need_conf
&& !(m
->m_flags
& M_DECRYPTED
))
1856 || (!need_auth
&& need_icv
&& !(m
->m_flags
& M_AUTHIPDGM
))
1857 || (need_auth
&& !(m
->m_flags
& M_AUTHIPHDR
)))
1864 * Check AH/ESP integrity.
1865 * This function is called from tcp_input(), udp_input(),
1866 * and {ah,esp}4_input for tunnel mode
1869 ipsec4_in_reject_so(m
, so
)
1873 struct secpolicy
*sp
= NULL
;
1877 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1880 return 0; /* XXX should be panic ? */
1882 /* get SP for this packet.
1883 * When we are called from ip_forward(), we call
1884 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1887 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1889 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1892 return 0; /* XXX should be panic ?
1893 * -> No, there may be error. */
1895 result
= ipsec_in_reject(sp
, m
);
1896 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1897 printf("DP ipsec4_in_reject_so call free SP:%p\n", sp
));
1898 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1904 ipsec4_in_reject(m
, inp
)
1909 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1911 return ipsec4_in_reject_so(m
, NULL
);
1912 if (inp
->inp_socket
)
1913 return ipsec4_in_reject_so(m
, inp
->inp_socket
);
1915 panic("ipsec4_in_reject: invalid inpcb/socket");
1923 * Check AH/ESP integrity.
1924 * This function is called from tcp6_input(), udp6_input(),
1925 * and {ah,esp}6_input for tunnel mode
1928 ipsec6_in_reject_so(m
, so
)
1932 struct secpolicy
*sp
= NULL
;
1936 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1939 return 0; /* XXX should be panic ? */
1941 /* get SP for this packet.
1942 * When we are called from ip_forward(), we call
1943 * ipsec6_getpolicybyaddr() with IP_FORWARDING flag.
1946 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1948 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1951 return 0; /* XXX should be panic ? */
1953 result
= ipsec_in_reject(sp
, m
);
1954 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1955 printf("DP ipsec6_in_reject_so call free SP:%p\n", sp
));
1956 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1962 ipsec6_in_reject(m
, in6p
)
1964 struct in6pcb
*in6p
;
1967 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1969 return ipsec6_in_reject_so(m
, NULL
);
1970 if (in6p
->in6p_socket
)
1971 return ipsec6_in_reject_so(m
, in6p
->in6p_socket
);
1973 panic("ipsec6_in_reject: invalid in6p/socket");
1981 * compute the byte size to be occupied by IPsec header.
1982 * in case it is tunneled, it includes the size of outer IP header.
1983 * NOTE: SP passed is free in this function.
1987 struct secpolicy
*sp
;
1989 struct ipsecrequest
*isr
;
1992 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1993 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1994 printf("ipsec_hdrsiz: using SP\n");
1995 kdebug_secpolicy(sp
));
1998 switch (sp
->policy
) {
1999 case IPSEC_POLICY_DISCARD
:
2000 case IPSEC_POLICY_GENERATE
:
2001 case IPSEC_POLICY_BYPASS
:
2002 case IPSEC_POLICY_NONE
:
2005 case IPSEC_POLICY_IPSEC
:
2008 case IPSEC_POLICY_ENTRUST
:
2010 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
2015 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2019 switch (isr
->saidx
.proto
) {
2022 clen
= esp_hdrsiz(isr
);
2028 clen
= ah_hdrsiz(isr
);
2030 case IPPROTO_IPCOMP
:
2031 clen
= sizeof(struct ipcomp
);
2035 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2036 switch (((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
) {
2038 clen
+= sizeof(struct ip
);
2042 clen
+= sizeof(struct ip6_hdr
);
2046 ipseclog((LOG_ERR
, "ipsec_hdrsiz: "
2047 "unknown AF %d in IPsec tunnel SA\n",
2048 ((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
));
2058 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
2060 ipsec4_hdrsiz(m
, dir
, inp
)
2065 struct secpolicy
*sp
= NULL
;
2069 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2072 return 0; /* XXX should be panic ? */
2073 if (inp
!= NULL
&& inp
->inp_socket
== NULL
)
2074 panic("ipsec4_hdrsize: why is socket NULL but there is PCB.");
2076 /* get SP for this packet.
2077 * When we are called from ip_forward(), we call
2078 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
2081 sp
= ipsec4_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2083 sp
= ipsec4_getpolicybysock(m
, dir
, inp
->inp_socket
, &error
);
2086 return 0; /* XXX should be panic ? */
2088 size
= ipsec_hdrsiz(sp
);
2089 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2090 printf("DP ipsec4_hdrsiz call free SP:%p\n", sp
));
2091 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2092 printf("ipsec4_hdrsiz: size:%lu.\n", (unsigned long)size
));
2093 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2099 /* This function is called from ipsec6_hdrsize_tcp(),
2100 * and maybe from ip6_forward.()
2103 ipsec6_hdrsiz(m
, dir
, in6p
)
2106 struct in6pcb
*in6p
;
2108 struct secpolicy
*sp
= NULL
;
2112 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2115 return 0; /* XXX shoud be panic ? */
2116 if (in6p
!= NULL
&& in6p
->in6p_socket
== NULL
)
2117 panic("ipsec6_hdrsize: why is socket NULL but there is PCB.");
2119 /* get SP for this packet */
2120 /* XXX Is it right to call with IP_FORWARDING. */
2122 sp
= ipsec6_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2124 sp
= ipsec6_getpolicybysock(m
, dir
, in6p
->in6p_socket
, &error
);
2128 size
= ipsec_hdrsiz(sp
);
2129 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2130 printf("DP ipsec6_hdrsiz call free SP:%p\n", sp
));
2131 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2132 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size
));
2133 key_freesp(sp
, KEY_SADB_UNLOCKED
);
2141 * encapsulate for ipsec tunnel.
2142 * ip->ip_src must be fixed later on.
2145 ipsec4_encapsulate(m
, sav
)
2147 struct secasvar
*sav
;
2154 /* can't tunnel between different AFs */
2155 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2156 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2157 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2162 /* XXX if the dst is myself, perform nothing. */
2163 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2169 if (m
->m_len
< sizeof(*ip
))
2170 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2172 ip
= mtod(m
, struct ip
*);
2174 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
2176 hlen
= ip
->ip_hl
<< 2;
2179 if (m
->m_len
!= hlen
)
2180 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2182 /* generate header checksum */
2185 ip
->ip_sum
= in_cksum(m
, hlen
);
2187 ip
->ip_sum
= in_cksum(m
, hlen
);
2190 plen
= m
->m_pkthdr
.len
;
2193 * grow the mbuf to accomodate the new IPv4 header.
2194 * NOTE: IPv4 options will never be copied.
2196 if (M_LEADINGSPACE(m
->m_next
) < hlen
) {
2198 MGET(n
, M_DONTWAIT
, MT_DATA
);
2204 n
->m_next
= m
->m_next
;
2206 m
->m_pkthdr
.len
+= hlen
;
2207 oip
= mtod(n
, struct ip
*);
2209 m
->m_next
->m_len
+= hlen
;
2210 m
->m_next
->m_data
-= hlen
;
2211 m
->m_pkthdr
.len
+= hlen
;
2212 oip
= mtod(m
->m_next
, struct ip
*);
2214 ip
= mtod(m
, struct ip
*);
2215 ovbcopy((caddr_t
)ip
, (caddr_t
)oip
, hlen
);
2216 m
->m_len
= sizeof(struct ip
);
2217 m
->m_pkthdr
.len
-= (hlen
- sizeof(struct ip
));
2219 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
2220 /* ECN consideration. */
2221 ip_ecn_ingress(ip4_ipsec_ecn
, &ip
->ip_tos
, &oip
->ip_tos
);
2223 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2225 ip
->ip_hl
= sizeof(struct ip
) >> 2;
2227 ip
->ip_off
&= htons(~IP_OFFMASK
);
2228 ip
->ip_off
&= htons(~IP_MF
);
2229 switch (ip4_ipsec_dfbit
) {
2230 case 0: /* clear DF bit */
2231 ip
->ip_off
&= htons(~IP_DF
);
2233 case 1: /* set DF bit */
2234 ip
->ip_off
|= htons(IP_DF
);
2236 default: /* copy DF bit */
2239 ip
->ip_p
= IPPROTO_IPIP
;
2240 if (plen
+ sizeof(struct ip
) < IP_MAXPACKET
)
2241 ip
->ip_len
= htons(plen
+ sizeof(struct ip
));
2243 ipseclog((LOG_ERR
, "IPv4 ipsec: size exceeds limit: "
2244 "leave ip_len as is (invalid packet)\n"));
2247 ip
->ip_id
= ip_randomid();
2249 ip
->ip_id
= htons(ip_id
++);
2251 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
,
2252 &ip
->ip_src
, sizeof(ip
->ip_src
));
2253 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
,
2254 &ip
->ip_dst
, sizeof(ip
->ip_dst
));
2255 ip
->ip_ttl
= IPDEFTTL
;
2257 /* XXX Should ip_src be updated later ? */
2265 ipsec6_encapsulate(m
, sav
)
2267 struct secasvar
*sav
;
2269 struct ip6_hdr
*oip6
;
2270 struct ip6_hdr
*ip6
;
2273 /* can't tunnel between different AFs */
2274 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2275 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2276 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
2281 /* XXX if the dst is myself, perform nothing. */
2282 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2288 plen
= m
->m_pkthdr
.len
;
2291 * grow the mbuf to accomodate the new IPv6 header.
2293 if (m
->m_len
!= sizeof(struct ip6_hdr
))
2294 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2295 if (M_LEADINGSPACE(m
->m_next
) < sizeof(struct ip6_hdr
)) {
2297 MGET(n
, M_DONTWAIT
, MT_DATA
);
2302 n
->m_len
= sizeof(struct ip6_hdr
);
2303 n
->m_next
= m
->m_next
;
2305 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2306 oip6
= mtod(n
, struct ip6_hdr
*);
2308 m
->m_next
->m_len
+= sizeof(struct ip6_hdr
);
2309 m
->m_next
->m_data
-= sizeof(struct ip6_hdr
);
2310 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2311 oip6
= mtod(m
->m_next
, struct ip6_hdr
*);
2313 ip6
= mtod(m
, struct ip6_hdr
*);
2314 ovbcopy((caddr_t
)ip6
, (caddr_t
)oip6
, sizeof(struct ip6_hdr
));
2316 /* Fake link-local scope-class addresses */
2317 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
2318 oip6
->ip6_src
.s6_addr16
[1] = 0;
2319 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
2320 oip6
->ip6_dst
.s6_addr16
[1] = 0;
2322 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
2323 /* ECN consideration. */
2324 ip6_ecn_ingress(ip6_ipsec_ecn
, &ip6
->ip6_flow
, &oip6
->ip6_flow
);
2325 if (plen
< IPV6_MAXPACKET
- sizeof(struct ip6_hdr
))
2326 ip6
->ip6_plen
= htons(plen
);
2328 /* ip6->ip6_plen will be updated in ip6_output() */
2330 ip6
->ip6_nxt
= IPPROTO_IPV6
;
2331 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.src
)->sin6_addr
,
2332 &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
2333 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
)->sin6_addr
,
2334 &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
2335 ip6
->ip6_hlim
= IPV6_DEFHLIM
;
2337 /* XXX Should ip6_src be updated later ? */
2343 ipsec64_encapsulate(m
, sav
)
2345 struct secasvar
*sav
;
2347 struct ip6_hdr
*ip6
, *ip6i
;
2352 /* tunneling over IPv4 */
2353 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2354 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2355 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2360 /* XXX if the dst is myself, perform nothing. */
2361 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2367 plen
= m
->m_pkthdr
.len
;
2368 ip6
= mtod(m
, struct ip6_hdr
*);
2369 hlim
= ip6
->ip6_hlim
;
2371 * grow the mbuf to accomodate the new IPv4 header.
2373 if (m
->m_len
!= sizeof(struct ip6_hdr
))
2374 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2375 if (M_LEADINGSPACE(m
->m_next
) < sizeof(struct ip6_hdr
)) {
2377 MGET(n
, M_DONTWAIT
, MT_DATA
);
2382 n
->m_len
= sizeof(struct ip6_hdr
);
2383 n
->m_next
= m
->m_next
;
2385 m
->m_pkthdr
.len
+= sizeof(struct ip
);
2386 ip6i
= mtod(n
, struct ip6_hdr
*);
2388 m
->m_next
->m_len
+= sizeof(struct ip6_hdr
);
2389 m
->m_next
->m_data
-= sizeof(struct ip6_hdr
);
2390 m
->m_pkthdr
.len
+= sizeof(struct ip
);
2391 ip6i
= mtod(m
->m_next
, struct ip6_hdr
*);
2393 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
2394 /* ECN consideration. */
2395 /* XXX To be fixed later if needed */
2396 // ip_ecn_ingress(ip4_ipsec_ecn, &ip->ip_tos, &oip->ip_tos);
2398 bcopy(ip6
, ip6i
, sizeof(struct ip6_hdr
));
2399 ip
= mtod(m
, struct ip
*);
2400 m
->m_len
= sizeof(struct ip
);
2402 * Fill in some of the IPv4 fields - we don't need all of them
2403 * because the rest will be filled in by ip_output
2405 ip
->ip_v
= IPVERSION
;
2406 ip
->ip_hl
= sizeof(struct ip
) >> 2;
2412 ip
->ip_p
= IPPROTO_IPV6
;
2413 if (plen
+ sizeof(struct ip
) < IP_MAXPACKET
)
2414 ip
->ip_len
= htons(plen
+ sizeof(struct ip
));
2416 ip
->ip_len
= htons(plen
);
2417 ipseclog((LOG_ERR
, "IPv4 ipsec: size exceeds limit: "
2418 "leave ip_len as is (invalid packet)\n"));
2420 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
,
2421 &ip
->ip_src
, sizeof(ip
->ip_src
));
2422 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
,
2423 &ip
->ip_dst
, sizeof(ip
->ip_dst
));
2430 * Check the variable replay window.
2431 * ipsec_chkreplay() performs replay check before ICV verification.
2432 * ipsec_updatereplay() updates replay bitmap. This must be called after
2433 * ICV verification (it also performs replay check, which is usually done
2435 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2437 * based on RFC 2401.
2440 ipsec_chkreplay(seq
, sav
)
2442 struct secasvar
*sav
;
2444 const struct secreplay
*replay
;
2447 u_int32_t wsizeb
; /* constant: bits of window size */
2448 int frlast
; /* constant: last frame */
2453 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2455 lck_mtx_lock(sadb_mutex
);
2456 replay
= sav
->replay
;
2458 if (replay
->wsize
== 0) {
2459 lck_mtx_unlock(sadb_mutex
);
2460 return 1; /* no need to check replay. */
2464 frlast
= replay
->wsize
- 1;
2465 wsizeb
= replay
->wsize
<< 3;
2467 /* sequence number of 0 is invalid */
2469 lck_mtx_unlock(sadb_mutex
);
2473 /* first time is always okay */
2474 if (replay
->count
== 0) {
2475 lck_mtx_unlock(sadb_mutex
);
2479 if (seq
> replay
->lastseq
) {
2480 /* larger sequences are okay */
2481 lck_mtx_unlock(sadb_mutex
);
2484 /* seq is equal or less than lastseq. */
2485 diff
= replay
->lastseq
- seq
;
2487 /* over range to check, i.e. too old or wrapped */
2488 if (diff
>= wsizeb
) {
2489 lck_mtx_unlock(sadb_mutex
);
2493 fr
= frlast
- diff
/ 8;
2495 /* this packet already seen ? */
2496 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8))) {
2497 lck_mtx_unlock(sadb_mutex
);
2501 /* out of order but good */
2502 lck_mtx_unlock(sadb_mutex
);
2508 * check replay counter whether to update or not.
2513 ipsec_updatereplay(seq
, sav
)
2515 struct secasvar
*sav
;
2517 struct secreplay
*replay
;
2520 u_int32_t wsizeb
; /* constant: bits of window size */
2521 int frlast
; /* constant: last frame */
2525 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2527 lck_mtx_lock(sadb_mutex
);
2528 replay
= sav
->replay
;
2530 if (replay
->wsize
== 0)
2531 goto ok
; /* no need to check replay. */
2534 frlast
= replay
->wsize
- 1;
2535 wsizeb
= replay
->wsize
<< 3;
2537 /* sequence number of 0 is invalid */
2542 if (replay
->count
== 0) {
2543 replay
->lastseq
= seq
;
2544 bzero(replay
->bitmap
, replay
->wsize
);
2545 (replay
->bitmap
)[frlast
] = 1;
2549 if (seq
> replay
->lastseq
) {
2550 /* seq is larger than lastseq. */
2551 diff
= seq
- replay
->lastseq
;
2553 /* new larger sequence number */
2554 if (diff
< wsizeb
) {
2556 /* set bit for this packet */
2557 vshiftl((unsigned char *) replay
->bitmap
, diff
, replay
->wsize
);
2558 (replay
->bitmap
)[frlast
] |= 1;
2560 /* this packet has a "way larger" */
2561 bzero(replay
->bitmap
, replay
->wsize
);
2562 (replay
->bitmap
)[frlast
] = 1;
2564 replay
->lastseq
= seq
;
2566 /* larger is good */
2568 /* seq is equal or less than lastseq. */
2569 diff
= replay
->lastseq
- seq
;
2571 /* over range to check, i.e. too old or wrapped */
2572 if (diff
>= wsizeb
) {
2573 lck_mtx_unlock(sadb_mutex
);
2577 fr
= frlast
- diff
/ 8;
2579 /* this packet already seen ? */
2580 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8))) {
2581 lck_mtx_unlock(sadb_mutex
);
2586 (replay
->bitmap
)[fr
] |= (1 << (diff
% 8));
2588 /* out of order but good */
2592 if (replay
->count
== ~0) {
2594 /* set overflow flag */
2597 /* don't increment, no more packets accepted */
2598 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
2599 lck_mtx_unlock(sadb_mutex
);
2603 ipseclog((LOG_WARNING
, "replay counter made %d cycle. %s\n",
2604 replay
->overflow
, ipsec_logsastr(sav
)));
2609 lck_mtx_unlock(sadb_mutex
);
2614 * shift variable length buffer to left.
2615 * IN: bitmap: pointer to the buffer
2616 * nbit: the number of to shift.
2617 * wsize: buffer size (bytes).
2620 vshiftl(bitmap
, nbit
, wsize
)
2621 unsigned char *bitmap
;
2627 for (j
= 0; j
< nbit
; j
+= 8) {
2628 s
= (nbit
- j
< 8) ? (nbit
- j
): 8;
2630 for (i
= 1; i
< wsize
; i
++) {
2631 over
= (bitmap
[i
] >> (8 - s
));
2633 bitmap
[i
-1] |= over
;
2641 ipsec4_logpacketstr(ip
, spi
)
2645 static char buf
[256];
2649 s
= (u_int8_t
*)(&ip
->ip_src
);
2650 d
= (u_int8_t
*)(&ip
->ip_dst
);
2653 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2656 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%u.%u.%u.%u",
2657 s
[0], s
[1], s
[2], s
[3]);
2660 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%u.%u.%u.%u",
2661 d
[0], d
[1], d
[2], d
[3]);
2664 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2671 ipsec6_logpacketstr(ip6
, spi
)
2672 struct ip6_hdr
*ip6
;
2675 static char buf
[256];
2679 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2682 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%s",
2683 ip6_sprintf(&ip6
->ip6_src
));
2686 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%s",
2687 ip6_sprintf(&ip6
->ip6_dst
));
2690 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2698 struct secasvar
*sav
;
2700 static char buf
[256];
2702 struct secasindex
*saidx
= &sav
->sah
->saidx
;
2704 /* validity check */
2705 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2706 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
)
2707 panic("ipsec_logsastr: family mismatched.\n");
2710 snprintf(buf
, sizeof(buf
), "SA(SPI=%u ", (u_int32_t
)ntohl(sav
->spi
));
2713 if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET
) {
2715 s
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->src
)->sin_addr
;
2716 d
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->dst
)->sin_addr
;
2717 snprintf(p
, sizeof(buf
) - (p
- buf
),
2718 "src=%d.%d.%d.%d dst=%d.%d.%d.%d",
2719 s
[0], s
[1], s
[2], s
[3], d
[0], d
[1], d
[2], d
[3]);
2722 else if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET6
) {
2723 snprintf(p
, sizeof(buf
) - (p
- buf
),
2725 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->src
)->sin6_addr
));
2728 snprintf(p
, sizeof(buf
) - (p
- buf
),
2730 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->dst
)->sin6_addr
));
2735 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2751 p
= mtod(m
, u_char
*);
2752 for (i
= 0; i
< m
->m_len
; i
++) {
2753 printf("%02x ", p
[i
]);
2755 if (totlen
% 16 == 0)
2760 if (totlen
% 16 != 0)
2767 * IPsec output logic for IPv4.
2771 struct ipsec_output_state
*state
,
2772 struct secpolicy
*sp
,
2775 struct ip
*ip
= NULL
;
2776 struct ipsecrequest
*isr
= NULL
;
2777 struct secasindex saidx
;
2778 struct secasvar
*sav
= NULL
;
2780 struct sockaddr_in
*dst4
;
2781 struct sockaddr_in
*sin
;
2783 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2786 panic("state == NULL in ipsec4_output");
2788 panic("state->m == NULL in ipsec4_output");
2790 panic("state->ro == NULL in ipsec4_output");
2792 panic("state->dst == NULL in ipsec4_output");
2794 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_START
, 0,0,0,0,0);
2796 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2797 printf("ipsec4_output: applyed SP\n");
2798 kdebug_secpolicy(sp
));
2800 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2802 #if 0 /* give up to check restriction of transport mode */
2803 /* XXX but should be checked somewhere */
2805 * some of the IPsec operation must be performed only in
2808 if (isr
->saidx
.mode
== IPSEC_MODE_TRANSPORT
2809 && (flags
& IP_FORWARDING
))
2813 /* make SA index for search proper SA */
2814 ip
= mtod(state
->m
, struct ip
*);
2815 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2816 saidx
.mode
= isr
->saidx
.mode
;
2817 saidx
.reqid
= isr
->saidx
.reqid
;
2818 sin
= (struct sockaddr_in
*)&saidx
.src
;
2819 if (sin
->sin_len
== 0) {
2820 sin
->sin_len
= sizeof(*sin
);
2821 sin
->sin_family
= AF_INET
;
2822 sin
->sin_port
= IPSEC_PORT_ANY
;
2823 bcopy(&ip
->ip_src
, &sin
->sin_addr
,
2824 sizeof(sin
->sin_addr
));
2826 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2827 if (sin
->sin_len
== 0) {
2828 sin
->sin_len
= sizeof(*sin
);
2829 sin
->sin_family
= AF_INET
;
2830 sin
->sin_port
= IPSEC_PORT_ANY
;
2832 * Get port from packet if upper layer is UDP and nat traversal
2833 * is enabled and transport mode.
2836 if ((esp_udp_encap_port
& 0xFFFF) != 0 &&
2837 isr
->saidx
.mode
== IPSEC_MODE_TRANSPORT
) {
2839 if (ip
->ip_p
== IPPROTO_UDP
) {
2843 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
2845 hlen
= ip
->ip_hl
<< 2;
2847 if (state
->m
->m_len
< hlen
+ sizeof(struct udphdr
)) {
2848 state
->m
= m_pullup(state
->m
, hlen
+ sizeof(struct udphdr
));
2850 ipseclog((LOG_DEBUG
,
2851 "IPv4 output: can't pullup UDP header\n"));
2852 IPSEC_STAT_INCREMENT(ipsecstat
.in_inval
);
2855 ip
= mtod(state
->m
, struct ip
*);
2857 udp
= (struct udphdr
*)(((u_int8_t
*)ip
) + hlen
);
2858 sin
->sin_port
= udp
->uh_dport
;
2862 bcopy(&ip
->ip_dst
, &sin
->sin_addr
,
2863 sizeof(sin
->sin_addr
));
2866 if ((error
= key_checkrequest(isr
, &saidx
, &sav
)) != 0) {
2868 * IPsec processing is required, but no SA found.
2869 * I assume that key_acquire() had been called
2870 * to get/establish the SA. Here I discard
2871 * this packet because it is responsibility for
2872 * upper layer to retransmit the packet.
2874 IPSEC_STAT_INCREMENT(ipsecstat
.out_nosa
);
2878 /* validity check */
2880 switch (ipsec_get_reqlevel(isr
)) {
2881 case IPSEC_LEVEL_USE
:
2883 case IPSEC_LEVEL_REQUIRE
:
2884 /* must be not reached here. */
2885 panic("ipsec4_output: no SA found, but required.");
2890 * If there is no valid SA, we give up to process any
2891 * more. In such a case, the SA's status is changed
2892 * from DYING to DEAD after allocating. If a packet
2893 * send to the receiver by dead SA, the receiver can
2894 * not decode a packet because SA has been dead.
2896 if (sav
->state
!= SADB_SASTATE_MATURE
2897 && sav
->state
!= SADB_SASTATE_DYING
) {
2898 IPSEC_STAT_INCREMENT(ipsecstat
.out_nosa
);
2904 * There may be the case that SA status will be changed when
2905 * we are refering to one. So calling splsoftnet().
2908 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2910 * build IPsec tunnel.
2912 /* XXX should be processed with other familiy */
2913 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2914 ipseclog((LOG_ERR
, "ipsec4_output: "
2915 "family mismatched between inner and outer spi=%u\n",
2916 (u_int32_t
)ntohl(sav
->spi
)));
2917 error
= EAFNOSUPPORT
;
2921 state
->m
= ipsec4_splithdr(state
->m
);
2926 error
= ipsec4_encapsulate(state
->m
, sav
);
2931 ip
= mtod(state
->m
, struct ip
*);
2933 state
->ro
= &sav
->sah
->sa_route
;
2934 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
2935 dst4
= (struct sockaddr_in
*)state
->dst
;
2936 if (state
->ro
->ro_rt
2937 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
2938 || dst4
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
2939 rtfree(state
->ro
->ro_rt
);
2940 state
->ro
->ro_rt
= NULL
;
2942 if (state
->ro
->ro_rt
== 0) {
2943 dst4
->sin_family
= AF_INET
;
2944 dst4
->sin_len
= sizeof(*dst4
);
2945 dst4
->sin_addr
= ip
->ip_dst
;
2948 if (state
->ro
->ro_rt
== 0) {
2949 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_noroute
);
2950 error
= EHOSTUNREACH
;
2954 /* adjust state->dst if tunnel endpoint is offlink */
2955 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
2956 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
2957 dst4
= (struct sockaddr_in
*)state
->dst
;
2961 state
->m
= ipsec4_splithdr(state
->m
);
2966 switch (isr
->saidx
.proto
) {
2969 if ((error
= esp4_output(state
->m
, sav
)) != 0) {
2981 if ((error
= ah4_output(state
->m
, sav
)) != 0) {
2986 case IPPROTO_IPCOMP
:
2987 if ((error
= ipcomp4_output(state
->m
, sav
)) != 0) {
2994 "ipsec4_output: unknown ipsec protocol %d\n",
3002 if (state
->m
== 0) {
3006 ip
= mtod(state
->m
, struct ip
*);
3009 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_END
, 0,0,0,0,0);
3011 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3016 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3019 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_END
, error
,0,0,0,0);
3026 * IPsec output logic for IPv6, transport mode.
3029 ipsec6_output_trans(
3030 struct ipsec_output_state
*state
,
3033 struct secpolicy
*sp
,
3037 struct ip6_hdr
*ip6
;
3038 struct ipsecrequest
*isr
= NULL
;
3039 struct secasindex saidx
;
3042 struct sockaddr_in6
*sin6
;
3043 struct secasvar
*sav
= NULL
;
3045 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3048 panic("state == NULL in ipsec6_output_trans");
3050 panic("state->m == NULL in ipsec6_output_trans");
3052 panic("nexthdrp == NULL in ipsec6_output_trans");
3054 panic("mprev == NULL in ipsec6_output_trans");
3056 panic("sp == NULL in ipsec6_output_trans");
3058 panic("tun == NULL in ipsec6_output_trans");
3060 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
3061 printf("ipsec6_output_trans: applyed SP\n");
3062 kdebug_secpolicy(sp
));
3065 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
3066 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3067 /* the rest will be handled by ipsec6_output_tunnel() */
3071 /* make SA index for search proper SA */
3072 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3073 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
3074 saidx
.mode
= isr
->saidx
.mode
;
3075 saidx
.reqid
= isr
->saidx
.reqid
;
3076 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
3077 if (sin6
->sin6_len
== 0) {
3078 sin6
->sin6_len
= sizeof(*sin6
);
3079 sin6
->sin6_family
= AF_INET6
;
3080 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3081 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
3082 sizeof(ip6
->ip6_src
));
3083 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
3084 /* fix scope id for comparing SPD */
3085 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3086 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
3089 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
3090 if (sin6
->sin6_len
== 0) {
3091 sin6
->sin6_len
= sizeof(*sin6
);
3092 sin6
->sin6_family
= AF_INET6
;
3093 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3094 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
3095 sizeof(ip6
->ip6_dst
));
3096 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
3097 /* fix scope id for comparing SPD */
3098 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3099 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
3103 if (key_checkrequest(isr
, &saidx
, &sav
) == ENOENT
) {
3105 * IPsec processing is required, but no SA found.
3106 * I assume that key_acquire() had been called
3107 * to get/establish the SA. Here I discard
3108 * this packet because it is responsibility for
3109 * upper layer to retransmit the packet.
3111 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nosa
);
3115 * Notify the fact that the packet is discarded
3116 * to ourselves. I believe this is better than
3117 * just silently discarding. (jinmei@kame.net)
3118 * XXX: should we restrict the error to TCP packets?
3119 * XXX: should we directly notify sockets via
3122 icmp6_error(state
->m
, ICMP6_DST_UNREACH
,
3123 ICMP6_DST_UNREACH_ADMIN
, 0);
3124 state
->m
= NULL
; /* icmp6_error freed the mbuf */
3128 /* validity check */
3130 switch (ipsec_get_reqlevel(isr
)) {
3131 case IPSEC_LEVEL_USE
:
3133 case IPSEC_LEVEL_REQUIRE
:
3134 /* must be not reached here. */
3135 panic("ipsec6_output_trans: no SA found, but required.");
3140 * If there is no valid SA, we give up to process.
3141 * see same place at ipsec4_output().
3143 if (sav
->state
!= SADB_SASTATE_MATURE
3144 && sav
->state
!= SADB_SASTATE_DYING
) {
3145 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nosa
);
3150 switch (isr
->saidx
.proto
) {
3153 error
= esp6_output(state
->m
, nexthdrp
, mprev
->m_next
, sav
);
3160 error
= ah6_output(state
->m
, nexthdrp
, mprev
->m_next
, sav
);
3162 case IPPROTO_IPCOMP
:
3163 error
= ipcomp6_output(state
->m
, nexthdrp
, mprev
->m_next
, sav
);
3166 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
3167 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
3169 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
3177 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
3178 if (plen
> IPV6_MAXPACKET
) {
3179 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
3180 "IPsec with IPv6 jumbogram is not supported\n"));
3181 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
3182 error
= EINVAL
; /*XXX*/
3185 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3186 ip6
->ip6_plen
= htons(plen
);
3189 /* if we have more to go, we need a tunnel mode processing */
3194 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3199 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3206 * IPsec output logic for IPv6, tunnel mode.
3209 ipsec6_output_tunnel(
3210 struct ipsec_output_state
*state
,
3211 struct secpolicy
*sp
,
3215 struct ip6_hdr
*ip6
;
3216 struct ipsecrequest
*isr
= NULL
;
3217 struct secasindex saidx
;
3218 struct secasvar
*sav
= NULL
;
3221 struct sockaddr_in6
* dst6
;
3223 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3228 panic("state == NULL in ipsec6_output_tunnel");
3230 panic("state->m == NULL in ipsec6_output_tunnel");
3232 panic("sp == NULL in ipsec6_output_tunnel");
3234 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
3235 printf("ipsec6_output_tunnel: applyed SP\n");
3236 kdebug_secpolicy(sp
));
3239 * transport mode ipsec (before the 1st tunnel mode) is already
3240 * processed by ipsec6_output_trans().
3242 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
3243 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
3247 for (/* already initialized */; isr
; isr
= isr
->next
) {
3248 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3249 /* When tunnel mode, SA peers must be specified. */
3250 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
3252 /* make SA index to look for a proper SA */
3253 struct sockaddr_in6
*sin6
;
3255 bzero(&saidx
, sizeof(saidx
));
3256 saidx
.proto
= isr
->saidx
.proto
;
3257 saidx
.mode
= isr
->saidx
.mode
;
3258 saidx
.reqid
= isr
->saidx
.reqid
;
3260 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3261 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
3262 if (sin6
->sin6_len
== 0) {
3263 sin6
->sin6_len
= sizeof(*sin6
);
3264 sin6
->sin6_family
= AF_INET6
;
3265 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3266 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
3267 sizeof(ip6
->ip6_src
));
3268 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
3269 /* fix scope id for comparing SPD */
3270 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3271 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
3274 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
3275 if (sin6
->sin6_len
== 0) {
3276 sin6
->sin6_len
= sizeof(*sin6
);
3277 sin6
->sin6_family
= AF_INET6
;
3278 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3279 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
3280 sizeof(ip6
->ip6_dst
));
3281 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
3282 /* fix scope id for comparing SPD */
3283 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3284 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
3289 if (key_checkrequest(isr
, &saidx
, &sav
) == ENOENT
) {
3291 * IPsec processing is required, but no SA found.
3292 * I assume that key_acquire() had been called
3293 * to get/establish the SA. Here I discard
3294 * this packet because it is responsibility for
3295 * upper layer to retransmit the packet.
3297 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nosa
);
3302 /* validity check */
3304 switch (ipsec_get_reqlevel(isr
)) {
3305 case IPSEC_LEVEL_USE
:
3307 case IPSEC_LEVEL_REQUIRE
:
3308 /* must be not reached here. */
3309 panic("ipsec6_output_tunnel: no SA found, but required.");
3314 * If there is no valid SA, we give up to process.
3315 * see same place at ipsec4_output().
3317 if (sav
->state
!= SADB_SASTATE_MATURE
3318 && sav
->state
!= SADB_SASTATE_DYING
) {
3319 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nosa
);
3324 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3326 * build IPsec tunnel.
3328 state
->m
= ipsec6_splithdr(state
->m
);
3330 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nomem
);
3335 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
== AF_INET6
) {
3336 error
= ipsec6_encapsulate(state
->m
, sav
);
3341 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3342 } else if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
== AF_INET
) {
3345 struct sockaddr_in
* dst4
;
3346 struct route
*ro4
= NULL
;
3349 * must be last isr because encapsulated IPv6 packet
3350 * will be sent by calling ip_output
3353 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3354 "IPv4 must be outer layer, spi=%u\n",
3355 (u_int32_t
)ntohl(sav
->spi
)));
3359 *tunneledv4
= 1; /* must not process any further in ip6_output */
3360 error
= ipsec64_encapsulate(state
->m
, sav
);
3365 /* Now we have an IPv4 packet */
3366 ip
= mtod(state
->m
, struct ip
*);
3368 ro4
= &sav
->sah
->sa_route
;
3369 dst4
= (struct sockaddr_in
*)&ro4
->ro_dst
;
3371 && ((ro4
->ro_rt
->rt_flags
& RTF_UP
) == 0
3372 || dst4
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
3376 if (ro4
->ro_rt
== NULL
) {
3377 dst4
->sin_family
= AF_INET
;
3378 dst4
->sin_len
= sizeof(*dst4
);
3379 dst4
->sin_addr
= ip
->ip_dst
;
3382 if (ro4
->ro_rt
== NULL
) {
3383 OSAddAtomic(1, (SInt32
*)&ipstat
.ips_noroute
);
3384 error
= EHOSTUNREACH
;
3388 state
->m
= ipsec4_splithdr(state
->m
);
3393 switch (isr
->saidx
.proto
) {
3396 if ((error
= esp4_output(state
->m
, sav
)) != 0) {
3409 if ((error
= ah4_output(state
->m
, sav
)) != 0) {
3414 case IPPROTO_IPCOMP
:
3415 if ((error
= ipcomp4_output(state
->m
, sav
)) != 0) {
3422 "ipsec4_output: unknown ipsec protocol %d\n",
3430 if (state
->m
== 0) {
3434 ip
= mtod(state
->m
, struct ip
*);
3435 ip
->ip_len
= ntohs(ip
->ip_len
); /* flip len field before calling ip_output */
3436 ip_output(state
->m
, NULL
, ro4
, 0, NULL
, NULL
);
3440 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3441 "unsupported inner family, spi=%u\n",
3442 (u_int32_t
)ntohl(sav
->spi
)));
3443 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
3444 error
= EAFNOSUPPORT
;
3448 state
->ro
= &sav
->sah
->sa_route
;
3449 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
3450 dst6
= (struct sockaddr_in6
*)state
->dst
;
3451 if (state
->ro
->ro_rt
3452 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
3453 || !IN6_ARE_ADDR_EQUAL(&dst6
->sin6_addr
, &ip6
->ip6_dst
))) {
3454 rtfree(state
->ro
->ro_rt
);
3455 state
->ro
->ro_rt
= NULL
;
3457 if (state
->ro
->ro_rt
== 0) {
3458 bzero(dst6
, sizeof(*dst6
));
3459 dst6
->sin6_family
= AF_INET6
;
3460 dst6
->sin6_len
= sizeof(*dst6
);
3461 dst6
->sin6_addr
= ip6
->ip6_dst
;
3464 if (state
->ro
->ro_rt
== 0) {
3465 ip6stat
.ip6s_noroute
++;
3466 IPSEC_STAT_INCREMENT(ipsec6stat
.out_noroute
);
3467 error
= EHOSTUNREACH
;
3471 /* adjust state->dst if tunnel endpoint is offlink */
3472 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
3473 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
3474 dst6
= (struct sockaddr_in6
*)state
->dst
;
3478 state
->m
= ipsec6_splithdr(state
->m
);
3480 IPSEC_STAT_INCREMENT(ipsec6stat
.out_nomem
);
3484 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3485 switch (isr
->saidx
.proto
) {
3488 error
= esp6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, sav
);
3495 error
= ah6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, sav
);
3497 case IPPROTO_IPCOMP
:
3498 /* XXX code should be here */
3501 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3502 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
3504 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
3512 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
3513 if (plen
> IPV6_MAXPACKET
) {
3514 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3515 "IPsec with IPv6 jumbogram is not supported\n"));
3516 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
3517 error
= EINVAL
; /*XXX*/
3520 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3521 ip6
->ip6_plen
= htons(plen
);
3525 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3530 key_freesav(sav
, KEY_SADB_UNLOCKED
);
3540 * Chop IP header and option off from the payload.
3542 static struct mbuf
*
3550 if (m
->m_len
< sizeof(struct ip
))
3551 panic("ipsec4_splithdr: first mbuf too short");
3552 ip
= mtod(m
, struct ip
*);
3554 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
3556 hlen
= ip
->ip_hl
<< 2;
3558 if (m
->m_len
> hlen
) {
3559 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
3564 M_COPY_PKTHDR(mh
, m
);
3566 m
->m_flags
&= ~M_PKTHDR
;
3567 m_mchtype(m
, MT_DATA
);
3573 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), hlen
);
3574 } else if (m
->m_len
< hlen
) {
3575 m
= m_pullup(m
, hlen
);
3584 static struct mbuf
*
3589 struct ip6_hdr
*ip6
;
3592 if (m
->m_len
< sizeof(struct ip6_hdr
))
3593 panic("ipsec6_splithdr: first mbuf too short");
3594 ip6
= mtod(m
, struct ip6_hdr
*);
3595 hlen
= sizeof(struct ip6_hdr
);
3596 if (m
->m_len
> hlen
) {
3597 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
3602 M_COPY_PKTHDR(mh
, m
);
3604 m
->m_flags
&= ~M_PKTHDR
;
3605 m_mchtype(m
, MT_DATA
);
3611 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), hlen
);
3612 } else if (m
->m_len
< hlen
) {
3613 m
= m_pullup(m
, hlen
);
3621 /* validate inbound IPsec tunnel packet. */
3623 ipsec4_tunnel_validate(m
, off
, nxt0
, sav
, ifamily
)
3624 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3627 struct secasvar
*sav
;
3628 sa_family_t
*ifamily
;
3630 u_int8_t nxt
= nxt0
& 0xff;
3631 struct sockaddr_in
*sin
;
3632 struct sockaddr_in osrc
, odst
, i4src
, i4dst
;
3633 struct sockaddr_in6 i6src
, i6dst
;
3635 struct secpolicy
*sp
;
3638 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3641 if (m
->m_len
< sizeof(struct ip
))
3642 panic("too short mbuf on ipsec4_tunnel_validate");
3644 if (nxt
!= IPPROTO_IPV4
&& nxt
!= IPPROTO_IPV6
)
3646 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip
))
3648 /* do not decapsulate if the SA is for transport mode only */
3649 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3652 oip
= mtod(m
, struct ip
*);
3654 hlen
= _IP_VHL_HL(oip
->ip_vhl
) << 2;
3656 hlen
= oip
->ip_hl
<< 2;
3658 if (hlen
!= sizeof(struct ip
))
3661 sin
= (struct sockaddr_in
*)&sav
->sah
->saidx
.dst
;
3662 if (sin
->sin_family
!= AF_INET
)
3664 if (bcmp(&oip
->ip_dst
, &sin
->sin_addr
, sizeof(oip
->ip_dst
)) != 0)
3668 bzero(&osrc
, sizeof(osrc
));
3669 bzero(&odst
, sizeof(odst
));
3670 osrc
.sin_family
= odst
.sin_family
= AF_INET
;
3671 osrc
.sin_len
= odst
.sin_len
= sizeof(struct sockaddr_in
);
3672 osrc
.sin_addr
= oip
->ip_src
;
3673 odst
.sin_addr
= oip
->ip_dst
;
3675 * RFC2401 5.2.1 (b): (assume that we are using tunnel mode)
3676 * - if the inner destination is multicast address, there can be
3677 * multiple permissible inner source address. implementation
3678 * may want to skip verification of inner source address against
3680 * - if the inner protocol is ICMP, the packet may be an error report
3681 * from routers on the other side of the VPN cloud (R in the
3682 * following diagram). in this case, we cannot verify inner source
3683 * address against SPD selector.
3684 * me -- gw === gw -- R -- you
3686 * we consider the first bullet to be users responsibility on SPD entry
3687 * configuration (if you need to encrypt multicast traffic, set
3688 * the source range of SPD selector to 0.0.0.0/0, or have explicit
3689 * address ranges for possible senders).
3690 * the second bullet is not taken care of (yet).
3692 * therefore, we do not do anything special about inner source.
3694 if (nxt
== IPPROTO_IPV4
) {
3695 bzero(&i4src
, sizeof(struct sockaddr_in
));
3696 bzero(&i4dst
, sizeof(struct sockaddr_in
));
3697 i4src
.sin_family
= i4dst
.sin_family
= *ifamily
= AF_INET
;
3698 i4src
.sin_len
= i4dst
.sin_len
= sizeof(struct sockaddr_in
);
3699 m_copydata(m
, off
+ offsetof(struct ip
, ip_src
), sizeof(i4src
.sin_addr
),
3700 (caddr_t
)&i4src
.sin_addr
);
3701 m_copydata(m
, off
+ offsetof(struct ip
, ip_dst
), sizeof(i4dst
.sin_addr
),
3702 (caddr_t
)&i4dst
.sin_addr
);
3703 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3704 (struct sockaddr
*)&i4src
, (struct sockaddr
*)&i4dst
);
3705 } else if (nxt
== IPPROTO_IPV6
) {
3706 bzero(&i6src
, sizeof(struct sockaddr_in6
));
3707 bzero(&i6dst
, sizeof(struct sockaddr_in6
));
3708 i6src
.sin6_family
= i6dst
.sin6_family
= *ifamily
= AF_INET6
;
3709 i6src
.sin6_len
= i6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
3710 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_src
), sizeof(i6src
.sin6_addr
),
3711 (caddr_t
)&i6src
.sin6_addr
);
3712 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_dst
), sizeof(i6dst
.sin6_addr
),
3713 (caddr_t
)&i6dst
.sin6_addr
);
3714 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3715 (struct sockaddr
*)&i6src
, (struct sockaddr
*)&i6dst
);
3717 return 0; /* unsupported family */
3722 key_freesp(sp
, KEY_SADB_UNLOCKED
);
3728 /* validate inbound IPsec tunnel packet. */
3730 ipsec6_tunnel_validate(m
, off
, nxt0
, sav
)
3731 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3734 struct secasvar
*sav
;
3736 u_int8_t nxt
= nxt0
& 0xff;
3737 struct sockaddr_in6
*sin6
;
3738 struct sockaddr_in6 osrc
, odst
, isrc
, idst
;
3739 struct secpolicy
*sp
;
3740 struct ip6_hdr
*oip6
;
3742 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
3745 if (m
->m_len
< sizeof(struct ip6_hdr
))
3746 panic("too short mbuf on ipsec6_tunnel_validate");
3748 if (nxt
!= IPPROTO_IPV6
)
3750 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip6_hdr
))
3752 /* do not decapsulate if the SA is for transport mode only */
3753 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3756 oip6
= mtod(m
, struct ip6_hdr
*);
3757 /* AF_INET should be supported, but at this moment we don't. */
3758 sin6
= (struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
;
3759 if (sin6
->sin6_family
!= AF_INET6
)
3761 if (!IN6_ARE_ADDR_EQUAL(&oip6
->ip6_dst
, &sin6
->sin6_addr
))
3765 bzero(&osrc
, sizeof(osrc
));
3766 bzero(&odst
, sizeof(odst
));
3767 bzero(&isrc
, sizeof(isrc
));
3768 bzero(&idst
, sizeof(idst
));
3769 osrc
.sin6_family
= odst
.sin6_family
= isrc
.sin6_family
=
3770 idst
.sin6_family
= AF_INET6
;
3771 osrc
.sin6_len
= odst
.sin6_len
= isrc
.sin6_len
= idst
.sin6_len
=
3772 sizeof(struct sockaddr_in6
);
3773 osrc
.sin6_addr
= oip6
->ip6_src
;
3774 odst
.sin6_addr
= oip6
->ip6_dst
;
3775 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_src
),
3776 sizeof(isrc
.sin6_addr
), (caddr_t
)&isrc
.sin6_addr
);
3777 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_dst
),
3778 sizeof(idst
.sin6_addr
), (caddr_t
)&idst
.sin6_addr
);
3781 * regarding to inner source address validation, see a long comment
3782 * in ipsec4_tunnel_validate.
3785 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3786 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3788 * when there is no suitable inbound policy for the packet of the ipsec
3789 * tunnel mode, the kernel never decapsulate the tunneled packet
3790 * as the ipsec tunnel mode even when the system wide policy is "none".
3791 * then the kernel leaves the generic tunnel module to process this
3792 * packet. if there is no rule of the generic tunnel, the packet
3793 * is rejected and the statistics will be counted up.
3797 key_freesp(sp
, KEY_SADB_UNLOCKED
);
3804 * Make a mbuf chain for encryption.
3805 * If the original mbuf chain contains a mbuf with a cluster,
3806 * allocate a new cluster and copy the data to the new cluster.
3807 * XXX: this hack is inefficient, but is necessary to handle cases
3808 * of TCP retransmission...
3814 struct mbuf
*n
, **mpp
, *mnew
;
3816 for (n
= m
, mpp
= &m
; n
; n
= n
->m_next
) {
3817 if (n
->m_flags
& M_EXT
) {
3819 * Make a copy only if there are more than one references
3821 * XXX: is this approach effective?
3824 n
->m_ext
.ext_free
||
3825 m_mclhasreference(n
)
3831 if (n
->m_flags
& M_PKTHDR
) {
3832 MGETHDR(mnew
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
3835 mnew
->m_pkthdr
= n
->m_pkthdr
;
3836 M_COPY_PKTHDR(mnew
, n
);
3837 mnew
->m_flags
= n
->m_flags
& M_COPYFLAGS
;
3840 MGET(mnew
, M_DONTWAIT
, MT_DATA
);
3848 * Copy data. If we don't have enough space to
3849 * store the whole data, allocate a cluster
3850 * or additional mbufs.
3851 * XXX: we don't use m_copyback(), since the
3852 * function does not use clusters and thus is
3861 if (remain
<= (mm
->m_flags
& M_PKTHDR
? MHLEN
: MLEN
))
3863 else { /* allocate a cluster */
3864 MCLGET(mm
, M_DONTWAIT
);
3865 if (!(mm
->m_flags
& M_EXT
)) {
3869 len
= remain
< MCLBYTES
?
3873 bcopy(n
->m_data
+ copied
, mm
->m_data
,
3880 if (remain
<= 0) /* completed? */
3883 /* need another mbuf */
3884 MGETHDR(mn
, M_DONTWAIT
, MT_HEADER
); /* XXXMAC: tags copied next time in loop? */
3887 mn
->m_pkthdr
.rcvif
= NULL
;
3893 mm
->m_next
= m_free(n
);
3912 * Tags are allocated as mbufs for now, since our minimum size is MLEN, we
3913 * should make use of up to that much space.
3915 #define IPSEC_TAG_HEADER \
3918 struct socket
*socket
;
3919 u_int32_t history_count
;
3920 struct ipsec_history history
[];
3923 #define IPSEC_TAG_SIZE (MLEN - sizeof(struct m_tag))
3924 #define IPSEC_TAG_HDR_SIZE (offsetof(struct ipsec_tag, history[0]))
3925 #define IPSEC_HISTORY_MAX ((IPSEC_TAG_SIZE - IPSEC_TAG_HDR_SIZE) / \
3926 sizeof(struct ipsec_history))
3928 static struct ipsec_tag
*
3934 /* Check if the tag already exists */
3935 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPSEC
, NULL
);
3938 struct ipsec_tag
*itag
;
3940 /* Allocate a tag */
3941 tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPSEC
,
3942 IPSEC_TAG_SIZE
, M_DONTWAIT
);
3945 itag
= (struct ipsec_tag
*)(tag
+ 1);
3947 itag
->history_count
= 0;
3949 m_tag_prepend(m
, tag
);
3953 return tag
? (struct ipsec_tag
*)(tag
+ 1) : NULL
;
3956 static struct ipsec_tag
*
3962 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPSEC
, NULL
);
3964 return tag
? (struct ipsec_tag
*)(tag
+ 1) : NULL
;
3973 tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPSEC
, NULL
);
3976 m_tag_delete(m
, tag
);
3980 /* if the aux buffer is unnecessary, nuke it. */
3984 struct ipsec_tag
*itag
)
3986 if (itag
&& itag
->socket
== NULL
&& itag
->history_count
== 0) {
3987 m_tag_delete(m
, ((struct m_tag
*)itag
) - 1);
3996 struct ipsec_tag
*tag
;
3998 /* if so == NULL, don't insist on getting the aux mbuf */
4000 tag
= ipsec_addaux(m
);
4004 tag
= ipsec_findaux(m
);
4007 ipsec_optaux(m
, tag
);
4016 struct ipsec_tag
*itag
;
4018 itag
= ipsec_findaux(m
);
4020 return itag
->socket
;
4031 struct ipsec_tag
*itag
;
4032 struct ipsec_history
*p
;
4033 itag
= ipsec_addaux(m
);
4036 if (itag
->history_count
== IPSEC_HISTORY_MAX
)
4037 return ENOSPC
; /* XXX */
4039 p
= &itag
->history
[itag
->history_count
];
4040 itag
->history_count
++;
4042 bzero(p
, sizeof(*p
));
4043 p
->ih_proto
= proto
;
4049 struct ipsec_history
*
4054 struct ipsec_tag
*itag
;
4056 itag
= ipsec_findaux(m
);
4059 if (itag
->history_count
== 0)
4062 *lenp
= (int)(itag
->history_count
* sizeof(struct ipsec_history
));
4063 return itag
->history
;
4070 struct ipsec_tag
*itag
;
4072 itag
= ipsec_findaux(m
);
4074 itag
->history_count
= 0;
4076 ipsec_optaux(m
, itag
);
4079 __private_extern__
void
4080 ipsec_send_natt_keepalive(
4081 struct secasvar
*sav
)
4088 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
4090 if ((esp_udp_encap_port
& 0xFFFF) == 0 || sav
->remote_ike_port
== 0) return;
4092 m
= m_gethdr(M_NOWAIT
, MT_DATA
);
4093 if (m
== NULL
) return;
4096 * Create a UDP packet complete with IP header.
4097 * We must do this because UDP output requires
4098 * an inpcb which we don't have. UDP packet
4099 * contains one byte payload. The byte is set
4102 ip
= (struct ip
*)m_mtod(m
);
4103 uh
= (struct udphdr
*)((char*)m_mtod(m
) + sizeof(struct ip
));
4104 m
->m_len
= sizeof(struct udpiphdr
) + 1;
4105 bzero(m_mtod(m
), m
->m_len
);
4106 m
->m_pkthdr
.len
= m
->m_len
;
4108 ip
->ip_len
= m
->m_len
;
4109 ip
->ip_ttl
= ip_defttl
;
4110 ip
->ip_p
= IPPROTO_UDP
;
4111 ip
->ip_src
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
;
4112 ip
->ip_dst
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
;
4113 uh
->uh_sport
= htons((u_short
)esp_udp_encap_port
);
4114 uh
->uh_dport
= htons(sav
->remote_ike_port
);
4115 uh
->uh_ulen
= htons(1 + sizeof(struct udphdr
));
4117 *(u_int8_t
*)((char*)m_mtod(m
) + sizeof(struct ip
) + sizeof(struct udphdr
)) = 0xFF;
4119 error
= ip_output(m
, NULL
, &sav
->sah
->sa_route
, IP_NOIPSEC
, NULL
, NULL
);
4121 sav
->natt_last_activity
= natt_now
;