kernel: Disable TCP_SIGNATURE in preparation for removing IPSEC.
[dragonfly.git] / sys / netproto / ipsec / xform_ipip.c
blob204b77a744f92eb508ec6ce2811e89449c3eb75e
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 $ */
3 /*
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
15 * and Niels Provos.
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
30 * all.
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
36 * PURPOSE.
40 * IP-inside-IP processing
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/kernel.h>
50 #include <sys/protosw.h>
51 #include <sys/sysctl.h>
53 #include <net/if.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>
71 #ifdef MROUTING
72 #include <netinet/ip_mroute.h>
73 #endif
75 #ifdef INET6
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>
81 #endif
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.
94 int ipip_allow = 0;
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, "");
103 /* XXX IPCOMP */
104 #define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
106 static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
108 #ifdef INET6
110 * Really only a wrapper for ipip_input(), for use with IPv6.
113 ip4_input6(struct mbuf **mp, int *offp, int proto)
115 #if 0
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++;
120 m_freem(*m);
121 return IPPROTO_DONE;
123 #endif
124 _ipip_input(*mp, *offp, NULL);
125 return IPPROTO_DONE;
127 #endif /* INET6 */
129 #ifdef INET
131 * Really only a wrapper for ipip_input(), for use with IPv4.
134 ip4_input(struct mbuf **mp, int *offp, int proto)
136 #if 0
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++;
141 m_freem(m);
142 return;
144 #endif
145 _ipip_input(*mp, *offp, NULL);
146 return IPPROTO_DONE;
148 #endif /* INET */
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.
157 static void
158 _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
160 struct sockaddr_in *sin;
161 struct ifaddr *ifa;
162 struct ip *ipo;
163 #ifdef INET6
164 struct sockaddr_in6 *sin6;
165 struct ip6_hdr *ip6 = NULL;
166 u_int8_t itos;
167 #endif
168 u_int8_t nxt;
169 int isr;
170 u_int8_t otos;
171 u_int8_t v;
172 int hlen;
174 ipipstat.ipips_ipackets++;
176 m_copydata(m, 0, 1, &v);
178 switch (v >> 4) {
179 #ifdef INET
180 case 4:
181 hlen = sizeof(struct ip);
182 break;
183 #endif /* INET */
184 #ifdef INET6
185 case 6:
186 hlen = sizeof(struct ip6_hdr);
187 break;
188 #endif
189 default:
190 DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
191 "for outer header\n", v, v>>4));
192 ipipstat.ipips_family++;
193 m_freem(m);
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++;
202 return;
206 ipo = mtod(m, struct ip *);
208 #ifdef MROUTING
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);
212 return;
215 #endif /* MROUTING */
217 /* Keep outer ecn field. */
218 switch (v >> 4) {
219 #ifdef INET
220 case 4:
221 otos = ipo->ip_tos;
222 break;
223 #endif /* INET */
224 #ifdef INET6
225 case 6:
226 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
227 break;
228 #endif
229 default:
230 panic("ipip_input: unknown ip version %u (outer)", v>>4);
233 /* Remove outer IP header */
234 m_adj(m, iphlen);
236 /* Sanity check */
237 if (m->m_pkthdr.len < sizeof(struct ip)) {
238 ipipstat.ipips_hdrops++;
239 m_freem(m);
240 return;
243 m_copydata(m, 0, 1, &v);
245 switch (v >> 4) {
246 #ifdef INET
247 case 4:
248 hlen = sizeof(struct ip);
249 break;
250 #endif /* INET */
252 #ifdef INET6
253 case 6:
254 hlen = sizeof(struct ip6_hdr);
255 break;
256 #endif
257 default:
258 DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
259 "for inner header\n", v, v>>4));
260 ipipstat.ipips_family++;
261 m_freem(m);
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++;
272 return;
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 */
283 switch (v >> 4) {
284 #ifdef INET
285 case 4:
286 ipo = mtod(m, struct ip *);
287 nxt = ipo->ip_p;
288 ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos);
289 break;
290 #endif /* INET */
291 #ifdef INET6
292 case 6:
293 ip6 = (struct ip6_hdr *) ipo;
294 nxt = ip6->ip6_nxt;
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);
299 break;
300 #endif
301 default:
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)) &&
308 ipip_allow != 2) {
309 struct ifnet_array *arr;
310 int i;
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) {
318 ifa = ifac->ifa;
319 #ifdef INET
320 if (ipo) {
321 if (ifa->ifa_addr->sa_family !=
322 AF_INET)
323 continue;
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++;
330 m_freem(m);
331 return;
334 #endif /* INET */
336 #ifdef INET6
337 if (ip6) {
338 if (ifa->ifa_addr->sa_family !=
339 AF_INET6)
340 continue;
342 sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
344 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
345 ipipstat.ipips_spoof++;
346 m_freem(m);
347 return;
351 #endif /* INET6 */
356 /* Statistics */
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
364 * untrusted packets.
367 switch (v >> 4) {
368 #ifdef INET
369 case 4:
370 isr = NETISR_IP;
371 break;
372 #endif
373 #ifdef INET6
374 case 6:
375 isr = NETISR_IPV6;
376 break;
377 #endif
378 default:
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"));
390 ipip_output(
391 struct mbuf *m,
392 struct ipsecrequest *isr,
393 struct mbuf **mp,
394 int skip,
395 int protoff
398 struct secasvar *sav;
399 u_int8_t tp, otos;
400 struct secasindex *saidx;
401 int error;
402 #ifdef INET
403 u_int8_t itos;
404 struct ip *ipo;
405 #endif /* INET */
406 #ifdef INET6
407 struct ip6_hdr *ip6, *ip6o;
408 #endif /* INET6 */
410 sav = isr->sav;
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) {
421 #ifdef INET
422 case AF_INET:
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++;
431 error = EINVAL;
432 goto bad;
435 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
436 if (m == NULL) {
437 DPRINTF(("ipip_output: M_PREPEND failed\n"));
438 ipipstat.ipips_hdrops++;
439 error = ENOBUFS;
440 goto bad;
443 ipo = mtod(m, struct ip *);
445 ipo->ip_v = IPVERSION;
446 ipo->ip_hl = 5;
447 ipo->ip_len = htons(m->m_pkthdr.len);
448 ipo->ip_ttl = ip_defttl;
449 ipo->ip_sum = 0;
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);
475 #ifdef INET6
476 else if (tp == (IPV6_VERSION >> 4)) {
477 u_int32_t itos32;
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;
485 ipo->ip_off = 0;
487 #endif /* INET6 */
488 else {
489 goto nofamily;
492 otos = 0;
493 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
494 ipo->ip_tos = otos;
495 break;
496 #endif /* INET */
498 #ifdef INET6
499 case AF_INET6:
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++;
508 error = ENOBUFS;
509 goto bad;
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);
520 if (m == NULL) {
521 DPRINTF(("ipip_output: M_PREPEND failed\n"));
522 ipipstat.ipips_hdrops++;
523 *mp = NULL;
524 error = ENOBUFS;
525 goto bad;
528 /* Initialize IPv6 header */
529 ip6o = mtod(m, struct ip6_hdr *);
530 ip6o->ip6_flow = 0;
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;
538 #ifdef INET
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),
543 (caddr_t) &itos);
545 /* This is really IPVERSION. */
546 ip6o->ip6_nxt = IPPROTO_IPIP;
547 } else
548 #endif /* INET */
549 if (tp == (IPV6_VERSION >> 4)) {
550 u_int32_t itos32;
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;
559 } else {
560 goto nofamily;
563 otos = 0;
564 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
565 ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
566 break;
567 #endif /* INET6 */
569 default:
570 nofamily:
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 */
575 goto bad;
578 ipipstat.ipips_opackets++;
579 *mp = m;
581 #ifdef INET
582 if (saidx->dst.sa.sa_family == AF_INET) {
583 #if 0
584 if (sav->tdb_xform->xf_type == XF_IP4)
585 tdb->tdb_cur_bytes +=
586 m->m_pkthdr.len - sizeof(struct ip);
587 #endif
588 ipipstat.ipips_obytes += m->m_pkthdr.len - sizeof(struct ip);
590 #endif /* INET */
592 #ifdef INET6
593 if (saidx->dst.sa.sa_family == AF_INET6) {
594 #if 0
595 if (sav->tdb_xform->xf_type == XF_IP4)
596 tdb->tdb_cur_bytes +=
597 m->m_pkthdr.len - sizeof(struct ip6_hdr);
598 #endif
599 ipipstat.ipips_obytes +=
600 m->m_pkthdr.len - sizeof(struct ip6_hdr);
602 #endif /* INET6 */
604 return 0;
605 bad:
606 if (m)
607 m_freem(m), *mp = NULL;
608 return (error);
611 #ifdef FAST_IPSEC
612 static int
613 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
615 sav->tdb_xform = xsp;
616 return 0;
619 static int
620 ipe4_zeroize(struct secasvar *sav)
622 sav->tdb_xform = NULL;
623 return 0;
626 static int
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");
631 if (m)
632 m_freem(m);
633 return EOPNOTSUPP;
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[] = {
645 .pr_type = SOCK_RAW,
646 .pr_domain = &inetdomain,
647 .pr_protocol = IPPROTO_IPV4,
648 .pr_flags = PR_ATOMIC|PR_ADDR,
650 .pr_input = ip4_input,
651 .pr_output = NULL,
652 .pr_ctlinput = NULL,
653 .pr_ctloutput = rip_ctloutput,
655 .pr_ctlport = cpu0_ctlport,
656 .pr_init = raw_init,
657 .pr_usrreqs = &rip_usrreqs
659 #ifdef INET6
661 .pr_type = SOCK_RAW,
662 .pr_domain = &inetdomain,
663 .pr_protocol = IPPROTO_IPV6,
664 .pr_flags = PR_ATOMIC|PR_ADDR,
666 .pr_input = ip4_input6,
667 .pr_output = NULL,
668 .pr_ctlinput = NULL,
669 .pr_ctloutput = rip_ctloutput,
671 .pr_ctlport = cpu0_ctlport,
672 .pr_init = raw_init,
673 .pr_usrreqs = &rip_usrreqs
675 #endif
679 * Check the encapsulated packet to see if we want it
681 static int
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);
693 static void
694 ipe4_attach(void)
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);
701 #ifdef INET6
702 encap_attach_func(AF_INET6, -1,
703 ipe4_encapcheck, &ipe4_protosw[1], NULL);
704 #endif
706 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);
707 #endif /* FAST_IPSEC */