1 /* $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $ */
3 * The authors of this code are John Ioannidis (ji@tla.org),
4 * Angelos D. Keromytis (kermit@csd.uch.gr) and
5 * Niels Provos (provos@physnet.uni-hamburg.de).
7 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
10 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
11 * by Angelos D. Keromytis.
13 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
16 * Additional features in 1999 by Angelos D. Keromytis.
18 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
19 * Angelos D. Keromytis and Niels Provos.
20 * Copyright (c) 2001, Angelos D. Keromytis.
21 * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
23 * Permission to use, copy, and modify this software with or without fee
24 * is hereby granted, provided that this entire notice is included in
25 * all copies of any software which is or includes a copy or
26 * modification of this software.
27 * You may use this code under the GNU public license if you so wish. Please
28 * contribute changes back to the authors under this freer than GPL license
29 * so that we may further the use of strong encryption without limitations to
32 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
33 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
34 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
35 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
40 * IPsec input processing.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 #include "opt_inet6.h"
48 #include "opt_ipsec.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
54 #include <sys/domain.h>
55 #include <sys/protosw.h>
56 #include <sys/socket.h>
57 #include <sys/errno.h>
58 #include <sys/hhook.h>
59 #include <sys/syslog.h>
62 #include <net/if_var.h>
63 #include <net/if_enc.h>
64 #include <net/netisr.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/ip_icmp.h>
72 #include <netinet/in_var.h>
73 #include <netinet/tcp_var.h>
75 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
79 #include <netinet/in_pcb.h>
81 #include <netinet/icmp6.h>
84 #include <netipsec/ipsec.h>
86 #include <netipsec/ipsec6.h>
88 #include <netipsec/ah_var.h>
89 #include <netipsec/esp.h>
90 #include <netipsec/esp_var.h>
91 #include <netipsec/ipcomp_var.h>
93 #include <netipsec/key.h>
94 #include <netipsec/keydb.h>
95 #include <netipsec/key_debug.h>
97 #include <netipsec/xform.h>
98 #include <netinet6/ip6protosw.h>
100 #include <machine/in_cksum.h>
101 #include <machine/stdarg.h>
103 #define IPSEC_ISTAT(proto, name) do { \
104 if ((proto) == IPPROTO_ESP) \
105 ESPSTAT_INC(esps_##name); \
106 else if ((proto) == IPPROTO_AH) \
107 AHSTAT_INC(ahs_##name); \
109 IPCOMPSTAT_INC(ipcomps_##name); \
113 * ipsec_common_input gets called when an IPsec-protected packet
114 * is received by IPv4 or IPv6. Its job is to find the right SA
115 * and call the appropriate transform. The transform callback
116 * takes care of further processing (like ingress filtering).
119 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
121 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
122 union sockaddr_union dst_address;
123 struct secasvar *sav;
127 IPSEC_ISTAT(sproto, input);
129 IPSEC_ASSERT(m != NULL, ("null packet"));
131 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
132 sproto == IPPROTO_IPCOMP,
133 ("unexpected security protocol %u", sproto));
135 if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
136 (sproto == IPPROTO_AH && !V_ah_enable) ||
137 (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
139 IPSEC_ISTAT(sproto, pdrops);
143 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
145 IPSEC_ISTAT(sproto, hdrops);
146 DPRINTF(("%s: packet too small\n", __func__));
150 /* Retrieve the SPI from the relevant IPsec header */
151 if (sproto == IPPROTO_ESP)
152 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
153 else if (sproto == IPPROTO_AH)
154 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
156 else if (sproto == IPPROTO_IPCOMP) {
158 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
160 spi = ntohl(htons(cpi));
164 * Find the SA and (indirectly) call the appropriate
165 * kernel crypto routine. The resulting mbuf chain is a valid
166 * IP packet ready to go through input processing.
168 bzero(&dst_address, sizeof (dst_address));
169 dst_address.sa.sa_family = af;
173 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
174 m_copydata(m, offsetof(struct ip, ip_dst),
175 sizeof(struct in_addr),
176 (caddr_t) &dst_address.sin.sin_addr);
181 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
182 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
183 sizeof(struct in6_addr),
184 (caddr_t) &dst_address.sin6.sin6_addr);
185 /* We keep addresses in SADB without embedded scope id */
186 if (IN6_IS_SCOPE_LINKLOCAL(&dst_address.sin6.sin6_addr)) {
187 /* XXX: sa6_recoverscope() */
188 dst_address.sin6.sin6_scope_id =
189 ntohs(dst_address.sin6.sin6_addr.s6_addr16[1]);
190 dst_address.sin6.sin6_addr.s6_addr16[1] = 0;
195 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
197 IPSEC_ISTAT(sproto, nopf);
201 /* NB: only pass dst since key_allocsa follows RFC2401 */
202 sav = key_allocsa(&dst_address, sproto, spi);
204 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
205 __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
206 (u_long) ntohl(spi), sproto));
207 IPSEC_ISTAT(sproto, notdb);
212 if (sav->tdb_xform == NULL) {
213 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
214 __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
215 (u_long) ntohl(spi), sproto));
216 IPSEC_ISTAT(sproto, noxform);
223 * Call appropriate transform and return -- callback takes care of
226 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
231 extern struct protosw inetsw[];
234 * IPSEC_INPUT() method implementation for IPv4.
235 * 0 - Permitted by inbound security policy for further processing.
236 * EACCES - Forbidden by inbound security policy.
237 * EINPROGRESS - consumed by IPsec.
240 ipsec4_input(struct mbuf *m, int offset, int proto)
247 /* Do inbound IPsec processing for AH/ESP/IPCOMP */
248 ipsec_common_input(m, offset,
249 offsetof(struct ip, ip_p), AF_INET, proto);
250 return (EINPROGRESS); /* mbuf consumed by IPsec */
253 * Protocols with further headers get their IPsec treatment
254 * within the protocol specific processing.
256 if ((inetsw[ip_protox[proto]].pr_flags & PR_LASTHDR) == 0)
261 * Enforce IPsec policy checking if we are seeing last header.
263 if (ipsec4_in_reject(m, NULL) != 0) {
264 /* Forbidden by inbound security policy */
272 ipsec4_ctlinput(int code, struct sockaddr *sa, void *v)
274 struct in_conninfo inc;
275 struct secasvar *sav;
281 if (code != PRC_MSGSIZE || ip == NULL)
283 if (sa->sa_family != AF_INET ||
284 sa->sa_len != sizeof(struct sockaddr_in))
285 return (EAFNOSUPPORT);
287 icp = __containerof(ip, struct icmp, icmp_ip);
288 pmtu = ntohs(icp->icmp_nextmtu);
290 if (pmtu < V_ip4_ipsec_min_pmtu)
294 if (proto != IPPROTO_ESP && proto != IPPROTO_AH &&
295 proto != IPPROTO_IPCOMP)
298 memcpy(&spi, (caddr_t)ip + (ip->ip_hl << 2), sizeof(spi));
299 sav = key_allocsa((union sockaddr_union *)sa, proto, spi);
305 memset(&inc, 0, sizeof(inc));
306 inc.inc_faddr = satosin(sa)->sin_addr;
307 tcp_hc_updatemtu(&inc, pmtu);
312 * IPsec input callback for INET protocols.
313 * This routine is called as the transform callback.
314 * Takes care of filtering and other sanity checks on
315 * the processed packet.
318 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
321 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
322 struct epoch_tracker et;
323 struct ipsec_ctx_data ctx;
324 struct xform_history *xh;
325 struct secasindex *saidx;
328 int error, prot, af, sproto, isr_prot;
330 IPSEC_ASSERT(sav != NULL, ("null SA"));
331 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
332 saidx = &sav->sah->saidx;
333 af = saidx->dst.sa.sa_family;
334 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
335 sproto = saidx->proto;
336 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
337 sproto == IPPROTO_IPCOMP,
338 ("unexpected security protocol %u", sproto));
344 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
345 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
346 __func__, ipsec_address(&sav->sah->saidx.dst,
347 buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
348 IPSEC_ISTAT(sproto, hdrops);
353 ip = mtod(m, struct ip *);
354 ip->ip_len = htons(m->m_pkthdr.len);
356 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
358 ip = mtod(m, struct ip *);
362 * Check that we have NAT-T enabled and apply transport mode
363 * decapsulation NAT procedure (RFC3948).
364 * Do this before invoking into the PFIL.
366 if (sav->natt != NULL &&
367 (prot == IPPROTO_UDP || prot == IPPROTO_TCP))
368 udp_ipsec_adjust_cksum(m, sav, prot, skip);
370 IPSEC_INIT_CTX(&ctx, &m, NULL, sav, AF_INET, IPSEC_ENC_BEFORE);
371 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
373 ip = mtod(m, struct ip *); /* update pointer */
375 /* IP-in-IP encapsulation */
376 if (prot == IPPROTO_IPIP &&
377 saidx->mode != IPSEC_MODE_TRANSPORT) {
378 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
379 IPSEC_ISTAT(sproto, hdrops);
383 /* enc0: strip outer IPv4 header */
384 m_striphdr(m, 0, ip->ip_hl << 2);
387 /* IPv6-in-IP encapsulation. */
388 else if (prot == IPPROTO_IPV6 &&
389 saidx->mode != IPSEC_MODE_TRANSPORT) {
390 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
391 IPSEC_ISTAT(sproto, hdrops);
395 /* enc0: strip IPv4 header, keep IPv6 header only */
396 m_striphdr(m, 0, ip->ip_hl << 2);
399 else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
401 * When mode is wildcard, inner protocol is IPv6 and
402 * we have no INET6 support - drop this packet a bit later.
403 * In other cases we assume transport mode. Set prot to
404 * correctly choose netisr.
410 * Record what we've done to the packet (under what SA it was
413 if (sproto != IPPROTO_IPCOMP) {
414 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
415 sizeof(struct xform_history), M_NOWAIT);
417 DPRINTF(("%s: failed to get tag\n", __func__));
418 IPSEC_ISTAT(sproto, hdrops);
423 xh = (struct xform_history *)(mtag + 1);
424 bcopy(&saidx->dst, &xh->dst, saidx->dst.sa.sa_len);
427 xh->mode = saidx->mode;
428 m_tag_prepend(m, mtag);
431 key_sa_recordxfer(sav, m); /* record data transfer */
434 * In transport mode requeue decrypted mbuf back to IPv4 protocol
435 * handler. This is necessary to correctly expose rcvif.
437 if (saidx->mode == IPSEC_MODE_TRANSPORT)
440 * Re-dispatch via software interrupt.
444 isr_prot = NETISR_IP;
449 isr_prot = NETISR_IPV6;
454 DPRINTF(("%s: cannot handle inner ip proto %d\n",
456 IPSEC_ISTAT(sproto, nopf);
457 error = EPFNOSUPPORT;
461 IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_AFTER);
462 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
465 /* Handle virtual tunneling interfaces */
466 if (saidx->mode == IPSEC_MODE_TUNNEL)
467 error = ipsec_if_input(m, sav, af);
470 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
473 IPSEC_ISTAT(sproto, qfull);
474 DPRINTF(("%s: queue full; proto %u packet dropped\n",
490 * IPSEC_INPUT() method implementation for IPv6.
491 * 0 - Permitted by inbound security policy for further processing.
492 * EACCES - Forbidden by inbound security policy.
493 * EINPROGRESS - consumed by IPsec.
496 ipsec6_input(struct mbuf *m, int offset, int proto)
503 /* Do inbound IPsec processing for AH/ESP/IPCOMP */
504 ipsec_common_input(m, offset,
505 offsetof(struct ip6_hdr, ip6_nxt), AF_INET6, proto);
506 return (EINPROGRESS); /* mbuf consumed by IPsec */
509 * Protocols with further headers get their IPsec treatment
510 * within the protocol specific processing.
512 if ((inet6sw[ip6_protox[proto]].pr_flags & PR_LASTHDR) == 0)
517 * Enforce IPsec policy checking if we are seeing last header.
519 if (ipsec6_in_reject(m, NULL) != 0) {
520 /* Forbidden by inbound security policy */
528 ipsec6_ctlinput(int code, struct sockaddr *sa, void *v)
534 * IPsec input callback, called by the transform callback. Takes care of
535 * filtering and other sanity checks on the processed packet.
538 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
541 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
542 struct epoch_tracker et;
543 struct ipsec_ctx_data ctx;
544 struct xform_history *xh;
545 struct secasindex *saidx;
548 int prot, af, sproto;
553 IPSEC_ASSERT(sav != NULL, ("null SA"));
554 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
555 saidx = &sav->sah->saidx;
556 af = saidx->dst.sa.sa_family;
557 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
558 sproto = saidx->proto;
559 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
560 sproto == IPPROTO_IPCOMP,
561 ("unexpected security protocol %u", sproto));
563 /* Fix IPv6 header */
564 if (m->m_len < sizeof(struct ip6_hdr) &&
565 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
566 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
567 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
568 sizeof(buf)), (u_long) ntohl(sav->spi)));
570 IPSEC_ISTAT(sproto, hdrops);
575 IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_BEFORE);
576 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
579 ip6 = mtod(m, struct ip6_hdr *);
580 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
583 m_copydata(m, protoff, 1, &nxt8);
586 /* IPv6-in-IP encapsulation */
587 if (prot == IPPROTO_IPV6 &&
588 saidx->mode != IPSEC_MODE_TRANSPORT) {
589 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
590 IPSEC_ISTAT(sproto, hdrops);
594 /* ip6n will now contain the inner IPv6 header. */
595 m_striphdr(m, 0, skip);
599 /* IP-in-IP encapsulation */
600 else if (prot == IPPROTO_IPIP &&
601 saidx->mode != IPSEC_MODE_TRANSPORT) {
602 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
603 IPSEC_ISTAT(sproto, hdrops);
607 /* ipn will now contain the inner IPv4 header */
608 m_striphdr(m, 0, skip);
613 prot = IPPROTO_IPV6; /* for correct BPF processing */
617 * Record what we've done to the packet (under what SA it was
620 if (sproto != IPPROTO_IPCOMP) {
621 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
622 sizeof(struct xform_history), M_NOWAIT);
624 DPRINTF(("%s: failed to get tag\n", __func__));
625 IPSEC_ISTAT(sproto, hdrops);
630 xh = (struct xform_history *)(mtag + 1);
631 bcopy(&saidx->dst, &xh->dst, saidx->dst.sa.sa_len);
634 xh->mode = saidx->mode;
635 m_tag_prepend(m, mtag);
638 key_sa_recordxfer(sav, m);
641 if (prot == IPPROTO_IPIP)
646 IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_AFTER);
647 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
651 * We stripped outer IPv6 header.
652 * Now we should requeue decrypted packet via netisr.
657 isr_prot = NETISR_IP;
661 isr_prot = NETISR_IPV6;
664 DPRINTF(("%s: cannot handle inner ip proto %d\n",
666 IPSEC_ISTAT(sproto, nopf);
667 error = EPFNOSUPPORT;
670 /* Handle virtual tunneling interfaces */
671 if (saidx->mode == IPSEC_MODE_TUNNEL)
672 error = ipsec_if_input(m, sav, af);
675 error = netisr_queue_src(isr_prot,
676 (uintptr_t)sav->spi, m);
679 IPSEC_ISTAT(sproto, qfull);
680 DPRINTF(("%s: queue full; proto %u packet"
681 " dropped\n", __func__, sproto));
688 * See the end of ip6_input for this logic.
689 * IPPROTO_IPV[46] case will be processed just like other ones
694 while (nxt != IPPROTO_DONE) {
695 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
696 IP6STAT_INC(ip6s_toomanyhdr);
702 * Protection against faulty packet - there should be
703 * more sanity checks in header chain processing.
705 if (m->m_pkthdr.len < skip) {
706 IP6STAT_INC(ip6s_tooshort);
707 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
712 * Enforce IPsec policy checking if we are seeing last header.
713 * note that we do not visit this with protocols with pcb layer
714 * code - like udp/tcp/raw ip.
716 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
717 ipsec6_in_reject(m, NULL)) {
721 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);