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: releng/9.2/sys/netipsec/ipsec_output.c 252693 2013-07-04 08:59:34Z ae $
27 */
28
29 /*
30 * IPsec output processing.
31 */
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_ipsec.h"
35 #include "opt_enc.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
46 #include <net/if.h>
47 #include <net/pfil.h>
48 #include <net/route.h>
49 #include <net/vnet.h>
50
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip_ecn.h>
57 #ifdef INET6
58 #include <netinet6/ip6_ecn.h>
59 #endif
60
61 #include <netinet/ip6.h>
62 #ifdef INET6
63 #include <netinet6/ip6_var.h>
64 #endif
65 #include <netinet/in_pcb.h>
66 #ifdef INET6
67 #include <netinet/icmp6.h>
68 #endif
69
70 #include <netipsec/ipsec.h>
71 #ifdef INET6
72 #include <netipsec/ipsec6.h>
73 #endif
74 #include <netipsec/ah_var.h>
75 #include <netipsec/esp_var.h>
76 #include <netipsec/ipcomp_var.h>
77
78 #include <netipsec/xform.h>
79
80 #include <netipsec/key.h>
81 #include <netipsec/keydb.h>
82 #include <netipsec/key_debug.h>
83
84 #include <machine/in_cksum.h>
85
86 #ifdef IPSEC_NAT_T
87 #include <netinet/udp.h>
88 #endif
89
90 #ifdef DEV_ENC
91 #include <net/if_enc.h>
92 #endif
93
94
95 int
96 ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
97 {
98 struct tdb_ident *tdbi;
99 struct m_tag *mtag;
100 struct secasvar *sav;
101 struct secasindex *saidx;
102 int error;
103
104 IPSEC_ASSERT(m != NULL, ("null mbuf"));
105 IPSEC_ASSERT(isr != NULL, ("null ISR"));
106 sav = isr->sav;
107 IPSEC_ASSERT(sav != NULL, ("null SA"));
108 IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
109
110 saidx = &sav->sah->saidx;
111 switch (saidx->dst.sa.sa_family) {
112 #ifdef INET
113 case AF_INET:
114 /* Fix the header length, for AH processing. */
115 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
116 break;
117 #endif /* INET */
118 #ifdef INET6
119 case AF_INET6:
120 /* Fix the header length, for AH processing. */
121 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
122 error = ENXIO;
123 goto bad;
124 }
125 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
126 /* No jumbogram support. */
127 error = ENXIO; /*?*/
128 goto bad;
129 }
130 mtod(m, struct ip6_hdr *)->ip6_plen =
131 htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
132 break;
133 #endif /* INET6 */
134 default:
135 DPRINTF(("%s: unknown protocol family %u\n", __func__,
136 saidx->dst.sa.sa_family));
137 error = ENXIO;
138 goto bad;
139 }
140
141 /*
142 * Add a record of what we've done or what needs to be done to the
143 * packet.
144 */
145 mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
146 sizeof(struct tdb_ident), M_NOWAIT);
147 if (mtag == NULL) {
148 DPRINTF(("%s: could not get packet tag\n", __func__));
149 error = ENOMEM;
150 goto bad;
151 }
152
153 tdbi = (struct tdb_ident *)(mtag + 1);
154 tdbi->dst = saidx->dst;
155 tdbi->proto = saidx->proto;
156 tdbi->spi = sav->spi;
157 m_tag_prepend(m, mtag);
158
159 /*
160 * If there's another (bundled) SA to apply, do so.
161 * Note that this puts a burden on the kernel stack size.
162 * If this is a problem we'll need to introduce a queue
163 * to set the packet on so we can unwind the stack before
164 * doing further processing.
165 */
166 if (isr->next) {
167 IPSECSTAT_INC(ips_out_bundlesa);
168 /* XXX-BZ currently only support same AF bundles. */
169 switch (saidx->dst.sa.sa_family) {
170 #ifdef INET
171 case AF_INET:
172 return ipsec4_process_packet(m, isr->next, 0, 0);
173 /* NOTREACHED */
174 #endif
175 #ifdef notyet
176 #ifdef INET6
177 case AF_INET6:
178 /* XXX */
179 ipsec6_output_trans()
180 ipsec6_output_tunnel()
181 /* NOTREACHED */
182 #endif /* INET6 */
183 #endif
184 default:
185 DPRINTF(("%s: unknown protocol family %u\n", __func__,
186 saidx->dst.sa.sa_family));
187 error = ENXIO;
188 goto bad;
189 }
190 }
191 key_sa_recordxfer(sav, m); /* record data transfer */
192
193 m_addr_changed(m);
194
195 /*
196 * We're done with IPsec processing, transmit the packet using the
197 * appropriate network protocol (IP or IPv6). SPD lookup will be
198 * performed again there.
199 */
200 switch (saidx->dst.sa.sa_family) {
201 #ifdef INET
202 struct ip *ip;
203 case AF_INET:
204 ip = mtod(m, struct ip *);
205 ip->ip_len = ntohs(ip->ip_len);
206 ip->ip_off = ntohs(ip->ip_off);
207
208 #ifdef IPSEC_NAT_T
209 /*
210 * If NAT-T is enabled, now that all IPsec processing is done
211 * insert UDP encapsulation header after IP header.
212 */
213 if (sav->natt_type) {
214 #ifdef _IP_VHL
215 const int hlen = IP_VHL_HL(ip->ip_vhl);
216 #else
217 const int hlen = (ip->ip_hl << 2);
218 #endif
219 int size, off;
220 struct mbuf *mi;
221 struct udphdr *udp;
222
223 size = sizeof(struct udphdr);
224 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
225 /*
226 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
227 * draft-ietf-ipsec-udp-encaps-(00/)01.txt,
228 * ignoring possible AH mode
229 * non-IKE marker + non-ESP marker
230 * from draft-ietf-ipsec-udp-encaps-00.txt.
231 */
232 size += sizeof(u_int64_t);
233 }
234 mi = m_makespace(m, hlen, size, &off);
235 if (mi == NULL) {
236 DPRINTF(("%s: m_makespace for udphdr failed\n",
237 __func__));
238 error = ENOBUFS;
239 goto bad;
240 }
241
242 udp = (struct udphdr *)(mtod(mi, caddr_t) + off);
243 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
244 udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
245 else
246 udp->uh_sport =
247 KEY_PORTFROMSADDR(&sav->sah->saidx.src);
248 udp->uh_dport = KEY_PORTFROMSADDR(&sav->sah->saidx.dst);
249 udp->uh_sum = 0;
250 udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
251 ip->ip_len = m->m_pkthdr.len;
252 ip->ip_p = IPPROTO_UDP;
253
254 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
255 *(u_int64_t *)(udp + 1) = 0;
256 }
257 #endif /* IPSEC_NAT_T */
258
259 return ip_output(m, NULL, NULL, IP_RAWOUTPUT, NULL, NULL);
260 #endif /* INET */
261 #ifdef INET6
262 case AF_INET6:
263 /*
264 * We don't need massage, IPv6 header fields are always in
265 * net endian.
266 */
267 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
268 #endif /* INET6 */
269 }
270 panic("ipsec_process_done");
271 bad:
272 m_freem(m);
273 return (error);
274 }
275
276 static struct ipsecrequest *
277 ipsec_nextisr(
278 struct mbuf *m,
279 struct ipsecrequest *isr,
280 int af,
281 struct secasindex *saidx,
282 int *error
283 )
284 {
285 #define IPSEC_OSTAT(name) do { \
286 if (isr->saidx.proto == IPPROTO_ESP) \
287 ESPSTAT_INC(esps_##name); \
288 else if (isr->saidx.proto == IPPROTO_AH)\
289 AHSTAT_INC(ahs_##name); \
290 else \
291 IPCOMPSTAT_INC(ipcomps_##name); \
292 } while (0)
293 struct secasvar *sav;
294
295 IPSECREQUEST_LOCK_ASSERT(isr);
296
297 IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
298 ("invalid address family %u", af));
299 again:
300 /*
301 * Craft SA index to search for proper SA. Note that
302 * we only fillin unspecified SA peers for transport
303 * mode; for tunnel mode they must already be filled in.
304 */
305 *saidx = isr->saidx;
306 if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
307 /* Fillin unspecified SA peers only for transport mode */
308 if (af == AF_INET) {
309 struct sockaddr_in *sin;
310 struct ip *ip = mtod(m, struct ip *);
311
312 if (saidx->src.sa.sa_len == 0) {
313 sin = &saidx->src.sin;
314 sin->sin_len = sizeof(*sin);
315 sin->sin_family = AF_INET;
316 sin->sin_port = IPSEC_PORT_ANY;
317 sin->sin_addr = ip->ip_src;
318 }
319 if (saidx->dst.sa.sa_len == 0) {
320 sin = &saidx->dst.sin;
321 sin->sin_len = sizeof(*sin);
322 sin->sin_family = AF_INET;
323 sin->sin_port = IPSEC_PORT_ANY;
324 sin->sin_addr = ip->ip_dst;
325 }
326 } else {
327 struct sockaddr_in6 *sin6;
328 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
329
330 if (saidx->src.sin6.sin6_len == 0) {
331 sin6 = (struct sockaddr_in6 *)&saidx->src;
332 sin6->sin6_len = sizeof(*sin6);
333 sin6->sin6_family = AF_INET6;
334 sin6->sin6_port = IPSEC_PORT_ANY;
335 sin6->sin6_addr = ip6->ip6_src;
336 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
337 /* fix scope id for comparing SPD */
338 sin6->sin6_addr.s6_addr16[1] = 0;
339 sin6->sin6_scope_id =
340 ntohs(ip6->ip6_src.s6_addr16[1]);
341 }
342 }
343 if (saidx->dst.sin6.sin6_len == 0) {
344 sin6 = (struct sockaddr_in6 *)&saidx->dst;
345 sin6->sin6_len = sizeof(*sin6);
346 sin6->sin6_family = AF_INET6;
347 sin6->sin6_port = IPSEC_PORT_ANY;
348 sin6->sin6_addr = ip6->ip6_dst;
349 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
350 /* fix scope id for comparing SPD */
351 sin6->sin6_addr.s6_addr16[1] = 0;
352 sin6->sin6_scope_id =
353 ntohs(ip6->ip6_dst.s6_addr16[1]);
354 }
355 }
356 }
357 }
358
359 /*
360 * Lookup SA and validate it.
361 */
362 *error = key_checkrequest(isr, saidx);
363 if (*error != 0) {
364 /*
365 * IPsec processing is required, but no SA found.
366 * I assume that key_acquire() had been called
367 * to get/establish the SA. Here I discard
368 * this packet because it is responsibility for
369 * upper layer to retransmit the packet.
370 */
371 IPSECSTAT_INC(ips_out_nosa);
372 goto bad;
373 }
374 sav = isr->sav;
375 if (sav == NULL) {
376 IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
377 ("no SA found, but required; level %u",
378 ipsec_get_reqlevel(isr)));
379 IPSECREQUEST_UNLOCK(isr);
380 isr = isr->next;
381 /*
382 * If isr is NULL, we found a 'use' policy w/o SA.
383 * Return w/o error and w/o isr so we can drop out
384 * and continue w/o IPsec processing.
385 */
386 if (isr == NULL)
387 return isr;
388 IPSECREQUEST_LOCK(isr);
389 goto again;
390 }
391
392 /*
393 * Check system global policy controls.
394 */
395 if ((isr->saidx.proto == IPPROTO_ESP && !V_esp_enable) ||
396 (isr->saidx.proto == IPPROTO_AH && !V_ah_enable) ||
397 (isr->saidx.proto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
398 DPRINTF(("%s: IPsec outbound packet dropped due"
399 " to policy (check your sysctls)\n", __func__));
400 IPSEC_OSTAT(pdrops);
401 *error = EHOSTUNREACH;
402 goto bad;
403 }
404
405 /*
406 * Sanity check the SA contents for the caller
407 * before they invoke the xform output method.
408 */
409 if (sav->tdb_xform == NULL) {
410 DPRINTF(("%s: no transform for SA\n", __func__));
411 IPSEC_OSTAT(noxform);
412 *error = EHOSTUNREACH;
413 goto bad;
414 }
415 return isr;
416 bad:
417 IPSEC_ASSERT(*error != 0, ("error return w/ no error code"));
418 IPSECREQUEST_UNLOCK(isr);
419 return NULL;
420 #undef IPSEC_OSTAT
421 }
422
423 #ifdef INET
424 /*
425 * IPsec output logic for IPv4.
426 */
427 int
428 ipsec4_process_packet(
429 struct mbuf *m,
430 struct ipsecrequest *isr,
431 int flags,
432 int tunalready)
433 {
434 struct secasindex saidx;
435 struct secasvar *sav;
436 struct ip *ip;
437 int error, i, off;
438
439 IPSEC_ASSERT(m != NULL, ("null mbuf"));
440 IPSEC_ASSERT(isr != NULL, ("null isr"));
441
442 IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */
443
444 isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
445 if (isr == NULL) {
446 if (error != 0)
447 goto bad;
448 return EJUSTRETURN;
449 }
450
451 sav = isr->sav;
452
453 #ifdef DEV_ENC
454 encif->if_opackets++;
455 encif->if_obytes += m->m_pkthdr.len;
456
457 /* pass the mbuf to enc0 for bpf processing */
458 ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_BEFORE);
459 /* pass the mbuf to enc0 for packet filtering */
460 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
461 goto bad;
462 #endif
463
464 if (!tunalready) {
465 union sockaddr_union *dst = &sav->sah->saidx.dst;
466 int setdf;
467
468 /*
469 * Collect IP_DF state from the outer header.
470 */
471 if (dst->sa.sa_family == AF_INET) {
472 if (m->m_len < sizeof (struct ip) &&
473 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
474 error = ENOBUFS;
475 goto bad;
476 }
477 ip = mtod(m, struct ip *);
478 /* Honor system-wide control of how to handle IP_DF */
479 switch (V_ip4_ipsec_dfbit) {
480 case 0: /* clear in outer header */
481 case 1: /* set in outer header */
482 setdf = V_ip4_ipsec_dfbit;
483 break;
484 default: /* propagate to outer header */
485 setdf = ntohs(ip->ip_off & IP_DF);
486 break;
487 }
488 } else {
489 ip = NULL; /* keep compiler happy */
490 setdf = 0;
491 }
492 /* Do the appropriate encapsulation, if necessary */
493 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
494 dst->sa.sa_family != AF_INET || /* PF mismatch */
495 #if 0
496 (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
497 sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
498 #endif
499 (dst->sa.sa_family == AF_INET && /* Proxy */
500 dst->sin.sin_addr.s_addr != INADDR_ANY &&
501 dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
502 struct mbuf *mp;
503
504 /* Fix IPv4 header checksum and length */
505 if (m->m_len < sizeof (struct ip) &&
506 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
507 error = ENOBUFS;
508 goto bad;
509 }
510 ip = mtod(m, struct ip *);
511 ip->ip_len = htons(m->m_pkthdr.len);
512 ip->ip_sum = 0;
513 #ifdef _IP_VHL
514 if (ip->ip_vhl == IP_VHL_BORING)
515 ip->ip_sum = in_cksum_hdr(ip);
516 else
517 ip->ip_sum = in_cksum(m,
518 _IP_VHL_HL(ip->ip_vhl) << 2);
519 #else
520 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
521 #endif
522
523 /* Encapsulate the packet */
524 error = ipip_output(m, isr, &mp, 0, 0);
525 if (mp == NULL && !error) {
526 /* Should never happen. */
527 DPRINTF(("%s: ipip_output returns no mbuf and "
528 "no error!", __func__));
529 error = EFAULT;
530 }
531 if (error) {
532 if (mp) {
533 /* XXX: Should never happen! */
534 m_freem(mp);
535 }
536 m = NULL; /* ipip_output() already freed it */
537 goto bad;
538 }
539 m = mp, mp = NULL;
540 /*
541 * ipip_output clears IP_DF in the new header. If
542 * we need to propagate IP_DF from the outer header,
543 * then we have to do it here.
544 *
545 * XXX shouldn't assume what ipip_output does.
546 */
547 if (dst->sa.sa_family == AF_INET && setdf) {
548 if (m->m_len < sizeof (struct ip) &&
549 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
550 error = ENOBUFS;
551 goto bad;
552 }
553 ip = mtod(m, struct ip *);
554 ip->ip_off = ntohs(ip->ip_off);
555 ip->ip_off |= IP_DF;
556 ip->ip_off = htons(ip->ip_off);
557 }
558 }
559 }
560
561 #ifdef DEV_ENC
562 /* pass the mbuf to enc0 for bpf processing */
563 ipsec_bpf(m, sav, AF_INET, ENC_OUT|ENC_AFTER);
564 /* pass the mbuf to enc0 for packet filtering */
565 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
566 goto bad;
567 #endif
568
569 /*
570 * Dispatch to the appropriate IPsec transform logic. The
571 * packet will be returned for transmission after crypto
572 * processing, etc. are completed. For encapsulation we
573 * bypass this call because of the explicit call done above
574 * (necessary to deal with IP_DF handling for IPv4).
575 *
576 * NB: m & sav are ``passed to caller'' who's reponsible for
577 * for reclaiming their resources.
578 */
579 if (sav->tdb_xform->xf_type != XF_IP4) {
580 ip = mtod(m, struct ip *);
581 i = ip->ip_hl << 2;
582 off = offsetof(struct ip, ip_p);
583 error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
584 } else {
585 error = ipsec_process_done(m, isr);
586 }
587 IPSECREQUEST_UNLOCK(isr);
588 return error;
589 bad:
590 if (isr)
591 IPSECREQUEST_UNLOCK(isr);
592 if (m)
593 m_freem(m);
594 return error;
595 }
596 #endif
597
598 #ifdef INET6
599 /*
600 * Chop IP6 header from the payload.
601 */
602 static struct mbuf *
603 ipsec6_splithdr(struct mbuf *m)
604 {
605 struct mbuf *mh;
606 struct ip6_hdr *ip6;
607 int hlen;
608
609 IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
610 ("first mbuf too short, len %u", m->m_len));
611 ip6 = mtod(m, struct ip6_hdr *);
612 hlen = sizeof(struct ip6_hdr);
613 if (m->m_len > hlen) {
614 MGETHDR(mh, M_DONTWAIT, MT_DATA);
615 if (!mh) {
616 m_freem(m);
617 return NULL;
618 }
619 M_MOVE_PKTHDR(mh, m);
620 MH_ALIGN(mh, hlen);
621 m->m_len -= hlen;
622 m->m_data += hlen;
623 mh->m_next = m;
624 m = mh;
625 m->m_len = hlen;
626 bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
627 } else if (m->m_len < hlen) {
628 m = m_pullup(m, hlen);
629 if (!m)
630 return NULL;
631 }
632 return m;
633 }
634
635 /*
636 * IPsec output logic for IPv6, transport mode.
637 */
638 int
639 ipsec6_output_trans(
640 struct ipsec_output_state *state,
641 u_char *nexthdrp,
642 struct mbuf *mprev,
643 struct secpolicy *sp,
644 int flags,
645 int *tun)
646 {
647 struct ipsecrequest *isr;
648 struct secasindex saidx;
649 int error = 0;
650 struct mbuf *m;
651
652 IPSEC_ASSERT(state != NULL, ("null state"));
653 IPSEC_ASSERT(state->m != NULL, ("null m"));
654 IPSEC_ASSERT(nexthdrp != NULL, ("null nexthdrp"));
655 IPSEC_ASSERT(mprev != NULL, ("null mprev"));
656 IPSEC_ASSERT(sp != NULL, ("null sp"));
657 IPSEC_ASSERT(tun != NULL, ("null tun"));
658
659 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
660 printf("%s: applied SP\n", __func__);
661 kdebug_secpolicy(sp));
662
663 isr = sp->req;
664 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
665 /* the rest will be handled by ipsec6_output_tunnel() */
666 *tun = 1; /* need tunnel-mode processing */
667 return 0;
668 }
669
670 *tun = 0;
671 m = state->m;
672
673 IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */
674 isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
675 if (isr == NULL) {
676 if (error != 0) {
677 #ifdef notdef
678 /* XXX should notification be done for all errors ? */
679 /*
680 * Notify the fact that the packet is discarded
681 * to ourselves. I believe this is better than
682 * just silently discarding. (jinmei@kame.net)
683 * XXX: should we restrict the error to TCP packets?
684 * XXX: should we directly notify sockets via
685 * pfctlinputs?
686 */
687 icmp6_error(m, ICMP6_DST_UNREACH,
688 ICMP6_DST_UNREACH_ADMIN, 0);
689 m = NULL; /* NB: icmp6_error frees mbuf */
690 #endif
691 goto bad;
692 }
693 return EJUSTRETURN;
694 }
695
696 error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
697 sizeof (struct ip6_hdr),
698 offsetof(struct ip6_hdr,
699 ip6_nxt));
700 IPSECREQUEST_UNLOCK(isr);
701 return error;
702 bad:
703 if (isr)
704 IPSECREQUEST_UNLOCK(isr);
705 if (m)
706 m_freem(m);
707 state->m = NULL;
708 return error;
709 }
710
711 static int
712 ipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
713 {
714 struct ip6_hdr *oip6;
715 struct ip6_hdr *ip6;
716 size_t plen;
717
718 /* can't tunnel between different AFs */
719 if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
720 sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
721 m_freem(m);
722 return EINVAL;
723 }
724 IPSEC_ASSERT(m->m_len == sizeof (struct ip6_hdr),
725 ("mbuf wrong size; len %u", m->m_len));
726
727
728 /*
729 * grow the mbuf to accomodate the new IPv6 header.
730 */
731 plen = m->m_pkthdr.len;
732 if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
733 struct mbuf *n;
734 MGET(n, M_DONTWAIT, MT_DATA);
735 if (!n) {
736 m_freem(m);
737 return ENOBUFS;
738 }
739 n->m_len = sizeof(struct ip6_hdr);
740 n->m_next = m->m_next;
741 m->m_next = n;
742 m->m_pkthdr.len += sizeof(struct ip6_hdr);
743 oip6 = mtod(n, struct ip6_hdr *);
744 } else {
745 m->m_next->m_len += sizeof(struct ip6_hdr);
746 m->m_next->m_data -= sizeof(struct ip6_hdr);
747 m->m_pkthdr.len += sizeof(struct ip6_hdr);
748 oip6 = mtod(m->m_next, struct ip6_hdr *);
749 }
750 ip6 = mtod(m, struct ip6_hdr *);
751 bcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
752
753 /* Fake link-local scope-class addresses */
754 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
755 oip6->ip6_src.s6_addr16[1] = 0;
756 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
757 oip6->ip6_dst.s6_addr16[1] = 0;
758
759 /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
760 /* ECN consideration. */
761 ip6_ecn_ingress(V_ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
762 if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
763 ip6->ip6_plen = htons(plen);
764 else {
765 /* ip6->ip6_plen will be updated in ip6_output() */
766 }
767 ip6->ip6_nxt = IPPROTO_IPV6;
768 ip6->ip6_src = sav->sah->saidx.src.sin6.sin6_addr;
769 ip6->ip6_dst = sav->sah->saidx.dst.sin6.sin6_addr;
770 ip6->ip6_hlim = IPV6_DEFHLIM;
771
772 /* XXX Should ip6_src be updated later ? */
773
774 return 0;
775 }
776
777 /*
778 * IPsec output logic for IPv6, tunnel mode.
779 */
780 int
781 ipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
782 {
783 struct ip6_hdr *ip6;
784 struct ipsecrequest *isr;
785 struct secasindex saidx;
786 int error;
787 struct sockaddr_in6 *dst6;
788 struct mbuf *m;
789
790 IPSEC_ASSERT(state != NULL, ("null state"));
791 IPSEC_ASSERT(state->m != NULL, ("null m"));
792 IPSEC_ASSERT(sp != NULL, ("null sp"));
793
794 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
795 printf("%s: applied SP\n", __func__);
796 kdebug_secpolicy(sp));
797
798 m = state->m;
799 /*
800 * transport mode ipsec (before the 1st tunnel mode) is already
801 * processed by ipsec6_output_trans().
802 */
803 for (isr = sp->req; isr; isr = isr->next) {
804 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
805 break;
806 }
807
808 IPSECREQUEST_LOCK(isr); /* insure SA contents don't change */
809 isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
810 if (isr == NULL) {
811 if (error != 0)
812 goto bad;
813 return EJUSTRETURN;
814 }
815
816 #ifdef DEV_ENC
817 encif->if_opackets++;
818 encif->if_obytes += m->m_pkthdr.len;
819
820 /* pass the mbuf to enc0 for bpf processing */
821 ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_BEFORE);
822 /* pass the mbuf to enc0 for packet filtering */
823 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_BEFORE)) != 0)
824 goto bad;
825 #endif
826
827 /*
828 * There may be the case that SA status will be changed when
829 * we are refering to one. So calling splsoftnet().
830 */
831 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
832 /*
833 * build IPsec tunnel.
834 */
835 /* XXX should be processed with other familiy */
836 if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
837 ipseclog((LOG_ERR, "%s: family mismatched between "
838 "inner and outer, spi=%u\n", __func__,
839 ntohl(isr->sav->spi)));
840 IPSEC6STAT_INC(ips_out_inval);
841 error = EAFNOSUPPORT;
842 goto bad;
843 }
844
845 m = ipsec6_splithdr(m);
846 if (!m) {
847 IPSEC6STAT_INC(ips_out_nomem);
848 error = ENOMEM;
849 goto bad;
850 }
851 error = ipsec6_encapsulate(m, isr->sav);
852 if (error) {
853 m = NULL;
854 goto bad;
855 }
856 ip6 = mtod(m, struct ip6_hdr *);
857
858 state->ro =
859 (struct route *)&isr->sav->sah->route_cache.sin6_route;
860 state->dst = (struct sockaddr *)&state->ro->ro_dst;
861 dst6 = (struct sockaddr_in6 *)state->dst;
862 if (state->ro->ro_rt
863 && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
864 || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
865 RTFREE(state->ro->ro_rt);
866 state->ro->ro_rt = NULL;
867 }
868 if (state->ro->ro_rt == NULL) {
869 bzero(dst6, sizeof(*dst6));
870 dst6->sin6_family = AF_INET6;
871 dst6->sin6_len = sizeof(*dst6);
872 dst6->sin6_addr = ip6->ip6_dst;
873 rtalloc_ign_fib(state->ro, 0UL, M_GETFIB(m));
874 }
875 if (state->ro->ro_rt == NULL) {
876 IP6STAT_INC(ip6s_noroute);
877 IPSEC6STAT_INC(ips_out_noroute);
878 error = EHOSTUNREACH;
879 goto bad;
880 }
881
882 /* adjust state->dst if tunnel endpoint is offlink */
883 if (state->ro->ro_rt->rt_flags & RTF_GATEWAY)
884 state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
885 }
886
887 m = ipsec6_splithdr(m);
888 if (!m) {
889 IPSEC6STAT_INC(ips_out_nomem);
890 error = ENOMEM;
891 goto bad;
892 }
893 ip6 = mtod(m, struct ip6_hdr *);
894
895 #ifdef DEV_ENC
896 /* pass the mbuf to enc0 for bpf processing */
897 ipsec_bpf(m, isr->sav, AF_INET6, ENC_OUT|ENC_AFTER);
898 /* pass the mbuf to enc0 for packet filtering */
899 if ((error = ipsec_filter(&m, PFIL_OUT, ENC_OUT|ENC_AFTER)) != 0)
900 goto bad;
901 #endif
902
903 error = (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
904 sizeof (struct ip6_hdr),
905 offsetof(struct ip6_hdr, ip6_nxt));
906 IPSECREQUEST_UNLOCK(isr);
907 return error;
908 bad:
909 if (isr)
910 IPSECREQUEST_UNLOCK(isr);
911 if (m)
912 m_freem(m);
913 state->m = NULL;
914 return error;
915 }
916 #endif /*INET6*/
Cache object: 3214b6421047c4a2d12e976a6f65bfe5
|