Statistics
| Branch: | Revision:

root / slirp / ip_input.c @ 7878ff6b

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