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>
53 #include <sys/vimage.h>
57 #include <net/route.h>
58 #include <net/netisr.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_ecn.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/ip_encap.h>
69 #include <netinet/ip_mroute.h>
71 #include <netinet/vinet.h>
73 #include <netipsec/ipsec.h>
74 #include <netipsec/xform.h>
76 #include <netipsec/ipip_var.h>
79 #include <netinet/ip6.h>
80 #include <netipsec/ipsec6.h>
81 #include <netinet6/ip6_ecn.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet6/ip6protosw.h>
86 #include <netipsec/key.h>
87 #include <netipsec/key_debug.h>
89 #include <machine/stdarg.h>
92 * We can control the acceptance of IP4 packets by altering the sysctl
93 * net.inet.ipip.allow value. Zero means drop them, all else is acceptance.
97 struct ipipstat ipipstat;
100 SYSCTL_DECL(_net_inet_ipip);
101 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipip, OID_AUTO,
102 ipip_allow, CTLFLAG_RW, ipip_allow, 0, "");
103 SYSCTL_V_STRUCT(V_NET, vnet_ipsec, _net_inet_ipip, IPSECCTL_STATS,
104 stats, CTLFLAG_RD, ipipstat, ipipstat, "");
107 #define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
109 static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
113 * Really only a wrapper for ipip_input(), for use with IPv6.
116 ip4_input6(struct mbuf **m, int *offp, int proto)
119 /* If we do not accept IP-in-IP explicitly, drop. */
120 if (!V_ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) {
121 DPRINTF(("%s: dropped due to policy\n", __func__));
122 V_ipipstat.ipips_pdrops++;
127 _ipip_input(*m, *offp, NULL);
134 * Really only a wrapper for ipip_input(), for use with IPv4.
137 ip4_input(struct mbuf *m, int off)
140 /* If we do not accept IP-in-IP explicitly, drop. */
141 if (!V_ipip_allow && (m->m_flags & M_IPSEC) == 0) {
142 DPRINTF(("%s: dropped due to policy\n", __func__));
143 V_ipipstat.ipips_pdrops++;
148 _ipip_input(m, off, NULL);
153 * ipip_input gets called when we receive an IP{46} encapsulated packet,
154 * either because we got it at a real interface, or because AH or ESP
155 * were being used in tunnel mode (in which case the rcvif element will
156 * contain the address of the encX interface associated with the tunnel.
160 _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
162 INIT_VNET_NET(curvnet);
163 INIT_VNET_IPSEC(curvnet);
164 register struct sockaddr_in *sin;
165 register struct ifnet *ifp;
166 register struct ifaddr *ifa;
169 register struct sockaddr_in6 *sin6;
170 struct ip6_hdr *ip6 = NULL;
179 V_ipipstat.ipips_ipackets++;
181 m_copydata(m, 0, 1, &v);
186 hlen = sizeof(struct ip);
191 hlen = sizeof(struct ip6_hdr);
195 V_ipipstat.ipips_family++;
197 return /* EAFNOSUPPORT */;
200 /* Bring the IP header in the first mbuf, if not there already */
201 if (m->m_len < hlen) {
202 if ((m = m_pullup(m, hlen)) == NULL) {
203 DPRINTF(("%s: m_pullup (1) failed\n", __func__));
204 V_ipipstat.ipips_hdrops++;
209 ipo = mtod(m, struct ip *);
212 if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
213 if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
214 ipip_mroute_input (m, iphlen);
218 #endif /* MROUTING */
220 /* Keep outer ecn field. */
229 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
233 panic("ipip_input: unknown ip version %u (outer)", v>>4);
236 /* Remove outer IP header */
240 if (m->m_pkthdr.len < sizeof(struct ip)) {
241 V_ipipstat.ipips_hdrops++;
246 m_copydata(m, 0, 1, &v);
251 hlen = sizeof(struct ip);
257 hlen = sizeof(struct ip6_hdr);
261 V_ipipstat.ipips_family++;
263 return; /* EAFNOSUPPORT */
267 * Bring the inner IP header in the first mbuf, if not there already.
269 if (m->m_len < hlen) {
270 if ((m = m_pullup(m, hlen)) == NULL) {
271 DPRINTF(("%s: m_pullup (2) failed\n", __func__));
272 V_ipipstat.ipips_hdrops++;
278 * RFC 1853 specifies that the inner TTL should not be touched on
279 * decapsulation. There's no reason this comment should be here, but
280 * this is as good as any a position.
283 /* Some sanity checks in the inner IP header */
287 ipo = mtod(m, struct ip *);
289 ip_ecn_egress(V_ip4_ipsec_ecn, &otos, &ipo->ip_tos);
294 ip6 = (struct ip6_hdr *) ipo;
296 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
297 ip_ecn_egress(V_ip6_ipsec_ecn, &otos, &itos);
298 ip6->ip6_flow &= ~htonl(0xff << 20);
299 ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
303 panic("ipip_input: unknown ip version %u (inner)", v>>4);
306 /* Check for local address spoofing. */
307 if ((m->m_pkthdr.rcvif == NULL ||
308 !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) &&
311 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
312 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
315 if (ifa->ifa_addr->sa_family !=
319 sin = (struct sockaddr_in *) ifa->ifa_addr;
321 if (sin->sin_addr.s_addr ==
322 ipo->ip_src.s_addr) {
323 V_ipipstat.ipips_spoof++;
333 if (ifa->ifa_addr->sa_family !=
337 sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
339 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
340 V_ipipstat.ipips_spoof++;
354 V_ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen;
360 ipsec_bpf(m, NULL, AF_INET, ENC_IN|ENC_AFTER);
365 ipsec_bpf(m, NULL, AF_INET6, ENC_IN|ENC_AFTER);
369 panic("%s: bogus ip version %u", __func__, v>>4);
371 /* pass the mbuf to enc0 for packet filtering */
372 if (ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER) != 0)
377 * Interface pointer stays the same; if no IPsec processing has
378 * been done (or will be done), this will point to a normal
379 * interface. Otherwise, it'll point to an enc interface, which
380 * will allow a packet filter to distinguish between secure and
396 panic("%s: bogus ip version %u", __func__, v>>4);
399 if (netisr_queue(isr, m)) { /* (0) on success. */
400 V_ipipstat.ipips_qfull++;
401 DPRINTF(("%s: packet dropped because of full queue\n",
409 struct ipsecrequest *isr,
415 INIT_VNET_IPSEC(curvnet);
417 INIT_VNET_INET(curvnet);
419 struct secasvar *sav;
421 struct secasindex *saidx;
428 struct ip6_hdr *ip6, *ip6o;
432 IPSEC_ASSERT(sav != NULL, ("null SA"));
433 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
435 /* XXX Deal with empty TDB source/destination addresses. */
437 m_copydata(m, 0, 1, &tp);
438 tp = (tp >> 4) & 0xff; /* Get the IP version number. */
440 saidx = &sav->sah->saidx;
441 switch (saidx->dst.sa.sa_family) {
444 if (saidx->src.sa.sa_family != AF_INET ||
445 saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
446 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
447 DPRINTF(("%s: unspecified tunnel endpoint "
448 "address in SA %s/%08lx\n", __func__,
449 ipsec_address(&saidx->dst),
450 (u_long) ntohl(sav->spi)));
451 V_ipipstat.ipips_unspec++;
456 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
458 DPRINTF(("%s: M_PREPEND failed\n", __func__));
459 V_ipipstat.ipips_hdrops++;
464 ipo = mtod(m, struct ip *);
466 ipo->ip_v = IPVERSION;
468 ipo->ip_len = htons(m->m_pkthdr.len);
469 ipo->ip_ttl = V_ip_defttl;
471 ipo->ip_src = saidx->src.sin.sin_addr;
472 ipo->ip_dst = saidx->dst.sin.sin_addr;
474 ipo->ip_id = ip_newid();
476 /* If the inner protocol is IP... */
477 if (tp == IPVERSION) {
478 /* Save ECN notification */
479 m_copydata(m, sizeof(struct ip) +
480 offsetof(struct ip, ip_tos),
481 sizeof(u_int8_t), (caddr_t) &itos);
483 ipo->ip_p = IPPROTO_IPIP;
486 * We should be keeping tunnel soft-state and
487 * send back ICMPs if needed.
489 m_copydata(m, sizeof(struct ip) +
490 offsetof(struct ip, ip_off),
491 sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
492 ipo->ip_off = ntohs(ipo->ip_off);
493 ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
494 ipo->ip_off = htons(ipo->ip_off);
497 else if (tp == (IPV6_VERSION >> 4)) {
500 /* Save ECN notification. */
501 m_copydata(m, sizeof(struct ip) +
502 offsetof(struct ip6_hdr, ip6_flow),
503 sizeof(u_int32_t), (caddr_t) &itos32);
504 itos = ntohl(itos32) >> 20;
505 ipo->ip_p = IPPROTO_IPV6;
514 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
521 if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
522 saidx->src.sa.sa_family != AF_INET6 ||
523 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
524 DPRINTF(("%s: unspecified tunnel endpoint "
525 "address in SA %s/%08lx\n", __func__,
526 ipsec_address(&saidx->dst),
527 (u_long) ntohl(sav->spi)));
528 V_ipipstat.ipips_unspec++;
533 /* scoped address handling */
534 ip6 = mtod(m, struct ip6_hdr *);
535 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
536 ip6->ip6_src.s6_addr16[1] = 0;
537 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
538 ip6->ip6_dst.s6_addr16[1] = 0;
540 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
542 DPRINTF(("%s: M_PREPEND failed\n", __func__));
543 V_ipipstat.ipips_hdrops++;
548 /* Initialize IPv6 header */
549 ip6o = mtod(m, struct ip6_hdr *);
551 ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
552 ip6o->ip6_vfc |= IPV6_VERSION;
553 ip6o->ip6_plen = htons(m->m_pkthdr.len);
554 ip6o->ip6_hlim = V_ip_defttl;
555 ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
556 ip6o->ip6_src = saidx->src.sin6.sin6_addr;
559 if (tp == IPVERSION) {
560 /* Save ECN notification */
561 m_copydata(m, sizeof(struct ip6_hdr) +
562 offsetof(struct ip, ip_tos), sizeof(u_int8_t),
565 /* This is really IPVERSION. */
566 ip6o->ip6_nxt = IPPROTO_IPIP;
569 if (tp == (IPV6_VERSION >> 4)) {
572 /* Save ECN notification. */
573 m_copydata(m, sizeof(struct ip6_hdr) +
574 offsetof(struct ip6_hdr, ip6_flow),
575 sizeof(u_int32_t), (caddr_t) &itos32);
576 itos = ntohl(itos32) >> 20;
578 ip6o->ip6_nxt = IPPROTO_IPV6;
584 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
585 ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
591 DPRINTF(("%s: unsupported protocol family %u\n", __func__,
592 saidx->dst.sa.sa_family));
593 V_ipipstat.ipips_family++;
594 error = EAFNOSUPPORT; /* XXX diffs from openbsd */
598 V_ipipstat.ipips_opackets++;
602 if (saidx->dst.sa.sa_family == AF_INET) {
604 if (sav->tdb_xform->xf_type == XF_IP4)
605 tdb->tdb_cur_bytes +=
606 m->m_pkthdr.len - sizeof(struct ip);
608 V_ipipstat.ipips_obytes += m->m_pkthdr.len - sizeof(struct ip);
613 if (saidx->dst.sa.sa_family == AF_INET6) {
615 if (sav->tdb_xform->xf_type == XF_IP4)
616 tdb->tdb_cur_bytes +=
617 m->m_pkthdr.len - sizeof(struct ip6_hdr);
619 V_ipipstat.ipips_obytes +=
620 m->m_pkthdr.len - sizeof(struct ip6_hdr);
634 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
636 sav->tdb_xform = xsp;
641 ipe4_zeroize(struct secasvar *sav)
643 sav->tdb_xform = NULL;
648 ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
650 /* This is a rather serious mistake, so no conditional printing. */
651 printf("%s: should never be called\n", __func__);
657 static struct xformsw ipe4_xformsw = {
658 XF_IP4, 0, "IPv4 Simple Encapsulation",
659 ipe4_init, ipe4_zeroize, ipe4_input, ipip_output,
662 extern struct domain inetdomain;
663 static struct protosw ipe4_protosw =
664 { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
672 static struct ip6protosw ipe6_protosw =
673 { SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
683 * Check the encapsulated packet to see if we want it
686 ipe4_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
689 * Only take packets coming from IPSEC tunnels; the rest
690 * must be handled by the gif tunnel code. Note that we
691 * also return a minimum priority when we want the packet
692 * so any explicit gif tunnels take precedence.
694 return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
703 xform_register(&ipe4_xformsw);
704 /* attach to encapsulation framework */
705 /* XXX save return cookie for detach on module remove */
706 (void) encap_attach_func(AF_INET, -1,
707 ipe4_encapcheck, &ipe4_protosw, NULL);
709 (void) encap_attach_func(AF_INET6, -1,
710 ipe4_encapcheck, (struct protosw *)&ipe6_protosw, NULL);
713 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);