]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/netipsec/ipsec_output.c
MFC r322530 and r323220:
[FreeBSD/stable/9.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/pfil.h>
48 #include <net/route.h>
49 #include <net/vnet.h>
50
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip_ecn.h>
57 #ifdef INET6
58 #include <netinet6/ip6_ecn.h>
59 #endif
60
61 #include <netinet/ip6.h>
62 #ifdef INET6
63 #include <netinet6/ip6_var.h>
64 #endif
65 #include <netinet/in_pcb.h>
66 #ifdef INET6
67 #include <netinet/icmp6.h>
68 #endif
69
70 #include <netipsec/ipsec.h>
71 #ifdef INET6
72 #include <netipsec/ipsec6.h>
73 #endif
74 #include <netipsec/ah_var.h>
75 #include <netipsec/esp_var.h>
76 #include <netipsec/ipcomp_var.h>
77
78 #include <netipsec/xform.h>
79
80 #include <netipsec/key.h>
81 #include <netipsec/keydb.h>
82 #include <netipsec/key_debug.h>
83
84 #include <machine/in_cksum.h>
85
86 #ifdef IPSEC_NAT_T
87 #include <netinet/udp.h>
88 #endif
89
90 #ifdef DEV_ENC
91 #include <net/if_enc.h>
92 #endif
93
94
95 int
96 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
97 {
98         struct tdb_ident *tdbi;
99         struct m_tag *mtag;
100         struct secasvar *sav;
101         struct secasindex *saidx;
102         int error;
103
104         IPSEC_ASSERT(m != NULL, ("null mbuf"));
105         IPSEC_ASSERT(isr != NULL, ("null ISR"));
106         sav = isr->sav;
107         IPSEC_ASSERT(sav != NULL, ("null SA"));
108         IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
109
110         saidx = &sav->sah->saidx;
111         switch (saidx->dst.sa.sa_family) {
112 #ifdef INET
113         case AF_INET:
114                 /* Fix the header length, for AH processing. */
115                 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
116                 break;
117 #endif /* INET */
118 #ifdef INET6
119         case AF_INET6:
120                 /* Fix the header length, for AH processing. */
121                 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
122                         error = ENXIO;
123                         goto bad;
124                 }
125                 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
126                         /* No jumbogram support. */
127                         error = ENXIO;  /*?*/
128                         goto bad;
129                 }
130                 mtod(m, struct ip6_hdr *)->ip6_plen =
131                         htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
132                 break;
133 #endif /* INET6 */
134         default:
135                 DPRINTF(("%s: unknown protocol family %u\n", __func__,
136                     saidx->dst.sa.sa_family));
137                 error = ENXIO;
138                 goto bad;
139         }
140
141         /*
142          * Add a record of what we've done or what needs to be done to the
143          * packet.
144          */
145         mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
146                         sizeof(struct tdb_ident), M_NOWAIT);
147         if (mtag == NULL) {
148                 DPRINTF(("%s: could not get packet tag\n", __func__));
149                 error = ENOMEM;
150                 goto bad;
151         }
152
153         tdbi = (struct tdb_ident *)(mtag + 1);
154         tdbi->dst = saidx->dst;
155         tdbi->proto = saidx->proto;
156         tdbi->spi = sav->spi;
157         m_tag_prepend(m, mtag);
158
159         /*
160          * If there's another (bundled) SA to apply, do so.
161          * Note that this puts a burden on the kernel stack size.
162          * If this is a problem we'll need to introduce a queue
163          * to set the packet on so we can unwind the stack before
164          * doing further processing.
165          */
166         if (isr->next) {
167                 IPSECSTAT_INC(ips_out_bundlesa);
168                 /* XXX-BZ currently only support same AF bundles. */
169                 switch (saidx->dst.sa.sa_family) {
170 #ifdef INET
171                 case AF_INET:
172                         return ipsec4_process_packet(m, isr->next, 0, 0);
173                         /* NOTREACHED */
174 #endif
175 #ifdef notyet
176 #ifdef INET6
177                 case AF_INET6:
178                         /* XXX */
179                         ipsec6_output_trans()
180                         ipsec6_output_tunnel()
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         m_addr_changed(m);
194
195         /*
196          * We're done with IPsec processing, transmit the packet using the
197          * appropriate network protocol (IP or IPv6). SPD lookup will be
198          * performed again there.
199          */
200         switch (saidx->dst.sa.sa_family) {
201 #ifdef INET
202         struct ip *ip;
203         case AF_INET:
204                 ip = mtod(m, struct ip *);
205                 ip->ip_len = ntohs(ip->ip_len);
206                 ip->ip_off = ntohs(ip->ip_off);
207
208 #ifdef IPSEC_NAT_T
209                 /*
210                  * If NAT-T is enabled, now that all IPsec processing is done
211                  * insert UDP encapsulation header after IP header.
212                  */
213                 if (sav->natt_type) {
214                         const int hlen = (ip->ip_hl << 2);
215                         int size, off;
216                         struct mbuf *mi;
217                         struct udphdr *udp;
218
219                         size = sizeof(struct udphdr);
220                         if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
221                                 /*
222                                  * draft-ietf-ipsec-nat-t-ike-0[01].txt and
223                                  * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
224                                  * ignoring possible AH mode
225                                  * non-IKE marker + non-ESP marker
226                                  * from draft-ietf-ipsec-udp-encaps-00.txt.
227                                  */
228                                 size += sizeof(u_int64_t);
229                         }
230                         mi = m_makespace(m, hlen, size, &off);
231                         if (mi == NULL) {
232                                 DPRINTF(("%s: m_makespace for udphdr failed\n",
233                                     __func__));
234                                 error = ENOBUFS;
235                                 goto bad;
236                         }
237
238                         udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
239                         if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
240                                 udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
241                         else
242                                 udp->uh_sport =
243                                         KEY_PORTFROMSADDR(&sav->sah->saidx.src);
244                         udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
245                         udp->uh_sum = 0;
246                         udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
247                         ip->ip_len = m->m_pkthdr.len;
248                         ip->ip_p = IPPROTO_UDP;
249
250                         if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
251                                 *(u_int64_t *)(udp + 1) = 0;
252                 }
253 #endif /* IPSEC_NAT_T */
254
255                 return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
256 #endif /* INET */
257 #ifdef INET6
258         case AF_INET6:
259                 /*
260                  * We don't need massage, IPv6 header fields are always in
261                  * net endian.
262                  */
263                 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
264 #endif /* INET6 */
265         }
266         panic("ipsec_process_done");
267 bad:
268         m_freem(m);
269         return (error);
270 }
271
272 static struct ipsecrequest *
273 ipsec_nextisr(
274         struct mbuf *m,
275         struct ipsecrequest *isr,
276         int af,
277         struct secasindex *saidx,
278         int *error
279 )
280 {
281 #define IPSEC_OSTAT(name)       do {            \
282         if (isr->saidx.proto == IPPROTO_ESP)    \
283                 ESPSTAT_INC(esps_##name);       \
284         else if (isr->saidx.proto == IPPROTO_AH)\
285                 AHSTAT_INC(ahs_##name);         \
286         else                                    \
287                 IPCOMPSTAT_INC(ipcomps_##name); \
288 } while (0)
289         struct secasvar *sav;
290
291         IPSECREQUEST_LOCK_ASSERT(isr);
292
293         IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
294                 ("invalid address family %u", af));
295 again:
296         /*
297          * Craft SA index to search for proper SA.  Note that
298          * we only fillin unspecified SA peers for transport
299          * mode; for tunnel mode they must already be filled in.
300          */
301         *saidx = isr->saidx;
302         if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
303                 /* Fillin unspecified SA peers only for transport mode */
304                 if (af == AF_INET) {
305                         struct sockaddr_in *sin;
306                         struct ip *ip = mtod(m, struct ip *);
307
308                         if (saidx->src.sa.sa_len == 0) {
309                                 sin = &saidx->src.sin;
310                                 sin->sin_len = sizeof(*sin);
311                                 sin->sin_family = AF_INET;
312                                 sin->sin_port = IPSEC_PORT_ANY;
313                                 sin->sin_addr = ip->ip_src;
314                         }
315                         if (saidx->dst.sa.sa_len == 0) {
316                                 sin = &saidx->dst.sin;
317                                 sin->sin_len = sizeof(*sin);
318                                 sin->sin_family = AF_INET;
319                                 sin->sin_port = IPSEC_PORT_ANY;
320                                 sin->sin_addr = ip->ip_dst;
321                         }
322                 } else {
323                         struct sockaddr_in6 *sin6;
324                         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
325
326                         if (saidx->src.sin6.sin6_len == 0) {
327                                 sin6 = (struct sockaddr_in6 *)&saidx->src;
328                                 sin6->sin6_len = sizeof(*sin6);
329                                 sin6->sin6_family = AF_INET6;
330                                 sin6->sin6_port = IPSEC_PORT_ANY;
331                                 sin6->sin6_addr = ip6->ip6_src;
332                                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
333                                         /* fix scope id for comparing SPD */
334                                         sin6->sin6_addr.s6_addr16[1] = 0;
335                                         sin6->sin6_scope_id =
336                                             ntohs(ip6->ip6_src.s6_addr16[1]);
337                                 }
338                         }
339                         if (saidx->dst.sin6.sin6_len == 0) {
340                                 sin6 = (struct sockaddr_in6 *)&saidx->dst;
341                                 sin6->sin6_len = sizeof(*sin6);
342                                 sin6->sin6_family = AF_INET6;
343                                 sin6->sin6_port = IPSEC_PORT_ANY;
344                                 sin6->sin6_addr = ip6->ip6_dst;
345                                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
346                                         /* fix scope id for comparing SPD */
347                                         sin6->sin6_addr.s6_addr16[1] = 0;
348                                         sin6->sin6_scope_id =
349                                             ntohs(ip6->ip6_dst.s6_addr16[1]);
350                                 }
351                         }
352                 }
353         }
354
355         /*
356          * Lookup SA and validate it.
357          */
358         *error = key_checkrequest(isr, saidx);
359         if (*error != 0) {
360                 /*
361                  * IPsec processing is required, but no SA found.
362                  * I assume that key_acquire() had been called
363                  * to get/establish the SA. Here I discard
364                  * this packet because it is responsibility for
365                  * upper layer to retransmit the packet.
366                  */
367                 IPSECSTAT_INC(ips_out_nosa);
368                 goto bad;
369         }
370         sav = isr->sav;
371         if (sav == NULL) {
372                 IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
373                         ("no SA found, but required; level %u",
374                         ipsec_get_reqlevel(isr)));
375                 IPSECREQUEST_UNLOCK(isr);
376                 isr = isr->next;
377                 /*
378                  * If isr is NULL, we found a 'use' policy w/o SA.
379                  * Return w/o error and w/o isr so we can drop out
380                  * and continue w/o IPsec processing.
381                  */
382                 if (isr == NULL)
383                         return isr;
384                 IPSECREQUEST_LOCK(isr);
385                 goto again;
386         }
387
388         /*
389          * Check system global policy controls.
390          */
391         if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
392             (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
393             (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
394                 DPRINTF(("%s: IPsec outbound packet dropped due"
395                         " to policy (check your sysctls)\n", __func__));
396                 IPSEC_OSTAT(pdrops);
397                 *error = EHOSTUNREACH;
398                 goto bad;
399         }
400
401         /*
402          * Sanity check the SA contents for the caller
403          * before they invoke the xform output method.
404          */
405         if (sav->tdb_xform == NULL) {
406                 DPRINTF(("%s: no transform for SA\n", __func__));
407                 IPSEC_OSTAT(noxform);
408                 *error = EHOSTUNREACH;
409                 goto bad;
410         }
411         return isr;
412 bad:
413         IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
414         IPSECREQUEST_UNLOCK(isr);
415         return NULL;
416 #undef IPSEC_OSTAT
417 }
418
419 #ifdef INET
420 /*
421  * IPsec output logic for IPv4.
422  */
423 int
424 ipsec4_process_packet(
425         struct mbuf *m,
426         struct ipsecrequest *isr,
427         int flags,
428         int tunalready)
429 {
430         struct secasindex saidx;
431         struct secasvar *sav;
432         struct ip *ip;
433         int error, i, off;
434
435         IPSEC_ASSERT(m != NULL, ("null mbuf"));
436         IPSEC_ASSERT(isr != NULL, ("null isr"));
437
438         IPSECREQUEST_LOCK(isr);         /* insure SA contents don't change */
439
440         isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
441         if (isr == NULL) {
442                 if (error != 0)
443                         goto bad;
444                 return EJUSTRETURN;
445         }
446
447         sav = isr->sav;
448
449 #ifdef DEV_ENC
450         encif->if_opackets++;
451         encif->if_obytes += m->m_pkthdr.len;
452
453         /* pass the mbuf to enc0 for bpf processing */
454         ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
455         /* pass the mbuf to enc0 for packet filtering */
456         if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
457                 goto bad;
458 #endif
459
460         if (!tunalready) {
461                 union sockaddr_union *dst = &sav->sah->saidx.dst;
462                 int setdf;
463
464                 /*
465                  * Collect IP_DF state from the outer header.
466                  */
467                 if (dst->sa.sa_family == AF_INET) {
468                         if (m->m_len < sizeof (struct ip) &&
469                             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
470                                 error = ENOBUFS;
471                                 goto bad;
472                         }
473                         ip = mtod(m, struct ip *);
474                         /* Honor system-wide control of how to handle IP_DF */
475                         switch (V_ip4_ipsec_dfbit) {
476                         case 0:                 /* clear in outer header */
477                         case 1:                 /* set in outer header */
478                                 setdf = V_ip4_ipsec_dfbit;
479                                 break;
480                         default:                /* propagate to outer header */
481                                 setdf = ntohs(ip->ip_off & IP_DF);
482                                 break;
483                         }
484                 } else {
485                         ip = NULL;              /* keep compiler happy */
486                         setdf = 0;
487                 }
488                 /* Do the appropriate encapsulation, if necessary */
489                 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
490                     dst->sa.sa_family != AF_INET ||         /* PF mismatch */
491 #if 0
492                     (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
493                     sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
494 #endif
495                     (dst->sa.sa_family == AF_INET &&        /* Proxy */
496                      dst->sin.sin_addr.s_addr != INADDR_ANY &&
497                      dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
498                         struct mbuf *mp;
499
500                         /* Fix IPv4 header checksum and length */
501                         if (m->m_len < sizeof (struct ip) &&
502                             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
503                                 error = ENOBUFS;
504                                 goto bad;
505                         }
506                         ip = mtod(m, struct ip *);
507                         ip->ip_len = htons(m->m_pkthdr.len);
508                         ip->ip_sum = 0;
509                         ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
510
511                         /* Encapsulate the packet */
512                         error = ipip_output(m, isr, &mp, 0, 0);
513                         if (mp == NULL && !error) {
514                                 /* Should never happen. */
515                                 DPRINTF(("%s: ipip_output returns no mbuf and "
516                                         "no error!", __func__));
517                                 error = EFAULT;
518                         }
519                         if (error) {
520                                 if (mp) {
521                                         /* XXX: Should never happen! */
522                                         m_freem(mp);
523                                 }
524                                 m = NULL; /* ipip_output() already freed it */
525                                 goto bad;
526                         }
527                         m = mp, mp = NULL;
528                         /*
529                          * ipip_output clears IP_DF in the new header.  If
530                          * we need to propagate IP_DF from the outer header,
531                          * then we have to do it here.
532                          *
533                          * XXX shouldn't assume what ipip_output does.
534                          */
535                         if (dst->sa.sa_family == AF_INET && setdf) {
536                                 if (m->m_len < sizeof (struct ip) &&
537                                     (m = m_pullup(m, sizeof (struct ip))) == NULL) {
538                                         error = ENOBUFS;
539                                         goto bad;
540                                 }
541                                 ip = mtod(m, struct ip *);
542                                 ip->ip_off = ntohs(ip->ip_off);
543                                 ip->ip_off |= IP_DF;
544                                 ip->ip_off = htons(ip->ip_off);
545                         }
546                 }
547         }
548
549 #ifdef DEV_ENC
550         /* pass the mbuf to enc0 for bpf processing */
551         ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_AFTER);
552         /* pass the mbuf to enc0 for packet filtering */
553         if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
554                 goto bad;
555 #endif
556
557         /*
558          * Dispatch to the appropriate IPsec transform logic.  The
559          * packet will be returned for transmission after crypto
560          * processing, etc. are completed.  For encapsulation we
561          * bypass this call because of the explicit call done above
562          * (necessary to deal with IP_DF handling for IPv4).
563          *
564          * NB: m & sav are ``passed to caller'' who's reponsible for
565          *     for reclaiming their resources.
566          */
567         if (sav->tdb_xform->xf_type != XF_IP4) {
568                 ip = mtod(m, struct ip *);
569                 i = ip->ip_hl << 2;
570                 off = offsetof(struct ip, ip_p);
571                 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
572         } else {
573                 error = ipsec_process_done(m, isr);
574         }
575         IPSECREQUEST_UNLOCK(isr);
576         return error;
577 bad:
578         if (isr)
579                 IPSECREQUEST_UNLOCK(isr);
580         if (m)
581                 m_freem(m);
582         return error;
583 }
584 #endif
585
586 #ifdef INET6
587 /*
588  * Chop IP6 header from the payload.
589  */
590 static struct mbuf *
591 ipsec6_splithdr(struct mbuf *m)
592 {
593         struct mbuf *mh;
594         struct ip6_hdr *ip6;
595         int hlen;
596
597         IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
598                 ("first mbuf too short, len %u", m->m_len));
599         ip6 = mtod(m, struct ip6_hdr *);
600         hlen = sizeof(struct ip6_hdr);
601         if (m->m_len > hlen) {
602                 MGETHDR(mh, M_DONTWAIT, MT_DATA);
603                 if (!mh) {
604                         m_freem(m);
605                         return NULL;
606                 }
607                 M_MOVE_PKTHDR(mh, m);
608                 MH_ALIGN(mh, hlen);
609                 m->m_len -= hlen;
610                 m->m_data += hlen;
611                 mh->m_next = m;
612                 m = mh;
613                 m->m_len = hlen;
614                 bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
615         } else if (m->m_len < hlen) {
616                 m = m_pullup(m, hlen);
617                 if (!m)
618                         return NULL;
619         }
620         return m;
621 }
622
623 /*
624  * IPsec output logic for IPv6, transport mode.
625  */
626 int
627 ipsec6_output_trans(
628         struct ipsec_output_state *state,
629         u_char *nexthdrp,
630         struct mbuf *mprev,
631         struct secpolicy *sp,
632         int flags,
633         int *tun)
634 {
635         struct ipsecrequest *isr;
636         struct secasindex saidx;
637         int error = 0;
638         struct mbuf *m;
639
640         IPSEC_ASSERT(state != NULL, ("null state"));
641         IPSEC_ASSERT(state->m != NULL, ("null m"));
642         IPSEC_ASSERT(nexthdrp != NULL, ("null nexthdrp"));
643         IPSEC_ASSERT(mprev != NULL, ("null mprev"));
644         IPSEC_ASSERT(sp != NULL, ("null sp"));
645         IPSEC_ASSERT(tun != NULL, ("null tun"));
646
647         KEYDEBUG(KEYDEBUG_IPSEC_DATA,
648                 printf("%s: applied SP\n", __func__);
649                 kdebug_secpolicy(sp));
650
651         isr = sp->req;
652         if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
653                 /* the rest will be handled by ipsec6_output_tunnel() */
654                 *tun = 1;               /* need tunnel-mode processing */
655                 return 0;
656         }
657
658         *tun = 0;
659         m = state->m;
660
661         IPSECREQUEST_LOCK(isr);         /* insure SA contents don't change */
662         isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
663         if (isr == NULL) {
664                 if (error != 0) {
665 #ifdef notdef
666                         /* XXX should notification be done for all errors ? */
667                         /*
668                          * Notify the fact that the packet is discarded
669                          * to ourselves. I believe this is better than
670                          * just silently discarding. (jinmei@kame.net)
671                          * XXX: should we restrict the error to TCP packets?
672                          * XXX: should we directly notify sockets via
673                          *      pfctlinputs?
674                          */
675                         icmp6_error(m, ICMP6_DST_UNREACH,
676                                     ICMP6_DST_UNREACH_ADMIN, 0);
677                         m = NULL;       /* NB: icmp6_error frees mbuf */
678 #endif
679                         goto bad;
680                 }
681                 return EJUSTRETURN;
682         }
683
684         error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
685                                                   sizeof (struct ip6_hdr),
686                                                   offsetof(struct ip6_hdr, 
687                                                            ip6_nxt));
688         IPSECREQUEST_UNLOCK(isr);
689         return error;
690 bad:
691         if (isr)
692                 IPSECREQUEST_UNLOCK(isr);
693         if (m)
694                 m_freem(m);
695         state->m = NULL;
696         return error;
697 }
698
699 static int
700 ipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
701 {
702         struct ip6_hdr *oip6;
703         struct ip6_hdr *ip6;
704         size_t plen;
705
706         /* can't tunnel between different AFs */
707         if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
708             sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
709                 m_freem(m);
710                 return EINVAL;
711         }
712         IPSEC_ASSERT(m->m_len == sizeof (struct ip6_hdr),
713                 ("mbuf wrong size; len %u", m->m_len));
714
715
716         /*
717          * grow the mbuf to accomodate the new IPv6 header.
718          */
719         plen = m->m_pkthdr.len;
720         if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
721                 struct mbuf *n;
722                 MGET(n, M_DONTWAIT, MT_DATA);
723                 if (!n) {
724                         m_freem(m);
725                         return ENOBUFS;
726                 }
727                 n->m_len = sizeof(struct ip6_hdr);
728                 n->m_next = m->m_next;
729                 m->m_next = n;
730                 m->m_pkthdr.len += sizeof(struct ip6_hdr);
731                 oip6 = mtod(n, struct ip6_hdr *);
732         } else {
733                 m->m_next->m_len += sizeof(struct ip6_hdr);
734                 m->m_next->m_data -= sizeof(struct ip6_hdr);
735                 m->m_pkthdr.len += sizeof(struct ip6_hdr);
736                 oip6 = mtod(m->m_next, struct ip6_hdr *);
737         }
738         ip6 = mtod(m, struct ip6_hdr *);
739         bcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
740
741         /* Fake link-local scope-class addresses */
742         if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
743                 oip6->ip6_src.s6_addr16[1] = 0;
744         if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
745                 oip6->ip6_dst.s6_addr16[1] = 0;
746
747         /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
748         /* ECN consideration. */
749         ip6_ecn_ingress(V_ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
750         if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
751                 ip6->ip6_plen = htons(plen);
752         else {
753                 /* ip6->ip6_plen will be updated in ip6_output() */
754         }
755         ip6->ip6_nxt = IPPROTO_IPV6;
756         ip6->ip6_src = sav->sah->saidx.src.sin6.sin6_addr;
757         ip6->ip6_dst = sav->sah->saidx.dst.sin6.sin6_addr;
758         ip6->ip6_hlim = IPV6_DEFHLIM;
759
760         /* XXX Should ip6_src be updated later ? */
761
762         return 0;
763 }
764
765 /*
766  * IPsec output logic for IPv6, tunnel mode.
767  */
768 int
769 ipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
770 {
771         struct ip6_hdr *ip6;
772         struct ipsecrequest *isr;
773         struct secasindex saidx;
774         int error;
775         struct sockaddr_in6 *dst6;
776         struct mbuf *m;
777
778         IPSEC_ASSERT(state != NULL, ("null state"));
779         IPSEC_ASSERT(state->m != NULL, ("null m"));
780         IPSEC_ASSERT(sp != NULL, ("null sp"));
781
782         KEYDEBUG(KEYDEBUG_IPSEC_DATA,
783                 printf("%s: applied SP\n", __func__);
784                 kdebug_secpolicy(sp));
785
786         m = state->m;
787         /*
788          * transport mode ipsec (before the 1st tunnel mode) is already
789          * processed by ipsec6_output_trans().
790          */
791         for (isr = sp->req; isr; isr = isr->next) {
792                 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
793                         break;
794         }
795
796         IPSECREQUEST_LOCK(isr);         /* insure SA contents don't change */
797         isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
798         if (isr == NULL) {
799                 if (error != 0)
800                         goto bad;
801                 return EJUSTRETURN; 
802         }
803
804 #ifdef DEV_ENC
805         encif->if_opackets++;
806         encif->if_obytes += m->m_pkthdr.len;
807
808         /* pass the mbuf to enc0 for bpf processing */
809         ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
810         /* pass the mbuf to enc0 for packet filtering */
811         if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
812                 goto bad;
813 #endif
814
815         /*
816          * There may be the case that SA status will be changed when
817          * we are refering to one. So calling splsoftnet().
818          */
819         if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
820                 /*
821                  * build IPsec tunnel.
822                  */
823                 /* XXX should be processed with other familiy */
824                 if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
825                         ipseclog((LOG_ERR, "%s: family mismatched between "
826                             "inner and outer, spi=%u\n", __func__,
827                             ntohl(isr->sav->spi)));
828                         IPSEC6STAT_INC(ips_out_inval);
829                         error = EAFNOSUPPORT;
830                         goto bad;
831                 }
832
833                 m = ipsec6_splithdr(m);
834                 if (!m) {
835                         IPSEC6STAT_INC(ips_out_nomem);
836                         error = ENOMEM;
837                         goto bad;
838                 }
839                 error = ipsec6_encapsulate(m, isr->sav);
840                 if (error) {
841                         m = NULL;
842                         goto bad;
843                 }
844                 ip6 = mtod(m, struct ip6_hdr *);
845
846                 state->ro =
847                     (struct route *)&isr->sav->sah->route_cache.sin6_route;
848                 state->dst = (struct sockaddr *)&state->ro->ro_dst;
849                 dst6 = (struct sockaddr_in6 *)state->dst;
850                 if (state->ro->ro_rt
851                  && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
852                   || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
853                         RTFREE(state->ro->ro_rt);
854                         state->ro->ro_rt = NULL;
855                 }
856                 if (state->ro->ro_rt == NULL) {
857                         bzero(dst6, sizeof(*dst6));
858                         dst6->sin6_family = AF_INET6;
859                         dst6->sin6_len = sizeof(*dst6);
860                         dst6->sin6_addr = ip6->ip6_dst;
861                         rtalloc_ign_fib(state->ro, 0UL, M_GETFIB(m));
862                 }
863                 if (state->ro->ro_rt == NULL) {
864                         IP6STAT_INC(ip6s_noroute);
865                         IPSEC6STAT_INC(ips_out_noroute);
866                         error = EHOSTUNREACH;
867                         goto bad;
868                 }
869
870                 /* adjust state->dst if tunnel endpoint is offlink */
871                 if (state->ro->ro_rt->rt_flags & RTF_GATEWAY)
872                         state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
873         }
874
875         m = ipsec6_splithdr(m);
876         if (!m) {
877                 IPSEC6STAT_INC(ips_out_nomem);
878                 error = ENOMEM;
879                 goto bad;
880         }
881         ip6 = mtod(m, struct ip6_hdr *);
882
883 #ifdef DEV_ENC
884         /* pass the mbuf to enc0 for bpf processing */
885         ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_AFTER);
886         /* pass the mbuf to enc0 for packet filtering */
887         if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
888                 goto bad;
889 #endif
890
891         error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
892                 sizeof (struct ip6_hdr),
893                 offsetof(struct ip6_hdr, ip6_nxt));
894         IPSECREQUEST_UNLOCK(isr);
895         return error;
896 bad:
897         if (isr)
898                 IPSECREQUEST_UNLOCK(isr);
899         if (m)
900                 m_freem(m);
901         state->m = NULL;
902         return error;
903 }
904 #endif /*INET6*/