]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ipcomp_output.c
484c0b481232321a911ff9341541c924f3ab9ff9
1 /* $KAME: ipcomp_output.c,v 1.11 2000/02/22 14:04:23 itojun Exp $ */
4 * Copyright (C) 1999 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * RFC2393 IP payload compression protocol (IPComp).
37 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
50 #include <sys/kernel.h>
51 #include <sys/syslog.h>
54 #include <net/route.h>
55 #include <net/netisr.h>
57 #include <kern/cpu_number.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/ip_ecn.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
70 #include <netinet6/ipcomp.h>
72 #include <netinet6/ipsec.h>
73 #include <netkey/key.h>
74 #include <netkey/keydb.h>
75 #include <netkey/key_debug.h>
77 #include <net/net_osdep.h>
79 static int ipcomp_output
__P((struct mbuf
*, u_char
*, struct mbuf
*,
80 struct ipsecrequest
*, int));
83 * Modify the packet so that the payload is compressed.
84 * The mbuf (m) must start with IPv4 or IPv6 header.
85 * On failure, free the given mbuf and return NULL.
90 * IP ......... payload
91 * during the encryption:
94 * IP ............... ipcomp payload
98 * <-----------------> compoff
101 ipcomp_output(m
, nexthdrp
, md
, isr
, af
)
105 struct ipsecrequest
*isr
;
111 struct ipcomp
*ipcomp
;
112 struct secasvar
*sav
= isr
->sav
;
113 struct ipcomp_algorithm
*algo
;
114 u_int16_t cpi
; /* host order */
115 size_t plen0
, plen
; /*payload length to be compressed*/
132 ipseclog((LOG_ERR
, "ipcomp_output: unsupported af %d\n", af
));
133 return 0; /* no change at all */
136 /* grab parameters */
137 if ((ntohl(sav
->spi
) & ~0xffff) != 0 || sav
->alg_enc
>= IPCOMP_MAX
138 || ipcomp_algorithms
[sav
->alg_enc
].compress
== NULL
) {
139 ipsecstat
.out_inval
++;
143 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0)
146 cpi
= ntohl(sav
->spi
) & 0xffff;
147 algo
= &ipcomp_algorithms
[sav
->alg_enc
]; /*XXX*/
149 /* compute original payload length */
151 for (n
= md
; n
; n
= n
->m_next
)
154 /* if the payload is short enough, we don't need to compress */
155 if (plen
< algo
->minplen
)
159 * keep the original data packet, so that we can backout
160 * our changes when compression is not necessary.
162 md0
= m_copym(md
, 0, M_COPYALL
, M_NOWAIT
);
169 /* make the packet over-writable */
170 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
)
172 if (mprev
== NULL
|| mprev
->m_next
!= md
) {
173 ipseclog((LOG_DEBUG
, "ipcomp%d_output: md is not in chain\n",
178 ipsecstat
.out_inval
++;
183 ipsec6stat
.out_inval
++;
191 mprev
->m_next
= NULL
;
192 if ((md
= ipsec_copypkt(md
)) == NULL
) {
200 /* compress data part */
201 if ((*algo
->compress
)(m
, md
, &plen
) || mprev
->m_next
== NULL
) {
202 ipseclog((LOG_ERR
, "packet compression failure\n"));
208 ipsecstat
.out_inval
++;
213 ipsec6stat
.out_inval
++;
223 ipsecstat
.out_comphist
[sav
->alg_enc
]++;
228 ipsec6stat
.out_comphist
[sav
->alg_enc
]++;
235 * if the packet became bigger, meaningless to use IPComp.
236 * we've only wasted our cpu time.
244 /* no need to backout change beyond here */
247 m
->m_pkthdr
.len
-= plen0
;
248 m
->m_pkthdr
.len
+= plen
;
252 * insert IPComp header.
255 struct ip
*ip
= NULL
;
258 struct ip6_hdr
*ip6
= NULL
;
260 size_t hlen
= 0; /*ip header len*/
261 size_t complen
= sizeof(struct ipcomp
);
266 ip
= mtod(m
, struct ip
*);
268 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
270 hlen
= ip
->ip_hl
<< 2;
276 ip6
= mtod(m
, struct ip6_hdr
*);
282 compoff
= m
->m_pkthdr
.len
- plen
;
285 * grow the mbuf to accomodate ipcomp header.
286 * before: IP ... payload
287 * after: IP ... ipcomp payload
289 if (M_LEADINGSPACE(md
) < complen
) {
290 MGET(n
, M_DONTWAIT
, MT_DATA
);
299 m
->m_pkthdr
.len
+= complen
;
300 ipcomp
= mtod(n
, struct ipcomp
*);
302 md
->m_len
+= complen
;
303 md
->m_data
-= complen
;
304 m
->m_pkthdr
.len
+= complen
;
305 ipcomp
= mtod(md
, struct ipcomp
*);
308 bzero(ipcomp
, sizeof(*ipcomp
));
309 ipcomp
->comp_nxt
= *nexthdrp
;
310 *nexthdrp
= IPPROTO_IPCOMP
;
311 ipcomp
->comp_cpi
= htons(cpi
);
315 if (compoff
+ complen
+ plen
< IP_MAXPACKET
)
316 ip
->ip_len
= htons(compoff
+ complen
+ plen
);
319 "IPv4 ESP output: size exceeds limit\n"));
320 ipsecstat
.out_inval
++;
329 /* total packet length will be computed in ip6_output() */
337 "NULL mbuf after compression in ipcomp%d_output",
342 ipsecstat
.out_inval
++;
347 ipsec6stat
.out_inval
++;
355 ipsecstat
.out_success
++;
360 ipsec6stat
.out_success
++;
369 ipsecstat
.out_esphist
[sav
->alg_enc
]++;
374 ipsec6stat
.out_esphist
[sav
->alg_enc
]++;
379 key_sa_recordxfer(sav
, m
);
386 panic("something bad in ipcomp_output");
392 ipcomp4_output(m
, isr
)
394 struct ipsecrequest
*isr
;
397 if (m
->m_len
< sizeof(struct ip
)) {
398 ipseclog((LOG_DEBUG
, "ipcomp4_output: first mbuf too short\n"));
399 ipsecstat
.out_inval
++;
403 ip
= mtod(m
, struct ip
*);
404 /* XXX assumes that m->m_next points to payload */
405 return ipcomp_output(m
, &ip
->ip_p
, m
->m_next
, isr
, AF_INET
);
411 ipcomp6_output(m
, nexthdrp
, md
, isr
)
415 struct ipsecrequest
*isr
;
417 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
418 ipseclog((LOG_DEBUG
, "ipcomp6_output: first mbuf too short\n"));
419 ipsec6stat
.out_inval
++;
423 return ipcomp_output(m
, nexthdrp
, md
, isr
, AF_INET6
);