]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/esp_output.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / netinet6 / esp_output.c
CommitLineData
b0d623f7 1/*
5ba3f43e 2 * Copyright (c) 2008-2017 Apple Inc. All rights reserved.
b0d623f7
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39037602 5 *
b0d623f7
A
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.
39037602 14 *
b0d623f7
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39037602 17 *
b0d623f7
A
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.
39037602 25 *
b0d623f7
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
55e303ae
A
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 $ */
1c79356b
A
31
32/*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 */
60
61#define _IP_VHL
1c79356b
A
62
63/*
64 * RFC1827/2406 Encapsulated Security Payload.
65 */
66
67#include <sys/param.h>
68#include <sys/systm.h>
69#include <sys/malloc.h>
70#include <sys/mbuf.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>
76#include <sys/time.h>
77#include <sys/kernel.h>
78#include <sys/syslog.h>
79
80#include <net/if.h>
81#include <net/route.h>
cb323159 82#include <net/multi_layer_pkt_log.h>
1c79356b
A
83
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/in_var.h>
55e303ae 88#include <netinet/udp.h> /* for nat traversal */
cb323159
A
89#include <netinet/tcp.h>
90#include <netinet/in_tclass.h>
1c79356b 91
1c79356b
A
92#include <netinet/ip6.h>
93#include <netinet6/ip6_var.h>
94#include <netinet/icmp6.h>
1c79356b
A
95
96#include <netinet6/ipsec.h>
9bccf70c 97#include <netinet6/ipsec6.h>
1c79356b 98#include <netinet6/ah.h>
9bccf70c 99#include <netinet6/ah6.h>
1c79356b 100#include <netinet6/esp.h>
9bccf70c 101#include <netinet6/esp6.h>
1c79356b
A
102#include <netkey/key.h>
103#include <netkey/keydb.h>
1c79356b
A
104
105#include <net/net_osdep.h>
106
55e303ae 107#include <sys/kdebug.h>
0a7de745
A
108#define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
109#define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
110#define DBG_FNC_ESPOUT NETDBG_CODE(DBG_NETIPSEC, (4 << 8))
111#define DBG_FNC_ENCRYPT NETDBG_CODE(DBG_NETIPSEC, (5 << 8))
55e303ae 112
91447636 113static int esp_output(struct mbuf *, u_char *, struct mbuf *,
0a7de745 114 int, struct secasvar *sav);
1c79356b 115
0a7de745 116extern int esp_udp_encap_port;
39037602 117extern u_int64_t natt_now;
55e303ae 118
2d21ac55
A
119extern lck_mtx_t *sadb_mutex;
120
1c79356b
A
121/*
122 * compute ESP header size.
123 */
124size_t
3e170ce0 125esp_hdrsiz(__unused struct ipsecrequest *isr)
1c79356b 126{
3e170ce0 127#if 0
1c79356b 128 /* sanity check */
0a7de745 129 if (isr == NULL) {
1c79356b 130 panic("esp_hdrsiz: NULL was passed.\n");
0a7de745 131 }
1c79356b 132
1c79356b 133
2d21ac55
A
134 lck_mtx_lock(sadb_mutex);
135 {
136 struct secasvar *sav;
137 const struct esp_algorithm *algo;
138 const struct ah_algorithm *aalgo;
139 size_t ivlen;
140 size_t authlen;
141 size_t hdrsiz;
142 size_t maxpad;
0a7de745 143
2d21ac55
A
144 /*%%%% this needs to change - no sav in ipsecrequest any more */
145 sav = isr->sav;
0a7de745
A
146
147 if (isr->saidx.proto != IPPROTO_ESP) {
2d21ac55 148 panic("unsupported mode passed to esp_hdrsiz");
0a7de745
A
149 }
150
151 if (sav == NULL) {
2d21ac55 152 goto estimate;
0a7de745 153 }
2d21ac55 154 if (sav->state != SADB_SASTATE_MATURE
0a7de745 155 && sav->state != SADB_SASTATE_DYING) {
2d21ac55 156 goto estimate;
0a7de745
A
157 }
158
2d21ac55
A
159 /* we need transport mode ESP. */
160 algo = esp_algorithm_lookup(sav->alg_enc);
0a7de745 161 if (!algo) {
2d21ac55 162 goto estimate;
0a7de745 163 }
2d21ac55 164 ivlen = sav->ivlen;
0a7de745 165 if (ivlen < 0) {
2d21ac55 166 goto estimate;
0a7de745
A
167 }
168
169 if (algo->padbound) {
2d21ac55 170 maxpad = algo->padbound;
0a7de745 171 } else {
2d21ac55 172 maxpad = 4;
0a7de745 173 }
2d21ac55 174 maxpad += 1; /* maximum 'extendsiz' is padbound + 1, see esp_output */
0a7de745 175
2d21ac55
A
176 if (sav->flags & SADB_X_EXT_OLD) {
177 /* RFC 1827 */
178 hdrsiz = sizeof(struct esp) + ivlen + maxpad;
179 } else {
180 /* RFC 2406 */
181 aalgo = ah_algorithm_lookup(sav->alg_auth);
cb323159 182 if (aalgo && sav->replay[0] != NULL && sav->key_auth) {
2d21ac55 183 authlen = (aalgo->sumsiz)(sav);
0a7de745 184 } else {
2d21ac55 185 authlen = 0;
0a7de745 186 }
2d21ac55
A
187 hdrsiz = sizeof(struct newesp) + ivlen + maxpad + authlen;
188 }
0a7de745 189
2d21ac55
A
190 /*
191 * If the security association indicates that NATT is required,
192 * add the size of the NATT encapsulation header:
193 */
0a7de745
A
194 if ((sav->flags & SADB_X_EXT_NATT) != 0) {
195 hdrsiz += sizeof(struct udphdr) + 4;
196 }
197
2d21ac55
A
198 lck_mtx_unlock(sadb_mutex);
199 return hdrsiz;
200 }
201estimate:
0a7de745 202 lck_mtx_unlock(sadb_mutex);
2d21ac55 203#endif
1c79356b
A
204 /*
205 * ASSUMING:
55e303ae 206 * sizeof(struct newesp) > sizeof(struct esp). (8)
9bccf70c 207 * esp_max_ivlen() = max ivlen for CBC mode
55e303ae 208 * 17 = (maximum padding length without random padding length)
1c79356b 209 * + (Pad Length field) + (Next Header field).
b0d623f7 210 * 64 = maximum ICV we support.
55e303ae 211 * sizeof(struct udphdr) in case NAT traversal is used
1c79356b 212 */
b0d623f7 213 return sizeof(struct newesp) + esp_max_ivlen() + 17 + AH_MAXSUMSIZE + sizeof(struct udphdr);
1c79356b
A
214}
215
216/*
217 * Modify the packet so that the payload is encrypted.
218 * The mbuf (m) must start with IPv4 or IPv6 header.
219 * On failure, free the given mbuf and return NULL.
220 *
221 * on invocation:
222 * m nexthdrp md
223 * v v v
224 * IP ......... payload
225 * during the encryption:
226 * m nexthdrp mprev md
227 * v v v v
228 * IP ............... esp iv payload pad padlen nxthdr
229 * <--><-><------><--------------->
230 * esplen plen extendsiz
231 * ivlen
232 * <-----> esphlen
233 * <-> hlen
234 * <-----------------> espoff
235 */
236static int
39037602
A
237esp_output(
238 struct mbuf *m,
239 u_char *nexthdrp,
240 struct mbuf *md,
241 int af,
242 struct secasvar *sav)
1c79356b
A
243{
244 struct mbuf *n;
245 struct mbuf *mprev;
246 struct esp *esp;
247 struct esptail *esptail;
9bccf70c 248 const struct esp_algorithm *algo;
cb323159 249 struct tcphdr th = {};
1c79356b 250 u_int32_t spi;
cb323159 251 u_int32_t seq;
f427ee49 252 size_t inner_payload_len = 0;
cb323159 253 u_int8_t inner_protocol = 0;
1c79356b 254 u_int8_t nxt = 0;
0a7de745 255 size_t plen; /*payload length to be encrypted*/
1c79356b 256 size_t espoff;
0a7de745 257 size_t esphlen; /* sizeof(struct esp/newesp) + ivlen */
1c79356b
A
258 int ivlen;
259 int afnumber;
260 size_t extendsiz;
261 int error = 0;
9bccf70c 262 struct ipsecstat *stat;
55e303ae 263 struct udphdr *udp = NULL;
0a7de745 264 int udp_encapsulate = (sav->flags & SADB_X_EXT_NATT && (af == AF_INET || af == AF_INET6) &&
cb323159 265 ((esp_udp_encap_port & 0xFFFF) != 0 || sav->natt_encapsulated_src_port != 0));
1c79356b 266
0a7de745 267 KERNEL_DEBUG(DBG_FNC_ESPOUT | DBG_FUNC_START, sav->ivlen, 0, 0, 0, 0);
1c79356b 268 switch (af) {
1c79356b
A
269 case AF_INET:
270 afnumber = 4;
9bccf70c 271 stat = &ipsecstat;
1c79356b 272 break;
1c79356b
A
273 case AF_INET6:
274 afnumber = 6;
9bccf70c 275 stat = &ipsec6stat;
1c79356b 276 break;
1c79356b
A
277 default:
278 ipseclog((LOG_ERR, "esp_output: unsupported af %d\n", af));
0a7de745
A
279 KERNEL_DEBUG(DBG_FNC_ESPOUT | DBG_FUNC_END, 1, 0, 0, 0, 0);
280 return 0; /* no change at all */
1c79356b
A
281 }
282
cb323159
A
283 mbuf_traffic_class_t traffic_class = 0;
284 if ((sav->flags2 & SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS) ==
285 SADB_X_EXT_SA2_SEQ_PER_TRAFFIC_CLASS) {
286 u_int8_t dscp = 0;
287 switch (af) {
cb323159
A
288 case AF_INET:
289 {
290 struct ip *ip = mtod(m, struct ip *);
291 dscp = ip->ip_tos >> IPTOS_DSCP_SHIFT;
292 break;
293 }
cb323159
A
294 case AF_INET6:
295 {
296 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
297 dscp = (ntohl(ip6->ip6_flow) & IP6FLOW_DSCP_MASK) >> IP6FLOW_DSCP_SHIFT;
298 break;
299 }
cb323159
A
300 default:
301 panic("esp_output: should not reach here");
302 }
303 traffic_class = rfc4594_dscp_to_tc(dscp);
304 }
305
1c79356b 306 /* some sanity check */
cb323159 307 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay[traffic_class] == NULL) {
1c79356b 308 switch (af) {
1c79356b 309 case AF_INET:
0a7de745 310 {
1c79356b
A
311 struct ip *ip;
312
313 ip = mtod(m, struct ip *);
314 ipseclog((LOG_DEBUG, "esp4_output: internal error: "
0a7de745
A
315 "sav->replay is null: %x->%x, SPI=%u\n",
316 (u_int32_t)ntohl(ip->ip_src.s_addr),
317 (u_int32_t)ntohl(ip->ip_dst.s_addr),
318 (u_int32_t)ntohl(sav->spi)));
2d21ac55 319 IPSEC_STAT_INCREMENT(ipsecstat.out_inval);
9bccf70c 320 break;
0a7de745 321 }
1c79356b 322 case AF_INET6:
1c79356b 323 ipseclog((LOG_DEBUG, "esp6_output: internal error: "
0a7de745
A
324 "sav->replay is null: SPI=%u\n",
325 (u_int32_t)ntohl(sav->spi)));
2d21ac55 326 IPSEC_STAT_INCREMENT(ipsec6stat.out_inval);
9bccf70c 327 break;
9bccf70c
A
328 default:
329 panic("esp_output: should not reach here");
1c79356b 330 }
9bccf70c 331 m_freem(m);
0a7de745 332 KERNEL_DEBUG(DBG_FNC_ESPOUT | DBG_FUNC_END, 2, 0, 0, 0, 0);
9bccf70c 333 return EINVAL;
1c79356b
A
334 }
335
9bccf70c
A
336 algo = esp_algorithm_lookup(sav->alg_enc);
337 if (!algo) {
338 ipseclog((LOG_ERR, "esp_output: unsupported algorithm: "
339 "SPI=%u\n", (u_int32_t)ntohl(sav->spi)));
340 m_freem(m);
0a7de745 341 KERNEL_DEBUG(DBG_FNC_ESPOUT | DBG_FUNC_END, 3, 0, 0, 0, 0);
9bccf70c
A
342 return EINVAL;
343 }
1c79356b
A
344 spi = sav->spi;
345 ivlen = sav->ivlen;
346 /* should be okey */
347 if (ivlen < 0) {
348 panic("invalid ivlen");
349 }
350
0a7de745
A
351 {
352 /*
353 * insert ESP header.
354 * XXX inserts ESP header right after IPv4 header. should
355 * chase the header chain.
356 * XXX sequential number
357 */
0a7de745 358 struct ip *ip = NULL;
0a7de745 359 struct ip6_hdr *ip6 = NULL;
0a7de745
A
360 size_t esplen; /* sizeof(struct esp/newesp) */
361 size_t hlen = 0; /* ip header len */
1c79356b 362
0a7de745
A
363 if (sav->flags & SADB_X_EXT_OLD) {
364 /* RFC 1827 */
1c79356b 365 esplen = sizeof(struct esp);
0a7de745
A
366 } else {
367 /* RFC 2406 */
368 if (sav->flags & SADB_X_EXT_DERIV) {
369 esplen = sizeof(struct esp);
370 } else {
371 esplen = sizeof(struct newesp);
372 }
373 }
374 esphlen = esplen + ivlen;
1c79356b 375
0a7de745
A
376 for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next) {
377 ;
378 }
379 if (mprev == NULL || mprev->m_next != md) {
380 ipseclog((LOG_DEBUG, "esp%d_output: md is not in chain\n",
381 afnumber));
382 m_freem(m);
383 KERNEL_DEBUG(DBG_FNC_ESPOUT | DBG_FUNC_END, 4, 0, 0, 0, 0);
384 return EINVAL;
385 }
1c79356b 386
0a7de745
A
387 plen = 0;
388 for (n = md; n; n = n->m_next) {
389 plen += n->m_len;
390 }
1c79356b 391
0a7de745 392 switch (af) {
0a7de745
A
393 case AF_INET:
394 ip = mtod(m, struct ip *);
1c79356b 395#ifdef _IP_VHL
0a7de745 396 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
1c79356b 397#else
0a7de745 398 hlen = ip->ip_hl << 2;
1c79356b 399#endif
0a7de745 400 break;
0a7de745
A
401 case AF_INET6:
402 ip6 = mtod(m, struct ip6_hdr *);
403 hlen = sizeof(*ip6);
404 break;
0a7de745 405 }
1c79356b 406
cb323159
A
407 /* grab info for packet logging */
408 struct secashead *sah = sav->sah;
409 if (net_mpklog_enabled &&
410 sah != NULL && sah->ipsec_if != NULL) {
411 ifnet_t ifp = sah->ipsec_if;
412
413 if ((ifp->if_xflags & IFXF_MPK_LOG) == IFXF_MPK_LOG) {
414 size_t iphlen = 0;
415
416 if (sav->sah->saidx.mode == IPSEC_MODE_TUNNEL) {
417 struct ip *inner_ip = mtod(md, struct ip *);
418 if (IP_VHL_V(inner_ip->ip_vhl) == IPVERSION) {
419#ifdef _IP_VHL
420 iphlen = IP_VHL_HL(inner_ip->ip_vhl) << 2;
421#else
422 iphlen = inner_ip->ip_hl << 2;
423#endif
424 inner_protocol = inner_ip->ip_p;
425 } else if (IP_VHL_V(inner_ip->ip_vhl) == IPV6_VERSION) {
426 struct ip6_hdr *inner_ip6 = mtod(md, struct ip6_hdr *);
427 iphlen = sizeof(struct ip6_hdr);
428 inner_protocol = inner_ip6->ip6_nxt;
429 }
430
431 if (inner_protocol == IPPROTO_TCP) {
432 if ((int)(iphlen + sizeof(th)) <=
433 (m->m_pkthdr.len - m->m_len)) {
f427ee49 434 m_copydata(md, (int)iphlen, sizeof(th), (u_int8_t *)&th);
cb323159
A
435 }
436
437 inner_payload_len = m->m_pkthdr.len - m->m_len - iphlen - (th.th_off << 2);
438 }
439 } else {
440 iphlen = hlen;
441 if (af == AF_INET) {
442 inner_protocol = ip->ip_p;
443 } else if (af == AF_INET6) {
444 inner_protocol = ip6->ip6_nxt;
445 }
446
447 if (inner_protocol == IPPROTO_TCP) {
448 if ((int)(iphlen + sizeof(th)) <=
449 m->m_pkthdr.len) {
f427ee49 450 m_copydata(m, (int)iphlen, sizeof(th), (u_int8_t *)&th);
cb323159
A
451 }
452
453 inner_payload_len = m->m_pkthdr.len - iphlen - (th.th_off << 2);
454 }
455 }
456 }
457 }
458
0a7de745
A
459 /* make the packet over-writable */
460 mprev->m_next = NULL;
461 if ((md = ipsec_copypkt(md)) == NULL) {
2d21ac55 462 m_freem(m);
0a7de745 463 error = ENOBUFS;
2d21ac55 464 goto fail;
0a7de745
A
465 }
466 mprev->m_next = md;
2d21ac55 467
0a7de745
A
468 /*
469 * Translate UDP source port back to its original value.
470 * SADB_X_EXT_NATT_MULTIPLEUSERS is only set for transort mode.
471 */
472 if ((sav->flags & SADB_X_EXT_NATT_MULTIPLEUSERS) != 0) {
473 /* if not UDP - drop it */
474 if (ip->ip_p != IPPROTO_UDP) {
475 IPSEC_STAT_INCREMENT(ipsecstat.out_inval);
2d21ac55
A
476 m_freem(m);
477 error = EINVAL;
478 goto fail;
479 }
1c79356b 480
0a7de745 481 udp = mtod(md, struct udphdr *);
1c79356b 482
0a7de745
A
483 /* if src port not set in sav - find it */
484 if (sav->natt_encapsulated_src_port == 0) {
485 if (key_natt_get_translated_port(sav) == 0) {
486 m_freem(m);
487 error = EINVAL;
488 goto fail;
489 }
490 }
491 if (sav->remote_ike_port == htons(udp->uh_dport)) {
492 /* translate UDP port */
493 udp->uh_dport = sav->natt_encapsulated_src_port;
494 udp->uh_sum = 0; /* don't need checksum with ESP auth */
495 } else {
496 /* drop the packet - can't translate the port */
497 IPSEC_STAT_INCREMENT(ipsecstat.out_inval);
498 m_freem(m);
499 error = EINVAL;
500 goto fail;
501 }
1c79356b 502 }
0a7de745
A
503
504
505 espoff = m->m_pkthdr.len - plen;
506
55e303ae 507 if (udp_encapsulate) {
0a7de745
A
508 esphlen += sizeof(struct udphdr);
509 espoff += sizeof(struct udphdr);
55e303ae 510 }
0a7de745
A
511
512 /*
513 * grow the mbuf to accomodate ESP header.
514 * before: IP ... payload
515 * after: IP ... [UDP] ESP IV payload
516 */
517 if (M_LEADINGSPACE(md) < esphlen || (md->m_flags & M_EXT) != 0) {
518 MGET(n, M_DONTWAIT, MT_DATA);
519 if (!n) {
520 m_freem(m);
521 error = ENOBUFS;
522 goto fail;
523 }
f427ee49
A
524 VERIFY(esphlen <= INT32_MAX);
525 n->m_len = (int)esphlen;
0a7de745
A
526 mprev->m_next = n;
527 n->m_next = md;
528 m->m_pkthdr.len += esphlen;
529 if (udp_encapsulate) {
530 udp = mtod(n, struct udphdr *);
531 esp = (struct esp *)(void *)((caddr_t)udp + sizeof(struct udphdr));
532 } else {
533 esp = mtod(n, struct esp *);
534 }
55e303ae 535 } else {
0a7de745
A
536 md->m_len += esphlen;
537 md->m_data -= esphlen;
538 m->m_pkthdr.len += esphlen;
55e303ae 539 esp = mtod(md, struct esp *);
0a7de745
A
540 if (udp_encapsulate) {
541 udp = mtod(md, struct udphdr *);
542 esp = (struct esp *)(void *)((caddr_t)udp + sizeof(struct udphdr));
543 } else {
544 esp = mtod(md, struct esp *);
545 }
55e303ae 546 }
0a7de745
A
547
548 switch (af) {
0a7de745
A
549 case AF_INET:
550 if (esphlen < (IP_MAXPACKET - ntohs(ip->ip_len))) {
f427ee49 551 ip->ip_len = htons(ntohs(ip->ip_len) + (u_short)esphlen);
0a7de745
A
552 } else {
553 ipseclog((LOG_ERR,
554 "IPv4 ESP output: size exceeds limit\n"));
555 IPSEC_STAT_INCREMENT(ipsecstat.out_inval);
556 m_freem(m);
557 error = EMSGSIZE;
558 goto fail;
559 }
560 break;
0a7de745
A
561 case AF_INET6:
562 /* total packet length will be computed in ip6_output() */
563 break;
0a7de745 564 }
1c79356b 565 }
1c79356b
A
566
567 /* initialize esp header. */
568 esp->esp_spi = spi;
569 if ((sav->flags & SADB_X_EXT_OLD) == 0) {
570 struct newesp *nesp;
571 nesp = (struct newesp *)esp;
cb323159 572 if (sav->replay[traffic_class]->count == sav->replay[traffic_class]->lastseq) {
1c79356b
A
573 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
574 /* XXX Is it noisy ? */
575 ipseclog((LOG_WARNING,
576 "replay counter overflowed. %s\n",
577 ipsec_logsastr(sav)));
2d21ac55 578 IPSEC_STAT_INCREMENT(stat->out_inval);
1c79356b 579 m_freem(m);
0a7de745 580 KERNEL_DEBUG(DBG_FNC_ESPOUT | DBG_FUNC_END, 5, 0, 0, 0, 0);
1c79356b
A
581 return EINVAL;
582 }
583 }
2d21ac55 584 lck_mtx_lock(sadb_mutex);
cb323159 585 sav->replay[traffic_class]->count++;
2d21ac55 586 lck_mtx_unlock(sadb_mutex);
1c79356b
A
587 /*
588 * XXX sequence number must not be cycled, if the SA is
589 * installed by IKE daemon.
590 */
cb323159
A
591 nesp->esp_seq = htonl(sav->replay[traffic_class]->count);
592 seq = sav->replay[traffic_class]->count;
1c79356b
A
593 }
594
0a7de745
A
595 {
596 /*
597 * find the last mbuf. make some room for ESP trailer.
598 */
0a7de745 599 struct ip *ip = NULL;
0a7de745
A
600 size_t padbound;
601 u_char *extend;
602 int i;
603 int randpadmax;
604
605 if (algo->padbound) {
606 padbound = algo->padbound;
607 } else {
608 padbound = 4;
609 }
610 /* ESP packet, including nxthdr field, must be length of 4n */
611 if (padbound < 4) {
612 padbound = 4;
613 }
614
615 extendsiz = padbound - (plen % padbound);
616 if (extendsiz == 1) {
617 extendsiz = padbound + 1;
618 }
619
620 /* random padding */
621 switch (af) {
0a7de745
A
622 case AF_INET:
623 randpadmax = ip4_esp_randpad;
624 break;
0a7de745
A
625 case AF_INET6:
626 randpadmax = ip6_esp_randpad;
627 break;
0a7de745
A
628 default:
629 randpadmax = -1;
630 break;
631 }
632 if (randpadmax < 0 || plen + extendsiz >= randpadmax) {
633 ;
634 } else {
f427ee49 635 size_t pad;
9bccf70c 636
0a7de745 637 /* round */
f427ee49 638 randpadmax = (int)((randpadmax / padbound) * padbound);
0a7de745 639 pad = (randpadmax - plen + extendsiz) / padbound;
9bccf70c 640
0a7de745
A
641 if (pad > 0) {
642 pad = (random() % pad) * padbound;
643 } else {
644 pad = 0;
645 }
9bccf70c 646
0a7de745
A
647 /*
648 * make sure we do not pad too much.
649 * MLEN limitation comes from the trailer attachment
650 * code below.
651 * 256 limitation comes from sequential padding.
652 * also, the 1-octet length field in ESP trailer imposes
653 * limitation (but is less strict than sequential padding
654 * as length field do not count the last 2 octets).
655 */
656 if (extendsiz + pad <= MLEN && extendsiz + pad < 256) {
657 extendsiz += pad;
658 }
659 }
9bccf70c 660
0a7de745
A
661 n = m;
662 while (n->m_next) {
663 n = n->m_next;
664 }
1c79356b 665
0a7de745
A
666 /*
667 * if M_EXT, the external mbuf data may be shared among
668 * two consequtive TCP packets, and it may be unsafe to use the
669 * trailing space.
670 */
671 if (!(n->m_flags & M_EXT) && extendsiz < M_TRAILINGSPACE(n)) {
672 extend = mtod(n, u_char *) + n->m_len;
f427ee49 673 n->m_len += (int)extendsiz;
0a7de745
A
674 m->m_pkthdr.len += extendsiz;
675 } else {
676 struct mbuf *nn;
1c79356b 677
0a7de745
A
678 MGET(nn, M_DONTWAIT, MT_DATA);
679 if (!nn) {
680 ipseclog((LOG_DEBUG, "esp%d_output: can't alloc mbuf",
681 afnumber));
682 m_freem(m);
683 error = ENOBUFS;
684 goto fail;
685 }
686 extend = mtod(nn, u_char *);
f427ee49
A
687 VERIFY(extendsiz <= INT_MAX);
688 nn->m_len = (int)extendsiz;
0a7de745
A
689 nn->m_next = NULL;
690 n->m_next = nn;
691 n = nn;
692 m->m_pkthdr.len += extendsiz;
693 }
694 switch (sav->flags & SADB_X_EXT_PMASK) {
695 case SADB_X_EXT_PRAND:
696 key_randomfill(extend, extendsiz);
697 break;
698 case SADB_X_EXT_PZERO:
699 bzero(extend, extendsiz);
700 break;
701 case SADB_X_EXT_PSEQ:
702 for (i = 0; i < extendsiz; i++) {
703 extend[i] = (i + 1) & 0xff;
704 }
705 break;
1c79356b 706 }
55e303ae 707
0a7de745
A
708 nxt = *nexthdrp;
709 if (udp_encapsulate) {
710 *nexthdrp = IPPROTO_UDP;
711
712 /* Fill out the UDP header */
cb323159
A
713 if (sav->natt_encapsulated_src_port != 0) {
714 udp->uh_sport = (u_short)sav->natt_encapsulated_src_port;
715 } else {
716 udp->uh_sport = htons((u_short)esp_udp_encap_port);
717 }
718 udp->uh_dport = htons(sav->remote_ike_port);
719 // udp->uh_len set later, after all length tweaks are complete
0a7de745 720 udp->uh_sum = 0;
1c79356b 721
0a7de745
A
722 /* Update last sent so we know if we need to send keepalive */
723 sav->natt_last_activity = natt_now;
724 } else {
725 *nexthdrp = IPPROTO_ESP;
726 }
1c79356b 727
0a7de745
A
728 /* initialize esp trailer. */
729 esptail = (struct esptail *)
730 (mtod(n, u_int8_t *) + n->m_len - sizeof(struct esptail));
731 esptail->esp_nxt = nxt;
f427ee49
A
732 VERIFY((extendsiz - 2) <= UINT8_MAX);
733 esptail->esp_padlen = (u_int8_t)(extendsiz - 2);
0a7de745
A
734
735 /* modify IP header (for ESP header part only) */
736 switch (af) {
0a7de745
A
737 case AF_INET:
738 ip = mtod(m, struct ip *);
739 if (extendsiz < (IP_MAXPACKET - ntohs(ip->ip_len))) {
f427ee49 740 ip->ip_len = htons(ntohs(ip->ip_len) + (u_short)extendsiz);
0a7de745
A
741 } else {
742 ipseclog((LOG_ERR,
743 "IPv4 ESP output: size exceeds limit\n"));
744 IPSEC_STAT_INCREMENT(ipsecstat.out_inval);
745 m_freem(m);
746 error = EMSGSIZE;
747 goto fail;
748 }
749 break;
0a7de745
A
750 case AF_INET6:
751 /* total packet length will be computed in ip6_output() */
752 break;
0a7de745 753 }
1c79356b 754 }
1c79356b 755
9bccf70c
A
756 /*
757 * pre-compute and cache intermediate key
758 */
759 error = esp_schedule(algo, sav);
760 if (error) {
761 m_freem(m);
2d21ac55 762 IPSEC_STAT_INCREMENT(stat->out_inval);
9bccf70c
A
763 goto fail;
764 }
765
1c79356b
A
766 /*
767 * encrypt the packet, based on security association
768 * and the algorithm specified.
769 */
0a7de745 770 if (!algo->encrypt) {
1c79356b 771 panic("internal error: no encrypt function");
0a7de745
A
772 }
773 KERNEL_DEBUG(DBG_FNC_ENCRYPT | DBG_FUNC_START, 0, 0, 0, 0, 0);
1c79356b 774 if ((*algo->encrypt)(m, espoff, plen + extendsiz, sav, algo, ivlen)) {
9bccf70c 775 /* m is already freed */
1c79356b 776 ipseclog((LOG_ERR, "packet encryption failure\n"));
2d21ac55 777 IPSEC_STAT_INCREMENT(stat->out_inval);
1c79356b 778 error = EINVAL;
0a7de745 779 KERNEL_DEBUG(DBG_FNC_ENCRYPT | DBG_FUNC_END, 1, error, 0, 0, 0);
1c79356b
A
780 goto fail;
781 }
0a7de745 782 KERNEL_DEBUG(DBG_FNC_ENCRYPT | DBG_FUNC_END, 2, 0, 0, 0, 0);
1c79356b
A
783
784 /*
785 * calculate ICV if required.
786 */
3e170ce0
A
787 size_t siz = 0;
788 u_char authbuf[AH_MAXSUMSIZE] __attribute__((aligned(4)));
789
0a7de745 790 if (algo->finalizeencrypt) {
3e170ce0
A
791 siz = algo->icvlen;
792 if ((*algo->finalizeencrypt)(sav, authbuf, siz)) {
0a7de745 793 ipseclog((LOG_ERR, "packet encryption ICV failure\n"));
3e170ce0
A
794 IPSEC_STAT_INCREMENT(stat->out_inval);
795 error = EINVAL;
0a7de745 796 KERNEL_DEBUG(DBG_FNC_ENCRYPT | DBG_FUNC_END, 1, error, 0, 0, 0);
3e170ce0
A
797 goto fail;
798 }
799 goto fill_icv;
800 }
801
cb323159 802 if (!sav->replay[traffic_class]) {
1c79356b 803 goto noantireplay;
0a7de745
A
804 }
805 if (!sav->key_auth) {
1c79356b 806 goto noantireplay;
0a7de745
A
807 }
808 if (sav->key_auth == SADB_AALG_NONE) {
1c79356b 809 goto noantireplay;
0a7de745 810 }
9bccf70c 811
0a7de745 812 {
2d21ac55 813 const struct ah_algorithm *aalgo;
0a7de745 814
2d21ac55 815 aalgo = ah_algorithm_lookup(sav->alg_auth);
0a7de745 816 if (!aalgo) {
2d21ac55 817 goto noantireplay;
0a7de745 818 }
2d21ac55 819 siz = ((aalgo->sumsiz)(sav) + 3) & ~(4 - 1);
0a7de745 820 if (AH_MAXSUMSIZE < siz) {
2d21ac55 821 panic("assertion failed for AH_MAXSUMSIZE");
0a7de745
A
822 }
823
2d21ac55
A
824 if (esp_auth(m, espoff, m->m_pkthdr.len - espoff, sav, authbuf)) {
825 ipseclog((LOG_ERR, "ESP checksum generation failure\n"));
1c79356b 826 m_freem(m);
2d21ac55
A
827 error = EINVAL;
828 IPSEC_STAT_INCREMENT(stat->out_inval);
1c79356b
A
829 goto fail;
830 }
0a7de745 831 }
3e170ce0 832
0a7de745
A
833fill_icv:
834 {
835 struct ip *ip;
3e170ce0
A
836 u_char *p;
837
2d21ac55 838 n = m;
0a7de745 839 while (n->m_next) {
2d21ac55 840 n = n->m_next;
0a7de745
A
841 }
842
2d21ac55
A
843 if (!(n->m_flags & M_EXT) && siz < M_TRAILINGSPACE(n)) { /* XXX */
844 n->m_len += siz;
845 m->m_pkthdr.len += siz;
846 p = mtod(n, u_char *) + n->m_len - siz;
847 } else {
848 struct mbuf *nn;
0a7de745 849
2d21ac55
A
850 MGET(nn, M_DONTWAIT, MT_DATA);
851 if (!nn) {
852 ipseclog((LOG_DEBUG, "can't alloc mbuf in esp%d_output",
0a7de745 853 afnumber));
2d21ac55
A
854 m_freem(m);
855 error = ENOBUFS;
856 goto fail;
857 }
f427ee49 858 nn->m_len = (int)siz;
2d21ac55
A
859 nn->m_next = NULL;
860 n->m_next = nn;
861 n = nn;
862 m->m_pkthdr.len += siz;
863 p = mtod(nn, u_char *);
864 }
865 bcopy(authbuf, p, siz);
0a7de745 866
2d21ac55
A
867 /* modify IP header (for ESP header part only) */
868 switch (af) {
2d21ac55
A
869 case AF_INET:
870 ip = mtod(m, struct ip *);
0a7de745 871 if (siz < (IP_MAXPACKET - ntohs(ip->ip_len))) {
f427ee49 872 ip->ip_len = htons(ntohs(ip->ip_len) + (u_short)siz);
0a7de745 873 } else {
2d21ac55 874 ipseclog((LOG_ERR,
0a7de745 875 "IPv4 ESP output: size exceeds limit\n"));
2d21ac55
A
876 IPSEC_STAT_INCREMENT(ipsecstat.out_inval);
877 m_freem(m);
878 error = EMSGSIZE;
879 goto fail;
880 }
881 break;
2d21ac55
A
882 case AF_INET6:
883 /* total packet length will be computed in ip6_output() */
884 break;
1c79356b 885 }
0a7de745
A
886 }
887
55e303ae
A
888 if (udp_encapsulate) {
889 struct ip *ip;
3e170ce0 890 struct ip6_hdr *ip6;
55e303ae 891
3e170ce0
A
892 switch (af) {
893 case AF_INET:
0a7de745 894 ip = mtod(m, struct ip *);
f427ee49 895 udp->uh_ulen = htons((u_int16_t)(ntohs(ip->ip_len) - (IP_VHL_HL(ip->ip_vhl) << 2)));
0a7de745 896 break;
3e170ce0 897 case AF_INET6:
0a7de745 898 ip6 = mtod(m, struct ip6_hdr *);
f427ee49
A
899 VERIFY((plen + siz + extendsiz + esphlen) <= UINT16_MAX);
900 udp->uh_ulen = htons((u_int16_t)(plen + siz + extendsiz + esphlen));
0a7de745
A
901 udp->uh_sum = in6_pseudo(&ip6->ip6_src, &ip6->ip6_dst, htonl(ntohs(udp->uh_ulen) + IPPROTO_UDP));
902 m->m_pkthdr.csum_flags = (CSUM_UDPIPV6 | CSUM_ZERO_INVERT);
903 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
904 break;
3e170ce0
A
905 }
906 }
1c79356b
A
907
908noantireplay:
cb323159
A
909 if (net_mpklog_enabled && sav->sah != NULL &&
910 sav->sah->ipsec_if != NULL &&
911 (sav->sah->ipsec_if->if_xflags & IFXF_MPK_LOG) &&
912 inner_protocol == IPPROTO_TCP) {
913 MPKL_ESP_OUTPUT_TCP(esp_mpkl_log_object,
914 ntohl(spi), seq,
915 ntohs(th.th_sport), ntohs(th.th_dport),
916 ntohl(th.th_seq), ntohl(th.th_ack),
f427ee49 917 inner_payload_len, th.th_flags);
cb323159
A
918 }
919
2d21ac55 920 lck_mtx_lock(sadb_mutex);
1c79356b
A
921 if (!m) {
922 ipseclog((LOG_ERR,
923 "NULL mbuf after encryption in esp%d_output", afnumber));
0a7de745 924 } else {
9bccf70c 925 stat->out_success++;
0a7de745 926 }
9bccf70c 927 stat->out_esphist[sav->alg_enc]++;
2d21ac55 928 lck_mtx_unlock(sadb_mutex);
1c79356b 929 key_sa_recordxfer(sav, m);
0a7de745 930 KERNEL_DEBUG(DBG_FNC_ESPOUT | DBG_FUNC_END, 6, 0, 0, 0, 0);
1c79356b
A
931 return 0;
932
933fail:
0a7de745 934 KERNEL_DEBUG(DBG_FNC_ESPOUT | DBG_FUNC_END, 7, error, 0, 0, 0);
1c79356b 935 return error;
1c79356b
A
936}
937
1c79356b 938int
39037602
A
939esp4_output(
940 struct mbuf *m,
941 struct secasvar *sav)
1c79356b
A
942{
943 struct ip *ip;
944 if (m->m_len < sizeof(struct ip)) {
945 ipseclog((LOG_DEBUG, "esp4_output: first mbuf too short\n"));
946 m_freem(m);
2d21ac55 947 return EINVAL;
1c79356b
A
948 }
949 ip = mtod(m, struct ip *);
950 /* XXX assumes that m->m_next points to payload */
2d21ac55 951 return esp_output(m, &ip->ip_p, m->m_next, AF_INET, sav);
1c79356b 952}
1c79356b 953
1c79356b 954int
39037602
A
955esp6_output(
956 struct mbuf *m,
957 u_char *nexthdrp,
958 struct mbuf *md,
959 struct secasvar *sav)
1c79356b
A
960{
961 if (m->m_len < sizeof(struct ip6_hdr)) {
962 ipseclog((LOG_DEBUG, "esp6_output: first mbuf too short\n"));
963 m_freem(m);
2d21ac55 964 return EINVAL;
1c79356b 965 }
2d21ac55 966 return esp_output(m, nexthdrp, md, AF_INET6, sav);
1c79356b 967}