kernel: Move us to using M_NOWAIT and M_WAITOK for mbuf functions.
[dragonfly.git] / sys / netproto / ipsec / 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: src/sys/netipsec/ipsec_output.c,v 1.3.2.2 2003/03/28 20:32:53 sam Exp $
27  */
28
29 /*
30  * IPsec output processing.
31  */
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_ipsec.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/domain.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
43 #include <sys/syslog.h>
44 #include <sys/in_cksum.h>
45
46 #include <net/if.h>
47 #include <net/route.h>
48
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip_ecn.h>
55 #ifdef INET6
56 #include <netinet6/ip6_ecn.h>
57 #endif
58
59 #include <netinet/ip6.h>
60 #ifdef INET6
61 #include <netinet6/ip6_var.h>
62 #endif
63 #include <netinet/in_pcb.h>
64 #ifdef INET6
65 #include <netinet/icmp6.h>
66 #endif
67
68 #include <netproto/ipsec/ipsec.h>
69 #ifdef INET6
70 #include <netproto/ipsec/ipsec6.h>
71 #endif
72 #include <netproto/ipsec/ah_var.h>
73 #include <netproto/ipsec/esp_var.h>
74 #include <netproto/ipsec/ipcomp_var.h>
75
76 #include <netproto/ipsec/xform.h>
77
78 #include <netproto/ipsec/key.h>
79 #include <netproto/ipsec/keydb.h>
80 #include <netproto/ipsec/key_debug.h>
81
82 int
83 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
84 {
85         struct tdb_ident *tdbi;
86         struct m_tag *mtag;
87         struct secasvar *sav;
88         struct secasindex *saidx;
89         int error;
90
91         KASSERT(m != NULL, ("ipsec_process_done: null mbuf"));
92         KASSERT(isr != NULL, ("ipsec_process_done: null ISR"));
93         sav = isr->sav;
94         KASSERT(sav != NULL, ("ipsec_process_done: null SA"));
95         KASSERT(sav->sah != NULL, ("ipsec_process_done: null SAH"));
96
97         saidx = &sav->sah->saidx;
98         switch (saidx->dst.sa.sa_family) {
99 #ifdef INET
100         case AF_INET:
101                 /* Fix the header length, for AH processing. */
102                 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
103                 break;
104 #endif /* INET */
105 #ifdef INET6
106         case AF_INET6:
107                 /* Fix the header length, for AH processing. */
108                 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
109                         error = ENXIO;
110                         goto bad;
111                 }
112                 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
113                         /* No jumbogram support. */
114                         error = ENXIO;  /*?*/
115                         goto bad;
116                 }
117                 mtod(m, struct ip6_hdr *)->ip6_plen =
118                         htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
119                 break;
120 #endif /* INET6 */
121         default:
122                 DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
123                     saidx->dst.sa.sa_family));
124                 error = ENXIO;
125                 goto bad;
126         }
127
128         /*
129          * Add a record of what we've done or what needs to be done to the
130          * packet.
131          */
132         mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
133                          sizeof(struct tdb_ident), M_NOWAIT);
134         if (mtag == NULL) {
135                 DPRINTF(("ipsec_process_done: could not get packet tag\n"));
136                 error = ENOMEM;
137                 goto bad;
138         }
139
140         tdbi = (struct tdb_ident *)m_tag_data(mtag);
141         tdbi->dst = saidx->dst;
142         tdbi->proto = saidx->proto;
143         tdbi->spi = sav->spi;
144         m_tag_prepend(m, mtag);
145
146         /*
147          * If there's another (bundled) SA to apply, do so.
148          * Note that this puts a burden on the kernel stack size.
149          * If this is a problem we'll need to introduce a queue
150          * to set the packet on so we can unwind the stack before
151          * doing further processing.
152          */
153         if (isr->next) {
154                 newipsecstat.ips_out_bundlesa++;
155                 return ipsec4_process_packet(m, isr->next, 0, 0);
156         }
157
158         /*
159          * We're done with IPsec processing, transmit the packet using the
160          * appropriate network protocol (IP or IPv6). SPD lookup will be
161          * performed again there.
162          */
163         switch (saidx->dst.sa.sa_family) {
164 #ifdef INET
165         struct ip *ip;
166         case AF_INET:
167                 ip = mtod(m, struct ip *);
168                 ip->ip_len = ntohs(ip->ip_len);
169                 ip->ip_off = ntohs(ip->ip_off);
170
171                 return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
172 #endif /* INET */
173 #ifdef INET6
174         case AF_INET6:
175                 /*
176                  * We don't need massage, IPv6 header fields are always in
177                  * net endian.
178                  */
179                 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
180 #endif /* INET6 */
181         }
182         panic("ipsec_process_done");
183 bad:
184         m_freem(m);
185         KEY_FREESAV(&sav);
186         return (error);
187 }
188
189 static struct ipsecrequest *
190 ipsec_nextisr(
191         struct mbuf *m,
192         struct ipsecrequest *isr,
193         int af,
194         struct secasindex *saidx,
195         int *error
196 )
197 {
198 #define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
199                             isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
200         struct secasvar *sav;
201
202         KASSERT(af == AF_INET || af == AF_INET6,
203                 ("ipsec_nextisr: invalid address family %u", af));
204 again:
205         /*
206          * Craft SA index to search for proper SA.  Note that
207          * we only fillin unspecified SA peers for transport
208          * mode; for tunnel mode they must already be filled in.
209          */
210         *saidx = isr->saidx;
211         if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
212                 /* Fillin unspecified SA peers only for transport mode */
213                 if (af == AF_INET) {
214                         struct sockaddr_in *sin;
215                         struct ip *ip = mtod(m, struct ip *);
216
217                         if (saidx->src.sa.sa_len == 0) {
218                                 sin = &saidx->src.sin;
219                                 sin->sin_len = sizeof(*sin);
220                                 sin->sin_family = AF_INET;
221                                 sin->sin_port = IPSEC_PORT_ANY;
222                                 sin->sin_addr = ip->ip_src;
223                         }
224                         if (saidx->dst.sa.sa_len == 0) {
225                                 sin = &saidx->dst.sin;
226                                 sin->sin_len = sizeof(*sin);
227                                 sin->sin_family = AF_INET;
228                                 sin->sin_port = IPSEC_PORT_ANY;
229                                 sin->sin_addr = ip->ip_dst;
230                         }
231                 } else {
232                         struct sockaddr_in6 *sin6;
233                         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
234
235                         if (saidx->src.sin6.sin6_len == 0) {
236                                 sin6 = (struct sockaddr_in6 *)&saidx->src;
237                                 sin6->sin6_len = sizeof(*sin6);
238                                 sin6->sin6_family = AF_INET6;
239                                 sin6->sin6_port = IPSEC_PORT_ANY;
240                                 sin6->sin6_addr = ip6->ip6_src;
241                                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
242                                         /* fix scope id for comparing SPD */
243                                         sin6->sin6_addr.s6_addr16[1] = 0;
244                                         sin6->sin6_scope_id =
245                                             ntohs(ip6->ip6_src.s6_addr16[1]);
246                                 }
247                         }
248                         if (saidx->dst.sin6.sin6_len == 0) {
249                                 sin6 = (struct sockaddr_in6 *)&saidx->dst;
250                                 sin6->sin6_len = sizeof(*sin6);
251                                 sin6->sin6_family = AF_INET6;
252                                 sin6->sin6_port = IPSEC_PORT_ANY;
253                                 sin6->sin6_addr = ip6->ip6_dst;
254                                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
255                                         /* fix scope id for comparing SPD */
256                                         sin6->sin6_addr.s6_addr16[1] = 0;
257                                         sin6->sin6_scope_id =
258                                             ntohs(ip6->ip6_dst.s6_addr16[1]);
259                                 }
260                         }
261                 }
262         }
263
264         /*
265          * Lookup SA and validate it.
266          */
267         *error = key_checkrequest(isr, saidx);
268         if (*error != 0) {
269                 /*
270                  * IPsec processing is required, but no SA found.
271                  * I assume that key_acquire() had been called
272                  * to get/establish the SA. Here I discard
273                  * this packet because it is responsibility for
274                  * upper layer to retransmit the packet.
275                  */
276                 newipsecstat.ips_out_nosa++;
277                 goto bad;
278         }
279         sav = isr->sav;
280         if (sav == NULL) {              /* XXX valid return */
281                 KASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
282                         ("ipsec_nextisr: no SA found, but required; level %u",
283                         ipsec_get_reqlevel(isr)));
284                 isr = isr->next;
285                 if (isr == NULL) {
286                         /*XXXstatistic??*/
287                         *error = EINVAL;                /*XXX*/
288                         return isr;
289                 }
290                 goto again;
291         }
292
293         /*
294          * Check system global policy controls.
295          */
296         if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
297             (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
298             (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
299                 DPRINTF(("ipsec_nextisr: IPsec outbound packet dropped due"
300                         " to policy (check your sysctls)\n"));
301                 IPSEC_OSTAT(espstat.esps_pdrops, ahstat.ahs_pdrops,
302                     ipcompstat.ipcomps_pdrops);
303                 *error = EHOSTUNREACH;
304                 goto bad;
305         }
306
307         /*
308          * Sanity check the SA contents for the caller
309          * before they invoke the xform output method.
310          */
311         if (sav->tdb_xform == NULL) {
312                 DPRINTF(("ipsec_nextisr: no transform for SA\n"));
313                 IPSEC_OSTAT(espstat.esps_noxform, ahstat.ahs_noxform,
314                     ipcompstat.ipcomps_noxform);
315                 *error = EHOSTUNREACH;
316                 goto bad;
317         }
318         return isr;
319 bad:
320         KASSERT(*error != 0, ("ipsec_nextisr: error return w/ no error code"));
321         return NULL;
322 #undef IPSEC_OSTAT
323 }
324
325 #ifdef INET
326 /*
327  * IPsec output logic for IPv4.
328  */
329 int
330 ipsec4_process_packet(
331         struct mbuf *m,
332         struct ipsecrequest *isr,
333         int flags,
334         int tunalready)
335 {
336         struct secasindex saidx;
337         struct secasvar *sav;
338         struct ip *ip;
339         int error, i, off;
340
341         KASSERT(m != NULL, ("ipsec4_process_packet: null mbuf"));
342         KASSERT(isr != NULL, ("ipsec4_process_packet: null isr"));
343
344         crit_enter();
345
346         isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
347         if (isr == NULL)
348                 goto bad;
349
350         sav = isr->sav;
351         if (!tunalready) {
352                 union sockaddr_union *dst = &sav->sah->saidx.dst;
353                 int setdf;
354
355                 /*
356                  * Collect IP_DF state from the outer header.
357                  */
358                 if (dst->sa.sa_family == AF_INET) {
359                         if (m->m_len < sizeof (struct ip) &&
360                             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
361                                 error = ENOBUFS;
362                                 goto bad;
363                         }
364                         ip = mtod(m, struct ip *);
365                         /* Honor system-wide control of how to handle IP_DF */
366                         switch (ip4_ipsec_dfbit) {
367                         case 0:                 /* clear in outer header */
368                         case 1:                 /* set in outer header */
369                                 setdf = ip4_ipsec_dfbit;
370                                 break;
371                         default:                /* propagate to outer header */
372                                 setdf = ntohs(ip->ip_off & IP_DF);
373                                 break;
374                         }
375                 } else {
376                         ip = NULL;              /* keep compiler happy */
377                         setdf = 0;
378                 }
379                 /* Do the appropriate encapsulation, if necessary */
380                 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
381                     dst->sa.sa_family != AF_INET ||         /* PF mismatch */
382 #if 0
383                     (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
384                     sav->tdb_xform->xf_type == XF_IP4 ||    /* ditto */
385 #endif
386                     (dst->sa.sa_family == AF_INET &&        /* Proxy */
387                      dst->sin.sin_addr.s_addr != INADDR_ANY &&
388                      dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
389                         struct mbuf *mp;
390
391                         /* Fix IPv4 header checksum and length */
392                         if (m->m_len < sizeof (struct ip) &&
393                             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
394                                 error = ENOBUFS;
395                                 goto bad;
396                         }
397                         ip = mtod(m, struct ip *);
398                         ip->ip_len = htons(m->m_pkthdr.len);
399                         ip->ip_sum = 0;
400 #ifdef _IP_VHL
401                         if (ip->ip_vhl == IP_VHL_BORING)
402                                 ip->ip_sum = in_cksum_hdr(ip);
403                         else
404                                 ip->ip_sum = in_cksum(m,
405                                         _IP_VHL_HL(ip->ip_vhl) << 2);
406 #else
407                         ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
408 #endif
409
410                         /* Encapsulate the packet */
411                         error = ipip_output(m, isr, &mp, 0, 0);
412                         if (mp == NULL && !error) {
413                                 /* Should never happen. */
414                                 DPRINTF(("ipsec4_process_packet: ipip_output "
415                                         "returns no mbuf and no error!"));
416                                 error = EFAULT;
417                         }
418                         if (error) {
419                                 if (mp)
420                                         m_freem(mp);
421                                 goto bad;
422                         }
423                         m = mp, mp = NULL;
424                         /*
425                          * ipip_output clears IP_DF in the new header.  If
426                          * we need to propagate IP_DF from the outer header,
427                          * then we have to do it here.
428                          *
429                          * XXX shouldn't assume what ipip_output does.
430                          */
431                         if (dst->sa.sa_family == AF_INET && setdf) {
432                                 if (m->m_len < sizeof (struct ip) &&
433                                     (m = m_pullup(m, sizeof (struct ip))) == NULL) {
434                                         error = ENOBUFS;
435                                         goto bad;
436                                 }
437                                 ip = mtod(m, struct ip *);
438                                 ip->ip_off = ntohs(ip->ip_off);
439                                 ip->ip_off |= IP_DF;
440                                 ip->ip_off = htons(ip->ip_off);
441                         }
442                 }
443         }
444
445         /*
446          * Dispatch to the appropriate IPsec transform logic.  The
447          * packet will be returned for transmission after crypto
448          * processing, etc. are completed.  For encapsulation we
449          * bypass this call because of the explicit call done above
450          * (necessary to deal with IP_DF handling for IPv4).
451          *
452          * NB: m & sav are ``passed to caller'' who's reponsible for
453          *     for reclaiming their resources.
454          */
455         if (sav->tdb_xform->xf_type != XF_IP4) {
456                 ip = mtod(m, struct ip *);
457                 i = ip->ip_hl << 2;
458                 off = offsetof(struct ip, ip_p);
459                 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
460         } else {
461                 error = ipsec_process_done(m, isr);
462         }
463         crit_exit();
464         return error;
465 bad:
466         crit_exit();
467         if (m)
468                 m_freem(m);
469         return error;
470 }
471 #endif
472
473 #ifdef INET6
474 /*
475  * Chop IP6 header from the payload.
476  */
477 static struct mbuf *
478 ipsec6_splithdr(struct mbuf *m)
479 {
480         struct mbuf *mh;
481         struct ip6_hdr *ip6;
482         int hlen;
483
484         KASSERT(m->m_len >= sizeof (struct ip6_hdr),
485                 ("ipsec6_splithdr: first mbuf too short, len %u", m->m_len));
486         ip6 = mtod(m, struct ip6_hdr *);
487         hlen = sizeof(struct ip6_hdr);
488         if (m->m_len > hlen) {
489                 MGETHDR(mh, M_NOWAIT, MT_HEADER);
490                 if (!mh) {
491                         m_freem(m);
492                         return NULL;
493                 }
494                 M_MOVE_PKTHDR(mh, m);
495                 MH_ALIGN(mh, hlen);
496                 m->m_len -= hlen;
497                 m->m_data += hlen;
498                 mh->m_next = m;
499                 m = mh;
500                 m->m_len = hlen;
501                 bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
502         } else if (m->m_len < hlen) {
503                 m = m_pullup(m, hlen);
504                 if (!m)
505                         return NULL;
506         }
507         return m;
508 }
509
510 /*
511  * IPsec output logic for IPv6, transport mode.
512  */
513 int
514 ipsec6_output_trans(
515         struct ipsec_output_state *state,
516         u_char *nexthdrp,
517         struct mbuf *mprev,
518         struct secpolicy *sp,
519         int flags,
520         int *tun)
521 {
522         struct ipsecrequest *isr;
523         struct secasindex saidx;
524         int error = 0;
525         struct mbuf *m;
526
527         KASSERT(state != NULL, ("ipsec6_output: null state"));
528         KASSERT(state->m != NULL, ("ipsec6_output: null m"));
529         KASSERT(nexthdrp != NULL, ("ipsec6_output: null nexthdrp"));
530         KASSERT(mprev != NULL, ("ipsec6_output: null mprev"));
531         KASSERT(sp != NULL, ("ipsec6_output: null sp"));
532         KASSERT(tun != NULL, ("ipsec6_output: null tun"));
533
534         KEYDEBUG(KEYDEBUG_IPSEC_DATA,
535                 kprintf("ipsec6_output_trans: applyed SP\n");
536                 kdebug_secpolicy(sp));
537
538         isr = sp->req;
539         if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
540                 /* the rest will be handled by ipsec6_output_tunnel() */
541                 *tun = 1;               /* need tunnel-mode processing */
542                 return 0;
543         }
544
545         *tun = 0;
546         m = state->m;
547
548         isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
549         if (isr == NULL) {
550 #ifdef notdef
551                 /* XXX should notification be done for all errors ? */
552                 /*
553                  * Notify the fact that the packet is discarded
554                  * to ourselves. I believe this is better than
555                  * just silently discarding. (jinmei@kame.net)
556                  * XXX: should we restrict the error to TCP packets?
557                  * XXX: should we directly notify sockets via
558                  *      kpfctlinputs?
559                  */
560                 icmp6_error(m, ICMP6_DST_UNREACH,
561                             ICMP6_DST_UNREACH_ADMIN, 0);
562                 m = NULL;       /* NB: icmp6_error frees mbuf */
563 #endif
564                 goto bad;
565         }
566
567         return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
568                 sizeof (struct ip6_hdr),
569                 offsetof(struct ip6_hdr, ip6_nxt));
570 bad:
571         if (m)
572                 m_freem(m);
573         state->m = NULL;
574         return error;
575 }
576
577 static int
578 ipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
579 {
580         struct ip6_hdr *oip6;
581         struct ip6_hdr *ip6;
582         size_t plen;
583
584         /* can't tunnel between different AFs */
585         if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
586             sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
587                 m_freem(m);
588                 return EINVAL;
589         }
590         KASSERT(m->m_len != sizeof (struct ip6_hdr),
591                 ("ipsec6_encapsulate: mbuf wrong size; len %u", m->m_len));
592
593
594         /*
595          * grow the mbuf to accomodate the new IPv6 header.
596          */
597         plen = m->m_pkthdr.len;
598         if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
599                 struct mbuf *n;
600                 MGET(n, M_NOWAIT, MT_DATA);
601                 if (!n) {
602                         m_freem(m);
603                         return ENOBUFS;
604                 }
605                 n->m_len = sizeof(struct ip6_hdr);
606                 n->m_next = m->m_next;
607                 m->m_next = n;
608                 m->m_pkthdr.len += sizeof(struct ip6_hdr);
609                 oip6 = mtod(n, struct ip6_hdr *);
610         } else {
611                 m->m_next->m_len += sizeof(struct ip6_hdr);
612                 m->m_next->m_data -= sizeof(struct ip6_hdr);
613                 m->m_pkthdr.len += sizeof(struct ip6_hdr);
614                 oip6 = mtod(m->m_next, struct ip6_hdr *);
615         }
616         ip6 = mtod(m, struct ip6_hdr *);
617         ovbcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
618
619         /* Fake link-local scope-class addresses */
620         if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
621                 oip6->ip6_src.s6_addr16[1] = 0;
622         if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
623                 oip6->ip6_dst.s6_addr16[1] = 0;
624
625         /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
626         /* ECN consideration. */
627         ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
628         if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
629                 ip6->ip6_plen = htons(plen);
630         else {
631                 /* ip6->ip6_plen will be updated in ip6_output() */
632         }
633         ip6->ip6_nxt = IPPROTO_IPV6;
634         sav->sah->saidx.src.sin6.sin6_addr = ip6->ip6_src;
635         sav->sah->saidx.dst.sin6.sin6_addr = ip6->ip6_dst;
636         ip6->ip6_hlim = IPV6_DEFHLIM;
637
638         /* XXX Should ip6_src be updated later ? */
639
640         return 0;
641 }
642
643 /*
644  * IPsec output logic for IPv6, tunnel mode.
645  */
646 int
647 ipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
648 {
649         struct ip6_hdr *ip6;
650         struct ipsecrequest *isr;
651         struct secasindex saidx;
652         int error;
653         struct sockaddr_in6* dst6;
654         struct mbuf *m;
655
656         KASSERT(state != NULL, ("ipsec6_output: null state"));
657         KASSERT(state->m != NULL, ("ipsec6_output: null m"));
658         KASSERT(sp != NULL, ("ipsec6_output: null sp"));
659
660         KEYDEBUG(KEYDEBUG_IPSEC_DATA,
661                 kprintf("ipsec6_output_tunnel: applyed SP\n");
662                 kdebug_secpolicy(sp));
663
664         m = state->m;
665         /*
666          * transport mode ipsec (before the 1st tunnel mode) is already
667          * processed by ipsec6_output_trans().
668          */
669         for (isr = sp->req; isr; isr = isr->next) {
670                 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
671                         break;
672         }
673         isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
674         if (isr == NULL)
675                 goto bad;
676
677         /*
678          * There may be the case that SA status will be changed when
679          * we are refering to one. So calling splsoftnet().
680          */
681         if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
682                 /*
683                  * build IPsec tunnel.
684                  */
685                 /* XXX should be processed with other familiy */
686                 if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
687                         ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
688                             "family mismatched between inner and outer, spi=%u\n",
689                             ntohl(isr->sav->spi)));
690                         newipsecstat.ips_out_inval++;
691                         error = EAFNOSUPPORT;
692                         goto bad;
693                 }
694
695                 m = ipsec6_splithdr(m);
696                 if (!m) {
697                         newipsecstat.ips_out_nomem++;
698                         error = ENOMEM;
699                         goto bad;
700                 }
701                 error = ipsec6_encapsulate(m, isr->sav);
702                 if (error) {
703                         m = NULL;
704                         goto bad;
705                 }
706                 ip6 = mtod(m, struct ip6_hdr *);
707
708                 state->ro = &isr->sav->sah->sa_route;
709                 state->dst = (struct sockaddr *)&state->ro->ro_dst;
710                 dst6 = (struct sockaddr_in6 *)state->dst;
711                 if (state->ro->ro_rt
712                  && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
713                   || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
714                         RTFREE(state->ro->ro_rt);
715                         state->ro->ro_rt = NULL;
716                 }
717                 if (state->ro->ro_rt == 0) {
718                         bzero(dst6, sizeof(*dst6));
719                         dst6->sin6_family = AF_INET6;
720                         dst6->sin6_len = sizeof(*dst6);
721                         dst6->sin6_addr = ip6->ip6_dst;
722                         rtalloc(state->ro);
723                 }
724                 if (state->ro->ro_rt == 0) {
725                         ip6stat.ip6s_noroute++;
726                         newipsecstat.ips_out_noroute++;
727                         error = EHOSTUNREACH;
728                         goto bad;
729                 }
730
731                 /* adjust state->dst if tunnel endpoint is offlink */
732                 if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
733                         state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
734                         dst6 = (struct sockaddr_in6 *)state->dst;
735                 }
736         }
737
738         m = ipsec6_splithdr(m);
739         if (!m) {
740                 newipsecstat.ips_out_nomem++;
741                 error = ENOMEM;
742                 goto bad;
743         }
744         ip6 = mtod(m, struct ip6_hdr *);
745         return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
746                 sizeof (struct ip6_hdr),
747                 offsetof(struct ip6_hdr, ip6_nxt));
748 bad:
749         if (m)
750                 m_freem(m);
751         state->m = NULL;
752         return error;
753 }
754 #endif /*INET6*/