1 /* $FreeBSD: releng/5.1/sys/netinet6/ipcomp_input.c 95023 2002-04-19 04:46:24Z suz $ */
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 int proto;
104
105 if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
106 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
107 "(packet too short)\n"));
108 ipsecstat.in_inval++;
109 goto fail;
110 }
111
112 md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
113 if (!m) {
114 m = NULL; /* already freed */
115 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
116 "(pulldown failure)\n"));
117 ipsecstat.in_inval++;
118 goto fail;
119 }
120 ipcomp = mtod(md, struct ipcomp *);
121 ip = mtod(m, struct ip *);
122 proto = ip->ip_p;
123 nxt = ipcomp->comp_nxt;
124 #ifdef _IP_VHL
125 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
126 #else
127 hlen = ip->ip_hl << 2;
128 #endif
129
130 cpi = ntohs(ipcomp->comp_cpi);
131
132 if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
133 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
134 (caddr_t)&ip->ip_dst, IPPROTO_IPCOMP, htonl(cpi));
135 if (sav != NULL
136 && (sav->state == SADB_SASTATE_MATURE
137 || sav->state == SADB_SASTATE_DYING)) {
138 cpi = sav->alg_enc; /* XXX */
139 /* other parameters to look at? */
140 }
141 }
142 algo = ipcomp_algorithm_lookup(cpi);
143 if (!algo) {
144 ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
145 cpi));
146 ipsecstat.in_nosa++;
147 goto fail;
148 }
149
150 /* chop ipcomp header */
151 ipcomp = NULL;
152 md->m_data += sizeof(struct ipcomp);
153 md->m_len -= sizeof(struct ipcomp);
154 m->m_pkthdr.len -= sizeof(struct ipcomp);
155 #ifdef IPLEN_FLIPPED
156 ip->ip_len -= sizeof(struct ipcomp);
157 #else
158 ip->ip_len = htons(ntohs(ip->ip_len) - sizeof(struct ipcomp));
159 #endif
160
161 olen = m->m_pkthdr.len;
162 newlen = m->m_pkthdr.len - off;
163 error = (*algo->decompress)(m, m->m_next, &newlen);
164 if (error != 0) {
165 if (error == EINVAL)
166 ipsecstat.in_inval++;
167 else if (error == ENOBUFS)
168 ipsecstat.in_nomem++;
169 m = NULL;
170 goto fail;
171 }
172 ipsecstat.in_comphist[cpi]++;
173
174 /*
175 * returning decompressed packet onto icmp is meaningless.
176 * mark it decrypted to prevent icmp from attaching original packet.
177 */
178 m->m_flags |= M_DECRYPTED;
179
180 m->m_pkthdr.len = off + newlen;
181 ip = mtod(m, struct ip *);
182 {
183 size_t len;
184 #ifdef IPLEN_FLIPPED
185 len = ip->ip_len;
186 #else
187 len = ntohs(ip->ip_len);
188 #endif
189 /*
190 * be careful about underflow. also, do not assign exact value
191 * as ip_len is manipulated differently on *BSDs.
192 */
193 len += m->m_pkthdr.len;
194 len -= olen;
195 if (len & ~0xffff) {
196 /* packet too big after decompress */
197 ipsecstat.in_inval++;
198 goto fail;
199 }
200 #ifdef IPLEN_FLIPPED
201 ip->ip_len = len & 0xffff;
202 #else
203 ip->ip_len = htons(len & 0xffff);
204 #endif
205 ip->ip_p = nxt;
206 }
207
208 if (sav) {
209 key_sa_recordxfer(sav, m);
210 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
211 ipsecstat.in_nomem++;
212 goto fail;
213 }
214 key_freesav(sav);
215 sav = NULL;
216 }
217
218 if (nxt != IPPROTO_DONE) {
219 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
220 ipsec4_in_reject(m, NULL)) {
221 ipsecstat.in_polvio++;
222 goto fail;
223 }
224 (*inetsw[ip_protox[nxt]].pr_input)(m, off);
225 } else
226 m_freem(m);
227 m = NULL;
228
229 ipsecstat.in_success++;
230 return;
231
232 fail:
233 if (sav)
234 key_freesav(sav);
235 if (m)
236 m_freem(m);
237 return;
238 }
239 #endif /* INET */
240
241 #ifdef INET6
242 int
243 ipcomp6_input(mp, offp, proto)
244 struct mbuf **mp;
245 int *offp, proto;
246 {
247 struct mbuf *m, *md;
248 int off;
249 struct ip6_hdr *ip6;
250 struct ipcomp *ipcomp;
251 const struct ipcomp_algorithm *algo;
252 u_int16_t cpi; /* host order */
253 u_int16_t nxt;
254 int error;
255 size_t newlen;
256 struct secasvar *sav = NULL;
257 char *prvnxtp;
258
259 m = *mp;
260 off = *offp;
261
262 md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
263 if (!m) {
264 m = NULL; /* already freed */
265 ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
266 "(pulldown failure)\n"));
267 ipsec6stat.in_inval++;
268 goto fail;
269 }
270 ipcomp = mtod(md, struct ipcomp *);
271 ip6 = mtod(m, struct ip6_hdr *);
272 nxt = ipcomp->comp_nxt;
273
274 cpi = ntohs(ipcomp->comp_cpi);
275
276 if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
277 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
278 (caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
279 if (sav != NULL
280 && (sav->state == SADB_SASTATE_MATURE
281 || sav->state == SADB_SASTATE_DYING)) {
282 cpi = sav->alg_enc; /* XXX */
283 /* other parameters to look at? */
284 }
285 }
286 algo = ipcomp_algorithm_lookup(cpi);
287 if (!algo) {
288 ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
289 "dropping the packet for simplicity\n", cpi));
290 ipsec6stat.in_nosa++;
291 goto fail;
292 }
293
294 /* chop ipcomp header */
295 ipcomp = NULL;
296 md->m_data += sizeof(struct ipcomp);
297 md->m_len -= sizeof(struct ipcomp);
298 m->m_pkthdr.len -= sizeof(struct ipcomp);
299
300 newlen = m->m_pkthdr.len - off;
301 error = (*algo->decompress)(m, md, &newlen);
302 if (error != 0) {
303 if (error == EINVAL)
304 ipsec6stat.in_inval++;
305 else if (error == ENOBUFS)
306 ipsec6stat.in_nomem++;
307 m = NULL;
308 goto fail;
309 }
310 ipsec6stat.in_comphist[cpi]++;
311 m->m_pkthdr.len = off + newlen;
312
313 /*
314 * returning decompressed packet onto icmp is meaningless.
315 * mark it decrypted to prevent icmp from attaching original packet.
316 */
317 m->m_flags |= M_DECRYPTED;
318
319 /* update next header field */
320 prvnxtp = ip6_get_prevhdr(m, off);
321 *prvnxtp = nxt;
322
323 /*
324 * no need to adjust payload length, as all the IPv6 protocols
325 * look at m->m_pkthdr.len
326 */
327
328 if (sav) {
329 key_sa_recordxfer(sav, m);
330 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
331 ipsec6stat.in_nomem++;
332 goto fail;
333 }
334 key_freesav(sav);
335 sav = NULL;
336 }
337 *offp = off;
338 *mp = m;
339 ipsec6stat.in_success++;
340 return nxt;
341
342 fail:
343 if (m)
344 m_freem(m);
345 if (sav)
346 key_freesav(sav);
347 return IPPROTO_DONE;
348 }
349 #endif /* INET6 */
Cache object: a190c0fc6366fff80974cd69d07108a5
|