Statistics
| Branch: | Revision:

root / slirp / ip_input.c @ 1b2b0af5

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