Statistics
| Branch: | Revision:

root / slirp / ip_input.c @ e3b42536

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