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 *);
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);
254 static void vshiftl(unsigned char *, int, int);
256 MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy");
259 * Return a held reference to the default SP.
261 static struct secpolicy *
262 key_allocsp_default(const char* where, int tag)
264 struct secpolicy *sp;
266 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
267 printf("DP key_allocsp_default from %s:%u\n", where, tag));
270 if (sp->policy != IPSEC_POLICY_DISCARD &&
271 sp->policy != IPSEC_POLICY_NONE) {
272 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
273 sp->policy, IPSEC_POLICY_NONE));
274 sp->policy = IPSEC_POLICY_NONE;
278 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
279 printf("DP key_allocsp_default returns SP:%p (%u)\n",
283 #define KEY_ALLOCSP_DEFAULT() \
284 key_allocsp_default(__FILE__, __LINE__)
287 * For OUTBOUND packet having a socket. Searching SPD for packet,
288 * and return a pointer to SP.
289 * OUT: NULL: no apropreate SP found, the following value is set to error.
291 * EACCES : discard packet.
292 * ENOENT : ipsec_acquire() in progress, maybe.
293 * others : error occurred.
294 * others: a pointer to SP
296 * NOTE: IPv6 mapped adddress concern is implemented here.
299 ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
301 struct secpolicy *sp;
303 IPSEC_ASSERT(tdbi != NULL, ("null tdbi"));
304 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
305 ("invalid direction %u", dir));
307 sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
308 if (sp == NULL) /*XXX????*/
309 sp = KEY_ALLOCSP_DEFAULT();
310 IPSEC_ASSERT(sp != NULL, ("null SP"));
315 * For OUTBOUND packet having a socket. Searching SPD for packet,
316 * and return a pointer to SP.
317 * OUT: NULL: no apropreate SP found, the following value is set to error.
319 * EACCES : discard packet.
320 * ENOENT : ipsec_acquire() in progress, maybe.
321 * others : error occurred.
322 * others: a pointer to SP
324 * NOTE: IPv6 mapped adddress concern is implemented here.
326 static struct secpolicy *
327 ipsec_getpolicybysock(const struct mbuf *m, u_int dir, struct inpcb *inp,
330 struct inpcbpolicy *pcbsp;
331 struct secpolicy *currsp = NULL; /* Policy on socket. */
332 struct secpolicy *sp;
334 IPSEC_ASSERT(m != NULL, ("null mbuf"));
335 IPSEC_ASSERT(inp != NULL, ("null inpcb"));
336 IPSEC_ASSERT(error != NULL, ("null error"));
337 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
338 ("invalid direction %u", dir));
340 if (!key_havesp(dir)) {
341 /* No SP found, use system default. */
342 sp = KEY_ALLOCSP_DEFAULT();
346 /* Set spidx in pcb. */
347 *error = ipsec_setspidx_inpcb(m, inp);
352 IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
354 case IPSEC_DIR_INBOUND:
355 currsp = pcbsp->sp_in;
357 case IPSEC_DIR_OUTBOUND:
358 currsp = pcbsp->sp_out;
361 IPSEC_ASSERT(currsp != NULL, ("null currsp"));
363 if (pcbsp->priv) { /* When privilieged socket. */
364 switch (currsp->policy) {
365 case IPSEC_POLICY_BYPASS:
366 case IPSEC_POLICY_IPSEC:
371 case IPSEC_POLICY_ENTRUST:
372 /* Look for a policy in SPD. */
373 sp = KEY_ALLOCSP(&currsp->spidx, dir);
374 if (sp == NULL) /* No SP found. */
375 sp = KEY_ALLOCSP_DEFAULT();
379 ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
380 __func__, currsp->policy));
384 } else { /* Unpriv, SPD has policy. */
385 sp = KEY_ALLOCSP(&currsp->spidx, dir);
386 if (sp == NULL) { /* No SP found. */
387 switch (currsp->policy) {
388 case IPSEC_POLICY_BYPASS:
389 ipseclog((LOG_ERR, "%s: Illegal policy for "
390 "non-priviliged defined %d\n",
391 __func__, currsp->policy));
395 case IPSEC_POLICY_ENTRUST:
396 sp = KEY_ALLOCSP_DEFAULT();
399 case IPSEC_POLICY_IPSEC:
405 ipseclog((LOG_ERR, "%s: Invalid policy for "
406 "PCB %d\n", __func__, currsp->policy));
412 IPSEC_ASSERT(sp != NULL,
413 ("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
414 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
415 printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
416 __func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
421 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
422 * and return a pointer to SP.
423 * OUT: positive: a pointer to the entry for security policy leaf matched.
424 * NULL: no apropreate SP found, the following value is set to error.
426 * EACCES : discard packet.
427 * ENOENT : ipsec_acquire() in progress, maybe.
428 * others : error occurred.
431 ipsec_getpolicybyaddr(const struct mbuf *m, u_int dir, int *error)
433 struct secpolicyindex spidx;
434 struct secpolicy *sp;
436 IPSEC_ASSERT(m != NULL, ("null mbuf"));
437 IPSEC_ASSERT(error != NULL, ("null error"));
438 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
439 ("invalid direction %u", dir));
443 if (key_havesp(dir)) {
444 /* Make an index to look for a policy. */
445 *error = ipsec_setspidx(m, &spidx, 0);
447 DPRINTF(("%s: setpidx failed, dir %u\n",
452 sp = KEY_ALLOCSP(&spidx, dir);
454 if (sp == NULL) /* No SP found, use system default. */
455 sp = KEY_ALLOCSP_DEFAULT();
456 IPSEC_ASSERT(sp != NULL, ("null SP"));
461 ipsec4_checkpolicy(const struct mbuf *m, u_int dir, int *error,
464 struct secpolicy *sp;
468 sp = ipsec_getpolicybyaddr(m, dir, error);
470 sp = ipsec_getpolicybysock(m, dir, inp, error);
472 IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
473 IPSECSTAT_INC(ips_out_inval);
476 IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
477 switch (sp->policy) {
478 case IPSEC_POLICY_ENTRUST:
480 printf("%s: invalid policy %u\n", __func__, sp->policy);
482 case IPSEC_POLICY_DISCARD:
483 IPSECSTAT_INC(ips_out_polvio);
484 *error = -EINVAL; /* Packet is discarded by caller. */
486 case IPSEC_POLICY_BYPASS:
487 case IPSEC_POLICY_NONE:
489 sp = NULL; /* NB: force NULL result. */
491 case IPSEC_POLICY_IPSEC:
492 if (sp->req == NULL) /* Acquire a SA. */
493 *error = key_spdacquire(sp);
504 ipsec_setspidx_inpcb(const struct mbuf *m, struct inpcb *inp)
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 error = ipsec_setspidx(m, &inp->inp_sp->sp_in->spidx, 1);
515 inp->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
516 inp->inp_sp->sp_out->spidx = inp->inp_sp->sp_in->spidx;
517 inp->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
519 bzero(&inp->inp_sp->sp_in->spidx,
520 sizeof (inp->inp_sp->sp_in->spidx));
521 bzero(&inp->inp_sp->sp_out->spidx,
522 sizeof (inp->inp_sp->sp_in->spidx));
528 * Configure security policy index (src/dst/proto/sport/dport)
529 * by looking at the content of mbuf.
530 * The caller is responsible for error recovery (like clearing up spidx).
533 ipsec_setspidx(const struct mbuf *m, struct secpolicyindex *spidx,
537 const struct ip *ip = NULL;
538 const struct mbuf *n;
543 IPSEC_ASSERT(m != NULL, ("null mbuf"));
546 * Validate m->m_pkthdr.len. We see incorrect length if we
547 * mistakenly call this function with inconsistent mbuf chain
548 * (like 4.4BSD tcp/udp processing). XXX Should we panic here?
551 for (n = m; n; n = n->m_next)
553 if (m->m_pkthdr.len != len) {
554 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
555 printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
556 __func__, len, m->m_pkthdr.len));
560 if (m->m_pkthdr.len < sizeof(struct ip)) {
561 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
562 printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
563 __func__, m->m_pkthdr.len));
567 if (m->m_len >= sizeof(*ip))
568 ip = mtod(m, const struct ip *);
570 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
576 error = ipsec4_setspidx_ipaddr(m, spidx);
579 ipsec4_get_ulp(m, spidx, needport);
583 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
584 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
585 printf("%s: pkthdr len(%d) too small (v6), "
586 "ignored\n", __func__, m->m_pkthdr.len));
589 error = ipsec6_setspidx_ipaddr(m, spidx);
592 ipsec6_get_ulp(m, spidx, needport);
596 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
597 printf("%s: " "unknown IP version %u, ignored.\n",
604 ipsec4_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx,
611 IPSEC_ASSERT(m != NULL, ("null mbuf"));
612 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
614 if (m->m_len >= sizeof (struct ip)) {
615 const struct ip *ip = mtod(m, const struct ip *);
616 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
618 off = ip->ip_hl << 2;
623 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
624 if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
630 while (off < m->m_pkthdr.len) {
637 spidx->ul_proto = nxt;
640 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
642 m_copydata(m, off, sizeof (th), (caddr_t) &th);
643 spidx->src.sin.sin_port = th.th_sport;
644 spidx->dst.sin.sin_port = th.th_dport;
647 spidx->ul_proto = nxt;
650 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
652 m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
653 spidx->src.sin.sin_port = uh.uh_sport;
654 spidx->dst.sin.sin_port = uh.uh_dport;
657 if (off + sizeof(ip6e) > m->m_pkthdr.len)
659 /* XXX Sigh, this works but is totally bogus. */
660 m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
661 off += (ip6e.ip6e_len + 2) << 2;
666 /* XXX Intermediate headers??? */
667 spidx->ul_proto = nxt;
672 spidx->ul_proto = IPSEC_ULPROTO_ANY;
674 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
675 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
678 /* Assumes that m is sane. */
680 ipsec4_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
682 static const struct sockaddr_in template = {
683 sizeof (struct sockaddr_in),
685 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
688 spidx->src.sin = template;
689 spidx->dst.sin = template;
691 if (m->m_len < sizeof (struct ip)) {
692 m_copydata(m, offsetof(struct ip, ip_src),
693 sizeof (struct in_addr),
694 (caddr_t) &spidx->src.sin.sin_addr);
695 m_copydata(m, offsetof(struct ip, ip_dst),
696 sizeof (struct in_addr),
697 (caddr_t) &spidx->dst.sin.sin_addr);
699 const struct ip *ip = mtod(m, const struct ip *);
700 spidx->src.sin.sin_addr = ip->ip_src;
701 spidx->dst.sin.sin_addr = ip->ip_dst;
704 spidx->prefs = sizeof(struct in_addr) << 3;
705 spidx->prefd = sizeof(struct in_addr) << 3;
712 ipsec6_get_ulp(const struct mbuf *m, struct secpolicyindex *spidx,
722 panic("%s: NULL pointer was passed.\n", __func__);
724 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
725 printf("%s:\n", __func__); kdebug_mbuf(m));
728 spidx->ul_proto = IPSEC_ULPROTO_ANY;
729 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
730 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
733 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
734 if (off < 0 || m->m_pkthdr.len < off)
739 spidx->ul_proto = nxt;
742 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
744 m_copydata(m, off, sizeof(th), (caddr_t)&th);
745 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
746 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
749 spidx->ul_proto = nxt;
752 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
754 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
755 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
756 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
759 spidx->ul_proto = nxt;
760 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
762 m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
763 ((struct sockaddr_in6 *)&spidx->src)->sin6_port =
764 htons((uint16_t)ih.icmp6_type);
765 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
766 htons((uint16_t)ih.icmp6_code);
769 /* XXX Intermediate headers??? */
770 spidx->ul_proto = nxt;
775 /* Assumes that m is sane. */
777 ipsec6_setspidx_ipaddr(const struct mbuf *m, struct secpolicyindex *spidx)
779 struct ip6_hdr ip6buf;
780 const struct ip6_hdr *ip6 = NULL;
781 struct sockaddr_in6 *sin6;
783 if (m->m_len >= sizeof(*ip6))
784 ip6 = mtod(m, const struct ip6_hdr *);
786 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
790 sin6 = (struct sockaddr_in6 *)&spidx->src;
791 bzero(sin6, sizeof(*sin6));
792 sin6->sin6_family = AF_INET6;
793 sin6->sin6_len = sizeof(struct sockaddr_in6);
794 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
795 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
796 sin6->sin6_addr.s6_addr16[1] = 0;
797 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
799 spidx->prefs = sizeof(struct in6_addr) << 3;
801 sin6 = (struct sockaddr_in6 *)&spidx->dst;
802 bzero(sin6, sizeof(*sin6));
803 sin6->sin6_family = AF_INET6;
804 sin6->sin6_len = sizeof(struct sockaddr_in6);
805 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
806 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
807 sin6->sin6_addr.s6_addr16[1] = 0;
808 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
810 spidx->prefd = sizeof(struct in6_addr) << 3;
817 ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int type)
824 idx = HHOOK_IPSEC_INET;
829 idx = HHOOK_IPSEC_INET6;
833 return (EPFNOSUPPORT);
835 if (type == HHOOK_TYPE_IPSEC_IN)
836 HHOOKS_RUN_IF(V_ipsec_hhh_in[idx], ctx, NULL);
838 HHOOKS_RUN_IF(V_ipsec_hhh_out[idx], ctx, NULL);
839 if (*ctx->mp == NULL)
845 ipsec_delpcbpolicy(struct inpcbpolicy *p)
848 free(p, M_IPSEC_INPCB);
851 /* Initialize policy in PCB. */
853 ipsec_init_policy(struct socket *so, struct inpcbpolicy **pcb_sp)
855 struct inpcbpolicy *new;
858 if (so == NULL || pcb_sp == NULL)
859 panic("%s: NULL pointer was passed.\n", __func__);
861 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
862 M_IPSEC_INPCB, M_NOWAIT|M_ZERO);
864 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
868 new->priv = IPSEC_IS_PRIVILEGED_SO(so);
870 if ((new->sp_in = KEY_NEWSP()) == NULL) {
871 ipsec_delpcbpolicy(new);
874 new->sp_in->policy = IPSEC_POLICY_ENTRUST;
875 if ((new->sp_out = KEY_NEWSP()) == NULL) {
876 KEY_FREESP(&new->sp_in);
877 ipsec_delpcbpolicy(new);
880 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
886 /* Copy old IPsec policy into new. */
888 ipsec_copy_policy(struct inpcbpolicy *old, struct inpcbpolicy *new)
890 struct secpolicy *sp;
892 sp = ipsec_deepcopy_policy(old->sp_in);
894 KEY_FREESP(&new->sp_in);
899 sp = ipsec_deepcopy_policy(old->sp_out);
901 KEY_FREESP(&new->sp_out);
906 new->priv = old->priv;
911 struct ipsecrequest *
914 struct ipsecrequest *p;
916 p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
918 IPSECREQUEST_LOCK_INIT(p);
923 ipsec_delisr(struct ipsecrequest *p)
926 IPSECREQUEST_LOCK_DESTROY(p);
930 /* Deep-copy a policy in PCB. */
931 static struct secpolicy *
932 ipsec_deepcopy_policy(struct secpolicy *src)
934 struct ipsecrequest *newchain = NULL;
935 struct ipsecrequest *p;
936 struct ipsecrequest **q;
937 struct ipsecrequest *r;
938 struct secpolicy *dst;
947 * Deep-copy IPsec request chain. This is required since struct
948 * ipsecrequest is not reference counted.
951 for (p = src->req; p; p = p->next) {
955 (*q)->saidx.proto = p->saidx.proto;
956 (*q)->saidx.mode = p->saidx.mode;
957 (*q)->level = p->level;
958 (*q)->saidx.reqid = p->saidx.reqid;
960 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
961 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
969 dst->policy = src->policy;
970 /* Do not touch the refcnt fields. */
975 for (p = newchain; p; p = r) {
984 /* Set policy and IPsec request if present. */
986 ipsec_set_policy_internal(struct secpolicy **pcb_sp, int optname,
987 caddr_t request, size_t len, struct ucred *cred)
989 struct sadb_x_policy *xpl;
990 struct secpolicy *newsp = NULL;
994 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
996 if (len < sizeof(*xpl))
998 xpl = (struct sadb_x_policy *)request;
1000 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1001 printf("%s: passed policy\n", __func__);
1002 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
1004 /* Check policy type. */
1005 /* ipsec_set_policy_internal() accepts IPSEC, ENTRUST and BYPASS. */
1006 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
1007 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1010 /* Check privileged socket. */
1011 if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1012 error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0);
1017 /* Allocating new SP entry. */
1018 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1021 /* Clear old SP and set new SP. */
1024 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1025 printf("%s: new policy\n", __func__);
1026 kdebug_secpolicy(newsp));
1032 ipsec_set_policy(struct inpcb *inp, int optname, caddr_t request,
1033 size_t len, struct ucred *cred)
1035 struct sadb_x_policy *xpl;
1036 struct secpolicy **pcb_sp;
1039 if (inp == NULL || request == NULL)
1041 if (len < sizeof(*xpl))
1043 xpl = (struct sadb_x_policy *)request;
1045 /* Select direction. */
1046 switch (xpl->sadb_x_policy_dir) {
1047 case IPSEC_DIR_INBOUND:
1048 pcb_sp = &inp->inp_sp->sp_in;
1050 case IPSEC_DIR_OUTBOUND:
1051 pcb_sp = &inp->inp_sp->sp_out;
1054 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1055 xpl->sadb_x_policy_dir));
1059 return (ipsec_set_policy_internal(pcb_sp, optname, request, len, cred));
1063 ipsec_get_policy(struct inpcb *inp, caddr_t request, size_t len,
1066 struct sadb_x_policy *xpl;
1067 struct secpolicy *pcb_sp;
1070 if (inp == NULL || request == NULL || mp == NULL)
1072 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
1073 if (len < sizeof(*xpl))
1075 xpl = (struct sadb_x_policy *)request;
1077 /* Select direction. */
1078 switch (xpl->sadb_x_policy_dir) {
1079 case IPSEC_DIR_INBOUND:
1080 pcb_sp = inp->inp_sp->sp_in;
1082 case IPSEC_DIR_OUTBOUND:
1083 pcb_sp = inp->inp_sp->sp_out;
1086 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1087 xpl->sadb_x_policy_dir));
1091 /* Sanity check. Should be an IPSEC_ASSERT. */
1095 *mp = key_sp2msg(pcb_sp);
1097 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
1101 (*mp)->m_type = MT_DATA;
1102 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1103 printf("%s:\n", __func__); kdebug_mbuf(*mp));
1108 /* Delete policy in PCB. */
1110 ipsec_delete_pcbpolicy(struct inpcb *inp)
1112 IPSEC_ASSERT(inp != NULL, ("null inp"));
1114 if (inp->inp_sp == NULL)
1117 if (inp->inp_sp->sp_in != NULL)
1118 KEY_FREESP(&inp->inp_sp->sp_in);
1120 if (inp->inp_sp->sp_out != NULL)
1121 KEY_FREESP(&inp->inp_sp->sp_out);
1123 ipsec_delpcbpolicy(inp->inp_sp);
1130 * Return current level.
1131 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1134 ipsec_get_reqlevel(struct ipsecrequest *isr)
1137 u_int esp_trans_deflev, esp_net_deflev;
1138 u_int ah_trans_deflev, ah_net_deflev;
1140 IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
1141 IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1142 ("af family mismatch, src %u, dst %u",
1143 isr->sp->spidx.src.sa.sa_family,
1144 isr->sp->spidx.dst.sa.sa_family));
1146 /* XXX Note that we have ipseclog() expanded here - code sync issue. */
1147 #define IPSEC_CHECK_DEFAULT(lev) \
1148 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1149 && (lev) != IPSEC_LEVEL_UNIQUE) \
1151 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1152 (lev), IPSEC_LEVEL_REQUIRE) \
1154 (lev) = IPSEC_LEVEL_REQUIRE, \
1158 /* Set default level. */
1159 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1162 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev);
1163 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev);
1164 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev);
1165 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev);
1170 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev);
1171 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev);
1172 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev);
1173 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev);
1177 panic("%s: unknown af %u",
1178 __func__, isr->sp->spidx.src.sa.sa_family);
1181 #undef IPSEC_CHECK_DEFAULT
1184 switch (isr->level) {
1185 case IPSEC_LEVEL_DEFAULT:
1186 switch (isr->saidx.proto) {
1188 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1189 level = esp_net_deflev;
1191 level = esp_trans_deflev;
1194 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1195 level = ah_net_deflev;
1197 level = ah_trans_deflev;
1199 case IPPROTO_IPCOMP:
1201 * We don't really care, as IPcomp document says that
1202 * we shouldn't compress small packets.
1204 level = IPSEC_LEVEL_USE;
1207 panic("%s: Illegal protocol defined %u\n", __func__,
1212 case IPSEC_LEVEL_USE:
1213 case IPSEC_LEVEL_REQUIRE:
1216 case IPSEC_LEVEL_UNIQUE:
1217 level = IPSEC_LEVEL_REQUIRE;
1221 panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
1228 * Check security policy requirements against the actual
1229 * packet contents. Return one if the packet should be
1230 * reject as "invalid"; otherwiser return zero to have the
1231 * packet treated as "valid".
1238 ipsec_in_reject(struct secpolicy *sp, const struct mbuf *m)
1240 struct ipsecrequest *isr;
1243 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1244 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1247 switch (sp->policy) {
1248 case IPSEC_POLICY_DISCARD:
1250 case IPSEC_POLICY_BYPASS:
1251 case IPSEC_POLICY_NONE:
1255 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1256 ("invalid policy %u", sp->policy));
1258 /* XXX Should compare policy against IPsec header history. */
1261 for (isr = sp->req; isr != NULL; isr = isr->next) {
1262 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1264 switch (isr->saidx.proto) {
1266 if ((m->m_flags & M_DECRYPTED) == 0) {
1267 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1268 printf("%s: ESP m_flags:%x\n", __func__,
1275 isr->sav->tdb_authalgxform != NULL &&
1276 (m->m_flags & M_AUTHIPDGM) == 0) {
1277 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1278 printf("%s: ESP/AH m_flags:%x\n", __func__,
1285 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1286 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1287 printf("%s: AH m_flags:%x\n", __func__,
1292 case IPPROTO_IPCOMP:
1294 * We don't really care, as IPcomp document
1295 * says that we shouldn't compress small
1296 * packets. IPComp policy should always be
1297 * treated as being in "use" level.
1302 return (0); /* Valid. */
1306 * Non zero return value means security policy DISCARD or policy violation.
1309 ipsec46_in_reject(const struct mbuf *m, struct inpcb *inp)
1311 struct secpolicy *sp;
1315 if (!key_havesp(IPSEC_DIR_INBOUND))
1318 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1320 /* Get SP for this packet. */
1322 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, &error);
1324 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1327 result = ipsec_in_reject(sp, m);
1330 result = 1; /* treat errors as policy violation */
1336 * Check AH/ESP integrity.
1337 * This function is called from tcp_input(), udp_input(),
1338 * and {ah,esp}4_input for tunnel mode.
1341 ipsec4_in_reject(const struct mbuf *m, struct inpcb *inp)
1345 result = ipsec46_in_reject(m, inp);
1347 IPSECSTAT_INC(ips_in_polvio);
1354 * Check AH/ESP integrity.
1355 * This function is called from tcp6_input(), udp6_input(),
1356 * and {ah,esp}6_input for tunnel mode.
1359 ipsec6_in_reject(const struct mbuf *m, struct inpcb *inp)
1363 result = ipsec46_in_reject(m, inp);
1365 IPSEC6STAT_INC(ips_in_polvio);
1372 * Compute the byte size to be occupied by IPsec header.
1373 * In case it is tunnelled, it includes the size of outer IP header.
1374 * NOTE: SP passed is freed in this function.
1377 ipsec_hdrsiz_internal(struct secpolicy *sp)
1379 struct ipsecrequest *isr;
1382 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1383 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1385 switch (sp->policy) {
1386 case IPSEC_POLICY_DISCARD:
1387 case IPSEC_POLICY_BYPASS:
1388 case IPSEC_POLICY_NONE:
1392 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1393 ("invalid policy %u", sp->policy));
1396 for (isr = sp->req; isr != NULL; isr = isr->next) {
1399 switch (isr->saidx.proto) {
1401 clen = esp_hdrsiz(isr->sav);
1404 clen = ah_hdrsiz(isr->sav);
1406 case IPPROTO_IPCOMP:
1407 clen = sizeof(struct ipcomp);
1411 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1412 switch (isr->saidx.dst.sa.sa_family) {
1414 clen += sizeof(struct ip);
1418 clen += sizeof(struct ip6_hdr);
1422 ipseclog((LOG_ERR, "%s: unknown AF %d in "
1423 "IPsec tunnel SA\n", __func__,
1424 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1435 * This function is called from ipsec_hdrsiz_tcp(), ip_ipsec_mtu(),
1436 * disabled ip6_ipsec_mtu() and ip6_forward().
1439 ipsec_hdrsiz(const struct mbuf *m, u_int dir, struct inpcb *inp)
1441 struct secpolicy *sp;
1445 if (!key_havesp(dir))
1448 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1450 /* Get SP for this packet. */
1452 sp = ipsec_getpolicybyaddr(m, dir, &error);
1454 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1457 size = ipsec_hdrsiz_internal(sp);
1458 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1459 printf("%s: size:%lu.\n", __func__,
1460 (unsigned long)size));
1464 size = 0; /* XXX Should be panic?
1465 * -> No, we are called w/o knowing if
1466 * IPsec processing is needed. */
1472 * Check the variable replay window.
1473 * ipsec_chkreplay() performs replay check before ICV verification.
1474 * ipsec_updatereplay() updates replay bitmap. This must be called after
1475 * ICV verification (it also performs replay check, which is usually done
1477 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1479 * Based on RFC 2401.
1482 ipsec_chkreplay(u_int32_t seq, struct secasvar *sav)
1484 const struct secreplay *replay;
1487 u_int32_t wsizeb; /* Constant: bits of window size. */
1488 int frlast; /* Constant: last frame. */
1490 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1491 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1493 replay = sav->replay;
1495 if (replay->wsize == 0)
1496 return (1); /* No need to check replay. */
1499 frlast = replay->wsize - 1;
1500 wsizeb = replay->wsize << 3;
1502 /* Sequence number of 0 is invalid. */
1506 /* First time is always okay. */
1507 if (replay->count == 0)
1510 if (seq > replay->lastseq) {
1511 /* Larger sequences are okay. */
1514 /* seq is equal or less than lastseq. */
1515 diff = replay->lastseq - seq;
1517 /* Over range to check, i.e. too old or wrapped. */
1521 fr = frlast - diff / 8;
1523 /* This packet already seen? */
1524 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1527 /* Out of order but good. */
1533 * Check replay counter whether to update or not.
1538 ipsec_updatereplay(u_int32_t seq, struct secasvar *sav)
1541 struct secreplay *replay;
1544 u_int32_t wsizeb; /* Constant: bits of window size. */
1545 int frlast; /* Constant: last frame. */
1547 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1548 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1550 replay = sav->replay;
1552 if (replay->wsize == 0)
1553 goto ok; /* No need to check replay. */
1556 frlast = replay->wsize - 1;
1557 wsizeb = replay->wsize << 3;
1559 /* Sequence number of 0 is invalid. */
1564 if (replay->count == 0) {
1565 replay->lastseq = seq;
1566 bzero(replay->bitmap, replay->wsize);
1567 (replay->bitmap)[frlast] = 1;
1571 if (seq > replay->lastseq) {
1572 /* seq is larger than lastseq. */
1573 diff = seq - replay->lastseq;
1575 /* New larger sequence number. */
1576 if (diff < wsizeb) {
1578 /* Set bit for this packet. */
1579 vshiftl(replay->bitmap, diff, replay->wsize);
1580 (replay->bitmap)[frlast] |= 1;
1582 /* This packet has a "way larger". */
1583 bzero(replay->bitmap, replay->wsize);
1584 (replay->bitmap)[frlast] = 1;
1586 replay->lastseq = seq;
1588 /* Larger is good. */
1590 /* seq is equal or less than lastseq. */
1591 diff = replay->lastseq - seq;
1593 /* Over range to check, i.e. too old or wrapped. */
1597 fr = frlast - diff / 8;
1599 /* This packet already seen? */
1600 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1604 (replay->bitmap)[fr] |= (1 << (diff % 8));
1606 /* Out of order but good. */
1610 if (replay->count == ~0) {
1612 /* Set overflow flag. */
1615 /* Don't increment, no more packets accepted. */
1616 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1619 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1620 __func__, replay->overflow,
1621 ipsec_logsastr(sav, buf, sizeof(buf))));
1630 * Shift variable length buffer to left.
1631 * IN: bitmap: pointer to the buffer
1632 * nbit: the number of to shift.
1633 * wsize: buffer size (bytes).
1636 vshiftl(unsigned char *bitmap, int nbit, int wsize)
1641 for (j = 0; j < nbit; j += 8) {
1642 s = (nbit - j < 8) ? (nbit - j): 8;
1644 for (i = 1; i < wsize; i++) {
1645 over = (bitmap[i] >> (8 - s));
1647 bitmap[i-1] |= over;
1652 /* Return a printable string for the address. */
1654 ipsec_address(union sockaddr_union* sa, char *buf, socklen_t size)
1657 switch (sa->sa.sa_family) {
1660 return (inet_ntop(AF_INET, &sa->sin.sin_addr, buf, size));
1664 return (inet_ntop(AF_INET6, &sa->sin6.sin6_addr, buf, size));
1667 return ("(unknown address family)");
1672 ipsec_logsastr(struct secasvar *sav, char *buf, size_t size)
1674 char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
1676 IPSEC_ASSERT(sav->sah->saidx.src.sa.sa_family ==
1677 sav->sah->saidx.dst.sa.sa_family, ("address family mismatch"));
1679 snprintf(buf, size, "SA(SPI=%08lx src=%s dst=%s)",
1680 (u_long)ntohl(sav->spi),
1681 ipsec_address(&sav->sah->saidx.src, sbuf, sizeof(sbuf)),
1682 ipsec_address(&sav->sah->saidx.dst, dbuf, sizeof(dbuf)));
1687 ipsec_dumpmbuf(const struct mbuf *m)
1696 p = mtod(m, const u_char *);
1697 for (i = 0; i < m->m_len; i++) {
1698 printf("%02x ", p[i]);
1700 if (totlen % 16 == 0)
1705 if (totlen % 16 != 0)
1711 def_policy_init(const void *unused __unused)
1714 bzero(&V_def_policy, sizeof(struct secpolicy));
1715 V_def_policy.policy = IPSEC_POLICY_NONE;
1716 V_def_policy.refcnt = 1;
1718 VNET_SYSINIT(def_policy_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
1719 def_policy_init, NULL);
1722 /* XXX This stuff doesn't belong here... */
1724 static struct xformsw* xforms = NULL;
1727 * Register a transform; typically at system startup.
1730 xform_register(struct xformsw* xsp)
1733 xsp->xf_next = xforms;
1738 * Initialize transform support in an sav.
1741 xform_init(struct secasvar *sav, int xftype)
1743 struct xformsw *xsp;
1745 if (sav->tdb_xform != NULL) /* Previously initialized. */
1747 for (xsp = xforms; xsp; xsp = xsp->xf_next)
1748 if (xsp->xf_type == xftype)
1749 return ((*xsp->xf_init)(sav, xsp));