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