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 switch (so
->so_proto
->pr_domain
->dom_family
) {
291 pcbsp
= sotoinpcb(so
)->inp_sp
;
295 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
301 /* Socket has not specified an IPSEC policy */
302 return ipsec4_getpolicybyaddr(m
, dir
, 0, error
);
305 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_START
, 0,0,0,0,0);
307 switch (so
->so_proto
->pr_domain
->dom_family
) {
309 /* set spidx in pcb */
310 *error
= ipsec4_setspidx_inpcb(m
, sotoinpcb(so
));
314 /* set spidx in pcb */
315 *error
= ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
319 panic("ipsec4_getpolicybysock: unsupported address family\n");
322 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 1,*error
,0,0,0);
328 panic("ipsec4_getpolicybysock: pcbsp is NULL.\n");
331 case IPSEC_DIR_INBOUND
:
332 currsp
= pcbsp
->sp_in
;
334 case IPSEC_DIR_OUTBOUND
:
335 currsp
= pcbsp
->sp_out
;
338 panic("ipsec4_getpolicybysock: illegal direction.\n");
343 panic("ipsec4_getpolicybysock: currsp is NULL.\n");
345 /* when privilieged socket */
347 switch (currsp
->policy
) {
348 case IPSEC_POLICY_BYPASS
:
351 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 2,*error
,0,0,0);
354 case IPSEC_POLICY_ENTRUST
:
355 /* look for a policy in SPD */
356 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
359 if (kernsp
!= NULL
) {
360 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
361 printf("DP ipsec4_getpolicybysock called "
362 "to allocate SP:%p\n", kernsp
));
364 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 3,*error
,0,0,0);
369 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
370 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
372 "fixed system default policy: %d->%d\n",
373 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
374 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
376 ip4_def_policy
.refcnt
++;
378 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 4,*error
,0,0,0);
379 return &ip4_def_policy
;
381 case IPSEC_POLICY_IPSEC
:
384 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 5,*error
,0,0,0);
388 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
389 "Invalid policy for PCB %d\n", currsp
->policy
));
391 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 6,*error
,0,0,0);
397 /* when non-privilieged socket */
398 /* look for a policy in SPD */
399 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
402 if (kernsp
!= NULL
) {
403 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
404 printf("DP ipsec4_getpolicybysock called "
405 "to allocate SP:%p\n", kernsp
));
407 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 7,*error
,0,0,0);
412 switch (currsp
->policy
) {
413 case IPSEC_POLICY_BYPASS
:
414 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
415 "Illegal policy for non-priviliged defined %d\n",
418 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 8,*error
,0,0,0);
421 case IPSEC_POLICY_ENTRUST
:
422 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
423 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
425 "fixed system default policy: %d->%d\n",
426 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
427 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
429 ip4_def_policy
.refcnt
++;
431 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 9,*error
,0,0,0);
432 return &ip4_def_policy
;
434 case IPSEC_POLICY_IPSEC
:
437 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 10,*error
,0,0,0);
441 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
442 "Invalid policy for PCB %d\n", currsp
->policy
));
444 KERNEL_DEBUG(DBG_FNC_GETPOL_SOCK
| DBG_FUNC_END
, 11,*error
,0,0,0);
451 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
452 * and return a pointer to SP.
453 * OUT: positive: a pointer to the entry for security policy leaf matched.
454 * NULL: no apropreate SP found, the following value is set to error.
456 * EACCES : discard packet.
457 * ENOENT : ipsec_acquire() in progress, maybe.
458 * others : error occured.
461 ipsec4_getpolicybyaddr(m
, dir
, flag
, error
)
467 struct secpolicy
*sp
= NULL
;
469 if (ipsec_bypass
!= 0)
473 if (m
== NULL
|| error
== NULL
)
474 panic("ipsec4_getpolicybyaddr: NULL pointer was passed.\n");
477 struct secpolicyindex spidx
;
479 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_START
, 0,0,0,0,0);
480 bzero(&spidx
, sizeof(spidx
));
482 /* make a index to look for a policy */
483 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET
, m
,
484 (flag
& IP_FORWARDING
) ? 0 : 1);
487 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 1,*error
,0,0,0);
491 sp
= key_allocsp(&spidx
, dir
);
496 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
497 printf("DP ipsec4_getpolicybyaddr called "
498 "to allocate SP:%p\n", sp
));
500 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 2,*error
,0,0,0);
505 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
506 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
507 ipseclog((LOG_INFO
, "fixed system default policy:%d->%d\n",
508 ip4_def_policy
.policy
,
510 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
512 ip4_def_policy
.refcnt
++;
514 KERNEL_DEBUG(DBG_FNC_GETPOL_ADDR
| DBG_FUNC_END
, 3,*error
,0,0,0);
515 return &ip4_def_policy
;
520 * For OUTBOUND packet having a socket. Searching SPD for packet,
521 * and return a pointer to SP.
522 * OUT: NULL: no apropreate SP found, the following value is set to error.
524 * EACCES : discard packet.
525 * ENOENT : ipsec_acquire() in progress, maybe.
526 * others : error occured.
527 * others: a pointer to SP
530 ipsec6_getpolicybysock(m
, dir
, so
, error
)
536 struct inpcbpolicy
*pcbsp
= NULL
;
537 struct secpolicy
*currsp
= NULL
; /* policy on socket */
538 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
541 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
542 panic("ipsec6_getpolicybysock: NULL pointer was passed.\n");
545 if (so
->so_proto
->pr_domain
->dom_family
!= AF_INET6
)
546 panic("ipsec6_getpolicybysock: socket domain != inet6\n");
549 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
552 return ipsec6_getpolicybyaddr(m
, dir
, 0, error
);
555 /* set spidx in pcb */
556 ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
560 panic("ipsec6_getpolicybysock: pcbsp is NULL.\n");
563 case IPSEC_DIR_INBOUND
:
564 currsp
= pcbsp
->sp_in
;
566 case IPSEC_DIR_OUTBOUND
:
567 currsp
= pcbsp
->sp_out
;
570 panic("ipsec6_getpolicybysock: illegal direction.\n");
575 panic("ipsec6_getpolicybysock: currsp is NULL.\n");
577 /* when privilieged socket */
579 switch (currsp
->policy
) {
580 case IPSEC_POLICY_BYPASS
:
585 case IPSEC_POLICY_ENTRUST
:
586 /* look for a policy in SPD */
587 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
590 if (kernsp
!= NULL
) {
591 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
592 printf("DP ipsec6_getpolicybysock called "
593 "to allocate SP:%p\n", kernsp
));
599 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
600 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
602 "fixed system default policy: %d->%d\n",
603 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
604 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
606 ip6_def_policy
.refcnt
++;
608 return &ip6_def_policy
;
610 case IPSEC_POLICY_IPSEC
:
616 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
617 "Invalid policy for PCB %d\n", currsp
->policy
));
624 /* when non-privilieged socket */
625 /* look for a policy in SPD */
626 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
629 if (kernsp
!= NULL
) {
630 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
631 printf("DP ipsec6_getpolicybysock called "
632 "to allocate SP:%p\n", kernsp
));
638 switch (currsp
->policy
) {
639 case IPSEC_POLICY_BYPASS
:
640 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
641 "Illegal policy for non-priviliged defined %d\n",
646 case IPSEC_POLICY_ENTRUST
:
647 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
648 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
650 "fixed system default policy: %d->%d\n",
651 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
652 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
654 ip6_def_policy
.refcnt
++;
656 return &ip6_def_policy
;
658 case IPSEC_POLICY_IPSEC
:
665 "ipsec6_policybysock: Invalid policy for PCB %d\n",
674 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
675 * and return a pointer to SP.
676 * `flag' means that packet is to be forwarded whether or not.
678 * OUT: positive: a pointer to the entry for security policy leaf matched.
679 * NULL: no apropreate SP found, the following value is set to error.
681 * EACCES : discard packet.
682 * ENOENT : ipsec_acquire() in progress, maybe.
683 * others : error occured.
685 #ifndef IP_FORWARDING
686 #define IP_FORWARDING 1
690 ipsec6_getpolicybyaddr(m
, dir
, flag
, error
)
696 struct secpolicy
*sp
= NULL
;
699 if (m
== NULL
|| error
== NULL
)
700 panic("ipsec6_getpolicybyaddr: NULL pointer was passed.\n");
703 struct secpolicyindex spidx
;
705 bzero(&spidx
, sizeof(spidx
));
707 /* make a index to look for a policy */
708 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET6
, m
,
709 (flag
& IP_FORWARDING
) ? 0 : 1);
714 sp
= key_allocsp(&spidx
, dir
);
719 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
720 printf("DP ipsec6_getpolicybyaddr called "
721 "to allocate SP:%p\n", sp
));
727 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
728 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
729 ipseclog((LOG_INFO
, "fixed system default policy: %d->%d\n",
730 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
731 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
733 ip6_def_policy
.refcnt
++;
735 return &ip6_def_policy
;
740 * set IP address into spidx from mbuf.
741 * When Forwarding packet and ICMP echo reply, this function is used.
743 * IN: get the followings from mbuf.
744 * protocol family, src, dst, next protocol
747 * other: failure, and set errno.
750 ipsec_setspidx_mbuf(spidx
, dir
, family
, m
, needport
)
751 struct secpolicyindex
*spidx
;
759 if (spidx
== NULL
|| m
== NULL
)
760 panic("ipsec_setspidx_mbuf: NULL pointer was passed.\n");
762 bzero(spidx
, sizeof(*spidx
));
764 error
= ipsec_setspidx(m
, spidx
, needport
);
773 bzero(spidx
, sizeof(*spidx
));
778 ipsec4_setspidx_inpcb(m
, pcb
)
782 struct secpolicyindex
*spidx
;
785 if (ipsec_bypass
!= 0)
790 panic("ipsec4_setspidx_inpcb: no PCB found.\n");
791 if (pcb
->inp_sp
== NULL
)
792 panic("ipsec4_setspidx_inpcb: no inp_sp found.\n");
793 if (pcb
->inp_sp
->sp_out
== NULL
|| pcb
->inp_sp
->sp_in
== NULL
)
794 panic("ipsec4_setspidx_inpcb: no sp_in/out found.\n");
796 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
797 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
799 spidx
= &pcb
->inp_sp
->sp_in
->spidx
;
800 error
= ipsec_setspidx(m
, spidx
, 1);
803 spidx
->dir
= IPSEC_DIR_INBOUND
;
805 spidx
= &pcb
->inp_sp
->sp_out
->spidx
;
806 error
= ipsec_setspidx(m
, spidx
, 1);
809 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
814 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
815 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
821 ipsec6_setspidx_in6pcb(m
, pcb
)
825 struct secpolicyindex
*spidx
;
830 panic("ipsec6_setspidx_in6pcb: no PCB found.\n");
831 if (pcb
->in6p_sp
== NULL
)
832 panic("ipsec6_setspidx_in6pcb: no in6p_sp found.\n");
833 if (pcb
->in6p_sp
->sp_out
== NULL
|| pcb
->in6p_sp
->sp_in
== NULL
)
834 panic("ipsec6_setspidx_in6pcb: no sp_in/out found.\n");
836 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
837 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
839 spidx
= &pcb
->in6p_sp
->sp_in
->spidx
;
840 error
= ipsec_setspidx(m
, spidx
, 1);
843 spidx
->dir
= IPSEC_DIR_INBOUND
;
845 spidx
= &pcb
->in6p_sp
->sp_out
->spidx
;
846 error
= ipsec_setspidx(m
, spidx
, 1);
849 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
854 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
855 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
861 * configure security policy index (src/dst/proto/sport/dport)
862 * by looking at the content of mbuf.
863 * the caller is responsible for error recovery (like clearing up spidx).
866 ipsec_setspidx(m
, spidx
, needport
)
868 struct secpolicyindex
*spidx
;
871 struct ip
*ip
= NULL
;
879 panic("ipsec_setspidx: m == 0 passed.\n");
882 * validate m->m_pkthdr.len. we see incorrect length if we
883 * mistakenly call this function with inconsistent mbuf chain
884 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
887 for (n
= m
; n
; n
= n
->m_next
)
889 if (m
->m_pkthdr
.len
!= len
) {
890 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
891 printf("ipsec_setspidx: "
892 "total of m_len(%d) != pkthdr.len(%d), "
894 len
, m
->m_pkthdr
.len
));
898 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
899 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
900 printf("ipsec_setspidx: "
901 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
906 if (m
->m_len
>= sizeof(*ip
))
907 ip
= mtod(m
, struct ip
*);
909 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
913 v
= _IP_VHL_V(ip
->ip_vhl
);
919 error
= ipsec4_setspidx_ipaddr(m
, spidx
);
922 ipsec4_get_ulp(m
, spidx
, needport
);
926 if (m
->m_pkthdr
.len
< sizeof(struct ip6_hdr
)) {
927 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
928 printf("ipsec_setspidx: "
929 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
930 "ignored.\n", m
->m_pkthdr
.len
));
933 error
= ipsec6_setspidx_ipaddr(m
, spidx
);
936 ipsec6_get_ulp(m
, spidx
, needport
);
940 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
941 printf("ipsec_setspidx: "
942 "unknown IP version %u, ignored.\n", v
));
948 ipsec4_get_ulp(m
, spidx
, needport
)
950 struct secpolicyindex
*spidx
;
962 panic("ipsec4_get_ulp: NULL pointer was passed.\n");
963 if (m
->m_pkthdr
.len
< sizeof(ip
))
964 panic("ipsec4_get_ulp: too short\n");
967 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
968 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
= IPSEC_PORT_ANY
;
969 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
= IPSEC_PORT_ANY
;
971 m_copydata(m
, 0, sizeof(ip
), (caddr_t
)&ip
);
972 /* ip_input() flips it into host endian XXX need more checking */
973 if (ip
.ip_off
& (IP_MF
| IP_OFFMASK
))
978 off
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
982 while (off
< m
->m_pkthdr
.len
) {
985 spidx
->ul_proto
= nxt
;
988 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
990 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
991 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
993 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
997 spidx
->ul_proto
= nxt
;
1000 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1002 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1003 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
1005 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
1009 if (m
->m_pkthdr
.len
> off
+ sizeof(ip6e
))
1011 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
1012 off
+= (ip6e
.ip6e_len
+ 2) << 2;
1013 nxt
= ip6e
.ip6e_nxt
;
1017 /* XXX intermediate headers??? */
1018 spidx
->ul_proto
= nxt
;
1024 /* assumes that m is sane */
1026 ipsec4_setspidx_ipaddr(m
, spidx
)
1028 struct secpolicyindex
*spidx
;
1030 struct ip
*ip
= NULL
;
1032 struct sockaddr_in
*sin
;
1034 if (m
->m_len
>= sizeof(*ip
))
1035 ip
= mtod(m
, struct ip
*);
1037 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
1041 sin
= (struct sockaddr_in
*)&spidx
->src
;
1042 bzero(sin
, sizeof(*sin
));
1043 sin
->sin_family
= AF_INET
;
1044 sin
->sin_len
= sizeof(struct sockaddr_in
);
1045 bcopy(&ip
->ip_src
, &sin
->sin_addr
, sizeof(ip
->ip_src
));
1046 spidx
->prefs
= sizeof(struct in_addr
) << 3;
1048 sin
= (struct sockaddr_in
*)&spidx
->dst
;
1049 bzero(sin
, sizeof(*sin
));
1050 sin
->sin_family
= AF_INET
;
1051 sin
->sin_len
= sizeof(struct sockaddr_in
);
1052 bcopy(&ip
->ip_dst
, &sin
->sin_addr
, sizeof(ip
->ip_dst
));
1053 spidx
->prefd
= sizeof(struct in_addr
) << 3;
1059 ipsec6_get_ulp(m
, spidx
, needport
)
1061 struct secpolicyindex
*spidx
;
1070 panic("ipsec6_get_ulp: NULL pointer was passed.\n");
1072 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1073 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m
));
1076 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
1077 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= IPSEC_PORT_ANY
;
1078 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= IPSEC_PORT_ANY
;
1081 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
1082 if (off
< 0 || m
->m_pkthdr
.len
< off
)
1087 spidx
->ul_proto
= nxt
;
1090 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
1092 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
1093 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= th
.th_sport
;
1094 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= th
.th_dport
;
1097 spidx
->ul_proto
= nxt
;
1100 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1102 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1103 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= uh
.uh_sport
;
1104 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= uh
.uh_dport
;
1106 case IPPROTO_ICMPV6
:
1108 /* XXX intermediate headers??? */
1109 spidx
->ul_proto
= nxt
;
1114 /* assumes that m is sane */
1116 ipsec6_setspidx_ipaddr(m
, spidx
)
1118 struct secpolicyindex
*spidx
;
1120 struct ip6_hdr
*ip6
= NULL
;
1121 struct ip6_hdr ip6buf
;
1122 struct sockaddr_in6
*sin6
;
1124 if (m
->m_len
>= sizeof(*ip6
))
1125 ip6
= mtod(m
, struct ip6_hdr
*);
1127 m_copydata(m
, 0, sizeof(ip6buf
), (caddr_t
)&ip6buf
);
1131 sin6
= (struct sockaddr_in6
*)&spidx
->src
;
1132 bzero(sin6
, sizeof(*sin6
));
1133 sin6
->sin6_family
= AF_INET6
;
1134 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1135 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_src
));
1136 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1137 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1138 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
1140 spidx
->prefs
= sizeof(struct in6_addr
) << 3;
1142 sin6
= (struct sockaddr_in6
*)&spidx
->dst
;
1143 bzero(sin6
, sizeof(*sin6
));
1144 sin6
->sin6_family
= AF_INET6
;
1145 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1146 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_dst
));
1147 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
1148 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1149 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
1151 spidx
->prefd
= sizeof(struct in6_addr
) << 3;
1157 static struct inpcbpolicy
*
1158 ipsec_newpcbpolicy()
1160 struct inpcbpolicy
*p
;
1162 p
= (struct inpcbpolicy
*)_MALLOC(sizeof(*p
), M_SECA
, M_WAITOK
);
1167 ipsec_delpcbpolicy(p
)
1168 struct inpcbpolicy
*p
;
1173 /* initialize policy in PCB */
1175 ipsec_init_policy(so
, pcb_sp
)
1177 struct inpcbpolicy
**pcb_sp
;
1179 struct inpcbpolicy
*new;
1182 if (so
== NULL
|| pcb_sp
== NULL
)
1183 panic("ipsec_init_policy: NULL pointer was passed.\n");
1185 new = ipsec_newpcbpolicy();
1187 ipseclog((LOG_DEBUG
, "ipsec_init_policy: No more memory.\n"));
1190 bzero(new, sizeof(*new));
1193 if (so
->so_uid
== 0)
1195 if (so
->so_cred
!= 0 && so
->so_cred
->pc_ucred
->cr_uid
== 0)
1201 if ((new->sp_in
= key_newsp()) == NULL
) {
1202 ipsec_delpcbpolicy(new);
1205 new->sp_in
->state
= IPSEC_SPSTATE_ALIVE
;
1206 new->sp_in
->policy
= IPSEC_POLICY_ENTRUST
;
1208 if ((new->sp_out
= key_newsp()) == NULL
) {
1209 key_freesp(new->sp_in
);
1210 ipsec_delpcbpolicy(new);
1213 new->sp_out
->state
= IPSEC_SPSTATE_ALIVE
;
1214 new->sp_out
->policy
= IPSEC_POLICY_ENTRUST
;
1221 /* copy old ipsec policy into new */
1223 ipsec_copy_policy(old
, new)
1224 struct inpcbpolicy
*old
, *new;
1226 struct secpolicy
*sp
;
1228 if (ipsec_bypass
!= 0)
1231 sp
= ipsec_deepcopy_policy(old
->sp_in
);
1233 key_freesp(new->sp_in
);
1238 sp
= ipsec_deepcopy_policy(old
->sp_out
);
1240 key_freesp(new->sp_out
);
1245 new->priv
= old
->priv
;
1250 /* deep-copy a policy in PCB */
1251 static struct secpolicy
*
1252 ipsec_deepcopy_policy(src
)
1253 struct secpolicy
*src
;
1255 struct ipsecrequest
*newchain
= NULL
;
1256 struct ipsecrequest
*p
;
1257 struct ipsecrequest
**q
;
1258 struct ipsecrequest
*r
;
1259 struct secpolicy
*dst
;
1262 if (src
== NULL
|| dst
== NULL
)
1266 * deep-copy IPsec request chain. This is required since struct
1267 * ipsecrequest is not reference counted.
1270 for (p
= src
->req
; p
; p
= p
->next
) {
1271 *q
= (struct ipsecrequest
*)_MALLOC(sizeof(struct ipsecrequest
),
1275 bzero(*q
, sizeof(**q
));
1278 (*q
)->saidx
.proto
= p
->saidx
.proto
;
1279 (*q
)->saidx
.mode
= p
->saidx
.mode
;
1280 (*q
)->level
= p
->level
;
1281 (*q
)->saidx
.reqid
= p
->saidx
.reqid
;
1283 bcopy(&p
->saidx
.src
, &(*q
)->saidx
.src
, sizeof((*q
)->saidx
.src
));
1284 bcopy(&p
->saidx
.dst
, &(*q
)->saidx
.dst
, sizeof((*q
)->saidx
.dst
));
1292 dst
->req
= newchain
;
1293 dst
->state
= src
->state
;
1294 dst
->policy
= src
->policy
;
1295 /* do not touch the refcnt fields */
1300 for (p
= newchain
; p
; p
= r
) {
1308 /* set policy and ipsec request if present. */
1310 ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
)
1311 struct secpolicy
**pcb_sp
;
1317 struct sadb_x_policy
*xpl
;
1318 struct secpolicy
*newsp
= NULL
;
1322 if (pcb_sp
== NULL
|| *pcb_sp
== NULL
|| request
== NULL
)
1324 if (len
< sizeof(*xpl
))
1326 xpl
= (struct sadb_x_policy
*)request
;
1328 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1329 printf("ipsec_set_policy: passed policy\n");
1330 kdebug_sadb_x_policy((struct sadb_ext
*)xpl
));
1332 /* check policy type */
1333 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1334 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
1335 || xpl
->sadb_x_policy_type
== IPSEC_POLICY_NONE
)
1338 /* check privileged socket */
1339 if (priv
== 0 && xpl
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
)
1342 /* allocation new SP entry */
1343 if ((newsp
= key_msg2sp(xpl
, len
, &error
)) == NULL
)
1346 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1348 /* clear old SP and set new SP */
1349 key_freesp(*pcb_sp
);
1351 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1352 printf("ipsec_set_policy: new policy\n");
1353 kdebug_secpolicy(newsp
));
1359 ipsec_get_policy(pcb_sp
, mp
)
1360 struct secpolicy
*pcb_sp
;
1365 if (pcb_sp
== NULL
|| mp
== NULL
)
1368 *mp
= key_sp2msg(pcb_sp
);
1370 ipseclog((LOG_DEBUG
, "ipsec_get_policy: No more memory.\n"));
1374 (*mp
)->m_type
= MT_DATA
;
1375 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1376 printf("ipsec_get_policy:\n");
1383 ipsec4_set_policy(inp
, optname
, request
, len
, priv
)
1390 struct sadb_x_policy
*xpl
;
1391 struct secpolicy
**pcb_sp
;
1395 if (inp
== NULL
|| request
== NULL
)
1397 if (len
< sizeof(*xpl
))
1399 xpl
= (struct sadb_x_policy
*)request
;
1401 if (inp
->inp_sp
== NULL
) {
1402 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1407 /* select direction */
1408 switch (xpl
->sadb_x_policy_dir
) {
1409 case IPSEC_DIR_INBOUND
:
1410 pcb_sp
= &inp
->inp_sp
->sp_in
;
1412 case IPSEC_DIR_OUTBOUND
:
1413 pcb_sp
= &inp
->inp_sp
->sp_out
;
1416 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1417 xpl
->sadb_x_policy_dir
));
1421 /* turn bypass off */
1422 if (ipsec_bypass
!= 0)
1425 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1429 ipsec4_get_policy(inp
, request
, len
, mp
)
1435 struct sadb_x_policy
*xpl
;
1436 struct secpolicy
*pcb_sp
;
1440 if (inp
== NULL
|| request
== NULL
|| mp
== NULL
)
1442 if (len
< sizeof(*xpl
))
1444 xpl
= (struct sadb_x_policy
*)request
;
1446 if (inp
->inp_sp
== NULL
) {
1447 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1452 /* select direction */
1453 switch (xpl
->sadb_x_policy_dir
) {
1454 case IPSEC_DIR_INBOUND
:
1455 pcb_sp
= inp
->inp_sp
->sp_in
;
1457 case IPSEC_DIR_OUTBOUND
:
1458 pcb_sp
= inp
->inp_sp
->sp_out
;
1461 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1462 xpl
->sadb_x_policy_dir
));
1466 return ipsec_get_policy(pcb_sp
, mp
);
1469 /* delete policy in PCB */
1471 ipsec4_delete_pcbpolicy(inp
)
1476 panic("ipsec4_delete_pcbpolicy: NULL pointer was passed.\n");
1478 if (inp
->inp_sp
== NULL
)
1481 if (inp
->inp_sp
->sp_in
!= NULL
) {
1482 key_freesp(inp
->inp_sp
->sp_in
);
1483 inp
->inp_sp
->sp_in
= NULL
;
1486 if (inp
->inp_sp
->sp_out
!= NULL
) {
1487 key_freesp(inp
->inp_sp
->sp_out
);
1488 inp
->inp_sp
->sp_out
= NULL
;
1491 ipsec_delpcbpolicy(inp
->inp_sp
);
1499 ipsec6_set_policy(in6p
, optname
, request
, len
, priv
)
1500 struct in6pcb
*in6p
;
1506 struct sadb_x_policy
*xpl
;
1507 struct secpolicy
**pcb_sp
;
1511 if (in6p
== NULL
|| request
== NULL
)
1513 if (len
< sizeof(*xpl
))
1515 xpl
= (struct sadb_x_policy
*)request
;
1517 if (in6p
->in6p_sp
== NULL
) {
1518 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1523 /* select direction */
1524 switch (xpl
->sadb_x_policy_dir
) {
1525 case IPSEC_DIR_INBOUND
:
1526 pcb_sp
= &in6p
->in6p_sp
->sp_in
;
1528 case IPSEC_DIR_OUTBOUND
:
1529 pcb_sp
= &in6p
->in6p_sp
->sp_out
;
1532 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1533 xpl
->sadb_x_policy_dir
));
1537 /* turn bypass off */
1538 if (ipsec_bypass
!= 0)
1541 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1545 ipsec6_get_policy(in6p
, request
, len
, mp
)
1546 struct in6pcb
*in6p
;
1551 struct sadb_x_policy
*xpl
;
1552 struct secpolicy
*pcb_sp
;
1556 if (in6p
== NULL
|| request
== NULL
|| mp
== NULL
)
1558 if (len
< sizeof(*xpl
))
1560 xpl
= (struct sadb_x_policy
*)request
;
1562 if (in6p
->in6p_sp
== NULL
) {
1563 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1568 /* select direction */
1569 switch (xpl
->sadb_x_policy_dir
) {
1570 case IPSEC_DIR_INBOUND
:
1571 pcb_sp
= in6p
->in6p_sp
->sp_in
;
1573 case IPSEC_DIR_OUTBOUND
:
1574 pcb_sp
= in6p
->in6p_sp
->sp_out
;
1577 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1578 xpl
->sadb_x_policy_dir
));
1582 return ipsec_get_policy(pcb_sp
, mp
);
1586 ipsec6_delete_pcbpolicy(in6p
)
1587 struct in6pcb
*in6p
;
1591 panic("ipsec6_delete_pcbpolicy: NULL pointer was passed.\n");
1593 if (in6p
->in6p_sp
== NULL
)
1596 if (in6p
->in6p_sp
->sp_in
!= NULL
) {
1597 key_freesp(in6p
->in6p_sp
->sp_in
);
1598 in6p
->in6p_sp
->sp_in
= NULL
;
1601 if (in6p
->in6p_sp
->sp_out
!= NULL
) {
1602 key_freesp(in6p
->in6p_sp
->sp_out
);
1603 in6p
->in6p_sp
->sp_out
= NULL
;
1606 ipsec_delpcbpolicy(in6p
->in6p_sp
);
1607 in6p
->in6p_sp
= NULL
;
1614 * return current level.
1615 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1618 ipsec_get_reqlevel(isr
)
1619 struct ipsecrequest
*isr
;
1622 u_int esp_trans_deflev
, esp_net_deflev
, ah_trans_deflev
, ah_net_deflev
;
1625 if (isr
== NULL
|| isr
->sp
== NULL
)
1626 panic("ipsec_get_reqlevel: NULL pointer is passed.\n");
1627 if (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
1628 != ((struct sockaddr
*)&isr
->sp
->spidx
.dst
)->sa_family
)
1629 panic("ipsec_get_reqlevel: family mismatched.\n");
1631 /* XXX note that we have ipseclog() expanded here - code sync issue */
1632 #define IPSEC_CHECK_DEFAULT(lev) \
1633 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1634 && (lev) != IPSEC_LEVEL_UNIQUE) \
1636 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1637 (lev), IPSEC_LEVEL_REQUIRE) \
1639 (lev) = IPSEC_LEVEL_REQUIRE, \
1643 /* set default level */
1644 switch (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
) {
1647 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev
);
1648 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev
);
1649 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev
);
1650 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev
);
1655 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev
);
1656 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev
);
1657 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev
);
1658 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev
);
1662 panic("key_get_reqlevel: Unknown family. %d\n",
1663 ((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
);
1666 #undef IPSEC_CHECK_DEFAULT
1669 switch (isr
->level
) {
1670 case IPSEC_LEVEL_DEFAULT
:
1671 switch (isr
->saidx
.proto
) {
1673 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1674 level
= esp_net_deflev
;
1676 level
= esp_trans_deflev
;
1679 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1680 level
= ah_net_deflev
;
1682 level
= ah_trans_deflev
;
1683 case IPPROTO_IPCOMP
:
1685 * we don't really care, as IPcomp document says that
1686 * we shouldn't compress small packets
1688 level
= IPSEC_LEVEL_USE
;
1691 panic("ipsec_get_reqlevel: "
1692 "Illegal protocol defined %u\n",
1697 case IPSEC_LEVEL_USE
:
1698 case IPSEC_LEVEL_REQUIRE
:
1701 case IPSEC_LEVEL_UNIQUE
:
1702 level
= IPSEC_LEVEL_REQUIRE
;
1706 panic("ipsec_get_reqlevel: Illegal IPsec level %u\n",
1714 * Check AH/ESP integrity.
1720 ipsec_in_reject(sp
, m
)
1721 struct secpolicy
*sp
;
1724 struct ipsecrequest
*isr
;
1726 int need_auth
, need_conf
, need_icv
;
1728 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1729 printf("ipsec_in_reject: using SP\n");
1730 kdebug_secpolicy(sp
));
1733 switch (sp
->policy
) {
1734 case IPSEC_POLICY_DISCARD
:
1736 case IPSEC_POLICY_BYPASS
:
1737 case IPSEC_POLICY_NONE
:
1740 case IPSEC_POLICY_IPSEC
:
1743 case IPSEC_POLICY_ENTRUST
:
1745 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1752 /* XXX should compare policy against ipsec header history */
1754 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1756 /* get current level */
1757 level
= ipsec_get_reqlevel(isr
);
1759 switch (isr
->saidx
.proto
) {
1761 if (level
== IPSEC_LEVEL_REQUIRE
) {
1764 if (isr
->sav
!= NULL
1765 && isr
->sav
->flags
== SADB_X_EXT_NONE
1766 && isr
->sav
->alg_auth
!= SADB_AALG_NONE
)
1771 if (level
== IPSEC_LEVEL_REQUIRE
) {
1776 case IPPROTO_IPCOMP
:
1778 * we don't really care, as IPcomp document says that
1779 * we shouldn't compress small packets, IPComp policy
1780 * should always be treated as being in "use" level.
1786 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1787 printf("ipsec_in_reject: auth:%d conf:%d icv:%d m_flags:%x\n",
1788 need_auth
, need_conf
, need_icv
, m
->m_flags
));
1790 if ((need_conf
&& !(m
->m_flags
& M_DECRYPTED
))
1791 || (!need_auth
&& need_icv
&& !(m
->m_flags
& M_AUTHIPDGM
))
1792 || (need_auth
&& !(m
->m_flags
& M_AUTHIPHDR
)))
1799 * Check AH/ESP integrity.
1800 * This function is called from tcp_input(), udp_input(),
1801 * and {ah,esp}4_input for tunnel mode
1804 ipsec4_in_reject_so(m
, so
)
1808 struct secpolicy
*sp
= NULL
;
1814 return 0; /* XXX should be panic ? */
1816 /* get SP for this packet.
1817 * When we are called from ip_forward(), we call
1818 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1821 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1823 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1826 return 0; /* XXX should be panic ?
1827 * -> No, there may be error. */
1829 result
= ipsec_in_reject(sp
, m
);
1830 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1831 printf("DP ipsec4_in_reject_so call free SP:%p\n", sp
));
1838 ipsec4_in_reject(m
, inp
)
1843 return ipsec4_in_reject_so(m
, NULL
);
1844 if (inp
->inp_socket
)
1845 return ipsec4_in_reject_so(m
, inp
->inp_socket
);
1847 panic("ipsec4_in_reject: invalid inpcb/socket");
1852 * Check AH/ESP integrity.
1853 * This function is called from tcp6_input(), udp6_input(),
1854 * and {ah,esp}6_input for tunnel mode
1857 ipsec6_in_reject_so(m
, so
)
1861 struct secpolicy
*sp
= NULL
;
1867 return 0; /* XXX should be panic ? */
1869 /* get SP for this packet.
1870 * When we are called from ip_forward(), we call
1871 * ipsec6_getpolicybyaddr() with IP_FORWARDING flag.
1874 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1876 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1879 return 0; /* XXX should be panic ? */
1881 result
= ipsec_in_reject(sp
, m
);
1882 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1883 printf("DP ipsec6_in_reject_so call free SP:%p\n", sp
));
1890 ipsec6_in_reject(m
, in6p
)
1892 struct in6pcb
*in6p
;
1895 return ipsec6_in_reject_so(m
, NULL
);
1896 if (in6p
->in6p_socket
)
1897 return ipsec6_in_reject_so(m
, in6p
->in6p_socket
);
1899 panic("ipsec6_in_reject: invalid in6p/socket");
1904 * compute the byte size to be occupied by IPsec header.
1905 * in case it is tunneled, it includes the size of outer IP header.
1906 * NOTE: SP passed is free in this function.
1910 struct secpolicy
*sp
;
1912 struct ipsecrequest
*isr
;
1915 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1916 printf("ipsec_hdrsiz: using SP\n");
1917 kdebug_secpolicy(sp
));
1920 switch (sp
->policy
) {
1921 case IPSEC_POLICY_DISCARD
:
1922 case IPSEC_POLICY_BYPASS
:
1923 case IPSEC_POLICY_NONE
:
1926 case IPSEC_POLICY_IPSEC
:
1929 case IPSEC_POLICY_ENTRUST
:
1931 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1936 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1940 switch (isr
->saidx
.proto
) {
1943 clen
= esp_hdrsiz(isr
);
1949 clen
= ah_hdrsiz(isr
);
1951 case IPPROTO_IPCOMP
:
1952 clen
= sizeof(struct ipcomp
);
1956 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
1957 switch (((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
) {
1959 clen
+= sizeof(struct ip
);
1963 clen
+= sizeof(struct ip6_hdr
);
1967 ipseclog((LOG_ERR
, "ipsec_hdrsiz: "
1968 "unknown AF %d in IPsec tunnel SA\n",
1969 ((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
));
1979 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1981 ipsec4_hdrsiz(m
, dir
, inp
)
1986 struct secpolicy
*sp
= NULL
;
1992 return 0; /* XXX should be panic ? */
1993 if (inp
!= NULL
&& inp
->inp_socket
== NULL
)
1994 panic("ipsec4_hdrsize: why is socket NULL but there is PCB.");
1996 /* get SP for this packet.
1997 * When we are called from ip_forward(), we call
1998 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
2001 sp
= ipsec4_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2003 sp
= ipsec4_getpolicybysock(m
, dir
, inp
->inp_socket
, &error
);
2006 return 0; /* XXX should be panic ? */
2008 size
= ipsec_hdrsiz(sp
);
2009 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2010 printf("DP ipsec4_hdrsiz call free SP:%p\n", sp
));
2011 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2012 printf("ipsec4_hdrsiz: size:%lu.\n", (unsigned long)size
));
2019 /* This function is called from ipsec6_hdrsize_tcp(),
2020 * and maybe from ip6_forward.()
2023 ipsec6_hdrsiz(m
, dir
, in6p
)
2026 struct in6pcb
*in6p
;
2028 struct secpolicy
*sp
= NULL
;
2034 return 0; /* XXX shoud be panic ? */
2035 if (in6p
!= NULL
&& in6p
->in6p_socket
== NULL
)
2036 panic("ipsec6_hdrsize: why is socket NULL but there is PCB.");
2038 /* get SP for this packet */
2039 /* XXX Is it right to call with IP_FORWARDING. */
2041 sp
= ipsec6_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2043 sp
= ipsec6_getpolicybysock(m
, dir
, in6p
->in6p_socket
, &error
);
2047 size
= ipsec_hdrsiz(sp
);
2048 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2049 printf("DP ipsec6_hdrsiz call free SP:%p\n", sp
));
2050 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2051 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size
));
2060 * encapsulate for ipsec tunnel.
2061 * ip->ip_src must be fixed later on.
2064 ipsec4_encapsulate(m
, sav
)
2066 struct secasvar
*sav
;
2073 /* can't tunnel between different AFs */
2074 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2075 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2076 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2081 /* XXX if the dst is myself, perform nothing. */
2082 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2088 if (m
->m_len
< sizeof(*ip
))
2089 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2091 ip
= mtod(m
, struct ip
*);
2093 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
2095 hlen
= ip
->ip_hl
<< 2;
2098 if (m
->m_len
!= hlen
)
2099 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2101 /* generate header checksum */
2104 ip
->ip_sum
= in_cksum(m
, hlen
);
2106 ip
->ip_sum
= in_cksum(m
, hlen
);
2109 plen
= m
->m_pkthdr
.len
;
2112 * grow the mbuf to accomodate the new IPv4 header.
2113 * NOTE: IPv4 options will never be copied.
2115 if (M_LEADINGSPACE(m
->m_next
) < hlen
) {
2117 MGET(n
, M_DONTWAIT
, MT_DATA
);
2123 n
->m_next
= m
->m_next
;
2125 m
->m_pkthdr
.len
+= hlen
;
2126 oip
= mtod(n
, struct ip
*);
2128 m
->m_next
->m_len
+= hlen
;
2129 m
->m_next
->m_data
-= hlen
;
2130 m
->m_pkthdr
.len
+= hlen
;
2131 oip
= mtod(m
->m_next
, struct ip
*);
2133 ip
= mtod(m
, struct ip
*);
2134 ovbcopy((caddr_t
)ip
, (caddr_t
)oip
, hlen
);
2135 m
->m_len
= sizeof(struct ip
);
2136 m
->m_pkthdr
.len
-= (hlen
- sizeof(struct ip
));
2138 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
2139 /* ECN consideration. */
2140 ip_ecn_ingress(ip4_ipsec_ecn
, &ip
->ip_tos
, &oip
->ip_tos
);
2142 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2144 ip
->ip_hl
= sizeof(struct ip
) >> 2;
2146 ip
->ip_off
&= htons(~IP_OFFMASK
);
2147 ip
->ip_off
&= htons(~IP_MF
);
2148 switch (ip4_ipsec_dfbit
) {
2149 case 0: /* clear DF bit */
2150 ip
->ip_off
&= htons(~IP_DF
);
2152 case 1: /* set DF bit */
2153 ip
->ip_off
|= htons(IP_DF
);
2155 default: /* copy DF bit */
2158 ip
->ip_p
= IPPROTO_IPIP
;
2159 if (plen
+ sizeof(struct ip
) < IP_MAXPACKET
)
2160 ip
->ip_len
= htons(plen
+ sizeof(struct ip
));
2162 ipseclog((LOG_ERR
, "IPv4 ipsec: size exceeds limit: "
2163 "leave ip_len as is (invalid packet)\n"));
2166 ip
->ip_id
= ip_randomid();
2168 ip
->ip_id
= htons(ip_id
++);
2170 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
,
2171 &ip
->ip_src
, sizeof(ip
->ip_src
));
2172 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
,
2173 &ip
->ip_dst
, sizeof(ip
->ip_dst
));
2174 ip
->ip_ttl
= IPDEFTTL
;
2176 /* XXX Should ip_src be updated later ? */
2184 ipsec6_encapsulate(m
, sav
)
2186 struct secasvar
*sav
;
2188 struct ip6_hdr
*oip6
;
2189 struct ip6_hdr
*ip6
;
2192 /* can't tunnel between different AFs */
2193 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2194 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2195 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
2200 /* XXX if the dst is myself, perform nothing. */
2201 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2207 plen
= m
->m_pkthdr
.len
;
2210 * grow the mbuf to accomodate the new IPv6 header.
2212 if (m
->m_len
!= sizeof(struct ip6_hdr
))
2213 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2214 if (M_LEADINGSPACE(m
->m_next
) < sizeof(struct ip6_hdr
)) {
2216 MGET(n
, M_DONTWAIT
, MT_DATA
);
2221 n
->m_len
= sizeof(struct ip6_hdr
);
2222 n
->m_next
= m
->m_next
;
2224 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2225 oip6
= mtod(n
, struct ip6_hdr
*);
2227 m
->m_next
->m_len
+= sizeof(struct ip6_hdr
);
2228 m
->m_next
->m_data
-= sizeof(struct ip6_hdr
);
2229 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2230 oip6
= mtod(m
->m_next
, struct ip6_hdr
*);
2232 ip6
= mtod(m
, struct ip6_hdr
*);
2233 ovbcopy((caddr_t
)ip6
, (caddr_t
)oip6
, sizeof(struct ip6_hdr
));
2235 /* Fake link-local scope-class addresses */
2236 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
2237 oip6
->ip6_src
.s6_addr16
[1] = 0;
2238 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
2239 oip6
->ip6_dst
.s6_addr16
[1] = 0;
2241 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
2242 /* ECN consideration. */
2243 ip6_ecn_ingress(ip6_ipsec_ecn
, &ip6
->ip6_flow
, &oip6
->ip6_flow
);
2244 if (plen
< IPV6_MAXPACKET
- sizeof(struct ip6_hdr
))
2245 ip6
->ip6_plen
= htons(plen
);
2247 /* ip6->ip6_plen will be updated in ip6_output() */
2249 ip6
->ip6_nxt
= IPPROTO_IPV6
;
2250 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.src
)->sin6_addr
,
2251 &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
2252 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
)->sin6_addr
,
2253 &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
2254 ip6
->ip6_hlim
= IPV6_DEFHLIM
;
2256 /* XXX Should ip6_src be updated later ? */
2263 * Check the variable replay window.
2264 * ipsec_chkreplay() performs replay check before ICV verification.
2265 * ipsec_updatereplay() updates replay bitmap. This must be called after
2266 * ICV verification (it also performs replay check, which is usually done
2268 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2270 * based on RFC 2401.
2273 ipsec_chkreplay(seq
, sav
)
2275 struct secasvar
*sav
;
2277 const struct secreplay
*replay
;
2280 u_int32_t wsizeb
; /* constant: bits of window size */
2281 int frlast
; /* constant: last frame */
2285 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2287 replay
= sav
->replay
;
2289 if (replay
->wsize
== 0)
2290 return 1; /* no need to check replay. */
2293 frlast
= replay
->wsize
- 1;
2294 wsizeb
= replay
->wsize
<< 3;
2296 /* sequence number of 0 is invalid */
2300 /* first time is always okay */
2301 if (replay
->count
== 0)
2304 if (seq
> replay
->lastseq
) {
2305 /* larger sequences are okay */
2308 /* seq is equal or less than lastseq. */
2309 diff
= replay
->lastseq
- seq
;
2311 /* over range to check, i.e. too old or wrapped */
2315 fr
= frlast
- diff
/ 8;
2317 /* this packet already seen ? */
2318 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2321 /* out of order but good */
2327 * check replay counter whether to update or not.
2332 ipsec_updatereplay(seq
, sav
)
2334 struct secasvar
*sav
;
2336 struct secreplay
*replay
;
2339 u_int32_t wsizeb
; /* constant: bits of window size */
2340 int frlast
; /* constant: last frame */
2344 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2346 replay
= sav
->replay
;
2348 if (replay
->wsize
== 0)
2349 goto ok
; /* no need to check replay. */
2352 frlast
= replay
->wsize
- 1;
2353 wsizeb
= replay
->wsize
<< 3;
2355 /* sequence number of 0 is invalid */
2360 if (replay
->count
== 0) {
2361 replay
->lastseq
= seq
;
2362 bzero(replay
->bitmap
, replay
->wsize
);
2363 (replay
->bitmap
)[frlast
] = 1;
2367 if (seq
> replay
->lastseq
) {
2368 /* seq is larger than lastseq. */
2369 diff
= seq
- replay
->lastseq
;
2371 /* new larger sequence number */
2372 if (diff
< wsizeb
) {
2374 /* set bit for this packet */
2375 vshiftl(replay
->bitmap
, diff
, replay
->wsize
);
2376 (replay
->bitmap
)[frlast
] |= 1;
2378 /* this packet has a "way larger" */
2379 bzero(replay
->bitmap
, replay
->wsize
);
2380 (replay
->bitmap
)[frlast
] = 1;
2382 replay
->lastseq
= seq
;
2384 /* larger is good */
2386 /* seq is equal or less than lastseq. */
2387 diff
= replay
->lastseq
- seq
;
2389 /* over range to check, i.e. too old or wrapped */
2393 fr
= frlast
- diff
/ 8;
2395 /* this packet already seen ? */
2396 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2400 (replay
->bitmap
)[fr
] |= (1 << (diff
% 8));
2402 /* out of order but good */
2406 if (replay
->count
== ~0) {
2408 /* set overflow flag */
2411 /* don't increment, no more packets accepted */
2412 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0)
2415 ipseclog((LOG_WARNING
, "replay counter made %d cycle. %s\n",
2416 replay
->overflow
, ipsec_logsastr(sav
)));
2425 * shift variable length buffer to left.
2426 * IN: bitmap: pointer to the buffer
2427 * nbit: the number of to shift.
2428 * wsize: buffer size (bytes).
2431 vshiftl(bitmap
, nbit
, wsize
)
2432 unsigned char *bitmap
;
2438 for (j
= 0; j
< nbit
; j
+= 8) {
2439 s
= (nbit
- j
< 8) ? (nbit
- j
): 8;
2441 for (i
= 1; i
< wsize
; i
++) {
2442 over
= (bitmap
[i
] >> (8 - s
));
2444 bitmap
[i
-1] |= over
;
2452 ipsec4_logpacketstr(ip
, spi
)
2456 static char buf
[256];
2460 s
= (u_int8_t
*)(&ip
->ip_src
);
2461 d
= (u_int8_t
*)(&ip
->ip_dst
);
2464 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2467 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%u.%u.%u.%u",
2468 s
[0], s
[1], s
[2], s
[3]);
2471 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%u.%u.%u.%u",
2472 d
[0], d
[1], d
[2], d
[3]);
2475 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2482 ipsec6_logpacketstr(ip6
, spi
)
2483 struct ip6_hdr
*ip6
;
2486 static char buf
[256];
2490 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2493 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%s",
2494 ip6_sprintf(&ip6
->ip6_src
));
2497 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%s",
2498 ip6_sprintf(&ip6
->ip6_dst
));
2501 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2509 struct secasvar
*sav
;
2511 static char buf
[256];
2513 struct secasindex
*saidx
= &sav
->sah
->saidx
;
2515 /* validity check */
2516 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2517 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
)
2518 panic("ipsec_logsastr: family mismatched.\n");
2521 snprintf(buf
, sizeof(buf
), "SA(SPI=%u ", (u_int32_t
)ntohl(sav
->spi
));
2524 if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET
) {
2526 s
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->src
)->sin_addr
;
2527 d
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->dst
)->sin_addr
;
2528 snprintf(p
, sizeof(buf
) - (p
- buf
),
2529 "src=%d.%d.%d.%d dst=%d.%d.%d.%d",
2530 s
[0], s
[1], s
[2], s
[3], d
[0], d
[1], d
[2], d
[3]);
2533 else if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET6
) {
2534 snprintf(p
, sizeof(buf
) - (p
- buf
),
2536 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->src
)->sin6_addr
));
2539 snprintf(p
, sizeof(buf
) - (p
- buf
),
2541 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->dst
)->sin6_addr
));
2546 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2562 p
= mtod(m
, u_char
*);
2563 for (i
= 0; i
< m
->m_len
; i
++) {
2564 printf("%02x ", p
[i
]);
2566 if (totlen
% 16 == 0)
2571 if (totlen
% 16 != 0)
2578 * IPsec output logic for IPv4.
2581 ipsec4_output(state
, sp
, flags
)
2582 struct ipsec_output_state
*state
;
2583 struct secpolicy
*sp
;
2586 struct ip
*ip
= NULL
;
2587 struct ipsecrequest
*isr
= NULL
;
2588 struct secasindex saidx
;
2591 struct sockaddr_in
*dst4
;
2592 struct sockaddr_in
*sin
;
2595 panic("state == NULL in ipsec4_output");
2597 panic("state->m == NULL in ipsec4_output");
2599 panic("state->ro == NULL in ipsec4_output");
2601 panic("state->dst == NULL in ipsec4_output");
2603 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_START
, 0,0,0,0,0);
2605 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2606 printf("ipsec4_output: applyed SP\n");
2607 kdebug_secpolicy(sp
));
2609 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2611 #if 0 /* give up to check restriction of transport mode */
2612 /* XXX but should be checked somewhere */
2614 * some of the IPsec operation must be performed only in
2617 if (isr
->saidx
.mode
== IPSEC_MODE_TRANSPORT
2618 && (flags
& IP_FORWARDING
))
2622 /* make SA index for search proper SA */
2623 ip
= mtod(state
->m
, struct ip
*);
2624 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2625 saidx
.mode
= isr
->saidx
.mode
;
2626 saidx
.reqid
= isr
->saidx
.reqid
;
2627 sin
= (struct sockaddr_in
*)&saidx
.src
;
2628 if (sin
->sin_len
== 0) {
2629 sin
->sin_len
= sizeof(*sin
);
2630 sin
->sin_family
= AF_INET
;
2631 sin
->sin_port
= IPSEC_PORT_ANY
;
2632 bcopy(&ip
->ip_src
, &sin
->sin_addr
,
2633 sizeof(sin
->sin_addr
));
2635 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2636 if (sin
->sin_len
== 0) {
2637 sin
->sin_len
= sizeof(*sin
);
2638 sin
->sin_family
= AF_INET
;
2639 sin
->sin_port
= IPSEC_PORT_ANY
;
2640 bcopy(&ip
->ip_dst
, &sin
->sin_addr
,
2641 sizeof(sin
->sin_addr
));
2644 if ((error
= key_checkrequest(isr
, &saidx
)) != 0) {
2646 * IPsec processing is required, but no SA found.
2647 * I assume that key_acquire() had been called
2648 * to get/establish the SA. Here I discard
2649 * this packet because it is responsibility for
2650 * upper layer to retransmit the packet.
2652 ipsecstat
.out_nosa
++;
2656 /* validity check */
2657 if (isr
->sav
== NULL
) {
2658 switch (ipsec_get_reqlevel(isr
)) {
2659 case IPSEC_LEVEL_USE
:
2661 case IPSEC_LEVEL_REQUIRE
:
2662 /* must be not reached here. */
2663 panic("ipsec4_output: no SA found, but required.");
2668 * If there is no valid SA, we give up to process any
2669 * more. In such a case, the SA's status is changed
2670 * from DYING to DEAD after allocating. If a packet
2671 * send to the receiver by dead SA, the receiver can
2672 * not decode a packet because SA has been dead.
2674 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2675 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2676 ipsecstat
.out_nosa
++;
2682 * There may be the case that SA status will be changed when
2683 * we are refering to one. So calling splsoftnet().
2687 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2689 * build IPsec tunnel.
2691 /* XXX should be processed with other familiy */
2692 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2693 ipseclog((LOG_ERR
, "ipsec4_output: "
2694 "family mismatched between inner and outer spi=%u\n",
2695 (u_int32_t
)ntohl(isr
->sav
->spi
)));
2697 error
= EAFNOSUPPORT
;
2701 state
->m
= ipsec4_splithdr(state
->m
);
2707 error
= ipsec4_encapsulate(state
->m
, isr
->sav
);
2713 ip
= mtod(state
->m
, struct ip
*);
2715 state
->ro
= &isr
->sav
->sah
->sa_route
;
2716 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
2717 dst4
= (struct sockaddr_in
*)state
->dst
;
2718 if (state
->ro
->ro_rt
2719 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
2720 || dst4
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
2721 rtfree(state
->ro
->ro_rt
);
2722 state
->ro
->ro_rt
= NULL
;
2724 if (state
->ro
->ro_rt
== 0) {
2725 dst4
->sin_family
= AF_INET
;
2726 dst4
->sin_len
= sizeof(*dst4
);
2727 dst4
->sin_addr
= ip
->ip_dst
;
2730 if (state
->ro
->ro_rt
== 0) {
2731 ipstat
.ips_noroute
++;
2732 error
= EHOSTUNREACH
;
2736 /* adjust state->dst if tunnel endpoint is offlink */
2737 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
2738 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
2739 dst4
= (struct sockaddr_in
*)state
->dst
;
2744 state
->m
= ipsec4_splithdr(state
->m
);
2749 switch (isr
->saidx
.proto
) {
2752 if ((error
= esp4_output(state
->m
, isr
)) != 0) {
2764 if ((error
= ah4_output(state
->m
, isr
)) != 0) {
2769 case IPPROTO_IPCOMP
:
2770 if ((error
= ipcomp4_output(state
->m
, isr
)) != 0) {
2777 "ipsec4_output: unknown ipsec protocol %d\n",
2785 if (state
->m
== 0) {
2789 ip
= mtod(state
->m
, struct ip
*);
2792 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_END
, 0,0,0,0,0);
2798 KERNEL_DEBUG(DBG_FNC_IPSEC_OUT
| DBG_FUNC_END
, error
,0,0,0,0);
2805 * IPsec output logic for IPv6, transport mode.
2808 ipsec6_output_trans(state
, nexthdrp
, mprev
, sp
, flags
, tun
)
2809 struct ipsec_output_state
*state
;
2812 struct secpolicy
*sp
;
2816 struct ip6_hdr
*ip6
;
2817 struct ipsecrequest
*isr
= NULL
;
2818 struct secasindex saidx
;
2821 struct sockaddr_in6
*sin6
;
2824 panic("state == NULL in ipsec6_output_trans");
2826 panic("state->m == NULL in ipsec6_output_trans");
2828 panic("nexthdrp == NULL in ipsec6_output_trans");
2830 panic("mprev == NULL in ipsec6_output_trans");
2832 panic("sp == NULL in ipsec6_output_trans");
2834 panic("tun == NULL in ipsec6_output_trans");
2836 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2837 printf("ipsec6_output_trans: applyed SP\n");
2838 kdebug_secpolicy(sp
));
2841 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
2842 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2843 /* the rest will be handled by ipsec6_output_tunnel() */
2847 /* make SA index for search proper SA */
2848 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2849 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2850 saidx
.mode
= isr
->saidx
.mode
;
2851 saidx
.reqid
= isr
->saidx
.reqid
;
2852 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
2853 if (sin6
->sin6_len
== 0) {
2854 sin6
->sin6_len
= sizeof(*sin6
);
2855 sin6
->sin6_family
= AF_INET6
;
2856 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2857 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
2858 sizeof(ip6
->ip6_src
));
2859 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
2860 /* fix scope id for comparing SPD */
2861 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2862 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
2865 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
2866 if (sin6
->sin6_len
== 0) {
2867 sin6
->sin6_len
= sizeof(*sin6
);
2868 sin6
->sin6_family
= AF_INET6
;
2869 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2870 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
2871 sizeof(ip6
->ip6_dst
));
2872 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
2873 /* fix scope id for comparing SPD */
2874 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2875 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
2879 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
2881 * IPsec processing is required, but no SA found.
2882 * I assume that key_acquire() had been called
2883 * to get/establish the SA. Here I discard
2884 * this packet because it is responsibility for
2885 * upper layer to retransmit the packet.
2887 ipsec6stat
.out_nosa
++;
2891 * Notify the fact that the packet is discarded
2892 * to ourselves. I believe this is better than
2893 * just silently discarding. (jinmei@kame.net)
2894 * XXX: should we restrict the error to TCP packets?
2895 * XXX: should we directly notify sockets via
2898 icmp6_error(state
->m
, ICMP6_DST_UNREACH
,
2899 ICMP6_DST_UNREACH_ADMIN
, 0);
2900 state
->m
= NULL
; /* icmp6_error freed the mbuf */
2904 /* validity check */
2905 if (isr
->sav
== NULL
) {
2906 switch (ipsec_get_reqlevel(isr
)) {
2907 case IPSEC_LEVEL_USE
:
2909 case IPSEC_LEVEL_REQUIRE
:
2910 /* must be not reached here. */
2911 panic("ipsec6_output_trans: no SA found, but required.");
2916 * If there is no valid SA, we give up to process.
2917 * see same place at ipsec4_output().
2919 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2920 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2921 ipsec6stat
.out_nosa
++;
2926 switch (isr
->saidx
.proto
) {
2929 error
= esp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2936 error
= ah6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2938 case IPPROTO_IPCOMP
:
2939 error
= ipcomp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2942 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
2943 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
2945 ipsec6stat
.out_inval
++;
2953 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2954 if (plen
> IPV6_MAXPACKET
) {
2955 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
2956 "IPsec with IPv6 jumbogram is not supported\n"));
2957 ipsec6stat
.out_inval
++;
2958 error
= EINVAL
; /*XXX*/
2961 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2962 ip6
->ip6_plen
= htons(plen
);
2965 /* if we have more to go, we need a tunnel mode processing */
2978 * IPsec output logic for IPv6, tunnel mode.
2981 ipsec6_output_tunnel(state
, sp
, flags
)
2982 struct ipsec_output_state
*state
;
2983 struct secpolicy
*sp
;
2986 struct ip6_hdr
*ip6
;
2987 struct ipsecrequest
*isr
= NULL
;
2988 struct secasindex saidx
;
2991 struct sockaddr_in6
* dst6
;
2995 panic("state == NULL in ipsec6_output_tunnel");
2997 panic("state->m == NULL in ipsec6_output_tunnel");
2999 panic("sp == NULL in ipsec6_output_tunnel");
3001 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
3002 printf("ipsec6_output_tunnel: applyed SP\n");
3003 kdebug_secpolicy(sp
));
3006 * transport mode ipsec (before the 1st tunnel mode) is already
3007 * processed by ipsec6_output_trans().
3009 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
3010 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
3014 for (/* already initialized */; isr
; isr
= isr
->next
) {
3015 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3016 /* When tunnel mode, SA peers must be specified. */
3017 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
3019 /* make SA index to look for a proper SA */
3020 struct sockaddr_in6
*sin6
;
3022 bzero(&saidx
, sizeof(saidx
));
3023 saidx
.proto
= isr
->saidx
.proto
;
3024 saidx
.mode
= isr
->saidx
.mode
;
3025 saidx
.reqid
= isr
->saidx
.reqid
;
3027 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3028 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
3029 if (sin6
->sin6_len
== 0) {
3030 sin6
->sin6_len
= sizeof(*sin6
);
3031 sin6
->sin6_family
= AF_INET6
;
3032 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3033 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
3034 sizeof(ip6
->ip6_src
));
3035 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
3036 /* fix scope id for comparing SPD */
3037 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3038 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
3041 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
3042 if (sin6
->sin6_len
== 0) {
3043 sin6
->sin6_len
= sizeof(*sin6
);
3044 sin6
->sin6_family
= AF_INET6
;
3045 sin6
->sin6_port
= IPSEC_PORT_ANY
;
3046 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
3047 sizeof(ip6
->ip6_dst
));
3048 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
3049 /* fix scope id for comparing SPD */
3050 sin6
->sin6_addr
.s6_addr16
[1] = 0;
3051 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
3056 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
3058 * IPsec processing is required, but no SA found.
3059 * I assume that key_acquire() had been called
3060 * to get/establish the SA. Here I discard
3061 * this packet because it is responsibility for
3062 * upper layer to retransmit the packet.
3064 ipsec6stat
.out_nosa
++;
3069 /* validity check */
3070 if (isr
->sav
== NULL
) {
3071 switch (ipsec_get_reqlevel(isr
)) {
3072 case IPSEC_LEVEL_USE
:
3074 case IPSEC_LEVEL_REQUIRE
:
3075 /* must be not reached here. */
3076 panic("ipsec6_output_tunnel: no SA found, but required.");
3081 * If there is no valid SA, we give up to process.
3082 * see same place at ipsec4_output().
3084 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
3085 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
3086 ipsec6stat
.out_nosa
++;
3092 * There may be the case that SA status will be changed when
3093 * we are refering to one. So calling splsoftnet().
3097 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3099 * build IPsec tunnel.
3101 /* XXX should be processed with other familiy */
3102 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
3103 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3104 "family mismatched between inner and outer, spi=%u\n",
3105 (u_int32_t
)ntohl(isr
->sav
->spi
)));
3107 ipsec6stat
.out_inval
++;
3108 error
= EAFNOSUPPORT
;
3112 state
->m
= ipsec6_splithdr(state
->m
);
3115 ipsec6stat
.out_nomem
++;
3119 error
= ipsec6_encapsulate(state
->m
, isr
->sav
);
3125 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3127 state
->ro
= &isr
->sav
->sah
->sa_route
;
3128 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
3129 dst6
= (struct sockaddr_in6
*)state
->dst
;
3130 if (state
->ro
->ro_rt
3131 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
3132 || !IN6_ARE_ADDR_EQUAL(&dst6
->sin6_addr
, &ip6
->ip6_dst
))) {
3133 rtfree(state
->ro
->ro_rt
);
3134 state
->ro
->ro_rt
= NULL
;
3136 if (state
->ro
->ro_rt
== 0) {
3137 bzero(dst6
, sizeof(*dst6
));
3138 dst6
->sin6_family
= AF_INET6
;
3139 dst6
->sin6_len
= sizeof(*dst6
);
3140 dst6
->sin6_addr
= ip6
->ip6_dst
;
3143 if (state
->ro
->ro_rt
== 0) {
3144 ip6stat
.ip6s_noroute
++;
3145 ipsec6stat
.out_noroute
++;
3146 error
= EHOSTUNREACH
;
3150 /* adjust state->dst if tunnel endpoint is offlink */
3151 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
3152 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
3153 dst6
= (struct sockaddr_in6
*)state
->dst
;
3158 state
->m
= ipsec6_splithdr(state
->m
);
3160 ipsec6stat
.out_nomem
++;
3164 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3165 switch (isr
->saidx
.proto
) {
3168 error
= esp6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
3175 error
= ah6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
3177 case IPPROTO_IPCOMP
:
3178 /* XXX code should be here */
3181 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3182 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
3184 ipsec6stat
.out_inval
++;
3192 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
3193 if (plen
> IPV6_MAXPACKET
) {
3194 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3195 "IPsec with IPv6 jumbogram is not supported\n"));
3196 ipsec6stat
.out_inval
++;
3197 error
= EINVAL
; /*XXX*/
3200 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3201 ip6
->ip6_plen
= htons(plen
);
3215 * Chop IP header and option off from the payload.
3217 static struct mbuf
*
3225 if (m
->m_len
< sizeof(struct ip
))
3226 panic("ipsec4_splithdr: first mbuf too short");
3227 ip
= mtod(m
, struct ip
*);
3229 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
3231 hlen
= ip
->ip_hl
<< 2;
3233 if (m
->m_len
> hlen
) {
3234 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3239 M_COPY_PKTHDR(mh
, m
);
3241 m
->m_flags
&= ~M_PKTHDR
;
3247 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), hlen
);
3248 } else if (m
->m_len
< hlen
) {
3249 m
= m_pullup(m
, hlen
);
3258 static struct mbuf
*
3263 struct ip6_hdr
*ip6
;
3266 if (m
->m_len
< sizeof(struct ip6_hdr
))
3267 panic("ipsec6_splithdr: first mbuf too short");
3268 ip6
= mtod(m
, struct ip6_hdr
*);
3269 hlen
= sizeof(struct ip6_hdr
);
3270 if (m
->m_len
> hlen
) {
3271 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3276 M_COPY_PKTHDR(mh
, m
);
3278 m
->m_flags
&= ~M_PKTHDR
;
3284 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), hlen
);
3285 } else if (m
->m_len
< hlen
) {
3286 m
= m_pullup(m
, hlen
);
3294 /* validate inbound IPsec tunnel packet. */
3296 ipsec4_tunnel_validate(m
, off
, nxt0
, sav
)
3297 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3300 struct secasvar
*sav
;
3302 u_int8_t nxt
= nxt0
& 0xff;
3303 struct sockaddr_in
*sin
;
3304 struct sockaddr_in osrc
, odst
, isrc
, idst
;
3306 struct secpolicy
*sp
;
3310 if (m
->m_len
< sizeof(struct ip
))
3311 panic("too short mbuf on ipsec4_tunnel_validate");
3313 if (nxt
!= IPPROTO_IPV4
)
3315 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip
))
3317 /* do not decapsulate if the SA is for transport mode only */
3318 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3321 oip
= mtod(m
, struct ip
*);
3323 hlen
= _IP_VHL_HL(oip
->ip_vhl
) << 2;
3325 hlen
= oip
->ip_hl
<< 2;
3327 if (hlen
!= sizeof(struct ip
))
3330 /* AF_INET6 should be supported, but at this moment we don't. */
3331 sin
= (struct sockaddr_in
*)&sav
->sah
->saidx
.dst
;
3332 if (sin
->sin_family
!= AF_INET
)
3334 if (bcmp(&oip
->ip_dst
, &sin
->sin_addr
, sizeof(oip
->ip_dst
)) != 0)
3338 bzero(&osrc
, sizeof(osrc
));
3339 bzero(&odst
, sizeof(odst
));
3340 bzero(&isrc
, sizeof(isrc
));
3341 bzero(&idst
, sizeof(idst
));
3342 osrc
.sin_family
= odst
.sin_family
= isrc
.sin_family
= idst
.sin_family
=
3344 osrc
.sin_len
= odst
.sin_len
= isrc
.sin_len
= idst
.sin_len
=
3345 sizeof(struct sockaddr_in
);
3346 osrc
.sin_addr
= oip
->ip_src
;
3347 odst
.sin_addr
= oip
->ip_dst
;
3348 m_copydata(m
, off
+ offsetof(struct ip
, ip_src
), sizeof(isrc
.sin_addr
),
3349 (caddr_t
)&isrc
.sin_addr
);
3350 m_copydata(m
, off
+ offsetof(struct ip
, ip_dst
), sizeof(idst
.sin_addr
),
3351 (caddr_t
)&idst
.sin_addr
);
3354 * RFC2401 5.2.1 (b): (assume that we are using tunnel mode)
3355 * - if the inner destination is multicast address, there can be
3356 * multiple permissible inner source address. implementation
3357 * may want to skip verification of inner source address against
3359 * - if the inner protocol is ICMP, the packet may be an error report
3360 * from routers on the other side of the VPN cloud (R in the
3361 * following diagram). in this case, we cannot verify inner source
3362 * address against SPD selector.
3363 * me -- gw === gw -- R -- you
3365 * we consider the first bullet to be users responsibility on SPD entry
3366 * configuration (if you need to encrypt multicast traffic, set
3367 * the source range of SPD selector to 0.0.0.0/0, or have explicit
3368 * address ranges for possible senders).
3369 * the second bullet is not taken care of (yet).
3371 * therefore, we do not do anything special about inner source.
3374 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3375 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3384 /* validate inbound IPsec tunnel packet. */
3386 ipsec6_tunnel_validate(m
, off
, nxt0
, sav
)
3387 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3390 struct secasvar
*sav
;
3392 u_int8_t nxt
= nxt0
& 0xff;
3393 struct sockaddr_in6
*sin6
;
3394 struct sockaddr_in6 osrc
, odst
, isrc
, idst
;
3395 struct secpolicy
*sp
;
3396 struct ip6_hdr
*oip6
;
3399 if (m
->m_len
< sizeof(struct ip6_hdr
))
3400 panic("too short mbuf on ipsec6_tunnel_validate");
3402 if (nxt
!= IPPROTO_IPV6
)
3404 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip6_hdr
))
3406 /* do not decapsulate if the SA is for transport mode only */
3407 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3410 oip6
= mtod(m
, struct ip6_hdr
*);
3411 /* AF_INET should be supported, but at this moment we don't. */
3412 sin6
= (struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
;
3413 if (sin6
->sin6_family
!= AF_INET6
)
3415 if (!IN6_ARE_ADDR_EQUAL(&oip6
->ip6_dst
, &sin6
->sin6_addr
))
3419 bzero(&osrc
, sizeof(osrc
));
3420 bzero(&odst
, sizeof(odst
));
3421 bzero(&isrc
, sizeof(isrc
));
3422 bzero(&idst
, sizeof(idst
));
3423 osrc
.sin6_family
= odst
.sin6_family
= isrc
.sin6_family
=
3424 idst
.sin6_family
= AF_INET6
;
3425 osrc
.sin6_len
= odst
.sin6_len
= isrc
.sin6_len
= idst
.sin6_len
=
3426 sizeof(struct sockaddr_in6
);
3427 osrc
.sin6_addr
= oip6
->ip6_src
;
3428 odst
.sin6_addr
= oip6
->ip6_dst
;
3429 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_src
),
3430 sizeof(isrc
.sin6_addr
), (caddr_t
)&isrc
.sin6_addr
);
3431 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_dst
),
3432 sizeof(idst
.sin6_addr
), (caddr_t
)&idst
.sin6_addr
);
3435 * regarding to inner source address validation, see a long comment
3436 * in ipsec4_tunnel_validate.
3439 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3440 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3442 * when there is no suitable inbound policy for the packet of the ipsec
3443 * tunnel mode, the kernel never decapsulate the tunneled packet
3444 * as the ipsec tunnel mode even when the system wide policy is "none".
3445 * then the kernel leaves the generic tunnel module to process this
3446 * packet. if there is no rule of the generic tunnel, the packet
3447 * is rejected and the statistics will be counted up.
3458 * Make a mbuf chain for encryption.
3459 * If the original mbuf chain contains a mbuf with a cluster,
3460 * allocate a new cluster and copy the data to the new cluster.
3461 * XXX: this hack is inefficient, but is necessary to handle cases
3462 * of TCP retransmission...
3468 struct mbuf
*n
, **mpp
, *mnew
;
3470 for (n
= m
, mpp
= &m
; n
; n
= n
->m_next
) {
3471 if (n
->m_flags
& M_EXT
) {
3473 * Make a copy only if there are more than one references
3475 * XXX: is this approach effective?
3478 n
->m_ext
.ext_free
||
3479 m_mclhasreference(n
)
3485 if (n
->m_flags
& M_PKTHDR
) {
3486 MGETHDR(mnew
, M_DONTWAIT
, MT_HEADER
);
3489 mnew
->m_pkthdr
= n
->m_pkthdr
;
3491 if (n
->m_pkthdr
.aux
) {
3492 mnew
->m_pkthdr
.aux
=
3493 m_copym(n
->m_pkthdr
.aux
,
3494 0, M_COPYALL
, M_DONTWAIT
);
3497 M_COPY_PKTHDR(mnew
, n
);
3498 mnew
->m_flags
= n
->m_flags
& M_COPYFLAGS
;
3501 MGET(mnew
, M_DONTWAIT
, MT_DATA
);
3509 * Copy data. If we don't have enough space to
3510 * store the whole data, allocate a cluster
3511 * or additional mbufs.
3512 * XXX: we don't use m_copyback(), since the
3513 * function does not use clusters and thus is
3522 if (remain
<= (mm
->m_flags
& M_PKTHDR
? MHLEN
: MLEN
))
3524 else { /* allocate a cluster */
3525 MCLGET(mm
, M_DONTWAIT
);
3526 if (!(mm
->m_flags
& M_EXT
)) {
3530 len
= remain
< MCLBYTES
?
3534 bcopy(n
->m_data
+ copied
, mm
->m_data
,
3541 if (remain
<= 0) /* completed? */
3544 /* need another mbuf */
3545 MGETHDR(mn
, M_DONTWAIT
, MT_HEADER
);
3548 mn
->m_pkthdr
.rcvif
= NULL
;
3554 mm
->m_next
= m_free(n
);
3572 static struct mbuf
*
3578 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3580 n
= m_aux_add(m
, AF_INET
, IPPROTO_ESP
);
3582 return n
; /* ENOBUFS */
3583 n
->m_len
= sizeof(struct socket
*);
3584 bzero(mtod(n
, void *), n
->m_len
);
3588 static struct mbuf
*
3594 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3596 if (n
&& n
->m_len
< sizeof(struct socket
*))
3597 panic("invalid ipsec m_aux");
3608 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3613 /* if the aux buffer is unnecessary, nuke it. */
3622 if (n
->m_len
== sizeof(struct socket
*) && !*mtod(n
, struct socket
**))
3627 ipsec_setsocket(m
, so
)
3633 /* if so == NULL, don't insist on getting the aux mbuf */
3635 n
= ipsec_addaux(m
);
3639 n
= ipsec_findaux(m
);
3640 if (n
&& n
->m_len
>= sizeof(struct socket
*))
3641 *mtod(n
, struct socket
**) = so
;
3652 n
= ipsec_findaux(m
);
3653 if (n
&& n
->m_len
>= sizeof(struct socket
*))
3654 return *mtod(n
, struct socket
**);
3660 ipsec_addhist(m
, proto
, spi
)
3666 struct ipsec_history
*p
;
3668 n
= ipsec_addaux(m
);
3671 if (M_TRAILINGSPACE(n
) < sizeof(*p
))
3672 return ENOSPC
; /* XXX */
3673 p
= (struct ipsec_history
*)(mtod(n
, caddr_t
) + n
->m_len
);
3674 n
->m_len
+= sizeof(*p
);
3675 bzero(p
, sizeof(*p
));
3676 p
->ih_proto
= proto
;
3681 struct ipsec_history
*
3682 ipsec_gethist(m
, lenp
)
3689 n
= ipsec_findaux(m
);
3693 if (sizeof(struct socket
*) > l
)
3695 if ((l
- sizeof(struct socket
*)) % sizeof(struct ipsec_history
))
3697 /* XXX does it make more sense to divide by sizeof(ipsec_history)? */
3699 *lenp
= l
- sizeof(struct socket
*);
3700 return (struct ipsec_history
*)
3701 (mtod(n
, caddr_t
) + sizeof(struct socket
*));
3710 n
= ipsec_findaux(m
);
3711 if ((n
) && n
->m_len
> sizeof(struct socket
*))
3712 n
->m_len
= sizeof(struct socket
*);
3716 __private_extern__
void
3717 ipsec_send_natt_keepalive(
3718 struct secasvar
*sav
)
3724 if ((esp_udp_encap_port
& 0xFFFF) == 0 || sav
->remote_ike_port
== 0) return;
3726 m
= m_gethdr(M_NOWAIT
, MT_DATA
);
3727 if (m
== NULL
) return;
3730 * Create a UDP packet complete with IP header.
3731 * We must do this because UDP output requires
3732 * an inpcb which we don't have. UDP packet
3733 * contains one byte payload. The byte is set
3736 ip
= (struct ip
*)m_mtod(m
);
3737 uh
= (struct udphdr
*)((char*)m_mtod(m
) + sizeof(struct ip
));
3738 m
->m_len
= sizeof(struct udpiphdr
) + 1;
3739 bzero(m_mtod(m
), m
->m_len
);
3740 ip
->ip_len
= ntohs(m
->m_len
);
3741 ip
->ip_ttl
= ip_defttl
;
3742 ip
->ip_p
= IPPROTO_UDP
;
3743 ip
->ip_src
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
;
3744 ip
->ip_dst
= ((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
;
3745 uh
->uh_sport
= ntohs((u_short
)esp_udp_encap_port
);
3746 uh
->uh_dport
= ntohs(sav
->remote_ike_port
);
3747 uh
->uh_ulen
= htons(1 + sizeof(struct udphdr
));
3749 *(u_int8_t
*)((char*)m_mtod(m
) + sizeof(struct ip
) + sizeof(struct udphdr
)) = 0xFF;
3751 if (ip_output(m
, NULL
, &sav
->sah
->sa_route
, IP_NOIPSEC
, NULL
) == 0)
3752 sav
->natt_last_activity
= natt_now
;