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