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