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"
44 #include "opt_route.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/socket.h>
51 #include <sys/kernel.h>
52 #include <sys/protosw.h>
53 #include <sys/sysctl.h>
54 #include <sys/vimage.h>
58 #include <net/route.h>
59 #include <net/netisr.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_ecn.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/ip_encap.h>
70 #include <netinet/ip_mroute.h>
72 #include <netinet/vinet.h>
74 #include <netipsec/ipsec.h>
75 #include <netipsec/xform.h>
77 #include <netipsec/ipip_var.h>
80 #include <netinet/ip6.h>
81 #include <netipsec/ipsec6.h>
82 #include <netinet6/ip6_ecn.h>
83 #include <netinet6/in6_var.h>
84 #include <netinet6/ip6protosw.h>
87 #include <netipsec/key.h>
88 #include <netipsec/key_debug.h>
90 #include <machine/stdarg.h>
93 * We can control the acceptance of IP4 packets by altering the sysctl
94 * net.inet.ipip.allow value. Zero means drop them, all else is acceptance.
98 struct ipipstat ipipstat;
101 SYSCTL_DECL(_net_inet_ipip);
102 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipip, OID_AUTO,
103 ipip_allow, CTLFLAG_RW, ipip_allow, 0, "");
104 SYSCTL_V_STRUCT(V_NET, vnet_ipsec, _net_inet_ipip, IPSECCTL_STATS,
105 stats, CTLFLAG_RD, ipipstat, ipipstat, "");
108 #define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
110 static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
114 * Really only a wrapper for ipip_input(), for use with IPv6.
117 ip4_input6(struct mbuf **m, int *offp, int proto)
120 /* If we do not accept IP-in-IP explicitly, drop. */
121 if (!V_ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) {
122 DPRINTF(("%s: dropped due to policy\n", __func__));
123 V_ipipstat.ipips_pdrops++;
128 _ipip_input(*m, *offp, NULL);
135 * Really only a wrapper for ipip_input(), for use with IPv4.
138 ip4_input(struct mbuf *m, int off)
141 /* If we do not accept IP-in-IP explicitly, drop. */
142 if (!V_ipip_allow && (m->m_flags & M_IPSEC) == 0) {
143 DPRINTF(("%s: dropped due to policy\n", __func__));
144 V_ipipstat.ipips_pdrops++;
149 _ipip_input(m, off, NULL);
154 * ipip_input gets called when we receive an IP{46} encapsulated packet,
155 * either because we got it at a real interface, or because AH or ESP
156 * were being used in tunnel mode (in which case the rcvif element will
157 * contain the address of the encX interface associated with the tunnel.
161 _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
163 INIT_VNET_NET(curvnet);
164 INIT_VNET_IPSEC(curvnet);
165 register struct sockaddr_in *sin;
166 register struct ifnet *ifp;
167 register struct ifaddr *ifa;
170 register struct sockaddr_in6 *sin6;
171 struct ip6_hdr *ip6 = NULL;
180 V_ipipstat.ipips_ipackets++;
182 m_copydata(m, 0, 1, &v);
187 hlen = sizeof(struct ip);
192 hlen = sizeof(struct ip6_hdr);
196 V_ipipstat.ipips_family++;
198 return /* EAFNOSUPPORT */;
201 /* Bring the IP header in the first mbuf, if not there already */
202 if (m->m_len < hlen) {
203 if ((m = m_pullup(m, hlen)) == NULL) {
204 DPRINTF(("%s: m_pullup (1) failed\n", __func__));
205 V_ipipstat.ipips_hdrops++;
210 ipo = mtod(m, struct ip *);
213 if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
214 if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
215 ipip_mroute_input (m, iphlen);
219 #endif /* MROUTING */
221 /* Keep outer ecn field. */
230 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
234 panic("ipip_input: unknown ip version %u (outer)", v>>4);
237 /* Remove outer IP header */
241 if (m->m_pkthdr.len < sizeof(struct ip)) {
242 V_ipipstat.ipips_hdrops++;
247 m_copydata(m, 0, 1, &v);
252 hlen = sizeof(struct ip);
258 hlen = sizeof(struct ip6_hdr);
262 V_ipipstat.ipips_family++;
264 return; /* EAFNOSUPPORT */
268 * Bring the inner IP header in the first mbuf, if not there already.
270 if (m->m_len < hlen) {
271 if ((m = m_pullup(m, hlen)) == NULL) {
272 DPRINTF(("%s: m_pullup (2) failed\n", __func__));
273 V_ipipstat.ipips_hdrops++;
279 * RFC 1853 specifies that the inner TTL should not be touched on
280 * decapsulation. There's no reason this comment should be here, but
281 * this is as good as any a position.
284 /* Some sanity checks in the inner IP header */
288 ipo = mtod(m, struct ip *);
290 ip_ecn_egress(V_ip4_ipsec_ecn, &otos, &ipo->ip_tos);
295 ip6 = (struct ip6_hdr *) ipo;
297 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
298 ip_ecn_egress(V_ip6_ipsec_ecn, &otos, &itos);
299 ip6->ip6_flow &= ~htonl(0xff << 20);
300 ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
304 panic("ipip_input: unknown ip version %u (inner)", v>>4);
307 /* Check for local address spoofing. */
308 if ((m->m_pkthdr.rcvif == NULL ||
309 !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) &&
312 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
313 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
316 if (ifa->ifa_addr->sa_family !=
320 sin = (struct sockaddr_in *) ifa->ifa_addr;
322 if (sin->sin_addr.s_addr ==
323 ipo->ip_src.s_addr) {
324 V_ipipstat.ipips_spoof++;
334 if (ifa->ifa_addr->sa_family !=
338 sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
340 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
341 V_ipipstat.ipips_spoof++;
355 V_ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen;
361 ipsec_bpf(m, NULL, AF_INET, ENC_IN|ENC_AFTER);
366 ipsec_bpf(m, NULL, AF_INET6, ENC_IN|ENC_AFTER);
370 panic("%s: bogus ip version %u", __func__, v>>4);
372 /* pass the mbuf to enc0 for packet filtering */
373 if (ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_AFTER) != 0)
378 * Interface pointer stays the same; if no IPsec processing has
379 * been done (or will be done), this will point to a normal
380 * interface. Otherwise, it'll point to an enc interface, which
381 * will allow a packet filter to distinguish between secure and
397 panic("%s: bogus ip version %u", __func__, v>>4);
400 if (netisr_queue(isr, m)) { /* (0) on success. */
401 V_ipipstat.ipips_qfull++;
402 DPRINTF(("%s: packet dropped because of full queue\n",
410 struct ipsecrequest *isr,
416 INIT_VNET_IPSEC(curvnet);
418 INIT_VNET_INET(curvnet);
420 struct secasvar *sav;
422 struct secasindex *saidx;
429 struct ip6_hdr *ip6, *ip6o;
433 IPSEC_ASSERT(sav != NULL, ("null SA"));
434 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
436 /* XXX Deal with empty TDB source/destination addresses. */
438 m_copydata(m, 0, 1, &tp);
439 tp = (tp >> 4) & 0xff; /* Get the IP version number. */
441 saidx = &sav->sah->saidx;
442 switch (saidx->dst.sa.sa_family) {
445 if (saidx->src.sa.sa_family != AF_INET ||
446 saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
447 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
448 DPRINTF(("%s: unspecified tunnel endpoint "
449 "address in SA %s/%08lx\n", __func__,
450 ipsec_address(&saidx->dst),
451 (u_long) ntohl(sav->spi)));
452 V_ipipstat.ipips_unspec++;
457 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
459 DPRINTF(("%s: M_PREPEND failed\n", __func__));
460 V_ipipstat.ipips_hdrops++;
465 ipo = mtod(m, struct ip *);
467 ipo->ip_v = IPVERSION;
469 ipo->ip_len = htons(m->m_pkthdr.len);
470 ipo->ip_ttl = V_ip_defttl;
472 ipo->ip_src = saidx->src.sin.sin_addr;
473 ipo->ip_dst = saidx->dst.sin.sin_addr;
475 ipo->ip_id = ip_newid();
477 /* If the inner protocol is IP... */
478 if (tp == IPVERSION) {
479 /* Save ECN notification */
480 m_copydata(m, sizeof(struct ip) +
481 offsetof(struct ip, ip_tos),
482 sizeof(u_int8_t), (caddr_t) &itos);
484 ipo->ip_p = IPPROTO_IPIP;
487 * We should be keeping tunnel soft-state and
488 * send back ICMPs if needed.
490 m_copydata(m, sizeof(struct ip) +
491 offsetof(struct ip, ip_off),
492 sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
493 ipo->ip_off = ntohs(ipo->ip_off);
494 ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
495 ipo->ip_off = htons(ipo->ip_off);
498 else if (tp == (IPV6_VERSION >> 4)) {
501 /* Save ECN notification. */
502 m_copydata(m, sizeof(struct ip) +
503 offsetof(struct ip6_hdr, ip6_flow),
504 sizeof(u_int32_t), (caddr_t) &itos32);
505 itos = ntohl(itos32) >> 20;
506 ipo->ip_p = IPPROTO_IPV6;
515 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
522 if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
523 saidx->src.sa.sa_family != AF_INET6 ||
524 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
525 DPRINTF(("%s: unspecified tunnel endpoint "
526 "address in SA %s/%08lx\n", __func__,
527 ipsec_address(&saidx->dst),
528 (u_long) ntohl(sav->spi)));
529 V_ipipstat.ipips_unspec++;
534 /* scoped address handling */
535 ip6 = mtod(m, struct ip6_hdr *);
536 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
537 ip6->ip6_src.s6_addr16[1] = 0;
538 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
539 ip6->ip6_dst.s6_addr16[1] = 0;
541 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
543 DPRINTF(("%s: M_PREPEND failed\n", __func__));
544 V_ipipstat.ipips_hdrops++;
549 /* Initialize IPv6 header */
550 ip6o = mtod(m, struct ip6_hdr *);
552 ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
553 ip6o->ip6_vfc |= IPV6_VERSION;
554 ip6o->ip6_plen = htons(m->m_pkthdr.len);
555 ip6o->ip6_hlim = V_ip_defttl;
556 ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
557 ip6o->ip6_src = saidx->src.sin6.sin6_addr;
560 if (tp == IPVERSION) {
561 /* Save ECN notification */
562 m_copydata(m, sizeof(struct ip6_hdr) +
563 offsetof(struct ip, ip_tos), sizeof(u_int8_t),
566 /* This is really IPVERSION. */
567 ip6o->ip6_nxt = IPPROTO_IPIP;
570 if (tp == (IPV6_VERSION >> 4)) {
573 /* Save ECN notification. */
574 m_copydata(m, sizeof(struct ip6_hdr) +
575 offsetof(struct ip6_hdr, ip6_flow),
576 sizeof(u_int32_t), (caddr_t) &itos32);
577 itos = ntohl(itos32) >> 20;
579 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);
635 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
637 sav->tdb_xform = xsp;
642 ipe4_zeroize(struct secasvar *sav)
644 sav->tdb_xform = NULL;
649 ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
651 /* This is a rather serious mistake, so no conditional printing. */
652 printf("%s: should never be called\n", __func__);
658 static struct xformsw ipe4_xformsw = {
659 XF_IP4, 0, "IPv4 Simple Encapsulation",
660 ipe4_init, ipe4_zeroize, ipe4_input, ipip_output,
663 extern struct domain inetdomain;
664 static struct protosw ipe4_protosw = {
666 .pr_domain = &inetdomain,
667 .pr_protocol = IPPROTO_IPV4,
668 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
669 .pr_input = ip4_input,
670 .pr_ctloutput = rip_ctloutput,
671 .pr_usrreqs = &rip_usrreqs
674 static struct ip6protosw ipe6_protosw = {
676 .pr_domain = &inetdomain,
677 .pr_protocol = IPPROTO_IPV6,
678 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
679 .pr_input = ip4_input6,
680 .pr_ctloutput = rip_ctloutput,
681 .pr_usrreqs = &rip_usrreqs
686 * Check the encapsulated packet to see if we want it
689 ipe4_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
692 * Only take packets coming from IPSEC tunnels; the rest
693 * must be handled by the gif tunnel code. Note that we
694 * also return a minimum priority when we want the packet
695 * so any explicit gif tunnels take precedence.
697 return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
706 xform_register(&ipe4_xformsw);
707 /* attach to encapsulation framework */
708 /* XXX save return cookie for detach on module remove */
709 (void) encap_attach_func(AF_INET, -1,
710 ipe4_encapcheck, &ipe4_protosw, NULL);
712 (void) encap_attach_func(AF_INET6, -1,
713 ipe4_encapcheck, (struct protosw *)&ipe6_protosw, NULL);
716 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);