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