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 $ */
5 * Copyright (C) 1999 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 * RFC2393 IP payload compression protocol (IPComp).
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
48 #include <sys/syslog.h>
51 #include <net/route.h>
52 #include <net/netisr.h>
53 #include <machine/cpu.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_ecn.h>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
66 #include <netinet6/ipcomp.h>
68 #include <netinet6/ipcomp6.h>
71 #include <netinet6/ipsec.h>
73 #include <netinet6/ipsec6.h>
75 #include <netproto/key/key.h>
76 #include <netproto/key/keydb.h>
78 #include <machine/stdarg.h>
80 #include <net/net_osdep.h>
85 extern struct protosw inetsw
[];
88 ipcomp4_input(struct mbuf
**mp
, int *offp
, int proto
)
94 struct ipcomp
*ipcomp
;
95 const struct ipcomp_algorithm
*algo
;
96 u_int16_t cpi
; /* host order */
100 struct secasvar
*sav
= NULL
;
106 if (m
->m_pkthdr
.len
< off
+ sizeof(struct ipcomp
)) {
107 ipseclog((LOG_DEBUG
, "IPv4 IPComp input: assumption failed "
108 "(packet too short)\n"));
109 ipsecstat
.in_inval
++;
113 md
= m_pulldown(m
, off
, sizeof(*ipcomp
), NULL
);
115 m
= NULL
; /* already freed */
116 ipseclog((LOG_DEBUG
, "IPv4 IPComp input: assumption failed "
117 "(pulldown failure)\n"));
118 ipsecstat
.in_inval
++;
121 ipcomp
= mtod(md
, struct ipcomp
*);
122 ip
= mtod(m
, struct ip
*);
123 nxt
= ipcomp
->comp_nxt
;
125 cpi
= ntohs(ipcomp
->comp_cpi
);
127 if (cpi
>= IPCOMP_CPI_NEGOTIATE_MIN
) {
128 sav
= key_allocsa(AF_INET
, (caddr_t
)&ip
->ip_src
,
129 (caddr_t
)&ip
->ip_dst
, IPPROTO_IPCOMP
, htonl(cpi
));
131 && (sav
->state
== SADB_SASTATE_MATURE
132 || sav
->state
== SADB_SASTATE_DYING
)) {
133 cpi
= sav
->alg_enc
; /* XXX */
134 /* other parameters to look at? */
137 algo
= ipcomp_algorithm_lookup(cpi
);
139 ipseclog((LOG_WARNING
, "IPv4 IPComp input: unknown cpi %u\n",
145 /* chop ipcomp header */
147 md
->m_data
+= sizeof(struct ipcomp
);
148 md
->m_len
-= sizeof(struct ipcomp
);
149 m
->m_pkthdr
.len
-= sizeof(struct ipcomp
);
151 ip
->ip_len
-= sizeof(struct ipcomp
);
153 ip
->ip_len
= htons(ntohs(ip
->ip_len
) - sizeof(struct ipcomp
));
156 olen
= m
->m_pkthdr
.len
;
157 newlen
= m
->m_pkthdr
.len
- off
;
158 error
= (*algo
->decompress
)(m
, m
->m_next
, &newlen
);
161 ipsecstat
.in_inval
++;
162 else if (error
== ENOBUFS
)
163 ipsecstat
.in_nomem
++;
167 ipsecstat
.in_comphist
[cpi
]++;
170 * returning decompressed packet onto icmp is meaningless.
171 * mark it decrypted to prevent icmp from attaching original packet.
173 m
->m_flags
|= M_DECRYPTED
;
175 m
->m_pkthdr
.len
= off
+ newlen
;
176 ip
= mtod(m
, struct ip
*);
182 len
= ntohs(ip
->ip_len
);
185 * be careful about underflow. also, do not assign exact value
186 * as ip_len is manipulated differently on *BSDs.
188 len
+= m
->m_pkthdr
.len
;
191 /* packet too big after decompress */
192 ipsecstat
.in_inval
++;
196 ip
->ip_len
= len
& 0xffff;
198 ip
->ip_len
= htons(len
& 0xffff);
204 key_sa_recordxfer(sav
, m
);
205 if (ipsec_addhist(m
, IPPROTO_IPCOMP
, (u_int32_t
)cpi
) != 0) {
206 ipsecstat
.in_nomem
++;
213 if (nxt
!= IPPROTO_DONE
) {
214 if ((inetsw
[ip_protox
[nxt
]].pr_flags
& PR_LASTHDR
) &&
215 ipsec4_in_reject(m
, NULL
)) {
216 ipsecstat
.in_polvio
++;
221 (*inetsw
[ip_protox
[nxt
]].pr_input
)(mp
, offp
, nxt
);
227 ipsecstat
.in_success
++;
228 return(IPPROTO_DONE
);
235 return(IPPROTO_DONE
);
241 ipcomp6_input(struct mbuf
**mp
, int *offp
, int proto
)
246 struct ipcomp
*ipcomp
;
247 const struct ipcomp_algorithm
*algo
;
248 u_int16_t cpi
; /* host order */
252 struct secasvar
*sav
= NULL
;
258 md
= m_pulldown(m
, off
, sizeof(*ipcomp
), NULL
);
260 m
= NULL
; /* already freed */
261 ipseclog((LOG_DEBUG
, "IPv6 IPComp input: assumption failed "
262 "(pulldown failure)\n"));
263 ipsec6stat
.in_inval
++;
266 ipcomp
= mtod(md
, struct ipcomp
*);
267 ip6
= mtod(m
, struct ip6_hdr
*);
268 nxt
= ipcomp
->comp_nxt
;
270 cpi
= ntohs(ipcomp
->comp_cpi
);
272 if (cpi
>= IPCOMP_CPI_NEGOTIATE_MIN
) {
273 sav
= key_allocsa(AF_INET6
, (caddr_t
)&ip6
->ip6_src
,
274 (caddr_t
)&ip6
->ip6_dst
, IPPROTO_IPCOMP
, htonl(cpi
));
276 && (sav
->state
== SADB_SASTATE_MATURE
277 || sav
->state
== SADB_SASTATE_DYING
)) {
278 cpi
= sav
->alg_enc
; /* XXX */
279 /* other parameters to look at? */
282 algo
= ipcomp_algorithm_lookup(cpi
);
284 ipseclog((LOG_WARNING
, "IPv6 IPComp input: unknown cpi %u; "
285 "dropping the packet for simplicity\n", cpi
));
286 ipsec6stat
.in_nosa
++;
290 /* chop ipcomp header */
292 md
->m_data
+= sizeof(struct ipcomp
);
293 md
->m_len
-= sizeof(struct ipcomp
);
294 m
->m_pkthdr
.len
-= sizeof(struct ipcomp
);
296 newlen
= m
->m_pkthdr
.len
- off
;
297 error
= (*algo
->decompress
)(m
, md
, &newlen
);
300 ipsec6stat
.in_inval
++;
301 else if (error
== ENOBUFS
)
302 ipsec6stat
.in_nomem
++;
306 ipsec6stat
.in_comphist
[cpi
]++;
307 m
->m_pkthdr
.len
= off
+ newlen
;
310 * returning decompressed packet onto icmp is meaningless.
311 * mark it decrypted to prevent icmp from attaching original packet.
313 m
->m_flags
|= M_DECRYPTED
;
315 /* update next header field */
316 prvnxtp
= ip6_get_prevhdr(m
, off
);
320 * no need to adjust payload length, as all the IPv6 protocols
321 * look at m->m_pkthdr.len
325 key_sa_recordxfer(sav
, m
);
326 if (ipsec_addhist(m
, IPPROTO_IPCOMP
, (u_int32_t
)cpi
) != 0) {
327 ipsec6stat
.in_nomem
++;
335 ipsec6stat
.in_success
++;