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.6 2004/04/22 05:09:48 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 = malloc(sizeof(struct inpcbpolicy),
865 M_SECA, M_INTWAIT | M_ZERO | M_NULLOK);
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 = malloc(sizeof(struct ipsecrequest),
946 M_SECA, M_INTWAIT | M_ZERO | M_NULLOK);
951 (*q)->saidx.proto = p->saidx.proto;
952 (*q)->saidx.mode = p->saidx.mode;
953 (*q)->level = p->level;
954 (*q)->saidx.reqid = p->saidx.reqid;
956 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
957 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
966 dst->state = src->state;
967 dst->policy = src->policy;
968 /* do not touch the refcnt fields */
973 for (p = newchain; p; p = r) {
981 /* set policy and ipsec request if present. */
983 ipsec_set_policy(pcb_sp, optname, request, len, priv)
984 struct secpolicy **pcb_sp;
990 struct sadb_x_policy *xpl;
991 struct secpolicy *newsp = NULL;
995 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
997 if (len < sizeof(*xpl))
999 xpl = (struct sadb_x_policy *)request;
1001 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1002 printf("ipsec_set_policy: passed policy\n");
1003 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
1005 /* check policy type */
1006 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1007 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
1008 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1011 /* check privileged socket */
1012 if (priv == 0 && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS)
1015 /* allocation new SP entry */
1016 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1019 newsp->state = IPSEC_SPSTATE_ALIVE;
1021 /* clear old SP and set new SP */
1024 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1025 printf("ipsec_set_policy: new policy\n");
1026 kdebug_secpolicy(newsp));
1032 ipsec_get_policy(pcb_sp, mp)
1033 struct secpolicy *pcb_sp;
1038 if (pcb_sp == NULL || mp == NULL)
1041 *mp = key_sp2msg(pcb_sp);
1043 ipseclog((LOG_DEBUG, "ipsec_get_policy: No more memory.\n"));
1047 (*mp)->m_type = MT_DATA;
1048 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1049 printf("ipsec_get_policy:\n");
1056 ipsec4_set_policy(inp, optname, request, len, priv)
1063 struct sadb_x_policy *xpl;
1064 struct secpolicy **pcb_sp;
1067 if (inp == NULL || request == NULL)
1069 if (len < sizeof(*xpl))
1071 xpl = (struct sadb_x_policy *)request;
1073 /* select direction */
1074 switch (xpl->sadb_x_policy_dir) {
1075 case IPSEC_DIR_INBOUND:
1076 pcb_sp = &inp->inp_sp->sp_in;
1078 case IPSEC_DIR_OUTBOUND:
1079 pcb_sp = &inp->inp_sp->sp_out;
1082 ipseclog((LOG_ERR, "ipsec4_set_policy: invalid direction=%u\n",
1083 xpl->sadb_x_policy_dir));
1087 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1091 ipsec4_get_policy(inp, request, len, mp)
1097 struct sadb_x_policy *xpl;
1098 struct secpolicy *pcb_sp;
1101 if (inp == NULL || request == NULL || mp == NULL)
1103 KASSERT(inp->inp_sp != NULL, ("ipsec4_get_policy: null inp_sp"));
1104 if (len < sizeof(*xpl))
1106 xpl = (struct sadb_x_policy *)request;
1108 /* select direction */
1109 switch (xpl->sadb_x_policy_dir) {
1110 case IPSEC_DIR_INBOUND:
1111 pcb_sp = inp->inp_sp->sp_in;
1113 case IPSEC_DIR_OUTBOUND:
1114 pcb_sp = inp->inp_sp->sp_out;
1117 ipseclog((LOG_ERR, "ipsec4_set_policy: invalid direction=%u\n",
1118 xpl->sadb_x_policy_dir));
1122 return ipsec_get_policy(pcb_sp, mp);
1125 /* delete policy in PCB */
1127 ipsec4_delete_pcbpolicy(inp)
1130 KASSERT(inp != NULL, ("ipsec4_delete_pcbpolicy: null inp"));
1132 if (inp->inp_sp == NULL)
1135 if (inp->inp_sp->sp_in != NULL)
1136 KEY_FREESP(&inp->inp_sp->sp_in);
1138 if (inp->inp_sp->sp_out != NULL)
1139 KEY_FREESP(&inp->inp_sp->sp_out);
1141 ipsec_delpcbpolicy(inp->inp_sp);
1149 ipsec6_set_policy(in6p, optname, request, len, priv)
1150 struct in6pcb *in6p;
1156 struct sadb_x_policy *xpl;
1157 struct secpolicy **pcb_sp;
1160 if (in6p == NULL || request == NULL)
1162 if (len < sizeof(*xpl))
1164 xpl = (struct sadb_x_policy *)request;
1166 /* select direction */
1167 switch (xpl->sadb_x_policy_dir) {
1168 case IPSEC_DIR_INBOUND:
1169 pcb_sp = &in6p->in6p_sp->sp_in;
1171 case IPSEC_DIR_OUTBOUND:
1172 pcb_sp = &in6p->in6p_sp->sp_out;
1175 ipseclog((LOG_ERR, "ipsec6_set_policy: invalid direction=%u\n",
1176 xpl->sadb_x_policy_dir));
1180 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1184 ipsec6_get_policy(in6p, request, len, mp)
1185 struct in6pcb *in6p;
1190 struct sadb_x_policy *xpl;
1191 struct secpolicy *pcb_sp;
1194 if (in6p == NULL || request == NULL || mp == NULL)
1196 KASSERT(in6p->in6p_sp != NULL, ("ipsec6_get_policy: null in6p_sp"));
1197 if (len < sizeof(*xpl))
1199 xpl = (struct sadb_x_policy *)request;
1201 /* select direction */
1202 switch (xpl->sadb_x_policy_dir) {
1203 case IPSEC_DIR_INBOUND:
1204 pcb_sp = in6p->in6p_sp->sp_in;
1206 case IPSEC_DIR_OUTBOUND:
1207 pcb_sp = in6p->in6p_sp->sp_out;
1210 ipseclog((LOG_ERR, "ipsec6_set_policy: invalid direction=%u\n",
1211 xpl->sadb_x_policy_dir));
1215 return ipsec_get_policy(pcb_sp, mp);
1219 ipsec6_delete_pcbpolicy(in6p)
1220 struct in6pcb *in6p;
1222 KASSERT(in6p != NULL, ("ipsec6_delete_pcbpolicy: null in6p"));
1224 if (in6p->in6p_sp == NULL)
1227 if (in6p->in6p_sp->sp_in != NULL)
1228 KEY_FREESP(&in6p->in6p_sp->sp_in);
1230 if (in6p->in6p_sp->sp_out != NULL)
1231 KEY_FREESP(&in6p->in6p_sp->sp_out);
1233 ipsec_delpcbpolicy(in6p->in6p_sp);
1234 in6p->in6p_sp = NULL;
1241 * return current level.
1242 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1245 ipsec_get_reqlevel(isr)
1246 struct ipsecrequest *isr;
1249 u_int esp_trans_deflev, esp_net_deflev;
1250 u_int ah_trans_deflev, ah_net_deflev;
1252 KASSERT(isr != NULL && isr->sp != NULL,
1253 ("ipsec_get_reqlevel: null argument"));
1254 KASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1255 ("ipsec_get_reqlevel: af family mismatch, src %u, dst %u",
1256 isr->sp->spidx.src.sa.sa_family,
1257 isr->sp->spidx.dst.sa.sa_family));
1259 /* XXX note that we have ipseclog() expanded here - code sync issue */
1260 #define IPSEC_CHECK_DEFAULT(lev) \
1261 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1262 && (lev) != IPSEC_LEVEL_UNIQUE) \
1264 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1265 (lev), IPSEC_LEVEL_REQUIRE) \
1267 (lev) = IPSEC_LEVEL_REQUIRE, \
1271 /* set default level */
1272 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1275 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1276 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1277 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1278 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1283 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1284 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1285 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1286 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1290 panic("key_get_reqlevel: unknown af %u",
1291 isr->sp->spidx.src.sa.sa_family);
1294 #undef IPSEC_CHECK_DEFAULT
1297 switch (isr->level) {
1298 case IPSEC_LEVEL_DEFAULT:
1299 switch (isr->saidx.proto) {
1301 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1302 level = esp_net_deflev;
1304 level = esp_trans_deflev;
1307 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1308 level = ah_net_deflev;
1310 level = ah_trans_deflev;
1311 case IPPROTO_IPCOMP:
1313 * we don't really care, as IPcomp document says that
1314 * we shouldn't compress small packets
1316 level = IPSEC_LEVEL_USE;
1319 panic("ipsec_get_reqlevel: "
1320 "Illegal protocol defined %u\n",
1325 case IPSEC_LEVEL_USE:
1326 case IPSEC_LEVEL_REQUIRE:
1329 case IPSEC_LEVEL_UNIQUE:
1330 level = IPSEC_LEVEL_REQUIRE;
1334 panic("ipsec_get_reqlevel: Illegal IPsec level %u\n",
1342 * Check security policy requirements against the actual
1343 * packet contents. Return one if the packet should be
1344 * reject as "invalid"; otherwiser return zero to have the
1345 * packet treated as "valid".
1352 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1354 struct ipsecrequest *isr;
1357 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1358 printf("ipsec_in_reject: using SP\n");
1359 kdebug_secpolicy(sp));
1362 switch (sp->policy) {
1363 case IPSEC_POLICY_DISCARD:
1365 case IPSEC_POLICY_BYPASS:
1366 case IPSEC_POLICY_NONE:
1370 KASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1371 ("ipsec_in_reject: invalid policy %u", sp->policy));
1373 /* XXX should compare policy against ipsec header history */
1376 for (isr = sp->req; isr != NULL; isr = isr->next) {
1377 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1379 switch (isr->saidx.proto) {
1381 if ((m->m_flags & M_DECRYPTED) == 0) {
1382 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1383 printf("ipsec_in_reject: ESP m_flags:%x\n",
1390 isr->sav->tdb_authalgxform != NULL &&
1391 (m->m_flags & M_AUTHIPDGM) == 0) {
1392 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1393 printf("ipsec_in_reject: ESP/AH m_flags:%x\n",
1400 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1401 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1402 printf("ipsec_in_reject: AH m_flags:%x\n",
1407 case IPPROTO_IPCOMP:
1409 * we don't really care, as IPcomp document
1410 * says that we shouldn't compress small
1411 * packets, IPComp policy should always be
1412 * treated as being in "use" level.
1417 return 0; /* valid */
1421 * Check AH/ESP integrity.
1422 * This function is called from tcp_input(), udp_input(),
1423 * and {ah,esp}4_input for tunnel mode
1426 ipsec4_in_reject(m, inp)
1430 struct secpolicy *sp;
1434 KASSERT(m != NULL, ("ipsec4_in_reject_so: null mbuf"));
1436 /* get SP for this packet.
1437 * When we are called from ip_forward(), we call
1438 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1441 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1443 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1446 result = ipsec_in_reject(sp, m);
1448 newipsecstat.ips_in_polvio++;
1451 result = 0; /* XXX should be panic ?
1452 * -> No, there may be error. */
1459 * Check AH/ESP integrity.
1460 * This function is called from tcp6_input(), udp6_input(),
1461 * and {ah,esp}6_input for tunnel mode
1464 ipsec6_in_reject(m, inp)
1468 struct secpolicy *sp = NULL;
1474 return 0; /* XXX should be panic ? */
1476 /* get SP for this packet.
1477 * When we are called from ip_forward(), we call
1478 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1481 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1483 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1486 result = ipsec_in_reject(sp, m);
1488 newipsecstat.ips_in_polvio++;
1498 * compute the byte size to be occupied by IPsec header.
1499 * in case it is tunneled, it includes the size of outer IP header.
1500 * NOTE: SP passed is free in this function.
1503 ipsec_hdrsiz(struct secpolicy *sp)
1505 struct ipsecrequest *isr;
1508 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1509 printf("ipsec_hdrsiz: using SP\n");
1510 kdebug_secpolicy(sp));
1512 switch (sp->policy) {
1513 case IPSEC_POLICY_DISCARD:
1514 case IPSEC_POLICY_BYPASS:
1515 case IPSEC_POLICY_NONE:
1519 KASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1520 ("ipsec_hdrsiz: invalid policy %u", sp->policy));
1523 for (isr = sp->req; isr != NULL; isr = isr->next) {
1526 switch (isr->saidx.proto) {
1528 clen = esp_hdrsiz(isr->sav);
1531 clen = ah_hdrsiz(isr->sav);
1533 case IPPROTO_IPCOMP:
1534 clen = sizeof(struct ipcomp);
1538 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1539 switch (isr->saidx.dst.sa.sa_family) {
1541 clen += sizeof(struct ip);
1545 clen += sizeof(struct ip6_hdr);
1549 ipseclog((LOG_ERR, "ipsec_hdrsiz: "
1550 "unknown AF %d in IPsec tunnel SA\n",
1551 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1561 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1563 ipsec4_hdrsiz(m, dir, inp)
1568 struct secpolicy *sp;
1572 KASSERT(m != NULL, ("ipsec4_hdrsiz: null mbuf"));
1573 KASSERT(inp == NULL || inp->inp_socket != NULL,
1574 ("ipsec4_hdrsize: socket w/o inpcb"));
1576 /* get SP for this packet.
1577 * When we are called from ip_forward(), we call
1578 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1581 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1583 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1586 size = ipsec_hdrsiz(sp);
1587 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1588 printf("ipsec4_hdrsiz: size:%lu.\n",
1589 (unsigned long)size));
1593 size = 0; /* XXX should be panic ? */
1599 /* This function is called from ipsec6_hdrsize_tcp(),
1600 * and maybe from ip6_forward.()
1603 ipsec6_hdrsiz(m, dir, in6p)
1606 struct in6pcb *in6p;
1608 struct secpolicy *sp;
1612 KASSERT(m != NULL, ("ipsec6_hdrsiz: null mbuf"));
1613 KASSERT(in6p == NULL || in6p->in6p_socket != NULL,
1614 ("ipsec6_hdrsize: socket w/o inpcb"));
1616 /* get SP for this packet */
1617 /* XXX Is it right to call with IP_FORWARDING. */
1619 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1621 sp = ipsec_getpolicybysock(m, dir, in6p, &error);
1625 size = ipsec_hdrsiz(sp);
1626 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1627 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size));
1635 * Check the variable replay window.
1636 * ipsec_chkreplay() performs replay check before ICV verification.
1637 * ipsec_updatereplay() updates replay bitmap. This must be called after
1638 * ICV verification (it also performs replay check, which is usually done
1640 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1642 * based on RFC 2401.
1645 ipsec_chkreplay(seq, sav)
1647 struct secasvar *sav;
1649 const struct secreplay *replay;
1652 u_int32_t wsizeb; /* constant: bits of window size */
1653 int frlast; /* constant: last frame */
1655 SPLASSERT(net, "ipsec_chkreplay");
1657 KASSERT(sav != NULL, ("ipsec_chkreplay: Null SA"));
1658 KASSERT(sav->replay != NULL, ("ipsec_chkreplay: Null replay state"));
1660 replay = sav->replay;
1662 if (replay->wsize == 0)
1663 return 1; /* no need to check replay. */
1666 frlast = replay->wsize - 1;
1667 wsizeb = replay->wsize << 3;
1669 /* sequence number of 0 is invalid */
1673 /* first time is always okay */
1674 if (replay->count == 0)
1677 if (seq > replay->lastseq) {
1678 /* larger sequences are okay */
1681 /* seq is equal or less than lastseq. */
1682 diff = replay->lastseq - seq;
1684 /* over range to check, i.e. too old or wrapped */
1688 fr = frlast - diff / 8;
1690 /* this packet already seen ? */
1691 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1694 /* out of order but good */
1700 * check replay counter whether to update or not.
1705 ipsec_updatereplay(seq, sav)
1707 struct secasvar *sav;
1709 struct secreplay *replay;
1712 u_int32_t wsizeb; /* constant: bits of window size */
1713 int frlast; /* constant: last frame */
1715 SPLASSERT(net, "ipsec_updatereplay");
1717 KASSERT(sav != NULL, ("ipsec_updatereplay: Null SA"));
1718 KASSERT(sav->replay != NULL, ("ipsec_updatereplay: Null replay state"));
1720 replay = sav->replay;
1722 if (replay->wsize == 0)
1723 goto ok; /* no need to check replay. */
1726 frlast = replay->wsize - 1;
1727 wsizeb = replay->wsize << 3;
1729 /* sequence number of 0 is invalid */
1734 if (replay->count == 0) {
1735 replay->lastseq = seq;
1736 bzero(replay->bitmap, replay->wsize);
1737 (replay->bitmap)[frlast] = 1;
1741 if (seq > replay->lastseq) {
1742 /* seq is larger than lastseq. */
1743 diff = seq - replay->lastseq;
1745 /* new larger sequence number */
1746 if (diff < wsizeb) {
1748 /* set bit for this packet */
1749 vshiftl(replay->bitmap, diff, replay->wsize);
1750 (replay->bitmap)[frlast] |= 1;
1752 /* this packet has a "way larger" */
1753 bzero(replay->bitmap, replay->wsize);
1754 (replay->bitmap)[frlast] = 1;
1756 replay->lastseq = seq;
1758 /* larger is good */
1760 /* seq is equal or less than lastseq. */
1761 diff = replay->lastseq - seq;
1763 /* over range to check, i.e. too old or wrapped */
1767 fr = frlast - diff / 8;
1769 /* this packet already seen ? */
1770 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1774 (replay->bitmap)[fr] |= (1 << (diff % 8));
1776 /* out of order but good */
1780 if (replay->count == ~0) {
1782 /* set overflow flag */
1785 /* don't increment, no more packets accepted */
1786 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1789 ipseclog((LOG_WARNING, "replay counter made %d cycle. %s\n",
1790 replay->overflow, ipsec_logsastr(sav)));
1799 * shift variable length bunffer to left.
1800 * IN: bitmap: pointer to the buffer
1801 * nbit: the number of to shift.
1802 * wsize: buffer size (bytes).
1805 vshiftl(bitmap, nbit, wsize)
1806 unsigned char *bitmap;
1812 for (j = 0; j < nbit; j += 8) {
1813 s = (nbit - j < 8) ? (nbit - j): 8;
1815 for (i = 1; i < wsize; i++) {
1816 over = (bitmap[i] >> (8 - s));
1818 bitmap[i-1] |= over;
1825 /* Return a printable string for the IPv4 address. */
1827 inet_ntoa4(struct in_addr ina)
1829 static char buf[4][4 * sizeof "123" + 4];
1830 unsigned char *ucp = (unsigned char *) &ina;
1834 sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff,
1835 ucp[2] & 0xff, ucp[3] & 0xff);
1839 /* Return a printable string for the address. */
1841 ipsec_address(union sockaddr_union* sa)
1843 switch (sa->sa.sa_family) {
1846 return inet_ntoa4(sa->sin.sin_addr);
1851 return ip6_sprintf(&sa->sin6.sin6_addr);
1855 return "(unknown address family)";
1861 struct secasvar *sav;
1863 static char buf[256];
1865 struct secasindex *saidx = &sav->sah->saidx;
1867 KASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
1868 ("ipsec_logsastr: address family mismatch"));
1871 snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
1874 /* NB: only use ipsec_address on one address at a time */
1875 snprintf(p, sizeof (buf) - (p - buf), "src=%s ",
1876 ipsec_address(&saidx->src));
1879 snprintf(p, sizeof (buf) - (p - buf), "dst=%s)",
1880 ipsec_address(&saidx->dst));
1896 p = mtod(m, u_char *);
1897 for (i = 0; i < m->m_len; i++) {
1898 printf("%02x ", p[i]);
1900 if (totlen % 16 == 0)
1905 if (totlen % 16 != 0)
1910 /* XXX this stuff doesn't belong here... */
1912 static struct xformsw* xforms = NULL;
1915 * Register a transform; typically at system startup.
1918 xform_register(struct xformsw* xsp)
1920 xsp->xf_next = xforms;
1925 * Initialize transform support in an sav.
1928 xform_init(struct secasvar *sav, int xftype)
1930 struct xformsw *xsp;
1932 for (xsp = xforms; xsp; xsp = xsp->xf_next)
1933 if (xsp->xf_type == xftype)
1934 return (*xsp->xf_init)(sav, xsp);