]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/ipcomp_input.c
This commit was generated by cvs2svn to compensate for changes in r170349,
[FreeBSD/FreeBSD.git] / sys / netinet6 / ipcomp_input.c
1 /*      $FreeBSD$       */
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 #define IPLEN_FLIPPED
82
83 #ifdef INET
84 extern struct protosw inetsw[];
85
86 void
87 ipcomp4_input(m, off)
88         struct mbuf *m;
89         int off;
90 {
91         struct mbuf *md;
92         struct ip *ip;
93         struct ipcomp *ipcomp;
94         const struct ipcomp_algorithm *algo;
95         u_int16_t cpi;  /* host order */
96         u_int16_t nxt;
97         size_t hlen;
98         int error;
99         size_t newlen, olen;
100         struct secasvar *sav = NULL;
101
102         if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
103                 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
104                     "(packet too short)\n"));
105                 ipsecstat.in_inval++;
106                 goto fail;
107         }
108
109         md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
110         if (!md) {
111                 m = NULL;       /* already freed */
112                 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
113                     "(pulldown failure)\n"));
114                 ipsecstat.in_inval++;
115                 goto fail;
116         }
117         ipcomp = mtod(md, struct ipcomp *);
118         ip = mtod(m, struct ip *);
119         nxt = ipcomp->comp_nxt;
120 #ifdef _IP_VHL
121         hlen = IP_VHL_HL(ip->ip_vhl) << 2;
122 #else
123         hlen = ip->ip_hl << 2;
124 #endif
125
126         cpi = ntohs(ipcomp->comp_cpi);
127
128         if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
129                 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
130                         (caddr_t)&ip->ip_dst, IPPROTO_IPCOMP, htonl(cpi));
131                 if (sav != NULL
132                  && (sav->state == SADB_SASTATE_MATURE
133                   || sav->state == SADB_SASTATE_DYING)) {
134                         cpi = sav->alg_enc;     /* XXX */
135                         /* other parameters to look at? */
136                 }
137         }
138         algo = ipcomp_algorithm_lookup(cpi);
139         if (!algo) {
140                 ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
141                         cpi));
142                 ipsecstat.in_nosa++;
143                 goto fail;
144         }
145
146         /* chop ipcomp header */
147         ipcomp = NULL;
148         md->m_data += sizeof(struct ipcomp);
149         md->m_len -= sizeof(struct ipcomp);
150         m->m_pkthdr.len -= sizeof(struct ipcomp);
151 #ifdef IPLEN_FLIPPED
152         ip->ip_len -= sizeof(struct ipcomp);
153 #else
154         ip->ip_len = htons(ntohs(ip->ip_len) - sizeof(struct ipcomp));
155 #endif
156
157         olen = m->m_pkthdr.len;
158         newlen = m->m_pkthdr.len - off;
159         error = (*algo->decompress)(m, m->m_next, &newlen);
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 ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
216                     ipsec4_in_reject(m, NULL)) {
217                         ipsecstat.in_polvio++;
218                         goto fail;
219                 }
220                 (*inetsw[ip_protox[nxt]].pr_input)(m, off);
221         } else
222                 m_freem(m);
223         m = NULL;
224
225         ipsecstat.in_success++;
226         return;
227
228 fail:
229         if (sav)
230                 key_freesav(sav);
231         if (m)
232                 m_freem(m);
233         return;
234 }
235 #endif /* INET */
236
237 #ifdef INET6
238 int
239 ipcomp6_input(mp, offp, proto)
240         struct mbuf **mp;
241         int *offp, proto;
242 {
243         struct mbuf *m, *md;
244         int off;
245         struct ip6_hdr *ip6;
246         struct ipcomp *ipcomp;
247         const struct ipcomp_algorithm *algo;
248         u_int16_t cpi;  /* host order */
249         u_int16_t nxt;
250         int error;
251         size_t newlen;
252         struct secasvar *sav = NULL;
253         u_int8_t *prvnxtp;
254
255         m = *mp;
256         off = *offp;
257
258         md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
259         if (!md) {
260                 m = NULL;       /* already freed */
261                 ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
262                     "(pulldown failure)\n"));
263                 ipsec6stat.in_inval++;
264                 goto fail;
265         }
266         ipcomp = mtod(md, struct ipcomp *);
267         ip6 = mtod(m, struct ip6_hdr *);
268         nxt = ipcomp->comp_nxt;
269
270         cpi = ntohs(ipcomp->comp_cpi);
271
272         if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
273                 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
274                     (caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
275                 if (sav != NULL
276                  && (sav->state == SADB_SASTATE_MATURE
277                   || sav->state == SADB_SASTATE_DYING)) {
278                         cpi = sav->alg_enc;     /* XXX */
279                         /* other parameters to look at? */
280                 }
281         }
282         algo = ipcomp_algorithm_lookup(cpi);
283         if (!algo) {
284                 ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
285                         "dropping the packet for simplicity\n", cpi));
286                 ipsec6stat.in_nosa++;
287                 goto fail;
288         }
289
290         /* chop ipcomp header */
291         ipcomp = NULL;
292         md->m_data += sizeof(struct ipcomp);
293         md->m_len -= sizeof(struct ipcomp);
294         m->m_pkthdr.len -= sizeof(struct ipcomp);
295
296         newlen = m->m_pkthdr.len - off;
297         error = (*algo->decompress)(m, md, &newlen);
298         if (error != 0) {
299                 if (error == EINVAL)
300                         ipsec6stat.in_inval++;
301                 else if (error == ENOBUFS)
302                         ipsec6stat.in_nomem++;
303                 m = NULL;
304                 goto fail;
305         }
306         ipsec6stat.in_comphist[cpi]++;
307         m->m_pkthdr.len = off + newlen;
308
309         /*
310          * returning decompressed packet onto icmp is meaningless.
311          * mark it decrypted to prevent icmp from attaching original packet.
312          */
313         m->m_flags |= M_DECRYPTED;
314
315         /* update next header field */
316         prvnxtp = ip6_get_prevhdr(m, off);
317         *prvnxtp = nxt;
318
319         /*
320          * no need to adjust payload length, as all the IPv6 protocols
321          * look at m->m_pkthdr.len
322          */
323
324         if (sav) {
325                 key_sa_recordxfer(sav, m);
326                 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
327                         ipsec6stat.in_nomem++;
328                         goto fail;
329                 }
330                 key_freesav(sav);
331                 sav = NULL;
332         }
333         *offp = off;
334         *mp = m;
335         ipsec6stat.in_success++;
336         return nxt;
337
338 fail:
339         if (m)
340                 m_freem(m);
341         if (sav)
342                 key_freesav(sav);
343         return IPPROTO_DONE;
344 }
345 #endif /* INET6 */