]>
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.
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
);
55 * IP initialization: fill in IP protocol switch table.
56 * All protocols not implemented in kernel go to raw IP protocol handler.
61 ipq
.ip_link
.next
= ipq
.ip_link
.prev
= &ipq
.ip_link
;
62 ip_id
= tt
.tv_sec
& 0xffff;
68 * Ip input routine. Checksum and byte swap header. If fragmented
69 * try to reassemble. Process options. Pass to next level.
72 ip_input(struct mbuf
*m
)
74 register struct ip
*ip
;
77 DEBUG_CALL("ip_input");
78 DEBUG_ARG("m = %lx", (long)m
);
79 DEBUG_ARG("m_len = %d", m
->m_len
);
81 if (m
->m_len
< sizeof (struct ip
)) {
85 ip
= mtod(m
, struct ip
*);
87 if (ip
->ip_v
!= IPVERSION
) {
91 hlen
= ip
->ip_hl
<< 2;
92 if (hlen
<sizeof(struct ip
) || hlen
>m
->m_len
) {/* min header length */
93 goto bad
; /* or packet too short */
96 /* keep ip header intact for ICMP reply
97 * ip->ip_sum = cksum(m, hlen);
105 * Convert fields to host representation.
108 if (ip
->ip_len
< hlen
) {
115 * Check that the amount of data in the buffers
116 * is as at least much as the IP header would have us expect.
117 * Trim mbufs if longer than we expect.
118 * Drop packet if shorter than we expect.
120 if (m
->m_len
< ip
->ip_len
) {
124 if (slirp_restrict
) {
125 if ((ip
->ip_dst
.s_addr
& vnetwork_mask
.s_addr
) ==
126 vnetwork_addr
.s_addr
) {
127 if (ip
->ip_dst
.s_addr
== 0xffffffff && ip
->ip_p
!= IPPROTO_UDP
)
130 struct ex_list
*ex_ptr
;
132 if ((ip
->ip_dst
.s_addr
& ~vnetwork_mask
.s_addr
) ==
133 ~vnetwork_mask
.s_addr
)
136 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
)
137 if (ex_ptr
->ex_addr
.s_addr
== ip
->ip_dst
.s_addr
)
145 /* Should drop packet if mbuf too long? hmmm... */
146 if (m
->m_len
> ip
->ip_len
)
147 m_adj(m
, ip
->ip_len
- m
->m_len
);
149 /* check ip_ttl for a correct ICMP reply */
150 if(ip
->ip_ttl
==0 || ip
->ip_ttl
==1) {
151 icmp_error(m
, ICMP_TIMXCEED
,ICMP_TIMXCEED_INTRANS
, 0,"ttl");
156 * If offset or IP_MF are set, must reassemble.
157 * Otherwise, nothing need be done.
158 * (We could look in the reassembly queue to see
159 * if the packet was previously fragmented,
160 * but it's not worth the time; just let them time out.)
162 * XXX This should fail, don't fragment yet
164 if (ip
->ip_off
&~ IP_DF
) {
165 register struct ipq
*fp
;
168 * Look for queue of fragments
171 for (l
= ipq
.ip_link
.next
; l
!= &ipq
.ip_link
; l
= l
->next
) {
172 fp
= container_of(l
, struct ipq
, ip_link
);
173 if (ip
->ip_id
== fp
->ipq_id
&&
174 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
175 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
176 ip
->ip_p
== fp
->ipq_p
)
183 * Adjust ip_len to not reflect header,
184 * set ip_mff if more fragments are expected,
185 * convert offset of this to bytes.
188 if (ip
->ip_off
& IP_MF
)
196 * If datagram marked as having more fragments
197 * or if this is not the first fragment,
198 * attempt reassembly; if it succeeds, proceed.
200 if (ip
->ip_tos
& 1 || ip
->ip_off
) {
201 ip
= ip_reass(ip
, fp
);
213 * Switch out to protocol's input routine.
217 tcp_input(m
, hlen
, (struct socket
*)NULL
);
234 #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
235 #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
237 * Take incoming datagram fragment and try to
238 * reassemble it into whole datagram. If a chain for
239 * reassembly of this datagram already exists, then it
240 * is given as fp; otherwise have to make a chain.
243 ip_reass(register struct ip
*ip
, register struct ipq
*fp
)
245 register struct mbuf
*m
= dtom(ip
);
246 register struct ipasfrag
*q
;
247 int hlen
= ip
->ip_hl
<< 2;
250 DEBUG_CALL("ip_reass");
251 DEBUG_ARG("ip = %lx", (long)ip
);
252 DEBUG_ARG("fp = %lx", (long)fp
);
253 DEBUG_ARG("m = %lx", (long)m
);
256 * Presence of header sizes in mbufs
257 * would confuse code below.
258 * Fragment m_data is concatenated.
264 * If first fragment to arrive, create a reassembly queue.
268 if ((t
= m_get()) == NULL
) goto dropfrag
;
269 fp
= mtod(t
, struct ipq
*);
270 insque(&fp
->ip_link
, &ipq
.ip_link
);
271 fp
->ipq_ttl
= IPFRAGTTL
;
272 fp
->ipq_p
= ip
->ip_p
;
273 fp
->ipq_id
= ip
->ip_id
;
274 fp
->frag_link
.next
= fp
->frag_link
.prev
= &fp
->frag_link
;
275 fp
->ipq_src
= ip
->ip_src
;
276 fp
->ipq_dst
= ip
->ip_dst
;
277 q
= (struct ipasfrag
*)fp
;
282 * Find a segment which begins after this one does.
284 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
;
286 if (q
->ipf_off
> ip
->ip_off
)
290 * If there is a preceding segment, it may provide some of
291 * our data already. If so, drop the data from the incoming
292 * segment. If it provides all of our data, drop us.
294 if (q
->ipf_prev
!= &fp
->frag_link
) {
295 struct ipasfrag
*pq
= q
->ipf_prev
;
296 i
= pq
->ipf_off
+ pq
->ipf_len
- ip
->ip_off
;
307 * While we overlap succeeding segments trim them or,
308 * if they are completely covered, dequeue them.
310 while (q
!= (struct ipasfrag
*)&fp
->frag_link
&&
311 ip
->ip_off
+ ip
->ip_len
> q
->ipf_off
) {
312 i
= (ip
->ip_off
+ ip
->ip_len
) - q
->ipf_off
;
313 if (i
< q
->ipf_len
) {
320 m_freem(dtom(q
->ipf_prev
));
326 * Stick new segment in its place;
327 * check for complete reassembly.
329 ip_enq(iptofrag(ip
), q
->ipf_prev
);
331 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
;
333 if (q
->ipf_off
!= next
)
337 if (((struct ipasfrag
*)(q
->ipf_prev
))->ipf_tos
& 1)
341 * Reassembly is complete; concatenate fragments.
343 q
= fp
->frag_link
.next
;
346 q
= (struct ipasfrag
*) q
->ipf_next
;
347 while (q
!= (struct ipasfrag
*)&fp
->frag_link
) {
348 struct mbuf
*t
= dtom(q
);
349 q
= (struct ipasfrag
*) q
->ipf_next
;
354 * Create header for new ip packet by
355 * modifying header of first packet;
356 * dequeue and discard fragment reassembly header.
357 * Make header visible.
359 q
= fp
->frag_link
.next
;
362 * If the fragments concatenated to an mbuf that's
363 * bigger than the total size of the fragment, then and
364 * m_ext buffer was alloced. But fp->ipq_next points to
365 * the old buffer (in the mbuf), so we must point ip
366 * into the new buffer.
368 if (m
->m_flags
& M_EXT
) {
369 int delta
= (char *)q
- m
->m_dat
;
370 q
= (struct ipasfrag
*)(m
->m_ext
+ delta
);
376 ip
->ip_src
= fp
->ipq_src
;
377 ip
->ip_dst
= fp
->ipq_dst
;
378 remque(&fp
->ip_link
);
379 (void) m_free(dtom(fp
));
380 m
->m_len
+= (ip
->ip_hl
<< 2);
381 m
->m_data
-= (ip
->ip_hl
<< 2);
391 * Free a fragment reassembly header and all
392 * associated datagrams.
395 ip_freef(struct ipq
*fp
)
397 register struct ipasfrag
*q
, *p
;
399 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
; q
= p
) {
404 remque(&fp
->ip_link
);
405 (void) m_free(dtom(fp
));
409 * Put an ip fragment on a reassembly chain.
410 * Like insque, but pointers in middle of structure.
413 ip_enq(register struct ipasfrag
*p
, register struct ipasfrag
*prev
)
415 DEBUG_CALL("ip_enq");
416 DEBUG_ARG("prev = %lx", (long)prev
);
418 p
->ipf_next
= prev
->ipf_next
;
419 ((struct ipasfrag
*)(prev
->ipf_next
))->ipf_prev
= p
;
424 * To ip_enq as remque is to insque.
427 ip_deq(register struct ipasfrag
*p
)
429 ((struct ipasfrag
*)(p
->ipf_prev
))->ipf_next
= p
->ipf_next
;
430 ((struct ipasfrag
*)(p
->ipf_next
))->ipf_prev
= p
->ipf_prev
;
434 * IP timer processing;
435 * if a timer expires on a reassembly
443 DEBUG_CALL("ip_slowtimo");
445 l
= ipq
.ip_link
.next
;
450 while (l
!= &ipq
.ip_link
) {
451 struct ipq
*fp
= container_of(l
, struct ipq
, ip_link
);
453 if (--fp
->ipq_ttl
== 0) {
460 * Do option processing on a datagram,
461 * possibly discarding it if bad options are encountered,
462 * or forwarding it if source-routed.
463 * Returns 1 if packet has been forwarded/freed,
464 * 0 if the packet should be processed further.
473 register struct ip
*ip
= mtod(m
, struct ip
*);
475 register struct ip_timestamp
*ipt
;
476 register struct in_ifaddr
*ia
;
477 int opt
, optlen
, cnt
, off
, code
, type
, forward
= 0;
478 struct in_addr
*sin
, dst
;
479 typedef u_int32_t n_time
;
483 cp
= (u_char
*)(ip
+ 1);
484 cnt
= (ip
->ip_hl
<< 2) - sizeof (struct ip
);
485 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
486 opt
= cp
[IPOPT_OPTVAL
];
487 if (opt
== IPOPT_EOL
)
489 if (opt
== IPOPT_NOP
)
492 optlen
= cp
[IPOPT_OLEN
];
493 if (optlen
<= 0 || optlen
> cnt
) {
494 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
504 * Source routing with record.
505 * Find interface with current destination address.
506 * If none on this machine then drop if strictly routed,
507 * or do nothing if loosely routed.
508 * Record interface address and bring up next address
509 * component. If strictly routed make sure next
510 * address is on directly accessible net.
514 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
515 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
518 ipaddr
.sin_addr
= ip
->ip_dst
;
519 ia
= (struct in_ifaddr
*)
520 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
522 if (opt
== IPOPT_SSRR
) {
524 code
= ICMP_UNREACH_SRCFAIL
;
528 * Loose routing, and not at next destination
529 * yet; nothing to do except forward.
533 off
--; / * 0 origin
* /
534 if (off
> optlen
- sizeof(struct in_addr
)) {
536 * End of source route. Should be for us.
538 save_rte(cp
, ip
->ip_src
);
542 * locate outgoing interface
544 bcopy((caddr_t
)(cp
+ off
), (caddr_t
)&ipaddr
.sin_addr
,
545 sizeof(ipaddr
.sin_addr
));
546 if (opt
== IPOPT_SSRR
) {
547 #define INA struct in_ifaddr *
548 #define SA struct sockaddr *
549 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0)
550 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
552 ia
= ip_rtaddr(ipaddr
.sin_addr
);
555 code
= ICMP_UNREACH_SRCFAIL
;
558 ip
->ip_dst
= ipaddr
.sin_addr
;
559 bcopy((caddr_t
)&(IA_SIN(ia
)->sin_addr
),
560 (caddr_t
)(cp
+ off
), sizeof(struct in_addr
));
561 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
563 * Let ip_intr's mcast routing check handle mcast pkts
565 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
569 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
570 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
574 * If no space remains, ignore.
577 if (off
> optlen
- sizeof(struct in_addr
))
579 bcopy((caddr_t
)(&ip
->ip_dst
), (caddr_t
)&ipaddr
.sin_addr
,
580 sizeof(ipaddr
.sin_addr
));
582 * locate outgoing interface; if we're the destination,
583 * use the incoming interface (should be same).
585 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0 &&
586 (ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
588 code
= ICMP_UNREACH_HOST
;
591 bcopy((caddr_t
)&(IA_SIN(ia
)->sin_addr
),
592 (caddr_t
)(cp
+ off
), sizeof(struct in_addr
));
593 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
597 code
= cp
- (u_char
*)ip
;
598 ipt
= (struct ip_timestamp
*)cp
;
599 if (ipt
->ipt_len
< 5)
601 if (ipt
->ipt_ptr
> ipt
->ipt_len
- sizeof (int32_t)) {
602 if (++ipt
->ipt_oflw
== 0)
606 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
607 switch (ipt
->ipt_flg
) {
609 case IPOPT_TS_TSONLY
:
612 case IPOPT_TS_TSANDADDR
:
613 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
614 sizeof(struct in_addr
) > ipt
->ipt_len
)
616 ipaddr
.sin_addr
= dst
;
617 ia
= (INA
)ifaof_ i f p
foraddr((SA
)&ipaddr
,
621 bcopy((caddr_t
)&IA_SIN(ia
)->sin_addr
,
622 (caddr_t
)sin
, sizeof(struct in_addr
));
623 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
626 case IPOPT_TS_PRESPEC
:
627 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
628 sizeof(struct in_addr
) > ipt
->ipt_len
)
630 bcopy((caddr_t
)sin
, (caddr_t
)&ipaddr
.sin_addr
,
631 sizeof(struct in_addr
));
632 if (ifa_ifwithaddr((SA
)&ipaddr
) == 0)
634 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
641 bcopy((caddr_t
)&ntime
, (caddr_t
)cp
+ ipt
->ipt_ptr
- 1,
643 ipt
->ipt_ptr
+= sizeof(n_time
);
654 icmp_error(m
, type
, code
, 0, 0);
662 * Strip out IP options, at higher
663 * level protocol in the kernel.
664 * Second argument is buffer to which options
665 * will be moved, and return value is their length.
666 * (XXX) should be deleted; last arg currently ignored.
669 ip_stripoptions(register struct mbuf
*m
, struct mbuf
*mopt
)
672 struct ip
*ip
= mtod(m
, struct ip
*);
673 register caddr_t opts
;
676 olen
= (ip
->ip_hl
<<2) - sizeof (struct ip
);
677 opts
= (caddr_t
)(ip
+ 1);
678 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
679 memcpy(opts
, opts
+ olen
, (unsigned)i
);
682 ip
->ip_hl
= sizeof(struct ip
) >> 2;