1 /* $FreeBSD: releng/6.3/sys/netipsec/xform_ipip.c 160632 2006-07-24 23:20:59Z thompsa $ */
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).
7 *
8 * The original version of this code was written by John Ioannidis
9 * for BSD/OS in Athens, Greece, in November 1995.
10 *
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
13 *
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15 * and Niels Provos.
16 *
17 * Additional features in 1999 by Angelos D. Keromytis.
18 *
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.
22 *
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.
31 *
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.
37 */
38
39 /*
40 * IP-inside-IP processing
41 */
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44 #include "opt_enc.h"
45
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>
53
54 #include <net/if.h>
55 #include <net/route.h>
56 #include <net/netisr.h>
57
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>
65
66 #include <netipsec/ipsec.h>
67 #include <netipsec/xform.h>
68
69 #include <netipsec/ipip_var.h>
70
71 #ifdef MROUTING
72 #include <netinet/ip_mroute.h>
73 #endif
74
75 #ifdef INET6
76 #include <netinet/ip6.h>
77 #include <netipsec/ipsec6.h>
78 #include <netinet6/ip6_ecn.h>
79 #include <netinet6/in6_var.h>
80 #include <netinet6/ip6protosw.h>
81 #endif
82
83 #include <netipsec/key.h>
84 #include <netipsec/key_debug.h>
85
86 #include <machine/stdarg.h>
87
88 /*
89 * We can control the acceptance of IP4 packets by altering the sysctl
90 * net.inet.ipip.allow value. Zero means drop them, all else is acceptance.
91 */
92 int ipip_allow = 0;
93 struct ipipstat ipipstat;
94
95 SYSCTL_DECL(_net_inet_ipip);
96 SYSCTL_INT(_net_inet_ipip, OID_AUTO,
97 ipip_allow, CTLFLAG_RW, &ipip_allow, 0, "");
98 SYSCTL_STRUCT(_net_inet_ipip, IPSECCTL_STATS,
99 stats, CTLFLAG_RD, &ipipstat, ipipstat, "");
100
101 /* XXX IPCOMP */
102 #define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
103
104 static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
105
106 #ifdef INET6
107 /*
108 * Really only a wrapper for ipip_input(), for use with IPv6.
109 */
110 int
111 ip4_input6(struct mbuf **m, int *offp, int proto)
112 {
113 #if 0
114 /* If we do not accept IP-in-IP explicitly, drop. */
115 if (!ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) {
116 DPRINTF(("%s: dropped due to policy\n", __func__));
117 ipipstat.ipips_pdrops++;
118 m_freem(*m);
119 return IPPROTO_DONE;
120 }
121 #endif
122 _ipip_input(*m, *offp, NULL);
123 return IPPROTO_DONE;
124 }
125 #endif /* INET6 */
126
127 #ifdef INET
128 /*
129 * Really only a wrapper for ipip_input(), for use with IPv4.
130 */
131 void
132 ip4_input(struct mbuf *m, int off)
133 {
134 #if 0
135 /* If we do not accept IP-in-IP explicitly, drop. */
136 if (!ipip_allow && (m->m_flags & M_IPSEC) == 0) {
137 DPRINTF(("%s: dropped due to policy\n", __func__));
138 ipipstat.ipips_pdrops++;
139 m_freem(m);
140 return;
141 }
142 #endif
143 _ipip_input(m, off, NULL);
144 }
145 #endif /* INET */
146
147 /*
148 * ipip_input gets called when we receive an IP{46} encapsulated packet,
149 * either because we got it at a real interface, or because AH or ESP
150 * were being used in tunnel mode (in which case the rcvif element will
151 * contain the address of the encX interface associated with the tunnel.
152 */
153
154 static void
155 _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
156 {
157 register struct sockaddr_in *sin;
158 register struct ifnet *ifp;
159 register struct ifaddr *ifa;
160 struct ip *ipo;
161 #ifdef INET6
162 register struct sockaddr_in6 *sin6;
163 struct ip6_hdr *ip6 = NULL;
164 u_int8_t itos;
165 #endif
166 u_int8_t nxt;
167 int isr;
168 u_int8_t otos;
169 u_int8_t v;
170 int hlen;
171
172 ipipstat.ipips_ipackets++;
173
174 m_copydata(m, 0, 1, &v);
175
176 switch (v >> 4) {
177 #ifdef INET
178 case 4:
179 hlen = sizeof(struct ip);
180 break;
181 #endif /* INET */
182 #ifdef INET6
183 case 6:
184 hlen = sizeof(struct ip6_hdr);
185 break;
186 #endif
187 default:
188 ipipstat.ipips_family++;
189 m_freem(m);
190 return /* EAFNOSUPPORT */;
191 }
192
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.ipips_hdrops++;
198 return;
199 }
200 }
201
202 ipo = mtod(m, struct ip *);
203
204 #ifdef MROUTING
205 if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
206 if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
207 ipip_mroute_input (m, iphlen);
208 return;
209 }
210 }
211 #endif /* MROUTING */
212
213 /* Keep outer ecn field. */
214 switch (v >> 4) {
215 #ifdef INET
216 case 4:
217 otos = ipo->ip_tos;
218 break;
219 #endif /* INET */
220 #ifdef INET6
221 case 6:
222 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
223 break;
224 #endif
225 default:
226 panic("ipip_input: unknown ip version %u (outer)", v>>4);
227 }
228
229 /* Remove outer IP header */
230 m_adj(m, iphlen);
231
232 /* Sanity check */
233 if (m->m_pkthdr.len < sizeof(struct ip)) {
234 ipipstat.ipips_hdrops++;
235 m_freem(m);
236 return;
237 }
238
239 m_copydata(m, 0, 1, &v);
240
241 switch (v >> 4) {
242 #ifdef INET
243 case 4:
244 hlen = sizeof(struct ip);
245 break;
246 #endif /* INET */
247
248 #ifdef INET6
249 case 6:
250 hlen = sizeof(struct ip6_hdr);
251 break;
252 #endif
253 default:
254 ipipstat.ipips_family++;
255 m_freem(m);
256 return; /* EAFNOSUPPORT */
257 }
258
259 /*
260 * Bring the inner IP header in the first mbuf, if not there already.
261 */
262 if (m->m_len < hlen) {
263 if ((m = m_pullup(m, hlen)) == NULL) {
264 DPRINTF(("%s: m_pullup (2) failed\n", __func__));
265 ipipstat.ipips_hdrops++;
266 return;
267 }
268 }
269
270 /*
271 * RFC 1853 specifies that the inner TTL should not be touched on
272 * decapsulation. There's no reason this comment should be here, but
273 * this is as good as any a position.
274 */
275
276 /* Some sanity checks in the inner IP header */
277 switch (v >> 4) {
278 #ifdef INET
279 case 4:
280 ipo = mtod(m, struct ip *);
281 nxt = ipo->ip_p;
282 ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos);
283 break;
284 #endif /* INET */
285 #ifdef INET6
286 case 6:
287 ip6 = (struct ip6_hdr *) ipo;
288 nxt = ip6->ip6_nxt;
289 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
290 ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
291 ip6->ip6_flow &= ~htonl(0xff << 20);
292 ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
293 break;
294 #endif
295 default:
296 panic("ipip_input: unknown ip version %u (inner)", v>>4);
297 }
298
299 /* Check for local address spoofing. */
300 if ((m->m_pkthdr.rcvif == NULL ||
301 !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) &&
302 ipip_allow != 2) {
303 IFNET_RLOCK();
304 TAILQ_FOREACH(ifp, &ifnet, if_link) {
305 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
306 #ifdef INET
307 if (ipo) {
308 if (ifa->ifa_addr->sa_family !=
309 AF_INET)
310 continue;
311
312 sin = (struct sockaddr_in *) ifa->ifa_addr;
313
314 if (sin->sin_addr.s_addr ==
315 ipo->ip_src.s_addr) {
316 ipipstat.ipips_spoof++;
317 m_freem(m);
318 IFNET_RUNLOCK();
319 return;
320 }
321 }
322 #endif /* INET */
323
324 #ifdef INET6
325 if (ip6) {
326 if (ifa->ifa_addr->sa_family !=
327 AF_INET6)
328 continue;
329
330 sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
331
332 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
333 ipipstat.ipips_spoof++;
334 m_freem(m);
335 IFNET_RUNLOCK();
336 return;
337 }
338
339 }
340 #endif /* INET6 */
341 }
342 }
343 IFNET_RUNLOCK();
344 }
345
346 /* Statistics */
347 ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen;
348
349 #ifdef DEV_ENC
350 /* pass the mbuf to enc0 for packet filtering */
351 if (ipsec_filter(&m, 1) != 0)
352 return;
353 #endif
354
355 /*
356 * Interface pointer stays the same; if no IPsec processing has
357 * been done (or will be done), this will point to a normal
358 * interface. Otherwise, it'll point to an enc interface, which
359 * will allow a packet filter to distinguish between secure and
360 * untrusted packets.
361 */
362
363 switch (v >> 4) {
364 #ifdef INET
365 case 4:
366 isr = NETISR_IP;
367 break;
368 #endif
369 #ifdef INET6
370 case 6:
371 isr = NETISR_IPV6;
372 break;
373 #endif
374 default:
375 panic("%s: bogus ip version %u", __func__, v>>4);
376 }
377
378 if (netisr_queue(isr, m)) { /* (0) on success. */
379 ipipstat.ipips_qfull++;
380 DPRINTF(("%s: packet dropped because of full queue\n",
381 __func__));
382 }
383 }
384
385 int
386 ipip_output(
387 struct mbuf *m,
388 struct ipsecrequest *isr,
389 struct mbuf **mp,
390 int skip,
391 int protoff
392 )
393 {
394 struct secasvar *sav;
395 u_int8_t tp, otos;
396 struct secasindex *saidx;
397 int error;
398 #ifdef INET
399 u_int8_t itos;
400 struct ip *ipo;
401 #endif /* INET */
402 #ifdef INET6
403 struct ip6_hdr *ip6, *ip6o;
404 #endif /* INET6 */
405
406 IPSEC_SPLASSERT_SOFTNET(__func__);
407
408 sav = isr->sav;
409 IPSEC_ASSERT(sav != NULL, ("null SA"));
410 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
411
412 /* XXX Deal with empty TDB source/destination addresses. */
413
414 m_copydata(m, 0, 1, &tp);
415 tp = (tp >> 4) & 0xff; /* Get the IP version number. */
416
417 saidx = &sav->sah->saidx;
418 switch (saidx->dst.sa.sa_family) {
419 #ifdef INET
420 case AF_INET:
421 if (saidx->src.sa.sa_family != AF_INET ||
422 saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
423 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
424 DPRINTF(("%s: unspecified tunnel endpoint "
425 "address in SA %s/%08lx\n", __func__,
426 ipsec_address(&saidx->dst),
427 (u_long) ntohl(sav->spi)));
428 ipipstat.ipips_unspec++;
429 error = EINVAL;
430 goto bad;
431 }
432
433 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
434 if (m == 0) {
435 DPRINTF(("%s: M_PREPEND failed\n", __func__));
436 ipipstat.ipips_hdrops++;
437 error = ENOBUFS;
438 goto bad;
439 }
440
441 ipo = mtod(m, struct ip *);
442
443 ipo->ip_v = IPVERSION;
444 ipo->ip_hl = 5;
445 ipo->ip_len = htons(m->m_pkthdr.len);
446 ipo->ip_ttl = ip_defttl;
447 ipo->ip_sum = 0;
448 ipo->ip_src = saidx->src.sin.sin_addr;
449 ipo->ip_dst = saidx->dst.sin.sin_addr;
450
451 ipo->ip_id = ip_newid();
452
453 /* If the inner protocol is IP... */
454 if (tp == IPVERSION) {
455 /* Save ECN notification */
456 m_copydata(m, sizeof(struct ip) +
457 offsetof(struct ip, ip_tos),
458 sizeof(u_int8_t), (caddr_t) &itos);
459
460 ipo->ip_p = IPPROTO_IPIP;
461
462 /*
463 * We should be keeping tunnel soft-state and
464 * send back ICMPs if needed.
465 */
466 m_copydata(m, sizeof(struct ip) +
467 offsetof(struct ip, ip_off),
468 sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
469 ipo->ip_off = ntohs(ipo->ip_off);
470 ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
471 ipo->ip_off = htons(ipo->ip_off);
472 }
473 #ifdef INET6
474 else if (tp == (IPV6_VERSION >> 4)) {
475 u_int32_t itos32;
476
477 /* Save ECN notification. */
478 m_copydata(m, sizeof(struct ip) +
479 offsetof(struct ip6_hdr, ip6_flow),
480 sizeof(u_int32_t), (caddr_t) &itos32);
481 itos = ntohl(itos32) >> 20;
482 ipo->ip_p = IPPROTO_IPV6;
483 ipo->ip_off = 0;
484 }
485 #endif /* INET6 */
486 else {
487 goto nofamily;
488 }
489
490 otos = 0;
491 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
492 ipo->ip_tos = otos;
493 break;
494 #endif /* INET */
495
496 #ifdef INET6
497 case AF_INET6:
498 if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
499 saidx->src.sa.sa_family != AF_INET6 ||
500 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
501 DPRINTF(("%s: unspecified tunnel endpoint "
502 "address in SA %s/%08lx\n", __func__,
503 ipsec_address(&saidx->dst),
504 (u_long) ntohl(sav->spi)));
505 ipipstat.ipips_unspec++;
506 error = ENOBUFS;
507 goto bad;
508 }
509
510 /* scoped address handling */
511 ip6 = mtod(m, struct ip6_hdr *);
512 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
513 ip6->ip6_src.s6_addr16[1] = 0;
514 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
515 ip6->ip6_dst.s6_addr16[1] = 0;
516
517 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
518 if (m == 0) {
519 DPRINTF(("%s: M_PREPEND failed\n", __func__));
520 ipipstat.ipips_hdrops++;
521 error = ENOBUFS;
522 goto bad;
523 }
524
525 /* Initialize IPv6 header */
526 ip6o = mtod(m, struct ip6_hdr *);
527 ip6o->ip6_flow = 0;
528 ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
529 ip6o->ip6_vfc |= IPV6_VERSION;
530 ip6o->ip6_plen = htons(m->m_pkthdr.len);
531 ip6o->ip6_hlim = ip_defttl;
532 ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
533 ip6o->ip6_src = saidx->src.sin6.sin6_addr;
534
535 #ifdef INET
536 if (tp == IPVERSION) {
537 /* Save ECN notification */
538 m_copydata(m, sizeof(struct ip6_hdr) +
539 offsetof(struct ip, ip_tos), sizeof(u_int8_t),
540 (caddr_t) &itos);
541
542 /* This is really IPVERSION. */
543 ip6o->ip6_nxt = IPPROTO_IPIP;
544 } else
545 #endif /* INET */
546 if (tp == (IPV6_VERSION >> 4)) {
547 u_int32_t itos32;
548
549 /* Save ECN notification. */
550 m_copydata(m, sizeof(struct ip6_hdr) +
551 offsetof(struct ip6_hdr, ip6_flow),
552 sizeof(u_int32_t), (caddr_t) &itos32);
553 itos = ntohl(itos32) >> 20;
554
555 ip6o->ip6_nxt = IPPROTO_IPV6;
556 } else {
557 goto nofamily;
558 }
559
560 otos = 0;
561 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
562 ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
563 break;
564 #endif /* INET6 */
565
566 default:
567 nofamily:
568 DPRINTF(("%s: unsupported protocol family %u\n", __func__,
569 saidx->dst.sa.sa_family));
570 ipipstat.ipips_family++;
571 error = EAFNOSUPPORT; /* XXX diffs from openbsd */
572 goto bad;
573 }
574
575 ipipstat.ipips_opackets++;
576 *mp = m;
577
578 #ifdef INET
579 if (saidx->dst.sa.sa_family == AF_INET) {
580 #if 0
581 if (sav->tdb_xform->xf_type == XF_IP4)
582 tdb->tdb_cur_bytes +=
583 m->m_pkthdr.len - sizeof(struct ip);
584 #endif
585 ipipstat.ipips_obytes += m->m_pkthdr.len - sizeof(struct ip);
586 }
587 #endif /* INET */
588
589 #ifdef INET6
590 if (saidx->dst.sa.sa_family == AF_INET6) {
591 #if 0
592 if (sav->tdb_xform->xf_type == XF_IP4)
593 tdb->tdb_cur_bytes +=
594 m->m_pkthdr.len - sizeof(struct ip6_hdr);
595 #endif
596 ipipstat.ipips_obytes +=
597 m->m_pkthdr.len - sizeof(struct ip6_hdr);
598 }
599 #endif /* INET6 */
600
601 return 0;
602 bad:
603 if (m)
604 m_freem(m);
605 *mp = NULL;
606 return (error);
607 }
608
609 #ifdef FAST_IPSEC
610 static int
611 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
612 {
613 sav->tdb_xform = xsp;
614 return 0;
615 }
616
617 static int
618 ipe4_zeroize(struct secasvar *sav)
619 {
620 sav->tdb_xform = NULL;
621 return 0;
622 }
623
624 static int
625 ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
626 {
627 /* This is a rather serious mistake, so no conditional printing. */
628 printf("%s: should never be called\n", __func__);
629 if (m)
630 m_freem(m);
631 return EOPNOTSUPP;
632 }
633
634 static struct xformsw ipe4_xformsw = {
635 XF_IP4, 0, "IPv4 Simple Encapsulation",
636 ipe4_init, ipe4_zeroize, ipe4_input, ipip_output,
637 };
638
639 extern struct domain inetdomain;
640 static struct protosw ipe4_protosw =
641 { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
642 ip4_input,
643 0, 0, rip_ctloutput,
644 0,
645 0, 0, 0, 0,
646 &rip_usrreqs
647 };
648 #ifdef INET6
649 static struct ip6protosw ipe6_protosw =
650 { SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
651 ip4_input6,
652 0, 0, rip_ctloutput,
653 0,
654 0, 0, 0, 0,
655 &rip_usrreqs
656 };
657 #endif
658
659 /*
660 * Check the encapsulated packet to see if we want it
661 */
662 static int
663 ipe4_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
664 {
665 /*
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.
670 */
671 return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
672 }
673
674 static void
675 ipe4_attach(void)
676 {
677 xform_register(&ipe4_xformsw);
678 /* attach to encapsulation framework */
679 /* XXX save return cookie for detach on module remove */
680 (void) encap_attach_func(AF_INET, -1,
681 ipe4_encapcheck, &ipe4_protosw, NULL);
682 #ifdef INET6
683 (void) encap_attach_func(AF_INET6, -1,
684 ipe4_encapcheck, (struct protosw *)&ipe6_protosw, NULL);
685 #endif
686 }
687 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);
688 #endif /* FAST_IPSEC */
Cache object: 30602873e02d5017662f95e1be777f25
|