]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ah_output.c
fa6388fbc8dfdb1d3114c9959efffaaa45128081
2 * Copyright (c) 2008-2019 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/ah_output.c,v 1.1.2.3 2001/07/03 11:01:49 ume Exp $ */
30 /* $KAME: ah_output.c,v 1.30 2001/02/21 00:50:53 itojun 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
62 * RFC1826/2402 authentication header.
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>
83 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet/icmp6.h>
93 #include <netinet6/ipsec.h>
94 #include <netinet6/ipsec6.h>
95 #include <netinet6/ah.h>
96 #include <netinet6/ah6.h>
97 #include <netkey/key.h>
98 #include <netkey/keydb.h>
100 #include <net/net_osdep.h>
103 static struct in_addr
*ah4_finaldst(struct mbuf
*);
106 extern lck_mtx_t
*sadb_mutex
;
109 * compute AH header size.
110 * transport mode only. for tunnel mode, we should implement
111 * virtual interface, and control MTU/MSS by the interface MTU.
114 ah_hdrsiz(struct ipsecrequest
*isr
)
118 panic("ah_hdrsiz: NULL was passed.\n");
121 if (isr
->saidx
.proto
!= IPPROTO_AH
) {
122 panic("unsupported mode passed to ah_hdrsiz");
127 lck_mtx_lock(sadb_mutex
);
128 const struct ah_algorithm
*algo
;
131 /*%%%%% this needs to change - no sav in ipsecrequest any more */
132 if (isr
->sav
== NULL
) {
135 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
136 && isr
->sav
->state
!= SADB_SASTATE_DYING
) {
140 /* we need transport mode AH. */
141 algo
= ah_algorithm_lookup(isr
->sav
->alg_auth
);
148 * right now we don't calcurate the padding size. simply
149 * treat the padding size as constant, for simplicity.
151 * XXX variable size padding support
153 hdrsiz
= (((*algo
->sumsiz
)(isr
->sav
) + 3) & ~(4 - 1));
154 if (isr
->sav
->flags
& SADB_X_EXT_OLD
) {
155 hdrsiz
+= sizeof(struct ah
);
157 hdrsiz
+= sizeof(struct newah
);
160 lck_mtx_unlock(sadb_mutex
);
167 //lck_mtx_unlock(sadb_mutex);
169 * sizeof(struct newah) > sizeof(struct ah).
170 * 16 = (16 + 3) & ~(4 - 1).
172 return sizeof(struct newah
) + 16;
177 * Modify the packet so that it includes the authentication data.
178 * The mbuf passed must start with IPv4 header.
180 * assumes that the first mbuf contains IPv4 header + option only.
181 * the function does not modify m.
184 ah4_output(struct mbuf
*m
, struct secasvar
*sav
)
186 const struct ah_algorithm
*algo
;
189 u_char
*ahsumpos
= NULL
;
190 size_t hlen
= 0; /*IP header+option in bytes*/
191 size_t plen
= 0; /*AH payload size in bytes*/
192 size_t ahlen
= 0; /*plen + sizeof(ah)*/
194 struct in_addr dst
= { .s_addr
= 0 };
195 struct in_addr
*finaldst
;
199 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
[0] == NULL
) {
200 ip
= mtod(m
, struct ip
*);
201 ipseclog((LOG_DEBUG
, "ah4_output: internal error: "
202 "sav->replay is null: %x->%x, SPI=%u\n",
203 (u_int32_t
)ntohl(ip
->ip_src
.s_addr
),
204 (u_int32_t
)ntohl(ip
->ip_dst
.s_addr
),
205 (u_int32_t
)ntohl(sav
->spi
)));
206 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
211 algo
= ah_algorithm_lookup(sav
->alg_auth
);
213 ipseclog((LOG_ERR
, "ah4_output: unsupported algorithm: "
214 "SPI=%u\n", (u_int32_t
)ntohl(sav
->spi
)));
215 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
222 * determine the size to grow.
224 if (sav
->flags
& SADB_X_EXT_OLD
) {
226 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /*XXX pad to 8byte?*/
227 ahlen
= plen
+ sizeof(struct ah
);
230 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /*XXX pad to 8byte?*/
231 ahlen
= plen
+ sizeof(struct newah
);
234 VERIFY(ahlen
<= UINT16_MAX
);
237 * grow the mbuf to accomodate AH.
239 ip
= mtod(m
, struct ip
*);
241 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
243 hlen
= ip
->ip_hl
<< 2;
246 if (m
->m_len
!= hlen
) {
247 panic("ah4_output: assumption failed (first mbuf length)");
249 if (M_LEADINGSPACE(m
->m_next
) < ahlen
) {
251 MGET(n
, M_DONTWAIT
, MT_DATA
);
253 ipseclog((LOG_DEBUG
, "ENOBUFS in ah4_output %d\n",
258 n
->m_len
= (int32_t)ahlen
;
259 n
->m_next
= m
->m_next
;
261 m
->m_pkthdr
.len
+= ahlen
;
262 ahdrpos
= mtod(n
, u_char
*);
264 m
->m_next
->m_len
+= ahlen
;
265 m
->m_next
->m_data
-= ahlen
;
266 m
->m_pkthdr
.len
+= ahlen
;
267 ahdrpos
= mtod(m
->m_next
, u_char
*);
270 ip
= mtod(m
, struct ip
*); /*just to be sure*/
275 if (sav
->flags
& SADB_X_EXT_OLD
) {
278 VERIFY((plen
>> 2) <= UINT8_MAX
);
279 ahdr
= (struct ah
*)(void *)ahdrpos
;
280 ahsumpos
= (u_char
*)(ahdr
+ 1);
281 ahdr
->ah_len
= (u_int8_t
)(plen
>> 2);
282 ahdr
->ah_nxt
= ip
->ip_p
;
283 ahdr
->ah_reserve
= htons(0);
285 bzero(ahdr
+ 1, plen
);
289 VERIFY(((plen
>> 2) + 1) <= UINT8_MAX
);
290 ahdr
= (struct newah
*)(void *)ahdrpos
;
291 ahsumpos
= (u_char
*)(ahdr
+ 1);
292 ahdr
->ah_len
= (u_int8_t
)((plen
>> 2) + 1); /* plus one for seq# */
293 ahdr
->ah_nxt
= ip
->ip_p
;
294 ahdr
->ah_reserve
= htons(0);
296 if (sav
->replay
[0]->count
== ~0) {
297 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
298 /* XXX Is it noisy ? */
299 ipseclog((LOG_WARNING
,
300 "replay counter overflowed. %s\n",
301 ipsec_logsastr(sav
)));
302 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
307 lck_mtx_lock(sadb_mutex
);
308 sav
->replay
[0]->count
++;
309 lck_mtx_unlock(sadb_mutex
);
311 * XXX sequence number must not be cycled, if the SA is
312 * installed by IKE daemon.
314 ahdr
->ah_seq
= htonl(sav
->replay
[0]->count
);
315 bzero(ahdr
+ 1, plen
);
319 * modify IPv4 header.
321 ip
->ip_p
= IPPROTO_AH
;
322 if (ahlen
< (IP_MAXPACKET
- ntohs(ip
->ip_len
))) {
323 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + (u_int16_t
)ahlen
);
325 ipseclog((LOG_ERR
, "IPv4 AH output: size exceeds limit\n"));
326 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
332 * If there is source routing option, update destination field in
333 * the IPv4 header to the final destination.
334 * Note that we do not need to update source routing option itself
335 * (as done in IPv4 AH processing -- see ip6_output()), since
336 * source routing option is not part of the ICV computation.
338 finaldst
= ah4_finaldst(m
);
340 dst
.s_addr
= ip
->ip_dst
.s_addr
;
341 ip
->ip_dst
.s_addr
= finaldst
->s_addr
;
345 * calcurate the checksum, based on security association
346 * and the algorithm specified.
348 error
= ah4_calccksum(m
, (caddr_t
)ahsumpos
, plen
, algo
, sav
);
351 "error after ah4_calccksum, called from ah4_output"));
354 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
359 ip
= mtod(m
, struct ip
*); /*just to make sure*/
360 ip
->ip_dst
.s_addr
= dst
.s_addr
;
362 lck_mtx_lock(sadb_stat_mutex
);
363 ipsecstat
.out_success
++;
364 ipsecstat
.out_ahhist
[sav
->alg_auth
]++;
365 lck_mtx_unlock(sadb_stat_mutex
);
366 key_sa_recordxfer(sav
, m
);
372 /* Calculate AH length */
374 ah_hdrlen(struct secasvar
*sav
)
376 const struct ah_algorithm
*algo
;
379 algo
= ah_algorithm_lookup(sav
->alg_auth
);
383 if (sav
->flags
& SADB_X_EXT_OLD
) {
385 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /*XXX pad to 8byte?*/
386 ahlen
= plen
+ sizeof(struct ah
);
389 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /*XXX pad to 8byte?*/
390 ahlen
= plen
+ sizeof(struct newah
);
397 * Fill in the Authentication Header and calculate checksum.
400 ah6_output(struct mbuf
*m
, u_char
*nexthdrp
, struct mbuf
*md
,
401 struct secasvar
*sav
)
405 const struct ah_algorithm
*algo
;
407 u_char
*ahsumpos
= NULL
;
408 size_t plen
; /*AH payload size in bytes*/
413 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
414 ipseclog((LOG_DEBUG
, "ah6_output: first mbuf too short\n"));
419 ahlen
= ah_hdrlen(sav
);
424 VERIFY(ahlen
<= UINT16_MAX
);
426 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
) {
429 if (!mprev
|| mprev
->m_next
!= md
) {
430 ipseclog((LOG_DEBUG
, "ah6_output: md is not in chain\n"));
435 MGET(mah
, M_DONTWAIT
, MT_DATA
);
441 MCLGET(mah
, M_DONTWAIT
);
442 if ((mah
->m_flags
& M_EXT
) == 0) {
448 mah
->m_len
= (int32_t)ahlen
;
451 m
->m_pkthdr
.len
+= ahlen
;
454 if (m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
) > IPV6_MAXPACKET
) {
456 "ip6_output: AH with IPv6 jumbogram is not supported\n"));
461 ip6
= mtod(m
, struct ip6_hdr
*);
462 ip6
->ip6_plen
= htons((u_int16_t
)(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
)));
464 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && sav
->replay
[0] == NULL
) {
465 ipseclog((LOG_DEBUG
, "ah6_output: internal error: "
466 "sav->replay is null: SPI=%u\n",
467 (u_int32_t
)ntohl(sav
->spi
)));
468 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
473 algo
= ah_algorithm_lookup(sav
->alg_auth
);
475 ipseclog((LOG_ERR
, "ah6_output: unsupported algorithm: "
476 "SPI=%u\n", (u_int32_t
)ntohl(sav
->spi
)));
477 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
486 if (sav
->flags
& SADB_X_EXT_OLD
) {
487 struct ah
*ahdr
= mtod(mah
, struct ah
*);
489 plen
= mah
->m_len
- sizeof(struct ah
);
490 VERIFY((plen
>> 2) <= UINT8_MAX
);
491 ahsumpos
= (u_char
*)(ahdr
+ 1);
492 ahdr
->ah_nxt
= *nexthdrp
;
493 *nexthdrp
= IPPROTO_AH
;
494 ahdr
->ah_len
= (u_int8_t
)(plen
>> 2);
495 ahdr
->ah_reserve
= htons(0);
497 bzero(ahdr
+ 1, plen
);
499 struct newah
*ahdr
= mtod(mah
, struct newah
*);
501 plen
= mah
->m_len
- sizeof(struct newah
);
502 VERIFY(((plen
>> 2) + 1) <= UINT8_MAX
);
503 ahsumpos
= (u_char
*)(ahdr
+ 1);
504 ahdr
->ah_nxt
= *nexthdrp
;
505 *nexthdrp
= IPPROTO_AH
;
506 ahdr
->ah_len
= (u_int8_t
)((plen
>> 2) + 1); /* plus one for seq# */
507 ahdr
->ah_reserve
= htons(0);
509 if (sav
->replay
[0]->count
== ~0) {
510 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
511 /* XXX Is it noisy ? */
512 ipseclog((LOG_WARNING
,
513 "replay counter overflowed. %s\n",
514 ipsec_logsastr(sav
)));
515 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
520 lck_mtx_lock(sadb_mutex
);
521 sav
->replay
[0]->count
++;
522 lck_mtx_unlock(sadb_mutex
);
524 * XXX sequence number must not be cycled, if the SA is
525 * installed by IKE daemon.
527 ahdr
->ah_seq
= htonl(sav
->replay
[0]->count
);
528 bzero(ahdr
+ 1, plen
);
532 * calcurate the checksum, based on security association
533 * and the algorithm specified.
535 error
= ah6_calccksum(m
, (caddr_t
)ahsumpos
, plen
, algo
, sav
);
537 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
540 IPSEC_STAT_INCREMENT(ipsec6stat
.out_success
);
541 key_sa_recordxfer(sav
, m
);
543 IPSEC_STAT_INCREMENT(ipsec6stat
.out_ahhist
[sav
->alg_auth
]);
549 * Find the final destination if there is loose/strict source routing option.
550 * Returns NULL if there's no source routing options.
551 * Returns NULL on errors too.
552 * Note that this function will return a pointer INTO the given parameter,
554 * The mbuf must be pulled up toward, at least, ip option part.
556 static struct in_addr
*
557 ah4_finaldst(struct mbuf
*m
)
566 panic("ah4_finaldst: m == NULL");
568 ip
= mtod(m
, struct ip
*);
570 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
572 hlen
= ip
->ip_hl
<< 2;
575 if (m
->m_len
< hlen
) {
577 "ah4_finaldst: parameter mbuf wrong (not pulled up)\n"));
581 if (hlen
== sizeof(struct ip
)) {
585 optlen
= hlen
- sizeof(struct ip
);
587 ipseclog((LOG_DEBUG
, "ah4_finaldst: wrong optlen %d\n",
592 q
= (u_char
*)(ip
+ 1);
595 if (i
+ IPOPT_OPTVAL
>= optlen
) {
598 if (q
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
||
599 q
[i
+ IPOPT_OPTVAL
] == IPOPT_NOP
||
600 i
+ IPOPT_OLEN
< optlen
) {
606 switch (q
[i
+ IPOPT_OPTVAL
]) {
608 i
= optlen
; /* bye */
615 if (q
[i
+ IPOPT_OLEN
] < 2 + sizeof(struct in_addr
) ||
616 optlen
- i
< q
[i
+ IPOPT_OLEN
]) {
618 "ip_finaldst: invalid IP option "
619 "(code=%02x len=%02x)\n",
620 q
[i
+ IPOPT_OPTVAL
], q
[i
+ IPOPT_OLEN
]));
623 i
+= q
[i
+ IPOPT_OLEN
] - sizeof(struct in_addr
);
624 return (struct in_addr
*)(void *)(q
+ i
);
626 if (q
[i
+ IPOPT_OLEN
] < 2 ||
627 optlen
- i
< q
[i
+ IPOPT_OLEN
]) {
629 "ip_finaldst: invalid IP option "
630 "(code=%02x len=%02x)\n",
631 q
[i
+ IPOPT_OPTVAL
], q
[i
+ IPOPT_OLEN
]));
634 i
+= q
[i
+ IPOPT_OLEN
];