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.3 2003/08/07 21:17:37 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>
58 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/in_var.h>
65 #include <netinet/udp.h>
66 #include <netinet/udp_var.h>
67 #include <netinet/tcp.h>
68 #include <netinet/udp.h>
70 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
74 #include <netinet/in_pcb.h>
76 #include <netinet/icmp6.h>
85 #include "ipcomp.h" /*XXX*/
86 #include "ipcomp_var.h"
90 #include "key_debug.h"
94 #include <machine/in_cksum.h>
96 #include <net/net_osdep.h>
104 /* NB: name changed so netstat doesn't use it */
105 struct newipsecstat newipsecstat;
106 int ip4_ah_offsetmask = 0; /* maybe IP_DF? */
107 int ip4_ipsec_dfbit = 0; /* DF bit on encap. 0: clear 1: set 2: copy */
108 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
109 int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
110 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
111 int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
112 struct secpolicy ip4_def_policy;
113 int ip4_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
114 int ip4_esp_randpad = -1;
116 * Crypto support requirements:
118 * 1 require hardware support
119 * -1 require software support
122 int crypto_support = 0;
124 SYSCTL_DECL(_net_inet_ipsec);
127 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY,
128 def_policy, CTLFLAG_RW, &ip4_def_policy.policy, 0, "");
129 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
130 CTLFLAG_RW, &ip4_esp_trans_deflev, 0, "");
131 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
132 CTLFLAG_RW, &ip4_esp_net_deflev, 0, "");
133 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
134 CTLFLAG_RW, &ip4_ah_trans_deflev, 0, "");
135 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
136 CTLFLAG_RW, &ip4_ah_net_deflev, 0, "");
137 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS,
138 ah_cleartos, CTLFLAG_RW, &ah_cleartos, 0, "");
139 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK,
140 ah_offsetmask, CTLFLAG_RW, &ip4_ah_offsetmask, 0, "");
141 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT,
142 dfbit, CTLFLAG_RW, &ip4_ipsec_dfbit, 0, "");
143 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN,
144 ecn, CTLFLAG_RW, &ip4_ipsec_ecn, 0, "");
145 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG,
146 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
147 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ESP_RANDPAD,
148 esp_randpad, CTLFLAG_RW, &ip4_esp_randpad, 0, "");
149 SYSCTL_INT(_net_inet_ipsec, OID_AUTO,
150 crypto_support, CTLFLAG_RW, &crypto_support,0, "");
151 SYSCTL_STRUCT(_net_inet_ipsec, OID_AUTO,
152 ipsecstats, CTLFLAG_RD, &newipsecstat, newipsecstat, "");
155 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
156 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
157 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
158 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
159 int ip6_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
160 int ip6_esp_randpad = -1;
162 SYSCTL_DECL(_net_inet6_ipsec6);
164 /* net.inet6.ipsec6 */
166 SYSCTL_OID(_net_inet6_ipsec6, IPSECCTL_STATS, stats, CTLFLAG_RD,
167 0,0, compat_ipsecstats_sysctl, "S", "");
168 #endif /* COMPAT_KAME */
169 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY,
170 def_policy, CTLFLAG_RW, &ip4_def_policy.policy, 0, "");
171 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
172 CTLFLAG_RW, &ip6_esp_trans_deflev, 0, "");
173 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
174 CTLFLAG_RW, &ip6_esp_net_deflev, 0, "");
175 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
176 CTLFLAG_RW, &ip6_ah_trans_deflev, 0, "");
177 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
178 CTLFLAG_RW, &ip6_ah_net_deflev, 0, "");
179 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN,
180 ecn, CTLFLAG_RW, &ip6_ipsec_ecn, 0, "");
181 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG,
182 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
183 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ESP_RANDPAD,
184 esp_randpad, CTLFLAG_RW, &ip6_esp_randpad, 0, "");
187 static int ipsec4_setspidx_inpcb __P((struct mbuf *, struct inpcb *pcb));
189 static int ipsec6_setspidx_in6pcb __P((struct mbuf *, struct in6pcb *pcb));
191 static int ipsec_setspidx __P((struct mbuf *, struct secpolicyindex *, int));
192 static void ipsec4_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
193 static int ipsec4_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
195 static void ipsec6_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
196 static int ipsec6_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
198 static void ipsec_delpcbpolicy __P((struct inpcbpolicy *));
199 static struct secpolicy *ipsec_deepcopy_policy __P((struct secpolicy *src));
200 static int ipsec_set_policy __P((struct secpolicy **pcb_sp,
201 int optname, caddr_t request, size_t len, int priv));
202 static int ipsec_get_policy __P((struct secpolicy *pcb_sp, struct mbuf **mp));
203 static void vshiftl __P((unsigned char *, int, int));
204 static size_t ipsec_hdrsiz __P((struct secpolicy *));
207 * Return a held reference to the default SP.
209 static struct secpolicy *
210 key_allocsp_default(const char* where, int tag)
212 struct secpolicy *sp;
214 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
215 printf("DP key_allocsp_default from %s:%u\n", where, tag));
217 sp = &ip4_def_policy;
218 if (sp->policy != IPSEC_POLICY_DISCARD &&
219 sp->policy != IPSEC_POLICY_NONE) {
220 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
221 sp->policy, IPSEC_POLICY_NONE));
222 sp->policy = IPSEC_POLICY_NONE;
226 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
227 printf("DP key_allocsp_default returns SP:%p (%u)\n",
231 #define KEY_ALLOCSP_DEFAULT() \
232 key_allocsp_default(__FILE__, __LINE__)
235 * For OUTBOUND packet having a socket. Searching SPD for packet,
236 * and return a pointer to SP.
237 * OUT: NULL: no apropreate SP found, the following value is set to error.
239 * EACCES : discard packet.
240 * ENOENT : ipsec_acquire() in progress, maybe.
241 * others : error occured.
242 * others: a pointer to SP
244 * NOTE: IPv6 mapped adddress concern is implemented here.
247 ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
249 struct secpolicy *sp;
251 KASSERT(tdbi != NULL, ("ipsec_getpolicy: null tdbi"));
252 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
253 ("ipsec_getpolicy: invalid direction %u", dir));
255 sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
256 if (sp == NULL) /*XXX????*/
257 sp = KEY_ALLOCSP_DEFAULT();
258 KASSERT(sp != NULL, ("ipsec_getpolicy: null SP"));
263 * For OUTBOUND packet having a socket. Searching SPD for packet,
264 * and return a pointer to SP.
265 * OUT: NULL: no apropreate SP found, the following value is set to error.
267 * EACCES : discard packet.
268 * ENOENT : ipsec_acquire() in progress, maybe.
269 * others : error occured.
270 * others: a pointer to SP
272 * NOTE: IPv6 mapped adddress concern is implemented here.
275 ipsec_getpolicybysock(m, dir, inp, error)
281 struct inpcbpolicy *pcbsp = NULL;
282 struct secpolicy *currsp = NULL; /* policy on socket */
283 struct secpolicy *sp;
286 KASSERT(m != NULL, ("ipsec_getpolicybysock: null mbuf"));
287 KASSERT(inp != NULL, ("ipsec_getpolicybysock: null inpcb"));
288 KASSERT(error != NULL, ("ipsec_getpolicybysock: null error"));
289 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
290 ("ipsec_getpolicybysock: invalid direction %u", dir));
292 af = inp->inp_socket->so_proto->pr_domain->dom_family;
293 KASSERT(af == AF_INET || af == AF_INET6,
294 ("ipsec_getpolicybysock: unexpected protocol family %u", af));
298 /* set spidx in pcb */
299 *error = ipsec4_setspidx_inpcb(m, inp);
304 /* set spidx in pcb */
305 *error = ipsec6_setspidx_in6pcb(m, inp);
306 pcbsp = inp->in6p_sp;
310 *error = EPFNOSUPPORT;
316 KASSERT(pcbsp != NULL, ("ipsec_getpolicybysock: null pcbsp"));
318 case IPSEC_DIR_INBOUND:
319 currsp = pcbsp->sp_in;
321 case IPSEC_DIR_OUTBOUND:
322 currsp = pcbsp->sp_out;
325 KASSERT(currsp != NULL, ("ipsec_getpolicybysock: null currsp"));
327 if (pcbsp->priv) { /* when privilieged socket */
328 switch (currsp->policy) {
329 case IPSEC_POLICY_BYPASS:
330 case IPSEC_POLICY_IPSEC:
335 case IPSEC_POLICY_ENTRUST:
336 /* look for a policy in SPD */
337 sp = KEY_ALLOCSP(&currsp->spidx, dir);
338 if (sp == NULL) /* no SP found */
339 sp = KEY_ALLOCSP_DEFAULT();
343 ipseclog((LOG_ERR, "ipsec_getpolicybysock: "
344 "Invalid policy for PCB %d\n", currsp->policy));
348 } else { /* unpriv, SPD has policy */
349 sp = KEY_ALLOCSP(&currsp->spidx, dir);
350 if (sp == NULL) { /* no SP found */
351 switch (currsp->policy) {
352 case IPSEC_POLICY_BYPASS:
353 ipseclog((LOG_ERR, "ipsec_getpolicybysock: "
354 "Illegal policy for non-priviliged defined %d\n",
359 case IPSEC_POLICY_ENTRUST:
360 sp = KEY_ALLOCSP_DEFAULT();
363 case IPSEC_POLICY_IPSEC:
369 ipseclog((LOG_ERR, "ipsec_getpolicybysock: "
370 "Invalid policy for PCB %d\n", currsp->policy));
377 ("ipsec_getpolicybysock: null SP (priv %u policy %u",
378 pcbsp->priv, currsp->policy));
379 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
380 printf("DP ipsec_getpolicybysock (priv %u policy %u) allocates "
381 "SP:%p (refcnt %u)\n", pcbsp->priv, currsp->policy,
387 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
388 * and return a pointer to SP.
389 * OUT: positive: a pointer to the entry for security policy leaf matched.
390 * NULL: no apropreate SP found, the following value is set to error.
392 * EACCES : discard packet.
393 * ENOENT : ipsec_acquire() in progress, maybe.
394 * others : error occured.
397 ipsec_getpolicybyaddr(m, dir, flag, error)
403 struct secpolicyindex spidx;
404 struct secpolicy *sp;
406 KASSERT(m != NULL, ("ipsec_getpolicybyaddr: null mbuf"));
407 KASSERT(error != NULL, ("ipsec_getpolicybyaddr: null error"));
408 KASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
409 ("ipsec4_getpolicybaddr: invalid direction %u", dir));
412 if (key_havesp(dir)) {
413 /* Make an index to look for a policy. */
414 *error = ipsec_setspidx(m, &spidx,
415 (flag & IP_FORWARDING) ? 0 : 1);
417 DPRINTF(("ipsec_getpolicybyaddr: setpidx failed,"
418 " dir %u flag %u\n", dir, flag));
419 bzero(&spidx, sizeof (spidx));
424 sp = KEY_ALLOCSP(&spidx, dir);
426 if (sp == NULL) /* no SP found, use system default */
427 sp = KEY_ALLOCSP_DEFAULT();
428 KASSERT(sp != NULL, ("ipsec_getpolicybyaddr: null SP"));
433 ipsec4_checkpolicy(m, dir, flag, error, inp)
439 struct secpolicy *sp;
443 sp = ipsec_getpolicybyaddr(m, dir, flag, error);
445 sp = ipsec_getpolicybysock(m, dir, inp, error);
448 ("ipsec4_checkpolicy: getpolicy failed w/o error"));
449 newipsecstat.ips_out_inval++;
453 ("ipsec4_checkpolicy: sp w/ error set to %u", *error));
454 switch (sp->policy) {
455 case IPSEC_POLICY_ENTRUST:
457 printf("ipsec4_checkpolicy: invalid policy %u\n", sp->policy);
459 case IPSEC_POLICY_DISCARD:
460 newipsecstat.ips_out_polvio++;
461 *error = -EINVAL; /* packet is discarded by caller */
463 case IPSEC_POLICY_BYPASS:
464 case IPSEC_POLICY_NONE:
466 sp = NULL; /* NB: force NULL result */
468 case IPSEC_POLICY_IPSEC:
469 if (sp->req == NULL) /* acquire an SA */
470 *error = key_spdacquire(sp);
481 ipsec4_setspidx_inpcb(m, pcb)
487 KASSERT(pcb != NULL, ("ipsec4_setspidx_inpcb: null pcb"));
488 KASSERT(pcb->inp_sp != NULL, ("ipsec4_setspidx_inpcb: null inp_sp"));
489 KASSERT(pcb->inp_sp->sp_out != NULL && pcb->inp_sp->sp_in != NULL,
490 ("ipsec4_setspidx_inpcb: null sp_in || sp_out"));
492 error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
494 pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
495 pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
496 pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
498 bzero(&pcb->inp_sp->sp_in->spidx,
499 sizeof (pcb->inp_sp->sp_in->spidx));
500 bzero(&pcb->inp_sp->sp_out->spidx,
501 sizeof (pcb->inp_sp->sp_in->spidx));
508 ipsec6_setspidx_in6pcb(m, pcb)
512 struct secpolicyindex *spidx;
515 KASSERT(pcb != NULL, ("ipsec6_setspidx_in6pcb: null pcb"));
516 KASSERT(pcb->in6p_sp != NULL, ("ipsec6_setspidx_in6pcb: null inp_sp"));
517 KASSERT(pcb->in6p_sp->sp_out != NULL && pcb->in6p_sp->sp_in != NULL,
518 ("ipsec6_setspidx_in6pcb: null sp_in || sp_out"));
520 bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
521 bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
523 spidx = &pcb->in6p_sp->sp_in->spidx;
524 error = ipsec_setspidx(m, spidx, 1);
527 spidx->dir = IPSEC_DIR_INBOUND;
529 spidx = &pcb->in6p_sp->sp_out->spidx;
530 error = ipsec_setspidx(m, spidx, 1);
533 spidx->dir = IPSEC_DIR_OUTBOUND;
538 bzero(&pcb->in6p_sp->sp_in->spidx, sizeof(*spidx));
539 bzero(&pcb->in6p_sp->sp_out->spidx, sizeof(*spidx));
545 * configure security policy index (src/dst/proto/sport/dport)
546 * by looking at the content of mbuf.
547 * the caller is responsible for error recovery (like clearing up spidx).
550 ipsec_setspidx(m, spidx, needport)
552 struct secpolicyindex *spidx;
555 struct ip *ip = NULL;
562 KASSERT(m != NULL, ("ipsec_setspidx: null mbuf"));
565 * validate m->m_pkthdr.len. we see incorrect length if we
566 * mistakenly call this function with inconsistent mbuf chain
567 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
570 for (n = m; n; n = n->m_next)
572 if (m->m_pkthdr.len != len) {
573 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
574 printf("ipsec_setspidx: "
575 "total of m_len(%d) != pkthdr.len(%d), "
577 len, m->m_pkthdr.len));
581 if (m->m_pkthdr.len < sizeof(struct ip)) {
582 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
583 printf("ipsec_setspidx: "
584 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
589 if (m->m_len >= sizeof(*ip))
590 ip = mtod(m, struct ip *);
592 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
596 v = _IP_VHL_V(ip->ip_vhl);
602 error = ipsec4_setspidx_ipaddr(m, spidx);
605 ipsec4_get_ulp(m, spidx, needport);
609 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
610 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
611 printf("ipsec_setspidx: "
612 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
613 "ignored.\n", m->m_pkthdr.len));
616 error = ipsec6_setspidx_ipaddr(m, spidx);
619 ipsec6_get_ulp(m, spidx, needport);
623 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
624 printf("ipsec_setspidx: "
625 "unknown IP version %u, ignored.\n", v));
631 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
637 KASSERT(m != NULL, ("ipsec4_get_ulp: null mbuf"));
638 KASSERT(m->m_pkthdr.len >= sizeof(struct ip),
639 ("ipsec4_get_ulp: packet too short"));
641 /* NB: ip_input() flips it into host endian XXX need more checking */
642 if (m->m_len < sizeof (struct ip)) {
643 struct ip *ip = mtod(m, struct ip *);
644 if (ip->ip_off & (IP_MF | IP_OFFMASK))
647 off = _IP_VHL_HL(ip->ip_vhl) << 2;
649 off = ip->ip_hl << 2;
655 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
656 if (ih.ip_off & (IP_MF | IP_OFFMASK))
659 off = _IP_VHL_HL(ih.ip_vhl) << 2;
666 while (off < m->m_pkthdr.len) {
673 spidx->ul_proto = nxt;
676 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
678 m_copydata(m, off, sizeof (th), (caddr_t) &th);
679 spidx->src.sin.sin_port = th.th_sport;
680 spidx->dst.sin.sin_port = th.th_dport;
683 spidx->ul_proto = nxt;
686 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
688 m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
689 spidx->src.sin.sin_port = uh.uh_sport;
690 spidx->dst.sin.sin_port = uh.uh_dport;
693 if (m->m_pkthdr.len > off + sizeof(ip6e))
695 /* XXX sigh, this works but is totally bogus */
696 m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
697 off += (ip6e.ip6e_len + 2) << 2;
702 /* XXX intermediate headers??? */
703 spidx->ul_proto = nxt;
708 spidx->ul_proto = IPSEC_ULPROTO_ANY;
710 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
711 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
714 /* assumes that m is sane */
716 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
718 static const struct sockaddr_in template = {
719 sizeof (struct sockaddr_in),
721 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
724 spidx->src.sin = template;
725 spidx->dst.sin = template;
727 if (m->m_len < sizeof (struct ip)) {
728 m_copydata(m, offsetof(struct ip, ip_src),
729 sizeof (struct in_addr),
730 (caddr_t) &spidx->src.sin.sin_addr);
731 m_copydata(m, offsetof(struct ip, ip_dst),
732 sizeof (struct in_addr),
733 (caddr_t) &spidx->dst.sin.sin_addr);
735 struct ip *ip = mtod(m, struct ip *);
736 spidx->src.sin.sin_addr = ip->ip_src;
737 spidx->dst.sin.sin_addr = ip->ip_dst;
740 spidx->prefs = sizeof(struct in_addr) << 3;
741 spidx->prefd = sizeof(struct in_addr) << 3;
748 ipsec6_get_ulp(m, spidx, needport)
750 struct secpolicyindex *spidx;
759 panic("ipsec6_get_ulp: NULL pointer was passed.\n");
761 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
762 printf("ipsec6_get_ulp:\n"); kdebug_mbuf(m));
765 spidx->ul_proto = IPSEC_ULPROTO_ANY;
766 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
767 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
770 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
771 if (off < 0 || m->m_pkthdr.len < off)
776 spidx->ul_proto = nxt;
779 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
781 m_copydata(m, off, sizeof(th), (caddr_t)&th);
782 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
783 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
786 spidx->ul_proto = nxt;
789 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
791 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
792 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
793 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
797 /* XXX intermediate headers??? */
798 spidx->ul_proto = nxt;
803 /* assumes that m is sane */
805 ipsec6_setspidx_ipaddr(m, spidx)
807 struct secpolicyindex *spidx;
809 struct ip6_hdr *ip6 = NULL;
810 struct ip6_hdr ip6buf;
811 struct sockaddr_in6 *sin6;
813 if (m->m_len >= sizeof(*ip6))
814 ip6 = mtod(m, struct ip6_hdr *);
816 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
820 sin6 = (struct sockaddr_in6 *)&spidx->src;
821 bzero(sin6, sizeof(*sin6));
822 sin6->sin6_family = AF_INET6;
823 sin6->sin6_len = sizeof(struct sockaddr_in6);
824 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
825 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
826 sin6->sin6_addr.s6_addr16[1] = 0;
827 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
829 spidx->prefs = sizeof(struct in6_addr) << 3;
831 sin6 = (struct sockaddr_in6 *)&spidx->dst;
832 bzero(sin6, sizeof(*sin6));
833 sin6->sin6_family = AF_INET6;
834 sin6->sin6_len = sizeof(struct sockaddr_in6);
835 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
836 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
837 sin6->sin6_addr.s6_addr16[1] = 0;
838 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
840 spidx->prefd = sizeof(struct in6_addr) << 3;
847 ipsec_delpcbpolicy(p)
848 struct inpcbpolicy *p;
853 /* initialize policy in PCB */
855 ipsec_init_policy(so, pcb_sp)
857 struct inpcbpolicy **pcb_sp;
859 struct inpcbpolicy *new;
862 if (so == NULL || pcb_sp == NULL)
863 panic("ipsec_init_policy: NULL pointer was passed.\n");
865 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
866 M_SECA, M_NOWAIT|M_ZERO);
868 ipseclog((LOG_DEBUG, "ipsec_init_policy: No more memory.\n"));
872 if (so->so_cred != 0 && so->so_cred->cr_uid == 0)
877 if ((new->sp_in = KEY_NEWSP()) == NULL) {
878 ipsec_delpcbpolicy(new);
881 new->sp_in->state = IPSEC_SPSTATE_ALIVE;
882 new->sp_in->policy = IPSEC_POLICY_ENTRUST;
884 if ((new->sp_out = KEY_NEWSP()) == NULL) {
885 KEY_FREESP(&new->sp_in);
886 ipsec_delpcbpolicy(new);
889 new->sp_out->state = IPSEC_SPSTATE_ALIVE;
890 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
897 /* copy old ipsec policy into new */
899 ipsec_copy_policy(old, new)
900 struct inpcbpolicy *old, *new;
902 struct secpolicy *sp;
904 sp = ipsec_deepcopy_policy(old->sp_in);
906 KEY_FREESP(&new->sp_in);
911 sp = ipsec_deepcopy_policy(old->sp_out);
913 KEY_FREESP(&new->sp_out);
918 new->priv = old->priv;
923 /* deep-copy a policy in PCB */
924 static struct secpolicy *
925 ipsec_deepcopy_policy(src)
926 struct secpolicy *src;
928 struct ipsecrequest *newchain = NULL;
929 struct ipsecrequest *p;
930 struct ipsecrequest **q;
931 struct ipsecrequest *r;
932 struct secpolicy *dst;
941 * deep-copy IPsec request chain. This is required since struct
942 * ipsecrequest is not reference counted.
945 for (p = src->req; p; p = p->next) {
946 *q = (struct ipsecrequest *)malloc(sizeof(struct ipsecrequest),
950 bzero(*q, sizeof(**q));
953 (*q)->saidx.proto = p->saidx.proto;
954 (*q)->saidx.mode = p->saidx.mode;
955 (*q)->level = p->level;
956 (*q)->saidx.reqid = p->saidx.reqid;
958 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
959 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
968 dst->state = src->state;
969 dst->policy = src->policy;
970 /* do not touch the refcnt fields */
975 for (p = newchain; p; p = r) {
983 /* set policy and ipsec request if present. */
985 ipsec_set_policy(pcb_sp, optname, request, len, priv)
986 struct secpolicy **pcb_sp;
992 struct sadb_x_policy *xpl;
993 struct secpolicy *newsp = NULL;
997 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
999 if (len < sizeof(*xpl))
1001 xpl = (struct sadb_x_policy *)request;
1003 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1004 printf("ipsec_set_policy: passed policy\n");
1005 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
1007 /* check policy type */
1008 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1009 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
1010 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1013 /* check privileged socket */
1014 if (priv == 0 && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS)
1017 /* allocation new SP entry */
1018 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1021 newsp->state = IPSEC_SPSTATE_ALIVE;
1023 /* clear old SP and set new SP */
1026 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1027 printf("ipsec_set_policy: new policy\n");
1028 kdebug_secpolicy(newsp));
1034 ipsec_get_policy(pcb_sp, mp)
1035 struct secpolicy *pcb_sp;
1040 if (pcb_sp == NULL || mp == NULL)
1043 *mp = key_sp2msg(pcb_sp);
1045 ipseclog((LOG_DEBUG, "ipsec_get_policy: No more memory.\n"));
1049 (*mp)->m_type = MT_DATA;
1050 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1051 printf("ipsec_get_policy:\n");
1058 ipsec4_set_policy(inp, optname, request, len, priv)
1065 struct sadb_x_policy *xpl;
1066 struct secpolicy **pcb_sp;
1069 if (inp == NULL || request == NULL)
1071 if (len < sizeof(*xpl))
1073 xpl = (struct sadb_x_policy *)request;
1075 /* select direction */
1076 switch (xpl->sadb_x_policy_dir) {
1077 case IPSEC_DIR_INBOUND:
1078 pcb_sp = &inp->inp_sp->sp_in;
1080 case IPSEC_DIR_OUTBOUND:
1081 pcb_sp = &inp->inp_sp->sp_out;
1084 ipseclog((LOG_ERR, "ipsec4_set_policy: invalid direction=%u\n",
1085 xpl->sadb_x_policy_dir));
1089 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1093 ipsec4_get_policy(inp, request, len, mp)
1099 struct sadb_x_policy *xpl;
1100 struct secpolicy *pcb_sp;
1103 if (inp == NULL || request == NULL || mp == NULL)
1105 KASSERT(inp->inp_sp != NULL, ("ipsec4_get_policy: null inp_sp"));
1106 if (len < sizeof(*xpl))
1108 xpl = (struct sadb_x_policy *)request;
1110 /* select direction */
1111 switch (xpl->sadb_x_policy_dir) {
1112 case IPSEC_DIR_INBOUND:
1113 pcb_sp = inp->inp_sp->sp_in;
1115 case IPSEC_DIR_OUTBOUND:
1116 pcb_sp = inp->inp_sp->sp_out;
1119 ipseclog((LOG_ERR, "ipsec4_set_policy: invalid direction=%u\n",
1120 xpl->sadb_x_policy_dir));
1124 return ipsec_get_policy(pcb_sp, mp);
1127 /* delete policy in PCB */
1129 ipsec4_delete_pcbpolicy(inp)
1132 KASSERT(inp != NULL, ("ipsec4_delete_pcbpolicy: null inp"));
1134 if (inp->inp_sp == NULL)
1137 if (inp->inp_sp->sp_in != NULL)
1138 KEY_FREESP(&inp->inp_sp->sp_in);
1140 if (inp->inp_sp->sp_out != NULL)
1141 KEY_FREESP(&inp->inp_sp->sp_out);
1143 ipsec_delpcbpolicy(inp->inp_sp);
1151 ipsec6_set_policy(in6p, optname, request, len, priv)
1152 struct in6pcb *in6p;
1158 struct sadb_x_policy *xpl;
1159 struct secpolicy **pcb_sp;
1162 if (in6p == NULL || request == NULL)
1164 if (len < sizeof(*xpl))
1166 xpl = (struct sadb_x_policy *)request;
1168 /* select direction */
1169 switch (xpl->sadb_x_policy_dir) {
1170 case IPSEC_DIR_INBOUND:
1171 pcb_sp = &in6p->in6p_sp->sp_in;
1173 case IPSEC_DIR_OUTBOUND:
1174 pcb_sp = &in6p->in6p_sp->sp_out;
1177 ipseclog((LOG_ERR, "ipsec6_set_policy: invalid direction=%u\n",
1178 xpl->sadb_x_policy_dir));
1182 return ipsec_set_policy(pcb_sp, optname, request, len, priv);
1186 ipsec6_get_policy(in6p, request, len, mp)
1187 struct in6pcb *in6p;
1192 struct sadb_x_policy *xpl;
1193 struct secpolicy *pcb_sp;
1196 if (in6p == NULL || request == NULL || mp == NULL)
1198 KASSERT(in6p->in6p_sp != NULL, ("ipsec6_get_policy: null in6p_sp"));
1199 if (len < sizeof(*xpl))
1201 xpl = (struct sadb_x_policy *)request;
1203 /* select direction */
1204 switch (xpl->sadb_x_policy_dir) {
1205 case IPSEC_DIR_INBOUND:
1206 pcb_sp = in6p->in6p_sp->sp_in;
1208 case IPSEC_DIR_OUTBOUND:
1209 pcb_sp = in6p->in6p_sp->sp_out;
1212 ipseclog((LOG_ERR, "ipsec6_set_policy: invalid direction=%u\n",
1213 xpl->sadb_x_policy_dir));
1217 return ipsec_get_policy(pcb_sp, mp);
1221 ipsec6_delete_pcbpolicy(in6p)
1222 struct in6pcb *in6p;
1224 KASSERT(in6p != NULL, ("ipsec6_delete_pcbpolicy: null in6p"));
1226 if (in6p->in6p_sp == NULL)
1229 if (in6p->in6p_sp->sp_in != NULL)
1230 KEY_FREESP(&in6p->in6p_sp->sp_in);
1232 if (in6p->in6p_sp->sp_out != NULL)
1233 KEY_FREESP(&in6p->in6p_sp->sp_out);
1235 ipsec_delpcbpolicy(in6p->in6p_sp);
1236 in6p->in6p_sp = NULL;
1243 * return current level.
1244 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1247 ipsec_get_reqlevel(isr)
1248 struct ipsecrequest *isr;
1251 u_int esp_trans_deflev, esp_net_deflev;
1252 u_int ah_trans_deflev, ah_net_deflev;
1254 KASSERT(isr != NULL && isr->sp != NULL,
1255 ("ipsec_get_reqlevel: null argument"));
1256 KASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1257 ("ipsec_get_reqlevel: af family mismatch, src %u, dst %u",
1258 isr->sp->spidx.src.sa.sa_family,
1259 isr->sp->spidx.dst.sa.sa_family));
1261 /* XXX note that we have ipseclog() expanded here - code sync issue */
1262 #define IPSEC_CHECK_DEFAULT(lev) \
1263 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1264 && (lev) != IPSEC_LEVEL_UNIQUE) \
1266 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1267 (lev), IPSEC_LEVEL_REQUIRE) \
1269 (lev) = IPSEC_LEVEL_REQUIRE, \
1273 /* set default level */
1274 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1277 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1278 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1279 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1280 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1285 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1286 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1287 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1288 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1292 panic("key_get_reqlevel: unknown af %u",
1293 isr->sp->spidx.src.sa.sa_family);
1296 #undef IPSEC_CHECK_DEFAULT
1299 switch (isr->level) {
1300 case IPSEC_LEVEL_DEFAULT:
1301 switch (isr->saidx.proto) {
1303 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1304 level = esp_net_deflev;
1306 level = esp_trans_deflev;
1309 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1310 level = ah_net_deflev;
1312 level = ah_trans_deflev;
1313 case IPPROTO_IPCOMP:
1315 * we don't really care, as IPcomp document says that
1316 * we shouldn't compress small packets
1318 level = IPSEC_LEVEL_USE;
1321 panic("ipsec_get_reqlevel: "
1322 "Illegal protocol defined %u\n",
1327 case IPSEC_LEVEL_USE:
1328 case IPSEC_LEVEL_REQUIRE:
1331 case IPSEC_LEVEL_UNIQUE:
1332 level = IPSEC_LEVEL_REQUIRE;
1336 panic("ipsec_get_reqlevel: Illegal IPsec level %u\n",
1344 * Check security policy requirements against the actual
1345 * packet contents. Return one if the packet should be
1346 * reject as "invalid"; otherwiser return zero to have the
1347 * packet treated as "valid".
1354 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1356 struct ipsecrequest *isr;
1359 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1360 printf("ipsec_in_reject: using SP\n");
1361 kdebug_secpolicy(sp));
1364 switch (sp->policy) {
1365 case IPSEC_POLICY_DISCARD:
1367 case IPSEC_POLICY_BYPASS:
1368 case IPSEC_POLICY_NONE:
1372 KASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1373 ("ipsec_in_reject: invalid policy %u", sp->policy));
1375 /* XXX should compare policy against ipsec header history */
1378 for (isr = sp->req; isr != NULL; isr = isr->next) {
1379 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1381 switch (isr->saidx.proto) {
1383 if ((m->m_flags & M_DECRYPTED) == 0) {
1384 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1385 printf("ipsec_in_reject: ESP m_flags:%x\n",
1392 isr->sav->tdb_authalgxform != NULL &&
1393 (m->m_flags & M_AUTHIPDGM) == 0) {
1394 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1395 printf("ipsec_in_reject: ESP/AH m_flags:%x\n",
1402 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1403 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1404 printf("ipsec_in_reject: AH m_flags:%x\n",
1409 case IPPROTO_IPCOMP:
1411 * we don't really care, as IPcomp document
1412 * says that we shouldn't compress small
1413 * packets, IPComp policy should always be
1414 * treated as being in "use" level.
1419 return 0; /* valid */
1423 * Check AH/ESP integrity.
1424 * This function is called from tcp_input(), udp_input(),
1425 * and {ah,esp}4_input for tunnel mode
1428 ipsec4_in_reject(m, inp)
1432 struct secpolicy *sp;
1436 KASSERT(m != NULL, ("ipsec4_in_reject_so: null mbuf"));
1438 /* get SP for this packet.
1439 * When we are called from ip_forward(), we call
1440 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1443 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1445 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1448 result = ipsec_in_reject(sp, m);
1450 newipsecstat.ips_in_polvio++;
1453 result = 0; /* XXX should be panic ?
1454 * -> No, there may be error. */
1461 * Check AH/ESP integrity.
1462 * This function is called from tcp6_input(), udp6_input(),
1463 * and {ah,esp}6_input for tunnel mode
1466 ipsec6_in_reject(m, inp)
1470 struct secpolicy *sp = NULL;
1476 return 0; /* XXX should be panic ? */
1478 /* get SP for this packet.
1479 * When we are called from ip_forward(), we call
1480 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1483 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1485 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1488 result = ipsec_in_reject(sp, m);
1490 newipsecstat.ips_in_polvio++;
1500 * compute the byte size to be occupied by IPsec header.
1501 * in case it is tunneled, it includes the size of outer IP header.
1502 * NOTE: SP passed is free in this function.
1505 ipsec_hdrsiz(struct secpolicy *sp)
1507 struct ipsecrequest *isr;
1510 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1511 printf("ipsec_hdrsiz: using SP\n");
1512 kdebug_secpolicy(sp));
1514 switch (sp->policy) {
1515 case IPSEC_POLICY_DISCARD:
1516 case IPSEC_POLICY_BYPASS:
1517 case IPSEC_POLICY_NONE:
1521 KASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1522 ("ipsec_hdrsiz: invalid policy %u", sp->policy));
1525 for (isr = sp->req; isr != NULL; isr = isr->next) {
1528 switch (isr->saidx.proto) {
1530 clen = esp_hdrsiz(isr->sav);
1533 clen = ah_hdrsiz(isr->sav);
1535 case IPPROTO_IPCOMP:
1536 clen = sizeof(struct ipcomp);
1540 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1541 switch (isr->saidx.dst.sa.sa_family) {
1543 clen += sizeof(struct ip);
1547 clen += sizeof(struct ip6_hdr);
1551 ipseclog((LOG_ERR, "ipsec_hdrsiz: "
1552 "unknown AF %d in IPsec tunnel SA\n",
1553 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1563 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1565 ipsec4_hdrsiz(m, dir, inp)
1570 struct secpolicy *sp;
1574 KASSERT(m != NULL, ("ipsec4_hdrsiz: null mbuf"));
1575 KASSERT(inp == NULL || inp->inp_socket != NULL,
1576 ("ipsec4_hdrsize: socket w/o inpcb"));
1578 /* get SP for this packet.
1579 * When we are called from ip_forward(), we call
1580 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1583 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1585 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1588 size = ipsec_hdrsiz(sp);
1589 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1590 printf("ipsec4_hdrsiz: size:%lu.\n",
1591 (unsigned long)size));
1595 size = 0; /* XXX should be panic ? */
1601 /* This function is called from ipsec6_hdrsize_tcp(),
1602 * and maybe from ip6_forward.()
1605 ipsec6_hdrsiz(m, dir, in6p)
1608 struct in6pcb *in6p;
1610 struct secpolicy *sp;
1614 KASSERT(m != NULL, ("ipsec6_hdrsiz: null mbuf"));
1615 KASSERT(in6p == NULL || in6p->in6p_socket != NULL,
1616 ("ipsec6_hdrsize: socket w/o inpcb"));
1618 /* get SP for this packet */
1619 /* XXX Is it right to call with IP_FORWARDING. */
1621 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1623 sp = ipsec_getpolicybysock(m, dir, in6p, &error);
1627 size = ipsec_hdrsiz(sp);
1628 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1629 printf("ipsec6_hdrsiz: size:%lu.\n", (unsigned long)size));
1637 * Check the variable replay window.
1638 * ipsec_chkreplay() performs replay check before ICV verification.
1639 * ipsec_updatereplay() updates replay bitmap. This must be called after
1640 * ICV verification (it also performs replay check, which is usually done
1642 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1644 * based on RFC 2401.
1647 ipsec_chkreplay(seq, sav)
1649 struct secasvar *sav;
1651 const struct secreplay *replay;
1654 u_int32_t wsizeb; /* constant: bits of window size */
1655 int frlast; /* constant: last frame */
1657 SPLASSERT(net, "ipsec_chkreplay");
1659 KASSERT(sav != NULL, ("ipsec_chkreplay: Null SA"));
1660 KASSERT(sav->replay != NULL, ("ipsec_chkreplay: Null replay state"));
1662 replay = sav->replay;
1664 if (replay->wsize == 0)
1665 return 1; /* no need to check replay. */
1668 frlast = replay->wsize - 1;
1669 wsizeb = replay->wsize << 3;
1671 /* sequence number of 0 is invalid */
1675 /* first time is always okay */
1676 if (replay->count == 0)
1679 if (seq > replay->lastseq) {
1680 /* larger sequences are okay */
1683 /* seq is equal or less than lastseq. */
1684 diff = replay->lastseq - seq;
1686 /* over range to check, i.e. too old or wrapped */
1690 fr = frlast - diff / 8;
1692 /* this packet already seen ? */
1693 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1696 /* out of order but good */
1702 * check replay counter whether to update or not.
1707 ipsec_updatereplay(seq, sav)
1709 struct secasvar *sav;
1711 struct secreplay *replay;
1714 u_int32_t wsizeb; /* constant: bits of window size */
1715 int frlast; /* constant: last frame */
1717 SPLASSERT(net, "ipsec_updatereplay");
1719 KASSERT(sav != NULL, ("ipsec_updatereplay: Null SA"));
1720 KASSERT(sav->replay != NULL, ("ipsec_updatereplay: Null replay state"));
1722 replay = sav->replay;
1724 if (replay->wsize == 0)
1725 goto ok; /* no need to check replay. */
1728 frlast = replay->wsize - 1;
1729 wsizeb = replay->wsize << 3;
1731 /* sequence number of 0 is invalid */
1736 if (replay->count == 0) {
1737 replay->lastseq = seq;
1738 bzero(replay->bitmap, replay->wsize);
1739 (replay->bitmap)[frlast] = 1;
1743 if (seq > replay->lastseq) {
1744 /* seq is larger than lastseq. */
1745 diff = seq - replay->lastseq;
1747 /* new larger sequence number */
1748 if (diff < wsizeb) {
1750 /* set bit for this packet */
1751 vshiftl(replay->bitmap, diff, replay->wsize);
1752 (replay->bitmap)[frlast] |= 1;
1754 /* this packet has a "way larger" */
1755 bzero(replay->bitmap, replay->wsize);
1756 (replay->bitmap)[frlast] = 1;
1758 replay->lastseq = seq;
1760 /* larger is good */
1762 /* seq is equal or less than lastseq. */
1763 diff = replay->lastseq - seq;
1765 /* over range to check, i.e. too old or wrapped */
1769 fr = frlast - diff / 8;
1771 /* this packet already seen ? */
1772 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1776 (replay->bitmap)[fr] |= (1 << (diff % 8));
1778 /* out of order but good */
1782 if (replay->count == ~0) {
1784 /* set overflow flag */
1787 /* don't increment, no more packets accepted */
1788 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1791 ipseclog((LOG_WARNING, "replay counter made %d cycle. %s\n",
1792 replay->overflow, ipsec_logsastr(sav)));
1801 * shift variable length bunffer to left.
1802 * IN: bitmap: pointer to the buffer
1803 * nbit: the number of to shift.
1804 * wsize: buffer size (bytes).
1807 vshiftl(bitmap, nbit, wsize)
1808 unsigned char *bitmap;
1814 for (j = 0; j < nbit; j += 8) {
1815 s = (nbit - j < 8) ? (nbit - j): 8;
1817 for (i = 1; i < wsize; i++) {
1818 over = (bitmap[i] >> (8 - s));
1820 bitmap[i-1] |= over;
1827 /* Return a printable string for the IPv4 address. */
1829 inet_ntoa4(struct in_addr ina)
1831 static char buf[4][4 * sizeof "123" + 4];
1832 unsigned char *ucp = (unsigned char *) &ina;
1836 sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff,
1837 ucp[2] & 0xff, ucp[3] & 0xff);
1841 /* Return a printable string for the address. */
1843 ipsec_address(union sockaddr_union* sa)
1845 switch (sa->sa.sa_family) {
1848 return inet_ntoa4(sa->sin.sin_addr);
1853 return ip6_sprintf(&sa->sin6.sin6_addr);
1857 return "(unknown address family)";
1863 struct secasvar *sav;
1865 static char buf[256];
1867 struct secasindex *saidx = &sav->sah->saidx;
1869 KASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
1870 ("ipsec_logsastr: address family mismatch"));
1873 snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
1876 /* NB: only use ipsec_address on one address at a time */
1877 snprintf(p, sizeof (buf) - (p - buf), "src=%s ",
1878 ipsec_address(&saidx->src));
1881 snprintf(p, sizeof (buf) - (p - buf), "dst=%s)",
1882 ipsec_address(&saidx->dst));
1898 p = mtod(m, u_char *);
1899 for (i = 0; i < m->m_len; i++) {
1900 printf("%02x ", p[i]);
1902 if (totlen % 16 == 0)
1907 if (totlen % 16 != 0)
1912 /* XXX this stuff doesn't belong here... */
1914 static struct xformsw* xforms = NULL;
1917 * Register a transform; typically at system startup.
1920 xform_register(struct xformsw* xsp)
1922 xsp->xf_next = xforms;
1927 * Initialize transform support in an sav.
1930 xform_init(struct secasvar *sav, int xftype)
1932 struct xformsw *xsp;
1934 for (xsp = xforms; xsp; xsp = xsp->xf_next)
1935 if (xsp->xf_type == xftype)
1936 return (*xsp->xf_init)(sav, xsp);