1 //==========================================================================
3 // src/sys/netinet6/ipcomp_input.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
22 /* $KAME: ipcomp_input.c,v 1.29 2001/09/04 08:43:19 itojun Exp $ */
25 * Copyright (C) 1999 WIDE Project.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the project nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * RFC2393 IP payload compression protocol (IPComp).
57 #include <sys/param.h>
58 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 4)
59 #include <sys/malloc.h>
62 #include <sys/domain.h>
63 #include <sys/protosw.h>
64 #include <sys/socket.h>
65 #include <sys/errno.h>
69 #include <net/route.h>
70 #include <net/netisr.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/ip_ecn.h>
81 #include <netinet/ip6.h>
82 #include <netinet6/ip6_var.h>
84 #include <netinet6/ipcomp.h>
86 #include <netinet6/ipsec.h>
87 #include <netkey/key.h>
88 #include <netkey/keydb.h>
93 extern struct protosw inetsw[];
94 #if defined(__bsdi__) || defined(__NetBSD__)
95 extern u_char ip_protox[];
99 #if (defined(__FreeBSD__) && __FreeBSD__ >= 4)
100 ipcomp4_input(m, off)
105 ipcomp4_input(struct mbuf *m, ...)
107 ipcomp4_input(m, va_alist)
111 #endif /* (defined(__FreeBSD__) && __FreeBSD__ >= 4) */
115 struct ipcomp *ipcomp;
116 const struct ipcomp_algorithm *algo;
117 u_int16_t cpi; /* host order */
122 struct secasvar *sav = NULL;
123 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 4)
126 #endif /* !(defined(__FreeBSD__) && __FreeBSD__ >= 4) */
128 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 4)
130 off = va_arg(ap, int);
131 proto = va_arg(ap, int);
135 if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
136 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
137 "(packet too short)\n"));
138 ipsecstat.in_inval++;
142 md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
144 m = NULL; /* already freed */
145 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
146 "(pulldown failure)\n"));
147 ipsecstat.in_inval++;
150 ipcomp = mtod(md, struct ipcomp *);
151 ip = mtod(m, struct ip *);
152 nxt = ipcomp->comp_nxt;
154 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
156 hlen = ip->ip_hl << 2;
159 cpi = ntohs(ipcomp->comp_cpi);
161 if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
162 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
163 (caddr_t)&ip->ip_dst, IPPROTO_IPCOMP, htonl(cpi));
165 && (sav->state == SADB_SASTATE_MATURE
166 || sav->state == SADB_SASTATE_DYING)) {
167 cpi = sav->alg_enc; /* XXX */
168 /* other parameters to look at? */
171 algo = ipcomp_algorithm_lookup(cpi);
173 ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
179 /* chop ipcomp header */
181 md->m_data += sizeof(struct ipcomp);
182 md->m_len -= sizeof(struct ipcomp);
183 m->m_pkthdr.len -= sizeof(struct ipcomp);
185 ip->ip_len -= sizeof(struct ipcomp);
187 ip->ip_len = htons(ntohs(ip->ip_len) - sizeof(struct ipcomp));
190 olen = m->m_pkthdr.len;
191 newlen = m->m_pkthdr.len - off;
192 error = (*algo->decompress)(m, m->m_next, &newlen);
195 ipsecstat.in_inval++;
196 else if (error == ENOBUFS)
197 ipsecstat.in_nomem++;
201 ipsecstat.in_comphist[cpi]++;
204 * returning decompressed packet onto icmp is meaningless.
205 * mark it decrypted to prevent icmp from attaching original packet.
207 m->m_flags |= M_DECRYPTED;
209 m->m_pkthdr.len = off + newlen;
210 ip = mtod(m, struct ip *);
216 len = ntohs(ip->ip_len);
219 * be careful about underflow. also, do not assign exact value
220 * as ip_len is manipulated differently on *BSDs.
222 len += m->m_pkthdr.len;
225 /* packet too big after decompress */
226 ipsecstat.in_inval++;
230 ip->ip_len = len & 0xffff;
232 ip->ip_len = htons(len & 0xffff);
238 key_sa_recordxfer(sav, m);
239 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
240 ipsecstat.in_nomem++;
247 if (nxt != IPPROTO_DONE) {
248 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
249 ipsec4_in_reject(m, NULL)) {
250 ipsecstat.in_polvio++;
253 #if defined(__FreeBSD__) && __FreeBSD__ >= 4
254 (*inetsw[ip_protox[nxt]].pr_input)(m, off);
256 (*inetsw[ip_protox[nxt]].pr_input)(m, off, nxt);
262 ipsecstat.in_success++;
276 ipcomp6_input(mp, offp, proto)
283 struct ipcomp *ipcomp;
284 const struct ipcomp_algorithm *algo;
285 u_int16_t cpi; /* host order */
289 struct secasvar *sav = NULL;
295 md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
297 m = NULL; /* already freed */
298 ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
299 "(pulldown failure)\n"));
300 ipsec6stat.in_inval++;
303 ipcomp = mtod(md, struct ipcomp *);
304 ip6 = mtod(m, struct ip6_hdr *);
305 nxt = ipcomp->comp_nxt;
307 cpi = ntohs(ipcomp->comp_cpi);
309 if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
310 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
311 (caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
313 && (sav->state == SADB_SASTATE_MATURE
314 || sav->state == SADB_SASTATE_DYING)) {
315 cpi = sav->alg_enc; /* XXX */
316 /* other parameters to look at? */
319 algo = ipcomp_algorithm_lookup(cpi);
321 ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
322 "dropping the packet for simplicity\n", cpi));
323 ipsec6stat.in_nosa++;
327 /* chop ipcomp header */
329 md->m_data += sizeof(struct ipcomp);
330 md->m_len -= sizeof(struct ipcomp);
331 m->m_pkthdr.len -= sizeof(struct ipcomp);
333 newlen = m->m_pkthdr.len - off;
334 error = (*algo->decompress)(m, md, &newlen);
337 ipsec6stat.in_inval++;
338 else if (error == ENOBUFS)
339 ipsec6stat.in_nomem++;
343 ipsec6stat.in_comphist[cpi]++;
344 m->m_pkthdr.len = off + newlen;
347 * returning decompressed packet onto icmp is meaningless.
348 * mark it decrypted to prevent icmp from attaching original packet.
350 m->m_flags |= M_DECRYPTED;
352 /* update next header field */
353 prvnxtp = ip6_get_prevhdr(m, off);
357 * no need to adjust payload length, as all the IPv6 protocols
358 * look at m->m_pkthdr.len
362 key_sa_recordxfer(sav, m);
363 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
364 ipsec6stat.in_nomem++;
372 ipsec6stat.in_success++;