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>
55 #include <net/if_var.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 <netipsec/ipsec.h>
70 #include <netipsec/xform.h>
72 #include <netipsec/ipip_var.h>
75 #include <netinet/ip6.h>
76 #include <netipsec/ipsec6.h>
77 #include <netinet6/ip6_ecn.h>
78 #include <netinet6/in6_var.h>
79 #include <netinet6/ip6protosw.h>
82 #include <netipsec/key.h>
83 #include <netipsec/key_debug.h>
85 #include <machine/stdarg.h>
88 * We can control the acceptance of IP4 packets by altering the sysctl
89 * net.inet.ipip.allow value. Zero means drop them, all else is acceptance.
91 VNET_DEFINE(int, ipip_allow) = 0;
92 VNET_PCPUSTAT_DEFINE(struct ipipstat, ipipstat);
93 VNET_PCPUSTAT_SYSINIT(ipipstat);
96 VNET_PCPUSTAT_SYSUNINIT(ipipstat);
99 SYSCTL_DECL(_net_inet_ipip);
100 SYSCTL_VNET_INT(_net_inet_ipip, OID_AUTO,
101 ipip_allow, CTLFLAG_RW, &VNET_NAME(ipip_allow), 0, "");
102 SYSCTL_VNET_PCPUSTAT(_net_inet_ipip, IPSECCTL_STATS, stats,
103 struct ipipstat, ipipstat,
104 "IPIP statistics (struct ipipstat, netipsec/ipip_var.h)");
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 IPIPSTAT_INC(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 IPIPSTAT_INC(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)
164 struct ip6_hdr *ip6 = NULL;
172 IPIPSTAT_INC(ipips_ipackets);
174 m_copydata(m, 0, 1, &v);
179 hlen = sizeof(struct ip);
184 hlen = sizeof(struct ip6_hdr);
188 IPIPSTAT_INC(ipips_family);
190 return /* EAFNOSUPPORT */;
193 /* Bring the IP header in the first mbuf, if not there already */
194 if (m->m_len < hlen) {
195 if ((m = m_pullup(m, hlen)) == NULL) {
196 DPRINTF(("%s: m_pullup (1) failed\n", __func__));
197 IPIPSTAT_INC(ipips_hdrops);
201 ipo = mtod(m, struct ip *);
203 /* Keep outer ecn field. */
212 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
216 panic("ipip_input: unknown ip version %u (outer)", v>>4);
219 /* Remove outer IP header */
223 if (m->m_pkthdr.len < sizeof(struct ip)) {
224 IPIPSTAT_INC(ipips_hdrops);
229 m_copydata(m, 0, 1, &v);
234 hlen = sizeof(struct ip);
240 hlen = sizeof(struct ip6_hdr);
244 IPIPSTAT_INC(ipips_family);
246 return; /* EAFNOSUPPORT */
250 * Bring the inner IP header in the first mbuf, if not there already.
252 if (m->m_len < hlen) {
253 if ((m = m_pullup(m, hlen)) == NULL) {
254 DPRINTF(("%s: m_pullup (2) failed\n", __func__));
255 IPIPSTAT_INC(ipips_hdrops);
261 * RFC 1853 specifies that the inner TTL should not be touched on
262 * decapsulation. There's no reason this comment should be here, but
263 * this is as good as any a position.
266 /* Some sanity checks in the inner IP header */
270 ipo = mtod(m, struct ip *);
271 ip_ecn_egress(V_ip4_ipsec_ecn, &otos, &ipo->ip_tos);
276 ip6 = (struct ip6_hdr *) ipo;
277 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
278 ip_ecn_egress(V_ip6_ipsec_ecn, &otos, &itos);
279 ip6->ip6_flow &= ~htonl(0xff << 20);
280 ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
284 panic("ipip_input: unknown ip version %u (inner)", v>>4);
287 /* Check for local address spoofing. */
288 if ((m->m_pkthdr.rcvif == NULL ||
289 !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) &&
292 if ((v >> 4) == IPVERSION &&
293 in_localip(ipo->ip_src) != 0) {
294 IPIPSTAT_INC(ipips_spoof);
300 if ((v & IPV6_VERSION_MASK) == IPV6_VERSION &&
301 in6_localip(&ip6->ip6_src) != 0) {
302 IPIPSTAT_INC(ipips_spoof);
310 IPIPSTAT_ADD(ipips_ibytes, m->m_pkthdr.len - iphlen);
313 * Interface pointer stays the same; if no IPsec processing has
314 * been done (or will be done), this will point to a normal
315 * interface. Otherwise, it'll point to an enc interface, which
316 * will allow a packet filter to distinguish between secure and
332 panic("%s: bogus ip version %u", __func__, v>>4);
335 if (netisr_queue(isr, m)) { /* (0) on success. */
336 IPIPSTAT_INC(ipips_qfull);
337 DPRINTF(("%s: packet dropped because of full queue\n",
345 struct ipsecrequest *isr,
351 struct secasvar *sav;
353 struct secasindex *saidx;
355 #if defined(INET) || defined(INET6)
362 struct ip6_hdr *ip6, *ip6o;
366 IPSEC_ASSERT(sav != NULL, ("null SA"));
367 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
369 /* XXX Deal with empty TDB source/destination addresses. */
371 m_copydata(m, 0, 1, &tp);
372 tp = (tp >> 4) & 0xff; /* Get the IP version number. */
374 saidx = &sav->sah->saidx;
375 switch (saidx->dst.sa.sa_family) {
378 if (saidx->src.sa.sa_family != AF_INET ||
379 saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
380 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
381 DPRINTF(("%s: unspecified tunnel endpoint "
382 "address in SA %s/%08lx\n", __func__,
383 ipsec_address(&saidx->dst),
384 (u_long) ntohl(sav->spi)));
385 IPIPSTAT_INC(ipips_unspec);
390 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
392 DPRINTF(("%s: M_PREPEND failed\n", __func__));
393 IPIPSTAT_INC(ipips_hdrops);
398 ipo = mtod(m, struct ip *);
400 ipo->ip_v = IPVERSION;
402 ipo->ip_len = htons(m->m_pkthdr.len);
403 ipo->ip_ttl = V_ip_defttl;
405 ipo->ip_src = saidx->src.sin.sin_addr;
406 ipo->ip_dst = saidx->dst.sin.sin_addr;
408 ipo->ip_id = ip_newid();
410 /* If the inner protocol is IP... */
413 /* Save ECN notification */
414 m_copydata(m, sizeof(struct ip) +
415 offsetof(struct ip, ip_tos),
416 sizeof(u_int8_t), (caddr_t) &itos);
418 ipo->ip_p = IPPROTO_IPIP;
421 * We should be keeping tunnel soft-state and
422 * send back ICMPs if needed.
424 m_copydata(m, sizeof(struct ip) +
425 offsetof(struct ip, ip_off),
426 sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
427 ipo->ip_off = ntohs(ipo->ip_off);
428 ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
429 ipo->ip_off = htons(ipo->ip_off);
432 case (IPV6_VERSION >> 4):
436 /* Save ECN notification. */
437 m_copydata(m, sizeof(struct ip) +
438 offsetof(struct ip6_hdr, ip6_flow),
439 sizeof(u_int32_t), (caddr_t) &itos32);
440 itos = ntohl(itos32) >> 20;
441 ipo->ip_p = IPPROTO_IPV6;
451 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
458 if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
459 saidx->src.sa.sa_family != AF_INET6 ||
460 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
461 DPRINTF(("%s: unspecified tunnel endpoint "
462 "address in SA %s/%08lx\n", __func__,
463 ipsec_address(&saidx->dst),
464 (u_long) ntohl(sav->spi)));
465 IPIPSTAT_INC(ipips_unspec);
470 /* scoped address handling */
471 ip6 = mtod(m, struct ip6_hdr *);
472 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
473 ip6->ip6_src.s6_addr16[1] = 0;
474 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
475 ip6->ip6_dst.s6_addr16[1] = 0;
477 M_PREPEND(m, sizeof(struct ip6_hdr), M_NOWAIT);
479 DPRINTF(("%s: M_PREPEND failed\n", __func__));
480 IPIPSTAT_INC(ipips_hdrops);
485 /* Initialize IPv6 header */
486 ip6o = mtod(m, struct ip6_hdr *);
488 ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
489 ip6o->ip6_vfc |= IPV6_VERSION;
490 ip6o->ip6_plen = htons(m->m_pkthdr.len);
491 ip6o->ip6_hlim = IPV6_DEFHLIM;
492 ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
493 ip6o->ip6_src = saidx->src.sin6.sin6_addr;
495 /* Fix payload length */
496 ip6o->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
501 /* Save ECN notification */
502 m_copydata(m, sizeof(struct ip6_hdr) +
503 offsetof(struct ip, ip_tos), sizeof(u_int8_t),
506 /* This is really IPVERSION. */
507 ip6o->ip6_nxt = IPPROTO_IPIP;
510 case (IPV6_VERSION >> 4):
514 /* Save ECN notification. */
515 m_copydata(m, sizeof(struct ip6_hdr) +
516 offsetof(struct ip6_hdr, ip6_flow),
517 sizeof(u_int32_t), (caddr_t) &itos32);
518 itos = ntohl(itos32) >> 20;
520 ip6o->ip6_nxt = IPPROTO_IPV6;
528 ip_ecn_ingress(V_ip6_ipsec_ecn, &otos, &itos);
529 ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
535 DPRINTF(("%s: unsupported protocol family %u\n", __func__,
536 saidx->dst.sa.sa_family));
537 IPIPSTAT_INC(ipips_family);
538 error = EAFNOSUPPORT; /* XXX diffs from openbsd */
542 IPIPSTAT_INC(ipips_opackets);
546 if (saidx->dst.sa.sa_family == AF_INET) {
548 if (sav->tdb_xform->xf_type == XF_IP4)
549 tdb->tdb_cur_bytes +=
550 m->m_pkthdr.len - sizeof(struct ip);
552 IPIPSTAT_ADD(ipips_obytes,
553 m->m_pkthdr.len - sizeof(struct ip));
558 if (saidx->dst.sa.sa_family == AF_INET6) {
560 if (sav->tdb_xform->xf_type == XF_IP4)
561 tdb->tdb_cur_bytes +=
562 m->m_pkthdr.len - sizeof(struct ip6_hdr);
564 IPIPSTAT_ADD(ipips_obytes,
565 m->m_pkthdr.len - sizeof(struct ip6_hdr));
578 #if defined(INET) || defined(INET6)
580 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
582 sav->tdb_xform = xsp;
587 ipe4_zeroize(struct secasvar *sav)
589 sav->tdb_xform = NULL;
594 ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
596 /* This is a rather serious mistake, so no conditional printing. */
597 printf("%s: should never be called\n", __func__);
603 static struct xformsw ipe4_xformsw = {
604 XF_IP4, 0, "IPv4 Simple Encapsulation",
605 ipe4_init, ipe4_zeroize, ipe4_input, ipip_output,
608 extern struct domain inetdomain;
609 #endif /* INET || INET6 */
611 static struct protosw ipe4_protosw = {
613 .pr_domain = &inetdomain,
614 .pr_protocol = IPPROTO_IPV4,
615 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
616 .pr_input = ip4_input,
617 .pr_ctloutput = rip_ctloutput,
618 .pr_usrreqs = &rip_usrreqs
621 #if defined(INET6) && defined(INET)
622 static struct ip6protosw ipe6_protosw = {
624 .pr_domain = &inetdomain,
625 .pr_protocol = IPPROTO_IPV6,
626 .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
627 .pr_input = ip4_input6,
628 .pr_ctloutput = rip_ctloutput,
629 .pr_usrreqs = &rip_usrreqs
631 #endif /* INET6 && INET */
635 * Check the encapsulated packet to see if we want it
638 ipe4_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
641 * Only take packets coming from IPSEC tunnels; the rest
642 * must be handled by the gif tunnel code. Note that we
643 * also return a minimum priority when we want the packet
644 * so any explicit gif tunnels take precedence.
646 return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
654 xform_register(&ipe4_xformsw);
655 /* attach to encapsulation framework */
656 /* XXX save return cookie for detach on module remove */
658 (void) encap_attach_func(AF_INET, -1,
659 ipe4_encapcheck, &ipe4_protosw, NULL);
661 #if defined(INET6) && defined(INET)
662 (void) encap_attach_func(AF_INET6, -1,
663 ipe4_encapcheck, (struct protosw *)&ipe6_protosw, NULL);
666 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);