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