Merge from vendor branch FILE:
[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 /*      $DragonFly: src/sys/netproto/ipsec/xform_ipip.c,v 1.11 2005/06/10 23:59:31 dillon Exp $ */
3 /*      $OpenBSD: ip_ipip.c,v 1.25 2002/06/10 18:04:55 itojun Exp $ */
4 /*
5  * The authors of this code are John Ioannidis (ji@tla.org),
6  * Angelos D. Keromytis (kermit@csd.uch.gr) and
7  * Niels Provos (provos@physnet.uni-hamburg.de).
8  *
9  * The original version of this code was written by John Ioannidis
10  * for BSD/OS in Athens, Greece, in November 1995.
11  *
12  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
13  * by Angelos D. Keromytis.
14  *
15  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
16  * and Niels Provos.
17  *
18  * Additional features in 1999 by Angelos D. Keromytis.
19  *
20  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
21  * Angelos D. Keromytis and Niels Provos.
22  * Copyright (c) 2001, Angelos D. Keromytis.
23  *
24  * Permission to use, copy, and modify this software with or without fee
25  * is hereby granted, provided that this entire notice is included in
26  * all copies of any software which is or includes a copy or
27  * modification of this software.
28  * You may use this code under the GNU public license if you so wish. Please
29  * contribute changes back to the authors under this freer than GPL license
30  * so that we may further the use of strong encryption without limitations to
31  * all.
32  *
33  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
34  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
35  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
36  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
37  * PURPOSE.
38  */
39
40 /*
41  * IP-inside-IP processing
42  */
43 #include "opt_inet.h"
44 #include "opt_inet6.h"
45 #include "opt_random_ip_id.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
55 #include <net/if.h>
56 #include <net/route.h>
57 #include <net/netisr.h>
58
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_ecn.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/ip_encap.h>
66
67 #include <netproto/ipsec/ipsec.h>
68 #include <netproto/ipsec/xform.h>
69
70 #include <netproto/ipsec/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 <netproto/ipsec/ipsec6.h>
79 #include <netinet6/ip6_ecn.h>
80 #include <netinet6/in6_var.h>
81 #include <netinet6/ip6protosw.h>
82 #endif
83
84 #include <netproto/ipsec/key.h>
85 #include <netproto/ipsec/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         struct sockaddr_in *sin;
168         struct ifnet *ifp;
169         struct ifaddr *ifa;
170         struct ip *ipo;
171 #ifdef INET6
172         struct sockaddr_in6 *sin6;
173         struct ip6_hdr *ip6 = NULL;
174         u_int8_t itos;
175 #endif
176         u_int8_t nxt;
177         int isr;
178         u_int8_t otos;
179         u_int8_t v;
180         int hlen;
181
182         ipipstat.ipips_ipackets++;
183
184         m_copydata(m, 0, 1, &v);
185
186         switch (v >> 4) {
187 #ifdef INET
188         case 4:
189                 hlen = sizeof(struct ip);
190                 break;
191 #endif /* INET */
192 #ifdef INET6
193         case 6:
194                 hlen = sizeof(struct ip6_hdr);
195                 break;
196 #endif
197         default:
198                 DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
199                         "for outer header\n", v, v>>4));
200                 ipipstat.ipips_family++;
201                 m_freem(m);
202                 return /* EAFNOSUPPORT */;
203         }
204
205         /* Bring the IP header in the first mbuf, if not there already */
206         if (m->m_len < hlen) {
207                 if ((m = m_pullup(m, hlen)) == NULL) {
208                         DPRINTF(("ipip_input: m_pullup (1) failed\n"));
209                         ipipstat.ipips_hdrops++;
210                         return;
211                 }
212         }
213
214         ipo = mtod(m, struct ip *);
215
216 #ifdef MROUTING
217         if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
218                 if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
219                         ipip_mroute_input (m, iphlen);
220                         return;
221                 }
222         }
223 #endif /* MROUTING */
224
225         /* Keep outer ecn field. */
226         switch (v >> 4) {
227 #ifdef INET
228         case 4:
229                 otos = ipo->ip_tos;
230                 break;
231 #endif /* INET */
232 #ifdef INET6
233         case 6:
234                 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
235                 break;
236 #endif
237         default:
238                 panic("ipip_input: unknown ip version %u (outer)", v>>4);
239         }
240
241         /* Remove outer IP header */
242         m_adj(m, iphlen);
243
244         /* Sanity check */
245         if (m->m_pkthdr.len < sizeof(struct ip))  {
246                 ipipstat.ipips_hdrops++;
247                 m_freem(m);
248                 return;
249         }
250
251         m_copydata(m, 0, 1, &v);
252
253         switch (v >> 4) {
254 #ifdef INET
255         case 4:
256                 hlen = sizeof(struct ip);
257                 break;
258 #endif /* INET */
259
260 #ifdef INET6
261         case 6:
262                 hlen = sizeof(struct ip6_hdr);
263                 break;
264 #endif
265         default:
266                 DPRINTF(("_ipip_input: bad protocol version 0x%x (%u) "
267                         "for inner header\n", v, v>>4));
268                 ipipstat.ipips_family++;
269                 m_freem(m);
270                 return; /* EAFNOSUPPORT */
271         }
272
273         /*
274          * Bring the inner IP header in the first mbuf, if not there already.
275          */
276         if (m->m_len < hlen) {
277                 if ((m = m_pullup(m, hlen)) == NULL) {
278                         DPRINTF(("ipip_input: m_pullup (2) failed\n"));
279                         ipipstat.ipips_hdrops++;
280                         return;
281                 }
282         }
283
284         /*
285          * RFC 1853 specifies that the inner TTL should not be touched on
286          * decapsulation. There's no reason this comment should be here, but
287          * this is as good as any a position.
288          */
289
290         /* Some sanity checks in the inner IP header */
291         switch (v >> 4) {
292 #ifdef INET
293         case 4:
294                 ipo = mtod(m, struct ip *);
295                 nxt = ipo->ip_p;
296                 ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos);
297                 break;
298 #endif /* INET */
299 #ifdef INET6
300         case 6:
301                 ip6 = (struct ip6_hdr *) ipo;
302                 nxt = ip6->ip6_nxt;
303                 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
304                 ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
305                 ip6->ip6_flow &= ~htonl(0xff << 20);
306                 ip6->ip6_flow |= htonl((u_int32_t) itos << 20);
307                 break;
308 #endif
309         default:
310                 panic("ipip_input: unknown ip version %u (inner)", v>>4);
311         }
312
313         /* Check for local address spoofing. */
314         if ((m->m_pkthdr.rcvif == NULL ||
315             !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) &&
316             ipip_allow != 2) {
317                 TAILQ_FOREACH(ifp, &ifnet, if_link) {
318                         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_link) {
319 #ifdef INET
320                                 if (ipo) {
321                                         if (ifa->ifa_addr->sa_family !=
322                                             AF_INET)
323                                                 continue;
324
325                                         sin = (struct sockaddr_in *) ifa->ifa_addr;
326
327                                         if (sin->sin_addr.s_addr ==
328                                             ipo->ip_src.s_addr) {
329                                                 ipipstat.ipips_spoof++;
330                                                 m_freem(m);
331                                                 return;
332                                         }
333                                 }
334 #endif /* INET */
335
336 #ifdef INET6
337                                 if (ip6) {
338                                         if (ifa->ifa_addr->sa_family !=
339                                             AF_INET6)
340                                                 continue;
341
342                                         sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
343
344                                         if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
345                                                 ipipstat.ipips_spoof++;
346                                                 m_freem(m);
347                                                 return;
348                                         }
349
350                                 }
351 #endif /* INET6 */
352                         }
353                 }
354         }
355
356         /* Statistics */
357         ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen;
358
359         /*
360          * Interface pointer stays the same; if no IPsec processing has
361          * been done (or will be done), this will point to a normal
362          * interface. Otherwise, it'll point to an enc interface, which
363          * will allow a packet filter to distinguish between secure and
364          * untrusted packets.
365          */
366
367         switch (v >> 4) {
368 #ifdef INET
369         case 4:
370                 isr = NETISR_IP;
371                 break;
372 #endif
373 #ifdef INET6
374         case 6:
375                 isr = NETISR_IPV6;
376                 break;
377 #endif
378         default:
379                 panic("ipip_input: should never reach here");
380         }
381
382         if (netisr_queue(isr, m)) {
383                 ipipstat.ipips_qfull++;
384
385                 DPRINTF(("ipip_input: packet dropped because of full queue\n"));
386         }
387 }
388
389 int
390 ipip_output(
391         struct mbuf *m,
392         struct ipsecrequest *isr,
393         struct mbuf **mp,
394         int skip,
395         int protoff
396 )
397 {
398         struct secasvar *sav;
399         u_int8_t tp, otos;
400         struct secasindex *saidx;
401         int error;
402 #ifdef INET
403         u_int8_t itos;
404         struct ip *ipo;
405 #endif /* INET */
406 #ifdef INET6
407         struct ip6_hdr *ip6, *ip6o;
408 #endif /* INET6 */
409
410         sav = isr->sav;
411         KASSERT(sav != NULL, ("ipip_output: null SA"));
412         KASSERT(sav->sah != NULL, ("ipip_output: null SAH"));
413
414         /* XXX Deal with empty TDB source/destination addresses. */
415
416         m_copydata(m, 0, 1, &tp);
417         tp = (tp >> 4) & 0xff;  /* Get the IP version number. */
418
419         saidx = &sav->sah->saidx;
420         switch (saidx->dst.sa.sa_family) {
421 #ifdef INET
422         case AF_INET:
423                 if (saidx->src.sa.sa_family != AF_INET ||
424                     saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
425                     saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
426                         DPRINTF(("ipip_output: unspecified tunnel endpoint "
427                             "address in SA %s/%08lx\n",
428                             ipsec_address(&saidx->dst),
429                             (u_long) ntohl(sav->spi)));
430                         ipipstat.ipips_unspec++;
431                         error = EINVAL;
432                         goto bad;
433                 }
434
435                 M_PREPEND(m, sizeof(struct ip), MB_DONTWAIT);
436                 if (m == 0) {
437                         DPRINTF(("ipip_output: M_PREPEND failed\n"));
438                         ipipstat.ipips_hdrops++;
439                         error = ENOBUFS;
440                         goto bad;
441                 }
442
443                 ipo = mtod(m, struct ip *);
444
445                 ipo->ip_v = IPVERSION;
446                 ipo->ip_hl = 5;
447                 ipo->ip_len = htons(m->m_pkthdr.len);
448                 ipo->ip_ttl = ip_defttl;
449                 ipo->ip_sum = 0;
450                 ipo->ip_src = saidx->src.sin.sin_addr;
451                 ipo->ip_dst = saidx->dst.sin.sin_addr;
452
453 #ifdef RANDOM_IP_ID
454                 ipo->ip_id = ip_randomid();
455 #else
456                 ipo->ip_id = htons(ip_id++);
457 #endif
458
459                 /* If the inner protocol is IP... */
460                 if (tp == IPVERSION) {
461                         /* Save ECN notification */
462                         m_copydata(m, sizeof(struct ip) +
463                             offsetof(struct ip, ip_tos),
464                             sizeof(u_int8_t), (caddr_t) &itos);
465
466                         ipo->ip_p = IPPROTO_IPIP;
467
468                         /*
469                          * We should be keeping tunnel soft-state and
470                          * send back ICMPs if needed.
471                          */
472                         m_copydata(m, sizeof(struct ip) +
473                             offsetof(struct ip, ip_off),
474                             sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
475                         ipo->ip_off = ntohs(ipo->ip_off);
476                         ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
477                         ipo->ip_off = htons(ipo->ip_off);
478                 }
479 #ifdef INET6
480                 else if (tp == (IPV6_VERSION >> 4)) {
481                         u_int32_t itos32;
482
483                         /* Save ECN notification. */
484                         m_copydata(m, sizeof(struct ip) +
485                             offsetof(struct ip6_hdr, ip6_flow),
486                             sizeof(u_int32_t), (caddr_t) &itos32);
487                         itos = ntohl(itos32) >> 20;
488                         ipo->ip_p = IPPROTO_IPV6;
489                         ipo->ip_off = 0;
490                 }
491 #endif /* INET6 */
492                 else {
493                         goto nofamily;
494                 }
495
496                 otos = 0;
497                 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
498                 ipo->ip_tos = otos;
499                 break;
500 #endif /* INET */
501
502 #ifdef INET6
503         case AF_INET6:
504                 if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
505                     saidx->src.sa.sa_family != AF_INET6 ||
506                     IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
507                         DPRINTF(("ipip_output: unspecified tunnel endpoint "
508                             "address in SA %s/%08lx\n",
509                             ipsec_address(&saidx->dst),
510                             (u_long) ntohl(sav->spi)));
511                         ipipstat.ipips_unspec++;
512                         error = ENOBUFS;
513                         goto bad;
514                 }
515
516                 /* scoped address handling */
517                 ip6 = mtod(m, struct ip6_hdr *);
518                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
519                         ip6->ip6_src.s6_addr16[1] = 0;
520                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
521                         ip6->ip6_dst.s6_addr16[1] = 0;
522
523                 M_PREPEND(m, sizeof(struct ip6_hdr), MB_DONTWAIT);
524                 if (m == 0) {
525                         DPRINTF(("ipip_output: M_PREPEND failed\n"));
526                         ipipstat.ipips_hdrops++;
527                         *mp = NULL;
528                         error = ENOBUFS;
529                         goto bad;
530                 }
531
532                 /* Initialize IPv6 header */
533                 ip6o = mtod(m, struct ip6_hdr *);
534                 ip6o->ip6_flow = 0;
535                 ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
536                 ip6o->ip6_vfc |= IPV6_VERSION;
537                 ip6o->ip6_plen = htons(m->m_pkthdr.len);
538                 ip6o->ip6_hlim = ip_defttl;
539                 ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
540                 ip6o->ip6_src = saidx->src.sin6.sin6_addr;
541
542 #ifdef INET
543                 if (tp == IPVERSION) {
544                         /* Save ECN notification */
545                         m_copydata(m, sizeof(struct ip6_hdr) +
546                             offsetof(struct ip, ip_tos), sizeof(u_int8_t),
547                             (caddr_t) &itos);
548
549                         /* This is really IPVERSION. */
550                         ip6o->ip6_nxt = IPPROTO_IPIP;
551                 } else
552 #endif /* INET */
553                         if (tp == (IPV6_VERSION >> 4)) {
554                                 u_int32_t itos32;
555
556                                 /* Save ECN notification. */
557                                 m_copydata(m, sizeof(struct ip6_hdr) +
558                                     offsetof(struct ip6_hdr, ip6_flow),
559                                     sizeof(u_int32_t), (caddr_t) &itos32);
560                                 itos = ntohl(itos32) >> 20;
561
562                                 ip6o->ip6_nxt = IPPROTO_IPV6;
563                         } else {
564                                 goto nofamily;
565                         }
566
567                 otos = 0;
568                 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
569                 ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
570                 break;
571 #endif /* INET6 */
572
573         default:
574 nofamily:
575                 DPRINTF(("ipip_output: unsupported protocol family %u\n",
576                     saidx->dst.sa.sa_family));
577                 ipipstat.ipips_family++;
578                 error = EAFNOSUPPORT;           /* XXX diffs from openbsd */
579                 goto bad;
580         }
581
582         ipipstat.ipips_opackets++;
583         *mp = m;
584
585 #ifdef INET
586         if (saidx->dst.sa.sa_family == AF_INET) {
587 #if 0
588                 if (sav->tdb_xform->xf_type == XF_IP4)
589                         tdb->tdb_cur_bytes +=
590                             m->m_pkthdr.len - sizeof(struct ip);
591 #endif
592                 ipipstat.ipips_obytes += m->m_pkthdr.len - sizeof(struct ip);
593         }
594 #endif /* INET */
595
596 #ifdef INET6
597         if (saidx->dst.sa.sa_family == AF_INET6) {
598 #if 0
599                 if (sav->tdb_xform->xf_type == XF_IP4)
600                         tdb->tdb_cur_bytes +=
601                             m->m_pkthdr.len - sizeof(struct ip6_hdr);
602 #endif
603                 ipipstat.ipips_obytes +=
604                     m->m_pkthdr.len - sizeof(struct ip6_hdr);
605         }
606 #endif /* INET6 */
607
608         return 0;
609 bad:
610         if (m)
611                 m_freem(m), *mp = NULL;
612         return (error);
613 }
614
615 #ifdef FAST_IPSEC
616 static int
617 ipe4_init(struct secasvar *sav, struct xformsw *xsp)
618 {
619         sav->tdb_xform = xsp;
620         return 0;
621 }
622
623 static int
624 ipe4_zeroize(struct secasvar *sav)
625 {
626         sav->tdb_xform = NULL;
627         return 0;
628 }
629
630 static int
631 ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
632 {
633         /* This is a rather serious mistake, so no conditional printing. */
634         printf("ipe4_input: should never be called\n");
635         if (m)
636                 m_freem(m);
637         return EOPNOTSUPP;
638 }
639
640 static struct xformsw ipe4_xformsw = {
641         XF_IP4,         0,              "IPv4 Simple Encapsulation",
642         ipe4_init,      ipe4_zeroize,   ipe4_input,     ipip_output,
643 };
644
645 extern struct domain inetdomain;
646 static struct protosw ipe4_protosw[] = {
647 { SOCK_RAW,     &inetdomain,    IPPROTO_IPV4,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
648   ip4_input,
649                 0,              0,              rip_ctloutput,
650   cpu0_soport,
651   0,            0,              0,              0,
652   &rip_usrreqs
653 },
654 #ifdef INET6
655 { SOCK_RAW,     &inetdomain,    IPPROTO_IPV6,   PR_ATOMIC|PR_ADDR|PR_LASTHDR,
656   ip4_input,
657                 0,              0,              rip_ctloutput,
658   cpu0_soport,
659   0,            0,              0,              0,
660   &rip_usrreqs
661 }
662 #endif
663 };
664
665 /*
666  * Check the encapsulated packet to see if we want it
667  */
668 static int
669 ipe4_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
670 {
671         /*
672          * Only take packets coming from IPSEC tunnels; the rest
673          * must be handled by the gif tunnel code.  Note that we
674          * also return a minimum priority when we want the packet
675          * so any explicit gif tunnels take precedence.
676          */
677         return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
678 }
679
680 static void
681 ipe4_attach(void)
682 {
683         xform_register(&ipe4_xformsw);
684         /* attach to encapsulation framework */
685         /* XXX save return cookie for detach on module remove */
686         (void) encap_attach_func(AF_INET, -1,
687                 ipe4_encapcheck, (struct protosw*) &ipe4_protosw[0], NULL);
688 #ifdef INET6
689         (void) encap_attach_func(AF_INET6, -1,
690                 ipe4_encapcheck, (struct protosw*) &ipe4_protosw[1], NULL);
691 #endif
692 }
693 SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);
694 #endif  /* FAST_IPSEC */