]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ipcomp_output.c
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;
177 /* compute original payload length */
179 for (n
= md
; n
; n
= n
->m_next
)
182 /* if the payload is short enough, we don't need to compress */
183 if (plen
< algo
->minplen
)
187 * retain the original packet for two purposes:
188 * (1) we need to backout our changes when compression is not necessary.
189 * (2) byte lifetime computation should use the original packet.
190 * see RFC2401 page 23.
191 * compromise two m_copym(). we will be going through every byte of
192 * the payload during compression process anyways.
194 mcopy
= m_copym(m
, 0, M_COPYALL
, M_NOWAIT
);
199 md0
= m_copym(md
, 0, M_COPYALL
, M_NOWAIT
);
207 /* make the packet over-writable */
208 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
)
210 if (mprev
== NULL
|| mprev
->m_next
!= md
) {
211 ipseclog((LOG_DEBUG
, "ipcomp%d_output: md is not in chain\n",
213 IPSEC_STAT_INCREMENT(stat
->out_inval
);
219 mprev
->m_next
= NULL
;
220 if ((md
= ipsec_copypkt(md
)) == NULL
) {
229 /* compress data part */
230 if ((*algo
->compress
)(m
, md
, &plen
) || mprev
->m_next
== NULL
) {
231 ipseclog((LOG_ERR
, "packet compression failure\n"));
235 IPSEC_STAT_INCREMENT(stat
->out_inval
);
239 IPSEC_STAT_INCREMENT(stat
->out_comphist
[sav
->alg_enc
]);
243 * if the packet became bigger, meaningless to use IPComp.
244 * we've only wasted our cpu time.
254 * no need to backout change beyond here.
259 m
->m_pkthdr
.len
-= plen0
;
260 m
->m_pkthdr
.len
+= plen
;
264 * insert IPComp header.
267 struct ip
*ip
= NULL
;
270 struct ip6_hdr
*ip6
= NULL
;
272 size_t hlen
= 0; /*ip header len*/
273 size_t complen
= sizeof(struct ipcomp
);
278 ip
= mtod(m
, struct ip
*);
280 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
282 hlen
= ip
->ip_hl
<< 2;
288 ip6
= mtod(m
, struct ip6_hdr
*);
294 compoff
= m
->m_pkthdr
.len
- plen
;
297 * grow the mbuf to accomodate ipcomp header.
298 * before: IP ... payload
299 * after: IP ... ipcomp payload
301 if (M_LEADINGSPACE(md
) < complen
) {
302 MGET(n
, M_DONTWAIT
, MT_DATA
);
311 m
->m_pkthdr
.len
+= complen
;
312 ipcomp
= mtod(n
, struct ipcomp
*);
314 md
->m_len
+= complen
;
315 md
->m_data
-= complen
;
316 m
->m_pkthdr
.len
+= complen
;
317 ipcomp
= mtod(md
, struct ipcomp
*);
320 bzero(ipcomp
, sizeof(*ipcomp
));
321 ipcomp
->comp_nxt
= *nexthdrp
;
322 *nexthdrp
= IPPROTO_IPCOMP
;
323 ipcomp
->comp_cpi
= htons(cpi
);
327 if (compoff
+ complen
+ plen
< IP_MAXPACKET
)
328 ip
->ip_len
= htons(compoff
+ complen
+ plen
);
331 "IPv4 ESP output: size exceeds limit\n"));
332 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
341 /* total packet length will be computed in ip6_output() */
349 "NULL mbuf after compression in ipcomp%d_output",
351 IPSEC_STAT_INCREMENT(stat
->out_inval
);
353 IPSEC_STAT_INCREMENT(stat
->out_success
);
355 /* compute byte lifetime against original packet */
356 key_sa_recordxfer(sav
, mcopy
);
365 panic("something bad in ipcomp_output");
371 ipcomp4_output(struct mbuf
*m
, struct secasvar
*sav
)
374 if (m
->m_len
< sizeof(struct ip
)) {
375 ipseclog((LOG_DEBUG
, "ipcomp4_output: first mbuf too short\n"));
376 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
380 ip
= mtod(m
, struct ip
*);
381 /* XXX assumes that m->m_next points to payload */
382 return ipcomp_output(m
, &ip
->ip_p
, m
->m_next
, AF_INET
, sav
);
392 struct secasvar
*sav
)
394 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
395 ipseclog((LOG_DEBUG
, "ipcomp6_output: first mbuf too short\n"));
396 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
400 return ipcomp_output(m
, nexthdrp
, md
, AF_INET6
, sav
);