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