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