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