Merge from vendor branch READLINE:
[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.6 2004/06/03 18:30:04 joerg 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 #include <netinet/ipprotosw.h>
88 extern struct ipprotosw inetsw[];
89
90 void
91 ipcomp4_input(struct mbuf *m, ...)
92 {
93         int off, proto;
94         struct mbuf *md;
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         __va_list ap;
105
106         __va_start(ap, m);
107         off = __va_arg(ap, int);
108         proto = __va_arg(ap, int);
109         __va_end(ap);
110
111         if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
112                 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
113                     "(packet too short)\n"));
114                 ipsecstat.in_inval++;
115                 goto fail;
116         }
117
118         md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
119         if (!m) {
120                 m = NULL;       /* already freed */
121                 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
122                     "(pulldown failure)\n"));
123                 ipsecstat.in_inval++;
124                 goto fail;
125         }
126         ipcomp = mtod(md, struct ipcomp *);
127         ip = mtod(m, struct ip *);
128         nxt = ipcomp->comp_nxt;
129 #ifdef _IP_VHL
130         hlen = IP_VHL_HL(ip->ip_vhl) << 2;
131 #else
132         hlen = ip->ip_hl << 2;
133 #endif
134
135         cpi = ntohs(ipcomp->comp_cpi);
136
137         if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
138                 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
139                         (caddr_t)&ip->ip_dst, IPPROTO_IPCOMP, htonl(cpi));
140                 if (sav != NULL
141                  && (sav->state == SADB_SASTATE_MATURE
142                   || sav->state == SADB_SASTATE_DYING)) {
143                         cpi = sav->alg_enc;     /* XXX */
144                         /* other parameters to look at? */
145                 }
146         }
147         algo = ipcomp_algorithm_lookup(cpi);
148         if (!algo) {
149                 ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
150                         cpi));
151                 ipsecstat.in_nosa++;
152                 goto fail;
153         }
154
155         /* chop ipcomp header */
156         ipcomp = NULL;
157         md->m_data += sizeof(struct ipcomp);
158         md->m_len -= sizeof(struct ipcomp);
159         m->m_pkthdr.len -= sizeof(struct ipcomp);
160 #ifdef IPLEN_FLIPPED
161         ip->ip_len -= sizeof(struct ipcomp);
162 #else
163         ip->ip_len = htons(ntohs(ip->ip_len) - sizeof(struct ipcomp));
164 #endif
165
166         olen = m->m_pkthdr.len;
167         newlen = m->m_pkthdr.len - off;
168         error = (*algo->decompress)(m, m->m_next, &newlen);
169         if (error != 0) {
170                 if (error == EINVAL)
171                         ipsecstat.in_inval++;
172                 else if (error == ENOBUFS)
173                         ipsecstat.in_nomem++;
174                 m = NULL;
175                 goto fail;
176         }
177         ipsecstat.in_comphist[cpi]++;
178
179         /*
180          * returning decompressed packet onto icmp is meaningless.
181          * mark it decrypted to prevent icmp from attaching original packet.
182          */
183         m->m_flags |= M_DECRYPTED;
184
185         m->m_pkthdr.len = off + newlen;
186         ip = mtod(m, struct ip *);
187     {
188         size_t len;
189 #ifdef IPLEN_FLIPPED
190         len = ip->ip_len;
191 #else
192         len = ntohs(ip->ip_len);
193 #endif
194         /*
195          * be careful about underflow.  also, do not assign exact value
196          * as ip_len is manipulated differently on *BSDs.
197          */
198         len += m->m_pkthdr.len;
199         len -= olen;
200         if (len & ~0xffff) {
201                 /* packet too big after decompress */
202                 ipsecstat.in_inval++;
203                 goto fail;
204         }
205 #ifdef IPLEN_FLIPPED
206         ip->ip_len = len & 0xffff;
207 #else
208         ip->ip_len = htons(len & 0xffff);
209 #endif
210         ip->ip_p = nxt;
211     }
212
213         if (sav) {
214                 key_sa_recordxfer(sav, m);
215                 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
216                         ipsecstat.in_nomem++;
217                         goto fail;
218                 }
219                 key_freesav(sav);
220                 sav = NULL;
221         }
222
223         if (nxt != IPPROTO_DONE) {
224                 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
225                     ipsec4_in_reject(m, NULL)) {
226                         ipsecstat.in_polvio++;
227                         goto fail;
228                 }
229                 (*inetsw[ip_protox[nxt]].pr_input)(m, off, nxt);
230         } else
231                 m_freem(m);
232         m = NULL;
233
234         ipsecstat.in_success++;
235         return;
236
237 fail:
238         if (sav)
239                 key_freesav(sav);
240         if (m)
241                 m_freem(m);
242         return;
243 }
244 #endif /* INET */
245
246 #ifdef INET6
247 int
248 ipcomp6_input(struct mbuf **mp, int *offp, int proto)
249 {
250         struct mbuf *m, *md;
251         int off;
252         struct ip6_hdr *ip6;
253         struct ipcomp *ipcomp;
254         const struct ipcomp_algorithm *algo;
255         u_int16_t cpi;  /* host order */
256         u_int16_t nxt;
257         int error;
258         size_t newlen;
259         struct secasvar *sav = NULL;
260         char *prvnxtp;
261
262         m = *mp;
263         off = *offp;
264
265         md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
266         if (!m) {
267                 m = NULL;       /* already freed */
268                 ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
269                     "(pulldown failure)\n"));
270                 ipsec6stat.in_inval++;
271                 goto fail;
272         }
273         ipcomp = mtod(md, struct ipcomp *);
274         ip6 = mtod(m, struct ip6_hdr *);
275         nxt = ipcomp->comp_nxt;
276
277         cpi = ntohs(ipcomp->comp_cpi);
278
279         if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
280                 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
281                         (caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
282                 if (sav != NULL
283                  && (sav->state == SADB_SASTATE_MATURE
284                   || sav->state == SADB_SASTATE_DYING)) {
285                         cpi = sav->alg_enc;     /* XXX */
286                         /* other parameters to look at? */
287                 }
288         }
289         algo = ipcomp_algorithm_lookup(cpi);
290         if (!algo) {
291                 ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
292                         "dropping the packet for simplicity\n", cpi));
293                 ipsec6stat.in_nosa++;
294                 goto fail;
295         }
296
297         /* chop ipcomp header */
298         ipcomp = NULL;
299         md->m_data += sizeof(struct ipcomp);
300         md->m_len -= sizeof(struct ipcomp);
301         m->m_pkthdr.len -= sizeof(struct ipcomp);
302
303         newlen = m->m_pkthdr.len - off;
304         error = (*algo->decompress)(m, md, &newlen);
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         return nxt;
344
345 fail:
346         if (m)
347                 m_freem(m);
348         if (sav)
349                 key_freesav(sav);
350         return IPPROTO_DONE;
351 }
352 #endif /* INET6 */