xref: /freebsd/sys/netipsec/ipsec_input.c (revision 3157ba21)
1 /*	$FreeBSD$	*/
2 /*	$OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $	*/
3 /*-
4  * The authors of this code are John Ioannidis (ji@tla.org),
5  * Angelos D. Keromytis (kermit@csd.uch.gr) and
6  * Niels Provos (provos@physnet.uni-hamburg.de).
7  *
8  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9  * in November 1995.
10  *
11  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12  * by Angelos D. Keromytis.
13  *
14  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15  * and Niels Provos.
16  *
17  * Additional features in 1999 by Angelos D. Keromytis.
18  *
19  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
20  * Angelos D. Keromytis and Niels Provos.
21  * Copyright (c) 2001, Angelos D. Keromytis.
22  *
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
30  * all.
31  *
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
36  * PURPOSE.
37  */
38 
39 /*
40  * IPsec input processing.
41  */
42 
43 #include "opt_inet.h"
44 #include "opt_inet6.h"
45 #include "opt_ipsec.h"
46 #include "opt_enc.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
57 
58 #include <net/if.h>
59 #include <net/pfil.h>
60 #include <net/route.h>
61 #include <net/netisr.h>
62 #include <net/vnet.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 
70 #include <netinet/ip6.h>
71 #ifdef INET6
72 #include <netinet6/ip6_var.h>
73 #endif
74 #include <netinet/in_pcb.h>
75 #ifdef INET6
76 #include <netinet/icmp6.h>
77 #endif
78 
79 #include <netipsec/ipsec.h>
80 #ifdef INET6
81 #include <netipsec/ipsec6.h>
82 #endif
83 #include <netipsec/ah_var.h>
84 #include <netipsec/esp.h>
85 #include <netipsec/esp_var.h>
86 #include <netipsec/ipcomp_var.h>
87 
88 #include <netipsec/key.h>
89 #include <netipsec/keydb.h>
90 
91 #include <netipsec/xform.h>
92 #include <netinet6/ip6protosw.h>
93 
94 #include <machine/in_cksum.h>
95 #include <machine/stdarg.h>
96 
97 #ifdef DEV_ENC
98 #include <net/if_enc.h>
99 #endif
100 
101 
102 #define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \
103 			    (p) == IPPROTO_AH ? (y)++ : (z)++)
104 
105 #ifdef INET
106 static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
107 #endif
108 
109 /*
110  * ipsec_common_input gets called when an IPsec-protected packet
111  * is received by IPv4 or IPv6.  It's job is to find the right SA
112  * and call the appropriate transform.  The transform callback
113  * takes care of further processing (like ingress filtering).
114  */
115 static int
116 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
117 {
118 	union sockaddr_union dst_address;
119 	struct secasvar *sav;
120 	u_int32_t spi;
121 	int error;
122 #ifdef IPSEC_NAT_T
123 	struct m_tag *tag;
124 #endif
125 
126 	IPSEC_ISTAT(sproto, V_espstat.esps_input, V_ahstat.ahs_input,
127 		V_ipcompstat.ipcomps_input);
128 
129 	IPSEC_ASSERT(m != NULL, ("null packet"));
130 
131 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
132 		sproto == IPPROTO_IPCOMP,
133 		("unexpected security protocol %u", sproto));
134 
135 	if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
136 	    (sproto == IPPROTO_AH && !V_ah_enable) ||
137 	    (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
138 		m_freem(m);
139 		IPSEC_ISTAT(sproto, V_espstat.esps_pdrops, V_ahstat.ahs_pdrops,
140 		    V_ipcompstat.ipcomps_pdrops);
141 		return EOPNOTSUPP;
142 	}
143 
144 	if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
145 		m_freem(m);
146 		IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops,
147 		    V_ipcompstat.ipcomps_hdrops);
148 		DPRINTF(("%s: packet too small\n", __func__));
149 		return EINVAL;
150 	}
151 
152 	/* Retrieve the SPI from the relevant IPsec header */
153 	if (sproto == IPPROTO_ESP)
154 		m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
155 	else if (sproto == IPPROTO_AH)
156 		m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
157 		    (caddr_t) &spi);
158 	else if (sproto == IPPROTO_IPCOMP) {
159 		u_int16_t cpi;
160 		m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
161 		    (caddr_t) &cpi);
162 		spi = ntohl(htons(cpi));
163 	}
164 
165 	/*
166 	 * Find the SA and (indirectly) call the appropriate
167 	 * kernel crypto routine. The resulting mbuf chain is a valid
168 	 * IP packet ready to go through input processing.
169 	 */
170 	bzero(&dst_address, sizeof (dst_address));
171 	dst_address.sa.sa_family = af;
172 	switch (af) {
173 #ifdef INET
174 	case AF_INET:
175 		dst_address.sin.sin_len = sizeof(struct sockaddr_in);
176 		m_copydata(m, offsetof(struct ip, ip_dst),
177 		    sizeof(struct in_addr),
178 		    (caddr_t) &dst_address.sin.sin_addr);
179 #ifdef IPSEC_NAT_T
180 		/* Find the source port for NAT-T; see udp*_espdecap. */
181 		tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL);
182 		if (tag != NULL)
183 			dst_address.sin.sin_port = ((u_int16_t *)(tag + 1))[1];
184 #endif /* IPSEC_NAT_T */
185 		break;
186 #endif /* INET */
187 #ifdef INET6
188 	case AF_INET6:
189 		dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
190 		m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
191 		    sizeof(struct in6_addr),
192 		    (caddr_t) &dst_address.sin6.sin6_addr);
193 		break;
194 #endif /* INET6 */
195 	default:
196 		DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
197 		m_freem(m);
198 		IPSEC_ISTAT(sproto, V_espstat.esps_nopf, V_ahstat.ahs_nopf,
199 		    V_ipcompstat.ipcomps_nopf);
200 		return EPFNOSUPPORT;
201 	}
202 
203 	/* NB: only pass dst since key_allocsa follows RFC2401 */
204 	sav = KEY_ALLOCSA(&dst_address, sproto, spi);
205 	if (sav == NULL) {
206 		DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
207 			  __func__, ipsec_address(&dst_address),
208 			  (u_long) ntohl(spi), sproto));
209 		IPSEC_ISTAT(sproto, V_espstat.esps_notdb, V_ahstat.ahs_notdb,
210 		    V_ipcompstat.ipcomps_notdb);
211 		m_freem(m);
212 		return ENOENT;
213 	}
214 
215 	if (sav->tdb_xform == NULL) {
216 		DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
217 			 __func__, ipsec_address(&dst_address),
218 			 (u_long) ntohl(spi), sproto));
219 		IPSEC_ISTAT(sproto, V_espstat.esps_noxform, V_ahstat.ahs_noxform,
220 		    V_ipcompstat.ipcomps_noxform);
221 		KEY_FREESAV(&sav);
222 		m_freem(m);
223 		return ENXIO;
224 	}
225 
226 	/*
227 	 * Call appropriate transform and return -- callback takes care of
228 	 * everything else.
229 	 */
230 	error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
231 	KEY_FREESAV(&sav);
232 	return error;
233 }
234 
235 #ifdef INET
236 /*
237  * Common input handler for IPv4 AH, ESP, and IPCOMP.
238  */
239 int
240 ipsec4_common_input(struct mbuf *m, ...)
241 {
242 	va_list ap;
243 	int off, nxt;
244 
245 	va_start(ap, m);
246 	off = va_arg(ap, int);
247 	nxt = va_arg(ap, int);
248 	va_end(ap);
249 
250 	return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
251 				  AF_INET, nxt);
252 }
253 
254 void
255 ah4_input(struct mbuf *m, int off)
256 {
257 	ipsec4_common_input(m, off, IPPROTO_AH);
258 }
259 void
260 ah4_ctlinput(int cmd, struct sockaddr *sa, void *v)
261 {
262 	if (sa->sa_family == AF_INET &&
263 	    sa->sa_len == sizeof(struct sockaddr_in))
264 		ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_AH);
265 }
266 
267 void
268 esp4_input(struct mbuf *m, int off)
269 {
270 	ipsec4_common_input(m, off, IPPROTO_ESP);
271 }
272 void
273 esp4_ctlinput(int cmd, struct sockaddr *sa, void *v)
274 {
275 	if (sa->sa_family == AF_INET &&
276 	    sa->sa_len == sizeof(struct sockaddr_in))
277 		ipsec4_common_ctlinput(cmd, sa, v, IPPROTO_ESP);
278 }
279 
280 void
281 ipcomp4_input(struct mbuf *m, int off)
282 {
283 	ipsec4_common_input(m, off, IPPROTO_IPCOMP);
284 }
285 
286 /*
287  * IPsec input callback for INET protocols.
288  * This routine is called as the transform callback.
289  * Takes care of filtering and other sanity checks on
290  * the processed packet.
291  */
292 int
293 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
294 			int skip, int protoff, struct m_tag *mt)
295 {
296 	int prot, af, sproto;
297 	struct ip *ip;
298 	struct m_tag *mtag;
299 	struct tdb_ident *tdbi;
300 	struct secasindex *saidx;
301 	int error;
302 #ifdef INET6
303 #ifdef notyet
304 	char ip6buf[INET6_ADDRSTRLEN];
305 #endif
306 #endif
307 
308 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
309 	IPSEC_ASSERT(sav != NULL, ("null SA"));
310 	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
311 	saidx = &sav->sah->saidx;
312 	af = saidx->dst.sa.sa_family;
313 	IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
314 	sproto = saidx->proto;
315 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
316 		sproto == IPPROTO_IPCOMP,
317 		("unexpected security protocol %u", sproto));
318 
319 	/* Sanity check */
320 	if (m == NULL) {
321 		DPRINTF(("%s: null mbuf", __func__));
322 		IPSEC_ISTAT(sproto, V_espstat.esps_badkcr, V_ahstat.ahs_badkcr,
323 		    V_ipcompstat.ipcomps_badkcr);
324 		KEY_FREESAV(&sav);
325 		return EINVAL;
326 	}
327 
328 	if (skip != 0) {
329 		/* Fix IPv4 header */
330 		if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
331 			DPRINTF(("%s: processing failed for SA %s/%08lx\n",
332 			    __func__, ipsec_address(&sav->sah->saidx.dst),
333 			    (u_long) ntohl(sav->spi)));
334 			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops,
335 			    V_ipcompstat.ipcomps_hdrops);
336 			error = ENOBUFS;
337 			goto bad;
338 		}
339 
340 		ip = mtod(m, struct ip *);
341 		ip->ip_len = htons(m->m_pkthdr.len);
342 		ip->ip_off = htons(ip->ip_off);
343 		ip->ip_sum = 0;
344 		ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
345 	} else {
346 		ip = mtod(m, struct ip *);
347 	}
348 	prot = ip->ip_p;
349 
350 #ifdef notyet
351 	/* IP-in-IP encapsulation */
352 	if (prot == IPPROTO_IPIP) {
353 		struct ip ipn;
354 
355 		if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
356 			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
357 			    V_ahstat.ahs_hdrops,
358 			    V_ipcompstat.ipcomps_hdrops);
359 			error = EINVAL;
360 			goto bad;
361 		}
362 		/* ipn will now contain the inner IPv4 header */
363 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip),
364 		    (caddr_t) &ipn);
365 
366 		/* XXX PROXY address isn't recorded in SAH */
367 		/*
368 		 * Check that the inner source address is the same as
369 		 * the proxy address, if available.
370 		 */
371 		if ((saidx->proxy.sa.sa_family == AF_INET &&
372 		    saidx->proxy.sin.sin_addr.s_addr !=
373 		    INADDR_ANY &&
374 		    ipn.ip_src.s_addr !=
375 		    saidx->proxy.sin.sin_addr.s_addr) ||
376 		    (saidx->proxy.sa.sa_family != AF_INET &&
377 			saidx->proxy.sa.sa_family != 0)) {
378 
379 			DPRINTF(("%s: inner source address %s doesn't "
380 			    "correspond to expected proxy source %s, "
381 			    "SA %s/%08lx\n", __func__,
382 			    inet_ntoa4(ipn.ip_src),
383 			    ipsp_address(saidx->proxy),
384 			    ipsp_address(saidx->dst),
385 			    (u_long) ntohl(sav->spi)));
386 
387 			IPSEC_ISTAT(sproto, V_espstat.esps_pdrops,
388 			    V_ahstat.ahs_pdrops,
389 			    V_ipcompstat.ipcomps_pdrops);
390 			error = EACCES;
391 			goto bad;
392 		}
393 	}
394 #ifdef INET6
395 	/* IPv6-in-IP encapsulation. */
396 	if (prot == IPPROTO_IPV6) {
397 		struct ip6_hdr ip6n;
398 
399 		if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
400 			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
401 			    V_ahstat.ahs_hdrops,
402 			    V_ipcompstat.ipcomps_hdrops);
403 			error = EINVAL;
404 			goto bad;
405 		}
406 		/* ip6n will now contain the inner IPv6 header. */
407 		m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr),
408 		    (caddr_t) &ip6n);
409 
410 		/*
411 		 * Check that the inner source address is the same as
412 		 * the proxy address, if available.
413 		 */
414 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
415 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
416 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
417 			&saidx->proxy.sin6.sin6_addr)) ||
418 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
419 			saidx->proxy.sa.sa_family != 0)) {
420 
421 			DPRINTF(("%s: inner source address %s doesn't "
422 			    "correspond to expected proxy source %s, "
423 			    "SA %s/%08lx\n", __func__,
424 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
425 			    ipsec_address(&saidx->proxy),
426 			    ipsec_address(&saidx->dst),
427 			    (u_long) ntohl(sav->spi)));
428 
429 			IPSEC_ISTAT(sproto, V_espstat.esps_pdrops,
430 			    V_ahstat.ahs_pdrops,
431 			    V_ipcompstat.ipcomps_pdrops);
432 			error = EACCES;
433 			goto bad;
434 		}
435 	}
436 #endif /* INET6 */
437 #endif /*XXX*/
438 
439 	/*
440 	 * Record what we've done to the packet (under what SA it was
441 	 * processed). If we've been passed an mtag, it means the packet
442 	 * was already processed by an ethernet/crypto combo card and
443 	 * thus has a tag attached with all the right information, but
444 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
445 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
446 	 */
447 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
448 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
449 		    sizeof(struct tdb_ident), M_NOWAIT);
450 		if (mtag == NULL) {
451 			DPRINTF(("%s: failed to get tag\n", __func__));
452 			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
453 			    V_ahstat.ahs_hdrops, V_ipcompstat.ipcomps_hdrops);
454 			error = ENOMEM;
455 			goto bad;
456 		}
457 
458 		tdbi = (struct tdb_ident *)(mtag + 1);
459 		bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
460 		tdbi->proto = sproto;
461 		tdbi->spi = sav->spi;
462 		/* Cache those two for enc(4) in xform_ipip. */
463 		tdbi->alg_auth = sav->alg_auth;
464 		tdbi->alg_enc = sav->alg_enc;
465 
466 		m_tag_prepend(m, mtag);
467 	} else if (mt != NULL) {
468 		mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
469 		/* XXX do we need to mark m_flags??? */
470 	}
471 
472 	key_sa_recordxfer(sav, m);		/* record data transfer */
473 
474 #ifdef DEV_ENC
475 	encif->if_ipackets++;
476 	encif->if_ibytes += m->m_pkthdr.len;
477 
478 	/*
479 	 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
480 	 * packet later after it has been decapsulated.
481 	 */
482 	ipsec_bpf(m, sav, AF_INET, ENC_IN|ENC_BEFORE);
483 
484 	if (prot != IPPROTO_IPIP)
485 		if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
486 			return (error);
487 #endif
488 
489 	/*
490 	 * Re-dispatch via software interrupt.
491 	 */
492 	if ((error = netisr_queue_src(NETISR_IP, (uintptr_t)sav->spi, m))) {
493 		IPSEC_ISTAT(sproto, V_espstat.esps_qfull, V_ahstat.ahs_qfull,
494 			    V_ipcompstat.ipcomps_qfull);
495 
496 		DPRINTF(("%s: queue full; proto %u packet dropped\n",
497 			__func__, sproto));
498 		return error;
499 	}
500 	return 0;
501 bad:
502 	m_freem(m);
503 	return error;
504 }
505 
506 void
507 ipsec4_common_ctlinput(int cmd, struct sockaddr *sa, void *v, int proto)
508 {
509 	/* XXX nothing just yet */
510 }
511 #endif /* INET */
512 
513 #ifdef INET6
514 /* IPv6 AH wrapper. */
515 int
516 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
517 {
518 	int l = 0;
519 	int protoff;
520 	struct ip6_ext ip6e;
521 
522 	if (*offp < sizeof(struct ip6_hdr)) {
523 		DPRINTF(("%s: bad offset %u\n", __func__, *offp));
524 		return IPPROTO_DONE;
525 	} else if (*offp == sizeof(struct ip6_hdr)) {
526 		protoff = offsetof(struct ip6_hdr, ip6_nxt);
527 	} else {
528 		/* Chase down the header chain... */
529 		protoff = sizeof(struct ip6_hdr);
530 
531 		do {
532 			protoff += l;
533 			m_copydata(*mp, protoff, sizeof(ip6e),
534 			    (caddr_t) &ip6e);
535 
536 			if (ip6e.ip6e_nxt == IPPROTO_AH)
537 				l = (ip6e.ip6e_len + 2) << 2;
538 			else
539 				l = (ip6e.ip6e_len + 1) << 3;
540 			IPSEC_ASSERT(l > 0, ("l went zero or negative"));
541 		} while (protoff + l < *offp);
542 
543 		/* Malformed packet check */
544 		if (protoff + l != *offp) {
545 			DPRINTF(("%s: bad packet header chain, protoff %u, "
546 				"l %u, off %u\n", __func__, protoff, l, *offp));
547 			IPSEC_ISTAT(proto, V_espstat.esps_hdrops,
548 				    V_ahstat.ahs_hdrops,
549 				    V_ipcompstat.ipcomps_hdrops);
550 			m_freem(*mp);
551 			*mp = NULL;
552 			return IPPROTO_DONE;
553 		}
554 		protoff += offsetof(struct ip6_ext, ip6e_nxt);
555 	}
556 	(void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
557 	return IPPROTO_DONE;
558 }
559 
560 /*
561  * IPsec input callback, called by the transform callback. Takes care of
562  * filtering and other sanity checks on the processed packet.
563  */
564 int
565 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
566     struct m_tag *mt)
567 {
568 	int prot, af, sproto;
569 	struct ip6_hdr *ip6;
570 	struct m_tag *mtag;
571 	struct tdb_ident *tdbi;
572 	struct secasindex *saidx;
573 	int nxt;
574 	u_int8_t nxt8;
575 	int error, nest;
576 #ifdef notyet
577 	char ip6buf[INET6_ADDRSTRLEN];
578 #endif
579 
580 	IPSEC_ASSERT(m != NULL, ("null mbuf"));
581 	IPSEC_ASSERT(sav != NULL, ("null SA"));
582 	IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
583 	saidx = &sav->sah->saidx;
584 	af = saidx->dst.sa.sa_family;
585 	IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
586 	sproto = saidx->proto;
587 	IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
588 		sproto == IPPROTO_IPCOMP,
589 		("unexpected security protocol %u", sproto));
590 
591 	/* Sanity check */
592 	if (m == NULL) {
593 		DPRINTF(("%s: null mbuf", __func__));
594 		IPSEC_ISTAT(sproto, V_espstat.esps_badkcr, V_ahstat.ahs_badkcr,
595 		    V_ipcompstat.ipcomps_badkcr);
596 		error = EINVAL;
597 		goto bad;
598 	}
599 
600 	/* Fix IPv6 header */
601 	if (m->m_len < sizeof(struct ip6_hdr) &&
602 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
603 
604 		DPRINTF(("%s: processing failed for SA %s/%08lx\n",
605 		    __func__, ipsec_address(&sav->sah->saidx.dst),
606 		    (u_long) ntohl(sav->spi)));
607 
608 		IPSEC_ISTAT(sproto, V_espstat.esps_hdrops, V_ahstat.ahs_hdrops,
609 		    V_ipcompstat.ipcomps_hdrops);
610 		error = EACCES;
611 		goto bad;
612 	}
613 
614 	ip6 = mtod(m, struct ip6_hdr *);
615 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
616 
617 	/* Save protocol */
618 	m_copydata(m, protoff, 1, (unsigned char *) &prot);
619 
620 #ifdef notyet
621 #ifdef INET
622 	/* IP-in-IP encapsulation */
623 	if (prot == IPPROTO_IPIP) {
624 		struct ip ipn;
625 
626 		if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
627 			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
628 			    V_ahstat.ahs_hdrops,
629 			    V_ipcompstat.ipcomps_hdrops);
630 			error = EINVAL;
631 			goto bad;
632 		}
633 		/* ipn will now contain the inner IPv4 header */
634 		m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
635 
636 		/*
637 		 * Check that the inner source address is the same as
638 		 * the proxy address, if available.
639 		 */
640 		if ((saidx->proxy.sa.sa_family == AF_INET &&
641 		    saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
642 		    ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
643 		    (saidx->proxy.sa.sa_family != AF_INET &&
644 			saidx->proxy.sa.sa_family != 0)) {
645 
646 			DPRINTF(("%s: inner source address %s doesn't "
647 			    "correspond to expected proxy source %s, "
648 			    "SA %s/%08lx\n", __func__,
649 			    inet_ntoa4(ipn.ip_src),
650 			    ipsec_address(&saidx->proxy),
651 			    ipsec_address(&saidx->dst),
652 			    (u_long) ntohl(sav->spi)));
653 
654 			IPSEC_ISTATsproto, (V_espstat.esps_pdrops,
655 			    V_ahstat.ahs_pdrops, V_ipcompstat.ipcomps_pdrops);
656 			error = EACCES;
657 			goto bad;
658 		}
659 	}
660 #endif /* INET */
661 
662 	/* IPv6-in-IP encapsulation */
663 	if (prot == IPPROTO_IPV6) {
664 		struct ip6_hdr ip6n;
665 
666 		if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
667 			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
668 			    V_ahstat.ahs_hdrops,
669 			    V_ipcompstat.ipcomps_hdrops);
670 			error = EINVAL;
671 			goto bad;
672 		}
673 		/* ip6n will now contain the inner IPv6 header. */
674 		m_copydata(m, skip, sizeof(struct ip6_hdr),
675 		    (caddr_t) &ip6n);
676 
677 		/*
678 		 * Check that the inner source address is the same as
679 		 * the proxy address, if available.
680 		 */
681 		if ((saidx->proxy.sa.sa_family == AF_INET6 &&
682 		    !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
683 		    !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
684 			&saidx->proxy.sin6.sin6_addr)) ||
685 		    (saidx->proxy.sa.sa_family != AF_INET6 &&
686 			saidx->proxy.sa.sa_family != 0)) {
687 
688 			DPRINTF(("%s: inner source address %s doesn't "
689 			    "correspond to expected proxy source %s, "
690 			    "SA %s/%08lx\n", __func__,
691 			    ip6_sprintf(ip6buf, &ip6n.ip6_src),
692 			    ipsec_address(&saidx->proxy),
693 			    ipsec_address(&saidx->dst),
694 			    (u_long) ntohl(sav->spi)));
695 
696 			IPSEC_ISTAT(sproto, V_espstat.esps_pdrops,
697 			    V_ahstat.ahs_pdrops, V_ipcompstat.ipcomps_pdrops);
698 			error = EACCES;
699 			goto bad;
700 		}
701 	}
702 #endif /*XXX*/
703 
704 	/*
705 	 * Record what we've done to the packet (under what SA it was
706 	 * processed). If we've been passed an mtag, it means the packet
707 	 * was already processed by an ethernet/crypto combo card and
708 	 * thus has a tag attached with all the right information, but
709 	 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
710 	 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
711 	 */
712 	if (mt == NULL && sproto != IPPROTO_IPCOMP) {
713 		mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
714 		    sizeof(struct tdb_ident), M_NOWAIT);
715 		if (mtag == NULL) {
716 			DPRINTF(("%s: failed to get tag\n", __func__));
717 			IPSEC_ISTAT(sproto, V_espstat.esps_hdrops,
718 			    V_ahstat.ahs_hdrops, V_ipcompstat.ipcomps_hdrops);
719 			error = ENOMEM;
720 			goto bad;
721 		}
722 
723 		tdbi = (struct tdb_ident *)(mtag + 1);
724 		bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
725 		tdbi->proto = sproto;
726 		tdbi->spi = sav->spi;
727 		/* Cache those two for enc(4) in xform_ipip. */
728 		tdbi->alg_auth = sav->alg_auth;
729 		tdbi->alg_enc = sav->alg_enc;
730 
731 		m_tag_prepend(m, mtag);
732 	} else {
733 		if (mt != NULL)
734 			mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
735 		/* XXX do we need to mark m_flags??? */
736 	}
737 
738 	key_sa_recordxfer(sav, m);
739 
740 #ifdef DEV_ENC
741 	encif->if_ipackets++;
742 	encif->if_ibytes += m->m_pkthdr.len;
743 
744 	/*
745 	 * Pass the mbuf to enc0 for bpf and pfil. We will filter the IPIP
746 	 * packet later after it has been decapsulated.
747 	 */
748 	ipsec_bpf(m, sav, AF_INET6, ENC_IN|ENC_BEFORE);
749 
750 	/* XXX-BZ does not make sense. */
751 	if (prot != IPPROTO_IPIP)
752 		if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
753 			return (error);
754 #endif
755 
756 	/* Retrieve new protocol */
757 	m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8);
758 
759 	/*
760 	 * See the end of ip6_input for this logic.
761 	 * IPPROTO_IPV[46] case will be processed just like other ones
762 	 */
763 	nest = 0;
764 	nxt = nxt8;
765 	while (nxt != IPPROTO_DONE) {
766 		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
767 			V_ip6stat.ip6s_toomanyhdr++;
768 			error = EINVAL;
769 			goto bad;
770 		}
771 
772 		/*
773 		 * Protection against faulty packet - there should be
774 		 * more sanity checks in header chain processing.
775 		 */
776 		if (m->m_pkthdr.len < skip) {
777 			V_ip6stat.ip6s_tooshort++;
778 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
779 			error = EINVAL;
780 			goto bad;
781 		}
782 		/*
783 		 * Enforce IPsec policy checking if we are seeing last header.
784 		 * note that we do not visit this with protocols with pcb layer
785 		 * code - like udp/tcp/raw ip.
786 		 */
787 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
788 		    ipsec6_in_reject(m, NULL)) {
789 			error = EINVAL;
790 			goto bad;
791 		}
792 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
793 	}
794 	return 0;
795 bad:
796 	if (m)
797 		m_freem(m);
798 	return error;
799 }
800 
801 void
802 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
803 {
804 	struct ip6ctlparam *ip6cp = NULL;
805 	struct mbuf *m = NULL;
806 	struct ip6_hdr *ip6;
807 	int off;
808 
809 	if (sa->sa_family != AF_INET6 ||
810 	    sa->sa_len != sizeof(struct sockaddr_in6))
811 		return;
812 	if ((unsigned)cmd >= PRC_NCMDS)
813 		return;
814 
815 	/* if the parameter is from icmp6, decode it. */
816 	if (d != NULL) {
817 		ip6cp = (struct ip6ctlparam *)d;
818 		m = ip6cp->ip6c_m;
819 		ip6 = ip6cp->ip6c_ip6;
820 		off = ip6cp->ip6c_off;
821 	} else {
822 		m = NULL;
823 		ip6 = NULL;
824 		off = 0;	/* calm gcc */
825 	}
826 
827 	if (ip6 != NULL) {
828 
829 		struct ip6ctlparam ip6cp1;
830 
831 		/*
832 		 * Notify the error to all possible sockets via pfctlinput2.
833 		 * Since the upper layer information (such as protocol type,
834 		 * source and destination ports) is embedded in the encrypted
835 		 * data and might have been cut, we can't directly call
836 		 * an upper layer ctlinput function. However, the pcbnotify
837 		 * function will consider source and destination addresses
838 		 * as well as the flow info value, and may be able to find
839 		 * some PCB that should be notified.
840 		 * Although pfctlinput2 will call esp6_ctlinput(), there is
841 		 * no possibility of an infinite loop of function calls,
842 		 * because we don't pass the inner IPv6 header.
843 		 */
844 		bzero(&ip6cp1, sizeof(ip6cp1));
845 		ip6cp1.ip6c_src = ip6cp->ip6c_src;
846 		pfctlinput2(cmd, sa, (void *)&ip6cp1);
847 
848 		/*
849 		 * Then go to special cases that need ESP header information.
850 		 * XXX: We assume that when ip6 is non NULL,
851 		 * M and OFF are valid.
852 		 */
853 
854 		if (cmd == PRC_MSGSIZE) {
855 			struct secasvar *sav;
856 			u_int32_t spi;
857 			int valid;
858 
859 			/* check header length before using m_copydata */
860 			if (m->m_pkthdr.len < off + sizeof (struct esp))
861 				return;
862 			m_copydata(m, off + offsetof(struct esp, esp_spi),
863 				sizeof(u_int32_t), (caddr_t) &spi);
864 			/*
865 			 * Check to see if we have a valid SA corresponding to
866 			 * the address in the ICMP message payload.
867 			 */
868 			sav = KEY_ALLOCSA((union sockaddr_union *)sa,
869 					IPPROTO_ESP, spi);
870 			valid = (sav != NULL);
871 			if (sav)
872 				KEY_FREESAV(&sav);
873 
874 			/* XXX Further validation? */
875 
876 			/*
877 			 * Depending on whether the SA is "valid" and
878 			 * routing table size (mtudisc_{hi,lo}wat), we will:
879 			 * - recalcurate the new MTU and create the
880 			 *   corresponding routing entry, or
881 			 * - ignore the MTU change notification.
882 			 */
883 			icmp6_mtudisc_update(ip6cp, valid);
884 		}
885 	} else {
886 		/* we normally notify any pcb here */
887 	}
888 }
889 #endif /* INET6 */
890