1 /* $FreeBSD: src/sys/netipsec/ipsec.c,v 1.2.2.1 2003/01/24 05:11:35 sam Exp $ */
2 /* $DragonFly: src/sys/netproto/ipsec/ipsec.c,v 1.5 2004/02/14 21:15:33 dillon Exp $ */
3 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * IPsec controller part.
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/errno.h>
52 #include <sys/kernel.h>
53 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
56 #include <sys/in_cksum.h>
59 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/in_var.h>
66 #include <netinet/udp.h>
67 #include <netinet/udp_var.h>
68 #include <netinet/tcp.h>
69 #include <netinet/udp.h>
71 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
75 #include <netinet/in_pcb.h>
77 #include <netinet/icmp6.h>
86 #include "ipcomp.h" /*XXX*/
87 #include "ipcomp_var.h"
91 #include "key_debug.h"
95 #include <net/net_osdep.h>
103 /* NB: name changed so netstat doesn't use it */
104 struct newipsecstat newipsecstat;
105 int ip4_ah_offsetmask = 0; /* maybe IP_DF? */
106 int ip4_ipsec_dfbit = 0; /* DF bit on encap. 0: clear 1: set 2: copy */
107 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
108 int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
109 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
110 int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
111 struct secpolicy ip4_def_policy;
112 int ip4_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
113 int ip4_esp_randpad = -1;
115 * Crypto support requirements:
117 * 1 require hardware support
118 * -1 require software support
121 int crypto_support = 0;
123 SYSCTL_DECL(_net_inet_ipsec);
126 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY,
127 def_policy, CTLFLAG_RW, &ip4_def_policy.policy, 0, "");
128 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
129 CTLFLAG_RW, &ip4_esp_trans_deflev, 0, "");
130 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
131 CTLFLAG_RW, &ip4_esp_net_deflev, 0, "");
132 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
133 CTLFLAG_RW, &ip4_ah_trans_deflev, 0, "");
134 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
135 CTLFLAG_RW, &ip4_ah_net_deflev, 0, "");
136 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS,
137 ah_cleartos, CTLFLAG_RW, &ah_cleartos, 0, "");
138 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK,
139 ah_offsetmask, CTLFLAG_RW, &ip4_ah_offsetmask, 0, "");
140 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT,
141 dfbit, CTLFLAG_RW, &ip4_ipsec_dfbit, 0, "");
142 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN,
143 ecn, CTLFLAG_RW, &ip4_ipsec_ecn, 0, "");
144 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG,
145 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
146 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ESP_RANDPAD,
147 esp_randpad, CTLFLAG_RW, &ip4_esp_randpad, 0, "");
148 SYSCTL_INT(_net_inet_ipsec, OID_AUTO,
149 crypto_support, CTLFLAG_RW, &crypto_support,0, "");
150 SYSCTL_STRUCT(_net_inet_ipsec, OID_AUTO,
151 ipsecstats, CTLFLAG_RD, &newipsecstat, newipsecstat, "");
154 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
155 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
156 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
157 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
158 int ip6_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
159 int ip6_esp_randpad = -1;
161 SYSCTL_DECL(_net_inet6_ipsec6);
163 /* net.inet6.ipsec6 */
165 SYSCTL_OID(_net_inet6_ipsec6, IPSECCTL_STATS, stats, CTLFLAG_RD,
166 0,0, compat_ipsecstats_sysctl, "S", "");
167 #endif /* COMPAT_KAME */
168 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY,
169 def_policy, CTLFLAG_RW, &ip4_def_policy.policy, 0, "");
170 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
171 CTLFLAG_RW, &ip6_esp_trans_deflev, 0, "");
172 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
173 CTLFLAG_RW, &ip6_esp_net_deflev, 0, "");
174 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
175 CTLFLAG_RW, &ip6_ah_trans_deflev, 0, "");
176 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
177 CTLFLAG_RW, &ip6_ah_net_deflev, 0, "");
178 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN,
179 ecn, CTLFLAG_RW, &ip6_ipsec_ecn, 0, "");
180 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG,
181 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
182 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ESP_RANDPAD,
183 esp_randpad, CTLFLAG_RW, &ip6_esp_randpad, 0, "");
186 static int ipsec4_setspidx_inpcb (struct mbuf *, struct inpcb *pcb);
188 static int ipsec6_setspidx_in6pcb (struct mbuf *, struct in6pcb *pcb);
190 static int ipsec_setspidx (struct mbuf *, struct secpolicyindex *, int);
191 static void ipsec4_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
192 static int ipsec4_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
194 static void ipsec6_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
195 static int ipsec6_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
197 static void ipsec_delpcbpolicy (struct inpcbpolicy *);
198 static struct secpolicy *ipsec_deepcopy_policy (struct secpolicy *src);
199 static int ipsec_set_policy (struct secpolicy **pcb_sp,
200 int optname, caddr_t request, size_t len, int priv);
201 static int ipsec_get_policy (struct secpolicy *pcb_sp, struct mbuf **mp);
202 static void vshiftl (unsigned char *, int, int);
203 static size_t ipsec_hdrsiz (struct secpolicy *);
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 KASSERT(tdbi != NULL, ("ipsec_getpolicy: null tdbi"));
251 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
252 ("ipsec_getpolicy: 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 KASSERT(sp != NULL, ("ipsec_getpolicy: 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;
285 KASSERT(m != NULL, ("ipsec_getpolicybysock: null mbuf"));
286 KASSERT(inp != NULL, ("ipsec_getpolicybysock: null inpcb"));
287 KASSERT(error != NULL, ("ipsec_getpolicybysock: null error"));
288 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
289 ("ipsec_getpolicybysock: invalid direction %u", dir));
291 af = inp->inp_socket->so_proto->pr_domain->dom_family;
292 KASSERT(af == AF_INET || af == AF_INET6,
293 ("ipsec_getpolicybysock: unexpected protocol family %u", af));
297 /* set spidx in pcb */
298 *error = ipsec4_setspidx_inpcb(m, inp);
303 /* set spidx in pcb */
304 *error = ipsec6_setspidx_in6pcb(m, inp);
305 pcbsp = inp->in6p_sp;
309 *error = EPFNOSUPPORT;
315 KASSERT(pcbsp != NULL, ("ipsec_getpolicybysock: null pcbsp"));
317 case IPSEC_DIR_INBOUND:
318 currsp = pcbsp->sp_in;
320 case IPSEC_DIR_OUTBOUND:
321 currsp = pcbsp->sp_out;
324 KASSERT(currsp != NULL, ("ipsec_getpolicybysock: null currsp"));
326 if (pcbsp->priv) { /* when privilieged socket */
327 switch (currsp->policy) {
328 case IPSEC_POLICY_BYPASS:
329 case IPSEC_POLICY_IPSEC:
334 case IPSEC_POLICY_ENTRUST:
335 /* look for a policy in SPD */
336 sp = KEY_ALLOCSP(&currsp->spidx, dir);
337 if (sp == NULL) /* no SP found */
338 sp = KEY_ALLOCSP_DEFAULT();
342 ipseclog((LOG_ERR, "ipsec_getpolicybysock: "
343 "Invalid policy for PCB %d\n", currsp->policy));
347 } else { /* unpriv, SPD has policy */
348 sp = KEY_ALLOCSP(&currsp->spidx, dir);
349 if (sp == NULL) { /* no SP found */
350 switch (currsp->policy) {
351 case IPSEC_POLICY_BYPASS:
352 ipseclog((LOG_ERR, "ipsec_getpolicybysock: "
353 "Illegal policy for non-priviliged defined %d\n",
358 case IPSEC_POLICY_ENTRUST:
359 sp = KEY_ALLOCSP_DEFAULT();
362 case IPSEC_POLICY_IPSEC:
368 ipseclog((LOG_ERR, "ipsec_getpolicybysock: "
369 "Invalid policy for PCB %d\n", currsp->policy));
376 ("ipsec_getpolicybysock: null SP (priv %u policy %u",
377 pcbsp->priv, currsp->policy));
378 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
379 printf("DP ipsec_getpolicybysock (priv %u policy %u) allocates "
380 "SP:%p (refcnt %u)\n", pcbsp->priv, currsp->policy,
386 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
387 * and return a pointer to SP.
388 * OUT: positive: a pointer to the entry for security policy leaf matched.
389 * NULL: no apropreate SP found, the following value is set to error.
391 * EACCES : discard packet.
392 * ENOENT : ipsec_acquire() in progress, maybe.
393 * others : error occured.
396 ipsec_getpolicybyaddr(m, dir, flag, error)
402 struct secpolicyindex spidx;
403 struct secpolicy *sp;
405 KASSERT(m != NULL, ("ipsec_getpolicybyaddr: null mbuf"));
406 KASSERT(error != NULL, ("ipsec_getpolicybyaddr: null error"));
407 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
408 ("ipsec4_getpolicybaddr: invalid direction %u", dir));
411 if (key_havesp(dir)) {
412 /* Make an index to look for a policy. */
413 *error = ipsec_setspidx(m, &spidx,
414 (flag & IP_FORWARDING) ? 0 : 1);
416 DPRINTF(("ipsec_getpolicybyaddr: setpidx failed,"
417 " dir %u flag %u\n", dir, flag));
418 bzero(&spidx, sizeof (spidx));
423 sp = KEY_ALLOCSP(&spidx, dir);
425 if (sp == NULL) /* no SP found, use system default */
426 sp = KEY_ALLOCSP_DEFAULT();
427 KASSERT(sp != NULL, ("ipsec_getpolicybyaddr: null SP"));
432 ipsec4_checkpolicy(m, dir, flag, error, inp)
438 struct secpolicy *sp;
442 sp = ipsec_getpolicybyaddr(m, dir, flag, error);
444 sp = ipsec_getpolicybysock(m, dir, inp, error);
447 ("ipsec4_checkpolicy: getpolicy failed w/o error"));
448 newipsecstat.ips_out_inval++;
452 ("ipsec4_checkpolicy: sp w/ error set to %u", *error));
453 switch (sp->policy) {
454 case IPSEC_POLICY_ENTRUST:
456 printf("ipsec4_checkpolicy: invalid policy %u\n", sp->policy);
458 case IPSEC_POLICY_DISCARD:
459 newipsecstat.ips_out_polvio++;
460 *error = -EINVAL; /* packet is discarded by caller */
462 case IPSEC_POLICY_BYPASS:
463 case IPSEC_POLICY_NONE:
465 sp = NULL; /* NB: force NULL result */
467 case IPSEC_POLICY_IPSEC:
468 if (sp->req == NULL) /* acquire an SA */
469 *error = key_spdacquire(sp);
480 ipsec4_setspidx_inpcb(m, pcb)
486 KASSERT(pcb != NULL, ("ipsec4_setspidx_inpcb: null pcb"));
487 KASSERT(pcb->inp_sp != NULL, ("ipsec4_setspidx_inpcb: null inp_sp"));
488 KASSERT(pcb->inp_sp->sp_out != NULL && pcb->inp_sp->sp_in != NULL,
489 ("ipsec4_setspidx_inpcb: null sp_in || sp_out"));
491 error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
493 pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
494 pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
495 pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
497 bzero(&pcb->inp_sp->sp_in->spidx,
498 sizeof (pcb->inp_sp->sp_in->spidx));
499 bzero(&pcb->inp_sp->sp_out->spidx,
500 sizeof (pcb->inp_sp->sp_in->spidx));
507 ipsec6_setspidx_in6pcb(m, pcb)
511 struct secpolicyindex *spidx;
514 KASSERT(pcb != NULL, ("ipsec6_setspidx_in6pcb: null pcb"));
515 KASSERT(pcb->in6p_sp != NULL, ("ipsec6_setspidx_in6pcb: null inp_sp"));
516 KASSERT(pcb->in6p_sp->sp_out != NULL && pcb->in6p_sp->sp_in != NULL,
517 ("ipsec6_setspidx_in6pcb: null sp_in || sp_out"));
519 bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
520 bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
522 spidx = &pcb->in6p_sp->sp_in->spidx;
523 error = ipsec_setspidx(m, spidx, 1);
526 spidx->dir = IPSEC_DIR_INBOUND;
528 spidx = &pcb->in6p_sp->sp_out->spidx;
529 error = ipsec_setspidx(m, spidx, 1);
532 spidx->dir = IPSEC_DIR_OUTBOUND;
537 bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
538 bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
544 * configure security policy index (src/dst/proto/sport/dport)
545 * by looking at the content of mbuf.
546 * the caller is responsible for error recovery (like clearing up spidx).
549 ipsec_setspidx(m, spidx, needport)
551 struct secpolicyindex *spidx;
554 struct ip *ip = NULL;
561 KASSERT(m != NULL, ("ipsec_setspidx: null mbuf"));
564 * validate m->m_pkthdr.len. we see incorrect length if we
565 * mistakenly call this function with inconsistent mbuf chain
566 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
569 for (n = m; n; n = n->m_next)
571 if (m->m_pkthdr.len != len) {
572 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
573 printf("ipsec_setspidx: "
574 "total of m_len(%d) != pkthdr.len(%d), "
576 len, m->m_pkthdr.len));
580 if (m->m_pkthdr.len < sizeof(struct ip)) {
581 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
582 printf("ipsec_setspidx: "
583 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
588 if (m->m_len >= sizeof(*ip))
589 ip = mtod(m, struct ip *);
591 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
595 v = _IP_VHL_V(ip->ip_vhl);
601 error = ipsec4_setspidx_ipaddr(m, spidx);
604 ipsec4_get_ulp(m, spidx, needport);
608 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
609 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
610 printf("ipsec_setspidx: "
611 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
612 "ignored.\n", m->m_pkthdr.len));
615 error = ipsec6_setspidx_ipaddr(m, spidx);
618 ipsec6_get_ulp(m, spidx, needport);
622 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
623 printf("ipsec_setspidx: "
624 "unknown IP version %u, ignored.\n", v));
630 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
636 KASSERT(m != NULL, ("ipsec4_get_ulp: null mbuf"));
637 KASSERT(m->m_pkthdr.len >= sizeof(struct ip),
638 ("ipsec4_get_ulp: packet too short"));
640 /* NB: ip_input() flips it into host endian XXX need more checking */
641 if (m->m_len < sizeof (struct ip)) {
642 struct ip *ip = mtod(m, struct ip *);
643 if (ip->ip_off & (IP_MF | IP_OFFMASK))
646 off = _IP_VHL_HL(ip->ip_vhl) << 2;
648 off = ip->ip_hl << 2;
654 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
655 if (ih.ip_off & (IP_MF | IP_OFFMASK))
658 off = _IP_VHL_HL(ih.ip_vhl) << 2;
665 while (off < m->m_pkthdr.len) {
672 spidx->ul_proto = nxt;
675 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
677 m_copydata(m, off, sizeof (th), (caddr_t) &th);
678 spidx->src.sin.sin_port = th.th_sport;
679 spidx->dst.sin.sin_port = th.th_dport;
682 spidx->ul_proto = nxt;
685 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
687 m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
688 spidx->src.sin.sin_port = uh.uh_sport;
689 spidx->dst.sin.sin_port = uh.uh_dport;
692 if (m->m_pkthdr.len > off + sizeof(ip6e))
694 /* XXX sigh, this works but is totally bogus */
695 m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
696 off += (ip6e.ip6e_len + 2) << 2;
701 /* XXX intermediate headers??? */
702 spidx->ul_proto = nxt;
707 spidx->ul_proto = IPSEC_ULPROTO_ANY;
709 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
710 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
713 /* assumes that m is sane */
715 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
717 static const struct sockaddr_in template = {
718 sizeof (struct sockaddr_in),
720 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
723 spidx->src.sin = template;
724 spidx->dst.sin = template;
726 if (m->m_len < sizeof (struct ip)) {
727 m_copydata(m, offsetof(struct ip, ip_src),
728 sizeof (struct in_addr),
729 (caddr_t) &spidx->src.sin.sin_addr);
730 m_copydata(m, offsetof(struct ip, ip_dst),
731 sizeof (struct in_addr),
732 (caddr_t) &spidx->dst.sin.sin_addr);
734 struct ip *ip = mtod(m, struct ip *);
735 spidx->src.sin.sin_addr = ip->ip_src;
736 spidx->dst.sin.sin_addr = ip->ip_dst;
739 spidx->prefs = sizeof(struct in_addr) << 3;
740 spidx->prefd = sizeof(struct in_addr) << 3;
747 ipsec6_get_ulp(m, spidx, needport)
749 struct secpolicyindex *spidx;
758 panic("ipsec6_get_ulp: NULL pointer was passed.\n");
760 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
761 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m));
764 spidx->ul_proto = IPSEC_ULPROTO_ANY;
765 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
766 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
769 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
770 if (off < 0 || m->m_pkthdr.len < off)
775 spidx->ul_proto = nxt;
778 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
780 m_copydata(m, off, sizeof(th), (caddr_t)&th);
781 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
782 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
785 spidx->ul_proto = nxt;
788 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
790 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
791 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
792 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
796 /* XXX intermediate headers??? */
797 spidx->ul_proto = nxt;
802 /* assumes that m is sane */
804 ipsec6_setspidx_ipaddr(m, spidx)
806 struct secpolicyindex *spidx;
808 struct ip6_hdr *ip6 = NULL;
809 struct ip6_hdr ip6buf;
810 struct sockaddr_in6 *sin6;
812 if (m->m_len >= sizeof(*ip6))
813 ip6 = mtod(m, struct ip6_hdr *);
815 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
819 sin6 = (struct sockaddr_in6 *)&spidx->src;
820 bzero(sin6, sizeof(*sin6));
821 sin6->sin6_family = AF_INET6;
822 sin6->sin6_len = sizeof(struct sockaddr_in6);
823 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
824 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
825 sin6->sin6_addr.s6_addr16[1] = 0;
826 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
828 spidx->prefs = sizeof(struct in6_addr) << 3;
830 sin6 = (struct sockaddr_in6 *)&spidx->dst;
831 bzero(sin6, sizeof(*sin6));
832 sin6->sin6_family = AF_INET6;
833 sin6->sin6_len = sizeof(struct sockaddr_in6);
834 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
835 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
836 sin6->sin6_addr.s6_addr16[1] = 0;
837 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
839 spidx->prefd = sizeof(struct in6_addr) << 3;
846 ipsec_delpcbpolicy(p)
847 struct inpcbpolicy *p;
852 /* initialize policy in PCB */
854 ipsec_init_policy(so, pcb_sp)
856 struct inpcbpolicy **pcb_sp;
858 struct inpcbpolicy *new;
861 if (so == NULL || pcb_sp == NULL)
862 panic("ipsec_init_policy: NULL pointer was passed.\n");
864 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
865 M_SECA, M_NOWAIT|M_ZERO);
867 ipseclog((LOG_DEBUG, "ipsec_init_policy: No more memory.\n"));
871 if (so->so_cred != 0 && so->so_cred->cr_uid == 0)
876 if ((new->sp_in = KEY_NEWSP()) == NULL) {
877 ipsec_delpcbpolicy(new);
880 new->sp_in->state = IPSEC_SPSTATE_ALIVE;
881 new->sp_in->policy = IPSEC_POLICY_ENTRUST;
883 if ((new->sp_out = KEY_NEWSP()) == NULL) {
884 KEY_FREESP(&new->sp_in);
885 ipsec_delpcbpolicy(new);
888 new->sp_out->state = IPSEC_SPSTATE_ALIVE;
889 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
896 /* copy old ipsec policy into new */
898 ipsec_copy_policy(old, new)
899 struct inpcbpolicy *old, *new;
901 struct secpolicy *sp;
903 sp = ipsec_deepcopy_policy(old->sp_in);
905 KEY_FREESP(&new->sp_in);
910 sp = ipsec_deepcopy_policy(old->sp_out);
912 KEY_FREESP(&new->sp_out);
917 new->priv = old->priv;
922 /* deep-copy a policy in PCB */
923 static struct secpolicy *
924 ipsec_deepcopy_policy(src)
925 struct secpolicy *src;
927 struct ipsecrequest *newchain = NULL;
928 struct ipsecrequest *p;
929 struct ipsecrequest **q;
930 struct ipsecrequest *r;
931 struct secpolicy *dst;
940 * deep-copy IPsec request chain. This is required since struct
941 * ipsecrequest is not reference counted.
944 for (p = src->req; p; p = p->next) {
945 *q = (struct ipsecrequest *)malloc(sizeof(struct ipsecrequest),
949 bzero(*q, sizeof(**q));
952 (*q)->saidx.proto = p->saidx.proto;
953 (*q)->saidx.mode = p->saidx.mode;
954 (*q)->level = p->level;
955 (*q)->saidx.reqid = p->saidx.reqid;
957 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
958 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
967 dst->state = src->state;
968 dst->policy = src->policy;
969 /* do not touch the refcnt fields */
974 for (p = newchain; p; p = r) {
982 /* set policy and ipsec request if present. */
984 ipsec_set_policy(pcb_sp, optname, request, len, priv)
985 struct secpolicy **pcb_sp;
991 struct sadb_x_policy *xpl;
992 struct secpolicy *newsp = NULL;
996 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
998 if (len < sizeof(*xpl))
1000 xpl = (struct sadb_x_policy *)request;
1002 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1003 printf("ipsec_set_policy: passed policy\n");
1004 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
1006 /* check policy type */
1007 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1008 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
1009 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1012 /* check privileged socket */
1013 if (priv == 0 && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS)
1016 /* allocation new SP entry */
1017 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1020 newsp->state = IPSEC_SPSTATE_ALIVE;
1022 /* clear old SP and set new SP */
1025 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1026 printf("ipsec_set_policy: new policy\n");
1027 kdebug_secpolicy(newsp));
1033 ipsec_get_policy(pcb_sp, mp)
1034 struct secpolicy *pcb_sp;
1039 if (pcb_sp == NULL || mp == NULL)
1042 *mp = key_sp2msg(pcb_sp);
1044 ipseclog((LOG_DEBUG, "ipsec_get_policy: No more memory.\n"));
1048 (*mp)->m_type = MT_DATA;
1049 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1050 printf("ipsec_get_policy:\n");
1057 ipsec4_set_policy(inp, optname, request, len, priv)
1064 struct sadb_x_policy *xpl;
1065 struct secpolicy **pcb_sp;
1068 if (inp == NULL || request == NULL)
1070 if (len < sizeof(*xpl))
1072 xpl = (struct sadb_x_policy *)request;
1074 /* select direction */
1075 switch (xpl->sadb_x_policy_dir) {
1076 case IPSEC_DIR_INBOUND:
1077 pcb_sp = &inp->inp_sp->sp_in;
1079 case IPSEC_DIR_OUTBOUND:
1080 pcb_sp = &inp->inp_sp->sp_out;
1083 ipseclog((LOG_ERR, "ipsec4_set_policy: invalid direction=%u\n",
1084 xpl->sadb_x_policy_dir));
1088 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1092 ipsec4_get_policy(inp, request, len, mp)
1098 struct sadb_x_policy *xpl;
1099 struct secpolicy *pcb_sp;
1102 if (inp == NULL || request == NULL || mp == NULL)
1104 KASSERT(inp->inp_sp != NULL, ("ipsec4_get_policy: null inp_sp"));
1105 if (len < sizeof(*xpl))
1107 xpl = (struct sadb_x_policy *)request;
1109 /* select direction */
1110 switch (xpl->sadb_x_policy_dir) {
1111 case IPSEC_DIR_INBOUND:
1112 pcb_sp = inp->inp_sp->sp_in;
1114 case IPSEC_DIR_OUTBOUND:
1115 pcb_sp = inp->inp_sp->sp_out;
1118 ipseclog((LOG_ERR, "ipsec4_set_policy: invalid direction=%u\n",
1119 xpl->sadb_x_policy_dir));
1123 return ipsec_get_policy(pcb_sp, mp);
1126 /* delete policy in PCB */
1128 ipsec4_delete_pcbpolicy(inp)
1131 KASSERT(inp != NULL, ("ipsec4_delete_pcbpolicy: null inp"));
1133 if (inp->inp_sp == NULL)
1136 if (inp->inp_sp->sp_in != NULL)
1137 KEY_FREESP(&inp->inp_sp->sp_in);
1139 if (inp->inp_sp->sp_out != NULL)
1140 KEY_FREESP(&inp->inp_sp->sp_out);
1142 ipsec_delpcbpolicy(inp->inp_sp);
1150 ipsec6_set_policy(in6p, optname, request, len, priv)
1151 struct in6pcb *in6p;
1157 struct sadb_x_policy *xpl;
1158 struct secpolicy **pcb_sp;
1161 if (in6p == NULL || request == NULL)
1163 if (len < sizeof(*xpl))
1165 xpl = (struct sadb_x_policy *)request;
1167 /* select direction */
1168 switch (xpl->sadb_x_policy_dir) {
1169 case IPSEC_DIR_INBOUND:
1170 pcb_sp = &in6p->in6p_sp->sp_in;
1172 case IPSEC_DIR_OUTBOUND:
1173 pcb_sp = &in6p->in6p_sp->sp_out;
1176 ipseclog((LOG_ERR, "ipsec6_set_policy: invalid direction=%u\n",
1177 xpl->sadb_x_policy_dir));
1181 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1185 ipsec6_get_policy(in6p, request, len, mp)
1186 struct in6pcb *in6p;
1191 struct sadb_x_policy *xpl;
1192 struct secpolicy *pcb_sp;
1195 if (in6p == NULL || request == NULL || mp == NULL)
1197 KASSERT(in6p->in6p_sp != NULL, ("ipsec6_get_policy: null in6p_sp"));
1198 if (len < sizeof(*xpl))
1200 xpl = (struct sadb_x_policy *)request;
1202 /* select direction */
1203 switch (xpl->sadb_x_policy_dir) {
1204 case IPSEC_DIR_INBOUND:
1205 pcb_sp = in6p->in6p_sp->sp_in;
1207 case IPSEC_DIR_OUTBOUND:
1208 pcb_sp = in6p->in6p_sp->sp_out;
1211 ipseclog((LOG_ERR, "ipsec6_set_policy: invalid direction=%u\n",
1212 xpl->sadb_x_policy_dir));
1216 return ipsec_get_policy(pcb_sp, mp);
1220 ipsec6_delete_pcbpolicy(in6p)
1221 struct in6pcb *in6p;
1223 KASSERT(in6p != NULL, ("ipsec6_delete_pcbpolicy: null in6p"));
1225 if (in6p->in6p_sp == NULL)
1228 if (in6p->in6p_sp->sp_in != NULL)
1229 KEY_FREESP(&in6p->in6p_sp->sp_in);
1231 if (in6p->in6p_sp->sp_out != NULL)
1232 KEY_FREESP(&in6p->in6p_sp->sp_out);
1234 ipsec_delpcbpolicy(in6p->in6p_sp);
1235 in6p->in6p_sp = NULL;
1242 * return current level.
1243 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1246 ipsec_get_reqlevel(isr)
1247 struct ipsecrequest *isr;
1250 u_int esp_trans_deflev, esp_net_deflev;
1251 u_int ah_trans_deflev, ah_net_deflev;
1253 KASSERT(isr != NULL && isr->sp != NULL,
1254 ("ipsec_get_reqlevel: null argument"));
1255 KASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1256 ("ipsec_get_reqlevel: af family mismatch, src %u, dst %u",
1257 isr->sp->spidx.src.sa.sa_family,
1258 isr->sp->spidx.dst.sa.sa_family));
1260 /* XXX note that we have ipseclog() expanded here - code sync issue */
1261 #define IPSEC_CHECK_DEFAULT(lev) \
1262 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1263 && (lev) != IPSEC_LEVEL_UNIQUE) \
1265 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1266 (lev), IPSEC_LEVEL_REQUIRE) \
1268 (lev) = IPSEC_LEVEL_REQUIRE, \
1272 /* set default level */
1273 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1276 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1277 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1278 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1279 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1284 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1285 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1286 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1287 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1291 panic("key_get_reqlevel: unknown af %u",
1292 isr->sp->spidx.src.sa.sa_family);
1295 #undef IPSEC_CHECK_DEFAULT
1298 switch (isr->level) {
1299 case IPSEC_LEVEL_DEFAULT:
1300 switch (isr->saidx.proto) {
1302 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1303 level = esp_net_deflev;
1305 level = esp_trans_deflev;
1308 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1309 level = ah_net_deflev;
1311 level = ah_trans_deflev;
1312 case IPPROTO_IPCOMP:
1314 * we don't really care, as IPcomp document says that
1315 * we shouldn't compress small packets
1317 level = IPSEC_LEVEL_USE;
1320 panic("ipsec_get_reqlevel: "
1321 "Illegal protocol defined %u\n",
1326 case IPSEC_LEVEL_USE:
1327 case IPSEC_LEVEL_REQUIRE:
1330 case IPSEC_LEVEL_UNIQUE:
1331 level = IPSEC_LEVEL_REQUIRE;
1335 panic("ipsec_get_reqlevel: Illegal IPsec level %u\n",
1343 * Check security policy requirements against the actual
1344 * packet contents. Return one if the packet should be
1345 * reject as "invalid"; otherwiser return zero to have the
1346 * packet treated as "valid".
1353 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1355 struct ipsecrequest *isr;
1358 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1359 printf("ipsec_in_reject: using SP\n");
1360 kdebug_secpolicy(sp));
1363 switch (sp->policy) {
1364 case IPSEC_POLICY_DISCARD:
1366 case IPSEC_POLICY_BYPASS:
1367 case IPSEC_POLICY_NONE:
1371 KASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1372 ("ipsec_in_reject: invalid policy %u", sp->policy));
1374 /* XXX should compare policy against ipsec header history */
1377 for (isr = sp->req; isr != NULL; isr = isr->next) {
1378 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1380 switch (isr->saidx.proto) {
1382 if ((m->m_flags & M_DECRYPTED) == 0) {
1383 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1384 printf("ipsec_in_reject: ESP m_flags:%x\n",
1391 isr->sav->tdb_authalgxform != NULL &&
1392 (m->m_flags & M_AUTHIPDGM) == 0) {
1393 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1394 printf("ipsec_in_reject: ESP/AH m_flags:%x\n",
1401 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1402 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1403 printf("ipsec_in_reject: AH m_flags:%x\n",
1408 case IPPROTO_IPCOMP:
1410 * we don't really care, as IPcomp document
1411 * says that we shouldn't compress small
1412 * packets, IPComp policy should always be
1413 * treated as being in "use" level.
1418 return 0; /* valid */
1422 * Check AH/ESP integrity.
1423 * This function is called from tcp_input(), udp_input(),
1424 * and {ah,esp}4_input for tunnel mode
1427 ipsec4_in_reject(m, inp)
1431 struct secpolicy *sp;
1435 KASSERT(m != NULL, ("ipsec4_in_reject_so: null mbuf"));
1437 /* get SP for this packet.
1438 * When we are called from ip_forward(), we call
1439 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1442 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1444 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1447 result = ipsec_in_reject(sp, m);
1449 newipsecstat.ips_in_polvio++;
1452 result = 0; /* XXX should be panic ?
1453 * -> No, there may be error. */
1460 * Check AH/ESP integrity.
1461 * This function is called from tcp6_input(), udp6_input(),
1462 * and {ah,esp}6_input for tunnel mode
1465 ipsec6_in_reject(m, inp)
1469 struct secpolicy *sp = NULL;
1475 return 0; /* XXX should be panic ? */
1477 /* get SP for this packet.
1478 * When we are called from ip_forward(), we call
1479 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1482 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1484 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1487 result = ipsec_in_reject(sp, m);
1489 newipsecstat.ips_in_polvio++;
1499 * compute the byte size to be occupied by IPsec header.
1500 * in case it is tunneled, it includes the size of outer IP header.
1501 * NOTE: SP passed is free in this function.
1504 ipsec_hdrsiz(struct secpolicy *sp)
1506 struct ipsecrequest *isr;
1509 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1510 printf("ipsec_hdrsiz: using SP\n");
1511 kdebug_secpolicy(sp));
1513 switch (sp->policy) {
1514 case IPSEC_POLICY_DISCARD:
1515 case IPSEC_POLICY_BYPASS:
1516 case IPSEC_POLICY_NONE:
1520 KASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1521 ("ipsec_hdrsiz: invalid policy %u", sp->policy));
1524 for (isr = sp->req; isr != NULL; isr = isr->next) {
1527 switch (isr->saidx.proto) {
1529 clen = esp_hdrsiz(isr->sav);
1532 clen = ah_hdrsiz(isr->sav);
1534 case IPPROTO_IPCOMP:
1535 clen = sizeof(struct ipcomp);
1539 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1540 switch (isr->saidx.dst.sa.sa_family) {
1542 clen += sizeof(struct ip);
1546 clen += sizeof(struct ip6_hdr);
1550 ipseclog((LOG_ERR, "ipsec_hdrsiz: "
1551 "unknown AF %d in IPsec tunnel SA\n",
1552 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1562 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1564 ipsec4_hdrsiz(m, dir, inp)
1569 struct secpolicy *sp;
1573 KASSERT(m != NULL, ("ipsec4_hdrsiz: null mbuf"));
1574 KASSERT(inp == NULL || inp->inp_socket != NULL,
1575 ("ipsec4_hdrsize: socket w/o inpcb"));
1577 /* get SP for this packet.
1578 * When we are called from ip_forward(), we call
1579 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1582 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1584 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1587 size = ipsec_hdrsiz(sp);
1588 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1589 printf("ipsec4_hdrsiz: size:%lu.\n",
1590 (unsigned long)size));
1594 size = 0; /* XXX should be panic ? */
1600 /* This function is called from ipsec6_hdrsize_tcp(),
1601 * and maybe from ip6_forward.()
1604 ipsec6_hdrsiz(m, dir, in6p)
1607 struct in6pcb *in6p;
1609 struct secpolicy *sp;
1613 KASSERT(m != NULL, ("ipsec6_hdrsiz: null mbuf"));
1614 KASSERT(in6p == NULL || in6p->in6p_socket != NULL,
1615 ("ipsec6_hdrsize: socket w/o inpcb"));
1617 /* get SP for this packet */
1618 /* XXX Is it right to call with IP_FORWARDING. */
1620 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1622 sp = ipsec_getpolicybysock(m, dir, in6p, &error);
1626 size = ipsec_hdrsiz(sp);
1627 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1628 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size));
1636 * Check the variable replay window.
1637 * ipsec_chkreplay() performs replay check before ICV verification.
1638 * ipsec_updatereplay() updates replay bitmap. This must be called after
1639 * ICV verification (it also performs replay check, which is usually done
1641 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1643 * based on RFC 2401.
1646 ipsec_chkreplay(seq, sav)
1648 struct secasvar *sav;
1650 const struct secreplay *replay;
1653 u_int32_t wsizeb; /* constant: bits of window size */
1654 int frlast; /* constant: last frame */
1656 SPLASSERT(net, "ipsec_chkreplay");
1658 KASSERT(sav != NULL, ("ipsec_chkreplay: Null SA"));
1659 KASSERT(sav->replay != NULL, ("ipsec_chkreplay: Null replay state"));
1661 replay = sav->replay;
1663 if (replay->wsize == 0)
1664 return 1; /* no need to check replay. */
1667 frlast = replay->wsize - 1;
1668 wsizeb = replay->wsize << 3;
1670 /* sequence number of 0 is invalid */
1674 /* first time is always okay */
1675 if (replay->count == 0)
1678 if (seq > replay->lastseq) {
1679 /* larger sequences are okay */
1682 /* seq is equal or less than lastseq. */
1683 diff = replay->lastseq - seq;
1685 /* over range to check, i.e. too old or wrapped */
1689 fr = frlast - diff / 8;
1691 /* this packet already seen ? */
1692 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1695 /* out of order but good */
1701 * check replay counter whether to update or not.
1706 ipsec_updatereplay(seq, sav)
1708 struct secasvar *sav;
1710 struct secreplay *replay;
1713 u_int32_t wsizeb; /* constant: bits of window size */
1714 int frlast; /* constant: last frame */
1716 SPLASSERT(net, "ipsec_updatereplay");
1718 KASSERT(sav != NULL, ("ipsec_updatereplay: Null SA"));
1719 KASSERT(sav->replay != NULL, ("ipsec_updatereplay: Null replay state"));
1721 replay = sav->replay;
1723 if (replay->wsize == 0)
1724 goto ok; /* no need to check replay. */
1727 frlast = replay->wsize - 1;
1728 wsizeb = replay->wsize << 3;
1730 /* sequence number of 0 is invalid */
1735 if (replay->count == 0) {
1736 replay->lastseq = seq;
1737 bzero(replay->bitmap, replay->wsize);
1738 (replay->bitmap)[frlast] = 1;
1742 if (seq > replay->lastseq) {
1743 /* seq is larger than lastseq. */
1744 diff = seq - replay->lastseq;
1746 /* new larger sequence number */
1747 if (diff < wsizeb) {
1749 /* set bit for this packet */
1750 vshiftl(replay->bitmap, diff, replay->wsize);
1751 (replay->bitmap)[frlast] |= 1;
1753 /* this packet has a "way larger" */
1754 bzero(replay->bitmap, replay->wsize);
1755 (replay->bitmap)[frlast] = 1;
1757 replay->lastseq = seq;
1759 /* larger is good */
1761 /* seq is equal or less than lastseq. */
1762 diff = replay->lastseq - seq;
1764 /* over range to check, i.e. too old or wrapped */
1768 fr = frlast - diff / 8;
1770 /* this packet already seen ? */
1771 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1775 (replay->bitmap)[fr] |= (1 << (diff % 8));
1777 /* out of order but good */
1781 if (replay->count == ~0) {
1783 /* set overflow flag */
1786 /* don't increment, no more packets accepted */
1787 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1790 ipseclog((LOG_WARNING, "replay counter made %d cycle. %s\n",
1791 replay->overflow, ipsec_logsastr(sav)));
1800 * shift variable length bunffer to left.
1801 * IN: bitmap: pointer to the buffer
1802 * nbit: the number of to shift.
1803 * wsize: buffer size (bytes).
1806 vshiftl(bitmap, nbit, wsize)
1807 unsigned char *bitmap;
1813 for (j = 0; j < nbit; j += 8) {
1814 s = (nbit - j < 8) ? (nbit - j): 8;
1816 for (i = 1; i < wsize; i++) {
1817 over = (bitmap[i] >> (8 - s));
1819 bitmap[i-1] |= over;
1826 /* Return a printable string for the IPv4 address. */
1828 inet_ntoa4(struct in_addr ina)
1830 static char buf[4][4 * sizeof "123" + 4];
1831 unsigned char *ucp = (unsigned char *) &ina;
1835 sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff,
1836 ucp[2] & 0xff, ucp[3] & 0xff);
1840 /* Return a printable string for the address. */
1842 ipsec_address(union sockaddr_union* sa)
1844 switch (sa->sa.sa_family) {
1847 return inet_ntoa4(sa->sin.sin_addr);
1852 return ip6_sprintf(&sa->sin6.sin6_addr);
1856 return "(unknown address family)";
1862 struct secasvar *sav;
1864 static char buf[256];
1866 struct secasindex *saidx = &sav->sah->saidx;
1868 KASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
1869 ("ipsec_logsastr: address family mismatch"));
1872 snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
1875 /* NB: only use ipsec_address on one address at a time */
1876 snprintf(p, sizeof (buf) - (p - buf), "src=%s ",
1877 ipsec_address(&saidx->src));
1880 snprintf(p, sizeof (buf) - (p - buf), "dst=%s)",
1881 ipsec_address(&saidx->dst));
1897 p = mtod(m, u_char *);
1898 for (i = 0; i < m->m_len; i++) {
1899 printf("%02x ", p[i]);
1901 if (totlen % 16 == 0)
1906 if (totlen % 16 != 0)
1911 /* XXX this stuff doesn't belong here... */
1913 static struct xformsw* xforms = NULL;
1916 * Register a transform; typically at system startup.
1919 xform_register(struct xformsw* xsp)
1921 xsp->xf_next = xforms;
1926 * Initialize transform support in an sav.
1929 xform_init(struct secasvar *sav, int xftype)
1931 struct xformsw *xsp;
1933 for (xsp = xforms; xsp; xsp = xsp->xf_next)
1934 if (xsp->xf_type == xftype)
1935 return (*xsp->xf_init)(sav, xsp);