hw/smbios: fix offset of type 3 sku field
[qemu/ar7.git] / slirp / ip_input.c
blobe0b94b0e426b3103e1fd6651932651b22b227a2a
1 /*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
30 * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
34 * Changes and additions relating to SLiRP are
35 * Copyright (c) 1995 Danny Gasparovski.
37 * Please read the file COPYRIGHT for the
38 * terms and conditions of the copyright.
41 #include "slirp.h"
42 #include "ip_icmp.h"
44 static struct ip *ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp);
45 static void ip_freef(Slirp *slirp, struct ipq *fp);
46 static void ip_enq(register struct ipasfrag *p,
47 register struct ipasfrag *prev);
48 static void ip_deq(register struct ipasfrag *p);
51 * IP initialization: fill in IP protocol switch table.
52 * All protocols not implemented in kernel go to raw IP protocol handler.
54 void
55 ip_init(Slirp *slirp)
57 slirp->ipq.ip_link.next = slirp->ipq.ip_link.prev = &slirp->ipq.ip_link;
58 udp_init(slirp);
59 tcp_init(slirp);
60 icmp_init(slirp);
63 void ip_cleanup(Slirp *slirp)
65 udp_cleanup(slirp);
66 tcp_cleanup(slirp);
67 icmp_cleanup(slirp);
71 * Ip input routine. Checksum and byte swap header. If fragmented
72 * try to reassemble. Process options. Pass to next level.
74 void
75 ip_input(struct mbuf *m)
77 Slirp *slirp = m->slirp;
78 register struct ip *ip;
79 int hlen;
81 if (!slirp->in_enabled) {
82 goto bad;
85 DEBUG_CALL("ip_input");
86 DEBUG_ARG("m = %p", m);
87 DEBUG_ARG("m_len = %d", m->m_len);
89 if (m->m_len < sizeof (struct ip)) {
90 goto bad;
93 ip = mtod(m, struct ip *);
95 if (ip->ip_v != IPVERSION) {
96 goto bad;
99 hlen = ip->ip_hl << 2;
100 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
101 goto bad; /* or packet too short */
104 /* keep ip header intact for ICMP reply
105 * ip->ip_sum = cksum(m, hlen);
106 * if (ip->ip_sum) {
108 if(cksum(m,hlen)) {
109 goto bad;
113 * Convert fields to host representation.
115 NTOHS(ip->ip_len);
116 if (ip->ip_len < hlen) {
117 goto bad;
119 NTOHS(ip->ip_id);
120 NTOHS(ip->ip_off);
123 * Check that the amount of data in the buffers
124 * is as at least much as the IP header would have us expect.
125 * Trim mbufs if longer than we expect.
126 * Drop packet if shorter than we expect.
128 if (m->m_len < ip->ip_len) {
129 goto bad;
132 /* Should drop packet if mbuf too long? hmmm... */
133 if (m->m_len > ip->ip_len)
134 m_adj(m, ip->ip_len - m->m_len);
136 /* check ip_ttl for a correct ICMP reply */
137 if (ip->ip_ttl == 0) {
138 icmp_send_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, "ttl");
139 goto bad;
143 * If offset or IP_MF are set, must reassemble.
144 * Otherwise, nothing need be done.
145 * (We could look in the reassembly queue to see
146 * if the packet was previously fragmented,
147 * but it's not worth the time; just let them time out.)
149 * XXX This should fail, don't fragment yet
151 if (ip->ip_off &~ IP_DF) {
152 register struct ipq *fp;
153 struct qlink *l;
155 * Look for queue of fragments
156 * of this datagram.
158 for (l = slirp->ipq.ip_link.next; l != &slirp->ipq.ip_link;
159 l = l->next) {
160 fp = container_of(l, struct ipq, ip_link);
161 if (ip->ip_id == fp->ipq_id &&
162 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
163 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
164 ip->ip_p == fp->ipq_p)
165 goto found;
167 fp = NULL;
168 found:
171 * Adjust ip_len to not reflect header,
172 * set ip_mff if more fragments are expected,
173 * convert offset of this to bytes.
175 ip->ip_len -= hlen;
176 if (ip->ip_off & IP_MF)
177 ip->ip_tos |= 1;
178 else
179 ip->ip_tos &= ~1;
181 ip->ip_off <<= 3;
184 * If datagram marked as having more fragments
185 * or if this is not the first fragment,
186 * attempt reassembly; if it succeeds, proceed.
188 if (ip->ip_tos & 1 || ip->ip_off) {
189 ip = ip_reass(slirp, ip, fp);
190 if (ip == NULL)
191 return;
192 m = dtom(slirp, ip);
193 } else
194 if (fp)
195 ip_freef(slirp, fp);
197 } else
198 ip->ip_len -= hlen;
201 * Switch out to protocol's input routine.
203 switch (ip->ip_p) {
204 case IPPROTO_TCP:
205 tcp_input(m, hlen, (struct socket *)NULL, AF_INET);
206 break;
207 case IPPROTO_UDP:
208 udp_input(m, hlen);
209 break;
210 case IPPROTO_ICMP:
211 icmp_input(m, hlen);
212 break;
213 default:
214 m_free(m);
216 return;
217 bad:
218 m_free(m);
221 #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
222 #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
224 * Take incoming datagram fragment and try to
225 * reassemble it into whole datagram. If a chain for
226 * reassembly of this datagram already exists, then it
227 * is given as fp; otherwise have to make a chain.
229 static struct ip *
230 ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp)
232 register struct mbuf *m = dtom(slirp, ip);
233 register struct ipasfrag *q;
234 int hlen = ip->ip_hl << 2;
235 int i, next;
237 DEBUG_CALL("ip_reass");
238 DEBUG_ARG("ip = %p", ip);
239 DEBUG_ARG("fp = %p", fp);
240 DEBUG_ARG("m = %p", m);
243 * Presence of header sizes in mbufs
244 * would confuse code below.
245 * Fragment m_data is concatenated.
247 m->m_data += hlen;
248 m->m_len -= hlen;
251 * If first fragment to arrive, create a reassembly queue.
253 if (fp == NULL) {
254 struct mbuf *t = m_get(slirp);
256 if (t == NULL) {
257 goto dropfrag;
259 fp = mtod(t, struct ipq *);
260 insque(&fp->ip_link, &slirp->ipq.ip_link);
261 fp->ipq_ttl = IPFRAGTTL;
262 fp->ipq_p = ip->ip_p;
263 fp->ipq_id = ip->ip_id;
264 fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
265 fp->ipq_src = ip->ip_src;
266 fp->ipq_dst = ip->ip_dst;
267 q = (struct ipasfrag *)fp;
268 goto insert;
272 * Find a segment which begins after this one does.
274 for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
275 q = q->ipf_next)
276 if (q->ipf_off > ip->ip_off)
277 break;
280 * If there is a preceding segment, it may provide some of
281 * our data already. If so, drop the data from the incoming
282 * segment. If it provides all of our data, drop us.
284 if (q->ipf_prev != &fp->frag_link) {
285 struct ipasfrag *pq = q->ipf_prev;
286 i = pq->ipf_off + pq->ipf_len - ip->ip_off;
287 if (i > 0) {
288 if (i >= ip->ip_len)
289 goto dropfrag;
290 m_adj(dtom(slirp, ip), i);
291 ip->ip_off += i;
292 ip->ip_len -= i;
297 * While we overlap succeeding segments trim them or,
298 * if they are completely covered, dequeue them.
300 while (q != (struct ipasfrag*)&fp->frag_link &&
301 ip->ip_off + ip->ip_len > q->ipf_off) {
302 i = (ip->ip_off + ip->ip_len) - q->ipf_off;
303 if (i < q->ipf_len) {
304 q->ipf_len -= i;
305 q->ipf_off += i;
306 m_adj(dtom(slirp, q), i);
307 break;
309 q = q->ipf_next;
310 m_free(dtom(slirp, q->ipf_prev));
311 ip_deq(q->ipf_prev);
314 insert:
316 * Stick new segment in its place;
317 * check for complete reassembly.
319 ip_enq(iptofrag(ip), q->ipf_prev);
320 next = 0;
321 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
322 q = q->ipf_next) {
323 if (q->ipf_off != next)
324 return NULL;
325 next += q->ipf_len;
327 if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
328 return NULL;
331 * Reassembly is complete; concatenate fragments.
333 q = fp->frag_link.next;
334 m = dtom(slirp, q);
336 q = (struct ipasfrag *) q->ipf_next;
337 while (q != (struct ipasfrag*)&fp->frag_link) {
338 struct mbuf *t = dtom(slirp, q);
339 q = (struct ipasfrag *) q->ipf_next;
340 m_cat(m, t);
344 * Create header for new ip packet by
345 * modifying header of first packet;
346 * dequeue and discard fragment reassembly header.
347 * Make header visible.
349 q = fp->frag_link.next;
352 * If the fragments concatenated to an mbuf that's
353 * bigger than the total size of the fragment, then and
354 * m_ext buffer was alloced. But fp->ipq_next points to
355 * the old buffer (in the mbuf), so we must point ip
356 * into the new buffer.
358 if (m->m_flags & M_EXT) {
359 int delta = (char *)q - m->m_dat;
360 q = (struct ipasfrag *)(m->m_ext + delta);
363 ip = fragtoip(q);
364 ip->ip_len = next;
365 ip->ip_tos &= ~1;
366 ip->ip_src = fp->ipq_src;
367 ip->ip_dst = fp->ipq_dst;
368 remque(&fp->ip_link);
369 (void) m_free(dtom(slirp, fp));
370 m->m_len += (ip->ip_hl << 2);
371 m->m_data -= (ip->ip_hl << 2);
373 return ip;
375 dropfrag:
376 m_free(m);
377 return NULL;
381 * Free a fragment reassembly header and all
382 * associated datagrams.
384 static void
385 ip_freef(Slirp *slirp, struct ipq *fp)
387 register struct ipasfrag *q, *p;
389 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
390 p = q->ipf_next;
391 ip_deq(q);
392 m_free(dtom(slirp, q));
394 remque(&fp->ip_link);
395 (void) m_free(dtom(slirp, fp));
399 * Put an ip fragment on a reassembly chain.
400 * Like insque, but pointers in middle of structure.
402 static void
403 ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
405 DEBUG_CALL("ip_enq");
406 DEBUG_ARG("prev = %p", prev);
407 p->ipf_prev = prev;
408 p->ipf_next = prev->ipf_next;
409 ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
410 prev->ipf_next = p;
414 * To ip_enq as remque is to insque.
416 static void
417 ip_deq(register struct ipasfrag *p)
419 ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
420 ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
424 * IP timer processing;
425 * if a timer expires on a reassembly
426 * queue, discard it.
428 void
429 ip_slowtimo(Slirp *slirp)
431 struct qlink *l;
433 DEBUG_CALL("ip_slowtimo");
435 l = slirp->ipq.ip_link.next;
437 if (l == NULL)
438 return;
440 while (l != &slirp->ipq.ip_link) {
441 struct ipq *fp = container_of(l, struct ipq, ip_link);
442 l = l->next;
443 if (--fp->ipq_ttl == 0) {
444 ip_freef(slirp, fp);
450 * Strip out IP options, at higher
451 * level protocol in the kernel.
452 * Second argument is buffer to which options
453 * will be moved, and return value is their length.
454 * (XXX) should be deleted; last arg currently ignored.
456 void
457 ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
459 register int i;
460 struct ip *ip = mtod(m, struct ip *);
461 register char *opts;
462 int olen;
464 olen = (ip->ip_hl<<2) - sizeof (struct ip);
465 opts = (char *)(ip + 1);
466 i = m->m_len - (sizeof (struct ip) + olen);
467 memcpy(opts, opts + olen, (unsigned)i);
468 m->m_len -= olen;
470 ip->ip_hl = sizeof(struct ip) >> 2;