Statistics
| Branch: | Revision:

root / slirp / ip_input.c @ 45724d6d

History | View | Annotate | Download (16.5 kB)

1 f0cbd3ec bellard
/*
2 f0cbd3ec bellard
 * Copyright (c) 1982, 1986, 1988, 1993
3 f0cbd3ec bellard
 *        The Regents of the University of California.  All rights reserved.
4 f0cbd3ec bellard
 *
5 f0cbd3ec bellard
 * Redistribution and use in source and binary forms, with or without
6 f0cbd3ec bellard
 * modification, are permitted provided that the following conditions
7 f0cbd3ec bellard
 * are met:
8 f0cbd3ec bellard
 * 1. Redistributions of source code must retain the above copyright
9 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer.
10 f0cbd3ec bellard
 * 2. Redistributions in binary form must reproduce the above copyright
11 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer in the
12 f0cbd3ec bellard
 *    documentation and/or other materials provided with the distribution.
13 2f5f8996 aliguori
 * 3. Neither the name of the University nor the names of its contributors
14 f0cbd3ec bellard
 *    may be used to endorse or promote products derived from this software
15 f0cbd3ec bellard
 *    without specific prior written permission.
16 f0cbd3ec bellard
 *
17 f0cbd3ec bellard
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 f0cbd3ec bellard
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 f0cbd3ec bellard
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 f0cbd3ec bellard
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 f0cbd3ec bellard
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 f0cbd3ec bellard
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 f0cbd3ec bellard
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 f0cbd3ec bellard
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 f0cbd3ec bellard
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 f0cbd3ec bellard
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 f0cbd3ec bellard
 * SUCH DAMAGE.
28 f0cbd3ec bellard
 *
29 f0cbd3ec bellard
 *        @(#)ip_input.c        8.2 (Berkeley) 1/4/94
30 f0cbd3ec bellard
 * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
31 f0cbd3ec bellard
 */
32 f0cbd3ec bellard
33 f0cbd3ec bellard
/*
34 f0cbd3ec bellard
 * Changes and additions relating to SLiRP are
35 f0cbd3ec bellard
 * Copyright (c) 1995 Danny Gasparovski.
36 5fafdf24 ths
 *
37 f0cbd3ec bellard
 * Please read the file COPYRIGHT for the
38 f0cbd3ec bellard
 * terms and conditions of the copyright.
39 f0cbd3ec bellard
 */
40 f0cbd3ec bellard
41 f0cbd3ec bellard
#include <slirp.h>
42 429d0a3d blueswir1
#include <osdep.h>
43 f0cbd3ec bellard
#include "ip_icmp.h"
44 f0cbd3ec bellard
45 460fec67 Jan Kiszka
static struct ip *ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp);
46 460fec67 Jan Kiszka
static void ip_freef(Slirp *slirp, struct ipq *fp);
47 9634d903 blueswir1
static void ip_enq(register struct ipasfrag *p,
48 9634d903 blueswir1
                   register struct ipasfrag *prev);
49 9634d903 blueswir1
static void ip_deq(register struct ipasfrag *p);
50 9634d903 blueswir1
51 f0cbd3ec bellard
/*
52 f0cbd3ec bellard
 * IP initialization: fill in IP protocol switch table.
53 f0cbd3ec bellard
 * All protocols not implemented in kernel go to raw IP protocol handler.
54 f0cbd3ec bellard
 */
55 f0cbd3ec bellard
void
56 460fec67 Jan Kiszka
ip_init(Slirp *slirp)
57 f0cbd3ec bellard
{
58 460fec67 Jan Kiszka
    slirp->ipq.ip_link.next = slirp->ipq.ip_link.prev = &slirp->ipq.ip_link;
59 460fec67 Jan Kiszka
    udp_init(slirp);
60 460fec67 Jan Kiszka
    tcp_init(slirp);
61 e6d43cfb Jan Kiszka
    icmp_init(slirp);
62 f0cbd3ec bellard
}
63 f0cbd3ec bellard
64 a68adc22 Jan Kiszka
void ip_cleanup(Slirp *slirp)
65 a68adc22 Jan Kiszka
{
66 a68adc22 Jan Kiszka
    udp_cleanup(slirp);
67 a68adc22 Jan Kiszka
    tcp_cleanup(slirp);
68 a68adc22 Jan Kiszka
    icmp_cleanup(slirp);
69 a68adc22 Jan Kiszka
}
70 a68adc22 Jan Kiszka
71 f0cbd3ec bellard
/*
72 f0cbd3ec bellard
 * Ip input routine.  Checksum and byte swap header.  If fragmented
73 f0cbd3ec bellard
 * try to reassemble.  Process options.  Pass to next level.
74 f0cbd3ec bellard
 */
75 f0cbd3ec bellard
void
76 511d2b14 blueswir1
ip_input(struct mbuf *m)
77 f0cbd3ec bellard
{
78 460fec67 Jan Kiszka
        Slirp *slirp = m->slirp;
79 f0cbd3ec bellard
        register struct ip *ip;
80 f0cbd3ec bellard
        int hlen;
81 5fafdf24 ths
82 f0cbd3ec bellard
        DEBUG_CALL("ip_input");
83 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
84 f0cbd3ec bellard
        DEBUG_ARG("m_len = %d", m->m_len);
85 f0cbd3ec bellard
86 f0cbd3ec bellard
        if (m->m_len < sizeof (struct ip)) {
87 f0cbd3ec bellard
                return;
88 f0cbd3ec bellard
        }
89 5fafdf24 ths
90 f0cbd3ec bellard
        ip = mtod(m, struct ip *);
91 5fafdf24 ths
92 f0cbd3ec bellard
        if (ip->ip_v != IPVERSION) {
93 f0cbd3ec bellard
                goto bad;
94 f0cbd3ec bellard
        }
95 f0cbd3ec bellard
96 f0cbd3ec bellard
        hlen = ip->ip_hl << 2;
97 f0cbd3ec bellard
        if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
98 0fe6a7f2 Jan Kiszka
          goto bad;                                  /* or packet too short */
99 f0cbd3ec bellard
        }
100 f0cbd3ec bellard
101 f0cbd3ec bellard
        /* keep ip header intact for ICMP reply
102 5fafdf24 ths
         * ip->ip_sum = cksum(m, hlen);
103 5fafdf24 ths
         * if (ip->ip_sum) {
104 f0cbd3ec bellard
         */
105 f0cbd3ec bellard
        if(cksum(m,hlen)) {
106 f0cbd3ec bellard
          goto bad;
107 f0cbd3ec bellard
        }
108 f0cbd3ec bellard
109 f0cbd3ec bellard
        /*
110 f0cbd3ec bellard
         * Convert fields to host representation.
111 f0cbd3ec bellard
         */
112 f0cbd3ec bellard
        NTOHS(ip->ip_len);
113 f0cbd3ec bellard
        if (ip->ip_len < hlen) {
114 f0cbd3ec bellard
                goto bad;
115 f0cbd3ec bellard
        }
116 f0cbd3ec bellard
        NTOHS(ip->ip_id);
117 f0cbd3ec bellard
        NTOHS(ip->ip_off);
118 f0cbd3ec bellard
119 f0cbd3ec bellard
        /*
120 f0cbd3ec bellard
         * Check that the amount of data in the buffers
121 f0cbd3ec bellard
         * is as at least much as the IP header would have us expect.
122 f0cbd3ec bellard
         * Trim mbufs if longer than we expect.
123 f0cbd3ec bellard
         * Drop packet if shorter than we expect.
124 f0cbd3ec bellard
         */
125 f0cbd3ec bellard
        if (m->m_len < ip->ip_len) {
126 f0cbd3ec bellard
                goto bad;
127 f0cbd3ec bellard
        }
128 a9ba3a85 aliguori
129 f0cbd3ec bellard
        /* Should drop packet if mbuf too long? hmmm... */
130 f0cbd3ec bellard
        if (m->m_len > ip->ip_len)
131 f0cbd3ec bellard
           m_adj(m, ip->ip_len - m->m_len);
132 f0cbd3ec bellard
133 f0cbd3ec bellard
        /* check ip_ttl for a correct ICMP reply */
134 0d491754 Hervé Poussineau
        if(ip->ip_ttl==0) {
135 f0cbd3ec bellard
          icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
136 f0cbd3ec bellard
          goto bad;
137 f0cbd3ec bellard
        }
138 f0cbd3ec bellard
139 f0cbd3ec bellard
        /*
140 f0cbd3ec bellard
         * If offset or IP_MF are set, must reassemble.
141 f0cbd3ec bellard
         * Otherwise, nothing need be done.
142 f0cbd3ec bellard
         * (We could look in the reassembly queue to see
143 f0cbd3ec bellard
         * if the packet was previously fragmented,
144 f0cbd3ec bellard
         * but it's not worth the time; just let them time out.)
145 5fafdf24 ths
         *
146 f0cbd3ec bellard
         * XXX This should fail, don't fragment yet
147 f0cbd3ec bellard
         */
148 f0cbd3ec bellard
        if (ip->ip_off &~ IP_DF) {
149 f0cbd3ec bellard
          register struct ipq *fp;
150 429d0a3d blueswir1
      struct qlink *l;
151 f0cbd3ec bellard
                /*
152 f0cbd3ec bellard
                 * Look for queue of fragments
153 f0cbd3ec bellard
                 * of this datagram.
154 f0cbd3ec bellard
                 */
155 460fec67 Jan Kiszka
                for (l = slirp->ipq.ip_link.next; l != &slirp->ipq.ip_link;
156 460fec67 Jan Kiszka
                     l = l->next) {
157 429d0a3d blueswir1
            fp = container_of(l, struct ipq, ip_link);
158 429d0a3d blueswir1
            if (ip->ip_id == fp->ipq_id &&
159 429d0a3d blueswir1
                    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
160 429d0a3d blueswir1
                    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
161 429d0a3d blueswir1
                    ip->ip_p == fp->ipq_p)
162 f0cbd3ec bellard
                    goto found;
163 429d0a3d blueswir1
        }
164 429d0a3d blueswir1
        fp = NULL;
165 f0cbd3ec bellard
        found:
166 f0cbd3ec bellard
167 f0cbd3ec bellard
                /*
168 f0cbd3ec bellard
                 * Adjust ip_len to not reflect header,
169 f0cbd3ec bellard
                 * set ip_mff if more fragments are expected,
170 f0cbd3ec bellard
                 * convert offset of this to bytes.
171 f0cbd3ec bellard
                 */
172 f0cbd3ec bellard
                ip->ip_len -= hlen;
173 f0cbd3ec bellard
                if (ip->ip_off & IP_MF)
174 429d0a3d blueswir1
                  ip->ip_tos |= 1;
175 5fafdf24 ths
                else
176 429d0a3d blueswir1
                  ip->ip_tos &= ~1;
177 f0cbd3ec bellard
178 f0cbd3ec bellard
                ip->ip_off <<= 3;
179 f0cbd3ec bellard
180 f0cbd3ec bellard
                /*
181 f0cbd3ec bellard
                 * If datagram marked as having more fragments
182 f0cbd3ec bellard
                 * or if this is not the first fragment,
183 f0cbd3ec bellard
                 * attempt reassembly; if it succeeds, proceed.
184 f0cbd3ec bellard
                 */
185 429d0a3d blueswir1
                if (ip->ip_tos & 1 || ip->ip_off) {
186 460fec67 Jan Kiszka
                        ip = ip_reass(slirp, ip, fp);
187 511d2b14 blueswir1
                        if (ip == NULL)
188 f0cbd3ec bellard
                                return;
189 460fec67 Jan Kiszka
                        m = dtom(slirp, ip);
190 f0cbd3ec bellard
                } else
191 f0cbd3ec bellard
                        if (fp)
192 460fec67 Jan Kiszka
                              ip_freef(slirp, fp);
193 f0cbd3ec bellard
194 f0cbd3ec bellard
        } else
195 f0cbd3ec bellard
                ip->ip_len -= hlen;
196 f0cbd3ec bellard
197 f0cbd3ec bellard
        /*
198 f0cbd3ec bellard
         * Switch out to protocol's input routine.
199 f0cbd3ec bellard
         */
200 f0cbd3ec bellard
        switch (ip->ip_p) {
201 f0cbd3ec bellard
         case IPPROTO_TCP:
202 f0cbd3ec bellard
                tcp_input(m, hlen, (struct socket *)NULL);
203 f0cbd3ec bellard
                break;
204 f0cbd3ec bellard
         case IPPROTO_UDP:
205 f0cbd3ec bellard
                udp_input(m, hlen);
206 f0cbd3ec bellard
                break;
207 f0cbd3ec bellard
         case IPPROTO_ICMP:
208 f0cbd3ec bellard
                icmp_input(m, hlen);
209 f0cbd3ec bellard
                break;
210 f0cbd3ec bellard
         default:
211 f0cbd3ec bellard
                m_free(m);
212 f0cbd3ec bellard
        }
213 f0cbd3ec bellard
        return;
214 f0cbd3ec bellard
bad:
215 3acccfc6 Jan Kiszka
        m_free(m);
216 f0cbd3ec bellard
        return;
217 f0cbd3ec bellard
}
218 f0cbd3ec bellard
219 429d0a3d blueswir1
#define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
220 429d0a3d blueswir1
#define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
221 f0cbd3ec bellard
/*
222 f0cbd3ec bellard
 * Take incoming datagram fragment and try to
223 f0cbd3ec bellard
 * reassemble it into whole datagram.  If a chain for
224 f0cbd3ec bellard
 * reassembly of this datagram already exists, then it
225 f0cbd3ec bellard
 * is given as fp; otherwise have to make a chain.
226 f0cbd3ec bellard
 */
227 9634d903 blueswir1
static struct ip *
228 460fec67 Jan Kiszka
ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp)
229 f0cbd3ec bellard
{
230 460fec67 Jan Kiszka
        register struct mbuf *m = dtom(slirp, ip);
231 f0cbd3ec bellard
        register struct ipasfrag *q;
232 f0cbd3ec bellard
        int hlen = ip->ip_hl << 2;
233 f0cbd3ec bellard
        int i, next;
234 5fafdf24 ths
235 f0cbd3ec bellard
        DEBUG_CALL("ip_reass");
236 f0cbd3ec bellard
        DEBUG_ARG("ip = %lx", (long)ip);
237 f0cbd3ec bellard
        DEBUG_ARG("fp = %lx", (long)fp);
238 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
239 f0cbd3ec bellard
240 f0cbd3ec bellard
        /*
241 f0cbd3ec bellard
         * Presence of header sizes in mbufs
242 f0cbd3ec bellard
         * would confuse code below.
243 f0cbd3ec bellard
         * Fragment m_data is concatenated.
244 f0cbd3ec bellard
         */
245 f0cbd3ec bellard
        m->m_data += hlen;
246 f0cbd3ec bellard
        m->m_len -= hlen;
247 f0cbd3ec bellard
248 f0cbd3ec bellard
        /*
249 f0cbd3ec bellard
         * If first fragment to arrive, create a reassembly queue.
250 f0cbd3ec bellard
         */
251 511d2b14 blueswir1
        if (fp == NULL) {
252 460fec67 Jan Kiszka
          struct mbuf *t = m_get(slirp);
253 460fec67 Jan Kiszka
254 460fec67 Jan Kiszka
          if (t == NULL) {
255 460fec67 Jan Kiszka
              goto dropfrag;
256 460fec67 Jan Kiszka
          }
257 f0cbd3ec bellard
          fp = mtod(t, struct ipq *);
258 460fec67 Jan Kiszka
          insque(&fp->ip_link, &slirp->ipq.ip_link);
259 f0cbd3ec bellard
          fp->ipq_ttl = IPFRAGTTL;
260 f0cbd3ec bellard
          fp->ipq_p = ip->ip_p;
261 f0cbd3ec bellard
          fp->ipq_id = ip->ip_id;
262 429d0a3d blueswir1
          fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
263 429d0a3d blueswir1
          fp->ipq_src = ip->ip_src;
264 429d0a3d blueswir1
          fp->ipq_dst = ip->ip_dst;
265 f0cbd3ec bellard
          q = (struct ipasfrag *)fp;
266 f0cbd3ec bellard
          goto insert;
267 f0cbd3ec bellard
        }
268 5fafdf24 ths
269 f0cbd3ec bellard
        /*
270 f0cbd3ec bellard
         * Find a segment which begins after this one does.
271 f0cbd3ec bellard
         */
272 429d0a3d blueswir1
        for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
273 429d0a3d blueswir1
            q = q->ipf_next)
274 429d0a3d blueswir1
                if (q->ipf_off > ip->ip_off)
275 f0cbd3ec bellard
                        break;
276 f0cbd3ec bellard
277 f0cbd3ec bellard
        /*
278 f0cbd3ec bellard
         * If there is a preceding segment, it may provide some of
279 f0cbd3ec bellard
         * our data already.  If so, drop the data from the incoming
280 f0cbd3ec bellard
         * segment.  If it provides all of our data, drop us.
281 f0cbd3ec bellard
         */
282 429d0a3d blueswir1
        if (q->ipf_prev != &fp->frag_link) {
283 429d0a3d blueswir1
        struct ipasfrag *pq = q->ipf_prev;
284 429d0a3d blueswir1
                i = pq->ipf_off + pq->ipf_len - ip->ip_off;
285 f0cbd3ec bellard
                if (i > 0) {
286 f0cbd3ec bellard
                        if (i >= ip->ip_len)
287 f0cbd3ec bellard
                                goto dropfrag;
288 460fec67 Jan Kiszka
                        m_adj(dtom(slirp, ip), i);
289 f0cbd3ec bellard
                        ip->ip_off += i;
290 f0cbd3ec bellard
                        ip->ip_len -= i;
291 f0cbd3ec bellard
                }
292 f0cbd3ec bellard
        }
293 f0cbd3ec bellard
294 f0cbd3ec bellard
        /*
295 f0cbd3ec bellard
         * While we overlap succeeding segments trim them or,
296 f0cbd3ec bellard
         * if they are completely covered, dequeue them.
297 f0cbd3ec bellard
         */
298 429d0a3d blueswir1
        while (q != (struct ipasfrag*)&fp->frag_link &&
299 429d0a3d blueswir1
            ip->ip_off + ip->ip_len > q->ipf_off) {
300 429d0a3d blueswir1
                i = (ip->ip_off + ip->ip_len) - q->ipf_off;
301 429d0a3d blueswir1
                if (i < q->ipf_len) {
302 429d0a3d blueswir1
                        q->ipf_len -= i;
303 429d0a3d blueswir1
                        q->ipf_off += i;
304 460fec67 Jan Kiszka
                        m_adj(dtom(slirp, q), i);
305 f0cbd3ec bellard
                        break;
306 f0cbd3ec bellard
                }
307 429d0a3d blueswir1
                q = q->ipf_next;
308 3acccfc6 Jan Kiszka
                m_free(dtom(slirp, q->ipf_prev));
309 429d0a3d blueswir1
                ip_deq(q->ipf_prev);
310 f0cbd3ec bellard
        }
311 f0cbd3ec bellard
312 f0cbd3ec bellard
insert:
313 f0cbd3ec bellard
        /*
314 f0cbd3ec bellard
         * Stick new segment in its place;
315 f0cbd3ec bellard
         * check for complete reassembly.
316 f0cbd3ec bellard
         */
317 429d0a3d blueswir1
        ip_enq(iptofrag(ip), q->ipf_prev);
318 f0cbd3ec bellard
        next = 0;
319 429d0a3d blueswir1
        for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
320 429d0a3d blueswir1
            q = q->ipf_next) {
321 429d0a3d blueswir1
                if (q->ipf_off != next)
322 511d2b14 blueswir1
                        return NULL;
323 429d0a3d blueswir1
                next += q->ipf_len;
324 f0cbd3ec bellard
        }
325 429d0a3d blueswir1
        if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
326 511d2b14 blueswir1
                return NULL;
327 f0cbd3ec bellard
328 f0cbd3ec bellard
        /*
329 f0cbd3ec bellard
         * Reassembly is complete; concatenate fragments.
330 f0cbd3ec bellard
         */
331 429d0a3d blueswir1
    q = fp->frag_link.next;
332 460fec67 Jan Kiszka
        m = dtom(slirp, q);
333 f0cbd3ec bellard
334 f0cbd3ec bellard
        q = (struct ipasfrag *) q->ipf_next;
335 429d0a3d blueswir1
        while (q != (struct ipasfrag*)&fp->frag_link) {
336 460fec67 Jan Kiszka
          struct mbuf *t = dtom(slirp, q);
337 f0cbd3ec bellard
          q = (struct ipasfrag *) q->ipf_next;
338 fedc54ad bellard
          m_cat(m, t);
339 f0cbd3ec bellard
        }
340 f0cbd3ec bellard
341 f0cbd3ec bellard
        /*
342 f0cbd3ec bellard
         * Create header for new ip packet by
343 f0cbd3ec bellard
         * modifying header of first packet;
344 f0cbd3ec bellard
         * dequeue and discard fragment reassembly header.
345 f0cbd3ec bellard
         * Make header visible.
346 f0cbd3ec bellard
         */
347 429d0a3d blueswir1
        q = fp->frag_link.next;
348 f0cbd3ec bellard
349 f0cbd3ec bellard
        /*
350 f0cbd3ec bellard
         * If the fragments concatenated to an mbuf that's
351 f0cbd3ec bellard
         * bigger than the total size of the fragment, then and
352 f0cbd3ec bellard
         * m_ext buffer was alloced. But fp->ipq_next points to
353 f0cbd3ec bellard
         * the old buffer (in the mbuf), so we must point ip
354 f0cbd3ec bellard
         * into the new buffer.
355 f0cbd3ec bellard
         */
356 f0cbd3ec bellard
        if (m->m_flags & M_EXT) {
357 f2ba730e blueswir1
          int delta = (char *)q - m->m_dat;
358 429d0a3d blueswir1
          q = (struct ipasfrag *)(m->m_ext + delta);
359 f0cbd3ec bellard
        }
360 f0cbd3ec bellard
361 429d0a3d blueswir1
    ip = fragtoip(q);
362 f0cbd3ec bellard
        ip->ip_len = next;
363 429d0a3d blueswir1
        ip->ip_tos &= ~1;
364 429d0a3d blueswir1
        ip->ip_src = fp->ipq_src;
365 429d0a3d blueswir1
        ip->ip_dst = fp->ipq_dst;
366 429d0a3d blueswir1
        remque(&fp->ip_link);
367 460fec67 Jan Kiszka
        (void) m_free(dtom(slirp, fp));
368 f0cbd3ec bellard
        m->m_len += (ip->ip_hl << 2);
369 f0cbd3ec bellard
        m->m_data -= (ip->ip_hl << 2);
370 f0cbd3ec bellard
371 429d0a3d blueswir1
        return ip;
372 f0cbd3ec bellard
373 f0cbd3ec bellard
dropfrag:
374 3acccfc6 Jan Kiszka
        m_free(m);
375 511d2b14 blueswir1
        return NULL;
376 f0cbd3ec bellard
}
377 f0cbd3ec bellard
378 f0cbd3ec bellard
/*
379 f0cbd3ec bellard
 * Free a fragment reassembly header and all
380 f0cbd3ec bellard
 * associated datagrams.
381 f0cbd3ec bellard
 */
382 9634d903 blueswir1
static void
383 460fec67 Jan Kiszka
ip_freef(Slirp *slirp, struct ipq *fp)
384 f0cbd3ec bellard
{
385 f0cbd3ec bellard
        register struct ipasfrag *q, *p;
386 f0cbd3ec bellard
387 429d0a3d blueswir1
        for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
388 429d0a3d blueswir1
                p = q->ipf_next;
389 f0cbd3ec bellard
                ip_deq(q);
390 3acccfc6 Jan Kiszka
                m_free(dtom(slirp, q));
391 f0cbd3ec bellard
        }
392 429d0a3d blueswir1
        remque(&fp->ip_link);
393 460fec67 Jan Kiszka
        (void) m_free(dtom(slirp, fp));
394 f0cbd3ec bellard
}
395 f0cbd3ec bellard
396 f0cbd3ec bellard
/*
397 f0cbd3ec bellard
 * Put an ip fragment on a reassembly chain.
398 f0cbd3ec bellard
 * Like insque, but pointers in middle of structure.
399 f0cbd3ec bellard
 */
400 9634d903 blueswir1
static void
401 9634d903 blueswir1
ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
402 f0cbd3ec bellard
{
403 f0cbd3ec bellard
        DEBUG_CALL("ip_enq");
404 f0cbd3ec bellard
        DEBUG_ARG("prev = %lx", (long)prev);
405 429d0a3d blueswir1
        p->ipf_prev =  prev;
406 f0cbd3ec bellard
        p->ipf_next = prev->ipf_next;
407 429d0a3d blueswir1
        ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
408 429d0a3d blueswir1
        prev->ipf_next = p;
409 f0cbd3ec bellard
}
410 f0cbd3ec bellard
411 f0cbd3ec bellard
/*
412 f0cbd3ec bellard
 * To ip_enq as remque is to insque.
413 f0cbd3ec bellard
 */
414 9634d903 blueswir1
static void
415 9634d903 blueswir1
ip_deq(register struct ipasfrag *p)
416 f0cbd3ec bellard
{
417 f0cbd3ec bellard
        ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
418 f0cbd3ec bellard
        ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
419 f0cbd3ec bellard
}
420 f0cbd3ec bellard
421 f0cbd3ec bellard
/*
422 f0cbd3ec bellard
 * IP timer processing;
423 f0cbd3ec bellard
 * if a timer expires on a reassembly
424 f0cbd3ec bellard
 * queue, discard it.
425 f0cbd3ec bellard
 */
426 f0cbd3ec bellard
void
427 460fec67 Jan Kiszka
ip_slowtimo(Slirp *slirp)
428 f0cbd3ec bellard
{
429 429d0a3d blueswir1
    struct qlink *l;
430 5fafdf24 ths
431 f0cbd3ec bellard
        DEBUG_CALL("ip_slowtimo");
432 5fafdf24 ths
433 460fec67 Jan Kiszka
    l = slirp->ipq.ip_link.next;
434 429d0a3d blueswir1
435 511d2b14 blueswir1
        if (l == NULL)
436 f0cbd3ec bellard
           return;
437 f0cbd3ec bellard
438 460fec67 Jan Kiszka
    while (l != &slirp->ipq.ip_link) {
439 429d0a3d blueswir1
        struct ipq *fp = container_of(l, struct ipq, ip_link);
440 429d0a3d blueswir1
        l = l->next;
441 429d0a3d blueswir1
                if (--fp->ipq_ttl == 0) {
442 460fec67 Jan Kiszka
                        ip_freef(slirp, fp);
443 f0cbd3ec bellard
                }
444 460fec67 Jan Kiszka
    }
445 f0cbd3ec bellard
}
446 f0cbd3ec bellard
447 f0cbd3ec bellard
/*
448 f0cbd3ec bellard
 * Do option processing on a datagram,
449 f0cbd3ec bellard
 * possibly discarding it if bad options are encountered,
450 f0cbd3ec bellard
 * or forwarding it if source-routed.
451 f0cbd3ec bellard
 * Returns 1 if packet has been forwarded/freed,
452 f0cbd3ec bellard
 * 0 if the packet should be processed further.
453 f0cbd3ec bellard
 */
454 f0cbd3ec bellard
455 f0cbd3ec bellard
#ifdef notdef
456 f0cbd3ec bellard
457 f0cbd3ec bellard
int
458 f0cbd3ec bellard
ip_dooptions(m)
459 f0cbd3ec bellard
        struct mbuf *m;
460 f0cbd3ec bellard
{
461 f0cbd3ec bellard
        register struct ip *ip = mtod(m, struct ip *);
462 f0cbd3ec bellard
        register u_char *cp;
463 f0cbd3ec bellard
        register struct ip_timestamp *ipt;
464 f0cbd3ec bellard
        register struct in_ifaddr *ia;
465 f0cbd3ec bellard
        int opt, optlen, cnt, off, code, type, forward = 0;
466 f0cbd3ec bellard
        struct in_addr *sin, dst;
467 b6dce92e Stefan Weil
typedef uint32_t n_time;
468 f0cbd3ec bellard
        n_time ntime;
469 f0cbd3ec bellard
470 f0cbd3ec bellard
        dst = ip->ip_dst;
471 f0cbd3ec bellard
        cp = (u_char *)(ip + 1);
472 f0cbd3ec bellard
        cnt = (ip->ip_hl << 2) - sizeof (struct ip);
473 f0cbd3ec bellard
        for (; cnt > 0; cnt -= optlen, cp += optlen) {
474 f0cbd3ec bellard
                opt = cp[IPOPT_OPTVAL];
475 f0cbd3ec bellard
                if (opt == IPOPT_EOL)
476 f0cbd3ec bellard
                        break;
477 f0cbd3ec bellard
                if (opt == IPOPT_NOP)
478 f0cbd3ec bellard
                        optlen = 1;
479 f0cbd3ec bellard
                else {
480 f0cbd3ec bellard
                        optlen = cp[IPOPT_OLEN];
481 f0cbd3ec bellard
                        if (optlen <= 0 || optlen > cnt) {
482 f0cbd3ec bellard
                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
483 f0cbd3ec bellard
                                goto bad;
484 f0cbd3ec bellard
                        }
485 f0cbd3ec bellard
                }
486 f0cbd3ec bellard
                switch (opt) {
487 f0cbd3ec bellard
488 f0cbd3ec bellard
                default:
489 f0cbd3ec bellard
                        break;
490 f0cbd3ec bellard
491 f0cbd3ec bellard
                /*
492 f0cbd3ec bellard
                 * Source routing with record.
493 f0cbd3ec bellard
                 * Find interface with current destination address.
494 f0cbd3ec bellard
                 * If none on this machine then drop if strictly routed,
495 f0cbd3ec bellard
                 * or do nothing if loosely routed.
496 f0cbd3ec bellard
                 * Record interface address and bring up next address
497 f0cbd3ec bellard
                 * component.  If strictly routed make sure next
498 f0cbd3ec bellard
                 * address is on directly accessible net.
499 f0cbd3ec bellard
                 */
500 f0cbd3ec bellard
                case IPOPT_LSRR:
501 f0cbd3ec bellard
                case IPOPT_SSRR:
502 f0cbd3ec bellard
                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
503 f0cbd3ec bellard
                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
504 f0cbd3ec bellard
                                goto bad;
505 f0cbd3ec bellard
                        }
506 f0cbd3ec bellard
                        ipaddr.sin_addr = ip->ip_dst;
507 f0cbd3ec bellard
                        ia = (struct in_ifaddr *)
508 f0cbd3ec bellard
                                ifa_ifwithaddr((struct sockaddr *)&ipaddr);
509 f0cbd3ec bellard
                        if (ia == 0) {
510 f0cbd3ec bellard
                                if (opt == IPOPT_SSRR) {
511 f0cbd3ec bellard
                                        type = ICMP_UNREACH;
512 f0cbd3ec bellard
                                        code = ICMP_UNREACH_SRCFAIL;
513 f0cbd3ec bellard
                                        goto bad;
514 f0cbd3ec bellard
                                }
515 f0cbd3ec bellard
                                /*
516 f0cbd3ec bellard
                                 * Loose routing, and not at next destination
517 f0cbd3ec bellard
                                 * yet; nothing to do except forward.
518 f0cbd3ec bellard
                                 */
519 f0cbd3ec bellard
                                break;
520 f0cbd3ec bellard
                        }
521 cf2846b5 Stefan Weil
                        off--; /* 0 origin */
522 f0cbd3ec bellard
                        if (off > optlen - sizeof(struct in_addr)) {
523 f0cbd3ec bellard
                                /*
524 f0cbd3ec bellard
                                 * End of source route.  Should be for us.
525 f0cbd3ec bellard
                                 */
526 f0cbd3ec bellard
                                save_rte(cp, ip->ip_src);
527 f0cbd3ec bellard
                                break;
528 f0cbd3ec bellard
                        }
529 f0cbd3ec bellard
                        /*
530 f0cbd3ec bellard
                         * locate outgoing interface
531 f0cbd3ec bellard
                         */
532 f0cbd3ec bellard
                        bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
533 f0cbd3ec bellard
                            sizeof(ipaddr.sin_addr));
534 f0cbd3ec bellard
                        if (opt == IPOPT_SSRR) {
535 f0cbd3ec bellard
#define        INA        struct in_ifaddr *
536 f0cbd3ec bellard
#define        SA        struct sockaddr *
537 f0cbd3ec bellard
                             if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
538 f0cbd3ec bellard
                                ia = (INA)ifa_ifwithnet((SA)&ipaddr);
539 f0cbd3ec bellard
                        } else
540 f0cbd3ec bellard
                                ia = ip_rtaddr(ipaddr.sin_addr);
541 f0cbd3ec bellard
                        if (ia == 0) {
542 f0cbd3ec bellard
                                type = ICMP_UNREACH;
543 f0cbd3ec bellard
                                code = ICMP_UNREACH_SRCFAIL;
544 f0cbd3ec bellard
                                goto bad;
545 f0cbd3ec bellard
                        }
546 f0cbd3ec bellard
                        ip->ip_dst = ipaddr.sin_addr;
547 f0cbd3ec bellard
                        bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
548 f0cbd3ec bellard
                            (caddr_t)(cp + off), sizeof(struct in_addr));
549 f0cbd3ec bellard
                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
550 f0cbd3ec bellard
                        /*
551 f0cbd3ec bellard
                         * Let ip_intr's mcast routing check handle mcast pkts
552 f0cbd3ec bellard
                         */
553 f0cbd3ec bellard
                        forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
554 f0cbd3ec bellard
                        break;
555 f0cbd3ec bellard
556 f0cbd3ec bellard
                case IPOPT_RR:
557 f0cbd3ec bellard
                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
558 f0cbd3ec bellard
                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
559 f0cbd3ec bellard
                                goto bad;
560 f0cbd3ec bellard
                        }
561 f0cbd3ec bellard
                        /*
562 f0cbd3ec bellard
                         * If no space remains, ignore.
563 f0cbd3ec bellard
                         */
564 cf2846b5 Stefan Weil
                        off--; /* 0 origin */
565 f0cbd3ec bellard
                        if (off > optlen - sizeof(struct in_addr))
566 f0cbd3ec bellard
                                break;
567 f0cbd3ec bellard
                        bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
568 f0cbd3ec bellard
                            sizeof(ipaddr.sin_addr));
569 f0cbd3ec bellard
                        /*
570 f0cbd3ec bellard
                         * locate outgoing interface; if we're the destination,
571 f0cbd3ec bellard
                         * use the incoming interface (should be same).
572 f0cbd3ec bellard
                         */
573 f0cbd3ec bellard
                        if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
574 f0cbd3ec bellard
                            (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
575 f0cbd3ec bellard
                                type = ICMP_UNREACH;
576 f0cbd3ec bellard
                                code = ICMP_UNREACH_HOST;
577 f0cbd3ec bellard
                                goto bad;
578 f0cbd3ec bellard
                        }
579 f0cbd3ec bellard
                        bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
580 f0cbd3ec bellard
                            (caddr_t)(cp + off), sizeof(struct in_addr));
581 f0cbd3ec bellard
                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
582 f0cbd3ec bellard
                        break;
583 f0cbd3ec bellard
584 f0cbd3ec bellard
                case IPOPT_TS:
585 f0cbd3ec bellard
                        code = cp - (u_char *)ip;
586 f0cbd3ec bellard
                        ipt = (struct ip_timestamp *)cp;
587 f0cbd3ec bellard
                        if (ipt->ipt_len < 5)
588 f0cbd3ec bellard
                                goto bad;
589 f0cbd3ec bellard
                        if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
590 f0cbd3ec bellard
                                if (++ipt->ipt_oflw == 0)
591 f0cbd3ec bellard
                                        goto bad;
592 f0cbd3ec bellard
                                break;
593 f0cbd3ec bellard
                        }
594 f0cbd3ec bellard
                        sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
595 f0cbd3ec bellard
                        switch (ipt->ipt_flg) {
596 f0cbd3ec bellard
597 f0cbd3ec bellard
                        case IPOPT_TS_TSONLY:
598 f0cbd3ec bellard
                                break;
599 f0cbd3ec bellard
600 f0cbd3ec bellard
                        case IPOPT_TS_TSANDADDR:
601 f0cbd3ec bellard
                                if (ipt->ipt_ptr + sizeof(n_time) +
602 f0cbd3ec bellard
                                    sizeof(struct in_addr) > ipt->ipt_len)
603 f0cbd3ec bellard
                                        goto bad;
604 f0cbd3ec bellard
                                ipaddr.sin_addr = dst;
605 f0cbd3ec bellard
                                ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
606 f0cbd3ec bellard
                                                            m->m_pkthdr.rcvif);
607 f0cbd3ec bellard
                                if (ia == 0)
608 f0cbd3ec bellard
                                        continue;
609 f0cbd3ec bellard
                                bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
610 f0cbd3ec bellard
                                    (caddr_t)sin, sizeof(struct in_addr));
611 f0cbd3ec bellard
                                ipt->ipt_ptr += sizeof(struct in_addr);
612 f0cbd3ec bellard
                                break;
613 f0cbd3ec bellard
614 f0cbd3ec bellard
                        case IPOPT_TS_PRESPEC:
615 f0cbd3ec bellard
                                if (ipt->ipt_ptr + sizeof(n_time) +
616 f0cbd3ec bellard
                                    sizeof(struct in_addr) > ipt->ipt_len)
617 f0cbd3ec bellard
                                        goto bad;
618 f0cbd3ec bellard
                                bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
619 f0cbd3ec bellard
                                    sizeof(struct in_addr));
620 f0cbd3ec bellard
                                if (ifa_ifwithaddr((SA)&ipaddr) == 0)
621 f0cbd3ec bellard
                                        continue;
622 f0cbd3ec bellard
                                ipt->ipt_ptr += sizeof(struct in_addr);
623 f0cbd3ec bellard
                                break;
624 f0cbd3ec bellard
625 f0cbd3ec bellard
                        default:
626 f0cbd3ec bellard
                                goto bad;
627 f0cbd3ec bellard
                        }
628 f0cbd3ec bellard
                        ntime = iptime();
629 f0cbd3ec bellard
                        bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
630 f0cbd3ec bellard
                            sizeof(n_time));
631 f0cbd3ec bellard
                        ipt->ipt_ptr += sizeof(n_time);
632 f0cbd3ec bellard
                }
633 f0cbd3ec bellard
        }
634 f0cbd3ec bellard
        if (forward) {
635 f0cbd3ec bellard
                ip_forward(m, 1);
636 f0cbd3ec bellard
                return (1);
637 f0cbd3ec bellard
        }
638 f0cbd3ec bellard
        return (0);
639 f0cbd3ec bellard
bad:
640 f0cbd3ec bellard
         icmp_error(m, type, code, 0, 0);
641 f0cbd3ec bellard
642 f0cbd3ec bellard
        return (1);
643 f0cbd3ec bellard
}
644 f0cbd3ec bellard
645 f0cbd3ec bellard
#endif /* notdef */
646 f0cbd3ec bellard
647 f0cbd3ec bellard
/*
648 f0cbd3ec bellard
 * Strip out IP options, at higher
649 f0cbd3ec bellard
 * level protocol in the kernel.
650 f0cbd3ec bellard
 * Second argument is buffer to which options
651 f0cbd3ec bellard
 * will be moved, and return value is their length.
652 f0cbd3ec bellard
 * (XXX) should be deleted; last arg currently ignored.
653 f0cbd3ec bellard
 */
654 f0cbd3ec bellard
void
655 511d2b14 blueswir1
ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
656 f0cbd3ec bellard
{
657 f0cbd3ec bellard
        register int i;
658 f0cbd3ec bellard
        struct ip *ip = mtod(m, struct ip *);
659 f0cbd3ec bellard
        register caddr_t opts;
660 f0cbd3ec bellard
        int olen;
661 f0cbd3ec bellard
662 f0cbd3ec bellard
        olen = (ip->ip_hl<<2) - sizeof (struct ip);
663 f0cbd3ec bellard
        opts = (caddr_t)(ip + 1);
664 f0cbd3ec bellard
        i = m->m_len - (sizeof (struct ip) + olen);
665 f0cbd3ec bellard
        memcpy(opts, opts  + olen, (unsigned)i);
666 f0cbd3ec bellard
        m->m_len -= olen;
667 5fafdf24 ths
668 f0cbd3ec bellard
        ip->ip_hl = sizeof(struct ip) >> 2;
669 f0cbd3ec bellard
}