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>
52 #include <sys/kernel.h>
53 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
58 #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>
80 #include <sys/types.h>
81 #include <netipsec/ipsec.h>
83 #include <netipsec/ipsec6.h>
85 #include <netipsec/ah_var.h>
86 #include <netipsec/esp_var.h>
87 #include <netipsec/ipcomp.h> /*XXX*/
88 #include <netipsec/ipcomp_var.h>
90 #include <netipsec/key.h>
91 #include <netipsec/keydb.h>
92 #include <netipsec/key_debug.h>
94 #include <netipsec/xform.h>
96 #include <machine/in_cksum.h>
98 #include <opencrypto/cryptodev.h>
101 VNET_DEFINE(int, ipsec_debug) = 1;
103 VNET_DEFINE(int, ipsec_debug) = 0;
106 /* NB: name changed so netstat doesn't use it. */
107 VNET_DEFINE(struct ipsecstat, ipsec4stat);
108 VNET_DEFINE(int, ip4_ah_offsetmask) = 0; /* maybe IP_DF? */
109 /* DF bit on encap. 0: clear 1: set 2: copy */
110 VNET_DEFINE(int, ip4_ipsec_dfbit) = 0;
111 VNET_DEFINE(int, ip4_esp_trans_deflev) = IPSEC_LEVEL_USE;
112 VNET_DEFINE(int, ip4_esp_net_deflev) = IPSEC_LEVEL_USE;
113 VNET_DEFINE(int, ip4_ah_trans_deflev) = IPSEC_LEVEL_USE;
114 VNET_DEFINE(int, ip4_ah_net_deflev) = IPSEC_LEVEL_USE;
115 VNET_DEFINE(struct secpolicy, ip4_def_policy);
116 /* ECN ignore(-1)/forbidden(0)/allowed(1) */
117 VNET_DEFINE(int, ip4_ipsec_ecn) = 0;
118 VNET_DEFINE(int, ip4_esp_randpad) = -1;
121 * Crypto support requirements:
123 * 1 require hardware support
124 * -1 require software support
127 VNET_DEFINE(int, crypto_support) = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
129 FEATURE(ipsec, "Internet Protocol Security (IPsec)");
131 FEATURE(ipsec_natt, "UDP Encapsulation of IPsec ESP Packets ('NAT-T')");
134 SYSCTL_DECL(_net_inet_ipsec);
137 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY, def_policy,
138 CTLFLAG_RW, &VNET_NAME(ip4_def_policy).policy, 0,
139 "IPsec default policy.");
140 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
141 CTLFLAG_RW, &VNET_NAME(ip4_esp_trans_deflev), 0,
142 "Default ESP transport mode level");
143 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
144 CTLFLAG_RW, &VNET_NAME(ip4_esp_net_deflev), 0,
145 "Default ESP tunnel mode level.");
146 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
147 CTLFLAG_RW, &VNET_NAME(ip4_ah_trans_deflev), 0,
148 "AH transfer mode default level.");
149 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
150 CTLFLAG_RW, &VNET_NAME(ip4_ah_net_deflev), 0,
151 "AH tunnel mode default level.");
152 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS, ah_cleartos,
153 CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0,
154 "If set clear type-of-service field when doing AH computation.");
155 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK, ah_offsetmask,
156 CTLFLAG_RW, &VNET_NAME(ip4_ah_offsetmask), 0,
157 "If not set clear offset field mask when doing AH computation.");
158 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DFBIT, dfbit,
159 CTLFLAG_RW, &VNET_NAME(ip4_ipsec_dfbit), 0,
160 "Do not fragment bit on encap.");
161 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_ECN, ecn,
162 CTLFLAG_RW, &VNET_NAME(ip4_ipsec_ecn), 0,
163 "Explicit Congestion Notification handling.");
164 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEBUG, debug,
165 CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0,
166 "Enable IPsec debugging output when set.");
167 SYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, crypto_support,
168 CTLFLAG_RW, &VNET_NAME(crypto_support), 0,
169 "Crypto driver selection.");
170 SYSCTL_VNET_STRUCT(_net_inet_ipsec, OID_AUTO, ipsecstats,
171 CTLFLAG_RD, &VNET_NAME(ipsec4stat), ipsecstat,
172 "IPsec IPv4 statistics.");
176 * When set to 1, IPsec will send packets with the same sequence number.
177 * This allows to verify if the other side has proper replay attacks detection.
179 VNET_DEFINE(int, ipsec_replay) = 0;
180 SYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, test_replay,
181 CTLFLAG_RW, &VNET_NAME(ipsec_replay), 0,
182 "Emulate replay attack");
184 * When set 1, IPsec will send packets with corrupted HMAC.
185 * This allows to verify if the other side properly detects modified packets.
187 VNET_DEFINE(int, ipsec_integrity) = 0;
188 SYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, test_integrity,
189 CTLFLAG_RW, &VNET_NAME(ipsec_integrity), 0,
190 "Emulate man-in-the-middle attack");
194 VNET_DEFINE(struct ipsecstat, ipsec6stat);
195 VNET_DEFINE(int, ip6_esp_trans_deflev) = IPSEC_LEVEL_USE;
196 VNET_DEFINE(int, ip6_esp_net_deflev) = IPSEC_LEVEL_USE;
197 VNET_DEFINE(int, ip6_ah_trans_deflev) = IPSEC_LEVEL_USE;
198 VNET_DEFINE(int, ip6_ah_net_deflev) = IPSEC_LEVEL_USE;
199 VNET_DEFINE(int, ip6_ipsec_ecn) = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
201 SYSCTL_DECL(_net_inet6_ipsec6);
203 /* net.inet6.ipsec6 */
205 SYSCTL_OID(_net_inet6_ipsec6, IPSECCTL_STATS, stats, CTLFLAG_RD,
206 0, 0, compat_ipsecstats_sysctl, "S", "IPsec IPv6 statistics.");
207 #endif /* COMPAT_KAME */
208 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY, def_policy, CTLFLAG_RW,
209 &VNET_NAME(ip4_def_policy).policy, 0,
210 "IPsec default policy.");
211 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV,
212 esp_trans_deflev, CTLFLAG_RW, &VNET_NAME(ip6_esp_trans_deflev), 0,
213 "Default ESP transport mode level.");
214 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV,
215 esp_net_deflev, CTLFLAG_RW, &VNET_NAME(ip6_esp_net_deflev), 0,
216 "Default ESP tunnel mode level.");
217 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV,
218 ah_trans_deflev, CTLFLAG_RW, &VNET_NAME(ip6_ah_trans_deflev), 0,
219 "AH transfer mode default level.");
220 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV,
221 ah_net_deflev, CTLFLAG_RW, &VNET_NAME(ip6_ah_net_deflev), 0,
222 "AH tunnel mode default level.");
223 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_ECN,
224 ecn, CTLFLAG_RW, &VNET_NAME(ip6_ipsec_ecn), 0,
225 "Explicit Congestion Notification handling.");
226 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG, debug, CTLFLAG_RW,
227 &VNET_NAME(ipsec_debug), 0,
228 "Enable IPsec debugging output when set.");
229 SYSCTL_VNET_STRUCT(_net_inet6_ipsec6, IPSECCTL_STATS,
230 ipsecstats, CTLFLAG_RD, &VNET_NAME(ipsec6stat), ipsecstat,
231 "IPsec IPv6 statistics.");
234 static int ipsec_setspidx_inpcb __P((struct mbuf *, struct inpcb *));
235 static int ipsec_setspidx __P((struct mbuf *, struct secpolicyindex *, int));
236 static void ipsec4_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
237 static int ipsec4_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
239 static void ipsec6_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
240 static int ipsec6_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
242 static void ipsec_delpcbpolicy __P((struct inpcbpolicy *));
243 static struct secpolicy *ipsec_deepcopy_policy __P((struct secpolicy *src));
244 static void vshiftl __P((unsigned char *, int, int));
246 MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy");
249 * Return a held reference to the default SP.
251 static struct secpolicy *
252 key_allocsp_default(const char* where, int tag)
254 struct secpolicy *sp;
256 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
257 printf("DP key_allocsp_default from %s:%u\n", where, tag));
259 sp = &V_ip4_def_policy;
260 if (sp->policy != IPSEC_POLICY_DISCARD &&
261 sp->policy != IPSEC_POLICY_NONE) {
262 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
263 sp->policy, IPSEC_POLICY_NONE));
264 sp->policy = IPSEC_POLICY_NONE;
268 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
269 printf("DP key_allocsp_default returns SP:%p (%u)\n",
273 #define KEY_ALLOCSP_DEFAULT() \
274 key_allocsp_default(__FILE__, __LINE__)
277 * For OUTBOUND packet having a socket. Searching SPD for packet,
278 * and return a pointer to SP.
279 * OUT: NULL: no apropreate SP found, the following value is set to error.
281 * EACCES : discard packet.
282 * ENOENT : ipsec_acquire() in progress, maybe.
283 * others : error occured.
284 * others: a pointer to SP
286 * NOTE: IPv6 mapped adddress concern is implemented here.
289 ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
291 struct secpolicy *sp;
293 IPSEC_ASSERT(tdbi != NULL, ("null tdbi"));
294 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
295 ("invalid direction %u", dir));
297 sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
298 if (sp == NULL) /*XXX????*/
299 sp = KEY_ALLOCSP_DEFAULT();
300 IPSEC_ASSERT(sp != NULL, ("null SP"));
305 * For OUTBOUND packet having a socket. Searching SPD for packet,
306 * and return a pointer to SP.
307 * OUT: NULL: no apropreate SP found, the following value is set to error.
309 * EACCES : discard packet.
310 * ENOENT : ipsec_acquire() in progress, maybe.
311 * others : error occured.
312 * others: a pointer to SP
314 * NOTE: IPv6 mapped adddress concern is implemented here.
316 static struct secpolicy *
317 ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb *inp, int *error)
319 struct inpcbpolicy *pcbsp;
320 struct secpolicy *currsp = NULL; /* Policy on socket. */
321 struct secpolicy *sp;
323 IPSEC_ASSERT(m != NULL, ("null mbuf"));
324 IPSEC_ASSERT(inp != NULL, ("null inpcb"));
325 IPSEC_ASSERT(error != NULL, ("null error"));
326 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
327 ("invalid direction %u", dir));
329 /* Set spidx in pcb. */
330 *error = ipsec_setspidx_inpcb(m, inp);
335 IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
337 case IPSEC_DIR_INBOUND:
338 currsp = pcbsp->sp_in;
340 case IPSEC_DIR_OUTBOUND:
341 currsp = pcbsp->sp_out;
344 IPSEC_ASSERT(currsp != NULL, ("null currsp"));
346 if (pcbsp->priv) { /* When privilieged socket. */
347 switch (currsp->policy) {
348 case IPSEC_POLICY_BYPASS:
349 case IPSEC_POLICY_IPSEC:
354 case IPSEC_POLICY_ENTRUST:
355 /* Look for a policy in SPD. */
356 sp = KEY_ALLOCSP(&currsp->spidx, dir);
357 if (sp == NULL) /* No SP found. */
358 sp = KEY_ALLOCSP_DEFAULT();
362 ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
363 __func__, currsp->policy));
367 } else { /* Unpriv, SPD has policy. */
368 sp = KEY_ALLOCSP(&currsp->spidx, dir);
369 if (sp == NULL) { /* No SP found. */
370 switch (currsp->policy) {
371 case IPSEC_POLICY_BYPASS:
372 ipseclog((LOG_ERR, "%s: Illegal policy for "
373 "non-priviliged defined %d\n",
374 __func__, currsp->policy));
378 case IPSEC_POLICY_ENTRUST:
379 sp = KEY_ALLOCSP_DEFAULT();
382 case IPSEC_POLICY_IPSEC:
388 ipseclog((LOG_ERR, "%s: Invalid policy for "
389 "PCB %d\n", __func__, currsp->policy));
395 IPSEC_ASSERT(sp != NULL,
396 ("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
397 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
398 printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
399 __func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
404 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
405 * and return a pointer to SP.
406 * OUT: positive: a pointer to the entry for security policy leaf matched.
407 * NULL: no apropreate SP found, the following value is set to error.
409 * EACCES : discard packet.
410 * ENOENT : ipsec_acquire() in progress, maybe.
411 * others : error occured.
414 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
416 struct secpolicyindex spidx;
417 struct secpolicy *sp;
419 IPSEC_ASSERT(m != NULL, ("null mbuf"));
420 IPSEC_ASSERT(error != NULL, ("null error"));
421 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
422 ("invalid direction %u", dir));
425 if (key_havesp(dir)) {
426 /* Make an index to look for a policy. */
427 *error = ipsec_setspidx(m, &spidx,
428 (flag & IP_FORWARDING) ? 0 : 1);
430 DPRINTF(("%s: setpidx failed, dir %u flag %u\n",
431 __func__, dir, flag));
436 sp = KEY_ALLOCSP(&spidx, dir);
438 if (sp == NULL) /* No SP found, use system default. */
439 sp = KEY_ALLOCSP_DEFAULT();
440 IPSEC_ASSERT(sp != NULL, ("null SP"));
445 ipsec4_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
448 struct secpolicy *sp;
452 sp = ipsec_getpolicybyaddr(m, dir, flag, error);
454 sp = ipsec_getpolicybysock(m, dir, inp, error);
456 IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
457 IPSECSTAT_INC(ips_out_inval);
460 IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
461 switch (sp->policy) {
462 case IPSEC_POLICY_ENTRUST:
464 printf("%s: invalid policy %u\n", __func__, sp->policy);
466 case IPSEC_POLICY_DISCARD:
467 IPSECSTAT_INC(ips_out_polvio);
468 *error = -EINVAL; /* Packet is discarded by caller. */
470 case IPSEC_POLICY_BYPASS:
471 case IPSEC_POLICY_NONE:
473 sp = NULL; /* NB: force NULL result. */
475 case IPSEC_POLICY_IPSEC:
476 if (sp->req == NULL) /* Acquire a SA. */
477 *error = key_spdacquire(sp);
488 ipsec_setspidx_inpcb(struct mbuf *m, struct inpcb *inp)
492 IPSEC_ASSERT(inp != NULL, ("null inp"));
493 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
494 IPSEC_ASSERT(inp->inp_sp->sp_out != NULL && inp->inp_sp->sp_in != NULL,
495 ("null sp_in || sp_out"));
497 error = ipsec_setspidx(m, &inp->inp_sp->sp_in->spidx, 1);
499 inp->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
500 inp->inp_sp->sp_out->spidx = inp->inp_sp->sp_in->spidx;
501 inp->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
503 bzero(&inp->inp_sp->sp_in->spidx,
504 sizeof (inp->inp_sp->sp_in->spidx));
505 bzero(&inp->inp_sp->sp_out->spidx,
506 sizeof (inp->inp_sp->sp_in->spidx));
512 * Configure security policy index (src/dst/proto/sport/dport)
513 * by looking at the content of mbuf.
514 * The caller is responsible for error recovery (like clearing up spidx).
517 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
519 struct ip *ip = NULL;
526 IPSEC_ASSERT(m != NULL, ("null mbuf"));
529 * Validate m->m_pkthdr.len. We see incorrect length if we
530 * mistakenly call this function with inconsistent mbuf chain
531 * (like 4.4BSD tcp/udp processing). XXX Should we panic here?
534 for (n = m; n; n = n->m_next)
536 if (m->m_pkthdr.len != len) {
537 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
538 printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
539 __func__, len, m->m_pkthdr.len));
543 if (m->m_pkthdr.len < sizeof(struct ip)) {
544 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
545 printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
546 __func__, m->m_pkthdr.len));
550 if (m->m_len >= sizeof(*ip))
551 ip = mtod(m, struct ip *);
553 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
557 v = _IP_VHL_V(ip->ip_vhl);
563 error = ipsec4_setspidx_ipaddr(m, spidx);
566 ipsec4_get_ulp(m, spidx, needport);
570 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
571 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
572 printf("%s: pkthdr len(%d) too small (v6), "
573 "ignored\n", __func__, m->m_pkthdr.len));
576 error = ipsec6_setspidx_ipaddr(m, spidx);
579 ipsec6_get_ulp(m, spidx, needport);
583 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
584 printf("%s: " "unknown IP version %u, ignored.\n",
591 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
597 IPSEC_ASSERT(m != NULL, ("null mbuf"));
598 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
600 if (m->m_len >= sizeof (struct ip)) {
601 struct ip *ip = mtod(m, struct ip *);
602 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
605 off = _IP_VHL_HL(ip->ip_vhl) << 2;
607 off = ip->ip_hl << 2;
613 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
614 if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
617 off = _IP_VHL_HL(ih.ip_vhl) << 2;
624 while (off < m->m_pkthdr.len) {
631 spidx->ul_proto = nxt;
634 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
636 m_copydata(m, off, sizeof (th), (caddr_t) &th);
637 spidx->src.sin.sin_port = th.th_sport;
638 spidx->dst.sin.sin_port = th.th_dport;
641 spidx->ul_proto = nxt;
644 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
646 m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
647 spidx->src.sin.sin_port = uh.uh_sport;
648 spidx->dst.sin.sin_port = uh.uh_dport;
651 if (off + sizeof(ip6e) > m->m_pkthdr.len)
653 /* XXX Sigh, this works but is totally bogus. */
654 m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
655 off += (ip6e.ip6e_len + 2) << 2;
660 /* XXX Intermediate headers??? */
661 spidx->ul_proto = nxt;
666 spidx->ul_proto = IPSEC_ULPROTO_ANY;
668 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
669 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
672 /* Assumes that m is sane. */
674 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
676 static const struct sockaddr_in template = {
677 sizeof (struct sockaddr_in),
679 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
682 spidx->src.sin = template;
683 spidx->dst.sin = template;
685 if (m->m_len < sizeof (struct ip)) {
686 m_copydata(m, offsetof(struct ip, ip_src),
687 sizeof (struct in_addr),
688 (caddr_t) &spidx->src.sin.sin_addr);
689 m_copydata(m, offsetof(struct ip, ip_dst),
690 sizeof (struct in_addr),
691 (caddr_t) &spidx->dst.sin.sin_addr);
693 struct ip *ip = mtod(m, struct ip *);
694 spidx->src.sin.sin_addr = ip->ip_src;
695 spidx->dst.sin.sin_addr = ip->ip_dst;
698 spidx->prefs = sizeof(struct in_addr) << 3;
699 spidx->prefd = sizeof(struct in_addr) << 3;
706 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
715 panic("%s: NULL pointer was passed.\n", __func__);
717 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
718 printf("%s:\n", __func__); kdebug_mbuf(m));
721 spidx->ul_proto = IPSEC_ULPROTO_ANY;
722 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
723 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
726 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
727 if (off < 0 || m->m_pkthdr.len < off)
732 spidx->ul_proto = nxt;
735 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
737 m_copydata(m, off, sizeof(th), (caddr_t)&th);
738 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
739 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
742 spidx->ul_proto = nxt;
745 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
747 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
748 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
749 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
752 spidx->ul_proto = nxt;
753 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
755 m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
756 ((struct sockaddr_in6 *)&spidx->src)->sin6_port =
757 htons((uint16_t)ih.icmp6_type);
758 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
759 htons((uint16_t)ih.icmp6_code);
762 /* XXX Intermediate headers??? */
763 spidx->ul_proto = nxt;
768 /* Assumes that m is sane. */
770 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
772 struct ip6_hdr *ip6 = NULL;
773 struct ip6_hdr ip6buf;
774 struct sockaddr_in6 *sin6;
776 if (m->m_len >= sizeof(*ip6))
777 ip6 = mtod(m, struct ip6_hdr *);
779 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
783 sin6 = (struct sockaddr_in6 *)&spidx->src;
784 bzero(sin6, sizeof(*sin6));
785 sin6->sin6_family = AF_INET6;
786 sin6->sin6_len = sizeof(struct sockaddr_in6);
787 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
788 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
789 sin6->sin6_addr.s6_addr16[1] = 0;
790 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
792 spidx->prefs = sizeof(struct in6_addr) << 3;
794 sin6 = (struct sockaddr_in6 *)&spidx->dst;
795 bzero(sin6, sizeof(*sin6));
796 sin6->sin6_family = AF_INET6;
797 sin6->sin6_len = sizeof(struct sockaddr_in6);
798 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
799 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
800 sin6->sin6_addr.s6_addr16[1] = 0;
801 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
803 spidx->prefd = sizeof(struct in6_addr) << 3;
810 ipsec_delpcbpolicy(struct inpcbpolicy *p)
813 free(p, M_IPSEC_INPCB);
816 /* Initialize policy in PCB. */
818 ipsec_init_policy(struct socket *so, struct inpcbpolicy **pcb_sp)
820 struct inpcbpolicy *new;
823 if (so == NULL || pcb_sp == NULL)
824 panic("%s: NULL pointer was passed.\n", __func__);
826 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
827 M_IPSEC_INPCB, M_NOWAIT|M_ZERO);
829 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
833 new->priv = IPSEC_IS_PRIVILEGED_SO(so);
835 if ((new->sp_in = KEY_NEWSP()) == NULL) {
836 ipsec_delpcbpolicy(new);
839 new->sp_in->state = IPSEC_SPSTATE_ALIVE;
840 new->sp_in->policy = IPSEC_POLICY_ENTRUST;
842 if ((new->sp_out = KEY_NEWSP()) == NULL) {
843 KEY_FREESP(&new->sp_in);
844 ipsec_delpcbpolicy(new);
847 new->sp_out->state = IPSEC_SPSTATE_ALIVE;
848 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
855 /* Copy old IPsec policy into new. */
857 ipsec_copy_policy(struct inpcbpolicy *old, struct inpcbpolicy *new)
859 struct secpolicy *sp;
861 sp = ipsec_deepcopy_policy(old->sp_in);
863 KEY_FREESP(&new->sp_in);
868 sp = ipsec_deepcopy_policy(old->sp_out);
870 KEY_FREESP(&new->sp_out);
875 new->priv = old->priv;
880 struct ipsecrequest *
883 struct ipsecrequest *p;
885 p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
887 IPSECREQUEST_LOCK_INIT(p);
892 ipsec_delisr(struct ipsecrequest *p)
895 IPSECREQUEST_LOCK_DESTROY(p);
899 /* Deep-copy a policy in PCB. */
900 static struct secpolicy *
901 ipsec_deepcopy_policy(struct secpolicy *src)
903 struct ipsecrequest *newchain = NULL;
904 struct ipsecrequest *p;
905 struct ipsecrequest **q;
906 struct ipsecrequest *r;
907 struct secpolicy *dst;
916 * Deep-copy IPsec request chain. This is required since struct
917 * ipsecrequest is not reference counted.
920 for (p = src->req; p; p = p->next) {
924 (*q)->saidx.proto = p->saidx.proto;
925 (*q)->saidx.mode = p->saidx.mode;
926 (*q)->level = p->level;
927 (*q)->saidx.reqid = p->saidx.reqid;
929 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
930 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
938 dst->state = src->state;
939 dst->policy = src->policy;
940 /* Do not touch the refcnt fields. */
945 for (p = newchain; p; p = r) {
953 /* Set policy and IPsec request if present. */
955 ipsec_set_policy_internal(struct secpolicy **pcb_sp, int optname,
956 caddr_t request, size_t len, struct ucred *cred)
958 struct sadb_x_policy *xpl;
959 struct secpolicy *newsp = NULL;
963 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
965 if (len < sizeof(*xpl))
967 xpl = (struct sadb_x_policy *)request;
969 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
970 printf("%s: passed policy\n", __func__);
971 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
973 /* Check policy type. */
974 /* ipsec_set_policy_internal() accepts IPSEC, ENTRUST and BYPASS. */
975 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
976 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
979 /* Check privileged socket. */
980 if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
981 error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0);
986 /* Allocating new SP entry. */
987 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
990 newsp->state = IPSEC_SPSTATE_ALIVE;
992 /* Clear old SP and set new SP. */
995 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
996 printf("%s: new policy\n", __func__);
997 kdebug_secpolicy(newsp));
1003 ipsec_set_policy(struct inpcb *inp, int optname, caddr_t request,
1004 size_t len, struct ucred *cred)
1006 struct sadb_x_policy *xpl;
1007 struct secpolicy **pcb_sp;
1010 if (inp == NULL || request == NULL)
1012 if (len < sizeof(*xpl))
1014 xpl = (struct sadb_x_policy *)request;
1016 /* Select direction. */
1017 switch (xpl->sadb_x_policy_dir) {
1018 case IPSEC_DIR_INBOUND:
1019 pcb_sp = &inp->inp_sp->sp_in;
1021 case IPSEC_DIR_OUTBOUND:
1022 pcb_sp = &inp->inp_sp->sp_out;
1025 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1026 xpl->sadb_x_policy_dir));
1030 return (ipsec_set_policy_internal(pcb_sp, optname, request, len, cred));
1034 ipsec_get_policy(struct inpcb *inp, caddr_t request, size_t len,
1037 struct sadb_x_policy *xpl;
1038 struct secpolicy *pcb_sp;
1041 if (inp == NULL || request == NULL || mp == NULL)
1043 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
1044 if (len < sizeof(*xpl))
1046 xpl = (struct sadb_x_policy *)request;
1048 /* Select direction. */
1049 switch (xpl->sadb_x_policy_dir) {
1050 case IPSEC_DIR_INBOUND:
1051 pcb_sp = inp->inp_sp->sp_in;
1053 case IPSEC_DIR_OUTBOUND:
1054 pcb_sp = inp->inp_sp->sp_out;
1057 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1058 xpl->sadb_x_policy_dir));
1062 /* Sanity check. Should be an IPSEC_ASSERT. */
1066 *mp = key_sp2msg(pcb_sp);
1068 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
1072 (*mp)->m_type = MT_DATA;
1073 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1074 printf("%s:\n", __func__); kdebug_mbuf(*mp));
1079 /* Delete policy in PCB. */
1081 ipsec_delete_pcbpolicy(struct inpcb *inp)
1083 IPSEC_ASSERT(inp != NULL, ("null inp"));
1085 if (inp->inp_sp == NULL)
1088 if (inp->inp_sp->sp_in != NULL)
1089 KEY_FREESP(&inp->inp_sp->sp_in);
1091 if (inp->inp_sp->sp_out != NULL)
1092 KEY_FREESP(&inp->inp_sp->sp_out);
1094 ipsec_delpcbpolicy(inp->inp_sp);
1101 * Return current level.
1102 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1105 ipsec_get_reqlevel(struct ipsecrequest *isr)
1108 u_int esp_trans_deflev, esp_net_deflev;
1109 u_int ah_trans_deflev, ah_net_deflev;
1111 IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
1112 IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1113 ("af family mismatch, src %u, dst %u",
1114 isr->sp->spidx.src.sa.sa_family,
1115 isr->sp->spidx.dst.sa.sa_family));
1117 /* XXX Note that we have ipseclog() expanded here - code sync issue. */
1118 #define IPSEC_CHECK_DEFAULT(lev) \
1119 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1120 && (lev) != IPSEC_LEVEL_UNIQUE) \
1122 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1123 (lev), IPSEC_LEVEL_REQUIRE) \
1125 (lev) = IPSEC_LEVEL_REQUIRE, \
1129 /* Set default level. */
1130 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1133 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev);
1134 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev);
1135 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev);
1136 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev);
1141 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev);
1142 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev);
1143 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev);
1144 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev);
1148 panic("%s: unknown af %u",
1149 __func__, isr->sp->spidx.src.sa.sa_family);
1152 #undef IPSEC_CHECK_DEFAULT
1155 switch (isr->level) {
1156 case IPSEC_LEVEL_DEFAULT:
1157 switch (isr->saidx.proto) {
1159 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1160 level = esp_net_deflev;
1162 level = esp_trans_deflev;
1165 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1166 level = ah_net_deflev;
1168 level = ah_trans_deflev;
1170 case IPPROTO_IPCOMP:
1172 * We don't really care, as IPcomp document says that
1173 * we shouldn't compress small packets.
1175 level = IPSEC_LEVEL_USE;
1178 panic("%s: Illegal protocol defined %u\n", __func__,
1183 case IPSEC_LEVEL_USE:
1184 case IPSEC_LEVEL_REQUIRE:
1187 case IPSEC_LEVEL_UNIQUE:
1188 level = IPSEC_LEVEL_REQUIRE;
1192 panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
1199 * Check security policy requirements against the actual
1200 * packet contents. Return one if the packet should be
1201 * reject as "invalid"; otherwiser return zero to have the
1202 * packet treated as "valid".
1209 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1211 struct ipsecrequest *isr;
1214 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1215 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1218 switch (sp->policy) {
1219 case IPSEC_POLICY_DISCARD:
1221 case IPSEC_POLICY_BYPASS:
1222 case IPSEC_POLICY_NONE:
1226 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1227 ("invalid policy %u", sp->policy));
1229 /* XXX Should compare policy against IPsec header history. */
1232 for (isr = sp->req; isr != NULL; isr = isr->next) {
1233 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1235 switch (isr->saidx.proto) {
1237 if ((m->m_flags & M_DECRYPTED) == 0) {
1238 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1239 printf("%s: ESP m_flags:%x\n", __func__,
1246 isr->sav->tdb_authalgxform != NULL &&
1247 (m->m_flags & M_AUTHIPDGM) == 0) {
1248 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1249 printf("%s: ESP/AH m_flags:%x\n", __func__,
1256 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1257 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1258 printf("%s: AH m_flags:%x\n", __func__,
1263 case IPPROTO_IPCOMP:
1265 * We don't really care, as IPcomp document
1266 * says that we shouldn't compress small
1267 * packets. IPComp policy should always be
1268 * treated as being in "use" level.
1273 return (0); /* Valid. */
1277 ipsec46_in_reject(struct mbuf *m, struct inpcb *inp)
1279 struct secpolicy *sp;
1283 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1286 * Get SP for this packet.
1287 * When we are called from ip_forward(), we call
1288 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1291 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1293 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1296 result = ipsec_in_reject(sp, m);
1299 result = 0; /* XXX Should be panic?
1300 * -> No, there may be error. */
1306 * Check AH/ESP integrity.
1307 * This function is called from tcp_input(), udp_input(),
1308 * and {ah,esp}4_input for tunnel mode.
1311 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp)
1315 result = ipsec46_in_reject(m, inp);
1317 IPSECSTAT_INC(ips_in_polvio);
1324 * Check AH/ESP integrity.
1325 * This function is called from tcp6_input(), udp6_input(),
1326 * and {ah,esp}6_input for tunnel mode.
1329 ipsec6_in_reject(struct mbuf *m, struct inpcb *inp)
1333 result = ipsec46_in_reject(m, inp);
1335 IPSEC6STAT_INC(ips_in_polvio);
1342 * Compute the byte size to be occupied by IPsec header.
1343 * In case it is tunnelled, it includes the size of outer IP header.
1344 * NOTE: SP passed is freed in this function.
1347 ipsec_hdrsiz_internal(struct secpolicy *sp)
1349 struct ipsecrequest *isr;
1352 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1353 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1355 switch (sp->policy) {
1356 case IPSEC_POLICY_DISCARD:
1357 case IPSEC_POLICY_BYPASS:
1358 case IPSEC_POLICY_NONE:
1362 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1363 ("invalid policy %u", sp->policy));
1366 for (isr = sp->req; isr != NULL; isr = isr->next) {
1369 switch (isr->saidx.proto) {
1371 clen = esp_hdrsiz(isr->sav);
1374 clen = ah_hdrsiz(isr->sav);
1376 case IPPROTO_IPCOMP:
1377 clen = sizeof(struct ipcomp);
1381 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1382 switch (isr->saidx.dst.sa.sa_family) {
1384 clen += sizeof(struct ip);
1388 clen += sizeof(struct ip6_hdr);
1392 ipseclog((LOG_ERR, "%s: unknown AF %d in "
1393 "IPsec tunnel SA\n", __func__,
1394 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1405 * This function is called from ipsec_hdrsiz_tcp(), ip_ipsec_mtu(),
1406 * disabled ip6_ipsec_mtu() and ip6_forward().
1409 ipsec_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
1411 struct secpolicy *sp;
1415 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1417 /* Get SP for this packet.
1418 * When we are called from ip_forward(), we call
1419 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1422 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1424 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1427 size = ipsec_hdrsiz_internal(sp);
1428 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1429 printf("%s: size:%lu.\n", __func__,
1430 (unsigned long)size));
1434 size = 0; /* XXX Should be panic?
1435 * -> No, we are called w/o knowing if
1436 * IPsec processing is needed. */
1442 * Check the variable replay window.
1443 * ipsec_chkreplay() performs replay check before ICV verification.
1444 * ipsec_updatereplay() updates replay bitmap. This must be called after
1445 * ICV verification (it also performs replay check, which is usually done
1447 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1449 * Based on RFC 2401.
1452 ipsec_chkreplay(u_int32_t seq, struct secasvar *sav)
1454 const struct secreplay *replay;
1457 u_int32_t wsizeb; /* Constant: bits of window size. */
1458 int frlast; /* Constant: last frame. */
1460 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1461 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1463 replay = sav->replay;
1465 if (replay->wsize == 0)
1466 return (1); /* No need to check replay. */
1469 frlast = replay->wsize - 1;
1470 wsizeb = replay->wsize << 3;
1472 /* Sequence number of 0 is invalid. */
1476 /* First time is always okay. */
1477 if (replay->count == 0)
1480 if (seq > replay->lastseq) {
1481 /* Larger sequences are okay. */
1484 /* seq is equal or less than lastseq. */
1485 diff = replay->lastseq - seq;
1487 /* Over range to check, i.e. too old or wrapped. */
1491 fr = frlast - diff / 8;
1493 /* This packet already seen? */
1494 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1497 /* Out of order but good. */
1503 * Check replay counter whether to update or not.
1508 ipsec_updatereplay(u_int32_t seq, struct secasvar *sav)
1510 struct secreplay *replay;
1513 u_int32_t wsizeb; /* Constant: bits of window size. */
1514 int frlast; /* Constant: last frame. */
1516 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1517 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1519 replay = sav->replay;
1521 if (replay->wsize == 0)
1522 goto ok; /* No need to check replay. */
1525 frlast = replay->wsize - 1;
1526 wsizeb = replay->wsize << 3;
1528 /* Sequence number of 0 is invalid. */
1533 if (replay->count == 0) {
1534 replay->lastseq = seq;
1535 bzero(replay->bitmap, replay->wsize);
1536 (replay->bitmap)[frlast] = 1;
1540 if (seq > replay->lastseq) {
1541 /* seq is larger than lastseq. */
1542 diff = seq - replay->lastseq;
1544 /* New larger sequence number. */
1545 if (diff < wsizeb) {
1547 /* Set bit for this packet. */
1548 vshiftl(replay->bitmap, diff, replay->wsize);
1549 (replay->bitmap)[frlast] |= 1;
1551 /* This packet has a "way larger". */
1552 bzero(replay->bitmap, replay->wsize);
1553 (replay->bitmap)[frlast] = 1;
1555 replay->lastseq = seq;
1557 /* Larger is good. */
1559 /* seq is equal or less than lastseq. */
1560 diff = replay->lastseq - seq;
1562 /* Over range to check, i.e. too old or wrapped. */
1566 fr = frlast - diff / 8;
1568 /* This packet already seen? */
1569 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1573 (replay->bitmap)[fr] |= (1 << (diff % 8));
1575 /* Out of order but good. */
1579 if (replay->count == ~0) {
1581 /* Set overflow flag. */
1584 /* Don't increment, no more packets accepted. */
1585 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1588 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1589 __func__, replay->overflow, ipsec_logsastr(sav)));
1598 * Shift variable length buffer to left.
1599 * IN: bitmap: pointer to the buffer
1600 * nbit: the number of to shift.
1601 * wsize: buffer size (bytes).
1604 vshiftl(unsigned char *bitmap, int nbit, int wsize)
1609 for (j = 0; j < nbit; j += 8) {
1610 s = (nbit - j < 8) ? (nbit - j): 8;
1612 for (i = 1; i < wsize; i++) {
1613 over = (bitmap[i] >> (8 - s));
1615 bitmap[i-1] |= over;
1621 /* Return a printable string for the IPv4 address. */
1623 inet_ntoa4(struct in_addr ina)
1625 static char buf[4][4 * sizeof "123" + 4];
1626 unsigned char *ucp = (unsigned char *) &ina;
1629 /* XXX-BZ Returns static buffer. */
1631 sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff,
1632 ucp[2] & 0xff, ucp[3] & 0xff);
1637 /* Return a printable string for the address. */
1639 ipsec_address(union sockaddr_union* sa)
1642 char ip6buf[INET6_ADDRSTRLEN];
1645 switch (sa->sa.sa_family) {
1648 return (inet_ntoa4(sa->sin.sin_addr));
1652 return (ip6_sprintf(ip6buf, &sa->sin6.sin6_addr));
1655 return ("(unknown address family)");
1660 ipsec_logsastr(struct secasvar *sav)
1662 static char buf[256];
1664 struct secasindex *saidx = &sav->sah->saidx;
1666 IPSEC_ASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
1667 ("address family mismatch"));
1670 snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
1673 /* NB: only use ipsec_address on one address at a time. */
1674 snprintf(p, sizeof (buf) - (p - buf), "src=%s ",
1675 ipsec_address(&saidx->src));
1678 snprintf(p, sizeof (buf) - (p - buf), "dst=%s)",
1679 ipsec_address(&saidx->dst));
1685 ipsec_dumpmbuf(struct mbuf *m)
1694 p = mtod(m, u_char *);
1695 for (i = 0; i < m->m_len; i++) {
1696 printf("%02x ", p[i]);
1698 if (totlen % 16 == 0)
1703 if (totlen % 16 != 0)
1709 ipsec_init(const void *unused __unused)
1712 SECPOLICY_LOCK_INIT(&V_ip4_def_policy);
1713 V_ip4_def_policy.refcnt = 1; /* NB: disallow free. */
1715 VNET_SYSINIT(ipsec_init, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, ipsec_init,
1719 /* XXX This stuff doesn't belong here... */
1721 static struct xformsw* xforms = NULL;
1724 * Register a transform; typically at system startup.
1727 xform_register(struct xformsw* xsp)
1730 xsp->xf_next = xforms;
1735 * Initialize transform support in an sav.
1738 xform_init(struct secasvar *sav, int xftype)
1740 struct xformsw *xsp;
1742 if (sav->tdb_xform != NULL) /* Previously initialized. */
1744 for (xsp = xforms; xsp; xsp = xsp->xf_next)
1745 if (xsp->xf_type == xftype)
1746 return ((*xsp->xf_init)(sav, xsp));