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 /* Set spidx in pcb. */
338 *error = ipsec_setspidx_inpcb(m, inp);
343 IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
345 case IPSEC_DIR_INBOUND:
346 currsp = pcbsp->sp_in;
348 case IPSEC_DIR_OUTBOUND:
349 currsp = pcbsp->sp_out;
352 IPSEC_ASSERT(currsp != NULL, ("null currsp"));
354 if (pcbsp->priv) { /* When privilieged socket. */
355 switch (currsp->policy) {
356 case IPSEC_POLICY_BYPASS:
357 case IPSEC_POLICY_IPSEC:
362 case IPSEC_POLICY_ENTRUST:
363 /* Look for a policy in SPD. */
364 sp = KEY_ALLOCSP(&currsp->spidx, dir);
365 if (sp == NULL) /* No SP found. */
366 sp = KEY_ALLOCSP_DEFAULT();
370 ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
371 __func__, currsp->policy));
375 } else { /* Unpriv, SPD has policy. */
376 sp = KEY_ALLOCSP(&currsp->spidx, dir);
377 if (sp == NULL) { /* No SP found. */
378 switch (currsp->policy) {
379 case IPSEC_POLICY_BYPASS:
380 ipseclog((LOG_ERR, "%s: Illegal policy for "
381 "non-priviliged defined %d\n",
382 __func__, currsp->policy));
386 case IPSEC_POLICY_ENTRUST:
387 sp = KEY_ALLOCSP_DEFAULT();
390 case IPSEC_POLICY_IPSEC:
396 ipseclog((LOG_ERR, "%s: Invalid policy for "
397 "PCB %d\n", __func__, currsp->policy));
403 IPSEC_ASSERT(sp != NULL,
404 ("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
405 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
406 printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
407 __func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
412 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
413 * and return a pointer to SP.
414 * OUT: positive: a pointer to the entry for security policy leaf matched.
415 * NULL: no apropreate SP found, the following value is set to error.
417 * EACCES : discard packet.
418 * ENOENT : ipsec_acquire() in progress, maybe.
419 * others : error occured.
422 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int *error)
424 struct secpolicyindex spidx;
425 struct secpolicy *sp;
427 IPSEC_ASSERT(m != NULL, ("null mbuf"));
428 IPSEC_ASSERT(error != NULL, ("null error"));
429 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
430 ("invalid direction %u", dir));
434 if (key_havesp(dir)) {
435 /* Make an index to look for a policy. */
436 *error = ipsec_setspidx(m, &spidx, 0);
438 DPRINTF(("%s: setpidx failed, dir %u\n",
443 sp = KEY_ALLOCSP(&spidx, dir);
445 if (sp == NULL) /* No SP found, use system default. */
446 sp = KEY_ALLOCSP_DEFAULT();
447 IPSEC_ASSERT(sp != NULL, ("null SP"));
452 ipsec4_checkpolicy(struct mbuf *m, u_int dir, int *error, struct inpcb *inp)
454 struct secpolicy *sp;
458 sp = ipsec_getpolicybyaddr(m, dir, error);
460 sp = ipsec_getpolicybysock(m, dir, inp, error);
462 IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
463 IPSECSTAT_INC(ips_out_inval);
466 IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
467 switch (sp->policy) {
468 case IPSEC_POLICY_ENTRUST:
470 printf("%s: invalid policy %u\n", __func__, sp->policy);
472 case IPSEC_POLICY_DISCARD:
473 IPSECSTAT_INC(ips_out_polvio);
474 *error = -EINVAL; /* Packet is discarded by caller. */
476 case IPSEC_POLICY_BYPASS:
477 case IPSEC_POLICY_NONE:
479 sp = NULL; /* NB: force NULL result. */
481 case IPSEC_POLICY_IPSEC:
482 if (sp->req == NULL) /* Acquire a SA. */
483 *error = key_spdacquire(sp);
494 ipsec_setspidx_inpcb(struct mbuf *m, struct inpcb *inp)
498 IPSEC_ASSERT(inp != NULL, ("null inp"));
499 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
500 IPSEC_ASSERT(inp->inp_sp->sp_out != NULL && inp->inp_sp->sp_in != NULL,
501 ("null sp_in || sp_out"));
503 error = ipsec_setspidx(m, &inp->inp_sp->sp_in->spidx, 1);
505 inp->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
506 inp->inp_sp->sp_out->spidx = inp->inp_sp->sp_in->spidx;
507 inp->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
509 bzero(&inp->inp_sp->sp_in->spidx,
510 sizeof (inp->inp_sp->sp_in->spidx));
511 bzero(&inp->inp_sp->sp_out->spidx,
512 sizeof (inp->inp_sp->sp_in->spidx));
518 * Configure security policy index (src/dst/proto/sport/dport)
519 * by looking at the content of mbuf.
520 * The caller is responsible for error recovery (like clearing up spidx).
523 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
525 struct ip *ip = NULL;
532 IPSEC_ASSERT(m != NULL, ("null mbuf"));
535 * Validate m->m_pkthdr.len. We see incorrect length if we
536 * mistakenly call this function with inconsistent mbuf chain
537 * (like 4.4BSD tcp/udp processing). XXX Should we panic here?
540 for (n = m; n; n = n->m_next)
542 if (m->m_pkthdr.len != len) {
543 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
544 printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
545 __func__, len, m->m_pkthdr.len));
549 if (m->m_pkthdr.len < sizeof(struct ip)) {
550 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
551 printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
552 __func__, m->m_pkthdr.len));
556 if (m->m_len >= sizeof(*ip))
557 ip = mtod(m, struct ip *);
559 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
565 error = ipsec4_setspidx_ipaddr(m, spidx);
568 ipsec4_get_ulp(m, spidx, needport);
572 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
573 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
574 printf("%s: pkthdr len(%d) too small (v6), "
575 "ignored\n", __func__, m->m_pkthdr.len));
578 error = ipsec6_setspidx_ipaddr(m, spidx);
581 ipsec6_get_ulp(m, spidx, needport);
585 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
586 printf("%s: " "unknown IP version %u, ignored.\n",
593 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
599 IPSEC_ASSERT(m != NULL, ("null mbuf"));
600 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
602 if (m->m_len >= sizeof (struct ip)) {
603 struct ip *ip = mtod(m, struct ip *);
604 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
606 off = ip->ip_hl << 2;
611 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
612 if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
618 while (off < m->m_pkthdr.len) {
625 spidx->ul_proto = nxt;
628 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
630 m_copydata(m, off, sizeof (th), (caddr_t) &th);
631 spidx->src.sin.sin_port = th.th_sport;
632 spidx->dst.sin.sin_port = th.th_dport;
635 spidx->ul_proto = nxt;
638 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
640 m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
641 spidx->src.sin.sin_port = uh.uh_sport;
642 spidx->dst.sin.sin_port = uh.uh_dport;
645 if (off + sizeof(ip6e) > m->m_pkthdr.len)
647 /* XXX Sigh, this works but is totally bogus. */
648 m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
649 off += (ip6e.ip6e_len + 2) << 2;
654 /* XXX Intermediate headers??? */
655 spidx->ul_proto = nxt;
660 spidx->ul_proto = IPSEC_ULPROTO_ANY;
662 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
663 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
666 /* Assumes that m is sane. */
668 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
670 static const struct sockaddr_in template = {
671 sizeof (struct sockaddr_in),
673 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
676 spidx->src.sin = template;
677 spidx->dst.sin = template;
679 if (m->m_len < sizeof (struct ip)) {
680 m_copydata(m, offsetof(struct ip, ip_src),
681 sizeof (struct in_addr),
682 (caddr_t) &spidx->src.sin.sin_addr);
683 m_copydata(m, offsetof(struct ip, ip_dst),
684 sizeof (struct in_addr),
685 (caddr_t) &spidx->dst.sin.sin_addr);
687 struct ip *ip = mtod(m, struct ip *);
688 spidx->src.sin.sin_addr = ip->ip_src;
689 spidx->dst.sin.sin_addr = ip->ip_dst;
692 spidx->prefs = sizeof(struct in_addr) << 3;
693 spidx->prefd = sizeof(struct in_addr) << 3;
700 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
709 panic("%s: NULL pointer was passed.\n", __func__);
711 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
712 printf("%s:\n", __func__); kdebug_mbuf(m));
715 spidx->ul_proto = IPSEC_ULPROTO_ANY;
716 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
717 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
720 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
721 if (off < 0 || m->m_pkthdr.len < off)
726 spidx->ul_proto = nxt;
729 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
731 m_copydata(m, off, sizeof(th), (caddr_t)&th);
732 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
733 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
736 spidx->ul_proto = nxt;
739 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
741 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
742 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
743 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
746 spidx->ul_proto = nxt;
747 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
749 m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
750 ((struct sockaddr_in6 *)&spidx->src)->sin6_port =
751 htons((uint16_t)ih.icmp6_type);
752 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
753 htons((uint16_t)ih.icmp6_code);
756 /* XXX Intermediate headers??? */
757 spidx->ul_proto = nxt;
762 /* Assumes that m is sane. */
764 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
766 struct ip6_hdr *ip6 = NULL;
767 struct ip6_hdr ip6buf;
768 struct sockaddr_in6 *sin6;
770 if (m->m_len >= sizeof(*ip6))
771 ip6 = mtod(m, struct ip6_hdr *);
773 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
777 sin6 = (struct sockaddr_in6 *)&spidx->src;
778 bzero(sin6, sizeof(*sin6));
779 sin6->sin6_family = AF_INET6;
780 sin6->sin6_len = sizeof(struct sockaddr_in6);
781 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
782 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
783 sin6->sin6_addr.s6_addr16[1] = 0;
784 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
786 spidx->prefs = sizeof(struct in6_addr) << 3;
788 sin6 = (struct sockaddr_in6 *)&spidx->dst;
789 bzero(sin6, sizeof(*sin6));
790 sin6->sin6_family = AF_INET6;
791 sin6->sin6_len = sizeof(struct sockaddr_in6);
792 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
793 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
794 sin6->sin6_addr.s6_addr16[1] = 0;
795 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
797 spidx->prefd = sizeof(struct in6_addr) << 3;
804 ipsec_delpcbpolicy(struct inpcbpolicy *p)
807 free(p, M_IPSEC_INPCB);
810 /* Initialize policy in PCB. */
812 ipsec_init_policy(struct socket *so, struct inpcbpolicy **pcb_sp)
814 struct inpcbpolicy *new;
817 if (so == NULL || pcb_sp == NULL)
818 panic("%s: NULL pointer was passed.\n", __func__);
820 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
821 M_IPSEC_INPCB, M_NOWAIT|M_ZERO);
823 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
827 new->priv = IPSEC_IS_PRIVILEGED_SO(so);
829 if ((new->sp_in = KEY_NEWSP()) == NULL) {
830 ipsec_delpcbpolicy(new);
833 new->sp_in->policy = IPSEC_POLICY_ENTRUST;
834 if ((new->sp_out = KEY_NEWSP()) == NULL) {
835 KEY_FREESP(&new->sp_in);
836 ipsec_delpcbpolicy(new);
839 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
845 /* Copy old IPsec policy into new. */
847 ipsec_copy_policy(struct inpcbpolicy *old, struct inpcbpolicy *new)
849 struct secpolicy *sp;
851 sp = ipsec_deepcopy_policy(old->sp_in);
853 KEY_FREESP(&new->sp_in);
858 sp = ipsec_deepcopy_policy(old->sp_out);
860 KEY_FREESP(&new->sp_out);
865 new->priv = old->priv;
870 struct ipsecrequest *
873 struct ipsecrequest *p;
875 p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
877 IPSECREQUEST_LOCK_INIT(p);
882 ipsec_delisr(struct ipsecrequest *p)
885 IPSECREQUEST_LOCK_DESTROY(p);
889 /* Deep-copy a policy in PCB. */
890 static struct secpolicy *
891 ipsec_deepcopy_policy(struct secpolicy *src)
893 struct ipsecrequest *newchain = NULL;
894 struct ipsecrequest *p;
895 struct ipsecrequest **q;
896 struct ipsecrequest *r;
897 struct secpolicy *dst;
906 * Deep-copy IPsec request chain. This is required since struct
907 * ipsecrequest is not reference counted.
910 for (p = src->req; p; p = p->next) {
914 (*q)->saidx.proto = p->saidx.proto;
915 (*q)->saidx.mode = p->saidx.mode;
916 (*q)->level = p->level;
917 (*q)->saidx.reqid = p->saidx.reqid;
919 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
920 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
928 dst->policy = src->policy;
929 /* Do not touch the refcnt fields. */
934 for (p = newchain; p; p = r) {
942 /* Set policy and IPsec request if present. */
944 ipsec_set_policy_internal(struct secpolicy **pcb_sp, int optname,
945 caddr_t request, size_t len, struct ucred *cred)
947 struct sadb_x_policy *xpl;
948 struct secpolicy *newsp = NULL;
952 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
954 if (len < sizeof(*xpl))
956 xpl = (struct sadb_x_policy *)request;
958 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
959 printf("%s: passed policy\n", __func__);
960 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
962 /* Check policy type. */
963 /* ipsec_set_policy_internal() accepts IPSEC, ENTRUST and BYPASS. */
964 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
965 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
968 /* Check privileged socket. */
969 if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
970 error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0);
975 /* Allocating new SP entry. */
976 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
979 /* Clear old SP and set new SP. */
982 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
983 printf("%s: new policy\n", __func__);
984 kdebug_secpolicy(newsp));
990 ipsec_set_policy(struct inpcb *inp, int optname, caddr_t request,
991 size_t len, struct ucred *cred)
993 struct sadb_x_policy *xpl;
994 struct secpolicy **pcb_sp;
997 if (inp == NULL || request == NULL)
999 if (len < sizeof(*xpl))
1001 xpl = (struct sadb_x_policy *)request;
1003 /* Select direction. */
1004 switch (xpl->sadb_x_policy_dir) {
1005 case IPSEC_DIR_INBOUND:
1006 pcb_sp = &inp->inp_sp->sp_in;
1008 case IPSEC_DIR_OUTBOUND:
1009 pcb_sp = &inp->inp_sp->sp_out;
1012 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1013 xpl->sadb_x_policy_dir));
1017 return (ipsec_set_policy_internal(pcb_sp, optname, request, len, cred));
1021 ipsec_get_policy(struct inpcb *inp, caddr_t request, size_t len,
1024 struct sadb_x_policy *xpl;
1025 struct secpolicy *pcb_sp;
1028 if (inp == NULL || request == NULL || mp == NULL)
1030 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
1031 if (len < sizeof(*xpl))
1033 xpl = (struct sadb_x_policy *)request;
1035 /* Select direction. */
1036 switch (xpl->sadb_x_policy_dir) {
1037 case IPSEC_DIR_INBOUND:
1038 pcb_sp = inp->inp_sp->sp_in;
1040 case IPSEC_DIR_OUTBOUND:
1041 pcb_sp = inp->inp_sp->sp_out;
1044 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1045 xpl->sadb_x_policy_dir));
1049 /* Sanity check. Should be an IPSEC_ASSERT. */
1053 *mp = key_sp2msg(pcb_sp);
1055 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
1059 (*mp)->m_type = MT_DATA;
1060 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1061 printf("%s:\n", __func__); kdebug_mbuf(*mp));
1066 /* Delete policy in PCB. */
1068 ipsec_delete_pcbpolicy(struct inpcb *inp)
1070 IPSEC_ASSERT(inp != NULL, ("null inp"));
1072 if (inp->inp_sp == NULL)
1075 if (inp->inp_sp->sp_in != NULL)
1076 KEY_FREESP(&inp->inp_sp->sp_in);
1078 if (inp->inp_sp->sp_out != NULL)
1079 KEY_FREESP(&inp->inp_sp->sp_out);
1081 ipsec_delpcbpolicy(inp->inp_sp);
1088 * Return current level.
1089 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1092 ipsec_get_reqlevel(struct ipsecrequest *isr)
1095 u_int esp_trans_deflev, esp_net_deflev;
1096 u_int ah_trans_deflev, ah_net_deflev;
1098 IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
1099 IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1100 ("af family mismatch, src %u, dst %u",
1101 isr->sp->spidx.src.sa.sa_family,
1102 isr->sp->spidx.dst.sa.sa_family));
1104 /* XXX Note that we have ipseclog() expanded here - code sync issue. */
1105 #define IPSEC_CHECK_DEFAULT(lev) \
1106 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1107 && (lev) != IPSEC_LEVEL_UNIQUE) \
1109 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1110 (lev), IPSEC_LEVEL_REQUIRE) \
1112 (lev) = IPSEC_LEVEL_REQUIRE, \
1116 /* Set default level. */
1117 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1120 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev);
1121 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev);
1122 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev);
1123 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev);
1128 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev);
1129 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev);
1130 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev);
1131 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev);
1135 panic("%s: unknown af %u",
1136 __func__, isr->sp->spidx.src.sa.sa_family);
1139 #undef IPSEC_CHECK_DEFAULT
1142 switch (isr->level) {
1143 case IPSEC_LEVEL_DEFAULT:
1144 switch (isr->saidx.proto) {
1146 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1147 level = esp_net_deflev;
1149 level = esp_trans_deflev;
1152 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1153 level = ah_net_deflev;
1155 level = ah_trans_deflev;
1157 case IPPROTO_IPCOMP:
1159 * We don't really care, as IPcomp document says that
1160 * we shouldn't compress small packets.
1162 level = IPSEC_LEVEL_USE;
1165 panic("%s: Illegal protocol defined %u\n", __func__,
1170 case IPSEC_LEVEL_USE:
1171 case IPSEC_LEVEL_REQUIRE:
1174 case IPSEC_LEVEL_UNIQUE:
1175 level = IPSEC_LEVEL_REQUIRE;
1179 panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
1186 * Check security policy requirements against the actual
1187 * packet contents. Return one if the packet should be
1188 * reject as "invalid"; otherwiser return zero to have the
1189 * packet treated as "valid".
1196 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1198 struct ipsecrequest *isr;
1201 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1202 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1205 switch (sp->policy) {
1206 case IPSEC_POLICY_DISCARD:
1208 case IPSEC_POLICY_BYPASS:
1209 case IPSEC_POLICY_NONE:
1213 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1214 ("invalid policy %u", sp->policy));
1216 /* XXX Should compare policy against IPsec header history. */
1219 for (isr = sp->req; isr != NULL; isr = isr->next) {
1220 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1222 switch (isr->saidx.proto) {
1224 if ((m->m_flags & M_DECRYPTED) == 0) {
1225 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1226 printf("%s: ESP m_flags:%x\n", __func__,
1233 isr->sav->tdb_authalgxform != NULL &&
1234 (m->m_flags & M_AUTHIPDGM) == 0) {
1235 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1236 printf("%s: ESP/AH m_flags:%x\n", __func__,
1243 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1244 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1245 printf("%s: AH m_flags:%x\n", __func__,
1250 case IPPROTO_IPCOMP:
1252 * We don't really care, as IPcomp document
1253 * says that we shouldn't compress small
1254 * packets. IPComp policy should always be
1255 * treated as being in "use" level.
1260 return (0); /* Valid. */
1264 * Non zero return value means security policy DISCARD or policy violation.
1267 ipsec46_in_reject(struct mbuf *m, struct inpcb *inp)
1269 struct secpolicy *sp;
1273 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1275 /* Get SP for this packet. */
1277 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, &error);
1279 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1282 result = ipsec_in_reject(sp, m);
1285 result = 1; /* treat errors as policy violation */
1291 * Check AH/ESP integrity.
1292 * This function is called from tcp_input(), udp_input(),
1293 * and {ah,esp}4_input for tunnel mode.
1296 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp)
1300 result = ipsec46_in_reject(m, inp);
1302 IPSECSTAT_INC(ips_in_polvio);
1309 * Check AH/ESP integrity.
1310 * This function is called from tcp6_input(), udp6_input(),
1311 * and {ah,esp}6_input for tunnel mode.
1314 ipsec6_in_reject(struct mbuf *m, struct inpcb *inp)
1318 result = ipsec46_in_reject(m, inp);
1320 IPSEC6STAT_INC(ips_in_polvio);
1327 * Compute the byte size to be occupied by IPsec header.
1328 * In case it is tunnelled, it includes the size of outer IP header.
1329 * NOTE: SP passed is freed in this function.
1332 ipsec_hdrsiz_internal(struct secpolicy *sp)
1334 struct ipsecrequest *isr;
1337 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1338 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1340 switch (sp->policy) {
1341 case IPSEC_POLICY_DISCARD:
1342 case IPSEC_POLICY_BYPASS:
1343 case IPSEC_POLICY_NONE:
1347 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1348 ("invalid policy %u", sp->policy));
1351 for (isr = sp->req; isr != NULL; isr = isr->next) {
1354 switch (isr->saidx.proto) {
1356 clen = esp_hdrsiz(isr->sav);
1359 clen = ah_hdrsiz(isr->sav);
1361 case IPPROTO_IPCOMP:
1362 clen = sizeof(struct ipcomp);
1366 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1367 switch (isr->saidx.dst.sa.sa_family) {
1369 clen += sizeof(struct ip);
1373 clen += sizeof(struct ip6_hdr);
1377 ipseclog((LOG_ERR, "%s: unknown AF %d in "
1378 "IPsec tunnel SA\n", __func__,
1379 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1390 * This function is called from ipsec_hdrsiz_tcp(), ip_ipsec_mtu(),
1391 * disabled ip6_ipsec_mtu() and ip6_forward().
1394 ipsec_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
1396 struct secpolicy *sp;
1400 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1402 /* Get SP for this packet. */
1404 sp = ipsec_getpolicybyaddr(m, dir, &error);
1406 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1409 size = ipsec_hdrsiz_internal(sp);
1410 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1411 printf("%s: size:%lu.\n", __func__,
1412 (unsigned long)size));
1416 size = 0; /* XXX Should be panic?
1417 * -> No, we are called w/o knowing if
1418 * IPsec processing is needed. */
1424 * Check the variable replay window.
1425 * ipsec_chkreplay() performs replay check before ICV verification.
1426 * ipsec_updatereplay() updates replay bitmap. This must be called after
1427 * ICV verification (it also performs replay check, which is usually done
1429 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1431 * Based on RFC 2401.
1434 ipsec_chkreplay(u_int32_t seq, struct secasvar *sav)
1436 const struct secreplay *replay;
1439 u_int32_t wsizeb; /* Constant: bits of window size. */
1440 int frlast; /* Constant: last frame. */
1442 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1443 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1445 replay = sav->replay;
1447 if (replay->wsize == 0)
1448 return (1); /* No need to check replay. */
1451 frlast = replay->wsize - 1;
1452 wsizeb = replay->wsize << 3;
1454 /* Sequence number of 0 is invalid. */
1458 /* First time is always okay. */
1459 if (replay->count == 0)
1462 if (seq > replay->lastseq) {
1463 /* Larger sequences are okay. */
1466 /* seq is equal or less than lastseq. */
1467 diff = replay->lastseq - seq;
1469 /* Over range to check, i.e. too old or wrapped. */
1473 fr = frlast - diff / 8;
1475 /* This packet already seen? */
1476 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1479 /* Out of order but good. */
1485 * Check replay counter whether to update or not.
1490 ipsec_updatereplay(u_int32_t seq, struct secasvar *sav)
1493 struct secreplay *replay;
1496 u_int32_t wsizeb; /* Constant: bits of window size. */
1497 int frlast; /* Constant: last frame. */
1499 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1500 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1502 replay = sav->replay;
1504 if (replay->wsize == 0)
1505 goto ok; /* No need to check replay. */
1508 frlast = replay->wsize - 1;
1509 wsizeb = replay->wsize << 3;
1511 /* Sequence number of 0 is invalid. */
1516 if (replay->count == 0) {
1517 replay->lastseq = seq;
1518 bzero(replay->bitmap, replay->wsize);
1519 (replay->bitmap)[frlast] = 1;
1523 if (seq > replay->lastseq) {
1524 /* seq is larger than lastseq. */
1525 diff = seq - replay->lastseq;
1527 /* New larger sequence number. */
1528 if (diff < wsizeb) {
1530 /* Set bit for this packet. */
1531 vshiftl(replay->bitmap, diff, replay->wsize);
1532 (replay->bitmap)[frlast] |= 1;
1534 /* This packet has a "way larger". */
1535 bzero(replay->bitmap, replay->wsize);
1536 (replay->bitmap)[frlast] = 1;
1538 replay->lastseq = seq;
1540 /* Larger is good. */
1542 /* seq is equal or less than lastseq. */
1543 diff = replay->lastseq - seq;
1545 /* Over range to check, i.e. too old or wrapped. */
1549 fr = frlast - diff / 8;
1551 /* This packet already seen? */
1552 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1556 (replay->bitmap)[fr] |= (1 << (diff % 8));
1558 /* Out of order but good. */
1562 if (replay->count == ~0) {
1564 /* Set overflow flag. */
1567 /* Don't increment, no more packets accepted. */
1568 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1571 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1572 __func__, replay->overflow,
1573 ipsec_logsastr(sav, buf, sizeof(buf))));
1582 * Shift variable length buffer to left.
1583 * IN: bitmap: pointer to the buffer
1584 * nbit: the number of to shift.
1585 * wsize: buffer size (bytes).
1588 vshiftl(unsigned char *bitmap, int nbit, int wsize)
1593 for (j = 0; j < nbit; j += 8) {
1594 s = (nbit - j < 8) ? (nbit - j): 8;
1596 for (i = 1; i < wsize; i++) {
1597 over = (bitmap[i] >> (8 - s));
1599 bitmap[i-1] |= over;
1604 /* Return a printable string for the address. */
1606 ipsec_address(union sockaddr_union* sa, char *buf, socklen_t size)
1609 switch (sa->sa.sa_family) {
1612 return (inet_ntop(AF_INET, &sa->sin.sin_addr, buf, size));
1616 return (inet_ntop(AF_INET6, &sa->sin6.sin6_addr, buf, size));
1619 return ("(unknown address family)");
1624 ipsec_logsastr(struct secasvar *sav, char *buf, size_t size)
1626 char sbuf[INET6_ADDRSTRLEN], dbuf[INET6_ADDRSTRLEN];
1628 IPSEC_ASSERT(sav->sah->saidx.src.sa.sa_family ==
1629 sav->sah->saidx.dst.sa.sa_family, ("address family mismatch"));
1631 snprintf(buf, size, "SA(SPI=%08lx src=%s dst=%s)",
1632 (u_long)ntohl(sav->spi),
1633 ipsec_address(&sav->sah->saidx.src, sbuf, sizeof(sbuf)),
1634 ipsec_address(&sav->sah->saidx.dst, dbuf, sizeof(dbuf)));
1639 ipsec_dumpmbuf(struct mbuf *m)
1648 p = mtod(m, u_char *);
1649 for (i = 0; i < m->m_len; i++) {
1650 printf("%02x ", p[i]);
1652 if (totlen % 16 == 0)
1657 if (totlen % 16 != 0)
1663 def_policy_init(const void *unused __unused)
1666 bzero(&V_def_policy, sizeof(struct secpolicy));
1667 V_def_policy.policy = IPSEC_POLICY_NONE;
1668 V_def_policy.refcnt = 1;
1670 VNET_SYSINIT(def_policy_init, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY,
1671 def_policy_init, NULL);
1674 /* XXX This stuff doesn't belong here... */
1676 static struct xformsw* xforms = NULL;
1679 * Register a transform; typically at system startup.
1682 xform_register(struct xformsw* xsp)
1685 xsp->xf_next = xforms;
1690 * Initialize transform support in an sav.
1693 xform_init(struct secasvar *sav, int xftype)
1695 struct xformsw *xsp;
1697 if (sav->tdb_xform != NULL) /* Previously initialized. */
1699 for (xsp = xforms; xsp; xsp = xsp->xf_next)
1700 if (xsp->xf_type == xftype)
1701 return ((*xsp->xf_init)(sav, xsp));