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
;
164 struct sockaddr_in6
*sin6
;
165 struct ip6_hdr
*ip6
= NULL
;
174 ipipstat
.ipips_ipackets
++;
176 m_copydata(m
, 0, 1, &v
);
181 hlen
= sizeof(struct ip
);
186 hlen
= sizeof(struct ip6_hdr
);
190 DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
191 "for outer header\n", v
, v
>>4));
192 ipipstat
.ipips_family
++;
194 return /* EAFNOSUPPORT */;
197 /* Bring the IP header in the first mbuf, if not there already */
198 if (m
->m_len
< hlen
) {
199 if ((m
= m_pullup(m
, hlen
)) == NULL
) {
200 DPRINTF(("ipip_input: m_pullup (1) failed\n"));
201 ipipstat
.ipips_hdrops
++;
206 ipo
= mtod(m
, struct ip
*);
209 if (ipo
->ip_v
== IPVERSION
&& ipo
->ip_p
== IPPROTO_IPV4
) {
210 if (IN_MULTICAST(((struct ip
*)((char *) ipo
+ iphlen
))->ip_dst
.s_addr
)) {
211 ipip_mroute_input (m
, iphlen
);
215 #endif /* MROUTING */
217 /* Keep outer ecn field. */
226 otos
= (ntohl(mtod(m
, struct ip6_hdr
*)->ip6_flow
) >> 20) & 0xff;
230 panic("ipip_input: unknown ip version %u (outer)", v
>>4);
233 /* Remove outer IP header */
237 if (m
->m_pkthdr
.len
< sizeof(struct ip
)) {
238 ipipstat
.ipips_hdrops
++;
243 m_copydata(m
, 0, 1, &v
);
248 hlen
= sizeof(struct ip
);
254 hlen
= sizeof(struct ip6_hdr
);
258 DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
259 "for inner header\n", v
, v
>>4));
260 ipipstat
.ipips_family
++;
262 return; /* EAFNOSUPPORT */
266 * Bring the inner IP header in the first mbuf, if not there already.
268 if (m
->m_len
< hlen
) {
269 if ((m
= m_pullup(m
, hlen
)) == NULL
) {
270 DPRINTF(("ipip_input: m_pullup (2) failed\n"));
271 ipipstat
.ipips_hdrops
++;
277 * RFC 1853 specifies that the inner TTL should not be touched on
278 * decapsulation. There's no reason this comment should be here, but
279 * this is as good as any a position.
282 /* Some sanity checks in the inner IP header */
286 ipo
= mtod(m
, struct ip
*);
288 ip_ecn_egress(ip4_ipsec_ecn
, &otos
, &ipo
->ip_tos
);
293 ip6
= (struct ip6_hdr
*) ipo
;
295 itos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
296 ip_ecn_egress(ip6_ipsec_ecn
, &otos
, &itos
);
297 ip6
->ip6_flow
&= ~htonl(0xff << 20);
298 ip6
->ip6_flow
|= htonl((u_int32_t
) itos
<< 20);
302 panic("ipip_input: unknown ip version %u (inner)", v
>>4);
305 /* Check for local address spoofing. */
306 if ((m
->m_pkthdr
.rcvif
== NULL
||
307 !(m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
)) &&
309 struct ifnet_array
*arr
;
312 arr
= ifnet_array_get();
313 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
314 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
315 struct ifaddr_container
*ifac
;
317 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
321 if (ifa
->ifa_addr
->sa_family
!=
325 sin
= (struct sockaddr_in
*) ifa
->ifa_addr
;
327 if (sin
->sin_addr
.s_addr
==
328 ipo
->ip_src
.s_addr
) {
329 ipipstat
.ipips_spoof
++;
338 if (ifa
->ifa_addr
->sa_family
!=
342 sin6
= (struct sockaddr_in6
*) ifa
->ifa_addr
;
344 if (IN6_ARE_ADDR_EQUAL(&sin6
->sin6_addr
, &ip6
->ip6_src
)) {
345 ipipstat
.ipips_spoof
++;
357 ipipstat
.ipips_ibytes
+= m
->m_pkthdr
.len
- iphlen
;
360 * Interface pointer stays the same; if no IPsec processing has
361 * been done (or will be done), this will point to a normal
362 * interface. Otherwise, it'll point to an enc interface, which
363 * will allow a packet filter to distinguish between secure and
379 panic("ipip_input: should never reach here");
382 if (netisr_queue(isr
, m
)) {
383 ipipstat
.ipips_qfull
++;
385 DPRINTF(("ipip_input: packet dropped because of full queue\n"));
392 struct ipsecrequest
*isr
,
398 struct secasvar
*sav
;
400 struct secasindex
*saidx
;
407 struct ip6_hdr
*ip6
, *ip6o
;
411 KASSERT(sav
!= NULL
, ("ipip_output: null SA"));
412 KASSERT(sav
->sah
!= NULL
, ("ipip_output: null SAH"));
414 /* XXX Deal with empty TDB source/destination addresses. */
416 m_copydata(m
, 0, 1, &tp
);
417 tp
= (tp
>> 4) & 0xff; /* Get the IP version number. */
419 saidx
= &sav
->sah
->saidx
;
420 switch (saidx
->dst
.sa
.sa_family
) {
423 if (saidx
->src
.sa
.sa_family
!= AF_INET
||
424 saidx
->src
.sin
.sin_addr
.s_addr
== INADDR_ANY
||
425 saidx
->dst
.sin
.sin_addr
.s_addr
== INADDR_ANY
) {
426 DPRINTF(("ipip_output: unspecified tunnel endpoint "
427 "address in SA %s/%08lx\n",
428 ipsec_address(&saidx
->dst
),
429 (u_long
) ntohl(sav
->spi
)));
430 ipipstat
.ipips_unspec
++;
435 M_PREPEND(m
, sizeof(struct ip
), M_NOWAIT
);
437 DPRINTF(("ipip_output: M_PREPEND failed\n"));
438 ipipstat
.ipips_hdrops
++;
443 ipo
= mtod(m
, struct ip
*);
445 ipo
->ip_v
= IPVERSION
;
447 ipo
->ip_len
= htons(m
->m_pkthdr
.len
);
448 ipo
->ip_ttl
= ip_defttl
;
450 ipo
->ip_src
= saidx
->src
.sin
.sin_addr
;
451 ipo
->ip_dst
= saidx
->dst
.sin
.sin_addr
;
453 ipo
->ip_id
= ip_newid();
455 /* If the inner protocol is IP... */
456 if (tp
== IPVERSION
) {
457 /* Save ECN notification */
458 m_copydata(m
, sizeof(struct ip
) +
459 offsetof(struct ip
, ip_tos
),
460 sizeof(u_int8_t
), (caddr_t
) &itos
);
462 ipo
->ip_p
= IPPROTO_IPIP
;
465 * We should be keeping tunnel soft-state and
466 * send back ICMPs if needed.
468 m_copydata(m
, sizeof(struct ip
) +
469 offsetof(struct ip
, ip_off
),
470 sizeof(u_int16_t
), (caddr_t
) &ipo
->ip_off
);
471 ipo
->ip_off
= ntohs(ipo
->ip_off
);
472 ipo
->ip_off
&= ~(IP_DF
| IP_MF
| IP_OFFMASK
);
473 ipo
->ip_off
= htons(ipo
->ip_off
);
476 else if (tp
== (IPV6_VERSION
>> 4)) {
479 /* Save ECN notification. */
480 m_copydata(m
, sizeof(struct ip
) +
481 offsetof(struct ip6_hdr
, ip6_flow
),
482 sizeof(u_int32_t
), (caddr_t
) &itos32
);
483 itos
= ntohl(itos32
) >> 20;
484 ipo
->ip_p
= IPPROTO_IPV6
;
493 ip_ecn_ingress(ECN_ALLOWED
, &otos
, &itos
);
500 if (IN6_IS_ADDR_UNSPECIFIED(&saidx
->dst
.sin6
.sin6_addr
) ||
501 saidx
->src
.sa
.sa_family
!= AF_INET6
||
502 IN6_IS_ADDR_UNSPECIFIED(&saidx
->src
.sin6
.sin6_addr
)) {
503 DPRINTF(("ipip_output: unspecified tunnel endpoint "
504 "address in SA %s/%08lx\n",
505 ipsec_address(&saidx
->dst
),
506 (u_long
) ntohl(sav
->spi
)));
507 ipipstat
.ipips_unspec
++;
512 /* scoped address handling */
513 ip6
= mtod(m
, struct ip6_hdr
*);
514 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
515 ip6
->ip6_src
.s6_addr16
[1] = 0;
516 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
517 ip6
->ip6_dst
.s6_addr16
[1] = 0;
519 M_PREPEND(m
, sizeof(struct ip6_hdr
), M_NOWAIT
);
521 DPRINTF(("ipip_output: M_PREPEND failed\n"));
522 ipipstat
.ipips_hdrops
++;
528 /* Initialize IPv6 header */
529 ip6o
= mtod(m
, struct ip6_hdr
*);
531 ip6o
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
532 ip6o
->ip6_vfc
|= IPV6_VERSION
;
533 ip6o
->ip6_plen
= htons(m
->m_pkthdr
.len
);
534 ip6o
->ip6_hlim
= ip_defttl
;
535 ip6o
->ip6_dst
= saidx
->dst
.sin6
.sin6_addr
;
536 ip6o
->ip6_src
= saidx
->src
.sin6
.sin6_addr
;
539 if (tp
== IPVERSION
) {
540 /* Save ECN notification */
541 m_copydata(m
, sizeof(struct ip6_hdr
) +
542 offsetof(struct ip
, ip_tos
), sizeof(u_int8_t
),
545 /* This is really IPVERSION. */
546 ip6o
->ip6_nxt
= IPPROTO_IPIP
;
549 if (tp
== (IPV6_VERSION
>> 4)) {
552 /* Save ECN notification. */
553 m_copydata(m
, sizeof(struct ip6_hdr
) +
554 offsetof(struct ip6_hdr
, ip6_flow
),
555 sizeof(u_int32_t
), (caddr_t
) &itos32
);
556 itos
= ntohl(itos32
) >> 20;
558 ip6o
->ip6_nxt
= IPPROTO_IPV6
;
564 ip_ecn_ingress(ECN_ALLOWED
, &otos
, &itos
);
565 ip6o
->ip6_flow
|= htonl((u_int32_t
) otos
<< 20);
571 DPRINTF(("ipip_output: unsupported protocol family %u\n",
572 saidx
->dst
.sa
.sa_family
));
573 ipipstat
.ipips_family
++;
574 error
= EAFNOSUPPORT
; /* XXX diffs from openbsd */
578 ipipstat
.ipips_opackets
++;
582 if (saidx
->dst
.sa
.sa_family
== AF_INET
) {
584 if (sav
->tdb_xform
->xf_type
== XF_IP4
)
585 tdb
->tdb_cur_bytes
+=
586 m
->m_pkthdr
.len
- sizeof(struct ip
);
588 ipipstat
.ipips_obytes
+= m
->m_pkthdr
.len
- sizeof(struct ip
);
593 if (saidx
->dst
.sa
.sa_family
== AF_INET6
) {
595 if (sav
->tdb_xform
->xf_type
== XF_IP4
)
596 tdb
->tdb_cur_bytes
+=
597 m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
599 ipipstat
.ipips_obytes
+=
600 m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
607 m_freem(m
), *mp
= NULL
;
613 ipe4_init(struct secasvar
*sav
, struct xformsw
*xsp
)
615 sav
->tdb_xform
= xsp
;
620 ipe4_zeroize(struct secasvar
*sav
)
622 sav
->tdb_xform
= NULL
;
627 ipe4_input(struct mbuf
*m
, struct secasvar
*sav
, int skip
, int protoff
)
629 /* This is a rather serious mistake, so no conditional printing. */
630 kprintf("ipe4_input: should never be called\n");
636 static struct xformsw ipe4_xformsw
= {
637 XF_IP4
, 0, "IPv4 Simple Encapsulation",
638 ipe4_init
, ipe4_zeroize
, ipe4_input
, ipip_output
,
641 extern struct domain inetdomain
;
643 static struct protosw ipe4_protosw
[] = {
646 .pr_domain
= &inetdomain
,
647 .pr_protocol
= IPPROTO_IPV4
,
648 .pr_flags
= PR_ATOMIC
|PR_ADDR
,
650 .pr_input
= ip4_input
,
653 .pr_ctloutput
= rip_ctloutput
,
655 .pr_ctlport
= cpu0_ctlport
,
657 .pr_usrreqs
= &rip_usrreqs
662 .pr_domain
= &inetdomain
,
663 .pr_protocol
= IPPROTO_IPV6
,
664 .pr_flags
= PR_ATOMIC
|PR_ADDR
,
666 .pr_input
= ip4_input6
,
669 .pr_ctloutput
= rip_ctloutput
,
671 .pr_ctlport
= cpu0_ctlport
,
673 .pr_usrreqs
= &rip_usrreqs
679 * Check the encapsulated packet to see if we want it
682 ipe4_encapcheck(const struct mbuf
*m
, int off
, int proto
, void *arg
)
685 * Only take packets coming from IPSEC tunnels; the rest
686 * must be handled by the gif tunnel code. Note that we
687 * also return a minimum priority when we want the packet
688 * so any explicit gif tunnels take precedence.
690 return ((m
->m_flags
& M_IPSEC
) != 0 ? 1 : 0);
696 xform_register(&ipe4_xformsw
);
697 /* attach to encapsulation framework */
698 /* XXX save return cookie for detach on module remove */
699 encap_attach_func(AF_INET
, -1,
700 ipe4_encapcheck
, &ipe4_protosw
[0], NULL
);
702 encap_attach_func(AF_INET6
, -1,
703 ipe4_encapcheck
, &ipe4_protosw
[1], NULL
);
706 SYSINIT(ipe4_xform_init
, SI_SUB_PROTO_DOMAIN
, SI_ORDER_MIDDLE
, ipe4_attach
, NULL
);
707 #endif /* FAST_IPSEC */