2 * Copyright (c) 2008-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $FreeBSD: src/sys/netinet6/esp_output.c,v 1.1.2.3 2002/04/28 05:40:26 suz Exp $ */
30 /* $KAME: esp_output.c,v 1.44 2001/07/26 06:53:15 jinmei Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * RFC1827/2406 Encapsulated Security Payload.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
71 #include <sys/domain.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/errno.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
81 #include <net/route.h>
82 #include <net/multi_layer_pkt_log.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_var.h>
88 #include <netinet/udp.h> /* for nat traversal */
89 #include <netinet/tcp.h>
90 #include <netinet/in_tclass.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet/icmp6.h>
98 #include <netinet6/ipsec.h>
100 #include <netinet6/ipsec6.h>
102 #include <netinet6/ah.h>
104 #include <netinet6/ah6.h>
106 #include <netinet6/esp.h>
108 #include <netinet6/esp6.h>
110 #include <netkey/key.h>
111 #include <netkey/keydb.h>
113 #include <net/net_osdep.h>
115 #include <sys/kdebug.h>
116 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
117 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
118 #define DBG_FNC_ESPOUT NETDBG_CODE(DBG_NETIPSEC, (4 << 8))
119 #define DBG_FNC_ENCRYPT NETDBG_CODE(DBG_NETIPSEC, (5 << 8))
121 static int esp_output(struct mbuf
*, u_char
*, struct mbuf
*,
122 int, struct secasvar
*sav
);
124 extern int esp_udp_encap_port
;
125 extern u_int64_t natt_now
;
127 extern lck_mtx_t
*sadb_mutex
;
130 * compute ESP header size.
133 esp_hdrsiz(__unused
struct ipsecrequest
*isr
)
138 panic("esp_hdrsiz: NULL was passed.\n");
142 lck_mtx_lock(sadb_mutex
);
144 struct secasvar
*sav
;
145 const struct esp_algorithm
*algo
;
146 const struct ah_algorithm
*aalgo
;
152 /*%%%% this needs to change - no sav in ipsecrequest any more */
155 if (isr
->saidx
.proto
!= IPPROTO_ESP
) {
156 panic("unsupported mode passed to esp_hdrsiz");
162 if (sav
->state
!= SADB_SASTATE_MATURE
163 && sav
->state
!= SADB_SASTATE_DYING
) {
167 /* we need transport mode ESP. */
168 algo
= esp_algorithm_lookup(sav
->alg_enc
);
177 if (algo
->padbound
) {
178 maxpad
= algo
->padbound
;
182 maxpad
+= 1; /* maximum 'extendsiz' is padbound + 1, see esp_output */
184 if (sav
->flags
& SADB_X_EXT_OLD
) {
186 hdrsiz
= sizeof(struct esp
) + ivlen
+ maxpad
;
189 aalgo
= ah_algorithm_lookup(sav
->alg_auth
);
190 if (aalgo
&& sav
->replay
[0] != NULL
&& sav
->key_auth
) {
191 authlen
= (aalgo
->sumsiz
)(sav
);
195 hdrsiz
= sizeof(struct newesp
) + ivlen
+ maxpad
+ authlen
;
199 * If the security association indicates that NATT is required,
200 * add the size of the NATT encapsulation header:
202 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) {
203 hdrsiz
+= sizeof(struct udphdr
) + 4;
206 lck_mtx_unlock(sadb_mutex
);
210 lck_mtx_unlock(sadb_mutex
);
214 * sizeof(struct newesp) > sizeof(struct esp). (8)
215 * esp_max_ivlen() = max ivlen for CBC mode
216 * 17 = (maximum padding length without random padding length)
217 * + (Pad Length field) + (Next Header field).
218 * 64 = maximum ICV we support.
219 * sizeof(struct udphdr) in case NAT traversal is used
221 return sizeof(struct newesp
) + esp_max_ivlen() + 17 + AH_MAXSUMSIZE
+ sizeof(struct udphdr
);
225 * Modify the packet so that the payload is encrypted.
226 * The mbuf (m) must start with IPv4 or IPv6 header.
227 * On failure, free the given mbuf and return NULL.
232 * IP ......... payload
233 * during the encryption:
234 * m nexthdrp mprev md
236 * IP ............... esp iv payload pad padlen nxthdr
237 * <--><-><------><--------------->
238 * esplen plen extendsiz
242 * <-----------------> espoff
250 struct secasvar
*sav
)
255 struct esptail
*esptail
;
256 const struct esp_algorithm
*algo
;
257 struct tcphdr th
= {};
260 u_int32_t inner_payload_len
= 0;
261 u_int8_t inner_protocol
= 0;
263 size_t plen
; /*payload length to be encrypted*/
265 size_t esphlen
; /* sizeof(struct esp/newesp) + ivlen */
270 struct ipsecstat
*stat
;
271 struct udphdr
*udp
= NULL
;
272 int udp_encapsulate
= (sav
->flags
& SADB_X_EXT_NATT
&& (af
== AF_INET
|| af
== AF_INET6
) &&
273 ((esp_udp_encap_port
& 0xFFFF) != 0 || sav
->natt_encapsulated_src_port
!= 0));
275 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_START
, sav
->ivlen
, 0, 0, 0, 0);
290 ipseclog((LOG_ERR
, "esp_output: unsupported af %d\n", af
));
291 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 1, 0, 0, 0, 0);
292 return 0; /* no change at all */
295 mbuf_traffic_class_t traffic_class
= 0;
296 if ((sav
->flags2
& SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS
) ==
297 SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS
) {
303 struct ip
*ip
= mtod(m
, struct ip
*);
304 dscp
= ip
->ip_tos
>> IPTOS_DSCP_SHIFT
;
311 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
312 dscp
= (ntohl(ip6
->ip6_flow
) & IP6FLOW_DSCP_MASK
) >> IP6FLOW_DSCP_SHIFT
;
317 panic("esp_output: should not reach here");
319 traffic_class
= rfc4594_dscp_to_tc(dscp
);
322 /* some sanity check */
323 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
[traffic_class
] == NULL
) {
330 ip
= mtod(m
, struct ip
*);
331 ipseclog((LOG_DEBUG
, "esp4_output: internal error: "
332 "sav->replay is null: %x->%x, SPI=%u\n",
333 (u_int32_t
)ntohl(ip
->ip_src
.s_addr
),
334 (u_int32_t
)ntohl(ip
->ip_dst
.s_addr
),
335 (u_int32_t
)ntohl(sav
->spi
)));
336 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
342 ipseclog((LOG_DEBUG
, "esp6_output: internal error: "
343 "sav->replay is null: SPI=%u\n",
344 (u_int32_t
)ntohl(sav
->spi
)));
345 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
349 panic("esp_output: should not reach here");
352 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 2, 0, 0, 0, 0);
356 algo
= esp_algorithm_lookup(sav
->alg_enc
);
358 ipseclog((LOG_ERR
, "esp_output: unsupported algorithm: "
359 "SPI=%u\n", (u_int32_t
)ntohl(sav
->spi
)));
361 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 3, 0, 0, 0, 0);
368 panic("invalid ivlen");
374 * XXX inserts ESP header right after IPv4 header. should
375 * chase the header chain.
376 * XXX sequential number
379 struct ip
*ip
= NULL
;
382 struct ip6_hdr
*ip6
= NULL
;
384 size_t esplen
; /* sizeof(struct esp/newesp) */
385 size_t hlen
= 0; /* ip header len */
387 if (sav
->flags
& SADB_X_EXT_OLD
) {
389 esplen
= sizeof(struct esp
);
392 if (sav
->flags
& SADB_X_EXT_DERIV
) {
393 esplen
= sizeof(struct esp
);
395 esplen
= sizeof(struct newesp
);
398 esphlen
= esplen
+ ivlen
;
400 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
) {
403 if (mprev
== NULL
|| mprev
->m_next
!= md
) {
404 ipseclog((LOG_DEBUG
, "esp%d_output: md is not in chain\n",
407 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 4, 0, 0, 0, 0);
412 for (n
= md
; n
; n
= n
->m_next
) {
419 ip
= mtod(m
, struct ip
*);
421 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
423 hlen
= ip
->ip_hl
<< 2;
429 ip6
= mtod(m
, struct ip6_hdr
*);
435 /* grab info for packet logging */
436 struct secashead
*sah
= sav
->sah
;
437 if (net_mpklog_enabled
&&
438 sah
!= NULL
&& sah
->ipsec_if
!= NULL
) {
439 ifnet_t ifp
= sah
->ipsec_if
;
441 if ((ifp
->if_xflags
& IFXF_MPK_LOG
) == IFXF_MPK_LOG
) {
444 if (sav
->sah
->saidx
.mode
== IPSEC_MODE_TUNNEL
) {
445 struct ip
*inner_ip
= mtod(md
, struct ip
*);
446 if (IP_VHL_V(inner_ip
->ip_vhl
) == IPVERSION
) {
448 iphlen
= IP_VHL_HL(inner_ip
->ip_vhl
) << 2;
450 iphlen
= inner_ip
->ip_hl
<< 2;
452 inner_protocol
= inner_ip
->ip_p
;
453 } else if (IP_VHL_V(inner_ip
->ip_vhl
) == IPV6_VERSION
) {
454 struct ip6_hdr
*inner_ip6
= mtod(md
, struct ip6_hdr
*);
455 iphlen
= sizeof(struct ip6_hdr
);
456 inner_protocol
= inner_ip6
->ip6_nxt
;
459 if (inner_protocol
== IPPROTO_TCP
) {
460 if ((int)(iphlen
+ sizeof(th
)) <=
461 (m
->m_pkthdr
.len
- m
->m_len
)) {
462 m_copydata(md
, iphlen
, sizeof(th
), (u_int8_t
*)&th
);
465 inner_payload_len
= m
->m_pkthdr
.len
- m
->m_len
- iphlen
- (th
.th_off
<< 2);
470 inner_protocol
= ip
->ip_p
;
471 } else if (af
== AF_INET6
) {
472 inner_protocol
= ip6
->ip6_nxt
;
475 if (inner_protocol
== IPPROTO_TCP
) {
476 if ((int)(iphlen
+ sizeof(th
)) <=
478 m_copydata(m
, iphlen
, sizeof(th
), (u_int8_t
*)&th
);
481 inner_payload_len
= m
->m_pkthdr
.len
- iphlen
- (th
.th_off
<< 2);
487 /* make the packet over-writable */
488 mprev
->m_next
= NULL
;
489 if ((md
= ipsec_copypkt(md
)) == NULL
) {
497 * Translate UDP source port back to its original value.
498 * SADB_X_EXT_NATT_MULTIPLEUSERS is only set for transort mode.
500 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) {
501 /* if not UDP - drop it */
502 if (ip
->ip_p
!= IPPROTO_UDP
) {
503 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
509 udp
= mtod(md
, struct udphdr
*);
511 /* if src port not set in sav - find it */
512 if (sav
->natt_encapsulated_src_port
== 0) {
513 if (key_natt_get_translated_port(sav
) == 0) {
519 if (sav
->remote_ike_port
== htons(udp
->uh_dport
)) {
520 /* translate UDP port */
521 udp
->uh_dport
= sav
->natt_encapsulated_src_port
;
522 udp
->uh_sum
= 0; /* don't need checksum with ESP auth */
524 /* drop the packet - can't translate the port */
525 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
533 espoff
= m
->m_pkthdr
.len
- plen
;
535 if (udp_encapsulate
) {
536 esphlen
+= sizeof(struct udphdr
);
537 espoff
+= sizeof(struct udphdr
);
541 * grow the mbuf to accomodate ESP header.
542 * before: IP ... payload
543 * after: IP ... [UDP] ESP IV payload
545 if (M_LEADINGSPACE(md
) < esphlen
|| (md
->m_flags
& M_EXT
) != 0) {
546 MGET(n
, M_DONTWAIT
, MT_DATA
);
555 m
->m_pkthdr
.len
+= esphlen
;
556 if (udp_encapsulate
) {
557 udp
= mtod(n
, struct udphdr
*);
558 esp
= (struct esp
*)(void *)((caddr_t
)udp
+ sizeof(struct udphdr
));
560 esp
= mtod(n
, struct esp
*);
563 md
->m_len
+= esphlen
;
564 md
->m_data
-= esphlen
;
565 m
->m_pkthdr
.len
+= esphlen
;
566 esp
= mtod(md
, struct esp
*);
567 if (udp_encapsulate
) {
568 udp
= mtod(md
, struct udphdr
*);
569 esp
= (struct esp
*)(void *)((caddr_t
)udp
+ sizeof(struct udphdr
));
571 esp
= mtod(md
, struct esp
*);
578 if (esphlen
< (IP_MAXPACKET
- ntohs(ip
->ip_len
))) {
579 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + esphlen
);
582 "IPv4 ESP output: size exceeds limit\n"));
583 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
592 /* total packet length will be computed in ip6_output() */
598 /* initialize esp header. */
600 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0) {
602 nesp
= (struct newesp
*)esp
;
603 if (sav
->replay
[traffic_class
]->count
== sav
->replay
[traffic_class
]->lastseq
) {
604 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
605 /* XXX Is it noisy ? */
606 ipseclog((LOG_WARNING
,
607 "replay counter overflowed. %s\n",
608 ipsec_logsastr(sav
)));
609 IPSEC_STAT_INCREMENT(stat
->out_inval
);
611 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 5, 0, 0, 0, 0);
615 lck_mtx_lock(sadb_mutex
);
616 sav
->replay
[traffic_class
]->count
++;
617 lck_mtx_unlock(sadb_mutex
);
619 * XXX sequence number must not be cycled, if the SA is
620 * installed by IKE daemon.
622 nesp
->esp_seq
= htonl(sav
->replay
[traffic_class
]->count
);
623 seq
= sav
->replay
[traffic_class
]->count
;
628 * find the last mbuf. make some room for ESP trailer.
631 struct ip
*ip
= NULL
;
638 if (algo
->padbound
) {
639 padbound
= algo
->padbound
;
643 /* ESP packet, including nxthdr field, must be length of 4n */
648 extendsiz
= padbound
- (plen
% padbound
);
649 if (extendsiz
== 1) {
650 extendsiz
= padbound
+ 1;
657 randpadmax
= ip4_esp_randpad
;
662 randpadmax
= ip6_esp_randpad
;
669 if (randpadmax
< 0 || plen
+ extendsiz
>= randpadmax
) {
675 randpadmax
= (randpadmax
/ padbound
) * padbound
;
676 pad
= (randpadmax
- plen
+ extendsiz
) / padbound
;
679 pad
= (random() % pad
) * padbound
;
685 * make sure we do not pad too much.
686 * MLEN limitation comes from the trailer attachment
688 * 256 limitation comes from sequential padding.
689 * also, the 1-octet length field in ESP trailer imposes
690 * limitation (but is less strict than sequential padding
691 * as length field do not count the last 2 octets).
693 if (extendsiz
+ pad
<= MLEN
&& extendsiz
+ pad
< 256) {
699 if (extendsiz
> MLEN
|| extendsiz
>= 256) {
700 panic("extendsiz too big in esp_output");
710 * if M_EXT, the external mbuf data may be shared among
711 * two consequtive TCP packets, and it may be unsafe to use the
714 if (!(n
->m_flags
& M_EXT
) && extendsiz
< M_TRAILINGSPACE(n
)) {
715 extend
= mtod(n
, u_char
*) + n
->m_len
;
716 n
->m_len
+= extendsiz
;
717 m
->m_pkthdr
.len
+= extendsiz
;
721 MGET(nn
, M_DONTWAIT
, MT_DATA
);
723 ipseclog((LOG_DEBUG
, "esp%d_output: can't alloc mbuf",
729 extend
= mtod(nn
, u_char
*);
730 nn
->m_len
= extendsiz
;
734 m
->m_pkthdr
.len
+= extendsiz
;
736 switch (sav
->flags
& SADB_X_EXT_PMASK
) {
737 case SADB_X_EXT_PRAND
:
738 key_randomfill(extend
, extendsiz
);
740 case SADB_X_EXT_PZERO
:
741 bzero(extend
, extendsiz
);
743 case SADB_X_EXT_PSEQ
:
744 for (i
= 0; i
< extendsiz
; i
++) {
745 extend
[i
] = (i
+ 1) & 0xff;
751 if (udp_encapsulate
) {
752 *nexthdrp
= IPPROTO_UDP
;
754 /* Fill out the UDP header */
755 if (sav
->natt_encapsulated_src_port
!= 0) {
756 udp
->uh_sport
= (u_short
)sav
->natt_encapsulated_src_port
;
758 udp
->uh_sport
= htons((u_short
)esp_udp_encap_port
);
760 udp
->uh_dport
= htons(sav
->remote_ike_port
);
761 // udp->uh_len set later, after all length tweaks are complete
764 /* Update last sent so we know if we need to send keepalive */
765 sav
->natt_last_activity
= natt_now
;
767 *nexthdrp
= IPPROTO_ESP
;
770 /* initialize esp trailer. */
771 esptail
= (struct esptail
*)
772 (mtod(n
, u_int8_t
*) + n
->m_len
- sizeof(struct esptail
));
773 esptail
->esp_nxt
= nxt
;
774 esptail
->esp_padlen
= extendsiz
- 2;
776 /* modify IP header (for ESP header part only) */
780 ip
= mtod(m
, struct ip
*);
781 if (extendsiz
< (IP_MAXPACKET
- ntohs(ip
->ip_len
))) {
782 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + extendsiz
);
785 "IPv4 ESP output: size exceeds limit\n"));
786 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
795 /* total packet length will be computed in ip6_output() */
802 * pre-compute and cache intermediate key
804 error
= esp_schedule(algo
, sav
);
807 IPSEC_STAT_INCREMENT(stat
->out_inval
);
812 * encrypt the packet, based on security association
813 * and the algorithm specified.
815 if (!algo
->encrypt
) {
816 panic("internal error: no encrypt function");
818 KERNEL_DEBUG(DBG_FNC_ENCRYPT
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
819 if ((*algo
->encrypt
)(m
, espoff
, plen
+ extendsiz
, sav
, algo
, ivlen
)) {
820 /* m is already freed */
821 ipseclog((LOG_ERR
, "packet encryption failure\n"));
822 IPSEC_STAT_INCREMENT(stat
->out_inval
);
824 KERNEL_DEBUG(DBG_FNC_ENCRYPT
| DBG_FUNC_END
, 1, error
, 0, 0, 0);
827 KERNEL_DEBUG(DBG_FNC_ENCRYPT
| DBG_FUNC_END
, 2, 0, 0, 0, 0);
830 * calculate ICV if required.
833 u_char authbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
835 if (algo
->finalizeencrypt
) {
837 if ((*algo
->finalizeencrypt
)(sav
, authbuf
, siz
)) {
838 ipseclog((LOG_ERR
, "packet encryption ICV failure\n"));
839 IPSEC_STAT_INCREMENT(stat
->out_inval
);
841 KERNEL_DEBUG(DBG_FNC_ENCRYPT
| DBG_FUNC_END
, 1, error
, 0, 0, 0);
847 if (!sav
->replay
[traffic_class
]) {
850 if (!sav
->key_auth
) {
853 if (sav
->key_auth
== SADB_AALG_NONE
) {
858 const struct ah_algorithm
*aalgo
;
860 aalgo
= ah_algorithm_lookup(sav
->alg_auth
);
864 siz
= ((aalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1);
865 if (AH_MAXSUMSIZE
< siz
) {
866 panic("assertion failed for AH_MAXSUMSIZE");
869 if (esp_auth(m
, espoff
, m
->m_pkthdr
.len
- espoff
, sav
, authbuf
)) {
870 ipseclog((LOG_ERR
, "ESP checksum generation failure\n"));
873 IPSEC_STAT_INCREMENT(stat
->out_inval
);
888 if (!(n
->m_flags
& M_EXT
) && siz
< M_TRAILINGSPACE(n
)) { /* XXX */
890 m
->m_pkthdr
.len
+= siz
;
891 p
= mtod(n
, u_char
*) + n
->m_len
- siz
;
895 MGET(nn
, M_DONTWAIT
, MT_DATA
);
897 ipseclog((LOG_DEBUG
, "can't alloc mbuf in esp%d_output",
907 m
->m_pkthdr
.len
+= siz
;
908 p
= mtod(nn
, u_char
*);
910 bcopy(authbuf
, p
, siz
);
912 /* modify IP header (for ESP header part only) */
916 ip
= mtod(m
, struct ip
*);
917 if (siz
< (IP_MAXPACKET
- ntohs(ip
->ip_len
))) {
918 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + siz
);
921 "IPv4 ESP output: size exceeds limit\n"));
922 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
931 /* total packet length will be computed in ip6_output() */
937 if (udp_encapsulate
) {
943 ip
= mtod(m
, struct ip
*);
944 udp
->uh_ulen
= htons(ntohs(ip
->ip_len
) - (IP_VHL_HL(ip
->ip_vhl
) << 2));
947 ip6
= mtod(m
, struct ip6_hdr
*);
948 udp
->uh_ulen
= htons(plen
+ siz
+ extendsiz
+ esphlen
);
949 udp
->uh_sum
= in6_pseudo(&ip6
->ip6_src
, &ip6
->ip6_dst
, htonl(ntohs(udp
->uh_ulen
) + IPPROTO_UDP
));
950 m
->m_pkthdr
.csum_flags
= (CSUM_UDPIPV6
| CSUM_ZERO_INVERT
);
951 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
, uh_sum
);
957 if (net_mpklog_enabled
&& sav
->sah
!= NULL
&&
958 sav
->sah
->ipsec_if
!= NULL
&&
959 (sav
->sah
->ipsec_if
->if_xflags
& IFXF_MPK_LOG
) &&
960 inner_protocol
== IPPROTO_TCP
) {
961 MPKL_ESP_OUTPUT_TCP(esp_mpkl_log_object
,
963 ntohs(th
.th_sport
), ntohs(th
.th_dport
),
964 ntohl(th
.th_seq
), ntohl(th
.th_ack
),
965 th
.th_flags
, inner_payload_len
);
968 lck_mtx_lock(sadb_mutex
);
971 "NULL mbuf after encryption in esp%d_output", afnumber
));
975 stat
->out_esphist
[sav
->alg_enc
]++;
976 lck_mtx_unlock(sadb_mutex
);
977 key_sa_recordxfer(sav
, m
);
978 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 6, 0, 0, 0, 0);
983 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 7, error
, 0, 0, 0);
986 panic("something bad in esp_output");
994 struct secasvar
*sav
)
997 if (m
->m_len
< sizeof(struct ip
)) {
998 ipseclog((LOG_DEBUG
, "esp4_output: first mbuf too short\n"));
1002 ip
= mtod(m
, struct ip
*);
1003 /* XXX assumes that m->m_next points to payload */
1004 return esp_output(m
, &ip
->ip_p
, m
->m_next
, AF_INET
, sav
);
1014 struct secasvar
*sav
)
1016 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
1017 ipseclog((LOG_DEBUG
, "esp6_output: first mbuf too short\n"));
1021 return esp_output(m
, nexthdrp
, md
, AF_INET6
, sav
);