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