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/in_var.h>
73 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
77 #include <netinet/in_pcb.h>
79 #include <netinet/icmp6.h>
82 #include <netipsec/ipsec.h>
84 #include <netipsec/ipsec6.h>
86 #include <netipsec/ah_var.h>
87 #include <netipsec/esp.h>
88 #include <netipsec/esp_var.h>
89 #include <netipsec/ipcomp_var.h>
91 #include <netipsec/key.h>
92 #include <netipsec/keydb.h>
93 #include <netipsec/key_debug.h>
95 #include <netipsec/xform.h>
96 #include <netinet6/ip6protosw.h>
98 #include <machine/in_cksum.h>
99 #include <machine/stdarg.h>
101 #define IPSEC_ISTAT(proto, name) do { \
102 if ((proto) == IPPROTO_ESP) \
103 ESPSTAT_INC(esps_##name); \
104 else if ((proto) == IPPROTO_AH) \
105 AHSTAT_INC(ahs_##name); \
107 IPCOMPSTAT_INC(ipcomps_##name); \
111 * ipsec_common_input gets called when an IPsec-protected packet
112 * is received by IPv4 or IPv6. Its job is to find the right SA
113 * and call the appropriate transform. The transform callback
114 * takes care of further processing (like ingress filtering).
117 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
119 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
120 union sockaddr_union dst_address;
121 struct secasvar *sav;
125 IPSEC_ISTAT(sproto, input);
127 IPSEC_ASSERT(m != NULL, ("null packet"));
129 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
130 sproto == IPPROTO_IPCOMP,
131 ("unexpected security protocol %u", sproto));
133 if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
134 (sproto == IPPROTO_AH && !V_ah_enable) ||
135 (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
137 IPSEC_ISTAT(sproto, pdrops);
141 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
143 IPSEC_ISTAT(sproto, hdrops);
144 DPRINTF(("%s: packet too small\n", __func__));
148 /* Retrieve the SPI from the relevant IPsec header */
149 if (sproto == IPPROTO_ESP)
150 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
151 else if (sproto == IPPROTO_AH)
152 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
154 else if (sproto == IPPROTO_IPCOMP) {
156 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
158 spi = ntohl(htons(cpi));
162 * Find the SA and (indirectly) call the appropriate
163 * kernel crypto routine. The resulting mbuf chain is a valid
164 * IP packet ready to go through input processing.
166 bzero(&dst_address, sizeof (dst_address));
167 dst_address.sa.sa_family = af;
171 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
172 m_copydata(m, offsetof(struct ip, ip_dst),
173 sizeof(struct in_addr),
174 (caddr_t) &dst_address.sin.sin_addr);
179 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
180 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
181 sizeof(struct in6_addr),
182 (caddr_t) &dst_address.sin6.sin6_addr);
183 /* We keep addresses in SADB without embedded scope id */
184 if (IN6_IS_SCOPE_LINKLOCAL(&dst_address.sin6.sin6_addr)) {
185 /* XXX: sa6_recoverscope() */
186 dst_address.sin6.sin6_scope_id =
187 ntohs(dst_address.sin6.sin6_addr.s6_addr16[1]);
188 dst_address.sin6.sin6_addr.s6_addr16[1] = 0;
193 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
195 IPSEC_ISTAT(sproto, nopf);
199 /* NB: only pass dst since key_allocsa follows RFC2401 */
200 sav = key_allocsa(&dst_address, sproto, spi);
202 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
203 __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
204 (u_long) ntohl(spi), sproto));
205 IPSEC_ISTAT(sproto, notdb);
210 if (sav->tdb_xform == NULL) {
211 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
212 __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
213 (u_long) ntohl(spi), sproto));
214 IPSEC_ISTAT(sproto, noxform);
221 * Call appropriate transform and return -- callback takes care of
224 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
229 extern struct protosw inetsw[];
232 * IPSEC_INPUT() method implementation for IPv4.
233 * 0 - Permitted by inbound security policy for further processing.
234 * EACCES - Forbidden by inbound security policy.
235 * EINPROGRESS - consumed by IPsec.
238 ipsec4_input(struct mbuf *m, int offset, int proto)
245 /* Do inbound IPsec processing for AH/ESP/IPCOMP */
246 ipsec_common_input(m, offset,
247 offsetof(struct ip, ip_p), AF_INET, proto);
248 return (EINPROGRESS); /* mbuf consumed by IPsec */
251 * Protocols with further headers get their IPsec treatment
252 * within the protocol specific processing.
254 if ((inetsw[ip_protox[proto]].pr_flags & PR_LASTHDR) == 0)
259 * Enforce IPsec policy checking if we are seeing last header.
261 if (ipsec4_in_reject(m, NULL) != 0) {
262 /* Forbidden by inbound security policy */
270 * IPsec input callback for INET protocols.
271 * This routine is called as the transform callback.
272 * Takes care of filtering and other sanity checks on
273 * the processed packet.
276 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
279 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
280 struct epoch_tracker et;
281 struct ipsec_ctx_data ctx;
282 struct xform_history *xh;
283 struct secasindex *saidx;
286 int error, prot, af, sproto, isr_prot;
288 IPSEC_ASSERT(sav != NULL, ("null SA"));
289 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
290 saidx = &sav->sah->saidx;
291 af = saidx->dst.sa.sa_family;
292 IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
293 sproto = saidx->proto;
294 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
295 sproto == IPPROTO_IPCOMP,
296 ("unexpected security protocol %u", sproto));
302 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
303 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
304 __func__, ipsec_address(&sav->sah->saidx.dst,
305 buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
306 IPSEC_ISTAT(sproto, hdrops);
311 ip = mtod(m, struct ip *);
312 ip->ip_len = htons(m->m_pkthdr.len);
314 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
316 ip = mtod(m, struct ip *);
320 * Check that we have NAT-T enabled and apply transport mode
321 * decapsulation NAT procedure (RFC3948).
322 * Do this before invoking into the PFIL.
324 if (sav->natt != NULL &&
325 (prot == IPPROTO_UDP || prot == IPPROTO_TCP))
326 udp_ipsec_adjust_cksum(m, sav, prot, skip);
328 IPSEC_INIT_CTX(&ctx, &m, NULL, sav, AF_INET, IPSEC_ENC_BEFORE);
329 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
331 ip = mtod(m, struct ip *); /* update pointer */
333 /* IP-in-IP encapsulation */
334 if (prot == IPPROTO_IPIP &&
335 saidx->mode != IPSEC_MODE_TRANSPORT) {
336 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
337 IPSEC_ISTAT(sproto, hdrops);
341 /* enc0: strip outer IPv4 header */
342 m_striphdr(m, 0, ip->ip_hl << 2);
345 /* IPv6-in-IP encapsulation. */
346 else if (prot == IPPROTO_IPV6 &&
347 saidx->mode != IPSEC_MODE_TRANSPORT) {
348 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
349 IPSEC_ISTAT(sproto, hdrops);
353 /* enc0: strip IPv4 header, keep IPv6 header only */
354 m_striphdr(m, 0, ip->ip_hl << 2);
357 else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
359 * When mode is wildcard, inner protocol is IPv6 and
360 * we have no INET6 support - drop this packet a bit later.
361 * In other cases we assume transport mode. Set prot to
362 * correctly choose netisr.
368 * Record what we've done to the packet (under what SA it was
371 if (sproto != IPPROTO_IPCOMP) {
372 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
373 sizeof(struct xform_history), M_NOWAIT);
375 DPRINTF(("%s: failed to get tag\n", __func__));
376 IPSEC_ISTAT(sproto, hdrops);
381 xh = (struct xform_history *)(mtag + 1);
382 bcopy(&saidx->dst, &xh->dst, saidx->dst.sa.sa_len);
385 xh->mode = saidx->mode;
386 m_tag_prepend(m, mtag);
389 key_sa_recordxfer(sav, m); /* record data transfer */
392 * In transport mode requeue decrypted mbuf back to IPv4 protocol
393 * handler. This is necessary to correctly expose rcvif.
395 if (saidx->mode == IPSEC_MODE_TRANSPORT)
398 * Re-dispatch via software interrupt.
402 isr_prot = NETISR_IP;
407 isr_prot = NETISR_IPV6;
412 DPRINTF(("%s: cannot handle inner ip proto %d\n",
414 IPSEC_ISTAT(sproto, nopf);
415 error = EPFNOSUPPORT;
419 IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_AFTER);
420 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
423 /* Handle virtual tunneling interfaces */
424 if (saidx->mode == IPSEC_MODE_TUNNEL)
425 error = ipsec_if_input(m, sav, af);
428 error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
431 IPSEC_ISTAT(sproto, qfull);
432 DPRINTF(("%s: queue full; proto %u packet dropped\n",
448 * IPSEC_INPUT() method implementation for IPv6.
449 * 0 - Permitted by inbound security policy for further processing.
450 * EACCES - Forbidden by inbound security policy.
451 * EINPROGRESS - consumed by IPsec.
454 ipsec6_input(struct mbuf *m, int offset, int proto)
461 /* Do inbound IPsec processing for AH/ESP/IPCOMP */
462 ipsec_common_input(m, offset,
463 offsetof(struct ip6_hdr, ip6_nxt), AF_INET6, proto);
464 return (EINPROGRESS); /* mbuf consumed by IPsec */
467 * Protocols with further headers get their IPsec treatment
468 * within the protocol specific processing.
470 if ((inet6sw[ip6_protox[proto]].pr_flags & PR_LASTHDR) == 0)
475 * Enforce IPsec policy checking if we are seeing last header.
477 if (ipsec6_in_reject(m, NULL) != 0) {
478 /* Forbidden by inbound security policy */
486 * IPsec input callback, called by the transform callback. Takes care of
487 * filtering and other sanity checks on the processed packet.
490 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
493 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
494 struct epoch_tracker et;
495 struct ipsec_ctx_data ctx;
496 struct xform_history *xh;
497 struct secasindex *saidx;
500 int prot, af, sproto;
505 IPSEC_ASSERT(sav != NULL, ("null SA"));
506 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
507 saidx = &sav->sah->saidx;
508 af = saidx->dst.sa.sa_family;
509 IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
510 sproto = saidx->proto;
511 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
512 sproto == IPPROTO_IPCOMP,
513 ("unexpected security protocol %u", sproto));
515 /* Fix IPv6 header */
516 if (m->m_len < sizeof(struct ip6_hdr) &&
517 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
518 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
519 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
520 sizeof(buf)), (u_long) ntohl(sav->spi)));
522 IPSEC_ISTAT(sproto, hdrops);
527 IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_BEFORE);
528 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
531 ip6 = mtod(m, struct ip6_hdr *);
532 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
535 m_copydata(m, protoff, 1, &nxt8);
538 /* IPv6-in-IP encapsulation */
539 if (prot == IPPROTO_IPV6 &&
540 saidx->mode != IPSEC_MODE_TRANSPORT) {
541 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
542 IPSEC_ISTAT(sproto, hdrops);
546 /* ip6n will now contain the inner IPv6 header. */
547 m_striphdr(m, 0, skip);
551 /* IP-in-IP encapsulation */
552 else if (prot == IPPROTO_IPIP &&
553 saidx->mode != IPSEC_MODE_TRANSPORT) {
554 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
555 IPSEC_ISTAT(sproto, hdrops);
559 /* ipn will now contain the inner IPv4 header */
560 m_striphdr(m, 0, skip);
565 prot = IPPROTO_IPV6; /* for correct BPF processing */
569 * Record what we've done to the packet (under what SA it was
572 if (sproto != IPPROTO_IPCOMP) {
573 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
574 sizeof(struct xform_history), M_NOWAIT);
576 DPRINTF(("%s: failed to get tag\n", __func__));
577 IPSEC_ISTAT(sproto, hdrops);
582 xh = (struct xform_history *)(mtag + 1);
583 bcopy(&saidx->dst, &xh->dst, saidx->dst.sa.sa_len);
586 xh->mode = saidx->mode;
587 m_tag_prepend(m, mtag);
590 key_sa_recordxfer(sav, m);
593 if (prot == IPPROTO_IPIP)
598 IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_AFTER);
599 if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
603 * We stripped outer IPv6 header.
604 * Now we should requeue decrypted packet via netisr.
609 isr_prot = NETISR_IP;
613 isr_prot = NETISR_IPV6;
616 DPRINTF(("%s: cannot handle inner ip proto %d\n",
618 IPSEC_ISTAT(sproto, nopf);
619 error = EPFNOSUPPORT;
622 /* Handle virtual tunneling interfaces */
623 if (saidx->mode == IPSEC_MODE_TUNNEL)
624 error = ipsec_if_input(m, sav, af);
627 error = netisr_queue_src(isr_prot,
628 (uintptr_t)sav->spi, m);
631 IPSEC_ISTAT(sproto, qfull);
632 DPRINTF(("%s: queue full; proto %u packet"
633 " dropped\n", __func__, sproto));
640 * See the end of ip6_input for this logic.
641 * IPPROTO_IPV[46] case will be processed just like other ones
646 while (nxt != IPPROTO_DONE) {
647 if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
648 IP6STAT_INC(ip6s_toomanyhdr);
654 * Protection against faulty packet - there should be
655 * more sanity checks in header chain processing.
657 if (m->m_pkthdr.len < skip) {
658 IP6STAT_INC(ip6s_tooshort);
659 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
664 * Enforce IPsec policy checking if we are seeing last header.
665 * note that we do not visit this with protocols with pcb layer
666 * code - like udp/tcp/raw ip.
668 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
669 ipsec6_in_reject(m, NULL)) {
673 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);