1 /* $FreeBSD: releng/6.0/sys/netinet6/ipcomp_input.c 139826 2005-01-07 02:30:35Z imp $ */
2 /* $KAME: ipcomp_input.c,v 1.25 2001/03/01 09:12:09 itojun Exp $ */
3
4 /*-
5 * Copyright (C) 1999 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * RFC2393 IP payload compression protocol (IPComp).
35 */
36
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/syslog.h>
49
50 #include <net/if.h>
51 #include <net/route.h>
52 #include <net/netisr.h>
53 #include <net/zlib.h>
54 #include <machine/cpu.h>
55
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>
62
63 #ifdef INET6
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #endif
67 #include <netinet6/ipcomp.h>
68 #ifdef INET6
69 #include <netinet6/ipcomp6.h>
70 #endif
71
72 #include <netinet6/ipsec.h>
73 #ifdef INET6
74 #include <netinet6/ipsec6.h>
75 #endif
76 #include <netkey/key.h>
77 #include <netkey/keydb.h>
78
79 #include <machine/stdarg.h>
80
81 #include <net/net_osdep.h>
82
83 #define IPLEN_FLIPPED
84
85 #ifdef INET
86 extern struct protosw inetsw[];
87
88 void
89 ipcomp4_input(m, off)
90 struct mbuf *m;
91 int off;
92 {
93 struct mbuf *md;
94 struct ip *ip;
95 struct ipcomp *ipcomp;
96 const struct ipcomp_algorithm *algo;
97 u_int16_t cpi; /* host order */
98 u_int16_t nxt;
99 size_t hlen;
100 int error;
101 size_t newlen, olen;
102 struct secasvar *sav = NULL;
103
104 if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
105 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
106 "(packet too short)\n"));
107 ipsecstat.in_inval++;
108 goto fail;
109 }
110
111 md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
112 if (!md) {
113 m = NULL; /* already freed */
114 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
115 "(pulldown failure)\n"));
116 ipsecstat.in_inval++;
117 goto fail;
118 }
119 ipcomp = mtod(md, struct ipcomp *);
120 ip = mtod(m, struct ip *);
121 nxt = ipcomp->comp_nxt;
122 #ifdef _IP_VHL
123 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
124 #else
125 hlen = ip->ip_hl << 2;
126 #endif
127
128 cpi = ntohs(ipcomp->comp_cpi);
129
130 if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
131 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
132 (caddr_t)&ip->ip_dst, IPPROTO_IPCOMP, htonl(cpi));
133 if (sav != NULL
134 && (sav->state == SADB_SASTATE_MATURE
135 || sav->state == SADB_SASTATE_DYING)) {
136 cpi = sav->alg_enc; /* XXX */
137 /* other parameters to look at? */
138 }
139 }
140 algo = ipcomp_algorithm_lookup(cpi);
141 if (!algo) {
142 ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
143 cpi));
144 ipsecstat.in_nosa++;
145 goto fail;
146 }
147
148 /* chop ipcomp header */
149 ipcomp = NULL;
150 md->m_data += sizeof(struct ipcomp);
151 md->m_len -= sizeof(struct ipcomp);
152 m->m_pkthdr.len -= sizeof(struct ipcomp);
153 #ifdef IPLEN_FLIPPED
154 ip->ip_len -= sizeof(struct ipcomp);
155 #else
156 ip->ip_len = htons(ntohs(ip->ip_len) - sizeof(struct ipcomp));
157 #endif
158
159 olen = m->m_pkthdr.len;
160 newlen = m->m_pkthdr.len - off;
161 error = (*algo->decompress)(m, m->m_next, &newlen);
162 if (error != 0) {
163 if (error == EINVAL)
164 ipsecstat.in_inval++;
165 else if (error == ENOBUFS)
166 ipsecstat.in_nomem++;
167 m = NULL;
168 goto fail;
169 }
170 ipsecstat.in_comphist[cpi]++;
171
172 /*
173 * returning decompressed packet onto icmp is meaningless.
174 * mark it decrypted to prevent icmp from attaching original packet.
175 */
176 m->m_flags |= M_DECRYPTED;
177
178 m->m_pkthdr.len = off + newlen;
179 ip = mtod(m, struct ip *);
180 {
181 size_t len;
182 #ifdef IPLEN_FLIPPED
183 len = ip->ip_len;
184 #else
185 len = ntohs(ip->ip_len);
186 #endif
187 /*
188 * be careful about underflow. also, do not assign exact value
189 * as ip_len is manipulated differently on *BSDs.
190 */
191 len += m->m_pkthdr.len;
192 len -= olen;
193 if (len & ~0xffff) {
194 /* packet too big after decompress */
195 ipsecstat.in_inval++;
196 goto fail;
197 }
198 #ifdef IPLEN_FLIPPED
199 ip->ip_len = len & 0xffff;
200 #else
201 ip->ip_len = htons(len & 0xffff);
202 #endif
203 ip->ip_p = nxt;
204 }
205
206 if (sav) {
207 key_sa_recordxfer(sav, m);
208 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
209 ipsecstat.in_nomem++;
210 goto fail;
211 }
212 key_freesav(sav);
213 sav = NULL;
214 }
215
216 if (nxt != IPPROTO_DONE) {
217 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
218 ipsec4_in_reject(m, NULL)) {
219 ipsecstat.in_polvio++;
220 goto fail;
221 }
222 (*inetsw[ip_protox[nxt]].pr_input)(m, off);
223 } else
224 m_freem(m);
225 m = NULL;
226
227 ipsecstat.in_success++;
228 return;
229
230 fail:
231 if (sav)
232 key_freesav(sav);
233 if (m)
234 m_freem(m);
235 return;
236 }
237 #endif /* INET */
238
239 #ifdef INET6
240 int
241 ipcomp6_input(mp, offp, proto)
242 struct mbuf **mp;
243 int *offp, proto;
244 {
245 struct mbuf *m, *md;
246 int off;
247 struct ip6_hdr *ip6;
248 struct ipcomp *ipcomp;
249 const struct ipcomp_algorithm *algo;
250 u_int16_t cpi; /* host order */
251 u_int16_t nxt;
252 int error;
253 size_t newlen;
254 struct secasvar *sav = NULL;
255 u_int8_t *prvnxtp;
256
257 m = *mp;
258 off = *offp;
259
260 md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
261 if (!m) {
262 m = NULL; /* already freed */
263 ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
264 "(pulldown failure)\n"));
265 ipsec6stat.in_inval++;
266 goto fail;
267 }
268 ipcomp = mtod(md, struct ipcomp *);
269 ip6 = mtod(m, struct ip6_hdr *);
270 nxt = ipcomp->comp_nxt;
271
272 cpi = ntohs(ipcomp->comp_cpi);
273
274 if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
275 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
276 (caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
277 if (sav != NULL
278 && (sav->state == SADB_SASTATE_MATURE
279 || sav->state == SADB_SASTATE_DYING)) {
280 cpi = sav->alg_enc; /* XXX */
281 /* other parameters to look at? */
282 }
283 }
284 algo = ipcomp_algorithm_lookup(cpi);
285 if (!algo) {
286 ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
287 "dropping the packet for simplicity\n", cpi));
288 ipsec6stat.in_nosa++;
289 goto fail;
290 }
291
292 /* chop ipcomp header */
293 ipcomp = NULL;
294 md->m_data += sizeof(struct ipcomp);
295 md->m_len -= sizeof(struct ipcomp);
296 m->m_pkthdr.len -= sizeof(struct ipcomp);
297
298 newlen = m->m_pkthdr.len - off;
299 error = (*algo->decompress)(m, md, &newlen);
300 if (error != 0) {
301 if (error == EINVAL)
302 ipsec6stat.in_inval++;
303 else if (error == ENOBUFS)
304 ipsec6stat.in_nomem++;
305 m = NULL;
306 goto fail;
307 }
308 ipsec6stat.in_comphist[cpi]++;
309 m->m_pkthdr.len = off + newlen;
310
311 /*
312 * returning decompressed packet onto icmp is meaningless.
313 * mark it decrypted to prevent icmp from attaching original packet.
314 */
315 m->m_flags |= M_DECRYPTED;
316
317 /* update next header field */
318 prvnxtp = ip6_get_prevhdr(m, off);
319 *prvnxtp = nxt;
320
321 /*
322 * no need to adjust payload length, as all the IPv6 protocols
323 * look at m->m_pkthdr.len
324 */
325
326 if (sav) {
327 key_sa_recordxfer(sav, m);
328 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
329 ipsec6stat.in_nomem++;
330 goto fail;
331 }
332 key_freesav(sav);
333 sav = NULL;
334 }
335 *offp = off;
336 *mp = m;
337 ipsec6stat.in_success++;
338 return nxt;
339
340 fail:
341 if (m)
342 m_freem(m);
343 if (sav)
344 key_freesav(sav);
345 return IPPROTO_DONE;
346 }
347 #endif /* INET6 */
Cache object: bc428965e521de5c94712646afae29db
|