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