1 /* $FreeBSD: src/sys/netinet6/ipcomp_output.c,v 1.1.2.4 2003/04/29 08:33:50 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/ipcomp_output.c,v 1.5 2003/08/23 11:02:45 rob Exp $ */
3 /* $KAME: ipcomp_output.c,v 1.25 2002/06/09 14:44:00 itojun Exp $ */
6 * Copyright (C) 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * RFC2393 IP payload compression protocol (IPComp).
39 #include "opt_inet6.h"
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/syslog.h>
53 #include <net/route.h>
54 #include <net/netisr.h>
56 #include <machine/cpu.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/ip_ecn.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/ip6_var.h>
69 #include <netinet6/ipcomp.h>
71 #include <netinet6/ipcomp6.h>
74 #include <netinet6/ipsec.h>
76 #include <netinet6/ipsec6.h>
78 #include <netproto/key/key.h>
79 #include <netproto/key/keydb.h>
81 #include <machine/stdarg.h>
83 #include <net/net_osdep.h>
85 static int ipcomp_output (struct mbuf *, u_char *, struct mbuf *,
86 struct ipsecrequest *, int);
89 * Modify the packet so that the payload is compressed.
90 * The mbuf (m) must start with IPv4 or IPv6 header.
91 * On failure, free the given mbuf and return non-zero.
96 * IP ......... payload
97 * during the encryption:
100 * IP ............... ipcomp payload
104 * <-----------------> compoff
107 ipcomp_output(m, nexthdrp, md, isr, af)
111 struct ipsecrequest *isr;
118 struct ipcomp *ipcomp;
119 struct secasvar *sav = isr->sav;
120 const struct ipcomp_algorithm *algo;
121 u_int16_t cpi; /* host order */
122 size_t plen0, plen; /* payload length to be compressed */
126 struct ipsecstat *stat;
142 ipseclog((LOG_ERR, "ipcomp_output: unsupported af %d\n", af));
143 return 0; /* no change at all */
146 /* grab parameters */
147 algo = ipcomp_algorithm_lookup(sav->alg_enc);
148 if ((ntohl(sav->spi) & ~0xffff) != 0 || !algo) {
153 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
156 cpi = ntohl(sav->spi) & 0xffff;
158 /* compute original payload length */
160 for (n = md; n; n = n->m_next)
163 /* if the payload is short enough, we don't need to compress */
164 if (plen < algo->minplen)
168 * retain the original packet for two purposes:
169 * (1) we need to backout our changes when compression is not necessary.
170 * (2) byte lifetime computation should use the original packet.
171 * see RFC2401 page 23.
172 * compromise two m_copym(). we will be going through every byte of
173 * the payload during compression process anyways.
175 mcopy = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
180 md0 = m_copym(md, 0, M_COPYALL, M_DONTWAIT);
188 /* make the packet over-writable */
189 for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
191 if (mprev == NULL || mprev->m_next != md) {
192 ipseclog((LOG_DEBUG, "ipcomp%d_output: md is not in chain\n",
200 mprev->m_next = NULL;
201 if ((md = ipsec_copypkt(md)) == NULL) {
210 /* compress data part */
211 if ((*algo->compress)(m, md, &plen) || mprev->m_next == NULL) {
212 ipseclog((LOG_ERR, "packet compression failure\n"));
220 stat->out_comphist[sav->alg_enc]++;
224 * if the packet became bigger, meaningless to use IPComp.
225 * we've only wasted our cpu time.
235 * no need to backout change beyond here.
240 m->m_pkthdr.len -= plen0;
241 m->m_pkthdr.len += plen;
245 * insert IPComp header.
248 struct ip *ip = NULL;
251 struct ip6_hdr *ip6 = NULL;
253 size_t hlen = 0; /* ip header len */
254 size_t complen = sizeof(struct ipcomp);
259 ip = mtod(m, struct ip *);
261 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
263 hlen = ip->ip_hl << 2;
269 ip6 = mtod(m, struct ip6_hdr *);
275 compoff = m->m_pkthdr.len - plen;
278 * grow the mbuf to accomodate ipcomp header.
279 * before: IP ... payload
280 * after: IP ... ipcomp payload
282 if (M_LEADINGSPACE(md) < complen) {
283 MGET(n, M_DONTWAIT, MT_DATA);
292 m->m_pkthdr.len += complen;
293 ipcomp = mtod(n, struct ipcomp *);
295 md->m_len += complen;
296 md->m_data -= complen;
297 m->m_pkthdr.len += complen;
298 ipcomp = mtod(md, struct ipcomp *);
301 bzero(ipcomp, sizeof(*ipcomp));
302 ipcomp->comp_nxt = *nexthdrp;
303 *nexthdrp = IPPROTO_IPCOMP;
304 ipcomp->comp_cpi = htons(cpi);
308 if (compoff + complen + plen < IP_MAXPACKET)
309 ip->ip_len = htons(compoff + complen + plen);
312 "IPv4 ESP output: size exceeds limit\n"));
313 ipsecstat.out_inval++;
322 /* total packet length will be computed in ip6_output() */
330 "NULL mbuf after compression in ipcomp%d_output",
336 /* compute byte lifetime against original packet */
337 key_sa_recordxfer(sav, mcopy);
346 panic("something bad in ipcomp_output");
352 ipcomp4_output(m, isr)
354 struct ipsecrequest *isr;
357 if (m->m_len < sizeof(struct ip)) {
358 ipseclog((LOG_DEBUG, "ipcomp4_output: first mbuf too short\n"));
359 ipsecstat.out_inval++;
363 ip = mtod(m, struct ip *);
364 /* XXX assumes that m->m_next points to payload */
365 return ipcomp_output(m, &ip->ip_p, m->m_next, isr, AF_INET);
371 ipcomp6_output(m, nexthdrp, md, isr)
375 struct ipsecrequest *isr;
377 if (m->m_len < sizeof(struct ip6_hdr)) {
378 ipseclog((LOG_DEBUG, "ipcomp6_output: first mbuf too short\n"));
379 ipsec6stat.out_inval++;
383 return ipcomp_output(m, nexthdrp, md, isr, AF_INET6);