Home | History | Annotate | Line # | Download | only in netipsec
ipsec_input.c revision 1.72
      1 /*	$NetBSD: ipsec_input.c,v 1.72 2018/10/27 05:42:23 maxv Exp $	*/
      2 /*	$FreeBSD: ipsec_input.c,v 1.2.4.2 2003/03/28 20:32:53 sam Exp $	*/
      3 /*	$OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $	*/
      4 
      5 /*
      6  * The authors of this code are John Ioannidis (ji (at) tla.org),
      7  * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
      8  * Niels Provos (provos (at) physnet.uni-hamburg.de).
      9  *
     10  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
     11  * in November 1995.
     12  *
     13  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
     14  * by Angelos D. Keromytis.
     15  *
     16  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
     17  * and Niels Provos.
     18  *
     19  * Additional features in 1999 by Angelos D. Keromytis.
     20  *
     21  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
     22  * Angelos D. Keromytis and Niels Provos.
     23  * Copyright (c) 2001, Angelos D. Keromytis.
     24  *
     25  * Permission to use, copy, and modify this software with or without fee
     26  * is hereby granted, provided that this entire notice is included in
     27  * all copies of any software which is or includes a copy or
     28  * modification of this software.
     29  * You may use this code under the GNU public license if you so wish. Please
     30  * contribute changes back to the authors under this freer than GPL license
     31  * so that we may further the use of strong encryption without limitations to
     32  * all.
     33  *
     34  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
     35  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
     36  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
     37  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
     38  * PURPOSE.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: ipsec_input.c,v 1.72 2018/10/27 05:42:23 maxv Exp $");
     43 
     44 /*
     45  * IPsec input processing.
     46  */
     47 
     48 #if defined(_KERNEL_OPT)
     49 #include "opt_inet.h"
     50 #endif
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/domain.h>
     56 #include <sys/protosw.h>
     57 #include <sys/socket.h>
     58 #include <sys/errno.h>
     59 #include <sys/syslog.h>
     60 
     61 #include <net/if.h>
     62 #include <net/route.h>
     63 
     64 #include <netinet/in.h>
     65 #include <netinet/in_systm.h>
     66 #include <netinet/ip.h>
     67 #include <netinet/ip_var.h>
     68 #include <netinet/in_var.h>
     69 #include <netinet/in_proto.h>
     70 #include <netinet/udp.h>
     71 #include <netinet/tcp.h>
     72 
     73 #include <netinet/ip6.h>
     74 #ifdef INET6
     75 #include <netinet6/in6.h>
     76 #include <netinet6/ip6_var.h>
     77 #include <netinet6/ip6_private.h>
     78 #include <netinet6/scope6_var.h>
     79 #endif
     80 #include <netinet/in_pcb.h>
     81 
     82 #include <netipsec/ipsec.h>
     83 #include <netipsec/ipsec_private.h>
     84 #ifdef INET6
     85 #include <netipsec/ipsec6.h>
     86 #endif
     87 #include <netipsec/ah_var.h>
     88 #include <netipsec/esp.h>
     89 #include <netipsec/esp_var.h>
     90 #include <netipsec/ipcomp_var.h>
     91 
     92 #include <netipsec/key.h>
     93 #include <netipsec/keydb.h>
     94 
     95 #include <netipsec/xform.h>
     96 #include <netinet6/ip6protosw.h>
     97 
     98 #define	IPSEC_ISTAT(p, x, y, z)						\
     99 do {									\
    100 	switch (p) {							\
    101 	case IPPROTO_ESP:						\
    102 		ESP_STATINC(x);						\
    103 		break;							\
    104 	case IPPROTO_AH:						\
    105 		AH_STATINC(y);						\
    106 		break;							\
    107 	default:							\
    108 		IPCOMP_STATINC(z);					\
    109 		break;							\
    110 	}								\
    111 } while (/*CONSTCOND*/0)
    112 
    113 /*
    114  * fixup TCP/UDP checksum
    115  *
    116  * XXX: if we have NAT-OA payload from IKE server,
    117  *      we must do the differential update of checksum.
    118  *
    119  * XXX: NAT-OAi/NAT-OAr drived from IKE initiator/responder.
    120  *      how to know the IKE side from kernel?
    121  */
    122 static struct mbuf *
    123 ipsec4_fixup_checksum(struct mbuf *m)
    124 {
    125 	struct ip *ip;
    126 	struct tcphdr *th;
    127 	struct udphdr *uh;
    128 	int poff, off;
    129 	int plen;
    130 
    131 	if (m->m_len < sizeof(*ip)) {
    132 		m = m_pullup(m, sizeof(*ip));
    133 		if (m == NULL)
    134 			return NULL;
    135 	}
    136 	ip = mtod(m, struct ip *);
    137 	poff = ip->ip_hl << 2;
    138 	plen = ntohs(ip->ip_len) - poff;
    139 
    140 	switch (ip->ip_p) {
    141 	case IPPROTO_TCP:
    142 		M_REGION_GET(th, struct tcphdr *, m, poff, sizeof(*th));
    143 		if (th == NULL)
    144 			return NULL;
    145 		off = th->th_off << 2;
    146 		if (off < sizeof(*th) || off > plen) {
    147 			m_freem(m);
    148 			return NULL;
    149 		}
    150 		th->th_sum = 0;
    151 		th->th_sum = in4_cksum(m, IPPROTO_TCP, poff, plen);
    152 		break;
    153 	case IPPROTO_UDP:
    154 		M_REGION_GET(uh, struct udphdr *, m, poff, sizeof(*uh));
    155 		if (uh == NULL)
    156 			return NULL;
    157 		off = sizeof(*uh);
    158 		if (off > plen) {
    159 			m_freem(m);
    160 			return NULL;
    161 		}
    162 		uh->uh_sum = 0;
    163 		uh->uh_sum = in4_cksum(m, IPPROTO_UDP, poff, plen);
    164 		break;
    165 	default:
    166 		/* no checksum */
    167 		return m;
    168 	}
    169 
    170 	return m;
    171 }
    172 
    173 static void
    174 nat_t_ports_get(struct mbuf *m, uint16_t *dport, uint16_t *sport)
    175 {
    176 	struct m_tag *tag;
    177 
    178 	if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL))) {
    179 		*sport = ((uint16_t *)(tag + 1))[0];
    180 		*dport = ((uint16_t *)(tag + 1))[1];
    181 	} else
    182 		*sport = *dport = 0;
    183 }
    184 
    185 /*
    186  * ipsec_common_input gets called when an IPsec-protected packet
    187  * is received by IPv4 or IPv6.  Its job is to find the right SA
    188  * and call the appropriate transform.  The transform callback
    189  * takes care of further processing (like ingress filtering).
    190  */
    191 static int
    192 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
    193 {
    194 	char buf[IPSEC_ADDRSTRLEN];
    195 	union sockaddr_union dst_address;
    196 	struct secasvar *sav;
    197 	u_int32_t spi;
    198 	u_int16_t sport;
    199 	u_int16_t dport;
    200 	int s, error;
    201 
    202 	IPSEC_ISTAT(sproto, ESP_STAT_INPUT, AH_STAT_INPUT,
    203 		IPCOMP_STAT_INPUT);
    204 
    205 	KASSERT(m != NULL);
    206 
    207 	if ((sproto == IPPROTO_ESP && !esp_enable) ||
    208 	    (sproto == IPPROTO_AH && !ah_enable) ||
    209 	    (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
    210 		m_freem(m);
    211 		IPSEC_ISTAT(sproto, ESP_STAT_PDROPS, AH_STAT_PDROPS,
    212 		    IPCOMP_STAT_PDROPS);
    213 		return EOPNOTSUPP;
    214 	}
    215 
    216 	if (m->m_pkthdr.len - skip < 2 * sizeof(u_int32_t)) {
    217 		m_freem(m);
    218 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    219 		    IPCOMP_STAT_HDROPS);
    220 		IPSECLOG(LOG_DEBUG, "packet too small\n");
    221 		return EINVAL;
    222 	}
    223 
    224 	/* Retrieve the SPI from the relevant IPsec header */
    225 	if (sproto == IPPROTO_ESP) {
    226 		m_copydata(m, skip, sizeof(u_int32_t), &spi);
    227 	} else if (sproto == IPPROTO_AH) {
    228 		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), &spi);
    229 	} else if (sproto == IPPROTO_IPCOMP) {
    230 		u_int16_t cpi;
    231 		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), &cpi);
    232 		spi = ntohl(htons(cpi));
    233 	} else {
    234 		panic("%s called with bad protocol number: %d\n", __func__,
    235 		    sproto);
    236 	}
    237 
    238 	/* find the source port for NAT-T */
    239 	nat_t_ports_get(m, &dport, &sport);
    240 
    241 	/*
    242 	 * Find the SA and (indirectly) call the appropriate
    243 	 * kernel crypto routine. The resulting mbuf chain is a valid
    244 	 * IP packet ready to go through input processing.
    245 	 */
    246 	memset(&dst_address, 0, sizeof(dst_address));
    247 	dst_address.sa.sa_family = af;
    248 	switch (af) {
    249 #ifdef INET
    250 	case AF_INET:
    251 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
    252 		m_copydata(m, offsetof(struct ip, ip_dst),
    253 		    sizeof(struct in_addr),
    254 		    &dst_address.sin.sin_addr);
    255 		break;
    256 #endif
    257 #ifdef INET6
    258 	case AF_INET6:
    259 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
    260 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
    261 		    sizeof(struct in6_addr),
    262 		    &dst_address.sin6.sin6_addr);
    263 		if (sa6_recoverscope(&dst_address.sin6)) {
    264 			m_freem(m);
    265 			return EINVAL;
    266 		}
    267 		break;
    268 #endif
    269 	default:
    270 		IPSECLOG(LOG_DEBUG, "unsupported protocol family %u\n", af);
    271 		m_freem(m);
    272 		IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF,
    273 		    IPCOMP_STAT_NOPF);
    274 		return EPFNOSUPPORT;
    275 	}
    276 
    277 	s = splsoftnet();
    278 
    279 	/* NB: only pass dst since key_lookup_sa follows RFC2401 */
    280 	sav = KEY_LOOKUP_SA(&dst_address, sproto, spi, sport, dport);
    281 	if (sav == NULL) {
    282 		IPSECLOG(LOG_DEBUG,
    283 		    "no key association found for SA %s/%08lx/%u/%u\n",
    284 		    ipsec_address(&dst_address, buf, sizeof(buf)),
    285 		    (u_long) ntohl(spi), sproto, ntohs(dport));
    286 		IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB,
    287 		    IPCOMP_STAT_NOTDB);
    288 		splx(s);
    289 		m_freem(m);
    290 		return ENOENT;
    291 	}
    292 
    293 	KASSERT(sav->tdb_xform != NULL);
    294 
    295 	/*
    296 	 * Call appropriate transform and return -- callback takes care of
    297 	 * everything else.
    298 	 */
    299 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
    300 	KEY_SA_UNREF(&sav);
    301 	splx(s);
    302 	return error;
    303 }
    304 
    305 #ifdef INET
    306 /*
    307  * Common input handler for IPv4 AH, ESP, and IPCOMP.
    308  */
    309 void
    310 ipsec4_common_input(struct mbuf *m, int off, int proto)
    311 {
    312 	(void)ipsec_common_input(m, off, offsetof(struct ip, ip_p),
    313 	    AF_INET, proto);
    314 }
    315 
    316 /*
    317  * IPsec input callback for INET protocols.
    318  * This routine is called as the transform callback.
    319  * Takes care of filtering and other sanity checks on
    320  * the processed packet.
    321  */
    322 int
    323 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
    324     int skip, int protoff)
    325 {
    326 	int prot, af __diagused, sproto;
    327 	struct ip *ip;
    328 	struct secasindex *saidx;
    329 	int error;
    330 
    331 	if (__predict_false(m == NULL)) {
    332 		panic("%s: NULL mbuf", __func__);
    333 	}
    334 	if (__predict_false(skip < sizeof(struct ip))) {
    335 		panic("%s: short skip", __func__);
    336 	}
    337 
    338 	KASSERT(sav != NULL);
    339 	saidx = &sav->sah->saidx;
    340 	af = saidx->dst.sa.sa_family;
    341 	KASSERTMSG(af == AF_INET, "unexpected af %u", af);
    342 	sproto = saidx->proto;
    343 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
    344 	    sproto == IPPROTO_IPCOMP,
    345 	    "unexpected security protocol %u", sproto);
    346 
    347 	/*
    348 	 * Update the IPv4 header. The length of the packet may have changed,
    349 	 * so fix it, and recompute the checksum.
    350 	 */
    351 	if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
    352 		char buf[IPSEC_ADDRSTRLEN];
    353 cantpull:
    354 		IPSECLOG(LOG_DEBUG,
    355 		    "processing failed for SA %s/%08lx\n",
    356 		    ipsec_address(&sav->sah->saidx.dst, buf,
    357 		    sizeof(buf)), (u_long) ntohl(sav->spi));
    358 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    359 		    IPCOMP_STAT_HDROPS);
    360 		error = ENOBUFS;
    361 		goto bad;
    362 	}
    363 	ip = mtod(m, struct ip *);
    364 	ip->ip_len = htons(m->m_pkthdr.len);
    365 	ip->ip_sum = 0;
    366 	ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
    367 
    368 	/*
    369 	 * Update TCP/UDP checksum
    370 	 * XXX: should only do it in NAT-T case
    371 	 * XXX: should do it incrementally, see FreeBSD code.
    372 	 */
    373 	m = ipsec4_fixup_checksum(m);
    374 	if (m == NULL)
    375 		goto cantpull;
    376 	ip = mtod(m, struct ip *);
    377 
    378 	prot = ip->ip_p;
    379 
    380 	M_VERIFY_PACKET(m);
    381 
    382 	key_sa_recordxfer(sav, m);
    383 
    384 	if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
    385 	    ipsec_in_reject(m, NULL)) {
    386 		error = EINVAL;
    387 		goto bad;
    388 	}
    389 	(*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
    390 	return 0;
    391 
    392 bad:
    393 	m_freem(m);
    394 	return error;
    395 }
    396 #endif /* INET */
    397 
    398 #ifdef INET6
    399 int
    400 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
    401 {
    402 	int l = 0;
    403 	int protoff, nxt;
    404 	struct ip6_ext ip6e;
    405 
    406 	if (*offp < sizeof(struct ip6_hdr)) {
    407 		IPSECLOG(LOG_DEBUG, "bad offset %u\n", *offp);
    408 		IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    409 			    IPCOMP_STAT_HDROPS);
    410 		m_freem(*mp);
    411 		return IPPROTO_DONE;
    412 	} else if (*offp == sizeof(struct ip6_hdr)) {
    413 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
    414 	} else {
    415 		/* Chase down the header chain... */
    416 		protoff = sizeof(struct ip6_hdr);
    417 		nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
    418 
    419 		do {
    420 			protoff += l;
    421 			m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
    422 
    423 			if (nxt == IPPROTO_AH)
    424 				l = (ip6e.ip6e_len + 2) << 2;
    425 			else if (nxt == IPPROTO_FRAGMENT)
    426 				l = sizeof(struct ip6_frag);
    427 			else
    428 				l = (ip6e.ip6e_len + 1) << 3;
    429 			KASSERT(l > 0);
    430 
    431 			nxt = ip6e.ip6e_nxt;
    432 		} while (protoff + l < *offp);
    433 
    434 		/* Malformed packet check */
    435 		if (protoff + l != *offp) {
    436 			IPSECLOG(LOG_DEBUG, "bad packet header chain, "
    437 			    "protoff %u, l %u, off %u\n", protoff, l, *offp);
    438 			IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
    439 				    AH_STAT_HDROPS,
    440 				    IPCOMP_STAT_HDROPS);
    441 			m_freem(*mp);
    442 			*mp = NULL;
    443 			return IPPROTO_DONE;
    444 		}
    445 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
    446 	}
    447 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
    448 	return IPPROTO_DONE;
    449 }
    450 
    451 /*
    452  * IPsec input callback, called by the transform callback. Takes care of
    453  * filtering and other sanity checks on the processed packet.
    454  */
    455 int
    456 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
    457     int protoff)
    458 {
    459 	int af __diagused, sproto;
    460 	struct ip6_hdr *ip6;
    461 	struct secasindex *saidx;
    462 	int nxt;
    463 	u_int8_t prot;
    464 	int error, nest;
    465 
    466 	if (__predict_false(m == NULL)) {
    467 		panic("%s: NULL mbuf", __func__);
    468 	}
    469 
    470 	KASSERT(sav != NULL);
    471 	saidx = &sav->sah->saidx;
    472 	af = saidx->dst.sa.sa_family;
    473 	KASSERTMSG(af == AF_INET6, "unexpected af %u", af);
    474 	sproto = saidx->proto;
    475 	KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
    476 	    sproto == IPPROTO_IPCOMP,
    477 	    "unexpected security protocol %u", sproto);
    478 
    479 	/* Fix IPv6 header */
    480 	if (m->m_len < sizeof(struct ip6_hdr) &&
    481 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    482 		char buf[IPSEC_ADDRSTRLEN];
    483 		IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n",
    484 		    ipsec_address(&sav->sah->saidx.dst,
    485 		    buf, sizeof(buf)), (u_long) ntohl(sav->spi));
    486 		IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
    487 		    IPCOMP_STAT_HDROPS);
    488 		error = EACCES;
    489 		goto bad;
    490 	}
    491 
    492 	ip6 = mtod(m, struct ip6_hdr *);
    493 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
    494 
    495 	m_copydata(m, protoff, sizeof(prot), &prot);
    496 
    497 	key_sa_recordxfer(sav, m);
    498 
    499 	/*
    500 	 * See the end of ip6_input for this logic.
    501 	 * IPPROTO_IPV[46] case will be processed just like other ones
    502 	 */
    503 	nest = 0;
    504 	nxt = prot;
    505 	while (nxt != IPPROTO_DONE) {
    506 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    507 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
    508 			error = EINVAL;
    509 			goto bad;
    510 		}
    511 
    512 		M_VERIFY_PACKET(m);
    513 
    514 		/*
    515 		 * Protection against faulty packet - there should be
    516 		 * more sanity checks in header chain processing.
    517 		 */
    518 		if (m->m_pkthdr.len < skip) {
    519 			IP6_STATINC(IP6_STAT_TOOSHORT);
    520 			in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
    521 			    ifs6_in_truncated);
    522 			error = EINVAL;
    523 			goto bad;
    524 		}
    525 
    526 		/*
    527 		 * Enforce IPsec policy checking if we are seeing last header.
    528 		 * Note that we do not visit this with protocols with pcb layer
    529 		 * code - like udp/tcp/raw ip.
    530 		 */
    531 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
    532 		    ipsec_in_reject(m, NULL)) {
    533 			error = EINVAL;
    534 			goto bad;
    535 		}
    536 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
    537 	}
    538 	return 0;
    539 
    540 bad:
    541 	if (m)
    542 		m_freem(m);
    543 	return error;
    544 }
    545 #endif /* INET6 */
    546