2 /* $OpenBSD: ip_ipip.c,v 1.25 2002/06/10 18:04:55 itojun Exp $ */
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).
8 * The original version of this code was written by John Ioannidis
9 * for BSD/OS in Athens, Greece, in November 1995.
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17 * Additional features in 1999 by Angelos D. Keromytis.
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.
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 * IP-inside-IP processing
43 #include "opt_inet6.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <sys/kernel.h>
51 #include <sys/protosw.h>
52 #include <sys/sysctl.h>
56 #include <net/route.h>
57 #include <net/netisr.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_ecn.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/ip_encap.h>
68 #include <netinet/ip_mroute.h>
71 #include <netipsec/ipsec.h>
72 #include <netipsec/xform.h>
74 #include <netipsec/ipip_var.h>
77 #include <netinet/ip6.h>
78 #include <netipsec/ipsec6.h>
79 #include <netinet6/ip6_ecn.h>
80 #include <netinet6/in6_var.h>
81 #include <netinet6/ip6protosw.h>
84 #include <netipsec/key.h>
85 #include <netipsec/key_debug.h>
87 #include <machine/stdarg.h>
90 * We can control the acceptance of IP4 packets by altering the sysctl
91 * net.inet.ipip.allow value. Zero means drop them, all else is acceptance.
93 VNET_DEFINE(int, ipip_allow) = 0;
94 VNET_DEFINE(struct ipipstat, ipipstat);
96 SYSCTL_DECL(_net_inet_ipip);
97 SYSCTL_VNET_INT(_net_inet_ipip, OID_AUTO,
98 ipip_allow, CTLFLAG_RW, &VNET_NAME(ipip_allow), 0, "");
99 SYSCTL_VNET_STRUCT(_net_inet_ipip, IPSECCTL_STATS,
100 stats, CTLFLAG_RD, &VNET_NAME(ipipstat), ipipstat, "");
103 #define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
105 static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
109 * Really only a wrapper for ipip_input(), for use with IPv6.
112 ip4_input6(struct mbuf **m, int *offp, int proto)
115 /* If we do not accept IP-in-IP explicitly, drop. */
116 if (!V_ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) {
117 DPRINTF(("%s: dropped due to policy\n", __func__));
118 V_ipipstat.ipips_pdrops++;
123 _ipip_input(*m, *offp, NULL);
130 * Really only a wrapper for ipip_input(), for use with IPv4.
133 ip4_input(struct mbuf *m, int off)
136 /* If we do not accept IP-in-IP explicitly, drop. */
137 if (!V_ipip_allow && (m->m_flags & M_IPSEC) == 0) {
138 DPRINTF(("%s: dropped due to policy\n", __func__));
139 V_ipipstat.ipips_pdrops++;
144 _ipip_input(m, off, NULL);
149 * ipip_input gets called when we receive an IP{46} encapsulated packet,
150 * either because we got it at a real interface, or because AH or ESP
151 * were being used in tunnel mode (in which case the rcvif element will
152 * contain the address of the encX interface associated with the tunnel.
156 _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
159 register struct sockaddr_in *sin;
161 register struct ifnet *ifp;
162 register struct ifaddr *ifa;
165 register struct sockaddr_in6 *sin6;
166 struct ip6_hdr *ip6 = NULL;
175 V_ipipstat.ipips_ipackets++;
177 m_copydata(m, 0, 1, &v);
182 hlen = sizeof(struct ip);
187 hlen = sizeof(struct ip6_hdr);
191 V_ipipstat.ipips_family++;
193 return /* EAFNOSUPPORT */;
196 /* Bring the IP header in the first mbuf, if not there already */
197 if (m->m_len < hlen) {
198 if ((m = m_pullup(m, hlen)) == NULL) {
199 DPRINTF(("%s: m_pullup (1) failed\n", __func__));
200 V_ipipstat.ipips_hdrops++;
205 ipo = mtod(m, struct ip *);
208 if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
209 if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
210 ipip_mroute_input (m, iphlen);
214 #endif /* MROUTING */
216 /* Keep outer ecn field. */
225 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
229 panic("ipip_input: unknown ip version %u (outer)", v>>4);
232 /* Remove outer IP header */
236 if (m->m_pkthdr.len < sizeof(struct ip)) {
237 V_ipipstat.ipips_hdrops++;
242 m_copydata(m, 0, 1, &v);
247 hlen = sizeof(struct ip);
253 hlen = sizeof(struct ip6_hdr);
257 V_ipipstat.ipips_family++;
259 return; /* EAFNOSUPPORT */
263 * Bring the inner IP header in the first mbuf, if not there already.
265 if (m->m_len < hlen) {
266 if ((m = m_pullup(m, hlen)) == NULL) {
267 DPRINTF(("%s: m_pullup (2) failed\n", __func__));
268 V_ipipstat.ipips_hdrops++;
274 * RFC 1853 specifies that the inner TTL should not be touched on
275 * decapsulation. There's no reason this comment should be here, but
276 * this is as good as any a position.
279 /* Some sanity checks in the inner IP header */
283 ipo = mtod(m, struct ip *);
285 ip_ecn_egress(V_ip4_ipsec_ecn, &otos, &ipo->ip_tos);
290 ip6 = (struct ip6_hdr *) ipo;
292 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
293 ip_ecn_egress(V_ip6_ipsec_ecn, &otos, &itos);
294 ip6->ip6_flow &= ~htonl(0xff << 20);
295 ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
299 panic("ipip_input: unknown ip version %u (inner)", v>>4);
302 /* Check for local address spoofing. */
303 if ((m->m_pkthdr.rcvif == NULL ||
304 !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) &&
306 IFNET_RLOCK_NOSLEEP();
307 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
308 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
311 if (ifa->ifa_addr->sa_family !=
315 sin = (struct sockaddr_in *) ifa->ifa_addr;
317 if (sin->sin_addr.s_addr ==
318 ipo->ip_src.s_addr) {
319 V_ipipstat.ipips_spoof++;
321 IFNET_RUNLOCK_NOSLEEP();
329 if (ifa->ifa_addr->sa_family !=
333 sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
335 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
336 V_ipipstat.ipips_spoof++;
338 IFNET_RUNLOCK_NOSLEEP();
346 IFNET_RUNLOCK_NOSLEEP();
350 V_ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen;
356 ipsec_bpf(m, NULL, AF_INET, ENC_IN|ENC_AFTER);
361 ipsec_bpf(m, NULL, AF_INET6, ENC_IN|ENC_AFTER);
365 panic("%s: bogus ip version %u", __func__, v>>4);
367 /* pass the mbuf to enc0 for packet filtering */
368 if (ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER) != 0)
373 * Interface pointer stays the same; if no IPsec processing has
374 * been done (or will be done), this will point to a normal
375 * interface. Otherwise, it'll point to an enc interface, which
376 * will allow a packet filter to distinguish between secure and
392 panic("%s: bogus ip version %u", __func__, v>>4);
395 if (netisr_queue(isr, m)) { /* (0) on success. */
396 V_ipipstat.ipips_qfull++;
397 DPRINTF(("%s: packet dropped because of full queue\n",
405 struct ipsecrequest *isr,
411 struct secasvar *sav;
413 struct secasindex *saidx;
415 #if defined(INET) || defined(INET6)
422 struct ip6_hdr *ip6, *ip6o;
426 IPSEC_ASSERT(sav != NULL, ("null SA"));
427 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
429 /* XXX Deal with empty TDB source/destination addresses. */
431 m_copydata(m, 0, 1, &tp);
432 tp = (tp >> 4) & 0xff; /* Get the IP version number. */
434 saidx = &sav->sah->saidx;
435 switch (saidx->dst.sa.sa_family) {
438 if (saidx->src.sa.sa_family != AF_INET ||
439 saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
440 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
441 DPRINTF(("%s: unspecified tunnel endpoint "
442 "address in SA %s/%08lx\n", __func__,
443 ipsec_address(&saidx->dst),
444 (u_long) ntohl(sav->spi)));
445 V_ipipstat.ipips_unspec++;
450 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
452 DPRINTF(("%s: M_PREPEND failed\n", __func__));
453 V_ipipstat.ipips_hdrops++;
458 ipo = mtod(m, struct ip *);
460 ipo->ip_v = IPVERSION;
462 ipo->ip_len = htons(m->m_pkthdr.len);
463 ipo->ip_ttl = V_ip_defttl;
465 ipo->ip_src = saidx->src.sin.sin_addr;
466 ipo->ip_dst = saidx->dst.sin.sin_addr;
468 ipo->ip_id = ip_newid();
470 /* If the inner protocol is IP... */
473 /* Save ECN notification */
474 m_copydata(m, sizeof(struct ip) +
475 offsetof(struct ip, ip_tos),
476 sizeof(u_int8_t), (caddr_t) &itos);
478 ipo->ip_p = IPPROTO_IPIP;
481 * We should be keeping tunnel soft-state and
482 * send back ICMPs if needed.
484 m_copydata(m, sizeof(struct ip) +
485 offsetof(struct ip, ip_off),
486 sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
487 ipo->ip_off = ntohs(ipo->ip_off);
488 ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
489 ipo->ip_off = htons(ipo->ip_off);
492 case (IPV6_VERSION >> 4):
496 /* Save ECN notification. */
497 m_copydata(m, sizeof(struct ip) +
498 offsetof(struct ip6_hdr, ip6_flow),
499 sizeof(u_int32_t), (caddr_t) &itos32);
500 itos = ntohl(itos32) >> 20;
501 ipo->ip_p = IPPROTO_IPV6;
511 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
518 if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
519 saidx->src.sa.sa_family != AF_INET6 ||
520 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
521 DPRINTF(("%s: unspecified tunnel endpoint "
522 "address in SA %s/%08lx\n", __func__,
523 ipsec_address(&saidx->dst),
524 (u_long) ntohl(sav->spi)));
525 V_ipipstat.ipips_unspec++;
530 /* scoped address handling */
531 ip6 = mtod(m, struct ip6_hdr *);
532 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
533 ip6->ip6_src.s6_addr16[1] = 0;
534 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
535 ip6->ip6_dst.s6_addr16[1] = 0;
537 M_PREPEND(m, sizeof(struct ip6_hdr), M_NOWAIT);
539 DPRINTF(("%s: M_PREPEND failed\n", __func__));
540 V_ipipstat.ipips_hdrops++;
545 /* Initialize IPv6 header */
546 ip6o = mtod(m, struct ip6_hdr *);
548 ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
549 ip6o->ip6_vfc |= IPV6_VERSION;
550 ip6o->ip6_plen = htons(m->m_pkthdr.len);
551 ip6o->ip6_hlim = V_ip_defttl;
552 ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
553 ip6o->ip6_src = saidx->src.sin6.sin6_addr;
558 /* Save ECN notification */
559 m_copydata(m, sizeof(struct ip6_hdr) +
560 offsetof(struct ip, ip_tos), sizeof(u_int8_t),
563 /* This is really IPVERSION. */
564 ip6o->ip6_nxt = IPPROTO_IPIP;
567 case (IPV6_VERSION >> 4):
571 /* Save ECN notification. */
572 m_copydata(m, sizeof(struct ip6_hdr) +
573 offsetof(struct ip6_hdr, ip6_flow),
574 sizeof(u_int32_t), (caddr_t) &itos32);
575 itos = ntohl(itos32) >> 20;
577 ip6o->ip6_nxt = IPPROTO_IPV6;
585 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
586 ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
592 DPRINTF(("%s: unsupported protocol family %u\n", __func__,
593 saidx->dst.sa.sa_family));
594 V_ipipstat.ipips_family++;
595 error = EAFNOSUPPORT; /* XXX diffs from openbsd */
599 V_ipipstat.ipips_opackets++;
603 if (saidx->dst.sa.sa_family == AF_INET) {
605 if (sav->tdb_xform->xf_type == XF_IP4)
606 tdb->tdb_cur_bytes +=
607 m->m_pkthdr.len - sizeof(struct ip);
609 V_ipipstat.ipips_obytes += m->m_pkthdr.len - sizeof(struct ip);
614 if (saidx->dst.sa.sa_family == AF_INET6) {
616 if (sav->tdb_xform->xf_type == XF_IP4)
617 tdb->tdb_cur_bytes +=
618 m->m_pkthdr.len - sizeof(struct ip6_hdr);
620 V_ipipstat.ipips_obytes +=
621 m->m_pkthdr.len - sizeof(struct ip6_hdr);
634 #if defined(INET) || defined(INET6)
636 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
638 sav->tdb_xform = xsp;
643 ipe4_zeroize(struct secasvar *sav)
645 sav->tdb_xform = NULL;
650 ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
652 /* This is a rather serious mistake, so no conditional printing. */
653 printf("%s: should never be called\n", __func__);
659 static struct xformsw ipe4_xformsw = {
660 XF_IP4, 0, "IPv4 Simple Encapsulation",
661 ipe4_init, ipe4_zeroize, ipe4_input, ipip_output,
664 extern struct domain inetdomain;
665 #endif /* INET || INET6 */
667 static struct protosw ipe4_protosw = {
669 .pr_domain = &inetdomain,
670 .pr_protocol = IPPROTO_IPV4,
671 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
672 .pr_input = ip4_input,
673 .pr_ctloutput = rip_ctloutput,
674 .pr_usrreqs = &rip_usrreqs
677 #if defined(INET6) && defined(INET)
678 static struct ip6protosw ipe6_protosw = {
680 .pr_domain = &inetdomain,
681 .pr_protocol = IPPROTO_IPV6,
682 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
683 .pr_input = ip4_input6,
684 .pr_ctloutput = rip_ctloutput,
685 .pr_usrreqs = &rip_usrreqs
687 #endif /* INET6 && INET */
691 * Check the encapsulated packet to see if we want it
694 ipe4_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
697 * Only take packets coming from IPSEC tunnels; the rest
698 * must be handled by the gif tunnel code. Note that we
699 * also return a minimum priority when we want the packet
700 * so any explicit gif tunnels take precedence.
702 return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
710 xform_register(&ipe4_xformsw);
711 /* attach to encapsulation framework */
712 /* XXX save return cookie for detach on module remove */
714 (void) encap_attach_func(AF_INET, -1,
715 ipe4_encapcheck, &ipe4_protosw, NULL);
717 #if defined(INET6) && defined(INET)
718 (void) encap_attach_func(AF_INET6, -1,
719 ipe4_encapcheck, (struct protosw *)&ipe6_protosw, NULL);
722 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);