Statistics
| Branch: | Revision:

root / slirp / ip_input.c @ 460fec67

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