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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
34 * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
38 * Changes and additions relating to SLiRP are
39 * Copyright (c) 1995 Danny Gasparovski.
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
55 static struct ip
*ip_reass(register struct ip
*ip
,
56 register struct ipq
*fp
);
57 static void ip_freef(struct ipq
*fp
);
58 static void ip_enq(register struct ipasfrag
*p
,
59 register struct ipasfrag
*prev
);
60 static void ip_deq(register struct ipasfrag
*p
);
63 * IP initialization: fill in IP protocol switch table.
64 * All protocols not implemented in kernel go to raw IP protocol handler.
69 ipq
.ip_link
.next
= ipq
.ip_link
.prev
= &ipq
.ip_link
;
70 ip_id
= tt
.tv_sec
& 0xffff;
76 * Ip input routine. Checksum and byte swap header. If fragmented
77 * try to reassemble. Process options. Pass to next level.
83 register struct ip
*ip
;
86 DEBUG_CALL("ip_input");
87 DEBUG_ARG("m = %lx", (long)m
);
88 DEBUG_ARG("m_len = %d", m
->m_len
);
90 STAT(ipstat
.ips_total
++);
92 if (m
->m_len
< sizeof (struct ip
)) {
93 STAT(ipstat
.ips_toosmall
++);
97 ip
= mtod(m
, struct ip
*);
99 if (ip
->ip_v
!= IPVERSION
) {
100 STAT(ipstat
.ips_badvers
++);
104 hlen
= ip
->ip_hl
<< 2;
105 if (hlen
<sizeof(struct ip
) || hlen
>m
->m_len
) {/* min header length */
106 STAT(ipstat
.ips_badhlen
++); /* or packet too short */
110 /* keep ip header intact for ICMP reply
111 * ip->ip_sum = cksum(m, hlen);
115 STAT(ipstat
.ips_badsum
++);
120 * Convert fields to host representation.
123 if (ip
->ip_len
< hlen
) {
124 STAT(ipstat
.ips_badlen
++);
131 * Check that the amount of data in the buffers
132 * is as at least much as the IP header would have us expect.
133 * Trim mbufs if longer than we expect.
134 * Drop packet if shorter than we expect.
136 if (m
->m_len
< ip
->ip_len
) {
137 STAT(ipstat
.ips_tooshort
++);
141 if (slirp_restrict
) {
142 if (memcmp(&ip
->ip_dst
.s_addr
, &special_addr
, 3)) {
143 if (ip
->ip_dst
.s_addr
== 0xffffffff && ip
->ip_p
!= IPPROTO_UDP
)
146 int host
= ntohl(ip
->ip_dst
.s_addr
) & 0xff;
147 struct ex_list
*ex_ptr
;
152 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
)
153 if (ex_ptr
->ex_addr
== host
)
161 /* Should drop packet if mbuf too long? hmmm... */
162 if (m
->m_len
> ip
->ip_len
)
163 m_adj(m
, ip
->ip_len
- m
->m_len
);
165 /* check ip_ttl for a correct ICMP reply */
166 if(ip
->ip_ttl
==0 || ip
->ip_ttl
==1) {
167 icmp_error(m
, ICMP_TIMXCEED
,ICMP_TIMXCEED_INTRANS
, 0,"ttl");
172 * Process options and, if not destined for us,
173 * ship it on. ip_dooptions returns 1 when an
174 * error was detected (causing an icmp message
175 * to be sent and the original packet to be freed).
177 /* We do no IP options */
178 /* if (hlen > sizeof (struct ip) && ip_dooptions(m))
182 * If offset or IP_MF are set, must reassemble.
183 * Otherwise, nothing need be done.
184 * (We could look in the reassembly queue to see
185 * if the packet was previously fragmented,
186 * but it's not worth the time; just let them time out.)
188 * XXX This should fail, don't fragment yet
190 if (ip
->ip_off
&~ IP_DF
) {
191 register struct ipq
*fp
;
194 * Look for queue of fragments
197 for (l
= ipq
.ip_link
.next
; l
!= &ipq
.ip_link
; l
= l
->next
) {
198 fp
= container_of(l
, struct ipq
, ip_link
);
199 if (ip
->ip_id
== fp
->ipq_id
&&
200 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
201 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
202 ip
->ip_p
== fp
->ipq_p
)
209 * Adjust ip_len to not reflect header,
210 * set ip_mff if more fragments are expected,
211 * convert offset of this to bytes.
214 if (ip
->ip_off
& IP_MF
)
222 * If datagram marked as having more fragments
223 * or if this is not the first fragment,
224 * attempt reassembly; if it succeeds, proceed.
226 if (ip
->ip_tos
& 1 || ip
->ip_off
) {
227 STAT(ipstat
.ips_fragments
++);
228 ip
= ip_reass(ip
, fp
);
231 STAT(ipstat
.ips_reassembled
++);
241 * Switch out to protocol's input routine.
243 STAT(ipstat
.ips_delivered
++);
246 tcp_input(m
, hlen
, (struct socket
*)NULL
);
255 STAT(ipstat
.ips_noproto
++);
264 #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
265 #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
267 * Take incoming datagram fragment and try to
268 * reassemble it into whole datagram. If a chain for
269 * reassembly of this datagram already exists, then it
270 * is given as fp; otherwise have to make a chain.
273 ip_reass(register struct ip
*ip
, register struct ipq
*fp
)
275 register struct mbuf
*m
= dtom(ip
);
276 register struct ipasfrag
*q
;
277 int hlen
= ip
->ip_hl
<< 2;
280 DEBUG_CALL("ip_reass");
281 DEBUG_ARG("ip = %lx", (long)ip
);
282 DEBUG_ARG("fp = %lx", (long)fp
);
283 DEBUG_ARG("m = %lx", (long)m
);
286 * Presence of header sizes in mbufs
287 * would confuse code below.
288 * Fragment m_data is concatenated.
294 * If first fragment to arrive, create a reassembly queue.
298 if ((t
= m_get()) == NULL
) goto dropfrag
;
299 fp
= mtod(t
, struct ipq
*);
300 insque(&fp
->ip_link
, &ipq
.ip_link
);
301 fp
->ipq_ttl
= IPFRAGTTL
;
302 fp
->ipq_p
= ip
->ip_p
;
303 fp
->ipq_id
= ip
->ip_id
;
304 fp
->frag_link
.next
= fp
->frag_link
.prev
= &fp
->frag_link
;
305 fp
->ipq_src
= ip
->ip_src
;
306 fp
->ipq_dst
= ip
->ip_dst
;
307 q
= (struct ipasfrag
*)fp
;
312 * Find a segment which begins after this one does.
314 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
;
316 if (q
->ipf_off
> ip
->ip_off
)
320 * If there is a preceding segment, it may provide some of
321 * our data already. If so, drop the data from the incoming
322 * segment. If it provides all of our data, drop us.
324 if (q
->ipf_prev
!= &fp
->frag_link
) {
325 struct ipasfrag
*pq
= q
->ipf_prev
;
326 i
= pq
->ipf_off
+ pq
->ipf_len
- ip
->ip_off
;
337 * While we overlap succeeding segments trim them or,
338 * if they are completely covered, dequeue them.
340 while (q
!= (struct ipasfrag
*)&fp
->frag_link
&&
341 ip
->ip_off
+ ip
->ip_len
> q
->ipf_off
) {
342 i
= (ip
->ip_off
+ ip
->ip_len
) - q
->ipf_off
;
343 if (i
< q
->ipf_len
) {
350 m_freem(dtom(q
->ipf_prev
));
356 * Stick new segment in its place;
357 * check for complete reassembly.
359 ip_enq(iptofrag(ip
), q
->ipf_prev
);
361 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
;
363 if (q
->ipf_off
!= next
)
367 if (((struct ipasfrag
*)(q
->ipf_prev
))->ipf_tos
& 1)
371 * Reassembly is complete; concatenate fragments.
373 q
= fp
->frag_link
.next
;
376 q
= (struct ipasfrag
*) q
->ipf_next
;
377 while (q
!= (struct ipasfrag
*)&fp
->frag_link
) {
378 struct mbuf
*t
= dtom(q
);
379 q
= (struct ipasfrag
*) q
->ipf_next
;
384 * Create header for new ip packet by
385 * modifying header of first packet;
386 * dequeue and discard fragment reassembly header.
387 * Make header visible.
389 q
= fp
->frag_link
.next
;
392 * If the fragments concatenated to an mbuf that's
393 * bigger than the total size of the fragment, then and
394 * m_ext buffer was alloced. But fp->ipq_next points to
395 * the old buffer (in the mbuf), so we must point ip
396 * into the new buffer.
398 if (m
->m_flags
& M_EXT
) {
400 delta
= (char *)ip
- m
->m_dat
;
401 q
= (struct ipasfrag
*)(m
->m_ext
+ delta
);
404 /* DEBUG_ARG("ip = %lx", (long)ip);
405 * ip=(struct ipasfrag *)m->m_data; */
410 ip
->ip_src
= fp
->ipq_src
;
411 ip
->ip_dst
= fp
->ipq_dst
;
412 remque(&fp
->ip_link
);
413 (void) m_free(dtom(fp
));
414 m
->m_len
+= (ip
->ip_hl
<< 2);
415 m
->m_data
-= (ip
->ip_hl
<< 2);
420 STAT(ipstat
.ips_fragdropped
++);
426 * Free a fragment reassembly header and all
427 * associated datagrams.
430 ip_freef(struct ipq
*fp
)
432 register struct ipasfrag
*q
, *p
;
434 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
; q
= p
) {
439 remque(&fp
->ip_link
);
440 (void) m_free(dtom(fp
));
444 * Put an ip fragment on a reassembly chain.
445 * Like insque, but pointers in middle of structure.
448 ip_enq(register struct ipasfrag
*p
, register struct ipasfrag
*prev
)
450 DEBUG_CALL("ip_enq");
451 DEBUG_ARG("prev = %lx", (long)prev
);
453 p
->ipf_next
= prev
->ipf_next
;
454 ((struct ipasfrag
*)(prev
->ipf_next
))->ipf_prev
= p
;
459 * To ip_enq as remque is to insque.
462 ip_deq(register struct ipasfrag
*p
)
464 ((struct ipasfrag
*)(p
->ipf_prev
))->ipf_next
= p
->ipf_next
;
465 ((struct ipasfrag
*)(p
->ipf_next
))->ipf_prev
= p
->ipf_prev
;
469 * IP timer processing;
470 * if a timer expires on a reassembly
478 DEBUG_CALL("ip_slowtimo");
480 l
= ipq
.ip_link
.next
;
485 while (l
!= &ipq
.ip_link
) {
486 struct ipq
*fp
= container_of(l
, struct ipq
, ip_link
);
488 if (--fp
->ipq_ttl
== 0) {
489 STAT(ipstat
.ips_fragtimeout
++);
496 * Do option processing on a datagram,
497 * possibly discarding it if bad options are encountered,
498 * or forwarding it if source-routed.
499 * Returns 1 if packet has been forwarded/freed,
500 * 0 if the packet should be processed further.
509 register struct ip
*ip
= mtod(m
, struct ip
*);
511 register struct ip_timestamp
*ipt
;
512 register struct in_ifaddr
*ia
;
513 /* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
514 int opt
, optlen
, cnt
, off
, code
, type
, forward
= 0;
515 struct in_addr
*sin
, dst
;
516 typedef u_int32_t n_time
;
520 cp
= (u_char
*)(ip
+ 1);
521 cnt
= (ip
->ip_hl
<< 2) - sizeof (struct ip
);
522 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
523 opt
= cp
[IPOPT_OPTVAL
];
524 if (opt
== IPOPT_EOL
)
526 if (opt
== IPOPT_NOP
)
529 optlen
= cp
[IPOPT_OLEN
];
530 if (optlen
<= 0 || optlen
> cnt
) {
531 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
541 * Source routing with record.
542 * Find interface with current destination address.
543 * If none on this machine then drop if strictly routed,
544 * or do nothing if loosely routed.
545 * Record interface address and bring up next address
546 * component. If strictly routed make sure next
547 * address is on directly accessible net.
551 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
552 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
555 ipaddr
.sin_addr
= ip
->ip_dst
;
556 ia
= (struct in_ifaddr
*)
557 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
559 if (opt
== IPOPT_SSRR
) {
561 code
= ICMP_UNREACH_SRCFAIL
;
565 * Loose routing, and not at next destination
566 * yet; nothing to do except forward.
570 off
--; / * 0 origin
* /
571 if (off
> optlen
- sizeof(struct in_addr
)) {
573 * End of source route. Should be for us.
575 save_rte(cp
, ip
->ip_src
);
579 * locate outgoing interface
581 bcopy((caddr_t
)(cp
+ off
), (caddr_t
)&ipaddr
.sin_addr
,
582 sizeof(ipaddr
.sin_addr
));
583 if (opt
== IPOPT_SSRR
) {
584 #define INA struct in_ifaddr *
585 #define SA struct sockaddr *
586 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0)
587 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
589 ia
= ip_rtaddr(ipaddr
.sin_addr
);
592 code
= ICMP_UNREACH_SRCFAIL
;
595 ip
->ip_dst
= ipaddr
.sin_addr
;
596 bcopy((caddr_t
)&(IA_SIN(ia
)->sin_addr
),
597 (caddr_t
)(cp
+ off
), sizeof(struct in_addr
));
598 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
600 * Let ip_intr's mcast routing check handle mcast pkts
602 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
606 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
607 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
611 * If no space remains, ignore.
614 if (off
> optlen
- sizeof(struct in_addr
))
616 bcopy((caddr_t
)(&ip
->ip_dst
), (caddr_t
)&ipaddr
.sin_addr
,
617 sizeof(ipaddr
.sin_addr
));
619 * locate outgoing interface; if we're the destination,
620 * use the incoming interface (should be same).
622 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0 &&
623 (ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
625 code
= ICMP_UNREACH_HOST
;
628 bcopy((caddr_t
)&(IA_SIN(ia
)->sin_addr
),
629 (caddr_t
)(cp
+ off
), sizeof(struct in_addr
));
630 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
634 code
= cp
- (u_char
*)ip
;
635 ipt
= (struct ip_timestamp
*)cp
;
636 if (ipt
->ipt_len
< 5)
638 if (ipt
->ipt_ptr
> ipt
->ipt_len
- sizeof (int32_t)) {
639 if (++ipt
->ipt_oflw
== 0)
643 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
644 switch (ipt
->ipt_flg
) {
646 case IPOPT_TS_TSONLY
:
649 case IPOPT_TS_TSANDADDR
:
650 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
651 sizeof(struct in_addr
) > ipt
->ipt_len
)
653 ipaddr
.sin_addr
= dst
;
654 ia
= (INA
)ifaof_ i f p
foraddr((SA
)&ipaddr
,
658 bcopy((caddr_t
)&IA_SIN(ia
)->sin_addr
,
659 (caddr_t
)sin
, sizeof(struct in_addr
));
660 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
663 case IPOPT_TS_PRESPEC
:
664 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
665 sizeof(struct in_addr
) > ipt
->ipt_len
)
667 bcopy((caddr_t
)sin
, (caddr_t
)&ipaddr
.sin_addr
,
668 sizeof(struct in_addr
));
669 if (ifa_ifwithaddr((SA
)&ipaddr
) == 0)
671 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
678 bcopy((caddr_t
)&ntime
, (caddr_t
)cp
+ ipt
->ipt_ptr
- 1,
680 ipt
->ipt_ptr
+= sizeof(n_time
);
691 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
694 icmp_error(m
, type
, code
, 0, 0);
696 STAT(ipstat
.ips_badoptions
++);
703 * Strip out IP options, at higher
704 * level protocol in the kernel.
705 * Second argument is buffer to which options
706 * will be moved, and return value is their length.
707 * (XXX) should be deleted; last arg currently ignored.
710 ip_stripoptions(m
, mopt
)
711 register struct mbuf
*m
;
715 struct ip
*ip
= mtod(m
, struct ip
*);
716 register caddr_t opts
;
719 olen
= (ip
->ip_hl
<<2) - sizeof (struct ip
);
720 opts
= (caddr_t
)(ip
+ 1);
721 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
722 memcpy(opts
, opts
+ olen
, (unsigned)i
);
725 ip
->ip_hl
= sizeof(struct ip
) >> 2;