]>
git.proxmox.com Git - qemu.git/blob - slirp/ip_input.c
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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
34 * Changes and additions relating to SLiRP are
35 * Copyright (c) 1995 Danny Gasparovski.
37 * Please read the file COPYRIGHT for the
38 * terms and conditions of the copyright.
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
);
52 * IP initialization: fill in IP protocol switch table.
53 * All protocols not implemented in kernel go to raw IP protocol handler.
58 slirp
->ipq
.ip_link
.next
= slirp
->ipq
.ip_link
.prev
= &slirp
->ipq
.ip_link
;
64 * Ip input routine. Checksum and byte swap header. If fragmented
65 * try to reassemble. Process options. Pass to next level.
68 ip_input(struct mbuf
*m
)
70 Slirp
*slirp
= m
->slirp
;
71 register struct ip
*ip
;
74 DEBUG_CALL("ip_input");
75 DEBUG_ARG("m = %lx", (long)m
);
76 DEBUG_ARG("m_len = %d", m
->m_len
);
78 if (m
->m_len
< sizeof (struct ip
)) {
82 ip
= mtod(m
, struct ip
*);
84 if (ip
->ip_v
!= IPVERSION
) {
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 */
93 /* keep ip header intact for ICMP reply
94 * ip->ip_sum = cksum(m, hlen);
102 * Convert fields to host representation.
105 if (ip
->ip_len
< hlen
) {
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.
117 if (m
->m_len
< ip
->ip_len
) {
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
);
125 /* check ip_ttl for a correct ICMP reply */
127 icmp_error(m
, ICMP_TIMXCEED
,ICMP_TIMXCEED_INTRANS
, 0,"ttl");
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.)
138 * XXX This should fail, don't fragment yet
140 if (ip
->ip_off
&~ IP_DF
) {
141 register struct ipq
*fp
;
144 * Look for queue of fragments
147 for (l
= slirp
->ipq
.ip_link
.next
; l
!= &slirp
->ipq
.ip_link
;
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
)
160 * Adjust ip_len to not reflect header,
161 * set ip_mff if more fragments are expected,
162 * convert offset of this to bytes.
165 if (ip
->ip_off
& IP_MF
)
173 * If datagram marked as having more fragments
174 * or if this is not the first fragment,
175 * attempt reassembly; if it succeeds, proceed.
177 if (ip
->ip_tos
& 1 || ip
->ip_off
) {
178 ip
= ip_reass(slirp
, ip
, fp
);
190 * Switch out to protocol's input routine.
194 tcp_input(m
, hlen
, (struct socket
*)NULL
);
211 #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
212 #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
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.
220 ip_reass(Slirp
*slirp
, struct ip
*ip
, struct ipq
*fp
)
222 register struct mbuf
*m
= dtom(slirp
, ip
);
223 register struct ipasfrag
*q
;
224 int hlen
= ip
->ip_hl
<< 2;
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
);
233 * Presence of header sizes in mbufs
234 * would confuse code below.
235 * Fragment m_data is concatenated.
241 * If first fragment to arrive, create a reassembly queue.
244 struct mbuf
*t
= m_get(slirp
);
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
;
262 * Find a segment which begins after this one does.
264 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
;
266 if (q
->ipf_off
> ip
->ip_off
)
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.
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
;
280 m_adj(dtom(slirp
, ip
), i
);
287 * While we overlap succeeding segments trim them or,
288 * if they are completely covered, dequeue them.
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
) {
296 m_adj(dtom(slirp
, q
), i
);
300 m_freem(dtom(slirp
, q
->ipf_prev
));
306 * Stick new segment in its place;
307 * check for complete reassembly.
309 ip_enq(iptofrag(ip
), q
->ipf_prev
);
311 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
;
313 if (q
->ipf_off
!= next
)
317 if (((struct ipasfrag
*)(q
->ipf_prev
))->ipf_tos
& 1)
321 * Reassembly is complete; concatenate fragments.
323 q
= fp
->frag_link
.next
;
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
;
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.
339 q
= fp
->frag_link
.next
;
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.
348 if (m
->m_flags
& M_EXT
) {
349 int delta
= (char *)q
- m
->m_dat
;
350 q
= (struct ipasfrag
*)(m
->m_ext
+ delta
);
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);
371 * Free a fragment reassembly header and all
372 * associated datagrams.
375 ip_freef(Slirp
*slirp
, struct ipq
*fp
)
377 register struct ipasfrag
*q
, *p
;
379 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
; q
= p
) {
382 m_freem(dtom(slirp
, q
));
384 remque(&fp
->ip_link
);
385 (void) m_free(dtom(slirp
, fp
));
389 * Put an ip fragment on a reassembly chain.
390 * Like insque, but pointers in middle of structure.
393 ip_enq(register struct ipasfrag
*p
, register struct ipasfrag
*prev
)
395 DEBUG_CALL("ip_enq");
396 DEBUG_ARG("prev = %lx", (long)prev
);
398 p
->ipf_next
= prev
->ipf_next
;
399 ((struct ipasfrag
*)(prev
->ipf_next
))->ipf_prev
= p
;
404 * To ip_enq as remque is to insque.
407 ip_deq(register struct ipasfrag
*p
)
409 ((struct ipasfrag
*)(p
->ipf_prev
))->ipf_next
= p
->ipf_next
;
410 ((struct ipasfrag
*)(p
->ipf_next
))->ipf_prev
= p
->ipf_prev
;
414 * IP timer processing;
415 * if a timer expires on a reassembly
419 ip_slowtimo(Slirp
*slirp
)
423 DEBUG_CALL("ip_slowtimo");
425 l
= slirp
->ipq
.ip_link
.next
;
430 while (l
!= &slirp
->ipq
.ip_link
) {
431 struct ipq
*fp
= container_of(l
, struct ipq
, ip_link
);
433 if (--fp
->ipq_ttl
== 0) {
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.
453 register struct ip
*ip
= mtod(m
, struct ip
*);
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
;
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
)
469 if (opt
== IPOPT_NOP
)
472 optlen
= cp
[IPOPT_OLEN
];
473 if (optlen
<= 0 || optlen
> cnt
) {
474 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
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.
494 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
495 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
498 ipaddr
.sin_addr
= ip
->ip_dst
;
499 ia
= (struct in_ifaddr
*)
500 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
502 if (opt
== IPOPT_SSRR
) {
504 code
= ICMP_UNREACH_SRCFAIL
;
508 * Loose routing, and not at next destination
509 * yet; nothing to do except forward.
513 off
--; / * 0 origin
* /
514 if (off
> optlen
- sizeof(struct in_addr
)) {
516 * End of source route. Should be for us.
518 save_rte(cp
, ip
->ip_src
);
522 * locate outgoing interface
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
);
532 ia
= ip_rtaddr(ipaddr
.sin_addr
);
535 code
= ICMP_UNREACH_SRCFAIL
;
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
);
543 * Let ip_intr's mcast routing check handle mcast pkts
545 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
549 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
550 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
554 * If no space remains, ignore.
557 if (off
> optlen
- sizeof(struct in_addr
))
559 bcopy((caddr_t
)(&ip
->ip_dst
), (caddr_t
)&ipaddr
.sin_addr
,
560 sizeof(ipaddr
.sin_addr
));
562 * locate outgoing interface; if we're the destination,
563 * use the incoming interface (should be same).
565 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0 &&
566 (ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
568 code
= ICMP_UNREACH_HOST
;
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
);
577 code
= cp
- (u_char
*)ip
;
578 ipt
= (struct ip_timestamp
*)cp
;
579 if (ipt
->ipt_len
< 5)
581 if (ipt
->ipt_ptr
> ipt
->ipt_len
- sizeof (int32_t)) {
582 if (++ipt
->ipt_oflw
== 0)
586 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
587 switch (ipt
->ipt_flg
) {
589 case IPOPT_TS_TSONLY
:
592 case IPOPT_TS_TSANDADDR
:
593 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
594 sizeof(struct in_addr
) > ipt
->ipt_len
)
596 ipaddr
.sin_addr
= dst
;
597 ia
= (INA
)ifaof_ i f p
foraddr((SA
)&ipaddr
,
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
);
606 case IPOPT_TS_PRESPEC
:
607 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
608 sizeof(struct in_addr
) > ipt
->ipt_len
)
610 bcopy((caddr_t
)sin
, (caddr_t
)&ipaddr
.sin_addr
,
611 sizeof(struct in_addr
));
612 if (ifa_ifwithaddr((SA
)&ipaddr
) == 0)
614 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
621 bcopy((caddr_t
)&ntime
, (caddr_t
)cp
+ ipt
->ipt_ptr
- 1,
623 ipt
->ipt_ptr
+= sizeof(n_time
);
632 icmp_error(m
, type
, code
, 0, 0);
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.
647 ip_stripoptions(register struct mbuf
*m
, struct mbuf
*mopt
)
650 struct ip
*ip
= mtod(m
, struct ip
*);
651 register caddr_t opts
;
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
);
660 ip
->ip_hl
= sizeof(struct ip
) >> 2;