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.5 2004/02/14 21:15:31 dillon 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>
59 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/in_var.h>
66 #include <netinet/udp.h>
67 #include <netinet/udp_var.h>
68 #include <netinet/ip_ecn.h>
70 #include <netinet6/ip6_ecn.h>
72 #include <netinet/tcp.h>
73 #include <netinet/udp.h>
75 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
79 #include <netinet/in_pcb.h>
81 #include <netinet/icmp6.h>
84 #include <netinet6/ipsec.h>
86 #include <netinet6/ipsec6.h>
88 #include <netinet6/ah.h>
90 #include <netinet6/ah6.h>
93 #include <netinet6/esp.h>
95 #include <netinet6/esp6.h>
98 #include <netinet6/ipcomp.h>
100 #include <netinet6/ipcomp6.h>
102 #include <netproto/key/key.h>
103 #include <netproto/key/keydb.h>
104 #include <netproto/key/key_debug.h>
106 #include <net/net_osdep.h>
114 struct ipsecstat ipsecstat;
115 int ip4_ah_cleartos = 1;
116 int ip4_ah_offsetmask = 0; /* maybe IP_DF? */
117 int ip4_ipsec_dfbit = 0; /* DF bit on encap. 0: clear 1: set 2: copy */
118 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
119 int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
120 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
121 int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
122 struct secpolicy ip4_def_policy;
123 int ip4_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
124 int ip4_esp_randpad = -1;
127 SYSCTL_DECL(_net_inet_ipsec);
129 SYSCTL_DECL(_net_inet6_ipsec6);
134 SYSCTL_STRUCT(_net_inet_ipsec, IPSECCTL_STATS,
135 stats, CTLFLAG_RD, &ipsecstat, ipsecstat, "");
136 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY,
137 def_policy, CTLFLAG_RW, &ip4_def_policy.policy, 0, "");
138 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
139 CTLFLAG_RW, &ip4_esp_trans_deflev, 0, "");
140 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
141 CTLFLAG_RW, &ip4_esp_net_deflev, 0, "");
142 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
143 CTLFLAG_RW, &ip4_ah_trans_deflev, 0, "");
144 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
145 CTLFLAG_RW, &ip4_ah_net_deflev, 0, "");
146 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS,
147 ah_cleartos, CTLFLAG_RW, &ip4_ah_cleartos, 0, "");
148 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK,
149 ah_offsetmask, CTLFLAG_RW, &ip4_ah_offsetmask, 0, "");
150 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT,
151 dfbit, CTLFLAG_RW, &ip4_ipsec_dfbit, 0, "");
152 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN,
153 ecn, CTLFLAG_RW, &ip4_ipsec_ecn, 0, "");
154 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG,
155 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
156 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ESP_RANDPAD,
157 esp_randpad, CTLFLAG_RW, &ip4_esp_randpad, 0, "");
160 struct ipsecstat ipsec6stat;
161 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
162 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
163 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
164 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
165 struct secpolicy ip6_def_policy;
166 int ip6_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
167 int ip6_esp_randpad = -1;
169 /* net.inet6.ipsec6 */
170 SYSCTL_STRUCT(_net_inet6_ipsec6, IPSECCTL_STATS,
171 stats, CTLFLAG_RD, &ipsec6stat, ipsecstat, "");
172 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY,
173 def_policy, CTLFLAG_RW, &ip6_def_policy.policy, 0, "");
174 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
175 CTLFLAG_RW, &ip6_esp_trans_deflev, 0, "");
176 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
177 CTLFLAG_RW, &ip6_esp_net_deflev, 0, "");
178 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
179 CTLFLAG_RW, &ip6_ah_trans_deflev, 0, "");
180 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
181 CTLFLAG_RW, &ip6_ah_net_deflev, 0, "");
182 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN,
183 ecn, CTLFLAG_RW, &ip6_ipsec_ecn, 0, "");
184 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG,
185 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
186 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ESP_RANDPAD,
187 esp_randpad, CTLFLAG_RW, &ip6_esp_randpad, 0, "");
190 static int ipsec_setspidx_mbuf
191 (struct secpolicyindex *, u_int, u_int, struct mbuf *, int);
192 static int ipsec4_setspidx_inpcb (struct mbuf *, struct inpcb *pcb);
194 static int ipsec6_setspidx_in6pcb (struct mbuf *, struct in6pcb *pcb);
196 static int ipsec_setspidx (struct mbuf *, struct secpolicyindex *, int);
197 static void ipsec4_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
198 static int ipsec4_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
200 static void ipsec6_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
201 static int ipsec6_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
203 static struct inpcbpolicy *ipsec_newpcbpolicy (void);
204 static void ipsec_delpcbpolicy (struct inpcbpolicy *);
205 static struct secpolicy *ipsec_deepcopy_policy (struct secpolicy *src);
206 static int ipsec_set_policy (struct secpolicy **pcb_sp,
207 int optname, caddr_t request, size_t len, int priv);
208 static int ipsec_get_policy (struct secpolicy *pcb_sp, struct mbuf **mp);
209 static void vshiftl (unsigned char *, int, int);
210 static int ipsec_in_reject (struct secpolicy *, struct mbuf *);
211 static size_t ipsec_hdrsiz (struct secpolicy *);
213 static struct mbuf *ipsec4_splithdr (struct mbuf *);
216 static struct mbuf *ipsec6_splithdr (struct mbuf *);
219 static int ipsec4_encapsulate (struct mbuf *, struct secasvar *);
222 static int ipsec6_encapsulate (struct mbuf *, struct secasvar *);
226 * For OUTBOUND packet having a socket. Searching SPD for packet,
227 * and return a pointer to SP.
228 * OUT: NULL: no apropreate SP found, the following value is set to error.
230 * EACCES : discard packet.
231 * ENOENT : ipsec_acquire() in progress, maybe.
232 * others : error occured.
233 * others: a pointer to SP
235 * NOTE: IPv6 mapped adddress concern is implemented here.
238 ipsec4_getpolicybysock(m, dir, so, error)
244 struct inpcbpolicy *pcbsp = NULL;
245 struct secpolicy *currsp = NULL; /* policy on socket */
246 struct secpolicy *kernsp = NULL; /* policy on kernel */
249 if (m == NULL || so == NULL || error == NULL)
250 panic("ipsec4_getpolicybysock: NULL pointer was passed.");
252 switch (so->so_proto->pr_domain->dom_family) {
254 /* set spidx in pcb */
255 *error = ipsec4_setspidx_inpcb(m, sotoinpcb(so));
259 /* set spidx in pcb */
260 *error = ipsec6_setspidx_in6pcb(m, sotoin6pcb(so));
264 panic("ipsec4_getpolicybysock: unsupported address family\n");
268 switch (so->so_proto->pr_domain->dom_family) {
270 pcbsp = sotoinpcb(so)->inp_sp;
274 pcbsp = sotoin6pcb(so)->in6p_sp;
281 panic("ipsec4_getpolicybysock: pcbsp is NULL.");
284 case IPSEC_DIR_INBOUND:
285 currsp = pcbsp->sp_in;
287 case IPSEC_DIR_OUTBOUND:
288 currsp = pcbsp->sp_out;
291 panic("ipsec4_getpolicybysock: illegal direction.");
296 panic("ipsec4_getpolicybysock: currsp is NULL.");
298 /* when privilieged socket */
300 switch (currsp->policy) {
301 case IPSEC_POLICY_BYPASS:
306 case IPSEC_POLICY_ENTRUST:
307 /* look for a policy in SPD */
308 kernsp = key_allocsp(&currsp->spidx, dir);
311 if (kernsp != NULL) {
312 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
313 printf("DP ipsec4_getpolicybysock called "
314 "to allocate SP:%p\n", kernsp));
320 if (ip4_def_policy.policy != IPSEC_POLICY_DISCARD
321 && ip4_def_policy.policy != IPSEC_POLICY_NONE) {
323 "fixed system default policy: %d->%d\n",
324 ip4_def_policy.policy, IPSEC_POLICY_NONE));
325 ip4_def_policy.policy = IPSEC_POLICY_NONE;
327 ip4_def_policy.refcnt++;
329 return &ip4_def_policy;
331 case IPSEC_POLICY_IPSEC:
337 ipseclog((LOG_ERR, "ipsec4_getpolicybysock: "
338 "Invalid policy for PCB %d\n", currsp->policy));
345 /* when non-privilieged socket */
346 /* look for a policy in SPD */
347 kernsp = key_allocsp(&currsp->spidx, dir);
350 if (kernsp != NULL) {
351 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
352 printf("DP ipsec4_getpolicybysock called "
353 "to allocate SP:%p\n", kernsp));
359 switch (currsp->policy) {
360 case IPSEC_POLICY_BYPASS:
361 ipseclog((LOG_ERR, "ipsec4_getpolicybysock: "
362 "Illegal policy for non-priviliged defined %d\n",
367 case IPSEC_POLICY_ENTRUST:
368 if (ip4_def_policy.policy != IPSEC_POLICY_DISCARD
369 && ip4_def_policy.policy != IPSEC_POLICY_NONE) {
371 "fixed system default policy: %d->%d\n",
372 ip4_def_policy.policy, IPSEC_POLICY_NONE));
373 ip4_def_policy.policy = IPSEC_POLICY_NONE;
375 ip4_def_policy.refcnt++;
377 return &ip4_def_policy;
379 case IPSEC_POLICY_IPSEC:
385 ipseclog((LOG_ERR, "ipsec4_getpolicybysock: "
386 "Invalid policy for PCB %d\n", currsp->policy));
394 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
395 * and return a pointer to SP.
396 * OUT: positive: a pointer to the entry for security policy leaf matched.
397 * NULL: no apropreate SP found, the following value is set to error.
399 * EACCES : discard packet.
400 * ENOENT : ipsec_acquire() in progress, maybe.
401 * others : error occured.
404 ipsec4_getpolicybyaddr(m, dir, flag, error)
410 struct secpolicy *sp = NULL;
413 if (m == NULL || error == NULL)
414 panic("ipsec4_getpolicybyaddr: NULL pointer was passed.");
417 struct secpolicyindex spidx;
419 bzero(&spidx, sizeof(spidx));
421 /* make a index to look for a policy */
422 *error = ipsec_setspidx_mbuf(&spidx, dir, AF_INET, m,
423 (flag & IP_FORWARDING) ? 0 : 1);
428 sp = key_allocsp(&spidx, dir);
433 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
434 printf("DP ipsec4_getpolicybyaddr called "
435 "to allocate SP:%p\n", sp));
441 if (ip4_def_policy.policy != IPSEC_POLICY_DISCARD
442 && ip4_def_policy.policy != IPSEC_POLICY_NONE) {
443 ipseclog((LOG_INFO, "fixed system default policy:%d->%d\n",
444 ip4_def_policy.policy,
446 ip4_def_policy.policy = IPSEC_POLICY_NONE;
448 ip4_def_policy.refcnt++;
450 return &ip4_def_policy;
455 * For OUTBOUND packet having a socket. Searching SPD for packet,
456 * and return a pointer to SP.
457 * OUT: NULL: no apropreate SP found, the following value is set to error.
459 * EACCES : discard packet.
460 * ENOENT : ipsec_acquire() in progress, maybe.
461 * others : error occured.
462 * others: a pointer to SP
465 ipsec6_getpolicybysock(m, dir, so, error)
471 struct inpcbpolicy *pcbsp = NULL;
472 struct secpolicy *currsp = NULL; /* policy on socket */
473 struct secpolicy *kernsp = NULL; /* policy on kernel */
476 if (m == NULL || so == NULL || error == NULL)
477 panic("ipsec6_getpolicybysock: NULL pointer was passed.");
480 if (so->so_proto->pr_domain->dom_family != AF_INET6)
481 panic("ipsec6_getpolicybysock: socket domain != inet6");
484 /* set spidx in pcb */
485 ipsec6_setspidx_in6pcb(m, sotoin6pcb(so));
487 pcbsp = sotoin6pcb(so)->in6p_sp;
491 panic("ipsec6_getpolicybysock: pcbsp is NULL.");
494 case IPSEC_DIR_INBOUND:
495 currsp = pcbsp->sp_in;
497 case IPSEC_DIR_OUTBOUND:
498 currsp = pcbsp->sp_out;
501 panic("ipsec6_getpolicybysock: illegal direction.");
506 panic("ipsec6_getpolicybysock: currsp is NULL.");
508 /* when privilieged socket */
510 switch (currsp->policy) {
511 case IPSEC_POLICY_BYPASS:
516 case IPSEC_POLICY_ENTRUST:
517 /* look for a policy in SPD */
518 kernsp = key_allocsp(&currsp->spidx, dir);
521 if (kernsp != NULL) {
522 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
523 printf("DP ipsec6_getpolicybysock called "
524 "to allocate SP:%p\n", kernsp));
530 if (ip6_def_policy.policy != IPSEC_POLICY_DISCARD
531 && ip6_def_policy.policy != IPSEC_POLICY_NONE) {
533 "fixed system default policy: %d->%d\n",
534 ip6_def_policy.policy, IPSEC_POLICY_NONE));
535 ip6_def_policy.policy = IPSEC_POLICY_NONE;
537 ip6_def_policy.refcnt++;
539 return &ip6_def_policy;
541 case IPSEC_POLICY_IPSEC:
547 ipseclog((LOG_ERR, "ipsec6_getpolicybysock: "
548 "Invalid policy for PCB %d\n", currsp->policy));
555 /* when non-privilieged socket */
556 /* look for a policy in SPD */
557 kernsp = key_allocsp(&currsp->spidx, dir);
560 if (kernsp != NULL) {
561 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
562 printf("DP ipsec6_getpolicybysock called "
563 "to allocate SP:%p\n", kernsp));
569 switch (currsp->policy) {
570 case IPSEC_POLICY_BYPASS:
571 ipseclog((LOG_ERR, "ipsec6_getpolicybysock: "
572 "Illegal policy for non-priviliged defined %d\n",
577 case IPSEC_POLICY_ENTRUST:
578 if (ip6_def_policy.policy != IPSEC_POLICY_DISCARD
579 && ip6_def_policy.policy != IPSEC_POLICY_NONE) {
581 "fixed system default policy: %d->%d\n",
582 ip6_def_policy.policy, IPSEC_POLICY_NONE));
583 ip6_def_policy.policy = IPSEC_POLICY_NONE;
585 ip6_def_policy.refcnt++;
587 return &ip6_def_policy;
589 case IPSEC_POLICY_IPSEC:
596 "ipsec6_policybysock: Invalid policy for PCB %d\n",
605 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
606 * and return a pointer to SP.
607 * `flag' means that packet is to be forwarded whether or not.
609 * OUT: positive: a pointer to the entry for security policy leaf matched.
610 * NULL: no apropreate SP found, the following value is set to error.
612 * EACCES : discard packet.
613 * ENOENT : ipsec_acquire() in progress, maybe.
614 * others : error occured.
616 #ifndef IP_FORWARDING
617 #define IP_FORWARDING 1
621 ipsec6_getpolicybyaddr(m, dir, flag, error)
627 struct secpolicy *sp = NULL;
630 if (m == NULL || error == NULL)
631 panic("ipsec6_getpolicybyaddr: NULL pointer was passed.");
634 struct secpolicyindex spidx;
636 bzero(&spidx, sizeof(spidx));
638 /* make a index to look for a policy */
639 *error = ipsec_setspidx_mbuf(&spidx, dir, AF_INET6, m,
640 (flag & IP_FORWARDING) ? 0 : 1);
645 sp = key_allocsp(&spidx, dir);
650 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
651 printf("DP ipsec6_getpolicybyaddr called "
652 "to allocate SP:%p\n", sp));
658 if (ip6_def_policy.policy != IPSEC_POLICY_DISCARD
659 && ip6_def_policy.policy != IPSEC_POLICY_NONE) {
660 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
661 ip6_def_policy.policy, IPSEC_POLICY_NONE));
662 ip6_def_policy.policy = IPSEC_POLICY_NONE;
664 ip6_def_policy.refcnt++;
666 return &ip6_def_policy;
671 * set IP address into spidx from mbuf.
672 * When Forwarding packet and ICMP echo reply, this function is used.
674 * IN: get the followings from mbuf.
675 * protocol family, src, dst, next protocol
678 * other: failure, and set errno.
681 ipsec_setspidx_mbuf(spidx, dir, family, m, needport)
682 struct secpolicyindex *spidx;
690 if (spidx == NULL || m == NULL)
691 panic("ipsec_setspidx_mbuf: NULL pointer was passed.");
693 bzero(spidx, sizeof(*spidx));
695 error = ipsec_setspidx(m, spidx, needport);
704 bzero(spidx, sizeof(*spidx));
709 ipsec4_setspidx_inpcb(m, pcb)
713 struct secpolicyindex *spidx;
718 panic("ipsec4_setspidx_inpcb: no PCB found.");
719 if (pcb->inp_sp == NULL)
720 panic("ipsec4_setspidx_inpcb: no inp_sp found.");
721 if (pcb->inp_sp->sp_out == NULL || pcb->inp_sp->sp_in == NULL)
722 panic("ipsec4_setspidx_inpcb: no sp_in/out found.");
724 bzero(&pcb->inp_sp->sp_in->spidx, sizeof(*spidx));
725 bzero(&pcb->inp_sp->sp_out->spidx, sizeof(*spidx));
727 spidx = &pcb->inp_sp->sp_in->spidx;
728 error = ipsec_setspidx(m, spidx, 1);
731 spidx->dir = IPSEC_DIR_INBOUND;
733 spidx = &pcb->inp_sp->sp_out->spidx;
734 error = ipsec_setspidx(m, spidx, 1);
737 spidx->dir = IPSEC_DIR_OUTBOUND;
742 bzero(&pcb->inp_sp->sp_in->spidx, sizeof(*spidx));
743 bzero(&pcb->inp_sp->sp_out->spidx, sizeof(*spidx));
749 ipsec6_setspidx_in6pcb(m, pcb)
753 struct secpolicyindex *spidx;
758 panic("ipsec6_setspidx_in6pcb: no PCB found.");
759 if (pcb->in6p_sp == NULL)
760 panic("ipsec6_setspidx_in6pcb: no in6p_sp found.");
761 if (pcb->in6p_sp->sp_out == NULL || pcb->in6p_sp->sp_in == NULL)
762 panic("ipsec6_setspidx_in6pcb: no sp_in/out found.");
764 bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
765 bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
767 spidx = &pcb->in6p_sp->sp_in->spidx;
768 error = ipsec_setspidx(m, spidx, 1);
771 spidx->dir = IPSEC_DIR_INBOUND;
773 spidx = &pcb->in6p_sp->sp_out->spidx;
774 error = ipsec_setspidx(m, spidx, 1);
777 spidx->dir = IPSEC_DIR_OUTBOUND;
782 bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
783 bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
789 * configure security policy index (src/dst/proto/sport/dport)
790 * by looking at the content of mbuf.
791 * the caller is responsible for error recovery (like clearing up spidx).
794 ipsec_setspidx(m, spidx, needport)
796 struct secpolicyindex *spidx;
799 struct ip *ip = NULL;
807 panic("ipsec_setspidx: m == 0 passed.");
810 * validate m->m_pkthdr.len. we see incorrect length if we
811 * mistakenly call this function with inconsistent mbuf chain
812 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
815 for (n = m; n; n = n->m_next)
817 if (m->m_pkthdr.len != len) {
818 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
819 printf("ipsec_setspidx: "
820 "total of m_len(%d) != pkthdr.len(%d), "
822 len, m->m_pkthdr.len));
826 if (m->m_pkthdr.len < sizeof(struct ip)) {
827 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
828 printf("ipsec_setspidx: "
829 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
834 if (m->m_len >= sizeof(*ip))
835 ip = mtod(m, struct ip *);
837 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
841 v = _IP_VHL_V(ip->ip_vhl);
847 error = ipsec4_setspidx_ipaddr(m, spidx);
850 ipsec4_get_ulp(m, spidx, needport);
854 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
855 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
856 printf("ipsec_setspidx: "
857 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
858 "ignored.\n", m->m_pkthdr.len));
861 error = ipsec6_setspidx_ipaddr(m, spidx);
864 ipsec6_get_ulp(m, spidx, needport);
868 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
869 printf("ipsec_setspidx: "
870 "unknown IP version %u, ignored.\n", v));
876 ipsec4_get_ulp(m, spidx, needport)
878 struct secpolicyindex *spidx;
890 panic("ipsec4_get_ulp: NULL pointer was passed.");
891 if (m->m_pkthdr.len < sizeof(ip))
892 panic("ipsec4_get_ulp: too short");
895 spidx->ul_proto = IPSEC_ULPROTO_ANY;
896 ((struct sockaddr_in *)&spidx->src)->sin_port = IPSEC_PORT_ANY;
897 ((struct sockaddr_in *)&spidx->dst)->sin_port = IPSEC_PORT_ANY;
899 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
900 /* ip_input() flips it into host endian XXX need more checking */
901 if (ip.ip_off & (IP_MF | IP_OFFMASK))
906 off = _IP_VHL_HL(ip->ip_vhl) << 2;
910 while (off < m->m_pkthdr.len) {
913 spidx->ul_proto = nxt;
916 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
918 m_copydata(m, off, sizeof(th), (caddr_t)&th);
919 ((struct sockaddr_in *)&spidx->src)->sin_port =
921 ((struct sockaddr_in *)&spidx->dst)->sin_port =
925 spidx->ul_proto = nxt;
928 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
930 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
931 ((struct sockaddr_in *)&spidx->src)->sin_port =
933 ((struct sockaddr_in *)&spidx->dst)->sin_port =
937 if (m->m_pkthdr.len > off + sizeof(ip6e))
939 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
940 off += (ip6e.ip6e_len + 2) << 2;
945 /* XXX intermediate headers??? */
946 spidx->ul_proto = nxt;
952 /* assumes that m is sane */
954 ipsec4_setspidx_ipaddr(m, spidx)
956 struct secpolicyindex *spidx;
958 struct ip *ip = NULL;
960 struct sockaddr_in *sin;
962 if (m->m_len >= sizeof(*ip))
963 ip = mtod(m, struct ip *);
965 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
969 sin = (struct sockaddr_in *)&spidx->src;
970 bzero(sin, sizeof(*sin));
971 sin->sin_family = AF_INET;
972 sin->sin_len = sizeof(struct sockaddr_in);
973 bcopy(&ip->ip_src, &sin->sin_addr, sizeof(ip->ip_src));
974 spidx->prefs = sizeof(struct in_addr) << 3;
976 sin = (struct sockaddr_in *)&spidx->dst;
977 bzero(sin, sizeof(*sin));
978 sin->sin_family = AF_INET;
979 sin->sin_len = sizeof(struct sockaddr_in);
980 bcopy(&ip->ip_dst, &sin->sin_addr, sizeof(ip->ip_dst));
981 spidx->prefd = sizeof(struct in_addr) << 3;
987 ipsec6_get_ulp(m, spidx, needport)
989 struct secpolicyindex *spidx;
998 panic("ipsec6_get_ulp: NULL pointer was passed.");
1000 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1001 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m));
1004 spidx->ul_proto = IPSEC_ULPROTO_ANY;
1005 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
1006 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
1009 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
1010 if (off < 0 || m->m_pkthdr.len < off)
1015 spidx->ul_proto = nxt;
1018 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
1020 m_copydata(m, off, sizeof(th), (caddr_t)&th);
1021 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
1022 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
1025 spidx->ul_proto = nxt;
1028 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
1030 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
1031 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
1032 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
1034 case IPPROTO_ICMPV6:
1036 /* XXX intermediate headers??? */
1037 spidx->ul_proto = nxt;
1042 /* assumes that m is sane */
1044 ipsec6_setspidx_ipaddr(m, spidx)
1046 struct secpolicyindex *spidx;
1048 struct ip6_hdr *ip6 = NULL;
1049 struct ip6_hdr ip6buf;
1050 struct sockaddr_in6 *sin6;
1052 if (m->m_len >= sizeof(*ip6))
1053 ip6 = mtod(m, struct ip6_hdr *);
1055 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
1059 sin6 = (struct sockaddr_in6 *)&spidx->src;
1060 bzero(sin6, sizeof(*sin6));
1061 sin6->sin6_family = AF_INET6;
1062 sin6->sin6_len = sizeof(struct sockaddr_in6);
1063 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
1064 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
1065 sin6->sin6_addr.s6_addr16[1] = 0;
1066 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
1068 spidx->prefs = sizeof(struct in6_addr) << 3;
1070 sin6 = (struct sockaddr_in6 *)&spidx->dst;
1071 bzero(sin6, sizeof(*sin6));
1072 sin6->sin6_family = AF_INET6;
1073 sin6->sin6_len = sizeof(struct sockaddr_in6);
1074 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
1075 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
1076 sin6->sin6_addr.s6_addr16[1] = 0;
1077 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
1079 spidx->prefd = sizeof(struct in6_addr) << 3;
1085 static struct inpcbpolicy *
1086 ipsec_newpcbpolicy()
1088 struct inpcbpolicy *p;
1090 p = (struct inpcbpolicy *)malloc(sizeof(*p), M_SECA, M_NOWAIT);
1095 ipsec_delpcbpolicy(p)
1096 struct inpcbpolicy *p;
1101 /* initialize policy in PCB */
1103 ipsec_init_policy(so, pcb_sp)
1105 struct inpcbpolicy **pcb_sp;
1107 struct inpcbpolicy *new;
1110 if (so == NULL || pcb_sp == NULL)
1111 panic("ipsec_init_policy: NULL pointer was passed.");
1113 new = ipsec_newpcbpolicy();
1115 ipseclog((LOG_DEBUG, "ipsec_init_policy: No more memory.\n"));
1118 bzero(new, sizeof(*new));
1120 if (so->so_cred != 0 && so->so_cred->cr_uid == 0)
1125 if ((new->sp_in = key_newsp()) == NULL) {
1126 ipsec_delpcbpolicy(new);
1129 new->sp_in->state = IPSEC_SPSTATE_ALIVE;
1130 new->sp_in->policy = IPSEC_POLICY_ENTRUST;
1132 if ((new->sp_out = key_newsp()) == NULL) {
1133 key_freesp(new->sp_in);
1134 ipsec_delpcbpolicy(new);
1137 new->sp_out->state = IPSEC_SPSTATE_ALIVE;
1138 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
1145 /* copy old ipsec policy into new */
1147 ipsec_copy_policy(old, new)
1148 struct inpcbpolicy *old, *new;
1150 struct secpolicy *sp;
1152 sp = ipsec_deepcopy_policy(old->sp_in);
1154 key_freesp(new->sp_in);
1159 sp = ipsec_deepcopy_policy(old->sp_out);
1161 key_freesp(new->sp_out);
1166 new->priv = old->priv;
1171 /* deep-copy a policy in PCB */
1172 static struct secpolicy *
1173 ipsec_deepcopy_policy(src)
1174 struct secpolicy *src;
1176 struct ipsecrequest *newchain = NULL;
1177 struct ipsecrequest *p;
1178 struct ipsecrequest **q;
1179 struct ipsecrequest *r;
1180 struct secpolicy *dst;
1183 if (src == NULL || dst == NULL)
1187 * deep-copy IPsec request chain. This is required since struct
1188 * ipsecrequest is not reference counted.
1191 for (p = src->req; p; p = p->next) {
1192 *q = (struct ipsecrequest *)malloc(sizeof(struct ipsecrequest),
1196 bzero(*q, sizeof(**q));
1199 (*q)->saidx.proto = p->saidx.proto;
1200 (*q)->saidx.mode = p->saidx.mode;
1201 (*q)->level = p->level;
1202 (*q)->saidx.reqid = p->saidx.reqid;
1204 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
1205 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
1213 dst->req = newchain;
1214 dst->state = src->state;
1215 dst->policy = src->policy;
1216 /* do not touch the refcnt fields */
1221 for (p = newchain; p; p = r) {
1229 /* set policy and ipsec request if present. */
1231 ipsec_set_policy(pcb_sp, optname, request, len, priv)
1232 struct secpolicy **pcb_sp;
1238 struct sadb_x_policy *xpl;
1239 struct secpolicy *newsp = NULL;
1243 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
1245 if (len < sizeof(*xpl))
1247 xpl = (struct sadb_x_policy *)request;
1249 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1250 printf("ipsec_set_policy: passed policy\n");
1251 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
1253 /* check policy type */
1254 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1255 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
1256 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1259 /* check privileged socket */
1260 if (priv == 0 && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS)
1263 /* allocation new SP entry */
1264 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1267 newsp->state = IPSEC_SPSTATE_ALIVE;
1269 /* clear old SP and set new SP */
1270 key_freesp(*pcb_sp);
1272 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1273 printf("ipsec_set_policy: new policy\n");
1274 kdebug_secpolicy(newsp));
1280 ipsec_get_policy(pcb_sp, mp)
1281 struct secpolicy *pcb_sp;
1286 if (pcb_sp == NULL || mp == NULL)
1289 *mp = key_sp2msg(pcb_sp);
1291 ipseclog((LOG_DEBUG, "ipsec_get_policy: No more memory.\n"));
1295 (*mp)->m_type = MT_DATA;
1296 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1297 printf("ipsec_get_policy:\n");
1304 ipsec4_set_policy(inp, optname, request, len, priv)
1311 struct sadb_x_policy *xpl;
1312 struct secpolicy **pcb_sp;
1315 if (inp == NULL || request == NULL)
1317 if (len < sizeof(*xpl))
1319 xpl = (struct sadb_x_policy *)request;
1321 /* select direction */
1322 switch (xpl->sadb_x_policy_dir) {
1323 case IPSEC_DIR_INBOUND:
1324 pcb_sp = &inp->inp_sp->sp_in;
1326 case IPSEC_DIR_OUTBOUND:
1327 pcb_sp = &inp->inp_sp->sp_out;
1330 ipseclog((LOG_ERR, "ipsec4_set_policy: invalid direction=%u\n",
1331 xpl->sadb_x_policy_dir));
1335 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1339 ipsec4_get_policy(inp, request, len, mp)
1345 struct sadb_x_policy *xpl;
1346 struct secpolicy *pcb_sp;
1349 if (inp == NULL || request == NULL || mp == NULL)
1351 if (inp->inp_sp == NULL)
1352 panic("policy in PCB is NULL");
1353 if (len < sizeof(*xpl))
1355 xpl = (struct sadb_x_policy *)request;
1357 /* select direction */
1358 switch (xpl->sadb_x_policy_dir) {
1359 case IPSEC_DIR_INBOUND:
1360 pcb_sp = inp->inp_sp->sp_in;
1362 case IPSEC_DIR_OUTBOUND:
1363 pcb_sp = inp->inp_sp->sp_out;
1366 ipseclog((LOG_ERR, "ipsec4_set_policy: invalid direction=%u\n",
1367 xpl->sadb_x_policy_dir));
1371 return ipsec_get_policy(pcb_sp, mp);
1374 /* delete policy in PCB */
1376 ipsec4_delete_pcbpolicy(inp)
1381 panic("ipsec4_delete_pcbpolicy: NULL pointer was passed.");
1383 if (inp->inp_sp == NULL)
1386 if (inp->inp_sp->sp_in != NULL) {
1387 key_freesp(inp->inp_sp->sp_in);
1388 inp->inp_sp->sp_in = NULL;
1391 if (inp->inp_sp->sp_out != NULL) {
1392 key_freesp(inp->inp_sp->sp_out);
1393 inp->inp_sp->sp_out = NULL;
1396 ipsec_delpcbpolicy(inp->inp_sp);
1404 ipsec6_set_policy(in6p, optname, request, len, priv)
1405 struct in6pcb *in6p;
1411 struct sadb_x_policy *xpl;
1412 struct secpolicy **pcb_sp;
1415 if (in6p == NULL || request == NULL)
1417 if (len < sizeof(*xpl))
1419 xpl = (struct sadb_x_policy *)request;
1421 /* select direction */
1422 switch (xpl->sadb_x_policy_dir) {
1423 case IPSEC_DIR_INBOUND:
1424 pcb_sp = &in6p->in6p_sp->sp_in;
1426 case IPSEC_DIR_OUTBOUND:
1427 pcb_sp = &in6p->in6p_sp->sp_out;
1430 ipseclog((LOG_ERR, "ipsec6_set_policy: invalid direction=%u\n",
1431 xpl->sadb_x_policy_dir));
1435 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1439 ipsec6_get_policy(in6p, request, len, mp)
1440 struct in6pcb *in6p;
1445 struct sadb_x_policy *xpl;
1446 struct secpolicy *pcb_sp;
1449 if (in6p == NULL || request == NULL || mp == NULL)
1451 if (in6p->in6p_sp == NULL)
1452 panic("policy in PCB is NULL");
1453 if (len < sizeof(*xpl))
1455 xpl = (struct sadb_x_policy *)request;
1457 /* select direction */
1458 switch (xpl->sadb_x_policy_dir) {
1459 case IPSEC_DIR_INBOUND:
1460 pcb_sp = in6p->in6p_sp->sp_in;
1462 case IPSEC_DIR_OUTBOUND:
1463 pcb_sp = in6p->in6p_sp->sp_out;
1466 ipseclog((LOG_ERR, "ipsec6_set_policy: invalid direction=%u\n",
1467 xpl->sadb_x_policy_dir));
1471 return ipsec_get_policy(pcb_sp, mp);
1475 ipsec6_delete_pcbpolicy(in6p)
1476 struct in6pcb *in6p;
1480 panic("ipsec6_delete_pcbpolicy: NULL pointer was passed.");
1482 if (in6p->in6p_sp == NULL)
1485 if (in6p->in6p_sp->sp_in != NULL) {
1486 key_freesp(in6p->in6p_sp->sp_in);
1487 in6p->in6p_sp->sp_in = NULL;
1490 if (in6p->in6p_sp->sp_out != NULL) {
1491 key_freesp(in6p->in6p_sp->sp_out);
1492 in6p->in6p_sp->sp_out = NULL;
1495 ipsec_delpcbpolicy(in6p->in6p_sp);
1496 in6p->in6p_sp = NULL;
1503 * return current level.
1504 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1507 ipsec_get_reqlevel(isr)
1508 struct ipsecrequest *isr;
1511 u_int esp_trans_deflev, esp_net_deflev, ah_trans_deflev, ah_net_deflev;
1514 if (isr == NULL || isr->sp == NULL)
1515 panic("ipsec_get_reqlevel: NULL pointer is passed.");
1516 if (((struct sockaddr *)&isr->sp->spidx.src)->sa_family
1517 != ((struct sockaddr *)&isr->sp->spidx.dst)->sa_family)
1518 panic("ipsec_get_reqlevel: family mismatched.");
1520 /* XXX note that we have ipseclog() expanded here - code sync issue */
1521 #define IPSEC_CHECK_DEFAULT(lev) \
1522 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1523 && (lev) != IPSEC_LEVEL_UNIQUE) \
1525 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1526 (lev), IPSEC_LEVEL_REQUIRE) \
1528 (lev) = IPSEC_LEVEL_REQUIRE, \
1532 /* set default level */
1533 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1536 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1537 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1538 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1539 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1544 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1545 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1546 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1547 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1551 panic("key_get_reqlevel: Unknown family. %d",
1552 ((struct sockaddr *)&isr->sp->spidx.src)->sa_family);
1555 #undef IPSEC_CHECK_DEFAULT
1558 switch (isr->level) {
1559 case IPSEC_LEVEL_DEFAULT:
1560 switch (isr->saidx.proto) {
1562 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1563 level = esp_net_deflev;
1565 level = esp_trans_deflev;
1568 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1569 level = ah_net_deflev;
1571 level = ah_trans_deflev;
1572 case IPPROTO_IPCOMP:
1574 * we don't really care, as IPcomp document says that
1575 * we shouldn't compress small packets
1577 level = IPSEC_LEVEL_USE;
1580 panic("ipsec_get_reqlevel: "
1581 "Illegal protocol defined %u",
1586 case IPSEC_LEVEL_USE:
1587 case IPSEC_LEVEL_REQUIRE:
1590 case IPSEC_LEVEL_UNIQUE:
1591 level = IPSEC_LEVEL_REQUIRE;
1595 panic("ipsec_get_reqlevel: Illegal IPsec level %u",
1603 * Check AH/ESP integrity.
1609 ipsec_in_reject(sp, m)
1610 struct secpolicy *sp;
1613 struct ipsecrequest *isr;
1615 int need_auth, need_conf, need_icv;
1617 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1618 printf("ipsec_in_reject: using SP\n");
1619 kdebug_secpolicy(sp));
1622 switch (sp->policy) {
1623 case IPSEC_POLICY_DISCARD:
1625 case IPSEC_POLICY_BYPASS:
1626 case IPSEC_POLICY_NONE:
1629 case IPSEC_POLICY_IPSEC:
1632 case IPSEC_POLICY_ENTRUST:
1634 panic("ipsec_hdrsiz: Invalid policy found. %d", sp->policy);
1641 /* XXX should compare policy against ipsec header history */
1643 for (isr = sp->req; isr != NULL; isr = isr->next) {
1645 /* get current level */
1646 level = ipsec_get_reqlevel(isr);
1648 switch (isr->saidx.proto) {
1650 if (level == IPSEC_LEVEL_REQUIRE) {
1653 if (isr->sav != NULL
1654 && isr->sav->flags == SADB_X_EXT_NONE
1655 && isr->sav->alg_auth != SADB_AALG_NONE)
1660 if (level == IPSEC_LEVEL_REQUIRE) {
1665 case IPPROTO_IPCOMP:
1667 * we don't really care, as IPcomp document says that
1668 * we shouldn't compress small packets, IPComp policy
1669 * should always be treated as being in "use" level.
1675 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1676 printf("ipsec_in_reject: auth:%d conf:%d icv:%d m_flags:%x\n",
1677 need_auth, need_conf, need_icv, m->m_flags));
1679 if ((need_conf && !(m->m_flags & M_DECRYPTED))
1680 || (!need_auth && need_icv && !(m->m_flags & M_AUTHIPDGM))
1681 || (need_auth && !(m->m_flags & M_AUTHIPHDR)))
1688 * Check AH/ESP integrity.
1689 * This function is called from tcp_input(), udp_input(),
1690 * and {ah,esp}4_input for tunnel mode
1693 ipsec4_in_reject_so(m, so)
1697 struct secpolicy *sp = NULL;
1703 return 0; /* XXX should be panic ? */
1705 /* get SP for this packet.
1706 * When we are called from ip_forward(), we call
1707 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1710 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1712 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_INBOUND, so, &error);
1715 return 0; /* XXX should be panic ?
1716 * -> No, there may be error. */
1718 result = ipsec_in_reject(sp, m);
1719 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1720 printf("DP ipsec4_in_reject_so call free SP:%p\n", sp));
1727 ipsec4_in_reject(m, inp)
1732 return ipsec4_in_reject_so(m, NULL);
1733 if (inp->inp_socket)
1734 return ipsec4_in_reject_so(m, inp->inp_socket);
1736 panic("ipsec4_in_reject: invalid inpcb/socket");
1741 * Check AH/ESP integrity.
1742 * This function is called from tcp6_input(), udp6_input(),
1743 * and {ah,esp}6_input for tunnel mode
1746 ipsec6_in_reject_so(m, so)
1750 struct secpolicy *sp = NULL;
1756 return 0; /* XXX should be panic ? */
1758 /* get SP for this packet.
1759 * When we are called from ip_forward(), we call
1760 * ipsec6_getpolicybyaddr() with IP_FORWARDING flag.
1763 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1765 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_INBOUND, so, &error);
1768 return 0; /* XXX should be panic ? */
1770 result = ipsec_in_reject(sp, m);
1771 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1772 printf("DP ipsec6_in_reject_so call free SP:%p\n", sp));
1779 ipsec6_in_reject(m, in6p)
1781 struct in6pcb *in6p;
1784 return ipsec6_in_reject_so(m, NULL);
1785 if (in6p->in6p_socket)
1786 return ipsec6_in_reject_so(m, in6p->in6p_socket);
1788 panic("ipsec6_in_reject: invalid in6p/socket");
1793 * compute the byte size to be occupied by IPsec header.
1794 * in case it is tunneled, it includes the size of outer IP header.
1795 * NOTE: SP passed is free in this function.
1799 struct secpolicy *sp;
1801 struct ipsecrequest *isr;
1804 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1805 printf("ipsec_hdrsiz: using SP\n");
1806 kdebug_secpolicy(sp));
1809 switch (sp->policy) {
1810 case IPSEC_POLICY_DISCARD:
1811 case IPSEC_POLICY_BYPASS:
1812 case IPSEC_POLICY_NONE:
1815 case IPSEC_POLICY_IPSEC:
1818 case IPSEC_POLICY_ENTRUST:
1820 panic("ipsec_hdrsiz: Invalid policy found. %d", sp->policy);
1825 for (isr = sp->req; isr != NULL; isr = isr->next) {
1829 switch (isr->saidx.proto) {
1832 clen = esp_hdrsiz(isr);
1838 clen = ah_hdrsiz(isr);
1840 case IPPROTO_IPCOMP:
1841 clen = sizeof(struct ipcomp);
1845 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1846 switch (((struct sockaddr *)&isr->saidx.dst)->sa_family) {
1848 clen += sizeof(struct ip);
1852 clen += sizeof(struct ip6_hdr);
1856 ipseclog((LOG_ERR, "ipsec_hdrsiz: "
1857 "unknown AF %d in IPsec tunnel SA\n",
1858 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1868 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1870 ipsec4_hdrsiz(m, dir, inp)
1875 struct secpolicy *sp = NULL;
1881 return 0; /* XXX should be panic ? */
1882 if (inp != NULL && inp->inp_socket == NULL)
1883 panic("ipsec4_hdrsize: why is socket NULL but there is PCB.");
1885 /* get SP for this packet.
1886 * When we are called from ip_forward(), we call
1887 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1890 sp = ipsec4_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1892 sp = ipsec4_getpolicybysock(m, dir, inp->inp_socket, &error);
1895 return 0; /* XXX should be panic ? */
1897 size = ipsec_hdrsiz(sp);
1898 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1899 printf("DP ipsec4_hdrsiz call free SP:%p\n", sp));
1900 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1901 printf("ipsec4_hdrsiz: size:%lu.\n", (unsigned long)size));
1908 /* This function is called from ipsec6_hdrsize_tcp(),
1909 * and maybe from ip6_forward.()
1912 ipsec6_hdrsiz(m, dir, in6p)
1915 struct in6pcb *in6p;
1917 struct secpolicy *sp = NULL;
1923 return 0; /* XXX shoud be panic ? */
1924 if (in6p != NULL && in6p->in6p_socket == NULL)
1925 panic("ipsec6_hdrsize: why is socket NULL but there is PCB.");
1927 /* get SP for this packet */
1928 /* XXX Is it right to call with IP_FORWARDING. */
1930 sp = ipsec6_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1932 sp = ipsec6_getpolicybysock(m, dir, in6p->in6p_socket, &error);
1936 size = ipsec_hdrsiz(sp);
1937 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1938 printf("DP ipsec6_hdrsiz call free SP:%p\n", sp));
1939 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1940 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size));
1949 * encapsulate for ipsec tunnel.
1950 * ip->ip_src must be fixed later on.
1953 ipsec4_encapsulate(m, sav)
1955 struct secasvar *sav;
1962 /* can't tunnel between different AFs */
1963 if (((struct sockaddr *)&sav->sah->saidx.src)->sa_family
1964 != ((struct sockaddr *)&sav->sah->saidx.dst)->sa_family
1965 || ((struct sockaddr *)&sav->sah->saidx.src)->sa_family != AF_INET) {
1970 /* XXX if the dst is myself, perform nothing. */
1971 if (key_ismyaddr((struct sockaddr *)&sav->sah->saidx.dst)) {
1977 if (m->m_len < sizeof(*ip))
1978 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
1980 ip = mtod(m, struct ip *);
1982 hlen = _IP_VHL_HL(ip->ip_vhl) << 2;
1984 hlen = ip->ip_hl << 2;
1987 if (m->m_len != hlen)
1988 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
1990 /* generate header checksum */
1993 if (ip->ip_vhl == IP_VHL_BORING)
1994 ip->ip_sum = in_cksum_hdr(ip);
1996 ip->ip_sum = in_cksum(m, hlen);
1998 ip->ip_sum = in_cksum(m, hlen);
2001 plen = m->m_pkthdr.len;
2004 * grow the mbuf to accomodate the new IPv4 header.
2005 * NOTE: IPv4 options will never be copied.
2007 if (M_LEADINGSPACE(m->m_next) < hlen) {
2009 MGET(n, M_DONTWAIT, MT_DATA);
2015 n->m_next = m->m_next;
2017 m->m_pkthdr.len += hlen;
2018 oip = mtod(n, struct ip *);
2020 m->m_next->m_len += hlen;
2021 m->m_next->m_data -= hlen;
2022 m->m_pkthdr.len += hlen;
2023 oip = mtod(m->m_next, struct ip *);
2025 ip = mtod(m, struct ip *);
2026 ovbcopy((caddr_t)ip, (caddr_t)oip, hlen);
2027 m->m_len = sizeof(struct ip);
2028 m->m_pkthdr.len -= (hlen - sizeof(struct ip));
2030 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
2031 /* ECN consideration. */
2032 ip_ecn_ingress(ip4_ipsec_ecn, &ip->ip_tos, &oip->ip_tos);
2034 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
2036 ip->ip_hl = sizeof(struct ip) >> 2;
2038 ip->ip_off &= htons(~IP_OFFMASK);
2039 ip->ip_off &= htons(~IP_MF);
2040 switch (ip4_ipsec_dfbit) {
2041 case 0: /* clear DF bit */
2042 ip->ip_off &= htons(~IP_DF);
2044 case 1: /* set DF bit */
2045 ip->ip_off |= htons(IP_DF);
2047 default: /* copy DF bit */
2050 ip->ip_p = IPPROTO_IPIP;
2051 if (plen + sizeof(struct ip) < IP_MAXPACKET)
2052 ip->ip_len = htons(plen + sizeof(struct ip));
2054 ipseclog((LOG_ERR, "IPv4 ipsec: size exceeds limit: "
2055 "leave ip_len as is (invalid packet)\n"));
2058 ip->ip_id = ip_randomid();
2060 ip->ip_id = htons(ip_id++);
2062 bcopy(&((struct sockaddr_in *)&sav->sah->saidx.src)->sin_addr,
2063 &ip->ip_src, sizeof(ip->ip_src));
2064 bcopy(&((struct sockaddr_in *)&sav->sah->saidx.dst)->sin_addr,
2065 &ip->ip_dst, sizeof(ip->ip_dst));
2066 ip->ip_ttl = IPDEFTTL;
2068 /* XXX Should ip_src be updated later ? */
2076 ipsec6_encapsulate(m, sav)
2078 struct secasvar *sav;
2080 struct ip6_hdr *oip6;
2081 struct ip6_hdr *ip6;
2084 /* can't tunnel between different AFs */
2085 if (((struct sockaddr *)&sav->sah->saidx.src)->sa_family
2086 != ((struct sockaddr *)&sav->sah->saidx.dst)->sa_family
2087 || ((struct sockaddr *)&sav->sah->saidx.src)->sa_family != AF_INET6) {
2092 /* XXX if the dst is myself, perform nothing. */
2093 if (key_ismyaddr((struct sockaddr *)&sav->sah->saidx.dst)) {
2099 plen = m->m_pkthdr.len;
2102 * grow the mbuf to accomodate the new IPv6 header.
2104 if (m->m_len != sizeof(struct ip6_hdr))
2105 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2106 if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
2108 MGET(n, M_DONTWAIT, MT_DATA);
2113 n->m_len = sizeof(struct ip6_hdr);
2114 n->m_next = m->m_next;
2116 m->m_pkthdr.len += sizeof(struct ip6_hdr);
2117 oip6 = mtod(n, struct ip6_hdr *);
2119 m->m_next->m_len += sizeof(struct ip6_hdr);
2120 m->m_next->m_data -= sizeof(struct ip6_hdr);
2121 m->m_pkthdr.len += sizeof(struct ip6_hdr);
2122 oip6 = mtod(m->m_next, struct ip6_hdr *);
2124 ip6 = mtod(m, struct ip6_hdr *);
2125 ovbcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
2127 /* Fake link-local scope-class addresses */
2128 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
2129 oip6->ip6_src.s6_addr16[1] = 0;
2130 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
2131 oip6->ip6_dst.s6_addr16[1] = 0;
2133 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
2134 /* ECN consideration. */
2135 ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
2136 if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
2137 ip6->ip6_plen = htons(plen);
2139 /* ip6->ip6_plen will be updated in ip6_output() */
2141 ip6->ip6_nxt = IPPROTO_IPV6;
2142 bcopy(&((struct sockaddr_in6 *)&sav->sah->saidx.src)->sin6_addr,
2143 &ip6->ip6_src, sizeof(ip6->ip6_src));
2144 bcopy(&((struct sockaddr_in6 *)&sav->sah->saidx.dst)->sin6_addr,
2145 &ip6->ip6_dst, sizeof(ip6->ip6_dst));
2146 ip6->ip6_hlim = IPV6_DEFHLIM;
2148 /* XXX Should ip6_src be updated later ? */
2155 * Check the variable replay window.
2156 * ipsec_chkreplay() performs replay check before ICV verification.
2157 * ipsec_updatereplay() updates replay bitmap. This must be called after
2158 * ICV verification (it also performs replay check, which is usually done
2160 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2162 * based on RFC 2401.
2165 ipsec_chkreplay(seq, sav)
2167 struct secasvar *sav;
2169 const struct secreplay *replay;
2172 u_int32_t wsizeb; /* constant: bits of window size */
2173 int frlast; /* constant: last frame */
2177 panic("ipsec_chkreplay: NULL pointer was passed.");
2179 replay = sav->replay;
2181 if (replay->wsize == 0)
2182 return 1; /* no need to check replay. */
2185 frlast = replay->wsize - 1;
2186 wsizeb = replay->wsize << 3;
2188 /* sequence number of 0 is invalid */
2192 /* first time is always okay */
2193 if (replay->count == 0)
2196 if (seq > replay->lastseq) {
2197 /* larger sequences are okay */
2200 /* seq is equal or less than lastseq. */
2201 diff = replay->lastseq - seq;
2203 /* over range to check, i.e. too old or wrapped */
2207 fr = frlast - diff / 8;
2209 /* this packet already seen ? */
2210 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
2213 /* out of order but good */
2219 * check replay counter whether to update or not.
2224 ipsec_updatereplay(seq, sav)
2226 struct secasvar *sav;
2228 struct secreplay *replay;
2231 u_int32_t wsizeb; /* constant: bits of window size */
2232 int frlast; /* constant: last frame */
2236 panic("ipsec_chkreplay: NULL pointer was passed.");
2238 replay = sav->replay;
2240 if (replay->wsize == 0)
2241 goto ok; /* no need to check replay. */
2244 frlast = replay->wsize - 1;
2245 wsizeb = replay->wsize << 3;
2247 /* sequence number of 0 is invalid */
2252 if (replay->count == 0) {
2253 replay->lastseq = seq;
2254 bzero(replay->bitmap, replay->wsize);
2255 (replay->bitmap)[frlast] = 1;
2259 if (seq > replay->lastseq) {
2260 /* seq is larger than lastseq. */
2261 diff = seq - replay->lastseq;
2263 /* new larger sequence number */
2264 if (diff < wsizeb) {
2266 /* set bit for this packet */
2267 vshiftl(replay->bitmap, diff, replay->wsize);
2268 (replay->bitmap)[frlast] |= 1;
2270 /* this packet has a "way larger" */
2271 bzero(replay->bitmap, replay->wsize);
2272 (replay->bitmap)[frlast] = 1;
2274 replay->lastseq = seq;
2276 /* larger is good */
2278 /* seq is equal or less than lastseq. */
2279 diff = replay->lastseq - seq;
2281 /* over range to check, i.e. too old or wrapped */
2285 fr = frlast - diff / 8;
2287 /* this packet already seen ? */
2288 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
2292 (replay->bitmap)[fr] |= (1 << (diff % 8));
2294 /* out of order but good */
2298 if (replay->count == ~0) {
2300 /* set overflow flag */
2303 /* don't increment, no more packets accepted */
2304 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
2307 ipseclog((LOG_WARNING, "replay counter made %d cycle. %s\n",
2308 replay->overflow, ipsec_logsastr(sav)));
2317 * shift variable length buffer to left.
2318 * IN: bitmap: pointer to the buffer
2319 * nbit: the number of to shift.
2320 * wsize: buffer size (bytes).
2323 vshiftl(bitmap, nbit, wsize)
2324 unsigned char *bitmap;
2330 for (j = 0; j < nbit; j += 8) {
2331 s = (nbit - j < 8) ? (nbit - j): 8;
2333 for (i = 1; i < wsize; i++) {
2334 over = (bitmap[i] >> (8 - s));
2336 bitmap[i-1] |= over;
2344 ipsec4_logpacketstr(ip, spi)
2348 static char buf[256];
2352 s = (u_int8_t *)(&ip->ip_src);
2353 d = (u_int8_t *)(&ip->ip_dst);
2356 snprintf(buf, sizeof(buf), "packet(SPI=%u ", (u_int32_t)ntohl(spi));
2359 snprintf(p, sizeof(buf) - (p - buf), "src=%u.%u.%u.%u",
2360 s[0], s[1], s[2], s[3]);
2363 snprintf(p, sizeof(buf) - (p - buf), " dst=%u.%u.%u.%u",
2364 d[0], d[1], d[2], d[3]);
2367 snprintf(p, sizeof(buf) - (p - buf), ")");
2374 ipsec6_logpacketstr(ip6, spi)
2375 struct ip6_hdr *ip6;
2378 static char buf[256];
2382 snprintf(buf, sizeof(buf), "packet(SPI=%u ", (u_int32_t)ntohl(spi));
2385 snprintf(p, sizeof(buf) - (p - buf), "src=%s",
2386 ip6_sprintf(&ip6->ip6_src));
2389 snprintf(p, sizeof(buf) - (p - buf), " dst=%s",
2390 ip6_sprintf(&ip6->ip6_dst));
2393 snprintf(p, sizeof(buf) - (p - buf), ")");
2401 struct secasvar *sav;
2403 static char buf[256];
2405 struct secasindex *saidx = &sav->sah->saidx;
2407 /* validity check */
2408 if (((struct sockaddr *)&sav->sah->saidx.src)->sa_family
2409 != ((struct sockaddr *)&sav->sah->saidx.dst)->sa_family)
2410 panic("ipsec_logsastr: family mismatched.");
2413 snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
2416 if (((struct sockaddr *)&saidx->src)->sa_family == AF_INET) {
2418 s = (u_int8_t *)&((struct sockaddr_in *)&saidx->src)->sin_addr;
2419 d = (u_int8_t *)&((struct sockaddr_in *)&saidx->dst)->sin_addr;
2420 snprintf(p, sizeof(buf) - (p - buf),
2421 "src=%d.%d.%d.%d dst=%d.%d.%d.%d",
2422 s[0], s[1], s[2], s[3], d[0], d[1], d[2], d[3]);
2425 else if (((struct sockaddr *)&saidx->src)->sa_family == AF_INET6) {
2426 snprintf(p, sizeof(buf) - (p - buf),
2428 ip6_sprintf(&((struct sockaddr_in6 *)&saidx->src)->sin6_addr));
2431 snprintf(p, sizeof(buf) - (p - buf),
2433 ip6_sprintf(&((struct sockaddr_in6 *)&saidx->dst)->sin6_addr));
2438 snprintf(p, sizeof(buf) - (p - buf), ")");
2454 p = mtod(m, u_char *);
2455 for (i = 0; i < m->m_len; i++) {
2456 printf("%02x ", p[i]);
2458 if (totlen % 16 == 0)
2463 if (totlen % 16 != 0)
2470 * IPsec output logic for IPv4.
2473 ipsec4_output(state, sp, flags)
2474 struct ipsec_output_state *state;
2475 struct secpolicy *sp;
2478 struct ip *ip = NULL;
2479 struct ipsecrequest *isr = NULL;
2480 struct secasindex saidx;
2483 struct sockaddr_in *dst4;
2484 struct sockaddr_in *sin;
2487 panic("state == NULL in ipsec4_output");
2489 panic("state->m == NULL in ipsec4_output");
2491 panic("state->ro == NULL in ipsec4_output");
2493 panic("state->dst == NULL in ipsec4_output");
2495 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
2496 printf("ipsec4_output: applyed SP\n");
2497 kdebug_secpolicy(sp));
2499 for (isr = sp->req; isr != NULL; isr = isr->next) {
2501 #if 0 /* give up to check restriction of transport mode */
2502 /* XXX but should be checked somewhere */
2504 * some of the IPsec operation must be performed only in
2507 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT
2508 && (flags & IP_FORWARDING))
2512 /* make SA index for search proper SA */
2513 ip = mtod(state->m, struct ip *);
2514 bcopy(&isr->saidx, &saidx, sizeof(saidx));
2515 saidx.mode = isr->saidx.mode;
2516 saidx.reqid = isr->saidx.reqid;
2517 sin = (struct sockaddr_in *)&saidx.src;
2518 if (sin->sin_len == 0) {
2519 sin->sin_len = sizeof(*sin);
2520 sin->sin_family = AF_INET;
2521 sin->sin_port = IPSEC_PORT_ANY;
2522 bcopy(&ip->ip_src, &sin->sin_addr,
2523 sizeof(sin->sin_addr));
2525 sin = (struct sockaddr_in *)&saidx.dst;
2526 if (sin->sin_len == 0) {
2527 sin->sin_len = sizeof(*sin);
2528 sin->sin_family = AF_INET;
2529 sin->sin_port = IPSEC_PORT_ANY;
2530 bcopy(&ip->ip_dst, &sin->sin_addr,
2531 sizeof(sin->sin_addr));
2534 if ((error = key_checkrequest(isr, &saidx)) != 0) {
2536 * IPsec processing is required, but no SA found.
2537 * I assume that key_acquire() had been called
2538 * to get/establish the SA. Here I discard
2539 * this packet because it is responsibility for
2540 * upper layer to retransmit the packet.
2542 ipsecstat.out_nosa++;
2546 /* validity check */
2547 if (isr->sav == NULL) {
2548 switch (ipsec_get_reqlevel(isr)) {
2549 case IPSEC_LEVEL_USE:
2551 case IPSEC_LEVEL_REQUIRE:
2552 /* must be not reached here. */
2553 panic("ipsec4_output: no SA found, but required.");
2558 * If there is no valid SA, we give up to process any
2559 * more. In such a case, the SA's status is changed
2560 * from DYING to DEAD after allocating. If a packet
2561 * send to the receiver by dead SA, the receiver can
2562 * not decode a packet because SA has been dead.
2564 if (isr->sav->state != SADB_SASTATE_MATURE
2565 && isr->sav->state != SADB_SASTATE_DYING) {
2566 ipsecstat.out_nosa++;
2572 * There may be the case that SA status will be changed when
2573 * we are refering to one. So calling splsoftnet().
2577 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
2579 * build IPsec tunnel.
2581 /* XXX should be processed with other familiy */
2582 if (((struct sockaddr *)&isr->sav->sah->saidx.src)->sa_family != AF_INET) {
2583 ipseclog((LOG_ERR, "ipsec4_output: "
2584 "family mismatched between inner and outer spi=%u\n",
2585 (u_int32_t)ntohl(isr->sav->spi)));
2587 error = EAFNOSUPPORT;
2591 state->m = ipsec4_splithdr(state->m);
2597 error = ipsec4_encapsulate(state->m, isr->sav);
2603 ip = mtod(state->m, struct ip *);
2605 state->ro = &isr->sav->sah->sa_route;
2606 state->dst = (struct sockaddr *)&state->ro->ro_dst;
2607 dst4 = (struct sockaddr_in *)state->dst;
2608 if (state->ro->ro_rt
2609 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
2610 || dst4->sin_addr.s_addr != ip->ip_dst.s_addr)) {
2611 RTFREE(state->ro->ro_rt);
2612 state->ro->ro_rt = NULL;
2614 if (state->ro->ro_rt == 0) {
2615 dst4->sin_family = AF_INET;
2616 dst4->sin_len = sizeof(*dst4);
2617 dst4->sin_addr = ip->ip_dst;
2620 if (state->ro->ro_rt == 0) {
2621 ipstat.ips_noroute++;
2622 error = EHOSTUNREACH;
2626 /* adjust state->dst if tunnel endpoint is offlink */
2627 if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
2628 state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
2629 dst4 = (struct sockaddr_in *)state->dst;
2634 state->m = ipsec4_splithdr(state->m);
2639 switch (isr->saidx.proto) {
2642 if ((error = esp4_output(state->m, isr)) != 0) {
2654 if ((error = ah4_output(state->m, isr)) != 0) {
2659 case IPPROTO_IPCOMP:
2660 if ((error = ipcomp4_output(state->m, isr)) != 0) {
2667 "ipsec4_output: unknown ipsec protocol %d\n",
2675 if (state->m == 0) {
2679 ip = mtod(state->m, struct ip *);
2693 * IPsec output logic for IPv6, transport mode.
2696 ipsec6_output_trans(state, nexthdrp, mprev, sp, flags, tun)
2697 struct ipsec_output_state *state;
2700 struct secpolicy *sp;
2704 struct ip6_hdr *ip6;
2705 struct ipsecrequest *isr = NULL;
2706 struct secasindex saidx;
2709 struct sockaddr_in6 *sin6;
2712 panic("state == NULL in ipsec6_output_trans");
2714 panic("state->m == NULL in ipsec6_output_trans");
2716 panic("nexthdrp == NULL in ipsec6_output_trans");
2718 panic("mprev == NULL in ipsec6_output_trans");
2720 panic("sp == NULL in ipsec6_output_trans");
2722 panic("tun == NULL in ipsec6_output_trans");
2724 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
2725 printf("ipsec6_output_trans: applyed SP\n");
2726 kdebug_secpolicy(sp));
2729 for (isr = sp->req; isr; isr = isr->next) {
2730 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
2731 /* the rest will be handled by ipsec6_output_tunnel() */
2735 /* make SA index for search proper SA */
2736 ip6 = mtod(state->m, struct ip6_hdr *);
2737 bcopy(&isr->saidx, &saidx, sizeof(saidx));
2738 saidx.mode = isr->saidx.mode;
2739 saidx.reqid = isr->saidx.reqid;
2740 sin6 = (struct sockaddr_in6 *)&saidx.src;
2741 if (sin6->sin6_len == 0) {
2742 sin6->sin6_len = sizeof(*sin6);
2743 sin6->sin6_family = AF_INET6;
2744 sin6->sin6_port = IPSEC_PORT_ANY;
2745 bcopy(&ip6->ip6_src, &sin6->sin6_addr,
2746 sizeof(ip6->ip6_src));
2747 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
2748 /* fix scope id for comparing SPD */
2749 sin6->sin6_addr.s6_addr16[1] = 0;
2750 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
2753 sin6 = (struct sockaddr_in6 *)&saidx.dst;
2754 if (sin6->sin6_len == 0) {
2755 sin6->sin6_len = sizeof(*sin6);
2756 sin6->sin6_family = AF_INET6;
2757 sin6->sin6_port = IPSEC_PORT_ANY;
2758 bcopy(&ip6->ip6_dst, &sin6->sin6_addr,
2759 sizeof(ip6->ip6_dst));
2760 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
2761 /* fix scope id for comparing SPD */
2762 sin6->sin6_addr.s6_addr16[1] = 0;
2763 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
2767 if (key_checkrequest(isr, &saidx) == ENOENT) {
2769 * IPsec processing is required, but no SA found.
2770 * I assume that key_acquire() had been called
2771 * to get/establish the SA. Here I discard
2772 * this packet because it is responsibility for
2773 * upper layer to retransmit the packet.
2775 ipsec6stat.out_nosa++;
2779 * Notify the fact that the packet is discarded
2780 * to ourselves. I believe this is better than
2781 * just silently discarding. (jinmei@kame.net)
2782 * XXX: should we restrict the error to TCP packets?
2783 * XXX: should we directly notify sockets via
2786 icmp6_error(state->m, ICMP6_DST_UNREACH,
2787 ICMP6_DST_UNREACH_ADMIN, 0);
2788 state->m = NULL; /* icmp6_error freed the mbuf */
2792 /* validity check */
2793 if (isr->sav == NULL) {
2794 switch (ipsec_get_reqlevel(isr)) {
2795 case IPSEC_LEVEL_USE:
2797 case IPSEC_LEVEL_REQUIRE:
2798 /* must be not reached here. */
2799 panic("ipsec6_output_trans: no SA found, but required.");
2804 * If there is no valid SA, we give up to process.
2805 * see same place at ipsec4_output().
2807 if (isr->sav->state != SADB_SASTATE_MATURE
2808 && isr->sav->state != SADB_SASTATE_DYING) {
2809 ipsec6stat.out_nosa++;
2814 switch (isr->saidx.proto) {
2817 error = esp6_output(state->m, nexthdrp, mprev->m_next, isr);
2824 error = ah6_output(state->m, nexthdrp, mprev->m_next, isr);
2826 case IPPROTO_IPCOMP:
2827 error = ipcomp6_output(state->m, nexthdrp, mprev->m_next, isr);
2830 ipseclog((LOG_ERR, "ipsec6_output_trans: "
2831 "unknown ipsec protocol %d\n", isr->saidx.proto));
2833 ipsec6stat.out_inval++;
2841 plen = state->m->m_pkthdr.len - sizeof(struct ip6_hdr);
2842 if (plen > IPV6_MAXPACKET) {
2843 ipseclog((LOG_ERR, "ipsec6_output_trans: "
2844 "IPsec with IPv6 jumbogram is not supported\n"));
2845 ipsec6stat.out_inval++;
2846 error = EINVAL; /* XXX */
2849 ip6 = mtod(state->m, struct ip6_hdr *);
2850 ip6->ip6_plen = htons(plen);
2853 /* if we have more to go, we need a tunnel mode processing */
2866 * IPsec output logic for IPv6, tunnel mode.
2869 ipsec6_output_tunnel(state, sp, flags)
2870 struct ipsec_output_state *state;
2871 struct secpolicy *sp;
2874 struct ip6_hdr *ip6;
2875 struct ipsecrequest *isr = NULL;
2876 struct secasindex saidx;
2879 struct sockaddr_in6* dst6;
2883 panic("state == NULL in ipsec6_output_tunnel");
2885 panic("state->m == NULL in ipsec6_output_tunnel");
2887 panic("sp == NULL in ipsec6_output_tunnel");
2889 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
2890 printf("ipsec6_output_tunnel: applyed SP\n");
2891 kdebug_secpolicy(sp));
2894 * transport mode ipsec (before the 1st tunnel mode) is already
2895 * processed by ipsec6_output_trans().
2897 for (isr = sp->req; isr; isr = isr->next) {
2898 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
2902 for (/* already initialized */; isr; isr = isr->next) {
2903 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
2904 /* When tunnel mode, SA peers must be specified. */
2905 bcopy(&isr->saidx, &saidx, sizeof(saidx));
2907 /* make SA index to look for a proper SA */
2908 struct sockaddr_in6 *sin6;
2910 bzero(&saidx, sizeof(saidx));
2911 saidx.proto = isr->saidx.proto;
2912 saidx.mode = isr->saidx.mode;
2913 saidx.reqid = isr->saidx.reqid;
2915 ip6 = mtod(state->m, struct ip6_hdr *);
2916 sin6 = (struct sockaddr_in6 *)&saidx.src;
2917 if (sin6->sin6_len == 0) {
2918 sin6->sin6_len = sizeof(*sin6);
2919 sin6->sin6_family = AF_INET6;
2920 sin6->sin6_port = IPSEC_PORT_ANY;
2921 bcopy(&ip6->ip6_src, &sin6->sin6_addr,
2922 sizeof(ip6->ip6_src));
2923 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
2924 /* fix scope id for comparing SPD */
2925 sin6->sin6_addr.s6_addr16[1] = 0;
2926 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
2929 sin6 = (struct sockaddr_in6 *)&saidx.dst;
2930 if (sin6->sin6_len == 0) {
2931 sin6->sin6_len = sizeof(*sin6);
2932 sin6->sin6_family = AF_INET6;
2933 sin6->sin6_port = IPSEC_PORT_ANY;
2934 bcopy(&ip6->ip6_dst, &sin6->sin6_addr,
2935 sizeof(ip6->ip6_dst));
2936 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
2937 /* fix scope id for comparing SPD */
2938 sin6->sin6_addr.s6_addr16[1] = 0;
2939 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
2944 if (key_checkrequest(isr, &saidx) == ENOENT) {
2946 * IPsec processing is required, but no SA found.
2947 * I assume that key_acquire() had been called
2948 * to get/establish the SA. Here I discard
2949 * this packet because it is responsibility for
2950 * upper layer to retransmit the packet.
2952 ipsec6stat.out_nosa++;
2957 /* validity check */
2958 if (isr->sav == NULL) {
2959 switch (ipsec_get_reqlevel(isr)) {
2960 case IPSEC_LEVEL_USE:
2962 case IPSEC_LEVEL_REQUIRE:
2963 /* must be not reached here. */
2964 panic("ipsec6_output_tunnel: no SA found, but required.");
2969 * If there is no valid SA, we give up to process.
2970 * see same place at ipsec4_output().
2972 if (isr->sav->state != SADB_SASTATE_MATURE
2973 && isr->sav->state != SADB_SASTATE_DYING) {
2974 ipsec6stat.out_nosa++;
2980 * There may be the case that SA status will be changed when
2981 * we are refering to one. So calling splsoftnet().
2985 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
2987 * build IPsec tunnel.
2989 /* XXX should be processed with other familiy */
2990 if (((struct sockaddr *)&isr->sav->sah->saidx.src)->sa_family != AF_INET6) {
2991 ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
2992 "family mismatched between inner and outer, spi=%u\n",
2993 (u_int32_t)ntohl(isr->sav->spi)));
2995 ipsec6stat.out_inval++;
2996 error = EAFNOSUPPORT;
3000 state->m = ipsec6_splithdr(state->m);
3003 ipsec6stat.out_nomem++;
3007 error = ipsec6_encapsulate(state->m, isr->sav);
3013 ip6 = mtod(state->m, struct ip6_hdr *);
3015 state->ro = &isr->sav->sah->sa_route;
3016 state->dst = (struct sockaddr *)&state->ro->ro_dst;
3017 dst6 = (struct sockaddr_in6 *)state->dst;
3018 if (state->ro->ro_rt
3019 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
3020 || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
3021 RTFREE(state->ro->ro_rt);
3022 state->ro->ro_rt = NULL;
3024 if (state->ro->ro_rt == 0) {
3025 bzero(dst6, sizeof(*dst6));
3026 dst6->sin6_family = AF_INET6;
3027 dst6->sin6_len = sizeof(*dst6);
3028 dst6->sin6_addr = ip6->ip6_dst;
3031 if (state->ro->ro_rt == 0) {
3032 ip6stat.ip6s_noroute++;
3033 ipsec6stat.out_noroute++;
3034 error = EHOSTUNREACH;
3038 /* adjust state->dst if tunnel endpoint is offlink */
3039 if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
3040 state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
3041 dst6 = (struct sockaddr_in6 *)state->dst;
3046 state->m = ipsec6_splithdr(state->m);
3048 ipsec6stat.out_nomem++;
3052 ip6 = mtod(state->m, struct ip6_hdr *);
3053 switch (isr->saidx.proto) {
3056 error = esp6_output(state->m, &ip6->ip6_nxt, state->m->m_next, isr);
3063 error = ah6_output(state->m, &ip6->ip6_nxt, state->m->m_next, isr);
3065 case IPPROTO_IPCOMP:
3066 /* XXX code should be here */
3069 ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
3070 "unknown ipsec protocol %d\n", isr->saidx.proto));
3072 ipsec6stat.out_inval++;
3080 plen = state->m->m_pkthdr.len - sizeof(struct ip6_hdr);
3081 if (plen > IPV6_MAXPACKET) {
3082 ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
3083 "IPsec with IPv6 jumbogram is not supported\n"));
3084 ipsec6stat.out_inval++;
3085 error = EINVAL; /* XXX */
3088 ip6 = mtod(state->m, struct ip6_hdr *);
3089 ip6->ip6_plen = htons(plen);
3103 * Chop IP header and option off from the payload.
3105 static struct mbuf *
3113 if (m->m_len < sizeof(struct ip))
3114 panic("ipsec4_splithdr: first mbuf too short");
3115 ip = mtod(m, struct ip *);
3117 hlen = _IP_VHL_HL(ip->ip_vhl) << 2;
3119 hlen = ip->ip_hl << 2;
3121 if (m->m_len > hlen) {
3122 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3127 M_MOVE_PKTHDR(mh, m);
3134 bcopy((caddr_t)ip, mtod(m, caddr_t), hlen);
3135 } else if (m->m_len < hlen) {
3136 m = m_pullup(m, hlen);
3145 static struct mbuf *
3150 struct ip6_hdr *ip6;
3153 if (m->m_len < sizeof(struct ip6_hdr))
3154 panic("ipsec6_splithdr: first mbuf too short");
3155 ip6 = mtod(m, struct ip6_hdr *);
3156 hlen = sizeof(struct ip6_hdr);
3157 if (m->m_len > hlen) {
3158 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3163 M_MOVE_PKTHDR(mh, m);
3170 bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
3171 } else if (m->m_len < hlen) {
3172 m = m_pullup(m, hlen);
3180 /* validate inbound IPsec tunnel packet. */
3182 ipsec4_tunnel_validate(m, off, nxt0, sav)
3183 struct mbuf *m; /* no pullup permitted, m->m_len >= ip */
3186 struct secasvar *sav;
3188 u_int8_t nxt = nxt0 & 0xff;
3189 struct sockaddr_in *sin;
3190 struct sockaddr_in osrc, odst, isrc, idst;
3192 struct secpolicy *sp;
3196 if (m->m_len < sizeof(struct ip))
3197 panic("too short mbuf on ipsec4_tunnel_validate");
3199 if (nxt != IPPROTO_IPV4)
3201 if (m->m_pkthdr.len < off + sizeof(struct ip))
3203 /* do not decapsulate if the SA is for transport mode only */
3204 if (sav->sah->saidx.mode == IPSEC_MODE_TRANSPORT)
3207 oip = mtod(m, struct ip *);
3209 hlen = _IP_VHL_HL(oip->ip_vhl) << 2;
3211 hlen = oip->ip_hl << 2;
3213 if (hlen != sizeof(struct ip))
3216 /* AF_INET6 should be supported, but at this moment we don't. */
3217 sin = (struct sockaddr_in *)&sav->sah->saidx.dst;
3218 if (sin->sin_family != AF_INET)
3220 if (bcmp(&oip->ip_dst, &sin->sin_addr, sizeof(oip->ip_dst)) != 0)
3224 bzero(&osrc, sizeof(osrc));
3225 bzero(&odst, sizeof(odst));
3226 bzero(&isrc, sizeof(isrc));
3227 bzero(&idst, sizeof(idst));
3228 osrc.sin_family = odst.sin_family = isrc.sin_family = idst.sin_family =
3230 osrc.sin_len = odst.sin_len = isrc.sin_len = idst.sin_len =
3231 sizeof(struct sockaddr_in);
3232 osrc.sin_addr = oip->ip_src;
3233 odst.sin_addr = oip->ip_dst;
3234 m_copydata(m, off + offsetof(struct ip, ip_src), sizeof(isrc.sin_addr),
3235 (caddr_t)&isrc.sin_addr);
3236 m_copydata(m, off + offsetof(struct ip, ip_dst), sizeof(idst.sin_addr),
3237 (caddr_t)&idst.sin_addr);
3240 * RFC2401 5.2.1 (b): (assume that we are using tunnel mode)
3241 * - if the inner destination is multicast address, there can be
3242 * multiple permissible inner source address. implementation
3243 * may want to skip verification of inner source address against
3245 * - if the inner protocol is ICMP, the packet may be an error report
3246 * from routers on the other side of the VPN cloud (R in the
3247 * following diagram). in this case, we cannot verify inner source
3248 * address against SPD selector.
3249 * me -- gw === gw -- R -- you
3251 * we consider the first bullet to be users responsibility on SPD entry
3252 * configuration (if you need to encrypt multicast traffic, set
3253 * the source range of SPD selector to 0.0.0.0/0, or have explicit
3254 * address ranges for possible senders).
3255 * the second bullet is not taken care of (yet).
3257 * therefore, we do not do anything special about inner source.
3260 sp = key_gettunnel((struct sockaddr *)&osrc, (struct sockaddr *)&odst,
3261 (struct sockaddr *)&isrc, (struct sockaddr *)&idst);
3270 /* validate inbound IPsec tunnel packet. */
3272 ipsec6_tunnel_validate(m, off, nxt0, sav)
3273 struct mbuf *m; /* no pullup permitted, m->m_len >= ip */
3276 struct secasvar *sav;
3278 u_int8_t nxt = nxt0 & 0xff;
3279 struct sockaddr_in6 *sin6;
3280 struct sockaddr_in6 osrc, odst, isrc, idst;
3281 struct secpolicy *sp;
3282 struct ip6_hdr *oip6;
3285 if (m->m_len < sizeof(struct ip6_hdr))
3286 panic("too short mbuf on ipsec6_tunnel_validate");
3288 if (nxt != IPPROTO_IPV6)
3290 if (m->m_pkthdr.len < off + sizeof(struct ip6_hdr))
3292 /* do not decapsulate if the SA is for transport mode only */
3293 if (sav->sah->saidx.mode == IPSEC_MODE_TRANSPORT)
3296 oip6 = mtod(m, struct ip6_hdr *);
3297 /* AF_INET should be supported, but at this moment we don't. */
3298 sin6 = (struct sockaddr_in6 *)&sav->sah->saidx.dst;
3299 if (sin6->sin6_family != AF_INET6)
3301 if (!IN6_ARE_ADDR_EQUAL(&oip6->ip6_dst, &sin6->sin6_addr))
3305 bzero(&osrc, sizeof(osrc));
3306 bzero(&odst, sizeof(odst));
3307 bzero(&isrc, sizeof(isrc));
3308 bzero(&idst, sizeof(idst));
3309 osrc.sin6_family = odst.sin6_family = isrc.sin6_family =
3310 idst.sin6_family = AF_INET6;
3311 osrc.sin6_len = odst.sin6_len = isrc.sin6_len = idst.sin6_len =
3312 sizeof(struct sockaddr_in6);
3313 osrc.sin6_addr = oip6->ip6_src;
3314 odst.sin6_addr = oip6->ip6_dst;
3315 m_copydata(m, off + offsetof(struct ip6_hdr, ip6_src),
3316 sizeof(isrc.sin6_addr), (caddr_t)&isrc.sin6_addr);
3317 m_copydata(m, off + offsetof(struct ip6_hdr, ip6_dst),
3318 sizeof(idst.sin6_addr), (caddr_t)&idst.sin6_addr);
3321 * regarding to inner source address validation, see a long comment
3322 * in ipsec4_tunnel_validate.
3325 sp = key_gettunnel((struct sockaddr *)&osrc, (struct sockaddr *)&odst,
3326 (struct sockaddr *)&isrc, (struct sockaddr *)&idst);
3328 * when there is no suitable inbound policy for the packet of the ipsec
3329 * tunnel mode, the kernel never decapsulate the tunneled packet
3330 * as the ipsec tunnel mode even when the system wide policy is "none".
3331 * then the kernel leaves the generic tunnel module to process this
3332 * packet. if there is no rule of the generic tunnel, the packet
3333 * is rejected and the statistics will be counted up.
3344 * Make a mbuf chain for encryption.
3345 * If the original mbuf chain contains a mbuf with a cluster,
3346 * allocate a new cluster and copy the data to the new cluster.
3347 * XXX: this hack is inefficient, but is necessary to handle cases
3348 * of TCP retransmission...
3354 struct mbuf *n, **mpp, *mnew;
3356 for (n = m, mpp = &m; n; n = n->m_next) {
3357 if (n->m_flags & M_EXT) {
3359 * Make a copy only if there are more than one references
3361 * XXX: is this approach effective?
3364 n->m_ext.ext_free ||
3365 mclrefcnt[mtocl(n->m_ext.ext_buf)] > 1
3371 if (n->m_flags & M_PKTHDR) {
3372 MGETHDR(mnew, M_DONTWAIT, MT_HEADER);
3375 if (!m_dup_pkthdr(mnew, n, M_DONTWAIT)) {
3381 MGET(mnew, M_DONTWAIT, MT_DATA);
3389 * Copy data. If we don't have enough space to
3390 * store the whole data, allocate a cluster
3391 * or additional mbufs.
3392 * XXX: we don't use m_copyback(), since the
3393 * function does not use clusters and thus is
3402 if (remain <= (mm->m_flags & M_PKTHDR ? MHLEN : MLEN))
3404 else { /* allocate a cluster */
3405 MCLGET(mm, M_DONTWAIT);
3406 if (!(mm->m_flags & M_EXT)) {
3410 len = remain < MCLBYTES ?
3414 bcopy(n->m_data + copied, mm->m_data,
3421 if (remain <= 0) /* completed? */
3424 /* need another mbuf */
3425 MGETHDR(mn, M_DONTWAIT, MT_HEADER);
3428 mn->m_pkthdr.rcvif = NULL;
3434 mm->m_next = m_free(n);
3458 while ((tag = m_tag_find(m, PACKET_TAG_IPSEC_HISTORY, NULL)) != NULL)
3459 m_tag_delete(m, tag);
3463 ipsec_addhist(m, proto, spi)
3469 struct ipsec_history *p;
3471 tag = m_tag_get(PACKET_TAG_IPSEC_HISTORY,
3472 sizeof (struct ipsec_history), M_NOWAIT);
3475 p = (struct ipsec_history *)(tag+1);
3476 bzero(p, sizeof(*p));
3477 p->ih_proto = proto;
3479 m_tag_prepend(m, tag);
3483 struct ipsec_history *
3484 ipsec_gethist(m, lenp)
3490 tag = m_tag_find(m, PACKET_TAG_IPSEC_HISTORY, NULL);
3493 /* XXX NB: noone uses this so fake it */
3495 *lenp = sizeof (struct ipsec_history);
3496 return ((struct ipsec_history *)(tag+1));