1 /* $FreeBSD: src/sys/netipsec/xform_ipip.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $ */
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"
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/kernel.h>
50 #include <sys/protosw.h>
51 #include <sys/sysctl.h>
54 #include <net/route.h>
55 #include <net/netisr.h>
56 #include <net/raw_cb.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_ecn.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/ip_encap.h>
66 #include <netproto/ipsec/ipsec.h>
67 #include <netproto/ipsec/xform.h>
69 #include <netproto/ipsec/ipip_var.h>
72 #include <netinet/ip_mroute.h>
76 #include <netinet/ip6.h>
77 #include <netproto/ipsec/ipsec6.h>
78 #include <netinet6/ip6_ecn.h>
79 #include <netinet6/in6_var.h>
80 #include <netinet6/ip6protosw.h>
83 #include <netproto/ipsec/key.h>
84 #include <netproto/ipsec/key_debug.h>
86 #include <machine/stdarg.h>
88 typedef void pr_in_input_t (struct mbuf *, int, int); /* XXX FIX THIS */
91 * We can control the acceptance of IP4 packets by altering the sysctl
92 * net.inet.ipip.allow value. Zero means drop them, all else is acceptance.
95 struct ipipstat ipipstat;
97 SYSCTL_DECL(_net_inet_ipip);
98 SYSCTL_INT(_net_inet_ipip, OID_AUTO,
99 ipip_allow, CTLFLAG_RW, &ipip_allow, 0, "");
100 SYSCTL_STRUCT(_net_inet_ipip, IPSECCTL_STATS,
101 stats, CTLFLAG_RD, &ipipstat, ipipstat, "");
104 #define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
106 static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
110 * Really only a wrapper for ipip_input(), for use with IPv6.
113 ip4_input6(struct mbuf **mp, int *offp, int proto)
116 /* If we do not accept IP-in-IP explicitly, drop. */
117 if (!ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) {
118 DPRINTF(("ip4_input6: dropped due to policy\n"));
119 ipipstat.ipips_pdrops++;
124 _ipip_input(*mp, *offp, NULL);
131 * Really only a wrapper for ipip_input(), for use with IPv4.
134 ip4_input(struct mbuf **mp, int *offp, int proto)
137 /* If we do not accept IP-in-IP explicitly, drop. */
138 if (!ipip_allow && (m->m_flags & M_IPSEC) == 0) {
139 DPRINTF(("ip4_input: dropped due to policy\n"));
140 ipipstat.ipips_pdrops++;
145 _ipip_input(*mp, *offp, NULL);
151 * ipip_input gets called when we receive an IP{46} encapsulated packet,
152 * either because we got it at a real interface, or because AH or ESP
153 * were being used in tunnel mode (in which case the rcvif element will
154 * contain the address of the encX interface associated with the tunnel.
158 _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
160 struct sockaddr_in *sin;
165 struct sockaddr_in6 *sin6;
166 struct ip6_hdr *ip6 = NULL;
175 ipipstat.ipips_ipackets++;
177 m_copydata(m, 0, 1, &v);
182 hlen = sizeof(struct ip);
187 hlen = sizeof(struct ip6_hdr);
191 DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
192 "for outer header\n", v, v>>4));
193 ipipstat.ipips_family++;
195 return /* EAFNOSUPPORT */;
198 /* Bring the IP header in the first mbuf, if not there already */
199 if (m->m_len < hlen) {
200 if ((m = m_pullup(m, hlen)) == NULL) {
201 DPRINTF(("ipip_input: m_pullup (1) failed\n"));
202 ipipstat.ipips_hdrops++;
207 ipo = mtod(m, struct ip *);
210 if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
211 if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
212 ipip_mroute_input (m, iphlen);
216 #endif /* MROUTING */
218 /* Keep outer ecn field. */
227 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
231 panic("ipip_input: unknown ip version %u (outer)", v>>4);
234 /* Remove outer IP header */
238 if (m->m_pkthdr.len < sizeof(struct ip)) {
239 ipipstat.ipips_hdrops++;
244 m_copydata(m, 0, 1, &v);
249 hlen = sizeof(struct ip);
255 hlen = sizeof(struct ip6_hdr);
259 DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
260 "for inner header\n", v, v>>4));
261 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(("ipip_input: m_pullup (2) failed\n"));
272 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(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(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)) &&
310 TAILQ_FOREACH(ifp, &ifnet, if_link) {
311 struct ifaddr_container *ifac;
312 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], 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 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 ipipstat.ipips_spoof++;
352 ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen;
355 * Interface pointer stays the same; if no IPsec processing has
356 * been done (or will be done), this will point to a normal
357 * interface. Otherwise, it'll point to an enc interface, which
358 * will allow a packet filter to distinguish between secure and
374 panic("ipip_input: should never reach here");
377 if (netisr_queue(isr, m)) {
378 ipipstat.ipips_qfull++;
380 DPRINTF(("ipip_input: packet dropped because of full queue\n"));
387 struct ipsecrequest *isr,
393 struct secasvar *sav;
395 struct secasindex *saidx;
402 struct ip6_hdr *ip6, *ip6o;
406 KASSERT(sav != NULL, ("ipip_output: null SA"));
407 KASSERT(sav->sah != NULL, ("ipip_output: null SAH"));
409 /* XXX Deal with empty TDB source/destination addresses. */
411 m_copydata(m, 0, 1, &tp);
412 tp = (tp >> 4) & 0xff; /* Get the IP version number. */
414 saidx = &sav->sah->saidx;
415 switch (saidx->dst.sa.sa_family) {
418 if (saidx->src.sa.sa_family != AF_INET ||
419 saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
420 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
421 DPRINTF(("ipip_output: unspecified tunnel endpoint "
422 "address in SA %s/%08lx\n",
423 ipsec_address(&saidx->dst),
424 (u_long) ntohl(sav->spi)));
425 ipipstat.ipips_unspec++;
430 M_PREPEND(m, sizeof(struct ip), MB_DONTWAIT);
432 DPRINTF(("ipip_output: M_PREPEND failed\n"));
433 ipipstat.ipips_hdrops++;
438 ipo = mtod(m, struct ip *);
440 ipo->ip_v = IPVERSION;
442 ipo->ip_len = htons(m->m_pkthdr.len);
443 ipo->ip_ttl = ip_defttl;
445 ipo->ip_src = saidx->src.sin.sin_addr;
446 ipo->ip_dst = saidx->dst.sin.sin_addr;
448 ipo->ip_id = ip_newid();
450 /* If the inner protocol is IP... */
451 if (tp == IPVERSION) {
452 /* Save ECN notification */
453 m_copydata(m, sizeof(struct ip) +
454 offsetof(struct ip, ip_tos),
455 sizeof(u_int8_t), (caddr_t) &itos);
457 ipo->ip_p = IPPROTO_IPIP;
460 * We should be keeping tunnel soft-state and
461 * send back ICMPs if needed.
463 m_copydata(m, sizeof(struct ip) +
464 offsetof(struct ip, ip_off),
465 sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
466 ipo->ip_off = ntohs(ipo->ip_off);
467 ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
468 ipo->ip_off = htons(ipo->ip_off);
471 else if (tp == (IPV6_VERSION >> 4)) {
474 /* Save ECN notification. */
475 m_copydata(m, sizeof(struct ip) +
476 offsetof(struct ip6_hdr, ip6_flow),
477 sizeof(u_int32_t), (caddr_t) &itos32);
478 itos = ntohl(itos32) >> 20;
479 ipo->ip_p = IPPROTO_IPV6;
488 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
495 if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
496 saidx->src.sa.sa_family != AF_INET6 ||
497 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
498 DPRINTF(("ipip_output: unspecified tunnel endpoint "
499 "address in SA %s/%08lx\n",
500 ipsec_address(&saidx->dst),
501 (u_long) ntohl(sav->spi)));
502 ipipstat.ipips_unspec++;
507 /* scoped address handling */
508 ip6 = mtod(m, struct ip6_hdr *);
509 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
510 ip6->ip6_src.s6_addr16[1] = 0;
511 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
512 ip6->ip6_dst.s6_addr16[1] = 0;
514 M_PREPEND(m, sizeof(struct ip6_hdr), MB_DONTWAIT);
516 DPRINTF(("ipip_output: M_PREPEND failed\n"));
517 ipipstat.ipips_hdrops++;
523 /* Initialize IPv6 header */
524 ip6o = mtod(m, struct ip6_hdr *);
526 ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
527 ip6o->ip6_vfc |= IPV6_VERSION;
528 ip6o->ip6_plen = htons(m->m_pkthdr.len);
529 ip6o->ip6_hlim = ip_defttl;
530 ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
531 ip6o->ip6_src = saidx->src.sin6.sin6_addr;
534 if (tp == IPVERSION) {
535 /* Save ECN notification */
536 m_copydata(m, sizeof(struct ip6_hdr) +
537 offsetof(struct ip, ip_tos), sizeof(u_int8_t),
540 /* This is really IPVERSION. */
541 ip6o->ip6_nxt = IPPROTO_IPIP;
544 if (tp == (IPV6_VERSION >> 4)) {
547 /* Save ECN notification. */
548 m_copydata(m, sizeof(struct ip6_hdr) +
549 offsetof(struct ip6_hdr, ip6_flow),
550 sizeof(u_int32_t), (caddr_t) &itos32);
551 itos = ntohl(itos32) >> 20;
553 ip6o->ip6_nxt = IPPROTO_IPV6;
559 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
560 ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
566 DPRINTF(("ipip_output: unsupported protocol family %u\n",
567 saidx->dst.sa.sa_family));
568 ipipstat.ipips_family++;
569 error = EAFNOSUPPORT; /* XXX diffs from openbsd */
573 ipipstat.ipips_opackets++;
577 if (saidx->dst.sa.sa_family == AF_INET) {
579 if (sav->tdb_xform->xf_type == XF_IP4)
580 tdb->tdb_cur_bytes +=
581 m->m_pkthdr.len - sizeof(struct ip);
583 ipipstat.ipips_obytes += m->m_pkthdr.len - sizeof(struct ip);
588 if (saidx->dst.sa.sa_family == AF_INET6) {
590 if (sav->tdb_xform->xf_type == XF_IP4)
591 tdb->tdb_cur_bytes +=
592 m->m_pkthdr.len - sizeof(struct ip6_hdr);
594 ipipstat.ipips_obytes +=
595 m->m_pkthdr.len - sizeof(struct ip6_hdr);
602 m_freem(m), *mp = NULL;
608 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
610 sav->tdb_xform = xsp;
615 ipe4_zeroize(struct secasvar *sav)
617 sav->tdb_xform = NULL;
622 ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
624 /* This is a rather serious mistake, so no conditional printing. */
625 kprintf("ipe4_input: should never be called\n");
631 static struct xformsw ipe4_xformsw = {
632 XF_IP4, 0, "IPv4 Simple Encapsulation",
633 ipe4_init, ipe4_zeroize, ipe4_input, ipip_output,
636 extern struct domain inetdomain;
638 static struct protosw ipe4_protosw[] = {
641 .pr_domain = &inetdomain,
642 .pr_protocol = IPPROTO_IPV4,
643 .pr_flags = PR_ATOMIC|PR_ADDR,
645 .pr_input = ip4_input,
648 .pr_ctloutput = rip_ctloutput,
650 .pr_ctlport = cpu0_ctlport,
652 .pr_usrreqs = &rip_usrreqs
657 .pr_domain = &inetdomain,
658 .pr_protocol = IPPROTO_IPV6,
659 .pr_flags = PR_ATOMIC|PR_ADDR,
661 .pr_input = ip4_input6,
664 .pr_ctloutput = rip_ctloutput,
666 .pr_ctlport = cpu0_ctlport,
668 .pr_usrreqs = &rip_usrreqs
674 * Check the encapsulated packet to see if we want it
677 ipe4_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
680 * Only take packets coming from IPSEC tunnels; the rest
681 * must be handled by the gif tunnel code. Note that we
682 * also return a minimum priority when we want the packet
683 * so any explicit gif tunnels take precedence.
685 return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
691 xform_register(&ipe4_xformsw);
692 /* attach to encapsulation framework */
693 /* XXX save return cookie for detach on module remove */
694 encap_attach_func(AF_INET, -1,
695 ipe4_encapcheck, &ipe4_protosw[0], NULL);
697 encap_attach_func(AF_INET6, -1,
698 ipe4_encapcheck, &ipe4_protosw[1], NULL);
701 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);
702 #endif /* FAST_IPSEC */