pc64: Setup TSC_AUX register with each cpu's id, when rdtscp is available.
[dragonfly.git] / sys / netinet6 / ipcomp_input.c
blob48bc36efc39edf98d7e0ebdeef3a00dfd158bd2d
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 $ */
4 /*
5 * Copyright (C) 1999 WIDE Project.
6 * All rights reserved.
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.
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.
34 * RFC2393 IP payload compression protocol (IPComp).
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
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>
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>
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>
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
72 #include <netinet6/ipsec.h>
73 #ifdef INET6
74 #include <netinet6/ipsec6.h>
75 #endif
76 #include <netproto/key/key.h>
77 #include <netproto/key/keydb.h>
79 #include <machine/stdarg.h>
81 #include <net/net_osdep.h>
83 #define IPLEN_FLIPPED
85 #ifdef INET
86 extern struct protosw inetsw[];
88 int
89 ipcomp4_input(struct mbuf **mp, int *offp, int proto)
91 int off;
92 struct mbuf *md;
93 struct mbuf *m;
94 struct ip *ip;
95 struct ipcomp *ipcomp;
96 const struct ipcomp_algorithm *algo;
97 u_int16_t cpi; /* host order */
98 u_int16_t nxt;
99 int error;
100 size_t newlen, olen;
101 struct secasvar *sav = NULL;
103 off = *offp;
104 m = *mp;
105 *mp = NULL;
107 if (m->m_pkthdr.len < off + sizeof(struct ipcomp)) {
108 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
109 "(packet too short)\n"));
110 ipsecstat.in_inval++;
111 goto fail;
114 md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
115 if (!md) {
116 m = NULL; /* already freed */
117 ipseclog((LOG_DEBUG, "IPv4 IPComp input: assumption failed "
118 "(pulldown failure)\n"));
119 ipsecstat.in_inval++;
120 goto fail;
122 ipcomp = mtod(md, struct ipcomp *);
123 ip = mtod(m, struct ip *);
124 nxt = ipcomp->comp_nxt;
126 cpi = ntohs(ipcomp->comp_cpi);
128 if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
129 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src,
130 (caddr_t)&ip->ip_dst, IPPROTO_IPCOMP, htonl(cpi));
131 if (sav != NULL
132 && (sav->state == SADB_SASTATE_MATURE
133 || sav->state == SADB_SASTATE_DYING)) {
134 cpi = sav->alg_enc; /* XXX */
135 /* other parameters to look at? */
138 algo = ipcomp_algorithm_lookup(cpi);
139 if (!algo) {
140 ipseclog((LOG_WARNING, "IPv4 IPComp input: unknown cpi %u\n",
141 cpi));
142 ipsecstat.in_nosa++;
143 goto fail;
146 /* chop ipcomp header */
147 ipcomp = NULL;
148 md->m_data += sizeof(struct ipcomp);
149 md->m_len -= sizeof(struct ipcomp);
150 m->m_pkthdr.len -= sizeof(struct ipcomp);
151 #ifdef IPLEN_FLIPPED
152 ip->ip_len -= sizeof(struct ipcomp);
153 #else
154 ip->ip_len = htons(ntohs(ip->ip_len) - sizeof(struct ipcomp));
155 #endif
157 olen = m->m_pkthdr.len;
158 newlen = m->m_pkthdr.len - off;
159 error = (*algo->decompress)(m, m->m_next, &newlen);
160 if (error != 0) {
161 if (error == EINVAL)
162 ipsecstat.in_inval++;
163 else if (error == ENOBUFS)
164 ipsecstat.in_nomem++;
165 m = NULL;
166 goto fail;
168 ipsecstat.in_comphist[cpi]++;
171 * returning decompressed packet onto icmp is meaningless.
172 * mark it decrypted to prevent icmp from attaching original packet.
174 m->m_flags |= M_DECRYPTED;
176 m->m_pkthdr.len = off + newlen;
177 ip = mtod(m, struct ip *);
179 size_t len;
180 #ifdef IPLEN_FLIPPED
181 len = ip->ip_len;
182 #else
183 len = ntohs(ip->ip_len);
184 #endif
186 * be careful about underflow. also, do not assign exact value
187 * as ip_len is manipulated differently on *BSDs.
189 len += m->m_pkthdr.len;
190 len -= olen;
191 if (len & ~0xffff) {
192 /* packet too big after decompress */
193 ipsecstat.in_inval++;
194 goto fail;
196 #ifdef IPLEN_FLIPPED
197 ip->ip_len = len & 0xffff;
198 #else
199 ip->ip_len = htons(len & 0xffff);
200 #endif
201 ip->ip_p = nxt;
204 if (sav) {
205 key_sa_recordxfer(sav, m);
206 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
207 ipsecstat.in_nomem++;
208 goto fail;
210 key_freesav(sav);
211 sav = NULL;
214 if (nxt != IPPROTO_DONE) {
215 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) &&
216 ipsec4_in_reject(m, NULL)) {
217 ipsecstat.in_polvio++;
218 goto fail;
220 *mp = m;
221 *offp = off;
222 (*inetsw[ip_protox[nxt]].pr_input)(mp, offp, nxt);
223 } else {
224 m_freem(m);
226 m = NULL;
228 ipsecstat.in_success++;
229 return(IPPROTO_DONE);
231 fail:
232 if (sav)
233 key_freesav(sav);
234 if (m)
235 m_freem(m);
236 return(IPPROTO_DONE);
238 #endif /* INET */
240 #ifdef INET6
242 ipcomp6_input(struct mbuf **mp, int *offp, int proto)
244 struct mbuf *m, *md;
245 int off;
246 struct ip6_hdr *ip6;
247 struct ipcomp *ipcomp;
248 const struct ipcomp_algorithm *algo;
249 u_int16_t cpi; /* host order */
250 u_int16_t nxt;
251 int error;
252 size_t newlen;
253 struct secasvar *sav = NULL;
254 char *prvnxtp;
256 m = *mp;
257 off = *offp;
259 md = m_pulldown(m, off, sizeof(*ipcomp), NULL);
260 if (!md) {
261 m = NULL; /* already freed */
262 ipseclog((LOG_DEBUG, "IPv6 IPComp input: assumption failed "
263 "(pulldown failure)\n"));
264 ipsec6stat.in_inval++;
265 goto fail;
267 ipcomp = mtod(md, struct ipcomp *);
268 ip6 = mtod(m, struct ip6_hdr *);
269 nxt = ipcomp->comp_nxt;
271 cpi = ntohs(ipcomp->comp_cpi);
273 if (cpi >= IPCOMP_CPI_NEGOTIATE_MIN) {
274 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src,
275 (caddr_t)&ip6->ip6_dst, IPPROTO_IPCOMP, htonl(cpi));
276 if (sav != NULL
277 && (sav->state == SADB_SASTATE_MATURE
278 || sav->state == SADB_SASTATE_DYING)) {
279 cpi = sav->alg_enc; /* XXX */
280 /* other parameters to look at? */
283 algo = ipcomp_algorithm_lookup(cpi);
284 if (!algo) {
285 ipseclog((LOG_WARNING, "IPv6 IPComp input: unknown cpi %u; "
286 "dropping the packet for simplicity\n", cpi));
287 ipsec6stat.in_nosa++;
288 goto fail;
291 /* chop ipcomp header */
292 ipcomp = NULL;
293 md->m_data += sizeof(struct ipcomp);
294 md->m_len -= sizeof(struct ipcomp);
295 m->m_pkthdr.len -= sizeof(struct ipcomp);
297 newlen = m->m_pkthdr.len - off;
298 error = (*algo->decompress)(m, md, &newlen);
299 if (error != 0) {
300 if (error == EINVAL)
301 ipsec6stat.in_inval++;
302 else if (error == ENOBUFS)
303 ipsec6stat.in_nomem++;
304 m = NULL;
305 goto fail;
307 ipsec6stat.in_comphist[cpi]++;
308 m->m_pkthdr.len = off + newlen;
311 * returning decompressed packet onto icmp is meaningless.
312 * mark it decrypted to prevent icmp from attaching original packet.
314 m->m_flags |= M_DECRYPTED;
316 /* update next header field */
317 prvnxtp = ip6_get_prevhdr(m, off);
318 *prvnxtp = nxt;
321 * no need to adjust payload length, as all the IPv6 protocols
322 * look at m->m_pkthdr.len
325 if (sav) {
326 key_sa_recordxfer(sav, m);
327 if (ipsec_addhist(m, IPPROTO_IPCOMP, (u_int32_t)cpi) != 0) {
328 ipsec6stat.in_nomem++;
329 goto fail;
331 key_freesav(sav);
332 sav = NULL;
334 *offp = off;
335 *mp = m;
336 ipsec6stat.in_success++;
337 return nxt;
339 fail:
340 if (m)
341 m_freem(m);
342 if (sav)
343 key_freesav(sav);
344 return IPPROTO_DONE;
346 #endif /* INET6 */