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/if_var.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_PCPUSTAT_DEFINE(struct ipsecstat, ipsec4stat);
108 VNET_PCPUSTAT_SYSINIT(ipsec4stat);
111 VNET_PCPUSTAT_SYSUNINIT(ipsec4stat);
114 VNET_DEFINE(int, ip4_ah_offsetmask) = 0; /* maybe IP_DF? */
115 /* DF bit on encap. 0: clear 1: set 2: copy */
116 VNET_DEFINE(int, ip4_ipsec_dfbit) = 0;
117 VNET_DEFINE(int, ip4_esp_trans_deflev) = IPSEC_LEVEL_USE;
118 VNET_DEFINE(int, ip4_esp_net_deflev) = IPSEC_LEVEL_USE;
119 VNET_DEFINE(int, ip4_ah_trans_deflev) = IPSEC_LEVEL_USE;
120 VNET_DEFINE(int, ip4_ah_net_deflev) = IPSEC_LEVEL_USE;
121 /* ECN ignore(-1)/forbidden(0)/allowed(1) */
122 VNET_DEFINE(int, ip4_ipsec_ecn) = 0;
123 VNET_DEFINE(int, ip4_esp_randpad) = -1;
125 static VNET_DEFINE(struct secpolicy, def_policy);
126 #define V_def_policy VNET(def_policy)
128 * Crypto support requirements:
130 * 1 require hardware support
131 * -1 require software support
134 VNET_DEFINE(int, crypto_support) = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
136 FEATURE(ipsec, "Internet Protocol Security (IPsec)");
138 FEATURE(ipsec_natt, "UDP Encapsulation of IPsec ESP Packets ('NAT-T')");
141 SYSCTL_DECL(_net_inet_ipsec);
144 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY, def_policy,
145 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(def_policy).policy, 0,
146 "IPsec default policy.");
147 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
148 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_esp_trans_deflev), 0,
149 "Default ESP transport mode level");
150 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
151 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_esp_net_deflev), 0,
152 "Default ESP tunnel mode level.");
153 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
154 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_trans_deflev), 0,
155 "AH transfer mode default level.");
156 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
157 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_net_deflev), 0,
158 "AH tunnel mode default level.");
159 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS, ah_cleartos,
160 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0,
161 "If set clear type-of-service field when doing AH computation.");
162 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK, ah_offsetmask,
163 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ah_offsetmask), 0,
164 "If not set clear offset field mask when doing AH computation.");
165 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT, dfbit,
166 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_dfbit), 0,
167 "Do not fragment bit on encap.");
168 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN, ecn,
169 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip4_ipsec_ecn), 0,
170 "Explicit Congestion Notification handling.");
171 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG, debug,
172 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0,
173 "Enable IPsec debugging output when set.");
174 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, crypto_support,
175 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(crypto_support), 0,
176 "Crypto driver selection.");
177 SYSCTL_VNET_PCPUSTAT(_net_inet_ipsec, OID_AUTO, ipsecstats, struct ipsecstat,
178 ipsec4stat, "IPsec IPv4 statistics.");
182 * When set to 1, IPsec will send packets with the same sequence number.
183 * This allows to verify if the other side has proper replay attacks detection.
185 VNET_DEFINE(int, ipsec_replay) = 0;
186 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay,
187 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_replay), 0,
188 "Emulate replay attack");
190 * When set 1, IPsec will send packets with corrupted HMAC.
191 * This allows to verify if the other side properly detects modified packets.
193 VNET_DEFINE(int, ipsec_integrity) = 0;
194 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity,
195 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_integrity), 0,
196 "Emulate man-in-the-middle attack");
200 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec6stat);
201 VNET_PCPUSTAT_SYSINIT(ipsec6stat);
204 VNET_PCPUSTAT_SYSUNINIT(ipsec6stat);
207 VNET_DEFINE(int, ip6_esp_trans_deflev) = IPSEC_LEVEL_USE;
208 VNET_DEFINE(int, ip6_esp_net_deflev) = IPSEC_LEVEL_USE;
209 VNET_DEFINE(int, ip6_ah_trans_deflev) = IPSEC_LEVEL_USE;
210 VNET_DEFINE(int, ip6_ah_net_deflev) = IPSEC_LEVEL_USE;
211 VNET_DEFINE(int, ip6_ipsec_ecn) = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
213 SYSCTL_DECL(_net_inet6_ipsec6);
215 /* net.inet6.ipsec6 */
216 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY, def_policy,
217 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(def_policy).policy, 0,
218 "IPsec default policy.");
219 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
220 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_trans_deflev), 0,
221 "Default ESP transport mode level.");
222 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
223 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_esp_net_deflev), 0,
224 "Default ESP tunnel mode level.");
225 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
226 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_trans_deflev), 0,
227 "AH transfer mode default level.");
228 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
229 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ah_net_deflev), 0,
230 "AH tunnel mode default level.");
231 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN, ecn,
232 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_ipsec_ecn), 0,
233 "Explicit Congestion Notification handling.");
234 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG, debug,
235 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0,
236 "Enable IPsec debugging output when set.");
237 SYSCTL_VNET_PCPUSTAT(_net_inet6_ipsec6, IPSECCTL_STATS, ipsecstats,
238 struct ipsecstat, ipsec6stat, "IPsec IPv6 statistics.");
241 static int ipsec_in_reject(struct secpolicy *, struct mbuf *);
242 static int ipsec_setspidx_inpcb(struct mbuf *, struct inpcb *);
243 static int ipsec_setspidx(struct mbuf *, struct secpolicyindex *, int);
244 static void ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *, int);
245 static int ipsec4_setspidx_ipaddr(struct mbuf *, struct secpolicyindex *);
247 static void ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *, int);
248 static int ipsec6_setspidx_ipaddr(struct mbuf *, struct secpolicyindex *);
250 static void ipsec_delpcbpolicy(struct inpcbpolicy *);
251 static struct secpolicy *ipsec_deepcopy_policy(struct secpolicy *src);
252 static void vshiftl(unsigned char *, int, int);
254 MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy");
257 * Return a held reference to the default SP.
259 static struct secpolicy *
260 key_allocsp_default(const char* where, int tag)
262 struct secpolicy *sp;
264 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
265 printf("DP key_allocsp_default from %s:%u\n", where, tag));
268 if (sp->policy != IPSEC_POLICY_DISCARD &&
269 sp->policy != IPSEC_POLICY_NONE) {
270 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
271 sp->policy, IPSEC_POLICY_NONE));
272 sp->policy = IPSEC_POLICY_NONE;
276 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
277 printf("DP key_allocsp_default returns SP:%p (%u)\n",
281 #define KEY_ALLOCSP_DEFAULT() \
282 key_allocsp_default(__FILE__, __LINE__)
285 * For OUTBOUND packet having a socket. Searching SPD for packet,
286 * and return a pointer to SP.
287 * OUT: NULL: no apropreate SP found, the following value is set to error.
289 * EACCES : discard packet.
290 * ENOENT : ipsec_acquire() in progress, maybe.
291 * others : error occured.
292 * others: a pointer to SP
294 * NOTE: IPv6 mapped adddress concern is implemented here.
297 ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
299 struct secpolicy *sp;
301 IPSEC_ASSERT(tdbi != NULL, ("null tdbi"));
302 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
303 ("invalid direction %u", dir));
305 sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
306 if (sp == NULL) /*XXX????*/
307 sp = KEY_ALLOCSP_DEFAULT();
308 IPSEC_ASSERT(sp != NULL, ("null SP"));
313 * For OUTBOUND packet having a socket. Searching SPD for packet,
314 * and return a pointer to SP.
315 * OUT: NULL: no apropreate SP found, the following value is set to error.
317 * EACCES : discard packet.
318 * ENOENT : ipsec_acquire() in progress, maybe.
319 * others : error occured.
320 * others: a pointer to SP
322 * NOTE: IPv6 mapped adddress concern is implemented here.
324 static struct secpolicy *
325 ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb *inp, int *error)
327 struct inpcbpolicy *pcbsp;
328 struct secpolicy *currsp = NULL; /* Policy on socket. */
329 struct secpolicy *sp;
331 IPSEC_ASSERT(m != NULL, ("null mbuf"));
332 IPSEC_ASSERT(inp != NULL, ("null inpcb"));
333 IPSEC_ASSERT(error != NULL, ("null error"));
334 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
335 ("invalid direction %u", dir));
337 if (!key_havesp(dir)) {
338 /* No SP found, use system default. */
339 sp = KEY_ALLOCSP_DEFAULT();
343 /* Set spidx in pcb. */
344 *error = ipsec_setspidx_inpcb(m, inp);
349 IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
351 case IPSEC_DIR_INBOUND:
352 currsp = pcbsp->sp_in;
354 case IPSEC_DIR_OUTBOUND:
355 currsp = pcbsp->sp_out;
358 IPSEC_ASSERT(currsp != NULL, ("null currsp"));
360 if (pcbsp->priv) { /* When privilieged socket. */
361 switch (currsp->policy) {
362 case IPSEC_POLICY_BYPASS:
363 case IPSEC_POLICY_IPSEC:
368 case IPSEC_POLICY_ENTRUST:
369 /* Look for a policy in SPD. */
370 sp = KEY_ALLOCSP(&currsp->spidx, dir);
371 if (sp == NULL) /* No SP found. */
372 sp = KEY_ALLOCSP_DEFAULT();
376 ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
377 __func__, currsp->policy));
381 } else { /* Unpriv, SPD has policy. */
382 sp = KEY_ALLOCSP(&currsp->spidx, dir);
383 if (sp == NULL) { /* No SP found. */
384 switch (currsp->policy) {
385 case IPSEC_POLICY_BYPASS:
386 ipseclog((LOG_ERR, "%s: Illegal policy for "
387 "non-priviliged defined %d\n",
388 __func__, currsp->policy));
392 case IPSEC_POLICY_ENTRUST:
393 sp = KEY_ALLOCSP_DEFAULT();
396 case IPSEC_POLICY_IPSEC:
402 ipseclog((LOG_ERR, "%s: Invalid policy for "
403 "PCB %d\n", __func__, currsp->policy));
409 IPSEC_ASSERT(sp != NULL,
410 ("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
411 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
412 printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
413 __func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
418 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
419 * and return a pointer to SP.
420 * OUT: positive: a pointer to the entry for security policy leaf matched.
421 * NULL: no apropreate SP found, the following value is set to error.
423 * EACCES : discard packet.
424 * ENOENT : ipsec_acquire() in progress, maybe.
425 * others : error occured.
428 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int *error)
430 struct secpolicyindex spidx;
431 struct secpolicy *sp;
433 IPSEC_ASSERT(m != NULL, ("null mbuf"));
434 IPSEC_ASSERT(error != NULL, ("null error"));
435 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
436 ("invalid direction %u", dir));
440 if (key_havesp(dir)) {
441 /* Make an index to look for a policy. */
442 *error = ipsec_setspidx(m, &spidx, 0);
444 DPRINTF(("%s: setpidx failed, dir %u\n",
449 sp = KEY_ALLOCSP(&spidx, dir);
451 if (sp == NULL) /* No SP found, use system default. */
452 sp = KEY_ALLOCSP_DEFAULT();
453 IPSEC_ASSERT(sp != NULL, ("null SP"));
458 ipsec4_checkpolicy(struct mbuf *m, u_int dir, int *error, struct inpcb *inp)
460 struct secpolicy *sp;
464 sp = ipsec_getpolicybyaddr(m, dir, error);
466 sp = ipsec_getpolicybysock(m, dir, inp, error);
468 IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
469 IPSECSTAT_INC(ips_out_inval);
472 IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
473 switch (sp->policy) {
474 case IPSEC_POLICY_ENTRUST:
476 printf("%s: invalid policy %u\n", __func__, sp->policy);
478 case IPSEC_POLICY_DISCARD:
479 IPSECSTAT_INC(ips_out_polvio);
480 *error = -EINVAL; /* Packet is discarded by caller. */
482 case IPSEC_POLICY_BYPASS:
483 case IPSEC_POLICY_NONE:
485 sp = NULL; /* NB: force NULL result. */
487 case IPSEC_POLICY_IPSEC:
488 if (sp->req == NULL) /* Acquire a SA. */
489 *error = key_spdacquire(sp);
500 ipsec_setspidx_inpcb(struct mbuf *m, struct inpcb *inp)
504 IPSEC_ASSERT(inp != NULL, ("null inp"));
505 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
506 IPSEC_ASSERT(inp->inp_sp->sp_out != NULL && inp->inp_sp->sp_in != NULL,
507 ("null sp_in || sp_out"));
509 error = ipsec_setspidx(m, &inp->inp_sp->sp_in->spidx, 1);
511 inp->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
512 inp->inp_sp->sp_out->spidx = inp->inp_sp->sp_in->spidx;
513 inp->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
515 bzero(&inp->inp_sp->sp_in->spidx,
516 sizeof (inp->inp_sp->sp_in->spidx));
517 bzero(&inp->inp_sp->sp_out->spidx,
518 sizeof (inp->inp_sp->sp_in->spidx));
524 * Configure security policy index (src/dst/proto/sport/dport)
525 * by looking at the content of mbuf.
526 * The caller is responsible for error recovery (like clearing up spidx).
529 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
531 struct ip *ip = NULL;
538 IPSEC_ASSERT(m != NULL, ("null mbuf"));
541 * Validate m->m_pkthdr.len. We see incorrect length if we
542 * mistakenly call this function with inconsistent mbuf chain
543 * (like 4.4BSD tcp/udp processing). XXX Should we panic here?
546 for (n = m; n; n = n->m_next)
548 if (m->m_pkthdr.len != len) {
549 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
550 printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
551 __func__, len, m->m_pkthdr.len));
555 if (m->m_pkthdr.len < sizeof(struct ip)) {
556 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
557 printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
558 __func__, m->m_pkthdr.len));
562 if (m->m_len >= sizeof(*ip))
563 ip = mtod(m, struct ip *);
565 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
571 error = ipsec4_setspidx_ipaddr(m, spidx);
574 ipsec4_get_ulp(m, spidx, needport);
578 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
579 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
580 printf("%s: pkthdr len(%d) too small (v6), "
581 "ignored\n", __func__, m->m_pkthdr.len));
584 error = ipsec6_setspidx_ipaddr(m, spidx);
587 ipsec6_get_ulp(m, spidx, needport);
591 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
592 printf("%s: " "unknown IP version %u, ignored.\n",
599 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
605 IPSEC_ASSERT(m != NULL, ("null mbuf"));
606 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
608 if (m->m_len >= sizeof (struct ip)) {
609 struct ip *ip = mtod(m, struct ip *);
610 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
612 off = ip->ip_hl << 2;
617 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
618 if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
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->policy = IPSEC_POLICY_ENTRUST;
840 if ((new->sp_out = KEY_NEWSP()) == NULL) {
841 KEY_FREESP(&new->sp_in);
842 ipsec_delpcbpolicy(new);
845 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
851 /* Copy old IPsec policy into new. */
853 ipsec_copy_policy(struct inpcbpolicy *old, struct inpcbpolicy *new)
855 struct secpolicy *sp;
857 sp = ipsec_deepcopy_policy(old->sp_in);
859 KEY_FREESP(&new->sp_in);
864 sp = ipsec_deepcopy_policy(old->sp_out);
866 KEY_FREESP(&new->sp_out);
871 new->priv = old->priv;
876 struct ipsecrequest *
879 struct ipsecrequest *p;
881 p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
883 IPSECREQUEST_LOCK_INIT(p);
888 ipsec_delisr(struct ipsecrequest *p)
891 IPSECREQUEST_LOCK_DESTROY(p);
895 /* Deep-copy a policy in PCB. */
896 static struct secpolicy *
897 ipsec_deepcopy_policy(struct secpolicy *src)
899 struct ipsecrequest *newchain = NULL;
900 struct ipsecrequest *p;
901 struct ipsecrequest **q;
902 struct ipsecrequest *r;
903 struct secpolicy *dst;
912 * Deep-copy IPsec request chain. This is required since struct
913 * ipsecrequest is not reference counted.
916 for (p = src->req; p; p = p->next) {
920 (*q)->saidx.proto = p->saidx.proto;
921 (*q)->saidx.mode = p->saidx.mode;
922 (*q)->level = p->level;
923 (*q)->saidx.reqid = p->saidx.reqid;
925 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
926 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
934 dst->policy = src->policy;
935 /* Do not touch the refcnt fields. */
940 for (p = newchain; p; p = r) {
948 /* Set policy and IPsec request if present. */
950 ipsec_set_policy_internal(struct secpolicy **pcb_sp, int optname,
951 caddr_t request, size_t len, struct ucred *cred)
953 struct sadb_x_policy *xpl;
954 struct secpolicy *newsp = NULL;
958 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
960 if (len < sizeof(*xpl))
962 xpl = (struct sadb_x_policy *)request;
964 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
965 printf("%s: passed policy\n", __func__);
966 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
968 /* Check policy type. */
969 /* ipsec_set_policy_internal() accepts IPSEC, ENTRUST and BYPASS. */
970 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
971 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
974 /* Check privileged socket. */
975 if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
976 error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0);
981 /* Allocating new SP entry. */
982 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
985 /* Clear old SP and set new SP. */
988 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
989 printf("%s: new policy\n", __func__);
990 kdebug_secpolicy(newsp));
996 ipsec_set_policy(struct inpcb *inp, int optname, caddr_t request,
997 size_t len, struct ucred *cred)
999 struct sadb_x_policy *xpl;
1000 struct secpolicy **pcb_sp;
1003 if (inp == NULL || request == NULL)
1005 if (len < sizeof(*xpl))
1007 xpl = (struct sadb_x_policy *)request;
1009 /* Select direction. */
1010 switch (xpl->sadb_x_policy_dir) {
1011 case IPSEC_DIR_INBOUND:
1012 pcb_sp = &inp->inp_sp->sp_in;
1014 case IPSEC_DIR_OUTBOUND:
1015 pcb_sp = &inp->inp_sp->sp_out;
1018 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1019 xpl->sadb_x_policy_dir));
1023 return (ipsec_set_policy_internal(pcb_sp, optname, request, len, cred));
1027 ipsec_get_policy(struct inpcb *inp, caddr_t request, size_t len,
1030 struct sadb_x_policy *xpl;
1031 struct secpolicy *pcb_sp;
1034 if (inp == NULL || request == NULL || mp == NULL)
1036 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
1037 if (len < sizeof(*xpl))
1039 xpl = (struct sadb_x_policy *)request;
1041 /* Select direction. */
1042 switch (xpl->sadb_x_policy_dir) {
1043 case IPSEC_DIR_INBOUND:
1044 pcb_sp = inp->inp_sp->sp_in;
1046 case IPSEC_DIR_OUTBOUND:
1047 pcb_sp = inp->inp_sp->sp_out;
1050 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1051 xpl->sadb_x_policy_dir));
1055 /* Sanity check. Should be an IPSEC_ASSERT. */
1059 *mp = key_sp2msg(pcb_sp);
1061 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
1065 (*mp)->m_type = MT_DATA;
1066 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1067 printf("%s:\n", __func__); kdebug_mbuf(*mp));
1072 /* Delete policy in PCB. */
1074 ipsec_delete_pcbpolicy(struct inpcb *inp)
1076 IPSEC_ASSERT(inp != NULL, ("null inp"));
1078 if (inp->inp_sp == NULL)
1081 if (inp->inp_sp->sp_in != NULL)
1082 KEY_FREESP(&inp->inp_sp->sp_in);
1084 if (inp->inp_sp->sp_out != NULL)
1085 KEY_FREESP(&inp->inp_sp->sp_out);
1087 ipsec_delpcbpolicy(inp->inp_sp);
1094 * Return current level.
1095 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1098 ipsec_get_reqlevel(struct ipsecrequest *isr)
1101 u_int esp_trans_deflev, esp_net_deflev;
1102 u_int ah_trans_deflev, ah_net_deflev;
1104 IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
1105 IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1106 ("af family mismatch, src %u, dst %u",
1107 isr->sp->spidx.src.sa.sa_family,
1108 isr->sp->spidx.dst.sa.sa_family));
1110 /* XXX Note that we have ipseclog() expanded here - code sync issue. */
1111 #define IPSEC_CHECK_DEFAULT(lev) \
1112 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1113 && (lev) != IPSEC_LEVEL_UNIQUE) \
1115 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1116 (lev), IPSEC_LEVEL_REQUIRE) \
1118 (lev) = IPSEC_LEVEL_REQUIRE, \
1122 /* Set default level. */
1123 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1126 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev);
1127 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev);
1128 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev);
1129 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev);
1134 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev);
1135 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev);
1136 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev);
1137 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev);
1141 panic("%s: unknown af %u",
1142 __func__, isr->sp->spidx.src.sa.sa_family);
1145 #undef IPSEC_CHECK_DEFAULT
1148 switch (isr->level) {
1149 case IPSEC_LEVEL_DEFAULT:
1150 switch (isr->saidx.proto) {
1152 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1153 level = esp_net_deflev;
1155 level = esp_trans_deflev;
1158 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1159 level = ah_net_deflev;
1161 level = ah_trans_deflev;
1163 case IPPROTO_IPCOMP:
1165 * We don't really care, as IPcomp document says that
1166 * we shouldn't compress small packets.
1168 level = IPSEC_LEVEL_USE;
1171 panic("%s: Illegal protocol defined %u\n", __func__,
1176 case IPSEC_LEVEL_USE:
1177 case IPSEC_LEVEL_REQUIRE:
1180 case IPSEC_LEVEL_UNIQUE:
1181 level = IPSEC_LEVEL_REQUIRE;
1185 panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
1192 * Check security policy requirements against the actual
1193 * packet contents. Return one if the packet should be
1194 * reject as "invalid"; otherwiser return zero to have the
1195 * packet treated as "valid".
1202 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1204 struct ipsecrequest *isr;
1207 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1208 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1211 switch (sp->policy) {
1212 case IPSEC_POLICY_DISCARD:
1214 case IPSEC_POLICY_BYPASS:
1215 case IPSEC_POLICY_NONE:
1219 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1220 ("invalid policy %u", sp->policy));
1222 /* XXX Should compare policy against IPsec header history. */
1225 for (isr = sp->req; isr != NULL; isr = isr->next) {
1226 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1228 switch (isr->saidx.proto) {
1230 if ((m->m_flags & M_DECRYPTED) == 0) {
1231 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1232 printf("%s: ESP m_flags:%x\n", __func__,
1239 isr->sav->tdb_authalgxform != NULL &&
1240 (m->m_flags & M_AUTHIPDGM) == 0) {
1241 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1242 printf("%s: ESP/AH m_flags:%x\n", __func__,
1249 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1250 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1251 printf("%s: AH m_flags:%x\n", __func__,
1256 case IPPROTO_IPCOMP:
1258 * We don't really care, as IPcomp document
1259 * says that we shouldn't compress small
1260 * packets. IPComp policy should always be
1261 * treated as being in "use" level.
1266 return (0); /* Valid. */
1270 * Non zero return value means security policy DISCARD or policy violation.
1273 ipsec46_in_reject(struct mbuf *m, struct inpcb *inp)
1275 struct secpolicy *sp;
1279 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1281 /* Get SP for this packet. */
1283 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, &error);
1285 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1288 result = ipsec_in_reject(sp, m);
1291 result = 1; /* treat errors as policy violation */
1297 * Check AH/ESP integrity.
1298 * This function is called from tcp_input(), udp_input(),
1299 * and {ah,esp}4_input for tunnel mode.
1302 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp)
1306 result = ipsec46_in_reject(m, inp);
1308 IPSECSTAT_INC(ips_in_polvio);
1315 * Check AH/ESP integrity.
1316 * This function is called from tcp6_input(), udp6_input(),
1317 * and {ah,esp}6_input for tunnel mode.
1320 ipsec6_in_reject(struct mbuf *m, struct inpcb *inp)
1324 result = ipsec46_in_reject(m, inp);
1326 IPSEC6STAT_INC(ips_in_polvio);
1333 * Compute the byte size to be occupied by IPsec header.
1334 * In case it is tunnelled, it includes the size of outer IP header.
1335 * NOTE: SP passed is freed in this function.
1338 ipsec_hdrsiz_internal(struct secpolicy *sp)
1340 struct ipsecrequest *isr;
1343 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1344 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1346 switch (sp->policy) {
1347 case IPSEC_POLICY_DISCARD:
1348 case IPSEC_POLICY_BYPASS:
1349 case IPSEC_POLICY_NONE:
1353 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1354 ("invalid policy %u", sp->policy));
1357 for (isr = sp->req; isr != NULL; isr = isr->next) {
1360 switch (isr->saidx.proto) {
1362 clen = esp_hdrsiz(isr->sav);
1365 clen = ah_hdrsiz(isr->sav);
1367 case IPPROTO_IPCOMP:
1368 clen = sizeof(struct ipcomp);
1372 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1373 switch (isr->saidx.dst.sa.sa_family) {
1375 clen += sizeof(struct ip);
1379 clen += sizeof(struct ip6_hdr);
1383 ipseclog((LOG_ERR, "%s: unknown AF %d in "
1384 "IPsec tunnel SA\n", __func__,
1385 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1396 * This function is called from ipsec_hdrsiz_tcp(), ip_ipsec_mtu(),
1397 * disabled ip6_ipsec_mtu() and ip6_forward().
1400 ipsec_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
1402 struct secpolicy *sp;
1406 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1408 /* Get SP for this packet. */
1410 sp = ipsec_getpolicybyaddr(m, dir, &error);
1412 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1415 size = ipsec_hdrsiz_internal(sp);
1416 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1417 printf("%s: size:%lu.\n", __func__,
1418 (unsigned long)size));
1422 size = 0; /* XXX Should be panic?
1423 * -> No, we are called w/o knowing if
1424 * IPsec processing is needed. */
1430 * Check the variable replay window.
1431 * ipsec_chkreplay() performs replay check before ICV verification.
1432 * ipsec_updatereplay() updates replay bitmap. This must be called after
1433 * ICV verification (it also performs replay check, which is usually done
1435 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1437 * Based on RFC 2401.
1440 ipsec_chkreplay(u_int32_t seq, struct secasvar *sav)
1442 const struct secreplay *replay;
1445 u_int32_t wsizeb; /* Constant: bits of window size. */
1446 int frlast; /* Constant: last frame. */
1448 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1449 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1451 replay = sav->replay;
1453 if (replay->wsize == 0)
1454 return (1); /* No need to check replay. */
1457 frlast = replay->wsize - 1;
1458 wsizeb = replay->wsize << 3;
1460 /* Sequence number of 0 is invalid. */
1464 /* First time is always okay. */
1465 if (replay->count == 0)
1468 if (seq > replay->lastseq) {
1469 /* Larger sequences are okay. */
1472 /* seq is equal or less than lastseq. */
1473 diff = replay->lastseq - seq;
1475 /* Over range to check, i.e. too old or wrapped. */
1479 fr = frlast - diff / 8;
1481 /* This packet already seen? */
1482 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1485 /* Out of order but good. */
1491 * Check replay counter whether to update or not.
1496 ipsec_updatereplay(u_int32_t seq, struct secasvar *sav)
1499 struct secreplay *replay;
1502 u_int32_t wsizeb; /* Constant: bits of window size. */
1503 int frlast; /* Constant: last frame. */
1505 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1506 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1508 replay = sav->replay;
1510 if (replay->wsize == 0)
1511 goto ok; /* No need to check replay. */
1514 frlast = replay->wsize - 1;
1515 wsizeb = replay->wsize << 3;
1517 /* Sequence number of 0 is invalid. */
1522 if (replay->count == 0) {
1523 replay->lastseq = seq;
1524 bzero(replay->bitmap, replay->wsize);
1525 (replay->bitmap)[frlast] = 1;
1529 if (seq > replay->lastseq) {
1530 /* seq is larger than lastseq. */
1531 diff = seq - replay->lastseq;
1533 /* New larger sequence number. */
1534 if (diff < wsizeb) {
1536 /* Set bit for this packet. */
1537 vshiftl(replay->bitmap, diff, replay->wsize);
1538 (replay->bitmap)[frlast] |= 1;
1540 /* This packet has a "way larger". */
1541 bzero(replay->bitmap, replay->wsize);
1542 (replay->bitmap)[frlast] = 1;
1544 replay->lastseq = seq;
1546 /* Larger is good. */
1548 /* seq is equal or less than lastseq. */
1549 diff = replay->lastseq - seq;
1551 /* Over range to check, i.e. too old or wrapped. */
1555 fr = frlast - diff / 8;
1557 /* This packet already seen? */
1558 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1562 (replay->bitmap)[fr] |= (1 << (diff % 8));
1564 /* Out of order but good. */
1568 if (replay->count == ~0) {
1570 /* Set overflow flag. */
1573 /* Don't increment, no more packets accepted. */
1574 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1577 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1578 __func__, replay->overflow,
1579 ipsec_logsastr(sav, buf, sizeof(buf))));
1588 * Shift variable length buffer to left.
1589 * IN: bitmap: pointer to the buffer
1590 * nbit: the number of to shift.
1591 * wsize: buffer size (bytes).
1594 vshiftl(unsigned char *bitmap, int nbit, int wsize)
1599 for (j = 0; j < nbit; j += 8) {
1600 s = (nbit - j < 8) ? (nbit - j): 8;
1602 for (i = 1; i < wsize; i++) {
1603 over = (bitmap[i] >> (8 - s));
1605 bitmap[i-1] |= over;
1610 /* Return a printable string for the address. */
1612 ipsec_address(union sockaddr_union* sa, char *buf, socklen_t size)
1615 switch (sa->sa.sa_family) {
1618 return (inet_ntop(AF_INET, &sa->sin.sin_addr, buf, size));
1622 return (inet_ntop(AF_INET6, &sa->sin6.sin6_addr, buf, size));
1625 return ("(unknown address family)");
1630 ipsec_logsastr(struct secasvar *sav, char *buf, size_t size)
1632 char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
1634 IPSEC_ASSERT(sav->sah->saidx.src.sa.sa_family ==
1635 sav->sah->saidx.dst.sa.sa_family, ("address family mismatch"));
1637 snprintf(buf, size, "SA(SPI=%08lx src=%s dst=%s)",
1638 (u_long)ntohl(sav->spi),
1639 ipsec_address(&sav->sah->saidx.src, sbuf, sizeof(sbuf)),
1640 ipsec_address(&sav->sah->saidx.dst, dbuf, sizeof(dbuf)));
1645 ipsec_dumpmbuf(struct mbuf *m)
1654 p = mtod(m, u_char *);
1655 for (i = 0; i < m->m_len; i++) {
1656 printf("%02x ", p[i]);
1658 if (totlen % 16 == 0)
1663 if (totlen % 16 != 0)
1669 def_policy_init(const void *unused __unused)
1672 bzero(&V_def_policy, sizeof(struct secpolicy));
1673 V_def_policy.policy = IPSEC_POLICY_NONE;
1674 V_def_policy.refcnt = 1;
1676 VNET_SYSINIT(def_policy_init, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY,
1677 def_policy_init, NULL);
1680 /* XXX This stuff doesn't belong here... */
1682 static struct xformsw* xforms = NULL;
1685 * Register a transform; typically at system startup.
1688 xform_register(struct xformsw* xsp)
1691 xsp->xf_next = xforms;
1696 * Initialize transform support in an sav.
1699 xform_init(struct secasvar *sav, int xftype)
1701 struct xformsw *xsp;
1703 if (sav->tdb_xform != NULL) /* Previously initialized. */
1705 for (xsp = xforms; xsp; xsp = xsp->xf_next)
1706 if (xsp->xf_type == xftype)
1707 return ((*xsp->xf_init)(sav, xsp));