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>
52 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/in_var.h>
59 #include <netinet/udp.h>
60 #include <netinet/udp_var.h>
61 #include <netinet/ip_ecn.h>
63 #include <netinet6/ip6_ecn.h>
65 #include <netinet/tcp.h>
66 #include <netinet/udp.h>
68 #include <netinet/ip6.h>
70 #include <netinet6/ip6_var.h>
72 #include <netinet/in_pcb.h>
74 #include <netinet/icmp6.h>
77 #include <netinet6/ipsec.h>
79 #include <netinet6/ipsec6.h>
81 #include <netinet6/ah.h>
83 #include <netinet6/ah6.h>
86 #include <netinet6/esp.h>
88 #include <netinet6/esp6.h>
91 #include <netinet6/ipcomp.h>
93 #include <netinet6/ipcomp6.h>
95 #include <netkey/key.h>
96 #include <netkey/keydb.h>
97 #include <netkey/key_debug.h>
99 #include <net/net_osdep.h>
107 #include <sys/kdebug.h>
108 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
109 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
110 #define DBG_FNC_GETPOL_SOCK NETDBG_CODE(DBG_NETIPSEC, (1 << 8))
111 #define DBG_FNC_GETPOL_ADDR NETDBG_CODE(DBG_NETIPSEC, (2 << 8))
112 #define DBG_FNC_IPSEC_OUT NETDBG_CODE(DBG_NETIPSEC, (3 << 8))
115 struct ipsecstat ipsecstat
;
116 int ip4_ah_cleartos
= 1;
117 int ip4_ah_offsetmask
= 0; /* maybe IP_DF? */
118 int ip4_ipsec_dfbit
= 0; /* DF bit on encap. 0: clear 1: set 2: copy */
119 int ip4_esp_trans_deflev
= IPSEC_LEVEL_USE
;
120 int ip4_esp_net_deflev
= IPSEC_LEVEL_USE
;
121 int ip4_ah_trans_deflev
= IPSEC_LEVEL_USE
;
122 int ip4_ah_net_deflev
= IPSEC_LEVEL_USE
;
123 struct secpolicy ip4_def_policy
;
124 int ip4_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
125 int ip4_esp_randpad
= -1;
126 int esp_udp_encap_port
= 0;
127 static int sysctl_def_policy SYSCTL_HANDLER_ARGS
;
128 extern u_int32_t natt_now
;
130 SYSCTL_DECL(_net_inet_ipsec
);
132 SYSCTL_DECL(_net_inet6_ipsec6
);
135 SYSCTL_STRUCT(_net_inet_ipsec
, IPSECCTL_STATS
,
136 stats
, CTLFLAG_RD
, &ipsecstat
, ipsecstat
, "");
137 SYSCTL_PROC(_net_inet_ipsec
, IPSECCTL_DEF_POLICY
, def_policy
, CTLTYPE_INT
|CTLFLAG_RW
,
138 &ip4_def_policy
.policy
, 0, &sysctl_def_policy
, "I", "");
139 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
140 CTLFLAG_RW
, &ip4_esp_trans_deflev
, 0, "");
141 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
142 CTLFLAG_RW
, &ip4_esp_net_deflev
, 0, "");
143 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
144 CTLFLAG_RW
, &ip4_ah_trans_deflev
, 0, "");
145 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
146 CTLFLAG_RW
, &ip4_ah_net_deflev
, 0, "");
147 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_CLEARTOS
,
148 ah_cleartos
, CTLFLAG_RW
, &ip4_ah_cleartos
, 0, "");
149 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_OFFSETMASK
,
150 ah_offsetmask
, CTLFLAG_RW
, &ip4_ah_offsetmask
, 0, "");
151 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DFBIT
,
152 dfbit
, CTLFLAG_RW
, &ip4_ipsec_dfbit
, 0, "");
153 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ECN
,
154 ecn
, CTLFLAG_RW
, &ip4_ipsec_ecn
, 0, "");
155 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEBUG
,
156 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
157 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ESP_RANDPAD
,
158 esp_randpad
, CTLFLAG_RW
, &ip4_esp_randpad
, 0, "");
160 /* for performance, we bypass ipsec until a security policy is set */
161 int ipsec_bypass
= 1;
162 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, bypass
, CTLFLAG_RD
, &ipsec_bypass
,0, "");
165 * NAT Traversal requires a UDP port for encapsulation,
166 * esp_udp_encap_port controls which port is used. Racoon
167 * must set this port to the port racoon is using locally
170 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, esp_port
,
171 CTLFLAG_RW
, &esp_udp_encap_port
, 0, "");
174 struct ipsecstat ipsec6stat
;
175 int ip6_esp_trans_deflev
= IPSEC_LEVEL_USE
;
176 int ip6_esp_net_deflev
= IPSEC_LEVEL_USE
;
177 int ip6_ah_trans_deflev
= IPSEC_LEVEL_USE
;
178 int ip6_ah_net_deflev
= IPSEC_LEVEL_USE
;
179 struct secpolicy ip6_def_policy
;
180 int ip6_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
181 int ip6_esp_randpad
= -1;
183 /* net.inet6.ipsec6 */
184 SYSCTL_STRUCT(_net_inet6_ipsec6
, IPSECCTL_STATS
,
185 stats
, CTLFLAG_RD
, &ipsec6stat
, ipsecstat
, "");
186 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_POLICY
,
187 def_policy
, CTLFLAG_RW
, &ip6_def_policy
.policy
, 0, "");
188 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
189 CTLFLAG_RW
, &ip6_esp_trans_deflev
, 0, "");
190 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
191 CTLFLAG_RW
, &ip6_esp_net_deflev
, 0, "");
192 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
193 CTLFLAG_RW
, &ip6_ah_trans_deflev
, 0, "");
194 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
195 CTLFLAG_RW
, &ip6_ah_net_deflev
, 0, "");
196 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ECN
,
197 ecn
, CTLFLAG_RW
, &ip6_ipsec_ecn
, 0, "");
198 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEBUG
,
199 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
200 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ESP_RANDPAD
,
201 esp_randpad
, CTLFLAG_RW
, &ip6_esp_randpad
, 0, "");
204 static int ipsec_setspidx_mbuf
205 __P((struct secpolicyindex
*, u_int
, u_int
, struct mbuf
*, int));
206 static int ipsec4_setspidx_inpcb
__P((struct mbuf
*, struct inpcb
*pcb
));
208 static int ipsec6_setspidx_in6pcb
__P((struct mbuf
*, struct in6pcb
*pcb
));
210 static int ipsec_setspidx
__P((struct mbuf
*, struct secpolicyindex
*, int));
211 static void ipsec4_get_ulp
__P((struct mbuf
*m
, struct secpolicyindex
*, int));
212 static int ipsec4_setspidx_ipaddr
__P((struct mbuf
*, struct secpolicyindex
*));
214 static void ipsec6_get_ulp
__P((struct mbuf
*m
, struct secpolicyindex
*, int));
215 static int ipsec6_setspidx_ipaddr
__P((struct mbuf
*, struct secpolicyindex
*));
217 static struct inpcbpolicy
*ipsec_newpcbpolicy
__P((void));
218 static void ipsec_delpcbpolicy
__P((struct inpcbpolicy
*));
219 static struct secpolicy
*ipsec_deepcopy_policy
__P((struct secpolicy
*src
));
220 static int ipsec_set_policy
__P((struct secpolicy
**pcb_sp
,
221 int optname
, caddr_t request
, size_t len
, int priv
));
222 static int ipsec_get_policy
__P((struct secpolicy
*pcb_sp
, struct mbuf
**mp
));
223 static void vshiftl
__P((unsigned char *, int, int));
224 static int ipsec_in_reject
__P((struct secpolicy
*, struct mbuf
*));
225 static size_t ipsec_hdrsiz
__P((struct secpolicy
*));
227 static struct mbuf
*ipsec4_splithdr
__P((struct mbuf
*));
230 static struct mbuf
*ipsec6_splithdr
__P((struct mbuf
*));
233 static int ipsec4_encapsulate
__P((struct mbuf
*, struct secasvar
*));
236 static int ipsec6_encapsulate
__P((struct mbuf
*, struct secasvar
*));
238 static struct mbuf
*ipsec_addaux
__P((struct mbuf
*));
239 static struct mbuf
*ipsec_findaux
__P((struct mbuf
*));
240 static void ipsec_optaux
__P((struct mbuf
*, struct mbuf
*));
241 void ipsec_send_natt_keepalive(struct secasvar
*sav
);
244 sysctl_def_policy SYSCTL_HANDLER_ARGS
246 int old_policy
= ip4_def_policy
.policy
;
247 int error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
249 if (ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
&&
250 ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
) {
251 ip4_def_policy
.policy
= old_policy
;
255 /* Turn off the bypass if the default security policy changes */
256 if (ipsec_bypass
!= 0 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
)
263 * For OUTBOUND packet having a socket. Searching SPD for packet,
264 * and return a pointer to SP.
265 * OUT: NULL: no apropreate SP found, the following value is set to error.
267 * EACCES : discard packet.
268 * ENOENT : ipsec_acquire() in progress, maybe.
269 * others : error occured.
270 * others: a pointer to SP
272 * NOTE: IPv6 mapped adddress concern is implemented here.
275 ipsec4_getpolicybysock(m
, dir
, so
, error
)
281 struct inpcbpolicy
*pcbsp
= NULL
;
282 struct secpolicy
*currsp
= NULL
; /* policy on socket */
283 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
286 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
287 panic("ipsec4_getpolicybysock: NULL pointer was passed.\n");
289 if (so
->so_pcb
== NULL
) {
290 /* Socket may be closing or without PCB */
291 return ipsec4_getpolicybyaddr(m
, dir
, 0, error
);
294 switch (so
->so_proto
->pr_domain
->dom_family
) {
296 pcbsp
= sotoinpcb(so
)->inp_sp
;
300 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
306 /* Socket has not specified an IPSEC policy */
307 return ipsec4_getpolicybyaddr(m
, dir
, 0, error
);
310 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_START
, 0,0,0,0,0);
312 switch (so
->so_proto
->pr_domain
->dom_family
) {
314 /* set spidx in pcb */
315 *error
= ipsec4_setspidx_inpcb(m
, sotoinpcb(so
));
319 /* set spidx in pcb */
320 *error
= ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
324 panic("ipsec4_getpolicybysock: unsupported address family\n");
327 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 1,*error
,0,0,0);
333 panic("ipsec4_getpolicybysock: pcbsp is NULL.\n");
336 case IPSEC_DIR_INBOUND
:
337 currsp
= pcbsp
->sp_in
;
339 case IPSEC_DIR_OUTBOUND
:
340 currsp
= pcbsp
->sp_out
;
343 panic("ipsec4_getpolicybysock: illegal direction.\n");
348 panic("ipsec4_getpolicybysock: currsp is NULL.\n");
350 /* when privilieged socket */
352 switch (currsp
->policy
) {
353 case IPSEC_POLICY_BYPASS
:
356 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 2,*error
,0,0,0);
359 case IPSEC_POLICY_ENTRUST
:
360 /* look for a policy in SPD */
361 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
364 if (kernsp
!= NULL
) {
365 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
366 printf("DP ipsec4_getpolicybysock called "
367 "to allocate SP:%p\n", kernsp
));
369 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 3,*error
,0,0,0);
374 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
375 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
377 "fixed system default policy: %d->%d\n",
378 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
379 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
381 ip4_def_policy
.refcnt
++;
383 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 4,*error
,0,0,0);
384 return &ip4_def_policy
;
386 case IPSEC_POLICY_IPSEC
:
389 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 5,*error
,0,0,0);
393 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
394 "Invalid policy for PCB %d\n", currsp
->policy
));
396 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 6,*error
,0,0,0);
402 /* when non-privilieged socket */
403 /* look for a policy in SPD */
404 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
407 if (kernsp
!= NULL
) {
408 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
409 printf("DP ipsec4_getpolicybysock called "
410 "to allocate SP:%p\n", kernsp
));
412 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 7,*error
,0,0,0);
417 switch (currsp
->policy
) {
418 case IPSEC_POLICY_BYPASS
:
419 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
420 "Illegal policy for non-priviliged defined %d\n",
423 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 8,*error
,0,0,0);
426 case IPSEC_POLICY_ENTRUST
:
427 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
428 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
430 "fixed system default policy: %d->%d\n",
431 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
432 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
434 ip4_def_policy
.refcnt
++;
436 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 9,*error
,0,0,0);
437 return &ip4_def_policy
;
439 case IPSEC_POLICY_IPSEC
:
442 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 10,*error
,0,0,0);
446 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
447 "Invalid policy for PCB %d\n", currsp
->policy
));
449 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 11,*error
,0,0,0);
456 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
457 * and return a pointer to SP.
458 * OUT: positive: a pointer to the entry for security policy leaf matched.
459 * NULL: no apropreate SP found, the following value is set to error.
461 * EACCES : discard packet.
462 * ENOENT : ipsec_acquire() in progress, maybe.
463 * others : error occured.
466 ipsec4_getpolicybyaddr(m
, dir
, flag
, error
)
472 struct secpolicy
*sp
= NULL
;
474 if (ipsec_bypass
!= 0)
478 if (m
== NULL
|| error
== NULL
)
479 panic("ipsec4_getpolicybyaddr: NULL pointer was passed.\n");
482 struct secpolicyindex spidx
;
484 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_START
, 0,0,0,0,0);
485 bzero(&spidx
, sizeof(spidx
));
487 /* make a index to look for a policy */
488 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET
, m
,
489 (flag
& IP_FORWARDING
) ? 0 : 1);
492 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 1,*error
,0,0,0);
496 sp
= key_allocsp(&spidx
, dir
);
501 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
502 printf("DP ipsec4_getpolicybyaddr called "
503 "to allocate SP:%p\n", sp
));
505 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 2,*error
,0,0,0);
510 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
511 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
512 ipseclog((LOG_INFO
, "fixed system default policy:%d->%d\n",
513 ip4_def_policy
.policy
,
515 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
517 ip4_def_policy
.refcnt
++;
519 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 3,*error
,0,0,0);
520 return &ip4_def_policy
;
525 * For OUTBOUND packet having a socket. Searching SPD for packet,
526 * and return a pointer to SP.
527 * OUT: NULL: no apropreate SP found, the following value is set to error.
529 * EACCES : discard packet.
530 * ENOENT : ipsec_acquire() in progress, maybe.
531 * others : error occured.
532 * others: a pointer to SP
535 ipsec6_getpolicybysock(m
, dir
, so
, error
)
541 struct inpcbpolicy
*pcbsp
= NULL
;
542 struct secpolicy
*currsp
= NULL
; /* policy on socket */
543 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
546 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
547 panic("ipsec6_getpolicybysock: NULL pointer was passed.\n");
550 if (so
->so_proto
->pr_domain
->dom_family
!= AF_INET6
)
551 panic("ipsec6_getpolicybysock: socket domain != inet6\n");
554 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
557 return ipsec6_getpolicybyaddr(m
, dir
, 0, error
);
560 /* set spidx in pcb */
561 ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
565 panic("ipsec6_getpolicybysock: pcbsp is NULL.\n");
568 case IPSEC_DIR_INBOUND
:
569 currsp
= pcbsp
->sp_in
;
571 case IPSEC_DIR_OUTBOUND
:
572 currsp
= pcbsp
->sp_out
;
575 panic("ipsec6_getpolicybysock: illegal direction.\n");
580 panic("ipsec6_getpolicybysock: currsp is NULL.\n");
582 /* when privilieged socket */
584 switch (currsp
->policy
) {
585 case IPSEC_POLICY_BYPASS
:
590 case IPSEC_POLICY_ENTRUST
:
591 /* look for a policy in SPD */
592 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
595 if (kernsp
!= NULL
) {
596 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
597 printf("DP ipsec6_getpolicybysock called "
598 "to allocate SP:%p\n", kernsp
));
604 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
605 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
607 "fixed system default policy: %d->%d\n",
608 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
609 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
611 ip6_def_policy
.refcnt
++;
613 return &ip6_def_policy
;
615 case IPSEC_POLICY_IPSEC
:
621 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
622 "Invalid policy for PCB %d\n", currsp
->policy
));
629 /* when non-privilieged socket */
630 /* look for a policy in SPD */
631 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
634 if (kernsp
!= NULL
) {
635 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
636 printf("DP ipsec6_getpolicybysock called "
637 "to allocate SP:%p\n", kernsp
));
643 switch (currsp
->policy
) {
644 case IPSEC_POLICY_BYPASS
:
645 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
646 "Illegal policy for non-priviliged defined %d\n",
651 case IPSEC_POLICY_ENTRUST
:
652 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
653 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
655 "fixed system default policy: %d->%d\n",
656 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
657 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
659 ip6_def_policy
.refcnt
++;
661 return &ip6_def_policy
;
663 case IPSEC_POLICY_IPSEC
:
670 "ipsec6_policybysock: Invalid policy for PCB %d\n",
679 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
680 * and return a pointer to SP.
681 * `flag' means that packet is to be forwarded whether or not.
683 * OUT: positive: a pointer to the entry for security policy leaf matched.
684 * NULL: no apropreate SP found, the following value is set to error.
686 * EACCES : discard packet.
687 * ENOENT : ipsec_acquire() in progress, maybe.
688 * others : error occured.
690 #ifndef IP_FORWARDING
691 #define IP_FORWARDING 1
695 ipsec6_getpolicybyaddr(m
, dir
, flag
, error
)
701 struct secpolicy
*sp
= NULL
;
704 if (m
== NULL
|| error
== NULL
)
705 panic("ipsec6_getpolicybyaddr: NULL pointer was passed.\n");
708 struct secpolicyindex spidx
;
710 bzero(&spidx
, sizeof(spidx
));
712 /* make a index to look for a policy */
713 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET6
, m
,
714 (flag
& IP_FORWARDING
) ? 0 : 1);
719 sp
= key_allocsp(&spidx
, dir
);
724 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
725 printf("DP ipsec6_getpolicybyaddr called "
726 "to allocate SP:%p\n", sp
));
732 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
733 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
734 ipseclog((LOG_INFO
, "fixed system default policy: %d->%d\n",
735 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
736 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
738 ip6_def_policy
.refcnt
++;
740 return &ip6_def_policy
;
745 * set IP address into spidx from mbuf.
746 * When Forwarding packet and ICMP echo reply, this function is used.
748 * IN: get the followings from mbuf.
749 * protocol family, src, dst, next protocol
752 * other: failure, and set errno.
755 ipsec_setspidx_mbuf(spidx
, dir
, family
, m
, needport
)
756 struct secpolicyindex
*spidx
;
764 if (spidx
== NULL
|| m
== NULL
)
765 panic("ipsec_setspidx_mbuf: NULL pointer was passed.\n");
767 bzero(spidx
, sizeof(*spidx
));
769 error
= ipsec_setspidx(m
, spidx
, needport
);
778 bzero(spidx
, sizeof(*spidx
));
783 ipsec4_setspidx_inpcb(m
, pcb
)
787 struct secpolicyindex
*spidx
;
790 if (ipsec_bypass
!= 0)
795 panic("ipsec4_setspidx_inpcb: no PCB found.\n");
796 if (pcb
->inp_sp
== NULL
)
797 panic("ipsec4_setspidx_inpcb: no inp_sp found.\n");
798 if (pcb
->inp_sp
->sp_out
== NULL
|| pcb
->inp_sp
->sp_in
== NULL
)
799 panic("ipsec4_setspidx_inpcb: no sp_in/out found.\n");
801 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
802 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
804 spidx
= &pcb
->inp_sp
->sp_in
->spidx
;
805 error
= ipsec_setspidx(m
, spidx
, 1);
808 spidx
->dir
= IPSEC_DIR_INBOUND
;
810 spidx
= &pcb
->inp_sp
->sp_out
->spidx
;
811 error
= ipsec_setspidx(m
, spidx
, 1);
814 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
819 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
820 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
826 ipsec6_setspidx_in6pcb(m
, pcb
)
830 struct secpolicyindex
*spidx
;
835 panic("ipsec6_setspidx_in6pcb: no PCB found.\n");
836 if (pcb
->in6p_sp
== NULL
)
837 panic("ipsec6_setspidx_in6pcb: no in6p_sp found.\n");
838 if (pcb
->in6p_sp
->sp_out
== NULL
|| pcb
->in6p_sp
->sp_in
== NULL
)
839 panic("ipsec6_setspidx_in6pcb: no sp_in/out found.\n");
841 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
842 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
844 spidx
= &pcb
->in6p_sp
->sp_in
->spidx
;
845 error
= ipsec_setspidx(m
, spidx
, 1);
848 spidx
->dir
= IPSEC_DIR_INBOUND
;
850 spidx
= &pcb
->in6p_sp
->sp_out
->spidx
;
851 error
= ipsec_setspidx(m
, spidx
, 1);
854 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
859 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
860 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
866 * configure security policy index (src/dst/proto/sport/dport)
867 * by looking at the content of mbuf.
868 * the caller is responsible for error recovery (like clearing up spidx).
871 ipsec_setspidx(m
, spidx
, needport
)
873 struct secpolicyindex
*spidx
;
876 struct ip
*ip
= NULL
;
884 panic("ipsec_setspidx: m == 0 passed.\n");
887 * validate m->m_pkthdr.len. we see incorrect length if we
888 * mistakenly call this function with inconsistent mbuf chain
889 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
892 for (n
= m
; n
; n
= n
->m_next
)
894 if (m
->m_pkthdr
.len
!= len
) {
895 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
896 printf("ipsec_setspidx: "
897 "total of m_len(%d) != pkthdr.len(%d), "
899 len
, m
->m_pkthdr
.len
));
903 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
904 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
905 printf("ipsec_setspidx: "
906 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
911 if (m
->m_len
>= sizeof(*ip
))
912 ip
= mtod(m
, struct ip
*);
914 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
918 v
= _IP_VHL_V(ip
->ip_vhl
);
924 error
= ipsec4_setspidx_ipaddr(m
, spidx
);
927 ipsec4_get_ulp(m
, spidx
, needport
);
931 if (m
->m_pkthdr
.len
< sizeof(struct ip6_hdr
)) {
932 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
933 printf("ipsec_setspidx: "
934 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
935 "ignored.\n", m
->m_pkthdr
.len
));
938 error
= ipsec6_setspidx_ipaddr(m
, spidx
);
941 ipsec6_get_ulp(m
, spidx
, needport
);
945 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
946 printf("ipsec_setspidx: "
947 "unknown IP version %u, ignored.\n", v
));
953 ipsec4_get_ulp(m
, spidx
, needport
)
955 struct secpolicyindex
*spidx
;
967 panic("ipsec4_get_ulp: NULL pointer was passed.\n");
968 if (m
->m_pkthdr
.len
< sizeof(ip
))
969 panic("ipsec4_get_ulp: too short\n");
972 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
973 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
= IPSEC_PORT_ANY
;
974 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
= IPSEC_PORT_ANY
;
976 m_copydata(m
, 0, sizeof(ip
), (caddr_t
)&ip
);
977 /* ip_input() flips it into host endian XXX need more checking */
978 if (ip
.ip_off
& (IP_MF
| IP_OFFMASK
))
983 off
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
987 while (off
< m
->m_pkthdr
.len
) {
990 spidx
->ul_proto
= nxt
;
993 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
995 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
996 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
998 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
1002 spidx
->ul_proto
= nxt
;
1005 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1007 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1008 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
1010 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
1014 if (m
->m_pkthdr
.len
> off
+ sizeof(ip6e
))
1016 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1017 off
+= (ip6e
.ip6e_len
+ 2) << 2;
1018 nxt
= ip6e
.ip6e_nxt
;
1022 /* XXX intermediate headers??? */
1023 spidx
->ul_proto
= nxt
;
1029 /* assumes that m is sane */
1031 ipsec4_setspidx_ipaddr(m
, spidx
)
1033 struct secpolicyindex
*spidx
;
1035 struct ip
*ip
= NULL
;
1037 struct sockaddr_in
*sin
;
1039 if (m
->m_len
>= sizeof(*ip
))
1040 ip
= mtod(m
, struct ip
*);
1042 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
1046 sin
= (struct sockaddr_in
*)&spidx
->src
;
1047 bzero(sin
, sizeof(*sin
));
1048 sin
->sin_family
= AF_INET
;
1049 sin
->sin_len
= sizeof(struct sockaddr_in
);
1050 bcopy(&ip
->ip_src
, &sin
->sin_addr
, sizeof(ip
->ip_src
));
1051 spidx
->prefs
= sizeof(struct in_addr
) << 3;
1053 sin
= (struct sockaddr_in
*)&spidx
->dst
;
1054 bzero(sin
, sizeof(*sin
));
1055 sin
->sin_family
= AF_INET
;
1056 sin
->sin_len
= sizeof(struct sockaddr_in
);
1057 bcopy(&ip
->ip_dst
, &sin
->sin_addr
, sizeof(ip
->ip_dst
));
1058 spidx
->prefd
= sizeof(struct in_addr
) << 3;
1064 ipsec6_get_ulp(m
, spidx
, needport
)
1066 struct secpolicyindex
*spidx
;
1075 panic("ipsec6_get_ulp: NULL pointer was passed.\n");
1077 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1078 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m
));
1081 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
1082 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= IPSEC_PORT_ANY
;
1083 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= IPSEC_PORT_ANY
;
1086 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
1087 if (off
< 0 || m
->m_pkthdr
.len
< off
)
1092 spidx
->ul_proto
= nxt
;
1095 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
1097 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
1098 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= th
.th_sport
;
1099 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= th
.th_dport
;
1102 spidx
->ul_proto
= nxt
;
1105 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1107 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1108 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= uh
.uh_sport
;
1109 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= uh
.uh_dport
;
1111 case IPPROTO_ICMPV6
:
1113 /* XXX intermediate headers??? */
1114 spidx
->ul_proto
= nxt
;
1119 /* assumes that m is sane */
1121 ipsec6_setspidx_ipaddr(m
, spidx
)
1123 struct secpolicyindex
*spidx
;
1125 struct ip6_hdr
*ip6
= NULL
;
1126 struct ip6_hdr ip6buf
;
1127 struct sockaddr_in6
*sin6
;
1129 if (m
->m_len
>= sizeof(*ip6
))
1130 ip6
= mtod(m
, struct ip6_hdr
*);
1132 m_copydata(m
, 0, sizeof(ip6buf
), (caddr_t
)&ip6buf
);
1136 sin6
= (struct sockaddr_in6
*)&spidx
->src
;
1137 bzero(sin6
, sizeof(*sin6
));
1138 sin6
->sin6_family
= AF_INET6
;
1139 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1140 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_src
));
1141 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1142 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1143 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
1145 spidx
->prefs
= sizeof(struct in6_addr
) << 3;
1147 sin6
= (struct sockaddr_in6
*)&spidx
->dst
;
1148 bzero(sin6
, sizeof(*sin6
));
1149 sin6
->sin6_family
= AF_INET6
;
1150 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1151 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_dst
));
1152 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
1153 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1154 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
1156 spidx
->prefd
= sizeof(struct in6_addr
) << 3;
1162 static struct inpcbpolicy
*
1163 ipsec_newpcbpolicy()
1165 struct inpcbpolicy
*p
;
1167 p
= (struct inpcbpolicy
*)_MALLOC(sizeof(*p
), M_SECA
, M_WAITOK
);
1172 ipsec_delpcbpolicy(p
)
1173 struct inpcbpolicy
*p
;
1178 /* initialize policy in PCB */
1180 ipsec_init_policy(so
, pcb_sp
)
1182 struct inpcbpolicy
**pcb_sp
;
1184 struct inpcbpolicy
*new;
1187 if (so
== NULL
|| pcb_sp
== NULL
)
1188 panic("ipsec_init_policy: NULL pointer was passed.\n");
1190 new = ipsec_newpcbpolicy();
1192 ipseclog((LOG_DEBUG
, "ipsec_init_policy: No more memory.\n"));
1195 bzero(new, sizeof(*new));
1198 if (so
->so_uid
== 0)
1200 if (so
->so_cred
!= 0 && so
->so_cred
->pc_ucred
->cr_uid
== 0)
1206 if ((new->sp_in
= key_newsp()) == NULL
) {
1207 ipsec_delpcbpolicy(new);
1210 new->sp_in
->state
= IPSEC_SPSTATE_ALIVE
;
1211 new->sp_in
->policy
= IPSEC_POLICY_ENTRUST
;
1213 if ((new->sp_out
= key_newsp()) == NULL
) {
1214 key_freesp(new->sp_in
);
1215 ipsec_delpcbpolicy(new);
1218 new->sp_out
->state
= IPSEC_SPSTATE_ALIVE
;
1219 new->sp_out
->policy
= IPSEC_POLICY_ENTRUST
;
1226 /* copy old ipsec policy into new */
1228 ipsec_copy_policy(old
, new)
1229 struct inpcbpolicy
*old
, *new;
1231 struct secpolicy
*sp
;
1233 if (ipsec_bypass
!= 0)
1236 sp
= ipsec_deepcopy_policy(old
->sp_in
);
1238 key_freesp(new->sp_in
);
1243 sp
= ipsec_deepcopy_policy(old
->sp_out
);
1245 key_freesp(new->sp_out
);
1250 new->priv
= old
->priv
;
1255 /* deep-copy a policy in PCB */
1256 static struct secpolicy
*
1257 ipsec_deepcopy_policy(src
)
1258 struct secpolicy
*src
;
1260 struct ipsecrequest
*newchain
= NULL
;
1261 struct ipsecrequest
*p
;
1262 struct ipsecrequest
**q
;
1263 struct ipsecrequest
*r
;
1264 struct secpolicy
*dst
;
1267 if (src
== NULL
|| dst
== NULL
)
1271 * deep-copy IPsec request chain. This is required since struct
1272 * ipsecrequest is not reference counted.
1275 for (p
= src
->req
; p
; p
= p
->next
) {
1276 *q
= (struct ipsecrequest
*)_MALLOC(sizeof(struct ipsecrequest
),
1280 bzero(*q
, sizeof(**q
));
1283 (*q
)->saidx
.proto
= p
->saidx
.proto
;
1284 (*q
)->saidx
.mode
= p
->saidx
.mode
;
1285 (*q
)->level
= p
->level
;
1286 (*q
)->saidx
.reqid
= p
->saidx
.reqid
;
1288 bcopy(&p
->saidx
.src
, &(*q
)->saidx
.src
, sizeof((*q
)->saidx
.src
));
1289 bcopy(&p
->saidx
.dst
, &(*q
)->saidx
.dst
, sizeof((*q
)->saidx
.dst
));
1297 dst
->req
= newchain
;
1298 dst
->state
= src
->state
;
1299 dst
->policy
= src
->policy
;
1300 /* do not touch the refcnt fields */
1305 for (p
= newchain
; p
; p
= r
) {
1313 /* set policy and ipsec request if present. */
1315 ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
)
1316 struct secpolicy
**pcb_sp
;
1322 struct sadb_x_policy
*xpl
;
1323 struct secpolicy
*newsp
= NULL
;
1327 if (pcb_sp
== NULL
|| *pcb_sp
== NULL
|| request
== NULL
)
1329 if (len
< sizeof(*xpl
))
1331 xpl
= (struct sadb_x_policy
*)request
;
1333 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1334 printf("ipsec_set_policy: passed policy\n");
1335 kdebug_sadb_x_policy((struct sadb_ext
*)xpl
));
1337 /* check policy type */
1338 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1339 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
1340 || xpl
->sadb_x_policy_type
== IPSEC_POLICY_NONE
)
1343 /* check privileged socket */
1344 if (priv
== 0 && xpl
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
)
1347 /* allocation new SP entry */
1348 if ((newsp
= key_msg2sp(xpl
, len
, &error
)) == NULL
)
1351 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1353 /* clear old SP and set new SP */
1354 key_freesp(*pcb_sp
);
1356 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1357 printf("ipsec_set_policy: new policy\n");
1358 kdebug_secpolicy(newsp
));
1364 ipsec_get_policy(pcb_sp
, mp
)
1365 struct secpolicy
*pcb_sp
;
1370 if (pcb_sp
== NULL
|| mp
== NULL
)
1373 *mp
= key_sp2msg(pcb_sp
);
1375 ipseclog((LOG_DEBUG
, "ipsec_get_policy: No more memory.\n"));
1379 (*mp
)->m_type
= MT_DATA
;
1380 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1381 printf("ipsec_get_policy:\n");
1388 ipsec4_set_policy(inp
, optname
, request
, len
, priv
)
1395 struct sadb_x_policy
*xpl
;
1396 struct secpolicy
**pcb_sp
;
1400 if (inp
== NULL
|| request
== NULL
)
1402 if (len
< sizeof(*xpl
))
1404 xpl
= (struct sadb_x_policy
*)request
;
1406 if (inp
->inp_sp
== NULL
) {
1407 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1412 /* select direction */
1413 switch (xpl
->sadb_x_policy_dir
) {
1414 case IPSEC_DIR_INBOUND
:
1415 pcb_sp
= &inp
->inp_sp
->sp_in
;
1417 case IPSEC_DIR_OUTBOUND
:
1418 pcb_sp
= &inp
->inp_sp
->sp_out
;
1421 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1422 xpl
->sadb_x_policy_dir
));
1426 /* turn bypass off */
1427 if (ipsec_bypass
!= 0)
1430 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1434 ipsec4_get_policy(inp
, request
, len
, mp
)
1440 struct sadb_x_policy
*xpl
;
1441 struct secpolicy
*pcb_sp
;
1445 if (inp
== NULL
|| request
== NULL
|| mp
== NULL
)
1447 if (len
< sizeof(*xpl
))
1449 xpl
= (struct sadb_x_policy
*)request
;
1451 if (inp
->inp_sp
== NULL
) {
1452 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1457 /* select direction */
1458 switch (xpl
->sadb_x_policy_dir
) {
1459 case IPSEC_DIR_INBOUND
:
1460 pcb_sp
= inp
->inp_sp
->sp_in
;
1462 case IPSEC_DIR_OUTBOUND
:
1463 pcb_sp
= inp
->inp_sp
->sp_out
;
1466 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1467 xpl
->sadb_x_policy_dir
));
1471 return ipsec_get_policy(pcb_sp
, mp
);
1474 /* delete policy in PCB */
1476 ipsec4_delete_pcbpolicy(inp
)
1481 panic("ipsec4_delete_pcbpolicy: NULL pointer was passed.\n");
1483 if (inp
->inp_sp
== NULL
)
1486 if (inp
->inp_sp
->sp_in
!= NULL
) {
1487 key_freesp(inp
->inp_sp
->sp_in
);
1488 inp
->inp_sp
->sp_in
= NULL
;
1491 if (inp
->inp_sp
->sp_out
!= NULL
) {
1492 key_freesp(inp
->inp_sp
->sp_out
);
1493 inp
->inp_sp
->sp_out
= NULL
;
1496 ipsec_delpcbpolicy(inp
->inp_sp
);
1504 ipsec6_set_policy(in6p
, optname
, request
, len
, priv
)
1505 struct in6pcb
*in6p
;
1511 struct sadb_x_policy
*xpl
;
1512 struct secpolicy
**pcb_sp
;
1516 if (in6p
== NULL
|| request
== NULL
)
1518 if (len
< sizeof(*xpl
))
1520 xpl
= (struct sadb_x_policy
*)request
;
1522 if (in6p
->in6p_sp
== NULL
) {
1523 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1528 /* select direction */
1529 switch (xpl
->sadb_x_policy_dir
) {
1530 case IPSEC_DIR_INBOUND
:
1531 pcb_sp
= &in6p
->in6p_sp
->sp_in
;
1533 case IPSEC_DIR_OUTBOUND
:
1534 pcb_sp
= &in6p
->in6p_sp
->sp_out
;
1537 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1538 xpl
->sadb_x_policy_dir
));
1542 /* turn bypass off */
1543 if (ipsec_bypass
!= 0)
1546 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1550 ipsec6_get_policy(in6p
, request
, len
, mp
)
1551 struct in6pcb
*in6p
;
1556 struct sadb_x_policy
*xpl
;
1557 struct secpolicy
*pcb_sp
;
1561 if (in6p
== NULL
|| request
== NULL
|| mp
== 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 return ipsec_get_policy(pcb_sp
, mp
);
1591 ipsec6_delete_pcbpolicy(in6p
)
1592 struct in6pcb
*in6p
;
1596 panic("ipsec6_delete_pcbpolicy: NULL pointer was passed.\n");
1598 if (in6p
->in6p_sp
== NULL
)
1601 if (in6p
->in6p_sp
->sp_in
!= NULL
) {
1602 key_freesp(in6p
->in6p_sp
->sp_in
);
1603 in6p
->in6p_sp
->sp_in
= NULL
;
1606 if (in6p
->in6p_sp
->sp_out
!= NULL
) {
1607 key_freesp(in6p
->in6p_sp
->sp_out
);
1608 in6p
->in6p_sp
->sp_out
= NULL
;
1611 ipsec_delpcbpolicy(in6p
->in6p_sp
);
1612 in6p
->in6p_sp
= NULL
;
1619 * return current level.
1620 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1623 ipsec_get_reqlevel(isr
)
1624 struct ipsecrequest
*isr
;
1627 u_int esp_trans_deflev
, esp_net_deflev
, ah_trans_deflev
, ah_net_deflev
;
1630 if (isr
== NULL
|| isr
->sp
== NULL
)
1631 panic("ipsec_get_reqlevel: NULL pointer is passed.\n");
1632 if (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
1633 != ((struct sockaddr
*)&isr
->sp
->spidx
.dst
)->sa_family
)
1634 panic("ipsec_get_reqlevel: family mismatched.\n");
1636 /* XXX note that we have ipseclog() expanded here - code sync issue */
1637 #define IPSEC_CHECK_DEFAULT(lev) \
1638 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1639 && (lev) != IPSEC_LEVEL_UNIQUE) \
1641 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1642 (lev), IPSEC_LEVEL_REQUIRE) \
1644 (lev) = IPSEC_LEVEL_REQUIRE, \
1648 /* set default level */
1649 switch (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
) {
1652 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev
);
1653 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev
);
1654 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev
);
1655 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev
);
1660 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev
);
1661 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev
);
1662 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev
);
1663 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev
);
1667 panic("key_get_reqlevel: Unknown family. %d\n",
1668 ((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
);
1671 #undef IPSEC_CHECK_DEFAULT
1674 switch (isr
->level
) {
1675 case IPSEC_LEVEL_DEFAULT
:
1676 switch (isr
->saidx
.proto
) {
1678 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1679 level
= esp_net_deflev
;
1681 level
= esp_trans_deflev
;
1684 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1685 level
= ah_net_deflev
;
1687 level
= ah_trans_deflev
;
1688 case IPPROTO_IPCOMP
:
1690 * we don't really care, as IPcomp document says that
1691 * we shouldn't compress small packets
1693 level
= IPSEC_LEVEL_USE
;
1696 panic("ipsec_get_reqlevel: "
1697 "Illegal protocol defined %u\n",
1702 case IPSEC_LEVEL_USE
:
1703 case IPSEC_LEVEL_REQUIRE
:
1706 case IPSEC_LEVEL_UNIQUE
:
1707 level
= IPSEC_LEVEL_REQUIRE
;
1711 panic("ipsec_get_reqlevel: Illegal IPsec level %u\n",
1719 * Check AH/ESP integrity.
1725 ipsec_in_reject(sp
, m
)
1726 struct secpolicy
*sp
;
1729 struct ipsecrequest
*isr
;
1731 int need_auth
, need_conf
, need_icv
;
1733 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1734 printf("ipsec_in_reject: using SP\n");
1735 kdebug_secpolicy(sp
));
1738 switch (sp
->policy
) {
1739 case IPSEC_POLICY_DISCARD
:
1741 case IPSEC_POLICY_BYPASS
:
1742 case IPSEC_POLICY_NONE
:
1745 case IPSEC_POLICY_IPSEC
:
1748 case IPSEC_POLICY_ENTRUST
:
1750 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1757 /* XXX should compare policy against ipsec header history */
1759 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1761 /* get current level */
1762 level
= ipsec_get_reqlevel(isr
);
1764 switch (isr
->saidx
.proto
) {
1766 if (level
== IPSEC_LEVEL_REQUIRE
) {
1769 if (isr
->sav
!= NULL
1770 && isr
->sav
->flags
== SADB_X_EXT_NONE
1771 && isr
->sav
->alg_auth
!= SADB_AALG_NONE
)
1776 if (level
== IPSEC_LEVEL_REQUIRE
) {
1781 case IPPROTO_IPCOMP
:
1783 * we don't really care, as IPcomp document says that
1784 * we shouldn't compress small packets, IPComp policy
1785 * should always be treated as being in "use" level.
1791 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1792 printf("ipsec_in_reject: auth:%d conf:%d icv:%d m_flags:%x\n",
1793 need_auth
, need_conf
, need_icv
, m
->m_flags
));
1795 if ((need_conf
&& !(m
->m_flags
& M_DECRYPTED
))
1796 || (!need_auth
&& need_icv
&& !(m
->m_flags
& M_AUTHIPDGM
))
1797 || (need_auth
&& !(m
->m_flags
& M_AUTHIPHDR
)))
1804 * Check AH/ESP integrity.
1805 * This function is called from tcp_input(), udp_input(),
1806 * and {ah,esp}4_input for tunnel mode
1809 ipsec4_in_reject_so(m
, so
)
1813 struct secpolicy
*sp
= NULL
;
1819 return 0; /* XXX should be panic ? */
1821 /* get SP for this packet.
1822 * When we are called from ip_forward(), we call
1823 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1826 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1828 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1831 return 0; /* XXX should be panic ?
1832 * -> No, there may be error. */
1834 result
= ipsec_in_reject(sp
, m
);
1835 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1836 printf("DP ipsec4_in_reject_so call free SP:%p\n", sp
));
1843 ipsec4_in_reject(m
, inp
)
1848 return ipsec4_in_reject_so(m
, NULL
);
1849 if (inp
->inp_socket
)
1850 return ipsec4_in_reject_so(m
, inp
->inp_socket
);
1852 panic("ipsec4_in_reject: invalid inpcb/socket");
1857 * Check AH/ESP integrity.
1858 * This function is called from tcp6_input(), udp6_input(),
1859 * and {ah,esp}6_input for tunnel mode
1862 ipsec6_in_reject_so(m
, so
)
1866 struct secpolicy
*sp
= NULL
;
1872 return 0; /* XXX should be panic ? */
1874 /* get SP for this packet.
1875 * When we are called from ip_forward(), we call
1876 * ipsec6_getpolicybyaddr() with IP_FORWARDING flag.
1879 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1881 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1884 return 0; /* XXX should be panic ? */
1886 result
= ipsec_in_reject(sp
, m
);
1887 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1888 printf("DP ipsec6_in_reject_so call free SP:%p\n", sp
));
1895 ipsec6_in_reject(m
, in6p
)
1897 struct in6pcb
*in6p
;
1900 return ipsec6_in_reject_so(m
, NULL
);
1901 if (in6p
->in6p_socket
)
1902 return ipsec6_in_reject_so(m
, in6p
->in6p_socket
);
1904 panic("ipsec6_in_reject: invalid in6p/socket");
1909 * compute the byte size to be occupied by IPsec header.
1910 * in case it is tunneled, it includes the size of outer IP header.
1911 * NOTE: SP passed is free in this function.
1915 struct secpolicy
*sp
;
1917 struct ipsecrequest
*isr
;
1920 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1921 printf("ipsec_hdrsiz: using SP\n");
1922 kdebug_secpolicy(sp
));
1925 switch (sp
->policy
) {
1926 case IPSEC_POLICY_DISCARD
:
1927 case IPSEC_POLICY_BYPASS
:
1928 case IPSEC_POLICY_NONE
:
1931 case IPSEC_POLICY_IPSEC
:
1934 case IPSEC_POLICY_ENTRUST
:
1936 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1941 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1945 switch (isr
->saidx
.proto
) {
1948 clen
= esp_hdrsiz(isr
);
1954 clen
= ah_hdrsiz(isr
);
1956 case IPPROTO_IPCOMP
:
1957 clen
= sizeof(struct ipcomp
);
1961 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
1962 switch (((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
) {
1964 clen
+= sizeof(struct ip
);
1968 clen
+= sizeof(struct ip6_hdr
);
1972 ipseclog((LOG_ERR
, "ipsec_hdrsiz: "
1973 "unknown AF %d in IPsec tunnel SA\n",
1974 ((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
));
1984 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1986 ipsec4_hdrsiz(m
, dir
, inp
)
1991 struct secpolicy
*sp
= NULL
;
1997 return 0; /* XXX should be panic ? */
1998 if (inp
!= NULL
&& inp
->inp_socket
== NULL
)
1999 panic("ipsec4_hdrsize: why is socket NULL but there is PCB.");
2001 /* get SP for this packet.
2002 * When we are called from ip_forward(), we call
2003 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
2006 sp
= ipsec4_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2008 sp
= ipsec4_getpolicybysock(m
, dir
, inp
->inp_socket
, &error
);
2011 return 0; /* XXX should be panic ? */
2013 size
= ipsec_hdrsiz(sp
);
2014 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2015 printf("DP ipsec4_hdrsiz call free SP:%p\n", sp
));
2016 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2017 printf("ipsec4_hdrsiz: size:%lu.\n", (unsigned long)size
));
2024 /* This function is called from ipsec6_hdrsize_tcp(),
2025 * and maybe from ip6_forward.()
2028 ipsec6_hdrsiz(m
, dir
, in6p
)
2031 struct in6pcb
*in6p
;
2033 struct secpolicy
*sp
= NULL
;
2039 return 0; /* XXX shoud be panic ? */
2040 if (in6p
!= NULL
&& in6p
->in6p_socket
== NULL
)
2041 panic("ipsec6_hdrsize: why is socket NULL but there is PCB.");
2043 /* get SP for this packet */
2044 /* XXX Is it right to call with IP_FORWARDING. */
2046 sp
= ipsec6_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2048 sp
= ipsec6_getpolicybysock(m
, dir
, in6p
->in6p_socket
, &error
);
2052 size
= ipsec_hdrsiz(sp
);
2053 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2054 printf("DP ipsec6_hdrsiz call free SP:%p\n", sp
));
2055 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2056 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size
));
2065 * encapsulate for ipsec tunnel.
2066 * ip->ip_src must be fixed later on.
2069 ipsec4_encapsulate(m
, sav
)
2071 struct secasvar
*sav
;
2078 /* can't tunnel between different AFs */
2079 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2080 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2081 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2086 /* XXX if the dst is myself, perform nothing. */
2087 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2093 if (m
->m_len
< sizeof(*ip
))
2094 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2096 ip
= mtod(m
, struct ip
*);
2098 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
2100 hlen
= ip
->ip_hl
<< 2;
2103 if (m
->m_len
!= hlen
)
2104 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2106 /* generate header checksum */
2109 ip
->ip_sum
= in_cksum(m
, hlen
);
2111 ip
->ip_sum
= in_cksum(m
, hlen
);
2114 plen
= m
->m_pkthdr
.len
;
2117 * grow the mbuf to accomodate the new IPv4 header.
2118 * NOTE: IPv4 options will never be copied.
2120 if (M_LEADINGSPACE(m
->m_next
) < hlen
) {
2122 MGET(n
, M_DONTWAIT
, MT_DATA
);
2128 n
->m_next
= m
->m_next
;
2130 m
->m_pkthdr
.len
+= hlen
;
2131 oip
= mtod(n
, struct ip
*);
2133 m
->m_next
->m_len
+= hlen
;
2134 m
->m_next
->m_data
-= hlen
;
2135 m
->m_pkthdr
.len
+= hlen
;
2136 oip
= mtod(m
->m_next
, struct ip
*);
2138 ip
= mtod(m
, struct ip
*);
2139 ovbcopy((caddr_t
)ip
, (caddr_t
)oip
, hlen
);
2140 m
->m_len
= sizeof(struct ip
);
2141 m
->m_pkthdr
.len
-= (hlen
- sizeof(struct ip
));
2143 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
2144 /* ECN consideration. */
2145 ip_ecn_ingress(ip4_ipsec_ecn
, &ip
->ip_tos
, &oip
->ip_tos
);
2147 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2149 ip
->ip_hl
= sizeof(struct ip
) >> 2;
2151 ip
->ip_off
&= htons(~IP_OFFMASK
);
2152 ip
->ip_off
&= htons(~IP_MF
);
2153 switch (ip4_ipsec_dfbit
) {
2154 case 0: /* clear DF bit */
2155 ip
->ip_off
&= htons(~IP_DF
);
2157 case 1: /* set DF bit */
2158 ip
->ip_off
|= htons(IP_DF
);
2160 default: /* copy DF bit */
2163 ip
->ip_p
= IPPROTO_IPIP
;
2164 if (plen
+ sizeof(struct ip
) < IP_MAXPACKET
)
2165 ip
->ip_len
= htons(plen
+ sizeof(struct ip
));
2167 ipseclog((LOG_ERR
, "IPv4 ipsec: size exceeds limit: "
2168 "leave ip_len as is (invalid packet)\n"));
2171 ip
->ip_id
= ip_randomid();
2173 ip
->ip_id
= htons(ip_id
++);
2175 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
,
2176 &ip
->ip_src
, sizeof(ip
->ip_src
));
2177 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
,
2178 &ip
->ip_dst
, sizeof(ip
->ip_dst
));
2179 ip
->ip_ttl
= IPDEFTTL
;
2181 /* XXX Should ip_src be updated later ? */
2189 ipsec6_encapsulate(m
, sav
)
2191 struct secasvar
*sav
;
2193 struct ip6_hdr
*oip6
;
2194 struct ip6_hdr
*ip6
;
2197 /* can't tunnel between different AFs */
2198 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2199 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2200 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
2205 /* XXX if the dst is myself, perform nothing. */
2206 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2212 plen
= m
->m_pkthdr
.len
;
2215 * grow the mbuf to accomodate the new IPv6 header.
2217 if (m
->m_len
!= sizeof(struct ip6_hdr
))
2218 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2219 if (M_LEADINGSPACE(m
->m_next
) < sizeof(struct ip6_hdr
)) {
2221 MGET(n
, M_DONTWAIT
, MT_DATA
);
2226 n
->m_len
= sizeof(struct ip6_hdr
);
2227 n
->m_next
= m
->m_next
;
2229 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2230 oip6
= mtod(n
, struct ip6_hdr
*);
2232 m
->m_next
->m_len
+= sizeof(struct ip6_hdr
);
2233 m
->m_next
->m_data
-= sizeof(struct ip6_hdr
);
2234 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2235 oip6
= mtod(m
->m_next
, struct ip6_hdr
*);
2237 ip6
= mtod(m
, struct ip6_hdr
*);
2238 ovbcopy((caddr_t
)ip6
, (caddr_t
)oip6
, sizeof(struct ip6_hdr
));
2240 /* Fake link-local scope-class addresses */
2241 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
2242 oip6
->ip6_src
.s6_addr16
[1] = 0;
2243 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
2244 oip6
->ip6_dst
.s6_addr16
[1] = 0;
2246 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
2247 /* ECN consideration. */
2248 ip6_ecn_ingress(ip6_ipsec_ecn
, &ip6
->ip6_flow
, &oip6
->ip6_flow
);
2249 if (plen
< IPV6_MAXPACKET
- sizeof(struct ip6_hdr
))
2250 ip6
->ip6_plen
= htons(plen
);
2252 /* ip6->ip6_plen will be updated in ip6_output() */
2254 ip6
->ip6_nxt
= IPPROTO_IPV6
;
2255 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.src
)->sin6_addr
,
2256 &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
2257 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
)->sin6_addr
,
2258 &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
2259 ip6
->ip6_hlim
= IPV6_DEFHLIM
;
2261 /* XXX Should ip6_src be updated later ? */
2268 * Check the variable replay window.
2269 * ipsec_chkreplay() performs replay check before ICV verification.
2270 * ipsec_updatereplay() updates replay bitmap. This must be called after
2271 * ICV verification (it also performs replay check, which is usually done
2273 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2275 * based on RFC 2401.
2278 ipsec_chkreplay(seq
, sav
)
2280 struct secasvar
*sav
;
2282 const struct secreplay
*replay
;
2285 u_int32_t wsizeb
; /* constant: bits of window size */
2286 int frlast
; /* constant: last frame */
2290 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2292 replay
= sav
->replay
;
2294 if (replay
->wsize
== 0)
2295 return 1; /* no need to check replay. */
2298 frlast
= replay
->wsize
- 1;
2299 wsizeb
= replay
->wsize
<< 3;
2301 /* sequence number of 0 is invalid */
2305 /* first time is always okay */
2306 if (replay
->count
== 0)
2309 if (seq
> replay
->lastseq
) {
2310 /* larger sequences are okay */
2313 /* seq is equal or less than lastseq. */
2314 diff
= replay
->lastseq
- seq
;
2316 /* over range to check, i.e. too old or wrapped */
2320 fr
= frlast
- diff
/ 8;
2322 /* this packet already seen ? */
2323 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2326 /* out of order but good */
2332 * check replay counter whether to update or not.
2337 ipsec_updatereplay(seq
, sav
)
2339 struct secasvar
*sav
;
2341 struct secreplay
*replay
;
2344 u_int32_t wsizeb
; /* constant: bits of window size */
2345 int frlast
; /* constant: last frame */
2349 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2351 replay
= sav
->replay
;
2353 if (replay
->wsize
== 0)
2354 goto ok
; /* no need to check replay. */
2357 frlast
= replay
->wsize
- 1;
2358 wsizeb
= replay
->wsize
<< 3;
2360 /* sequence number of 0 is invalid */
2365 if (replay
->count
== 0) {
2366 replay
->lastseq
= seq
;
2367 bzero(replay
->bitmap
, replay
->wsize
);
2368 (replay
->bitmap
)[frlast
] = 1;
2372 if (seq
> replay
->lastseq
) {
2373 /* seq is larger than lastseq. */
2374 diff
= seq
- replay
->lastseq
;
2376 /* new larger sequence number */
2377 if (diff
< wsizeb
) {
2379 /* set bit for this packet */
2380 vshiftl(replay
->bitmap
, diff
, replay
->wsize
);
2381 (replay
->bitmap
)[frlast
] |= 1;
2383 /* this packet has a "way larger" */
2384 bzero(replay
->bitmap
, replay
->wsize
);
2385 (replay
->bitmap
)[frlast
] = 1;
2387 replay
->lastseq
= seq
;
2389 /* larger is good */
2391 /* seq is equal or less than lastseq. */
2392 diff
= replay
->lastseq
- seq
;
2394 /* over range to check, i.e. too old or wrapped */
2398 fr
= frlast
- diff
/ 8;
2400 /* this packet already seen ? */
2401 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2405 (replay
->bitmap
)[fr
] |= (1 << (diff
% 8));
2407 /* out of order but good */
2411 if (replay
->count
== ~0) {
2413 /* set overflow flag */
2416 /* don't increment, no more packets accepted */
2417 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0)
2420 ipseclog((LOG_WARNING
, "replay counter made %d cycle. %s\n",
2421 replay
->overflow
, ipsec_logsastr(sav
)));
2430 * shift variable length buffer to left.
2431 * IN: bitmap: pointer to the buffer
2432 * nbit: the number of to shift.
2433 * wsize: buffer size (bytes).
2436 vshiftl(bitmap
, nbit
, wsize
)
2437 unsigned char *bitmap
;
2443 for (j
= 0; j
< nbit
; j
+= 8) {
2444 s
= (nbit
- j
< 8) ? (nbit
- j
): 8;
2446 for (i
= 1; i
< wsize
; i
++) {
2447 over
= (bitmap
[i
] >> (8 - s
));
2449 bitmap
[i
-1] |= over
;
2457 ipsec4_logpacketstr(ip
, spi
)
2461 static char buf
[256];
2465 s
= (u_int8_t
*)(&ip
->ip_src
);
2466 d
= (u_int8_t
*)(&ip
->ip_dst
);
2469 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2472 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%u.%u.%u.%u",
2473 s
[0], s
[1], s
[2], s
[3]);
2476 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%u.%u.%u.%u",
2477 d
[0], d
[1], d
[2], d
[3]);
2480 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2487 ipsec6_logpacketstr(ip6
, spi
)
2488 struct ip6_hdr
*ip6
;
2491 static char buf
[256];
2495 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2498 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%s",
2499 ip6_sprintf(&ip6
->ip6_src
));
2502 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%s",
2503 ip6_sprintf(&ip6
->ip6_dst
));
2506 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2514 struct secasvar
*sav
;
2516 static char buf
[256];
2518 struct secasindex
*saidx
= &sav
->sah
->saidx
;
2520 /* validity check */
2521 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2522 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
)
2523 panic("ipsec_logsastr: family mismatched.\n");
2526 snprintf(buf
, sizeof(buf
), "SA(SPI=%u ", (u_int32_t
)ntohl(sav
->spi
));
2529 if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET
) {
2531 s
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->src
)->sin_addr
;
2532 d
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->dst
)->sin_addr
;
2533 snprintf(p
, sizeof(buf
) - (p
- buf
),
2534 "src=%d.%d.%d.%d dst=%d.%d.%d.%d",
2535 s
[0], s
[1], s
[2], s
[3], d
[0], d
[1], d
[2], d
[3]);
2538 else if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET6
) {
2539 snprintf(p
, sizeof(buf
) - (p
- buf
),
2541 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->src
)->sin6_addr
));
2544 snprintf(p
, sizeof(buf
) - (p
- buf
),
2546 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->dst
)->sin6_addr
));
2551 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2567 p
= mtod(m
, u_char
*);
2568 for (i
= 0; i
< m
->m_len
; i
++) {
2569 printf("%02x ", p
[i
]);
2571 if (totlen
% 16 == 0)
2576 if (totlen
% 16 != 0)
2583 * IPsec output logic for IPv4.
2586 ipsec4_output(state
, sp
, flags
)
2587 struct ipsec_output_state
*state
;
2588 struct secpolicy
*sp
;
2591 struct ip
*ip
= NULL
;
2592 struct ipsecrequest
*isr
= NULL
;
2593 struct secasindex saidx
;
2596 struct sockaddr_in
*dst4
;
2597 struct sockaddr_in
*sin
;
2600 panic("state == NULL in ipsec4_output");
2602 panic("state->m == NULL in ipsec4_output");
2604 panic("state->ro == NULL in ipsec4_output");
2606 panic("state->dst == NULL in ipsec4_output");
2608 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_START
, 0,0,0,0,0);
2610 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2611 printf("ipsec4_output: applyed SP\n");
2612 kdebug_secpolicy(sp
));
2614 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2616 #if 0 /* give up to check restriction of transport mode */
2617 /* XXX but should be checked somewhere */
2619 * some of the IPsec operation must be performed only in
2622 if (isr
->saidx
.mode
== IPSEC_MODE_TRANSPORT
2623 && (flags
& IP_FORWARDING
))
2627 /* make SA index for search proper SA */
2628 ip
= mtod(state
->m
, struct ip
*);
2629 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2630 saidx
.mode
= isr
->saidx
.mode
;
2631 saidx
.reqid
= isr
->saidx
.reqid
;
2632 sin
= (struct sockaddr_in
*)&saidx
.src
;
2633 if (sin
->sin_len
== 0) {
2634 sin
->sin_len
= sizeof(*sin
);
2635 sin
->sin_family
= AF_INET
;
2636 sin
->sin_port
= IPSEC_PORT_ANY
;
2637 bcopy(&ip
->ip_src
, &sin
->sin_addr
,
2638 sizeof(sin
->sin_addr
));
2640 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2641 if (sin
->sin_len
== 0) {
2642 sin
->sin_len
= sizeof(*sin
);
2643 sin
->sin_family
= AF_INET
;
2644 sin
->sin_port
= IPSEC_PORT_ANY
;
2645 bcopy(&ip
->ip_dst
, &sin
->sin_addr
,
2646 sizeof(sin
->sin_addr
));
2649 if ((error
= key_checkrequest(isr
, &saidx
)) != 0) {
2651 * IPsec processing is required, but no SA found.
2652 * I assume that key_acquire() had been called
2653 * to get/establish the SA. Here I discard
2654 * this packet because it is responsibility for
2655 * upper layer to retransmit the packet.
2657 ipsecstat
.out_nosa
++;
2661 /* validity check */
2662 if (isr
->sav
== NULL
) {
2663 switch (ipsec_get_reqlevel(isr
)) {
2664 case IPSEC_LEVEL_USE
:
2666 case IPSEC_LEVEL_REQUIRE
:
2667 /* must be not reached here. */
2668 panic("ipsec4_output: no SA found, but required.");
2673 * If there is no valid SA, we give up to process any
2674 * more. In such a case, the SA's status is changed
2675 * from DYING to DEAD after allocating. If a packet
2676 * send to the receiver by dead SA, the receiver can
2677 * not decode a packet because SA has been dead.
2679 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2680 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2681 ipsecstat
.out_nosa
++;
2687 * There may be the case that SA status will be changed when
2688 * we are refering to one. So calling splsoftnet().
2692 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2694 * build IPsec tunnel.
2696 /* XXX should be processed with other familiy */
2697 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2698 ipseclog((LOG_ERR
, "ipsec4_output: "
2699 "family mismatched between inner and outer spi=%u\n",
2700 (u_int32_t
)ntohl(isr
->sav
->spi
)));
2702 error
= EAFNOSUPPORT
;
2706 state
->m
= ipsec4_splithdr(state
->m
);
2712 error
= ipsec4_encapsulate(state
->m
, isr
->sav
);
2718 ip
= mtod(state
->m
, struct ip
*);
2720 state
->ro
= &isr
->sav
->sah
->sa_route
;
2721 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
2722 dst4
= (struct sockaddr_in
*)state
->dst
;
2723 if (state
->ro
->ro_rt
2724 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
2725 || dst4
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
2726 rtfree(state
->ro
->ro_rt
);
2727 state
->ro
->ro_rt
= NULL
;
2729 if (state
->ro
->ro_rt
== 0) {
2730 dst4
->sin_family
= AF_INET
;
2731 dst4
->sin_len
= sizeof(*dst4
);
2732 dst4
->sin_addr
= ip
->ip_dst
;
2735 if (state
->ro
->ro_rt
== 0) {
2736 ipstat
.ips_noroute
++;
2737 error
= EHOSTUNREACH
;
2741 /* adjust state->dst if tunnel endpoint is offlink */
2742 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
2743 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
2744 dst4
= (struct sockaddr_in
*)state
->dst
;
2749 state
->m
= ipsec4_splithdr(state
->m
);
2754 switch (isr
->saidx
.proto
) {
2757 if ((error
= esp4_output(state
->m
, isr
)) != 0) {
2769 if ((error
= ah4_output(state
->m
, isr
)) != 0) {
2774 case IPPROTO_IPCOMP
:
2775 if ((error
= ipcomp4_output(state
->m
, isr
)) != 0) {
2782 "ipsec4_output: unknown ipsec protocol %d\n",
2790 if (state
->m
== 0) {
2794 ip
= mtod(state
->m
, struct ip
*);
2797 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_END
, 0,0,0,0,0);
2803 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_END
, error
,0,0,0,0);
2810 * IPsec output logic for IPv6, transport mode.
2813 ipsec6_output_trans(state
, nexthdrp
, mprev
, sp
, flags
, tun
)
2814 struct ipsec_output_state
*state
;
2817 struct secpolicy
*sp
;
2821 struct ip6_hdr
*ip6
;
2822 struct ipsecrequest
*isr
= NULL
;
2823 struct secasindex saidx
;
2826 struct sockaddr_in6
*sin6
;
2829 panic("state == NULL in ipsec6_output_trans");
2831 panic("state->m == NULL in ipsec6_output_trans");
2833 panic("nexthdrp == NULL in ipsec6_output_trans");
2835 panic("mprev == NULL in ipsec6_output_trans");
2837 panic("sp == NULL in ipsec6_output_trans");
2839 panic("tun == NULL in ipsec6_output_trans");
2841 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2842 printf("ipsec6_output_trans: applyed SP\n");
2843 kdebug_secpolicy(sp
));
2846 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
2847 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2848 /* the rest will be handled by ipsec6_output_tunnel() */
2852 /* make SA index for search proper SA */
2853 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2854 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2855 saidx
.mode
= isr
->saidx
.mode
;
2856 saidx
.reqid
= isr
->saidx
.reqid
;
2857 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
2858 if (sin6
->sin6_len
== 0) {
2859 sin6
->sin6_len
= sizeof(*sin6
);
2860 sin6
->sin6_family
= AF_INET6
;
2861 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2862 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
2863 sizeof(ip6
->ip6_src
));
2864 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
2865 /* fix scope id for comparing SPD */
2866 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2867 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
2870 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
2871 if (sin6
->sin6_len
== 0) {
2872 sin6
->sin6_len
= sizeof(*sin6
);
2873 sin6
->sin6_family
= AF_INET6
;
2874 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2875 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
2876 sizeof(ip6
->ip6_dst
));
2877 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
2878 /* fix scope id for comparing SPD */
2879 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2880 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
2884 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
2886 * IPsec processing is required, but no SA found.
2887 * I assume that key_acquire() had been called
2888 * to get/establish the SA. Here I discard
2889 * this packet because it is responsibility for
2890 * upper layer to retransmit the packet.
2892 ipsec6stat
.out_nosa
++;
2896 * Notify the fact that the packet is discarded
2897 * to ourselves. I believe this is better than
2898 * just silently discarding. (jinmei@kame.net)
2899 * XXX: should we restrict the error to TCP packets?
2900 * XXX: should we directly notify sockets via
2903 icmp6_error(state
->m
, ICMP6_DST_UNREACH
,
2904 ICMP6_DST_UNREACH_ADMIN
, 0);
2905 state
->m
= NULL
; /* icmp6_error freed the mbuf */
2909 /* validity check */
2910 if (isr
->sav
== NULL
) {
2911 switch (ipsec_get_reqlevel(isr
)) {
2912 case IPSEC_LEVEL_USE
:
2914 case IPSEC_LEVEL_REQUIRE
:
2915 /* must be not reached here. */
2916 panic("ipsec6_output_trans: no SA found, but required.");
2921 * If there is no valid SA, we give up to process.
2922 * see same place at ipsec4_output().
2924 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2925 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2926 ipsec6stat
.out_nosa
++;
2931 switch (isr
->saidx
.proto
) {
2934 error
= esp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2941 error
= ah6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2943 case IPPROTO_IPCOMP
:
2944 error
= ipcomp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2947 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
2948 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
2950 ipsec6stat
.out_inval
++;
2958 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2959 if (plen
> IPV6_MAXPACKET
) {
2960 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
2961 "IPsec with IPv6 jumbogram is not supported\n"));
2962 ipsec6stat
.out_inval
++;
2963 error
= EINVAL
; /*XXX*/
2966 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2967 ip6
->ip6_plen
= htons(plen
);
2970 /* if we have more to go, we need a tunnel mode processing */
2983 * IPsec output logic for IPv6, tunnel mode.
2986 ipsec6_output_tunnel(state
, sp
, flags
)
2987 struct ipsec_output_state
*state
;
2988 struct secpolicy
*sp
;
2991 struct ip6_hdr
*ip6
;
2992 struct ipsecrequest
*isr
= NULL
;
2993 struct secasindex saidx
;
2996 struct sockaddr_in6
* dst6
;
3000 panic("state == NULL in ipsec6_output_tunnel");
3002 panic("state->m == NULL in ipsec6_output_tunnel");
3004 panic("sp == NULL in ipsec6_output_tunnel");
3006 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
3007 printf("ipsec6_output_tunnel: applyed SP\n");
3008 kdebug_secpolicy(sp
));
3011 * transport mode ipsec (before the 1st tunnel mode) is already
3012 * processed by ipsec6_output_trans().
3014 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
3015 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
3019 for (/* already initialized */; isr
; isr
= isr
->next
) {
3020 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3021 /* When tunnel mode, SA peers must be specified. */
3022 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
3024 /* make SA index to look for a proper SA */
3025 struct sockaddr_in6
*sin6
;
3027 bzero(&saidx
, sizeof(saidx
));
3028 saidx
.proto
= isr
->saidx
.proto
;
3029 saidx
.mode
= isr
->saidx
.mode
;
3030 saidx
.reqid
= isr
->saidx
.reqid
;
3032 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3033 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
3034 if (sin6
->sin6_len
== 0) {
3035 sin6
->sin6_len
= sizeof(*sin6
);
3036 sin6
->sin6_family
= AF_INET6
;
3037 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3038 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
3039 sizeof(ip6
->ip6_src
));
3040 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
3041 /* fix scope id for comparing SPD */
3042 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3043 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
3046 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
3047 if (sin6
->sin6_len
== 0) {
3048 sin6
->sin6_len
= sizeof(*sin6
);
3049 sin6
->sin6_family
= AF_INET6
;
3050 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3051 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
3052 sizeof(ip6
->ip6_dst
));
3053 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
3054 /* fix scope id for comparing SPD */
3055 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3056 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
3061 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
3063 * IPsec processing is required, but no SA found.
3064 * I assume that key_acquire() had been called
3065 * to get/establish the SA. Here I discard
3066 * this packet because it is responsibility for
3067 * upper layer to retransmit the packet.
3069 ipsec6stat
.out_nosa
++;
3074 /* validity check */
3075 if (isr
->sav
== NULL
) {
3076 switch (ipsec_get_reqlevel(isr
)) {
3077 case IPSEC_LEVEL_USE
:
3079 case IPSEC_LEVEL_REQUIRE
:
3080 /* must be not reached here. */
3081 panic("ipsec6_output_tunnel: no SA found, but required.");
3086 * If there is no valid SA, we give up to process.
3087 * see same place at ipsec4_output().
3089 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
3090 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
3091 ipsec6stat
.out_nosa
++;
3097 * There may be the case that SA status will be changed when
3098 * we are refering to one. So calling splsoftnet().
3102 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3104 * build IPsec tunnel.
3106 /* XXX should be processed with other familiy */
3107 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
3108 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3109 "family mismatched between inner and outer, spi=%u\n",
3110 (u_int32_t
)ntohl(isr
->sav
->spi
)));
3112 ipsec6stat
.out_inval
++;
3113 error
= EAFNOSUPPORT
;
3117 state
->m
= ipsec6_splithdr(state
->m
);
3120 ipsec6stat
.out_nomem
++;
3124 error
= ipsec6_encapsulate(state
->m
, isr
->sav
);
3130 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3132 state
->ro
= &isr
->sav
->sah
->sa_route
;
3133 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
3134 dst6
= (struct sockaddr_in6
*)state
->dst
;
3135 if (state
->ro
->ro_rt
3136 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
3137 || !IN6_ARE_ADDR_EQUAL(&dst6
->sin6_addr
, &ip6
->ip6_dst
))) {
3138 rtfree(state
->ro
->ro_rt
);
3139 state
->ro
->ro_rt
= NULL
;
3141 if (state
->ro
->ro_rt
== 0) {
3142 bzero(dst6
, sizeof(*dst6
));
3143 dst6
->sin6_family
= AF_INET6
;
3144 dst6
->sin6_len
= sizeof(*dst6
);
3145 dst6
->sin6_addr
= ip6
->ip6_dst
;
3148 if (state
->ro
->ro_rt
== 0) {
3149 ip6stat
.ip6s_noroute
++;
3150 ipsec6stat
.out_noroute
++;
3151 error
= EHOSTUNREACH
;
3155 /* adjust state->dst if tunnel endpoint is offlink */
3156 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
3157 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
3158 dst6
= (struct sockaddr_in6
*)state
->dst
;
3163 state
->m
= ipsec6_splithdr(state
->m
);
3165 ipsec6stat
.out_nomem
++;
3169 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3170 switch (isr
->saidx
.proto
) {
3173 error
= esp6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
3180 error
= ah6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
3182 case IPPROTO_IPCOMP
:
3183 /* XXX code should be here */
3186 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3187 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
3189 ipsec6stat
.out_inval
++;
3197 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
3198 if (plen
> IPV6_MAXPACKET
) {
3199 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3200 "IPsec with IPv6 jumbogram is not supported\n"));
3201 ipsec6stat
.out_inval
++;
3202 error
= EINVAL
; /*XXX*/
3205 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3206 ip6
->ip6_plen
= htons(plen
);
3220 * Chop IP header and option off from the payload.
3222 static struct mbuf
*
3230 if (m
->m_len
< sizeof(struct ip
))
3231 panic("ipsec4_splithdr: first mbuf too short");
3232 ip
= mtod(m
, struct ip
*);
3234 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
3236 hlen
= ip
->ip_hl
<< 2;
3238 if (m
->m_len
> hlen
) {
3239 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3244 M_COPY_PKTHDR(mh
, m
);
3246 m
->m_flags
&= ~M_PKTHDR
;
3252 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), hlen
);
3253 } else if (m
->m_len
< hlen
) {
3254 m
= m_pullup(m
, hlen
);
3263 static struct mbuf
*
3268 struct ip6_hdr
*ip6
;
3271 if (m
->m_len
< sizeof(struct ip6_hdr
))
3272 panic("ipsec6_splithdr: first mbuf too short");
3273 ip6
= mtod(m
, struct ip6_hdr
*);
3274 hlen
= sizeof(struct ip6_hdr
);
3275 if (m
->m_len
> hlen
) {
3276 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3281 M_COPY_PKTHDR(mh
, m
);
3283 m
->m_flags
&= ~M_PKTHDR
;
3289 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), hlen
);
3290 } else if (m
->m_len
< hlen
) {
3291 m
= m_pullup(m
, hlen
);
3299 /* validate inbound IPsec tunnel packet. */
3301 ipsec4_tunnel_validate(m
, off
, nxt0
, sav
)
3302 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3305 struct secasvar
*sav
;
3307 u_int8_t nxt
= nxt0
& 0xff;
3308 struct sockaddr_in
*sin
;
3309 struct sockaddr_in osrc
, odst
, isrc
, idst
;
3311 struct secpolicy
*sp
;
3315 if (m
->m_len
< sizeof(struct ip
))
3316 panic("too short mbuf on ipsec4_tunnel_validate");
3318 if (nxt
!= IPPROTO_IPV4
)
3320 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip
))
3322 /* do not decapsulate if the SA is for transport mode only */
3323 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3326 oip
= mtod(m
, struct ip
*);
3328 hlen
= _IP_VHL_HL(oip
->ip_vhl
) << 2;
3330 hlen
= oip
->ip_hl
<< 2;
3332 if (hlen
!= sizeof(struct ip
))
3335 /* AF_INET6 should be supported, but at this moment we don't. */
3336 sin
= (struct sockaddr_in
*)&sav
->sah
->saidx
.dst
;
3337 if (sin
->sin_family
!= AF_INET
)
3339 if (bcmp(&oip
->ip_dst
, &sin
->sin_addr
, sizeof(oip
->ip_dst
)) != 0)
3343 bzero(&osrc
, sizeof(osrc
));
3344 bzero(&odst
, sizeof(odst
));
3345 bzero(&isrc
, sizeof(isrc
));
3346 bzero(&idst
, sizeof(idst
));
3347 osrc
.sin_family
= odst
.sin_family
= isrc
.sin_family
= idst
.sin_family
=
3349 osrc
.sin_len
= odst
.sin_len
= isrc
.sin_len
= idst
.sin_len
=
3350 sizeof(struct sockaddr_in
);
3351 osrc
.sin_addr
= oip
->ip_src
;
3352 odst
.sin_addr
= oip
->ip_dst
;
3353 m_copydata(m
, off
+ offsetof(struct ip
, ip_src
), sizeof(isrc
.sin_addr
),
3354 (caddr_t
)&isrc
.sin_addr
);
3355 m_copydata(m
, off
+ offsetof(struct ip
, ip_dst
), sizeof(idst
.sin_addr
),
3356 (caddr_t
)&idst
.sin_addr
);
3359 * RFC2401 5.2.1 (b): (assume that we are using tunnel mode)
3360 * - if the inner destination is multicast address, there can be
3361 * multiple permissible inner source address. implementation
3362 * may want to skip verification of inner source address against
3364 * - if the inner protocol is ICMP, the packet may be an error report
3365 * from routers on the other side of the VPN cloud (R in the
3366 * following diagram). in this case, we cannot verify inner source
3367 * address against SPD selector.
3368 * me -- gw === gw -- R -- you
3370 * we consider the first bullet to be users responsibility on SPD entry
3371 * configuration (if you need to encrypt multicast traffic, set
3372 * the source range of SPD selector to 0.0.0.0/0, or have explicit
3373 * address ranges for possible senders).
3374 * the second bullet is not taken care of (yet).
3376 * therefore, we do not do anything special about inner source.
3379 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3380 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3389 /* validate inbound IPsec tunnel packet. */
3391 ipsec6_tunnel_validate(m
, off
, nxt0
, sav
)
3392 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3395 struct secasvar
*sav
;
3397 u_int8_t nxt
= nxt0
& 0xff;
3398 struct sockaddr_in6
*sin6
;
3399 struct sockaddr_in6 osrc
, odst
, isrc
, idst
;
3400 struct secpolicy
*sp
;
3401 struct ip6_hdr
*oip6
;
3404 if (m
->m_len
< sizeof(struct ip6_hdr
))
3405 panic("too short mbuf on ipsec6_tunnel_validate");
3407 if (nxt
!= IPPROTO_IPV6
)
3409 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip6_hdr
))
3411 /* do not decapsulate if the SA is for transport mode only */
3412 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3415 oip6
= mtod(m
, struct ip6_hdr
*);
3416 /* AF_INET should be supported, but at this moment we don't. */
3417 sin6
= (struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
;
3418 if (sin6
->sin6_family
!= AF_INET6
)
3420 if (!IN6_ARE_ADDR_EQUAL(&oip6
->ip6_dst
, &sin6
->sin6_addr
))
3424 bzero(&osrc
, sizeof(osrc
));
3425 bzero(&odst
, sizeof(odst
));
3426 bzero(&isrc
, sizeof(isrc
));
3427 bzero(&idst
, sizeof(idst
));
3428 osrc
.sin6_family
= odst
.sin6_family
= isrc
.sin6_family
=
3429 idst
.sin6_family
= AF_INET6
;
3430 osrc
.sin6_len
= odst
.sin6_len
= isrc
.sin6_len
= idst
.sin6_len
=
3431 sizeof(struct sockaddr_in6
);
3432 osrc
.sin6_addr
= oip6
->ip6_src
;
3433 odst
.sin6_addr
= oip6
->ip6_dst
;
3434 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_src
),
3435 sizeof(isrc
.sin6_addr
), (caddr_t
)&isrc
.sin6_addr
);
3436 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_dst
),
3437 sizeof(idst
.sin6_addr
), (caddr_t
)&idst
.sin6_addr
);
3440 * regarding to inner source address validation, see a long comment
3441 * in ipsec4_tunnel_validate.
3444 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3445 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3447 * when there is no suitable inbound policy for the packet of the ipsec
3448 * tunnel mode, the kernel never decapsulate the tunneled packet
3449 * as the ipsec tunnel mode even when the system wide policy is "none".
3450 * then the kernel leaves the generic tunnel module to process this
3451 * packet. if there is no rule of the generic tunnel, the packet
3452 * is rejected and the statistics will be counted up.
3463 * Make a mbuf chain for encryption.
3464 * If the original mbuf chain contains a mbuf with a cluster,
3465 * allocate a new cluster and copy the data to the new cluster.
3466 * XXX: this hack is inefficient, but is necessary to handle cases
3467 * of TCP retransmission...
3473 struct mbuf
*n
, **mpp
, *mnew
;
3475 for (n
= m
, mpp
= &m
; n
; n
= n
->m_next
) {
3476 if (n
->m_flags
& M_EXT
) {
3478 * Make a copy only if there are more than one references
3480 * XXX: is this approach effective?
3483 n
->m_ext
.ext_free
||
3484 m_mclhasreference(n
)
3490 if (n
->m_flags
& M_PKTHDR
) {
3491 MGETHDR(mnew
, M_DONTWAIT
, MT_HEADER
);
3494 mnew
->m_pkthdr
= n
->m_pkthdr
;
3496 if (n
->m_pkthdr
.aux
) {
3497 mnew
->m_pkthdr
.aux
=
3498 m_copym(n
->m_pkthdr
.aux
,
3499 0, M_COPYALL
, M_DONTWAIT
);
3502 M_COPY_PKTHDR(mnew
, n
);
3503 mnew
->m_flags
= n
->m_flags
& M_COPYFLAGS
;
3506 MGET(mnew
, M_DONTWAIT
, MT_DATA
);
3514 * Copy data. If we don't have enough space to
3515 * store the whole data, allocate a cluster
3516 * or additional mbufs.
3517 * XXX: we don't use m_copyback(), since the
3518 * function does not use clusters and thus is
3527 if (remain
<= (mm
->m_flags
& M_PKTHDR
? MHLEN
: MLEN
))
3529 else { /* allocate a cluster */
3530 MCLGET(mm
, M_DONTWAIT
);
3531 if (!(mm
->m_flags
& M_EXT
)) {
3535 len
= remain
< MCLBYTES
?
3539 bcopy(n
->m_data
+ copied
, mm
->m_data
,
3546 if (remain
<= 0) /* completed? */
3549 /* need another mbuf */
3550 MGETHDR(mn
, M_DONTWAIT
, MT_HEADER
);
3553 mn
->m_pkthdr
.rcvif
= NULL
;
3559 mm
->m_next
= m_free(n
);
3577 static struct mbuf
*
3583 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3585 n
= m_aux_add(m
, AF_INET
, IPPROTO_ESP
);
3587 return n
; /* ENOBUFS */
3588 n
->m_len
= sizeof(struct socket
*);
3589 bzero(mtod(n
, void *), n
->m_len
);
3593 static struct mbuf
*
3599 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3601 if (n
&& n
->m_len
< sizeof(struct socket
*))
3602 panic("invalid ipsec m_aux");
3613 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3618 /* if the aux buffer is unnecessary, nuke it. */
3627 if (n
->m_len
== sizeof(struct socket
*) && !*mtod(n
, struct socket
**))
3632 ipsec_setsocket(m
, so
)
3638 /* if so == NULL, don't insist on getting the aux mbuf */
3640 n
= ipsec_addaux(m
);
3644 n
= ipsec_findaux(m
);
3645 if (n
&& n
->m_len
>= sizeof(struct socket
*))
3646 *mtod(n
, struct socket
**) = so
;
3657 n
= ipsec_findaux(m
);
3658 if (n
&& n
->m_len
>= sizeof(struct socket
*))
3659 return *mtod(n
, struct socket
**);
3665 ipsec_addhist(m
, proto
, spi
)
3671 struct ipsec_history
*p
;
3673 n
= ipsec_addaux(m
);
3676 if (M_TRAILINGSPACE(n
) < sizeof(*p
))
3677 return ENOSPC
; /* XXX */
3678 p
= (struct ipsec_history
*)(mtod(n
, caddr_t
) + n
->m_len
);
3679 n
->m_len
+= sizeof(*p
);
3680 bzero(p
, sizeof(*p
));
3681 p
->ih_proto
= proto
;
3686 struct ipsec_history
*
3687 ipsec_gethist(m
, lenp
)
3694 n
= ipsec_findaux(m
);
3698 if (sizeof(struct socket
*) > l
)
3700 if ((l
- sizeof(struct socket
*)) % sizeof(struct ipsec_history
))
3702 /* XXX does it make more sense to divide by sizeof(ipsec_history)? */
3704 *lenp
= l
- sizeof(struct socket
*);
3705 return (struct ipsec_history
*)
3706 (mtod(n
, caddr_t
) + sizeof(struct socket
*));
3715 n
= ipsec_findaux(m
);
3716 if ((n
) && n
->m_len
> sizeof(struct socket
*))
3717 n
->m_len
= sizeof(struct socket
*);
3721 __private_extern__
void
3722 ipsec_send_natt_keepalive(
3723 struct secasvar
*sav
)
3729 if ((esp_udp_encap_port
& 0xFFFF) == 0 || sav
->remote_ike_port
== 0) return;
3731 m
= m_gethdr(M_NOWAIT
, MT_DATA
);
3732 if (m
== NULL
) return;
3735 * Create a UDP packet complete with IP header.
3736 * We must do this because UDP output requires
3737 * an inpcb which we don't have. UDP packet
3738 * contains one byte payload. The byte is set
3741 ip
= (struct ip
*)m_mtod(m
);
3742 uh
= (struct udphdr
*)((char*)m_mtod(m
) + sizeof(struct ip
));
3743 m
->m_len
= sizeof(struct udpiphdr
) + 1;
3744 bzero(m_mtod(m
), m
->m_len
);
3745 ip
->ip_len
= ntohs(m
->m_len
);
3746 ip
->ip_ttl
= ip_defttl
;
3747 ip
->ip_p
= IPPROTO_UDP
;
3748 ip
->ip_src
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
;
3749 ip
->ip_dst
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
;
3750 uh
->uh_sport
= ntohs((u_short
)esp_udp_encap_port
);
3751 uh
->uh_dport
= ntohs(sav
->remote_ike_port
);
3752 uh
->uh_ulen
= htons(1 + sizeof(struct udphdr
));
3754 *(u_int8_t
*)((char*)m_mtod(m
) + sizeof(struct ip
) + sizeof(struct udphdr
)) = 0xFF;
3756 if (ip_output(m
, NULL
, &sav
->sah
->sa_route
, IP_NOIPSEC
, NULL
) == 0)
3757 sav
->natt_last_activity
= natt_now
;