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