2 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * IPsec controller part.
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/errno.h>
52 #include <sys/kernel.h>
53 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
58 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/in_var.h>
65 #include <netinet/udp.h>
66 #include <netinet/udp_var.h>
67 #include <netinet/tcp.h>
68 #include <netinet/udp.h>
70 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
74 #include <netinet/in_pcb.h>
76 #include <netinet/icmp6.h>
79 #include <sys/types.h>
80 #include <netipsec/ipsec.h>
82 #include <netipsec/ipsec6.h>
84 #include <netipsec/ah_var.h>
85 #include <netipsec/esp_var.h>
86 #include <netipsec/ipcomp.h> /*XXX*/
87 #include <netipsec/ipcomp_var.h>
89 #include <netipsec/key.h>
90 #include <netipsec/keydb.h>
91 #include <netipsec/key_debug.h>
93 #include <netipsec/xform.h>
95 #include <machine/in_cksum.h>
97 #include <opencrypto/cryptodev.h>
105 /* NB: name changed so netstat doesn't use it */
106 struct ipsecstat ipsec4stat;
107 int ip4_ah_offsetmask = 0; /* maybe IP_DF? */
108 int ip4_ipsec_dfbit = 0; /* DF bit on encap. 0: clear 1: set 2: copy */
109 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
110 int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
111 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
112 int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
113 struct secpolicy ip4_def_policy;
114 int ip4_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
115 int ip4_esp_randpad = -1;
117 * Crypto support requirements:
119 * 1 require hardware support
120 * -1 require software support
123 int crypto_support = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
125 SYSCTL_DECL(_net_inet_ipsec);
128 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY,
129 def_policy, CTLFLAG_RW, &ip4_def_policy.policy, 0, "");
130 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
131 CTLFLAG_RW, &ip4_esp_trans_deflev, 0, "");
132 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
133 CTLFLAG_RW, &ip4_esp_net_deflev, 0, "");
134 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
135 CTLFLAG_RW, &ip4_ah_trans_deflev, 0, "");
136 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
137 CTLFLAG_RW, &ip4_ah_net_deflev, 0, "");
138 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS,
139 ah_cleartos, CTLFLAG_RW, &ah_cleartos, 0, "");
140 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK,
141 ah_offsetmask, CTLFLAG_RW, &ip4_ah_offsetmask, 0, "");
142 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DFBIT,
143 dfbit, CTLFLAG_RW, &ip4_ipsec_dfbit, 0, "");
144 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ECN,
145 ecn, CTLFLAG_RW, &ip4_ipsec_ecn, 0, "");
146 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_DEBUG,
147 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
148 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ESP_RANDPAD,
149 esp_randpad, CTLFLAG_RW, &ip4_esp_randpad, 0, "");
150 SYSCTL_INT(_net_inet_ipsec, OID_AUTO,
151 crypto_support, CTLFLAG_RW, &crypto_support,0, "");
152 SYSCTL_STRUCT(_net_inet_ipsec, OID_AUTO,
153 ipsecstats, CTLFLAG_RD, &ipsec4stat, ipsecstat, "");
157 * When set to 1, IPsec will send packets with the same sequence number.
158 * This allows to verify if the other side has proper replay attacks detection.
160 int ipsec_replay = 0;
161 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay, CTLFLAG_RW, &ipsec_replay, 0,
162 "Emulate replay attack");
164 * When set 1, IPsec will send packets with corrupted HMAC.
165 * This allows to verify if the other side properly detects modified packets.
167 int ipsec_integrity = 0;
168 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity, CTLFLAG_RW,
169 &ipsec_integrity, 0, "Emulate man-in-the-middle attack");
173 struct ipsecstat ipsec6stat;
174 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
175 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
176 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
177 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
178 int ip6_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
179 int ip6_esp_randpad = -1;
181 SYSCTL_DECL(_net_inet6_ipsec6);
183 /* net.inet6.ipsec6 */
185 SYSCTL_OID(_net_inet6_ipsec6, IPSECCTL_STATS, stats, CTLFLAG_RD,
186 0,0, compat_ipsecstats_sysctl, "S", "");
187 #endif /* COMPAT_KAME */
188 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY,
189 def_policy, CTLFLAG_RW, &ip4_def_policy.policy, 0, "");
190 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
191 CTLFLAG_RW, &ip6_esp_trans_deflev, 0, "");
192 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
193 CTLFLAG_RW, &ip6_esp_net_deflev, 0, "");
194 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
195 CTLFLAG_RW, &ip6_ah_trans_deflev, 0, "");
196 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
197 CTLFLAG_RW, &ip6_ah_net_deflev, 0, "");
198 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ECN,
199 ecn, CTLFLAG_RW, &ip6_ipsec_ecn, 0, "");
200 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG,
201 debug, CTLFLAG_RW, &ipsec_debug, 0, "");
202 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ESP_RANDPAD,
203 esp_randpad, CTLFLAG_RW, &ip6_esp_randpad, 0, "");
204 SYSCTL_STRUCT(_net_inet6_ipsec6, IPSECCTL_STATS,
205 ipsecstats, CTLFLAG_RD, &ipsec6stat, ipsecstat, "");
208 static int ipsec4_setspidx_inpcb __P((struct mbuf *, struct inpcb *pcb));
210 static int ipsec6_setspidx_in6pcb __P((struct mbuf *, struct inpcb *pcb));
212 static int ipsec_setspidx __P((struct mbuf *, struct secpolicyindex *, int));
213 static void ipsec4_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
214 static int ipsec4_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
216 static void ipsec6_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
217 static int ipsec6_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
219 static void ipsec_delpcbpolicy __P((struct inpcbpolicy *));
220 static struct secpolicy *ipsec_deepcopy_policy __P((struct secpolicy *src));
221 static int ipsec_set_policy __P((struct secpolicy **pcb_sp,
222 int optname, caddr_t request, size_t len, struct ucred *cred));
223 static int ipsec_get_policy __P((struct secpolicy *pcb_sp, struct mbuf **mp));
224 static void vshiftl __P((unsigned char *, int, int));
225 static size_t ipsec_hdrsiz __P((struct secpolicy *));
227 MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy");
230 * Return a held reference to the default SP.
232 static struct secpolicy *
233 key_allocsp_default(const char* where, int tag)
235 struct secpolicy *sp;
237 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
238 printf("DP key_allocsp_default from %s:%u\n", where, tag));
240 sp = &ip4_def_policy;
241 if (sp->policy != IPSEC_POLICY_DISCARD &&
242 sp->policy != IPSEC_POLICY_NONE) {
243 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
244 sp->policy, IPSEC_POLICY_NONE));
245 sp->policy = IPSEC_POLICY_NONE;
249 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
250 printf("DP key_allocsp_default returns SP:%p (%u)\n",
254 #define KEY_ALLOCSP_DEFAULT() \
255 key_allocsp_default(__FILE__, __LINE__)
258 * For OUTBOUND packet having a socket. Searching SPD for packet,
259 * and return a pointer to SP.
260 * OUT: NULL: no apropreate SP found, the following value is set to error.
262 * EACCES : discard packet.
263 * ENOENT : ipsec_acquire() in progress, maybe.
264 * others : error occured.
265 * others: a pointer to SP
267 * NOTE: IPv6 mapped adddress concern is implemented here.
270 ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
272 struct secpolicy *sp;
274 IPSEC_ASSERT(tdbi != NULL, ("null tdbi"));
275 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
276 ("invalid direction %u", dir));
278 sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
279 if (sp == NULL) /*XXX????*/
280 sp = KEY_ALLOCSP_DEFAULT();
281 IPSEC_ASSERT(sp != NULL, ("null SP"));
286 * For OUTBOUND packet having a socket. Searching SPD for packet,
287 * and return a pointer to SP.
288 * OUT: NULL: no apropreate SP found, the following value is set to error.
290 * EACCES : discard packet.
291 * ENOENT : ipsec_acquire() in progress, maybe.
292 * others : error occured.
293 * others: a pointer to SP
295 * NOTE: IPv6 mapped adddress concern is implemented here.
298 ipsec_getpolicybysock(m, dir, inp, error)
304 struct inpcbpolicy *pcbsp = NULL;
305 struct secpolicy *currsp = NULL; /* policy on socket */
306 struct secpolicy *sp;
308 IPSEC_ASSERT(m != NULL, ("null mbuf"));
309 IPSEC_ASSERT(inp != NULL, ("null inpcb"));
310 IPSEC_ASSERT(error != NULL, ("null error"));
311 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
312 ("invalid direction %u", dir));
314 /* set spidx in pcb */
315 if (inp->inp_vflag & INP_IPV6PROTO) {
317 *error = ipsec6_setspidx_in6pcb(m, inp);
320 *error = EINVAL; /* should not happen */
323 *error = ipsec4_setspidx_inpcb(m, inp);
329 IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
331 case IPSEC_DIR_INBOUND:
332 currsp = pcbsp->sp_in;
334 case IPSEC_DIR_OUTBOUND:
335 currsp = pcbsp->sp_out;
338 IPSEC_ASSERT(currsp != NULL, ("null currsp"));
340 if (pcbsp->priv) { /* when privilieged socket */
341 switch (currsp->policy) {
342 case IPSEC_POLICY_BYPASS:
343 case IPSEC_POLICY_IPSEC:
348 case IPSEC_POLICY_ENTRUST:
349 /* look for a policy in SPD */
350 sp = KEY_ALLOCSP(&currsp->spidx, dir);
351 if (sp == NULL) /* no SP found */
352 sp = KEY_ALLOCSP_DEFAULT();
356 ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
357 __func__, currsp->policy));
361 } else { /* unpriv, SPD has policy */
362 sp = KEY_ALLOCSP(&currsp->spidx, dir);
363 if (sp == NULL) { /* no SP found */
364 switch (currsp->policy) {
365 case IPSEC_POLICY_BYPASS:
366 ipseclog((LOG_ERR, "%s: Illegal policy for "
367 "non-priviliged defined %d\n",
368 __func__, currsp->policy));
372 case IPSEC_POLICY_ENTRUST:
373 sp = KEY_ALLOCSP_DEFAULT();
376 case IPSEC_POLICY_IPSEC:
382 ipseclog((LOG_ERR, "%s: Invalid policy for "
383 "PCB %d\n", __func__, currsp->policy));
389 IPSEC_ASSERT(sp != NULL,
390 ("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
391 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
392 printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
393 __func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
398 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
399 * and return a pointer to SP.
400 * OUT: positive: a pointer to the entry for security policy leaf matched.
401 * NULL: no apropreate SP found, the following value is set to error.
403 * EACCES : discard packet.
404 * ENOENT : ipsec_acquire() in progress, maybe.
405 * others : error occured.
408 ipsec_getpolicybyaddr(m, dir, flag, error)
414 struct secpolicyindex spidx;
415 struct secpolicy *sp;
417 IPSEC_ASSERT(m != NULL, ("null mbuf"));
418 IPSEC_ASSERT(error != NULL, ("null error"));
419 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
420 ("invalid direction %u", dir));
423 if (key_havesp(dir)) {
424 /* Make an index to look for a policy. */
425 *error = ipsec_setspidx(m, &spidx,
426 (flag & IP_FORWARDING) ? 0 : 1);
428 DPRINTF(("%s: setpidx failed, dir %u flag %u\n",
429 __func__, dir, flag));
434 sp = KEY_ALLOCSP(&spidx, dir);
436 if (sp == NULL) /* no SP found, use system default */
437 sp = KEY_ALLOCSP_DEFAULT();
438 IPSEC_ASSERT(sp != NULL, ("null SP"));
443 ipsec4_checkpolicy(m, dir, flag, error, inp)
449 struct secpolicy *sp;
453 sp = ipsec_getpolicybyaddr(m, dir, flag, error);
455 sp = ipsec_getpolicybysock(m, dir, inp, error);
457 IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
458 ipsec4stat.ips_out_inval++;
461 IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
462 switch (sp->policy) {
463 case IPSEC_POLICY_ENTRUST:
465 printf("%s: invalid policy %u\n", __func__, sp->policy);
467 case IPSEC_POLICY_DISCARD:
468 ipsec4stat.ips_out_polvio++;
469 *error = -EINVAL; /* packet is discarded by caller */
471 case IPSEC_POLICY_BYPASS:
472 case IPSEC_POLICY_NONE:
474 sp = NULL; /* NB: force NULL result */
476 case IPSEC_POLICY_IPSEC:
477 if (sp->req == NULL) /* acquire an SA */
478 *error = key_spdacquire(sp);
489 ipsec4_setspidx_inpcb(m, pcb)
495 IPSEC_ASSERT(pcb != NULL, ("null pcb"));
496 IPSEC_ASSERT(pcb->inp_sp != NULL, ("null inp_sp"));
497 IPSEC_ASSERT(pcb->inp_sp->sp_out != NULL && pcb->inp_sp->sp_in != NULL,
498 ("null sp_in || sp_out"));
500 error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
502 pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
503 pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
504 pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
506 bzero(&pcb->inp_sp->sp_in->spidx,
507 sizeof (pcb->inp_sp->sp_in->spidx));
508 bzero(&pcb->inp_sp->sp_out->spidx,
509 sizeof (pcb->inp_sp->sp_in->spidx));
516 ipsec6_setspidx_in6pcb(m, pcb)
520 struct secpolicyindex *spidx;
523 IPSEC_ASSERT(pcb != NULL, ("null pcb"));
524 IPSEC_ASSERT(pcb->inp_sp != NULL, ("null inp_sp"));
525 IPSEC_ASSERT(pcb->inp_sp->sp_out != NULL && pcb->inp_sp->sp_in != NULL,
526 ("null sp_in || sp_out"));
528 bzero(&pcb->inp_sp->sp_in->spidx, sizeof(*spidx));
529 bzero(&pcb->inp_sp->sp_out->spidx, sizeof(*spidx));
531 spidx = &pcb->inp_sp->sp_in->spidx;
532 error = ipsec_setspidx(m, spidx, 1);
535 spidx->dir = IPSEC_DIR_INBOUND;
537 spidx = &pcb->inp_sp->sp_out->spidx;
538 error = ipsec_setspidx(m, spidx, 1);
541 spidx->dir = IPSEC_DIR_OUTBOUND;
546 bzero(&pcb->inp_sp->sp_in->spidx, sizeof(*spidx));
547 bzero(&pcb->inp_sp->sp_out->spidx, sizeof(*spidx));
553 * configure security policy index (src/dst/proto/sport/dport)
554 * by looking at the content of mbuf.
555 * the caller is responsible for error recovery (like clearing up spidx).
558 ipsec_setspidx(m, spidx, needport)
560 struct secpolicyindex *spidx;
563 struct ip *ip = NULL;
570 IPSEC_ASSERT(m != NULL, ("null mbuf"));
573 * validate m->m_pkthdr.len. we see incorrect length if we
574 * mistakenly call this function with inconsistent mbuf chain
575 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
578 for (n = m; n; n = n->m_next)
580 if (m->m_pkthdr.len != len) {
581 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
582 printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
583 __func__, len, m->m_pkthdr.len));
587 if (m->m_pkthdr.len < sizeof(struct ip)) {
588 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
589 printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
590 __func__, m->m_pkthdr.len));
594 if (m->m_len >= sizeof(*ip))
595 ip = mtod(m, struct ip *);
597 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
601 v = _IP_VHL_V(ip->ip_vhl);
607 error = ipsec4_setspidx_ipaddr(m, spidx);
610 ipsec4_get_ulp(m, spidx, needport);
614 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
615 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
616 printf("%s: pkthdr len(%d) too small (v6), "
617 "ignored\n", __func__, m->m_pkthdr.len));
620 error = ipsec6_setspidx_ipaddr(m, spidx);
623 ipsec6_get_ulp(m, spidx, needport);
627 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
628 printf("%s: " "unknown IP version %u, ignored.\n",
635 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
641 IPSEC_ASSERT(m != NULL, ("null mbuf"));
642 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
644 /* NB: ip_input() flips it into host endian XXX need more checking */
645 if (m->m_len < sizeof (struct ip)) {
646 struct ip *ip = mtod(m, struct ip *);
647 if (ip->ip_off & (IP_MF | IP_OFFMASK))
650 off = _IP_VHL_HL(ip->ip_vhl) << 2;
652 off = ip->ip_hl << 2;
658 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
659 if (ih.ip_off & (IP_MF | IP_OFFMASK))
662 off = _IP_VHL_HL(ih.ip_vhl) << 2;
669 while (off < m->m_pkthdr.len) {
676 spidx->ul_proto = nxt;
679 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
681 m_copydata(m, off, sizeof (th), (caddr_t) &th);
682 spidx->src.sin.sin_port = th.th_sport;
683 spidx->dst.sin.sin_port = th.th_dport;
686 spidx->ul_proto = nxt;
689 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
691 m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
692 spidx->src.sin.sin_port = uh.uh_sport;
693 spidx->dst.sin.sin_port = uh.uh_dport;
696 if (off + sizeof(ip6e) > m->m_pkthdr.len)
698 /* XXX sigh, this works but is totally bogus */
699 m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
700 off += (ip6e.ip6e_len + 2) << 2;
705 /* XXX intermediate headers??? */
706 spidx->ul_proto = nxt;
711 spidx->ul_proto = IPSEC_ULPROTO_ANY;
713 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
714 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
717 /* assumes that m is sane */
719 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
721 static const struct sockaddr_in template = {
722 sizeof (struct sockaddr_in),
724 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
727 spidx->src.sin = template;
728 spidx->dst.sin = template;
730 if (m->m_len < sizeof (struct ip)) {
731 m_copydata(m, offsetof(struct ip, ip_src),
732 sizeof (struct in_addr),
733 (caddr_t) &spidx->src.sin.sin_addr);
734 m_copydata(m, offsetof(struct ip, ip_dst),
735 sizeof (struct in_addr),
736 (caddr_t) &spidx->dst.sin.sin_addr);
738 struct ip *ip = mtod(m, struct ip *);
739 spidx->src.sin.sin_addr = ip->ip_src;
740 spidx->dst.sin.sin_addr = ip->ip_dst;
743 spidx->prefs = sizeof(struct in_addr) << 3;
744 spidx->prefd = sizeof(struct in_addr) << 3;
751 ipsec6_get_ulp(m, spidx, needport)
753 struct secpolicyindex *spidx;
763 panic("%s: NULL pointer was passed.\n", __func__);
765 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
766 printf("%s:\n", __func__); kdebug_mbuf(m));
769 spidx->ul_proto = IPSEC_ULPROTO_ANY;
770 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
771 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
774 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
775 if (off < 0 || m->m_pkthdr.len < off)
780 spidx->ul_proto = nxt;
783 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
785 m_copydata(m, off, sizeof(th), (caddr_t)&th);
786 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
787 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
790 spidx->ul_proto = nxt;
793 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
795 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
796 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
797 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
800 spidx->ul_proto = nxt;
801 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
803 m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
804 ((struct sockaddr_in6 *)&spidx->src)->sin6_port =
805 htons((uint16_t)ih.icmp6_type);
806 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
807 htons((uint16_t)ih.icmp6_code);
810 /* XXX intermediate headers??? */
811 spidx->ul_proto = nxt;
816 /* assumes that m is sane */
818 ipsec6_setspidx_ipaddr(m, spidx)
820 struct secpolicyindex *spidx;
822 struct ip6_hdr *ip6 = NULL;
823 struct ip6_hdr ip6buf;
824 struct sockaddr_in6 *sin6;
826 if (m->m_len >= sizeof(*ip6))
827 ip6 = mtod(m, struct ip6_hdr *);
829 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
833 sin6 = (struct sockaddr_in6 *)&spidx->src;
834 bzero(sin6, sizeof(*sin6));
835 sin6->sin6_family = AF_INET6;
836 sin6->sin6_len = sizeof(struct sockaddr_in6);
837 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
838 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
839 sin6->sin6_addr.s6_addr16[1] = 0;
840 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
842 spidx->prefs = sizeof(struct in6_addr) << 3;
844 sin6 = (struct sockaddr_in6 *)&spidx->dst;
845 bzero(sin6, sizeof(*sin6));
846 sin6->sin6_family = AF_INET6;
847 sin6->sin6_len = sizeof(struct sockaddr_in6);
848 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
849 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
850 sin6->sin6_addr.s6_addr16[1] = 0;
851 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
853 spidx->prefd = sizeof(struct in6_addr) << 3;
860 ipsec_delpcbpolicy(p)
861 struct inpcbpolicy *p;
863 free(p, M_IPSEC_INPCB);
866 /* initialize policy in PCB */
868 ipsec_init_policy(so, pcb_sp)
870 struct inpcbpolicy **pcb_sp;
872 struct inpcbpolicy *new;
875 if (so == NULL || pcb_sp == NULL)
876 panic("%s: NULL pointer was passed.\n", __func__);
878 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
879 M_IPSEC_INPCB, M_NOWAIT|M_ZERO);
881 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
885 new->priv = IPSEC_IS_PRIVILEGED_SO(so);
887 if ((new->sp_in = KEY_NEWSP()) == NULL) {
888 ipsec_delpcbpolicy(new);
891 new->sp_in->state = IPSEC_SPSTATE_ALIVE;
892 new->sp_in->policy = IPSEC_POLICY_ENTRUST;
894 if ((new->sp_out = KEY_NEWSP()) == NULL) {
895 KEY_FREESP(&new->sp_in);
896 ipsec_delpcbpolicy(new);
899 new->sp_out->state = IPSEC_SPSTATE_ALIVE;
900 new->sp_out->policy = IPSEC_POLICY_ENTRUST;
907 /* copy old ipsec policy into new */
909 ipsec_copy_policy(old, new)
910 struct inpcbpolicy *old, *new;
912 struct secpolicy *sp;
914 sp = ipsec_deepcopy_policy(old->sp_in);
916 KEY_FREESP(&new->sp_in);
921 sp = ipsec_deepcopy_policy(old->sp_out);
923 KEY_FREESP(&new->sp_out);
928 new->priv = old->priv;
933 struct ipsecrequest *
936 struct ipsecrequest *p;
938 p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
940 IPSECREQUEST_LOCK_INIT(p);
945 ipsec_delisr(struct ipsecrequest *p)
947 IPSECREQUEST_LOCK_DESTROY(p);
951 /* deep-copy a policy in PCB */
952 static struct secpolicy *
953 ipsec_deepcopy_policy(src)
954 struct secpolicy *src;
956 struct ipsecrequest *newchain = NULL;
957 struct ipsecrequest *p;
958 struct ipsecrequest **q;
959 struct ipsecrequest *r;
960 struct secpolicy *dst;
969 * deep-copy IPsec request chain. This is required since struct
970 * ipsecrequest is not reference counted.
973 for (p = src->req; p; p = p->next) {
977 (*q)->saidx.proto = p->saidx.proto;
978 (*q)->saidx.mode = p->saidx.mode;
979 (*q)->level = p->level;
980 (*q)->saidx.reqid = p->saidx.reqid;
982 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
983 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
991 dst->state = src->state;
992 dst->policy = src->policy;
993 /* do not touch the refcnt fields */
998 for (p = newchain; p; p = r) {
1006 /* set policy and ipsec request if present. */
1008 ipsec_set_policy(pcb_sp, optname, request, len, cred)
1009 struct secpolicy **pcb_sp;
1015 struct sadb_x_policy *xpl;
1016 struct secpolicy *newsp = NULL;
1020 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
1022 if (len < sizeof(*xpl))
1024 xpl = (struct sadb_x_policy *)request;
1026 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1027 printf("%s: passed policy\n", __func__);
1028 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
1030 /* check policy type */
1031 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1032 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
1033 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1036 /* check privileged socket */
1037 if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1038 error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0);
1043 /* allocation new SP entry */
1044 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1047 newsp->state = IPSEC_SPSTATE_ALIVE;
1049 /* clear old SP and set new SP */
1052 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1053 printf("%s: new policy\n", __func__);
1054 kdebug_secpolicy(newsp));
1060 ipsec_get_policy(pcb_sp, mp)
1061 struct secpolicy *pcb_sp;
1066 if (pcb_sp == NULL || mp == NULL)
1069 *mp = key_sp2msg(pcb_sp);
1071 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
1075 (*mp)->m_type = MT_DATA;
1076 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1077 printf("%s:\n", __func__); kdebug_mbuf(*mp));
1083 ipsec4_set_policy(inp, optname, request, len, cred)
1090 struct sadb_x_policy *xpl;
1091 struct secpolicy **pcb_sp;
1094 if (inp == NULL || request == NULL)
1096 if (len < sizeof(*xpl))
1098 xpl = (struct sadb_x_policy *)request;
1100 /* select direction */
1101 switch (xpl->sadb_x_policy_dir) {
1102 case IPSEC_DIR_INBOUND:
1103 pcb_sp = &inp->inp_sp->sp_in;
1105 case IPSEC_DIR_OUTBOUND:
1106 pcb_sp = &inp->inp_sp->sp_out;
1109 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1110 xpl->sadb_x_policy_dir));
1114 return ipsec_set_policy(pcb_sp, optname, request, len, cred);
1118 ipsec4_get_policy(inp, request, len, mp)
1124 struct sadb_x_policy *xpl;
1125 struct secpolicy *pcb_sp;
1128 if (inp == NULL || request == NULL || mp == NULL)
1130 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
1131 if (len < sizeof(*xpl))
1133 xpl = (struct sadb_x_policy *)request;
1135 /* select direction */
1136 switch (xpl->sadb_x_policy_dir) {
1137 case IPSEC_DIR_INBOUND:
1138 pcb_sp = inp->inp_sp->sp_in;
1140 case IPSEC_DIR_OUTBOUND:
1141 pcb_sp = inp->inp_sp->sp_out;
1144 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1145 xpl->sadb_x_policy_dir));
1149 return ipsec_get_policy(pcb_sp, mp);
1152 /* delete policy in PCB */
1154 ipsec_delete_pcbpolicy(inp)
1157 IPSEC_ASSERT(inp != NULL, ("null inp"));
1159 if (inp->inp_sp == NULL)
1162 if (inp->inp_sp->sp_in != NULL)
1163 KEY_FREESP(&inp->inp_sp->sp_in);
1165 if (inp->inp_sp->sp_out != NULL)
1166 KEY_FREESP(&inp->inp_sp->sp_out);
1168 ipsec_delpcbpolicy(inp->inp_sp);
1176 ipsec6_set_policy(in6p, optname, request, len, cred)
1183 struct sadb_x_policy *xpl;
1184 struct secpolicy **pcb_sp;
1187 if (in6p == NULL || request == NULL)
1189 if (len < sizeof(*xpl))
1191 xpl = (struct sadb_x_policy *)request;
1193 /* select direction */
1194 switch (xpl->sadb_x_policy_dir) {
1195 case IPSEC_DIR_INBOUND:
1196 pcb_sp = &in6p->inp_sp->sp_in;
1198 case IPSEC_DIR_OUTBOUND:
1199 pcb_sp = &in6p->inp_sp->sp_out;
1202 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1203 xpl->sadb_x_policy_dir));
1207 return ipsec_set_policy(pcb_sp, optname, request, len, cred);
1211 ipsec6_get_policy(in6p, request, len, mp)
1217 struct sadb_x_policy *xpl;
1218 struct secpolicy *pcb_sp;
1221 if (in6p == NULL || request == NULL || mp == NULL)
1223 IPSEC_ASSERT(in6p->inp_sp != NULL, ("null inp_sp"));
1224 if (len < sizeof(*xpl))
1226 xpl = (struct sadb_x_policy *)request;
1228 /* select direction */
1229 switch (xpl->sadb_x_policy_dir) {
1230 case IPSEC_DIR_INBOUND:
1231 pcb_sp = in6p->inp_sp->sp_in;
1233 case IPSEC_DIR_OUTBOUND:
1234 pcb_sp = in6p->inp_sp->sp_out;
1237 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1238 xpl->sadb_x_policy_dir));
1242 return ipsec_get_policy(pcb_sp, mp);
1247 * return current level.
1248 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1251 ipsec_get_reqlevel(isr)
1252 struct ipsecrequest *isr;
1255 u_int esp_trans_deflev, esp_net_deflev;
1256 u_int ah_trans_deflev, ah_net_deflev;
1258 IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
1259 IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1260 ("af family mismatch, src %u, dst %u",
1261 isr->sp->spidx.src.sa.sa_family,
1262 isr->sp->spidx.dst.sa.sa_family));
1264 /* XXX note that we have ipseclog() expanded here - code sync issue */
1265 #define IPSEC_CHECK_DEFAULT(lev) \
1266 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1267 && (lev) != IPSEC_LEVEL_UNIQUE) \
1269 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1270 (lev), IPSEC_LEVEL_REQUIRE) \
1272 (lev) = IPSEC_LEVEL_REQUIRE, \
1276 /* set default level */
1277 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1280 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1281 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1282 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1283 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1288 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1289 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1290 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1291 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1295 panic("%s: unknown af %u",
1296 __func__, isr->sp->spidx.src.sa.sa_family);
1299 #undef IPSEC_CHECK_DEFAULT
1302 switch (isr->level) {
1303 case IPSEC_LEVEL_DEFAULT:
1304 switch (isr->saidx.proto) {
1306 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1307 level = esp_net_deflev;
1309 level = esp_trans_deflev;
1312 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1313 level = ah_net_deflev;
1315 level = ah_trans_deflev;
1317 case IPPROTO_IPCOMP:
1319 * we don't really care, as IPcomp document says that
1320 * we shouldn't compress small packets
1322 level = IPSEC_LEVEL_USE;
1325 panic("%s: Illegal protocol defined %u\n", __func__,
1330 case IPSEC_LEVEL_USE:
1331 case IPSEC_LEVEL_REQUIRE:
1334 case IPSEC_LEVEL_UNIQUE:
1335 level = IPSEC_LEVEL_REQUIRE;
1339 panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
1346 * Check security policy requirements against the actual
1347 * packet contents. Return one if the packet should be
1348 * reject as "invalid"; otherwiser return zero to have the
1349 * packet treated as "valid".
1356 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1358 struct ipsecrequest *isr;
1361 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1362 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1365 switch (sp->policy) {
1366 case IPSEC_POLICY_DISCARD:
1368 case IPSEC_POLICY_BYPASS:
1369 case IPSEC_POLICY_NONE:
1373 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1374 ("invalid policy %u", sp->policy));
1376 /* XXX should compare policy against ipsec header history */
1379 for (isr = sp->req; isr != NULL; isr = isr->next) {
1380 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1382 switch (isr->saidx.proto) {
1384 if ((m->m_flags & M_DECRYPTED) == 0) {
1385 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1386 printf("%s: ESP m_flags:%x\n", __func__,
1393 isr->sav->tdb_authalgxform != NULL &&
1394 (m->m_flags & M_AUTHIPDGM) == 0) {
1395 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1396 printf("%s: ESP/AH m_flags:%x\n", __func__,
1403 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1404 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1405 printf("%s: AH m_flags:%x\n", __func__,
1410 case IPPROTO_IPCOMP:
1412 * we don't really care, as IPcomp document
1413 * says that we shouldn't compress small
1414 * packets, IPComp policy should always be
1415 * treated as being in "use" level.
1420 return 0; /* valid */
1424 * Check AH/ESP integrity.
1425 * This function is called from tcp_input(), udp_input(),
1426 * and {ah,esp}4_input for tunnel mode
1429 ipsec4_in_reject(m, inp)
1433 struct secpolicy *sp;
1437 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1439 /* get SP for this packet.
1440 * When we are called from ip_forward(), we call
1441 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1444 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1446 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1449 result = ipsec_in_reject(sp, m);
1451 ipsec4stat.ips_in_polvio++;
1454 result = 0; /* XXX should be panic ?
1455 * -> No, there may be error. */
1462 * Check AH/ESP integrity.
1463 * This function is called from tcp6_input(), udp6_input(),
1464 * and {ah,esp}6_input for tunnel mode
1467 ipsec6_in_reject(m, inp)
1471 struct secpolicy *sp = NULL;
1477 return 0; /* XXX should be panic ? */
1479 /* get SP for this packet.
1480 * When we are called from ip_forward(), we call
1481 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1484 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1486 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1489 result = ipsec_in_reject(sp, m);
1491 ipsec6stat.ips_in_polvio++;
1501 * compute the byte size to be occupied by IPsec header.
1502 * in case it is tunneled, it includes the size of outer IP header.
1503 * NOTE: SP passed is free in this function.
1506 ipsec_hdrsiz(struct secpolicy *sp)
1508 struct ipsecrequest *isr;
1511 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1512 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1514 switch (sp->policy) {
1515 case IPSEC_POLICY_DISCARD:
1516 case IPSEC_POLICY_BYPASS:
1517 case IPSEC_POLICY_NONE:
1521 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1522 ("invalid policy %u", sp->policy));
1525 for (isr = sp->req; isr != NULL; isr = isr->next) {
1528 switch (isr->saidx.proto) {
1530 clen = esp_hdrsiz(isr->sav);
1533 clen = ah_hdrsiz(isr->sav);
1535 case IPPROTO_IPCOMP:
1536 clen = sizeof(struct ipcomp);
1540 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1541 switch (isr->saidx.dst.sa.sa_family) {
1543 clen += sizeof(struct ip);
1547 clen += sizeof(struct ip6_hdr);
1551 ipseclog((LOG_ERR, "%s: unknown AF %d in "
1552 "IPsec tunnel SA\n", __func__,
1553 ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1563 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1565 ipsec4_hdrsiz(m, dir, inp)
1570 struct secpolicy *sp;
1574 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1576 /* get SP for this packet.
1577 * When we are called from ip_forward(), we call
1578 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1581 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1583 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1586 size = ipsec_hdrsiz(sp);
1587 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1588 printf("%s: size:%lu.\n", __func__,
1589 (unsigned long)size));
1593 size = 0; /* XXX should be panic ? */
1599 /* This function is called from ipsec6_hdrsize_tcp(),
1600 * and maybe from ip6_forward.()
1603 ipsec6_hdrsiz(m, dir, in6p)
1608 struct secpolicy *sp;
1612 IPSEC_ASSERT(m != NULL, ("null mbuf"));
1613 IPSEC_ASSERT(in6p == NULL || in6p->inp_socket != NULL,
1614 ("socket w/o inpcb"));
1616 /* get SP for this packet */
1617 /* XXX Is it right to call with IP_FORWARDING. */
1619 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1621 sp = ipsec_getpolicybysock(m, dir, in6p, &error);
1625 size = ipsec_hdrsiz(sp);
1626 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1627 printf("%s: size:%lu.\n", __func__, (unsigned long)size));
1635 * Check the variable replay window.
1636 * ipsec_chkreplay() performs replay check before ICV verification.
1637 * ipsec_updatereplay() updates replay bitmap. This must be called after
1638 * ICV verification (it also performs replay check, which is usually done
1640 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1642 * based on RFC 2401.
1645 ipsec_chkreplay(seq, sav)
1647 struct secasvar *sav;
1649 const struct secreplay *replay;
1652 u_int32_t wsizeb; /* constant: bits of window size */
1653 int frlast; /* constant: last frame */
1655 IPSEC_SPLASSERT_SOFTNET(__func__);
1657 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1658 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1660 replay = sav->replay;
1662 if (replay->wsize == 0)
1663 return 1; /* no need to check replay. */
1666 frlast = replay->wsize - 1;
1667 wsizeb = replay->wsize << 3;
1669 /* sequence number of 0 is invalid */
1673 /* first time is always okay */
1674 if (replay->count == 0)
1677 if (seq > replay->lastseq) {
1678 /* larger sequences are okay */
1681 /* seq is equal or less than lastseq. */
1682 diff = replay->lastseq - seq;
1684 /* over range to check, i.e. too old or wrapped */
1688 fr = frlast - diff / 8;
1690 /* this packet already seen ? */
1691 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1694 /* out of order but good */
1700 * check replay counter whether to update or not.
1705 ipsec_updatereplay(seq, sav)
1707 struct secasvar *sav;
1709 struct secreplay *replay;
1712 u_int32_t wsizeb; /* constant: bits of window size */
1713 int frlast; /* constant: last frame */
1715 IPSEC_SPLASSERT_SOFTNET(__func__);
1717 IPSEC_ASSERT(sav != NULL, ("Null SA"));
1718 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1720 replay = sav->replay;
1722 if (replay->wsize == 0)
1723 goto ok; /* no need to check replay. */
1726 frlast = replay->wsize - 1;
1727 wsizeb = replay->wsize << 3;
1729 /* sequence number of 0 is invalid */
1734 if (replay->count == 0) {
1735 replay->lastseq = seq;
1736 bzero(replay->bitmap, replay->wsize);
1737 (replay->bitmap)[frlast] = 1;
1741 if (seq > replay->lastseq) {
1742 /* seq is larger than lastseq. */
1743 diff = seq - replay->lastseq;
1745 /* new larger sequence number */
1746 if (diff < wsizeb) {
1748 /* set bit for this packet */
1749 vshiftl(replay->bitmap, diff, replay->wsize);
1750 (replay->bitmap)[frlast] |= 1;
1752 /* this packet has a "way larger" */
1753 bzero(replay->bitmap, replay->wsize);
1754 (replay->bitmap)[frlast] = 1;
1756 replay->lastseq = seq;
1758 /* larger is good */
1760 /* seq is equal or less than lastseq. */
1761 diff = replay->lastseq - seq;
1763 /* over range to check, i.e. too old or wrapped */
1767 fr = frlast - diff / 8;
1769 /* this packet already seen ? */
1770 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1774 (replay->bitmap)[fr] |= (1 << (diff % 8));
1776 /* out of order but good */
1780 if (replay->count == ~0) {
1782 /* set overflow flag */
1785 /* don't increment, no more packets accepted */
1786 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1789 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1790 __func__, replay->overflow, ipsec_logsastr(sav)));
1799 * shift variable length buffer to left.
1800 * IN: bitmap: pointer to the buffer
1801 * nbit: the number of to shift.
1802 * wsize: buffer size (bytes).
1805 vshiftl(bitmap, nbit, wsize)
1806 unsigned char *bitmap;
1812 for (j = 0; j < nbit; j += 8) {
1813 s = (nbit - j < 8) ? (nbit - j): 8;
1815 for (i = 1; i < wsize; i++) {
1816 over = (bitmap[i] >> (8 - s));
1818 bitmap[i-1] |= over;
1825 /* Return a printable string for the IPv4 address. */
1827 inet_ntoa4(struct in_addr ina)
1829 static char buf[4][4 * sizeof "123" + 4];
1830 unsigned char *ucp = (unsigned char *) &ina;
1833 /* XXX-BZ returns static buffer. */
1835 sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff,
1836 ucp[2] & 0xff, ucp[3] & 0xff);
1840 /* Return a printable string for the address. */
1842 ipsec_address(union sockaddr_union* sa)
1845 char ip6buf[INET6_ADDRSTRLEN];
1847 switch (sa->sa.sa_family) {
1850 return inet_ntoa4(sa->sin.sin_addr);
1855 return ip6_sprintf(ip6buf, &sa->sin6.sin6_addr);
1859 return "(unknown address family)";
1865 struct secasvar *sav;
1867 static char buf[256];
1869 struct secasindex *saidx = &sav->sah->saidx;
1871 IPSEC_ASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
1872 ("address family mismatch"));
1875 snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
1878 /* NB: only use ipsec_address on one address at a time */
1879 snprintf(p, sizeof (buf) - (p - buf), "src=%s ",
1880 ipsec_address(&saidx->src));
1883 snprintf(p, sizeof (buf) - (p - buf), "dst=%s)",
1884 ipsec_address(&saidx->dst));
1900 p = mtod(m, u_char *);
1901 for (i = 0; i < m->m_len; i++) {
1902 printf("%02x ", p[i]);
1904 if (totlen % 16 == 0)
1909 if (totlen % 16 != 0)
1917 SECPOLICY_LOCK_INIT(&ip4_def_policy);
1918 ip4_def_policy.refcnt = 1; /* NB: disallow free */
1920 SYSINIT(ipsec, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, ipsec_attach, NULL);
1923 /* XXX this stuff doesn't belong here... */
1925 static struct xformsw* xforms = NULL;
1928 * Register a transform; typically at system startup.
1931 xform_register(struct xformsw* xsp)
1933 xsp->xf_next = xforms;
1938 * Initialize transform support in an sav.
1941 xform_init(struct secasvar *sav, int xftype)
1943 struct xformsw *xsp;
1945 if (sav->tdb_xform != NULL) /* previously initialized */
1947 for (xsp = xforms; xsp; xsp = xsp->xf_next)
1948 if (xsp->xf_type == xftype)
1949 return (*xsp->xf_init)(sav, xsp);