dma: rework config parsing
[dragonfly.git] / sys / netproto / ipsec / xform_ipip.c
blob9cc13c0b5cdde729f395d5d86977d1b98404b013
1 /* $FreeBSD: src/sys/netipsec/xform_ipip.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $ */
2 /* $DragonFly: src/sys/netproto/ipsec/xform_ipip.c,v 1.15 2008/11/01 04:22:15 sephe Exp $ */
3 /* $OpenBSD: ip_ipip.c,v 1.25 2002/06/10 18:04:55 itojun Exp $ */
4 /*
5 * The authors of this code are John Ioannidis (ji@tla.org),
6 * Angelos D. Keromytis (kermit@csd.uch.gr) and
7 * Niels Provos (provos@physnet.uni-hamburg.de).
9 * The original version of this code was written by John Ioannidis
10 * for BSD/OS in Athens, Greece, in November 1995.
12 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
13 * by Angelos D. Keromytis.
15 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
16 * and Niels Provos.
18 * Additional features in 1999 by Angelos D. Keromytis.
20 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
21 * Angelos D. Keromytis and Niels Provos.
22 * Copyright (c) 2001, Angelos D. Keromytis.
24 * Permission to use, copy, and modify this software with or without fee
25 * is hereby granted, provided that this entire notice is included in
26 * all copies of any software which is or includes a copy or
27 * modification of this software.
28 * You may use this code under the GNU public license if you so wish. Please
29 * contribute changes back to the authors under this freer than GPL license
30 * so that we may further the use of strong encryption without limitations to
31 * all.
33 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
34 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
35 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
36 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
37 * PURPOSE.
41 * IP-inside-IP processing
43 #include "opt_inet.h"
44 #include "opt_inet6.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/kernel.h>
51 #include <sys/protosw.h>
52 #include <sys/sysctl.h>
54 #include <net/if.h>
55 #include <net/route.h>
56 #include <net/netisr.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 **m, 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(*m, *offp, NULL);
125 return IPPROTO_DONE;
127 #endif /* INET6 */
129 #ifdef INET
131 * Really only a wrapper for ipip_input(), for use with IPv4.
133 void
134 ip4_input(struct mbuf *m, ...)
136 __va_list ap;
137 int iphlen;
139 #if 0
140 /* If we do not accept IP-in-IP explicitly, drop. */
141 if (!ipip_allow && (m->m_flags & M_IPSEC) == 0) {
142 DPRINTF(("ip4_input: dropped due to policy\n"));
143 ipipstat.ipips_pdrops++;
144 m_freem(m);
145 return;
147 #endif
148 __va_start(ap, m);
149 iphlen = __va_arg(ap, int);
150 __va_end(ap);
152 _ipip_input(m, iphlen, NULL);
154 #endif /* INET */
157 * ipip_input gets called when we receive an IP{46} encapsulated packet,
158 * either because we got it at a real interface, or because AH or ESP
159 * were being used in tunnel mode (in which case the rcvif element will
160 * contain the address of the encX interface associated with the tunnel.
163 static void
164 _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
166 struct sockaddr_in *sin;
167 struct ifnet *ifp;
168 struct ifaddr *ifa;
169 struct ip *ipo;
170 #ifdef INET6
171 struct sockaddr_in6 *sin6;
172 struct ip6_hdr *ip6 = NULL;
173 u_int8_t itos;
174 #endif
175 u_int8_t nxt;
176 int isr;
177 u_int8_t otos;
178 u_int8_t v;
179 int hlen;
181 ipipstat.ipips_ipackets++;
183 m_copydata(m, 0, 1, &v);
185 switch (v >> 4) {
186 #ifdef INET
187 case 4:
188 hlen = sizeof(struct ip);
189 break;
190 #endif /* INET */
191 #ifdef INET6
192 case 6:
193 hlen = sizeof(struct ip6_hdr);
194 break;
195 #endif
196 default:
197 DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
198 "for outer header\n", v, v>>4));
199 ipipstat.ipips_family++;
200 m_freem(m);
201 return /* EAFNOSUPPORT */;
204 /* Bring the IP header in the first mbuf, if not there already */
205 if (m->m_len < hlen) {
206 if ((m = m_pullup(m, hlen)) == NULL) {
207 DPRINTF(("ipip_input: m_pullup (1) failed\n"));
208 ipipstat.ipips_hdrops++;
209 return;
213 ipo = mtod(m, struct ip *);
215 #ifdef MROUTING
216 if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
217 if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
218 ipip_mroute_input (m, iphlen);
219 return;
222 #endif /* MROUTING */
224 /* Keep outer ecn field. */
225 switch (v >> 4) {
226 #ifdef INET
227 case 4:
228 otos = ipo->ip_tos;
229 break;
230 #endif /* INET */
231 #ifdef INET6
232 case 6:
233 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
234 break;
235 #endif
236 default:
237 panic("ipip_input: unknown ip version %u (outer)", v>>4);
240 /* Remove outer IP header */
241 m_adj(m, iphlen);
243 /* Sanity check */
244 if (m->m_pkthdr.len < sizeof(struct ip)) {
245 ipipstat.ipips_hdrops++;
246 m_freem(m);
247 return;
250 m_copydata(m, 0, 1, &v);
252 switch (v >> 4) {
253 #ifdef INET
254 case 4:
255 hlen = sizeof(struct ip);
256 break;
257 #endif /* INET */
259 #ifdef INET6
260 case 6:
261 hlen = sizeof(struct ip6_hdr);
262 break;
263 #endif
264 default:
265 DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
266 "for inner header\n", v, v>>4));
267 ipipstat.ipips_family++;
268 m_freem(m);
269 return; /* EAFNOSUPPORT */
273 * Bring the inner IP header in the first mbuf, if not there already.
275 if (m->m_len < hlen) {
276 if ((m = m_pullup(m, hlen)) == NULL) {
277 DPRINTF(("ipip_input: m_pullup (2) failed\n"));
278 ipipstat.ipips_hdrops++;
279 return;
284 * RFC 1853 specifies that the inner TTL should not be touched on
285 * decapsulation. There's no reason this comment should be here, but
286 * this is as good as any a position.
289 /* Some sanity checks in the inner IP header */
290 switch (v >> 4) {
291 #ifdef INET
292 case 4:
293 ipo = mtod(m, struct ip *);
294 nxt = ipo->ip_p;
295 ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos);
296 break;
297 #endif /* INET */
298 #ifdef INET6
299 case 6:
300 ip6 = (struct ip6_hdr *) ipo;
301 nxt = ip6->ip6_nxt;
302 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
303 ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
304 ip6->ip6_flow &= ~htonl(0xff << 20);
305 ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
306 break;
307 #endif
308 default:
309 panic("ipip_input: unknown ip version %u (inner)", v>>4);
312 /* Check for local address spoofing. */
313 if ((m->m_pkthdr.rcvif == NULL ||
314 !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) &&
315 ipip_allow != 2) {
316 TAILQ_FOREACH(ifp, &ifnet, if_link) {
317 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_link) {
318 #ifdef INET
319 if (ipo) {
320 if (ifa->ifa_addr->sa_family !=
321 AF_INET)
322 continue;
324 sin = (struct sockaddr_in *) ifa->ifa_addr;
326 if (sin->sin_addr.s_addr ==
327 ipo->ip_src.s_addr) {
328 ipipstat.ipips_spoof++;
329 m_freem(m);
330 return;
333 #endif /* INET */
335 #ifdef INET6
336 if (ip6) {
337 if (ifa->ifa_addr->sa_family !=
338 AF_INET6)
339 continue;
341 sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
343 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
344 ipipstat.ipips_spoof++;
345 m_freem(m);
346 return;
350 #endif /* INET6 */
355 /* Statistics */
356 ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen;
359 * Interface pointer stays the same; if no IPsec processing has
360 * been done (or will be done), this will point to a normal
361 * interface. Otherwise, it'll point to an enc interface, which
362 * will allow a packet filter to distinguish between secure and
363 * untrusted packets.
366 switch (v >> 4) {
367 #ifdef INET
368 case 4:
369 isr = NETISR_IP;
370 break;
371 #endif
372 #ifdef INET6
373 case 6:
374 isr = NETISR_IPV6;
375 break;
376 #endif
377 default:
378 panic("ipip_input: should never reach here");
381 if (netisr_queue(isr, m)) {
382 ipipstat.ipips_qfull++;
384 DPRINTF(("ipip_input: packet dropped because of full queue\n"));
389 ipip_output(
390 struct mbuf *m,
391 struct ipsecrequest *isr,
392 struct mbuf **mp,
393 int skip,
394 int protoff
397 struct secasvar *sav;
398 u_int8_t tp, otos;
399 struct secasindex *saidx;
400 int error;
401 #ifdef INET
402 u_int8_t itos;
403 struct ip *ipo;
404 #endif /* INET */
405 #ifdef INET6
406 struct ip6_hdr *ip6, *ip6o;
407 #endif /* INET6 */
409 sav = isr->sav;
410 KASSERT(sav != NULL, ("ipip_output: null SA"));
411 KASSERT(sav->sah != NULL, ("ipip_output: null SAH"));
413 /* XXX Deal with empty TDB source/destination addresses. */
415 m_copydata(m, 0, 1, &tp);
416 tp = (tp >> 4) & 0xff; /* Get the IP version number. */
418 saidx = &sav->sah->saidx;
419 switch (saidx->dst.sa.sa_family) {
420 #ifdef INET
421 case AF_INET:
422 if (saidx->src.sa.sa_family != AF_INET ||
423 saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
424 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
425 DPRINTF(("ipip_output: unspecified tunnel endpoint "
426 "address in SA %s/%08lx\n",
427 ipsec_address(&saidx->dst),
428 (u_long) ntohl(sav->spi)));
429 ipipstat.ipips_unspec++;
430 error = EINVAL;
431 goto bad;
434 M_PREPEND(m, sizeof(struct ip), MB_DONTWAIT);
435 if (m == 0) {
436 DPRINTF(("ipip_output: M_PREPEND failed\n"));
437 ipipstat.ipips_hdrops++;
438 error = ENOBUFS;
439 goto bad;
442 ipo = mtod(m, struct ip *);
444 ipo->ip_v = IPVERSION;
445 ipo->ip_hl = 5;
446 ipo->ip_len = htons(m->m_pkthdr.len);
447 ipo->ip_ttl = ip_defttl;
448 ipo->ip_sum = 0;
449 ipo->ip_src = saidx->src.sin.sin_addr;
450 ipo->ip_dst = saidx->dst.sin.sin_addr;
452 ipo->ip_id = ip_newid();
454 /* If the inner protocol is IP... */
455 if (tp == IPVERSION) {
456 /* Save ECN notification */
457 m_copydata(m, sizeof(struct ip) +
458 offsetof(struct ip, ip_tos),
459 sizeof(u_int8_t), (caddr_t) &itos);
461 ipo->ip_p = IPPROTO_IPIP;
464 * We should be keeping tunnel soft-state and
465 * send back ICMPs if needed.
467 m_copydata(m, sizeof(struct ip) +
468 offsetof(struct ip, ip_off),
469 sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
470 ipo->ip_off = ntohs(ipo->ip_off);
471 ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
472 ipo->ip_off = htons(ipo->ip_off);
474 #ifdef INET6
475 else if (tp == (IPV6_VERSION >> 4)) {
476 u_int32_t itos32;
478 /* Save ECN notification. */
479 m_copydata(m, sizeof(struct ip) +
480 offsetof(struct ip6_hdr, ip6_flow),
481 sizeof(u_int32_t), (caddr_t) &itos32);
482 itos = ntohl(itos32) >> 20;
483 ipo->ip_p = IPPROTO_IPV6;
484 ipo->ip_off = 0;
486 #endif /* INET6 */
487 else {
488 goto nofamily;
491 otos = 0;
492 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
493 ipo->ip_tos = otos;
494 break;
495 #endif /* INET */
497 #ifdef INET6
498 case AF_INET6:
499 if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
500 saidx->src.sa.sa_family != AF_INET6 ||
501 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
502 DPRINTF(("ipip_output: unspecified tunnel endpoint "
503 "address in SA %s/%08lx\n",
504 ipsec_address(&saidx->dst),
505 (u_long) ntohl(sav->spi)));
506 ipipstat.ipips_unspec++;
507 error = ENOBUFS;
508 goto bad;
511 /* scoped address handling */
512 ip6 = mtod(m, struct ip6_hdr *);
513 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
514 ip6->ip6_src.s6_addr16[1] = 0;
515 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
516 ip6->ip6_dst.s6_addr16[1] = 0;
518 M_PREPEND(m, sizeof(struct ip6_hdr), MB_DONTWAIT);
519 if (m == 0) {
520 DPRINTF(("ipip_output: M_PREPEND failed\n"));
521 ipipstat.ipips_hdrops++;
522 *mp = NULL;
523 error = ENOBUFS;
524 goto bad;
527 /* Initialize IPv6 header */
528 ip6o = mtod(m, struct ip6_hdr *);
529 ip6o->ip6_flow = 0;
530 ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
531 ip6o->ip6_vfc |= IPV6_VERSION;
532 ip6o->ip6_plen = htons(m->m_pkthdr.len);
533 ip6o->ip6_hlim = ip_defttl;
534 ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
535 ip6o->ip6_src = saidx->src.sin6.sin6_addr;
537 #ifdef INET
538 if (tp == IPVERSION) {
539 /* Save ECN notification */
540 m_copydata(m, sizeof(struct ip6_hdr) +
541 offsetof(struct ip, ip_tos), sizeof(u_int8_t),
542 (caddr_t) &itos);
544 /* This is really IPVERSION. */
545 ip6o->ip6_nxt = IPPROTO_IPIP;
546 } else
547 #endif /* INET */
548 if (tp == (IPV6_VERSION >> 4)) {
549 u_int32_t itos32;
551 /* Save ECN notification. */
552 m_copydata(m, sizeof(struct ip6_hdr) +
553 offsetof(struct ip6_hdr, ip6_flow),
554 sizeof(u_int32_t), (caddr_t) &itos32);
555 itos = ntohl(itos32) >> 20;
557 ip6o->ip6_nxt = IPPROTO_IPV6;
558 } else {
559 goto nofamily;
562 otos = 0;
563 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
564 ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
565 break;
566 #endif /* INET6 */
568 default:
569 nofamily:
570 DPRINTF(("ipip_output: unsupported protocol family %u\n",
571 saidx->dst.sa.sa_family));
572 ipipstat.ipips_family++;
573 error = EAFNOSUPPORT; /* XXX diffs from openbsd */
574 goto bad;
577 ipipstat.ipips_opackets++;
578 *mp = m;
580 #ifdef INET
581 if (saidx->dst.sa.sa_family == AF_INET) {
582 #if 0
583 if (sav->tdb_xform->xf_type == XF_IP4)
584 tdb->tdb_cur_bytes +=
585 m->m_pkthdr.len - sizeof(struct ip);
586 #endif
587 ipipstat.ipips_obytes += m->m_pkthdr.len - sizeof(struct ip);
589 #endif /* INET */
591 #ifdef INET6
592 if (saidx->dst.sa.sa_family == AF_INET6) {
593 #if 0
594 if (sav->tdb_xform->xf_type == XF_IP4)
595 tdb->tdb_cur_bytes +=
596 m->m_pkthdr.len - sizeof(struct ip6_hdr);
597 #endif
598 ipipstat.ipips_obytes +=
599 m->m_pkthdr.len - sizeof(struct ip6_hdr);
601 #endif /* INET6 */
603 return 0;
604 bad:
605 if (m)
606 m_freem(m), *mp = NULL;
607 return (error);
610 #ifdef FAST_IPSEC
611 static int
612 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
614 sav->tdb_xform = xsp;
615 return 0;
618 static int
619 ipe4_zeroize(struct secasvar *sav)
621 sav->tdb_xform = NULL;
622 return 0;
625 static int
626 ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
628 /* This is a rather serious mistake, so no conditional printing. */
629 kprintf("ipe4_input: should never be called\n");
630 if (m)
631 m_freem(m);
632 return EOPNOTSUPP;
635 static struct xformsw ipe4_xformsw = {
636 XF_IP4, 0, "IPv4 Simple Encapsulation",
637 ipe4_init, ipe4_zeroize, ipe4_input, ipip_output,
640 extern struct domain inetdomain;
641 static struct protosw ipe4_protosw[] = {
642 { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
643 ip4_input, 0, 0, rip_ctloutput,
644 cpu0_soport, NULL,
645 0, 0, 0, 0,
646 &rip_usrreqs
648 #ifdef INET6
649 { SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
650 ip4_input,
651 0, 0, rip_ctloutput,
652 cpu0_soport,
653 0, 0, 0, 0,
654 &rip_usrreqs
656 #endif
660 * Check the encapsulated packet to see if we want it
662 static int
663 ipe4_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
666 * Only take packets coming from IPSEC tunnels; the rest
667 * must be handled by the gif tunnel code. Note that we
668 * also return a minimum priority when we want the packet
669 * so any explicit gif tunnels take precedence.
671 return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
674 static void
675 ipe4_attach(void)
677 xform_register(&ipe4_xformsw);
678 /* attach to encapsulation framework */
679 /* XXX save return cookie for detach on module remove */
680 encap_attach_func(AF_INET, -1,
681 ipe4_encapcheck, (struct protosw*) &ipe4_protosw[0], NULL);
682 #ifdef INET6
683 encap_attach_func(AF_INET6, -1,
684 ipe4_encapcheck, (struct protosw*) &ipe4_protosw[1], NULL);
685 #endif
687 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);
688 #endif /* FAST_IPSEC */