blob: 77cef7c50b566e132eddf8a0e50695d151ac14b4 [file] [log] [blame]
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070013 * 3. Neither the name of the University nor the names of its contributors
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080014 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
30 * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
31 */
32
33/*
34 * Changes and additions relating to SLiRP are
35 * Copyright (c) 1995 Danny Gasparovski.
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070036 *
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080037 * Please read the file COPYRIGHT for the
38 * terms and conditions of the copyright.
39 */
40
41#include <slirp.h>
David 'Digit' Turner84569132013-12-13 17:34:07 +010042#include <qemu/osdep.h>
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080043#include "ip_icmp.h"
44
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070045#ifdef LOG_ENABLED
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080046struct ipstat ipstat;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070047#endif
48
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080049struct ipq ipq;
50
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070051static struct ip *ip_reass(register struct ip *ip,
52 register struct ipq *fp);
53static void ip_freef(struct ipq *fp);
54static void ip_enq(register struct ipasfrag *p,
55 register struct ipasfrag *prev);
56static void ip_deq(register struct ipasfrag *p);
57
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080058/*
59 * IP initialization: fill in IP protocol switch table.
60 * All protocols not implemented in kernel go to raw IP protocol handler.
61 */
62void
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070063ip_init(void)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080064{
65 ipq.ip_link.next = ipq.ip_link.prev = &ipq.ip_link;
66 ip_id = tt.tv_sec & 0xffff;
67 udp_init();
68 tcp_init();
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080069}
70
71/*
72 * Ip input routine. Checksum and byte swap header. If fragmented
73 * try to reassemble. Process options. Pass to next level.
74 */
75void
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070076ip_input(struct mbuf *m)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080077{
78 register struct ip *ip;
79 int hlen;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070080
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080081 DEBUG_CALL("ip_input");
82 DEBUG_ARG("m = %lx", (long)m);
83 DEBUG_ARG("m_len = %d", m->m_len);
84
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070085 STAT(ipstat.ips_total++);
86
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080087 if (m->m_len < sizeof (struct ip)) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070088 STAT(ipstat.ips_toosmall++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080089 return;
90 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070091
92 ip = mtod(m, struct ip *);
93
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080094 if (ip->ip_v != IPVERSION) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070095 STAT(ipstat.ips_badvers++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080096 goto bad;
97 }
98
99 hlen = ip->ip_hl << 2;
100 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700101 STAT(ipstat.ips_badhlen++); /* or packet too short */
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800102 goto bad;
103 }
104
105 /* keep ip header intact for ICMP reply
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700106 * ip->ip_sum = cksum(m, hlen);
107 * if (ip->ip_sum) {
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800108 */
109 if(cksum(m,hlen)) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700110 STAT(ipstat.ips_badsum++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800111 goto bad;
112 }
113
114 /*
115 * Convert fields to host representation.
116 */
117 NTOHS(ip->ip_len);
118 if (ip->ip_len < hlen) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700119 STAT(ipstat.ips_badlen++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800120 goto bad;
121 }
122 NTOHS(ip->ip_id);
123 NTOHS(ip->ip_off);
124
125 /*
126 * Check that the amount of data in the buffers
127 * is as at least much as the IP header would have us expect.
128 * Trim mbufs if longer than we expect.
129 * Drop packet if shorter than we expect.
130 */
131 if (m->m_len < ip->ip_len) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700132 STAT(ipstat.ips_tooshort++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800133 goto bad;
134 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700135
136 if (slirp_restrict) {
137 if (memcmp(&ip->ip_dst.s_addr, &special_addr, 3)) {
138 if (ip->ip_dst.s_addr == 0xffffffff && ip->ip_p != IPPROTO_UDP)
139 goto bad;
140 } else {
141 int host = ntohl(ip->ip_dst.s_addr) & 0xff;
142 struct ex_list *ex_ptr;
143
144 if (host == 0xff)
145 goto bad;
146
147 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
148 if (ex_ptr->ex_addr == host)
149 break;
150
151 if (!ex_ptr)
152 goto bad;
153 }
154 }
155
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800156 /* Should drop packet if mbuf too long? hmmm... */
157 if (m->m_len > ip->ip_len)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700158 m_adj(m, ip->ip_len - m->m_len);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800159
160 /* check ip_ttl for a correct ICMP reply */
161 if(ip->ip_ttl==0 || ip->ip_ttl==1) {
162 icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
163 goto bad;
164 }
165
166 /*
167 * Process options and, if not destined for us,
168 * ship it on. ip_dooptions returns 1 when an
169 * error was detected (causing an icmp message
170 * to be sent and the original packet to be freed).
171 */
172/* We do no IP options */
173/* if (hlen > sizeof (struct ip) && ip_dooptions(m))
174 * goto next;
175 */
176 /*
177 * If offset or IP_MF are set, must reassemble.
178 * Otherwise, nothing need be done.
179 * (We could look in the reassembly queue to see
180 * if the packet was previously fragmented,
181 * but it's not worth the time; just let them time out.)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700182 *
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800183 * XXX This should fail, don't fragment yet
184 */
185 if (ip->ip_off &~ IP_DF) {
186 register struct ipq *fp;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700187 struct qlink *l;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800188 /*
189 * Look for queue of fragments
190 * of this datagram.
191 */
192 for (l = ipq.ip_link.next; l != &ipq.ip_link; l = l->next) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700193 fp = container_of(l, struct ipq, ip_link);
194 if (ip->ip_id == fp->ipq_id &&
195 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
196 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
197 ip->ip_p == fp->ipq_p)
198 goto found;
199 }
200 fp = NULL;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800201 found:
202
203 /*
204 * Adjust ip_len to not reflect header,
205 * set ip_mff if more fragments are expected,
206 * convert offset of this to bytes.
207 */
208 ip->ip_len -= hlen;
209 if (ip->ip_off & IP_MF)
210 ip->ip_tos |= 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700211 else
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800212 ip->ip_tos &= ~1;
213
214 ip->ip_off <<= 3;
215
216 /*
217 * If datagram marked as having more fragments
218 * or if this is not the first fragment,
219 * attempt reassembly; if it succeeds, proceed.
220 */
221 if (ip->ip_tos & 1 || ip->ip_off) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700222 STAT(ipstat.ips_fragments++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800223 ip = ip_reass(ip, fp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700224 if (ip == NULL)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800225 return;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700226 STAT(ipstat.ips_reassembled++);
227 m = dtom(ip);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800228 } else
229 if (fp)
230 ip_freef(fp);
231
232 } else
233 ip->ip_len -= hlen;
234
235 /*
236 * Switch out to protocol's input routine.
237 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700238 STAT(ipstat.ips_delivered++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800239 switch (ip->ip_p) {
240 case IPPROTO_TCP:
241 tcp_input(m, hlen, (struct socket *)NULL);
242 break;
243 case IPPROTO_UDP:
244 udp_input(m, hlen);
245 break;
246 case IPPROTO_ICMP:
247 icmp_input(m, hlen);
248 break;
249 default:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700250 STAT(ipstat.ips_noproto++);
251 m_free(m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800252 }
253 return;
254bad:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700255 m_freem(m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800256 return;
257}
258
259#define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
260#define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800261/*
262 * Take incoming datagram fragment and try to
263 * reassemble it into whole datagram. If a chain for
264 * reassembly of this datagram already exists, then it
265 * is given as fp; otherwise have to make a chain.
266 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700267static struct ip *
268ip_reass(register struct ip *ip, register struct ipq *fp)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800269{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700270 register struct mbuf *m = dtom(ip);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800271 register struct ipasfrag *q;
272 int hlen = ip->ip_hl << 2;
273 int i, next;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700274
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800275 DEBUG_CALL("ip_reass");
276 DEBUG_ARG("ip = %lx", (long)ip);
277 DEBUG_ARG("fp = %lx", (long)fp);
278 DEBUG_ARG("m = %lx", (long)m);
279
280 /*
281 * Presence of header sizes in mbufs
282 * would confuse code below.
283 * Fragment m_data is concatenated.
284 */
285 m->m_data += hlen;
286 m->m_len -= hlen;
287
288 /*
289 * If first fragment to arrive, create a reassembly queue.
290 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700291 if (fp == NULL) {
292 struct mbuf *t;
293 if ((t = m_get()) == NULL) goto dropfrag;
294 fp = mtod(t, struct ipq *);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800295 insque(&fp->ip_link, &ipq.ip_link);
296 fp->ipq_ttl = IPFRAGTTL;
297 fp->ipq_p = ip->ip_p;
298 fp->ipq_id = ip->ip_id;
299 fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
300 fp->ipq_src = ip->ip_src;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700301 fp->ipq_dst = ip->ip_dst;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800302 q = (struct ipasfrag *)fp;
303 goto insert;
304 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700305
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800306 /*
307 * Find a segment which begins after this one does.
308 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700309 for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
310 q = q->ipf_next)
311 if (q->ipf_off > ip->ip_off)
312 break;
313
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800314 /*
315 * If there is a preceding segment, it may provide some of
316 * our data already. If so, drop the data from the incoming
317 * segment. If it provides all of our data, drop us.
318 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700319 if (q->ipf_prev != &fp->frag_link) {
320 struct ipasfrag *pq = q->ipf_prev;
321 i = pq->ipf_off + pq->ipf_len - ip->ip_off;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800322 if (i > 0) {
323 if (i >= ip->ip_len)
324 goto dropfrag;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700325 m_adj(dtom(ip), i);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800326 ip->ip_off += i;
327 ip->ip_len -= i;
328 }
329 }
330
331 /*
332 * While we overlap succeeding segments trim them or,
333 * if they are completely covered, dequeue them.
334 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700335 while (q != (struct ipasfrag*)&fp->frag_link &&
336 ip->ip_off + ip->ip_len > q->ipf_off) {
337 i = (ip->ip_off + ip->ip_len) - q->ipf_off;
338 if (i < q->ipf_len) {
339 q->ipf_len -= i;
340 q->ipf_off += i;
341 m_adj(dtom(q), i);
342 break;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800343 }
344 q = q->ipf_next;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700345 m_freem(dtom(q->ipf_prev));
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800346 ip_deq(q->ipf_prev);
347 }
348
349insert:
350 /*
351 * Stick new segment in its place;
352 * check for complete reassembly.
353 */
354 ip_enq(iptofrag(ip), q->ipf_prev);
355 next = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700356 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
357 q = q->ipf_next) {
358 if (q->ipf_off != next)
359 return NULL;
360 next += q->ipf_len;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800361 }
362 if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700363 return NULL;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800364
365 /*
366 * Reassembly is complete; concatenate fragments.
367 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700368 q = fp->frag_link.next;
369 m = dtom(q);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800370
371 q = (struct ipasfrag *) q->ipf_next;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700372 while (q != (struct ipasfrag*)&fp->frag_link) {
373 struct mbuf *t = dtom(q);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800374 q = (struct ipasfrag *) q->ipf_next;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700375 m_cat(m, t);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800376 }
377
378 /*
379 * Create header for new ip packet by
380 * modifying header of first packet;
381 * dequeue and discard fragment reassembly header.
382 * Make header visible.
383 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700384 q = fp->frag_link.next;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800385
386 /*
387 * If the fragments concatenated to an mbuf that's
388 * bigger than the total size of the fragment, then and
389 * m_ext buffer was alloced. But fp->ipq_next points to
390 * the old buffer (in the mbuf), so we must point ip
391 * into the new buffer.
392 */
393 if (m->m_flags & M_EXT) {
394 int delta = (char *)q - m->m_dat;
395 q = (struct ipasfrag *)(m->m_ext + delta);
396 }
397
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700398 /* DEBUG_ARG("ip = %lx", (long)ip);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800399 * ip=(struct ipasfrag *)m->m_data; */
400
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700401 ip = fragtoip(q);
402 ip->ip_len = next;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800403 ip->ip_tos &= ~1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700404 ip->ip_src = fp->ipq_src;
405 ip->ip_dst = fp->ipq_dst;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800406 remque(&fp->ip_link);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700407 (void) m_free(dtom(fp));
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800408 m->m_len += (ip->ip_hl << 2);
409 m->m_data -= (ip->ip_hl << 2);
410
411 return ip;
412
413dropfrag:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700414 STAT(ipstat.ips_fragdropped++);
415 m_freem(m);
416 return NULL;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800417}
418
419/*
420 * Free a fragment reassembly header and all
421 * associated datagrams.
422 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700423static void
424ip_freef(struct ipq *fp)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800425{
426 register struct ipasfrag *q, *p;
427
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700428 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800429 p = q->ipf_next;
430 ip_deq(q);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700431 m_freem(dtom(q));
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800432 }
433 remque(&fp->ip_link);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700434 (void) m_free(dtom(fp));
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800435}
436
437/*
438 * Put an ip fragment on a reassembly chain.
439 * Like insque, but pointers in middle of structure.
440 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700441static void
442ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800443{
444 DEBUG_CALL("ip_enq");
445 DEBUG_ARG("prev = %lx", (long)prev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700446 p->ipf_prev = prev;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800447 p->ipf_next = prev->ipf_next;
448 ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
449 prev->ipf_next = p;
450}
451
452/*
453 * To ip_enq as remque is to insque.
454 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700455static void
456ip_deq(register struct ipasfrag *p)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800457{
458 ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
459 ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
460}
461
462/*
463 * IP timer processing;
464 * if a timer expires on a reassembly
465 * queue, discard it.
466 */
467void
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700468ip_slowtimo(void)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800469{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700470 struct qlink *l;
471
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800472 DEBUG_CALL("ip_slowtimo");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700473
474 l = ipq.ip_link.next;
475
476 if (l == NULL)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800477 return;
478
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700479 while (l != &ipq.ip_link) {
480 struct ipq *fp = container_of(l, struct ipq, ip_link);
481 l = l->next;
482 if (--fp->ipq_ttl == 0) {
483 STAT(ipstat.ips_fragtimeout++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800484 ip_freef(fp);
485 }
486 }
487}
488
489/*
490 * Do option processing on a datagram,
491 * possibly discarding it if bad options are encountered,
492 * or forwarding it if source-routed.
493 * Returns 1 if packet has been forwarded/freed,
494 * 0 if the packet should be processed further.
495 */
496
497#ifdef notdef
498
499int
500ip_dooptions(m)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700501 struct mbuf *m;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800502{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700503 register struct ip *ip = mtod(m, struct ip *);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800504 register u_char *cp;
505 register struct ip_timestamp *ipt;
506 register struct in_ifaddr *ia;
507/* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
508 int opt, optlen, cnt, off, code, type, forward = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700509 struct in_addr *sin, dst;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800510typedef u_int32_t n_time;
511 n_time ntime;
512
513 dst = ip->ip_dst;
514 cp = (u_char *)(ip + 1);
515 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
516 for (; cnt > 0; cnt -= optlen, cp += optlen) {
517 opt = cp[IPOPT_OPTVAL];
518 if (opt == IPOPT_EOL)
519 break;
520 if (opt == IPOPT_NOP)
521 optlen = 1;
522 else {
523 optlen = cp[IPOPT_OLEN];
524 if (optlen <= 0 || optlen > cnt) {
525 code = &cp[IPOPT_OLEN] - (u_char *)ip;
526 goto bad;
527 }
528 }
529 switch (opt) {
530
531 default:
532 break;
533
534 /*
535 * Source routing with record.
536 * Find interface with current destination address.
537 * If none on this machine then drop if strictly routed,
538 * or do nothing if loosely routed.
539 * Record interface address and bring up next address
540 * component. If strictly routed make sure next
541 * address is on directly accessible net.
542 */
543 case IPOPT_LSRR:
544 case IPOPT_SSRR:
545 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
546 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
547 goto bad;
548 }
549 ipaddr.sin_addr = ip->ip_dst;
550 ia = (struct in_ifaddr *)
551 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
552 if (ia == 0) {
553 if (opt == IPOPT_SSRR) {
554 type = ICMP_UNREACH;
555 code = ICMP_UNREACH_SRCFAIL;
556 goto bad;
557 }
558 /*
559 * Loose routing, and not at next destination
560 * yet; nothing to do except forward.
561 */
562 break;
563 }
564 off--; / * 0 origin * /
565 if (off > optlen - sizeof(struct in_addr)) {
566 /*
567 * End of source route. Should be for us.
568 */
569 save_rte(cp, ip->ip_src);
570 break;
571 }
572 /*
573 * locate outgoing interface
574 */
575 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
576 sizeof(ipaddr.sin_addr));
577 if (opt == IPOPT_SSRR) {
578#define INA struct in_ifaddr *
579#define SA struct sockaddr *
580 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
581 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
582 } else
583 ia = ip_rtaddr(ipaddr.sin_addr);
584 if (ia == 0) {
585 type = ICMP_UNREACH;
586 code = ICMP_UNREACH_SRCFAIL;
587 goto bad;
588 }
589 ip->ip_dst = ipaddr.sin_addr;
590 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
591 (caddr_t)(cp + off), sizeof(struct in_addr));
592 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
593 /*
594 * Let ip_intr's mcast routing check handle mcast pkts
595 */
596 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
597 break;
598
599 case IPOPT_RR:
600 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
601 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
602 goto bad;
603 }
604 /*
605 * If no space remains, ignore.
606 */
607 off--; * 0 origin *
608 if (off > optlen - sizeof(struct in_addr))
609 break;
610 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
611 sizeof(ipaddr.sin_addr));
612 /*
613 * locate outgoing interface; if we're the destination,
614 * use the incoming interface (should be same).
615 */
616 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
617 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
618 type = ICMP_UNREACH;
619 code = ICMP_UNREACH_HOST;
620 goto bad;
621 }
622 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
623 (caddr_t)(cp + off), sizeof(struct in_addr));
624 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
625 break;
626
627 case IPOPT_TS:
628 code = cp - (u_char *)ip;
629 ipt = (struct ip_timestamp *)cp;
630 if (ipt->ipt_len < 5)
631 goto bad;
632 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
633 if (++ipt->ipt_oflw == 0)
634 goto bad;
635 break;
636 }
637 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
638 switch (ipt->ipt_flg) {
639
640 case IPOPT_TS_TSONLY:
641 break;
642
643 case IPOPT_TS_TSANDADDR:
644 if (ipt->ipt_ptr + sizeof(n_time) +
645 sizeof(struct in_addr) > ipt->ipt_len)
646 goto bad;
647 ipaddr.sin_addr = dst;
648 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
649 m->m_pkthdr.rcvif);
650 if (ia == 0)
651 continue;
652 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
653 (caddr_t)sin, sizeof(struct in_addr));
654 ipt->ipt_ptr += sizeof(struct in_addr);
655 break;
656
657 case IPOPT_TS_PRESPEC:
658 if (ipt->ipt_ptr + sizeof(n_time) +
659 sizeof(struct in_addr) > ipt->ipt_len)
660 goto bad;
661 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
662 sizeof(struct in_addr));
663 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
664 continue;
665 ipt->ipt_ptr += sizeof(struct in_addr);
666 break;
667
668 default:
669 goto bad;
670 }
671 ntime = iptime();
672 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
673 sizeof(n_time));
674 ipt->ipt_ptr += sizeof(n_time);
675 }
676 }
677 if (forward) {
678 ip_forward(m, 1);
679 return (1);
680 }
681 }
682 }
683 return (0);
684bad:
685 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
686
687/* Not yet */
688 icmp_error(m, type, code, 0, 0);
689
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700690 STAT(ipstat.ips_badoptions++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800691 return (1);
692}
693
694#endif /* notdef */
695
696/*
697 * Strip out IP options, at higher
698 * level protocol in the kernel.
699 * Second argument is buffer to which options
700 * will be moved, and return value is their length.
701 * (XXX) should be deleted; last arg currently ignored.
702 */
703void
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700704ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800705{
706 register int i;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700707 struct ip *ip = mtod(m, struct ip *);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800708 register caddr_t opts;
709 int olen;
710
711 olen = (ip->ip_hl<<2) - sizeof (struct ip);
712 opts = (caddr_t)(ip + 1);
713 i = m->m_len - (sizeof (struct ip) + olen);
714 memcpy(opts, opts + olen, (unsigned)i);
715 m->m_len -= olen;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700716
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800717 ip->ip_hl = sizeof(struct ip) >> 2;
718}