2 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * IPsec controller part.
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/errno.h>
51 #include <sys/hhook.h>
53 #include <sys/kernel.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
59 #include <net/if_enc.h>
60 #include <net/if_var.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/in_var.h>
68 #include <netinet/udp.h>
69 #include <netinet/udp_var.h>
70 #include <netinet/tcp.h>
71 #include <netinet/udp.h>
73 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
77 #include <netinet/in_pcb.h>
79 #include <netinet/icmp6.h>
82 #include <sys/types.h>
83 #include <netipsec/ipsec.h>
85 #include <netipsec/ipsec6.h>
87 #include <netipsec/ah_var.h>
88 #include <netipsec/esp_var.h>
89 #include <netipsec/ipcomp.h> /*XXX*/
90 #include <netipsec/ipcomp_var.h>
92 #include <netipsec/key.h>
93 #include <netipsec/keydb.h>
94 #include <netipsec/key_debug.h>
96 #include <netipsec/xform.h>
98 #include <machine/in_cksum.h>
100 #include <opencrypto/cryptodev.h>
103 VNET_DEFINE(int, ipsec_debug) = 1;
105 VNET_DEFINE(int, ipsec_debug) = 0;
108 /* NB: name changed so netstat doesn't use it. */
109 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec4stat);
110 VNET_PCPUSTAT_SYSINIT(ipsec4stat);
113 VNET_PCPUSTAT_SYSUNINIT(ipsec4stat);
116 VNET_DEFINE(int, ip4_ah_offsetmask) = 0; /* maybe IP_DF? */
117 /* DF bit on encap. 0: clear 1: set 2: copy */
118 VNET_DEFINE(int, ip4_ipsec_dfbit) = 0;
119 VNET_DEFINE(int, ip4_esp_trans_deflev) = IPSEC_LEVEL_USE;
120 VNET_DEFINE(int, ip4_esp_net_deflev) = IPSEC_LEVEL_USE;
121 VNET_DEFINE(int, ip4_ah_trans_deflev) = IPSEC_LEVEL_USE;
122 VNET_DEFINE(int, ip4_ah_net_deflev) = IPSEC_LEVEL_USE;
123 /* ECN ignore(-1)/forbidden(0)/allowed(1) */
124 VNET_DEFINE(int, ip4_ipsec_ecn) = 0;
125 VNET_DEFINE(int, ip4_esp_randpad) = -1;
127 static VNET_DEFINE(struct secpolicy, def_policy);
128 #define V_def_policy VNET(def_policy)
130 * Crypto support requirements:
132 * 1 require hardware support
133 * -1 require software support
136 VNET_DEFINE(int, crypto_support) = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
138 FEATURE(ipsec, "Internet Protocol Security (IPsec)");
140 FEATURE(ipsec_natt, "UDP Encapsulation of IPsec ESP Packets ('NAT-T')");
143 SYSCTL_DECL(_net_inet_ipsec);
146 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY, def_policy,
147 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(def_policy).policy, 0,
148 "IPsec default policy.");
149 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
150 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_esp_trans_deflev), 0,
151 "Default ESP transport mode level");
152 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
153 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_esp_net_deflev), 0,
154 "Default ESP tunnel mode level.");
155 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
156 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_trans_deflev), 0,
157 "AH transfer mode default level.");
158 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
159 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_net_deflev), 0,
160 "AH tunnel mode default level.");
161 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS, ah_cleartos,
162 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0,
163 "If set clear type-of-service field when doing AH computation.");
164 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK, ah_offsetmask,
165 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_offsetmask), 0,
166 "If not set clear offset field mask when doing AH computation.");
167 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT, dfbit,
168 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_dfbit), 0,
169 "Do not fragment bit on encap.");
170 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN, ecn,
171 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_ecn), 0,
172 "Explicit Congestion Notification handling.");
173 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG, debug,
174 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0,
175 "Enable IPsec debugging output when set.");
176 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, crypto_support,
177 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(crypto_support), 0,
178 "Crypto driver selection.");
179 SYSCTL_VNET_PCPUSTAT(_net_inet_ipsec, OID_AUTO, ipsecstats, struct ipsecstat,
180 ipsec4stat, "IPsec IPv4 statistics.");
184 * When set to 1, IPsec will send packets with the same sequence number.
185 * This allows to verify if the other side has proper replay attacks detection.
187 VNET_DEFINE(int, ipsec_replay) = 0;
188 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay,
189 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_replay), 0,
190 "Emulate replay attack");
192 * When set 1, IPsec will send packets with corrupted HMAC.
193 * This allows to verify if the other side properly detects modified packets.
195 VNET_DEFINE(int, ipsec_integrity) = 0;
196 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity,
197 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_integrity), 0,
198 "Emulate man-in-the-middle attack");
202 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec6stat);
203 VNET_PCPUSTAT_SYSINIT(ipsec6stat);
206 VNET_PCPUSTAT_SYSUNINIT(ipsec6stat);
209 VNET_DEFINE(int, ip6_esp_trans_deflev) = IPSEC_LEVEL_USE;
210 VNET_DEFINE(int, ip6_esp_net_deflev) = IPSEC_LEVEL_USE;
211 VNET_DEFINE(int, ip6_ah_trans_deflev) = IPSEC_LEVEL_USE;
212 VNET_DEFINE(int, ip6_ah_net_deflev) = IPSEC_LEVEL_USE;
213 VNET_DEFINE(int, ip6_ipsec_ecn) = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
215 SYSCTL_DECL(_net_inet6_ipsec6);
217 /* net.inet6.ipsec6 */
218 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY, def_policy,
219 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(def_policy).policy, 0,
220 "IPsec default policy.");
221 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
222 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_trans_deflev), 0,
223 "Default ESP transport mode level.");
224 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
225 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_net_deflev), 0,
226 "Default ESP tunnel mode level.");
227 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
228 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_trans_deflev), 0,
229 "AH transfer mode default level.");
230 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
231 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_net_deflev), 0,
232 "AH tunnel mode default level.");
233 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN, ecn,
234 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ipsec_ecn), 0,
235 "Explicit Congestion Notification handling.");
236 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG, debug,
237 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0,
238 "Enable IPsec debugging output when set.");
239 SYSCTL_VNET_PCPUSTAT(_net_inet6_ipsec6, IPSECCTL_STATS, ipsecstats,
240 struct ipsecstat, ipsec6stat, "IPsec IPv6 statistics.");
243 static int ipsec_in_reject(struct secpolicy *, const struct mbuf *);
244 static int ipsec_setspidx_inpcb(const struct mbuf *, struct inpcb *, u_int);
245 static int ipsec_setspidx(const struct mbuf *, struct secpolicyindex *, int);
246 static void ipsec4_get_ulp(const struct mbuf *m, struct secpolicyindex *, int);
247 static int ipsec4_setspidx_ipaddr(const struct mbuf *, struct secpolicyindex *);
249 static void ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *, int);
250 static int ipsec6_setspidx_ipaddr(const struct mbuf *, struct secpolicyindex *);
252 static void ipsec_delpcbpolicy(struct inpcbpolicy *);
253 static struct secpolicy *ipsec_deepcopy_policy(struct secpolicy *src);
255 MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy");
258 * Return a held reference to the default SP.
260 static struct secpolicy *
261 key_allocsp_default(const char* where, int tag)
263 struct secpolicy *sp;
265 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
266 printf("DP key_allocsp_default from %s:%u\n", where, tag));
269 if (sp->policy != IPSEC_POLICY_DISCARD &&
270 sp->policy != IPSEC_POLICY_NONE) {
271 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
272 sp->policy, IPSEC_POLICY_NONE));
273 sp->policy = IPSEC_POLICY_NONE;
277 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
278 printf("DP key_allocsp_default returns SP:%p (%u)\n",
282 #define KEY_ALLOCSP_DEFAULT() \
283 key_allocsp_default(__FILE__, __LINE__)
286 * For OUTBOUND packet having a socket. Searching SPD for packet,
287 * and return a pointer to SP.
288 * OUT: NULL: no apropreate SP found, the following value is set to error.
290 * EACCES : discard packet.
291 * ENOENT : ipsec_acquire() in progress, maybe.
292 * others : error occurred.
293 * others: a pointer to SP
295 * NOTE: IPv6 mapped adddress concern is implemented here.
298 ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
300 struct secpolicy *sp;
302 IPSEC_ASSERT(tdbi != NULL, ("null tdbi"));
303 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
304 ("invalid direction %u", dir));
306 sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
307 if (sp == NULL) /*XXX????*/
308 sp = KEY_ALLOCSP_DEFAULT();
309 IPSEC_ASSERT(sp != NULL, ("null SP"));
314 * For OUTBOUND packet having a socket. Searching SPD for packet,
315 * and return a pointer to SP.
316 * OUT: NULL: no apropreate SP found, the following value is set to error.
318 * EACCES : discard packet.
319 * ENOENT : ipsec_acquire() in progress, maybe.
320 * others : error occurred.
321 * others: a pointer to SP
323 * NOTE: IPv6 mapped adddress concern is implemented here.
325 static struct secpolicy *
326 ipsec_getpolicybysock(const struct mbuf *m, u_int dir, struct inpcb *inp,
329 struct inpcbpolicy *pcbsp;
330 struct secpolicy *currsp = NULL; /* Policy on socket. */
331 struct secpolicy *sp;
333 IPSEC_ASSERT(m != NULL, ("null mbuf"));
334 IPSEC_ASSERT(inp != NULL, ("null inpcb"));
335 IPSEC_ASSERT(error != NULL, ("null error"));
336 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
337 ("invalid direction %u", dir));
339 if (!key_havesp(dir)) {
340 /* No SP found, use system default. */
341 sp = KEY_ALLOCSP_DEFAULT();
345 /* Set spidx in pcb. */
346 *error = ipsec_setspidx_inpcb(m, inp, dir);
351 IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
353 case IPSEC_DIR_INBOUND:
354 currsp = pcbsp->sp_in;
356 case IPSEC_DIR_OUTBOUND:
357 currsp = pcbsp->sp_out;
360 IPSEC_ASSERT(currsp != NULL, ("null currsp"));
362 if (pcbsp->priv) { /* When privilieged socket. */
363 switch (currsp->policy) {
364 case IPSEC_POLICY_BYPASS:
365 case IPSEC_POLICY_IPSEC:
370 case IPSEC_POLICY_ENTRUST:
371 /* Look for a policy in SPD. */
372 sp = KEY_ALLOCSP(&currsp->spidx, dir);
373 if (sp == NULL) /* No SP found. */
374 sp = KEY_ALLOCSP_DEFAULT();
378 ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
379 __func__, currsp->policy));
383 } else { /* Unpriv, SPD has policy. */
384 sp = KEY_ALLOCSP(&currsp->spidx, dir);
385 if (sp == NULL) { /* No SP found. */
386 switch (currsp->policy) {
387 case IPSEC_POLICY_BYPASS:
388 ipseclog((LOG_ERR, "%s: Illegal policy for "
389 "non-priviliged defined %d\n",
390 __func__, currsp->policy));
394 case IPSEC_POLICY_ENTRUST:
395 sp = KEY_ALLOCSP_DEFAULT();
398 case IPSEC_POLICY_IPSEC:
404 ipseclog((LOG_ERR, "%s: Invalid policy for "
405 "PCB %d\n", __func__, currsp->policy));
411 IPSEC_ASSERT(sp != NULL,
412 ("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
413 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
414 printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
415 __func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
420 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
421 * and return a pointer to SP.
422 * OUT: positive: a pointer to the entry for security policy leaf matched.
423 * NULL: no apropreate SP found, the following value is set to error.
425 * EACCES : discard packet.
426 * ENOENT : ipsec_acquire() in progress, maybe.
427 * others : error occurred.
430 ipsec_getpolicybyaddr(const struct mbuf *m, u_int dir, int *error)
432 struct secpolicyindex spidx;
433 struct secpolicy *sp;
435 IPSEC_ASSERT(m != NULL, ("null mbuf"));
436 IPSEC_ASSERT(error != NULL, ("null error"));
437 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
438 ("invalid direction %u", dir));
442 if (key_havesp(dir)) {
443 /* Make an index to look for a policy. */
444 *error = ipsec_setspidx(m, &spidx, 0);
446 DPRINTF(("%s: setpidx failed, dir %u\n",
451 sp = KEY_ALLOCSP(&spidx, dir);
453 if (sp == NULL) /* No SP found, use system default. */
454 sp = KEY_ALLOCSP_DEFAULT();
455 IPSEC_ASSERT(sp != NULL, ("null SP"));
460 ipsec4_checkpolicy(const struct mbuf *m, u_int dir, int *error,
463 struct secpolicy *sp;
467 sp = ipsec_getpolicybyaddr(m, dir, error);
469 sp = ipsec_getpolicybysock(m, dir, inp, error);
471 IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
472 IPSECSTAT_INC(ips_out_inval);
475 IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
476 switch (sp->policy) {
477 case IPSEC_POLICY_ENTRUST:
479 printf("%s: invalid policy %u\n", __func__, sp->policy);
481 case IPSEC_POLICY_DISCARD:
482 IPSECSTAT_INC(ips_out_polvio);
483 *error = -EINVAL; /* Packet is discarded by caller. */
485 case IPSEC_POLICY_BYPASS:
486 case IPSEC_POLICY_NONE:
488 sp = NULL; /* NB: force NULL result. */
490 case IPSEC_POLICY_IPSEC:
491 if (sp->req == NULL) /* Acquire a SA. */
492 *error = key_spdacquire(sp);
503 ipsec_setspidx_inpcb(const struct mbuf *m, struct inpcb *inp, u_int dir)
505 struct secpolicyindex *spidx;
508 IPSEC_ASSERT(inp != NULL, ("null inp"));
509 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
510 IPSEC_ASSERT(inp->inp_sp->sp_out != NULL && inp->inp_sp->sp_in != NULL,
511 ("null sp_in || sp_out"));
513 if (dir == IPSEC_DIR_INBOUND)
514 spidx = &inp->inp_sp->sp_in->spidx;
516 spidx = &inp->inp_sp->sp_out->spidx;
517 error = ipsec_setspidx(m, spidx, 1);
521 bzero(&inp->inp_sp->sp_in->spidx,
522 sizeof (inp->inp_sp->sp_in->spidx));
523 bzero(&inp->inp_sp->sp_out->spidx,
524 sizeof (inp->inp_sp->sp_in->spidx));
530 * Configure security policy index (src/dst/proto/sport/dport)
531 * by looking at the content of mbuf.
532 * The caller is responsible for error recovery (like clearing up spidx).
535 ipsec_setspidx(const struct mbuf *m, struct secpolicyindex *spidx,
539 const struct ip *ip = NULL;
540 const struct mbuf *n;
545 IPSEC_ASSERT(m != NULL, ("null mbuf"));
548 * Validate m->m_pkthdr.len. We see incorrect length if we
549 * mistakenly call this function with inconsistent mbuf chain
550 * (like 4.4BSD tcp/udp processing). XXX Should we panic here?
553 for (n = m; n; n = n->m_next)
555 if (m->m_pkthdr.len != len) {
556 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
557 printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
558 __func__, len, m->m_pkthdr.len));
562 if (m->m_pkthdr.len < sizeof(struct ip)) {
563 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
564 printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
565 __func__, m->m_pkthdr.len));
569 if (m->m_len >= sizeof(*ip))
570 ip = mtod(m, const struct ip *);
572 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
578 error = ipsec4_setspidx_ipaddr(m, spidx);
581 ipsec4_get_ulp(m, spidx, needport);
585 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
586 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
587 printf("%s: pkthdr len(%d) too small (v6), "
588 "ignored\n", __func__, m->m_pkthdr.len));
591 error = ipsec6_setspidx_ipaddr(m, spidx);
594 ipsec6_get_ulp(m, spidx, needport);
598 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
599 printf("%s: " "unknown IP version %u, ignored.\n",
606 ipsec4_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx,
613 IPSEC_ASSERT(m != NULL, ("null mbuf"));
614 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
616 if (m->m_len >= sizeof (struct ip)) {
617 const struct ip *ip = mtod(m, const struct ip *);
618 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
620 off = ip->ip_hl << 2;
625 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
626 if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
632 while (off < m->m_pkthdr.len) {
639 spidx->ul_proto = nxt;
642 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
644 m_copydata(m, off, sizeof (th), (caddr_t) &th);
645 spidx->src.sin.sin_port = th.th_sport;
646 spidx->dst.sin.sin_port = th.th_dport;
649 spidx->ul_proto = nxt;
652 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
654 m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
655 spidx->src.sin.sin_port = uh.uh_sport;
656 spidx->dst.sin.sin_port = uh.uh_dport;
659 if (off + sizeof(ip6e) > m->m_pkthdr.len)
661 /* XXX Sigh, this works but is totally bogus. */
662 m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
663 off += (ip6e.ip6e_len + 2) << 2;
668 /* XXX Intermediate headers??? */
669 spidx->ul_proto = nxt;
674 spidx->ul_proto = IPSEC_ULPROTO_ANY;
676 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
677 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
680 /* Assumes that m is sane. */
682 ipsec4_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
684 static const struct sockaddr_in template = {
685 sizeof (struct sockaddr_in),
687 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
690 spidx->src.sin = template;
691 spidx->dst.sin = template;
693 if (m->m_len < sizeof (struct ip)) {
694 m_copydata(m, offsetof(struct ip, ip_src),
695 sizeof (struct in_addr),
696 (caddr_t) &spidx->src.sin.sin_addr);
697 m_copydata(m, offsetof(struct ip, ip_dst),
698 sizeof (struct in_addr),
699 (caddr_t) &spidx->dst.sin.sin_addr);
701 const struct ip *ip = mtod(m, const struct ip *);
702 spidx->src.sin.sin_addr = ip->ip_src;
703 spidx->dst.sin.sin_addr = ip->ip_dst;
706 spidx->prefs = sizeof(struct in_addr) << 3;
707 spidx->prefd = sizeof(struct in_addr) << 3;
714 ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx,
724 panic("%s: NULL pointer was passed.\n", __func__);
726 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
727 printf("%s:\n", __func__); kdebug_mbuf(m));
730 spidx->ul_proto = IPSEC_ULPROTO_ANY;
731 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
732 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
735 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
736 if (off < 0 || m->m_pkthdr.len < off)
741 spidx->ul_proto = nxt;
744 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
746 m_copydata(m, off, sizeof(th), (caddr_t)&th);
747 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
748 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
751 spidx->ul_proto = nxt;
754 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
756 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
757 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
758 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
761 spidx->ul_proto = nxt;
762 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
764 m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
765 ((struct sockaddr_in6 *)&spidx->src)->sin6_port =
766 htons((uint16_t)ih.icmp6_type);
767 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
768 htons((uint16_t)ih.icmp6_code);
771 /* XXX Intermediate headers??? */
772 spidx->ul_proto = nxt;
777 /* Assumes that m is sane. */
779 ipsec6_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
781 struct ip6_hdr ip6buf;
782 const struct ip6_hdr *ip6 = NULL;
783 struct sockaddr_in6 *sin6;
785 if (m->m_len >= sizeof(*ip6))
786 ip6 = mtod(m, const struct ip6_hdr *);
788 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
792 sin6 = (struct sockaddr_in6 *)&spidx->src;
793 bzero(sin6, sizeof(*sin6));
794 sin6->sin6_family = AF_INET6;
795 sin6->sin6_len = sizeof(struct sockaddr_in6);
796 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
797 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
798 sin6->sin6_addr.s6_addr16[1] = 0;
799 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
801 spidx->prefs = sizeof(struct in6_addr) << 3;
803 sin6 = (struct sockaddr_in6 *)&spidx->dst;
804 bzero(sin6, sizeof(*sin6));
805 sin6->sin6_family = AF_INET6;
806 sin6->sin6_len = sizeof(struct sockaddr_in6);
807 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
808 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
809 sin6->sin6_addr.s6_addr16[1] = 0;
810 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
812 spidx->prefd = sizeof(struct in6_addr) << 3;
819 ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int type)
826 idx = HHOOK_IPSEC_INET;
831 idx = HHOOK_IPSEC_INET6;
835 return (EPFNOSUPPORT);
837 if (type == HHOOK_TYPE_IPSEC_IN)
838 HHOOKS_RUN_IF(V_ipsec_hhh_in[idx], ctx, NULL);
840 HHOOKS_RUN_IF(V_ipsec_hhh_out[idx], ctx, NULL);
841 if (*ctx->mp == NULL)
847 ipsec_delpcbpolicy(struct inpcbpolicy *p)
850 free(p, M_IPSEC_INPCB);
853 /* Initialize policy in PCB. */
855 ipsec_init_policy(struct socket *so, struct inpcbpolicy **pcb_sp)
857 struct inpcbpolicy *new;
860 if (so == NULL || pcb_sp == NULL)
861 panic("%s: NULL pointer was passed.\n", __func__);
863 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
864 M_IPSEC_INPCB, M_NOWAIT|M_ZERO);
866 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
870 new->priv = IPSEC_IS_PRIVILEGED_SO(so);
872 if ((new->sp_in = KEY_NEWSP()) == NULL) {
873 ipsec_delpcbpolicy(new);
876 new->sp_in->policy = IPSEC_POLICY_ENTRUST;
877 if ((new->sp_out = KEY_NEWSP()) == NULL) {
878 KEY_FREESP(&new->sp_in);
879 ipsec_delpcbpolicy(new);
882 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
888 /* Copy old IPsec policy into new. */
890 ipsec_copy_policy(struct inpcbpolicy *old, struct inpcbpolicy *new)
892 struct secpolicy *sp;
894 sp = ipsec_deepcopy_policy(old->sp_in);
896 KEY_FREESP(&new->sp_in);
901 sp = ipsec_deepcopy_policy(old->sp_out);
903 KEY_FREESP(&new->sp_out);
908 new->priv = old->priv;
913 struct ipsecrequest *
916 struct ipsecrequest *p;
918 p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
920 IPSECREQUEST_LOCK_INIT(p);
925 ipsec_delisr(struct ipsecrequest *p)
928 IPSECREQUEST_LOCK_DESTROY(p);
932 /* Deep-copy a policy in PCB. */
933 static struct secpolicy *
934 ipsec_deepcopy_policy(struct secpolicy *src)
936 struct ipsecrequest *newchain = NULL;
937 struct ipsecrequest *p;
938 struct ipsecrequest **q;
939 struct ipsecrequest *r;
940 struct secpolicy *dst;
949 * Deep-copy IPsec request chain. This is required since struct
950 * ipsecrequest is not reference counted.
953 for (p = src->req; p; p = p->next) {
957 (*q)->saidx.proto = p->saidx.proto;
958 (*q)->saidx.mode = p->saidx.mode;
959 (*q)->level = p->level;
960 (*q)->saidx.reqid = p->saidx.reqid;
962 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
963 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
971 dst->policy = src->policy;
972 /* Do not touch the refcnt fields. */
977 for (p = newchain; p; p = r) {
986 /* Set policy and IPsec request if present. */
988 ipsec_set_policy_internal(struct secpolicy **pcb_sp, int optname,
989 caddr_t request, size_t len, struct ucred *cred)
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("%s: passed policy\n", __func__);
1004 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
1006 /* Check policy type. */
1007 /* ipsec_set_policy_internal() 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 (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1014 error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0);
1019 /* Allocating new SP entry. */
1020 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1023 /* Clear old SP and set new SP. */
1026 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1027 printf("%s: new policy\n", __func__);
1028 kdebug_secpolicy(newsp));
1034 ipsec_set_policy(struct inpcb *inp, int optname, caddr_t request,
1035 size_t len, struct ucred *cred)
1037 struct sadb_x_policy *xpl;
1038 struct secpolicy **pcb_sp;
1041 if (inp == NULL || request == NULL)
1043 if (len < sizeof(*xpl))
1045 xpl = (struct sadb_x_policy *)request;
1047 /* Select direction. */
1048 switch (xpl->sadb_x_policy_dir) {
1049 case IPSEC_DIR_INBOUND:
1050 pcb_sp = &inp->inp_sp->sp_in;
1052 case IPSEC_DIR_OUTBOUND:
1053 pcb_sp = &inp->inp_sp->sp_out;
1056 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1057 xpl->sadb_x_policy_dir));
1061 return (ipsec_set_policy_internal(pcb_sp, optname, request, len, cred));
1065 ipsec_get_policy(struct inpcb *inp, caddr_t request, size_t len,
1068 struct sadb_x_policy *xpl;
1069 struct secpolicy *pcb_sp;
1072 if (inp == NULL || request == NULL || mp == NULL)
1074 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
1075 if (len < sizeof(*xpl))
1077 xpl = (struct sadb_x_policy *)request;
1079 /* Select direction. */
1080 switch (xpl->sadb_x_policy_dir) {
1081 case IPSEC_DIR_INBOUND:
1082 pcb_sp = inp->inp_sp->sp_in;
1084 case IPSEC_DIR_OUTBOUND:
1085 pcb_sp = inp->inp_sp->sp_out;
1088 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1089 xpl->sadb_x_policy_dir));
1093 /* Sanity check. Should be an IPSEC_ASSERT. */
1097 *mp = key_sp2msg(pcb_sp);
1099 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
1103 (*mp)->m_type = MT_DATA;
1104 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1105 printf("%s:\n", __func__); kdebug_mbuf(*mp));
1110 /* Delete policy in PCB. */
1112 ipsec_delete_pcbpolicy(struct inpcb *inp)
1114 IPSEC_ASSERT(inp != NULL, ("null inp"));
1116 if (inp->inp_sp == NULL)
1119 if (inp->inp_sp->sp_in != NULL)
1120 KEY_FREESP(&inp->inp_sp->sp_in);
1122 if (inp->inp_sp->sp_out != NULL)
1123 KEY_FREESP(&inp->inp_sp->sp_out);
1125 ipsec_delpcbpolicy(inp->inp_sp);
1132 * Return current level.
1133 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1136 ipsec_get_reqlevel(struct ipsecrequest *isr)
1139 u_int esp_trans_deflev, esp_net_deflev;
1140 u_int ah_trans_deflev, ah_net_deflev;
1142 IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
1143 IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1144 ("af family mismatch, src %u, dst %u",
1145 isr->sp->spidx.src.sa.sa_family,
1146 isr->sp->spidx.dst.sa.sa_family));
1148 /* XXX Note that we have ipseclog() expanded here - code sync issue. */
1149 #define IPSEC_CHECK_DEFAULT(lev) \
1150 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1151 && (lev) != IPSEC_LEVEL_UNIQUE) \
1153 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1154 (lev), IPSEC_LEVEL_REQUIRE) \
1156 (lev) = IPSEC_LEVEL_REQUIRE, \
1160 /* Set default level. */
1161 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1164 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev);
1165 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev);
1166 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev);
1167 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev);
1172 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev);
1173 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev);
1174 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev);
1175 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev);
1179 panic("%s: unknown af %u",
1180 __func__, isr->sp->spidx.src.sa.sa_family);
1183 #undef IPSEC_CHECK_DEFAULT
1186 switch (isr->level) {
1187 case IPSEC_LEVEL_DEFAULT:
1188 switch (isr->saidx.proto) {
1190 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1191 level = esp_net_deflev;
1193 level = esp_trans_deflev;
1196 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1197 level = ah_net_deflev;
1199 level = ah_trans_deflev;
1201 case IPPROTO_IPCOMP:
1203 * We don't really care, as IPcomp document says that
1204 * we shouldn't compress small packets.
1206 level = IPSEC_LEVEL_USE;
1209 panic("%s: Illegal protocol defined %u\n", __func__,
1214 case IPSEC_LEVEL_USE:
1215 case IPSEC_LEVEL_REQUIRE:
1218 case IPSEC_LEVEL_UNIQUE:
1219 level = IPSEC_LEVEL_REQUIRE;
1223 panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
1230 * Check security policy requirements against the actual
1231 * packet contents. Return one if the packet should be
1232 * reject as "invalid"; otherwiser return zero to have the
1233 * packet treated as "valid".
1240 ipsec_in_reject(struct secpolicy *sp, const struct mbuf *m)
1242 struct ipsecrequest *isr;
1245 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1246 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1249 switch (sp->policy) {
1250 case IPSEC_POLICY_DISCARD:
1252 case IPSEC_POLICY_BYPASS:
1253 case IPSEC_POLICY_NONE:
1257 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1258 ("invalid policy %u", sp->policy));
1260 /* XXX Should compare policy against IPsec header history. */
1263 for (isr = sp->req; isr != NULL; isr = isr->next) {
1264 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1266 switch (isr->saidx.proto) {
1268 if ((m->m_flags & M_DECRYPTED) == 0) {
1269 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1270 printf("%s: ESP m_flags:%x\n", __func__,
1277 isr->sav->tdb_authalgxform != NULL &&
1278 (m->m_flags & M_AUTHIPDGM) == 0) {
1279 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1280 printf("%s: ESP/AH m_flags:%x\n", __func__,
1287 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1288 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1289 printf("%s: AH m_flags:%x\n", __func__,
1294 case IPPROTO_IPCOMP:
1296 * We don't really care, as IPcomp document
1297 * says that we shouldn't compress small
1298 * packets. IPComp policy should always be
1299 * treated as being in "use" level.
1304 return (0); /* Valid. */
1308 * Non zero return value means security policy DISCARD or policy violation.
1311 ipsec46_in_reject(const struct mbuf *m, struct inpcb *inp)
1313 struct secpolicy *sp;
1317 if (!key_havesp(IPSEC_DIR_INBOUND))
1320 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1322 /* Get SP for this packet. */
1324 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, &error);
1326 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1329 result = ipsec_in_reject(sp, m);
1332 result = 1; /* treat errors as policy violation */
1338 * Check AH/ESP integrity.
1339 * This function is called from tcp_input(), udp_input(),
1340 * and {ah,esp}4_input for tunnel mode.
1343 ipsec4_in_reject(const struct mbuf *m, struct inpcb *inp)
1347 result = ipsec46_in_reject(m, inp);
1349 IPSECSTAT_INC(ips_in_polvio);
1356 * Check AH/ESP integrity.
1357 * This function is called from tcp6_input(), udp6_input(),
1358 * and {ah,esp}6_input for tunnel mode.
1361 ipsec6_in_reject(const struct mbuf *m, struct inpcb *inp)
1365 result = ipsec46_in_reject(m, inp);
1367 IPSEC6STAT_INC(ips_in_polvio);
1374 * Compute the byte size to be occupied by IPsec header.
1375 * In case it is tunnelled, it includes the size of outer IP header.
1376 * NOTE: SP passed is freed in this function.
1379 ipsec_hdrsiz_internal(struct secpolicy *sp)
1381 struct ipsecrequest *isr;
1384 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1385 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1387 switch (sp->policy) {
1388 case IPSEC_POLICY_DISCARD:
1389 case IPSEC_POLICY_BYPASS:
1390 case IPSEC_POLICY_NONE:
1394 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1395 ("invalid policy %u", sp->policy));
1398 for (isr = sp->req; isr != NULL; isr = isr->next) {
1401 switch (isr->saidx.proto) {
1403 clen = esp_hdrsiz(isr->sav);
1406 clen = ah_hdrsiz(isr->sav);
1408 case IPPROTO_IPCOMP:
1409 clen = sizeof(struct ipcomp);
1413 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1414 switch (isr->saidx.dst.sa.sa_family) {
1416 clen += sizeof(struct ip);
1420 clen += sizeof(struct ip6_hdr);
1424 ipseclog((LOG_ERR, "%s: unknown AF %d in "
1425 "IPsec tunnel SA\n", __func__,
1426 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1437 * This function is called from ipsec_hdrsiz_tcp(), ip_ipsec_mtu(),
1438 * disabled ip6_ipsec_mtu() and ip6_forward().
1441 ipsec_hdrsiz(const struct mbuf *m, u_int dir, struct inpcb *inp)
1443 struct secpolicy *sp;
1447 if (!key_havesp(dir))
1450 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1452 /* Get SP for this packet. */
1454 sp = ipsec_getpolicybyaddr(m, dir, &error);
1456 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1459 size = ipsec_hdrsiz_internal(sp);
1460 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1461 printf("%s: size:%lu.\n", __func__,
1462 (unsigned long)size));
1466 size = 0; /* XXX Should be panic?
1467 * -> No, we are called w/o knowing if
1468 * IPsec processing is needed. */
1474 * Check the variable replay window.
1475 * ipsec_chkreplay() performs replay check before ICV verification.
1476 * ipsec_updatereplay() updates replay bitmap. This must be called after
1477 * ICV verification (it also performs replay check, which is usually done
1479 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1481 * Based on RFC 6479. Blocks are 32 bits unsigned integers
1484 #define IPSEC_BITMAP_INDEX_MASK(w) (w - 1)
1485 #define IPSEC_REDUNDANT_BIT_SHIFTS 5
1486 #define IPSEC_REDUNDANT_BITS (1 << IPSEC_REDUNDANT_BIT_SHIFTS)
1487 #define IPSEC_BITMAP_LOC_MASK (IPSEC_REDUNDANT_BITS - 1)
1490 ipsec_chkreplay(u_int32_t seq, struct secasvar *sav)
1492 const struct secreplay *replay;
1493 u_int32_t wsizeb; /* Constant: window size. */
1494 int ret, index, bit_location;
1496 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1497 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1502 replay = sav->replay;
1504 /* No need to check replay if disabled. */
1505 if (replay->wsize == 0)
1509 wsizeb = replay->wsize << 3;
1511 /* Sequence number of 0 is invalid. */
1515 /* First time is always okay. */
1516 if (replay->count == 0)
1519 /* Larger sequences are okay. */
1520 if (seq > replay->lastseq)
1523 /* Over range to check, i.e. too old or wrapped. */
1524 if (replay->lastseq - seq >= wsizeb)
1527 /* The sequence is inside the sliding window
1528 * now check the bit in the bitmap
1529 * bit location only depends on the sequence number
1531 bit_location = seq & IPSEC_BITMAP_LOC_MASK;
1532 index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS)
1533 & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size);
1535 /* This packet already seen? */
1536 if ((replay->bitmap)[index] & (1 << bit_location))
1542 SECASVAR_UNLOCK(sav);
1548 * Check replay counter whether to update or not.
1553 ipsec_updatereplay(u_int32_t seq, struct secasvar *sav)
1556 struct secreplay *replay;
1557 u_int32_t wsizeb; /* Constant: window size. */
1558 int ret, diff, index, bit_location;
1560 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1561 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1566 replay = sav->replay;
1568 if (replay->wsize == 0)
1569 goto ok; /* No need to check replay. */
1572 wsizeb = replay->wsize << 3;
1574 /* Sequence number of 0 is invalid. */
1578 /* The packet is too old, no need to update */
1579 if (wsizeb + seq < replay->lastseq)
1582 /* Now update the bit */
1583 index = (seq >> IPSEC_REDUNDANT_BIT_SHIFTS);
1585 /* First check if the sequence number is in the range */
1586 if (seq > replay->lastseq) {
1588 int index_cur = replay->lastseq >> IPSEC_REDUNDANT_BIT_SHIFTS;
1590 diff = index - index_cur;
1591 if (diff > replay->bitmap_size) {
1592 /* something unusual in this case */
1593 diff = replay->bitmap_size;
1596 for (id = 0; id < diff; ++id) {
1597 replay->bitmap[(id + index_cur + 1)
1598 & IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size)] = 0;
1601 replay->lastseq = seq;
1604 index &= IPSEC_BITMAP_INDEX_MASK(replay->bitmap_size);
1605 bit_location = seq & IPSEC_BITMAP_LOC_MASK;
1607 /* this packet has already been received */
1608 if (replay->bitmap[index] & (1 << bit_location))
1611 replay->bitmap[index] |= (1 << bit_location);
1614 if (replay->count == ~0) {
1616 /* Set overflow flag. */
1619 /* Don't increment, no more packets accepted. */
1620 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1623 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1624 __func__, replay->overflow,
1625 ipsec_logsastr(sav, buf, sizeof(buf))));
1631 SECASVAR_UNLOCK(sav);
1635 /* Return a printable string for the address. */
1637 ipsec_address(union sockaddr_union* sa, char *buf, socklen_t size)
1640 switch (sa->sa.sa_family) {
1643 return (inet_ntop(AF_INET, &sa->sin.sin_addr, buf, size));
1647 return (inet_ntop(AF_INET6, &sa->sin6.sin6_addr, buf, size));
1650 return ("(unknown address family)");
1655 ipsec_logsastr(struct secasvar *sav, char *buf, size_t size)
1657 char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
1659 IPSEC_ASSERT(sav->sah->saidx.src.sa.sa_family ==
1660 sav->sah->saidx.dst.sa.sa_family, ("address family mismatch"));
1662 snprintf(buf, size, "SA(SPI=%08lx src=%s dst=%s)",
1663 (u_long)ntohl(sav->spi),
1664 ipsec_address(&sav->sah->saidx.src, sbuf, sizeof(sbuf)),
1665 ipsec_address(&sav->sah->saidx.dst, dbuf, sizeof(dbuf)));
1670 ipsec_dumpmbuf(const struct mbuf *m)
1679 p = mtod(m, const u_char *);
1680 for (i = 0; i < m->m_len; i++) {
1681 printf("%02x ", p[i]);
1683 if (totlen % 16 == 0)
1688 if (totlen % 16 != 0)
1694 def_policy_init(const void *unused __unused)
1697 bzero(&V_def_policy, sizeof(struct secpolicy));
1698 V_def_policy.policy = IPSEC_POLICY_NONE;
1699 V_def_policy.refcnt = 1;
1701 VNET_SYSINIT(def_policy_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
1702 def_policy_init, NULL);
1705 /* XXX This stuff doesn't belong here... */
1707 static struct xformsw* xforms = NULL;
1710 * Register a transform; typically at system startup.
1713 xform_register(struct xformsw* xsp)
1716 xsp->xf_next = xforms;
1721 * Initialize transform support in an sav.
1724 xform_init(struct secasvar *sav, int xftype)
1726 struct xformsw *xsp;
1728 if (sav->tdb_xform != NULL) /* Previously initialized. */
1730 for (xsp = xforms; xsp; xsp = xsp->xf_next)
1731 if (xsp->xf_type == xftype)
1732 return ((*xsp->xf_init)(sav, xsp));