1 /* $NetBSD: ipsec.c,v 1.45 2009/06/25 17:39:51 christos Exp $ */
2 /* $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/netipsec/ipsec.c,v 1.2.2.2 2003/07/01 01:38:13 sam 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
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ipsec.c,v 1.45 2009/06/25 17:39:51 christos Exp $");
38 * IPsec controller part.
43 #include "opt_inet6.h"
45 #include "opt_ipsec.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
51 #include <sys/domain.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/errno.h>
57 #include <sys/kernel.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
61 #include <sys/kauth.h>
64 #include <net/route.h>
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/in_var.h>
71 #include <netinet/udp.h>
72 #include <netinet/udp_var.h>
73 #include <netinet/tcp.h>
74 #include <netinet/udp.h>
75 #include <netinet/ip_icmp.h>
77 #include <netinet/ip6.h>
79 #include <netinet6/ip6_var.h>
81 #include <netinet/in_pcb.h>
83 #include <netinet6/in6_pcb.h>
84 #include <netinet/icmp6.h>
87 #include <netipsec/ipsec.h>
88 #include <netipsec/ipsec_var.h>
89 #include <netipsec/ipsec_private.h>
91 #include <netipsec/ipsec6.h>
93 #include <netipsec/ah_var.h>
94 #include <netipsec/esp_var.h>
95 #include <netipsec/ipcomp.h> /*XXX*/
96 #include <netipsec/ipcomp_var.h>
98 #include <netipsec/key.h>
99 #include <netipsec/keydb.h>
100 #include <netipsec/key_debug.h>
102 #include <netipsec/xform.h>
104 #include <netipsec/ipsec_osdep.h>
106 #include <net/net_osdep.h>
112 * When set to 1, IPsec will send packets with the same sequence number.
113 * This allows to verify if the other side has proper replay attacks detection.
115 int ipsec_replay
= 0;
118 * When set 1, IPsec will send packets with corrupted HMAC.
119 * This allows to verify if the other side properly detects modified packets.
121 int ipsec_integrity
= 0;
126 percpu_t
*ipsecstat_percpu
;
127 int ip4_ah_offsetmask
= 0; /* maybe IP_DF? */
128 int ip4_ipsec_dfbit
= 2; /* DF bit on encap. 0: clear 1: set 2: copy */
129 int ip4_esp_trans_deflev
= IPSEC_LEVEL_USE
;
130 int ip4_esp_net_deflev
= IPSEC_LEVEL_USE
;
131 int ip4_ah_trans_deflev
= IPSEC_LEVEL_USE
;
132 int ip4_ah_net_deflev
= IPSEC_LEVEL_USE
;
133 struct secpolicy ip4_def_policy
;
134 int ip4_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
135 int ip4_esp_randpad
= -1;
138 u_int ipsec_spdgen
= 1; /* SPD generation # */
140 static struct secpolicy
*ipsec_checkpcbcache (struct mbuf
*,
141 struct inpcbpolicy
*, int);
142 static int ipsec_fillpcbcache (struct inpcbpolicy
*, struct mbuf
*,
143 struct secpolicy
*, int);
144 static int ipsec_invalpcbcache (struct inpcbpolicy
*, int);
145 #endif /* __NetBSD__ */
148 * Crypto support requirements:
150 * 1 require hardware support
151 * -1 require software support
154 int crypto_support
= 0;
156 static struct secpolicy
*ipsec_getpolicybysock(struct mbuf
*, u_int
,
160 SYSCTL_DECL(_net_inet_ipsec
);
163 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_POLICY
,
164 def_policy
, CTLFLAG_RW
, &ip4_def_policy
.policy
, 0, "");
165 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
166 CTLFLAG_RW
, &ip4_esp_trans_deflev
, 0, "");
167 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
168 CTLFLAG_RW
, &ip4_esp_net_deflev
, 0, "");
169 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
170 CTLFLAG_RW
, &ip4_ah_trans_deflev
, 0, "");
171 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
172 CTLFLAG_RW
, &ip4_ah_net_deflev
, 0, "");
173 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_CLEARTOS
,
174 ah_cleartos
, CTLFLAG_RW
, &ip4_ah_cleartos
, 0, "");
175 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_AH_OFFSETMASK
,
176 ah_offsetmask
, CTLFLAG_RW
, &ip4_ah_offsetmask
, 0, "");
177 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DFBIT
,
178 dfbit
, CTLFLAG_RW
, &ip4_ipsec_dfbit
, 0, "");
179 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ECN
,
180 ecn
, CTLFLAG_RW
, &ip4_ipsec_ecn
, 0, "");
181 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_DEBUG
,
182 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
183 SYSCTL_INT(_net_inet_ipsec
, IPSECCTL_ESP_RANDPAD
,
184 esp_randpad
, CTLFLAG_RW
, &ip4_esp_randpad
, 0, "");
185 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
,
186 crypto_support
, CTLFLAG_RW
, &crypto_support
,0, "");
187 SYSCTL_STRUCT(_net_inet_ipsec
, OID_AUTO
,
188 ipsecstats
, CTLFLAG_RD
, &newipsecstat
, newipsecstat
, "");
189 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, test_replay
, CTLFLAG_RW
, &ipsec_replay
, 0,
190 "Emulate replay attack");
191 SYSCTL_INT(_net_inet_ipsec
, OID_AUTO
, test_integrity
, CTLFLAG_RW
,
192 &ipsec_integrity
, 0, "Emulate man-in-the-middle attack");
193 #endif /* __FreeBSD__ */
196 int ip6_esp_trans_deflev
= IPSEC_LEVEL_USE
;
197 int ip6_esp_net_deflev
= IPSEC_LEVEL_USE
;
198 int ip6_ah_trans_deflev
= IPSEC_LEVEL_USE
;
199 int ip6_ah_net_deflev
= IPSEC_LEVEL_USE
;
200 struct secpolicy ip6_def_policy
;
201 int ip6_ipsec_ecn
= 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
202 int ip6_esp_randpad
= -1;
206 SYSCTL_DECL(_net_inet6_ipsec6
);
208 /* net.inet6.ipsec6 */
210 SYSCTL_OID(_net_inet6_ipsec6
, IPSECCTL_STATS
, stats
, CTLFLAG_RD
,
211 0,0, compat_ipsecstats_sysctl
, "S", "");
212 #endif /* COMPAT_KAME */
213 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_POLICY
,
214 def_policy
, CTLFLAG_RW
, &ip4_def_policy
.policy
, 0, "");
215 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_TRANSLEV
, esp_trans_deflev
,
216 CTLFLAG_RW
, &ip6_esp_trans_deflev
, 0, "");
217 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_ESP_NETLEV
, esp_net_deflev
,
218 CTLFLAG_RW
, &ip6_esp_net_deflev
, 0, "");
219 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_TRANSLEV
, ah_trans_deflev
,
220 CTLFLAG_RW
, &ip6_ah_trans_deflev
, 0, "");
221 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEF_AH_NETLEV
, ah_net_deflev
,
222 CTLFLAG_RW
, &ip6_ah_net_deflev
, 0, "");
223 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ECN
,
224 ecn
, CTLFLAG_RW
, &ip6_ipsec_ecn
, 0, "");
225 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_DEBUG
,
226 debug
, CTLFLAG_RW
, &ipsec_debug
, 0, "");
227 SYSCTL_INT(_net_inet6_ipsec6
, IPSECCTL_ESP_RANDPAD
,
228 esp_randpad
, CTLFLAG_RW
, &ip6_esp_randpad
, 0, "");
230 #endif /* __FreeBSD__ */
232 static int ipsec4_setspidx_inpcb (struct mbuf
*, struct inpcb
*);
234 static int ipsec6_setspidx_in6pcb (struct mbuf
*, struct in6pcb
*);
236 static int ipsec_setspidx (struct mbuf
*, struct secpolicyindex
*, int);
237 static void ipsec4_get_ulp (struct mbuf
*m
, struct secpolicyindex
*, int);
238 static int ipsec4_setspidx_ipaddr (struct mbuf
*, struct secpolicyindex
*);
240 static void ipsec6_get_ulp (struct mbuf
*m
, struct secpolicyindex
*, int);
241 static int ipsec6_setspidx_ipaddr (struct mbuf
*, struct secpolicyindex
*);
243 static void ipsec_delpcbpolicy (struct inpcbpolicy
*);
244 static struct secpolicy
*ipsec_deepcopy_policy (struct secpolicy
*);
245 static int ipsec_set_policy (struct secpolicy
**,int , void *, size_t ,
247 static int ipsec_get_policy (struct secpolicy
*, struct mbuf
**);
248 static void vshiftl (unsigned char *, int, int);
249 static size_t ipsec_hdrsiz (struct secpolicy
*);
253 * Try to validate and use cached policy on a PCB.
255 static struct secpolicy
*
256 ipsec_checkpcbcache(struct mbuf
*m
, struct inpcbpolicy
*pcbsp
, int dir
)
258 struct secpolicyindex spidx
;
261 case IPSEC_DIR_INBOUND
:
262 case IPSEC_DIR_OUTBOUND
:
270 printf("ipsec_checkpcbcache: NULL pcbsp\n");
277 if (dir
>= sizeof(pcbsp
->sp_cache
)/sizeof(pcbsp
->sp_cache
[0]))
278 panic("dir too big in ipsec_checkpcbcache");
280 /* SPD table change invalidate all the caches. */
281 if (ipsec_spdgen
!= pcbsp
->sp_cache
[dir
].cachegen
) {
282 ipsec_invalpcbcache(pcbsp
, dir
);
285 if (!pcbsp
->sp_cache
[dir
].cachesp
)
287 if (pcbsp
->sp_cache
[dir
].cachesp
->state
!= IPSEC_SPSTATE_ALIVE
) {
288 ipsec_invalpcbcache(pcbsp
, dir
);
291 if ((pcbsp
->sp_cacheflags
& IPSEC_PCBSP_CONNECTED
) == 0) {
292 if (!pcbsp
->sp_cache
[dir
].cachesp
)
294 if (ipsec_setspidx(m
, &spidx
, 1) != 0)
298 * We have to make an exact match here since the cached rule
299 * might have lower priority than a rule that would otherwise
300 * have matched the packet.
303 if (memcmp(&pcbsp
->sp_cache
[dir
].cacheidx
, &spidx
, sizeof(spidx
)))
308 * The pcb is connected, and the L4 code is sure that:
309 * - outgoing side uses inp_[lf]addr
310 * - incoming side looks up policy after inpcb lookup
311 * and address pair is know to be stable. We do not need
312 * to generate spidx again, nor check the address match again.
314 * For IPv4/v6 SOCK_STREAM sockets, this assumptions holds
315 * and there are calls to ipsec_pcbconn() from in_pcbconnect().
319 pcbsp
->sp_cache
[dir
].cachesp
->lastused
= time_second
;
320 pcbsp
->sp_cache
[dir
].cachesp
->refcnt
++;
321 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
322 printf("DP ipsec_checkpcbcache cause refcnt++:%d SP:%p\n",
323 pcbsp
->sp_cache
[dir
].cachesp
->refcnt
,
324 pcbsp
->sp_cache
[dir
].cachesp
));
325 return pcbsp
->sp_cache
[dir
].cachesp
;
329 ipsec_fillpcbcache(struct inpcbpolicy
*pcbsp
, struct mbuf
*m
,
330 struct secpolicy
*sp
, int dir
)
334 case IPSEC_DIR_INBOUND
:
335 case IPSEC_DIR_OUTBOUND
:
341 if (dir
>= sizeof(pcbsp
->sp_cache
)/sizeof(pcbsp
->sp_cache
[0]))
342 panic("dir too big in ipsec_fillpcbcache");
345 if (pcbsp
->sp_cache
[dir
].cachesp
)
346 KEY_FREESP(&pcbsp
->sp_cache
[dir
].cachesp
);
347 pcbsp
->sp_cache
[dir
].cachesp
= NULL
;
348 pcbsp
->sp_cache
[dir
].cachehint
= IPSEC_PCBHINT_MAYBE
;
349 if (ipsec_setspidx(m
, &pcbsp
->sp_cache
[dir
].cacheidx
, 1) != 0) {
352 pcbsp
->sp_cache
[dir
].cachesp
= sp
;
353 if (pcbsp
->sp_cache
[dir
].cachesp
) {
354 pcbsp
->sp_cache
[dir
].cachesp
->refcnt
++;
355 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
356 printf("DP ipsec_fillpcbcache cause refcnt++:%d SP:%p\n",
357 pcbsp
->sp_cache
[dir
].cachesp
->refcnt
,
358 pcbsp
->sp_cache
[dir
].cachesp
));
361 * If the PCB is connected, we can remember a hint to
362 * possibly short-circuit IPsec processing in other places.
364 if (pcbsp
->sp_cacheflags
& IPSEC_PCBSP_CONNECTED
) {
365 switch (pcbsp
->sp_cache
[dir
].cachesp
->policy
) {
366 case IPSEC_POLICY_NONE
:
367 case IPSEC_POLICY_BYPASS
:
368 pcbsp
->sp_cache
[dir
].cachehint
=
372 pcbsp
->sp_cache
[dir
].cachehint
=
377 pcbsp
->sp_cache
[dir
].cachegen
= ipsec_spdgen
;
383 ipsec_invalpcbcache(struct inpcbpolicy
*pcbsp
, int dir
)
387 for (i
= IPSEC_DIR_INBOUND
; i
<= IPSEC_DIR_OUTBOUND
; i
++) {
388 if (dir
!= IPSEC_DIR_ANY
&& i
!= dir
)
390 if (pcbsp
->sp_cache
[i
].cachesp
)
391 KEY_FREESP(&pcbsp
->sp_cache
[i
].cachesp
);
392 pcbsp
->sp_cache
[i
].cachesp
= NULL
;
393 pcbsp
->sp_cache
[i
].cachehint
= IPSEC_PCBHINT_MAYBE
;
394 pcbsp
->sp_cache
[i
].cachegen
= 0;
395 memset(&pcbsp
->sp_cache
[i
].cacheidx
, 0,
396 sizeof(pcbsp
->sp_cache
[i
].cacheidx
));
402 ipsec_pcbconn(struct inpcbpolicy
*pcbsp
)
405 pcbsp
->sp_cacheflags
|= IPSEC_PCBSP_CONNECTED
;
406 ipsec_invalpcbcache(pcbsp
, IPSEC_DIR_ANY
);
410 ipsec_pcbdisconn(struct inpcbpolicy
*pcbsp
)
413 pcbsp
->sp_cacheflags
&= ~IPSEC_PCBSP_CONNECTED
;
414 ipsec_invalpcbcache(pcbsp
, IPSEC_DIR_ANY
);
418 ipsec_invalpcbcacheall(void)
421 if (ipsec_spdgen
== UINT_MAX
)
426 #endif /* __NetBSD__ */
429 * Return a held reference to the default SP.
431 static struct secpolicy
*
432 key_allocsp_default(int af
, const char* where
, int tag
)
434 struct secpolicy
*sp
;
436 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
437 printf("DP key_allocsp_default from %s:%u\n", where
, tag
));
441 sp
= &ip4_def_policy
;
445 sp
= &ip6_def_policy
;
449 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
450 printf("key_allocsp_default : unexpected protocol family %u\n",
455 if (sp
->policy
!= IPSEC_POLICY_DISCARD
&&
456 sp
->policy
!= IPSEC_POLICY_NONE
) {
457 ipseclog((LOG_INFO
, "fixed system default policy: %d->%d\n",
458 sp
->policy
, IPSEC_POLICY_NONE
));
459 sp
->policy
= IPSEC_POLICY_NONE
;
463 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
464 printf("DP key_allocsp_default returns SP:%p (%u)\n",
468 #define KEY_ALLOCSP_DEFAULT(af) \
469 key_allocsp_default((af),__FILE__, __LINE__)
472 * For OUTBOUND packet having a socket. Searching SPD for packet,
473 * and return a pointer to SP.
474 * OUT: NULL: no apropreate SP found, the following value is set to error.
476 * EACCES : discard packet.
477 * ENOENT : ipsec_acquire() in progress, maybe.
478 * others : error occurred.
479 * others: a pointer to SP
481 * NOTE: IPv6 mapped address concern is implemented here.
484 ipsec_getpolicy(struct tdb_ident
*tdbi
, u_int dir
)
486 struct secpolicy
*sp
;
488 IPSEC_ASSERT(tdbi
!= NULL
, ("ipsec_getpolicy: null tdbi"));
489 IPSEC_ASSERT(dir
== IPSEC_DIR_INBOUND
|| dir
== IPSEC_DIR_OUTBOUND
,
490 ("ipsec_getpolicy: invalid direction %u", dir
));
492 sp
= KEY_ALLOCSP2(tdbi
->spi
, &tdbi
->dst
, tdbi
->proto
, dir
);
493 if (sp
== NULL
) /*XXX????*/
494 sp
= KEY_ALLOCSP_DEFAULT(tdbi
->dst
.sa
.sa_family
);
495 IPSEC_ASSERT(sp
!= NULL
, ("ipsec_getpolicy: null SP"));
500 * For OUTBOUND packet having a socket. Searching SPD for packet,
501 * and return a pointer to SP.
502 * OUT: NULL: no apropreate SP found, the following value is set to error.
504 * EACCES : discard packet.
505 * ENOENT : ipsec_acquire() in progress, maybe.
506 * others : error occurred.
507 * others: a pointer to SP
509 * NOTE: IPv6 mapped address concern is implemented here.
511 static struct secpolicy
*
512 ipsec_getpolicybysock(struct mbuf
*m
, u_int dir
, PCB_T
*inp
, int *error
)
514 struct inpcbpolicy
*pcbsp
= NULL
;
515 struct secpolicy
*currsp
= NULL
; /* policy on socket */
516 struct secpolicy
*sp
;
519 IPSEC_ASSERT(m
!= NULL
, ("ipsec_getpolicybysock: null mbuf"));
520 IPSEC_ASSERT(inp
!= NULL
, ("ipsec_getpolicybysock: null inpcb"));
521 IPSEC_ASSERT(error
!= NULL
, ("ipsec_getpolicybysock: null error"));
522 IPSEC_ASSERT(dir
== IPSEC_DIR_INBOUND
|| dir
== IPSEC_DIR_OUTBOUND
,
523 ("ipsec_getpolicybysock: invalid direction %u", dir
));
525 IPSEC_ASSERT(PCB_SOCKET(inp
) != NULL
,
526 ("ipsec_getppolicybysock: null socket\n"));
528 /* XXX FIXME inpcb/in6pcb vs socket*/
529 af
= PCB_FAMILY(inp
);
530 IPSEC_ASSERT(af
== AF_INET
|| af
== AF_INET6
,
531 ("ipsec_getpolicybysock: unexpected protocol family %u", af
));
534 IPSEC_ASSERT(inp
->inph_sp
!= NULL
, ("null PCB policy cache"));
535 /* If we have a cached entry, and if it is still valid, use it. */
536 IPSEC_STATINC(IPSEC_STAT_SPDCACHELOOKUP
);
537 currsp
= ipsec_checkpcbcache(m
, /*inpcb_hdr*/inp
->inph_sp
, dir
);
542 IPSEC_STATINC(IPSEC_STAT_SPDCACHEMISS
);
543 #endif /* __NetBSD__ */
547 struct inpcb
*in4p
= PCB_TO_IN4PCB(inp
);
548 /* set spidx in pcb */
549 *error
= ipsec4_setspidx_inpcb(m
, in4p
);
550 pcbsp
= in4p
->inp_sp
;
556 struct in6pcb
*in6p
= PCB_TO_IN6PCB(inp
);
557 /* set spidx in pcb */
558 *error
= ipsec6_setspidx_in6pcb(m
, in6p
);
559 pcbsp
= in6p
->in6p_sp
;
564 *error
= EPFNOSUPPORT
;
570 IPSEC_ASSERT(pcbsp
!= NULL
, ("ipsec_getpolicybysock: null pcbsp"));
572 case IPSEC_DIR_INBOUND
:
573 currsp
= pcbsp
->sp_in
;
575 case IPSEC_DIR_OUTBOUND
:
576 currsp
= pcbsp
->sp_out
;
579 IPSEC_ASSERT(currsp
!= NULL
, ("ipsec_getpolicybysock: null currsp"));
581 if (pcbsp
->priv
) { /* when privilieged socket */
582 switch (currsp
->policy
) {
583 case IPSEC_POLICY_BYPASS
:
584 case IPSEC_POLICY_IPSEC
:
589 case IPSEC_POLICY_ENTRUST
:
590 /* look for a policy in SPD */
591 sp
= KEY_ALLOCSP(&currsp
->spidx
, dir
);
592 if (sp
== NULL
) /* no SP found */
593 sp
= KEY_ALLOCSP_DEFAULT(af
);
597 ipseclog((LOG_ERR
, "ipsec_getpolicybysock: "
598 "Invalid policy for PCB %d\n", currsp
->policy
));
602 } else { /* unpriv, SPD has policy */
603 sp
= KEY_ALLOCSP(&currsp
->spidx
, dir
);
604 if (sp
== NULL
) { /* no SP found */
605 switch (currsp
->policy
) {
606 case IPSEC_POLICY_BYPASS
:
607 ipseclog((LOG_ERR
, "ipsec_getpolicybysock: "
608 "Illegal policy for non-priviliged defined %d\n",
613 case IPSEC_POLICY_ENTRUST
:
614 sp
= KEY_ALLOCSP_DEFAULT(af
);
617 case IPSEC_POLICY_IPSEC
:
623 ipseclog((LOG_ERR
, "ipsec_getpolicybysock: "
624 "Invalid policy for PCB %d\n", currsp
->policy
));
630 IPSEC_ASSERT(sp
!= NULL
,
631 ("ipsec_getpolicybysock: null SP (priv %u policy %u",
632 pcbsp
->priv
, currsp
->policy
));
633 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
634 printf("DP ipsec_getpolicybysock (priv %u policy %u) allocates "
635 "SP:%p (refcnt %u)\n", pcbsp
->priv
, currsp
->policy
,
638 ipsec_fillpcbcache(pcbsp
, m
, sp
, dir
);
639 #endif /* __NetBSD__ */
644 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
645 * and return a pointer to SP.
646 * OUT: positive: a pointer to the entry for security policy leaf matched.
647 * NULL: no apropreate SP found, the following value is set to error.
649 * EACCES : discard packet.
650 * ENOENT : ipsec_acquire() in progress, maybe.
651 * others : error occurred.
654 ipsec_getpolicybyaddr(struct mbuf
*m
, u_int dir
, int flag
, int *error
)
656 struct secpolicyindex spidx
;
657 struct secpolicy
*sp
;
659 IPSEC_ASSERT(m
!= NULL
, ("ipsec_getpolicybyaddr: null mbuf"));
660 IPSEC_ASSERT(error
!= NULL
, ("ipsec_getpolicybyaddr: null error"));
661 IPSEC_ASSERT(dir
== IPSEC_DIR_INBOUND
|| dir
== IPSEC_DIR_OUTBOUND
,
662 ("ipsec4_getpolicybaddr: invalid direction %u", dir
));
666 /* Make an index to look for a policy. */
667 *error
= ipsec_setspidx(m
, &spidx
, (flag
& IP_FORWARDING
) ? 0 : 1);
669 DPRINTF(("ipsec_getpolicybyaddr: setpidx failed,"
670 " dir %u flag %u\n", dir
, flag
));
671 memset(&spidx
, 0, sizeof (spidx
));
677 if (key_havesp(dir
)) {
678 sp
= KEY_ALLOCSP(&spidx
, dir
);
681 if (sp
== NULL
) /* no SP found, use system default */
682 sp
= KEY_ALLOCSP_DEFAULT(spidx
.dst
.sa
.sa_family
);
683 IPSEC_ASSERT(sp
!= NULL
, ("ipsec_getpolicybyaddr: null SP"));
688 ipsec4_checkpolicy(struct mbuf
*m
, u_int dir
, u_int flag
, int *error
,
691 struct secpolicy
*sp
;
696 /* XXX KAME IPv6 calls us with non-null inp but bogus inp_socket? */
697 if (inp
== NULL
|| inp
->inp_socket
== NULL
) {
698 sp
= ipsec_getpolicybyaddr(m
, dir
, flag
, error
);
700 sp
= ipsec_getpolicybysock(m
, dir
, IN4PCB_TO_PCB(inp
), error
);
702 IPSEC_ASSERT(*error
!= 0,
703 ("ipsec4_checkpolicy: getpolicy failed w/o error"));
704 IPSEC_STATINC(IPSEC_STAT_OUT_INVAL
);
707 IPSEC_ASSERT(*error
== 0,
708 ("ipsec4_checkpolicy: sp w/ error set to %u", *error
));
709 switch (sp
->policy
) {
710 case IPSEC_POLICY_ENTRUST
:
712 printf("ipsec4_checkpolicy: invalid policy %u\n", sp
->policy
);
714 case IPSEC_POLICY_DISCARD
:
715 IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO
);
716 *error
= -EINVAL
; /* packet is discarded by caller */
718 case IPSEC_POLICY_BYPASS
:
719 case IPSEC_POLICY_NONE
:
721 sp
= NULL
; /* NB: force NULL result */
723 case IPSEC_POLICY_IPSEC
:
724 if (sp
->req
== NULL
) /* acquire an SA */
725 *error
= key_spdacquire(sp
);
731 DPRINTF(("%s: done, error %d\n", __func__
, *error
));
738 ipsec6_checkpolicy(struct mbuf
*m
, u_int dir
, u_int flag
, int *error
,
741 struct secpolicy
*sp
;
746 /* XXX KAME IPv6 calls us with non-null inp but bogus inp_socket? */
747 if (in6p
== NULL
|| in6p
->in6p_socket
== NULL
) {
748 sp
= ipsec_getpolicybyaddr(m
, dir
, flag
, error
);
750 sp
= ipsec_getpolicybysock(m
, dir
, IN6PCB_TO_PCB(in6p
), error
);
752 IPSEC_ASSERT(*error
!= 0,
753 ("ipsec6_checkpolicy: getpolicy failed w/o error"));
754 IPSEC_STATINC(IPSEC_STAT_OUT_INVAL
);
757 IPSEC_ASSERT(*error
== 0,
758 ("ipsec6_checkpolicy: sp w/ error set to %u", *error
));
759 switch (sp
->policy
) {
760 case IPSEC_POLICY_ENTRUST
:
762 printf("ipsec6_checkpolicy: invalid policy %u\n", sp
->policy
);
764 case IPSEC_POLICY_DISCARD
:
765 IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO
);
766 *error
= -EINVAL
; /* packet is discarded by caller */
768 case IPSEC_POLICY_BYPASS
:
769 case IPSEC_POLICY_NONE
:
771 sp
= NULL
; /* NB: force NULL result */
773 case IPSEC_POLICY_IPSEC
:
774 if (sp
->req
== NULL
) /* acquire an SA */
775 *error
= key_spdacquire(sp
);
781 DPRINTF(("%s: done, error %d\n", __func__
, *error
));
788 ipsec4_setspidx_inpcb(struct mbuf
*m
,struct inpcb
*pcb
)
792 IPSEC_ASSERT(pcb
!= NULL
, ("ipsec4_setspidx_inpcb: null pcb"));
793 IPSEC_ASSERT(pcb
->inp_sp
!= NULL
, ("ipsec4_setspidx_inpcb: null inp_sp"));
794 IPSEC_ASSERT(pcb
->inp_sp
->sp_out
!= NULL
&& pcb
->inp_sp
->sp_in
!= NULL
,
795 ("ipsec4_setspidx_inpcb: null sp_in || sp_out"));
797 error
= ipsec_setspidx(m
, &pcb
->inp_sp
->sp_in
->spidx
, 1);
799 pcb
->inp_sp
->sp_in
->spidx
.dir
= IPSEC_DIR_INBOUND
;
800 pcb
->inp_sp
->sp_out
->spidx
= pcb
->inp_sp
->sp_in
->spidx
;
801 pcb
->inp_sp
->sp_out
->spidx
.dir
= IPSEC_DIR_OUTBOUND
;
803 memset(&pcb
->inp_sp
->sp_in
->spidx
, 0,
804 sizeof (pcb
->inp_sp
->sp_in
->spidx
));
805 memset(&pcb
->inp_sp
->sp_out
->spidx
, 0,
806 sizeof (pcb
->inp_sp
->sp_in
->spidx
));
813 ipsec6_setspidx_in6pcb(struct mbuf
*m
, struct in6pcb
*pcb
)
815 struct secpolicyindex
*spidx
;
818 IPSEC_ASSERT(pcb
!= NULL
, ("ipsec6_setspidx_in6pcb: null pcb"));
819 IPSEC_ASSERT(pcb
->in6p_sp
!= NULL
, ("ipsec6_setspidx_in6pcb: null inp_sp"));
820 IPSEC_ASSERT(pcb
->in6p_sp
->sp_out
!= NULL
&& pcb
->in6p_sp
->sp_in
!= NULL
,
821 ("ipsec6_setspidx_in6pcb: null sp_in || sp_out"));
823 memset(&pcb
->in6p_sp
->sp_in
->spidx
, 0, sizeof(*spidx
));
824 memset(&pcb
->in6p_sp
->sp_out
->spidx
, 0, sizeof(*spidx
));
826 spidx
= &pcb
->in6p_sp
->sp_in
->spidx
;
827 error
= ipsec_setspidx(m
, spidx
, 1);
830 spidx
->dir
= IPSEC_DIR_INBOUND
;
832 spidx
= &pcb
->in6p_sp
->sp_out
->spidx
;
833 error
= ipsec_setspidx(m
, spidx
, 1);
836 spidx
->dir
= IPSEC_DIR_OUTBOUND
;
841 memset(&pcb
->in6p_sp
->sp_in
->spidx
, 0, sizeof(*spidx
));
842 memset(&pcb
->in6p_sp
->sp_out
->spidx
, 0, sizeof(*spidx
));
848 * configure security policy index (src/dst/proto/sport/dport)
849 * by looking at the content of mbuf.
850 * the caller is responsible for error recovery (like clearing up spidx).
853 ipsec_setspidx(struct mbuf
*m
, struct secpolicyindex
*spidx
, int needport
)
855 struct ip
*ip
= NULL
;
862 IPSEC_ASSERT(m
!= NULL
, ("ipsec_setspidx: null mbuf"));
865 * validate m->m_pkthdr.len. we see incorrect length if we
866 * mistakenly call this function with inconsistent mbuf chain
867 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
870 for (n
= m
; n
; n
= n
->m_next
)
872 if (m
->m_pkthdr
.len
!= len
) {
873 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
874 printf("ipsec_setspidx: "
875 "total of m_len(%d) != pkthdr.len(%d), "
877 len
, m
->m_pkthdr
.len
));
881 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
882 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
883 printf("ipsec_setspidx: "
884 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
889 if (m
->m_len
>= sizeof(*ip
))
890 ip
= mtod(m
, struct ip
*);
892 m_copydata(m
, 0, sizeof(ipbuf
), &ipbuf
);
898 error
= ipsec4_setspidx_ipaddr(m
, spidx
);
901 ipsec4_get_ulp(m
, spidx
, needport
);
905 if (m
->m_pkthdr
.len
< sizeof(struct ip6_hdr
)) {
906 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
907 printf("ipsec_setspidx: "
908 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
909 "ignored.\n", m
->m_pkthdr
.len
));
912 error
= ipsec6_setspidx_ipaddr(m
, spidx
);
915 ipsec6_get_ulp(m
, spidx
, needport
);
919 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
920 printf("ipsec_setspidx: "
921 "unknown IP version %u, ignored.\n", v
));
927 ipsec4_get_ulp(struct mbuf
*m
, struct secpolicyindex
*spidx
, int needport
)
933 IPSEC_ASSERT(m
!= NULL
, ("ipsec4_get_ulp: null mbuf"));
934 IPSEC_ASSERT(m
->m_pkthdr
.len
>= sizeof(struct ip
),
935 ("ipsec4_get_ulp: packet too short"));
937 /* NB: ip_input() flips it into host endian XXX need more checking */
938 if (m
->m_len
>= sizeof(struct ip
)) {
939 struct ip
*ip
= mtod(m
, struct ip
*);
940 if (ip
->ip_off
& IP_OFF_CONVERT(IP_MF
| IP_OFFMASK
))
942 off
= ip
->ip_hl
<< 2;
947 m_copydata(m
, 0, sizeof (struct ip
), &ih
);
948 if (ih
.ip_off
& IP_OFF_CONVERT(IP_MF
| IP_OFFMASK
))
954 while (off
< m
->m_pkthdr
.len
) {
962 spidx
->ul_proto
= nxt
;
965 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
967 m_copydata(m
, off
, sizeof (th
), &th
);
968 spidx
->src
.sin
.sin_port
= th
.th_sport
;
969 spidx
->dst
.sin
.sin_port
= th
.th_dport
;
972 spidx
->ul_proto
= nxt
;
975 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
977 m_copydata(m
, off
, sizeof (uh
), &uh
);
978 spidx
->src
.sin
.sin_port
= uh
.uh_sport
;
979 spidx
->dst
.sin
.sin_port
= uh
.uh_dport
;
982 if (m
->m_pkthdr
.len
> off
+ sizeof(ip6e
))
984 /* XXX sigh, this works but is totally bogus */
985 m_copydata(m
, off
, sizeof(ip6e
), &ip6e
);
986 off
+= (ip6e
.ip6e_len
+ 2) << 2;
990 spidx
->ul_proto
= nxt
;
991 if (off
+ sizeof(struct icmp
) > m
->m_pkthdr
.len
)
993 m_copydata(m
, off
, sizeof(icmph
), &icmph
);
994 ((struct sockaddr_in
*)&spidx
->src
)->sin_port
=
995 htons((uint16_t)icmph
.icmp_type
);
996 ((struct sockaddr_in
*)&spidx
->dst
)->sin_port
=
997 htons((uint16_t)icmph
.icmp_code
);
1000 /* XXX intermediate headers??? */
1001 spidx
->ul_proto
= nxt
;
1006 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
1008 spidx
->src
.sin
.sin_port
= IPSEC_PORT_ANY
;
1009 spidx
->dst
.sin
.sin_port
= IPSEC_PORT_ANY
;
1012 /* assumes that m is sane */
1014 ipsec4_setspidx_ipaddr(struct mbuf
*m
, struct secpolicyindex
*spidx
)
1016 static const struct sockaddr_in
template = {
1017 sizeof (struct sockaddr_in
),
1019 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
1022 spidx
->src
.sin
= template;
1023 spidx
->dst
.sin
= template;
1025 if (m
->m_len
< sizeof (struct ip
)) {
1026 m_copydata(m
, offsetof(struct ip
, ip_src
),
1027 sizeof (struct in_addr
),
1028 &spidx
->src
.sin
.sin_addr
);
1029 m_copydata(m
, offsetof(struct ip
, ip_dst
),
1030 sizeof (struct in_addr
),
1031 &spidx
->dst
.sin
.sin_addr
);
1033 struct ip
*ip
= mtod(m
, struct ip
*);
1034 spidx
->src
.sin
.sin_addr
= ip
->ip_src
;
1035 spidx
->dst
.sin
.sin_addr
= ip
->ip_dst
;
1038 spidx
->prefs
= sizeof(struct in_addr
) << 3;
1039 spidx
->prefd
= sizeof(struct in_addr
) << 3;
1046 ipsec6_get_ulp(struct mbuf
*m
, struct secpolicyindex
*spidx
,
1052 struct icmp6_hdr icmph
;
1056 panic("ipsec6_get_ulp: NULL pointer was passed");
1058 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1059 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m
));
1062 spidx
->ul_proto
= IPSEC_ULPROTO_ANY
;
1063 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= IPSEC_PORT_ANY
;
1064 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= IPSEC_PORT_ANY
;
1067 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
1068 if (off
< 0 || m
->m_pkthdr
.len
< off
)
1073 spidx
->ul_proto
= nxt
;
1076 if (off
+ sizeof(struct tcphdr
) > m
->m_pkthdr
.len
)
1078 m_copydata(m
, off
, sizeof(th
), &th
);
1079 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= th
.th_sport
;
1080 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= th
.th_dport
;
1083 spidx
->ul_proto
= nxt
;
1086 if (off
+ sizeof(struct udphdr
) > m
->m_pkthdr
.len
)
1088 m_copydata(m
, off
, sizeof(uh
), &uh
);
1089 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
= uh
.uh_sport
;
1090 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
= uh
.uh_dport
;
1092 case IPPROTO_ICMPV6
:
1093 spidx
->ul_proto
= nxt
;
1094 if (off
+ sizeof(struct icmp6_hdr
) > m
->m_pkthdr
.len
)
1096 m_copydata(m
, off
, sizeof(icmph
), &icmph
);
1097 ((struct sockaddr_in6
*)&spidx
->src
)->sin6_port
=
1098 htons((uint16_t)icmph
.icmp6_type
);
1099 ((struct sockaddr_in6
*)&spidx
->dst
)->sin6_port
=
1100 htons((uint16_t)icmph
.icmp6_code
);
1103 /* XXX intermediate headers??? */
1104 spidx
->ul_proto
= nxt
;
1109 /* assumes that m is sane */
1111 ipsec6_setspidx_ipaddr(struct mbuf
*m
, struct secpolicyindex
*spidx
)
1113 struct ip6_hdr
*ip6
= NULL
;
1114 struct ip6_hdr ip6buf
;
1115 struct sockaddr_in6
*sin6
;
1117 if (m
->m_len
>= sizeof(*ip6
))
1118 ip6
= mtod(m
, struct ip6_hdr
*);
1120 m_copydata(m
, 0, sizeof(ip6buf
), &ip6buf
);
1124 sin6
= (struct sockaddr_in6
*)&spidx
->src
;
1125 memset(sin6
, 0, sizeof(*sin6
));
1126 sin6
->sin6_family
= AF_INET6
;
1127 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1128 memcpy(&sin6
->sin6_addr
, &ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
1129 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1130 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1131 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_src
.s6_addr16
[1]);
1133 spidx
->prefs
= sizeof(struct in6_addr
) << 3;
1135 sin6
= (struct sockaddr_in6
*)&spidx
->dst
;
1136 memset(sin6
, 0, sizeof(*sin6
));
1137 sin6
->sin6_family
= AF_INET6
;
1138 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1139 memcpy(&sin6
->sin6_addr
, &ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
1140 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
1141 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1142 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
1144 spidx
->prefd
= sizeof(struct in6_addr
) << 3;
1151 ipsec_delpcbpolicy(struct inpcbpolicy
*p
)
1156 /* initialize policy in PCB */
1158 ipsec_init_policy(struct socket
*so
, struct inpcbpolicy
**pcb_sp
)
1160 struct inpcbpolicy
*new;
1163 if (so
== NULL
|| pcb_sp
== NULL
)
1164 panic("ipsec_init_policy: NULL pointer was passed");
1166 new = (struct inpcbpolicy
*) malloc(sizeof(struct inpcbpolicy
),
1167 M_SECA
, M_NOWAIT
|M_ZERO
);
1169 ipseclog((LOG_DEBUG
, "ipsec_init_policy: No more memory.\n"));
1173 if (IPSEC_PRIVILEGED_SO(so
))
1178 if ((new->sp_in
= KEY_NEWSP()) == NULL
) {
1179 ipsec_delpcbpolicy(new);
1182 new->sp_in
->state
= IPSEC_SPSTATE_ALIVE
;
1183 new->sp_in
->policy
= IPSEC_POLICY_ENTRUST
;
1185 if ((new->sp_out
= KEY_NEWSP()) == NULL
) {
1186 KEY_FREESP(&new->sp_in
);
1187 ipsec_delpcbpolicy(new);
1190 new->sp_out
->state
= IPSEC_SPSTATE_ALIVE
;
1191 new->sp_out
->policy
= IPSEC_POLICY_ENTRUST
;
1198 /* copy old ipsec policy into new */
1200 ipsec_copy_policy(struct inpcbpolicy
*old
, struct inpcbpolicy
*new)
1202 struct secpolicy
*sp
;
1204 sp
= ipsec_deepcopy_policy(old
->sp_in
);
1206 KEY_FREESP(&new->sp_in
);
1211 sp
= ipsec_deepcopy_policy(old
->sp_out
);
1213 KEY_FREESP(&new->sp_out
);
1218 new->priv
= old
->priv
;
1223 /* deep-copy a policy in PCB */
1224 static struct secpolicy
*
1225 ipsec_deepcopy_policy(struct secpolicy
*src
)
1227 struct ipsecrequest
*newchain
= NULL
;
1228 struct ipsecrequest
*p
;
1229 struct ipsecrequest
**q
;
1230 struct ipsecrequest
*r
;
1231 struct secpolicy
*dst
;
1240 * deep-copy IPsec request chain. This is required since struct
1241 * ipsecrequest is not reference counted.
1244 for (p
= src
->req
; p
; p
= p
->next
) {
1245 *q
= (struct ipsecrequest
*)malloc(sizeof(struct ipsecrequest
),
1249 memset(*q
, 0, sizeof(**q
));
1252 (*q
)->saidx
.proto
= p
->saidx
.proto
;
1253 (*q
)->saidx
.mode
= p
->saidx
.mode
;
1254 (*q
)->level
= p
->level
;
1255 (*q
)->saidx
.reqid
= p
->saidx
.reqid
;
1257 memcpy(&(*q
)->saidx
.src
, &p
->saidx
.src
, sizeof((*q
)->saidx
.src
));
1258 memcpy(&(*q
)->saidx
.dst
, &p
->saidx
.dst
, sizeof((*q
)->saidx
.dst
));
1266 dst
->req
= newchain
;
1267 dst
->state
= src
->state
;
1268 dst
->policy
= src
->policy
;
1269 /* do not touch the refcnt fields */
1274 for (p
= newchain
; p
; p
= r
) {
1282 /* set policy and ipsec request if present. */
1285 struct secpolicy
**pcb_sp
,
1292 struct sadb_x_policy
*xpl
;
1293 struct secpolicy
*newsp
= NULL
;
1297 if (pcb_sp
== NULL
|| *pcb_sp
== NULL
|| request
== NULL
)
1299 if (len
< sizeof(*xpl
))
1301 xpl
= (struct sadb_x_policy
*)request
;
1303 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1304 printf("ipsec_set_policy: passed policy\n");
1305 kdebug_sadb_x_policy((struct sadb_ext
*)xpl
));
1307 /* check policy type */
1308 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1309 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
1310 || xpl
->sadb_x_policy_type
== IPSEC_POLICY_NONE
)
1313 /* check privileged socket */
1314 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_BYPASS
) {
1315 error
= kauth_authorize_generic(cred
, KAUTH_GENERIC_ISSUSER
,
1321 /* allocation new SP entry */
1322 if ((newsp
= key_msg2sp(xpl
, len
, &error
)) == NULL
)
1325 newsp
->state
= IPSEC_SPSTATE_ALIVE
;
1327 /* clear old SP and set new SP */
1330 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1331 printf("ipsec_set_policy: new policy\n");
1332 kdebug_secpolicy(newsp
));
1338 ipsec_get_policy(struct secpolicy
*pcb_sp
, struct mbuf
**mp
)
1342 if (pcb_sp
== NULL
|| mp
== NULL
)
1345 *mp
= key_sp2msg(pcb_sp
);
1347 ipseclog((LOG_DEBUG
, "ipsec_get_policy: No more memory.\n"));
1351 (*mp
)->m_type
= MT_DATA
;
1352 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1353 printf("ipsec_get_policy:\n");
1360 ipsec4_set_policy(struct inpcb
*inp
, int optname
,void *request
,
1361 size_t len
, kauth_cred_t cred
)
1363 struct sadb_x_policy
*xpl
;
1364 struct secpolicy
**pcb_sp
;
1367 if (inp
== NULL
|| request
== NULL
)
1369 if (len
< sizeof(*xpl
))
1371 xpl
= (struct sadb_x_policy
*)request
;
1373 IPSEC_ASSERT(inp
->inp_sp
!= NULL
,
1374 ("ipsec4_set_policy(): null inp->in_sp"));
1376 /* select direction */
1377 switch (xpl
->sadb_x_policy_dir
) {
1378 case IPSEC_DIR_INBOUND
:
1379 pcb_sp
= &inp
->inp_sp
->sp_in
;
1381 case IPSEC_DIR_OUTBOUND
:
1382 pcb_sp
= &inp
->inp_sp
->sp_out
;
1385 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1386 xpl
->sadb_x_policy_dir
));
1390 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, cred
);
1394 ipsec4_get_policy(struct inpcb
*inp
, void *request
, size_t len
,
1397 struct sadb_x_policy
*xpl
;
1398 struct secpolicy
*pcb_sp
;
1401 if (inp
== NULL
|| request
== NULL
|| mp
== NULL
)
1403 IPSEC_ASSERT(inp
->inp_sp
!= NULL
, ("ipsec4_get_policy: null inp_sp"));
1404 if (len
< sizeof(*xpl
))
1406 xpl
= (struct sadb_x_policy
*)request
;
1408 /* select direction */
1409 switch (xpl
->sadb_x_policy_dir
) {
1410 case IPSEC_DIR_INBOUND
:
1411 pcb_sp
= inp
->inp_sp
->sp_in
;
1413 case IPSEC_DIR_OUTBOUND
:
1414 pcb_sp
= inp
->inp_sp
->sp_out
;
1417 ipseclog((LOG_ERR
, "ipsec4_set_policy: invalid direction=%u\n",
1418 xpl
->sadb_x_policy_dir
));
1422 return ipsec_get_policy(pcb_sp
, mp
);
1425 /* delete policy in PCB */
1427 ipsec4_delete_pcbpolicy(struct inpcb
*inp
)
1429 IPSEC_ASSERT(inp
!= NULL
, ("ipsec4_delete_pcbpolicy: null inp"));
1431 if (inp
->inp_sp
== NULL
)
1434 if (inp
->inp_sp
->sp_in
!= NULL
)
1435 KEY_FREESP(&inp
->inp_sp
->sp_in
);
1437 if (inp
->inp_sp
->sp_out
!= NULL
)
1438 KEY_FREESP(&inp
->inp_sp
->sp_out
);
1440 ipsec_delpcbpolicy(inp
->inp_sp
);
1448 ipsec6_set_policy(struct in6pcb
*in6p
, int optname
, void *request
,
1449 size_t len
, kauth_cred_t cred
)
1451 struct sadb_x_policy
*xpl
;
1452 struct secpolicy
**pcb_sp
;
1455 if (in6p
== NULL
|| request
== NULL
)
1457 if (len
< sizeof(*xpl
))
1459 xpl
= (struct sadb_x_policy
*)request
;
1461 /* select direction */
1462 switch (xpl
->sadb_x_policy_dir
) {
1463 case IPSEC_DIR_INBOUND
:
1464 pcb_sp
= &in6p
->in6p_sp
->sp_in
;
1466 case IPSEC_DIR_OUTBOUND
:
1467 pcb_sp
= &in6p
->in6p_sp
->sp_out
;
1470 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1471 xpl
->sadb_x_policy_dir
));
1475 return ipsec_set_policy(pcb_sp
, optname
, request
, len
, cred
);
1479 ipsec6_get_policy(struct in6pcb
*in6p
, void *request
, size_t len
,
1482 struct sadb_x_policy
*xpl
;
1483 struct secpolicy
*pcb_sp
;
1486 if (in6p
== NULL
|| request
== NULL
|| mp
== NULL
)
1488 IPSEC_ASSERT(in6p
->in6p_sp
!= NULL
, ("ipsec6_get_policy: null in6p_sp"));
1489 if (len
< sizeof(*xpl
))
1491 xpl
= (struct sadb_x_policy
*)request
;
1493 /* select direction */
1494 switch (xpl
->sadb_x_policy_dir
) {
1495 case IPSEC_DIR_INBOUND
:
1496 pcb_sp
= in6p
->in6p_sp
->sp_in
;
1498 case IPSEC_DIR_OUTBOUND
:
1499 pcb_sp
= in6p
->in6p_sp
->sp_out
;
1502 ipseclog((LOG_ERR
, "ipsec6_set_policy: invalid direction=%u\n",
1503 xpl
->sadb_x_policy_dir
));
1507 return ipsec_get_policy(pcb_sp
, mp
);
1511 ipsec6_delete_pcbpolicy(struct in6pcb
*in6p
)
1513 IPSEC_ASSERT(in6p
!= NULL
, ("ipsec6_delete_pcbpolicy: null in6p"));
1515 if (in6p
->in6p_sp
== NULL
)
1518 if (in6p
->in6p_sp
->sp_in
!= NULL
)
1519 KEY_FREESP(&in6p
->in6p_sp
->sp_in
);
1521 if (in6p
->in6p_sp
->sp_out
!= NULL
)
1522 KEY_FREESP(&in6p
->in6p_sp
->sp_out
);
1524 ipsec_delpcbpolicy(in6p
->in6p_sp
);
1525 in6p
->in6p_sp
= NULL
;
1532 * return current level.
1533 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1536 ipsec_get_reqlevel(struct ipsecrequest
*isr
)
1539 u_int esp_trans_deflev
, esp_net_deflev
;
1540 u_int ah_trans_deflev
, ah_net_deflev
;
1542 IPSEC_ASSERT(isr
!= NULL
&& isr
->sp
!= NULL
,
1543 ("ipsec_get_reqlevel: null argument"));
1544 IPSEC_ASSERT(isr
->sp
->spidx
.src
.sa
.sa_family
== isr
->sp
->spidx
.dst
.sa
.sa_family
,
1545 ("ipsec_get_reqlevel: af family mismatch, src %u, dst %u",
1546 isr
->sp
->spidx
.src
.sa
.sa_family
,
1547 isr
->sp
->spidx
.dst
.sa
.sa_family
));
1549 /* XXX note that we have ipseclog() expanded here - code sync issue */
1550 #define IPSEC_CHECK_DEFAULT(lev) \
1551 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1552 && (lev) != IPSEC_LEVEL_UNIQUE) \
1554 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1555 (lev), IPSEC_LEVEL_REQUIRE) \
1557 (lev) = IPSEC_LEVEL_REQUIRE, \
1561 /* set default level */
1562 switch (((struct sockaddr
*)&isr
->sp
->spidx
.src
)->sa_family
) {
1565 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev
);
1566 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev
);
1567 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev
);
1568 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev
);
1573 esp_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev
);
1574 esp_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev
);
1575 ah_trans_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev
);
1576 ah_net_deflev
= IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev
);
1580 panic("key_get_reqlevel: unknown af %u",
1581 isr
->sp
->spidx
.src
.sa
.sa_family
);
1584 #undef IPSEC_CHECK_DEFAULT
1587 switch (isr
->level
) {
1588 case IPSEC_LEVEL_DEFAULT
:
1589 switch (isr
->saidx
.proto
) {
1591 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1592 level
= esp_net_deflev
;
1594 level
= esp_trans_deflev
;
1597 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
)
1598 level
= ah_net_deflev
;
1600 level
= ah_trans_deflev
;
1602 case IPPROTO_IPCOMP
:
1604 * we don't really care, as IPcomp document says that
1605 * we shouldn't compress small packets
1607 level
= IPSEC_LEVEL_USE
;
1610 panic("ipsec_get_reqlevel: Illegal protocol defined %u",
1615 case IPSEC_LEVEL_USE
:
1616 case IPSEC_LEVEL_REQUIRE
:
1619 case IPSEC_LEVEL_UNIQUE
:
1620 level
= IPSEC_LEVEL_REQUIRE
;
1624 panic("ipsec_get_reqlevel: Illegal IPsec level %u",
1632 * Check security policy requirements against the actual
1633 * packet contents. Return one if the packet should be
1634 * reject as "invalid"; otherwiser return zero to have the
1635 * packet treated as "valid".
1642 ipsec_in_reject(struct secpolicy
*sp
, struct mbuf
*m
)
1644 struct ipsecrequest
*isr
;
1647 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1648 printf("ipsec_in_reject: using SP\n");
1649 kdebug_secpolicy(sp
));
1652 switch (sp
->policy
) {
1653 case IPSEC_POLICY_DISCARD
:
1655 case IPSEC_POLICY_BYPASS
:
1656 case IPSEC_POLICY_NONE
:
1660 IPSEC_ASSERT(sp
->policy
== IPSEC_POLICY_IPSEC
,
1661 ("ipsec_in_reject: invalid policy %u", sp
->policy
));
1663 /* XXX should compare policy against ipsec header history */
1666 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1667 if (ipsec_get_reqlevel(isr
) != IPSEC_LEVEL_REQUIRE
)
1669 switch (isr
->saidx
.proto
) {
1671 if ((m
->m_flags
& M_DECRYPTED
) == 0) {
1672 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1673 printf("ipsec_in_reject: ESP m_flags:%x\n",
1680 isr
->sav
->tdb_authalgxform
!= NULL
&&
1681 (m
->m_flags
& M_AUTHIPDGM
) == 0) {
1682 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1683 printf("ipsec_in_reject: ESP/AH m_flags:%x\n",
1690 if ((m
->m_flags
& M_AUTHIPHDR
) == 0) {
1691 KEYDEBUG(KEYDEBUG_IPSEC_DUMP
,
1692 printf("ipsec_in_reject: AH m_flags:%x\n",
1697 case IPPROTO_IPCOMP
:
1699 * we don't really care, as IPcomp document
1700 * says that we shouldn't compress small
1701 * packets, IPComp policy should always be
1702 * treated as being in "use" level.
1707 return 0; /* valid */
1711 * Check AH/ESP integrity.
1712 * This function is called from tcp_input(), udp_input(),
1713 * and {ah,esp}4_input for tunnel mode
1716 ipsec4_in_reject(struct mbuf
*m
, struct inpcb
*inp
)
1718 struct secpolicy
*sp
;
1722 IPSEC_ASSERT(m
!= NULL
, ("ipsec4_in_reject_so: null mbuf"));
1724 /* get SP for this packet.
1725 * When we are called from ip_forward(), we call
1726 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1729 sp
= ipsec_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1731 sp
= ipsec_getpolicybysock(m
, IPSEC_DIR_INBOUND
,
1732 IN4PCB_TO_PCB(inp
), &error
);
1735 result
= ipsec_in_reject(sp
, m
);
1737 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO
);
1740 result
= 0; /* XXX should be panic ?
1741 * -> No, there may be error. */
1749 * Check AH/ESP integrity.
1750 * This function is called from tcp6_input(), udp6_input(),
1751 * and {ah,esp}6_input for tunnel mode
1754 ipsec6_in_reject(struct mbuf
*m
, struct in6pcb
*in6p
)
1756 struct secpolicy
*sp
= NULL
;
1762 return 0; /* XXX should be panic ? */
1764 /* get SP for this packet.
1765 * When we are called from ip_forward(), we call
1766 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1769 sp
= ipsec_getpolicybyaddr(m
, IPSEC_DIR_INBOUND
, IP_FORWARDING
, &error
);
1771 sp
= ipsec_getpolicybysock(m
, IPSEC_DIR_INBOUND
,
1772 IN6PCB_TO_PCB(in6p
),
1776 result
= ipsec_in_reject(sp
, m
);
1778 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO
);
1788 * compute the byte size to be occupied by IPsec header.
1789 * in case it is tunneled, it includes the size of outer IP header.
1790 * NOTE: SP passed is free in this function.
1793 ipsec_hdrsiz(struct secpolicy
*sp
)
1795 struct ipsecrequest
*isr
;
1798 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1799 printf("ipsec_hdrsiz: using SP\n");
1800 kdebug_secpolicy(sp
));
1802 switch (sp
->policy
) {
1803 case IPSEC_POLICY_DISCARD
:
1804 case IPSEC_POLICY_BYPASS
:
1805 case IPSEC_POLICY_NONE
:
1809 IPSEC_ASSERT(sp
->policy
== IPSEC_POLICY_IPSEC
,
1810 ("ipsec_hdrsiz: invalid policy %u", sp
->policy
));
1813 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
1816 switch (isr
->saidx
.proto
) {
1818 clen
= esp_hdrsiz(isr
->sav
);
1821 clen
= ah_hdrsiz(isr
->sav
);
1823 case IPPROTO_IPCOMP
:
1824 clen
= sizeof(struct ipcomp
);
1828 if (isr
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
1829 switch (isr
->saidx
.dst
.sa
.sa_family
) {
1831 clen
+= sizeof(struct ip
);
1835 clen
+= sizeof(struct ip6_hdr
);
1839 ipseclog((LOG_ERR
, "ipsec_hdrsiz: "
1840 "unknown AF %d in IPsec tunnel SA\n",
1841 ((struct sockaddr
*)&isr
->saidx
.dst
)->sa_family
));
1851 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1853 ipsec4_hdrsiz(struct mbuf
*m
, u_int dir
, struct inpcb
*inp
)
1855 struct secpolicy
*sp
;
1859 IPSEC_ASSERT(m
!= NULL
, ("ipsec4_hdrsiz: null mbuf"));
1860 IPSEC_ASSERT(inp
== NULL
|| inp
->inp_socket
!= NULL
,
1861 ("ipsec4_hdrsize: socket w/o inpcb"));
1863 /* get SP for this packet.
1864 * When we are called from ip_forward(), we call
1865 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1868 sp
= ipsec_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
1870 sp
= ipsec_getpolicybysock(m
, dir
,
1871 IN4PCB_TO_PCB(inp
), &error
);
1874 size
= ipsec_hdrsiz(sp
);
1875 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1876 printf("ipsec4_hdrsiz: size:%lu.\n",
1877 (unsigned long)size
));
1881 size
= 0; /* XXX should be panic ? */
1887 /* This function is called from ipsec6_hdrsize_tcp(),
1888 * and maybe from ip6_forward.()
1891 ipsec6_hdrsiz(struct mbuf
*m
, u_int dir
, struct in6pcb
*in6p
)
1893 struct secpolicy
*sp
;
1897 IPSEC_ASSERT(m
!= NULL
, ("ipsec6_hdrsiz: null mbuf"));
1898 IPSEC_ASSERT(in6p
== NULL
|| in6p
->in6p_socket
!= NULL
,
1899 ("ipsec6_hdrsize: socket w/o inpcb"));
1901 /* get SP for this packet */
1902 /* XXX Is it right to call with IP_FORWARDING. */
1904 sp
= ipsec_getpolicybyaddr(m
, dir
, IP_FORWARDING
, &error
);
1906 sp
= ipsec_getpolicybysock(m
, dir
,
1907 IN6PCB_TO_PCB(in6p
),
1912 size
= ipsec_hdrsiz(sp
);
1913 KEYDEBUG(KEYDEBUG_IPSEC_DATA
,
1914 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size
));
1922 * Check the variable replay window.
1923 * ipsec_chkreplay() performs replay check before ICV verification.
1924 * ipsec_updatereplay() updates replay bitmap. This must be called after
1925 * ICV verification (it also performs replay check, which is usually done
1927 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1929 * based on RFC 2401.
1932 ipsec_chkreplay(u_int32_t seq
, struct secasvar
*sav
)
1934 const struct secreplay
*replay
;
1937 u_int32_t wsizeb
; /* constant: bits of window size */
1938 int frlast
; /* constant: last frame */
1940 IPSEC_SPLASSERT_SOFTNET("ipsec_chkreplay");
1942 IPSEC_ASSERT(sav
!= NULL
, ("ipsec_chkreplay: Null SA"));
1943 IPSEC_ASSERT(sav
->replay
!= NULL
, ("ipsec_chkreplay: Null replay state"));
1945 replay
= sav
->replay
;
1947 if (replay
->wsize
== 0)
1948 return 1; /* no need to check replay. */
1951 frlast
= replay
->wsize
- 1;
1952 wsizeb
= replay
->wsize
<< 3;
1954 /* sequence number of 0 is invalid */
1958 /* first time is always okay */
1959 if (replay
->count
== 0)
1962 if (seq
> replay
->lastseq
) {
1963 /* larger sequences are okay */
1966 /* seq is equal or less than lastseq. */
1967 diff
= replay
->lastseq
- seq
;
1969 /* over range to check, i.e. too old or wrapped */
1973 fr
= frlast
- diff
/ 8;
1975 /* this packet already seen ? */
1976 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
1979 /* out of order but good */
1985 * check replay counter whether to update or not.
1990 ipsec_updatereplay(u_int32_t seq
, struct secasvar
*sav
)
1992 struct secreplay
*replay
;
1995 u_int32_t wsizeb
; /* constant: bits of window size */
1996 int frlast
; /* constant: last frame */
1998 IPSEC_SPLASSERT_SOFTNET("ipsec_updatereplay");
2000 IPSEC_ASSERT(sav
!= NULL
, ("ipsec_updatereplay: Null SA"));
2001 IPSEC_ASSERT(sav
->replay
!= NULL
, ("ipsec_updatereplay: Null replay state"));
2003 replay
= sav
->replay
;
2005 if (replay
->wsize
== 0)
2006 goto ok
; /* no need to check replay. */
2009 frlast
= replay
->wsize
- 1;
2010 wsizeb
= replay
->wsize
<< 3;
2012 /* sequence number of 0 is invalid */
2017 if (replay
->count
== 0) {
2018 replay
->lastseq
= seq
;
2019 memset(replay
->bitmap
, 0, replay
->wsize
);
2020 (replay
->bitmap
)[frlast
] = 1;
2024 if (seq
> replay
->lastseq
) {
2025 /* seq is larger than lastseq. */
2026 diff
= seq
- replay
->lastseq
;
2028 /* new larger sequence number */
2029 if (diff
< wsizeb
) {
2031 /* set bit for this packet */
2032 vshiftl(replay
->bitmap
, diff
, replay
->wsize
);
2033 (replay
->bitmap
)[frlast
] |= 1;
2035 /* this packet has a "way larger" */
2036 memset(replay
->bitmap
, 0, replay
->wsize
);
2037 (replay
->bitmap
)[frlast
] = 1;
2039 replay
->lastseq
= seq
;
2041 /* larger is good */
2043 /* seq is equal or less than lastseq. */
2044 diff
= replay
->lastseq
- seq
;
2046 /* over range to check, i.e. too old or wrapped */
2050 fr
= frlast
- diff
/ 8;
2052 /* this packet already seen ? */
2053 if ((replay
->bitmap
)[fr
] & (1 << (diff
% 8)))
2057 (replay
->bitmap
)[fr
] |= (1 << (diff
% 8));
2059 /* out of order but good */
2063 if (replay
->count
== ~0) {
2065 /* set overflow flag */
2068 /* don't increment, no more packets accepted */
2069 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0)
2072 ipseclog((LOG_WARNING
, "replay counter made %d cycle. %s\n",
2073 replay
->overflow
, ipsec_logsastr(sav
)));
2082 * shift variable length bunffer to left.
2083 * IN: bitmap: pointer to the buffer
2084 * nbit: the number of to shift.
2085 * wsize: buffer size (bytes).
2088 vshiftl(unsigned char *bitmap
, int nbit
, int wsize
)
2093 for (j
= 0; j
< nbit
; j
+= 8) {
2094 s
= (nbit
- j
< 8) ? (nbit
- j
): 8;
2096 for (i
= 1; i
< wsize
; i
++) {
2097 over
= (bitmap
[i
] >> (8 - s
));
2099 bitmap
[i
-1] |= over
;
2106 /* Return a printable string for the IPv4 address. */
2108 inet_ntoa4(struct in_addr ina
)
2110 static char buf
[4][4 * sizeof "123" + 4];
2111 unsigned char *ucp
= (unsigned char *) &ina
;
2115 snprintf(buf
[i
], sizeof(buf
[i
]), "%d.%d.%d.%d",
2116 ucp
[0] & 0xff, ucp
[1] & 0xff, ucp
[2] & 0xff, ucp
[3] & 0xff);
2120 /* Return a printable string for the address. */
2122 ipsec_address(union sockaddr_union
* sa
)
2124 switch (sa
->sa
.sa_family
) {
2127 return inet_ntoa4(sa
->sin
.sin_addr
);
2132 return ip6_sprintf(&sa
->sin6
.sin6_addr
);
2136 return "(unknown address family)";
2141 ipsec_logsastr(struct secasvar
*sav
)
2143 static char buf
[256];
2145 struct secasindex
*saidx
= &sav
->sah
->saidx
;
2147 IPSEC_ASSERT(saidx
->src
.sa
.sa_family
== saidx
->dst
.sa
.sa_family
,
2148 ("ipsec_logsastr: address family mismatch"));
2151 snprintf(buf
, sizeof(buf
), "SA(SPI=%u ", (u_int32_t
)ntohl(sav
->spi
));
2154 /* NB: only use ipsec_address on one address at a time */
2155 snprintf(p
, sizeof (buf
) - (p
- buf
), "src=%s ",
2156 ipsec_address(&saidx
->src
));
2159 snprintf(p
, sizeof (buf
) - (p
- buf
), "dst=%s)",
2160 ipsec_address(&saidx
->dst
));
2166 ipsec_dumpmbuf(struct mbuf
*m
)
2175 p
= mtod(m
, u_char
*);
2176 for (i
= 0; i
< m
->m_len
; i
++) {
2177 printf("%02x ", p
[i
]);
2179 if (totlen
% 16 == 0)
2184 if (totlen
% 16 != 0)
2191 ipsec6_check_policy(struct mbuf
* m
, const struct socket
* so
,
2192 int flags
, int * needipsecp
, int * errorp
)
2194 struct in6pcb
*in6p
= NULL
;
2195 struct secpolicy
*sp
= NULL
;
2200 if (so
!= NULL
&& so
->so_proto
->pr_domain
->dom_family
== AF_INET6
)
2201 in6p
= sotoin6pcb(so
);
2203 if (!ipsec_outdone(m
)) {
2206 IPSEC_PCB_SKIP_IPSEC(in6p
->in6p_sp
, IPSEC_DIR_OUTBOUND
)) {
2208 goto skippolicycheck
;
2210 sp
= ipsec6_checkpolicy(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
,in6p
);
2213 * There are four return cases:
2214 * sp != NULL apply IPsec policy
2215 * sp == NULL, error == 0 no IPsec handling needed
2216 * sp == NULL, error == -EINVAL discard packet w/o error
2217 * sp == NULL, error != 0 discard packet, report error
2223 * Caller must check the error return to see if it needs to discard
2234 *needipsecp
= needipsec
;
2241 /* XXX this stuff doesn't belong here... */
2243 static struct xformsw
* xforms
= NULL
;
2246 * Register a transform; typically at system startup.
2249 xform_register(struct xformsw
* xsp
)
2251 xsp
->xf_next
= xforms
;
2256 * Initialize transform support in an sav.
2259 xform_init(struct secasvar
*sav
, int xftype
)
2261 struct xformsw
*xsp
;
2263 if (sav
->tdb_xform
!= NULL
) /* previously initialized */
2265 for (xsp
= xforms
; xsp
; xsp
= xsp
->xf_next
)
2266 if (xsp
->xf_type
== xftype
)
2267 return (*xsp
->xf_init
)(sav
, xsp
);
2269 DPRINTF(("xform_init: no match for xform type %d\n", xftype
));
2275 * XXXJRT This should be done as a protosw init call.
2281 ipsecstat_percpu
= percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS
);
2283 printf("initializing IPsec...");
2288 #ifdef TCP_SIGNATURE
2289 tcpsignature_attach();
2293 #endif /* __NetBSD__ */