xref: /openbsd-src/sys/netinet/ip_ipip.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: ip_ipip.c,v 1.89 2018/11/14 23:55:04 dlg Exp $ */
2 /*
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).
6  *
7  * The original version of this code was written by John Ioannidis
8  * for BSD/OS in Athens, Greece, in November 1995.
9  *
10  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
11  * by Angelos D. Keromytis.
12  *
13  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
14  * and Niels Provos.
15  *
16  * Additional features in 1999 by Angelos D. Keromytis.
17  *
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  *
22  * Permission to use, copy, and modify this software with or without fee
23  * is hereby granted, provided that this entire notice is included in
24  * all copies of any software which is or includes a copy or
25  * modification of this software.
26  * You may use this code under the GNU public license if you so wish. Please
27  * contribute changes back to the authors under this freer than GPL license
28  * so that we may further the use of strong encryption without limitations to
29  * all.
30  *
31  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
32  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
33  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
34  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
35  * PURPOSE.
36  */
37 
38 /*
39  * IP-inside-IP processing
40  */
41 
42 #include "bpfilter.h"
43 #include "gif.h"
44 #include "pf.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 
52 #include <net/if.h>
53 #include <net/if_types.h>
54 #include <net/if_var.h>
55 #include <net/route.h>
56 #include <net/netisr.h>
57 #include <net/bpf.h>
58 
59 #include <netinet/in.h>
60 #include <netinet/ip.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/ip_ecn.h>
64 #include <netinet/ip_ipip.h>
65 
66 #ifdef MROUTING
67 #include <netinet/ip_mroute.h>
68 #endif
69 
70 #if NPF > 0
71 #include <net/pfvar.h>
72 #endif
73 
74 #ifdef ENCDEBUG
75 #define DPRINTF(x)	if (encdebug) printf x
76 #else
77 #define DPRINTF(x)
78 #endif
79 
80 /*
81  * We can control the acceptance of IP4 packets by altering the sysctl
82  * net.inet.ipip.allow value.  Zero means drop them, all else is acceptance.
83  */
84 int ipip_allow = 0;
85 
86 struct cpumem *ipipcounters;
87 
88 void
89 ipip_init(void)
90 {
91 	ipipcounters = counters_alloc(ipips_ncounters);
92 }
93 
94 /*
95  * Really only a wrapper for ipip_input_if(), for use with pr_input.
96  */
97 int
98 ipip_input(struct mbuf **mp, int *offp, int nxt, int af)
99 {
100 	struct ifnet *ifp;
101 
102 	/* If we do not accept IP-in-IP explicitly, drop.  */
103 	if (!ipip_allow && ((*mp)->m_flags & (M_AUTH|M_CONF)) == 0) {
104 		DPRINTF(("%s: dropped due to policy\n", __func__));
105 		ipipstat_inc(ipips_pdrops);
106 		m_freemp(mp);
107 		return IPPROTO_DONE;
108 	}
109 
110 	ifp = if_get((*mp)->m_pkthdr.ph_ifidx);
111 	if (ifp == NULL) {
112 		m_freemp(mp);
113 		return IPPROTO_DONE;
114 	}
115 	nxt = ipip_input_if(mp, offp, nxt, af, ifp);
116 	if_put(ifp);
117 
118 	return nxt;
119 }
120 
121 /*
122  * ipip_input gets called when we receive an IP{46} encapsulated packet,
123  * either because we got it at a real interface, or because AH or ESP
124  * were being used in tunnel mode (in which case the ph_ifidx element
125  * will contain the index of the encX interface associated with the
126  * tunnel.
127  */
128 
129 int
130 ipip_input_if(struct mbuf **mp, int *offp, int proto, int oaf,
131     struct ifnet *ifp)
132 {
133 	struct mbuf *m = *mp;
134 	struct sockaddr_in *sin;
135 	struct ip *ip;
136 #ifdef INET6
137 	struct sockaddr_in6 *sin6;
138 	struct ip6_hdr *ip6;
139 #endif
140 	int mode, hlen;
141 	u_int8_t itos, otos;
142 	sa_family_t iaf;
143 
144 	ipipstat_inc(ipips_ipackets);
145 
146 	switch (oaf) {
147 	case AF_INET:
148 		hlen = sizeof(struct ip);
149 		break;
150 #ifdef INET6
151 	case AF_INET6:
152 		hlen = sizeof(struct ip6_hdr);
153 		break;
154 #endif
155 	default:
156 		unhandled_af(oaf);
157 	}
158 
159 	/* Bring the IP header in the first mbuf, if not there already */
160 	if (m->m_len < hlen) {
161 		if ((m = *mp = m_pullup(m, hlen)) == NULL) {
162 			DPRINTF(("%s: m_pullup() failed\n", __func__));
163 			ipipstat_inc(ipips_hdrops);
164 			goto bad;
165 		}
166 	}
167 
168 	/* Keep outer ecn field. */
169 	switch (oaf) {
170 	case AF_INET:
171 		ip = mtod(m, struct ip *);
172 		otos = ip->ip_tos;
173 		break;
174 #ifdef INET6
175 	case AF_INET6:
176 		ip6 = mtod(m, struct ip6_hdr *);
177 		otos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
178 		break;
179 #endif
180 	}
181 
182 	/* Remove outer IP header */
183 	KASSERT(*offp > 0);
184 	m_adj(m, *offp);
185 	*offp = 0;
186 	ip = NULL;
187 #ifdef INET6
188 	ip6 = NULL;
189 #endif
190 
191 	switch (proto) {
192 	case IPPROTO_IPV4:
193 		hlen = sizeof(struct ip);
194 		break;
195 
196 #ifdef INET6
197 	case IPPROTO_IPV6:
198 		hlen = sizeof(struct ip6_hdr);
199 		break;
200 #endif
201 	default:
202 		ipipstat_inc(ipips_family);
203 		goto bad;
204 	}
205 
206 	/* Sanity check */
207 	if (m->m_pkthdr.len < hlen) {
208 		ipipstat_inc(ipips_hdrops);
209 		goto bad;
210 	}
211 
212 	/*
213 	 * Bring the inner header into the first mbuf, if not there already.
214 	 */
215 	if (m->m_len < hlen) {
216 		if ((m = *mp = m_pullup(m, hlen)) == NULL) {
217 			DPRINTF(("%s: m_pullup() failed\n", __func__));
218 			ipipstat_inc(ipips_hdrops);
219 			goto bad;
220 		}
221 	}
222 
223 	/*
224 	 * RFC 1853 specifies that the inner TTL should not be touched on
225 	 * decapsulation. There's no reason this comment should be here, but
226 	 * this is as good as any a position.
227 	 */
228 
229 	/* Some sanity checks in the inner IP header */
230 	switch (proto) {
231     	case IPPROTO_IPV4:
232 		iaf = AF_INET;
233 		ip = mtod(m, struct ip *);
234 		hlen = ip->ip_hl << 2;
235 		if (m->m_pkthdr.len < hlen) {
236 			ipipstat_inc(ipips_hdrops);
237 			goto bad;
238 		}
239 		itos = ip->ip_tos;
240 		mode = m->m_flags & (M_AUTH|M_CONF) ?
241 		    ECN_ALLOWED_IPSEC : ECN_ALLOWED;
242 		if (!ip_ecn_egress(mode, &otos, &itos)) {
243 			DPRINTF(("%s: ip_ecn_egress() failed\n", __func__));
244 			ipipstat_inc(ipips_pdrops);
245 			goto bad;
246 		}
247 		/* re-calculate the checksum if ip_tos was changed */
248 		if (itos != ip->ip_tos)
249 			ip_tos_patch(ip, itos);
250 		break;
251 #ifdef INET6
252     	case IPPROTO_IPV6:
253 		iaf = AF_INET6;
254 		ip6 = mtod(m, struct ip6_hdr *);
255 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
256 		if (!ip_ecn_egress(ECN_ALLOWED, &otos, &itos)) {
257 			DPRINTF(("%s: ip_ecn_egress() failed\n", __func__));
258 			ipipstat_inc(ipips_pdrops);
259 			goto bad;
260 		}
261 		ip6->ip6_flow &= ~htonl(0xff << 20);
262 		ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
263 		break;
264 #endif
265 	}
266 
267 	/* Check for local address spoofing. */
268 	if (!(ifp->if_flags & IFF_LOOPBACK) && ipip_allow != 2) {
269 		struct sockaddr_storage ss;
270 		struct rtentry *rt;
271 
272 		memset(&ss, 0, sizeof(ss));
273 
274 		if (ip) {
275 			sin = (struct sockaddr_in *)&ss;
276 			sin->sin_family = AF_INET;
277 			sin->sin_len = sizeof(*sin);
278 			sin->sin_addr = ip->ip_src;
279 #ifdef INET6
280 		} else if (ip6) {
281 			sin6 = (struct sockaddr_in6 *)&ss;
282 			sin6->sin6_family = AF_INET6;
283 			sin6->sin6_len = sizeof(*sin6);
284 			sin6->sin6_addr = ip6->ip6_src;
285 #endif /* INET6 */
286 		}
287 		rt = rtalloc(sstosa(&ss), 0, m->m_pkthdr.ph_rtableid);
288 		if ((rt != NULL) && (rt->rt_flags & RTF_LOCAL)) {
289 			ipipstat_inc(ipips_spoof);
290 			rtfree(rt);
291 			goto bad;
292  		}
293 		rtfree(rt);
294 	}
295 
296 	/* Statistics */
297 	ipipstat_add(ipips_ibytes, m->m_pkthdr.len - hlen);
298 
299 #if NBPFILTER > 0 && NGIF > 0
300 	if (ifp->if_type == IFT_GIF && ifp->if_bpf != NULL)
301 		bpf_mtap_af(ifp->if_bpf, iaf, m, BPF_DIRECTION_IN);
302 #endif
303 #if NPF > 0
304 	pf_pkt_addr_changed(m);
305 #endif
306 
307 	/*
308 	 * Interface pointer stays the same; if no IPsec processing has
309 	 * been done (or will be done), this will point to a normal
310 	 * interface. Otherwise, it'll point to an enc interface, which
311 	 * will allow a packet filter to distinguish between secure and
312 	 * untrusted packets.
313 	 */
314 
315 	switch (proto) {
316 	case IPPROTO_IPV4:
317 		return ip_input_if(mp, offp, proto, oaf, ifp);
318 #ifdef INET6
319 	case IPPROTO_IPV6:
320 		return ip6_input_if(mp, offp, proto, oaf, ifp);
321 #endif
322 	}
323  bad:
324 	m_freemp(mp);
325 	return IPPROTO_DONE;
326 }
327 
328 int
329 ipip_output(struct mbuf *m, struct tdb *tdb, struct mbuf **mp, int dummy,
330     int dummy2)
331 {
332 	u_int8_t tp, otos, itos;
333 	u_int64_t obytes;
334 	struct ip *ipo;
335 #ifdef INET6
336 	struct ip6_hdr *ip6, *ip6o;
337 #endif /* INET6 */
338 #ifdef ENCDEBUG
339 	char buf[INET6_ADDRSTRLEN];
340 #endif
341 
342 	/* XXX Deal with empty TDB source/destination addresses. */
343 
344 	m_copydata(m, 0, 1, &tp);
345 	tp = (tp >> 4) & 0xff;  /* Get the IP version number. */
346 
347 	switch (tdb->tdb_dst.sa.sa_family) {
348 	case AF_INET:
349 		if (tdb->tdb_src.sa.sa_family != AF_INET ||
350 		    tdb->tdb_src.sin.sin_addr.s_addr == INADDR_ANY ||
351 		    tdb->tdb_dst.sin.sin_addr.s_addr == INADDR_ANY) {
352 
353 			DPRINTF(("%s: unspecified tunnel endpoind "
354 			    "address in SA %s/%08x\n", __func__,
355 			    ipsp_address(&tdb->tdb_dst, buf, sizeof(buf)),
356 			    ntohl(tdb->tdb_spi)));
357 
358 			ipipstat_inc(ipips_unspec);
359 			m_freem(m);
360 			*mp = NULL;
361 			return EINVAL;
362 		}
363 
364 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
365 		if (m == NULL) {
366 			DPRINTF(("%s: M_PREPEND failed\n", __func__));
367 			ipipstat_inc(ipips_hdrops);
368 			*mp = NULL;
369 			return ENOBUFS;
370 		}
371 
372 		ipo = mtod(m, struct ip *);
373 
374 		ipo->ip_v = IPVERSION;
375 		ipo->ip_hl = 5;
376 		ipo->ip_len = htons(m->m_pkthdr.len);
377 		ipo->ip_ttl = ip_defttl;
378 		ipo->ip_sum = 0;
379 		ipo->ip_src = tdb->tdb_src.sin.sin_addr;
380 		ipo->ip_dst = tdb->tdb_dst.sin.sin_addr;
381 
382 		/*
383 		 * We do the htons() to prevent snoopers from determining our
384 		 * endianness.
385 		 */
386 		ipo->ip_id = htons(ip_randomid());
387 
388 		/* If the inner protocol is IP... */
389 		if (tp == IPVERSION) {
390 			/* Save ECN notification */
391 			m_copydata(m, sizeof(struct ip) +
392 			    offsetof(struct ip, ip_tos),
393 			    sizeof(u_int8_t), (caddr_t) &itos);
394 
395 			ipo->ip_p = IPPROTO_IPIP;
396 
397 			/*
398 			 * We should be keeping tunnel soft-state and
399 			 * send back ICMPs if needed.
400 			 */
401 			m_copydata(m, sizeof(struct ip) +
402 			    offsetof(struct ip, ip_off),
403 			    sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
404 			ipo->ip_off = ntohs(ipo->ip_off);
405 			ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
406 			ipo->ip_off = htons(ipo->ip_off);
407 		}
408 #ifdef INET6
409 		else if (tp == (IPV6_VERSION >> 4)) {
410 			u_int32_t itos32;
411 
412 			/* Save ECN notification. */
413 			m_copydata(m, sizeof(struct ip) +
414 			    offsetof(struct ip6_hdr, ip6_flow),
415 			    sizeof(u_int32_t), (caddr_t) &itos32);
416 			itos = ntohl(itos32) >> 20;
417 			ipo->ip_p = IPPROTO_IPV6;
418 			ipo->ip_off = 0;
419 		}
420 #endif /* INET6 */
421 		else {
422 			m_freem(m);
423 			*mp = NULL;
424 			ipipstat_inc(ipips_family);
425 			return EAFNOSUPPORT;
426 		}
427 
428 		otos = 0;
429 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
430 		ipo->ip_tos = otos;
431 		break;
432 
433 #ifdef INET6
434 	case AF_INET6:
435 		if (IN6_IS_ADDR_UNSPECIFIED(&tdb->tdb_dst.sin6.sin6_addr) ||
436 		    tdb->tdb_src.sa.sa_family != AF_INET6 ||
437 		    IN6_IS_ADDR_UNSPECIFIED(&tdb->tdb_src.sin6.sin6_addr)) {
438 
439 			DPRINTF(("%s: unspecified tunnel endpoind "
440 			    "address in SA %s/%08x\n", __func__,
441 			    ipsp_address(&tdb->tdb_dst, buf, sizeof(buf)),
442 			    ntohl(tdb->tdb_spi)));
443 
444 			ipipstat_inc(ipips_unspec);
445 			m_freem(m);
446 			*mp = NULL;
447 			return ENOBUFS;
448 		}
449 
450 		/* If the inner protocol is IPv6, clear link local scope */
451 		if (tp == (IPV6_VERSION >> 4)) {
452 			/* scoped address handling */
453 			ip6 = mtod(m, struct ip6_hdr *);
454 			if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
455 				ip6->ip6_src.s6_addr16[1] = 0;
456 			if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
457 				ip6->ip6_dst.s6_addr16[1] = 0;
458 		}
459 
460 		M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
461 		if (m == NULL) {
462 			DPRINTF(("%s: M_PREPEND failed\n", __func__));
463 			ipipstat_inc(ipips_hdrops);
464 			*mp = NULL;
465 			return ENOBUFS;
466 		}
467 
468 		/* Initialize IPv6 header */
469 		ip6o = mtod(m, struct ip6_hdr *);
470 		ip6o->ip6_flow = 0;
471 		ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
472 		ip6o->ip6_vfc |= IPV6_VERSION;
473 		ip6o->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6o));
474 		ip6o->ip6_hlim = ip_defttl;
475 		in6_embedscope(&ip6o->ip6_src, &tdb->tdb_src.sin6, NULL);
476 		in6_embedscope(&ip6o->ip6_dst, &tdb->tdb_dst.sin6, NULL);
477 
478 		if (tp == IPVERSION) {
479 			/* Save ECN notification */
480 			m_copydata(m, sizeof(struct ip6_hdr) +
481 			    offsetof(struct ip, ip_tos), sizeof(u_int8_t),
482 			    (caddr_t) &itos);
483 
484 			/* This is really IPVERSION. */
485 			ip6o->ip6_nxt = IPPROTO_IPIP;
486 		}
487 		else
488 			if (tp == (IPV6_VERSION >> 4)) {
489 				u_int32_t itos32;
490 
491 				/* Save ECN notification. */
492 				m_copydata(m, sizeof(struct ip6_hdr) +
493 				    offsetof(struct ip6_hdr, ip6_flow),
494 				    sizeof(u_int32_t), (caddr_t) &itos32);
495 				itos = ntohl(itos32) >> 20;
496 
497 				ip6o->ip6_nxt = IPPROTO_IPV6;
498 			} else {
499 				m_freem(m);
500 				*mp = NULL;
501 				ipipstat_inc(ipips_family);
502 				return EAFNOSUPPORT;
503 			}
504 
505 		otos = 0;
506 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
507 		ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
508 		break;
509 #endif /* INET6 */
510 
511 	default:
512 		DPRINTF(("%s: unsupported protocol family %d\n", __func__,
513 		    tdb->tdb_dst.sa.sa_family));
514 		m_freem(m);
515 		*mp = NULL;
516 		ipipstat_inc(ipips_family);
517 		return EAFNOSUPPORT;
518 	}
519 
520 	ipipstat_inc(ipips_opackets);
521 	*mp = m;
522 
523 	if (tdb->tdb_dst.sa.sa_family == AF_INET) {
524 		obytes = m->m_pkthdr.len - sizeof(struct ip);
525 		if (tdb->tdb_xform->xf_type == XF_IP4)
526 			tdb->tdb_cur_bytes += obytes;
527 
528 		ipipstat_add(ipips_obytes, obytes);
529 	}
530 
531 #ifdef INET6
532 	if (tdb->tdb_dst.sa.sa_family == AF_INET6) {
533 		obytes = m->m_pkthdr.len - sizeof(struct ip6_hdr);
534 		if (tdb->tdb_xform->xf_type == XF_IP4)
535 			tdb->tdb_cur_bytes += obytes;
536 
537 		ipipstat_add(ipips_obytes, obytes);
538 	}
539 #endif /* INET6 */
540 
541 	return 0;
542 }
543 
544 #ifdef IPSEC
545 int
546 ipe4_attach(void)
547 {
548 	return 0;
549 }
550 
551 int
552 ipe4_init(struct tdb *tdbp, struct xformsw *xsp, struct ipsecinit *ii)
553 {
554 	tdbp->tdb_xform = xsp;
555 	return 0;
556 }
557 
558 int
559 ipe4_zeroize(struct tdb *tdbp)
560 {
561 	return 0;
562 }
563 
564 int
565 ipe4_input(struct mbuf *m, struct tdb *tdb, int hlen, int proto)
566 {
567 	/* This is a rather serious mistake, so no conditional printing. */
568 	printf("ipe4_input(): should never be called\n");
569 	m_freem(m);
570 	return EINVAL;
571 }
572 #endif	/* IPSEC */
573 
574 int
575 ipip_sysctl_ipipstat(void *oldp, size_t *oldlenp, void *newp)
576 {
577 	struct ipipstat ipipstat;
578 
579 	CTASSERT(sizeof(ipipstat) == (ipips_ncounters * sizeof(uint64_t)));
580 	memset(&ipipstat, 0, sizeof ipipstat);
581 	counters_read(ipipcounters, (uint64_t *)&ipipstat, ipips_ncounters);
582 	return (sysctl_rdstruct(oldp, oldlenp, newp,
583 	    &ipipstat, sizeof(ipipstat)));
584 }
585 
586 int
587 ipip_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
588     size_t newlen)
589 {
590 	int error;
591 
592 	/* All sysctl names at this level are terminal. */
593 	if (namelen != 1)
594 		return (ENOTDIR);
595 
596 	switch (name[0]) {
597 	case IPIPCTL_ALLOW:
598 		NET_LOCK();
599 		error = sysctl_int(oldp, oldlenp, newp, newlen, &ipip_allow);
600 		NET_UNLOCK();
601 		return (error);
602 	case IPIPCTL_STATS:
603 		return (ipip_sysctl_ipipstat(oldp, oldlenp, newp));
604 	default:
605 		return (ENOPROTOOPT);
606 	}
607 	/* NOTREACHED */
608 }
609