2 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * IPsec controller part.
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/errno.h>
51 #include <sys/kernel.h>
52 #include <sys/syslog.h>
53 #include <sys/sysctl.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/in_var.h>
64 #include <netinet/udp.h>
65 #include <netinet/udp_var.h>
66 #include <netinet/tcp.h>
67 #include <netinet/udp.h>
69 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
73 #include <netinet/in_pcb.h>
75 #include <netinet/icmp6.h>
78 #include <netipsec/ipsec.h>
80 #include <netipsec/ipsec6.h>
82 #include <netipsec/ah_var.h>
83 #include <netipsec/esp_var.h>
84 #include <netipsec/ipcomp.h> /*XXX*/
85 #include <netipsec/ipcomp_var.h>
87 #include <netipsec/key.h>
88 #include <netipsec/keydb.h>
89 #include <netipsec/key_debug.h>
91 #include <netipsec/xform.h>
93 #include <machine/in_cksum.h>
101 /* NB: name changed so netstat doesn't use it */
102 struct newipsecstat newipsecstat;
103 int ip4_ah_offsetmask = 0; /* maybe IP_DF? */
104 int ip4_ipsec_dfbit = 0; /* DF bit on encap. 0: clear 1: set 2: copy */
105 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
106 int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
107 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
108 int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
109 struct secpolicy ip4_def_policy;
110 int ip4_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
111 int ip4_esp_randpad = -1;
113 * Crypto support requirements:
115 * 1 require hardware support
116 * -1 require software support
119 int crypto_support = 0;
121 SYSCTL_DECL(_net_inet_ipsec);
124 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY,
125 def_policy, CTLFLAG_RW, &ip4_def_policy.policy, 0, "");
126 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
127 CTLFLAG_RW, &ip4_esp_trans_deflev, 0, "");
128 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
129 CTLFLAG_RW, &ip4_esp_net_deflev, 0, "");
130 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
131 CTLFLAG_RW, &ip4_ah_trans_deflev, 0, "");
132 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
133 CTLFLAG_RW, &ip4_ah_net_deflev, 0, "");
134 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS,
135 ah_cleartos, CTLFLAG_RW, &ah_cleartos, 0, "");
136 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK,
137 ah_offsetmask, CTLFLAG_RW, &ip4_ah_offsetmask, 0, "");
138 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT,
139 dfbit, CTLFLAG_RW, &ip4_ipsec_dfbit, 0, "");
140 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN,
141 ecn, CTLFLAG_RW, &ip4_ipsec_ecn, 0, "");
142 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG,
143 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
144 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ESP_RANDPAD,
145 esp_randpad, CTLFLAG_RW, &ip4_esp_randpad, 0, "");
146 SYSCTL_INT(_net_inet_ipsec, OID_AUTO,
147 crypto_support, CTLFLAG_RW, &crypto_support,0, "");
148 SYSCTL_STRUCT(_net_inet_ipsec, OID_AUTO,
149 ipsecstats, CTLFLAG_RD, &newipsecstat, newipsecstat, "");
152 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
153 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
154 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
155 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
156 int ip6_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
157 int ip6_esp_randpad = -1;
159 SYSCTL_DECL(_net_inet6_ipsec6);
161 /* net.inet6.ipsec6 */
163 SYSCTL_OID(_net_inet6_ipsec6, IPSECCTL_STATS, stats, CTLFLAG_RD,
164 0,0, compat_ipsecstats_sysctl, "S", "");
165 #endif /* COMPAT_KAME */
166 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY,
167 def_policy, CTLFLAG_RW, &ip4_def_policy.policy, 0, "");
168 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
169 CTLFLAG_RW, &ip6_esp_trans_deflev, 0, "");
170 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
171 CTLFLAG_RW, &ip6_esp_net_deflev, 0, "");
172 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
173 CTLFLAG_RW, &ip6_ah_trans_deflev, 0, "");
174 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
175 CTLFLAG_RW, &ip6_ah_net_deflev, 0, "");
176 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN,
177 ecn, CTLFLAG_RW, &ip6_ipsec_ecn, 0, "");
178 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG,
179 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
180 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ESP_RANDPAD,
181 esp_randpad, CTLFLAG_RW, &ip6_esp_randpad, 0, "");
184 static int ipsec4_setspidx_inpcb __P((struct mbuf *, struct inpcb *pcb));
186 static int ipsec6_setspidx_in6pcb __P((struct mbuf *, struct in6pcb *pcb));
188 static int ipsec_setspidx __P((struct mbuf *, struct secpolicyindex *, int));
189 static void ipsec4_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
190 static int ipsec4_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
192 static void ipsec6_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
193 static int ipsec6_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
195 static void ipsec_delpcbpolicy __P((struct inpcbpolicy *));
196 static struct secpolicy *ipsec_deepcopy_policy __P((struct secpolicy *src));
197 static int ipsec_set_policy __P((struct secpolicy **pcb_sp,
198 int optname, caddr_t request, size_t len, int priv));
199 static int ipsec_get_policy __P((struct secpolicy *pcb_sp, struct mbuf **mp));
200 static void vshiftl __P((unsigned char *, int, int));
201 static size_t ipsec_hdrsiz __P((struct secpolicy *));
203 MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy");
206 * Return a held reference to the default SP.
208 static struct secpolicy *
209 key_allocsp_default(const char* where, int tag)
211 struct secpolicy *sp;
213 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
214 printf("DP key_allocsp_default from %s:%u\n", where, tag));
216 sp = &ip4_def_policy;
217 if (sp->policy != IPSEC_POLICY_DISCARD &&
218 sp->policy != IPSEC_POLICY_NONE) {
219 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
220 sp->policy, IPSEC_POLICY_NONE));
221 sp->policy = IPSEC_POLICY_NONE;
225 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
226 printf("DP key_allocsp_default returns SP:%p (%u)\n",
230 #define KEY_ALLOCSP_DEFAULT() \
231 key_allocsp_default(__FILE__, __LINE__)
234 * For OUTBOUND packet having a socket. Searching SPD for packet,
235 * and return a pointer to SP.
236 * OUT: NULL: no apropreate SP found, the following value is set to error.
238 * EACCES : discard packet.
239 * ENOENT : ipsec_acquire() in progress, maybe.
240 * others : error occured.
241 * others: a pointer to SP
243 * NOTE: IPv6 mapped adddress concern is implemented here.
246 ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
248 struct secpolicy *sp;
250 IPSEC_ASSERT(tdbi != NULL, ("null tdbi"));
251 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
252 ("invalid direction %u", dir));
254 sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
255 if (sp == NULL) /*XXX????*/
256 sp = KEY_ALLOCSP_DEFAULT();
257 IPSEC_ASSERT(sp != NULL, ("null SP"));
262 * For OUTBOUND packet having a socket. Searching SPD for packet,
263 * and return a pointer to SP.
264 * OUT: NULL: no apropreate SP found, the following value is set to error.
266 * EACCES : discard packet.
267 * ENOENT : ipsec_acquire() in progress, maybe.
268 * others : error occured.
269 * others: a pointer to SP
271 * NOTE: IPv6 mapped adddress concern is implemented here.
274 ipsec_getpolicybysock(m, dir, inp, error)
280 struct inpcbpolicy *pcbsp = NULL;
281 struct secpolicy *currsp = NULL; /* policy on socket */
282 struct secpolicy *sp;
284 IPSEC_ASSERT(m != NULL, ("null mbuf"));
285 IPSEC_ASSERT(inp != NULL, ("null inpcb"));
286 IPSEC_ASSERT(error != NULL, ("null error"));
287 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
288 ("invalid direction %u", dir));
290 /* set spidx in pcb */
291 if (inp->inp_vflag & INP_IPV6PROTO) {
293 *error = ipsec6_setspidx_in6pcb(m, inp);
294 pcbsp = inp->in6p_sp;
296 *error = EINVAL; /* should not happen */
299 *error = ipsec4_setspidx_inpcb(m, inp);
305 IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
307 case IPSEC_DIR_INBOUND:
308 currsp = pcbsp->sp_in;
310 case IPSEC_DIR_OUTBOUND:
311 currsp = pcbsp->sp_out;
314 IPSEC_ASSERT(currsp != NULL, ("null currsp"));
316 if (pcbsp->priv) { /* when privilieged socket */
317 switch (currsp->policy) {
318 case IPSEC_POLICY_BYPASS:
319 case IPSEC_POLICY_IPSEC:
324 case IPSEC_POLICY_ENTRUST:
325 /* look for a policy in SPD */
326 sp = KEY_ALLOCSP(&currsp->spidx, dir);
327 if (sp == NULL) /* no SP found */
328 sp = KEY_ALLOCSP_DEFAULT();
332 ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
333 __func__, currsp->policy));
337 } else { /* unpriv, SPD has policy */
338 sp = KEY_ALLOCSP(&currsp->spidx, dir);
339 if (sp == NULL) { /* no SP found */
340 switch (currsp->policy) {
341 case IPSEC_POLICY_BYPASS:
342 ipseclog((LOG_ERR, "%s: Illegal policy for "
343 "non-priviliged defined %d\n",
344 __func__, currsp->policy));
348 case IPSEC_POLICY_ENTRUST:
349 sp = KEY_ALLOCSP_DEFAULT();
352 case IPSEC_POLICY_IPSEC:
358 ipseclog((LOG_ERR, "%s: Invalid policy for "
359 "PCB %d\n", __func__, currsp->policy));
365 IPSEC_ASSERT(sp != NULL,
366 ("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
367 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
368 printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
369 __func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
374 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
375 * and return a pointer to SP.
376 * OUT: positive: a pointer to the entry for security policy leaf matched.
377 * NULL: no apropreate SP found, the following value is set to error.
379 * EACCES : discard packet.
380 * ENOENT : ipsec_acquire() in progress, maybe.
381 * others : error occured.
384 ipsec_getpolicybyaddr(m, dir, flag, error)
390 struct secpolicyindex spidx;
391 struct secpolicy *sp;
393 IPSEC_ASSERT(m != NULL, ("null mbuf"));
394 IPSEC_ASSERT(error != NULL, ("null error"));
395 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
396 ("invalid direction %u", dir));
399 if (key_havesp(dir)) {
400 /* Make an index to look for a policy. */
401 *error = ipsec_setspidx(m, &spidx,
402 (flag & IP_FORWARDING) ? 0 : 1);
404 DPRINTF(("%s: setpidx failed, dir %u flag %u\n",
405 __func__, dir, flag));
410 sp = KEY_ALLOCSP(&spidx, dir);
412 if (sp == NULL) /* no SP found, use system default */
413 sp = KEY_ALLOCSP_DEFAULT();
414 IPSEC_ASSERT(sp != NULL, ("null SP"));
419 ipsec4_checkpolicy(m, dir, flag, error, inp)
425 struct secpolicy *sp;
429 sp = ipsec_getpolicybyaddr(m, dir, flag, error);
431 sp = ipsec_getpolicybysock(m, dir, inp, error);
433 IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
434 newipsecstat.ips_out_inval++;
437 IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
438 switch (sp->policy) {
439 case IPSEC_POLICY_ENTRUST:
441 printf("%s: invalid policy %u\n", __func__, sp->policy);
443 case IPSEC_POLICY_DISCARD:
444 newipsecstat.ips_out_polvio++;
445 *error = -EINVAL; /* packet is discarded by caller */
447 case IPSEC_POLICY_BYPASS:
448 case IPSEC_POLICY_NONE:
450 sp = NULL; /* NB: force NULL result */
452 case IPSEC_POLICY_IPSEC:
453 if (sp->req == NULL) /* acquire an SA */
454 *error = key_spdacquire(sp);
465 ipsec4_setspidx_inpcb(m, pcb)
471 IPSEC_ASSERT(pcb != NULL, ("null pcb"));
472 IPSEC_ASSERT(pcb->inp_sp != NULL, ("null inp_sp"));
473 IPSEC_ASSERT(pcb->inp_sp->sp_out != NULL && pcb->inp_sp->sp_in != NULL,
474 ("null sp_in || sp_out"));
476 error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
478 pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
479 pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
480 pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
482 bzero(&pcb->inp_sp->sp_in->spidx,
483 sizeof (pcb->inp_sp->sp_in->spidx));
484 bzero(&pcb->inp_sp->sp_out->spidx,
485 sizeof (pcb->inp_sp->sp_in->spidx));
492 ipsec6_setspidx_in6pcb(m, pcb)
496 struct secpolicyindex *spidx;
499 IPSEC_ASSERT(pcb != NULL, ("null pcb"));
500 IPSEC_ASSERT(pcb->in6p_sp != NULL, ("null inp_sp"));
501 IPSEC_ASSERT(pcb->in6p_sp->sp_out != NULL && pcb->in6p_sp->sp_in != NULL,
502 ("null sp_in || sp_out"));
504 bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
505 bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
507 spidx = &pcb->in6p_sp->sp_in->spidx;
508 error = ipsec_setspidx(m, spidx, 1);
511 spidx->dir = IPSEC_DIR_INBOUND;
513 spidx = &pcb->in6p_sp->sp_out->spidx;
514 error = ipsec_setspidx(m, spidx, 1);
517 spidx->dir = IPSEC_DIR_OUTBOUND;
522 bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
523 bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
529 * configure security policy index (src/dst/proto/sport/dport)
530 * by looking at the content of mbuf.
531 * the caller is responsible for error recovery (like clearing up spidx).
534 ipsec_setspidx(m, spidx, needport)
536 struct secpolicyindex *spidx;
539 struct ip *ip = NULL;
546 IPSEC_ASSERT(m != NULL, ("null mbuf"));
549 * validate m->m_pkthdr.len. we see incorrect length if we
550 * mistakenly call this function with inconsistent mbuf chain
551 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
554 for (n = m; n; n = n->m_next)
556 if (m->m_pkthdr.len != len) {
557 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
558 printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
559 __func__, len, m->m_pkthdr.len));
563 if (m->m_pkthdr.len < sizeof(struct ip)) {
564 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
565 printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
566 __func__, m->m_pkthdr.len));
570 if (m->m_len >= sizeof(*ip))
571 ip = mtod(m, struct ip *);
573 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
577 v = _IP_VHL_V(ip->ip_vhl);
583 error = ipsec4_setspidx_ipaddr(m, spidx);
586 ipsec4_get_ulp(m, spidx, needport);
590 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
591 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
592 printf("%s: pkthdr len(%d) too small (v6), "
593 "ignored\n", __func__, m->m_pkthdr.len));
596 error = ipsec6_setspidx_ipaddr(m, spidx);
599 ipsec6_get_ulp(m, spidx, needport);
603 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
604 printf("%s: " "unknown IP version %u, ignored.\n",
611 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
617 IPSEC_ASSERT(m != NULL, ("null mbuf"));
618 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
620 /* NB: ip_input() flips it into host endian XXX need more checking */
621 if (m->m_len < sizeof (struct ip)) {
622 struct ip *ip = mtod(m, struct ip *);
623 if (ip->ip_off & (IP_MF | IP_OFFMASK))
626 off = _IP_VHL_HL(ip->ip_vhl) << 2;
628 off = ip->ip_hl << 2;
634 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
635 if (ih.ip_off & (IP_MF | IP_OFFMASK))
638 off = _IP_VHL_HL(ih.ip_vhl) << 2;
645 while (off < m->m_pkthdr.len) {
652 spidx->ul_proto = nxt;
655 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
657 m_copydata(m, off, sizeof (th), (caddr_t) &th);
658 spidx->src.sin.sin_port = th.th_sport;
659 spidx->dst.sin.sin_port = th.th_dport;
662 spidx->ul_proto = nxt;
665 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
667 m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
668 spidx->src.sin.sin_port = uh.uh_sport;
669 spidx->dst.sin.sin_port = uh.uh_dport;
672 if (off + sizeof(ip6e) > m->m_pkthdr.len)
674 /* XXX sigh, this works but is totally bogus */
675 m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
676 off += (ip6e.ip6e_len + 2) << 2;
681 /* XXX intermediate headers??? */
682 spidx->ul_proto = nxt;
687 spidx->ul_proto = IPSEC_ULPROTO_ANY;
689 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
690 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
693 /* assumes that m is sane */
695 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
697 static const struct sockaddr_in template = {
698 sizeof (struct sockaddr_in),
700 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
703 spidx->src.sin = template;
704 spidx->dst.sin = template;
706 if (m->m_len < sizeof (struct ip)) {
707 m_copydata(m, offsetof(struct ip, ip_src),
708 sizeof (struct in_addr),
709 (caddr_t) &spidx->src.sin.sin_addr);
710 m_copydata(m, offsetof(struct ip, ip_dst),
711 sizeof (struct in_addr),
712 (caddr_t) &spidx->dst.sin.sin_addr);
714 struct ip *ip = mtod(m, struct ip *);
715 spidx->src.sin.sin_addr = ip->ip_src;
716 spidx->dst.sin.sin_addr = ip->ip_dst;
719 spidx->prefs = sizeof(struct in_addr) << 3;
720 spidx->prefd = sizeof(struct in_addr) << 3;
727 ipsec6_get_ulp(m, spidx, needport)
729 struct secpolicyindex *spidx;
738 panic("%s: NULL pointer was passed.\n", __func__);
740 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
741 printf("%s:\n", __func__); kdebug_mbuf(m));
744 spidx->ul_proto = IPSEC_ULPROTO_ANY;
745 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
746 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
749 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
750 if (off < 0 || m->m_pkthdr.len < off)
755 spidx->ul_proto = nxt;
758 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
760 m_copydata(m, off, sizeof(th), (caddr_t)&th);
761 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
762 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
765 spidx->ul_proto = nxt;
768 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
770 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
771 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
772 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
776 /* XXX intermediate headers??? */
777 spidx->ul_proto = nxt;
782 /* assumes that m is sane */
784 ipsec6_setspidx_ipaddr(m, spidx)
786 struct secpolicyindex *spidx;
788 struct ip6_hdr *ip6 = NULL;
789 struct ip6_hdr ip6buf;
790 struct sockaddr_in6 *sin6;
792 if (m->m_len >= sizeof(*ip6))
793 ip6 = mtod(m, struct ip6_hdr *);
795 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
799 sin6 = (struct sockaddr_in6 *)&spidx->src;
800 bzero(sin6, sizeof(*sin6));
801 sin6->sin6_family = AF_INET6;
802 sin6->sin6_len = sizeof(struct sockaddr_in6);
803 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
804 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
805 sin6->sin6_addr.s6_addr16[1] = 0;
806 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
808 spidx->prefs = sizeof(struct in6_addr) << 3;
810 sin6 = (struct sockaddr_in6 *)&spidx->dst;
811 bzero(sin6, sizeof(*sin6));
812 sin6->sin6_family = AF_INET6;
813 sin6->sin6_len = sizeof(struct sockaddr_in6);
814 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
815 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
816 sin6->sin6_addr.s6_addr16[1] = 0;
817 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
819 spidx->prefd = sizeof(struct in6_addr) << 3;
826 ipsec_delpcbpolicy(p)
827 struct inpcbpolicy *p;
829 free(p, M_IPSEC_INPCB);
832 /* initialize policy in PCB */
834 ipsec_init_policy(so, pcb_sp)
836 struct inpcbpolicy **pcb_sp;
838 struct inpcbpolicy *new;
841 if (so == NULL || pcb_sp == NULL)
842 panic("%s: NULL pointer was passed.\n", __func__);
844 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
845 M_IPSEC_INPCB, M_NOWAIT|M_ZERO);
847 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
851 new->priv = IPSEC_IS_PRIVILEGED_SO(so);
853 if ((new->sp_in = KEY_NEWSP()) == NULL) {
854 ipsec_delpcbpolicy(new);
857 new->sp_in->state = IPSEC_SPSTATE_ALIVE;
858 new->sp_in->policy = IPSEC_POLICY_ENTRUST;
860 if ((new->sp_out = KEY_NEWSP()) == NULL) {
861 KEY_FREESP(&new->sp_in);
862 ipsec_delpcbpolicy(new);
865 new->sp_out->state = IPSEC_SPSTATE_ALIVE;
866 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
873 /* copy old ipsec policy into new */
875 ipsec_copy_policy(old, new)
876 struct inpcbpolicy *old, *new;
878 struct secpolicy *sp;
880 sp = ipsec_deepcopy_policy(old->sp_in);
882 KEY_FREESP(&new->sp_in);
887 sp = ipsec_deepcopy_policy(old->sp_out);
889 KEY_FREESP(&new->sp_out);
894 new->priv = old->priv;
899 struct ipsecrequest *
902 struct ipsecrequest *p;
904 p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
906 IPSECREQUEST_LOCK_INIT(p);
911 ipsec_delisr(struct ipsecrequest *p)
913 IPSECREQUEST_LOCK_DESTROY(p);
917 /* deep-copy a policy in PCB */
918 static struct secpolicy *
919 ipsec_deepcopy_policy(src)
920 struct secpolicy *src;
922 struct ipsecrequest *newchain = NULL;
923 struct ipsecrequest *p;
924 struct ipsecrequest **q;
925 struct ipsecrequest *r;
926 struct secpolicy *dst;
935 * deep-copy IPsec request chain. This is required since struct
936 * ipsecrequest is not reference counted.
939 for (p = src->req; p; p = p->next) {
943 (*q)->saidx.proto = p->saidx.proto;
944 (*q)->saidx.mode = p->saidx.mode;
945 (*q)->level = p->level;
946 (*q)->saidx.reqid = p->saidx.reqid;
948 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
949 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
957 dst->state = src->state;
958 dst->policy = src->policy;
959 /* do not touch the refcnt fields */
964 for (p = newchain; p; p = r) {
972 /* set policy and ipsec request if present. */
974 ipsec_set_policy(pcb_sp, optname, request, len, priv)
975 struct secpolicy **pcb_sp;
981 struct sadb_x_policy *xpl;
982 struct secpolicy *newsp = NULL;
986 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
988 if (len < sizeof(*xpl))
990 xpl = (struct sadb_x_policy *)request;
992 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
993 printf("%s: passed policy\n", __func__);
994 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
996 /* check policy type */
997 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
998 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
999 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1002 /* check privileged socket */
1003 if (priv == 0 && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS)
1006 /* allocation new SP entry */
1007 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1010 newsp->state = IPSEC_SPSTATE_ALIVE;
1012 /* clear old SP and set new SP */
1015 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1016 printf("%s: new policy\n", __func__);
1017 kdebug_secpolicy(newsp));
1023 ipsec_get_policy(pcb_sp, mp)
1024 struct secpolicy *pcb_sp;
1029 if (pcb_sp == NULL || mp == NULL)
1032 *mp = key_sp2msg(pcb_sp);
1034 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
1038 (*mp)->m_type = MT_DATA;
1039 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1040 printf("%s:\n", __func__); kdebug_mbuf(*mp));
1046 ipsec4_set_policy(inp, optname, request, len, priv)
1053 struct sadb_x_policy *xpl;
1054 struct secpolicy **pcb_sp;
1057 if (inp == NULL || request == NULL)
1059 if (len < sizeof(*xpl))
1061 xpl = (struct sadb_x_policy *)request;
1063 /* select direction */
1064 switch (xpl->sadb_x_policy_dir) {
1065 case IPSEC_DIR_INBOUND:
1066 pcb_sp = &inp->inp_sp->sp_in;
1068 case IPSEC_DIR_OUTBOUND:
1069 pcb_sp = &inp->inp_sp->sp_out;
1072 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1073 xpl->sadb_x_policy_dir));
1077 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1081 ipsec4_get_policy(inp, request, len, mp)
1087 struct sadb_x_policy *xpl;
1088 struct secpolicy *pcb_sp;
1091 if (inp == NULL || request == NULL || mp == NULL)
1093 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
1094 if (len < sizeof(*xpl))
1096 xpl = (struct sadb_x_policy *)request;
1098 /* select direction */
1099 switch (xpl->sadb_x_policy_dir) {
1100 case IPSEC_DIR_INBOUND:
1101 pcb_sp = inp->inp_sp->sp_in;
1103 case IPSEC_DIR_OUTBOUND:
1104 pcb_sp = inp->inp_sp->sp_out;
1107 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1108 xpl->sadb_x_policy_dir));
1112 return ipsec_get_policy(pcb_sp, mp);
1115 /* delete policy in PCB */
1117 ipsec4_delete_pcbpolicy(inp)
1120 IPSEC_ASSERT(inp != NULL, ("null inp"));
1122 if (inp->inp_sp == NULL)
1125 if (inp->inp_sp->sp_in != NULL)
1126 KEY_FREESP(&inp->inp_sp->sp_in);
1128 if (inp->inp_sp->sp_out != NULL)
1129 KEY_FREESP(&inp->inp_sp->sp_out);
1131 ipsec_delpcbpolicy(inp->inp_sp);
1139 ipsec6_set_policy(in6p, optname, request, len, priv)
1140 struct in6pcb *in6p;
1146 struct sadb_x_policy *xpl;
1147 struct secpolicy **pcb_sp;
1150 if (in6p == NULL || request == NULL)
1152 if (len < sizeof(*xpl))
1154 xpl = (struct sadb_x_policy *)request;
1156 /* select direction */
1157 switch (xpl->sadb_x_policy_dir) {
1158 case IPSEC_DIR_INBOUND:
1159 pcb_sp = &in6p->in6p_sp->sp_in;
1161 case IPSEC_DIR_OUTBOUND:
1162 pcb_sp = &in6p->in6p_sp->sp_out;
1165 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1166 xpl->sadb_x_policy_dir));
1170 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1174 ipsec6_get_policy(in6p, request, len, mp)
1175 struct in6pcb *in6p;
1180 struct sadb_x_policy *xpl;
1181 struct secpolicy *pcb_sp;
1184 if (in6p == NULL || request == NULL || mp == NULL)
1186 IPSEC_ASSERT(in6p->in6p_sp != NULL, ("null in6p_sp"));
1187 if (len < sizeof(*xpl))
1189 xpl = (struct sadb_x_policy *)request;
1191 /* select direction */
1192 switch (xpl->sadb_x_policy_dir) {
1193 case IPSEC_DIR_INBOUND:
1194 pcb_sp = in6p->in6p_sp->sp_in;
1196 case IPSEC_DIR_OUTBOUND:
1197 pcb_sp = in6p->in6p_sp->sp_out;
1200 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1201 xpl->sadb_x_policy_dir));
1205 return ipsec_get_policy(pcb_sp, mp);
1209 ipsec6_delete_pcbpolicy(in6p)
1210 struct in6pcb *in6p;
1212 IPSEC_ASSERT(in6p != NULL, ("null in6p"));
1214 if (in6p->in6p_sp == NULL)
1217 if (in6p->in6p_sp->sp_in != NULL)
1218 KEY_FREESP(&in6p->in6p_sp->sp_in);
1220 if (in6p->in6p_sp->sp_out != NULL)
1221 KEY_FREESP(&in6p->in6p_sp->sp_out);
1223 ipsec_delpcbpolicy(in6p->in6p_sp);
1224 in6p->in6p_sp = NULL;
1231 * return current level.
1232 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1235 ipsec_get_reqlevel(isr)
1236 struct ipsecrequest *isr;
1239 u_int esp_trans_deflev, esp_net_deflev;
1240 u_int ah_trans_deflev, ah_net_deflev;
1242 IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
1243 IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1244 ("af family mismatch, src %u, dst %u",
1245 isr->sp->spidx.src.sa.sa_family,
1246 isr->sp->spidx.dst.sa.sa_family));
1248 /* XXX note that we have ipseclog() expanded here - code sync issue */
1249 #define IPSEC_CHECK_DEFAULT(lev) \
1250 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1251 && (lev) != IPSEC_LEVEL_UNIQUE) \
1253 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1254 (lev), IPSEC_LEVEL_REQUIRE) \
1256 (lev) = IPSEC_LEVEL_REQUIRE, \
1260 /* set default level */
1261 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1264 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1265 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1266 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1267 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1272 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1273 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1274 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1275 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1279 panic("%s: unknown af %u",
1280 __func__, isr->sp->spidx.src.sa.sa_family);
1283 #undef IPSEC_CHECK_DEFAULT
1286 switch (isr->level) {
1287 case IPSEC_LEVEL_DEFAULT:
1288 switch (isr->saidx.proto) {
1290 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1291 level = esp_net_deflev;
1293 level = esp_trans_deflev;
1296 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1297 level = ah_net_deflev;
1299 level = ah_trans_deflev;
1301 case IPPROTO_IPCOMP:
1303 * we don't really care, as IPcomp document says that
1304 * we shouldn't compress small packets
1306 level = IPSEC_LEVEL_USE;
1309 panic("%s: Illegal protocol defined %u\n", __func__,
1314 case IPSEC_LEVEL_USE:
1315 case IPSEC_LEVEL_REQUIRE:
1318 case IPSEC_LEVEL_UNIQUE:
1319 level = IPSEC_LEVEL_REQUIRE;
1323 panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
1330 * Check security policy requirements against the actual
1331 * packet contents. Return one if the packet should be
1332 * reject as "invalid"; otherwiser return zero to have the
1333 * packet treated as "valid".
1340 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1342 struct ipsecrequest *isr;
1345 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1346 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1349 switch (sp->policy) {
1350 case IPSEC_POLICY_DISCARD:
1352 case IPSEC_POLICY_BYPASS:
1353 case IPSEC_POLICY_NONE:
1357 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1358 ("invalid policy %u", sp->policy));
1360 /* XXX should compare policy against ipsec header history */
1363 for (isr = sp->req; isr != NULL; isr = isr->next) {
1364 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1366 switch (isr->saidx.proto) {
1368 if ((m->m_flags & M_DECRYPTED) == 0) {
1369 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1370 printf("%s: ESP m_flags:%x\n", __func__,
1377 isr->sav->tdb_authalgxform != NULL &&
1378 (m->m_flags & M_AUTHIPDGM) == 0) {
1379 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1380 printf("%s: ESP/AH m_flags:%x\n", __func__,
1387 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1388 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1389 printf("%s: AH m_flags:%x\n", __func__,
1394 case IPPROTO_IPCOMP:
1396 * we don't really care, as IPcomp document
1397 * says that we shouldn't compress small
1398 * packets, IPComp policy should always be
1399 * treated as being in "use" level.
1404 return 0; /* valid */
1408 * Check AH/ESP integrity.
1409 * This function is called from tcp_input(), udp_input(),
1410 * and {ah,esp}4_input for tunnel mode
1413 ipsec4_in_reject(m, inp)
1417 struct secpolicy *sp;
1421 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1423 /* get SP for this packet.
1424 * When we are called from ip_forward(), we call
1425 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1428 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1430 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1433 result = ipsec_in_reject(sp, m);
1435 newipsecstat.ips_in_polvio++;
1438 result = 0; /* XXX should be panic ?
1439 * -> No, there may be error. */
1446 * Check AH/ESP integrity.
1447 * This function is called from tcp6_input(), udp6_input(),
1448 * and {ah,esp}6_input for tunnel mode
1451 ipsec6_in_reject(m, inp)
1455 struct secpolicy *sp = NULL;
1461 return 0; /* XXX should be panic ? */
1463 /* get SP for this packet.
1464 * When we are called from ip_forward(), we call
1465 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1468 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1470 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1473 result = ipsec_in_reject(sp, m);
1475 newipsecstat.ips_in_polvio++;
1485 * compute the byte size to be occupied by IPsec header.
1486 * in case it is tunneled, it includes the size of outer IP header.
1487 * NOTE: SP passed is free in this function.
1490 ipsec_hdrsiz(struct secpolicy *sp)
1492 struct ipsecrequest *isr;
1495 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1496 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1498 switch (sp->policy) {
1499 case IPSEC_POLICY_DISCARD:
1500 case IPSEC_POLICY_BYPASS:
1501 case IPSEC_POLICY_NONE:
1505 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1506 ("invalid policy %u", sp->policy));
1509 for (isr = sp->req; isr != NULL; isr = isr->next) {
1512 switch (isr->saidx.proto) {
1514 clen = esp_hdrsiz(isr->sav);
1517 clen = ah_hdrsiz(isr->sav);
1519 case IPPROTO_IPCOMP:
1520 clen = sizeof(struct ipcomp);
1524 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1525 switch (isr->saidx.dst.sa.sa_family) {
1527 clen += sizeof(struct ip);
1531 clen += sizeof(struct ip6_hdr);
1535 ipseclog((LOG_ERR, "%s: unknown AF %d in "
1536 "IPsec tunnel SA\n", __func__,
1537 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1547 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1549 ipsec4_hdrsiz(m, dir, inp)
1554 struct secpolicy *sp;
1558 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1560 /* get SP for this packet.
1561 * When we are called from ip_forward(), we call
1562 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1565 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1567 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1570 size = ipsec_hdrsiz(sp);
1571 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1572 printf("%s: size:%lu.\n", __func__,
1573 (unsigned long)size));
1577 size = 0; /* XXX should be panic ? */
1583 /* This function is called from ipsec6_hdrsize_tcp(),
1584 * and maybe from ip6_forward.()
1587 ipsec6_hdrsiz(m, dir, in6p)
1590 struct in6pcb *in6p;
1592 struct secpolicy *sp;
1596 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1597 IPSEC_ASSERT(in6p == NULL || in6p->in6p_socket != NULL,
1598 ("socket w/o inpcb"));
1600 /* get SP for this packet */
1601 /* XXX Is it right to call with IP_FORWARDING. */
1603 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1605 sp = ipsec_getpolicybysock(m, dir, in6p, &error);
1609 size = ipsec_hdrsiz(sp);
1610 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1611 printf("%s: size:%lu.\n", __func__, (unsigned long)size));
1619 * Check the variable replay window.
1620 * ipsec_chkreplay() performs replay check before ICV verification.
1621 * ipsec_updatereplay() updates replay bitmap. This must be called after
1622 * ICV verification (it also performs replay check, which is usually done
1624 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1626 * based on RFC 2401.
1629 ipsec_chkreplay(seq, sav)
1631 struct secasvar *sav;
1633 const struct secreplay *replay;
1636 u_int32_t wsizeb; /* constant: bits of window size */
1637 int frlast; /* constant: last frame */
1639 IPSEC_SPLASSERT_SOFTNET(__func__);
1641 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1642 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1644 replay = sav->replay;
1646 if (replay->wsize == 0)
1647 return 1; /* no need to check replay. */
1650 frlast = replay->wsize - 1;
1651 wsizeb = replay->wsize << 3;
1653 /* sequence number of 0 is invalid */
1657 /* first time is always okay */
1658 if (replay->count == 0)
1661 if (seq > replay->lastseq) {
1662 /* larger sequences are okay */
1665 /* seq is equal or less than lastseq. */
1666 diff = replay->lastseq - seq;
1668 /* over range to check, i.e. too old or wrapped */
1672 fr = frlast - diff / 8;
1674 /* this packet already seen ? */
1675 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1678 /* out of order but good */
1684 * check replay counter whether to update or not.
1689 ipsec_updatereplay(seq, sav)
1691 struct secasvar *sav;
1693 struct secreplay *replay;
1696 u_int32_t wsizeb; /* constant: bits of window size */
1697 int frlast; /* constant: last frame */
1699 IPSEC_SPLASSERT_SOFTNET(__func__);
1701 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1702 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1704 replay = sav->replay;
1706 if (replay->wsize == 0)
1707 goto ok; /* no need to check replay. */
1710 frlast = replay->wsize - 1;
1711 wsizeb = replay->wsize << 3;
1713 /* sequence number of 0 is invalid */
1718 if (replay->count == 0) {
1719 replay->lastseq = seq;
1720 bzero(replay->bitmap, replay->wsize);
1721 (replay->bitmap)[frlast] = 1;
1725 if (seq > replay->lastseq) {
1726 /* seq is larger than lastseq. */
1727 diff = seq - replay->lastseq;
1729 /* new larger sequence number */
1730 if (diff < wsizeb) {
1732 /* set bit for this packet */
1733 vshiftl(replay->bitmap, diff, replay->wsize);
1734 (replay->bitmap)[frlast] |= 1;
1736 /* this packet has a "way larger" */
1737 bzero(replay->bitmap, replay->wsize);
1738 (replay->bitmap)[frlast] = 1;
1740 replay->lastseq = seq;
1742 /* larger is good */
1744 /* seq is equal or less than lastseq. */
1745 diff = replay->lastseq - seq;
1747 /* over range to check, i.e. too old or wrapped */
1751 fr = frlast - diff / 8;
1753 /* this packet already seen ? */
1754 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1758 (replay->bitmap)[fr] |= (1 << (diff % 8));
1760 /* out of order but good */
1764 if (replay->count == ~0) {
1766 /* set overflow flag */
1769 /* don't increment, no more packets accepted */
1770 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1773 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1774 __func__, replay->overflow, ipsec_logsastr(sav)));
1783 * shift variable length buffer to left.
1784 * IN: bitmap: pointer to the buffer
1785 * nbit: the number of to shift.
1786 * wsize: buffer size (bytes).
1789 vshiftl(bitmap, nbit, wsize)
1790 unsigned char *bitmap;
1796 for (j = 0; j < nbit; j += 8) {
1797 s = (nbit - j < 8) ? (nbit - j): 8;
1799 for (i = 1; i < wsize; i++) {
1800 over = (bitmap[i] >> (8 - s));
1802 bitmap[i-1] |= over;
1809 /* Return a printable string for the IPv4 address. */
1811 inet_ntoa4(struct in_addr ina)
1813 static char buf[4][4 * sizeof "123" + 4];
1814 unsigned char *ucp = (unsigned char *) &ina;
1818 sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff,
1819 ucp[2] & 0xff, ucp[3] & 0xff);
1823 /* Return a printable string for the address. */
1825 ipsec_address(union sockaddr_union* sa)
1827 switch (sa->sa.sa_family) {
1830 return inet_ntoa4(sa->sin.sin_addr);
1835 return ip6_sprintf(&sa->sin6.sin6_addr);
1839 return "(unknown address family)";
1845 struct secasvar *sav;
1847 static char buf[256];
1849 struct secasindex *saidx = &sav->sah->saidx;
1851 IPSEC_ASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
1852 ("address family mismatch"));
1855 snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
1858 /* NB: only use ipsec_address on one address at a time */
1859 snprintf(p, sizeof (buf) - (p - buf), "src=%s ",
1860 ipsec_address(&saidx->src));
1863 snprintf(p, sizeof (buf) - (p - buf), "dst=%s)",
1864 ipsec_address(&saidx->dst));
1880 p = mtod(m, u_char *);
1881 for (i = 0; i < m->m_len; i++) {
1882 printf("%02x ", p[i]);
1884 if (totlen % 16 == 0)
1889 if (totlen % 16 != 0)
1897 SECPOLICY_LOCK_INIT(&ip4_def_policy);
1898 ip4_def_policy.refcnt = 1; /* NB: disallow free */
1900 SYSINIT(ipsec, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, ipsec_attach, NULL)
1903 /* XXX this stuff doesn't belong here... */
1905 static struct xformsw* xforms = NULL;
1908 * Register a transform; typically at system startup.
1911 xform_register(struct xformsw* xsp)
1913 xsp->xf_next = xforms;
1918 * Initialize transform support in an sav.
1921 xform_init(struct secasvar *sav, int xftype)
1923 struct xformsw *xsp;
1925 if (sav->tdb_xform != NULL) /* previously initialized */
1927 for (xsp = xforms; xsp; xsp = xsp->xf_next)
1928 if (xsp->xf_type == xftype)
1929 return (*xsp->xf_init)(sav, xsp);