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