]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ipcomp_output.c
1 /* $FreeBSD: src/sys/netinet6/ipcomp_output.c,v 1.1.2.2 2001/07/03 11:01:54 ume Exp $ */
2 /* $KAME: ipcomp_output.c,v 1.23 2001/01/23 08:59:37 itojun Exp $ */
5 * Copyright (C) 1999 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * RFC2393 IP payload compression protocol (IPComp).
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/syslog.h>
51 #include <net/route.h>
53 #include <kern/cpu_number.h>
54 #include <kern/locks.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_ecn.h>
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
67 #include <netinet6/ipcomp.h>
69 #include <netinet6/ipcomp6.h>
72 #include <netinet6/ipsec.h>
74 #include <netinet6/ipsec6.h>
76 #include <netkey/key.h>
77 #include <netkey/keydb.h>
79 #include <net/net_osdep.h>
81 extern lck_mtx_t
*sadb_mutex
;
83 static int ipcomp_output(struct mbuf
*, u_char
*, struct mbuf
*,
84 struct ipsecrequest
*, int);
87 * Modify the packet so that the payload is compressed.
88 * The mbuf (m) must start with IPv4 or IPv6 header.
89 * On failure, free the given mbuf and return non-zero.
94 * IP ......... payload
95 * during the encryption:
98 * IP ............... ipcomp payload
102 * <-----------------> compoff
105 ipcomp_output(m
, nexthdrp
, md
, isr
, af
)
109 struct ipsecrequest
*isr
;
116 struct ipcomp
*ipcomp
;
117 struct secasvar
*sav
= isr
->sav
;
118 const struct ipcomp_algorithm
*algo
;
119 u_int16_t cpi
; /* host order */
120 size_t plen0
, plen
; /*payload length to be compressed*/
124 struct ipsecstat
*stat
;
140 ipseclog((LOG_ERR
, "ipcomp_output: unsupported af %d\n", af
));
141 return 0; /* no change at all */
144 /* grab parameters */
145 algo
= ipcomp_algorithm_lookup(sav
->alg_enc
);
146 if ((ntohl(sav
->spi
) & ~0xffff) != 0 || !algo
) {
151 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0)
154 cpi
= ntohl(sav
->spi
) & 0xffff;
156 /* compute original payload length */
158 for (n
= md
; n
; n
= n
->m_next
)
161 /* if the payload is short enough, we don't need to compress */
162 if (plen
< algo
->minplen
)
166 * retain the original packet for two purposes:
167 * (1) we need to backout our changes when compression is not necessary.
168 * (2) byte lifetime computation should use the original packet.
169 * see RFC2401 page 23.
170 * compromise two m_copym(). we will be going through every byte of
171 * the payload during compression process anyways.
173 mcopy
= m_copym(m
, 0, M_COPYALL
, M_NOWAIT
);
178 md0
= m_copym(md
, 0, M_COPYALL
, M_NOWAIT
);
186 /* make the packet over-writable */
187 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
)
189 if (mprev
== NULL
|| mprev
->m_next
!= md
) {
190 ipseclog((LOG_DEBUG
, "ipcomp%d_output: md is not in chain\n",
198 mprev
->m_next
= NULL
;
199 if ((md
= ipsec_copypkt(md
)) == NULL
) {
208 /* compress data part */
209 lck_mtx_unlock(sadb_mutex
);
210 if ((*algo
->compress
)(m
, md
, &plen
) || mprev
->m_next
== NULL
) {
211 lck_mtx_lock(sadb_mutex
);
212 ipseclog((LOG_ERR
, "packet compression failure\n"));
220 lck_mtx_lock(sadb_mutex
);
221 stat
->out_comphist
[sav
->alg_enc
]++;
225 * if the packet became bigger, meaningless to use IPComp.
226 * we've only wasted our cpu time.
236 * no need to backout change beyond here.
241 m
->m_pkthdr
.len
-= plen0
;
242 m
->m_pkthdr
.len
+= plen
;
246 * insert IPComp header.
249 struct ip
*ip
= NULL
;
252 struct ip6_hdr
*ip6
= NULL
;
254 size_t hlen
= 0; /*ip header len*/
255 size_t complen
= sizeof(struct ipcomp
);
260 ip
= mtod(m
, struct ip
*);
262 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
264 hlen
= ip
->ip_hl
<< 2;
270 ip6
= mtod(m
, struct ip6_hdr
*);
276 compoff
= m
->m_pkthdr
.len
- plen
;
279 * grow the mbuf to accomodate ipcomp header.
280 * before: IP ... payload
281 * after: IP ... ipcomp payload
283 if (M_LEADINGSPACE(md
) < complen
) {
284 MGET(n
, M_DONTWAIT
, MT_DATA
);
293 m
->m_pkthdr
.len
+= complen
;
294 ipcomp
= mtod(n
, struct ipcomp
*);
296 md
->m_len
+= complen
;
297 md
->m_data
-= complen
;
298 m
->m_pkthdr
.len
+= complen
;
299 ipcomp
= mtod(md
, struct ipcomp
*);
302 bzero(ipcomp
, sizeof(*ipcomp
));
303 ipcomp
->comp_nxt
= *nexthdrp
;
304 *nexthdrp
= IPPROTO_IPCOMP
;
305 ipcomp
->comp_cpi
= htons(cpi
);
309 if (compoff
+ complen
+ plen
< IP_MAXPACKET
)
310 ip
->ip_len
= htons(compoff
+ complen
+ plen
);
313 "IPv4 ESP output: size exceeds limit\n"));
314 ipsecstat
.out_inval
++;
323 /* total packet length will be computed in ip6_output() */
331 "NULL mbuf after compression in ipcomp%d_output",
337 /* compute byte lifetime against original packet */
338 key_sa_recordxfer(sav
, mcopy
);
347 panic("something bad in ipcomp_output");
353 ipcomp4_output(m
, isr
)
355 struct ipsecrequest
*isr
;
358 if (m
->m_len
< sizeof(struct ip
)) {
359 ipseclog((LOG_DEBUG
, "ipcomp4_output: first mbuf too short\n"));
360 ipsecstat
.out_inval
++;
364 ip
= mtod(m
, struct ip
*);
365 /* XXX assumes that m->m_next points to payload */
366 return ipcomp_output(m
, &ip
->ip_p
, m
->m_next
, isr
, AF_INET
);
372 ipcomp6_output(m
, nexthdrp
, md
, isr
)
376 struct ipsecrequest
*isr
;
378 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
379 ipseclog((LOG_DEBUG
, "ipcomp6_output: first mbuf too short\n"));
380 ipsec6stat
.out_inval
++;
384 return ipcomp_output(m
, nexthdrp
, md
, isr
, AF_INET6
);