1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41#include "qemu/osdep.h"
42#include <slirp.h>
43#include <qemu/osdep.h>
44#include "ip_icmp.h"
45
46static struct ip *ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp);
47static void ip_freef(Slirp *slirp, struct ipq *fp);
48static void ip_enq(register struct ipasfrag *p,
49 register struct ipasfrag *prev);
50static void ip_deq(register struct ipasfrag *p);
51
52
53
54
55
56void
57ip_init(Slirp *slirp)
58{
59 slirp->ipq.ip_link.next = slirp->ipq.ip_link.prev = &slirp->ipq.ip_link;
60 udp_init(slirp);
61 tcp_init(slirp);
62 icmp_init(slirp);
63}
64
65void ip_cleanup(Slirp *slirp)
66{
67 udp_cleanup(slirp);
68 tcp_cleanup(slirp);
69 icmp_cleanup(slirp);
70}
71
72
73
74
75
76void
77ip_input(struct mbuf *m)
78{
79 Slirp *slirp = m->slirp;
80 register struct ip *ip;
81 int hlen;
82
83 if (!slirp->in_enabled) {
84 goto bad;
85 }
86
87 DEBUG_CALL("ip_input");
88 DEBUG_ARG("m = %p", m);
89 DEBUG_ARG("m_len = %d", m->m_len);
90
91 if (m->m_len < sizeof (struct ip)) {
92 goto bad;
93 }
94
95 ip = mtod(m, struct ip *);
96
97 if (ip->ip_v != IPVERSION) {
98 goto bad;
99 }
100
101 hlen = ip->ip_hl << 2;
102 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {
103 goto bad;
104 }
105
106
107
108
109
110 if(cksum(m,hlen)) {
111 goto bad;
112 }
113
114
115
116
117 NTOHS(ip->ip_len);
118 if (ip->ip_len < hlen) {
119 goto bad;
120 }
121 NTOHS(ip->ip_id);
122 NTOHS(ip->ip_off);
123
124
125
126
127
128
129
130 if (m->m_len < ip->ip_len) {
131 goto bad;
132 }
133
134
135 if (m->m_len > ip->ip_len)
136 m_adj(m, ip->ip_len - m->m_len);
137
138
139 if (ip->ip_ttl == 0) {
140 icmp_send_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, "ttl");
141 goto bad;
142 }
143
144
145
146
147
148
149
150
151
152
153 if (ip->ip_off &~ IP_DF) {
154 register struct ipq *fp;
155 struct qlink *l;
156
157
158
159
160 for (l = slirp->ipq.ip_link.next; l != &slirp->ipq.ip_link;
161 l = l->next) {
162 fp = container_of(l, struct ipq, ip_link);
163 if (ip->ip_id == fp->ipq_id &&
164 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
165 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
166 ip->ip_p == fp->ipq_p)
167 goto found;
168 }
169 fp = NULL;
170 found:
171
172
173
174
175
176
177 ip->ip_len -= hlen;
178 if (ip->ip_off & IP_MF)
179 ip->ip_tos |= 1;
180 else
181 ip->ip_tos &= ~1;
182
183 ip->ip_off <<= 3;
184
185
186
187
188
189
190 if (ip->ip_tos & 1 || ip->ip_off) {
191 ip = ip_reass(slirp, ip, fp);
192 if (ip == NULL)
193 return;
194 m = dtom(slirp, ip);
195 } else
196 if (fp)
197 ip_freef(slirp, fp);
198
199 } else
200 ip->ip_len -= hlen;
201
202
203
204
205 switch (ip->ip_p) {
206 case IPPROTO_TCP:
207 tcp_input(m, hlen, (struct socket *)NULL, AF_INET);
208 break;
209 case IPPROTO_UDP:
210 udp_input(m, hlen);
211 break;
212 case IPPROTO_ICMP:
213 icmp_input(m, hlen);
214 break;
215 default:
216 m_free(m);
217 }
218 return;
219bad:
220 m_free(m);
221}
222
223#define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
224#define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
225
226
227
228
229
230
231static struct ip *
232ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp)
233{
234 register struct mbuf *m = dtom(slirp, ip);
235 register struct ipasfrag *q;
236 int hlen = ip->ip_hl << 2;
237 int i, next;
238
239 DEBUG_CALL("ip_reass");
240 DEBUG_ARG("ip = %p", ip);
241 DEBUG_ARG("fp = %p", fp);
242 DEBUG_ARG("m = %p", m);
243
244
245
246
247
248
249 m->m_data += hlen;
250 m->m_len -= hlen;
251
252
253
254
255 if (fp == NULL) {
256 struct mbuf *t = m_get(slirp);
257
258 if (t == NULL) {
259 goto dropfrag;
260 }
261 fp = mtod(t, struct ipq *);
262 insque(&fp->ip_link, &slirp->ipq.ip_link);
263 fp->ipq_ttl = IPFRAGTTL;
264 fp->ipq_p = ip->ip_p;
265 fp->ipq_id = ip->ip_id;
266 fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
267 fp->ipq_src = ip->ip_src;
268 fp->ipq_dst = ip->ip_dst;
269 q = (struct ipasfrag *)fp;
270 goto insert;
271 }
272
273
274
275
276 for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
277 q = q->ipf_next)
278 if (q->ipf_off > ip->ip_off)
279 break;
280
281
282
283
284
285
286 if (q->ipf_prev != &fp->frag_link) {
287 struct ipasfrag *pq = q->ipf_prev;
288 i = pq->ipf_off + pq->ipf_len - ip->ip_off;
289 if (i > 0) {
290 if (i >= ip->ip_len)
291 goto dropfrag;
292 m_adj(dtom(slirp, ip), i);
293 ip->ip_off += i;
294 ip->ip_len -= i;
295 }
296 }
297
298
299
300
301
302 while (q != (struct ipasfrag*)&fp->frag_link &&
303 ip->ip_off + ip->ip_len > q->ipf_off) {
304 i = (ip->ip_off + ip->ip_len) - q->ipf_off;
305 if (i < q->ipf_len) {
306 q->ipf_len -= i;
307 q->ipf_off += i;
308 m_adj(dtom(slirp, q), i);
309 break;
310 }
311 q = q->ipf_next;
312 m_free(dtom(slirp, q->ipf_prev));
313 ip_deq(q->ipf_prev);
314 }
315
316insert:
317
318
319
320
321 ip_enq(iptofrag(ip), q->ipf_prev);
322 next = 0;
323 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
324 q = q->ipf_next) {
325 if (q->ipf_off != next)
326 return NULL;
327 next += q->ipf_len;
328 }
329 if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
330 return NULL;
331
332
333
334
335 q = fp->frag_link.next;
336 m = dtom(slirp, q);
337
338 q = (struct ipasfrag *) q->ipf_next;
339 while (q != (struct ipasfrag*)&fp->frag_link) {
340 struct mbuf *t = dtom(slirp, q);
341 q = (struct ipasfrag *) q->ipf_next;
342 m_cat(m, t);
343 }
344
345
346
347
348
349
350
351 q = fp->frag_link.next;
352
353
354
355
356
357
358
359
360 if (m->m_flags & M_EXT) {
361 int delta = (char *)q - m->m_dat;
362 q = (struct ipasfrag *)(m->m_ext + delta);
363 }
364
365 ip = fragtoip(q);
366 ip->ip_len = next;
367 ip->ip_tos &= ~1;
368 ip->ip_src = fp->ipq_src;
369 ip->ip_dst = fp->ipq_dst;
370 remque(&fp->ip_link);
371 (void) m_free(dtom(slirp, fp));
372 m->m_len += (ip->ip_hl << 2);
373 m->m_data -= (ip->ip_hl << 2);
374
375 return ip;
376
377dropfrag:
378 m_free(m);
379 return NULL;
380}
381
382
383
384
385
386static void
387ip_freef(Slirp *slirp, struct ipq *fp)
388{
389 register struct ipasfrag *q, *p;
390
391 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
392 p = q->ipf_next;
393 ip_deq(q);
394 m_free(dtom(slirp, q));
395 }
396 remque(&fp->ip_link);
397 (void) m_free(dtom(slirp, fp));
398}
399
400
401
402
403
404static void
405ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
406{
407 DEBUG_CALL("ip_enq");
408 DEBUG_ARG("prev = %p", prev);
409 p->ipf_prev = prev;
410 p->ipf_next = prev->ipf_next;
411 ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
412 prev->ipf_next = p;
413}
414
415
416
417
418static void
419ip_deq(register struct ipasfrag *p)
420{
421 ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
422 ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
423}
424
425
426
427
428
429
430void
431ip_slowtimo(Slirp *slirp)
432{
433 struct qlink *l;
434
435 DEBUG_CALL("ip_slowtimo");
436
437 l = slirp->ipq.ip_link.next;
438
439 if (l == NULL)
440 return;
441
442 while (l != &slirp->ipq.ip_link) {
443 struct ipq *fp = container_of(l, struct ipq, ip_link);
444 l = l->next;
445 if (--fp->ipq_ttl == 0) {
446 ip_freef(slirp, fp);
447 }
448 }
449}
450
451
452
453
454
455
456
457
458
459#ifdef notdef
460
461int
462ip_dooptions(m)
463 struct mbuf *m;
464{
465 register struct ip *ip = mtod(m, struct ip *);
466 register u_char *cp;
467 register struct ip_timestamp *ipt;
468 register struct in_ifaddr *ia;
469 int opt, optlen, cnt, off, code, type, forward = 0;
470 struct in_addr *sin, dst;
471typedef uint32_t n_time;
472 n_time ntime;
473
474 dst = ip->ip_dst;
475 cp = (u_char *)(ip + 1);
476 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
477 for (; cnt > 0; cnt -= optlen, cp += optlen) {
478 opt = cp[IPOPT_OPTVAL];
479 if (opt == IPOPT_EOL)
480 break;
481 if (opt == IPOPT_NOP)
482 optlen = 1;
483 else {
484 optlen = cp[IPOPT_OLEN];
485 if (optlen <= 0 || optlen > cnt) {
486 code = &cp[IPOPT_OLEN] - (u_char *)ip;
487 goto bad;
488 }
489 }
490 switch (opt) {
491
492 default:
493 break;
494
495
496
497
498
499
500
501
502
503
504 case IPOPT_LSRR:
505 case IPOPT_SSRR:
506 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
507 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
508 goto bad;
509 }
510 ipaddr.sin_addr = ip->ip_dst;
511 ia = (struct in_ifaddr *)
512 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
513 if (ia == 0) {
514 if (opt == IPOPT_SSRR) {
515 type = ICMP_UNREACH;
516 code = ICMP_UNREACH_SRCFAIL;
517 goto bad;
518 }
519
520
521
522
523 break;
524 }
525 off--;
526 if (off > optlen - sizeof(struct in_addr)) {
527
528
529
530 save_rte(cp, ip->ip_src);
531 break;
532 }
533
534
535
536 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
537 sizeof(ipaddr.sin_addr));
538 if (opt == IPOPT_SSRR) {
539#define INA struct in_ifaddr *
540#define SA struct sockaddr *
541 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
542 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
543 } else
544 ia = ip_rtaddr(ipaddr.sin_addr);
545 if (ia == 0) {
546 type = ICMP_UNREACH;
547 code = ICMP_UNREACH_SRCFAIL;
548 goto bad;
549 }
550 ip->ip_dst = ipaddr.sin_addr;
551 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
552 (caddr_t)(cp + off), sizeof(struct in_addr));
553 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
554
555
556
557 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
558 break;
559
560 case IPOPT_RR:
561 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
562 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
563 goto bad;
564 }
565
566
567
568 off--;
569 if (off > optlen - sizeof(struct in_addr))
570 break;
571 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
572 sizeof(ipaddr.sin_addr));
573
574
575
576
577 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
578 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
579 type = ICMP_UNREACH;
580 code = ICMP_UNREACH_HOST;
581 goto bad;
582 }
583 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
584 (caddr_t)(cp + off), sizeof(struct in_addr));
585 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
586 break;
587
588 case IPOPT_TS:
589 code = cp - (u_char *)ip;
590 ipt = (struct ip_timestamp *)cp;
591 if (ipt->ipt_len < 5)
592 goto bad;
593 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
594 if (++ipt->ipt_oflw == 0)
595 goto bad;
596 break;
597 }
598 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
599 switch (ipt->ipt_flg) {
600
601 case IPOPT_TS_TSONLY:
602 break;
603
604 case IPOPT_TS_TSANDADDR:
605 if (ipt->ipt_ptr + sizeof(n_time) +
606 sizeof(struct in_addr) > ipt->ipt_len)
607 goto bad;
608 ipaddr.sin_addr = dst;
609 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
610 m->m_pkthdr.rcvif);
611 if (ia == 0)
612 continue;
613 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
614 (caddr_t)sin, sizeof(struct in_addr));
615 ipt->ipt_ptr += sizeof(struct in_addr);
616 break;
617
618 case IPOPT_TS_PRESPEC:
619 if (ipt->ipt_ptr + sizeof(n_time) +
620 sizeof(struct in_addr) > ipt->ipt_len)
621 goto bad;
622 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
623 sizeof(struct in_addr));
624 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
625 continue;
626 ipt->ipt_ptr += sizeof(struct in_addr);
627 break;
628
629 default:
630 goto bad;
631 }
632 ntime = iptime();
633 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
634 sizeof(n_time));
635 ipt->ipt_ptr += sizeof(n_time);
636 }
637 }
638 if (forward) {
639 ip_forward(m, 1);
640 return (1);
641 }
642 return (0);
643bad:
644 icmp_send_error(m, type, code, 0, 0);
645
646 return (1);
647}
648
649#endif
650
651
652
653
654
655
656
657
658void
659ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
660{
661 register int i;
662 struct ip *ip = mtod(m, struct ip *);
663 register caddr_t opts;
664 int olen;
665
666 olen = (ip->ip_hl<<2) - sizeof (struct ip);
667 opts = (caddr_t)(ip + 1);
668 i = m->m_len - (sizeof (struct ip) + olen);
669 memcpy(opts, opts + olen, (unsigned)i);
670 m->m_len -= olen;
671
672 ip->ip_hl = sizeof(struct ip) >> 2;
673}
674