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 struct ipsecstat ipsecstat
;
108 int ip4_ah_cleartos
= 1;
109 int ip4_ah_offsetmask
= 0; /* maybe IP_DF? */
110 int ip4_ipsec_dfbit
= 0; /* DF bit on encap. 0: clear 1: set 2: copy */
111 int ip4_esp_trans_deflev
= IPSEC_LEVEL_USE
;
112 int ip4_esp_net_deflev
= IPSEC_LEVEL_USE
;
113 int ip4_ah_trans_deflev
= IPSEC_LEVEL_USE
;
114 int ip4_ah_net_deflev
= IPSEC_LEVEL_USE
;
115 struct secpolicy ip4_def_policy
;
116 int ip4_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
117 int ip4_esp_randpad
= -1;
118 static int sysctl_def_policy SYSCTL_HANDLER_ARGS
;
120 SYSCTL_DECL(_net_inet_ipsec
);
122 SYSCTL_DECL(_net_inet6_ipsec6
);
125 SYSCTL_STRUCT(_net_inet_ipsec
, IPSECCTL_STATS
,
126 stats
, CTLFLAG_RD
, &ipsecstat
, ipsecstat
, "");
127 SYSCTL_PROC(_net_inet_ipsec
, IPSECCTL_DEF_POLICY
, def_policy
, CTLTYPE_INT
|CTLFLAG_RW
,
128 &ip4_def_policy
.policy
, 0, &sysctl_def_policy
, "I", "");
129 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
130 CTLFLAG_RW
, &ip4_esp_trans_deflev
, 0, "");
131 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
132 CTLFLAG_RW
, &ip4_esp_net_deflev
, 0, "");
133 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
134 CTLFLAG_RW
, &ip4_ah_trans_deflev
, 0, "");
135 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
136 CTLFLAG_RW
, &ip4_ah_net_deflev
, 0, "");
137 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_CLEARTOS
,
138 ah_cleartos
, CTLFLAG_RW
, &ip4_ah_cleartos
, 0, "");
139 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_OFFSETMASK
,
140 ah_offsetmask
, CTLFLAG_RW
, &ip4_ah_offsetmask
, 0, "");
141 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DFBIT
,
142 dfbit
, CTLFLAG_RW
, &ip4_ipsec_dfbit
, 0, "");
143 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ECN
,
144 ecn
, CTLFLAG_RW
, &ip4_ipsec_ecn
, 0, "");
145 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEBUG
,
146 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
147 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ESP_RANDPAD
,
148 esp_randpad
, CTLFLAG_RW
, &ip4_esp_randpad
, 0, "");
150 /* for performance, we bypass ipsec until a security policy is set */
151 int ipsec_bypass
= 1;
152 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, bypass
, CTLFLAG_RD
, &ipsec_bypass
,0, "");
155 struct ipsecstat ipsec6stat
;
156 int ip6_esp_trans_deflev
= IPSEC_LEVEL_USE
;
157 int ip6_esp_net_deflev
= IPSEC_LEVEL_USE
;
158 int ip6_ah_trans_deflev
= IPSEC_LEVEL_USE
;
159 int ip6_ah_net_deflev
= IPSEC_LEVEL_USE
;
160 struct secpolicy ip6_def_policy
;
161 int ip6_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
162 int ip6_esp_randpad
= -1;
164 /* net.inet6.ipsec6 */
165 SYSCTL_STRUCT(_net_inet6_ipsec6
, IPSECCTL_STATS
,
166 stats
, CTLFLAG_RD
, &ipsec6stat
, ipsecstat
, "");
167 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_POLICY
,
168 def_policy
, CTLFLAG_RW
, &ip6_def_policy
.policy
, 0, "");
169 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
170 CTLFLAG_RW
, &ip6_esp_trans_deflev
, 0, "");
171 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
172 CTLFLAG_RW
, &ip6_esp_net_deflev
, 0, "");
173 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
174 CTLFLAG_RW
, &ip6_ah_trans_deflev
, 0, "");
175 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
176 CTLFLAG_RW
, &ip6_ah_net_deflev
, 0, "");
177 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ECN
,
178 ecn
, CTLFLAG_RW
, &ip6_ipsec_ecn
, 0, "");
179 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEBUG
,
180 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
181 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ESP_RANDPAD
,
182 esp_randpad
, CTLFLAG_RW
, &ip6_esp_randpad
, 0, "");
185 static int ipsec_setspidx_mbuf
186 __P((struct secpolicyindex
*, u_int
, u_int
, struct mbuf
*, int));
187 static int ipsec4_setspidx_inpcb
__P((struct mbuf
*, struct inpcb
*pcb
));
189 static int ipsec6_setspidx_in6pcb
__P((struct mbuf
*, struct in6pcb
*pcb
));
191 static int ipsec_setspidx
__P((struct mbuf
*, struct secpolicyindex
*, int));
192 static void ipsec4_get_ulp
__P((struct mbuf
*m
, struct secpolicyindex
*, int));
193 static int ipsec4_setspidx_ipaddr
__P((struct mbuf
*, struct secpolicyindex
*));
195 static void ipsec6_get_ulp
__P((struct mbuf
*m
, struct secpolicyindex
*, int));
196 static int ipsec6_setspidx_ipaddr
__P((struct mbuf
*, struct secpolicyindex
*));
198 static struct inpcbpolicy
*ipsec_newpcbpolicy
__P((void));
199 static void ipsec_delpcbpolicy
__P((struct inpcbpolicy
*));
200 static struct secpolicy
*ipsec_deepcopy_policy
__P((struct secpolicy
*src
));
201 static int ipsec_set_policy
__P((struct secpolicy
**pcb_sp
,
202 int optname
, caddr_t request
, size_t len
, int priv
));
203 static int ipsec_get_policy
__P((struct secpolicy
*pcb_sp
, struct mbuf
**mp
));
204 static void vshiftl
__P((unsigned char *, int, int));
205 static int ipsec_in_reject
__P((struct secpolicy
*, struct mbuf
*));
206 static size_t ipsec_hdrsiz
__P((struct secpolicy
*));
208 static struct mbuf
*ipsec4_splithdr
__P((struct mbuf
*));
211 static struct mbuf
*ipsec6_splithdr
__P((struct mbuf
*));
214 static int ipsec4_encapsulate
__P((struct mbuf
*, struct secasvar
*));
217 static int ipsec6_encapsulate
__P((struct mbuf
*, struct secasvar
*));
219 static struct mbuf
*ipsec_addaux
__P((struct mbuf
*));
220 static struct mbuf
*ipsec_findaux
__P((struct mbuf
*));
221 static void ipsec_optaux
__P((struct mbuf
*, struct mbuf
*));
224 sysctl_def_policy SYSCTL_HANDLER_ARGS
226 int old_policy
= ip4_def_policy
.policy
;
227 int error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
229 if (ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
&&
230 ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
) {
231 ip4_def_policy
.policy
= old_policy
;
235 /* Turn off the bypass if the default security policy changes */
236 if (ipsec_bypass
!= 0 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
)
243 * For OUTBOUND packet having a socket. Searching SPD for packet,
244 * and return a pointer to SP.
245 * OUT: NULL: no apropreate SP found, the following value is set to error.
247 * EACCES : discard packet.
248 * ENOENT : ipsec_acquire() in progress, maybe.
249 * others : error occured.
250 * others: a pointer to SP
252 * NOTE: IPv6 mapped adddress concern is implemented here.
255 ipsec4_getpolicybysock(m
, dir
, so
, error
)
261 struct inpcbpolicy
*pcbsp
= NULL
;
262 struct secpolicy
*currsp
= NULL
; /* policy on socket */
263 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
266 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
267 panic("ipsec4_getpolicybysock: NULL pointer was passed.\n");
269 switch (so
->so_proto
->pr_domain
->dom_family
) {
271 pcbsp
= sotoinpcb(so
)->inp_sp
;
275 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
281 /* Socket has not specified an IPSEC policy */
282 return ipsec4_getpolicybyaddr(m
, dir
, 0, error
);
285 switch (so
->so_proto
->pr_domain
->dom_family
) {
287 /* set spidx in pcb */
288 *error
= ipsec4_setspidx_inpcb(m
, sotoinpcb(so
));
292 /* set spidx in pcb */
293 *error
= ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
297 panic("ipsec4_getpolicybysock: unsupported address family\n");
304 panic("ipsec4_getpolicybysock: pcbsp is NULL.\n");
307 case IPSEC_DIR_INBOUND
:
308 currsp
= pcbsp
->sp_in
;
310 case IPSEC_DIR_OUTBOUND
:
311 currsp
= pcbsp
->sp_out
;
314 panic("ipsec4_getpolicybysock: illegal direction.\n");
319 panic("ipsec4_getpolicybysock: currsp is NULL.\n");
321 /* when privilieged socket */
323 switch (currsp
->policy
) {
324 case IPSEC_POLICY_BYPASS
:
329 case IPSEC_POLICY_ENTRUST
:
330 /* look for a policy in SPD */
331 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
334 if (kernsp
!= NULL
) {
335 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
336 printf("DP ipsec4_getpolicybysock called "
337 "to allocate SP:%p\n", kernsp
));
343 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
344 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
346 "fixed system default policy: %d->%d\n",
347 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
348 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
350 ip4_def_policy
.refcnt
++;
352 return &ip4_def_policy
;
354 case IPSEC_POLICY_IPSEC
:
360 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
361 "Invalid policy for PCB %d\n", currsp
->policy
));
368 /* when non-privilieged socket */
369 /* look for a policy in SPD */
370 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
373 if (kernsp
!= NULL
) {
374 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
375 printf("DP ipsec4_getpolicybysock called "
376 "to allocate SP:%p\n", kernsp
));
382 switch (currsp
->policy
) {
383 case IPSEC_POLICY_BYPASS
:
384 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
385 "Illegal policy for non-priviliged defined %d\n",
390 case IPSEC_POLICY_ENTRUST
:
391 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
392 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
394 "fixed system default policy: %d->%d\n",
395 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
396 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
398 ip4_def_policy
.refcnt
++;
400 return &ip4_def_policy
;
402 case IPSEC_POLICY_IPSEC
:
408 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
409 "Invalid policy for PCB %d\n", currsp
->policy
));
417 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
418 * and return a pointer to SP.
419 * OUT: positive: a pointer to the entry for security policy leaf matched.
420 * NULL: no apropreate SP found, the following value is set to error.
422 * EACCES : discard packet.
423 * ENOENT : ipsec_acquire() in progress, maybe.
424 * others : error occured.
427 ipsec4_getpolicybyaddr(m
, dir
, flag
, error
)
433 struct secpolicy
*sp
= NULL
;
435 if (ipsec_bypass
!= 0)
439 if (m
== NULL
|| error
== NULL
)
440 panic("ipsec4_getpolicybyaddr: NULL pointer was passed.\n");
443 struct secpolicyindex spidx
;
445 bzero(&spidx
, sizeof(spidx
));
447 /* make a index to look for a policy */
448 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET
, m
,
449 (flag
& IP_FORWARDING
) ? 0 : 1);
454 sp
= key_allocsp(&spidx
, dir
);
459 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
460 printf("DP ipsec4_getpolicybyaddr called "
461 "to allocate SP:%p\n", sp
));
467 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
468 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
469 ipseclog((LOG_INFO
, "fixed system default policy:%d->%d\n",
470 ip4_def_policy
.policy
,
472 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
474 ip4_def_policy
.refcnt
++;
476 return &ip4_def_policy
;
481 * For OUTBOUND packet having a socket. Searching SPD for packet,
482 * and return a pointer to SP.
483 * OUT: NULL: no apropreate SP found, the following value is set to error.
485 * EACCES : discard packet.
486 * ENOENT : ipsec_acquire() in progress, maybe.
487 * others : error occured.
488 * others: a pointer to SP
491 ipsec6_getpolicybysock(m
, dir
, so
, error
)
497 struct inpcbpolicy
*pcbsp
= NULL
;
498 struct secpolicy
*currsp
= NULL
; /* policy on socket */
499 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
502 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
503 panic("ipsec6_getpolicybysock: NULL pointer was passed.\n");
506 if (so
->so_proto
->pr_domain
->dom_family
!= AF_INET6
)
507 panic("ipsec6_getpolicybysock: socket domain != inet6\n");
510 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
513 return ipsec6_getpolicybyaddr(m
, dir
, 0, error
);
516 /* set spidx in pcb */
517 ipsec6_setspidx_in6pcb(m
, sotoin6pcb(so
));
521 panic("ipsec6_getpolicybysock: pcbsp is NULL.\n");
524 case IPSEC_DIR_INBOUND
:
525 currsp
= pcbsp
->sp_in
;
527 case IPSEC_DIR_OUTBOUND
:
528 currsp
= pcbsp
->sp_out
;
531 panic("ipsec6_getpolicybysock: illegal direction.\n");
536 panic("ipsec6_getpolicybysock: currsp is NULL.\n");
538 /* when privilieged socket */
540 switch (currsp
->policy
) {
541 case IPSEC_POLICY_BYPASS
:
546 case IPSEC_POLICY_ENTRUST
:
547 /* look for a policy in SPD */
548 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
551 if (kernsp
!= NULL
) {
552 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
553 printf("DP ipsec6_getpolicybysock called "
554 "to allocate SP:%p\n", kernsp
));
560 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
561 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
563 "fixed system default policy: %d->%d\n",
564 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
565 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
567 ip6_def_policy
.refcnt
++;
569 return &ip6_def_policy
;
571 case IPSEC_POLICY_IPSEC
:
577 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
578 "Invalid policy for PCB %d\n", currsp
->policy
));
585 /* when non-privilieged socket */
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 switch (currsp
->policy
) {
600 case IPSEC_POLICY_BYPASS
:
601 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
602 "Illegal policy for non-priviliged defined %d\n",
607 case IPSEC_POLICY_ENTRUST
:
608 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
609 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
611 "fixed system default policy: %d->%d\n",
612 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
613 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
615 ip6_def_policy
.refcnt
++;
617 return &ip6_def_policy
;
619 case IPSEC_POLICY_IPSEC
:
626 "ipsec6_policybysock: Invalid policy for PCB %d\n",
635 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
636 * and return a pointer to SP.
637 * `flag' means that packet is to be forwarded whether or not.
639 * OUT: positive: a pointer to the entry for security policy leaf matched.
640 * NULL: no apropreate SP found, the following value is set to error.
642 * EACCES : discard packet.
643 * ENOENT : ipsec_acquire() in progress, maybe.
644 * others : error occured.
646 #ifndef IP_FORWARDING
647 #define IP_FORWARDING 1
651 ipsec6_getpolicybyaddr(m
, dir
, flag
, error
)
657 struct secpolicy
*sp
= NULL
;
660 if (m
== NULL
|| error
== NULL
)
661 panic("ipsec6_getpolicybyaddr: NULL pointer was passed.\n");
664 struct secpolicyindex spidx
;
666 bzero(&spidx
, sizeof(spidx
));
668 /* make a index to look for a policy */
669 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET6
, m
,
670 (flag
& IP_FORWARDING
) ? 0 : 1);
675 sp
= key_allocsp(&spidx
, dir
);
680 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
681 printf("DP ipsec6_getpolicybyaddr called "
682 "to allocate SP:%p\n", sp
));
688 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
689 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
690 ipseclog((LOG_INFO
, "fixed system default policy: %d->%d\n",
691 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
692 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
694 ip6_def_policy
.refcnt
++;
696 return &ip6_def_policy
;
701 * set IP address into spidx from mbuf.
702 * When Forwarding packet and ICMP echo reply, this function is used.
704 * IN: get the followings from mbuf.
705 * protocol family, src, dst, next protocol
708 * other: failure, and set errno.
711 ipsec_setspidx_mbuf(spidx
, dir
, family
, m
, needport
)
712 struct secpolicyindex
*spidx
;
720 if (spidx
== NULL
|| m
== NULL
)
721 panic("ipsec_setspidx_mbuf: NULL pointer was passed.\n");
723 bzero(spidx
, sizeof(*spidx
));
725 error
= ipsec_setspidx(m
, spidx
, needport
);
734 bzero(spidx
, sizeof(*spidx
));
739 ipsec4_setspidx_inpcb(m
, pcb
)
743 struct secpolicyindex
*spidx
;
746 if (ipsec_bypass
!= 0)
751 panic("ipsec4_setspidx_inpcb: no PCB found.\n");
752 if (pcb
->inp_sp
== NULL
)
753 panic("ipsec4_setspidx_inpcb: no inp_sp found.\n");
754 if (pcb
->inp_sp
->sp_out
== NULL
|| pcb
->inp_sp
->sp_in
== NULL
)
755 panic("ipsec4_setspidx_inpcb: no sp_in/out found.\n");
757 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
758 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
760 spidx
= &pcb
->inp_sp
->sp_in
->spidx
;
761 error
= ipsec_setspidx(m
, spidx
, 1);
764 spidx
->dir
= IPSEC_DIR_INBOUND
;
766 spidx
= &pcb
->inp_sp
->sp_out
->spidx
;
767 error
= ipsec_setspidx(m
, spidx
, 1);
770 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
775 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
776 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
782 ipsec6_setspidx_in6pcb(m
, pcb
)
786 struct secpolicyindex
*spidx
;
791 panic("ipsec6_setspidx_in6pcb: no PCB found.\n");
792 if (pcb
->in6p_sp
== NULL
)
793 panic("ipsec6_setspidx_in6pcb: no in6p_sp found.\n");
794 if (pcb
->in6p_sp
->sp_out
== NULL
|| pcb
->in6p_sp
->sp_in
== NULL
)
795 panic("ipsec6_setspidx_in6pcb: no sp_in/out found.\n");
797 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
798 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
800 spidx
= &pcb
->in6p_sp
->sp_in
->spidx
;
801 error
= ipsec_setspidx(m
, spidx
, 1);
804 spidx
->dir
= IPSEC_DIR_INBOUND
;
806 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
807 printf("ipsec_setspidx_mbuf: end\n");
808 kdebug_secpolicyindex(spidx
));
813 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
814 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
820 * configure security policy index (src/dst/proto/sport/dport)
821 * by looking at the content of mbuf.
822 * the caller is responsible for error recovery (like clearing up spidx).
825 ipsec_setspidx(m
, spidx
, needport
)
827 struct secpolicyindex
*spidx
;
830 struct ip
*ip
= NULL
;
838 panic("ipsec_setspidx: m == 0 passed.\n");
841 * validate m->m_pkthdr.len. we see incorrect length if we
842 * mistakenly call this function with inconsistent mbuf chain
843 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
846 for (n
= m
; n
; n
= n
->m_next
)
848 if (m
->m_pkthdr
.len
!= len
) {
849 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
850 printf("ipsec_setspidx: "
851 "total of m_len(%d) != pkthdr.len(%d), "
853 len
, m
->m_pkthdr
.len
));
857 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
858 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
859 printf("ipsec_setspidx: "
860 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
865 if (m
->m_len
>= sizeof(*ip
))
866 ip
= mtod(m
, struct ip
*);
868 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
872 v
= _IP_VHL_V(ip
->ip_vhl
);
878 error
= ipsec4_setspidx_ipaddr(m
, spidx
);
881 ipsec4_get_ulp(m
, spidx
, needport
);
885 if (m
->m_pkthdr
.len
< sizeof(struct ip6_hdr
)) {
886 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
887 printf("ipsec_setspidx: "
888 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
889 "ignored.\n", m
->m_pkthdr
.len
));
892 error
= ipsec6_setspidx_ipaddr(m
, spidx
);
895 ipsec6_get_ulp(m
, spidx
, needport
);
899 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
900 printf("ipsec_setspidx: "
901 "unknown IP version %u, ignored.\n", v
));
907 ipsec4_get_ulp(m
, spidx
, needport
)
909 struct secpolicyindex
*spidx
;
921 panic("ipsec4_get_ulp: NULL pointer was passed.\n");
922 if (m
->m_pkthdr
.len
< sizeof(ip
))
923 panic("ipsec4_get_ulp: too short\n");
926 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
927 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
= IPSEC_PORT_ANY
;
928 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
= IPSEC_PORT_ANY
;
930 m_copydata(m
, 0, sizeof(ip
), (caddr_t
)&ip
);
931 /* ip_input() flips it into host endian XXX need more checking */
932 if (ip
.ip_off
& (IP_MF
| IP_OFFMASK
))
937 off
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
941 while (off
< m
->m_pkthdr
.len
) {
944 spidx
->ul_proto
= nxt
;
947 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
949 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
950 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
952 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
956 spidx
->ul_proto
= nxt
;
959 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
961 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
962 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
964 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
968 if (m
->m_pkthdr
.len
> off
+ sizeof(ip6e
))
970 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
971 off
+= (ip6e
.ip6e_len
+ 2) << 2;
976 /* XXX intermediate headers??? */
977 spidx
->ul_proto
= nxt
;
983 /* assumes that m is sane */
985 ipsec4_setspidx_ipaddr(m
, spidx
)
987 struct secpolicyindex
*spidx
;
989 struct ip
*ip
= NULL
;
991 struct sockaddr_in
*sin
;
993 if (m
->m_len
>= sizeof(*ip
))
994 ip
= mtod(m
, struct ip
*);
996 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
1000 sin
= (struct sockaddr_in
*)&spidx
->src
;
1001 bzero(sin
, sizeof(*sin
));
1002 sin
->sin_family
= AF_INET
;
1003 sin
->sin_len
= sizeof(struct sockaddr_in
);
1004 bcopy(&ip
->ip_src
, &sin
->sin_addr
, sizeof(ip
->ip_src
));
1005 spidx
->prefs
= sizeof(struct in_addr
) << 3;
1007 sin
= (struct sockaddr_in
*)&spidx
->dst
;
1008 bzero(sin
, sizeof(*sin
));
1009 sin
->sin_family
= AF_INET
;
1010 sin
->sin_len
= sizeof(struct sockaddr_in
);
1011 bcopy(&ip
->ip_dst
, &sin
->sin_addr
, sizeof(ip
->ip_dst
));
1012 spidx
->prefd
= sizeof(struct in_addr
) << 3;
1018 ipsec6_get_ulp(m
, spidx
, needport
)
1020 struct secpolicyindex
*spidx
;
1029 panic("ipsec6_get_ulp: NULL pointer was passed.\n");
1031 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1032 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m
));
1035 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
1036 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= IPSEC_PORT_ANY
;
1037 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= IPSEC_PORT_ANY
;
1040 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
1041 if (off
< 0 || m
->m_pkthdr
.len
< off
)
1046 spidx
->ul_proto
= nxt
;
1049 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
1051 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
1052 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= th
.th_sport
;
1053 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= th
.th_dport
;
1056 spidx
->ul_proto
= nxt
;
1059 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1061 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
1062 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= uh
.uh_sport
;
1063 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= uh
.uh_dport
;
1065 case IPPROTO_ICMPV6
:
1067 /* XXX intermediate headers??? */
1068 spidx
->ul_proto
= nxt
;
1073 /* assumes that m is sane */
1075 ipsec6_setspidx_ipaddr(m
, spidx
)
1077 struct secpolicyindex
*spidx
;
1079 struct ip6_hdr
*ip6
= NULL
;
1080 struct ip6_hdr ip6buf
;
1081 struct sockaddr_in6
*sin6
;
1083 if (m
->m_len
>= sizeof(*ip6
))
1084 ip6
= mtod(m
, struct ip6_hdr
*);
1086 m_copydata(m
, 0, sizeof(ip6buf
), (caddr_t
)&ip6buf
);
1090 sin6
= (struct sockaddr_in6
*)&spidx
->src
;
1091 bzero(sin6
, sizeof(*sin6
));
1092 sin6
->sin6_family
= AF_INET6
;
1093 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1094 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_src
));
1095 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1096 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1097 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
1099 spidx
->prefs
= sizeof(struct in6_addr
) << 3;
1101 sin6
= (struct sockaddr_in6
*)&spidx
->dst
;
1102 bzero(sin6
, sizeof(*sin6
));
1103 sin6
->sin6_family
= AF_INET6
;
1104 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1105 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_dst
));
1106 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
1107 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1108 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
1110 spidx
->prefd
= sizeof(struct in6_addr
) << 3;
1116 static struct inpcbpolicy
*
1117 ipsec_newpcbpolicy()
1119 struct inpcbpolicy
*p
;
1121 p
= (struct inpcbpolicy
*)_MALLOC(sizeof(*p
), M_SECA
, M_WAITOK
);
1126 ipsec_delpcbpolicy(p
)
1127 struct inpcbpolicy
*p
;
1132 /* initialize policy in PCB */
1134 ipsec_init_policy(so
, pcb_sp
)
1136 struct inpcbpolicy
**pcb_sp
;
1138 struct inpcbpolicy
*new;
1141 if (so
== NULL
|| pcb_sp
== NULL
)
1142 panic("ipsec_init_policy: NULL pointer was passed.\n");
1144 new = ipsec_newpcbpolicy();
1146 ipseclog((LOG_DEBUG
, "ipsec_init_policy: No more memory.\n"));
1149 bzero(new, sizeof(*new));
1152 if (so
->so_uid
== 0)
1154 if (so
->so_cred
!= 0 && so
->so_cred
->pc_ucred
->cr_uid
== 0)
1160 if ((new->sp_in
= key_newsp()) == NULL
) {
1161 ipsec_delpcbpolicy(new);
1164 new->sp_in
->state
= IPSEC_SPSTATE_ALIVE
;
1165 new->sp_in
->policy
= IPSEC_POLICY_ENTRUST
;
1167 if ((new->sp_out
= key_newsp()) == NULL
) {
1168 key_freesp(new->sp_in
);
1169 ipsec_delpcbpolicy(new);
1172 new->sp_out
->state
= IPSEC_SPSTATE_ALIVE
;
1173 new->sp_out
->policy
= IPSEC_POLICY_ENTRUST
;
1180 /* copy old ipsec policy into new */
1182 ipsec_copy_policy(old
, new)
1183 struct inpcbpolicy
*old
, *new;
1185 struct secpolicy
*sp
;
1187 if (ipsec_bypass
!= 0)
1190 sp
= ipsec_deepcopy_policy(old
->sp_in
);
1192 key_freesp(new->sp_in
);
1197 sp
= ipsec_deepcopy_policy(old
->sp_out
);
1199 key_freesp(new->sp_out
);
1204 new->priv
= old
->priv
;
1209 /* deep-copy a policy in PCB */
1210 static struct secpolicy
*
1211 ipsec_deepcopy_policy(src
)
1212 struct secpolicy
*src
;
1214 struct ipsecrequest
*newchain
= NULL
;
1215 struct ipsecrequest
*p
;
1216 struct ipsecrequest
**q
;
1217 struct ipsecrequest
*r
;
1218 struct secpolicy
*dst
;
1221 if (src
== NULL
|| dst
== NULL
)
1225 * deep-copy IPsec request chain. This is required since struct
1226 * ipsecrequest is not reference counted.
1229 for (p
= src
->req
; p
; p
= p
->next
) {
1230 *q
= (struct ipsecrequest
*)_MALLOC(sizeof(struct ipsecrequest
),
1234 bzero(*q
, sizeof(**q
));
1237 (*q
)->saidx
.proto
= p
->saidx
.proto
;
1238 (*q
)->saidx
.mode
= p
->saidx
.mode
;
1239 (*q
)->level
= p
->level
;
1240 (*q
)->saidx
.reqid
= p
->saidx
.reqid
;
1242 bcopy(&p
->saidx
.src
, &(*q
)->saidx
.src
, sizeof((*q
)->saidx
.src
));
1243 bcopy(&p
->saidx
.dst
, &(*q
)->saidx
.dst
, sizeof((*q
)->saidx
.dst
));
1251 dst
->req
= newchain
;
1252 dst
->state
= src
->state
;
1253 dst
->policy
= src
->policy
;
1254 /* do not touch the refcnt fields */
1259 for (p
= newchain
; p
; p
= r
) {
1267 /* set policy and ipsec request if present. */
1269 ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
)
1270 struct secpolicy
**pcb_sp
;
1276 struct sadb_x_policy
*xpl
;
1277 struct secpolicy
*newsp
= NULL
;
1281 if (pcb_sp
== NULL
|| *pcb_sp
== NULL
|| request
== NULL
)
1283 if (len
< sizeof(*xpl
))
1285 xpl
= (struct sadb_x_policy
*)request
;
1287 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1288 printf("ipsec_set_policy: passed policy\n");
1289 kdebug_sadb_x_policy((struct sadb_ext
*)xpl
));
1291 /* check policy type */
1292 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1293 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
1294 || xpl
->sadb_x_policy_type
== IPSEC_POLICY_NONE
)
1297 /* check privileged socket */
1298 if (priv
== 0 && xpl
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
)
1301 /* allocation new SP entry */
1302 if ((newsp
= key_msg2sp(xpl
, len
, &error
)) == NULL
)
1305 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1307 /* clear old SP and set new SP */
1308 key_freesp(*pcb_sp
);
1310 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1311 printf("ipsec_set_policy: new policy\n");
1312 kdebug_secpolicy(newsp
));
1318 ipsec_get_policy(pcb_sp
, mp
)
1319 struct secpolicy
*pcb_sp
;
1324 if (pcb_sp
== NULL
|| mp
== NULL
)
1327 *mp
= key_sp2msg(pcb_sp
);
1329 ipseclog((LOG_DEBUG
, "ipsec_get_policy: No more memory.\n"));
1333 (*mp
)->m_type
= MT_DATA
;
1334 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1335 printf("ipsec_get_policy:\n");
1342 ipsec4_set_policy(inp
, optname
, request
, len
, priv
)
1349 struct sadb_x_policy
*xpl
;
1350 struct secpolicy
**pcb_sp
;
1354 if (inp
== NULL
|| request
== NULL
)
1356 if (len
< sizeof(*xpl
))
1358 xpl
= (struct sadb_x_policy
*)request
;
1360 if (inp
->inp_sp
== NULL
) {
1361 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1366 /* select direction */
1367 switch (xpl
->sadb_x_policy_dir
) {
1368 case IPSEC_DIR_INBOUND
:
1369 pcb_sp
= &inp
->inp_sp
->sp_in
;
1371 case IPSEC_DIR_OUTBOUND
:
1372 pcb_sp
= &inp
->inp_sp
->sp_out
;
1375 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1376 xpl
->sadb_x_policy_dir
));
1380 /* turn bypass off */
1381 if (ipsec_bypass
!= 0)
1384 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1388 ipsec4_get_policy(inp
, request
, len
, mp
)
1394 struct sadb_x_policy
*xpl
;
1395 struct secpolicy
*pcb_sp
;
1399 if (inp
== NULL
|| request
== NULL
|| mp
== NULL
)
1401 if (len
< sizeof(*xpl
))
1403 xpl
= (struct sadb_x_policy
*)request
;
1405 if (inp
->inp_sp
== NULL
) {
1406 error
= ipsec_init_policy(inp
->inp_socket
, &inp
->inp_sp
);
1411 /* select direction */
1412 switch (xpl
->sadb_x_policy_dir
) {
1413 case IPSEC_DIR_INBOUND
:
1414 pcb_sp
= inp
->inp_sp
->sp_in
;
1416 case IPSEC_DIR_OUTBOUND
:
1417 pcb_sp
= inp
->inp_sp
->sp_out
;
1420 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1421 xpl
->sadb_x_policy_dir
));
1425 return ipsec_get_policy(pcb_sp
, mp
);
1428 /* delete policy in PCB */
1430 ipsec4_delete_pcbpolicy(inp
)
1435 panic("ipsec4_delete_pcbpolicy: NULL pointer was passed.\n");
1437 if (inp
->inp_sp
== NULL
)
1440 if (inp
->inp_sp
->sp_in
!= NULL
) {
1441 key_freesp(inp
->inp_sp
->sp_in
);
1442 inp
->inp_sp
->sp_in
= NULL
;
1445 if (inp
->inp_sp
->sp_out
!= NULL
) {
1446 key_freesp(inp
->inp_sp
->sp_out
);
1447 inp
->inp_sp
->sp_out
= NULL
;
1450 ipsec_delpcbpolicy(inp
->inp_sp
);
1458 ipsec6_set_policy(in6p
, optname
, request
, len
, priv
)
1459 struct in6pcb
*in6p
;
1465 struct sadb_x_policy
*xpl
;
1466 struct secpolicy
**pcb_sp
;
1470 if (in6p
== NULL
|| request
== NULL
)
1472 if (len
< sizeof(*xpl
))
1474 xpl
= (struct sadb_x_policy
*)request
;
1476 if (in6p
->in6p_sp
== NULL
) {
1477 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1482 /* select direction */
1483 switch (xpl
->sadb_x_policy_dir
) {
1484 case IPSEC_DIR_INBOUND
:
1485 pcb_sp
= &in6p
->in6p_sp
->sp_in
;
1487 case IPSEC_DIR_OUTBOUND
:
1488 pcb_sp
= &in6p
->in6p_sp
->sp_out
;
1491 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1492 xpl
->sadb_x_policy_dir
));
1496 /* turn bypass off */
1497 if (ipsec_bypass
!= 0)
1500 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1504 ipsec6_get_policy(in6p
, request
, len
, mp
)
1505 struct in6pcb
*in6p
;
1510 struct sadb_x_policy
*xpl
;
1511 struct secpolicy
*pcb_sp
;
1515 if (in6p
== NULL
|| request
== NULL
|| mp
== NULL
)
1517 if (len
< sizeof(*xpl
))
1519 xpl
= (struct sadb_x_policy
*)request
;
1521 if (in6p
->in6p_sp
== NULL
) {
1522 error
= ipsec_init_policy(in6p
->inp_socket
, &in6p
->in6p_sp
);
1527 /* select direction */
1528 switch (xpl
->sadb_x_policy_dir
) {
1529 case IPSEC_DIR_INBOUND
:
1530 pcb_sp
= in6p
->in6p_sp
->sp_in
;
1532 case IPSEC_DIR_OUTBOUND
:
1533 pcb_sp
= in6p
->in6p_sp
->sp_out
;
1536 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1537 xpl
->sadb_x_policy_dir
));
1541 return ipsec_get_policy(pcb_sp
, mp
);
1545 ipsec6_delete_pcbpolicy(in6p
)
1546 struct in6pcb
*in6p
;
1550 panic("ipsec6_delete_pcbpolicy: NULL pointer was passed.\n");
1552 if (in6p
->in6p_sp
== NULL
)
1555 if (in6p
->in6p_sp
->sp_in
!= NULL
) {
1556 key_freesp(in6p
->in6p_sp
->sp_in
);
1557 in6p
->in6p_sp
->sp_in
= NULL
;
1560 if (in6p
->in6p_sp
->sp_out
!= NULL
) {
1561 key_freesp(in6p
->in6p_sp
->sp_out
);
1562 in6p
->in6p_sp
->sp_out
= NULL
;
1565 ipsec_delpcbpolicy(in6p
->in6p_sp
);
1566 in6p
->in6p_sp
= NULL
;
1573 * return current level.
1574 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1577 ipsec_get_reqlevel(isr
)
1578 struct ipsecrequest
*isr
;
1581 u_int esp_trans_deflev
, esp_net_deflev
, ah_trans_deflev
, ah_net_deflev
;
1584 if (isr
== NULL
|| isr
->sp
== NULL
)
1585 panic("ipsec_get_reqlevel: NULL pointer is passed.\n");
1586 if (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
1587 != ((struct sockaddr
*)&isr
->sp
->spidx
.dst
)->sa_family
)
1588 panic("ipsec_get_reqlevel: family mismatched.\n");
1590 /* XXX note that we have ipseclog() expanded here - code sync issue */
1591 #define IPSEC_CHECK_DEFAULT(lev) \
1592 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1593 && (lev) != IPSEC_LEVEL_UNIQUE) \
1595 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1596 (lev), IPSEC_LEVEL_REQUIRE) \
1598 (lev) = IPSEC_LEVEL_REQUIRE, \
1602 /* set default level */
1603 switch (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
) {
1606 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev
);
1607 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev
);
1608 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev
);
1609 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev
);
1614 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev
);
1615 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev
);
1616 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev
);
1617 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev
);
1621 panic("key_get_reqlevel: Unknown family. %d\n",
1622 ((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
);
1625 #undef IPSEC_CHECK_DEFAULT
1628 switch (isr
->level
) {
1629 case IPSEC_LEVEL_DEFAULT
:
1630 switch (isr
->saidx
.proto
) {
1632 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1633 level
= esp_net_deflev
;
1635 level
= esp_trans_deflev
;
1638 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1639 level
= ah_net_deflev
;
1641 level
= ah_trans_deflev
;
1642 case IPPROTO_IPCOMP
:
1644 * we don't really care, as IPcomp document says that
1645 * we shouldn't compress small packets
1647 level
= IPSEC_LEVEL_USE
;
1650 panic("ipsec_get_reqlevel: "
1651 "Illegal protocol defined %u\n",
1656 case IPSEC_LEVEL_USE
:
1657 case IPSEC_LEVEL_REQUIRE
:
1660 case IPSEC_LEVEL_UNIQUE
:
1661 level
= IPSEC_LEVEL_REQUIRE
;
1665 panic("ipsec_get_reqlevel: Illegal IPsec level %u\n",
1673 * Check AH/ESP integrity.
1679 ipsec_in_reject(sp
, m
)
1680 struct secpolicy
*sp
;
1683 struct ipsecrequest
*isr
;
1685 int need_auth
, need_conf
, need_icv
;
1687 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1688 printf("ipsec_in_reject: using SP\n");
1689 kdebug_secpolicy(sp
));
1692 switch (sp
->policy
) {
1693 case IPSEC_POLICY_DISCARD
:
1695 case IPSEC_POLICY_BYPASS
:
1696 case IPSEC_POLICY_NONE
:
1699 case IPSEC_POLICY_IPSEC
:
1702 case IPSEC_POLICY_ENTRUST
:
1704 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1711 /* XXX should compare policy against ipsec header history */
1713 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1715 /* get current level */
1716 level
= ipsec_get_reqlevel(isr
);
1718 switch (isr
->saidx
.proto
) {
1720 if (level
== IPSEC_LEVEL_REQUIRE
) {
1723 if (isr
->sav
!= NULL
1724 && isr
->sav
->flags
== SADB_X_EXT_NONE
1725 && isr
->sav
->alg_auth
!= SADB_AALG_NONE
)
1730 if (level
== IPSEC_LEVEL_REQUIRE
) {
1735 case IPPROTO_IPCOMP
:
1737 * we don't really care, as IPcomp document says that
1738 * we shouldn't compress small packets, IPComp policy
1739 * should always be treated as being in "use" level.
1745 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1746 printf("ipsec_in_reject: auth:%d conf:%d icv:%d m_flags:%x\n",
1747 need_auth
, need_conf
, need_icv
, m
->m_flags
));
1749 if ((need_conf
&& !(m
->m_flags
& M_DECRYPTED
))
1750 || (!need_auth
&& need_icv
&& !(m
->m_flags
& M_AUTHIPDGM
))
1751 || (need_auth
&& !(m
->m_flags
& M_AUTHIPHDR
)))
1758 * Check AH/ESP integrity.
1759 * This function is called from tcp_input(), udp_input(),
1760 * and {ah,esp}4_input for tunnel mode
1763 ipsec4_in_reject_so(m
, so
)
1767 struct secpolicy
*sp
= NULL
;
1773 return 0; /* XXX should be panic ? */
1775 /* get SP for this packet.
1776 * When we are called from ip_forward(), we call
1777 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1780 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1782 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1785 return 0; /* XXX should be panic ?
1786 * -> No, there may be error. */
1788 result
= ipsec_in_reject(sp
, m
);
1789 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1790 printf("DP ipsec4_in_reject_so call free SP:%p\n", sp
));
1797 ipsec4_in_reject(m
, inp
)
1802 return ipsec4_in_reject_so(m
, NULL
);
1803 if (inp
->inp_socket
)
1804 return ipsec4_in_reject_so(m
, inp
->inp_socket
);
1806 panic("ipsec4_in_reject: invalid inpcb/socket");
1811 * Check AH/ESP integrity.
1812 * This function is called from tcp6_input(), udp6_input(),
1813 * and {ah,esp}6_input for tunnel mode
1816 ipsec6_in_reject_so(m
, so
)
1820 struct secpolicy
*sp
= NULL
;
1826 return 0; /* XXX should be panic ? */
1828 /* get SP for this packet.
1829 * When we are called from ip_forward(), we call
1830 * ipsec6_getpolicybyaddr() with IP_FORWARDING flag.
1833 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1835 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1838 return 0; /* XXX should be panic ? */
1840 result
= ipsec_in_reject(sp
, m
);
1841 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1842 printf("DP ipsec6_in_reject_so call free SP:%p\n", sp
));
1849 ipsec6_in_reject(m
, in6p
)
1851 struct in6pcb
*in6p
;
1854 return ipsec6_in_reject_so(m
, NULL
);
1855 if (in6p
->in6p_socket
)
1856 return ipsec6_in_reject_so(m
, in6p
->in6p_socket
);
1858 panic("ipsec6_in_reject: invalid in6p/socket");
1863 * compute the byte size to be occupied by IPsec header.
1864 * in case it is tunneled, it includes the size of outer IP header.
1865 * NOTE: SP passed is free in this function.
1869 struct secpolicy
*sp
;
1871 struct ipsecrequest
*isr
;
1874 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1875 printf("ipsec_in_reject: using SP\n");
1876 kdebug_secpolicy(sp
));
1879 switch (sp
->policy
) {
1880 case IPSEC_POLICY_DISCARD
:
1881 case IPSEC_POLICY_BYPASS
:
1882 case IPSEC_POLICY_NONE
:
1885 case IPSEC_POLICY_IPSEC
:
1888 case IPSEC_POLICY_ENTRUST
:
1890 panic("ipsec_hdrsiz: Invalid policy found. %d\n", sp
->policy
);
1895 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1899 switch (isr
->saidx
.proto
) {
1902 clen
= esp_hdrsiz(isr
);
1908 clen
= ah_hdrsiz(isr
);
1910 case IPPROTO_IPCOMP
:
1911 clen
= sizeof(struct ipcomp
);
1915 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
1916 switch (((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
) {
1918 clen
+= sizeof(struct ip
);
1922 clen
+= sizeof(struct ip6_hdr
);
1926 ipseclog((LOG_ERR
, "ipsec_hdrsiz: "
1927 "unknown AF %d in IPsec tunnel SA\n",
1928 ((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
));
1938 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1940 ipsec4_hdrsiz(m
, dir
, inp
)
1945 struct secpolicy
*sp
= NULL
;
1951 return 0; /* XXX should be panic ? */
1952 if (inp
!= NULL
&& inp
->inp_socket
== NULL
)
1953 panic("ipsec4_hdrsize: why is socket NULL but there is PCB.");
1955 /* get SP for this packet.
1956 * When we are called from ip_forward(), we call
1957 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1960 sp
= ipsec4_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
1962 sp
= ipsec4_getpolicybysock(m
, dir
, inp
->inp_socket
, &error
);
1965 return 0; /* XXX should be panic ? */
1967 size
= ipsec_hdrsiz(sp
);
1968 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1969 printf("DP ipsec4_hdrsiz call free SP:%p\n", sp
));
1970 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1971 printf("ipsec4_hdrsiz: size:%lu.\n", (unsigned long)size
));
1978 /* This function is called from ipsec6_hdrsize_tcp(),
1979 * and maybe from ip6_forward.()
1982 ipsec6_hdrsiz(m
, dir
, in6p
)
1985 struct in6pcb
*in6p
;
1987 struct secpolicy
*sp
= NULL
;
1993 return 0; /* XXX shoud be panic ? */
1994 if (in6p
!= NULL
&& in6p
->in6p_socket
== NULL
)
1995 panic("ipsec6_hdrsize: why is socket NULL but there is PCB.");
1997 /* get SP for this packet */
1998 /* XXX Is it right to call with IP_FORWARDING. */
2000 sp
= ipsec6_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
2002 sp
= ipsec6_getpolicybysock(m
, dir
, in6p
->in6p_socket
, &error
);
2006 size
= ipsec_hdrsiz(sp
);
2007 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
2008 printf("DP ipsec6_hdrsiz call free SP:%p\n", sp
));
2009 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2010 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size
));
2019 * encapsulate for ipsec tunnel.
2020 * ip->ip_src must be fixed later on.
2023 ipsec4_encapsulate(m
, sav
)
2025 struct secasvar
*sav
;
2032 /* can't tunnel between different AFs */
2033 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2034 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2035 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2040 /* XXX if the dst is myself, perform nothing. */
2041 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2047 if (m
->m_len
< sizeof(*ip
))
2048 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2050 ip
= mtod(m
, struct ip
*);
2052 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
2054 hlen
= ip
->ip_hl
<< 2;
2057 if (m
->m_len
!= hlen
)
2058 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
2060 /* generate header checksum */
2063 ip
->ip_sum
= in_cksum(m
, hlen
);
2065 ip
->ip_sum
= in_cksum(m
, hlen
);
2068 plen
= m
->m_pkthdr
.len
;
2071 * grow the mbuf to accomodate the new IPv4 header.
2072 * NOTE: IPv4 options will never be copied.
2074 if (M_LEADINGSPACE(m
->m_next
) < hlen
) {
2076 MGET(n
, M_DONTWAIT
, MT_DATA
);
2082 n
->m_next
= m
->m_next
;
2084 m
->m_pkthdr
.len
+= hlen
;
2085 oip
= mtod(n
, struct ip
*);
2087 m
->m_next
->m_len
+= hlen
;
2088 m
->m_next
->m_data
-= hlen
;
2089 m
->m_pkthdr
.len
+= hlen
;
2090 oip
= mtod(m
->m_next
, struct ip
*);
2092 ip
= mtod(m
, struct ip
*);
2093 ovbcopy((caddr_t
)ip
, (caddr_t
)oip
, hlen
);
2094 m
->m_len
= sizeof(struct ip
);
2095 m
->m_pkthdr
.len
-= (hlen
- sizeof(struct ip
));
2097 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
2098 /* ECN consideration. */
2099 ip_ecn_ingress(ip4_ipsec_ecn
, &ip
->ip_tos
, &oip
->ip_tos
);
2101 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
2103 ip
->ip_hl
= sizeof(struct ip
) >> 2;
2105 ip
->ip_off
&= htons(~IP_OFFMASK
);
2106 ip
->ip_off
&= htons(~IP_MF
);
2107 switch (ip4_ipsec_dfbit
) {
2108 case 0: /*clear DF bit*/
2109 ip
->ip_off
&= htons(~IP_DF
);
2111 case 1: /*set DF bit*/
2112 ip
->ip_off
|= htons(IP_DF
);
2114 default: /*copy DF bit*/
2117 ip
->ip_p
= IPPROTO_IPIP
;
2118 if (plen
+ sizeof(struct ip
) < IP_MAXPACKET
)
2119 ip
->ip_len
= htons(plen
+ sizeof(struct ip
));
2121 ipseclog((LOG_ERR
, "IPv4 ipsec: size exceeds limit: "
2122 "leave ip_len as is (invalid packet)\n"));
2125 ip
->ip_id
= ip_randomid();
2127 ip
->ip_id
= htons(ip_id
++);
2129 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
,
2130 &ip
->ip_src
, sizeof(ip
->ip_src
));
2131 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
,
2132 &ip
->ip_dst
, sizeof(ip
->ip_dst
));
2133 ip
->ip_ttl
= IPDEFTTL
;
2135 /* XXX Should ip_src be updated later ? */
2143 ipsec6_encapsulate(m
, sav
)
2145 struct secasvar
*sav
;
2147 struct ip6_hdr
*oip6
;
2148 struct ip6_hdr
*ip6
;
2151 /* can't tunnel between different AFs */
2152 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2153 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2154 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
2159 /* XXX if the dst is myself, perform nothing. */
2160 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2166 plen
= m
->m_pkthdr
.len
;
2169 * grow the mbuf to accomodate the new IPv6 header.
2171 if (m
->m_len
!= sizeof(struct ip6_hdr
))
2172 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2173 if (M_LEADINGSPACE(m
->m_next
) < sizeof(struct ip6_hdr
)) {
2175 MGET(n
, M_DONTWAIT
, MT_DATA
);
2180 n
->m_len
= sizeof(struct ip6_hdr
);
2181 n
->m_next
= m
->m_next
;
2183 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2184 oip6
= mtod(n
, struct ip6_hdr
*);
2186 m
->m_next
->m_len
+= sizeof(struct ip6_hdr
);
2187 m
->m_next
->m_data
-= sizeof(struct ip6_hdr
);
2188 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2189 oip6
= mtod(m
->m_next
, struct ip6_hdr
*);
2191 ip6
= mtod(m
, struct ip6_hdr
*);
2192 ovbcopy((caddr_t
)ip6
, (caddr_t
)oip6
, sizeof(struct ip6_hdr
));
2194 /* Fake link-local scope-class addresses */
2195 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
2196 oip6
->ip6_src
.s6_addr16
[1] = 0;
2197 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
2198 oip6
->ip6_dst
.s6_addr16
[1] = 0;
2200 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
2201 /* ECN consideration. */
2202 ip6_ecn_ingress(ip6_ipsec_ecn
, &ip6
->ip6_flow
, &oip6
->ip6_flow
);
2203 if (plen
< IPV6_MAXPACKET
- sizeof(struct ip6_hdr
))
2204 ip6
->ip6_plen
= htons(plen
);
2206 /* ip6->ip6_plen will be updated in ip6_output() */
2208 ip6
->ip6_nxt
= IPPROTO_IPV6
;
2209 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.src
)->sin6_addr
,
2210 &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
2211 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
)->sin6_addr
,
2212 &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
2213 ip6
->ip6_hlim
= IPV6_DEFHLIM
;
2215 /* XXX Should ip6_src be updated later ? */
2222 * Check the variable replay window.
2223 * ipsec_chkreplay() performs replay check before ICV verification.
2224 * ipsec_updatereplay() updates replay bitmap. This must be called after
2225 * ICV verification (it also performs replay check, which is usually done
2227 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2229 * based on RFC 2401.
2232 ipsec_chkreplay(seq
, sav
)
2234 struct secasvar
*sav
;
2236 const struct secreplay
*replay
;
2239 u_int32_t wsizeb
; /* constant: bits of window size */
2240 int frlast
; /* constant: last frame */
2244 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2246 replay
= sav
->replay
;
2248 if (replay
->wsize
== 0)
2249 return 1; /* no need to check replay. */
2252 frlast
= replay
->wsize
- 1;
2253 wsizeb
= replay
->wsize
<< 3;
2255 /* sequence number of 0 is invalid */
2259 /* first time is always okay */
2260 if (replay
->count
== 0)
2263 if (seq
> replay
->lastseq
) {
2264 /* larger sequences are okay */
2267 /* seq is equal or less than lastseq. */
2268 diff
= replay
->lastseq
- seq
;
2270 /* over range to check, i.e. too old or wrapped */
2274 fr
= frlast
- diff
/ 8;
2276 /* this packet already seen ? */
2277 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2280 /* out of order but good */
2286 * check replay counter whether to update or not.
2291 ipsec_updatereplay(seq
, sav
)
2293 struct secasvar
*sav
;
2295 struct secreplay
*replay
;
2298 u_int32_t wsizeb
; /* constant: bits of window size */
2299 int frlast
; /* constant: last frame */
2303 panic("ipsec_chkreplay: NULL pointer was passed.\n");
2305 replay
= sav
->replay
;
2307 if (replay
->wsize
== 0)
2308 goto ok
; /* no need to check replay. */
2311 frlast
= replay
->wsize
- 1;
2312 wsizeb
= replay
->wsize
<< 3;
2314 /* sequence number of 0 is invalid */
2319 if (replay
->count
== 0) {
2320 replay
->lastseq
= seq
;
2321 bzero(replay
->bitmap
, replay
->wsize
);
2322 (replay
->bitmap
)[frlast
] = 1;
2326 if (seq
> replay
->lastseq
) {
2327 /* seq is larger than lastseq. */
2328 diff
= seq
- replay
->lastseq
;
2330 /* new larger sequence number */
2331 if (diff
< wsizeb
) {
2333 /* set bit for this packet */
2334 vshiftl(replay
->bitmap
, diff
, replay
->wsize
);
2335 (replay
->bitmap
)[frlast
] |= 1;
2337 /* this packet has a "way larger" */
2338 bzero(replay
->bitmap
, replay
->wsize
);
2339 (replay
->bitmap
)[frlast
] = 1;
2341 replay
->lastseq
= seq
;
2343 /* larger is good */
2345 /* seq is equal or less than lastseq. */
2346 diff
= replay
->lastseq
- seq
;
2348 /* over range to check, i.e. too old or wrapped */
2352 fr
= frlast
- diff
/ 8;
2354 /* this packet already seen ? */
2355 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2359 (replay
->bitmap
)[fr
] |= (1 << (diff
% 8));
2361 /* out of order but good */
2365 if (replay
->count
== ~0) {
2367 /* set overflow flag */
2370 /* don't increment, no more packets accepted */
2371 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0)
2374 ipseclog((LOG_WARNING
, "replay counter made %d cycle. %s\n",
2375 replay
->overflow
, ipsec_logsastr(sav
)));
2384 * shift variable length bunffer to left.
2385 * IN: bitmap: pointer to the buffer
2386 * nbit: the number of to shift.
2387 * wsize: buffer size (bytes).
2390 vshiftl(bitmap
, nbit
, wsize
)
2391 unsigned char *bitmap
;
2397 for (j
= 0; j
< nbit
; j
+= 8) {
2398 s
= (nbit
- j
< 8) ? (nbit
- j
): 8;
2400 for (i
= 1; i
< wsize
; i
++) {
2401 over
= (bitmap
[i
] >> (8 - s
));
2403 bitmap
[i
-1] |= over
;
2411 ipsec4_logpacketstr(ip
, spi
)
2415 static char buf
[256];
2419 s
= (u_int8_t
*)(&ip
->ip_src
);
2420 d
= (u_int8_t
*)(&ip
->ip_dst
);
2423 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2426 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%u.%u.%u.%u",
2427 s
[0], s
[1], s
[2], s
[3]);
2430 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%u.%u.%u.%u",
2431 d
[0], d
[1], d
[2], d
[3]);
2434 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2441 ipsec6_logpacketstr(ip6
, spi
)
2442 struct ip6_hdr
*ip6
;
2445 static char buf
[256];
2449 snprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2452 snprintf(p
, sizeof(buf
) - (p
- buf
), "src=%s",
2453 ip6_sprintf(&ip6
->ip6_src
));
2456 snprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%s",
2457 ip6_sprintf(&ip6
->ip6_dst
));
2460 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2468 struct secasvar
*sav
;
2470 static char buf
[256];
2472 struct secasindex
*saidx
= &sav
->sah
->saidx
;
2474 /* validity check */
2475 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2476 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
)
2477 panic("ipsec_logsastr: family mismatched.\n");
2480 snprintf(buf
, sizeof(buf
), "SA(SPI=%u ", (u_int32_t
)ntohl(sav
->spi
));
2483 if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET
) {
2485 s
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->src
)->sin_addr
;
2486 d
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->dst
)->sin_addr
;
2487 snprintf(p
, sizeof(buf
) - (p
- buf
),
2488 "src=%d.%d.%d.%d dst=%d.%d.%d.%d",
2489 s
[0], s
[1], s
[2], s
[3], d
[0], d
[1], d
[2], d
[3]);
2492 else if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET6
) {
2493 snprintf(p
, sizeof(buf
) - (p
- buf
),
2495 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->src
)->sin6_addr
));
2498 snprintf(p
, sizeof(buf
) - (p
- buf
),
2500 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->dst
)->sin6_addr
));
2505 snprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2521 p
= mtod(m
, u_char
*);
2522 for (i
= 0; i
< m
->m_len
; i
++) {
2523 printf("%02x ", p
[i
]);
2525 if (totlen
% 16 == 0)
2530 if (totlen
% 16 != 0)
2537 * IPsec output logic for IPv4.
2540 ipsec4_output(state
, sp
, flags
)
2541 struct ipsec_output_state
*state
;
2542 struct secpolicy
*sp
;
2545 struct ip
*ip
= NULL
;
2546 struct ipsecrequest
*isr
= NULL
;
2547 struct secasindex saidx
;
2550 struct sockaddr_in
*dst4
;
2551 struct sockaddr_in
*sin
;
2554 panic("state == NULL in ipsec4_output");
2556 panic("state->m == NULL in ipsec4_output");
2558 panic("state->ro == NULL in ipsec4_output");
2560 panic("state->dst == NULL in ipsec4_output");
2562 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2563 printf("ipsec4_output: applyed SP\n");
2564 kdebug_secpolicy(sp
));
2566 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2568 #if 0 /* give up to check restriction of transport mode */
2569 /* XXX but should be checked somewhere */
2571 * some of the IPsec operation must be performed only in
2574 if (isr
->saidx
.mode
== IPSEC_MODE_TRANSPORT
2575 && (flags
& IP_FORWARDING
))
2579 /* make SA index for search proper SA */
2580 ip
= mtod(state
->m
, struct ip
*);
2581 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2582 saidx
.mode
= isr
->saidx
.mode
;
2583 saidx
.reqid
= isr
->saidx
.reqid
;
2584 sin
= (struct sockaddr_in
*)&saidx
.src
;
2585 if (sin
->sin_len
== 0) {
2586 sin
->sin_len
= sizeof(*sin
);
2587 sin
->sin_family
= AF_INET
;
2588 sin
->sin_port
= IPSEC_PORT_ANY
;
2589 bcopy(&ip
->ip_src
, &sin
->sin_addr
,
2590 sizeof(sin
->sin_addr
));
2592 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2593 if (sin
->sin_len
== 0) {
2594 sin
->sin_len
= sizeof(*sin
);
2595 sin
->sin_family
= AF_INET
;
2596 sin
->sin_port
= IPSEC_PORT_ANY
;
2597 bcopy(&ip
->ip_dst
, &sin
->sin_addr
,
2598 sizeof(sin
->sin_addr
));
2601 if ((error
= key_checkrequest(isr
, &saidx
)) != 0) {
2603 * IPsec processing is required, but no SA found.
2604 * I assume that key_acquire() had been called
2605 * to get/establish the SA. Here I discard
2606 * this packet because it is responsibility for
2607 * upper layer to retransmit the packet.
2609 ipsecstat
.out_nosa
++;
2613 /* validity check */
2614 if (isr
->sav
== NULL
) {
2615 switch (ipsec_get_reqlevel(isr
)) {
2616 case IPSEC_LEVEL_USE
:
2618 case IPSEC_LEVEL_REQUIRE
:
2619 /* must be not reached here. */
2620 panic("ipsec4_output: no SA found, but required.");
2625 * If there is no valid SA, we give up to process any
2626 * more. In such a case, the SA's status is changed
2627 * from DYING to DEAD after allocating. If a packet
2628 * send to the receiver by dead SA, the receiver can
2629 * not decode a packet because SA has been dead.
2631 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2632 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2633 ipsecstat
.out_nosa
++;
2639 * There may be the case that SA status will be changed when
2640 * we are refering to one. So calling splsoftnet().
2644 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2646 * build IPsec tunnel.
2648 /* XXX should be processed with other familiy */
2649 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2650 ipseclog((LOG_ERR
, "ipsec4_output: "
2651 "family mismatched between inner and outer spi=%u\n",
2652 (u_int32_t
)ntohl(isr
->sav
->spi
)));
2654 error
= EAFNOSUPPORT
;
2658 state
->m
= ipsec4_splithdr(state
->m
);
2664 error
= ipsec4_encapsulate(state
->m
, isr
->sav
);
2670 ip
= mtod(state
->m
, struct ip
*);
2672 state
->ro
= &isr
->sav
->sah
->sa_route
;
2673 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
2674 dst4
= (struct sockaddr_in
*)state
->dst
;
2675 if (state
->ro
->ro_rt
2676 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
2677 || dst4
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
2678 rtfree(state
->ro
->ro_rt
);
2679 state
->ro
->ro_rt
= NULL
;
2681 if (state
->ro
->ro_rt
== 0) {
2682 dst4
->sin_family
= AF_INET
;
2683 dst4
->sin_len
= sizeof(*dst4
);
2684 dst4
->sin_addr
= ip
->ip_dst
;
2687 if (state
->ro
->ro_rt
== 0) {
2688 ipstat
.ips_noroute
++;
2689 error
= EHOSTUNREACH
;
2693 /* adjust state->dst if tunnel endpoint is offlink */
2694 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
2695 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
2696 dst4
= (struct sockaddr_in
*)state
->dst
;
2701 state
->m
= ipsec4_splithdr(state
->m
);
2706 switch (isr
->saidx
.proto
) {
2709 if ((error
= esp4_output(state
->m
, isr
)) != 0) {
2721 if ((error
= ah4_output(state
->m
, isr
)) != 0) {
2726 case IPPROTO_IPCOMP
:
2727 if ((error
= ipcomp4_output(state
->m
, isr
)) != 0) {
2734 "ipsec4_output: unknown ipsec protocol %d\n",
2742 if (state
->m
== 0) {
2746 ip
= mtod(state
->m
, struct ip
*);
2760 * IPsec output logic for IPv6, transport mode.
2763 ipsec6_output_trans(state
, nexthdrp
, mprev
, sp
, flags
, tun
)
2764 struct ipsec_output_state
*state
;
2767 struct secpolicy
*sp
;
2771 struct ip6_hdr
*ip6
;
2772 struct ipsecrequest
*isr
= NULL
;
2773 struct secasindex saidx
;
2776 struct sockaddr_in6
*sin6
;
2779 panic("state == NULL in ipsec6_output");
2781 panic("state->m == NULL in ipsec6_output");
2783 panic("nexthdrp == NULL in ipsec6_output");
2785 panic("mprev == NULL in ipsec6_output");
2787 panic("sp == NULL in ipsec6_output");
2789 panic("tun == NULL in ipsec6_output");
2791 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2792 printf("ipsec6_output_trans: applyed SP\n");
2793 kdebug_secpolicy(sp
));
2796 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
2797 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2798 /* the rest will be handled by ipsec6_output_tunnel() */
2802 /* make SA index for search proper SA */
2803 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2804 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2805 saidx
.mode
= isr
->saidx
.mode
;
2806 saidx
.reqid
= isr
->saidx
.reqid
;
2807 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
2808 if (sin6
->sin6_len
== 0) {
2809 sin6
->sin6_len
= sizeof(*sin6
);
2810 sin6
->sin6_family
= AF_INET6
;
2811 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2812 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
2813 sizeof(ip6
->ip6_src
));
2814 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
2815 /* fix scope id for comparing SPD */
2816 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2817 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
2820 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
2821 if (sin6
->sin6_len
== 0) {
2822 sin6
->sin6_len
= sizeof(*sin6
);
2823 sin6
->sin6_family
= AF_INET6
;
2824 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2825 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
2826 sizeof(ip6
->ip6_dst
));
2827 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
2828 /* fix scope id for comparing SPD */
2829 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2830 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
2834 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
2836 * IPsec processing is required, but no SA found.
2837 * I assume that key_acquire() had been called
2838 * to get/establish the SA. Here I discard
2839 * this packet because it is responsibility for
2840 * upper layer to retransmit the packet.
2842 ipsec6stat
.out_nosa
++;
2846 * Notify the fact that the packet is discarded
2847 * to ourselves. I believe this is better than
2848 * just silently discarding. (jinmei@kame.net)
2849 * XXX: should we restrict the error to TCP packets?
2850 * XXX: should we directly notify sockets via
2853 icmp6_error(state
->m
, ICMP6_DST_UNREACH
,
2854 ICMP6_DST_UNREACH_ADMIN
, 0);
2855 state
->m
= NULL
; /* icmp6_error freed the mbuf */
2859 /* validity check */
2860 if (isr
->sav
== NULL
) {
2861 switch (ipsec_get_reqlevel(isr
)) {
2862 case IPSEC_LEVEL_USE
:
2864 case IPSEC_LEVEL_REQUIRE
:
2865 /* must be not reached here. */
2866 panic("ipsec6_output_trans: no SA found, but required.");
2871 * If there is no valid SA, we give up to process.
2872 * see same place at ipsec4_output().
2874 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2875 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2876 ipsec6stat
.out_nosa
++;
2881 switch (isr
->saidx
.proto
) {
2884 error
= esp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2891 error
= ah6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2893 case IPPROTO_IPCOMP
:
2894 error
= ipcomp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2897 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
2898 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
2900 ipsec6stat
.out_inval
++;
2908 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2909 if (plen
> IPV6_MAXPACKET
) {
2910 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
2911 "IPsec with IPv6 jumbogram is not supported\n"));
2912 ipsec6stat
.out_inval
++;
2913 error
= EINVAL
; /*XXX*/
2916 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2917 ip6
->ip6_plen
= htons(plen
);
2920 /* if we have more to go, we need a tunnel mode processing */
2933 * IPsec output logic for IPv6, tunnel mode.
2936 ipsec6_output_tunnel(state
, sp
, flags
)
2937 struct ipsec_output_state
*state
;
2938 struct secpolicy
*sp
;
2941 struct ip6_hdr
*ip6
;
2942 struct ipsecrequest
*isr
= NULL
;
2943 struct secasindex saidx
;
2946 struct sockaddr_in6
* dst6
;
2950 panic("state == NULL in ipsec6_output");
2952 panic("state->m == NULL in ipsec6_output");
2954 panic("sp == NULL in ipsec6_output");
2956 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2957 printf("ipsec6_output_tunnel: applyed SP\n");
2958 kdebug_secpolicy(sp
));
2961 * transport mode ipsec (before the 1st tunnel mode) is already
2962 * processed by ipsec6_output_trans().
2964 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
2965 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
2969 for (/*already initialized*/; isr
; isr
= isr
->next
) {
2970 /* When tunnel mode, SA peers must be specified. */
2971 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2972 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
2974 * IPsec processing is required, but no SA found.
2975 * I assume that key_acquire() had been called
2976 * to get/establish the SA. Here I discard
2977 * this packet because it is responsibility for
2978 * upper layer to retransmit the packet.
2980 ipsec6stat
.out_nosa
++;
2985 /* validity check */
2986 if (isr
->sav
== NULL
) {
2987 switch (ipsec_get_reqlevel(isr
)) {
2988 case IPSEC_LEVEL_USE
:
2990 case IPSEC_LEVEL_REQUIRE
:
2991 /* must be not reached here. */
2992 panic("ipsec6_output_tunnel: no SA found, but required.");
2997 * If there is no valid SA, we give up to process.
2998 * see same place at ipsec4_output().
3000 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
3001 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
3002 ipsec6stat
.out_nosa
++;
3008 * There may be the case that SA status will be changed when
3009 * we are refering to one. So calling splsoftnet().
3013 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
3015 * build IPsec tunnel.
3017 /* XXX should be processed with other familiy */
3018 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
3019 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3020 "family mismatched between inner and outer, spi=%u\n",
3021 (u_int32_t
)ntohl(isr
->sav
->spi
)));
3023 ipsec6stat
.out_inval
++;
3024 error
= EAFNOSUPPORT
;
3028 state
->m
= ipsec6_splithdr(state
->m
);
3031 ipsec6stat
.out_nomem
++;
3035 error
= ipsec6_encapsulate(state
->m
, isr
->sav
);
3041 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3043 state
->ro
= &isr
->sav
->sah
->sa_route
;
3044 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
3045 dst6
= (struct sockaddr_in6
*)state
->dst
;
3046 if (state
->ro
->ro_rt
3047 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
3048 || !IN6_ARE_ADDR_EQUAL(&dst6
->sin6_addr
, &ip6
->ip6_dst
))) {
3049 rtfree(state
->ro
->ro_rt
);
3050 state
->ro
->ro_rt
= NULL
;
3052 if (state
->ro
->ro_rt
== 0) {
3053 bzero(dst6
, sizeof(*dst6
));
3054 dst6
->sin6_family
= AF_INET6
;
3055 dst6
->sin6_len
= sizeof(*dst6
);
3056 dst6
->sin6_addr
= ip6
->ip6_dst
;
3059 if (state
->ro
->ro_rt
== 0) {
3060 ip6stat
.ip6s_noroute
++;
3061 ipsec6stat
.out_noroute
++;
3062 error
= EHOSTUNREACH
;
3066 /* adjust state->dst if tunnel endpoint is offlink */
3067 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
3068 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
3069 dst6
= (struct sockaddr_in6
*)state
->dst
;
3074 state
->m
= ipsec6_splithdr(state
->m
);
3076 ipsec6stat
.out_nomem
++;
3080 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3081 switch (isr
->saidx
.proto
) {
3084 error
= esp6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
3091 error
= ah6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
3093 case IPPROTO_IPCOMP
:
3094 /* XXX code should be here */
3097 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3098 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
3100 ipsec6stat
.out_inval
++;
3108 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
3109 if (plen
> IPV6_MAXPACKET
) {
3110 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
3111 "IPsec with IPv6 jumbogram is not supported\n"));
3112 ipsec6stat
.out_inval
++;
3113 error
= EINVAL
; /*XXX*/
3116 ip6
= mtod(state
->m
, struct ip6_hdr
*);
3117 ip6
->ip6_plen
= htons(plen
);
3131 * Chop IP header and option off from the payload.
3133 static struct mbuf
*
3141 if (m
->m_len
< sizeof(struct ip
))
3142 panic("ipsec4_splithdr: first mbuf too short");
3143 ip
= mtod(m
, struct ip
*);
3145 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
3147 hlen
= ip
->ip_hl
<< 2;
3149 if (m
->m_len
> hlen
) {
3150 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3155 M_COPY_PKTHDR(mh
, m
);
3157 m
->m_flags
&= ~M_PKTHDR
;
3163 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), hlen
);
3164 } else if (m
->m_len
< hlen
) {
3165 m
= m_pullup(m
, hlen
);
3174 static struct mbuf
*
3179 struct ip6_hdr
*ip6
;
3182 if (m
->m_len
< sizeof(struct ip6_hdr
))
3183 panic("ipsec6_splithdr: first mbuf too short");
3184 ip6
= mtod(m
, struct ip6_hdr
*);
3185 hlen
= sizeof(struct ip6_hdr
);
3186 if (m
->m_len
> hlen
) {
3187 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3192 M_COPY_PKTHDR(mh
, m
);
3194 m
->m_flags
&= ~M_PKTHDR
;
3200 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), hlen
);
3201 } else if (m
->m_len
< hlen
) {
3202 m
= m_pullup(m
, hlen
);
3210 /* validate inbound IPsec tunnel packet. */
3212 ipsec4_tunnel_validate(m
, off
, nxt0
, sav
)
3213 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3216 struct secasvar
*sav
;
3218 u_int8_t nxt
= nxt0
& 0xff;
3219 struct sockaddr_in
*sin
;
3220 struct sockaddr_in osrc
, odst
, isrc
, idst
;
3222 struct secpolicy
*sp
;
3226 if (m
->m_len
< sizeof(struct ip
))
3227 panic("too short mbuf on ipsec4_tunnel_validate");
3229 if (nxt
!= IPPROTO_IPV4
)
3231 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip
))
3233 /* do not decapsulate if the SA is for transport mode only */
3234 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3237 oip
= mtod(m
, struct ip
*);
3239 hlen
= _IP_VHL_HL(oip
->ip_vhl
) << 2;
3241 hlen
= oip
->ip_hl
<< 2;
3243 if (hlen
!= sizeof(struct ip
))
3246 /* AF_INET6 should be supported, but at this moment we don't. */
3247 sin
= (struct sockaddr_in
*)&sav
->sah
->saidx
.dst
;
3248 if (sin
->sin_family
!= AF_INET
)
3250 if (bcmp(&oip
->ip_dst
, &sin
->sin_addr
, sizeof(oip
->ip_dst
)) != 0)
3254 bzero(&osrc
, sizeof(osrc
));
3255 bzero(&odst
, sizeof(odst
));
3256 bzero(&isrc
, sizeof(isrc
));
3257 bzero(&idst
, sizeof(idst
));
3258 osrc
.sin_family
= odst
.sin_family
= isrc
.sin_family
= idst
.sin_family
=
3260 osrc
.sin_len
= odst
.sin_len
= isrc
.sin_len
= idst
.sin_len
=
3261 sizeof(struct sockaddr_in
);
3262 osrc
.sin_addr
= oip
->ip_src
;
3263 odst
.sin_addr
= oip
->ip_dst
;
3264 m_copydata(m
, off
+ offsetof(struct ip
, ip_src
), sizeof(isrc
.sin_addr
),
3265 (caddr_t
)&isrc
.sin_addr
);
3266 m_copydata(m
, off
+ offsetof(struct ip
, ip_dst
), sizeof(idst
.sin_addr
),
3267 (caddr_t
)&idst
.sin_addr
);
3270 * RFC2401 5.2.1 (b): (assume that we are using tunnel mode)
3271 * - if the inner destination is multicast address, there can be
3272 * multiple permissible inner source address. implementation
3273 * may want to skip verification of inner source address against
3275 * - if the inner protocol is ICMP, the packet may be an error report
3276 * from routers on the other side of the VPN cloud (R in the
3277 * following diagram). in this case, we cannot verify inner source
3278 * address against SPD selector.
3279 * me -- gw === gw -- R -- you
3281 * we consider the first bullet to be users responsibility on SPD entry
3282 * configuration (if you need to encrypt multicast traffic, set
3283 * the source range of SPD selector to 0.0.0.0/0, or have explicit
3284 * address ranges for possible senders).
3285 * the second bullet is not taken care of (yet).
3287 * therefore, we do not do anything special about inner source.
3290 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3291 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3300 /* validate inbound IPsec tunnel packet. */
3302 ipsec6_tunnel_validate(m
, off
, nxt0
, sav
)
3303 struct mbuf
*m
; /* no pullup permitted, m->m_len >= ip */
3306 struct secasvar
*sav
;
3308 u_int8_t nxt
= nxt0
& 0xff;
3309 struct sockaddr_in6
*sin6
;
3310 struct sockaddr_in6 osrc
, odst
, isrc
, idst
;
3311 struct secpolicy
*sp
;
3312 struct ip6_hdr
*oip6
;
3315 if (m
->m_len
< sizeof(struct ip6_hdr
))
3316 panic("too short mbuf on ipsec6_tunnel_validate");
3318 if (nxt
!= IPPROTO_IPV6
)
3320 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip6_hdr
))
3322 /* do not decapsulate if the SA is for transport mode only */
3323 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3326 oip6
= mtod(m
, struct ip6_hdr
*);
3327 /* AF_INET should be supported, but at this moment we don't. */
3328 sin6
= (struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
;
3329 if (sin6
->sin6_family
!= AF_INET6
)
3331 if (!IN6_ARE_ADDR_EQUAL(&oip6
->ip6_dst
, &sin6
->sin6_addr
))
3335 bzero(&osrc
, sizeof(osrc
));
3336 bzero(&odst
, sizeof(odst
));
3337 bzero(&isrc
, sizeof(isrc
));
3338 bzero(&idst
, sizeof(idst
));
3339 osrc
.sin6_family
= odst
.sin6_family
= isrc
.sin6_family
=
3340 idst
.sin6_family
= AF_INET6
;
3341 osrc
.sin6_len
= odst
.sin6_len
= isrc
.sin6_len
= idst
.sin6_len
=
3342 sizeof(struct sockaddr_in6
);
3343 osrc
.sin6_addr
= oip6
->ip6_src
;
3344 odst
.sin6_addr
= oip6
->ip6_dst
;
3345 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_src
),
3346 sizeof(isrc
.sin6_addr
), (caddr_t
)&isrc
.sin6_addr
);
3347 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_dst
),
3348 sizeof(idst
.sin6_addr
), (caddr_t
)&idst
.sin6_addr
);
3351 * regarding to inner source address validation, see a long comment
3352 * in ipsec4_tunnel_validate.
3355 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3356 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3366 * Make a mbuf chain for encryption.
3367 * If the original mbuf chain contains a mbuf with a cluster,
3368 * allocate a new cluster and copy the data to the new cluster.
3369 * XXX: this hack is inefficient, but is necessary to handle cases
3370 * of TCP retransmission...
3376 struct mbuf
*n
, **mpp
, *mnew
;
3378 for (n
= m
, mpp
= &m
; n
; n
= n
->m_next
) {
3379 if (n
->m_flags
& M_EXT
) {
3381 * Make a copy only if there are more than one references
3383 * XXX: is this approach effective?
3386 n
->m_ext
.ext_free
||
3387 m_mclhasreference(n
)
3393 if (n
->m_flags
& M_PKTHDR
) {
3394 MGETHDR(mnew
, M_DONTWAIT
, MT_HEADER
);
3397 mnew
->m_pkthdr
= n
->m_pkthdr
;
3399 if (n
->m_pkthdr
.aux
) {
3400 mnew
->m_pkthdr
.aux
=
3401 m_copym(n
->m_pkthdr
.aux
,
3402 0, M_COPYALL
, M_DONTWAIT
);
3405 M_COPY_PKTHDR(mnew
, n
);
3406 mnew
->m_flags
= n
->m_flags
& M_COPYFLAGS
;
3409 MGET(mnew
, M_DONTWAIT
, MT_DATA
);
3417 * Copy data. If we don't have enough space to
3418 * store the whole data, allocate a cluster
3419 * or additional mbufs.
3420 * XXX: we don't use m_copyback(), since the
3421 * function does not use clusters and thus is
3430 if (remain
<= (mm
->m_flags
& M_PKTHDR
? MHLEN
: MLEN
))
3432 else { /* allocate a cluster */
3433 MCLGET(mm
, M_DONTWAIT
);
3434 if (!(mm
->m_flags
& M_EXT
)) {
3438 len
= remain
< MCLBYTES
?
3442 bcopy(n
->m_data
+ copied
, mm
->m_data
,
3449 if (remain
<= 0) /* completed? */
3452 /* need another mbuf */
3453 MGETHDR(mn
, M_DONTWAIT
, MT_HEADER
);
3456 mn
->m_pkthdr
.rcvif
= NULL
;
3462 mm
->m_next
= m_free(n
);
3480 static struct mbuf
*
3486 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3488 n
= m_aux_add(m
, AF_INET
, IPPROTO_ESP
);
3490 return n
; /* ENOBUFS */
3491 n
->m_len
= sizeof(struct socket
*);
3492 bzero(mtod(n
, void *), n
->m_len
);
3496 static struct mbuf
*
3502 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3504 if (n
&& n
->m_len
< sizeof(struct socket
*))
3505 panic("invalid ipsec m_aux");
3516 n
= m_aux_find(m
, AF_INET
, IPPROTO_ESP
);
3521 /* if the aux buffer is unnecessary, nuke it. */
3530 if (n
->m_len
== sizeof(struct socket
*) && !*mtod(n
, struct socket
**))
3535 ipsec_setsocket(m
, so
)
3541 /* if so == NULL, don't insist on getting the aux mbuf */
3543 n
= ipsec_addaux(m
);
3547 n
= ipsec_findaux(m
);
3548 if (n
&& n
->m_len
>= sizeof(struct socket
*))
3549 *mtod(n
, struct socket
**) = so
;
3560 n
= ipsec_findaux(m
);
3561 if (n
&& n
->m_len
>= sizeof(struct socket
*))
3562 return *mtod(n
, struct socket
**);
3568 ipsec_addhist(m
, proto
, spi
)
3574 struct ipsec_history
*p
;
3576 n
= ipsec_addaux(m
);
3579 if (M_TRAILINGSPACE(n
) < sizeof(*p
))
3580 return ENOSPC
; /*XXX*/
3581 p
= (struct ipsec_history
*)(mtod(n
, caddr_t
) + n
->m_len
);
3582 n
->m_len
+= sizeof(*p
);
3583 bzero(p
, sizeof(*p
));
3584 p
->ih_proto
= proto
;
3589 struct ipsec_history
*
3590 ipsec_gethist(m
, lenp
)
3597 n
= ipsec_findaux(m
);
3601 if (sizeof(struct socket
*) > l
)
3603 if ((l
- sizeof(struct socket
*)) % sizeof(struct ipsec_history
))
3605 /* XXX does it make more sense to divide by sizeof(ipsec_history)? */
3607 *lenp
= l
- sizeof(struct socket
*);
3608 return (struct ipsec_history
*)
3609 (mtod(n
, caddr_t
) + sizeof(struct socket
*));
3618 n
= ipsec_findaux(m
);
3619 if ((n
) && n
->m_len
> sizeof(struct socket
*))
3620 n
->m_len
= sizeof(struct socket
*);