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