Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:28:52 dillon 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 <netkey/key.h>
78 #include <netkey/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 #if __STDC__
92 ipcomp4_input(struct mbuf *m, ...)
93 #else
94 ipcomp4_input(m, va_alist)
95         struct mbuf *m;
96         va_dcl
97 #endif
98 {
99         struct mbuf *md;
100         struct ip *ip;
101         struct ipcomp *ipcomp;
102         const struct ipcomp_algorithm *algo;
103         u_int16_t cpi;  /* host order */
104         u_int16_t nxt;
105         size_t hlen;
106         int error;
107         size_t newlen, olen;
108         struct secasvar *sav = NULL;
109         int off, proto;
110         va_list ap;
111
112         va_start(ap, m);
113         off = va_arg(ap, int);
114         proto = va_arg(ap, int);
115         va_end(ap);
116
117         if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
118                 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
119                     "(packet too short)\n"));
120                 ipsecstat.in_inval++;
121                 goto fail;
122         }
123
124         md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
125         if (!m) {
126                 m = NULL;       /* already freed */
127                 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
128                     "(pulldown failure)\n"));
129                 ipsecstat.in_inval++;
130                 goto fail;
131         }
132         ipcomp = mtod(md, struct ipcomp *);
133         ip = mtod(m, struct ip *);
134         nxt = ipcomp->comp_nxt;
135 #ifdef _IP_VHL
136         hlen = IP_VHL_HL(ip->ip_vhl) << 2;
137 #else
138         hlen = ip->ip_hl << 2;
139 #endif
140
141         cpi = ntohs(ipcomp->comp_cpi);
142
143         if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
144                 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
145                         (caddr_t)&ip->ip_dst, IPPROTO_IPCOMP, htonl(cpi));
146                 if (sav != NULL
147                  && (sav->state == SADB_SASTATE_MATURE
148                   || sav->state == SADB_SASTATE_DYING)) {
149                         cpi = sav->alg_enc;     /* XXX */
150                         /* other parameters to look at? */
151                 }
152         }
153         algo = ipcomp_algorithm_lookup(cpi);
154         if (!algo) {
155                 ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
156                         cpi));
157                 ipsecstat.in_nosa++;
158                 goto fail;
159         }
160
161         /* chop ipcomp header */
162         ipcomp = NULL;
163         md->m_data += sizeof(struct ipcomp);
164         md->m_len -= sizeof(struct ipcomp);
165         m->m_pkthdr.len -= sizeof(struct ipcomp);
166 #ifdef IPLEN_FLIPPED
167         ip->ip_len -= sizeof(struct ipcomp);
168 #else
169         ip->ip_len = htons(ntohs(ip->ip_len) - sizeof(struct ipcomp));
170 #endif
171
172         olen = m->m_pkthdr.len;
173         newlen = m->m_pkthdr.len - off;
174         error = (*algo->decompress)(m, m->m_next, &newlen);
175         if (error != 0) {
176                 if (error == EINVAL)
177                         ipsecstat.in_inval++;
178                 else if (error == ENOBUFS)
179                         ipsecstat.in_nomem++;
180                 m = NULL;
181                 goto fail;
182         }
183         ipsecstat.in_comphist[cpi]++;
184
185         /*
186          * returning decompressed packet onto icmp is meaningless.
187          * mark it decrypted to prevent icmp from attaching original packet.
188          */
189         m->m_flags |= M_DECRYPTED;
190
191         m->m_pkthdr.len = off + newlen;
192         ip = mtod(m, struct ip *);
193     {
194         size_t len;
195 #ifdef IPLEN_FLIPPED
196         len = ip->ip_len;
197 #else
198         len = ntohs(ip->ip_len);
199 #endif
200         /*
201          * be careful about underflow.  also, do not assign exact value
202          * as ip_len is manipulated differently on *BSDs.
203          */
204         len += m->m_pkthdr.len;
205         len -= olen;
206         if (len & ~0xffff) {
207                 /* packet too big after decompress */
208                 ipsecstat.in_inval++;
209                 goto fail;
210         }
211 #ifdef IPLEN_FLIPPED
212         ip->ip_len = len & 0xffff;
213 #else
214         ip->ip_len = htons(len & 0xffff);
215 #endif
216         ip->ip_p = nxt;
217     }
218
219         if (sav) {
220                 key_sa_recordxfer(sav, m);
221                 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
222                         ipsecstat.in_nomem++;
223                         goto fail;
224                 }
225                 key_freesav(sav);
226                 sav = NULL;
227         }
228
229         if (nxt != IPPROTO_DONE) {
230                 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
231                     ipsec4_in_reject(m, NULL)) {
232                         ipsecstat.in_polvio++;
233                         goto fail;
234                 }
235                 (*inetsw[ip_protox[nxt]].pr_input)(m, off, nxt);
236         } else
237                 m_freem(m);
238         m = NULL;
239
240         ipsecstat.in_success++;
241         return;
242
243 fail:
244         if (sav)
245                 key_freesav(sav);
246         if (m)
247                 m_freem(m);
248         return;
249 }
250 #endif /* INET */
251
252 #ifdef INET6
253 int
254 ipcomp6_input(mp, offp, proto)
255         struct mbuf **mp;
256         int *offp, proto;
257 {
258         struct mbuf *m, *md;
259         int off;
260         struct ip6_hdr *ip6;
261         struct ipcomp *ipcomp;
262         const struct ipcomp_algorithm *algo;
263         u_int16_t cpi;  /* host order */
264         u_int16_t nxt;
265         int error;
266         size_t newlen;
267         struct secasvar *sav = NULL;
268         char *prvnxtp;
269
270         m = *mp;
271         off = *offp;
272
273         md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
274         if (!m) {
275                 m = NULL;       /* already freed */
276                 ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
277                     "(pulldown failure)\n"));
278                 ipsec6stat.in_inval++;
279                 goto fail;
280         }
281         ipcomp = mtod(md, struct ipcomp *);
282         ip6 = mtod(m, struct ip6_hdr *);
283         nxt = ipcomp->comp_nxt;
284
285         cpi = ntohs(ipcomp->comp_cpi);
286
287         if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
288                 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
289                         (caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
290                 if (sav != NULL
291                  && (sav->state == SADB_SASTATE_MATURE
292                   || sav->state == SADB_SASTATE_DYING)) {
293                         cpi = sav->alg_enc;     /* XXX */
294                         /* other parameters to look at? */
295                 }
296         }
297         algo = ipcomp_algorithm_lookup(cpi);
298         if (!algo) {
299                 ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
300                         "dropping the packet for simplicity\n", cpi));
301                 ipsec6stat.in_nosa++;
302                 goto fail;
303         }
304
305         /* chop ipcomp header */
306         ipcomp = NULL;
307         md->m_data += sizeof(struct ipcomp);
308         md->m_len -= sizeof(struct ipcomp);
309         m->m_pkthdr.len -= sizeof(struct ipcomp);
310
311         newlen = m->m_pkthdr.len - off;
312         error = (*algo->decompress)(m, md, &newlen);
313         if (error != 0) {
314                 if (error == EINVAL)
315                         ipsec6stat.in_inval++;
316                 else if (error == ENOBUFS)
317                         ipsec6stat.in_nomem++;
318                 m = NULL;
319                 goto fail;
320         }
321         ipsec6stat.in_comphist[cpi]++;
322         m->m_pkthdr.len = off + newlen;
323
324         /*
325          * returning decompressed packet onto icmp is meaningless.
326          * mark it decrypted to prevent icmp from attaching original packet.
327          */
328         m->m_flags |= M_DECRYPTED;
329
330         /* update next header field */
331         prvnxtp = ip6_get_prevhdr(m, off);
332         *prvnxtp = nxt;
333
334         /*
335          * no need to adjust payload length, as all the IPv6 protocols
336          * look at m->m_pkthdr.len
337          */
338
339         if (sav) {
340                 key_sa_recordxfer(sav, m);
341                 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
342                         ipsec6stat.in_nomem++;
343                         goto fail;
344                 }
345                 key_freesav(sav);
346                 sav = NULL;
347         }
348         *offp = off;
349         *mp = m;
350         ipsec6stat.in_success++;
351         return nxt;
352
353 fail:
354         if (m)
355                 m_freem(m);
356         if (sav)
357                 key_freesav(sav);
358         return IPPROTO_DONE;
359 }
360 #endif /* INET6 */