ipsec_output.c revision 1.52 1 /* $NetBSD: ipsec_output.c,v 1.52 2017/07/13 01:22:44 ozaki-r 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.52 2017/07/13 01:22:44 ozaki-r Exp $");
33
34 /*
35 * IPsec output processing.
36 */
37 #if defined(_KERNEL_OPT)
38 #include "opt_inet.h"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/errno.h>
48 #include <sys/syslog.h>
49
50 #include <net/if.h>
51 #include <net/route.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip_ecn.h>
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 #include <netinet/udp.h>
69
70 #include <netipsec/ipsec.h>
71 #include <netipsec/ipsec_var.h>
72 #include <netipsec/ipsec_private.h>
73 #ifdef INET6
74 #include <netipsec/ipsec6.h>
75 #endif
76 #include <netipsec/ah_var.h>
77 #include <netipsec/esp_var.h>
78 #include <netipsec/ipcomp_var.h>
79
80 #include <netipsec/xform.h>
81
82 #include <netipsec/key.h>
83 #include <netipsec/keydb.h>
84 #include <netipsec/key_debug.h>
85
86 #include <net/net_osdep.h> /* ovbcopy() in ipsec6_encapsulate() */
87
88
89 /*
90 * Add a IPSEC_OUT_DONE tag to mark that we have finished the ipsec processing
91 * It will be used by ip{,6}_output to check if we have already or not
92 * processed this packet.
93 */
94 static int
95 ipsec_register_done(struct mbuf *m, int * error)
96 {
97 struct m_tag *mtag;
98
99 mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 0, M_NOWAIT);
100 if (mtag == NULL) {
101 IPSECLOG(LOG_DEBUG, "could not get packet tag\n");
102 *error = ENOMEM;
103 return -1;
104 }
105
106 m_tag_prepend(m, mtag);
107 return 0;
108 }
109
110 static int
111 ipsec_reinject_ipstack(struct mbuf *m, int af)
112 {
113 #if defined(INET) || defined(INET6)
114 int rv;
115 #endif
116
117 switch (af) {
118 #ifdef INET
119 case AF_INET:
120 KERNEL_LOCK(1, NULL);
121 rv = ip_output(m, NULL, NULL, IP_RAWOUTPUT|IP_NOIPNEWID,
122 NULL, NULL);
123 KERNEL_UNLOCK_ONE(NULL);
124 return rv;
125
126 #endif /* INET */
127 #ifdef INET6
128 case AF_INET6:
129 /*
130 * We don't need massage, IPv6 header fields are always in
131 * net endian.
132 */
133 KERNEL_LOCK(1, NULL);
134 rv = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
135 KERNEL_UNLOCK_ONE(NULL);
136 return rv;
137 #endif /* INET6 */
138 }
139
140 panic("ipsec_reinject_ipstack : iunknown protocol family %u\n", af);
141 return -1; /* NOTREACHED */
142 }
143
144 int
145 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
146 {
147 struct secasvar *sav;
148 struct secasindex *saidx;
149 int error;
150 #ifdef INET
151 struct ip * ip;
152 #endif /* INET */
153 #ifdef INET6
154 struct ip6_hdr * ip6;
155 #endif /* INET6 */
156 struct mbuf * mo;
157 struct udphdr *udp = NULL;
158 uint64_t * data = NULL;
159 int hlen, roff;
160
161 IPSEC_SPLASSERT_SOFTNET("ipsec_process_done");
162
163 KASSERT(m != NULL);
164 KASSERT(isr != NULL);
165 sav = isr->sav;
166 KASSERT(sav != NULL);
167
168 saidx = &sav->sah->saidx;
169
170 if(sav->natt_type != 0) {
171 ip = mtod(m, struct ip *);
172
173 hlen = sizeof(struct udphdr);
174 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
175 hlen += sizeof(uint64_t);
176
177 mo = m_makespace(m, sizeof(struct ip), hlen, &roff);
178 if (mo == NULL) {
179 char buf[IPSEC_ADDRSTRLEN];
180 IPSECLOG(LOG_DEBUG,
181 "failed to inject %u byte UDP for SA %s/%08lx\n",
182 hlen, ipsec_address(&saidx->dst, buf, sizeof(buf)),
183 (u_long) ntohl(sav->spi));
184 error = ENOBUFS;
185 goto bad;
186 }
187
188 udp = (struct udphdr*) (mtod(mo, char*) + roff);
189 data = (uint64_t*) (udp + 1);
190
191 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
192 *data = 0; /* NON-IKE Marker */
193
194 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
195 udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
196 else
197 udp->uh_sport = key_portfromsaddr(&saidx->src);
198
199 udp->uh_dport = key_portfromsaddr(&saidx->dst);
200 udp->uh_sum = 0;
201 udp->uh_ulen = htons(m->m_pkthdr.len - (ip->ip_hl << 2));
202 }
203
204 switch (saidx->dst.sa.sa_family) {
205 #ifdef INET
206 case AF_INET:
207 /* Fix the header length, for AH processing. */
208 ip = mtod(m, struct ip *);
209 ip->ip_len = htons(m->m_pkthdr.len);
210 if (sav->natt_type != 0)
211 ip->ip_p = IPPROTO_UDP;
212 break;
213 #endif /* INET */
214 #ifdef INET6
215 case AF_INET6:
216 /* Fix the header length, for AH processing. */
217 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
218 error = ENXIO;
219 goto bad;
220 }
221 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
222 /* No jumbogram support. */
223 error = ENXIO; /*?*/
224 goto bad;
225 }
226 ip6 = mtod(m, struct ip6_hdr *);
227 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
228 if (sav->natt_type != 0)
229 ip6->ip6_nxt = IPPROTO_UDP;
230 break;
231 #endif /* INET6 */
232 default:
233 IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
234 saidx->dst.sa.sa_family);
235 error = ENXIO;
236 goto bad;
237 }
238
239 key_sa_recordxfer(sav, m);
240
241 /*
242 * If there's another (bundled) SA to apply, do so.
243 * Note that this puts a burden on the kernel stack size.
244 * If this is a problem we'll need to introduce a queue
245 * to set the packet on so we can unwind the stack before
246 * doing further processing.
247 */
248 if (isr->next) {
249 IPSEC_STATINC(IPSEC_STAT_OUT_BUNDLESA);
250 switch ( saidx->dst.sa.sa_family ) {
251 #ifdef INET
252 case AF_INET:
253 return ipsec4_process_packet(m, isr->next);
254 #endif /* INET */
255 #ifdef INET6
256 case AF_INET6:
257 return ipsec6_process_packet(m,isr->next);
258 #endif /* INET6 */
259 default :
260 IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
261 saidx->dst.sa.sa_family);
262 error = ENXIO;
263 goto bad;
264 }
265 }
266
267 /*
268 * We're done with IPsec processing,
269 * mark that we have already processed the packet
270 * transmit it packet using the appropriate network protocol (IP or IPv6).
271 */
272
273 if (ipsec_register_done(m, &error) < 0)
274 goto bad;
275
276 return ipsec_reinject_ipstack(m, saidx->dst.sa.sa_family);
277 bad:
278 m_freem(m);
279 KEY_FREESAV(&sav);
280 return (error);
281 }
282
283 /*
284 * ipsec_nextisr can return :
285 * - isr == NULL and error != 0 => something is bad : the packet must be
286 * discarded
287 * - isr == NULL and error == 0 => no more rules to apply, ipsec processing
288 * is done, reinject it in ip stack
289 * - isr != NULL (error == 0) => we need to apply one rule to the packet
290 */
291 static struct ipsecrequest *
292 ipsec_nextisr(
293 struct mbuf *m,
294 struct ipsecrequest *isr,
295 int af,
296 int *error
297 )
298 {
299 #define IPSEC_OSTAT(type) \
300 do { \
301 switch (isr->saidx.proto) { \
302 case IPPROTO_ESP: \
303 ESP_STATINC(ESP_STAT_ ## type); \
304 break; \
305 case IPPROTO_AH: \
306 AH_STATINC(AH_STAT_ ## type); \
307 break; \
308 default: \
309 IPCOMP_STATINC(IPCOMP_STAT_ ## type); \
310 break; \
311 } \
312 } while (/*CONSTCOND*/0)
313
314 struct secasvar *sav;
315 struct secasindex *saidx;
316
317 IPSEC_SPLASSERT_SOFTNET("ipsec_nextisr");
318 KASSERTMSG(af == AF_INET || af == AF_INET6,
319 "invalid address family %u", af);
320 again:
321 /*
322 * Craft SA index to search for proper SA. Note that
323 * we only fillin unspecified SA peers for transport
324 * mode; for tunnel mode they must already be filled in.
325 */
326 saidx = &isr->saidx;
327 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
328 /* Fillin unspecified SA peers only for transport mode */
329 if (af == AF_INET) {
330 struct sockaddr_in *sin;
331 struct ip *ip = mtod(m, struct ip *);
332
333 if (saidx->src.sa.sa_len == 0) {
334 sin = &saidx->src.sin;
335 sin->sin_len = sizeof(*sin);
336 sin->sin_family = AF_INET;
337 sin->sin_port = IPSEC_PORT_ANY;
338 sin->sin_addr = ip->ip_src;
339 }
340 if (saidx->dst.sa.sa_len == 0) {
341 sin = &saidx->dst.sin;
342 sin->sin_len = sizeof(*sin);
343 sin->sin_family = AF_INET;
344 sin->sin_port = IPSEC_PORT_ANY;
345 sin->sin_addr = ip->ip_dst;
346 }
347 } else {
348 struct sockaddr_in6 *sin6;
349 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
350
351 if (saidx->src.sin6.sin6_len == 0) {
352 sin6 = (struct sockaddr_in6 *)&saidx->src;
353 sin6->sin6_len = sizeof(*sin6);
354 sin6->sin6_family = AF_INET6;
355 sin6->sin6_port = IPSEC_PORT_ANY;
356 sin6->sin6_addr = ip6->ip6_src;
357 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
358 /* fix scope id for comparing SPD */
359 sin6->sin6_addr.s6_addr16[1] = 0;
360 sin6->sin6_scope_id =
361 ntohs(ip6->ip6_src.s6_addr16[1]);
362 }
363 }
364 if (saidx->dst.sin6.sin6_len == 0) {
365 sin6 = (struct sockaddr_in6 *)&saidx->dst;
366 sin6->sin6_len = sizeof(*sin6);
367 sin6->sin6_family = AF_INET6;
368 sin6->sin6_port = IPSEC_PORT_ANY;
369 sin6->sin6_addr = ip6->ip6_dst;
370 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
371 /* fix scope id for comparing SPD */
372 sin6->sin6_addr.s6_addr16[1] = 0;
373 sin6->sin6_scope_id =
374 ntohs(ip6->ip6_dst.s6_addr16[1]);
375 }
376 }
377 }
378 }
379
380 /*
381 * Lookup SA and validate it.
382 */
383 *error = key_checkrequest(isr);
384 if (*error != 0) {
385 /*
386 * IPsec processing is required, but no SA found.
387 * I assume that key_acquire() had been called
388 * to get/establish the SA. Here I discard
389 * this packet because it is responsibility for
390 * upper layer to retransmit the packet.
391 */
392 IPSEC_STATINC(IPSEC_STAT_OUT_NOSA);
393 goto bad;
394 }
395 sav = isr->sav;
396 /* sav may be NULL here if we have an USE rule */
397 if (sav == NULL) {
398 KASSERTMSG(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
399 "no SA found, but required; level %u",
400 ipsec_get_reqlevel(isr));
401 isr = isr->next;
402 /*
403 * No more rules to apply, return NULL isr and no error
404 * It can happen when the last rules are USE rules
405 * */
406 if (isr == NULL) {
407 *error = 0;
408 return isr;
409 }
410 goto again;
411 }
412
413 /*
414 * Check system global policy controls.
415 */
416 if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
417 (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
418 (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
419 IPSECLOG(LOG_DEBUG, "IPsec outbound packet dropped due"
420 " to policy (check your sysctls)\n");
421 IPSEC_OSTAT(PDROPS);
422 *error = EHOSTUNREACH;
423 goto bad;
424 }
425
426 /*
427 * Sanity check the SA contents for the caller
428 * before they invoke the xform output method.
429 */
430 KASSERT(sav->tdb_xform != NULL);
431 return isr;
432 bad:
433 KASSERTMSG(*error != 0, "error return w/ no error code");
434 return NULL;
435 #undef IPSEC_OSTAT
436 }
437
438 #ifdef INET
439 /*
440 * IPsec output logic for IPv4.
441 */
442 int
443 ipsec4_process_packet(struct mbuf *m, struct ipsecrequest *isr)
444 {
445 struct secasvar *sav;
446 struct ip *ip;
447 int s, error, i, off;
448 union sockaddr_union *dst;
449 int setdf;
450
451 KASSERT(m != NULL);
452 KASSERT(isr != NULL);
453
454 s = splsoftnet(); /* insure SA contents don't change */
455
456 isr = ipsec_nextisr(m, isr, AF_INET, &error);
457 if (isr == NULL) {
458 if (error != 0) {
459 goto bad;
460 } else {
461 if (ipsec_register_done(m, &error) < 0)
462 goto bad;
463
464 splx(s);
465 return ipsec_reinject_ipstack(m, AF_INET);
466 }
467 }
468
469 sav = isr->sav;
470 dst = &sav->sah->saidx.dst;
471
472 /*
473 * Collect IP_DF state from the outer header.
474 */
475 if (dst->sa.sa_family == AF_INET) {
476 if (m->m_len < sizeof (struct ip) &&
477 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
478 error = ENOBUFS;
479 goto bad;
480 }
481 ip = mtod(m, struct ip *);
482 /* Honor system-wide control of how to handle IP_DF */
483 switch (ip4_ipsec_dfbit) {
484 case 0: /* clear in outer header */
485 case 1: /* set in outer header */
486 setdf = ip4_ipsec_dfbit;
487 break;
488 default: /* propagate to outer header */
489 setdf = ip->ip_off;
490 setdf = ntohs(setdf);
491 setdf = htons(setdf & IP_DF);
492 break;
493 }
494 } else {
495 ip = NULL; /* keep compiler happy */
496 setdf = 0;
497 }
498 /* Do the appropriate encapsulation, if necessary */
499 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
500 dst->sa.sa_family != AF_INET || /* PF mismatch */
501 #if 0
502 (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
503 sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
504 #endif
505 (dst->sa.sa_family == AF_INET && /* Proxy */
506 dst->sin.sin_addr.s_addr != INADDR_ANY &&
507 dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
508 struct mbuf *mp;
509
510 /* Fix IPv4 header checksum and length */
511 if (m->m_len < sizeof (struct ip) &&
512 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
513 error = ENOBUFS;
514 goto bad;
515 }
516 ip = mtod(m, struct ip *);
517 ip->ip_len = htons(m->m_pkthdr.len);
518 ip->ip_sum = 0;
519 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
520
521 /* Encapsulate the packet */
522 error = ipip_output(m, isr, &mp, 0, 0);
523 if (mp == NULL && !error) {
524 /* Should never happen. */
525 IPSECLOG(LOG_DEBUG,
526 "ipip_output returns no mbuf and no error!");
527 error = EFAULT;
528 }
529 if (error) {
530 if (mp) {
531 /* XXX: Should never happen! */
532 m_freem(mp);
533 }
534 m = NULL; /* ipip_output() already freed it */
535 goto bad;
536 }
537 m = mp, mp = NULL;
538 /*
539 * ipip_output clears IP_DF in the new header. If
540 * we need to propagate IP_DF from the outer header,
541 * then we have to do it here.
542 *
543 * XXX shouldn't assume what ipip_output does.
544 */
545 if (dst->sa.sa_family == AF_INET && setdf) {
546 if (m->m_len < sizeof (struct ip) &&
547 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
548 error = ENOBUFS;
549 goto bad;
550 }
551 ip = mtod(m, struct ip *);
552 ip->ip_off |= htons(IP_DF);
553 }
554 }
555
556 /*
557 * Dispatch to the appropriate IPsec transform logic. The
558 * packet will be returned for transmission after crypto
559 * processing, etc. are completed. For encapsulation we
560 * bypass this call because of the explicit call done above
561 * (necessary to deal with IP_DF handling for IPv4).
562 *
563 * NB: m & sav are ``passed to caller'' who's reponsible for
564 * for reclaiming their resources.
565 */
566 if (sav->tdb_xform->xf_type != XF_IP4) {
567 if (dst->sa.sa_family == AF_INET) {
568 ip = mtod(m, struct ip *);
569 i = ip->ip_hl << 2;
570 off = offsetof(struct ip, ip_p);
571 } else {
572 i = sizeof(struct ip6_hdr);
573 off = offsetof(struct ip6_hdr, ip6_nxt);
574 }
575 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
576 } else {
577 error = ipsec_process_done(m, isr);
578 }
579 splx(s);
580 return error;
581 bad:
582 splx(s);
583 if (m)
584 m_freem(m);
585 return error;
586 }
587 #endif
588
589 #ifdef INET6
590 static void
591 compute_ipsec_pos(struct mbuf *m, int *i, int *off)
592 {
593 int nxt;
594 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr*);
595 struct ip6_ext ip6e;
596 int dstopt = 0;
597
598 *i = sizeof(struct ip6_hdr);
599 *off = offsetof(struct ip6_hdr, ip6_nxt);
600 nxt = ip6->ip6_nxt;
601
602 /*
603 * chase mbuf chain to find the appropriate place to
604 * put AH/ESP/IPcomp header.
605 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
606 */
607 do {
608 switch (nxt) {
609 case IPPROTO_AH:
610 case IPPROTO_ESP:
611 case IPPROTO_IPCOMP:
612 /*
613 * we should not skip security header added
614 * beforehand.
615 */
616 return;
617
618 case IPPROTO_HOPOPTS:
619 case IPPROTO_DSTOPTS:
620 case IPPROTO_ROUTING:
621 /*
622 * if we see 2nd destination option header,
623 * we should stop there.
624 */
625 if (nxt == IPPROTO_DSTOPTS && dstopt)
626 return;
627
628 if (nxt == IPPROTO_DSTOPTS) {
629 /*
630 * seen 1st or 2nd destination option.
631 * next time we see one, it must be 2nd.
632 */
633 dstopt = 1;
634 } else if (nxt == IPPROTO_ROUTING) {
635 /*
636 * if we see destionation option next
637 * time, it must be dest2.
638 */
639 dstopt = 2;
640 }
641
642 /* skip this header */
643 m_copydata(m, *i, sizeof(ip6e), &ip6e);
644 nxt = ip6e.ip6e_nxt;
645 *off = *i + offsetof(struct ip6_ext, ip6e_nxt);
646 /*
647 * we will never see nxt == IPPROTO_AH
648 * so it is safe to omit AH case.
649 */
650 *i += (ip6e.ip6e_len + 1) << 3;
651 break;
652 default:
653 return;
654 }
655 } while (*i < m->m_pkthdr.len);
656 }
657
658 static int
659 in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
660 {
661 struct in6_addr ia2;
662
663 memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
664 if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
665 ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
666
667 return IN6_ARE_ADDR_EQUAL(ia, &ia2);
668 }
669
670 int
671 ipsec6_process_packet(
672 struct mbuf *m,
673 struct ipsecrequest *isr
674 )
675 {
676 struct secasvar *sav;
677 struct ip6_hdr *ip6;
678 int s, error, i, off;
679 union sockaddr_union *dst;
680
681 KASSERT(m != NULL);
682 KASSERT(isr != NULL);
683
684 s = splsoftnet(); /* insure SA contents don't change */
685
686 isr = ipsec_nextisr(m, isr, AF_INET6, &error);
687 if (isr == NULL) {
688 if (error != 0) {
689 /* XXX Should we send a notification ? */
690 goto bad;
691 } else {
692 if (ipsec_register_done(m, &error) < 0)
693 goto bad;
694
695 splx(s);
696 return ipsec_reinject_ipstack(m, AF_INET6);
697 }
698 }
699
700 sav = isr->sav;
701 dst = &sav->sah->saidx.dst;
702
703 ip6 = mtod(m, struct ip6_hdr *); /* XXX */
704
705 /* Do the appropriate encapsulation, if necessary */
706 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
707 dst->sa.sa_family != AF_INET6 || /* PF mismatch */
708 ((dst->sa.sa_family == AF_INET6) &&
709 (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
710 (!in6_sa_equal_addrwithscope(&dst->sin6,
711 &ip6->ip6_dst)))) {
712 struct mbuf *mp;
713
714 /* Fix IPv6 header payload length. */
715 if (m->m_len < sizeof(struct ip6_hdr))
716 if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL)
717 return ENOBUFS;
718
719 if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
720 /* No jumbogram support. */
721 m_freem(m);
722 return ENXIO; /*XXX*/
723 }
724
725 ip6 = mtod(m, struct ip6_hdr *);
726 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
727
728 /* Encapsulate the packet */
729 error = ipip_output(m, isr, &mp, 0, 0);
730 if (mp == NULL && !error) {
731 /* Should never happen. */
732 IPSECLOG(LOG_DEBUG,
733 "ipip_output returns no mbuf and no error!");
734 error = EFAULT;
735 }
736
737 if (error) {
738 if (mp) {
739 /* XXX: Should never happen! */
740 m_freem(mp);
741 }
742 m = NULL; /* ipip_output() already freed it */
743 goto bad;
744 }
745
746 m = mp;
747 mp = NULL;
748 }
749
750 if (dst->sa.sa_family == AF_INET) {
751 struct ip *ip;
752 ip = mtod(m, struct ip *);
753 i = ip->ip_hl << 2;
754 off = offsetof(struct ip, ip_p);
755 } else {
756 compute_ipsec_pos(m, &i, &off);
757 }
758 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
759 splx(s);
760 return error;
761 bad:
762 splx(s);
763 if (m)
764 m_freem(m);
765 return error;
766 }
767 #endif /*INET6*/
768