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