1 /* $KAME: ipsec.c,v 1.56 2000/04/04 08:47:34 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * IPsec controller part.
37 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
39 #if __NetBSD__ /*XXX*/
40 #include "opt_ipsec.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/errno.h>
54 #include <sys/kernel.h>
55 #include <sys/syslog.h>
59 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined (__APPLE__)
60 #include <sys/sysctl.h>
64 #include <net/route.h>
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/in_var.h>
71 #include <netinet/udp.h>
72 #include <netinet/udp_var.h>
73 #include <netinet/ip_ecn.h>
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
79 #include <netinet/in_pcb.h>
81 #if !((defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__OpenBSD__) || (defined(__bsdi__) && _BSDI_VERSION >= 199802)) || defined (__APPLE__)
82 #include <netinet6/in6_pcb.h>
84 #include <netinet/icmp6.h>
87 #include <netinet6/ipsec.h>
88 #include <netinet6/ah.h>
90 #include <netinet6/esp.h>
92 #include <netinet6/ipcomp.h>
93 #include <netkey/key.h>
94 #include <netkey/keydb.h>
95 #include <netkey/key_debug.h>
97 #include <net/net_osdep.h>
101 #define in6p_sp inp_sp
102 #define in6p_fport inp_fport
103 #define in6p_lport inp_lport
104 #define in6p_socket inp_socket
105 #define sotoin6pcb(so) ((struct inpcb *)(so)->so_pcb)
109 #define ovbcopy bcopy
118 struct ipsecstat ipsecstat
;
119 int ip4_inbound_call_ike
= 0;
120 int ip4_ah_cleartos
= 1;
121 int ip4_ah_offsetmask
= 0; /* maybe IP_DF? */
122 int ip4_ipsec_dfbit
= 0; /* DF bit on encap. 0: clear 1: set 2: copy */
123 int ip4_esp_trans_deflev
= IPSEC_LEVEL_USE
;
124 int ip4_esp_net_deflev
= IPSEC_LEVEL_USE
;
125 int ip4_ah_trans_deflev
= IPSEC_LEVEL_USE
;
126 int ip4_ah_net_deflev
= IPSEC_LEVEL_USE
;
127 struct secpolicy ip4_def_policy
;
128 int ip4_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
130 #if defined(__FreeBSD__) || defined(__APPLE__)
131 SYSCTL_DECL(_net_inet_ipsec
);
133 SYSCTL_STRUCT(_net_inet_ipsec
, IPSECCTL_STATS
,
134 stats
, CTLFLAG_RD
, &ipsecstat
, ipsecstat
, "");
135 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_POLICY
,
136 def_policy
, CTLFLAG_RW
, &ip4_def_policy
.policy
, 0, "");
137 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
138 CTLFLAG_RW
, &ip4_esp_trans_deflev
, 0, "");
139 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
140 CTLFLAG_RW
, &ip4_esp_net_deflev
, 0, "");
141 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
142 CTLFLAG_RW
, &ip4_ah_trans_deflev
, 0, "");
143 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
144 CTLFLAG_RW
, &ip4_ah_net_deflev
, 0, "");
145 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_INBOUND_CALL_IKE
,
146 inbound_call_ike
, CTLFLAG_RW
, &ip4_inbound_call_ike
, 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 #endif /* __FreeBSD__ */
160 struct ipsecstat ipsec6stat
;
161 int ip6_inbound_call_ike
= 0;
162 int ip6_esp_trans_deflev
= IPSEC_LEVEL_USE
;
163 int ip6_esp_net_deflev
= IPSEC_LEVEL_USE
;
164 int ip6_ah_trans_deflev
= IPSEC_LEVEL_USE
;
165 int ip6_ah_net_deflev
= IPSEC_LEVEL_USE
;
166 struct secpolicy ip6_def_policy
;
167 int ip6_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
169 #if defined(__FreeBSD__) || defined(__APPLE__)
170 SYSCTL_DECL(_net_inet6_ipsec6
);
171 /* net.inet6.ipsec6 */
172 SYSCTL_STRUCT(_net_inet6_ipsec6
, IPSECCTL_STATS
,
173 stats
, CTLFLAG_RD
, &ipsec6stat
, ipsecstat
, "");
174 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_POLICY
,
175 def_policy
, CTLFLAG_RW
, &ip6_def_policy
.policy
, 0, "");
176 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
177 CTLFLAG_RW
, &ip6_esp_trans_deflev
, 0, "");
178 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
179 CTLFLAG_RW
, &ip6_esp_net_deflev
, 0, "");
180 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
181 CTLFLAG_RW
, &ip6_ah_trans_deflev
, 0, "");
182 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
183 CTLFLAG_RW
, &ip6_ah_net_deflev
, 0, "");
184 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_INBOUND_CALL_IKE
,
185 inbound_call_ike
, CTLFLAG_RW
, &ip6_inbound_call_ike
, 0, "");
186 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ECN
,
187 ecn
, CTLFLAG_RW
, &ip6_ipsec_ecn
, 0, "");
188 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEBUG
,
189 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
190 #endif /*__FreeBSD__*/
193 static int ipsec_setspidx_mbuf
194 __P((struct secpolicyindex
*, u_int
, u_int
, struct mbuf
*));
195 static void ipsec4_setspidx_inpcb
__P((struct mbuf
*, struct inpcb
*pcb
));
196 static void ipsec4_setspidx_ipaddr
__P((struct mbuf
*, struct secpolicyindex
*));
198 static void ipsec6_get_ulp
__P((struct mbuf
*m
, struct secpolicyindex
*));
199 static void ipsec6_setspidx_in6pcb
__P((struct mbuf
*, struct in6pcb
*pcb
));
200 static void ipsec6_setspidx_ipaddr
__P((struct mbuf
*, struct secpolicyindex
*));
202 static struct inpcbpolicy
*ipsec_newpcbpolicy
__P((void));
203 static void ipsec_delpcbpolicy
__P((struct inpcbpolicy
*));
204 static struct secpolicy
*ipsec_deepcopy_policy
__P((struct secpolicy
*src
));
205 static int ipsec_set_policy
__P((struct secpolicy
**pcb_sp
,
206 int optname
, caddr_t request
, size_t len
, int priv
));
207 static int ipsec_get_policy
__P((struct secpolicy
*pcb_sp
, struct mbuf
**mp
));
208 static void vshiftl
__P((unsigned char *, int, int));
209 static int ipsec_in_reject
__P((struct secpolicy
*, struct mbuf
*));
210 static size_t ipsec_hdrsiz
__P((struct secpolicy
*));
211 static struct mbuf
*ipsec4_splithdr
__P((struct mbuf
*));
213 static struct mbuf
*ipsec6_splithdr
__P((struct mbuf
*));
215 static int ipsec4_encapsulate
__P((struct mbuf
*, struct secasvar
*));
217 static int ipsec6_encapsulate
__P((struct mbuf
*, struct secasvar
*));
221 * For OUTBOUND packet having a socket. Searching SPD for packet,
222 * and return a pointer to SP.
223 * OUT: NULL: no apropreate SP found, the following value is set to error.
225 * EACCES : discard packet.
226 * ENOENT : ipsec_acquire() in progress, maybe.
227 * others : error occured.
228 * others: a pointer to SP
230 * NOTE: IPv6 mapped adddress concern is implemented here.
233 ipsec4_getpolicybysock(m
, dir
, so
, error
)
239 struct inpcbpolicy
*pcbsp
= NULL
;
240 struct secpolicy
*currsp
= NULL
; /* policy on socket */
241 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
244 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
245 panic("ipsec4_getpolicybysock: NULL pointer was passed.\n");
247 switch (so
->so_proto
->pr_domain
->dom_family
) {
249 /* set spidx in pcb */
250 ipsec4_setspidx_inpcb(m
, sotoinpcb(so
));
251 pcbsp
= sotoinpcb(so
)->inp_sp
;
255 /* set spidx in pcb */
256 ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
257 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
261 panic("ipsec4_getpolicybysock: unsupported address family\n");
266 panic("ipsec4_getpolicybysock: pcbsp is NULL.\n");
268 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined(__APPLE__)
269 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
270 printf("send: priv=%d ", pcbsp
->priv
);
272 printf("p_ruid=%d ", so
->so_cred
->p_ruid
);
273 printf("p_svuid=%d ", so
->so_cred
->p_svuid
);
274 printf("cr_uid=%d\n", so
->so_cred
->pc_ucred
->cr_uid
);
278 case IPSEC_DIR_INBOUND
:
279 currsp
= pcbsp
->sp_in
;
281 case IPSEC_DIR_OUTBOUND
:
282 currsp
= pcbsp
->sp_out
;
285 panic("ipsec4_getpolicybysock: illegal direction.\n");
290 panic("ipsec4_getpolicybysock: currsp is NULL.\n");
292 /* when privilieged socket */
294 switch (currsp
->policy
) {
295 case IPSEC_POLICY_BYPASS
:
300 case IPSEC_POLICY_ENTRUST
:
301 /* look for a policy in SPD */
302 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
305 if (kernsp
!= NULL
) {
306 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
307 printf("DP ipsec4_getpolicybysock called "
308 "to allocate SP:%p\n", kernsp
));
314 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
315 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
317 "fixed system default policy: %d->%d\n",
318 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
319 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
321 ip4_def_policy
.refcnt
++;
323 return &ip4_def_policy
;
325 case IPSEC_POLICY_IPSEC
:
331 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
332 "Invalid policy for PCB %d\n", currsp
->policy
));
339 /* when non-privilieged socket */
340 /* look for a policy in SPD */
341 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
344 if (kernsp
!= NULL
) {
345 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
346 printf("DP ipsec4_getpolicybysock called "
347 "to allocate SP:%p\n", kernsp
));
353 switch (currsp
->policy
) {
354 case IPSEC_POLICY_BYPASS
:
355 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
356 "Illegal policy for non-priviliged defined %d\n",
361 case IPSEC_POLICY_ENTRUST
:
362 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
363 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
365 "fixed system default policy: %d->%d\n",
366 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
367 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
369 ip4_def_policy
.refcnt
++;
371 return &ip4_def_policy
;
373 case IPSEC_POLICY_IPSEC
:
379 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
380 "Invalid policy for PCB %d\n", currsp
->policy
));
388 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
389 * and return a pointer to SP.
390 * OUT: positive: a pointer to the entry for security policy leaf matched.
391 * NULL: no apropreate SP found, the following value is set to error.
393 * EACCES : discard packet.
394 * ENOENT : ipsec_acquire() in progress, maybe.
395 * others : error occured.
398 ipsec4_getpolicybyaddr(m
, dir
, flag
, error
)
404 struct secpolicy
*sp
= NULL
;
407 if (m
== NULL
|| error
== NULL
)
408 panic("ipsec4_getpolicybyaddr: NULL pointer was passed.\n");
411 struct secpolicyindex spidx
;
413 bzero(&spidx
, sizeof(spidx
));
415 /* make a index to look for a policy */
416 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET
, m
);
421 sp
= key_allocsp(&spidx
, dir
);
426 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
427 printf("DP ipsec4_getpolicybyaddr called "
428 "to allocate SP:%p\n", sp
));
434 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
435 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
436 ipseclog((LOG_INFO
, "fixed system default policy:%d->%d\n",
437 ip4_def_policy
.policy
,
439 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
441 ip4_def_policy
.refcnt
++;
443 return &ip4_def_policy
;
448 * For OUTBOUND packet having a socket. Searching SPD for packet,
449 * and return a pointer to SP.
450 * OUT: NULL: no apropreate SP found, the following value is set to error.
452 * EACCES : discard packet.
453 * ENOENT : ipsec_acquire() in progress, maybe.
454 * others : error occured.
455 * others: a pointer to SP
458 ipsec6_getpolicybysock(m
, dir
, so
, error
)
464 struct inpcbpolicy
*pcbsp
= NULL
;
465 struct secpolicy
*currsp
= NULL
; /* policy on socket */
466 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
469 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
470 panic("ipsec6_getpolicybysock: NULL pointer was passed.\n");
472 /* set spidx in pcb */
473 ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
475 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
479 panic("ipsec6_getpolicybysock: pcbsp is NULL.\n");
482 case IPSEC_DIR_INBOUND
:
483 currsp
= pcbsp
->sp_in
;
485 case IPSEC_DIR_OUTBOUND
:
486 currsp
= pcbsp
->sp_out
;
489 panic("ipsec6_getpolicybysock: illegal direction.\n");
494 panic("ipsec6_getpolicybysock: currsp is NULL.\n");
496 /* when privilieged socket */
498 switch (currsp
->policy
) {
499 case IPSEC_POLICY_BYPASS
:
504 case IPSEC_POLICY_ENTRUST
:
505 /* look for a policy in SPD */
506 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
509 if (kernsp
!= NULL
) {
510 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
511 printf("DP ipsec6_getpolicybysock called "
512 "to allocate SP:%p\n", kernsp
));
518 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
519 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
521 "fixed system default policy: %d->%d\n",
522 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
523 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
525 ip6_def_policy
.refcnt
++;
527 return &ip6_def_policy
;
529 case IPSEC_POLICY_IPSEC
:
535 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
536 "Invalid policy for PCB %d\n", currsp
->policy
));
543 /* when non-privilieged socket */
544 /* look for a policy in SPD */
545 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
548 if (kernsp
!= NULL
) {
549 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
550 printf("DP ipsec6_getpolicybysock called "
551 "to allocate SP:%p\n", kernsp
));
557 switch (currsp
->policy
) {
558 case IPSEC_POLICY_BYPASS
:
559 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
560 "Illegal policy for non-priviliged defined %d\n",
565 case IPSEC_POLICY_ENTRUST
:
566 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
567 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
569 "fixed system default policy: %d->%d\n",
570 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
571 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
573 ip6_def_policy
.refcnt
++;
575 return &ip6_def_policy
;
577 case IPSEC_POLICY_IPSEC
:
584 "ipsec6_policybysock: Invalid policy for PCB %d\n",
593 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
594 * and return a pointer to SP.
595 * `flag' means that packet is to be forwarded whether or not.
597 * OUT: positive: a pointer to the entry for security policy leaf matched.
598 * NULL: no apropreate SP found, the following value is set to error.
600 * EACCES : discard packet.
601 * ENOENT : ipsec_acquire() in progress, maybe.
602 * others : error occured.
604 #ifndef IP_FORWARDING
605 #define IP_FORWARDING 1
609 ipsec6_getpolicybyaddr(m
, dir
, flag
, error
)
615 struct secpolicy
*sp
= NULL
;
618 if (m
== NULL
|| error
== NULL
)
619 panic("ipsec6_getpolicybyaddr: NULL pointer was passed.\n");
622 struct secpolicyindex spidx
;
624 bzero(&spidx
, sizeof(spidx
));
626 /* make a index to look for a policy */
627 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET6
, m
);
632 sp
= key_allocsp(&spidx
, dir
);
637 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
638 printf("DP ipsec6_getpolicybyaddr called "
639 "to allocate SP:%p\n", sp
));
645 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
646 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
647 ipseclog((LOG_INFO
, "fixed system default policy: %d->%d\n",
648 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
649 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
651 ip6_def_policy
.refcnt
++;
653 return &ip6_def_policy
;
658 * set IP address into spidx from mbuf.
659 * When Forwarding packet and ICMP echo reply, this function is used.
661 * IN: get the followings from mbuf.
662 * protocol family, src, dst, next protocol
665 * other: failure, and set errno.
668 ipsec_setspidx_mbuf(spidx
, dir
, family
, m
)
669 struct secpolicyindex
*spidx
;
673 struct sockaddr
*sa1
, *sa2
;
676 if (spidx
== NULL
|| m
== NULL
)
677 panic("ipsec_setspidx_mbuf: NULL pointer was passed.\n");
679 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
680 printf("ipsec_setspidx_mbuf: begin\n"); kdebug_mbuf(m
));
683 bzero(spidx
, sizeof(*spidx
));
686 sa1
= (struct sockaddr
*)&spidx
->src
;
687 sa2
= (struct sockaddr
*)&spidx
->dst
;
688 sa1
->sa_len
= sa2
->sa_len
= _SALENBYAF(family
);
689 sa1
->sa_family
= sa2
->sa_family
= family
;
690 spidx
->prefs
= spidx
->prefd
= _INALENBYAF(family
) << 3;
693 /* sanity check for packet length. */
698 for (n
= m
; n
; n
= n
->m_next
)
700 if (m
->m_pkthdr
.len
!= tlen
) {
701 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
702 printf("ipsec_setspidx_mbuf: "
703 "total of m_len(%d) != pkthdr.len(%d), "
705 tlen
, m
->m_pkthdr
.len
));
716 /* sanity check 1 for minimum ip header length */
717 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
718 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
719 printf("ipsec_setspidx_mbuf: "
720 "pkthdr.len(%d) < sizeof(struct ip), "
727 * get IPv4 header packet. usually the mbuf is contiguous
728 * and we need no copies.
730 if (m
->m_len
>= sizeof(*ip
))
731 ip
= mtod(m
, struct ip
*);
733 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
737 /* some more checks on IPv4 header. */
738 bcopy(&ip
->ip_src
, _INADDRBYSA(&spidx
->src
),
740 bcopy(&ip
->ip_dst
, _INADDRBYSA(&spidx
->dst
),
743 spidx
->ul_proto
= ip
->ip_p
;
744 _INPORTBYSA(&spidx
->src
) = IPSEC_PORT_ANY
;
745 _INPORTBYSA(&spidx
->dst
) = IPSEC_PORT_ANY
;
753 struct ip6_hdr ip6buf
;
755 /* sanity check 1 for minimum ip header length */
756 if (m
->m_pkthdr
.len
< sizeof(struct ip6_hdr
)) {
757 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
758 printf("ipsec_setspidx_mbuf: "
759 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
766 * get IPv6 header packet. usually the mbuf is contiguous
767 * and we need no copies.
769 if (m
->m_len
>= sizeof(*ip6
))
770 ip6
= mtod(m
, struct ip6_hdr
*);
772 m_copydata(m
, 0, sizeof(ip6buf
), (caddr_t
)&ip6buf
);
776 /* some more checks on IPv4 header. */
777 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
778 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
779 printf("ipsec_setspidx_mbuf: "
780 "wrong ip version on packet "
781 "(expected IPv6), ignored.\n"));
785 bcopy(&ip6
->ip6_src
, _INADDRBYSA(&spidx
->src
),
786 sizeof(ip6
->ip6_src
));
787 bcopy(&ip6
->ip6_dst
, _INADDRBYSA(&spidx
->dst
),
788 sizeof(ip6
->ip6_dst
));
790 ipsec6_get_ulp(m
, spidx
);
795 panic("ipsec_secsecidx: no supported family passed.\n");
798 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
799 printf("ipsec_setspidx_mbuf: end\n");
800 kdebug_secpolicyindex(spidx
));
806 bzero(spidx
, sizeof(*spidx
));
812 * Get upper layer protocol number and port number if there.
813 * Assumed all extension headers are in single mbuf.
815 #include <netinet/tcp.h>
816 #include <netinet/udp.h>
818 ipsec6_get_ulp(m
, spidx
)
820 struct secpolicyindex
*spidx
;
826 panic("ipsec6_get_ulp: NULL pointer was passed.\n");
828 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
829 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m
));
832 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
833 _INPORTBYSA(&spidx
->src
) = IPSEC_PORT_ANY
;
834 _INPORTBYSA(&spidx
->dst
) = IPSEC_PORT_ANY
;
837 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
838 if (off
< 0 || m
->m_pkthdr
.len
< off
)
843 spidx
->ul_proto
= nxt
;
844 if (off
+ sizeof(struct tcphdr
) <= m
->m_pkthdr
.len
) {
846 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
847 _INPORTBYSA(&spidx
->src
) = th
.th_sport
;
848 _INPORTBYSA(&spidx
->dst
) = th
.th_dport
;
852 spidx
->ul_proto
= nxt
;
853 if (off
+ sizeof(struct udphdr
) <= m
->m_pkthdr
.len
) {
855 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
856 _INPORTBYSA(&spidx
->src
) = uh
.uh_sport
;
857 _INPORTBYSA(&spidx
->dst
) = uh
.uh_dport
;
861 spidx
->ul_proto
= nxt
;
870 ipsec4_setspidx_inpcb(m
, pcb
)
874 struct secpolicyindex
*spidx
;
875 struct sockaddr
*sa1
, *sa2
;
879 panic("ipsec4_setspidx_inpcb: no PCB found.\n");
880 if (pcb
->inp_sp
== NULL
)
881 panic("ipsec4_setspidx_inpcb: no inp_sp found.\n");
882 if (pcb
->inp_sp
->sp_out
==NULL
|| pcb
->inp_sp
->sp_in
== NULL
)
883 panic("ipsec4_setspidx_inpcb: no sp_in/out found.\n");
885 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
886 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
888 spidx
= &pcb
->inp_sp
->sp_in
->spidx
;
889 spidx
->dir
= IPSEC_DIR_INBOUND
;
890 sa1
= (struct sockaddr
*)&spidx
->src
;
891 sa2
= (struct sockaddr
*)&spidx
->dst
;
892 sa1
->sa_len
= sa2
->sa_len
= _SALENBYAF(AF_INET
);
893 sa1
->sa_family
= sa2
->sa_family
= AF_INET
;
894 spidx
->prefs
= _INALENBYAF(AF_INET
) << 3;
895 spidx
->prefd
= _INALENBYAF(AF_INET
) << 3;
896 spidx
->ul_proto
= pcb
->inp_socket
->so_proto
->pr_protocol
;
897 _INPORTBYSA(&spidx
->src
) = pcb
->inp_fport
;
898 _INPORTBYSA(&spidx
->dst
) = pcb
->inp_lport
;
899 ipsec4_setspidx_ipaddr(m
, spidx
);
901 spidx
= &pcb
->inp_sp
->sp_out
->spidx
;
902 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
903 sa1
= (struct sockaddr
*)&spidx
->src
;
904 sa2
= (struct sockaddr
*)&spidx
->dst
;
905 sa1
->sa_len
= sa2
->sa_len
= _SALENBYAF(AF_INET
);
906 sa1
->sa_family
= sa2
->sa_family
= AF_INET
;
907 spidx
->prefs
= _INALENBYAF(AF_INET
) << 3;
908 spidx
->prefd
= _INALENBYAF(AF_INET
) << 3;
909 spidx
->ul_proto
= pcb
->inp_socket
->so_proto
->pr_protocol
;
910 _INPORTBYSA(&spidx
->src
) = pcb
->inp_lport
;
911 _INPORTBYSA(&spidx
->dst
) = pcb
->inp_fport
;
912 ipsec4_setspidx_ipaddr(m
, spidx
);
918 ipsec4_setspidx_ipaddr(m
, spidx
)
920 struct secpolicyindex
*spidx
;
922 struct ip
*ip
= NULL
;
925 /* sanity check 1 for minimum ip header length */
927 panic("ipsec4_setspidx_ipaddr: m == 0 passed.\n");
929 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
930 printf("ipsec4_setspidx_ipaddr: "
931 "pkthdr.len(%d) < sizeof(struct ip), "
937 if (m
&& m
->m_len
>= sizeof(*ip
))
938 ip
= mtod(m
, struct ip
*);
940 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
944 bcopy(&ip
->ip_src
, _INADDRBYSA(&spidx
->src
), sizeof(ip
->ip_src
));
945 bcopy(&ip
->ip_dst
, _INADDRBYSA(&spidx
->dst
), sizeof(ip
->ip_dst
));
952 ipsec6_setspidx_in6pcb(m
, pcb
)
956 struct secpolicyindex
*spidx
;
957 struct sockaddr
*sa1
, *sa2
;
961 panic("ipsec6_setspidx_in6pcb: no PCB found.\n");
962 if (pcb
->in6p_sp
== NULL
)
963 panic("ipsec6_setspidx_in6pcb: no in6p_sp found.\n");
964 if (pcb
->in6p_sp
->sp_out
==NULL
|| pcb
->in6p_sp
->sp_in
== NULL
)
965 panic("ipsec6_setspidx_in6pcb: no sp_in/out found.\n");
967 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
968 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
970 spidx
= &pcb
->in6p_sp
->sp_in
->spidx
;
971 spidx
->dir
= IPSEC_DIR_INBOUND
;
972 sa1
= (struct sockaddr
*)&spidx
->src
;
973 sa2
= (struct sockaddr
*)&spidx
->dst
;
974 sa1
->sa_len
= sa2
->sa_len
= _SALENBYAF(AF_INET6
);
975 sa1
->sa_family
= sa2
->sa_family
= AF_INET6
;
976 spidx
->prefs
= _INALENBYAF(AF_INET6
) << 3;
977 spidx
->prefd
= _INALENBYAF(AF_INET6
) << 3;
978 spidx
->ul_proto
= pcb
->in6p_socket
->so_proto
->pr_protocol
;
979 _INPORTBYSA(&spidx
->src
) = pcb
->in6p_fport
;
980 _INPORTBYSA(&spidx
->dst
) = pcb
->in6p_lport
;
981 ipsec6_setspidx_ipaddr(m
, spidx
);
983 spidx
= &pcb
->in6p_sp
->sp_out
->spidx
;
984 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
985 sa1
= (struct sockaddr
*)&spidx
->src
;
986 sa2
= (struct sockaddr
*)&spidx
->dst
;
987 sa1
->sa_len
= sa2
->sa_len
= _SALENBYAF(AF_INET6
);
988 sa1
->sa_family
= sa2
->sa_family
= AF_INET6
;
989 spidx
->prefs
= _INALENBYAF(AF_INET6
) << 3;
990 spidx
->prefd
= _INALENBYAF(AF_INET6
) << 3;
991 spidx
->ul_proto
= pcb
->in6p_socket
->so_proto
->pr_protocol
;
992 _INPORTBYSA(&spidx
->src
) = pcb
->in6p_lport
;
993 _INPORTBYSA(&spidx
->dst
) = pcb
->in6p_fport
;
994 ipsec6_setspidx_ipaddr(m
, spidx
);
1000 ipsec6_setspidx_ipaddr(m
, spidx
)
1002 struct secpolicyindex
*spidx
;
1004 struct ip6_hdr
*ip6
= NULL
;
1005 struct ip6_hdr ip6buf
;
1007 /* sanity check 1 for minimum ip header length */
1009 panic("ipsec6_setspidx_in6pcb: m == 0 passed.\n");
1011 if (m
->m_pkthdr
.len
< sizeof(struct ip6_hdr
)) {
1012 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1013 printf("ipsec6_setspidx_ipaddr: "
1014 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
1020 if (m
->m_len
>= sizeof(*ip6
))
1021 ip6
= mtod(m
, struct ip6_hdr
*);
1023 m_copydata(m
, 0, sizeof(ip6buf
), (caddr_t
)&ip6buf
);
1027 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
1028 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1029 printf("ipsec_setspidx_mbuf: "
1030 "wrong ip version on packet "
1031 "(expected IPv6), ignored.\n"));
1035 bcopy(&ip6
->ip6_src
, _INADDRBYSA(&spidx
->src
), sizeof(ip6
->ip6_src
));
1036 bcopy(&ip6
->ip6_dst
, _INADDRBYSA(&spidx
->dst
), sizeof(ip6
->ip6_dst
));
1042 static struct inpcbpolicy
*
1043 ipsec_newpcbpolicy()
1045 struct inpcbpolicy
*p
;
1047 p
= (struct inpcbpolicy
*)_MALLOC(sizeof(*p
), M_SECA
, M_WAITOK
);
1052 ipsec_delpcbpolicy(p
)
1053 struct inpcbpolicy
*p
;
1058 /* initialize policy in PCB */
1060 ipsec_init_policy(so
, pcb_sp
)
1062 struct inpcbpolicy
**pcb_sp
;
1064 struct inpcbpolicy
*new;
1067 if (so
== NULL
|| pcb_sp
== NULL
)
1068 panic("ipsec_init_policy: NULL pointer was passed.\n");
1070 new = ipsec_newpcbpolicy();
1072 ipseclog((LOG_DEBUG
, "ipsec_init_policy: No more memory.\n"));
1075 bzero(new, sizeof(*new));
1077 #if defined(__NetBSD__) || defined (__APPLE__)
1078 if (so
->so_uid
== 0) /*XXX*/
1082 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
1083 if (so
->so_cred
!= 0 && so
->so_cred
->pc_ucred
->cr_uid
== 0)
1088 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1089 printf("init: priv=%d ", new->priv
);
1091 printf("p_ruid=%d ", so
->so_cred
->p_ruid
);
1092 printf("p_svuid=%d ", so
->so_cred
->p_svuid
);
1093 printf("cr_uid=%d\n", so
->so_cred
->pc_ucred
->cr_uid
);
1095 printf("so_cred is NULL\n");
1098 new->priv
= so
->so_state
& SS_PRIV
;
1101 if ((new->sp_in
= key_newsp()) == NULL
) {
1102 ipsec_delpcbpolicy(new);
1105 new->sp_in
->state
= IPSEC_SPSTATE_ALIVE
;
1106 new->sp_in
->policy
= IPSEC_POLICY_ENTRUST
;
1108 if ((new->sp_out
= key_newsp()) == NULL
) {
1109 key_freesp(new->sp_in
);
1110 ipsec_delpcbpolicy(new);
1113 new->sp_out
->state
= IPSEC_SPSTATE_ALIVE
;
1114 new->sp_out
->policy
= IPSEC_POLICY_ENTRUST
;
1121 /* copy old ipsec policy into new */
1123 ipsec_copy_policy(old
, new)
1124 struct inpcbpolicy
*old
, *new;
1126 struct secpolicy
*sp
;
1128 sp
= ipsec_deepcopy_policy(old
->sp_in
);
1130 key_freesp(new->sp_in
);
1135 sp
= ipsec_deepcopy_policy(old
->sp_out
);
1137 key_freesp(new->sp_out
);
1142 new->priv
= old
->priv
;
1147 /* deep-copy a policy in PCB */
1148 static struct secpolicy
*
1149 ipsec_deepcopy_policy(src
)
1150 struct secpolicy
*src
;
1152 struct ipsecrequest
*newchain
= NULL
;
1153 struct ipsecrequest
*p
;
1154 struct ipsecrequest
**q
;
1155 struct ipsecrequest
*r
;
1156 struct secpolicy
*dst
;
1159 if (src
== NULL
|| dst
== NULL
)
1163 * deep-copy IPsec request chain. This is required since struct
1164 * ipsecrequest is not reference counted.
1167 for (p
= src
->req
; p
; p
= p
->next
) {
1168 *q
= (struct ipsecrequest
*)_MALLOC(sizeof(struct ipsecrequest
),
1172 bzero(*q
, sizeof(**q
));
1175 (*q
)->saidx
.proto
= p
->saidx
.proto
;
1176 (*q
)->saidx
.mode
= p
->saidx
.mode
;
1177 (*q
)->level
= p
->level
;
1178 (*q
)->saidx
.reqid
= p
->saidx
.reqid
;
1180 bcopy(&p
->saidx
.src
, &(*q
)->saidx
.src
, sizeof((*q
)->saidx
.src
));
1181 bcopy(&p
->saidx
.dst
, &(*q
)->saidx
.dst
, sizeof((*q
)->saidx
.dst
));
1189 dst
->req
= newchain
;
1190 dst
->state
= src
->state
;
1191 dst
->policy
= src
->policy
;
1192 /* do not touch the refcnt fields */
1197 for (p
= newchain
; p
; p
= r
) {
1205 /* set policy and ipsec request if present. */
1207 ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
)
1208 struct secpolicy
**pcb_sp
;
1214 struct sadb_x_policy
*xpl
;
1215 struct secpolicy
*newsp
= NULL
;
1219 if (pcb_sp
== NULL
|| *pcb_sp
== NULL
|| request
== NULL
)
1221 if (len
< sizeof(*xpl
))
1223 xpl
= (struct sadb_x_policy
*)request
;
1225 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1226 printf("ipsec_set_policy: passed policy\n");
1227 kdebug_sadb_x_policy((struct sadb_ext
*)xpl
));
1229 /* check policy type */
1230 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1231 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
1232 || xpl
->sadb_x_policy_type
== IPSEC_POLICY_NONE
)
1235 /* check privileged socket */
1236 if (priv
== 0 && xpl
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
)
1239 /* allocation new SP entry */
1240 if ((newsp
= key_msg2sp(xpl
, len
, &error
)) == NULL
)
1243 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1245 /* clear old SP and set new SP */
1246 key_freesp(*pcb_sp
);
1248 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1249 printf("ipsec_set_policy: new policy\n");
1250 kdebug_secpolicy(newsp
));
1256 ipsec_get_policy(pcb_sp
, mp
)
1257 struct secpolicy
*pcb_sp
;
1262 if (pcb_sp
== NULL
|| mp
== NULL
)
1265 *mp
= key_sp2msg(pcb_sp
);
1267 ipseclog((LOG_DEBUG
, "ipsec_get_policy: No more memory.\n"));
1271 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
1272 (*mp
)->m_type
= MT_DATA
;
1274 (*mp
)->m_type
= MT_SOOPTS
;
1276 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1277 printf("ipsec_get_policy:\n");
1284 ipsec4_set_policy(inp
, optname
, request
, len
, priv
)
1291 struct sadb_x_policy
*xpl
;
1292 struct secpolicy
**pcb_sp
;
1295 if (inp
== NULL
|| request
== NULL
)
1297 if (len
< sizeof(*xpl
))
1299 xpl
= (struct sadb_x_policy
*)request
;
1301 /* select direction */
1302 switch (xpl
->sadb_x_policy_dir
) {
1303 case IPSEC_DIR_INBOUND
:
1304 pcb_sp
= &inp
->inp_sp
->sp_in
;
1306 case IPSEC_DIR_OUTBOUND
:
1307 pcb_sp
= &inp
->inp_sp
->sp_out
;
1310 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1311 xpl
->sadb_x_policy_dir
));
1315 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1319 ipsec4_get_policy(inp
, request
, len
, mp
)
1325 struct sadb_x_policy
*xpl
;
1326 struct secpolicy
*pcb_sp
;
1329 if (inp
== NULL
|| request
== NULL
|| mp
== NULL
)
1331 if (inp
->inp_sp
== NULL
)
1332 panic("policy in PCB is NULL\n");
1333 if (len
< sizeof(*xpl
))
1335 xpl
= (struct sadb_x_policy
*)request
;
1337 /* select direction */
1338 switch (xpl
->sadb_x_policy_dir
) {
1339 case IPSEC_DIR_INBOUND
:
1340 pcb_sp
= inp
->inp_sp
->sp_in
;
1342 case IPSEC_DIR_OUTBOUND
:
1343 pcb_sp
= inp
->inp_sp
->sp_out
;
1346 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1347 xpl
->sadb_x_policy_dir
));
1351 return ipsec_get_policy(pcb_sp
, mp
);
1354 /* delete policy in PCB */
1356 ipsec4_delete_pcbpolicy(inp
)
1361 panic("ipsec4_delete_pcbpolicy: NULL pointer was passed.\n");
1363 if (inp
->inp_sp
== NULL
)
1366 if (inp
->inp_sp
->sp_in
!= NULL
) {
1367 key_freesp(inp
->inp_sp
->sp_in
);
1368 inp
->inp_sp
->sp_in
= NULL
;
1371 if (inp
->inp_sp
->sp_out
!= NULL
) {
1372 key_freesp(inp
->inp_sp
->sp_out
);
1373 inp
->inp_sp
->sp_out
= NULL
;
1376 ipsec_delpcbpolicy(inp
->inp_sp
);
1384 ipsec6_set_policy(in6p
, optname
, request
, len
, priv
)
1385 struct in6pcb
*in6p
;
1391 struct sadb_x_policy
*xpl
;
1392 struct secpolicy
**pcb_sp
;
1395 if (in6p
== NULL
|| request
== NULL
)
1397 if (len
< sizeof(*xpl
))
1399 xpl
= (struct sadb_x_policy
*)request
;
1401 /* select direction */
1402 switch (xpl
->sadb_x_policy_dir
) {
1403 case IPSEC_DIR_INBOUND
:
1404 pcb_sp
= &in6p
->in6p_sp
->sp_in
;
1406 case IPSEC_DIR_OUTBOUND
:
1407 pcb_sp
= &in6p
->in6p_sp
->sp_out
;
1410 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1411 xpl
->sadb_x_policy_dir
));
1415 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1419 ipsec6_get_policy(in6p
, request
, len
, mp
)
1420 struct in6pcb
*in6p
;
1425 struct sadb_x_policy
*xpl
;
1426 struct secpolicy
*pcb_sp
;
1429 if (in6p
== NULL
|| request
== NULL
|| mp
== NULL
)
1431 if (in6p
->in6p_sp
== NULL
)
1432 panic("policy in PCB is NULL\n");
1433 if (len
< sizeof(*xpl
))
1435 xpl
= (struct sadb_x_policy
*)request
;
1437 /* select direction */
1438 switch (xpl
->sadb_x_policy_dir
) {
1439 case IPSEC_DIR_INBOUND
:
1440 pcb_sp
= in6p
->in6p_sp
->sp_in
;
1442 case IPSEC_DIR_OUTBOUND
:
1443 pcb_sp
= in6p
->in6p_sp
->sp_out
;
1446 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1447 xpl
->sadb_x_policy_dir
));
1451 return ipsec_get_policy(pcb_sp
, mp
);
1455 ipsec6_delete_pcbpolicy(in6p
)
1456 struct in6pcb
*in6p
;
1460 panic("ipsec6_delete_pcbpolicy: NULL pointer was passed.\n");
1462 if (in6p
->in6p_sp
== NULL
)
1465 if (in6p
->in6p_sp
->sp_in
!= NULL
) {
1466 key_freesp(in6p
->in6p_sp
->sp_in
);
1467 in6p
->in6p_sp
->sp_in
= NULL
;
1470 if (in6p
->in6p_sp
->sp_out
!= NULL
) {
1471 key_freesp(in6p
->in6p_sp
->sp_out
);
1472 in6p
->in6p_sp
->sp_out
= NULL
;
1475 ipsec_delpcbpolicy(in6p
->in6p_sp
);
1476 in6p
->in6p_sp
= NULL
;
1483 * return current level.
1484 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1487 ipsec_get_reqlevel(isr
)
1488 struct ipsecrequest
*isr
;
1491 u_int esp_trans_deflev
, esp_net_deflev
, ah_trans_deflev
, ah_net_deflev
;
1494 if (isr
== NULL
|| isr
->sp
== NULL
)
1495 panic("ipsec_get_reqlevel: NULL pointer is passed.\n");
1496 if (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
1497 != ((struct sockaddr
*)&isr
->sp
->spidx
.dst
)->sa_family
)
1498 panic("ipsec_get_reqlevel: family mismatched.\n");
1500 /* XXX note that we have ipseclog() expanded here - code sync issue */
1501 #define IPSEC_CHECK_DEFAULT(lev) \
1502 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1503 && (lev) != IPSEC_LEVEL_UNIQUE) \
1505 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1506 (lev), IPSEC_LEVEL_REQUIRE) \
1508 (lev) = IPSEC_LEVEL_REQUIRE, \
1512 /* set default level */
1513 switch (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
) {
1516 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev
);
1517 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev
);
1518 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev
);
1519 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev
);
1524 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev
);
1525 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev
);
1526 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev
);
1527 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev
);
1531 panic("key_get_reqlevel: Unknown family. %d\n",
1532 ((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
);
1535 #undef IPSEC_CHECK_DEFAULT(lev)
1538 switch (isr
->level
) {
1539 case IPSEC_LEVEL_DEFAULT
:
1540 switch (isr
->saidx
.proto
) {
1542 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1543 level
= esp_net_deflev
;
1545 level
= esp_trans_deflev
;
1548 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1549 level
= ah_net_deflev
;
1551 level
= ah_trans_deflev
;
1552 case IPPROTO_IPCOMP
:
1554 * we don't really care, as IPcomp document says that
1555 * we shouldn't compress small packets
1557 level
= IPSEC_LEVEL_USE
;
1560 panic("ipsec_get_reqlevel: "
1561 "Illegal protocol defined %u\n",
1566 case IPSEC_LEVEL_USE
:
1567 case IPSEC_LEVEL_REQUIRE
:
1570 case IPSEC_LEVEL_UNIQUE
:
1571 level
= IPSEC_LEVEL_REQUIRE
;
1575 panic("ipsec_get_reqlevel: Illegal IPsec level %u\n",
1583 * Check AH/ESP integrity.
1589 ipsec_in_reject(sp
, m
)
1590 struct secpolicy
*sp
;
1593 struct ipsecrequest
*isr
;
1595 int need_auth
, need_conf
, need_icv
;
1597 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1598 printf("ipsec_in_reject: using SP\n");
1599 kdebug_secpolicy(sp
));
1602 switch (sp
->policy
) {
1603 case IPSEC_POLICY_DISCARD
:
1605 case IPSEC_POLICY_BYPASS
:
1606 case IPSEC_POLICY_NONE
:
1609 case IPSEC_POLICY_IPSEC
:
1612 case IPSEC_POLICY_ENTRUST
:
1614 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1621 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1623 /* get current level */
1624 level
= ipsec_get_reqlevel(isr
);
1626 switch (isr
->saidx
.proto
) {
1628 if (level
== IPSEC_LEVEL_REQUIRE
) {
1631 if (isr
->sav
!= NULL
1632 && isr
->sav
->flags
== SADB_X_EXT_NONE
1633 && isr
->sav
->alg_auth
!= SADB_AALG_NONE
)
1638 if (level
== IPSEC_LEVEL_REQUIRE
) {
1643 case IPPROTO_IPCOMP
:
1645 * we don't really care, as IPcomp document says that
1646 * we shouldn't compress small packets
1652 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1653 printf("ipsec_in_reject: auth:%d conf:%d icv:%d m_flags:%x\n",
1654 need_auth
, need_conf
, need_icv
, m
->m_flags
));
1656 if ((need_conf
&& !(m
->m_flags
& M_DECRYPTED
))
1657 || (!need_auth
&& need_icv
&& !(m
->m_flags
& M_AUTHIPDGM
))
1658 || (need_auth
&& !(m
->m_flags
& M_AUTHIPHDR
)))
1665 * Check AH/ESP integrity.
1666 * This function is called from tcp_input(), udp_input(),
1667 * and {ah,esp}4_input for tunnel mode
1670 ipsec4_in_reject_so(m
, so
)
1674 struct secpolicy
*sp
= NULL
;
1680 return 0; /* XXX should be panic ? */
1682 /* get SP for this packet.
1683 * When we are called from ip_forward(), we call
1684 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1687 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1689 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1692 return 0; /* XXX should be panic ?
1693 * -> No, there may be error. */
1695 result
= ipsec_in_reject(sp
, m
);
1696 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1697 printf("DP ipsec4_in_reject_so call free SP:%p\n", sp
));
1704 ipsec4_in_reject(m
, inp
)
1709 return ipsec4_in_reject_so(m
, NULL
);
1711 if (inp
->inp_socket
)
1712 return ipsec4_in_reject_so(m
, inp
->inp_socket
);
1714 panic("ipsec4_in_reject: invalid inpcb/socket");
1720 * Check AH/ESP integrity.
1721 * This function is called from tcp6_input(), udp6_input(),
1722 * and {ah,esp}6_input for tunnel mode
1725 ipsec6_in_reject_so(m
, so
)
1729 struct secpolicy
*sp
= NULL
;
1735 return 0; /* XXX should be panic ? */
1737 /* get SP for this packet.
1738 * When we are called from ip_forward(), we call
1739 * ipsec6_getpolicybyaddr() with IP_FORWARDING flag.
1742 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1744 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1747 return 0; /* XXX should be panic ? */
1749 result
= ipsec_in_reject(sp
, m
);
1750 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1751 printf("DP ipsec6_in_reject_so call free SP:%p\n", sp
));
1758 ipsec6_in_reject(m
, in6p
)
1760 struct in6pcb
*in6p
;
1763 return ipsec6_in_reject_so(m
, NULL
);
1765 if (in6p
->in6p_socket
)
1766 return ipsec6_in_reject_so(m
, in6p
->in6p_socket
);
1768 panic("ipsec6_in_reject: invalid in6p/socket");
1774 * compute the byte size to be occupied by IPsec header.
1775 * in case it is tunneled, it includes the size of outer IP header.
1776 * NOTE: SP passed is free in this function.
1780 struct secpolicy
*sp
;
1782 struct ipsecrequest
*isr
;
1785 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1786 printf("ipsec_in_reject: using SP\n");
1787 kdebug_secpolicy(sp
));
1790 switch (sp
->policy
) {
1791 case IPSEC_POLICY_DISCARD
:
1792 case IPSEC_POLICY_BYPASS
:
1793 case IPSEC_POLICY_NONE
:
1796 case IPSEC_POLICY_IPSEC
:
1799 case IPSEC_POLICY_ENTRUST
:
1801 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1806 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1810 switch (isr
->saidx
.proto
) {
1813 clen
= esp_hdrsiz(isr
);
1819 clen
= ah_hdrsiz(isr
);
1821 case IPPROTO_IPCOMP
:
1822 clen
= sizeof(struct ipcomp
);
1826 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
1827 switch (((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
) {
1829 clen
+= sizeof(struct ip
);
1833 clen
+= sizeof(struct ip6_hdr
);
1837 ipseclog((LOG_ERR
, "ipsec_hdrsiz: "
1838 "unknown AF %d in IPsec tunnel SA\n",
1839 ((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
));
1849 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1851 ipsec4_hdrsiz(m
, dir
, inp
)
1856 struct secpolicy
*sp
= NULL
;
1862 return 0; /* XXX should be panic ? */
1863 if (inp
!= NULL
&& inp
->inp_socket
== NULL
)
1864 panic("ipsec4_hdrsize: why is socket NULL but there is PCB.");
1866 /* get SP for this packet.
1867 * When we are called from ip_forward(), we call
1868 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1871 sp
= ipsec4_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
1873 sp
= ipsec4_getpolicybysock(m
, dir
, inp
->inp_socket
, &error
);
1876 return 0; /* XXX should be panic ? */
1878 size
= ipsec_hdrsiz(sp
);
1879 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1880 printf("DP ipsec4_hdrsiz call free SP:%p\n", sp
));
1881 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1882 printf("ipsec4_hdrsiz: size:%lu.\n", (unsigned long)size
));
1889 /* This function is called from ipsec6_hdrsize_tcp(),
1890 * and maybe from ip6_forward.()
1893 ipsec6_hdrsiz(m
, dir
, in6p
)
1896 struct in6pcb
*in6p
;
1898 struct secpolicy
*sp
= NULL
;
1904 return 0; /* XXX shoud be panic ? */
1905 if (in6p
!= NULL
&& in6p
->in6p_socket
== NULL
)
1906 panic("ipsec6_hdrsize: why is socket NULL but there is PCB.");
1908 /* get SP for this packet */
1909 /* XXX Is it right to call with IP_FORWARDING. */
1911 sp
= ipsec6_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
1913 sp
= ipsec6_getpolicybysock(m
, dir
, in6p
->in6p_socket
, &error
);
1917 size
= ipsec_hdrsiz(sp
);
1918 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1919 printf("DP ipsec6_hdrsiz call free SP:%p\n", sp
));
1920 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1921 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size
));
1930 * encapsulate for ipsec tunnel.
1931 * ip->ip_src must be fixed later on.
1934 ipsec4_encapsulate(m
, sav
)
1936 struct secasvar
*sav
;
1943 /* can't tunnel between different AFs */
1944 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
1945 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
1946 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
1951 /* XXX if the dst is myself, perform nothing. */
1952 if (key_ismyaddr(AF_INET
, _INADDRBYSA(&sav
->sah
->saidx
.dst
))) {
1958 if (m
->m_len
< sizeof(*ip
))
1959 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
1961 ip
= mtod(m
, struct ip
*);
1963 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1965 hlen
= ip
->ip_hl
<< 2;
1968 if (m
->m_len
!= hlen
)
1969 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
1971 /* generate header checksum */
1974 ip
->ip_sum
= in_cksum(m
, hlen
);
1976 ip
->ip_sum
= in_cksum(m
, hlen
);
1979 plen
= m
->m_pkthdr
.len
;
1982 * grow the mbuf to accomodate the new IPv4 header.
1983 * NOTE: IPv4 options will never be copied.
1985 if (M_LEADINGSPACE(m
->m_next
) < hlen
) {
1987 MGET(n
, M_DONTWAIT
, MT_DATA
);
1993 n
->m_next
= m
->m_next
;
1995 m
->m_pkthdr
.len
+= hlen
;
1996 oip
= mtod(n
, struct ip
*);
1998 m
->m_next
->m_len
+= hlen
;
1999 m
->m_next
->m_data
-= hlen
;
2000 m
->m_pkthdr
.len
+= hlen
;
2001 oip
= mtod(m
->m_next
, struct ip
*);
2003 ip
= mtod(m
, struct ip
*);
2004 ovbcopy((caddr_t
)ip
, (caddr_t
)oip
, hlen
);
2005 m
->m_len
= sizeof(struct ip
);
2006 m
->m_pkthdr
.len
-= (hlen
- sizeof(struct ip
));
2008 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
2009 /* ECN consideration. */
2010 ip_ecn_ingress(ip4_ipsec_ecn
, &ip
->ip_tos
, &oip
->ip_tos
);
2012 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2014 ip
->ip_hl
= sizeof(struct ip
) >> 2;
2016 ip
->ip_off
&= htons(~IP_OFFMASK
);
2017 ip
->ip_off
&= htons(~IP_MF
);
2018 switch (ip4_ipsec_dfbit
) {
2019 case 0: /*clear DF bit*/
2020 ip
->ip_off
&= htons(~IP_DF
);
2022 case 1: /*set DF bit*/
2023 ip
->ip_off
|= htons(IP_DF
);
2025 default: /*copy DF bit*/
2028 ip
->ip_p
= IPPROTO_IPIP
;
2029 if (plen
+ sizeof(struct ip
) < IP_MAXPACKET
)
2030 ip
->ip_len
= htons(plen
+ sizeof(struct ip
));
2032 ipseclog((LOG_ERR
, "IPv4 ipsec: size exceeds limit: "
2033 "leave ip_len as is (invalid packet)\n"));
2035 ip
->ip_id
= htons(ip_id
++);
2036 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
,
2037 &ip
->ip_src
, sizeof(ip
->ip_src
));
2038 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
,
2039 &ip
->ip_dst
, sizeof(ip
->ip_dst
));
2041 /* XXX Should ip_src be updated later ? */
2049 ipsec6_encapsulate(m
, sav
)
2051 struct secasvar
*sav
;
2053 struct ip6_hdr
*oip6
;
2054 struct ip6_hdr
*ip6
;
2057 /* can't tunnel between different AFs */
2058 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2059 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2060 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
2065 /* XXX if the dst is myself, perform nothing. */
2066 if (key_ismyaddr(AF_INET6
, _INADDRBYSA(&sav
->sah
->saidx
.dst
))) {
2072 plen
= m
->m_pkthdr
.len
;
2075 * grow the mbuf to accomodate the new IPv6 header.
2077 if (m
->m_len
!= sizeof(struct ip6_hdr
))
2078 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2079 if (M_LEADINGSPACE(m
->m_next
) < sizeof(struct ip6_hdr
)) {
2081 MGET(n
, M_DONTWAIT
, MT_DATA
);
2086 n
->m_len
= sizeof(struct ip6_hdr
);
2087 n
->m_next
= m
->m_next
;
2089 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2090 oip6
= mtod(n
, struct ip6_hdr
*);
2092 m
->m_next
->m_len
+= sizeof(struct ip6_hdr
);
2093 m
->m_next
->m_data
-= sizeof(struct ip6_hdr
);
2094 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2095 oip6
= mtod(m
->m_next
, struct ip6_hdr
*);
2097 ip6
= mtod(m
, struct ip6_hdr
*);
2098 ovbcopy((caddr_t
)ip6
, (caddr_t
)oip6
, sizeof(struct ip6_hdr
));
2100 /* Fake link-local scope-class addresses */
2101 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
2102 oip6
->ip6_src
.s6_addr16
[1] = 0;
2103 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
2104 oip6
->ip6_dst
.s6_addr16
[1] = 0;
2106 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
2107 /* ECN consideration. */
2108 ip6_ecn_ingress(ip6_ipsec_ecn
, &ip6
->ip6_flow
, &oip6
->ip6_flow
);
2109 if (plen
< IPV6_MAXPACKET
- sizeof(struct ip6_hdr
))
2110 ip6
->ip6_plen
= htons(plen
);
2112 /* ip6->ip6_plen will be updated in ip6_output() */
2114 ip6
->ip6_nxt
= IPPROTO_IPV6
;
2115 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.src
)->sin6_addr
,
2116 &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
2117 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
)->sin6_addr
,
2118 &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
2120 /* XXX Should ip6_src be updated later ? */
2127 * Check the variable replay window.
2128 * ipsec_chkreplay() performs replay check before ICV verification.
2129 * ipsec_updatereplay() updates replay bitmap. This must be called after
2130 * ICV verification (it also performs replay check, which is usually done
2132 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2134 * based on RFC 2401.
2137 ipsec_chkreplay(seq
, sav
)
2139 struct secasvar
*sav
;
2141 const struct secreplay
*replay
;
2144 u_int32_t wsizeb
; /* constant: bits of window size */
2145 int frlast
; /* constant: last frame */
2149 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2151 replay
= sav
->replay
;
2153 if (replay
->wsize
== 0)
2154 return 1; /* no need to check replay. */
2157 frlast
= replay
->wsize
- 1;
2158 wsizeb
= replay
->wsize
<< 3;
2160 /* sequence number of 0 is invalid */
2164 /* first time is always okay */
2165 if (replay
->count
== 0)
2168 if (seq
> replay
->lastseq
) {
2169 /* larger sequences are okay */
2172 /* seq is equal or less than lastseq. */
2173 diff
= replay
->lastseq
- seq
;
2175 /* over range to check, i.e. too old or wrapped */
2179 fr
= frlast
- diff
/ 8;
2181 /* this packet already seen ? */
2182 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2185 /* out of order but good */
2191 * check replay counter whether to update or not.
2196 ipsec_updatereplay(seq
, sav
)
2198 struct secasvar
*sav
;
2200 struct secreplay
*replay
;
2203 u_int32_t wsizeb
; /* constant: bits of window size */
2204 int frlast
; /* constant: last frame */
2208 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2210 replay
= sav
->replay
;
2212 if (replay
->wsize
== 0)
2213 goto ok
; /* no need to check replay. */
2216 frlast
= replay
->wsize
- 1;
2217 wsizeb
= replay
->wsize
<< 3;
2219 /* sequence number of 0 is invalid */
2224 if (replay
->count
== 0) {
2225 replay
->lastseq
= seq
;
2226 bzero(replay
->bitmap
, replay
->wsize
);
2227 (replay
->bitmap
)[frlast
] = 1;
2231 if (seq
> replay
->lastseq
) {
2232 /* seq is larger than lastseq. */
2233 diff
= seq
- replay
->lastseq
;
2235 /* new larger sequence number */
2236 if (diff
< wsizeb
) {
2238 /* set bit for this packet */
2239 vshiftl(replay
->bitmap
, diff
, replay
->wsize
);
2240 (replay
->bitmap
)[frlast
] |= 1;
2242 /* this packet has a "way larger" */
2243 bzero(replay
->bitmap
, replay
->wsize
);
2244 (replay
->bitmap
)[frlast
] = 1;
2246 replay
->lastseq
= seq
;
2248 /* larger is good */
2250 /* seq is equal or less than lastseq. */
2251 diff
= replay
->lastseq
- seq
;
2253 /* over range to check, i.e. too old or wrapped */
2257 fr
= frlast
- diff
/ 8;
2259 /* this packet already seen ? */
2260 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2264 (replay
->bitmap
)[fr
] |= (1 << (diff
% 8));
2266 /* out of order but good */
2270 if (replay
->count
== ~0) {
2272 /* set overflow flag */
2275 /* don't increment, no more packets accepted */
2276 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0)
2279 ipseclog((LOG_WARNING
, "replay counter made %d cycle. %s\n",
2280 replay
->overflow
, ipsec_logsastr(sav
)));
2289 * shift variable length bunffer to left.
2290 * IN: bitmap: pointer to the buffer
2291 * nbit: the number of to shift.
2292 * wsize: buffer size (bytes).
2295 vshiftl(bitmap
, nbit
, wsize
)
2296 unsigned char *bitmap
;
2302 for (j
= 0; j
< nbit
; j
+= 8) {
2303 s
= (nbit
- j
< 8) ? (nbit
- j
): 8;
2305 for (i
= 1; i
< wsize
; i
++) {
2306 over
= (bitmap
[i
] >> (8 - s
));
2308 bitmap
[i
-1] |= over
;
2316 ipsec4_logpacketstr(ip
, spi
)
2320 static char buf
[256];
2324 s
= (u_int8_t
*)(&ip
->ip_src
);
2325 d
= (u_int8_t
*)(&ip
->ip_dst
);
2328 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2331 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%d.%d.%d.%d",
2332 s
[0], s
[1], s
[2], s
[3]);
2335 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%d.%d.%d.%d",
2336 d
[0], d
[1], d
[2], d
[3]);
2339 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2346 ipsec6_logpacketstr(ip6
, spi
)
2347 struct ip6_hdr
*ip6
;
2350 static char buf
[256];
2354 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2357 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%s",
2358 ip6_sprintf(&ip6
->ip6_src
));
2361 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%s",
2362 ip6_sprintf(&ip6
->ip6_dst
));
2365 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2373 struct secasvar
*sav
;
2375 static char buf
[256];
2377 struct secasindex
*saidx
= &sav
->sah
->saidx
;
2379 /* validity check */
2380 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2381 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
)
2382 panic("ipsec_logsastr: family mismatched.\n");
2385 snprintf(buf
, sizeof(buf
), "SA(SPI=%u ", (u_int32_t
)ntohl(sav
->spi
));
2388 if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET
) {
2390 s
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->src
)->sin_addr
;
2391 d
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->dst
)->sin_addr
;
2392 snprintf(p
, sizeof(buf
) - (p
- buf
),
2393 "src=%d.%d.%d.%d dst=%d.%d.%d.%d",
2394 s
[0], s
[1], s
[2], s
[3], d
[0], d
[1], d
[2], d
[3]);
2397 else if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET6
) {
2398 snprintf(p
, sizeof(buf
) - (p
- buf
),
2400 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->src
)->sin6_addr
));
2403 snprintf(p
, sizeof(buf
) - (p
- buf
),
2405 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->dst
)->sin6_addr
));
2410 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2426 p
= mtod(m
, u_char
*);
2427 for (i
= 0; i
< m
->m_len
; i
++) {
2428 printf("%02x ", p
[i
]);
2430 if (totlen
% 16 == 0)
2435 if (totlen
% 16 != 0)
2441 * IPsec output logic for IPv4.
2444 ipsec4_output(state
, sp
, flags
)
2445 struct ipsec_output_state
*state
;
2446 struct secpolicy
*sp
;
2449 struct ip
*ip
= NULL
;
2450 struct ipsecrequest
*isr
= NULL
;
2451 struct secasindex saidx
;
2455 struct in_ifaddr
*ia
;
2457 struct sockaddr_in
*dst4
;
2458 struct sockaddr
*sa
;
2461 panic("state == NULL in ipsec4_output");
2463 panic("state->m == NULL in ipsec4_output");
2465 panic("state->ro == NULL in ipsec4_output");
2467 panic("state->dst == NULL in ipsec4_output");
2469 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2470 printf("ipsec4_output: applyed SP\n");
2471 kdebug_secpolicy(sp
));
2473 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2475 #if 0 /* give up to check restriction of transport mode */
2476 /* XXX but should be checked somewhere */
2478 * some of the IPsec operation must be performed only in
2481 if (isr
->saidx
.mode
== IPSEC_MODE_TRANSPORT
2482 && (flags
& IP_FORWARDING
))
2486 /* make SA index for search proper SA */
2487 ip
= mtod(state
->m
, struct ip
*);
2488 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2489 sa
= (struct sockaddr
*)&saidx
.src
;
2490 if (sa
->sa_len
== 0) {
2491 sa
->sa_len
= _SALENBYAF(AF_INET
);
2492 sa
->sa_family
= AF_INET
;
2493 _INPORTBYSA(&saidx
.src
) = IPSEC_PORT_ANY
;
2494 bcopy(&ip
->ip_src
, _INADDRBYSA(&saidx
.src
),
2495 sizeof(ip
->ip_src
));
2497 sa
= (struct sockaddr
*)&saidx
.dst
;
2498 if (sa
->sa_len
== 0) {
2499 sa
->sa_len
= _SALENBYAF(AF_INET
);
2500 sa
->sa_family
= AF_INET
;
2501 _INPORTBYSA(&saidx
.dst
) = IPSEC_PORT_ANY
;
2502 bcopy(&ip
->ip_dst
, _INADDRBYSA(&saidx
.dst
),
2503 sizeof(ip
->ip_dst
));
2506 if ((error
= key_checkrequest(isr
, &saidx
)) != 0) {
2508 * IPsec processing is required, but no SA found.
2509 * I assume that key_acquire() had been called
2510 * to get/establish the SA. Here I discard
2511 * this packet because it is responsibility for
2512 * upper layer to retransmit the packet.
2514 ipsecstat
.out_nosa
++;
2518 /* validity check */
2519 if (isr
->sav
== NULL
) {
2520 switch (ipsec_get_reqlevel(isr
)) {
2521 case IPSEC_LEVEL_USE
:
2523 case IPSEC_LEVEL_REQUIRE
:
2524 /* must be not reached here. */
2525 panic("ipsec4_output: no SA found, but required.");
2530 * If there is no valid SA, we give up to process any
2531 * more. In such a case, the SA's status is changed
2532 * from DYING to DEAD after allocating. If a packet
2533 * send to the receiver by dead SA, the receiver can
2534 * not decode a packet because SA has been dead.
2536 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2537 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2538 ipsecstat
.out_nosa
++;
2544 * There may be the case that SA status will be changed when
2545 * we are refering to one. So calling splsoftnet().
2553 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2555 * build IPsec tunnel.
2557 /* XXX should be processed with other familiy */
2558 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2559 ipseclog((LOG_ERR
, "ipsec4_output: "
2560 "family mismatched between inner and outer spi=%u\n",
2561 (u_int32_t
)ntohl(isr
->sav
->spi
)));
2563 error
= EAFNOSUPPORT
;
2567 ip
= mtod(state
->m
, struct ip
*);
2569 state
->m
= ipsec4_splithdr(state
->m
);
2575 error
= ipsec4_encapsulate(state
->m
, isr
->sav
);
2581 ip
= mtod(state
->m
, struct ip
*);
2583 state
->ro
= &isr
->sav
->sah
->sa_route
;
2584 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
2585 dst4
= (struct sockaddr_in
*)state
->dst
;
2586 if (state
->ro
->ro_rt
2587 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
2588 || dst4
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
2589 RTFREE(state
->ro
->ro_rt
);
2590 bzero((caddr_t
)state
->ro
, sizeof (*state
->ro
));
2592 if (state
->ro
->ro_rt
== 0) {
2593 dst4
->sin_family
= AF_INET
;
2594 dst4
->sin_len
= sizeof(*dst4
);
2595 dst4
->sin_addr
= ip
->ip_dst
;
2598 if (state
->ro
->ro_rt
== 0) {
2599 ipstat
.ips_noroute
++;
2600 error
= EHOSTUNREACH
;
2606 * Which address in SA or in routing table should I
2607 * select from ? But I had set from SA at
2608 * ipsec4_encapsulate().
2610 ia
= (struct in_ifaddr
*)(state
->ro
->ro_rt
->rt_ifa
);
2611 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
2612 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
2613 dst4
= (struct sockaddr_in
*)state
->dst
;
2615 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
2620 state
->m
= ipsec4_splithdr(state
->m
);
2625 switch (isr
->saidx
.proto
) {
2628 if ((error
= esp4_output(state
->m
, isr
)) != 0) {
2640 if ((error
= ah4_output(state
->m
, isr
)) != 0) {
2645 case IPPROTO_IPCOMP
:
2646 if ((error
= ipcomp4_output(state
->m
, isr
)) != 0) {
2653 "ipsec4_output: unknown ipsec protocol %d\n",
2661 if (state
->m
== 0) {
2665 ip
= mtod(state
->m
, struct ip
*);
2678 * IPsec output logic for IPv6, transport mode.
2681 ipsec6_output_trans(state
, nexthdrp
, mprev
, sp
, flags
, tun
)
2682 struct ipsec_output_state
*state
;
2685 struct secpolicy
*sp
;
2689 struct ip6_hdr
*ip6
;
2690 struct ipsecrequest
*isr
= NULL
;
2691 struct secasindex saidx
;
2694 struct sockaddr
*sa
;
2697 panic("state == NULL in ipsec6_output");
2699 panic("state->m == NULL in ipsec6_output");
2701 panic("nexthdrp == NULL in ipsec6_output");
2703 panic("mprev == NULL in ipsec6_output");
2705 panic("sp == NULL in ipsec6_output");
2707 panic("tun == NULL in ipsec6_output");
2709 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2710 printf("ipsec6_output_trans: applyed SP\n");
2711 kdebug_secpolicy(sp
));
2714 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
2715 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2716 /* the rest will be handled by ipsec6_output_tunnel() */
2720 /* make SA index for search proper SA */
2721 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2722 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2723 sa
= (struct sockaddr
*)&saidx
.src
;
2724 if (sa
->sa_len
== 0) {
2725 sa
->sa_len
= _SALENBYAF(AF_INET6
);
2726 sa
->sa_family
= AF_INET6
;
2727 _INPORTBYSA(&saidx
.src
) = IPSEC_PORT_ANY
;
2728 bcopy(&ip6
->ip6_src
, _INADDRBYSA(&saidx
.src
),
2729 sizeof(ip6
->ip6_src
));
2731 sa
= (struct sockaddr
*)&saidx
.dst
;
2732 if (sa
->sa_len
== 0) {
2733 sa
->sa_len
= _SALENBYAF(AF_INET6
);
2734 sa
->sa_family
= AF_INET6
;
2735 _INPORTBYSA(&saidx
.dst
) = IPSEC_PORT_ANY
;
2736 bcopy(&ip6
->ip6_dst
, _INADDRBYSA(&saidx
.dst
),
2737 sizeof(ip6
->ip6_dst
));
2740 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
2742 * IPsec processing is required, but no SA found.
2743 * I assume that key_acquire() had been called
2744 * to get/establish the SA. Here I discard
2745 * this packet because it is responsibility for
2746 * upper layer to retransmit the packet.
2748 ipsec6stat
.out_nosa
++;
2753 /* validity check */
2754 if (isr
->sav
== NULL
) {
2755 switch (ipsec_get_reqlevel(isr
)) {
2756 case IPSEC_LEVEL_USE
:
2758 case IPSEC_LEVEL_REQUIRE
:
2759 /* must be not reached here. */
2760 panic("ipsec6_output_trans: no SA found, but required.");
2765 * If there is no valid SA, we give up to process.
2766 * see same place at ipsec4_output().
2768 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2769 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2770 ipsec6stat
.out_nosa
++;
2775 switch (isr
->saidx
.proto
) {
2778 error
= esp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2785 error
= ah6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2787 case IPPROTO_IPCOMP
:
2788 error
= ipcomp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2791 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
2792 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
2794 ipsec6stat
.out_inval
++;
2802 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2803 if (plen
> IPV6_MAXPACKET
) {
2804 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
2805 "IPsec with IPv6 jumbogram is not supported\n"));
2806 ipsec6stat
.out_inval
++;
2807 error
= EINVAL
; /*XXX*/
2810 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2811 ip6
->ip6_plen
= htons(plen
);
2814 /* if we have more to go, we need a tunnel mode processing */
2827 * IPsec output logic for IPv6, tunnel mode.
2830 ipsec6_output_tunnel(state
, sp
, flags
)
2831 struct ipsec_output_state
*state
;
2832 struct secpolicy
*sp
;
2835 struct ip6_hdr
*ip6
;
2836 struct ipsecrequest
*isr
= NULL
;
2837 struct secasindex saidx
;
2841 struct in6_addr
*ia6
;
2843 struct sockaddr_in6
* dst6
;
2847 panic("state == NULL in ipsec6_output");
2849 panic("state->m == NULL in ipsec6_output");
2851 panic("sp == NULL in ipsec6_output");
2853 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2854 printf("ipsec6_output_tunnel: applyed SP\n");
2855 kdebug_secpolicy(sp
));
2858 * transport mode ipsec (before the 1st tunnel mode) is already
2859 * processed by ipsec6_output_trans().
2861 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
2862 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
2866 for (/*already initialized*/; isr
; isr
= isr
->next
) {
2867 /* When tunnel mode, SA peers must be specified. */
2868 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2869 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
2871 * IPsec processing is required, but no SA found.
2872 * I assume that key_acquire() had been called
2873 * to get/establish the SA. Here I discard
2874 * this packet because it is responsibility for
2875 * upper layer to retransmit the packet.
2877 ipsec6stat
.out_nosa
++;
2882 /* validity check */
2883 if (isr
->sav
== NULL
) {
2884 switch (ipsec_get_reqlevel(isr
)) {
2885 case IPSEC_LEVEL_USE
:
2887 case IPSEC_LEVEL_REQUIRE
:
2888 /* must be not reached here. */
2889 panic("ipsec6_output_tunnel: no SA found, but required.");
2894 * If there is no valid SA, we give up to process.
2895 * see same place at ipsec4_output().
2897 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2898 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2899 ipsec6stat
.out_nosa
++;
2905 * There may be the case that SA status will be changed when
2906 * we are refering to one. So calling splsoftnet().
2914 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2916 * build IPsec tunnel.
2918 /* XXX should be processed with other familiy */
2919 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
2920 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
2921 "family mismatched between inner and outer, spi=%u\n",
2922 (u_int32_t
)ntohl(isr
->sav
->spi
)));
2924 ipsec6stat
.out_inval
++;
2925 error
= EAFNOSUPPORT
;
2929 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2931 state
->m
= ipsec6_splithdr(state
->m
);
2934 ipsec6stat
.out_nomem
++;
2938 error
= ipsec6_encapsulate(state
->m
, isr
->sav
);
2944 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2946 state
->ro
= &isr
->sav
->sah
->sa_route
;
2947 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
2948 dst6
= (struct sockaddr_in6
*)state
->dst
;
2949 if (state
->ro
->ro_rt
2950 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
2951 || !IN6_ARE_ADDR_EQUAL(&dst6
->sin6_addr
, &ip6
->ip6_dst
))) {
2952 RTFREE(state
->ro
->ro_rt
);
2953 bzero((caddr_t
)state
->ro
, sizeof (*state
->ro
));
2955 if (state
->ro
->ro_rt
== 0) {
2956 bzero(dst6
, sizeof(*dst6
));
2957 dst6
->sin6_family
= AF_INET6
;
2958 dst6
->sin6_len
= sizeof(*dst6
);
2959 dst6
->sin6_addr
= ip6
->ip6_dst
;
2962 if (state
->ro
->ro_rt
== 0) {
2963 ip6stat
.ip6s_noroute
++;
2964 ipsec6stat
.out_noroute
++;
2965 error
= EHOSTUNREACH
;
2968 #if 0 /* XXX Is the following need ? */
2969 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
2970 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
2971 dst6
= (struct sockaddr_in6
*)state
->dst
;
2976 * Which address in SA or in routing table should I
2977 * select from ? But I had set from SA at
2978 * ipsec6_encapsulate().
2980 ia6
= in6_selectsrc(dst6
, NULL
, NULL
,
2981 (struct route_in6
*)state
->ro
,
2984 ip6stat
.ip6s_noroute
++;
2985 ipsec6stat
.out_noroute
++;
2988 ip6
->ip6_src
= *ia6
;
2993 state
->m
= ipsec6_splithdr(state
->m
);
2995 ipsec6stat
.out_nomem
++;
2999 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3000 switch (isr
->saidx
.proto
) {
3003 error
= esp6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
3010 error
= ah6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
3012 case IPPROTO_IPCOMP
:
3013 /* XXX code should be here */
3016 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3017 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
3019 ipsec6stat
.out_inval
++;
3027 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
3028 if (plen
> IPV6_MAXPACKET
) {
3029 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3030 "IPsec with IPv6 jumbogram is not supported\n"));
3031 ipsec6stat
.out_inval
++;
3032 error
= EINVAL
; /*XXX*/
3035 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3036 ip6
->ip6_plen
= htons(plen
);
3049 * Chop IP header and option off from the payload.
3051 static struct mbuf
*
3059 if (m
->m_len
< sizeof(struct ip
))
3060 panic("ipsec4_splithdr: first mbuf too short");
3061 ip
= mtod(m
, struct ip
*);
3063 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
3065 hlen
= ip
->ip_hl
<< 2;
3067 if (m
->m_len
> hlen
) {
3068 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3073 M_COPY_PKTHDR(mh
, m
);
3075 m
->m_flags
&= ~M_PKTHDR
;
3081 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), hlen
);
3082 } else if (m
->m_len
< hlen
) {
3083 m
= m_pullup(m
, hlen
);
3091 static struct mbuf
*
3096 struct ip6_hdr
*ip6
;
3099 if (m
->m_len
< sizeof(struct ip6_hdr
))
3100 panic("ipsec6_splithdr: first mbuf too short");
3101 ip6
= mtod(m
, struct ip6_hdr
*);
3102 hlen
= sizeof(struct ip6_hdr
);
3103 if (m
->m_len
> hlen
) {
3104 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3109 M_COPY_PKTHDR(mh
, m
);
3111 m
->m_flags
&= ~M_PKTHDR
;
3117 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), hlen
);
3118 } else if (m
->m_len
< hlen
) {
3119 m
= m_pullup(m
, hlen
);
3127 /* validate inbound IPsec tunnel packet. */
3129 ipsec4_tunnel_validate(ip
, nxt0
, sav
)
3132 struct secasvar
*sav
;
3134 u_int8_t nxt
= nxt0
& 0xff;
3135 struct sockaddr_in
*sin
;
3138 if (nxt
!= IPPROTO_IPV4
)
3141 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
3143 hlen
= ip
->ip_hl
<< 2;
3145 if (hlen
!= sizeof(struct ip
))
3147 switch (((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
) {
3149 sin
= (struct sockaddr_in
*)&sav
->sah
->saidx
.dst
;
3150 if (bcmp(&ip
->ip_dst
, &sin
->sin_addr
, sizeof(ip
->ip_dst
)) != 0)
3155 /* should be supported, but at this moment we don't. */
3166 /* validate inbound IPsec tunnel packet. */
3168 ipsec6_tunnel_validate(ip6
, nxt0
, sav
)
3169 struct ip6_hdr
*ip6
;
3171 struct secasvar
*sav
;
3173 u_int8_t nxt
= nxt0
& 0xff;
3174 struct sockaddr_in6
*sin6
;
3176 if (nxt
!= IPPROTO_IPV6
)
3178 switch (((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
) {
3180 sin6
= ((struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
);
3181 if (!IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &sin6
->sin6_addr
))
3185 /* should be supported, but at this moment we don't. */
3196 * Make a mbuf chain for encryption.
3197 * If the original mbuf chain contains a mbuf with a cluster,
3198 * allocate a new cluster and copy the data to the new cluster.
3199 * XXX: this hack is inefficient, but is necessary to handle cases
3200 * of TCP retransmission...
3206 struct mbuf
*n
, **mpp
, *mnew
;
3208 for (n
= m
, mpp
= &m
; n
; n
= n
->m_next
) {
3209 if (n
->m_flags
& M_EXT
) {
3211 * Make a copy only if there are more than one references
3213 * XXX: is this approach effective?
3217 n
->m_ext
.ext_func
||
3219 n
->m_ext
.ext_free
||
3224 mclrefcnt
[mtocl(n
->m_ext
.ext_buf
)] > 1
3231 if (n
->m_flags
& M_PKTHDR
) {
3232 MGETHDR(mnew
, M_DONTWAIT
, MT_HEADER
);
3235 mnew
->m_pkthdr
= n
->m_pkthdr
;
3237 if (n
->m_pkthdr
.aux
) {
3238 mnew
->m_pkthdr
.aux
=
3239 m_copym(n
->m_pkthdr
.aux
,
3240 0, M_COPYALL
, M_DONTWAIT
);
3243 M_COPY_PKTHDR(mnew
, n
);
3244 mnew
->m_flags
= n
->m_flags
& M_COPYFLAGS
;
3247 MGET(mnew
, M_DONTWAIT
, MT_DATA
);
3255 * Copy data. If we don't have enough space to
3256 * store the whole data, allocate a cluster
3257 * or additional mbufs.
3258 * XXX: we don't use m_copyback(), since the
3259 * function does not use clusters and thus is
3268 if (remain
<= (mm
->m_flags
& M_PKTHDR
? MHLEN
: MLEN
))
3270 else { /* allocate a cluster */
3271 MCLGET(mm
, M_DONTWAIT
);
3272 if (!(mm
->m_flags
& M_EXT
)) {
3276 len
= remain
< MCLBYTES
?
3280 bcopy(n
->m_data
+ copied
, mm
->m_data
,
3287 if (remain
<= 0) /* completed? */
3290 /* need another mbuf */
3291 MGETHDR(mn
, M_DONTWAIT
, MT_HEADER
);
3299 mm
->m_next
= m_free(n
);
3318 ipsec_setsocket(m
, so
)
3324 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3326 n
= m_aux_add(m
, AF_INET
, IPPROTO_ESP
);
3329 *mtod(n
, struct socket
**) = so
;
3331 * XXX think again about it when we put decryption
3332 * histrory into aux mbuf
3334 n
->m_len
= sizeof(struct socket
*);
3346 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3347 if (n
&& n
->m_len
>= sizeof(struct socket
*))
3348 return *mtod(n
, struct socket
**);
3355 * System control for IP
3357 u_char ipsecctlerrmap
[PRC_NCMDS
] = {
3359 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
3360 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
3361 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
3366 int *ipsec_sysvars
[] = IPSECCTL_VARS
;
3369 ipsec_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
3377 if (name
[0] >= IPSECCTL_MAXID
)
3378 return (EOPNOTSUPP
);
3381 case IPSECCTL_STATS
:
3382 return sysctl_rdtrunc(oldp
, oldlenp
, newp
, &ipsecstat
,
3384 case IPSECCTL_DEF_POLICY
:
3385 if (newp
!= NULL
&& newlen
== sizeof(int)) {
3386 switch (*(int *)newp
) {
3387 case IPSEC_POLICY_DISCARD
:
3388 case IPSEC_POLICY_NONE
:
3394 return (sysctl_int_arr(ipsec_sysvars
, name
, namelen
,
3395 oldp
, oldlenp
, newp
, newlen
));
3396 case IPSECCTL_DEF_ESP_TRANSLEV
:
3397 case IPSECCTL_DEF_ESP_NETLEV
:
3398 case IPSECCTL_DEF_AH_TRANSLEV
:
3399 case IPSECCTL_DEF_AH_NETLEV
:
3400 if (newp
!= NULL
&& newlen
== sizeof(int)) {
3401 switch (*(int *)newp
) {
3402 case IPSEC_LEVEL_USE
:
3403 case IPSEC_LEVEL_REQUIRE
:
3409 return (sysctl_int_arr(ipsec_sysvars
, name
, namelen
,
3410 oldp
, oldlenp
, newp
, newlen
));
3412 return (sysctl_int_arr(ipsec_sysvars
, name
, namelen
,
3413 oldp
, oldlenp
, newp
, newlen
));
3419 * System control for IP6
3421 u_char ipsec6ctlerrmap
[PRC_NCMDS
] = {
3423 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
3424 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
3425 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
3430 int *ipsec6_sysvars
[] = IPSEC6CTL_VARS
;
3433 ipsec6_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
3441 if (name
[0] >= IPSECCTL_MAXID
) /* xxx no 6 in this definition */
3442 return (EOPNOTSUPP
);
3445 case IPSECCTL_STATS
: /* xxx no 6 in this definition */
3446 return sysctl_rdtrunc(oldp
, oldlenp
, newp
, &ipsec6stat
,
3447 sizeof(ipsec6stat
));
3448 case IPSECCTL_DEF_POLICY
:
3449 if (newp
!= NULL
&& newlen
== sizeof(int)) {
3450 switch (*(int *)newp
) {
3451 case IPSEC_POLICY_DISCARD
:
3452 case IPSEC_POLICY_NONE
:
3458 return (sysctl_int_arr(ipsec6_sysvars
, name
, namelen
,
3459 oldp
, oldlenp
, newp
, newlen
));
3460 case IPSECCTL_DEF_ESP_TRANSLEV
:
3461 case IPSECCTL_DEF_ESP_NETLEV
:
3462 case IPSECCTL_DEF_AH_TRANSLEV
:
3463 case IPSECCTL_DEF_AH_NETLEV
:
3464 if (newp
!= NULL
&& newlen
== sizeof(int)) {
3465 switch (*(int *)newp
) {
3466 case IPSEC_LEVEL_USE
:
3467 case IPSEC_LEVEL_REQUIRE
:
3473 return (sysctl_int_arr(ipsec6_sysvars
, name
, namelen
,
3474 oldp
, oldlenp
, newp
, newlen
));
3476 return (sysctl_int_arr(ipsec6_sysvars
, name
, namelen
,
3477 oldp
, oldlenp
, newp
, newlen
));
3486 * System control for IPSEC
3488 u_char ipsecctlermap
[PRC_NCMDS
] = {
3490 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
3491 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
3492 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
3497 int *ipsec_sysvars
[] = IPSECCTL_VARS
;
3500 ipsec_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
3508 /* All sysctl names at this level are terminal. */
3512 /* common sanity checks */
3514 case IPSECCTL_DEF_ESP_TRANSLEV
:
3515 case IPSECCTL_DEF_ESP_NETLEV
:
3516 case IPSECCTL_DEF_AH_TRANSLEV
:
3517 case IPSECCTL_DEF_AH_NETLEV
:
3518 if (newp
!= NULL
&& newlen
== sizeof(int)) {
3519 switch (*(int *)newp
) {
3520 case IPSEC_LEVEL_USE
:
3521 case IPSEC_LEVEL_REQUIRE
:
3531 case IPSECCTL_STATS
:
3532 return sysctl_struct(oldp
, oldlenp
, newp
, newlen
,
3533 &ipsecstat
, sizeof(ipsecstat
));
3534 case IPSECCTL_DEF_POLICY
:
3535 if (newp
!= NULL
&& newlen
== sizeof(int)) {
3536 switch (*(int *)newp
) {
3537 case IPSEC_POLICY_DISCARD
:
3538 case IPSEC_POLICY_NONE
:
3544 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3545 &ip4_def_policy
.policy
);
3546 case IPSECCTL_DEF_ESP_TRANSLEV
:
3547 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3548 &ip4_esp_trans_deflev
);
3549 case IPSECCTL_DEF_ESP_NETLEV
:
3550 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3551 &ip4_esp_net_deflev
);
3552 case IPSECCTL_DEF_AH_TRANSLEV
:
3553 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3554 &ip4_ah_trans_deflev
);
3555 case IPSECCTL_DEF_AH_NETLEV
:
3556 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3557 &ip4_ah_net_deflev
);
3558 case IPSECCTL_INBOUND_CALL_IKE
:
3559 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3560 &ip4_inbound_call_ike
);
3561 case IPSECCTL_AH_CLEARTOS
:
3562 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3564 case IPSECCTL_AH_OFFSETMASK
:
3565 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3566 &ip4_ah_offsetmask
);
3567 case IPSECCTL_DFBIT
:
3568 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3571 return sysctl_int(oldp
, oldlenp
, newp
, newlen
, &ip4_ipsec_ecn
);
3572 case IPSECCTL_DEBUG
:
3573 return sysctl_int(oldp
, oldlenp
, newp
, newlen
, &ipsec_debug
);
3582 * System control for IPSEC6
3584 u_char ipsec6ctlermap
[PRC_NCMDS
] = {
3586 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
3587 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
3588 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
3593 int *ipsec6_sysvars
[] = IPSEC6CTL_VARS
;
3596 ipsec6_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
3604 /* All sysctl names at this level are terminal. */
3608 /* common sanity checks */
3610 case IPSECCTL_DEF_ESP_TRANSLEV
:
3611 case IPSECCTL_DEF_ESP_NETLEV
:
3612 case IPSECCTL_DEF_AH_TRANSLEV
:
3613 case IPSECCTL_DEF_AH_NETLEV
:
3614 if (newp
!= NULL
&& newlen
== sizeof(int)) {
3615 switch (*(int *)newp
) {
3616 case IPSEC_LEVEL_USE
:
3617 case IPSEC_LEVEL_REQUIRE
:
3627 case IPSECCTL_STATS
:
3628 return sysctl_struct(oldp
, oldlenp
, newp
, newlen
,
3629 &ipsec6stat
, sizeof(ipsec6stat
));
3630 case IPSECCTL_DEF_POLICY
:
3631 if (newp
!= NULL
&& newlen
== sizeof(int)) {
3632 switch (*(int *)newp
) {
3633 case IPSEC_POLICY_DISCARD
:
3634 case IPSEC_POLICY_NONE
:
3640 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3641 &ip6_def_policy
.policy
);
3642 case IPSECCTL_DEF_ESP_TRANSLEV
:
3643 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3644 &ip6_esp_trans_deflev
);
3645 case IPSECCTL_DEF_ESP_NETLEV
:
3646 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3647 &ip6_esp_net_deflev
);
3648 case IPSECCTL_DEF_AH_TRANSLEV
:
3649 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3650 &ip6_ah_trans_deflev
);
3651 case IPSECCTL_DEF_AH_NETLEV
:
3652 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3653 &ip6_ah_net_deflev
);
3654 case IPSECCTL_INBOUND_CALL_IKE
:
3655 return sysctl_int(oldp
, oldlenp
, newp
, newlen
,
3656 &ip6_inbound_call_ike
);
3658 return sysctl_int(oldp
, oldlenp
, newp
, newlen
, &ip6_ipsec_ecn
);
3659 case IPSECCTL_DEBUG
:
3660 return sysctl_int(oldp
, oldlenp
, newp
, newlen
, &ipsec_debug
);
3668 #endif /* __NetBSD__ */