20778172f0220df940be63fe023287a83bb2a7d0
[dragonfly.git] / sys / netinet6 / ipcomp_input.c
1 /*      $FreeBSD: src/sys/netinet6/ipcomp_input.c,v 1.1.2.3 2002/04/28 05:40:27 suz Exp $       */
2 /*      $DragonFly: src/sys/netinet6/ipcomp_input.c,v 1.9 2007/11/22 20:19:43 hasso Exp $       */
3 /*      $KAME: ipcomp_input.c,v 1.25 2001/03/01 09:12:09 itojun Exp $   */
4
5 /*
6  * Copyright (C) 1999 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * RFC2393 IP payload compression protocol (IPComp).
36  */
37
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/errno.h>
48 #include <sys/time.h>
49 #include <sys/syslog.h>
50
51 #include <net/if.h>
52 #include <net/route.h>
53 #include <net/netisr.h>
54 #include <net/zlib.h>
55 #include <machine/cpu.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/ip_ecn.h>
63
64 #ifdef INET6
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #endif
68 #include <netinet6/ipcomp.h>
69 #ifdef INET6
70 #include <netinet6/ipcomp6.h>
71 #endif
72
73 #include <netinet6/ipsec.h>
74 #ifdef INET6
75 #include <netinet6/ipsec6.h>
76 #endif
77 #include <netproto/key/key.h>
78 #include <netproto/key/keydb.h>
79
80 #include <machine/stdarg.h>
81
82 #include <net/net_osdep.h>
83
84 #define IPLEN_FLIPPED
85
86 #ifdef INET
87 extern struct protosw inetsw[];
88
89 int
90 ipcomp4_input(struct mbuf **mp, int *offp, int proto)
91 {
92         int off;
93         struct mbuf *md;
94         struct mbuf *m;
95         struct ip *ip;
96         struct ipcomp *ipcomp;
97         const struct ipcomp_algorithm *algo;
98         u_int16_t cpi;  /* host order */
99         u_int16_t nxt;
100         size_t hlen;
101         int error;
102         size_t newlen, olen;
103         struct secasvar *sav = NULL;
104
105         off = *offp;
106         m = *mp;
107         *mp = NULL;
108
109         if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
110                 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
111                     "(packet too short)\n"));
112                 ipsecstat.in_inval++;
113                 goto fail;
114         }
115
116         md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
117         if (!md) {
118                 m = NULL;       /* already freed */
119                 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
120                     "(pulldown failure)\n"));
121                 ipsecstat.in_inval++;
122                 goto fail;
123         }
124         ipcomp = mtod(md, struct ipcomp *);
125         ip = mtod(m, struct ip *);
126         nxt = ipcomp->comp_nxt;
127 #ifdef _IP_VHL
128         hlen = IP_VHL_HL(ip->ip_vhl) << 2;
129 #else
130         hlen = ip->ip_hl << 2;
131 #endif
132
133         cpi = ntohs(ipcomp->comp_cpi);
134
135         if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
136                 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
137                         (caddr_t)&ip->ip_dst, IPPROTO_IPCOMP, htonl(cpi));
138                 if (sav != NULL
139                  && (sav->state == SADB_SASTATE_MATURE
140                   || sav->state == SADB_SASTATE_DYING)) {
141                         cpi = sav->alg_enc;     /* XXX */
142                         /* other parameters to look at? */
143                 }
144         }
145         algo = ipcomp_algorithm_lookup(cpi);
146         if (!algo) {
147                 ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
148                         cpi));
149                 ipsecstat.in_nosa++;
150                 goto fail;
151         }
152
153         /* chop ipcomp header */
154         ipcomp = NULL;
155         md->m_data += sizeof(struct ipcomp);
156         md->m_len -= sizeof(struct ipcomp);
157         m->m_pkthdr.len -= sizeof(struct ipcomp);
158 #ifdef IPLEN_FLIPPED
159         ip->ip_len -= sizeof(struct ipcomp);
160 #else
161         ip->ip_len = htons(ntohs(ip->ip_len) - sizeof(struct ipcomp));
162 #endif
163
164         olen = m->m_pkthdr.len;
165         newlen = m->m_pkthdr.len - off;
166         error = (*algo->decompress)(m, m->m_next, &newlen);
167         if (error != 0) {
168                 if (error == EINVAL)
169                         ipsecstat.in_inval++;
170                 else if (error == ENOBUFS)
171                         ipsecstat.in_nomem++;
172                 m = NULL;
173                 goto fail;
174         }
175         ipsecstat.in_comphist[cpi]++;
176
177         /*
178          * returning decompressed packet onto icmp is meaningless.
179          * mark it decrypted to prevent icmp from attaching original packet.
180          */
181         m->m_flags |= M_DECRYPTED;
182
183         m->m_pkthdr.len = off + newlen;
184         ip = mtod(m, struct ip *);
185     {
186         size_t len;
187 #ifdef IPLEN_FLIPPED
188         len = ip->ip_len;
189 #else
190         len = ntohs(ip->ip_len);
191 #endif
192         /*
193          * be careful about underflow.  also, do not assign exact value
194          * as ip_len is manipulated differently on *BSDs.
195          */
196         len += m->m_pkthdr.len;
197         len -= olen;
198         if (len & ~0xffff) {
199                 /* packet too big after decompress */
200                 ipsecstat.in_inval++;
201                 goto fail;
202         }
203 #ifdef IPLEN_FLIPPED
204         ip->ip_len = len & 0xffff;
205 #else
206         ip->ip_len = htons(len & 0xffff);
207 #endif
208         ip->ip_p = nxt;
209     }
210
211         if (sav) {
212                 key_sa_recordxfer(sav, m);
213                 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
214                         ipsecstat.in_nomem++;
215                         goto fail;
216                 }
217                 key_freesav(sav);
218                 sav = NULL;
219         }
220
221         if (nxt != IPPROTO_DONE) {
222                 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) &&
223                     ipsec4_in_reject(m, NULL)) {
224                         ipsecstat.in_polvio++;
225                         goto fail;
226                 }
227                 *mp = m;
228                 *offp = off;
229                 (*inetsw[ip_protox[nxt]].pr_input)(mp, offp, nxt);
230         } else {
231                 m_freem(m);
232         }
233         m = NULL;
234
235         ipsecstat.in_success++;
236         return(IPPROTO_DONE);
237
238 fail:
239         if (sav)
240                 key_freesav(sav);
241         if (m)
242                 m_freem(m);
243         return(IPPROTO_DONE);
244 }
245 #endif /* INET */
246
247 #ifdef INET6
248 int
249 ipcomp6_input(struct mbuf **mp, int *offp, int proto)
250 {
251         struct mbuf *m, *md;
252         int off;
253         struct ip6_hdr *ip6;
254         struct ipcomp *ipcomp;
255         const struct ipcomp_algorithm *algo;
256         u_int16_t cpi;  /* host order */
257         u_int16_t nxt;
258         int error;
259         size_t newlen;
260         struct secasvar *sav = NULL;
261         char *prvnxtp;
262
263         m = *mp;
264         off = *offp;
265
266         md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
267         if (!md) {
268                 m = NULL;       /* already freed */
269                 ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
270                     "(pulldown failure)\n"));
271                 ipsec6stat.in_inval++;
272                 goto fail;
273         }
274         ipcomp = mtod(md, struct ipcomp *);
275         ip6 = mtod(m, struct ip6_hdr *);
276         nxt = ipcomp->comp_nxt;
277
278         cpi = ntohs(ipcomp->comp_cpi);
279
280         if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
281                 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
282                         (caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
283                 if (sav != NULL
284                  && (sav->state == SADB_SASTATE_MATURE
285                   || sav->state == SADB_SASTATE_DYING)) {
286                         cpi = sav->alg_enc;     /* XXX */
287                         /* other parameters to look at? */
288                 }
289         }
290         algo = ipcomp_algorithm_lookup(cpi);
291         if (!algo) {
292                 ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
293                         "dropping the packet for simplicity\n", cpi));
294                 ipsec6stat.in_nosa++;
295                 goto fail;
296         }
297
298         /* chop ipcomp header */
299         ipcomp = NULL;
300         md->m_data += sizeof(struct ipcomp);
301         md->m_len -= sizeof(struct ipcomp);
302         m->m_pkthdr.len -= sizeof(struct ipcomp);
303
304         newlen = m->m_pkthdr.len - off;
305         error = (*algo->decompress)(m, md, &newlen);
306         if (error != 0) {
307                 if (error == EINVAL)
308                         ipsec6stat.in_inval++;
309                 else if (error == ENOBUFS)
310                         ipsec6stat.in_nomem++;
311                 m = NULL;
312                 goto fail;
313         }
314         ipsec6stat.in_comphist[cpi]++;
315         m->m_pkthdr.len = off + newlen;
316
317         /*
318          * returning decompressed packet onto icmp is meaningless.
319          * mark it decrypted to prevent icmp from attaching original packet.
320          */
321         m->m_flags |= M_DECRYPTED;
322
323         /* update next header field */
324         prvnxtp = ip6_get_prevhdr(m, off);
325         *prvnxtp = nxt;
326
327         /*
328          * no need to adjust payload length, as all the IPv6 protocols
329          * look at m->m_pkthdr.len
330          */
331
332         if (sav) {
333                 key_sa_recordxfer(sav, m);
334                 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
335                         ipsec6stat.in_nomem++;
336                         goto fail;
337                 }
338                 key_freesav(sav);
339                 sav = NULL;
340         }
341         *offp = off;
342         *mp = m;
343         ipsec6stat.in_success++;
344         return nxt;
345
346 fail:
347         if (m)
348                 m_freem(m);
349         if (sav)
350                 key_freesav(sav);
351         return IPPROTO_DONE;
352 }
353 #endif /* INET6 */