ipsec_output.c revision 1.54 1 /* $NetBSD: ipsec_output.c,v 1.54 2017/07/14 12:26:26 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.54 2017/07/14 12:26:26 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 struct secasvar *sav)
147 {
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 KASSERT(sav != NULL);
166
167 saidx = &sav->sah->saidx;
168
169 if(sav->natt_type != 0) {
170 ip = mtod(m, struct ip *);
171
172 hlen = sizeof(struct udphdr);
173 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
174 hlen += sizeof(uint64_t);
175
176 mo = m_makespace(m, sizeof(struct ip), hlen, &roff);
177 if (mo == NULL) {
178 char buf[IPSEC_ADDRSTRLEN];
179 IPSECLOG(LOG_DEBUG,
180 "failed to inject %u byte UDP for SA %s/%08lx\n",
181 hlen, ipsec_address(&saidx->dst, buf, sizeof(buf)),
182 (u_long) ntohl(sav->spi));
183 error = ENOBUFS;
184 goto bad;
185 }
186
187 udp = (struct udphdr*) (mtod(mo, char*) + roff);
188 data = (uint64_t*) (udp + 1);
189
190 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
191 *data = 0; /* NON-IKE Marker */
192
193 if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
194 udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
195 else
196 udp->uh_sport = key_portfromsaddr(&saidx->src);
197
198 udp->uh_dport = key_portfromsaddr(&saidx->dst);
199 udp->uh_sum = 0;
200 udp->uh_ulen = htons(m->m_pkthdr.len - (ip->ip_hl << 2));
201 }
202
203 switch (saidx->dst.sa.sa_family) {
204 #ifdef INET
205 case AF_INET:
206 /* Fix the header length, for AH processing. */
207 ip = mtod(m, struct ip *);
208 ip->ip_len = htons(m->m_pkthdr.len);
209 if (sav->natt_type != 0)
210 ip->ip_p = IPPROTO_UDP;
211 break;
212 #endif /* INET */
213 #ifdef INET6
214 case AF_INET6:
215 /* Fix the header length, for AH processing. */
216 if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
217 error = ENXIO;
218 goto bad;
219 }
220 if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
221 /* No jumbogram support. */
222 error = ENXIO; /*?*/
223 goto bad;
224 }
225 ip6 = mtod(m, struct ip6_hdr *);
226 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
227 if (sav->natt_type != 0)
228 ip6->ip6_nxt = IPPROTO_UDP;
229 break;
230 #endif /* INET6 */
231 default:
232 IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
233 saidx->dst.sa.sa_family);
234 error = ENXIO;
235 goto bad;
236 }
237
238 key_sa_recordxfer(sav, m);
239
240 /*
241 * If there's another (bundled) SA to apply, do so.
242 * Note that this puts a burden on the kernel stack size.
243 * If this is a problem we'll need to introduce a queue
244 * to set the packet on so we can unwind the stack before
245 * doing further processing.
246 */
247 if (isr->next) {
248 IPSEC_STATINC(IPSEC_STAT_OUT_BUNDLESA);
249 switch ( saidx->dst.sa.sa_family ) {
250 #ifdef INET
251 case AF_INET:
252 return ipsec4_process_packet(m, isr->next);
253 #endif /* INET */
254 #ifdef INET6
255 case AF_INET6:
256 return ipsec6_process_packet(m,isr->next);
257 #endif /* INET6 */
258 default :
259 IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
260 saidx->dst.sa.sa_family);
261 error = ENXIO;
262 goto bad;
263 }
264 }
265
266 /*
267 * We're done with IPsec processing,
268 * mark that we have already processed the packet
269 * transmit it packet using the appropriate network protocol (IP or IPv6).
270 */
271
272 if (ipsec_register_done(m, &error) < 0)
273 goto bad;
274
275 return ipsec_reinject_ipstack(m, saidx->dst.sa.sa_family);
276 bad:
277 m_freem(m);
278 KEY_FREESAV(&sav);
279 return (error);
280 }
281
282 /*
283 * ipsec_nextisr can return :
284 * - isr == NULL and error != 0 => something is bad : the packet must be
285 * discarded
286 * - isr == NULL and error == 0 => no more rules to apply, ipsec processing
287 * is done, reinject it in ip stack
288 * - isr != NULL (error == 0) => we need to apply one rule to the packet
289 */
290 static struct ipsecrequest *
291 ipsec_nextisr(
292 struct mbuf *m,
293 struct ipsecrequest *isr,
294 int af,
295 int *error,
296 struct secasvar **ret
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 = NULL;
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, &sav);
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 may be NULL here if we have an USE rule */
396 if (sav == NULL) {
397 KASSERTMSG(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
398 "no SA found, but required; level %u",
399 ipsec_get_reqlevel(isr));
400 isr = isr->next;
401 /*
402 * No more rules to apply, return NULL isr and no error
403 * It can happen when the last rules are USE rules
404 * */
405 if (isr == NULL) {
406 *ret = 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 KEY_FREESAV(&sav);
424 goto bad;
425 }
426
427 /*
428 * Sanity check the SA contents for the caller
429 * before they invoke the xform output method.
430 */
431 KASSERT(sav->tdb_xform != NULL);
432 *ret = sav;
433 return isr;
434 bad:
435 KASSERTMSG(*error != 0, "error return w/ no error code");
436 return NULL;
437 #undef IPSEC_OSTAT
438 }
439
440 #ifdef INET
441 /*
442 * IPsec output logic for IPv4.
443 */
444 int
445 ipsec4_process_packet(struct mbuf *m, struct ipsecrequest *isr)
446 {
447 struct secasvar *sav = NULL;
448 struct ip *ip;
449 int s, error, i, off;
450 union sockaddr_union *dst;
451 int setdf;
452
453 KASSERT(m != NULL);
454 KASSERT(isr != NULL);
455
456 s = splsoftnet(); /* insure SA contents don't change */
457
458 isr = ipsec_nextisr(m, isr, AF_INET, &error, &sav);
459 if (isr == NULL) {
460 if (error != 0) {
461 goto bad;
462 } else {
463 if (ipsec_register_done(m, &error) < 0)
464 goto bad;
465
466 splx(s);
467 return ipsec_reinject_ipstack(m, AF_INET);
468 }
469 }
470
471 KASSERT(sav != NULL);
472 dst = &sav->sah->saidx.dst;
473
474 /*
475 * Collect IP_DF state from the outer header.
476 */
477 if (dst->sa.sa_family == AF_INET) {
478 if (m->m_len < sizeof (struct ip) &&
479 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
480 error = ENOBUFS;
481 goto unrefsav;
482 }
483 ip = mtod(m, struct ip *);
484 /* Honor system-wide control of how to handle IP_DF */
485 switch (ip4_ipsec_dfbit) {
486 case 0: /* clear in outer header */
487 case 1: /* set in outer header */
488 setdf = ip4_ipsec_dfbit;
489 break;
490 default: /* propagate to outer header */
491 setdf = ip->ip_off;
492 setdf = ntohs(setdf);
493 setdf = htons(setdf & IP_DF);
494 break;
495 }
496 } else {
497 ip = NULL; /* keep compiler happy */
498 setdf = 0;
499 }
500 /* Do the appropriate encapsulation, if necessary */
501 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
502 dst->sa.sa_family != AF_INET || /* PF mismatch */
503 #if 0
504 (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
505 sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
506 #endif
507 (dst->sa.sa_family == AF_INET && /* Proxy */
508 dst->sin.sin_addr.s_addr != INADDR_ANY &&
509 dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
510 struct mbuf *mp;
511
512 /* Fix IPv4 header checksum and length */
513 if (m->m_len < sizeof (struct ip) &&
514 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
515 error = ENOBUFS;
516 goto unrefsav;
517 }
518 ip = mtod(m, struct ip *);
519 ip->ip_len = htons(m->m_pkthdr.len);
520 ip->ip_sum = 0;
521 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
522
523 /* Encapsulate the packet */
524 error = ipip_output(m, isr, sav, &mp, 0, 0);
525 if (mp == NULL && !error) {
526 /* Should never happen. */
527 IPSECLOG(LOG_DEBUG,
528 "ipip_output returns no mbuf and no error!");
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 unrefsav;
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 unrefsav;
552 }
553 ip = mtod(m, struct ip *);
554 ip->ip_off |= htons(IP_DF);
555 }
556 }
557
558 /*
559 * Dispatch to the appropriate IPsec transform logic. The
560 * packet will be returned for transmission after crypto
561 * processing, etc. are completed. For encapsulation we
562 * bypass this call because of the explicit call done above
563 * (necessary to deal with IP_DF handling for IPv4).
564 *
565 * NB: m & sav are ``passed to caller'' who's reponsible for
566 * for reclaiming their resources.
567 */
568 if (sav->tdb_xform->xf_type != XF_IP4) {
569 if (dst->sa.sa_family == AF_INET) {
570 ip = mtod(m, struct ip *);
571 i = ip->ip_hl << 2;
572 off = offsetof(struct ip, ip_p);
573 } else {
574 i = sizeof(struct ip6_hdr);
575 off = offsetof(struct ip6_hdr, ip6_nxt);
576 }
577 error = (*sav->tdb_xform->xf_output)(m, isr, sav, NULL, i, off);
578 } else {
579 error = ipsec_process_done(m, isr, sav);
580 }
581 KEY_FREESAV(&sav);
582 splx(s);
583 return error;
584 unrefsav:
585 KEY_FREESAV(&sav);
586 bad:
587 splx(s);
588 if (m)
589 m_freem(m);
590 return error;
591 }
592 #endif
593
594 #ifdef INET6
595 static void
596 compute_ipsec_pos(struct mbuf *m, int *i, int *off)
597 {
598 int nxt;
599 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr*);
600 struct ip6_ext ip6e;
601 int dstopt = 0;
602
603 *i = sizeof(struct ip6_hdr);
604 *off = offsetof(struct ip6_hdr, ip6_nxt);
605 nxt = ip6->ip6_nxt;
606
607 /*
608 * chase mbuf chain to find the appropriate place to
609 * put AH/ESP/IPcomp header.
610 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
611 */
612 do {
613 switch (nxt) {
614 case IPPROTO_AH:
615 case IPPROTO_ESP:
616 case IPPROTO_IPCOMP:
617 /*
618 * we should not skip security header added
619 * beforehand.
620 */
621 return;
622
623 case IPPROTO_HOPOPTS:
624 case IPPROTO_DSTOPTS:
625 case IPPROTO_ROUTING:
626 /*
627 * if we see 2nd destination option header,
628 * we should stop there.
629 */
630 if (nxt == IPPROTO_DSTOPTS && dstopt)
631 return;
632
633 if (nxt == IPPROTO_DSTOPTS) {
634 /*
635 * seen 1st or 2nd destination option.
636 * next time we see one, it must be 2nd.
637 */
638 dstopt = 1;
639 } else if (nxt == IPPROTO_ROUTING) {
640 /*
641 * if we see destionation option next
642 * time, it must be dest2.
643 */
644 dstopt = 2;
645 }
646
647 /* skip this header */
648 m_copydata(m, *i, sizeof(ip6e), &ip6e);
649 nxt = ip6e.ip6e_nxt;
650 *off = *i + offsetof(struct ip6_ext, ip6e_nxt);
651 /*
652 * we will never see nxt == IPPROTO_AH
653 * so it is safe to omit AH case.
654 */
655 *i += (ip6e.ip6e_len + 1) << 3;
656 break;
657 default:
658 return;
659 }
660 } while (*i < m->m_pkthdr.len);
661 }
662
663 static int
664 in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
665 {
666 struct in6_addr ia2;
667
668 memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
669 if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
670 ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
671
672 return IN6_ARE_ADDR_EQUAL(ia, &ia2);
673 }
674
675 int
676 ipsec6_process_packet(
677 struct mbuf *m,
678 struct ipsecrequest *isr
679 )
680 {
681 struct secasvar *sav = NULL;
682 struct ip6_hdr *ip6;
683 int s, error, i, off;
684 union sockaddr_union *dst;
685
686 KASSERT(m != NULL);
687 KASSERT(isr != NULL);
688
689 s = splsoftnet(); /* insure SA contents don't change */
690
691 isr = ipsec_nextisr(m, isr, AF_INET6, &error, &sav);
692 if (isr == NULL) {
693 if (error != 0) {
694 /* XXX Should we send a notification ? */
695 goto bad;
696 } else {
697 if (ipsec_register_done(m, &error) < 0)
698 goto bad;
699
700 splx(s);
701 return ipsec_reinject_ipstack(m, AF_INET6);
702 }
703 }
704
705 KASSERT(sav != NULL);
706 dst = &sav->sah->saidx.dst;
707
708 ip6 = mtod(m, struct ip6_hdr *); /* XXX */
709
710 /* Do the appropriate encapsulation, if necessary */
711 if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
712 dst->sa.sa_family != AF_INET6 || /* PF mismatch */
713 ((dst->sa.sa_family == AF_INET6) &&
714 (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
715 (!in6_sa_equal_addrwithscope(&dst->sin6,
716 &ip6->ip6_dst)))) {
717 struct mbuf *mp;
718
719 /* Fix IPv6 header payload length. */
720 if (m->m_len < sizeof(struct ip6_hdr)) {
721 if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL) {
722 error = ENOBUFS;
723 goto unrefsav;
724 }
725 }
726
727 if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
728 /* No jumbogram support. */
729 error = ENXIO; /*XXX*/
730 goto unrefsav;
731 }
732
733 ip6 = mtod(m, struct ip6_hdr *);
734 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
735
736 /* Encapsulate the packet */
737 error = ipip_output(m, isr, sav, &mp, 0, 0);
738 if (mp == NULL && !error) {
739 /* Should never happen. */
740 IPSECLOG(LOG_DEBUG,
741 "ipip_output returns no mbuf and no error!");
742 error = EFAULT;
743 }
744
745 if (error) {
746 if (mp) {
747 /* XXX: Should never happen! */
748 m_freem(mp);
749 }
750 m = NULL; /* ipip_output() already freed it */
751 goto unrefsav;
752 }
753
754 m = mp;
755 mp = NULL;
756 }
757
758 if (dst->sa.sa_family == AF_INET) {
759 struct ip *ip;
760 ip = mtod(m, struct ip *);
761 i = ip->ip_hl << 2;
762 off = offsetof(struct ip, ip_p);
763 } else {
764 compute_ipsec_pos(m, &i, &off);
765 }
766 error = (*sav->tdb_xform->xf_output)(m, isr, sav, NULL, i, off);
767 KEY_FREESAV(&sav);
768 splx(s);
769 return error;
770 unrefsav:
771 KEY_FREESAV(&sav);
772 bad:
773 splx(s);
774 if (m)
775 m_freem(m);
776 return error;
777 }
778 #endif /*INET6*/
779