]>
Commit | Line | Data |
---|---|---|
9bccf70c A |
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 $ */ | |
1c79356b A |
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 | ||
1c79356b A |
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> | |
2d21ac55 | 52 | #include <libkern/zlib.h> |
1c79356b | 53 | #include <kern/cpu_number.h> |
91447636 | 54 | #include <kern/locks.h> |
1c79356b A |
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> | |
2d21ac55 | 62 | #include <netinet/kpi_ipfilter_var.h> |
1c79356b A |
63 | |
64 | #if INET6 | |
65 | #include <netinet/ip6.h> | |
66 | #include <netinet6/ip6_var.h> | |
67 | #endif | |
68 | #include <netinet6/ipcomp.h> | |
9bccf70c A |
69 | #if INET6 |
70 | #include <netinet6/ipcomp6.h> | |
71 | #endif | |
1c79356b A |
72 | |
73 | #include <netinet6/ipsec.h> | |
9bccf70c A |
74 | #if INET6 |
75 | #include <netinet6/ipsec6.h> | |
76 | #endif | |
1c79356b A |
77 | #include <netkey/key.h> |
78 | #include <netkey/keydb.h> | |
1c79356b A |
79 | |
80 | #include <net/net_osdep.h> | |
6d2010ae | 81 | #include <mach/sdt.h> |
1c79356b A |
82 | |
83 | #define IPLEN_FLIPPED | |
84 | ||
1c79356b A |
85 | void |
86 | ipcomp4_input(struct mbuf *m, int off) | |
87 | { | |
9bccf70c | 88 | struct mbuf *md; |
1c79356b A |
89 | struct ip *ip; |
90 | struct ipcomp *ipcomp; | |
9bccf70c | 91 | const struct ipcomp_algorithm *algo; |
1c79356b A |
92 | u_int16_t cpi; /* host order */ |
93 | u_int16_t nxt; | |
94 | size_t hlen; | |
95 | int error; | |
96 | size_t newlen, olen; | |
97 | struct secasvar *sav = NULL; | |
98 | ||
99 | ||
9bccf70c | 100 | if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) { |
1c79356b | 101 | ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed " |
9bccf70c | 102 | "(packet too short)\n")); |
2d21ac55 | 103 | IPSEC_STAT_INCREMENT(ipsecstat.in_inval); |
1c79356b A |
104 | goto fail; |
105 | } | |
1c79356b | 106 | |
9bccf70c | 107 | md = m_pulldown(m, off, sizeof(*ipcomp), NULL); |
cf7d32b8 | 108 | if (!md) { |
9bccf70c A |
109 | m = NULL; /*already freed*/ |
110 | ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed " | |
111 | "(pulldown failure)\n")); | |
2d21ac55 | 112 | IPSEC_STAT_INCREMENT(ipsecstat.in_inval); |
9bccf70c A |
113 | goto fail; |
114 | } | |
115 | ipcomp = mtod(md, struct ipcomp *); | |
1c79356b | 116 | ip = mtod(m, struct ip *); |
1c79356b A |
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 | } | |
9bccf70c | 136 | algo = ipcomp_algorithm_lookup(cpi); |
1c79356b A |
137 | if (!algo) { |
138 | ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n", | |
139 | cpi)); | |
2d21ac55 | 140 | IPSEC_STAT_INCREMENT(ipsecstat.in_nosa); |
1c79356b A |
141 | goto fail; |
142 | } | |
143 | ||
144 | /* chop ipcomp header */ | |
145 | ipcomp = NULL; | |
9bccf70c A |
146 | md->m_data += sizeof(struct ipcomp); |
147 | md->m_len -= sizeof(struct ipcomp); | |
1c79356b A |
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 | error = (*algo->decompress)(m, m->m_next, &newlen); | |
158 | if (error != 0) { | |
2d21ac55 A |
159 | if (error == EINVAL) { |
160 | IPSEC_STAT_INCREMENT(ipsecstat.in_inval); | |
161 | } else if (error == ENOBUFS) | |
162 | IPSEC_STAT_INCREMENT(ipsecstat.in_nomem); | |
1c79356b A |
163 | m = NULL; |
164 | goto fail; | |
165 | } | |
2d21ac55 | 166 | IPSEC_STAT_INCREMENT(ipsecstat.in_comphist[cpi]); |
1c79356b A |
167 | |
168 | /* | |
169 | * returning decompressed packet onto icmp is meaningless. | |
170 | * mark it decrypted to prevent icmp from attaching original packet. | |
171 | */ | |
172 | m->m_flags |= M_DECRYPTED; | |
173 | ||
174 | m->m_pkthdr.len = off + newlen; | |
175 | ip = mtod(m, struct ip *); | |
176 | { | |
177 | size_t len; | |
178 | #ifdef IPLEN_FLIPPED | |
179 | len = ip->ip_len; | |
180 | #else | |
181 | len = ntohs(ip->ip_len); | |
182 | #endif | |
183 | /* | |
184 | * be careful about underflow. also, do not assign exact value | |
185 | * as ip_len is manipulated differently on *BSDs. | |
186 | */ | |
187 | len += m->m_pkthdr.len; | |
188 | len -= olen; | |
189 | if (len & ~0xffff) { | |
190 | /* packet too big after decompress */ | |
2d21ac55 | 191 | IPSEC_STAT_INCREMENT(ipsecstat.in_inval); |
1c79356b A |
192 | goto fail; |
193 | } | |
194 | #ifdef IPLEN_FLIPPED | |
195 | ip->ip_len = len & 0xffff; | |
196 | #else | |
197 | ip->ip_len = htons(len & 0xffff); | |
198 | #endif | |
199 | ip->ip_p = nxt; | |
200 | } | |
201 | ||
202 | if (sav) { | |
203 | key_sa_recordxfer(sav, m); | |
9bccf70c | 204 | if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) { |
2d21ac55 | 205 | IPSEC_STAT_INCREMENT(ipsecstat.in_nomem); |
9bccf70c A |
206 | goto fail; |
207 | } | |
2d21ac55 | 208 | key_freesav(sav, KEY_SADB_UNLOCKED); |
1c79356b A |
209 | sav = NULL; |
210 | } | |
211 | ||
9bccf70c A |
212 | if (nxt != IPPROTO_DONE) { |
213 | if ((ip_protox[nxt]->pr_flags & PR_LASTHDR) != 0 && | |
214 | ipsec4_in_reject(m, NULL)) { | |
2d21ac55 | 215 | IPSEC_STAT_INCREMENT(ipsecstat.in_polvio); |
9bccf70c A |
216 | goto fail; |
217 | } | |
6d2010ae A |
218 | |
219 | DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL, | |
220 | struct ip *, ip, struct ifnet *, m->m_pkthdr.rcvif, | |
221 | struct ip *, ip, struct ip6_hdr *, NULL); | |
222 | ||
91447636 | 223 | ip_proto_dispatch_in(m, off, nxt, 0); |
9bccf70c | 224 | } else |
1c79356b A |
225 | m_freem(m); |
226 | m = NULL; | |
227 | ||
2d21ac55 | 228 | IPSEC_STAT_INCREMENT(ipsecstat.in_success); |
1c79356b A |
229 | return; |
230 | ||
231 | fail: | |
232 | if (sav) | |
2d21ac55 | 233 | key_freesav(sav, KEY_SADB_UNLOCKED); |
91447636 | 234 | |
1c79356b A |
235 | if (m) |
236 | m_freem(m); | |
237 | return; | |
238 | } | |
1c79356b A |
239 | |
240 | #if INET6 | |
241 | int | |
6d2010ae | 242 | ipcomp6_input(struct mbuf **mp, int *offp, int proto) |
1c79356b | 243 | { |
6d2010ae | 244 | #pragma unused(proto) |
1c79356b A |
245 | struct mbuf *m, *md; |
246 | int off; | |
247 | struct ip6_hdr *ip6; | |
1c79356b | 248 | struct ipcomp *ipcomp; |
9bccf70c | 249 | const struct ipcomp_algorithm *algo; |
1c79356b A |
250 | u_int16_t cpi; /* host order */ |
251 | u_int16_t nxt; | |
252 | int error; | |
253 | size_t newlen; | |
254 | struct secasvar *sav = NULL; | |
9bccf70c | 255 | char *prvnxtp; |
1c79356b A |
256 | |
257 | m = *mp; | |
258 | off = *offp; | |
259 | ||
9bccf70c | 260 | md = m_pulldown(m, off, sizeof(*ipcomp), NULL); |
cf7d32b8 | 261 | if (!md) { |
9bccf70c A |
262 | m = NULL; /*already freed*/ |
263 | ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed " | |
264 | "(pulldown failure)\n")); | |
2d21ac55 | 265 | IPSEC_STAT_INCREMENT(ipsec6stat.in_inval); |
1c79356b A |
266 | goto fail; |
267 | } | |
9bccf70c | 268 | ipcomp = mtod(md, struct ipcomp *); |
1c79356b | 269 | ip6 = mtod(m, struct ip6_hdr *); |
1c79356b | 270 | nxt = ipcomp->comp_nxt; |
9bccf70c | 271 | |
1c79356b A |
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 | } | |
9bccf70c | 284 | algo = ipcomp_algorithm_lookup(cpi); |
1c79356b A |
285 | if (!algo) { |
286 | ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; " | |
287 | "dropping the packet for simplicity\n", cpi)); | |
2d21ac55 | 288 | IPSEC_STAT_INCREMENT(ipsec6stat.in_nosa); |
1c79356b A |
289 | goto fail; |
290 | } | |
291 | ||
9bccf70c A |
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; | |
1c79356b A |
299 | error = (*algo->decompress)(m, md, &newlen); |
300 | if (error != 0) { | |
2d21ac55 A |
301 | if (error == EINVAL) { |
302 | IPSEC_STAT_INCREMENT(ipsec6stat.in_inval); | |
303 | } else if (error == ENOBUFS) | |
304 | IPSEC_STAT_INCREMENT(ipsec6stat.in_nomem); | |
1c79356b A |
305 | m = NULL; |
306 | goto fail; | |
307 | } | |
2d21ac55 | 308 | IPSEC_STAT_INCREMENT(ipsec6stat.in_comphist[cpi]); |
9bccf70c | 309 | m->m_pkthdr.len = off + newlen; |
1c79356b A |
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 | ||
9bccf70c | 317 | /* update next header field */ |
1c79356b A |
318 | prvnxtp = ip6_get_prevhdr(m, off); |
319 | *prvnxtp = nxt; | |
1c79356b | 320 | |
9bccf70c A |
321 | /* |
322 | * no need to adjust payload length, as all the IPv6 protocols | |
323 | * look at m->m_pkthdr.len | |
324 | */ | |
1c79356b A |
325 | |
326 | if (sav) { | |
327 | key_sa_recordxfer(sav, m); | |
9bccf70c | 328 | if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) { |
2d21ac55 | 329 | IPSEC_STAT_INCREMENT(ipsec6stat.in_nomem); |
9bccf70c A |
330 | goto fail; |
331 | } | |
2d21ac55 | 332 | key_freesav(sav, KEY_SADB_UNLOCKED); |
1c79356b A |
333 | sav = NULL; |
334 | } | |
335 | *offp = off; | |
336 | *mp = m; | |
2d21ac55 | 337 | IPSEC_STAT_INCREMENT(ipsec6stat.in_success); |
1c79356b A |
338 | return nxt; |
339 | ||
340 | fail: | |
341 | if (m) | |
342 | m_freem(m); | |
343 | if (sav) | |
2d21ac55 | 344 | key_freesav(sav, KEY_SADB_UNLOCKED); |
1c79356b A |
345 | return IPPROTO_DONE; |
346 | } | |
347 | #endif /* INET6 */ |