1 /* $FreeBSD: src/sys/netinet6/ipsec.c,v 1.3.2.12 2003/05/06 06:46:58 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/ipsec.c,v 1.19 2006/12/22 23:57:53 swildner Exp $ */
3 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * IPsec controller part.
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/errno.h>
52 #include <sys/kernel.h>
53 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
56 #include <sys/in_cksum.h>
57 #include <sys/thread2.h>
60 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/in_var.h>
67 #include <netinet/udp.h>
68 #include <netinet/udp_var.h>
69 #include <netinet/ip_ecn.h>
71 #include <netinet6/ip6_ecn.h>
73 #include <netinet/tcp.h>
74 #include <netinet/udp.h>
76 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
80 #include <netinet/in_pcb.h>
82 #include <netinet/icmp6.h>
85 #include <netinet6/ipsec.h>
87 #include <netinet6/ipsec6.h>
89 #include <netinet6/ah.h>
91 #include <netinet6/ah6.h>
94 #include <netinet6/esp.h>
96 #include <netinet6/esp6.h>
99 #include <netinet6/ipcomp.h>
101 #include <netinet6/ipcomp6.h>
103 #include <netproto/key/key.h>
104 #include <netproto/key/keydb.h>
105 #include <netproto/key/key_debug.h>
107 #include <net/net_osdep.h>
115 struct ipsecstat ipsecstat
;
116 int ip4_ah_cleartos
= 1;
117 int ip4_ah_offsetmask
= 0; /* maybe IP_DF? */
118 int ip4_ipsec_dfbit
= 0; /* DF bit on encap. 0: clear 1: set 2: copy */
119 int ip4_esp_trans_deflev
= IPSEC_LEVEL_USE
;
120 int ip4_esp_net_deflev
= IPSEC_LEVEL_USE
;
121 int ip4_ah_trans_deflev
= IPSEC_LEVEL_USE
;
122 int ip4_ah_net_deflev
= IPSEC_LEVEL_USE
;
123 struct secpolicy ip4_def_policy
;
124 int ip4_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
125 int ip4_esp_randpad
= -1;
128 SYSCTL_DECL(_net_inet_ipsec
);
130 SYSCTL_DECL(_net_inet6_ipsec6
);
135 SYSCTL_STRUCT(_net_inet_ipsec
, IPSECCTL_STATS
,
136 stats
, CTLFLAG_RD
, &ipsecstat
, ipsecstat
, "");
137 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_POLICY
,
138 def_policy
, CTLFLAG_RW
, &ip4_def_policy
.policy
, 0, "");
139 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
140 CTLFLAG_RW
, &ip4_esp_trans_deflev
, 0, "");
141 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
142 CTLFLAG_RW
, &ip4_esp_net_deflev
, 0, "");
143 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
144 CTLFLAG_RW
, &ip4_ah_trans_deflev
, 0, "");
145 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
146 CTLFLAG_RW
, &ip4_ah_net_deflev
, 0, "");
147 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_CLEARTOS
,
148 ah_cleartos
, CTLFLAG_RW
, &ip4_ah_cleartos
, 0, "");
149 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_OFFSETMASK
,
150 ah_offsetmask
, CTLFLAG_RW
, &ip4_ah_offsetmask
, 0, "");
151 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DFBIT
,
152 dfbit
, CTLFLAG_RW
, &ip4_ipsec_dfbit
, 0, "");
153 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ECN
,
154 ecn
, CTLFLAG_RW
, &ip4_ipsec_ecn
, 0, "");
155 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEBUG
,
156 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
157 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ESP_RANDPAD
,
158 esp_randpad
, CTLFLAG_RW
, &ip4_esp_randpad
, 0, "");
161 struct ipsecstat ipsec6stat
;
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) */
168 int ip6_esp_randpad
= -1;
170 /* net.inet6.ipsec6 */
171 SYSCTL_STRUCT(_net_inet6_ipsec6
, IPSECCTL_STATS
,
172 stats
, CTLFLAG_RD
, &ipsec6stat
, ipsecstat
, "");
173 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_POLICY
,
174 def_policy
, CTLFLAG_RW
, &ip6_def_policy
.policy
, 0, "");
175 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
176 CTLFLAG_RW
, &ip6_esp_trans_deflev
, 0, "");
177 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
178 CTLFLAG_RW
, &ip6_esp_net_deflev
, 0, "");
179 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
180 CTLFLAG_RW
, &ip6_ah_trans_deflev
, 0, "");
181 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
182 CTLFLAG_RW
, &ip6_ah_net_deflev
, 0, "");
183 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ECN
,
184 ecn
, CTLFLAG_RW
, &ip6_ipsec_ecn
, 0, "");
185 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEBUG
,
186 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
187 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ESP_RANDPAD
,
188 esp_randpad
, CTLFLAG_RW
, &ip6_esp_randpad
, 0, "");
191 static int ipsec_setspidx_mbuf
192 (struct secpolicyindex
*, u_int
, u_int
, struct mbuf
*, int);
193 static int ipsec4_setspidx_inpcb (struct mbuf
*, struct inpcb
*pcb
);
195 static int ipsec6_setspidx_in6pcb (struct mbuf
*, struct in6pcb
*pcb
);
197 static int ipsec_setspidx (struct mbuf
*, struct secpolicyindex
*, int);
198 static void ipsec4_get_ulp (struct mbuf
*m
, struct secpolicyindex
*, int);
199 static int ipsec4_setspidx_ipaddr (struct mbuf
*, struct secpolicyindex
*);
201 static void ipsec6_get_ulp (struct mbuf
*m
, struct secpolicyindex
*, int);
202 static int ipsec6_setspidx_ipaddr (struct mbuf
*, struct secpolicyindex
*);
204 static struct inpcbpolicy
*ipsec_newpcbpolicy (void);
205 static void ipsec_delpcbpolicy (struct inpcbpolicy
*);
206 static struct secpolicy
*ipsec_deepcopy_policy (struct secpolicy
*src
);
207 static int ipsec_set_policy (struct secpolicy
**pcb_sp
,
208 int optname
, caddr_t request
, size_t len
, int priv
);
209 static int ipsec_get_policy (struct secpolicy
*pcb_sp
, struct mbuf
**mp
);
210 static void vshiftl (unsigned char *, int, int);
211 static int ipsec_in_reject (struct secpolicy
*, struct mbuf
*);
212 static size_t ipsec_hdrsiz (struct secpolicy
*);
214 static struct mbuf
*ipsec4_splithdr (struct mbuf
*);
217 static struct mbuf
*ipsec6_splithdr (struct mbuf
*);
220 static int ipsec4_encapsulate (struct mbuf
*, struct secasvar
*);
223 static int ipsec6_encapsulate (struct mbuf
*, struct secasvar
*);
227 * For OUTBOUND packet having a socket. Searching SPD for packet,
228 * and return a pointer to SP.
229 * OUT: NULL: no apropreate SP found, the following value is set to error.
231 * EACCES : discard packet.
232 * ENOENT : ipsec_acquire() in progress, maybe.
233 * others : error occured.
234 * others: a pointer to SP
236 * NOTE: IPv6 mapped adddress concern is implemented here.
239 ipsec4_getpolicybysock(struct mbuf
*m
, u_int dir
, struct socket
*so
, int *error
)
241 struct inpcbpolicy
*pcbsp
= NULL
;
242 struct secpolicy
*currsp
= NULL
; /* policy on socket */
243 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
246 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
247 panic("ipsec4_getpolicybysock: NULL pointer was passed.");
249 switch (so
->so_proto
->pr_domain
->dom_family
) {
251 /* set spidx in pcb */
252 *error
= ipsec4_setspidx_inpcb(m
, so
->so_pcb
);
256 /* set spidx in pcb */
257 *error
= ipsec6_setspidx_in6pcb(m
, so
->so_pcb
);
261 panic("ipsec4_getpolicybysock: unsupported address family\n");
265 switch (so
->so_proto
->pr_domain
->dom_family
) {
267 pcbsp
= sotoinpcb(so
)->inp_sp
;
271 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
278 panic("ipsec4_getpolicybysock: pcbsp is NULL.");
281 case IPSEC_DIR_INBOUND
:
282 currsp
= pcbsp
->sp_in
;
284 case IPSEC_DIR_OUTBOUND
:
285 currsp
= pcbsp
->sp_out
;
288 panic("ipsec4_getpolicybysock: illegal direction.");
293 panic("ipsec4_getpolicybysock: currsp is NULL.");
295 /* when privilieged socket */
297 switch (currsp
->policy
) {
298 case IPSEC_POLICY_BYPASS
:
303 case IPSEC_POLICY_ENTRUST
:
304 /* look for a policy in SPD */
305 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
308 if (kernsp
!= NULL
) {
309 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
310 kprintf("DP ipsec4_getpolicybysock called "
311 "to allocate SP:%p\n", kernsp
));
317 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
318 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
320 "fixed system default policy: %d->%d\n",
321 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
322 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
324 ip4_def_policy
.refcnt
++;
326 return &ip4_def_policy
;
328 case IPSEC_POLICY_IPSEC
:
334 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
335 "Invalid policy for PCB %d\n", currsp
->policy
));
342 /* when non-privilieged socket */
343 /* look for a policy in SPD */
344 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
347 if (kernsp
!= NULL
) {
348 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
349 kprintf("DP ipsec4_getpolicybysock called "
350 "to allocate SP:%p\n", kernsp
));
356 switch (currsp
->policy
) {
357 case IPSEC_POLICY_BYPASS
:
358 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
359 "Illegal policy for non-priviliged defined %d\n",
364 case IPSEC_POLICY_ENTRUST
:
365 if (ip4_def_policy
.policy
!= IPSEC_POLICY_DISCARD
366 && ip4_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
368 "fixed system default policy: %d->%d\n",
369 ip4_def_policy
.policy
, IPSEC_POLICY_NONE
));
370 ip4_def_policy
.policy
= IPSEC_POLICY_NONE
;
372 ip4_def_policy
.refcnt
++;
374 return &ip4_def_policy
;
376 case IPSEC_POLICY_IPSEC
:
382 ipseclog((LOG_ERR
, "ipsec4_getpolicybysock: "
383 "Invalid policy for PCB %d\n", currsp
->policy
));
391 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
392 * and return a pointer to SP.
393 * OUT: positive: a pointer to the entry for security policy leaf matched.
394 * NULL: no apropreate SP found, the following value is set to error.
396 * EACCES : discard packet.
397 * ENOENT : ipsec_acquire() in progress, maybe.
398 * others : error occured.
401 ipsec4_getpolicybyaddr(struct mbuf
*m
, u_int dir
, int flag
, int *error
)
403 struct secpolicy
*sp
= NULL
;
406 if (m
== NULL
|| error
== NULL
)
407 panic("ipsec4_getpolicybyaddr: NULL pointer was passed.");
410 struct secpolicyindex spidx
;
412 bzero(&spidx
, sizeof(spidx
));
414 /* make a index to look for a policy */
415 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET
, m
,
416 (flag
& IP_FORWARDING
) ? 0 : 1);
421 sp
= key_allocsp(&spidx
, dir
);
426 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
427 kprintf("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(struct mbuf
*m
, u_int dir
, struct socket
*so
, int *error
)
460 struct inpcbpolicy
*pcbsp
= NULL
;
461 struct secpolicy
*currsp
= NULL
; /* policy on socket */
462 struct secpolicy
*kernsp
= NULL
; /* policy on kernel */
465 if (m
== NULL
|| so
== NULL
|| error
== NULL
)
466 panic("ipsec6_getpolicybysock: NULL pointer was passed.");
469 if (so
->so_proto
->pr_domain
->dom_family
!= AF_INET6
)
470 panic("ipsec6_getpolicybysock: socket domain != inet6");
473 /* set spidx in pcb */
474 ipsec6_setspidx_in6pcb(m
, so
->so_pcb
);
476 pcbsp
= sotoin6pcb(so
)->in6p_sp
;
480 panic("ipsec6_getpolicybysock: pcbsp is NULL.");
483 case IPSEC_DIR_INBOUND
:
484 currsp
= pcbsp
->sp_in
;
486 case IPSEC_DIR_OUTBOUND
:
487 currsp
= pcbsp
->sp_out
;
490 panic("ipsec6_getpolicybysock: illegal direction.");
495 panic("ipsec6_getpolicybysock: currsp is NULL.");
497 /* when privilieged socket */
499 switch (currsp
->policy
) {
500 case IPSEC_POLICY_BYPASS
:
505 case IPSEC_POLICY_ENTRUST
:
506 /* look for a policy in SPD */
507 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
510 if (kernsp
!= NULL
) {
511 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
512 kprintf("DP ipsec6_getpolicybysock called "
513 "to allocate SP:%p\n", kernsp
));
519 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
520 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
522 "fixed system default policy: %d->%d\n",
523 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
524 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
526 ip6_def_policy
.refcnt
++;
528 return &ip6_def_policy
;
530 case IPSEC_POLICY_IPSEC
:
536 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
537 "Invalid policy for PCB %d\n", currsp
->policy
));
544 /* when non-privilieged socket */
545 /* look for a policy in SPD */
546 kernsp
= key_allocsp(&currsp
->spidx
, dir
);
549 if (kernsp
!= NULL
) {
550 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
551 kprintf("DP ipsec6_getpolicybysock called "
552 "to allocate SP:%p\n", kernsp
));
558 switch (currsp
->policy
) {
559 case IPSEC_POLICY_BYPASS
:
560 ipseclog((LOG_ERR
, "ipsec6_getpolicybysock: "
561 "Illegal policy for non-priviliged defined %d\n",
566 case IPSEC_POLICY_ENTRUST
:
567 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
568 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
570 "fixed system default policy: %d->%d\n",
571 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
572 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
574 ip6_def_policy
.refcnt
++;
576 return &ip6_def_policy
;
578 case IPSEC_POLICY_IPSEC
:
585 "ipsec6_policybysock: Invalid policy for PCB %d\n",
594 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
595 * and return a pointer to SP.
596 * `flag' means that packet is to be forwarded whether or not.
598 * OUT: positive: a pointer to the entry for security policy leaf matched.
599 * NULL: no apropreate SP found, the following value is set to error.
601 * EACCES : discard packet.
602 * ENOENT : ipsec_acquire() in progress, maybe.
603 * others : error occured.
605 #ifndef IP_FORWARDING
606 #define IP_FORWARDING 1
610 ipsec6_getpolicybyaddr(struct mbuf
*m
, u_int dir
, int flag
, int *error
)
612 struct secpolicy
*sp
= NULL
;
615 if (m
== NULL
|| error
== NULL
)
616 panic("ipsec6_getpolicybyaddr: NULL pointer was passed.");
619 struct secpolicyindex spidx
;
621 bzero(&spidx
, sizeof(spidx
));
623 /* make a index to look for a policy */
624 *error
= ipsec_setspidx_mbuf(&spidx
, dir
, AF_INET6
, m
,
625 (flag
& IP_FORWARDING
) ? 0 : 1);
630 sp
= key_allocsp(&spidx
, dir
);
635 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
636 kprintf("DP ipsec6_getpolicybyaddr called "
637 "to allocate SP:%p\n", sp
));
643 if (ip6_def_policy
.policy
!= IPSEC_POLICY_DISCARD
644 && ip6_def_policy
.policy
!= IPSEC_POLICY_NONE
) {
645 ipseclog((LOG_INFO
, "fixed system default policy: %d->%d\n",
646 ip6_def_policy
.policy
, IPSEC_POLICY_NONE
));
647 ip6_def_policy
.policy
= IPSEC_POLICY_NONE
;
649 ip6_def_policy
.refcnt
++;
651 return &ip6_def_policy
;
656 * set IP address into spidx from mbuf.
657 * When Forwarding packet and ICMP echo reply, this function is used.
659 * IN: get the followings from mbuf.
660 * protocol family, src, dst, next protocol
663 * other: failure, and set errno.
666 ipsec_setspidx_mbuf(struct secpolicyindex
*spidx
, u_int dir
, u_int family
,
667 struct mbuf
*m
, int needport
)
672 if (spidx
== NULL
|| m
== NULL
)
673 panic("ipsec_setspidx_mbuf: NULL pointer was passed.");
675 bzero(spidx
, sizeof(*spidx
));
677 error
= ipsec_setspidx(m
, spidx
, needport
);
686 bzero(spidx
, sizeof(*spidx
));
691 ipsec4_setspidx_inpcb(struct mbuf
*m
, struct inpcb
*pcb
)
693 struct secpolicyindex
*spidx
;
698 panic("ipsec4_setspidx_inpcb: no PCB found.");
699 if (pcb
->inp_sp
== NULL
)
700 panic("ipsec4_setspidx_inpcb: no inp_sp found.");
701 if (pcb
->inp_sp
->sp_out
== NULL
|| pcb
->inp_sp
->sp_in
== NULL
)
702 panic("ipsec4_setspidx_inpcb: no sp_in/out found.");
704 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
705 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
707 spidx
= &pcb
->inp_sp
->sp_in
->spidx
;
708 error
= ipsec_setspidx(m
, spidx
, 1);
711 spidx
->dir
= IPSEC_DIR_INBOUND
;
713 spidx
= &pcb
->inp_sp
->sp_out
->spidx
;
714 error
= ipsec_setspidx(m
, spidx
, 1);
717 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
722 bzero(&pcb
->inp_sp
->sp_in
->spidx
, sizeof(*spidx
));
723 bzero(&pcb
->inp_sp
->sp_out
->spidx
, sizeof(*spidx
));
729 ipsec6_setspidx_in6pcb(struct mbuf
*m
, struct in6pcb
*pcb
)
731 struct secpolicyindex
*spidx
;
736 panic("ipsec6_setspidx_in6pcb: no PCB found.");
737 if (pcb
->in6p_sp
== NULL
)
738 panic("ipsec6_setspidx_in6pcb: no in6p_sp found.");
739 if (pcb
->in6p_sp
->sp_out
== NULL
|| pcb
->in6p_sp
->sp_in
== NULL
)
740 panic("ipsec6_setspidx_in6pcb: no sp_in/out found.");
742 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
743 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
745 spidx
= &pcb
->in6p_sp
->sp_in
->spidx
;
746 error
= ipsec_setspidx(m
, spidx
, 1);
749 spidx
->dir
= IPSEC_DIR_INBOUND
;
751 spidx
= &pcb
->in6p_sp
->sp_out
->spidx
;
752 error
= ipsec_setspidx(m
, spidx
, 1);
755 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
760 bzero(&pcb
->in6p_sp
->sp_in
->spidx
, sizeof(*spidx
));
761 bzero(&pcb
->in6p_sp
->sp_out
->spidx
, sizeof(*spidx
));
767 * configure security policy index (src/dst/proto/sport/dport)
768 * by looking at the content of mbuf.
769 * the caller is responsible for error recovery (like clearing up spidx).
772 ipsec_setspidx(struct mbuf
*m
, struct secpolicyindex
*spidx
, int needport
)
774 struct ip
*ip
= NULL
;
782 panic("ipsec_setspidx: m == 0 passed.");
785 * validate m->m_pkthdr.len. we see incorrect length if we
786 * mistakenly call this function with inconsistent mbuf chain
787 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
790 for (n
= m
; n
; n
= n
->m_next
)
792 if (m
->m_pkthdr
.len
!= len
) {
793 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
794 kprintf("ipsec_setspidx: "
795 "total of m_len(%d) != pkthdr.len(%d), "
797 len
, m
->m_pkthdr
.len
));
801 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
802 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
803 kprintf("ipsec_setspidx: "
804 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
809 if (m
->m_len
>= sizeof(*ip
))
810 ip
= mtod(m
, struct ip
*);
812 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
816 v
= _IP_VHL_V(ip
->ip_vhl
);
822 error
= ipsec4_setspidx_ipaddr(m
, spidx
);
825 ipsec4_get_ulp(m
, spidx
, needport
);
829 if (m
->m_pkthdr
.len
< sizeof(struct ip6_hdr
)) {
830 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
831 kprintf("ipsec_setspidx: "
832 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
833 "ignored.\n", m
->m_pkthdr
.len
));
836 error
= ipsec6_setspidx_ipaddr(m
, spidx
);
839 ipsec6_get_ulp(m
, spidx
, needport
);
843 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
844 kprintf("ipsec_setspidx: "
845 "unknown IP version %u, ignored.\n", v
));
851 ipsec4_get_ulp(struct mbuf
*m
, struct secpolicyindex
*spidx
, int needport
)
862 panic("ipsec4_get_ulp: NULL pointer was passed.");
863 if (m
->m_pkthdr
.len
< sizeof(ip
))
864 panic("ipsec4_get_ulp: too short");
867 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
868 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
= IPSEC_PORT_ANY
;
869 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
= IPSEC_PORT_ANY
;
871 m_copydata(m
, 0, sizeof(ip
), (caddr_t
)&ip
);
872 /* ip_input() flips it into host endian XXX need more checking */
873 if (ip
.ip_off
& (IP_MF
| IP_OFFMASK
))
878 off
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
882 while (off
< m
->m_pkthdr
.len
) {
885 spidx
->ul_proto
= nxt
;
888 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
890 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
891 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
893 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
897 spidx
->ul_proto
= nxt
;
900 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
902 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
903 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
905 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
909 if (off
+ sizeof(ip6e
) > m
->m_pkthdr
.len
)
911 m_copydata(m
, off
, sizeof(ip6e
), (caddr_t
)&ip6e
);
912 off
+= (ip6e
.ip6e_len
+ 2) << 2;
917 /* XXX intermediate headers??? */
918 spidx
->ul_proto
= nxt
;
924 /* assumes that m is sane */
926 ipsec4_setspidx_ipaddr(struct mbuf
*m
, struct secpolicyindex
*spidx
)
928 struct ip
*ip
= NULL
;
930 struct sockaddr_in
*sin
;
932 if (m
->m_len
>= sizeof(*ip
))
933 ip
= mtod(m
, struct ip
*);
935 m_copydata(m
, 0, sizeof(ipbuf
), (caddr_t
)&ipbuf
);
939 sin
= (struct sockaddr_in
*)&spidx
->src
;
940 bzero(sin
, sizeof(*sin
));
941 sin
->sin_family
= AF_INET
;
942 sin
->sin_len
= sizeof(struct sockaddr_in
);
943 bcopy(&ip
->ip_src
, &sin
->sin_addr
, sizeof(ip
->ip_src
));
944 spidx
->prefs
= sizeof(struct in_addr
) << 3;
946 sin
= (struct sockaddr_in
*)&spidx
->dst
;
947 bzero(sin
, sizeof(*sin
));
948 sin
->sin_family
= AF_INET
;
949 sin
->sin_len
= sizeof(struct sockaddr_in
);
950 bcopy(&ip
->ip_dst
, &sin
->sin_addr
, sizeof(ip
->ip_dst
));
951 spidx
->prefd
= sizeof(struct in_addr
) << 3;
957 ipsec6_get_ulp(struct mbuf
*m
, struct secpolicyindex
*spidx
, int needport
)
965 panic("ipsec6_get_ulp: NULL pointer was passed.");
967 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
968 kprintf("ipsec6_get_ulp:\n"); kdebug_mbuf(m
));
971 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
972 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= IPSEC_PORT_ANY
;
973 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= IPSEC_PORT_ANY
;
976 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
977 if (off
< 0 || m
->m_pkthdr
.len
< off
)
982 spidx
->ul_proto
= nxt
;
985 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
987 m_copydata(m
, off
, sizeof(th
), (caddr_t
)&th
);
988 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= th
.th_sport
;
989 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= th
.th_dport
;
992 spidx
->ul_proto
= nxt
;
995 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
997 m_copydata(m
, off
, sizeof(uh
), (caddr_t
)&uh
);
998 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= uh
.uh_sport
;
999 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= uh
.uh_dport
;
1001 case IPPROTO_ICMPV6
:
1003 /* XXX intermediate headers??? */
1004 spidx
->ul_proto
= nxt
;
1009 /* assumes that m is sane */
1011 ipsec6_setspidx_ipaddr(struct mbuf
*m
, struct secpolicyindex
*spidx
)
1013 struct ip6_hdr
*ip6
= NULL
;
1014 struct ip6_hdr ip6buf
;
1015 struct sockaddr_in6
*sin6
;
1017 if (m
->m_len
>= sizeof(*ip6
))
1018 ip6
= mtod(m
, struct ip6_hdr
*);
1020 m_copydata(m
, 0, sizeof(ip6buf
), (caddr_t
)&ip6buf
);
1024 sin6
= (struct sockaddr_in6
*)&spidx
->src
;
1025 bzero(sin6
, sizeof(*sin6
));
1026 sin6
->sin6_family
= AF_INET6
;
1027 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1028 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_src
));
1029 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1030 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1031 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
1033 spidx
->prefs
= sizeof(struct in6_addr
) << 3;
1035 sin6
= (struct sockaddr_in6
*)&spidx
->dst
;
1036 bzero(sin6
, sizeof(*sin6
));
1037 sin6
->sin6_family
= AF_INET6
;
1038 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1039 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
, sizeof(ip6
->ip6_dst
));
1040 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
1041 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1042 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
1044 spidx
->prefd
= sizeof(struct in6_addr
) << 3;
1050 static struct inpcbpolicy
*
1051 ipsec_newpcbpolicy(void)
1053 struct inpcbpolicy
*p
;
1055 p
= (struct inpcbpolicy
*)kmalloc(sizeof(*p
), M_SECA
, M_NOWAIT
);
1060 ipsec_delpcbpolicy(struct inpcbpolicy
*p
)
1065 /* initialize policy in PCB */
1067 ipsec_init_policy(struct socket
*so
, struct inpcbpolicy
**pcb_sp
)
1069 struct inpcbpolicy
*new;
1072 if (so
== NULL
|| pcb_sp
== NULL
)
1073 panic("ipsec_init_policy: NULL pointer was passed.");
1075 new = ipsec_newpcbpolicy();
1077 ipseclog((LOG_DEBUG
, "ipsec_init_policy: No more memory.\n"));
1080 bzero(new, sizeof(*new));
1082 if (so
->so_cred
!= 0 && so
->so_cred
->cr_uid
== 0)
1087 if ((new->sp_in
= key_newsp()) == NULL
) {
1088 ipsec_delpcbpolicy(new);
1091 new->sp_in
->state
= IPSEC_SPSTATE_ALIVE
;
1092 new->sp_in
->policy
= IPSEC_POLICY_ENTRUST
;
1094 if ((new->sp_out
= key_newsp()) == NULL
) {
1095 key_freesp(new->sp_in
);
1096 ipsec_delpcbpolicy(new);
1099 new->sp_out
->state
= IPSEC_SPSTATE_ALIVE
;
1100 new->sp_out
->policy
= IPSEC_POLICY_ENTRUST
;
1107 /* copy old ipsec policy into new */
1109 ipsec_copy_policy(struct inpcbpolicy
*old
, struct inpcbpolicy
*new)
1111 struct secpolicy
*sp
;
1113 sp
= ipsec_deepcopy_policy(old
->sp_in
);
1115 key_freesp(new->sp_in
);
1120 sp
= ipsec_deepcopy_policy(old
->sp_out
);
1122 key_freesp(new->sp_out
);
1127 new->priv
= old
->priv
;
1132 /* deep-copy a policy in PCB */
1133 static struct secpolicy
*
1134 ipsec_deepcopy_policy(struct secpolicy
*src
)
1136 struct ipsecrequest
*newchain
= NULL
;
1137 struct ipsecrequest
*p
;
1138 struct ipsecrequest
**q
;
1139 struct ipsecrequest
*r
;
1140 struct secpolicy
*dst
;
1143 if (src
== NULL
|| dst
== NULL
)
1147 * deep-copy IPsec request chain. This is required since struct
1148 * ipsecrequest is not reference counted.
1151 for (p
= src
->req
; p
; p
= p
->next
) {
1152 *q
= (struct ipsecrequest
*)kmalloc(sizeof(struct ipsecrequest
),
1156 bzero(*q
, sizeof(**q
));
1159 (*q
)->saidx
.proto
= p
->saidx
.proto
;
1160 (*q
)->saidx
.mode
= p
->saidx
.mode
;
1161 (*q
)->level
= p
->level
;
1162 (*q
)->saidx
.reqid
= p
->saidx
.reqid
;
1164 bcopy(&p
->saidx
.src
, &(*q
)->saidx
.src
, sizeof((*q
)->saidx
.src
));
1165 bcopy(&p
->saidx
.dst
, &(*q
)->saidx
.dst
, sizeof((*q
)->saidx
.dst
));
1173 dst
->req
= newchain
;
1174 dst
->state
= src
->state
;
1175 dst
->policy
= src
->policy
;
1176 /* do not touch the refcnt fields */
1181 for (p
= newchain
; p
; p
= r
) {
1189 /* set policy and ipsec request if present. */
1191 ipsec_set_policy(struct secpolicy
**pcb_sp
, int optname
, caddr_t request
,
1192 size_t len
, int priv
)
1194 struct sadb_x_policy
*xpl
;
1195 struct secpolicy
*newsp
= NULL
;
1199 if (pcb_sp
== NULL
|| *pcb_sp
== NULL
|| request
== NULL
)
1201 if (len
< sizeof(*xpl
))
1203 xpl
= (struct sadb_x_policy
*)request
;
1205 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1206 kprintf("ipsec_set_policy: passed policy\n");
1207 kdebug_sadb_x_policy((struct sadb_ext
*)xpl
));
1209 /* check policy type */
1210 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1211 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
1212 || xpl
->sadb_x_policy_type
== IPSEC_POLICY_NONE
)
1215 /* check privileged socket */
1216 if (priv
== 0 && xpl
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
)
1219 /* allocation new SP entry */
1220 if ((newsp
= key_msg2sp(xpl
, len
, &error
)) == NULL
)
1223 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1225 /* clear old SP and set new SP */
1226 key_freesp(*pcb_sp
);
1228 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1229 kprintf("ipsec_set_policy: new policy\n");
1230 kdebug_secpolicy(newsp
));
1236 ipsec_get_policy(struct secpolicy
*pcb_sp
, struct mbuf
**mp
)
1240 if (pcb_sp
== NULL
|| mp
== NULL
)
1243 *mp
= key_sp2msg(pcb_sp
);
1245 ipseclog((LOG_DEBUG
, "ipsec_get_policy: No more memory.\n"));
1249 KKASSERT((*mp
)->m_type
== MT_DATA
);
1250 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1251 kprintf("ipsec_get_policy:\n");
1258 ipsec4_set_policy(struct inpcb
*inp
, int optname
, caddr_t request
, size_t len
,
1261 struct sadb_x_policy
*xpl
;
1262 struct secpolicy
**pcb_sp
;
1265 if (inp
== NULL
|| request
== NULL
)
1267 if (len
< sizeof(*xpl
))
1269 xpl
= (struct sadb_x_policy
*)request
;
1271 /* select direction */
1272 switch (xpl
->sadb_x_policy_dir
) {
1273 case IPSEC_DIR_INBOUND
:
1274 pcb_sp
= &inp
->inp_sp
->sp_in
;
1276 case IPSEC_DIR_OUTBOUND
:
1277 pcb_sp
= &inp
->inp_sp
->sp_out
;
1280 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1281 xpl
->sadb_x_policy_dir
));
1285 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1289 ipsec4_get_policy(struct inpcb
*inp
, caddr_t request
, size_t len
,
1292 struct sadb_x_policy
*xpl
;
1293 struct secpolicy
*pcb_sp
;
1296 if (inp
== NULL
|| request
== NULL
|| mp
== NULL
)
1298 if (inp
->inp_sp
== NULL
)
1299 panic("policy in PCB is NULL");
1300 if (len
< sizeof(*xpl
))
1302 xpl
= (struct sadb_x_policy
*)request
;
1304 /* select direction */
1305 switch (xpl
->sadb_x_policy_dir
) {
1306 case IPSEC_DIR_INBOUND
:
1307 pcb_sp
= inp
->inp_sp
->sp_in
;
1309 case IPSEC_DIR_OUTBOUND
:
1310 pcb_sp
= inp
->inp_sp
->sp_out
;
1313 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1314 xpl
->sadb_x_policy_dir
));
1318 return ipsec_get_policy(pcb_sp
, mp
);
1321 /* delete policy in PCB */
1323 ipsec4_delete_pcbpolicy(struct inpcb
*inp
)
1327 panic("ipsec4_delete_pcbpolicy: NULL pointer was passed.");
1329 if (inp
->inp_sp
== NULL
)
1332 if (inp
->inp_sp
->sp_in
!= NULL
) {
1333 key_freesp(inp
->inp_sp
->sp_in
);
1334 inp
->inp_sp
->sp_in
= NULL
;
1337 if (inp
->inp_sp
->sp_out
!= NULL
) {
1338 key_freesp(inp
->inp_sp
->sp_out
);
1339 inp
->inp_sp
->sp_out
= NULL
;
1342 ipsec_delpcbpolicy(inp
->inp_sp
);
1350 ipsec6_set_policy(struct in6pcb
*in6p
, int optname
, caddr_t request
, size_t len
,
1353 struct sadb_x_policy
*xpl
;
1354 struct secpolicy
**pcb_sp
;
1357 if (in6p
== NULL
|| request
== NULL
)
1359 if (len
< sizeof(*xpl
))
1361 xpl
= (struct sadb_x_policy
*)request
;
1363 /* select direction */
1364 switch (xpl
->sadb_x_policy_dir
) {
1365 case IPSEC_DIR_INBOUND
:
1366 pcb_sp
= &in6p
->in6p_sp
->sp_in
;
1368 case IPSEC_DIR_OUTBOUND
:
1369 pcb_sp
= &in6p
->in6p_sp
->sp_out
;
1372 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1373 xpl
->sadb_x_policy_dir
));
1377 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, priv
);
1381 ipsec6_get_policy(struct in6pcb
*in6p
, caddr_t request
, size_t len
,
1384 struct sadb_x_policy
*xpl
;
1385 struct secpolicy
*pcb_sp
;
1388 if (in6p
== NULL
|| request
== NULL
|| mp
== NULL
)
1390 if (in6p
->in6p_sp
== NULL
)
1391 panic("policy in PCB is NULL");
1392 if (len
< sizeof(*xpl
))
1394 xpl
= (struct sadb_x_policy
*)request
;
1396 /* select direction */
1397 switch (xpl
->sadb_x_policy_dir
) {
1398 case IPSEC_DIR_INBOUND
:
1399 pcb_sp
= in6p
->in6p_sp
->sp_in
;
1401 case IPSEC_DIR_OUTBOUND
:
1402 pcb_sp
= in6p
->in6p_sp
->sp_out
;
1405 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1406 xpl
->sadb_x_policy_dir
));
1410 return ipsec_get_policy(pcb_sp
, mp
);
1414 ipsec6_delete_pcbpolicy(struct in6pcb
*in6p
)
1418 panic("ipsec6_delete_pcbpolicy: NULL pointer was passed.");
1420 if (in6p
->in6p_sp
== NULL
)
1423 if (in6p
->in6p_sp
->sp_in
!= NULL
) {
1424 key_freesp(in6p
->in6p_sp
->sp_in
);
1425 in6p
->in6p_sp
->sp_in
= NULL
;
1428 if (in6p
->in6p_sp
->sp_out
!= NULL
) {
1429 key_freesp(in6p
->in6p_sp
->sp_out
);
1430 in6p
->in6p_sp
->sp_out
= NULL
;
1433 ipsec_delpcbpolicy(in6p
->in6p_sp
);
1434 in6p
->in6p_sp
= NULL
;
1441 * return current level.
1442 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1445 ipsec_get_reqlevel(struct ipsecrequest
*isr
)
1448 u_int esp_trans_deflev
, esp_net_deflev
, ah_trans_deflev
, ah_net_deflev
;
1451 if (isr
== NULL
|| isr
->sp
== NULL
)
1452 panic("ipsec_get_reqlevel: NULL pointer is passed.");
1453 if (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
1454 != ((struct sockaddr
*)&isr
->sp
->spidx
.dst
)->sa_family
)
1455 panic("ipsec_get_reqlevel: family mismatched.");
1457 /* XXX note that we have ipseclog() expanded here - code sync issue */
1458 #define IPSEC_CHECK_DEFAULT(lev) \
1459 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1460 && (lev) != IPSEC_LEVEL_UNIQUE) \
1462 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1463 (lev), IPSEC_LEVEL_REQUIRE) \
1465 (lev) = IPSEC_LEVEL_REQUIRE, \
1469 /* set default level */
1470 switch (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
) {
1473 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev
);
1474 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev
);
1475 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev
);
1476 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev
);
1481 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev
);
1482 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev
);
1483 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev
);
1484 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev
);
1488 panic("key_get_reqlevel: Unknown family. %d",
1489 ((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
);
1492 #undef IPSEC_CHECK_DEFAULT
1495 switch (isr
->level
) {
1496 case IPSEC_LEVEL_DEFAULT
:
1497 switch (isr
->saidx
.proto
) {
1499 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1500 level
= esp_net_deflev
;
1502 level
= esp_trans_deflev
;
1505 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1506 level
= ah_net_deflev
;
1508 level
= ah_trans_deflev
;
1509 case IPPROTO_IPCOMP
:
1511 * we don't really care, as IPcomp document says that
1512 * we shouldn't compress small packets
1514 level
= IPSEC_LEVEL_USE
;
1517 panic("ipsec_get_reqlevel: "
1518 "Illegal protocol defined %u",
1523 case IPSEC_LEVEL_USE
:
1524 case IPSEC_LEVEL_REQUIRE
:
1527 case IPSEC_LEVEL_UNIQUE
:
1528 level
= IPSEC_LEVEL_REQUIRE
;
1532 panic("ipsec_get_reqlevel: Illegal IPsec level %u",
1540 * Check AH/ESP integrity.
1546 ipsec_in_reject(struct secpolicy
*sp
, struct mbuf
*m
)
1548 struct ipsecrequest
*isr
;
1550 int need_auth
, need_conf
, need_icv
;
1552 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1553 kprintf("ipsec_in_reject: using SP\n");
1554 kdebug_secpolicy(sp
));
1557 switch (sp
->policy
) {
1558 case IPSEC_POLICY_DISCARD
:
1560 case IPSEC_POLICY_BYPASS
:
1561 case IPSEC_POLICY_NONE
:
1564 case IPSEC_POLICY_IPSEC
:
1567 case IPSEC_POLICY_ENTRUST
:
1569 panic("ipsec_hdrsiz: Invalid policy found. %d", sp
->policy
);
1576 /* XXX should compare policy against ipsec header history */
1578 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1580 /* get current level */
1581 level
= ipsec_get_reqlevel(isr
);
1583 switch (isr
->saidx
.proto
) {
1585 if (level
== IPSEC_LEVEL_REQUIRE
) {
1588 if (isr
->sav
!= NULL
1589 && isr
->sav
->flags
== SADB_X_EXT_NONE
1590 && isr
->sav
->alg_auth
!= SADB_AALG_NONE
)
1595 if (level
== IPSEC_LEVEL_REQUIRE
) {
1600 case IPPROTO_IPCOMP
:
1602 * we don't really care, as IPcomp document says that
1603 * we shouldn't compress small packets, IPComp policy
1604 * should always be treated as being in "use" level.
1610 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1611 kprintf("ipsec_in_reject: auth:%d conf:%d icv:%d m_flags:%x\n",
1612 need_auth
, need_conf
, need_icv
, m
->m_flags
));
1614 if ((need_conf
&& !(m
->m_flags
& M_DECRYPTED
))
1615 || (!need_auth
&& need_icv
&& !(m
->m_flags
& M_AUTHIPDGM
))
1616 || (need_auth
&& !(m
->m_flags
& M_AUTHIPHDR
)))
1623 * Check AH/ESP integrity.
1624 * This function is called from tcp_input(), udp_input(),
1625 * and {ah,esp}4_input for tunnel mode
1628 ipsec4_in_reject_so(struct mbuf
*m
, struct socket
*so
)
1630 struct secpolicy
*sp
= NULL
;
1636 return 0; /* XXX should be panic ? */
1638 /* get SP for this packet.
1639 * When we are called from ip_forward(), we call
1640 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1643 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1645 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1648 return 0; /* XXX should be panic ?
1649 * -> No, there may be error. */
1651 result
= ipsec_in_reject(sp
, m
);
1652 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1653 kprintf("DP ipsec4_in_reject_so call free SP:%p\n", sp
));
1660 ipsec4_in_reject(struct mbuf
*m
, struct inpcb
*inp
)
1663 return ipsec4_in_reject_so(m
, NULL
);
1664 if (inp
->inp_socket
)
1665 return ipsec4_in_reject_so(m
, inp
->inp_socket
);
1667 panic("ipsec4_in_reject: invalid inpcb/socket");
1672 * Check AH/ESP integrity.
1673 * This function is called from tcp6_input(), udp6_input(),
1674 * and {ah,esp}6_input for tunnel mode
1677 ipsec6_in_reject_so(struct mbuf
*m
, struct socket
*so
)
1679 struct secpolicy
*sp
= NULL
;
1685 return 0; /* XXX should be panic ? */
1687 /* get SP for this packet.
1688 * When we are called from ip_forward(), we call
1689 * ipsec6_getpolicybyaddr() with IP_FORWARDING flag.
1692 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1694 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_INBOUND
, so
, &error
);
1697 return 0; /* XXX should be panic ? */
1699 result
= ipsec_in_reject(sp
, m
);
1700 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1701 kprintf("DP ipsec6_in_reject_so call free SP:%p\n", sp
));
1708 ipsec6_in_reject(struct mbuf
*m
, struct in6pcb
*in6p
)
1711 return ipsec6_in_reject_so(m
, NULL
);
1712 if (in6p
->in6p_socket
)
1713 return ipsec6_in_reject_so(m
, in6p
->in6p_socket
);
1715 panic("ipsec6_in_reject: invalid in6p/socket");
1720 * compute the byte size to be occupied by IPsec header.
1721 * in case it is tunneled, it includes the size of outer IP header.
1722 * NOTE: SP passed is free in this function.
1725 ipsec_hdrsiz(struct secpolicy
*sp
)
1727 struct ipsecrequest
*isr
;
1730 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1731 kprintf("ipsec_hdrsiz: using SP\n");
1732 kdebug_secpolicy(sp
));
1735 switch (sp
->policy
) {
1736 case IPSEC_POLICY_DISCARD
:
1737 case IPSEC_POLICY_BYPASS
:
1738 case IPSEC_POLICY_NONE
:
1741 case IPSEC_POLICY_IPSEC
:
1744 case IPSEC_POLICY_ENTRUST
:
1746 panic("ipsec_hdrsiz: Invalid policy found. %d", sp
->policy
);
1751 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1755 switch (isr
->saidx
.proto
) {
1758 clen
= esp_hdrsiz(isr
);
1764 clen
= ah_hdrsiz(isr
);
1766 case IPPROTO_IPCOMP
:
1767 clen
= sizeof(struct ipcomp
);
1771 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
1772 switch (((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
) {
1774 clen
+= sizeof(struct ip
);
1778 clen
+= sizeof(struct ip6_hdr
);
1782 ipseclog((LOG_ERR
, "ipsec_hdrsiz: "
1783 "unknown AF %d in IPsec tunnel SA\n",
1784 ((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
));
1794 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1796 ipsec4_hdrsiz(struct mbuf
*m
, u_int dir
, struct inpcb
*inp
)
1798 struct secpolicy
*sp
= NULL
;
1804 return 0; /* XXX should be panic ? */
1805 if (inp
!= NULL
&& inp
->inp_socket
== NULL
)
1806 panic("ipsec4_hdrsize: why is socket NULL but there is PCB.");
1808 /* get SP for this packet.
1809 * When we are called from ip_forward(), we call
1810 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1813 sp
= ipsec4_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
1815 sp
= ipsec4_getpolicybysock(m
, dir
, inp
->inp_socket
, &error
);
1818 return 0; /* XXX should be panic ? */
1820 size
= ipsec_hdrsiz(sp
);
1821 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1822 kprintf("DP ipsec4_hdrsiz call free SP:%p\n", sp
));
1823 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1824 kprintf("ipsec4_hdrsiz: size:%lu.\n", (unsigned long)size
));
1831 /* This function is called from ipsec6_hdrsize_tcp(),
1832 * and maybe from ip6_forward.()
1835 ipsec6_hdrsiz(struct mbuf
*m
, u_int dir
, struct in6pcb
*in6p
)
1837 struct secpolicy
*sp
= NULL
;
1843 return 0; /* XXX shoud be panic ? */
1844 if (in6p
!= NULL
&& in6p
->in6p_socket
== NULL
)
1845 panic("ipsec6_hdrsize: why is socket NULL but there is PCB.");
1847 /* get SP for this packet */
1848 /* XXX Is it right to call with IP_FORWARDING. */
1850 sp
= ipsec6_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
1852 sp
= ipsec6_getpolicybysock(m
, dir
, in6p
->in6p_socket
, &error
);
1856 size
= ipsec_hdrsiz(sp
);
1857 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1858 kprintf("DP ipsec6_hdrsiz call free SP:%p\n", sp
));
1859 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1860 kprintf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size
));
1869 * encapsulate for ipsec tunnel.
1870 * ip->ip_src must be fixed later on.
1873 ipsec4_encapsulate(struct mbuf
*m
, struct secasvar
*sav
)
1880 /* can't tunnel between different AFs */
1881 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
1882 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
1883 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
1888 /* XXX if the dst is myself, perform nothing. */
1889 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
1895 if (m
->m_len
< sizeof(*ip
))
1896 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
1898 ip
= mtod(m
, struct ip
*);
1900 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
1902 hlen
= ip
->ip_hl
<< 2;
1905 if (m
->m_len
!= hlen
)
1906 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
1908 /* generate header checksum */
1911 if (ip
->ip_vhl
== IP_VHL_BORING
)
1912 ip
->ip_sum
= in_cksum_hdr(ip
);
1914 ip
->ip_sum
= in_cksum(m
, hlen
);
1916 ip
->ip_sum
= in_cksum(m
, hlen
);
1919 plen
= m
->m_pkthdr
.len
;
1922 * grow the mbuf to accomodate the new IPv4 header.
1923 * NOTE: IPv4 options will never be copied.
1925 if (M_LEADINGSPACE(m
->m_next
) < hlen
) {
1927 MGET(n
, MB_DONTWAIT
, MT_DATA
);
1933 n
->m_next
= m
->m_next
;
1935 m
->m_pkthdr
.len
+= hlen
;
1936 oip
= mtod(n
, struct ip
*);
1938 m
->m_next
->m_len
+= hlen
;
1939 m
->m_next
->m_data
-= hlen
;
1940 m
->m_pkthdr
.len
+= hlen
;
1941 oip
= mtod(m
->m_next
, struct ip
*);
1943 ip
= mtod(m
, struct ip
*);
1944 ovbcopy((caddr_t
)ip
, (caddr_t
)oip
, hlen
);
1945 m
->m_len
= sizeof(struct ip
);
1946 m
->m_pkthdr
.len
-= (hlen
- sizeof(struct ip
));
1948 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
1949 /* ECN consideration. */
1950 ip_ecn_ingress(ip4_ipsec_ecn
, &ip
->ip_tos
, &oip
->ip_tos
);
1952 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
1954 ip
->ip_hl
= sizeof(struct ip
) >> 2;
1956 ip
->ip_off
&= htons(~IP_OFFMASK
);
1957 ip
->ip_off
&= htons(~IP_MF
);
1958 switch (ip4_ipsec_dfbit
) {
1959 case 0: /* clear DF bit */
1960 ip
->ip_off
&= htons(~IP_DF
);
1962 case 1: /* set DF bit */
1963 ip
->ip_off
|= htons(IP_DF
);
1965 default: /* copy DF bit */
1968 ip
->ip_p
= IPPROTO_IPIP
;
1969 if (plen
+ sizeof(struct ip
) < IP_MAXPACKET
)
1970 ip
->ip_len
= htons(plen
+ sizeof(struct ip
));
1972 ipseclog((LOG_ERR
, "IPv4 ipsec: size exceeds limit: "
1973 "leave ip_len as is (invalid packet)\n"));
1976 ip
->ip_id
= ip_randomid();
1978 ip
->ip_id
= htons(ip_id
++);
1980 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.src
)->sin_addr
,
1981 &ip
->ip_src
, sizeof(ip
->ip_src
));
1982 bcopy(&((struct sockaddr_in
*)&sav
->sah
->saidx
.dst
)->sin_addr
,
1983 &ip
->ip_dst
, sizeof(ip
->ip_dst
));
1984 ip
->ip_ttl
= IPDEFTTL
;
1986 /* XXX Should ip_src be updated later ? */
1994 ipsec6_encapsulate(struct mbuf
*m
, struct secasvar
*sav
)
1996 struct ip6_hdr
*oip6
;
1997 struct ip6_hdr
*ip6
;
2000 /* can't tunnel between different AFs */
2001 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2002 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
2003 || ((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
2008 /* XXX if the dst is myself, perform nothing. */
2009 if (key_ismyaddr((struct sockaddr
*)&sav
->sah
->saidx
.dst
)) {
2015 plen
= m
->m_pkthdr
.len
;
2018 * grow the mbuf to accomodate the new IPv6 header.
2020 if (m
->m_len
!= sizeof(struct ip6_hdr
))
2021 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2022 if (M_LEADINGSPACE(m
->m_next
) < sizeof(struct ip6_hdr
)) {
2024 MGET(n
, MB_DONTWAIT
, MT_DATA
);
2029 n
->m_len
= sizeof(struct ip6_hdr
);
2030 n
->m_next
= m
->m_next
;
2032 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2033 oip6
= mtod(n
, struct ip6_hdr
*);
2035 m
->m_next
->m_len
+= sizeof(struct ip6_hdr
);
2036 m
->m_next
->m_data
-= sizeof(struct ip6_hdr
);
2037 m
->m_pkthdr
.len
+= sizeof(struct ip6_hdr
);
2038 oip6
= mtod(m
->m_next
, struct ip6_hdr
*);
2040 ip6
= mtod(m
, struct ip6_hdr
*);
2041 ovbcopy((caddr_t
)ip6
, (caddr_t
)oip6
, sizeof(struct ip6_hdr
));
2043 /* Fake link-local scope-class addresses */
2044 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
2045 oip6
->ip6_src
.s6_addr16
[1] = 0;
2046 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
2047 oip6
->ip6_dst
.s6_addr16
[1] = 0;
2049 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
2050 /* ECN consideration. */
2051 ip6_ecn_ingress(ip6_ipsec_ecn
, &ip6
->ip6_flow
, &oip6
->ip6_flow
);
2052 if (plen
< IPV6_MAXPACKET
- sizeof(struct ip6_hdr
))
2053 ip6
->ip6_plen
= htons(plen
);
2055 /* ip6->ip6_plen will be updated in ip6_output() */
2057 ip6
->ip6_nxt
= IPPROTO_IPV6
;
2058 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.src
)->sin6_addr
,
2059 &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
2060 bcopy(&((struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
)->sin6_addr
,
2061 &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
2062 ip6
->ip6_hlim
= IPV6_DEFHLIM
;
2064 /* XXX Should ip6_src be updated later ? */
2071 * Check the variable replay window.
2072 * ipsec_chkreplay() performs replay check before ICV verification.
2073 * ipsec_updatereplay() updates replay bitmap. This must be called after
2074 * ICV verification (it also performs replay check, which is usually done
2076 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2078 * based on RFC 2401.
2081 ipsec_chkreplay(u_int32_t seq
, struct secasvar
*sav
)
2083 const struct secreplay
*replay
;
2086 u_int32_t wsizeb
; /* constant: bits of window size */
2087 int frlast
; /* constant: last frame */
2091 panic("ipsec_chkreplay: NULL pointer was passed.");
2093 replay
= sav
->replay
;
2095 if (replay
->wsize
== 0)
2096 return 1; /* no need to check replay. */
2099 frlast
= replay
->wsize
- 1;
2100 wsizeb
= replay
->wsize
<< 3;
2102 /* sequence number of 0 is invalid */
2106 /* first time is always okay */
2107 if (replay
->count
== 0)
2110 if (seq
> replay
->lastseq
) {
2111 /* larger sequences are okay */
2114 /* seq is equal or less than lastseq. */
2115 diff
= replay
->lastseq
- seq
;
2117 /* over range to check, i.e. too old or wrapped */
2121 fr
= frlast
- diff
/ 8;
2123 /* this packet already seen ? */
2124 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2127 /* out of order but good */
2133 * check replay counter whether to update or not.
2138 ipsec_updatereplay(u_int32_t seq
, struct secasvar
*sav
)
2140 struct secreplay
*replay
;
2143 u_int32_t wsizeb
; /* constant: bits of window size */
2144 int frlast
; /* constant: last frame */
2148 panic("ipsec_chkreplay: NULL pointer was passed.");
2150 replay
= sav
->replay
;
2152 if (replay
->wsize
== 0)
2153 goto ok
; /* no need to check replay. */
2156 frlast
= replay
->wsize
- 1;
2157 wsizeb
= replay
->wsize
<< 3;
2159 /* sequence number of 0 is invalid */
2164 if (replay
->count
== 0) {
2165 replay
->lastseq
= seq
;
2166 bzero(replay
->bitmap
, replay
->wsize
);
2167 (replay
->bitmap
)[frlast
] = 1;
2171 if (seq
> replay
->lastseq
) {
2172 /* seq is larger than lastseq. */
2173 diff
= seq
- replay
->lastseq
;
2175 /* new larger sequence number */
2176 if (diff
< wsizeb
) {
2178 /* set bit for this packet */
2179 vshiftl(replay
->bitmap
, diff
, replay
->wsize
);
2180 (replay
->bitmap
)[frlast
] |= 1;
2182 /* this packet has a "way larger" */
2183 bzero(replay
->bitmap
, replay
->wsize
);
2184 (replay
->bitmap
)[frlast
] = 1;
2186 replay
->lastseq
= seq
;
2188 /* larger is good */
2190 /* seq is equal or less than lastseq. */
2191 diff
= replay
->lastseq
- seq
;
2193 /* over range to check, i.e. too old or wrapped */
2197 fr
= frlast
- diff
/ 8;
2199 /* this packet already seen ? */
2200 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2204 (replay
->bitmap
)[fr
] |= (1 << (diff
% 8));
2206 /* out of order but good */
2210 if (replay
->count
== ~0) {
2212 /* set overflow flag */
2215 /* don't increment, no more packets accepted */
2216 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0)
2219 ipseclog((LOG_WARNING
, "replay counter made %d cycle. %s\n",
2220 replay
->overflow
, ipsec_logsastr(sav
)));
2229 * shift variable length buffer to left.
2230 * IN: bitmap: pointer to the buffer
2231 * nbit: the number of to shift.
2232 * wsize: buffer size (bytes).
2235 vshiftl(unsigned char *bitmap
, int nbit
, int wsize
)
2240 for (j
= 0; j
< nbit
; j
+= 8) {
2241 s
= (nbit
- j
< 8) ? (nbit
- j
): 8;
2243 for (i
= 1; i
< wsize
; i
++) {
2244 over
= (bitmap
[i
] >> (8 - s
));
2246 bitmap
[i
-1] |= over
;
2254 ipsec4_logpacketstr(struct ip
*ip
, u_int32_t spi
)
2256 static char buf
[256];
2260 s
= (u_int8_t
*)(&ip
->ip_src
);
2261 d
= (u_int8_t
*)(&ip
->ip_dst
);
2264 ksnprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2267 ksnprintf(p
, sizeof(buf
) - (p
- buf
), "src=%u.%u.%u.%u",
2268 s
[0], s
[1], s
[2], s
[3]);
2271 ksnprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%u.%u.%u.%u",
2272 d
[0], d
[1], d
[2], d
[3]);
2275 ksnprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2282 ipsec6_logpacketstr(struct ip6_hdr
*ip6
, u_int32_t spi
)
2284 static char buf
[256];
2288 ksnprintf(buf
, sizeof(buf
), "packet(SPI=%u ", (u_int32_t
)ntohl(spi
));
2291 ksnprintf(p
, sizeof(buf
) - (p
- buf
), "src=%s",
2292 ip6_sprintf(&ip6
->ip6_src
));
2295 ksnprintf(p
, sizeof(buf
) - (p
- buf
), " dst=%s",
2296 ip6_sprintf(&ip6
->ip6_dst
));
2299 ksnprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2306 ipsec_logsastr(struct secasvar
*sav
)
2308 static char buf
[256];
2310 struct secasindex
*saidx
= &sav
->sah
->saidx
;
2312 /* validity check */
2313 if (((struct sockaddr
*)&sav
->sah
->saidx
.src
)->sa_family
2314 != ((struct sockaddr
*)&sav
->sah
->saidx
.dst
)->sa_family
)
2315 panic("ipsec_logsastr: family mismatched.");
2318 ksnprintf(buf
, sizeof(buf
), "SA(SPI=%u ", (u_int32_t
)ntohl(sav
->spi
));
2321 if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET
) {
2323 s
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->src
)->sin_addr
;
2324 d
= (u_int8_t
*)&((struct sockaddr_in
*)&saidx
->dst
)->sin_addr
;
2325 ksnprintf(p
, sizeof(buf
) - (p
- buf
),
2326 "src=%d.%d.%d.%d dst=%d.%d.%d.%d",
2327 s
[0], s
[1], s
[2], s
[3], d
[0], d
[1], d
[2], d
[3]);
2330 else if (((struct sockaddr
*)&saidx
->src
)->sa_family
== AF_INET6
) {
2331 ksnprintf(p
, sizeof(buf
) - (p
- buf
),
2333 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->src
)->sin6_addr
));
2336 ksnprintf(p
, sizeof(buf
) - (p
- buf
),
2338 ip6_sprintf(&((struct sockaddr_in6
*)&saidx
->dst
)->sin6_addr
));
2343 ksnprintf(p
, sizeof(buf
) - (p
- buf
), ")");
2349 ipsec_dumpmbuf(struct mbuf
*m
)
2358 p
= mtod(m
, u_char
*);
2359 for (i
= 0; i
< m
->m_len
; i
++) {
2360 kprintf("%02x ", p
[i
]);
2362 if (totlen
% 16 == 0)
2367 if (totlen
% 16 != 0)
2374 * IPsec output logic for IPv4.
2377 ipsec4_output(struct ipsec_output_state
*state
, struct secpolicy
*sp
, int flags
)
2379 struct ip
*ip
= NULL
;
2380 struct ipsecrequest
*isr
= NULL
;
2381 struct secasindex saidx
;
2383 struct sockaddr_in
*dst4
;
2384 struct sockaddr_in
*sin
;
2387 panic("state == NULL in ipsec4_output");
2389 panic("state->m == NULL in ipsec4_output");
2391 panic("state->ro == NULL in ipsec4_output");
2393 panic("state->dst == NULL in ipsec4_output");
2395 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2396 kprintf("ipsec4_output: applyed SP\n");
2397 kdebug_secpolicy(sp
));
2399 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
2401 #if 0 /* give up to check restriction of transport mode */
2402 /* XXX but should be checked somewhere */
2404 * some of the IPsec operation must be performed only in
2407 if (isr
->saidx
.mode
== IPSEC_MODE_TRANSPORT
2408 && (flags
& IP_FORWARDING
))
2412 /* make SA index for search proper SA */
2413 ip
= mtod(state
->m
, struct ip
*);
2414 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2415 saidx
.mode
= isr
->saidx
.mode
;
2416 saidx
.reqid
= isr
->saidx
.reqid
;
2417 sin
= (struct sockaddr_in
*)&saidx
.src
;
2418 if (sin
->sin_len
== 0) {
2419 sin
->sin_len
= sizeof(*sin
);
2420 sin
->sin_family
= AF_INET
;
2421 sin
->sin_port
= IPSEC_PORT_ANY
;
2422 bcopy(&ip
->ip_src
, &sin
->sin_addr
,
2423 sizeof(sin
->sin_addr
));
2425 sin
= (struct sockaddr_in
*)&saidx
.dst
;
2426 if (sin
->sin_len
== 0) {
2427 sin
->sin_len
= sizeof(*sin
);
2428 sin
->sin_family
= AF_INET
;
2429 sin
->sin_port
= IPSEC_PORT_ANY
;
2430 bcopy(&ip
->ip_dst
, &sin
->sin_addr
,
2431 sizeof(sin
->sin_addr
));
2434 if ((error
= key_checkrequest(isr
, &saidx
)) != 0) {
2436 * IPsec processing is required, but no SA found.
2437 * I assume that key_acquire() had been called
2438 * to get/establish the SA. Here I discard
2439 * this packet because it is responsibility for
2440 * upper layer to retransmit the packet.
2442 ipsecstat
.out_nosa
++;
2446 /* validity check */
2447 if (isr
->sav
== NULL
) {
2448 switch (ipsec_get_reqlevel(isr
)) {
2449 case IPSEC_LEVEL_USE
:
2451 case IPSEC_LEVEL_REQUIRE
:
2452 /* must be not reached here. */
2453 panic("ipsec4_output: no SA found, but required.");
2458 * If there is no valid SA, we give up to process any
2459 * more. In such a case, the SA's status is changed
2460 * from DYING to DEAD after allocating. If a packet
2461 * send to the receiver by dead SA, the receiver can
2462 * not decode a packet because SA has been dead.
2464 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2465 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2466 ipsecstat
.out_nosa
++;
2472 * There may be the case that SA status will be changed when
2473 * we are refering to one. So calling crit_enter().
2477 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2479 * build IPsec tunnel.
2481 /* XXX should be processed with other familiy */
2482 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET
) {
2483 ipseclog((LOG_ERR
, "ipsec4_output: "
2484 "family mismatched between inner and outer spi=%u\n",
2485 (u_int32_t
)ntohl(isr
->sav
->spi
)));
2487 error
= EAFNOSUPPORT
;
2491 state
->m
= ipsec4_splithdr(state
->m
);
2497 error
= ipsec4_encapsulate(state
->m
, isr
->sav
);
2503 ip
= mtod(state
->m
, struct ip
*);
2505 state
->ro
= &isr
->sav
->sah
->sa_route
;
2506 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
2507 dst4
= (struct sockaddr_in
*)state
->dst
;
2508 if (state
->ro
->ro_rt
2509 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
2510 || dst4
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
2511 RTFREE(state
->ro
->ro_rt
);
2512 state
->ro
->ro_rt
= NULL
;
2514 if (state
->ro
->ro_rt
== 0) {
2515 dst4
->sin_family
= AF_INET
;
2516 dst4
->sin_len
= sizeof(*dst4
);
2517 dst4
->sin_addr
= ip
->ip_dst
;
2520 if (state
->ro
->ro_rt
== 0) {
2521 ipstat
.ips_noroute
++;
2522 error
= EHOSTUNREACH
;
2526 /* adjust state->dst if tunnel endpoint is offlink */
2527 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
2528 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
2529 dst4
= (struct sockaddr_in
*)state
->dst
;
2534 state
->m
= ipsec4_splithdr(state
->m
);
2539 switch (isr
->saidx
.proto
) {
2542 if ((error
= esp4_output(state
->m
, isr
)) != 0) {
2554 if ((error
= ah4_output(state
->m
, isr
)) != 0) {
2559 case IPPROTO_IPCOMP
:
2560 if ((error
= ipcomp4_output(state
->m
, isr
)) != 0) {
2567 "ipsec4_output: unknown ipsec protocol %d\n",
2575 if (state
->m
== 0) {
2579 ip
= mtod(state
->m
, struct ip
*);
2593 * IPsec output logic for IPv6, transport mode.
2596 ipsec6_output_trans(struct ipsec_output_state
*state
, u_char
*nexthdrp
,
2597 struct mbuf
*mprev
, struct secpolicy
*sp
, int flags
,
2600 struct ip6_hdr
*ip6
;
2601 struct ipsecrequest
*isr
= NULL
;
2602 struct secasindex saidx
;
2605 struct sockaddr_in6
*sin6
;
2608 panic("state == NULL in ipsec6_output_trans");
2610 panic("state->m == NULL in ipsec6_output_trans");
2612 panic("nexthdrp == NULL in ipsec6_output_trans");
2614 panic("mprev == NULL in ipsec6_output_trans");
2616 panic("sp == NULL in ipsec6_output_trans");
2618 panic("tun == NULL in ipsec6_output_trans");
2620 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2621 kprintf("ipsec6_output_trans: applyed SP\n");
2622 kdebug_secpolicy(sp
));
2625 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
2626 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2627 /* the rest will be handled by ipsec6_output_tunnel() */
2631 /* make SA index for search proper SA */
2632 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2633 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2634 saidx
.mode
= isr
->saidx
.mode
;
2635 saidx
.reqid
= isr
->saidx
.reqid
;
2636 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
2637 if (sin6
->sin6_len
== 0) {
2638 sin6
->sin6_len
= sizeof(*sin6
);
2639 sin6
->sin6_family
= AF_INET6
;
2640 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2641 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
2642 sizeof(ip6
->ip6_src
));
2643 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
2644 /* fix scope id for comparing SPD */
2645 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2646 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
2649 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
2650 if (sin6
->sin6_len
== 0) {
2651 sin6
->sin6_len
= sizeof(*sin6
);
2652 sin6
->sin6_family
= AF_INET6
;
2653 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2654 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
2655 sizeof(ip6
->ip6_dst
));
2656 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
2657 /* fix scope id for comparing SPD */
2658 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2659 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
2663 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
2665 * IPsec processing is required, but no SA found.
2666 * I assume that key_acquire() had been called
2667 * to get/establish the SA. Here I discard
2668 * this packet because it is responsibility for
2669 * upper layer to retransmit the packet.
2671 ipsec6stat
.out_nosa
++;
2675 * Notify the fact that the packet is discarded
2676 * to ourselves. I believe this is better than
2677 * just silently discarding. (jinmei@kame.net)
2678 * XXX: should we restrict the error to TCP packets?
2679 * XXX: should we directly notify sockets via
2682 icmp6_error(state
->m
, ICMP6_DST_UNREACH
,
2683 ICMP6_DST_UNREACH_ADMIN
, 0);
2684 state
->m
= NULL
; /* icmp6_error freed the mbuf */
2688 /* validity check */
2689 if (isr
->sav
== NULL
) {
2690 switch (ipsec_get_reqlevel(isr
)) {
2691 case IPSEC_LEVEL_USE
:
2693 case IPSEC_LEVEL_REQUIRE
:
2694 /* must be not reached here. */
2695 panic("ipsec6_output_trans: no SA found, but required.");
2700 * If there is no valid SA, we give up to process.
2701 * see same place at ipsec4_output().
2703 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2704 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2705 ipsec6stat
.out_nosa
++;
2710 switch (isr
->saidx
.proto
) {
2713 error
= esp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2720 error
= ah6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2722 case IPPROTO_IPCOMP
:
2723 error
= ipcomp6_output(state
->m
, nexthdrp
, mprev
->m_next
, isr
);
2726 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
2727 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
2729 ipsec6stat
.out_inval
++;
2737 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2738 if (plen
> IPV6_MAXPACKET
) {
2739 ipseclog((LOG_ERR
, "ipsec6_output_trans: "
2740 "IPsec with IPv6 jumbogram is not supported\n"));
2741 ipsec6stat
.out_inval
++;
2742 error
= EINVAL
; /* XXX */
2745 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2746 ip6
->ip6_plen
= htons(plen
);
2749 /* if we have more to go, we need a tunnel mode processing */
2762 * IPsec output logic for IPv6, tunnel mode.
2765 ipsec6_output_tunnel(struct ipsec_output_state
*state
, struct secpolicy
*sp
,
2768 struct ip6_hdr
*ip6
;
2769 struct ipsecrequest
*isr
= NULL
;
2770 struct secasindex saidx
;
2773 struct sockaddr_in6
* dst6
;
2776 panic("state == NULL in ipsec6_output_tunnel");
2778 panic("state->m == NULL in ipsec6_output_tunnel");
2780 panic("sp == NULL in ipsec6_output_tunnel");
2782 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
2783 kprintf("ipsec6_output_tunnel: applyed SP\n");
2784 kdebug_secpolicy(sp
));
2787 * transport mode ipsec (before the 1st tunnel mode) is already
2788 * processed by ipsec6_output_trans().
2790 for (isr
= sp
->req
; isr
; isr
= isr
->next
) {
2791 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
2795 for (/* already initialized */; isr
; isr
= isr
->next
) {
2796 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2797 /* When tunnel mode, SA peers must be specified. */
2798 bcopy(&isr
->saidx
, &saidx
, sizeof(saidx
));
2800 /* make SA index to look for a proper SA */
2801 struct sockaddr_in6
*sin6
;
2803 bzero(&saidx
, sizeof(saidx
));
2804 saidx
.proto
= isr
->saidx
.proto
;
2805 saidx
.mode
= isr
->saidx
.mode
;
2806 saidx
.reqid
= isr
->saidx
.reqid
;
2808 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2809 sin6
= (struct sockaddr_in6
*)&saidx
.src
;
2810 if (sin6
->sin6_len
== 0) {
2811 sin6
->sin6_len
= sizeof(*sin6
);
2812 sin6
->sin6_family
= AF_INET6
;
2813 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2814 bcopy(&ip6
->ip6_src
, &sin6
->sin6_addr
,
2815 sizeof(ip6
->ip6_src
));
2816 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
2817 /* fix scope id for comparing SPD */
2818 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2819 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
2822 sin6
= (struct sockaddr_in6
*)&saidx
.dst
;
2823 if (sin6
->sin6_len
== 0) {
2824 sin6
->sin6_len
= sizeof(*sin6
);
2825 sin6
->sin6_family
= AF_INET6
;
2826 sin6
->sin6_port
= IPSEC_PORT_ANY
;
2827 bcopy(&ip6
->ip6_dst
, &sin6
->sin6_addr
,
2828 sizeof(ip6
->ip6_dst
));
2829 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
2830 /* fix scope id for comparing SPD */
2831 sin6
->sin6_addr
.s6_addr16
[1] = 0;
2832 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
2837 if (key_checkrequest(isr
, &saidx
) == ENOENT
) {
2839 * IPsec processing is required, but no SA found.
2840 * I assume that key_acquire() had been called
2841 * to get/establish the SA. Here I discard
2842 * this packet because it is responsibility for
2843 * upper layer to retransmit the packet.
2845 ipsec6stat
.out_nosa
++;
2850 /* validity check */
2851 if (isr
->sav
== NULL
) {
2852 switch (ipsec_get_reqlevel(isr
)) {
2853 case IPSEC_LEVEL_USE
:
2855 case IPSEC_LEVEL_REQUIRE
:
2856 /* must be not reached here. */
2857 panic("ipsec6_output_tunnel: no SA found, but required.");
2862 * If there is no valid SA, we give up to process.
2863 * see same place at ipsec4_output().
2865 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
2866 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
2867 ipsec6stat
.out_nosa
++;
2873 * There may be the case that SA status will be changed when
2874 * we are refering to one. So calling crit_enter().
2878 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
2880 * build IPsec tunnel.
2882 /* XXX should be processed with other familiy */
2883 if (((struct sockaddr
*)&isr
->sav
->sah
->saidx
.src
)->sa_family
!= AF_INET6
) {
2884 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
2885 "family mismatched between inner and outer, spi=%u\n",
2886 (u_int32_t
)ntohl(isr
->sav
->spi
)));
2888 ipsec6stat
.out_inval
++;
2889 error
= EAFNOSUPPORT
;
2893 state
->m
= ipsec6_splithdr(state
->m
);
2896 ipsec6stat
.out_nomem
++;
2900 error
= ipsec6_encapsulate(state
->m
, isr
->sav
);
2906 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2908 state
->ro
= &isr
->sav
->sah
->sa_route
;
2909 state
->dst
= (struct sockaddr
*)&state
->ro
->ro_dst
;
2910 dst6
= (struct sockaddr_in6
*)state
->dst
;
2911 if (state
->ro
->ro_rt
2912 && ((state
->ro
->ro_rt
->rt_flags
& RTF_UP
) == 0
2913 || !IN6_ARE_ADDR_EQUAL(&dst6
->sin6_addr
, &ip6
->ip6_dst
))) {
2914 RTFREE(state
->ro
->ro_rt
);
2915 state
->ro
->ro_rt
= NULL
;
2917 if (state
->ro
->ro_rt
== 0) {
2918 bzero(dst6
, sizeof(*dst6
));
2919 dst6
->sin6_family
= AF_INET6
;
2920 dst6
->sin6_len
= sizeof(*dst6
);
2921 dst6
->sin6_addr
= ip6
->ip6_dst
;
2924 if (state
->ro
->ro_rt
== 0) {
2925 ip6stat
.ip6s_noroute
++;
2926 ipsec6stat
.out_noroute
++;
2927 error
= EHOSTUNREACH
;
2931 /* adjust state->dst if tunnel endpoint is offlink */
2932 if (state
->ro
->ro_rt
->rt_flags
& RTF_GATEWAY
) {
2933 state
->dst
= (struct sockaddr
*)state
->ro
->ro_rt
->rt_gateway
;
2934 dst6
= (struct sockaddr_in6
*)state
->dst
;
2939 state
->m
= ipsec6_splithdr(state
->m
);
2941 ipsec6stat
.out_nomem
++;
2945 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2946 switch (isr
->saidx
.proto
) {
2949 error
= esp6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
2956 error
= ah6_output(state
->m
, &ip6
->ip6_nxt
, state
->m
->m_next
, isr
);
2958 case IPPROTO_IPCOMP
:
2959 /* XXX code should be here */
2962 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
2963 "unknown ipsec protocol %d\n", isr
->saidx
.proto
));
2965 ipsec6stat
.out_inval
++;
2973 plen
= state
->m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2974 if (plen
> IPV6_MAXPACKET
) {
2975 ipseclog((LOG_ERR
, "ipsec6_output_tunnel: "
2976 "IPsec with IPv6 jumbogram is not supported\n"));
2977 ipsec6stat
.out_inval
++;
2978 error
= EINVAL
; /* XXX */
2981 ip6
= mtod(state
->m
, struct ip6_hdr
*);
2982 ip6
->ip6_plen
= htons(plen
);
2996 * Chop IP header and option off from the payload.
2998 static struct mbuf
*
2999 ipsec4_splithdr(struct mbuf
*m
)
3005 if (m
->m_len
< sizeof(struct ip
))
3006 panic("ipsec4_splithdr: first mbuf too short");
3007 ip
= mtod(m
, struct ip
*);
3009 hlen
= _IP_VHL_HL(ip
->ip_vhl
) << 2;
3011 hlen
= ip
->ip_hl
<< 2;
3013 if (m
->m_len
> hlen
) {
3014 MGETHDR(mh
, MB_DONTWAIT
, MT_HEADER
);
3019 M_MOVE_PKTHDR(mh
, m
);
3026 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), hlen
);
3027 } else if (m
->m_len
< hlen
) {
3028 m
= m_pullup(m
, hlen
);
3037 static struct mbuf
*
3038 ipsec6_splithdr(struct mbuf
*m
)
3041 struct ip6_hdr
*ip6
;
3044 if (m
->m_len
< sizeof(struct ip6_hdr
))
3045 panic("ipsec6_splithdr: first mbuf too short");
3046 ip6
= mtod(m
, struct ip6_hdr
*);
3047 hlen
= sizeof(struct ip6_hdr
);
3048 if (m
->m_len
> hlen
) {
3049 MGETHDR(mh
, MB_DONTWAIT
, MT_HEADER
);
3054 M_MOVE_PKTHDR(mh
, m
);
3061 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), hlen
);
3062 } else if (m
->m_len
< hlen
) {
3063 m
= m_pullup(m
, hlen
);
3071 /* validate inbound IPsec tunnel packet. */
3073 ipsec4_tunnel_validate(struct mbuf
*m
, /* no pullup permitted, m->m_len >= ip */
3074 int off
, u_int nxt0
, struct secasvar
*sav
)
3076 u_int8_t nxt
= nxt0
& 0xff;
3077 struct sockaddr_in
*sin
;
3078 struct sockaddr_in osrc
, odst
, isrc
, idst
;
3080 struct secpolicy
*sp
;
3084 if (m
->m_len
< sizeof(struct ip
))
3085 panic("too short mbuf on ipsec4_tunnel_validate");
3087 if (nxt
!= IPPROTO_IPV4
)
3089 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip
))
3091 /* do not decapsulate if the SA is for transport mode only */
3092 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3095 oip
= mtod(m
, struct ip
*);
3097 hlen
= _IP_VHL_HL(oip
->ip_vhl
) << 2;
3099 hlen
= oip
->ip_hl
<< 2;
3101 if (hlen
!= sizeof(struct ip
))
3104 /* AF_INET6 should be supported, but at this moment we don't. */
3105 sin
= (struct sockaddr_in
*)&sav
->sah
->saidx
.dst
;
3106 if (sin
->sin_family
!= AF_INET
)
3108 if (bcmp(&oip
->ip_dst
, &sin
->sin_addr
, sizeof(oip
->ip_dst
)) != 0)
3112 bzero(&osrc
, sizeof(osrc
));
3113 bzero(&odst
, sizeof(odst
));
3114 bzero(&isrc
, sizeof(isrc
));
3115 bzero(&idst
, sizeof(idst
));
3116 osrc
.sin_family
= odst
.sin_family
= isrc
.sin_family
= idst
.sin_family
=
3118 osrc
.sin_len
= odst
.sin_len
= isrc
.sin_len
= idst
.sin_len
=
3119 sizeof(struct sockaddr_in
);
3120 osrc
.sin_addr
= oip
->ip_src
;
3121 odst
.sin_addr
= oip
->ip_dst
;
3122 m_copydata(m
, off
+ offsetof(struct ip
, ip_src
), sizeof(isrc
.sin_addr
),
3123 (caddr_t
)&isrc
.sin_addr
);
3124 m_copydata(m
, off
+ offsetof(struct ip
, ip_dst
), sizeof(idst
.sin_addr
),
3125 (caddr_t
)&idst
.sin_addr
);
3128 * RFC2401 5.2.1 (b): (assume that we are using tunnel mode)
3129 * - if the inner destination is multicast address, there can be
3130 * multiple permissible inner source address. implementation
3131 * may want to skip verification of inner source address against
3133 * - if the inner protocol is ICMP, the packet may be an error report
3134 * from routers on the other side of the VPN cloud (R in the
3135 * following diagram). in this case, we cannot verify inner source
3136 * address against SPD selector.
3137 * me -- gw === gw -- R -- you
3139 * we consider the first bullet to be users responsibility on SPD entry
3140 * configuration (if you need to encrypt multicast traffic, set
3141 * the source range of SPD selector to 0.0.0.0/0, or have explicit
3142 * address ranges for possible senders).
3143 * the second bullet is not taken care of (yet).
3145 * therefore, we do not do anything special about inner source.
3148 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3149 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3158 /* validate inbound IPsec tunnel packet. */
3160 ipsec6_tunnel_validate(struct mbuf
*m
, /* no pullup permitted, m->m_len >= ip */
3161 int off
, u_int nxt0
, struct secasvar
*sav
)
3163 u_int8_t nxt
= nxt0
& 0xff;
3164 struct sockaddr_in6
*sin6
;
3165 struct sockaddr_in6 osrc
, odst
, isrc
, idst
;
3166 struct secpolicy
*sp
;
3167 struct ip6_hdr
*oip6
;
3170 if (m
->m_len
< sizeof(struct ip6_hdr
))
3171 panic("too short mbuf on ipsec6_tunnel_validate");
3173 if (nxt
!= IPPROTO_IPV6
)
3175 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ip6_hdr
))
3177 /* do not decapsulate if the SA is for transport mode only */
3178 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TRANSPORT
)
3181 oip6
= mtod(m
, struct ip6_hdr
*);
3182 /* AF_INET should be supported, but at this moment we don't. */
3183 sin6
= (struct sockaddr_in6
*)&sav
->sah
->saidx
.dst
;
3184 if (sin6
->sin6_family
!= AF_INET6
)
3186 if (!IN6_ARE_ADDR_EQUAL(&oip6
->ip6_dst
, &sin6
->sin6_addr
))
3190 bzero(&osrc
, sizeof(osrc
));
3191 bzero(&odst
, sizeof(odst
));
3192 bzero(&isrc
, sizeof(isrc
));
3193 bzero(&idst
, sizeof(idst
));
3194 osrc
.sin6_family
= odst
.sin6_family
= isrc
.sin6_family
=
3195 idst
.sin6_family
= AF_INET6
;
3196 osrc
.sin6_len
= odst
.sin6_len
= isrc
.sin6_len
= idst
.sin6_len
=
3197 sizeof(struct sockaddr_in6
);
3198 osrc
.sin6_addr
= oip6
->ip6_src
;
3199 odst
.sin6_addr
= oip6
->ip6_dst
;
3200 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_src
),
3201 sizeof(isrc
.sin6_addr
), (caddr_t
)&isrc
.sin6_addr
);
3202 m_copydata(m
, off
+ offsetof(struct ip6_hdr
, ip6_dst
),
3203 sizeof(idst
.sin6_addr
), (caddr_t
)&idst
.sin6_addr
);
3206 * regarding to inner source address validation, see a long comment
3207 * in ipsec4_tunnel_validate.
3210 sp
= key_gettunnel((struct sockaddr
*)&osrc
, (struct sockaddr
*)&odst
,
3211 (struct sockaddr
*)&isrc
, (struct sockaddr
*)&idst
);
3213 * when there is no suitable inbound policy for the packet of the ipsec
3214 * tunnel mode, the kernel never decapsulate the tunneled packet
3215 * as the ipsec tunnel mode even when the system wide policy is "none".
3216 * then the kernel leaves the generic tunnel module to process this
3217 * packet. if there is no rule of the generic tunnel, the packet
3218 * is rejected and the statistics will be counted up.
3229 * Make a mbuf chain for encryption.
3230 * If the original mbuf chain contains a mbuf with a cluster,
3231 * allocate a new cluster and copy the data to the new cluster.
3232 * XXX: this hack is inefficient, but is necessary to handle cases
3233 * of TCP retransmission...
3236 ipsec_copypkt(struct mbuf
*m
)
3238 struct mbuf
*n
, **mpp
, *mnew
;
3240 for (n
= m
, mpp
= &m
; n
; n
= n
->m_next
) {
3241 if (n
->m_flags
& M_EXT
) {
3243 * Make a copy only if there are more than one
3244 * references to the cluster.
3245 * XXX: is this approach effective?
3247 if (m_sharecount(n
) > 1) {
3251 if (n
->m_flags
& M_PKTHDR
) {
3252 MGETHDR(mnew
, MB_DONTWAIT
, MT_HEADER
);
3255 if (!m_dup_pkthdr(mnew
, n
, MB_DONTWAIT
)) {
3261 MGET(mnew
, MB_DONTWAIT
, MT_DATA
);
3269 * Copy data. If we don't have enough space to
3270 * store the whole data, allocate a cluster
3271 * or additional mbufs.
3272 * XXX: we don't use m_copyback(), since the
3273 * function does not use clusters and thus is
3282 if (remain
<= (mm
->m_flags
& M_PKTHDR
? MHLEN
: MLEN
))
3284 else { /* allocate a cluster */
3285 MCLGET(mm
, MB_DONTWAIT
);
3286 if (!(mm
->m_flags
& M_EXT
)) {
3290 len
= remain
< MCLBYTES
?
3294 bcopy(n
->m_data
+ copied
, mm
->m_data
,
3301 if (remain
<= 0) /* completed? */
3304 /* need another mbuf */
3305 MGETHDR(mn
, MB_DONTWAIT
, MT_HEADER
);
3308 mn
->m_pkthdr
.rcvif
= NULL
;
3314 mm
->m_next
= m_free(n
);
3333 ipsec_delaux(struct mbuf
*m
)
3337 while ((tag
= m_tag_find(m
, PACKET_TAG_IPSEC_HISTORY
, NULL
)) != NULL
)
3338 m_tag_delete(m
, tag
);
3342 ipsec_addhist(struct mbuf
*m
, int proto
, u_int32_t spi
)
3345 struct ipsec_history
*p
;
3347 tag
= m_tag_get(PACKET_TAG_IPSEC_HISTORY
,
3348 sizeof (struct ipsec_history
), M_NOWAIT
);
3351 p
= (struct ipsec_history
*)m_tag_data(tag
);
3352 bzero(p
, sizeof(*p
));
3353 p
->ih_proto
= proto
;
3355 m_tag_prepend(m
, tag
);
3359 struct ipsec_history
*
3360 ipsec_gethist(struct mbuf
*m
, int *lenp
)
3364 tag
= m_tag_find(m
, PACKET_TAG_IPSEC_HISTORY
, NULL
);
3367 /* XXX NB: noone uses this so fake it */
3369 *lenp
= sizeof (struct ipsec_history
);
3370 return ((struct ipsec_history
*)(tag
+1));