blob: 880bdfd3ccf43dcdabf324cb72a7545a79c4364e [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
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.
aliguori2f5f8992009-01-26 19:37:41 +000013 * 3. Neither the name of the University nor the names of its contributors
bellardf0cbd3e2004-04-22 00:10:48 +000014 * 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.
ths5fafdf22007-09-16 21:08:06 +000036 *
bellardf0cbd3e2004-04-22 00:10:48 +000037 * Please read the file COPYRIGHT for the
38 * terms and conditions of the copyright.
39 */
40
41#include <slirp.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010042#include <qemu/osdep.h>
bellardf0cbd3e2004-04-22 00:10:48 +000043#include "ip_icmp.h"
44
Jan Kiszka460fec62009-06-24 14:42:31 +020045static struct ip *ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp);
46static void ip_freef(Slirp *slirp, struct ipq *fp);
blueswir19634d902007-10-26 19:01:16 +000047static void ip_enq(register struct ipasfrag *p,
48 register struct ipasfrag *prev);
49static void ip_deq(register struct ipasfrag *p);
50
bellardf0cbd3e2004-04-22 00:10:48 +000051/*
52 * IP initialization: fill in IP protocol switch table.
53 * All protocols not implemented in kernel go to raw IP protocol handler.
54 */
55void
Jan Kiszka460fec62009-06-24 14:42:31 +020056ip_init(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +000057{
Jan Kiszka460fec62009-06-24 14:42:31 +020058 slirp->ipq.ip_link.next = slirp->ipq.ip_link.prev = &slirp->ipq.ip_link;
59 udp_init(slirp);
60 tcp_init(slirp);
Jan Kiszkae6d43cf2011-07-20 12:20:18 +020061 icmp_init(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +000062}
63
Jan Kiszkaa68adc22012-02-29 19:14:23 +010064void ip_cleanup(Slirp *slirp)
65{
66 udp_cleanup(slirp);
67 tcp_cleanup(slirp);
68 icmp_cleanup(slirp);
69}
70
bellardf0cbd3e2004-04-22 00:10:48 +000071/*
72 * Ip input routine. Checksum and byte swap header. If fragmented
73 * try to reassemble. Process options. Pass to next level.
74 */
75void
blueswir1511d2b12009-03-07 15:32:56 +000076ip_input(struct mbuf *m)
bellardf0cbd3e2004-04-22 00:10:48 +000077{
Jan Kiszka460fec62009-06-24 14:42:31 +020078 Slirp *slirp = m->slirp;
bellardf0cbd3e2004-04-22 00:10:48 +000079 register struct ip *ip;
80 int hlen;
ths5fafdf22007-09-16 21:08:06 +000081
bellardf0cbd3e2004-04-22 00:10:48 +000082 DEBUG_CALL("ip_input");
83 DEBUG_ARG("m = %lx", (long)m);
84 DEBUG_ARG("m_len = %d", m->m_len);
85
bellardf0cbd3e2004-04-22 00:10:48 +000086 if (m->m_len < sizeof (struct ip)) {
bellardf0cbd3e2004-04-22 00:10:48 +000087 return;
88 }
ths5fafdf22007-09-16 21:08:06 +000089
bellardf0cbd3e2004-04-22 00:10:48 +000090 ip = mtod(m, struct ip *);
ths5fafdf22007-09-16 21:08:06 +000091
bellardf0cbd3e2004-04-22 00:10:48 +000092 if (ip->ip_v != IPVERSION) {
bellardf0cbd3e2004-04-22 00:10:48 +000093 goto bad;
94 }
95
96 hlen = ip->ip_hl << 2;
97 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
Jan Kiszka0fe6a7f2009-06-24 14:42:29 +020098 goto bad; /* or packet too short */
bellardf0cbd3e2004-04-22 00:10:48 +000099 }
100
101 /* keep ip header intact for ICMP reply
ths5fafdf22007-09-16 21:08:06 +0000102 * ip->ip_sum = cksum(m, hlen);
103 * if (ip->ip_sum) {
bellardf0cbd3e2004-04-22 00:10:48 +0000104 */
105 if(cksum(m,hlen)) {
bellardf0cbd3e2004-04-22 00:10:48 +0000106 goto bad;
107 }
108
109 /*
110 * Convert fields to host representation.
111 */
112 NTOHS(ip->ip_len);
113 if (ip->ip_len < hlen) {
bellardf0cbd3e2004-04-22 00:10:48 +0000114 goto bad;
115 }
116 NTOHS(ip->ip_id);
117 NTOHS(ip->ip_off);
118
119 /*
120 * Check that the amount of data in the buffers
121 * is as at least much as the IP header would have us expect.
122 * Trim mbufs if longer than we expect.
123 * Drop packet if shorter than we expect.
124 */
125 if (m->m_len < ip->ip_len) {
bellardf0cbd3e2004-04-22 00:10:48 +0000126 goto bad;
127 }
aliguoria9ba3a82009-01-08 19:24:00 +0000128
bellardf0cbd3e2004-04-22 00:10:48 +0000129 /* Should drop packet if mbuf too long? hmmm... */
130 if (m->m_len > ip->ip_len)
131 m_adj(m, ip->ip_len - m->m_len);
132
133 /* check ip_ttl for a correct ICMP reply */
Hervé Poussineau0d491752010-09-13 23:01:30 +0200134 if(ip->ip_ttl==0) {
bellardf0cbd3e2004-04-22 00:10:48 +0000135 icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
136 goto bad;
137 }
138
139 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000140 * If offset or IP_MF are set, must reassemble.
141 * Otherwise, nothing need be done.
142 * (We could look in the reassembly queue to see
143 * if the packet was previously fragmented,
144 * but it's not worth the time; just let them time out.)
ths5fafdf22007-09-16 21:08:06 +0000145 *
bellardf0cbd3e2004-04-22 00:10:48 +0000146 * XXX This should fail, don't fragment yet
147 */
148 if (ip->ip_off &~ IP_DF) {
149 register struct ipq *fp;
blueswir1429d0a32009-01-13 19:48:42 +0000150 struct qlink *l;
bellardf0cbd3e2004-04-22 00:10:48 +0000151 /*
152 * Look for queue of fragments
153 * of this datagram.
154 */
Jan Kiszka460fec62009-06-24 14:42:31 +0200155 for (l = slirp->ipq.ip_link.next; l != &slirp->ipq.ip_link;
156 l = l->next) {
blueswir1429d0a32009-01-13 19:48:42 +0000157 fp = container_of(l, struct ipq, ip_link);
158 if (ip->ip_id == fp->ipq_id &&
159 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
160 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
161 ip->ip_p == fp->ipq_p)
bellardf0cbd3e2004-04-22 00:10:48 +0000162 goto found;
blueswir1429d0a32009-01-13 19:48:42 +0000163 }
164 fp = NULL;
bellardf0cbd3e2004-04-22 00:10:48 +0000165 found:
166
167 /*
168 * Adjust ip_len to not reflect header,
169 * set ip_mff if more fragments are expected,
170 * convert offset of this to bytes.
171 */
172 ip->ip_len -= hlen;
173 if (ip->ip_off & IP_MF)
blueswir1429d0a32009-01-13 19:48:42 +0000174 ip->ip_tos |= 1;
ths5fafdf22007-09-16 21:08:06 +0000175 else
blueswir1429d0a32009-01-13 19:48:42 +0000176 ip->ip_tos &= ~1;
bellardf0cbd3e2004-04-22 00:10:48 +0000177
178 ip->ip_off <<= 3;
179
180 /*
181 * If datagram marked as having more fragments
182 * or if this is not the first fragment,
183 * attempt reassembly; if it succeeds, proceed.
184 */
blueswir1429d0a32009-01-13 19:48:42 +0000185 if (ip->ip_tos & 1 || ip->ip_off) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200186 ip = ip_reass(slirp, ip, fp);
blueswir1511d2b12009-03-07 15:32:56 +0000187 if (ip == NULL)
bellardf0cbd3e2004-04-22 00:10:48 +0000188 return;
Jan Kiszka460fec62009-06-24 14:42:31 +0200189 m = dtom(slirp, ip);
bellardf0cbd3e2004-04-22 00:10:48 +0000190 } else
191 if (fp)
Jan Kiszka460fec62009-06-24 14:42:31 +0200192 ip_freef(slirp, fp);
bellardf0cbd3e2004-04-22 00:10:48 +0000193
194 } else
195 ip->ip_len -= hlen;
196
197 /*
198 * Switch out to protocol's input routine.
199 */
bellardf0cbd3e2004-04-22 00:10:48 +0000200 switch (ip->ip_p) {
201 case IPPROTO_TCP:
202 tcp_input(m, hlen, (struct socket *)NULL);
203 break;
204 case IPPROTO_UDP:
205 udp_input(m, hlen);
206 break;
207 case IPPROTO_ICMP:
208 icmp_input(m, hlen);
209 break;
210 default:
bellardf0cbd3e2004-04-22 00:10:48 +0000211 m_free(m);
212 }
213 return;
214bad:
Jan Kiszka3acccfc2011-07-20 12:20:16 +0200215 m_free(m);
bellardf0cbd3e2004-04-22 00:10:48 +0000216}
217
blueswir1429d0a32009-01-13 19:48:42 +0000218#define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
219#define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
bellardf0cbd3e2004-04-22 00:10:48 +0000220/*
221 * Take incoming datagram fragment and try to
222 * reassemble it into whole datagram. If a chain for
223 * reassembly of this datagram already exists, then it
224 * is given as fp; otherwise have to make a chain.
225 */
blueswir19634d902007-10-26 19:01:16 +0000226static struct ip *
Jan Kiszka460fec62009-06-24 14:42:31 +0200227ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp)
bellardf0cbd3e2004-04-22 00:10:48 +0000228{
Jan Kiszka460fec62009-06-24 14:42:31 +0200229 register struct mbuf *m = dtom(slirp, ip);
bellardf0cbd3e2004-04-22 00:10:48 +0000230 register struct ipasfrag *q;
231 int hlen = ip->ip_hl << 2;
232 int i, next;
ths5fafdf22007-09-16 21:08:06 +0000233
bellardf0cbd3e2004-04-22 00:10:48 +0000234 DEBUG_CALL("ip_reass");
235 DEBUG_ARG("ip = %lx", (long)ip);
236 DEBUG_ARG("fp = %lx", (long)fp);
237 DEBUG_ARG("m = %lx", (long)m);
238
239 /*
240 * Presence of header sizes in mbufs
241 * would confuse code below.
242 * Fragment m_data is concatenated.
243 */
244 m->m_data += hlen;
245 m->m_len -= hlen;
246
247 /*
248 * If first fragment to arrive, create a reassembly queue.
249 */
blueswir1511d2b12009-03-07 15:32:56 +0000250 if (fp == NULL) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200251 struct mbuf *t = m_get(slirp);
252
253 if (t == NULL) {
254 goto dropfrag;
255 }
bellardf0cbd3e2004-04-22 00:10:48 +0000256 fp = mtod(t, struct ipq *);
Jan Kiszka460fec62009-06-24 14:42:31 +0200257 insque(&fp->ip_link, &slirp->ipq.ip_link);
bellardf0cbd3e2004-04-22 00:10:48 +0000258 fp->ipq_ttl = IPFRAGTTL;
259 fp->ipq_p = ip->ip_p;
260 fp->ipq_id = ip->ip_id;
blueswir1429d0a32009-01-13 19:48:42 +0000261 fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
262 fp->ipq_src = ip->ip_src;
263 fp->ipq_dst = ip->ip_dst;
bellardf0cbd3e2004-04-22 00:10:48 +0000264 q = (struct ipasfrag *)fp;
265 goto insert;
266 }
ths5fafdf22007-09-16 21:08:06 +0000267
bellardf0cbd3e2004-04-22 00:10:48 +0000268 /*
269 * Find a segment which begins after this one does.
270 */
blueswir1429d0a32009-01-13 19:48:42 +0000271 for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
272 q = q->ipf_next)
273 if (q->ipf_off > ip->ip_off)
bellardf0cbd3e2004-04-22 00:10:48 +0000274 break;
275
276 /*
277 * If there is a preceding segment, it may provide some of
278 * our data already. If so, drop the data from the incoming
279 * segment. If it provides all of our data, drop us.
280 */
blueswir1429d0a32009-01-13 19:48:42 +0000281 if (q->ipf_prev != &fp->frag_link) {
282 struct ipasfrag *pq = q->ipf_prev;
283 i = pq->ipf_off + pq->ipf_len - ip->ip_off;
bellardf0cbd3e2004-04-22 00:10:48 +0000284 if (i > 0) {
285 if (i >= ip->ip_len)
286 goto dropfrag;
Jan Kiszka460fec62009-06-24 14:42:31 +0200287 m_adj(dtom(slirp, ip), i);
bellardf0cbd3e2004-04-22 00:10:48 +0000288 ip->ip_off += i;
289 ip->ip_len -= i;
290 }
291 }
292
293 /*
294 * While we overlap succeeding segments trim them or,
295 * if they are completely covered, dequeue them.
296 */
blueswir1429d0a32009-01-13 19:48:42 +0000297 while (q != (struct ipasfrag*)&fp->frag_link &&
298 ip->ip_off + ip->ip_len > q->ipf_off) {
299 i = (ip->ip_off + ip->ip_len) - q->ipf_off;
300 if (i < q->ipf_len) {
301 q->ipf_len -= i;
302 q->ipf_off += i;
Jan Kiszka460fec62009-06-24 14:42:31 +0200303 m_adj(dtom(slirp, q), i);
bellardf0cbd3e2004-04-22 00:10:48 +0000304 break;
305 }
blueswir1429d0a32009-01-13 19:48:42 +0000306 q = q->ipf_next;
Jan Kiszka3acccfc2011-07-20 12:20:16 +0200307 m_free(dtom(slirp, q->ipf_prev));
blueswir1429d0a32009-01-13 19:48:42 +0000308 ip_deq(q->ipf_prev);
bellardf0cbd3e2004-04-22 00:10:48 +0000309 }
310
311insert:
312 /*
313 * Stick new segment in its place;
314 * check for complete reassembly.
315 */
blueswir1429d0a32009-01-13 19:48:42 +0000316 ip_enq(iptofrag(ip), q->ipf_prev);
bellardf0cbd3e2004-04-22 00:10:48 +0000317 next = 0;
blueswir1429d0a32009-01-13 19:48:42 +0000318 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
319 q = q->ipf_next) {
320 if (q->ipf_off != next)
blueswir1511d2b12009-03-07 15:32:56 +0000321 return NULL;
blueswir1429d0a32009-01-13 19:48:42 +0000322 next += q->ipf_len;
bellardf0cbd3e2004-04-22 00:10:48 +0000323 }
blueswir1429d0a32009-01-13 19:48:42 +0000324 if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
blueswir1511d2b12009-03-07 15:32:56 +0000325 return NULL;
bellardf0cbd3e2004-04-22 00:10:48 +0000326
327 /*
328 * Reassembly is complete; concatenate fragments.
329 */
blueswir1429d0a32009-01-13 19:48:42 +0000330 q = fp->frag_link.next;
Jan Kiszka460fec62009-06-24 14:42:31 +0200331 m = dtom(slirp, q);
bellardf0cbd3e2004-04-22 00:10:48 +0000332
333 q = (struct ipasfrag *) q->ipf_next;
blueswir1429d0a32009-01-13 19:48:42 +0000334 while (q != (struct ipasfrag*)&fp->frag_link) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200335 struct mbuf *t = dtom(slirp, q);
bellardf0cbd3e2004-04-22 00:10:48 +0000336 q = (struct ipasfrag *) q->ipf_next;
bellardfedc54a2006-05-01 11:02:11 +0000337 m_cat(m, t);
bellardf0cbd3e2004-04-22 00:10:48 +0000338 }
339
340 /*
341 * Create header for new ip packet by
342 * modifying header of first packet;
343 * dequeue and discard fragment reassembly header.
344 * Make header visible.
345 */
blueswir1429d0a32009-01-13 19:48:42 +0000346 q = fp->frag_link.next;
bellardf0cbd3e2004-04-22 00:10:48 +0000347
348 /*
349 * If the fragments concatenated to an mbuf that's
350 * bigger than the total size of the fragment, then and
351 * m_ext buffer was alloced. But fp->ipq_next points to
352 * the old buffer (in the mbuf), so we must point ip
353 * into the new buffer.
354 */
355 if (m->m_flags & M_EXT) {
blueswir1f2ba7302009-02-06 21:37:40 +0000356 int delta = (char *)q - m->m_dat;
blueswir1429d0a32009-01-13 19:48:42 +0000357 q = (struct ipasfrag *)(m->m_ext + delta);
bellardf0cbd3e2004-04-22 00:10:48 +0000358 }
359
blueswir1429d0a32009-01-13 19:48:42 +0000360 ip = fragtoip(q);
bellardf0cbd3e2004-04-22 00:10:48 +0000361 ip->ip_len = next;
blueswir1429d0a32009-01-13 19:48:42 +0000362 ip->ip_tos &= ~1;
363 ip->ip_src = fp->ipq_src;
364 ip->ip_dst = fp->ipq_dst;
365 remque(&fp->ip_link);
Jan Kiszka460fec62009-06-24 14:42:31 +0200366 (void) m_free(dtom(slirp, fp));
bellardf0cbd3e2004-04-22 00:10:48 +0000367 m->m_len += (ip->ip_hl << 2);
368 m->m_data -= (ip->ip_hl << 2);
369
blueswir1429d0a32009-01-13 19:48:42 +0000370 return ip;
bellardf0cbd3e2004-04-22 00:10:48 +0000371
372dropfrag:
Jan Kiszka3acccfc2011-07-20 12:20:16 +0200373 m_free(m);
blueswir1511d2b12009-03-07 15:32:56 +0000374 return NULL;
bellardf0cbd3e2004-04-22 00:10:48 +0000375}
376
377/*
378 * Free a fragment reassembly header and all
379 * associated datagrams.
380 */
blueswir19634d902007-10-26 19:01:16 +0000381static void
Jan Kiszka460fec62009-06-24 14:42:31 +0200382ip_freef(Slirp *slirp, struct ipq *fp)
bellardf0cbd3e2004-04-22 00:10:48 +0000383{
384 register struct ipasfrag *q, *p;
385
blueswir1429d0a32009-01-13 19:48:42 +0000386 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
387 p = q->ipf_next;
bellardf0cbd3e2004-04-22 00:10:48 +0000388 ip_deq(q);
Jan Kiszka3acccfc2011-07-20 12:20:16 +0200389 m_free(dtom(slirp, q));
bellardf0cbd3e2004-04-22 00:10:48 +0000390 }
blueswir1429d0a32009-01-13 19:48:42 +0000391 remque(&fp->ip_link);
Jan Kiszka460fec62009-06-24 14:42:31 +0200392 (void) m_free(dtom(slirp, fp));
bellardf0cbd3e2004-04-22 00:10:48 +0000393}
394
395/*
396 * Put an ip fragment on a reassembly chain.
397 * Like insque, but pointers in middle of structure.
398 */
blueswir19634d902007-10-26 19:01:16 +0000399static void
400ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
bellardf0cbd3e2004-04-22 00:10:48 +0000401{
402 DEBUG_CALL("ip_enq");
403 DEBUG_ARG("prev = %lx", (long)prev);
blueswir1429d0a32009-01-13 19:48:42 +0000404 p->ipf_prev = prev;
bellardf0cbd3e2004-04-22 00:10:48 +0000405 p->ipf_next = prev->ipf_next;
blueswir1429d0a32009-01-13 19:48:42 +0000406 ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
407 prev->ipf_next = p;
bellardf0cbd3e2004-04-22 00:10:48 +0000408}
409
410/*
411 * To ip_enq as remque is to insque.
412 */
blueswir19634d902007-10-26 19:01:16 +0000413static void
414ip_deq(register struct ipasfrag *p)
bellardf0cbd3e2004-04-22 00:10:48 +0000415{
416 ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
417 ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
418}
419
420/*
421 * IP timer processing;
422 * if a timer expires on a reassembly
423 * queue, discard it.
424 */
425void
Jan Kiszka460fec62009-06-24 14:42:31 +0200426ip_slowtimo(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +0000427{
blueswir1429d0a32009-01-13 19:48:42 +0000428 struct qlink *l;
ths5fafdf22007-09-16 21:08:06 +0000429
bellardf0cbd3e2004-04-22 00:10:48 +0000430 DEBUG_CALL("ip_slowtimo");
ths5fafdf22007-09-16 21:08:06 +0000431
Jan Kiszka460fec62009-06-24 14:42:31 +0200432 l = slirp->ipq.ip_link.next;
blueswir1429d0a32009-01-13 19:48:42 +0000433
blueswir1511d2b12009-03-07 15:32:56 +0000434 if (l == NULL)
bellardf0cbd3e2004-04-22 00:10:48 +0000435 return;
436
Jan Kiszka460fec62009-06-24 14:42:31 +0200437 while (l != &slirp->ipq.ip_link) {
blueswir1429d0a32009-01-13 19:48:42 +0000438 struct ipq *fp = container_of(l, struct ipq, ip_link);
439 l = l->next;
440 if (--fp->ipq_ttl == 0) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200441 ip_freef(slirp, fp);
bellardf0cbd3e2004-04-22 00:10:48 +0000442 }
Jan Kiszka460fec62009-06-24 14:42:31 +0200443 }
bellardf0cbd3e2004-04-22 00:10:48 +0000444}
445
446/*
447 * Do option processing on a datagram,
448 * possibly discarding it if bad options are encountered,
449 * or forwarding it if source-routed.
450 * Returns 1 if packet has been forwarded/freed,
451 * 0 if the packet should be processed further.
452 */
453
454#ifdef notdef
455
456int
457ip_dooptions(m)
458 struct mbuf *m;
459{
460 register struct ip *ip = mtod(m, struct ip *);
461 register u_char *cp;
462 register struct ip_timestamp *ipt;
463 register struct in_ifaddr *ia;
bellardf0cbd3e2004-04-22 00:10:48 +0000464 int opt, optlen, cnt, off, code, type, forward = 0;
465 struct in_addr *sin, dst;
Stefan Weilb6dce922010-07-22 22:15:23 +0200466typedef uint32_t n_time;
bellardf0cbd3e2004-04-22 00:10:48 +0000467 n_time ntime;
468
469 dst = ip->ip_dst;
470 cp = (u_char *)(ip + 1);
471 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
472 for (; cnt > 0; cnt -= optlen, cp += optlen) {
473 opt = cp[IPOPT_OPTVAL];
474 if (opt == IPOPT_EOL)
475 break;
476 if (opt == IPOPT_NOP)
477 optlen = 1;
478 else {
479 optlen = cp[IPOPT_OLEN];
480 if (optlen <= 0 || optlen > cnt) {
481 code = &cp[IPOPT_OLEN] - (u_char *)ip;
482 goto bad;
483 }
484 }
485 switch (opt) {
486
487 default:
488 break;
489
490 /*
491 * Source routing with record.
492 * Find interface with current destination address.
493 * If none on this machine then drop if strictly routed,
494 * or do nothing if loosely routed.
495 * Record interface address and bring up next address
496 * component. If strictly routed make sure next
497 * address is on directly accessible net.
498 */
499 case IPOPT_LSRR:
500 case IPOPT_SSRR:
501 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
502 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
503 goto bad;
504 }
505 ipaddr.sin_addr = ip->ip_dst;
506 ia = (struct in_ifaddr *)
507 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
508 if (ia == 0) {
509 if (opt == IPOPT_SSRR) {
510 type = ICMP_UNREACH;
511 code = ICMP_UNREACH_SRCFAIL;
512 goto bad;
513 }
514 /*
515 * Loose routing, and not at next destination
516 * yet; nothing to do except forward.
517 */
518 break;
519 }
Stefan Weilcf2846b2011-07-21 21:46:45 +0200520 off--; /* 0 origin */
bellardf0cbd3e2004-04-22 00:10:48 +0000521 if (off > optlen - sizeof(struct in_addr)) {
522 /*
523 * End of source route. Should be for us.
524 */
525 save_rte(cp, ip->ip_src);
526 break;
527 }
528 /*
529 * locate outgoing interface
530 */
531 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
532 sizeof(ipaddr.sin_addr));
533 if (opt == IPOPT_SSRR) {
534#define INA struct in_ifaddr *
535#define SA struct sockaddr *
536 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
537 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
538 } else
539 ia = ip_rtaddr(ipaddr.sin_addr);
540 if (ia == 0) {
541 type = ICMP_UNREACH;
542 code = ICMP_UNREACH_SRCFAIL;
543 goto bad;
544 }
545 ip->ip_dst = ipaddr.sin_addr;
546 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
547 (caddr_t)(cp + off), sizeof(struct in_addr));
548 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
549 /*
550 * Let ip_intr's mcast routing check handle mcast pkts
551 */
552 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
553 break;
554
555 case IPOPT_RR:
556 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
557 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
558 goto bad;
559 }
560 /*
561 * If no space remains, ignore.
562 */
Stefan Weilcf2846b2011-07-21 21:46:45 +0200563 off--; /* 0 origin */
bellardf0cbd3e2004-04-22 00:10:48 +0000564 if (off > optlen - sizeof(struct in_addr))
565 break;
566 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
567 sizeof(ipaddr.sin_addr));
568 /*
569 * locate outgoing interface; if we're the destination,
570 * use the incoming interface (should be same).
571 */
572 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
573 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
574 type = ICMP_UNREACH;
575 code = ICMP_UNREACH_HOST;
576 goto bad;
577 }
578 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
579 (caddr_t)(cp + off), sizeof(struct in_addr));
580 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
581 break;
582
583 case IPOPT_TS:
584 code = cp - (u_char *)ip;
585 ipt = (struct ip_timestamp *)cp;
586 if (ipt->ipt_len < 5)
587 goto bad;
588 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
589 if (++ipt->ipt_oflw == 0)
590 goto bad;
591 break;
592 }
593 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
594 switch (ipt->ipt_flg) {
595
596 case IPOPT_TS_TSONLY:
597 break;
598
599 case IPOPT_TS_TSANDADDR:
600 if (ipt->ipt_ptr + sizeof(n_time) +
601 sizeof(struct in_addr) > ipt->ipt_len)
602 goto bad;
603 ipaddr.sin_addr = dst;
604 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
605 m->m_pkthdr.rcvif);
606 if (ia == 0)
607 continue;
608 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
609 (caddr_t)sin, sizeof(struct in_addr));
610 ipt->ipt_ptr += sizeof(struct in_addr);
611 break;
612
613 case IPOPT_TS_PRESPEC:
614 if (ipt->ipt_ptr + sizeof(n_time) +
615 sizeof(struct in_addr) > ipt->ipt_len)
616 goto bad;
617 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
618 sizeof(struct in_addr));
619 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
620 continue;
621 ipt->ipt_ptr += sizeof(struct in_addr);
622 break;
623
624 default:
625 goto bad;
626 }
627 ntime = iptime();
628 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
629 sizeof(n_time));
630 ipt->ipt_ptr += sizeof(n_time);
631 }
632 }
633 if (forward) {
634 ip_forward(m, 1);
635 return (1);
636 }
bellardf0cbd3e2004-04-22 00:10:48 +0000637 return (0);
638bad:
bellardf0cbd3e2004-04-22 00:10:48 +0000639 icmp_error(m, type, code, 0, 0);
640
bellardf0cbd3e2004-04-22 00:10:48 +0000641 return (1);
642}
643
644#endif /* notdef */
645
646/*
647 * Strip out IP options, at higher
648 * level protocol in the kernel.
649 * Second argument is buffer to which options
650 * will be moved, and return value is their length.
651 * (XXX) should be deleted; last arg currently ignored.
652 */
653void
blueswir1511d2b12009-03-07 15:32:56 +0000654ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
bellardf0cbd3e2004-04-22 00:10:48 +0000655{
656 register int i;
657 struct ip *ip = mtod(m, struct ip *);
658 register caddr_t opts;
659 int olen;
660
661 olen = (ip->ip_hl<<2) - sizeof (struct ip);
662 opts = (caddr_t)(ip + 1);
663 i = m->m_len - (sizeof (struct ip) + olen);
664 memcpy(opts, opts + olen, (unsigned)i);
665 m->m_len -= olen;
ths5fafdf22007-09-16 21:08:06 +0000666
bellardf0cbd3e2004-04-22 00:10:48 +0000667 ip->ip_hl = sizeof(struct ip) >> 2;
668}