]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipsec/ipsec_output.c
Reimport dts files from vendor repo now that it has been properly
[FreeBSD/FreeBSD.git] / sys / netipsec / ipsec_output.c
1 /*-
2  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * IPsec output processing.
31  */
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_ipsec.h"
35 #include "opt_enc.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/domain.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/errno.h>
44 #include <sys/syslog.h>
45
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/pfil.h>
49 #include <net/route.h>
50 #include <net/vnet.h>
51
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/in_var.h>
57 #include <netinet/ip_ecn.h>
58 #ifdef INET6
59 #include <netinet6/ip6_ecn.h>
60 #endif
61
62 #include <netinet/ip6.h>
63 #ifdef INET6
64 #include <netinet6/ip6_var.h>
65 #endif
66 #include <netinet/in_pcb.h>
67 #ifdef INET6
68 #include <netinet/icmp6.h>
69 #endif
70
71 #include <netipsec/ipsec.h>
72 #ifdef INET6
73 #include <netipsec/ipsec6.h>
74 #endif
75 #include <netipsec/ah_var.h>
76 #include <netipsec/esp_var.h>
77 #include <netipsec/ipcomp_var.h>
78
79 #include <netipsec/xform.h>
80
81 #include <netipsec/key.h>
82 #include <netipsec/keydb.h>
83 #include <netipsec/key_debug.h>
84
85 #include <machine/in_cksum.h>
86
87 #ifdef IPSEC_NAT_T
88 #include <netinet/udp.h>
89 #endif
90
91 #ifdef DEV_ENC
92 #include <net/if_enc.h>
93 #endif
94
95
96 int
97 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
98 {
99         struct tdb_ident *tdbi;
100         struct m_tag *mtag;
101         struct secasvar *sav;
102         struct secasindex *saidx;
103         int error;
104
105         IPSEC_ASSERT(m != NULL, ("null mbuf"));
106         IPSEC_ASSERT(isr != NULL, ("null ISR"));
107         sav = isr->sav;
108         IPSEC_ASSERT(sav != NULL, ("null SA"));
109         IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
110
111         saidx = &sav->sah->saidx;
112         switch (saidx->dst.sa.sa_family) {
113 #ifdef INET
114         case AF_INET:
115                 /* Fix the header length, for AH processing. */
116                 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
117                 break;
118 #endif /* INET */
119 #ifdef INET6
120         case AF_INET6:
121                 /* Fix the header length, for AH processing. */
122                 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
123                         error = ENXIO;
124                         goto bad;
125                 }
126                 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
127                         /* No jumbogram support. */
128                         error = ENXIO;  /*?*/
129                         goto bad;
130                 }
131                 mtod(m, struct ip6_hdr *)->ip6_plen =
132                         htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
133                 break;
134 #endif /* INET6 */
135         default:
136                 DPRINTF(("%s: unknown protocol family %u\n", __func__,
137                     saidx->dst.sa.sa_family));
138                 error = ENXIO;
139                 goto bad;
140         }
141
142         /*
143          * Add a record of what we've done or what needs to be done to the
144          * packet.
145          */
146         mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
147                         sizeof(struct tdb_ident), M_NOWAIT);
148         if (mtag == NULL) {
149                 DPRINTF(("%s: could not get packet tag\n", __func__));
150                 error = ENOMEM;
151                 goto bad;
152         }
153
154         tdbi = (struct tdb_ident *)(mtag + 1);
155         tdbi->dst = saidx->dst;
156         tdbi->proto = saidx->proto;
157         tdbi->spi = sav->spi;
158         m_tag_prepend(m, mtag);
159
160         /*
161          * If there's another (bundled) SA to apply, do so.
162          * Note that this puts a burden on the kernel stack size.
163          * If this is a problem we'll need to introduce a queue
164          * to set the packet on so we can unwind the stack before
165          * doing further processing.
166          */
167         if (isr->next) {
168                 IPSECSTAT_INC(ips_out_bundlesa);
169                 /* XXX-BZ currently only support same AF bundles. */
170                 switch (saidx->dst.sa.sa_family) {
171 #ifdef INET
172                 case AF_INET:
173                         return ipsec4_process_packet(m, isr->next, 0, 0);
174                         /* NOTREACHED */
175 #endif
176 #ifdef notyet
177 #ifdef INET6
178                 case AF_INET6:
179                         /* XXX */
180                         return ipsec6_process_packet(m, isr->next);
181                         /* NOTREACHED */
182 #endif /* INET6 */
183 #endif
184                 default:
185                         DPRINTF(("%s: unknown protocol family %u\n", __func__,
186                             saidx->dst.sa.sa_family));
187                         error = ENXIO;
188                         goto bad;
189                 }
190         }
191         key_sa_recordxfer(sav, m);              /* record data transfer */
192
193         /*
194          * We're done with IPsec processing, transmit the packet using the
195          * appropriate network protocol (IP or IPv6). SPD lookup will be
196          * performed again there.
197          */
198         switch (saidx->dst.sa.sa_family) {
199 #ifdef INET
200         case AF_INET:
201 #ifdef IPSEC_NAT_T
202                 /*
203                  * If NAT-T is enabled, now that all IPsec processing is done
204                  * insert UDP encapsulation header after IP header.
205                  */
206                 if (sav->natt_type) {
207                         struct ip *ip = mtod(m, struct ip *);
208                         const int hlen = (ip->ip_hl << 2);
209                         int size, off;
210                         struct mbuf *mi;
211                         struct udphdr *udp;
212
213                         size = sizeof(struct udphdr);
214                         if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
215                                 /*
216                                  * draft-ietf-ipsec-nat-t-ike-0[01].txt and
217                                  * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
218                                  * ignoring possible AH mode
219                                  * non-IKE marker + non-ESP marker
220                                  * from draft-ietf-ipsec-udp-encaps-00.txt.
221                                  */
222                                 size += sizeof(u_int64_t);
223                         }
224                         mi = m_makespace(m, hlen, size, &off);
225                         if (mi == NULL) {
226                                 DPRINTF(("%s: m_makespace for udphdr failed\n",
227                                     __func__));
228                                 error = ENOBUFS;
229                                 goto bad;
230                         }
231
232                         udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
233                         if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
234                                 udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
235                         else
236                                 udp->uh_sport =
237                                         KEY_PORTFROMSADDR(&sav->sah->saidx.src);
238                         udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
239                         udp->uh_sum = 0;
240                         udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
241                         ip->ip_len = htons(m->m_pkthdr.len);
242                         ip->ip_p = IPPROTO_UDP;
243
244                         if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
245                                 *(u_int64_t *)(udp + 1) = 0;
246                 }
247 #endif /* IPSEC_NAT_T */
248
249                 return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
250 #endif /* INET */
251 #ifdef INET6
252         case AF_INET6:
253                 /*
254                  * We don't need massage, IPv6 header fields are always in
255                  * net endian.
256                  */
257                 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
258 #endif /* INET6 */
259         }
260         panic("ipsec_process_done");
261 bad:
262         m_freem(m);
263         return (error);
264 }
265
266 static struct ipsecrequest *
267 ipsec_nextisr(
268         struct mbuf *m,
269         struct ipsecrequest *isr,
270         int af,
271         struct secasindex *saidx,
272         int *error
273 )
274 {
275 #define IPSEC_OSTAT(name)       do {            \
276         if (isr->saidx.proto == IPPROTO_ESP)    \
277                 ESPSTAT_INC(esps_##name);       \
278         else if (isr->saidx.proto == IPPROTO_AH)\
279                 AHSTAT_INC(ahs_##name);         \
280         else                                    \
281                 IPCOMPSTAT_INC(ipcomps_##name); \
282 } while (0)
283         struct secasvar *sav;
284
285         IPSECREQUEST_LOCK_ASSERT(isr);
286
287         IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
288                 ("invalid address family %u", af));
289 again:
290         /*
291          * Craft SA index to search for proper SA.  Note that
292          * we only fillin unspecified SA peers for transport
293          * mode; for tunnel mode they must already be filled in.
294          */
295         *saidx = isr->saidx;
296         if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
297                 /* Fillin unspecified SA peers only for transport mode */
298                 if (af == AF_INET) {
299                         struct sockaddr_in *sin;
300                         struct ip *ip = mtod(m, struct ip *);
301
302                         if (saidx->src.sa.sa_len == 0) {
303                                 sin = &saidx->src.sin;
304                                 sin->sin_len = sizeof(*sin);
305                                 sin->sin_family = AF_INET;
306                                 sin->sin_port = IPSEC_PORT_ANY;
307                                 sin->sin_addr = ip->ip_src;
308                         }
309                         if (saidx->dst.sa.sa_len == 0) {
310                                 sin = &saidx->dst.sin;
311                                 sin->sin_len = sizeof(*sin);
312                                 sin->sin_family = AF_INET;
313                                 sin->sin_port = IPSEC_PORT_ANY;
314                                 sin->sin_addr = ip->ip_dst;
315                         }
316                 } else {
317                         struct sockaddr_in6 *sin6;
318                         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
319
320                         if (saidx->src.sin6.sin6_len == 0) {
321                                 sin6 = (struct sockaddr_in6 *)&saidx->src;
322                                 sin6->sin6_len = sizeof(*sin6);
323                                 sin6->sin6_family = AF_INET6;
324                                 sin6->sin6_port = IPSEC_PORT_ANY;
325                                 sin6->sin6_addr = ip6->ip6_src;
326                                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
327                                         /* fix scope id for comparing SPD */
328                                         sin6->sin6_addr.s6_addr16[1] = 0;
329                                         sin6->sin6_scope_id =
330                                             ntohs(ip6->ip6_src.s6_addr16[1]);
331                                 }
332                         }
333                         if (saidx->dst.sin6.sin6_len == 0) {
334                                 sin6 = (struct sockaddr_in6 *)&saidx->dst;
335                                 sin6->sin6_len = sizeof(*sin6);
336                                 sin6->sin6_family = AF_INET6;
337                                 sin6->sin6_port = IPSEC_PORT_ANY;
338                                 sin6->sin6_addr = ip6->ip6_dst;
339                                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
340                                         /* fix scope id for comparing SPD */
341                                         sin6->sin6_addr.s6_addr16[1] = 0;
342                                         sin6->sin6_scope_id =
343                                             ntohs(ip6->ip6_dst.s6_addr16[1]);
344                                 }
345                         }
346                 }
347         }
348
349         /*
350          * Lookup SA and validate it.
351          */
352         *error = key_checkrequest(isr, saidx);
353         if (*error != 0) {
354                 /*
355                  * IPsec processing is required, but no SA found.
356                  * I assume that key_acquire() had been called
357                  * to get/establish the SA. Here I discard
358                  * this packet because it is responsibility for
359                  * upper layer to retransmit the packet.
360                  */
361                 IPSECSTAT_INC(ips_out_nosa);
362                 goto bad;
363         }
364         sav = isr->sav;
365         if (sav == NULL) {
366                 IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
367                         ("no SA found, but required; level %u",
368                         ipsec_get_reqlevel(isr)));
369                 IPSECREQUEST_UNLOCK(isr);
370                 isr = isr->next;
371                 /*
372                  * If isr is NULL, we found a 'use' policy w/o SA.
373                  * Return w/o error and w/o isr so we can drop out
374                  * and continue w/o IPsec processing.
375                  */
376                 if (isr == NULL)
377                         return isr;
378                 IPSECREQUEST_LOCK(isr);
379                 goto again;
380         }
381
382         /*
383          * Check system global policy controls.
384          */
385         if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
386             (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
387             (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
388                 DPRINTF(("%s: IPsec outbound packet dropped due"
389                         " to policy (check your sysctls)\n", __func__));
390                 IPSEC_OSTAT(pdrops);
391                 *error = EHOSTUNREACH;
392                 goto bad;
393         }
394
395         /*
396          * Sanity check the SA contents for the caller
397          * before they invoke the xform output method.
398          */
399         if (sav->tdb_xform == NULL) {
400                 DPRINTF(("%s: no transform for SA\n", __func__));
401                 IPSEC_OSTAT(noxform);
402                 *error = EHOSTUNREACH;
403                 goto bad;
404         }
405         return isr;
406 bad:
407         IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
408         IPSECREQUEST_UNLOCK(isr);
409         return NULL;
410 #undef IPSEC_OSTAT
411 }
412
413 #ifdef INET
414 /*
415  * IPsec output logic for IPv4.
416  */
417 int
418 ipsec4_process_packet(
419         struct mbuf *m,
420         struct ipsecrequest *isr,
421         int flags,
422         int tunalready)
423 {
424         struct secasindex saidx;
425         struct secasvar *sav;
426         struct ip *ip;
427         int error, i, off;
428
429         IPSEC_ASSERT(m != NULL, ("null mbuf"));
430         IPSEC_ASSERT(isr != NULL, ("null isr"));
431
432         IPSECREQUEST_LOCK(isr);         /* insure SA contents don't change */
433
434         isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
435         if (isr == NULL) {
436                 if (error != 0)
437                         goto bad;
438                 return EJUSTRETURN;
439         }
440
441         sav = isr->sav;
442
443 #ifdef DEV_ENC
444         encif->if_opackets++;
445         encif->if_obytes += m->m_pkthdr.len;
446
447         /* pass the mbuf to enc0 for bpf processing */
448         ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
449         /* pass the mbuf to enc0 for packet filtering */
450         if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
451                 goto bad;
452 #endif
453
454         if (!tunalready) {
455                 union sockaddr_union *dst = &sav->sah->saidx.dst;
456                 int setdf;
457
458                 /*
459                  * Collect IP_DF state from the outer header.
460                  */
461                 if (dst->sa.sa_family == AF_INET) {
462                         if (m->m_len < sizeof (struct ip) &&
463                             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
464                                 error = ENOBUFS;
465                                 goto bad;
466                         }
467                         ip = mtod(m, struct ip *);
468                         /* Honor system-wide control of how to handle IP_DF */
469                         switch (V_ip4_ipsec_dfbit) {
470                         case 0:                 /* clear in outer header */
471                         case 1:                 /* set in outer header */
472                                 setdf = V_ip4_ipsec_dfbit;
473                                 break;
474                         default:                /* propagate to outer header */
475                                 setdf = ntohs(ip->ip_off & IP_DF);
476                                 break;
477                         }
478                 } else {
479                         ip = NULL;              /* keep compiler happy */
480                         setdf = 0;
481                 }
482                 /* Do the appropriate encapsulation, if necessary */
483                 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
484                     dst->sa.sa_family != AF_INET ||         /* PF mismatch */
485 #if 0
486                     (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
487                     sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
488 #endif
489                     (dst->sa.sa_family == AF_INET &&        /* Proxy */
490                      dst->sin.sin_addr.s_addr != INADDR_ANY &&
491                      dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
492                         struct mbuf *mp;
493
494                         /* Fix IPv4 header checksum and length */
495                         if (m->m_len < sizeof (struct ip) &&
496                             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
497                                 error = ENOBUFS;
498                                 goto bad;
499                         }
500                         ip = mtod(m, struct ip *);
501                         if (ip->ip_v == IPVERSION) {
502                                 ip->ip_len = htons(m->m_pkthdr.len);
503                                 ip->ip_sum = 0;
504                                 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
505                         }
506
507                         /* Encapsulate the packet */
508                         error = ipip_output(m, isr, &mp, 0, 0);
509                         if (mp == NULL && !error) {
510                                 /* Should never happen. */
511                                 DPRINTF(("%s: ipip_output returns no mbuf and "
512                                         "no error!", __func__));
513                                 error = EFAULT;
514                         }
515                         if (error) {
516                                 if (mp) {
517                                         /* XXX: Should never happen! */
518                                         m_freem(mp);
519                                 }
520                                 m = NULL; /* ipip_output() already freed it */
521                                 goto bad;
522                         }
523                         m = mp, mp = NULL;
524                         /*
525                          * ipip_output clears IP_DF in the new header.  If
526                          * we need to propagate IP_DF from the outer header,
527                          * then we have to do it here.
528                          *
529                          * XXX shouldn't assume what ipip_output does.
530                          */
531                         if (dst->sa.sa_family == AF_INET && setdf) {
532                                 if (m->m_len < sizeof (struct ip) &&
533                                     (m = m_pullup(m, sizeof (struct ip))) == NULL) {
534                                         error = ENOBUFS;
535                                         goto bad;
536                                 }
537                                 ip = mtod(m, struct ip *);
538                                 ip->ip_off = ntohs(ip->ip_off);
539                                 ip->ip_off |= IP_DF;
540                                 ip->ip_off = htons(ip->ip_off);
541                         }
542                 }
543         }
544
545 #ifdef DEV_ENC
546         /* pass the mbuf to enc0 for bpf processing */
547         ipsec_bpf(m, sav, sav->sah->saidx.dst.sa.sa_family, ENC_OUT|ENC_AFTER);
548         /* pass the mbuf to enc0 for packet filtering */
549         if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
550                 goto bad;
551 #endif
552
553         /*
554          * Dispatch to the appropriate IPsec transform logic.  The
555          * packet will be returned for transmission after crypto
556          * processing, etc. are completed.  For encapsulation we
557          * bypass this call because of the explicit call done above
558          * (necessary to deal with IP_DF handling for IPv4).
559          *
560          * NB: m & sav are ``passed to caller'' who's reponsible for
561          *     for reclaiming their resources.
562          */
563         if (sav->tdb_xform->xf_type != XF_IP4) {
564                 union sockaddr_union *dst = &sav->sah->saidx.dst;
565                 switch(dst->sa.sa_family) {
566                 case AF_INET:
567                         ip = mtod(m, struct ip *);
568                         i = ip->ip_hl << 2;
569                         off = offsetof(struct ip, ip_p);
570                         break;
571 #ifdef INET6
572                 case AF_INET6:
573                         i = sizeof(struct ip6_hdr);
574                         off = offsetof(struct ip6_hdr, ip6_nxt);
575                         break;
576 #endif /* INET6 */
577                 default:
578                 DPRINTF(("%s: unsupported protocol family %u\n",
579                                  __func__, dst->sa.sa_family));
580                         error = EPFNOSUPPORT;
581                         IPSECSTAT_INC(ips_out_inval);
582                         goto bad;
583                 }
584                 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
585         } else {
586                 error = ipsec_process_done(m, isr);
587         }
588         IPSECREQUEST_UNLOCK(isr);
589         return error;
590 bad:
591         if (isr)
592                 IPSECREQUEST_UNLOCK(isr);
593         if (m)
594                 m_freem(m);
595         return error;
596 }
597 #endif
598
599
600 #ifdef INET6
601 static int
602 in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
603 {
604         struct in6_addr ia2;
605
606         memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
607         if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
608                 ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
609
610         return IN6_ARE_ADDR_EQUAL(ia, &ia2);
611 }
612
613 /*
614  * IPsec output logic for IPv6.
615  */
616 int
617 ipsec6_process_packet(
618         struct mbuf *m,
619         struct ipsecrequest *isr
620     )
621 {
622         struct secasindex saidx;
623         struct secasvar *sav;
624         struct ip6_hdr *ip6;
625         int error, i, off;
626         union sockaddr_union *dst;
627
628         IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf"));
629         IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr"));
630
631         IPSECREQUEST_LOCK(isr);         /* insure SA contents don't change */
632
633         isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
634         if (isr == NULL) {
635                 if (error != 0)
636                         goto bad;
637                 return EJUSTRETURN;
638         }
639
640         sav = isr->sav;
641         dst = &sav->sah->saidx.dst;
642
643 #ifdef DEV_ENC
644         encif->if_opackets++;
645         encif->if_obytes += m->m_pkthdr.len;
646
647         /* pass the mbuf to enc0 for bpf processing */
648         ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
649         /* pass the mbuf to enc0 for packet filtering */
650         if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
651                 goto bad;
652 #endif /* DEV_ENC */
653
654         ip6 = mtod(m, struct ip6_hdr *); /* XXX */
655
656         /* Do the appropriate encapsulation, if necessary */
657         if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
658             dst->sa.sa_family != AF_INET6 ||        /* PF mismatch */
659             ((dst->sa.sa_family == AF_INET6) &&
660              (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
661              (!in6_sa_equal_addrwithscope(&dst->sin6,
662                                   &ip6->ip6_dst)))) {
663                 struct mbuf *mp;
664
665                 /* Fix IPv6 header payload length. */
666                 if (m->m_len < sizeof(struct ip6_hdr))
667                         if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL) {
668                                 error = ENOBUFS;
669                                 goto bad;
670                         }
671
672                 if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
673                         /* No jumbogram support. */
674                         error = ENXIO;   /*XXX*/
675                         goto bad;
676                 }
677
678                 ip6 = mtod(m, struct ip6_hdr *);
679                 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
680
681                 /* Encapsulate the packet */
682                 error = ipip_output(m, isr, &mp, 0, 0);
683                 if (mp == NULL && !error) {
684                         /* Should never happen. */
685                         DPRINTF(("ipsec6_process_packet: ipip_output "
686                                  "returns no mbuf and no error!"));
687                         error = EFAULT;
688                         goto bad;
689                 }
690
691                 if (error) {
692                         if (mp) {
693                                 /* XXX: Should never happen! */
694                                 m_freem(mp);
695                         }
696                         m = NULL; /* ipip_output() already freed it */
697                         goto bad;
698                 }
699
700                 m = mp;
701                 mp = NULL;
702         }
703
704 #ifdef DEV_ENC
705         ipsec_bpf(m, isr->sav, dst->sa.sa_family, ENC_OUT|ENC_AFTER);
706         /* pass the mbuf to enc0 for packet filtering */
707         if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
708                 goto bad;
709 #endif /* DEV_ENC */
710
711         switch(dst->sa.sa_family) {
712 #ifdef INET
713         case AF_INET:
714                 {
715                 struct ip *ip;
716                 ip = mtod(m, struct ip *);
717                 i = ip->ip_hl << 2;
718                 off = offsetof(struct ip, ip_p);
719                 }
720                 break;
721 #endif /* AF_INET */
722         case AF_INET6:
723                 i = sizeof(struct ip6_hdr);
724                 off = offsetof(struct ip6_hdr, ip6_nxt);
725                 break;
726         default:
727                 DPRINTF(("%s: unsupported protocol family %u\n",
728                                  __func__, dst->sa.sa_family));
729                 error = EPFNOSUPPORT;
730                 IPSEC6STAT_INC(ips_out_inval);
731                 goto bad;
732         }
733         error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
734         IPSECREQUEST_UNLOCK(isr);
735         return error;
736 bad:
737
738         if (isr)
739                 IPSECREQUEST_UNLOCK(isr);
740         if (m)
741                 m_freem(m);
742         return error;
743 }
744 #endif /*INET6*/