]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ipcomp_output.c
6abaedef6a3c2a17ebc1b5057200b3420b5113fe
2 * Copyright (c) 2016 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/ipcomp_output.c,v 1.1.2.2 2001/07/03 11:01:54 ume Exp $ */
30 /* $KAME: ipcomp_output.c,v 1.23 2001/01/23 08:59:37 itojun Exp $ */
33 * Copyright (C) 1999 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 * RFC2393 IP payload compression protocol (IPComp).
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
70 #include <sys/domain.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/errno.h>
75 #include <sys/kernel.h>
76 #include <sys/syslog.h>
79 #include <net/route.h>
80 #include <libkern/zlib.h>
81 #include <kern/cpu_number.h>
82 #include <kern/locks.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/ip_ecn.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/ip6_var.h>
95 #include <netinet6/ipcomp.h>
97 #include <netinet6/ipcomp6.h>
100 #include <netinet6/ipsec.h>
102 #include <netinet6/ipsec6.h>
104 #include <netkey/key.h>
105 #include <netkey/keydb.h>
107 #include <net/net_osdep.h>
110 static int ipcomp_output(struct mbuf
*, u_char
*, struct mbuf
*,
111 int, struct secasvar
*sav
);
114 * Modify the packet so that the payload is compressed.
115 * The mbuf (m) must start with IPv4 or IPv6 header.
116 * On failure, free the given mbuf and return non-zero.
121 * IP ......... payload
122 * during the encryption:
123 * m nexthdrp mprev md
125 * IP ............... ipcomp payload
129 * <-----------------> compoff
132 ipcomp_output(struct mbuf
*m
, u_char
*nexthdrp
, struct mbuf
*md
, int af
, struct secasvar
*sav
)
138 struct ipcomp
*ipcomp
;
139 const struct ipcomp_algorithm
*algo
;
140 u_int16_t cpi
; /* host order */
141 size_t plen0
, plen
; /*payload length to be compressed*/
145 struct ipsecstat
*stat
;
161 ipseclog((LOG_ERR
, "ipcomp_output: unsupported af %d\n", af
));
162 return 0; /* no change at all */
165 /* grab parameters */
166 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
167 if ((ntohl(sav
->spi
) & ~0xffff) != 0 || !algo
) {
168 IPSEC_STAT_INCREMENT(stat
->out_inval
);
172 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0) {
175 cpi
= ntohl(sav
->spi
) & 0xffff;
178 /* compute original payload length */
180 for (n
= md
; n
; n
= n
->m_next
) {
184 /* if the payload is short enough, we don't need to compress */
185 if (plen
< algo
->minplen
) {
190 * retain the original packet for two purposes:
191 * (1) we need to backout our changes when compression is not necessary.
192 * (2) byte lifetime computation should use the original packet.
193 * see RFC2401 page 23.
194 * compromise two m_copym(). we will be going through every byte of
195 * the payload during compression process anyways.
197 mcopy
= m_copym(m
, 0, M_COPYALL
, M_NOWAIT
);
202 md0
= m_copym(md
, 0, M_COPYALL
, M_NOWAIT
);
210 /* make the packet over-writable */
211 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
) {
214 if (mprev
== NULL
|| mprev
->m_next
!= md
) {
215 ipseclog((LOG_DEBUG
, "ipcomp%d_output: md is not in chain\n",
217 IPSEC_STAT_INCREMENT(stat
->out_inval
);
223 mprev
->m_next
= NULL
;
224 if ((md
= ipsec_copypkt(md
)) == NULL
) {
233 /* compress data part */
234 if ((*algo
->compress
)(m
, md
, &plen
) || mprev
->m_next
== NULL
) {
235 ipseclog((LOG_ERR
, "packet compression failure\n"));
239 IPSEC_STAT_INCREMENT(stat
->out_inval
);
243 IPSEC_STAT_INCREMENT(stat
->out_comphist
[sav
->alg_enc
]);
247 * if the packet became bigger, meaningless to use IPComp.
248 * we've only wasted our cpu time.
258 * no need to backout change beyond here.
263 m
->m_pkthdr
.len
-= plen0
;
264 m
->m_pkthdr
.len
+= plen
;
268 * insert IPComp header.
271 struct ip
*ip
= NULL
;
274 struct ip6_hdr
*ip6
= NULL
;
276 size_t hlen
= 0; /*ip header len*/
277 size_t complen
= sizeof(struct ipcomp
);
282 ip
= mtod(m
, struct ip
*);
284 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
286 hlen
= ip
->ip_hl
<< 2;
292 ip6
= mtod(m
, struct ip6_hdr
*);
298 compoff
= m
->m_pkthdr
.len
- plen
;
301 * grow the mbuf to accomodate ipcomp header.
302 * before: IP ... payload
303 * after: IP ... ipcomp payload
305 if (M_LEADINGSPACE(md
) < complen
) {
306 MGET(n
, M_DONTWAIT
, MT_DATA
);
315 m
->m_pkthdr
.len
+= complen
;
316 ipcomp
= mtod(n
, struct ipcomp
*);
318 md
->m_len
+= complen
;
319 md
->m_data
-= complen
;
320 m
->m_pkthdr
.len
+= complen
;
321 ipcomp
= mtod(md
, struct ipcomp
*);
324 bzero(ipcomp
, sizeof(*ipcomp
));
325 ipcomp
->comp_nxt
= *nexthdrp
;
326 *nexthdrp
= IPPROTO_IPCOMP
;
327 ipcomp
->comp_cpi
= htons(cpi
);
331 if (compoff
+ complen
+ plen
< IP_MAXPACKET
) {
332 ip
->ip_len
= htons(compoff
+ complen
+ plen
);
335 "IPv4 ESP output: size exceeds limit\n"));
336 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
345 /* total packet length will be computed in ip6_output() */
353 "NULL mbuf after compression in ipcomp%d_output",
355 IPSEC_STAT_INCREMENT(stat
->out_inval
);
357 IPSEC_STAT_INCREMENT(stat
->out_success
);
359 /* compute byte lifetime against original packet */
360 key_sa_recordxfer(sav
, mcopy
);
369 panic("something bad in ipcomp_output");
375 ipcomp4_output(struct mbuf
*m
, struct secasvar
*sav
)
378 if (m
->m_len
< sizeof(struct ip
)) {
379 ipseclog((LOG_DEBUG
, "ipcomp4_output: first mbuf too short\n"));
380 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
384 ip
= mtod(m
, struct ip
*);
385 /* XXX assumes that m->m_next points to payload */
386 return ipcomp_output(m
, &ip
->ip_p
, m
->m_next
, AF_INET
, sav
);
396 struct secasvar
*sav
)
398 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
399 ipseclog((LOG_DEBUG
, "ipcomp6_output: first mbuf too short\n"));
400 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
404 return ipcomp_output(m
, nexthdrp
, md
, AF_INET6
, sav
);