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.22 2008/05/27 01:10:43 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>
57 #include <sys/thread2.h>
60 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/in_var.h>
67 #include <netinet/udp.h>
68 #include <netinet/udp_var.h>
69 #include <netinet/ip_ecn.h>
71 #include <netinet6/ip6_ecn.h>
73 #include <netinet/tcp.h>
74 #include <netinet/udp.h>
76 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
80 #include <netinet/in_pcb.h>
82 #include <netinet/icmp6.h>
85 #include <netinet6/ipsec.h>
87 #include <netinet6/ipsec6.h>
89 #include <netinet6/ah.h>
91 #include <netinet6/ah6.h>
94 #include <netinet6/esp.h>
96 #include <netinet6/esp6.h>
99 #include <netinet6/ipcomp.h>
101 #include <netinet6/ipcomp6.h>
103 #include <netproto/key/key.h>
104 #include <netproto/key/keydb.h>
105 #include <netproto/key/key_debug.h>
107 #include <net/net_osdep.h>
115 struct ipsecstat ipsecstat;
116 int ip4_ah_cleartos = 1;
117 int ip4_ah_offsetmask = 0; /* maybe IP_DF? */
118 int ip4_ipsec_dfbit = 0; /* DF bit on encap. 0: clear 1: set 2: copy */
119 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
120 int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
121 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
122 int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
123 struct secpolicy ip4_def_policy;
124 int ip4_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
125 int ip4_esp_randpad = -1;
128 SYSCTL_DECL(_net_inet_ipsec);
130 SYSCTL_DECL(_net_inet6_ipsec6);
135 SYSCTL_STRUCT(_net_inet_ipsec, IPSECCTL_STATS,
136 stats, CTLFLAG_RD, &ipsecstat, ipsecstat, "");
137 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY,
138 def_policy, CTLFLAG_RW, &ip4_def_policy.policy, 0, "");
139 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
140 CTLFLAG_RW, &ip4_esp_trans_deflev, 0, "");
141 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
142 CTLFLAG_RW, &ip4_esp_net_deflev, 0, "");
143 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
144 CTLFLAG_RW, &ip4_ah_trans_deflev, 0, "");
145 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
146 CTLFLAG_RW, &ip4_ah_net_deflev, 0, "");
147 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS,
148 ah_cleartos, CTLFLAG_RW, &ip4_ah_cleartos, 0, "");
149 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK,
150 ah_offsetmask, CTLFLAG_RW, &ip4_ah_offsetmask, 0, "");
151 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT,
152 dfbit, CTLFLAG_RW, &ip4_ipsec_dfbit, 0, "");
153 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN,
154 ecn, CTLFLAG_RW, &ip4_ipsec_ecn, 0, "");
155 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG,
156 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
157 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ESP_RANDPAD,
158 esp_randpad, CTLFLAG_RW, &ip4_esp_randpad, 0, "");
161 struct ipsecstat ipsec6stat;
162 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
163 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
164 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
165 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
166 struct secpolicy ip6_def_policy;
167 int ip6_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
168 int ip6_esp_randpad = -1;
170 /* net.inet6.ipsec6 */
171 SYSCTL_STRUCT(_net_inet6_ipsec6, IPSECCTL_STATS,
172 stats, CTLFLAG_RD, &ipsec6stat, ipsecstat, "");
173 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY,
174 def_policy, CTLFLAG_RW, &ip6_def_policy.policy, 0, "");
175 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
176 CTLFLAG_RW, &ip6_esp_trans_deflev, 0, "");
177 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
178 CTLFLAG_RW, &ip6_esp_net_deflev, 0, "");
179 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
180 CTLFLAG_RW, &ip6_ah_trans_deflev, 0, "");
181 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
182 CTLFLAG_RW, &ip6_ah_net_deflev, 0, "");
183 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN,
184 ecn, CTLFLAG_RW, &ip6_ipsec_ecn, 0, "");
185 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG,
186 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
187 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ESP_RANDPAD,
188 esp_randpad, CTLFLAG_RW, &ip6_esp_randpad, 0, "");
191 static int ipsec_setspidx_mbuf
192 (struct secpolicyindex *, u_int, u_int, struct mbuf *, int);
193 static int ipsec4_setspidx_inpcb (struct mbuf *, struct inpcb *pcb);
195 static int ipsec6_setspidx_in6pcb (struct mbuf *, struct in6pcb *pcb);
197 static int ipsec_setspidx (struct mbuf *, struct secpolicyindex *, int);
198 static void ipsec4_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
199 static int ipsec4_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
201 static void ipsec6_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
202 static int ipsec6_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
204 static struct inpcbpolicy *ipsec_newpcbpolicy (void);
205 static void ipsec_delpcbpolicy (struct inpcbpolicy *);
206 static struct secpolicy *ipsec_deepcopy_policy (struct secpolicy *src);
207 static int ipsec_set_policy (struct secpolicy **pcb_sp,
208 int optname, caddr_t request, size_t len, int priv);
209 static int ipsec_get_policy (struct secpolicy *pcb_sp, struct mbuf **mp);
210 static void vshiftl (unsigned char *, int, int);
211 static int ipsec_in_reject (struct secpolicy *, struct mbuf *);
212 static size_t ipsec_hdrsiz (struct secpolicy *);
214 static struct mbuf *ipsec4_splithdr (struct mbuf *);
217 static struct mbuf *ipsec6_splithdr (struct mbuf *);
220 static int ipsec4_encapsulate (struct mbuf *, struct secasvar *);
223 static int ipsec6_encapsulate (struct mbuf *, struct secasvar *);
227 * For OUTBOUND packet having a socket. Searching SPD for packet,
228 * and return a pointer to SP.
229 * OUT: NULL: no apropreate SP found, the following value is set to error.
231 * EACCES : discard packet.
232 * ENOENT : ipsec_acquire() in progress, maybe.
233 * others : error occured.
234 * others: a pointer to SP
236 * NOTE: IPv6 mapped adddress concern is implemented here.
239 ipsec4_getpolicybysock(struct mbuf *m, u_int dir, struct socket *so, int *error)
241 struct inpcbpolicy *pcbsp = NULL;
242 struct secpolicy *currsp = NULL; /* policy on socket */
243 struct secpolicy *kernsp = NULL; /* policy on kernel */
246 if (m == NULL || so == NULL || error == NULL)
247 panic("ipsec4_getpolicybysock: NULL pointer was passed.");
249 switch (so->so_proto->pr_domain->dom_family) {
251 /* set spidx in pcb */
252 *error = ipsec4_setspidx_inpcb(m, so->so_pcb);
256 /* set spidx in pcb */
257 *error = ipsec6_setspidx_in6pcb(m, so->so_pcb);
261 panic("ipsec4_getpolicybysock: unsupported address family\n");
265 switch (so->so_proto->pr_domain->dom_family) {
267 pcbsp = sotoinpcb(so)->inp_sp;
271 pcbsp = sotoin6pcb(so)->in6p_sp;
278 panic("ipsec4_getpolicybysock: pcbsp is NULL.");
281 case IPSEC_DIR_INBOUND:
282 currsp = pcbsp->sp_in;
284 case IPSEC_DIR_OUTBOUND:
285 currsp = pcbsp->sp_out;
288 panic("ipsec4_getpolicybysock: illegal direction.");
293 panic("ipsec4_getpolicybysock: currsp is NULL.");
295 /* when privilieged socket */
297 switch (currsp->policy) {
298 case IPSEC_POLICY_BYPASS:
303 case IPSEC_POLICY_ENTRUST:
304 /* look for a policy in SPD */
305 kernsp = key_allocsp(&currsp->spidx, dir);
308 if (kernsp != NULL) {
309 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
310 kprintf("DP ipsec4_getpolicybysock called "
311 "to allocate SP:%p\n", kernsp));
317 if (ip4_def_policy.policy != IPSEC_POLICY_DISCARD
318 && ip4_def_policy.policy != IPSEC_POLICY_NONE) {
320 "fixed system default policy: %d->%d\n",
321 ip4_def_policy.policy, IPSEC_POLICY_NONE));
322 ip4_def_policy.policy = IPSEC_POLICY_NONE;
324 ip4_def_policy.refcnt++;
326 return &ip4_def_policy;
328 case IPSEC_POLICY_IPSEC:
334 ipseclog((LOG_ERR, "ipsec4_getpolicybysock: "
335 "Invalid policy for PCB %d\n", currsp->policy));
342 /* when non-privilieged socket */
343 /* look for a policy in SPD */
344 kernsp = key_allocsp(&currsp->spidx, dir);
347 if (kernsp != NULL) {
348 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
349 kprintf("DP ipsec4_getpolicybysock called "
350 "to allocate SP:%p\n", kernsp));
356 switch (currsp->policy) {
357 case IPSEC_POLICY_BYPASS:
358 ipseclog((LOG_ERR, "ipsec4_getpolicybysock: "
359 "Illegal policy for non-priviliged defined %d\n",
364 case IPSEC_POLICY_ENTRUST:
365 if (ip4_def_policy.policy != IPSEC_POLICY_DISCARD
366 && ip4_def_policy.policy != IPSEC_POLICY_NONE) {
368 "fixed system default policy: %d->%d\n",
369 ip4_def_policy.policy, IPSEC_POLICY_NONE));
370 ip4_def_policy.policy = IPSEC_POLICY_NONE;
372 ip4_def_policy.refcnt++;
374 return &ip4_def_policy;
376 case IPSEC_POLICY_IPSEC:
382 ipseclog((LOG_ERR, "ipsec4_getpolicybysock: "
383 "Invalid policy for PCB %d\n", currsp->policy));
391 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
392 * and return a pointer to SP.
393 * OUT: positive: a pointer to the entry for security policy leaf matched.
394 * NULL: no apropreate SP found, the following value is set to error.
396 * EACCES : discard packet.
397 * ENOENT : ipsec_acquire() in progress, maybe.
398 * others : error occured.
401 ipsec4_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
403 struct secpolicy *sp = NULL;
406 if (m == NULL || error == NULL)
407 panic("ipsec4_getpolicybyaddr: NULL pointer was passed.");
410 struct secpolicyindex spidx;
412 bzero(&spidx, sizeof(spidx));
414 /* make a index to look for a policy */
415 *error = ipsec_setspidx_mbuf(&spidx, dir, AF_INET, m,
416 (flag & IP_FORWARDING) ? 0 : 1);
421 sp = key_allocsp(&spidx, dir);
426 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
427 kprintf("DP ipsec4_getpolicybyaddr called "
428 "to allocate SP:%p\n", sp));
434 if (ip4_def_policy.policy != IPSEC_POLICY_DISCARD
435 && ip4_def_policy.policy != IPSEC_POLICY_NONE) {
436 ipseclog((LOG_INFO, "fixed system default policy:%d->%d\n",
437 ip4_def_policy.policy,
439 ip4_def_policy.policy = IPSEC_POLICY_NONE;
441 ip4_def_policy.refcnt++;
443 return &ip4_def_policy;
448 * For OUTBOUND packet having a socket. Searching SPD for packet,
449 * and return a pointer to SP.
450 * OUT: NULL: no apropreate SP found, the following value is set to error.
452 * EACCES : discard packet.
453 * ENOENT : ipsec_acquire() in progress, maybe.
454 * others : error occured.
455 * others: a pointer to SP
458 ipsec6_getpolicybysock(struct mbuf *m, u_int dir, struct socket *so, int *error)
460 struct inpcbpolicy *pcbsp = NULL;
461 struct secpolicy *currsp = NULL; /* policy on socket */
462 struct secpolicy *kernsp = NULL; /* policy on kernel */
465 if (m == NULL || so == NULL || error == NULL)
466 panic("ipsec6_getpolicybysock: NULL pointer was passed.");
469 if (so->so_proto->pr_domain->dom_family != AF_INET6)
470 panic("ipsec6_getpolicybysock: socket domain != inet6");
473 /* set spidx in pcb */
474 ipsec6_setspidx_in6pcb(m, so->so_pcb);
476 pcbsp = sotoin6pcb(so)->in6p_sp;
480 panic("ipsec6_getpolicybysock: pcbsp is NULL.");
483 case IPSEC_DIR_INBOUND:
484 currsp = pcbsp->sp_in;
486 case IPSEC_DIR_OUTBOUND:
487 currsp = pcbsp->sp_out;
490 panic("ipsec6_getpolicybysock: illegal direction.");
495 panic("ipsec6_getpolicybysock: currsp is NULL.");
497 /* when privilieged socket */
499 switch (currsp->policy) {
500 case IPSEC_POLICY_BYPASS:
505 case IPSEC_POLICY_ENTRUST:
506 /* look for a policy in SPD */
507 kernsp = key_allocsp(&currsp->spidx, dir);
510 if (kernsp != NULL) {
511 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
512 kprintf("DP ipsec6_getpolicybysock called "
513 "to allocate SP:%p\n", kernsp));
519 if (ip6_def_policy.policy != IPSEC_POLICY_DISCARD
520 && ip6_def_policy.policy != IPSEC_POLICY_NONE) {
522 "fixed system default policy: %d->%d\n",
523 ip6_def_policy.policy, IPSEC_POLICY_NONE));
524 ip6_def_policy.policy = IPSEC_POLICY_NONE;
526 ip6_def_policy.refcnt++;
528 return &ip6_def_policy;
530 case IPSEC_POLICY_IPSEC:
536 ipseclog((LOG_ERR, "ipsec6_getpolicybysock: "
537 "Invalid policy for PCB %d\n", currsp->policy));
544 /* when non-privilieged socket */
545 /* look for a policy in SPD */
546 kernsp = key_allocsp(&currsp->spidx, dir);
549 if (kernsp != NULL) {
550 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
551 kprintf("DP ipsec6_getpolicybysock called "
552 "to allocate SP:%p\n", kernsp));
558 switch (currsp->policy) {
559 case IPSEC_POLICY_BYPASS:
560 ipseclog((LOG_ERR, "ipsec6_getpolicybysock: "
561 "Illegal policy for non-priviliged defined %d\n",
566 case IPSEC_POLICY_ENTRUST:
567 if (ip6_def_policy.policy != IPSEC_POLICY_DISCARD
568 && ip6_def_policy.policy != IPSEC_POLICY_NONE) {
570 "fixed system default policy: %d->%d\n",
571 ip6_def_policy.policy, IPSEC_POLICY_NONE));
572 ip6_def_policy.policy = IPSEC_POLICY_NONE;
574 ip6_def_policy.refcnt++;
576 return &ip6_def_policy;
578 case IPSEC_POLICY_IPSEC:
585 "ipsec6_policybysock: Invalid policy for PCB %d\n",
594 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
595 * and return a pointer to SP.
596 * `flag' means that packet is to be forwarded whether or not.
598 * OUT: positive: a pointer to the entry for security policy leaf matched.
599 * NULL: no apropreate SP found, the following value is set to error.
601 * EACCES : discard packet.
602 * ENOENT : ipsec_acquire() in progress, maybe.
603 * others : error occured.
605 #ifndef IP_FORWARDING
606 #define IP_FORWARDING 1
610 ipsec6_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
612 struct secpolicy *sp = NULL;
615 if (m == NULL || error == NULL)
616 panic("ipsec6_getpolicybyaddr: NULL pointer was passed.");
619 struct secpolicyindex spidx;
621 bzero(&spidx, sizeof(spidx));
623 /* make a index to look for a policy */
624 *error = ipsec_setspidx_mbuf(&spidx, dir, AF_INET6, m,
625 (flag & IP_FORWARDING) ? 0 : 1);
630 sp = key_allocsp(&spidx, dir);
635 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
636 kprintf("DP ipsec6_getpolicybyaddr called "
637 "to allocate SP:%p\n", sp));
643 if (ip6_def_policy.policy != IPSEC_POLICY_DISCARD
644 && ip6_def_policy.policy != IPSEC_POLICY_NONE) {
645 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
646 ip6_def_policy.policy, IPSEC_POLICY_NONE));
647 ip6_def_policy.policy = IPSEC_POLICY_NONE;
649 ip6_def_policy.refcnt++;
651 return &ip6_def_policy;
656 * set IP address into spidx from mbuf.
657 * When Forwarding packet and ICMP echo reply, this function is used.
659 * IN: get the followings from mbuf.
660 * protocol family, src, dst, next protocol
663 * other: failure, and set errno.
666 ipsec_setspidx_mbuf(struct secpolicyindex *spidx, u_int dir, u_int family,
667 struct mbuf *m, int needport)
672 if (spidx == NULL || m == NULL)
673 panic("ipsec_setspidx_mbuf: NULL pointer was passed.");
675 bzero(spidx, sizeof(*spidx));
677 error = ipsec_setspidx(m, spidx, needport);
686 bzero(spidx, sizeof(*spidx));
691 ipsec4_setspidx_inpcb(struct mbuf *m, struct inpcb *pcb)
693 struct secpolicyindex *spidx;
698 panic("ipsec4_setspidx_inpcb: no PCB found.");
699 if (pcb->inp_sp == NULL)
700 panic("ipsec4_setspidx_inpcb: no inp_sp found.");
701 if (pcb->inp_sp->sp_out == NULL || pcb->inp_sp->sp_in == NULL)
702 panic("ipsec4_setspidx_inpcb: no sp_in/out found.");
704 bzero(&pcb->inp_sp->sp_in->spidx, sizeof(*spidx));
705 bzero(&pcb->inp_sp->sp_out->spidx, sizeof(*spidx));
707 spidx = &pcb->inp_sp->sp_in->spidx;
708 error = ipsec_setspidx(m, spidx, 1);
711 spidx->dir = IPSEC_DIR_INBOUND;
713 spidx = &pcb->inp_sp->sp_out->spidx;
714 error = ipsec_setspidx(m, spidx, 1);
717 spidx->dir = IPSEC_DIR_OUTBOUND;
722 bzero(&pcb->inp_sp->sp_in->spidx, sizeof(*spidx));
723 bzero(&pcb->inp_sp->sp_out->spidx, sizeof(*spidx));
729 ipsec6_setspidx_in6pcb(struct mbuf *m, struct in6pcb *pcb)
731 struct secpolicyindex *spidx;
736 panic("ipsec6_setspidx_in6pcb: no PCB found.");
737 if (pcb->in6p_sp == NULL)
738 panic("ipsec6_setspidx_in6pcb: no in6p_sp found.");
739 if (pcb->in6p_sp->sp_out == NULL || pcb->in6p_sp->sp_in == NULL)
740 panic("ipsec6_setspidx_in6pcb: no sp_in/out found.");
742 bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
743 bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
745 spidx = &pcb->in6p_sp->sp_in->spidx;
746 error = ipsec_setspidx(m, spidx, 1);
749 spidx->dir = IPSEC_DIR_INBOUND;
751 spidx = &pcb->in6p_sp->sp_out->spidx;
752 error = ipsec_setspidx(m, spidx, 1);
755 spidx->dir = IPSEC_DIR_OUTBOUND;
760 bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
761 bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
767 * configure security policy index (src/dst/proto/sport/dport)
768 * by looking at the content of mbuf.
769 * the caller is responsible for error recovery (like clearing up spidx).
772 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
774 struct ip *ip = NULL;
782 panic("ipsec_setspidx: m == 0 passed.");
785 * validate m->m_pkthdr.len. we see incorrect length if we
786 * mistakenly call this function with inconsistent mbuf chain
787 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
790 for (n = m; n; n = n->m_next)
792 if (m->m_pkthdr.len != len) {
793 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
794 kprintf("ipsec_setspidx: "
795 "total of m_len(%d) != pkthdr.len(%d), "
797 len, m->m_pkthdr.len));
801 if (m->m_pkthdr.len < sizeof(struct ip)) {
802 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
803 kprintf("ipsec_setspidx: "
804 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
809 if (m->m_len >= sizeof(*ip))
810 ip = mtod(m, struct ip *);
812 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
816 v = _IP_VHL_V(ip->ip_vhl);
822 error = ipsec4_setspidx_ipaddr(m, spidx);
825 ipsec4_get_ulp(m, spidx, needport);
829 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
830 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
831 kprintf("ipsec_setspidx: "
832 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
833 "ignored.\n", m->m_pkthdr.len));
836 error = ipsec6_setspidx_ipaddr(m, spidx);
839 ipsec6_get_ulp(m, spidx, needport);
843 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
844 kprintf("ipsec_setspidx: "
845 "unknown IP version %u, ignored.\n", v));
851 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
862 panic("ipsec4_get_ulp: NULL pointer was passed.");
863 if (m->m_pkthdr.len < sizeof(ip))
864 panic("ipsec4_get_ulp: too short");
867 spidx->ul_proto = IPSEC_ULPROTO_ANY;
868 ((struct sockaddr_in *)&spidx->src)->sin_port = IPSEC_PORT_ANY;
869 ((struct sockaddr_in *)&spidx->dst)->sin_port = IPSEC_PORT_ANY;
871 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
872 /* ip_input() flips it into host endian XXX need more checking */
873 if (ip.ip_off & (IP_MF | IP_OFFMASK))
878 off = _IP_VHL_HL(ip->ip_vhl) << 2;
882 while (off < m->m_pkthdr.len) {
885 spidx->ul_proto = nxt;
888 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
890 m_copydata(m, off, sizeof(th), (caddr_t)&th);
891 ((struct sockaddr_in *)&spidx->src)->sin_port =
893 ((struct sockaddr_in *)&spidx->dst)->sin_port =
897 spidx->ul_proto = nxt;
900 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
902 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
903 ((struct sockaddr_in *)&spidx->src)->sin_port =
905 ((struct sockaddr_in *)&spidx->dst)->sin_port =
909 if (off + sizeof(ip6e) > m->m_pkthdr.len)
911 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
912 off += (ip6e.ip6e_len + 2) << 2;
917 /* XXX intermediate headers??? */
918 spidx->ul_proto = nxt;
924 /* assumes that m is sane */
926 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
928 struct ip *ip = NULL;
930 struct sockaddr_in *sin;
932 if (m->m_len >= sizeof(*ip))
933 ip = mtod(m, struct ip *);
935 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
939 sin = (struct sockaddr_in *)&spidx->src;
940 bzero(sin, sizeof(*sin));
941 sin->sin_family = AF_INET;
942 sin->sin_len = sizeof(struct sockaddr_in);
943 bcopy(&ip->ip_src, &sin->sin_addr, sizeof(ip->ip_src));
944 spidx->prefs = sizeof(struct in_addr) << 3;
946 sin = (struct sockaddr_in *)&spidx->dst;
947 bzero(sin, sizeof(*sin));
948 sin->sin_family = AF_INET;
949 sin->sin_len = sizeof(struct sockaddr_in);
950 bcopy(&ip->ip_dst, &sin->sin_addr, sizeof(ip->ip_dst));
951 spidx->prefd = sizeof(struct in_addr) << 3;
957 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
965 panic("ipsec6_get_ulp: NULL pointer was passed.");
967 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
968 kprintf("ipsec6_get_ulp:\n"); kdebug_mbuf(m));
971 spidx->ul_proto = IPSEC_ULPROTO_ANY;
972 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
973 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
976 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
977 if (off < 0 || m->m_pkthdr.len < off)
982 spidx->ul_proto = nxt;
985 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
987 m_copydata(m, off, sizeof(th), (caddr_t)&th);
988 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
989 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
992 spidx->ul_proto = nxt;
995 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
997 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
998 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
999 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
1001 case IPPROTO_ICMPV6:
1003 /* XXX intermediate headers??? */
1004 spidx->ul_proto = nxt;
1009 /* assumes that m is sane */
1011 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
1013 struct ip6_hdr *ip6 = NULL;
1014 struct ip6_hdr ip6buf;
1015 struct sockaddr_in6 *sin6;
1017 if (m->m_len >= sizeof(*ip6))
1018 ip6 = mtod(m, struct ip6_hdr *);
1020 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
1024 sin6 = (struct sockaddr_in6 *)&spidx->src;
1025 bzero(sin6, sizeof(*sin6));
1026 sin6->sin6_family = AF_INET6;
1027 sin6->sin6_len = sizeof(struct sockaddr_in6);
1028 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
1029 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
1030 sin6->sin6_addr.s6_addr16[1] = 0;
1031 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
1033 spidx->prefs = sizeof(struct in6_addr) << 3;
1035 sin6 = (struct sockaddr_in6 *)&spidx->dst;
1036 bzero(sin6, sizeof(*sin6));
1037 sin6->sin6_family = AF_INET6;
1038 sin6->sin6_len = sizeof(struct sockaddr_in6);
1039 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
1040 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
1041 sin6->sin6_addr.s6_addr16[1] = 0;
1042 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
1044 spidx->prefd = sizeof(struct in6_addr) << 3;
1050 static struct inpcbpolicy *
1051 ipsec_newpcbpolicy(void)
1053 struct inpcbpolicy *p;
1055 p = (struct inpcbpolicy *)kmalloc(sizeof(*p), M_SECA, M_NOWAIT);
1060 ipsec_delpcbpolicy(struct inpcbpolicy *p)
1065 /* initialize policy in PCB */
1067 ipsec_init_policy(struct socket *so, struct inpcbpolicy **pcb_sp)
1069 struct inpcbpolicy *new;
1072 if (so == NULL || pcb_sp == NULL)
1073 panic("ipsec_init_policy: NULL pointer was passed.");
1075 new = ipsec_newpcbpolicy();
1077 ipseclog((LOG_DEBUG, "ipsec_init_policy: No more memory.\n"));
1080 bzero(new, sizeof(*new));
1082 if (so->so_cred != 0 && so->so_cred->cr_uid == 0)
1087 if ((new->sp_in = key_newsp()) == NULL) {
1088 ipsec_delpcbpolicy(new);
1091 new->sp_in->state = IPSEC_SPSTATE_ALIVE;
1092 new->sp_in->policy = IPSEC_POLICY_ENTRUST;
1094 if ((new->sp_out = key_newsp()) == NULL) {
1095 key_freesp(new->sp_in);
1096 ipsec_delpcbpolicy(new);
1099 new->sp_out->state = IPSEC_SPSTATE_ALIVE;
1100 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
1107 /* copy old ipsec policy into new */
1109 ipsec_copy_policy(struct inpcbpolicy *old, struct inpcbpolicy *new)
1111 struct secpolicy *sp;
1113 sp = ipsec_deepcopy_policy(old->sp_in);
1115 key_freesp(new->sp_in);
1120 sp = ipsec_deepcopy_policy(old->sp_out);
1122 key_freesp(new->sp_out);
1127 new->priv = old->priv;
1132 /* deep-copy a policy in PCB */
1133 static struct secpolicy *
1134 ipsec_deepcopy_policy(struct secpolicy *src)
1136 struct ipsecrequest *newchain = NULL;
1137 struct ipsecrequest *p;
1138 struct ipsecrequest **q;
1139 struct ipsecrequest *r;
1140 struct secpolicy *dst;
1143 if (src == NULL || dst == NULL)
1147 * deep-copy IPsec request chain. This is required since struct
1148 * ipsecrequest is not reference counted.
1151 for (p = src->req; p; p = p->next) {
1152 *q = (struct ipsecrequest *)kmalloc(sizeof(struct ipsecrequest),
1153 M_SECA, M_NOWAIT | M_ZERO);
1158 (*q)->saidx.proto = p->saidx.proto;
1159 (*q)->saidx.mode = p->saidx.mode;
1160 (*q)->level = p->level;
1161 (*q)->saidx.reqid = p->saidx.reqid;
1163 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
1164 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
1172 dst->req = newchain;
1173 dst->state = src->state;
1174 dst->policy = src->policy;
1175 /* do not touch the refcnt fields */
1180 for (p = newchain; p; p = r) {
1188 /* set policy and ipsec request if present. */
1190 ipsec_set_policy(struct secpolicy **pcb_sp, int optname, caddr_t request,
1191 size_t len, int priv)
1193 struct sadb_x_policy *xpl;
1194 struct secpolicy *newsp = NULL;
1198 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
1200 if (len < sizeof(*xpl))
1202 xpl = (struct sadb_x_policy *)request;
1204 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1205 kprintf("ipsec_set_policy: passed policy\n");
1206 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
1208 /* check policy type */
1209 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1210 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
1211 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1214 /* check privileged socket */
1215 if (priv == 0 && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS)
1218 /* allocation new SP entry */
1219 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1222 newsp->state = IPSEC_SPSTATE_ALIVE;
1224 /* clear old SP and set new SP */
1225 key_freesp(*pcb_sp);
1227 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1228 kprintf("ipsec_set_policy: new policy\n");
1229 kdebug_secpolicy(newsp));
1235 ipsec_get_policy(struct secpolicy *pcb_sp, struct mbuf **mp)
1239 if (pcb_sp == NULL || mp == NULL)
1242 *mp = key_sp2msg(pcb_sp);
1244 ipseclog((LOG_DEBUG, "ipsec_get_policy: No more memory.\n"));
1248 KKASSERT((*mp)->m_type == MT_DATA);
1249 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1250 kprintf("ipsec_get_policy:\n");
1257 ipsec4_set_policy(struct inpcb *inp, int optname, caddr_t request, size_t len,
1260 struct sadb_x_policy *xpl;
1261 struct secpolicy **pcb_sp;
1264 if (inp == NULL || request == NULL)
1266 if (len < sizeof(*xpl))
1268 xpl = (struct sadb_x_policy *)request;
1270 /* select direction */
1271 switch (xpl->sadb_x_policy_dir) {
1272 case IPSEC_DIR_INBOUND:
1273 pcb_sp = &inp->inp_sp->sp_in;
1275 case IPSEC_DIR_OUTBOUND:
1276 pcb_sp = &inp->inp_sp->sp_out;
1279 ipseclog((LOG_ERR, "ipsec4_set_policy: invalid direction=%u\n",
1280 xpl->sadb_x_policy_dir));
1284 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1288 ipsec4_get_policy(struct inpcb *inp, caddr_t request, size_t len,
1291 struct sadb_x_policy *xpl;
1292 struct secpolicy *pcb_sp;
1295 if (inp == NULL || request == NULL || mp == NULL)
1297 if (inp->inp_sp == NULL)
1298 panic("policy in PCB is NULL");
1299 if (len < sizeof(*xpl))
1301 xpl = (struct sadb_x_policy *)request;
1303 /* select direction */
1304 switch (xpl->sadb_x_policy_dir) {
1305 case IPSEC_DIR_INBOUND:
1306 pcb_sp = inp->inp_sp->sp_in;
1308 case IPSEC_DIR_OUTBOUND:
1309 pcb_sp = inp->inp_sp->sp_out;
1312 ipseclog((LOG_ERR, "ipsec4_set_policy: invalid direction=%u\n",
1313 xpl->sadb_x_policy_dir));
1317 return ipsec_get_policy(pcb_sp, mp);
1320 /* delete policy in PCB */
1322 ipsec4_delete_pcbpolicy(struct inpcb *inp)
1326 panic("ipsec4_delete_pcbpolicy: NULL pointer was passed.");
1328 if (inp->inp_sp == NULL)
1331 if (inp->inp_sp->sp_in != NULL) {
1332 key_freesp(inp->inp_sp->sp_in);
1333 inp->inp_sp->sp_in = NULL;
1336 if (inp->inp_sp->sp_out != NULL) {
1337 key_freesp(inp->inp_sp->sp_out);
1338 inp->inp_sp->sp_out = NULL;
1341 ipsec_delpcbpolicy(inp->inp_sp);
1349 ipsec6_set_policy(struct in6pcb *in6p, int optname, caddr_t request, size_t len,
1352 struct sadb_x_policy *xpl;
1353 struct secpolicy **pcb_sp;
1356 if (in6p == NULL || request == NULL)
1358 if (len < sizeof(*xpl))
1360 xpl = (struct sadb_x_policy *)request;
1362 /* select direction */
1363 switch (xpl->sadb_x_policy_dir) {
1364 case IPSEC_DIR_INBOUND:
1365 pcb_sp = &in6p->in6p_sp->sp_in;
1367 case IPSEC_DIR_OUTBOUND:
1368 pcb_sp = &in6p->in6p_sp->sp_out;
1371 ipseclog((LOG_ERR, "ipsec6_set_policy: invalid direction=%u\n",
1372 xpl->sadb_x_policy_dir));
1376 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1380 ipsec6_get_policy(struct in6pcb *in6p, caddr_t request, size_t len,
1383 struct sadb_x_policy *xpl;
1384 struct secpolicy *pcb_sp;
1387 if (in6p == NULL || request == NULL || mp == NULL)
1389 if (in6p->in6p_sp == NULL)
1390 panic("policy in PCB is NULL");
1391 if (len < sizeof(*xpl))
1393 xpl = (struct sadb_x_policy *)request;
1395 /* select direction */
1396 switch (xpl->sadb_x_policy_dir) {
1397 case IPSEC_DIR_INBOUND:
1398 pcb_sp = in6p->in6p_sp->sp_in;
1400 case IPSEC_DIR_OUTBOUND:
1401 pcb_sp = in6p->in6p_sp->sp_out;
1404 ipseclog((LOG_ERR, "ipsec6_set_policy: invalid direction=%u\n",
1405 xpl->sadb_x_policy_dir));
1409 return ipsec_get_policy(pcb_sp, mp);
1413 ipsec6_delete_pcbpolicy(struct in6pcb *in6p)
1417 panic("ipsec6_delete_pcbpolicy: NULL pointer was passed.");
1419 if (in6p->in6p_sp == NULL)
1422 if (in6p->in6p_sp->sp_in != NULL) {
1423 key_freesp(in6p->in6p_sp->sp_in);
1424 in6p->in6p_sp->sp_in = NULL;
1427 if (in6p->in6p_sp->sp_out != NULL) {
1428 key_freesp(in6p->in6p_sp->sp_out);
1429 in6p->in6p_sp->sp_out = NULL;
1432 ipsec_delpcbpolicy(in6p->in6p_sp);
1433 in6p->in6p_sp = NULL;
1440 * return current level.
1441 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1444 ipsec_get_reqlevel(struct ipsecrequest *isr)
1447 u_int esp_trans_deflev, esp_net_deflev, ah_trans_deflev, ah_net_deflev;
1450 if (isr == NULL || isr->sp == NULL)
1451 panic("ipsec_get_reqlevel: NULL pointer is passed.");
1452 if (((struct sockaddr *)&isr->sp->spidx.src)->sa_family
1453 != ((struct sockaddr *)&isr->sp->spidx.dst)->sa_family)
1454 panic("ipsec_get_reqlevel: family mismatched.");
1456 /* XXX note that we have ipseclog() expanded here - code sync issue */
1457 #define IPSEC_CHECK_DEFAULT(lev) \
1458 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1459 && (lev) != IPSEC_LEVEL_UNIQUE) \
1461 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1462 (lev), IPSEC_LEVEL_REQUIRE) \
1464 (lev) = IPSEC_LEVEL_REQUIRE, \
1468 /* set default level */
1469 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1472 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1473 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1474 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1475 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1480 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1481 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1482 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1483 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1487 panic("key_get_reqlevel: Unknown family. %d",
1488 ((struct sockaddr *)&isr->sp->spidx.src)->sa_family);
1491 #undef IPSEC_CHECK_DEFAULT
1494 switch (isr->level) {
1495 case IPSEC_LEVEL_DEFAULT:
1496 switch (isr->saidx.proto) {
1498 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1499 level = esp_net_deflev;
1501 level = esp_trans_deflev;
1504 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1505 level = ah_net_deflev;
1507 level = ah_trans_deflev;
1508 case IPPROTO_IPCOMP:
1510 * we don't really care, as IPcomp document says that
1511 * we shouldn't compress small packets
1513 level = IPSEC_LEVEL_USE;
1516 panic("ipsec_get_reqlevel: "
1517 "Illegal protocol defined %u",
1522 case IPSEC_LEVEL_USE:
1523 case IPSEC_LEVEL_REQUIRE:
1526 case IPSEC_LEVEL_UNIQUE:
1527 level = IPSEC_LEVEL_REQUIRE;
1531 panic("ipsec_get_reqlevel: Illegal IPsec level %u",
1539 * Check AH/ESP integrity.
1545 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1547 struct ipsecrequest *isr;
1549 int need_auth, need_conf, need_icv;
1551 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1552 kprintf("ipsec_in_reject: using SP\n");
1553 kdebug_secpolicy(sp));
1556 switch (sp->policy) {
1557 case IPSEC_POLICY_DISCARD:
1559 case IPSEC_POLICY_BYPASS:
1560 case IPSEC_POLICY_NONE:
1563 case IPSEC_POLICY_IPSEC:
1566 case IPSEC_POLICY_ENTRUST:
1568 panic("ipsec_hdrsiz: Invalid policy found. %d", sp->policy);
1575 /* XXX should compare policy against ipsec header history */
1577 for (isr = sp->req; isr != NULL; isr = isr->next) {
1579 /* get current level */
1580 level = ipsec_get_reqlevel(isr);
1582 switch (isr->saidx.proto) {
1584 if (level == IPSEC_LEVEL_REQUIRE) {
1587 if (isr->sav != NULL
1588 && isr->sav->flags == SADB_X_EXT_NONE
1589 && isr->sav->alg_auth != SADB_AALG_NONE)
1594 if (level == IPSEC_LEVEL_REQUIRE) {
1599 case IPPROTO_IPCOMP:
1601 * we don't really care, as IPcomp document says that
1602 * we shouldn't compress small packets, IPComp policy
1603 * should always be treated as being in "use" level.
1609 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1610 kprintf("ipsec_in_reject: auth:%d conf:%d icv:%d m_flags:%x\n",
1611 need_auth, need_conf, need_icv, m->m_flags));
1613 if ((need_conf && !(m->m_flags & M_DECRYPTED))
1614 || (!need_auth && need_icv && !(m->m_flags & M_AUTHIPDGM))
1615 || (need_auth && !(m->m_flags & M_AUTHIPHDR)))
1622 * Check AH/ESP integrity.
1623 * This function is called from tcp_input(), udp_input(),
1624 * and {ah,esp}4_input for tunnel mode
1627 ipsec4_in_reject_so(struct mbuf *m, struct socket *so)
1629 struct secpolicy *sp = NULL;
1635 return 0; /* XXX should be panic ? */
1637 /* get SP for this packet.
1638 * When we are called from ip_forward(), we call
1639 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1642 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1644 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_INBOUND, so, &error);
1647 return 0; /* XXX should be panic ?
1648 * -> No, there may be error. */
1650 result = ipsec_in_reject(sp, m);
1651 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1652 kprintf("DP ipsec4_in_reject_so call free SP:%p\n", sp));
1659 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp)
1662 return ipsec4_in_reject_so(m, NULL);
1663 if (inp->inp_socket)
1664 return ipsec4_in_reject_so(m, inp->inp_socket);
1666 panic("ipsec4_in_reject: invalid inpcb/socket");
1671 * Check AH/ESP integrity.
1672 * This function is called from tcp6_input(), udp6_input(),
1673 * and {ah,esp}6_input for tunnel mode
1676 ipsec6_in_reject_so(struct mbuf *m, struct socket *so)
1678 struct secpolicy *sp = NULL;
1684 return 0; /* XXX should be panic ? */
1686 /* get SP for this packet.
1687 * When we are called from ip_forward(), we call
1688 * ipsec6_getpolicybyaddr() with IP_FORWARDING flag.
1691 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1693 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_INBOUND, so, &error);
1696 return 0; /* XXX should be panic ? */
1698 result = ipsec_in_reject(sp, m);
1699 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1700 kprintf("DP ipsec6_in_reject_so call free SP:%p\n", sp));
1707 ipsec6_in_reject(struct mbuf *m, struct in6pcb *in6p)
1710 return ipsec6_in_reject_so(m, NULL);
1711 if (in6p->in6p_socket)
1712 return ipsec6_in_reject_so(m, in6p->in6p_socket);
1714 panic("ipsec6_in_reject: invalid in6p/socket");
1719 * compute the byte size to be occupied by IPsec header.
1720 * in case it is tunneled, it includes the size of outer IP header.
1721 * NOTE: SP passed is free in this function.
1724 ipsec_hdrsiz(struct secpolicy *sp)
1726 struct ipsecrequest *isr;
1729 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1730 kprintf("ipsec_hdrsiz: using SP\n");
1731 kdebug_secpolicy(sp));
1734 switch (sp->policy) {
1735 case IPSEC_POLICY_DISCARD:
1736 case IPSEC_POLICY_BYPASS:
1737 case IPSEC_POLICY_NONE:
1740 case IPSEC_POLICY_IPSEC:
1743 case IPSEC_POLICY_ENTRUST:
1745 panic("ipsec_hdrsiz: Invalid policy found. %d", sp->policy);
1750 for (isr = sp->req; isr != NULL; isr = isr->next) {
1754 switch (isr->saidx.proto) {
1757 clen = esp_hdrsiz(isr);
1763 clen = ah_hdrsiz(isr);
1765 case IPPROTO_IPCOMP:
1766 clen = sizeof(struct ipcomp);
1770 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1771 switch (((struct sockaddr *)&isr->saidx.dst)->sa_family) {
1773 clen += sizeof(struct ip);
1777 clen += sizeof(struct ip6_hdr);
1781 ipseclog((LOG_ERR, "ipsec_hdrsiz: "
1782 "unknown AF %d in IPsec tunnel SA\n",
1783 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1793 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1795 ipsec4_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
1797 struct secpolicy *sp = NULL;
1803 return 0; /* XXX should be panic ? */
1804 if (inp != NULL && inp->inp_socket == NULL)
1805 panic("ipsec4_hdrsize: why is socket NULL but there is PCB.");
1807 /* get SP for this packet.
1808 * When we are called from ip_forward(), we call
1809 * ipsec4_getpolicybyaddr() with IP_FORWARDING flag.
1812 sp = ipsec4_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1814 sp = ipsec4_getpolicybysock(m, dir, inp->inp_socket, &error);
1817 return 0; /* XXX should be panic ? */
1819 size = ipsec_hdrsiz(sp);
1820 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1821 kprintf("DP ipsec4_hdrsiz call free SP:%p\n", sp));
1822 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1823 kprintf("ipsec4_hdrsiz: size:%lu.\n", (unsigned long)size));
1830 /* This function is called from ipsec6_hdrsize_tcp(),
1831 * and maybe from ip6_forward.()
1834 ipsec6_hdrsiz(struct mbuf *m, u_int dir, struct in6pcb *in6p)
1836 struct secpolicy *sp = NULL;
1842 return 0; /* XXX shoud be panic ? */
1843 if (in6p != NULL && in6p->in6p_socket == NULL)
1844 panic("ipsec6_hdrsize: why is socket NULL but there is PCB.");
1846 /* get SP for this packet */
1847 /* XXX Is it right to call with IP_FORWARDING. */
1849 sp = ipsec6_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1851 sp = ipsec6_getpolicybysock(m, dir, in6p->in6p_socket, &error);
1855 size = ipsec_hdrsiz(sp);
1856 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1857 kprintf("DP ipsec6_hdrsiz call free SP:%p\n", sp));
1858 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1859 kprintf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size));
1868 * encapsulate for ipsec tunnel.
1869 * ip->ip_src must be fixed later on.
1872 ipsec4_encapsulate(struct mbuf *m, struct secasvar *sav)
1879 /* can't tunnel between different AFs */
1880 if (((struct sockaddr *)&sav->sah->saidx.src)->sa_family
1881 != ((struct sockaddr *)&sav->sah->saidx.dst)->sa_family
1882 || ((struct sockaddr *)&sav->sah->saidx.src)->sa_family != AF_INET) {
1887 /* XXX if the dst is myself, perform nothing. */
1888 if (key_ismyaddr((struct sockaddr *)&sav->sah->saidx.dst)) {
1894 if (m->m_len < sizeof(*ip))
1895 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
1897 ip = mtod(m, struct ip *);
1899 hlen = _IP_VHL_HL(ip->ip_vhl) << 2;
1901 hlen = ip->ip_hl << 2;
1904 if (m->m_len != hlen)
1905 panic("ipsec4_encapsulate: assumption failed (first mbuf length)");
1907 /* generate header checksum */
1910 if (ip->ip_vhl == IP_VHL_BORING)
1911 ip->ip_sum = in_cksum_hdr(ip);
1913 ip->ip_sum = in_cksum(m, hlen);
1915 ip->ip_sum = in_cksum(m, hlen);
1918 plen = m->m_pkthdr.len;
1921 * grow the mbuf to accomodate the new IPv4 header.
1922 * NOTE: IPv4 options will never be copied.
1924 if (M_LEADINGSPACE(m->m_next) < hlen) {
1926 MGET(n, MB_DONTWAIT, MT_DATA);
1932 n->m_next = m->m_next;
1934 m->m_pkthdr.len += hlen;
1935 oip = mtod(n, struct ip *);
1937 m->m_next->m_len += hlen;
1938 m->m_next->m_data -= hlen;
1939 m->m_pkthdr.len += hlen;
1940 oip = mtod(m->m_next, struct ip *);
1942 ip = mtod(m, struct ip *);
1943 ovbcopy((caddr_t)ip, (caddr_t)oip, hlen);
1944 m->m_len = sizeof(struct ip);
1945 m->m_pkthdr.len -= (hlen - sizeof(struct ip));
1947 /* construct new IPv4 header. see RFC 2401 5.1.2.1 */
1948 /* ECN consideration. */
1949 ip_ecn_ingress(ip4_ipsec_ecn, &ip->ip_tos, &oip->ip_tos);
1951 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1953 ip->ip_hl = sizeof(struct ip) >> 2;
1955 ip->ip_off &= htons(~IP_OFFMASK);
1956 ip->ip_off &= htons(~IP_MF);
1957 switch (ip4_ipsec_dfbit) {
1958 case 0: /* clear DF bit */
1959 ip->ip_off &= htons(~IP_DF);
1961 case 1: /* set DF bit */
1962 ip->ip_off |= htons(IP_DF);
1964 default: /* copy DF bit */
1967 ip->ip_p = IPPROTO_IPIP;
1968 if (plen + sizeof(struct ip) < IP_MAXPACKET)
1969 ip->ip_len = htons(plen + sizeof(struct ip));
1971 ipseclog((LOG_ERR, "IPv4 ipsec: size exceeds limit: "
1972 "leave ip_len as is (invalid packet)\n"));
1975 ip->ip_id = ip_randomid();
1977 ip->ip_id = htons(ip_id++);
1979 bcopy(&((struct sockaddr_in *)&sav->sah->saidx.src)->sin_addr,
1980 &ip->ip_src, sizeof(ip->ip_src));
1981 bcopy(&((struct sockaddr_in *)&sav->sah->saidx.dst)->sin_addr,
1982 &ip->ip_dst, sizeof(ip->ip_dst));
1983 ip->ip_ttl = IPDEFTTL;
1985 /* XXX Should ip_src be updated later ? */
1993 ipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
1995 struct ip6_hdr *oip6;
1996 struct ip6_hdr *ip6;
1999 /* can't tunnel between different AFs */
2000 if (((struct sockaddr *)&sav->sah->saidx.src)->sa_family
2001 != ((struct sockaddr *)&sav->sah->saidx.dst)->sa_family
2002 || ((struct sockaddr *)&sav->sah->saidx.src)->sa_family != AF_INET6) {
2007 /* XXX if the dst is myself, perform nothing. */
2008 if (key_ismyaddr((struct sockaddr *)&sav->sah->saidx.dst)) {
2014 plen = m->m_pkthdr.len;
2017 * grow the mbuf to accomodate the new IPv6 header.
2019 if (m->m_len != sizeof(struct ip6_hdr))
2020 panic("ipsec6_encapsulate: assumption failed (first mbuf length)");
2021 if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
2023 MGET(n, MB_DONTWAIT, MT_DATA);
2028 n->m_len = sizeof(struct ip6_hdr);
2029 n->m_next = m->m_next;
2031 m->m_pkthdr.len += sizeof(struct ip6_hdr);
2032 oip6 = mtod(n, struct ip6_hdr *);
2034 m->m_next->m_len += sizeof(struct ip6_hdr);
2035 m->m_next->m_data -= sizeof(struct ip6_hdr);
2036 m->m_pkthdr.len += sizeof(struct ip6_hdr);
2037 oip6 = mtod(m->m_next, struct ip6_hdr *);
2039 ip6 = mtod(m, struct ip6_hdr *);
2040 ovbcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
2042 /* Fake link-local scope-class addresses */
2043 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
2044 oip6->ip6_src.s6_addr16[1] = 0;
2045 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
2046 oip6->ip6_dst.s6_addr16[1] = 0;
2048 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
2049 /* ECN consideration. */
2050 ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
2051 if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
2052 ip6->ip6_plen = htons(plen);
2054 /* ip6->ip6_plen will be updated in ip6_output() */
2056 ip6->ip6_nxt = IPPROTO_IPV6;
2057 bcopy(&((struct sockaddr_in6 *)&sav->sah->saidx.src)->sin6_addr,
2058 &ip6->ip6_src, sizeof(ip6->ip6_src));
2059 bcopy(&((struct sockaddr_in6 *)&sav->sah->saidx.dst)->sin6_addr,
2060 &ip6->ip6_dst, sizeof(ip6->ip6_dst));
2061 ip6->ip6_hlim = IPV6_DEFHLIM;
2063 /* XXX Should ip6_src be updated later ? */
2070 * Check the variable replay window.
2071 * ipsec_chkreplay() performs replay check before ICV verification.
2072 * ipsec_updatereplay() updates replay bitmap. This must be called after
2073 * ICV verification (it also performs replay check, which is usually done
2075 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2077 * based on RFC 2401.
2080 ipsec_chkreplay(u_int32_t seq, struct secasvar *sav)
2082 const struct secreplay *replay;
2085 u_int32_t wsizeb; /* constant: bits of window size */
2086 int frlast; /* constant: last frame */
2090 panic("ipsec_chkreplay: NULL pointer was passed.");
2092 replay = sav->replay;
2094 if (replay->wsize == 0)
2095 return 1; /* no need to check replay. */
2098 frlast = replay->wsize - 1;
2099 wsizeb = replay->wsize << 3;
2101 /* sequence number of 0 is invalid */
2105 /* first time is always okay */
2106 if (replay->count == 0)
2109 if (seq > replay->lastseq) {
2110 /* larger sequences are okay */
2113 /* seq is equal or less than lastseq. */
2114 diff = replay->lastseq - seq;
2116 /* over range to check, i.e. too old or wrapped */
2120 fr = frlast - diff / 8;
2122 /* this packet already seen ? */
2123 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
2126 /* out of order but good */
2132 * check replay counter whether to update or not.
2137 ipsec_updatereplay(u_int32_t seq, struct secasvar *sav)
2139 struct secreplay *replay;
2142 u_int32_t wsizeb; /* constant: bits of window size */
2143 int frlast; /* constant: last frame */
2147 panic("ipsec_chkreplay: NULL pointer was passed.");
2149 replay = sav->replay;
2151 if (replay->wsize == 0)
2152 goto ok; /* no need to check replay. */
2155 frlast = replay->wsize - 1;
2156 wsizeb = replay->wsize << 3;
2158 /* sequence number of 0 is invalid */
2163 if (replay->count == 0) {
2164 replay->lastseq = seq;
2165 bzero(replay->bitmap, replay->wsize);
2166 (replay->bitmap)[frlast] = 1;
2170 if (seq > replay->lastseq) {
2171 /* seq is larger than lastseq. */
2172 diff = seq - replay->lastseq;
2174 /* new larger sequence number */
2175 if (diff < wsizeb) {
2177 /* set bit for this packet */
2178 vshiftl(replay->bitmap, diff, replay->wsize);
2179 (replay->bitmap)[frlast] |= 1;
2181 /* this packet has a "way larger" */
2182 bzero(replay->bitmap, replay->wsize);
2183 (replay->bitmap)[frlast] = 1;
2185 replay->lastseq = seq;
2187 /* larger is good */
2189 /* seq is equal or less than lastseq. */
2190 diff = replay->lastseq - seq;
2192 /* over range to check, i.e. too old or wrapped */
2196 fr = frlast - diff / 8;
2198 /* this packet already seen ? */
2199 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
2203 (replay->bitmap)[fr] |= (1 << (diff % 8));
2205 /* out of order but good */
2209 if (replay->count == ~0) {
2211 /* set overflow flag */
2214 /* don't increment, no more packets accepted */
2215 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
2218 ipseclog((LOG_WARNING, "replay counter made %d cycle. %s\n",
2219 replay->overflow, ipsec_logsastr(sav)));
2228 * shift variable length buffer to left.
2229 * IN: bitmap: pointer to the buffer
2230 * nbit: the number of to shift.
2231 * wsize: buffer size (bytes).
2234 vshiftl(unsigned char *bitmap, int nbit, int wsize)
2239 for (j = 0; j < nbit; j += 8) {
2240 s = (nbit - j < 8) ? (nbit - j): 8;
2242 for (i = 1; i < wsize; i++) {
2243 over = (bitmap[i] >> (8 - s));
2245 bitmap[i-1] |= over;
2253 ipsec4_logpacketstr(struct ip *ip, u_int32_t spi)
2255 static char buf[256];
2259 s = (u_int8_t *)(&ip->ip_src);
2260 d = (u_int8_t *)(&ip->ip_dst);
2263 ksnprintf(buf, sizeof(buf), "packet(SPI=%u ", (u_int32_t)ntohl(spi));
2266 ksnprintf(p, sizeof(buf) - (p - buf), "src=%u.%u.%u.%u",
2267 s[0], s[1], s[2], s[3]);
2270 ksnprintf(p, sizeof(buf) - (p - buf), " dst=%u.%u.%u.%u",
2271 d[0], d[1], d[2], d[3]);
2274 ksnprintf(p, sizeof(buf) - (p - buf), ")");
2281 ipsec6_logpacketstr(struct ip6_hdr *ip6, u_int32_t spi)
2283 static char buf[256];
2287 ksnprintf(buf, sizeof(buf), "packet(SPI=%u ", (u_int32_t)ntohl(spi));
2290 ksnprintf(p, sizeof(buf) - (p - buf), "src=%s",
2291 ip6_sprintf(&ip6->ip6_src));
2294 ksnprintf(p, sizeof(buf) - (p - buf), " dst=%s",
2295 ip6_sprintf(&ip6->ip6_dst));
2298 ksnprintf(p, sizeof(buf) - (p - buf), ")");
2305 ipsec_logsastr(struct secasvar *sav)
2307 static char buf[256];
2309 struct secasindex *saidx = &sav->sah->saidx;
2311 /* validity check */
2312 if (((struct sockaddr *)&sav->sah->saidx.src)->sa_family
2313 != ((struct sockaddr *)&sav->sah->saidx.dst)->sa_family)
2314 panic("ipsec_logsastr: family mismatched.");
2317 ksnprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
2320 if (((struct sockaddr *)&saidx->src)->sa_family == AF_INET) {
2322 s = (u_int8_t *)&((struct sockaddr_in *)&saidx->src)->sin_addr;
2323 d = (u_int8_t *)&((struct sockaddr_in *)&saidx->dst)->sin_addr;
2324 ksnprintf(p, sizeof(buf) - (p - buf),
2325 "src=%d.%d.%d.%d dst=%d.%d.%d.%d",
2326 s[0], s[1], s[2], s[3], d[0], d[1], d[2], d[3]);
2329 else if (((struct sockaddr *)&saidx->src)->sa_family == AF_INET6) {
2330 ksnprintf(p, sizeof(buf) - (p - buf),
2332 ip6_sprintf(&((struct sockaddr_in6 *)&saidx->src)->sin6_addr));
2335 ksnprintf(p, sizeof(buf) - (p - buf),
2337 ip6_sprintf(&((struct sockaddr_in6 *)&saidx->dst)->sin6_addr));
2342 ksnprintf(p, sizeof(buf) - (p - buf), ")");
2348 ipsec_dumpmbuf(struct mbuf *m)
2357 p = mtod(m, u_char *);
2358 for (i = 0; i < m->m_len; i++) {
2359 kprintf("%02x ", p[i]);
2361 if (totlen % 16 == 0)
2366 if (totlen % 16 != 0)
2373 * IPsec output logic for IPv4.
2376 ipsec4_output(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
2378 struct ip *ip = NULL;
2379 struct ipsecrequest *isr = NULL;
2380 struct secasindex saidx;
2382 struct sockaddr_in *dst4;
2383 struct sockaddr_in *sin;
2386 panic("state == NULL in ipsec4_output");
2388 panic("state->m == NULL in ipsec4_output");
2390 panic("state->ro == NULL in ipsec4_output");
2392 panic("state->dst == NULL in ipsec4_output");
2394 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
2395 kprintf("ipsec4_output: applyed SP\n");
2396 kdebug_secpolicy(sp));
2398 for (isr = sp->req; isr != NULL; isr = isr->next) {
2400 #if 0 /* give up to check restriction of transport mode */
2401 /* XXX but should be checked somewhere */
2403 * some of the IPsec operation must be performed only in
2406 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT
2407 && (flags & IP_FORWARDING))
2411 /* make SA index for search proper SA */
2412 ip = mtod(state->m, struct ip *);
2413 bcopy(&isr->saidx, &saidx, sizeof(saidx));
2414 saidx.mode = isr->saidx.mode;
2415 saidx.reqid = isr->saidx.reqid;
2416 sin = (struct sockaddr_in *)&saidx.src;
2417 if (sin->sin_len == 0) {
2418 sin->sin_len = sizeof(*sin);
2419 sin->sin_family = AF_INET;
2420 sin->sin_port = IPSEC_PORT_ANY;
2421 bcopy(&ip->ip_src, &sin->sin_addr,
2422 sizeof(sin->sin_addr));
2424 sin = (struct sockaddr_in *)&saidx.dst;
2425 if (sin->sin_len == 0) {
2426 sin->sin_len = sizeof(*sin);
2427 sin->sin_family = AF_INET;
2428 sin->sin_port = IPSEC_PORT_ANY;
2429 bcopy(&ip->ip_dst, &sin->sin_addr,
2430 sizeof(sin->sin_addr));
2433 if ((error = key_checkrequest(isr, &saidx)) != 0) {
2435 * IPsec processing is required, but no SA found.
2436 * I assume that key_acquire() had been called
2437 * to get/establish the SA. Here I discard
2438 * this packet because it is responsibility for
2439 * upper layer to retransmit the packet.
2441 ipsecstat.out_nosa++;
2445 /* validity check */
2446 if (isr->sav == NULL) {
2447 switch (ipsec_get_reqlevel(isr)) {
2448 case IPSEC_LEVEL_USE:
2450 case IPSEC_LEVEL_REQUIRE:
2451 /* must be not reached here. */
2452 panic("ipsec4_output: no SA found, but required.");
2457 * If there is no valid SA, we give up to process any
2458 * more. In such a case, the SA's status is changed
2459 * from DYING to DEAD after allocating. If a packet
2460 * send to the receiver by dead SA, the receiver can
2461 * not decode a packet because SA has been dead.
2463 if (isr->sav->state != SADB_SASTATE_MATURE
2464 && isr->sav->state != SADB_SASTATE_DYING) {
2465 ipsecstat.out_nosa++;
2471 * There may be the case that SA status will be changed when
2472 * we are refering to one. So calling crit_enter().
2476 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
2478 * build IPsec tunnel.
2480 /* XXX should be processed with other familiy */
2481 if (((struct sockaddr *)&isr->sav->sah->saidx.src)->sa_family != AF_INET) {
2482 ipseclog((LOG_ERR, "ipsec4_output: "
2483 "family mismatched between inner and outer spi=%u\n",
2484 (u_int32_t)ntohl(isr->sav->spi)));
2486 error = EAFNOSUPPORT;
2490 state->m = ipsec4_splithdr(state->m);
2496 error = ipsec4_encapsulate(state->m, isr->sav);
2502 ip = mtod(state->m, struct ip *);
2504 state->ro = &isr->sav->sah->sa_route;
2505 state->dst = (struct sockaddr *)&state->ro->ro_dst;
2506 dst4 = (struct sockaddr_in *)state->dst;
2507 if (state->ro->ro_rt
2508 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
2509 || dst4->sin_addr.s_addr != ip->ip_dst.s_addr)) {
2510 RTFREE(state->ro->ro_rt);
2511 state->ro->ro_rt = NULL;
2513 if (state->ro->ro_rt == 0) {
2514 dst4->sin_family = AF_INET;
2515 dst4->sin_len = sizeof(*dst4);
2516 dst4->sin_addr = ip->ip_dst;
2519 if (state->ro->ro_rt == 0) {
2520 ipstat.ips_noroute++;
2521 error = EHOSTUNREACH;
2525 /* adjust state->dst if tunnel endpoint is offlink */
2526 if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
2527 state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
2528 dst4 = (struct sockaddr_in *)state->dst;
2533 state->m = ipsec4_splithdr(state->m);
2538 switch (isr->saidx.proto) {
2541 if ((error = esp4_output(state->m, isr)) != 0) {
2553 if ((error = ah4_output(state->m, isr)) != 0) {
2558 case IPPROTO_IPCOMP:
2559 if ((error = ipcomp4_output(state->m, isr)) != 0) {
2566 "ipsec4_output: unknown ipsec protocol %d\n",
2574 if (state->m == 0) {
2578 ip = mtod(state->m, struct ip *);
2592 * IPsec output logic for IPv6, transport mode.
2595 ipsec6_output_trans(struct ipsec_output_state *state, u_char *nexthdrp,
2596 struct mbuf *mprev, struct secpolicy *sp, int flags,
2599 struct ip6_hdr *ip6;
2600 struct ipsecrequest *isr = NULL;
2601 struct secasindex saidx;
2604 struct sockaddr_in6 *sin6;
2607 panic("state == NULL in ipsec6_output_trans");
2609 panic("state->m == NULL in ipsec6_output_trans");
2611 panic("nexthdrp == NULL in ipsec6_output_trans");
2613 panic("mprev == NULL in ipsec6_output_trans");
2615 panic("sp == NULL in ipsec6_output_trans");
2617 panic("tun == NULL in ipsec6_output_trans");
2619 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
2620 kprintf("ipsec6_output_trans: applyed SP\n");
2621 kdebug_secpolicy(sp));
2624 for (isr = sp->req; isr; isr = isr->next) {
2625 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
2626 /* the rest will be handled by ipsec6_output_tunnel() */
2630 /* make SA index for search proper SA */
2631 ip6 = mtod(state->m, struct ip6_hdr *);
2632 bcopy(&isr->saidx, &saidx, sizeof(saidx));
2633 saidx.mode = isr->saidx.mode;
2634 saidx.reqid = isr->saidx.reqid;
2635 sin6 = (struct sockaddr_in6 *)&saidx.src;
2636 if (sin6->sin6_len == 0) {
2637 sin6->sin6_len = sizeof(*sin6);
2638 sin6->sin6_family = AF_INET6;
2639 sin6->sin6_port = IPSEC_PORT_ANY;
2640 bcopy(&ip6->ip6_src, &sin6->sin6_addr,
2641 sizeof(ip6->ip6_src));
2642 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
2643 /* fix scope id for comparing SPD */
2644 sin6->sin6_addr.s6_addr16[1] = 0;
2645 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
2648 sin6 = (struct sockaddr_in6 *)&saidx.dst;
2649 if (sin6->sin6_len == 0) {
2650 sin6->sin6_len = sizeof(*sin6);
2651 sin6->sin6_family = AF_INET6;
2652 sin6->sin6_port = IPSEC_PORT_ANY;
2653 bcopy(&ip6->ip6_dst, &sin6->sin6_addr,
2654 sizeof(ip6->ip6_dst));
2655 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
2656 /* fix scope id for comparing SPD */
2657 sin6->sin6_addr.s6_addr16[1] = 0;
2658 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
2662 if (key_checkrequest(isr, &saidx) == ENOENT) {
2664 * IPsec processing is required, but no SA found.
2665 * I assume that key_acquire() had been called
2666 * to get/establish the SA. Here I discard
2667 * this packet because it is responsibility for
2668 * upper layer to retransmit the packet.
2670 ipsec6stat.out_nosa++;
2674 * Notify the fact that the packet is discarded
2675 * to ourselves. I believe this is better than
2676 * just silently discarding. (jinmei@kame.net)
2677 * XXX: should we restrict the error to TCP packets?
2678 * XXX: should we directly notify sockets via
2681 icmp6_error(state->m, ICMP6_DST_UNREACH,
2682 ICMP6_DST_UNREACH_ADMIN, 0);
2683 state->m = NULL; /* icmp6_error freed the mbuf */
2687 /* validity check */
2688 if (isr->sav == NULL) {
2689 switch (ipsec_get_reqlevel(isr)) {
2690 case IPSEC_LEVEL_USE:
2692 case IPSEC_LEVEL_REQUIRE:
2693 /* must be not reached here. */
2694 panic("ipsec6_output_trans: no SA found, but required.");
2699 * If there is no valid SA, we give up to process.
2700 * see same place at ipsec4_output().
2702 if (isr->sav->state != SADB_SASTATE_MATURE
2703 && isr->sav->state != SADB_SASTATE_DYING) {
2704 ipsec6stat.out_nosa++;
2709 switch (isr->saidx.proto) {
2712 error = esp6_output(state->m, nexthdrp, mprev->m_next, isr);
2719 error = ah6_output(state->m, nexthdrp, mprev->m_next, isr);
2721 case IPPROTO_IPCOMP:
2722 error = ipcomp6_output(state->m, nexthdrp, mprev->m_next, isr);
2725 ipseclog((LOG_ERR, "ipsec6_output_trans: "
2726 "unknown ipsec protocol %d\n", isr->saidx.proto));
2728 ipsec6stat.out_inval++;
2736 plen = state->m->m_pkthdr.len - sizeof(struct ip6_hdr);
2737 if (plen > IPV6_MAXPACKET) {
2738 ipseclog((LOG_ERR, "ipsec6_output_trans: "
2739 "IPsec with IPv6 jumbogram is not supported\n"));
2740 ipsec6stat.out_inval++;
2741 error = EINVAL; /* XXX */
2744 ip6 = mtod(state->m, struct ip6_hdr *);
2745 ip6->ip6_plen = htons(plen);
2748 /* if we have more to go, we need a tunnel mode processing */
2761 * IPsec output logic for IPv6, tunnel mode.
2764 ipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp,
2767 struct ip6_hdr *ip6;
2768 struct ipsecrequest *isr = NULL;
2769 struct secasindex saidx;
2772 struct sockaddr_in6* dst6;
2775 panic("state == NULL in ipsec6_output_tunnel");
2777 panic("state->m == NULL in ipsec6_output_tunnel");
2779 panic("sp == NULL in ipsec6_output_tunnel");
2781 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
2782 kprintf("ipsec6_output_tunnel: applyed SP\n");
2783 kdebug_secpolicy(sp));
2786 * transport mode ipsec (before the 1st tunnel mode) is already
2787 * processed by ipsec6_output_trans().
2789 for (isr = sp->req; isr; isr = isr->next) {
2790 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
2794 for (/* already initialized */; isr; isr = isr->next) {
2795 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
2796 /* When tunnel mode, SA peers must be specified. */
2797 bcopy(&isr->saidx, &saidx, sizeof(saidx));
2799 /* make SA index to look for a proper SA */
2800 struct sockaddr_in6 *sin6;
2802 bzero(&saidx, sizeof(saidx));
2803 saidx.proto = isr->saidx.proto;
2804 saidx.mode = isr->saidx.mode;
2805 saidx.reqid = isr->saidx.reqid;
2807 ip6 = mtod(state->m, struct ip6_hdr *);
2808 sin6 = (struct sockaddr_in6 *)&saidx.src;
2809 if (sin6->sin6_len == 0) {
2810 sin6->sin6_len = sizeof(*sin6);
2811 sin6->sin6_family = AF_INET6;
2812 sin6->sin6_port = IPSEC_PORT_ANY;
2813 bcopy(&ip6->ip6_src, &sin6->sin6_addr,
2814 sizeof(ip6->ip6_src));
2815 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
2816 /* fix scope id for comparing SPD */
2817 sin6->sin6_addr.s6_addr16[1] = 0;
2818 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
2821 sin6 = (struct sockaddr_in6 *)&saidx.dst;
2822 if (sin6->sin6_len == 0) {
2823 sin6->sin6_len = sizeof(*sin6);
2824 sin6->sin6_family = AF_INET6;
2825 sin6->sin6_port = IPSEC_PORT_ANY;
2826 bcopy(&ip6->ip6_dst, &sin6->sin6_addr,
2827 sizeof(ip6->ip6_dst));
2828 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
2829 /* fix scope id for comparing SPD */
2830 sin6->sin6_addr.s6_addr16[1] = 0;
2831 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
2836 if (key_checkrequest(isr, &saidx) == ENOENT) {
2838 * IPsec processing is required, but no SA found.
2839 * I assume that key_acquire() had been called
2840 * to get/establish the SA. Here I discard
2841 * this packet because it is responsibility for
2842 * upper layer to retransmit the packet.
2844 ipsec6stat.out_nosa++;
2849 /* validity check */
2850 if (isr->sav == NULL) {
2851 switch (ipsec_get_reqlevel(isr)) {
2852 case IPSEC_LEVEL_USE:
2854 case IPSEC_LEVEL_REQUIRE:
2855 /* must be not reached here. */
2856 panic("ipsec6_output_tunnel: no SA found, but required.");
2861 * If there is no valid SA, we give up to process.
2862 * see same place at ipsec4_output().
2864 if (isr->sav->state != SADB_SASTATE_MATURE
2865 && isr->sav->state != SADB_SASTATE_DYING) {
2866 ipsec6stat.out_nosa++;
2872 * There may be the case that SA status will be changed when
2873 * we are refering to one. So calling crit_enter().
2877 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
2879 * build IPsec tunnel.
2881 /* XXX should be processed with other familiy */
2882 if (((struct sockaddr *)&isr->sav->sah->saidx.src)->sa_family != AF_INET6) {
2883 ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
2884 "family mismatched between inner and outer, spi=%u\n",
2885 (u_int32_t)ntohl(isr->sav->spi)));
2887 ipsec6stat.out_inval++;
2888 error = EAFNOSUPPORT;
2892 state->m = ipsec6_splithdr(state->m);
2895 ipsec6stat.out_nomem++;
2899 error = ipsec6_encapsulate(state->m, isr->sav);
2905 ip6 = mtod(state->m, struct ip6_hdr *);
2907 state->ro = &isr->sav->sah->sa_route;
2908 state->dst = (struct sockaddr *)&state->ro->ro_dst;
2909 dst6 = (struct sockaddr_in6 *)state->dst;
2910 if (state->ro->ro_rt
2911 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
2912 || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
2913 RTFREE(state->ro->ro_rt);
2914 state->ro->ro_rt = NULL;
2916 if (state->ro->ro_rt == 0) {
2917 bzero(dst6, sizeof(*dst6));
2918 dst6->sin6_family = AF_INET6;
2919 dst6->sin6_len = sizeof(*dst6);
2920 dst6->sin6_addr = ip6->ip6_dst;
2923 if (state->ro->ro_rt == 0) {
2924 ip6stat.ip6s_noroute++;
2925 ipsec6stat.out_noroute++;
2926 error = EHOSTUNREACH;
2930 /* adjust state->dst if tunnel endpoint is offlink */
2931 if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
2932 state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
2933 dst6 = (struct sockaddr_in6 *)state->dst;
2938 state->m = ipsec6_splithdr(state->m);
2940 ipsec6stat.out_nomem++;
2944 ip6 = mtod(state->m, struct ip6_hdr *);
2945 switch (isr->saidx.proto) {
2948 error = esp6_output(state->m, &ip6->ip6_nxt, state->m->m_next, isr);
2955 error = ah6_output(state->m, &ip6->ip6_nxt, state->m->m_next, isr);
2957 case IPPROTO_IPCOMP:
2958 /* XXX code should be here */
2961 ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
2962 "unknown ipsec protocol %d\n", isr->saidx.proto));
2964 ipsec6stat.out_inval++;
2972 plen = state->m->m_pkthdr.len - sizeof(struct ip6_hdr);
2973 if (plen > IPV6_MAXPACKET) {
2974 ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
2975 "IPsec with IPv6 jumbogram is not supported\n"));
2976 ipsec6stat.out_inval++;
2977 error = EINVAL; /* XXX */
2980 ip6 = mtod(state->m, struct ip6_hdr *);
2981 ip6->ip6_plen = htons(plen);
2995 * Chop IP header and option off from the payload.
2997 static struct mbuf *
2998 ipsec4_splithdr(struct mbuf *m)
3004 if (m->m_len < sizeof(struct ip))
3005 panic("ipsec4_splithdr: first mbuf too short");
3006 ip = mtod(m, struct ip *);
3008 hlen = _IP_VHL_HL(ip->ip_vhl) << 2;
3010 hlen = ip->ip_hl << 2;
3012 if (m->m_len > hlen) {
3013 MGETHDR(mh, MB_DONTWAIT, MT_HEADER);
3018 M_MOVE_PKTHDR(mh, m);
3025 bcopy((caddr_t)ip, mtod(m, caddr_t), hlen);
3026 } else if (m->m_len < hlen) {
3027 m = m_pullup(m, hlen);
3036 static struct mbuf *
3037 ipsec6_splithdr(struct mbuf *m)
3040 struct ip6_hdr *ip6;
3043 if (m->m_len < sizeof(struct ip6_hdr))
3044 panic("ipsec6_splithdr: first mbuf too short");
3045 ip6 = mtod(m, struct ip6_hdr *);
3046 hlen = sizeof(struct ip6_hdr);
3047 if (m->m_len > hlen) {
3048 MGETHDR(mh, MB_DONTWAIT, MT_HEADER);
3053 M_MOVE_PKTHDR(mh, m);
3060 bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
3061 } else if (m->m_len < hlen) {
3062 m = m_pullup(m, hlen);
3070 /* validate inbound IPsec tunnel packet. */
3072 ipsec4_tunnel_validate(struct mbuf *m, /* no pullup permitted, m->m_len >= ip */
3073 int off, u_int nxt0, struct secasvar *sav)
3075 u_int8_t nxt = nxt0 & 0xff;
3076 struct sockaddr_in *sin;
3077 struct sockaddr_in osrc, odst, isrc, idst;
3079 struct secpolicy *sp;
3083 if (m->m_len < sizeof(struct ip))
3084 panic("too short mbuf on ipsec4_tunnel_validate");
3086 if (nxt != IPPROTO_IPV4)
3088 if (m->m_pkthdr.len < off + sizeof(struct ip))
3090 /* do not decapsulate if the SA is for transport mode only */
3091 if (sav->sah->saidx.mode == IPSEC_MODE_TRANSPORT)
3094 oip = mtod(m, struct ip *);
3096 hlen = _IP_VHL_HL(oip->ip_vhl) << 2;
3098 hlen = oip->ip_hl << 2;
3100 if (hlen != sizeof(struct ip))
3103 /* AF_INET6 should be supported, but at this moment we don't. */
3104 sin = (struct sockaddr_in *)&sav->sah->saidx.dst;
3105 if (sin->sin_family != AF_INET)
3107 if (bcmp(&oip->ip_dst, &sin->sin_addr, sizeof(oip->ip_dst)) != 0)
3111 bzero(&osrc, sizeof(osrc));
3112 bzero(&odst, sizeof(odst));
3113 bzero(&isrc, sizeof(isrc));
3114 bzero(&idst, sizeof(idst));
3115 osrc.sin_family = odst.sin_family = isrc.sin_family = idst.sin_family =
3117 osrc.sin_len = odst.sin_len = isrc.sin_len = idst.sin_len =
3118 sizeof(struct sockaddr_in);
3119 osrc.sin_addr = oip->ip_src;
3120 odst.sin_addr = oip->ip_dst;
3121 m_copydata(m, off + offsetof(struct ip, ip_src), sizeof(isrc.sin_addr),
3122 (caddr_t)&isrc.sin_addr);
3123 m_copydata(m, off + offsetof(struct ip, ip_dst), sizeof(idst.sin_addr),
3124 (caddr_t)&idst.sin_addr);
3127 * RFC2401 5.2.1 (b): (assume that we are using tunnel mode)
3128 * - if the inner destination is multicast address, there can be
3129 * multiple permissible inner source address. implementation
3130 * may want to skip verification of inner source address against
3132 * - if the inner protocol is ICMP, the packet may be an error report
3133 * from routers on the other side of the VPN cloud (R in the
3134 * following diagram). in this case, we cannot verify inner source
3135 * address against SPD selector.
3136 * me -- gw === gw -- R -- you
3138 * we consider the first bullet to be users responsibility on SPD entry
3139 * configuration (if you need to encrypt multicast traffic, set
3140 * the source range of SPD selector to 0.0.0.0/0, or have explicit
3141 * address ranges for possible senders).
3142 * the second bullet is not taken care of (yet).
3144 * therefore, we do not do anything special about inner source.
3147 sp = key_gettunnel((struct sockaddr *)&osrc, (struct sockaddr *)&odst,
3148 (struct sockaddr *)&isrc, (struct sockaddr *)&idst);
3157 /* validate inbound IPsec tunnel packet. */
3159 ipsec6_tunnel_validate(struct mbuf *m, /* no pullup permitted, m->m_len >= ip */
3160 int off, u_int nxt0, struct secasvar *sav)
3162 u_int8_t nxt = nxt0 & 0xff;
3163 struct sockaddr_in6 *sin6;
3164 struct sockaddr_in6 osrc, odst, isrc, idst;
3165 struct secpolicy *sp;
3166 struct ip6_hdr *oip6;
3169 if (m->m_len < sizeof(struct ip6_hdr))
3170 panic("too short mbuf on ipsec6_tunnel_validate");
3172 if (nxt != IPPROTO_IPV6)
3174 if (m->m_pkthdr.len < off + sizeof(struct ip6_hdr))
3176 /* do not decapsulate if the SA is for transport mode only */
3177 if (sav->sah->saidx.mode == IPSEC_MODE_TRANSPORT)
3180 oip6 = mtod(m, struct ip6_hdr *);
3181 /* AF_INET should be supported, but at this moment we don't. */
3182 sin6 = (struct sockaddr_in6 *)&sav->sah->saidx.dst;
3183 if (sin6->sin6_family != AF_INET6)
3185 if (!IN6_ARE_ADDR_EQUAL(&oip6->ip6_dst, &sin6->sin6_addr))
3189 bzero(&osrc, sizeof(osrc));
3190 bzero(&odst, sizeof(odst));
3191 bzero(&isrc, sizeof(isrc));
3192 bzero(&idst, sizeof(idst));
3193 osrc.sin6_family = odst.sin6_family = isrc.sin6_family =
3194 idst.sin6_family = AF_INET6;
3195 osrc.sin6_len = odst.sin6_len = isrc.sin6_len = idst.sin6_len =
3196 sizeof(struct sockaddr_in6);
3197 osrc.sin6_addr = oip6->ip6_src;
3198 odst.sin6_addr = oip6->ip6_dst;
3199 m_copydata(m, off + offsetof(struct ip6_hdr, ip6_src),
3200 sizeof(isrc.sin6_addr), (caddr_t)&isrc.sin6_addr);
3201 m_copydata(m, off + offsetof(struct ip6_hdr, ip6_dst),
3202 sizeof(idst.sin6_addr), (caddr_t)&idst.sin6_addr);
3205 * regarding to inner source address validation, see a long comment
3206 * in ipsec4_tunnel_validate.
3209 sp = key_gettunnel((struct sockaddr *)&osrc, (struct sockaddr *)&odst,
3210 (struct sockaddr *)&isrc, (struct sockaddr *)&idst);
3212 * when there is no suitable inbound policy for the packet of the ipsec
3213 * tunnel mode, the kernel never decapsulate the tunneled packet
3214 * as the ipsec tunnel mode even when the system wide policy is "none".
3215 * then the kernel leaves the generic tunnel module to process this
3216 * packet. if there is no rule of the generic tunnel, the packet
3217 * is rejected and the statistics will be counted up.
3228 * Make a mbuf chain for encryption.
3229 * If the original mbuf chain contains a mbuf with a cluster,
3230 * allocate a new cluster and copy the data to the new cluster.
3231 * XXX: this hack is inefficient, but is necessary to handle cases
3232 * of TCP retransmission...
3235 ipsec_copypkt(struct mbuf *m)
3237 struct mbuf *n, **mpp, *mnew;
3239 for (n = m, mpp = &m; n; n = n->m_next) {
3240 if (n->m_flags & M_EXT) {
3242 * Make a copy only if there are more than one
3243 * references to the cluster.
3244 * XXX: is this approach effective?
3246 if (m_sharecount(n) > 1) {
3250 if (n->m_flags & M_PKTHDR) {
3251 MGETHDR(mnew, MB_DONTWAIT, MT_HEADER);
3254 if (!m_dup_pkthdr(mnew, n, MB_DONTWAIT)) {
3260 MGET(mnew, MB_DONTWAIT, MT_DATA);
3268 * Copy data. If we don't have enough space to
3269 * store the whole data, allocate a cluster
3270 * or additional mbufs.
3271 * XXX: we don't use m_copyback(), since the
3272 * function does not use clusters and thus is
3281 if (remain <= (mm->m_flags & M_PKTHDR ? MHLEN : MLEN))
3283 else { /* allocate a cluster */
3284 MCLGET(mm, MB_DONTWAIT);
3285 if (!(mm->m_flags & M_EXT)) {
3289 len = remain < MCLBYTES ?
3293 bcopy(n->m_data + copied, mm->m_data,
3300 if (remain <= 0) /* completed? */
3303 /* need another mbuf */
3304 MGETHDR(mn, MB_DONTWAIT, MT_HEADER);
3307 mn->m_pkthdr.rcvif = NULL;
3313 mm->m_next = m_free(n);
3332 ipsec_delaux(struct mbuf *m)
3336 while ((tag = m_tag_find(m, PACKET_TAG_IPSEC_HISTORY, NULL)) != NULL)
3337 m_tag_delete(m, tag);
3341 ipsec_addhist(struct mbuf *m, int proto, u_int32_t spi)
3344 struct ipsec_history *p;
3346 tag = m_tag_get(PACKET_TAG_IPSEC_HISTORY,
3347 sizeof (struct ipsec_history), MB_DONTWAIT);
3350 p = (struct ipsec_history *)m_tag_data(tag);
3351 bzero(p, sizeof(*p));
3352 p->ih_proto = proto;
3354 m_tag_prepend(m, tag);
3358 struct ipsec_history *
3359 ipsec_gethist(struct mbuf *m, int *lenp)
3363 tag = m_tag_find(m, PACKET_TAG_IPSEC_HISTORY, NULL);
3366 /* XXX NB: noone uses this so fake it */
3368 *lenp = sizeof (struct ipsec_history);
3369 return ((struct ipsec_history *)(tag+1));