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.
54 static struct ip
*ip_reass(register struct ipasfrag
*ip
,
55 register struct ipq
*fp
);
56 static void ip_freef(struct ipq
*fp
);
57 static void ip_enq(register struct ipasfrag
*p
,
58 register struct ipasfrag
*prev
);
59 static void ip_deq(register struct ipasfrag
*p
);
62 * IP initialization: fill in IP protocol switch table.
63 * All protocols not implemented in kernel go to raw IP protocol handler.
68 ipq
.next
= ipq
.prev
= (ipqp_32
)&ipq
;
69 ip_id
= tt
.tv_sec
& 0xffff;
75 * Ip input routine. Checksum and byte swap header. If fragmented
76 * try to reassemble. Process options. Pass to next level.
82 register struct ip
*ip
;
85 DEBUG_CALL("ip_input");
86 DEBUG_ARG("m = %lx", (long)m
);
87 DEBUG_ARG("m_len = %d", m
->m_len
);
89 STAT(ipstat
.ips_total
++);
91 if (m
->m_len
< sizeof (struct ip
)) {
92 STAT(ipstat
.ips_toosmall
++);
96 ip
= mtod(m
, struct ip
*);
98 if (ip
->ip_v
!= IPVERSION
) {
99 STAT(ipstat
.ips_badvers
++);
103 hlen
= ip
->ip_hl
<< 2;
104 if (hlen
<sizeof(struct ip
) || hlen
>m
->m_len
) {/* min header length */
105 STAT(ipstat
.ips_badhlen
++); /* or packet too short */
109 /* keep ip header intact for ICMP reply
110 * ip->ip_sum = cksum(m, hlen);
114 STAT(ipstat
.ips_badsum
++);
119 * Convert fields to host representation.
122 if (ip
->ip_len
< hlen
) {
123 STAT(ipstat
.ips_badlen
++);
130 * Check that the amount of data in the buffers
131 * is as at least much as the IP header would have us expect.
132 * Trim mbufs if longer than we expect.
133 * Drop packet if shorter than we expect.
135 if (m
->m_len
< ip
->ip_len
) {
136 STAT(ipstat
.ips_tooshort
++);
140 if (slirp_restrict
) {
141 if (memcmp(&ip
->ip_dst
.s_addr
, &special_addr
, 3)) {
142 if (ip
->ip_dst
.s_addr
== 0xffffffff && ip
->ip_p
!= IPPROTO_UDP
)
145 int host
= ntohl(ip
->ip_dst
.s_addr
) & 0xff;
146 struct ex_list
*ex_ptr
;
151 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
)
152 if (ex_ptr
->ex_addr
== host
)
160 /* Should drop packet if mbuf too long? hmmm... */
161 if (m
->m_len
> ip
->ip_len
)
162 m_adj(m
, ip
->ip_len
- m
->m_len
);
164 /* check ip_ttl for a correct ICMP reply */
165 if(ip
->ip_ttl
==0 || ip
->ip_ttl
==1) {
166 icmp_error(m
, ICMP_TIMXCEED
,ICMP_TIMXCEED_INTRANS
, 0,"ttl");
171 * Process options and, if not destined for us,
172 * ship it on. ip_dooptions returns 1 when an
173 * error was detected (causing an icmp message
174 * to be sent and the original packet to be freed).
176 /* We do no IP options */
177 /* if (hlen > sizeof (struct ip) && ip_dooptions(m))
181 * If offset or IP_MF are set, must reassemble.
182 * Otherwise, nothing need be done.
183 * (We could look in the reassembly queue to see
184 * if the packet was previously fragmented,
185 * but it's not worth the time; just let them time out.)
187 * XXX This should fail, don't fragment yet
189 if (ip
->ip_off
&~ IP_DF
) {
190 register struct ipq
*fp
;
192 * Look for queue of fragments
195 for (fp
= (struct ipq
*) ipq
.next
; fp
!= &ipq
;
196 fp
= (struct ipq
*) fp
->next
)
197 if (ip
->ip_id
== fp
->ipq_id
&&
198 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
199 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
200 ip
->ip_p
== fp
->ipq_p
)
206 * Adjust ip_len to not reflect header,
207 * set ip_mff if more fragments are expected,
208 * convert offset of this to bytes.
211 if (ip
->ip_off
& IP_MF
)
212 ((struct ipasfrag
*)ip
)->ipf_mff
|= 1;
214 ((struct ipasfrag
*)ip
)->ipf_mff
&= ~1;
219 * If datagram marked as having more fragments
220 * or if this is not the first fragment,
221 * attempt reassembly; if it succeeds, proceed.
223 if (((struct ipasfrag
*)ip
)->ipf_mff
& 1 || ip
->ip_off
) {
224 STAT(ipstat
.ips_fragments
++);
225 ip
= ip_reass((struct ipasfrag
*)ip
, fp
);
228 STAT(ipstat
.ips_reassembled
++);
238 * Switch out to protocol's input routine.
240 STAT(ipstat
.ips_delivered
++);
243 tcp_input(m
, hlen
, (struct socket
*)NULL
);
252 STAT(ipstat
.ips_noproto
++);
262 * Take incoming datagram fragment and try to
263 * reassemble it into whole datagram. If a chain for
264 * reassembly of this datagram already exists, then it
265 * is given as fp; otherwise have to make a chain.
268 ip_reass(register struct ipasfrag
*ip
, register struct ipq
*fp
)
270 register struct mbuf
*m
= dtom(ip
);
271 register struct ipasfrag
*q
;
272 int hlen
= ip
->ip_hl
<< 2;
275 DEBUG_CALL("ip_reass");
276 DEBUG_ARG("ip = %lx", (long)ip
);
277 DEBUG_ARG("fp = %lx", (long)fp
);
278 DEBUG_ARG("m = %lx", (long)m
);
281 * Presence of header sizes in mbufs
282 * would confuse code below.
283 * Fragment m_data is concatenated.
289 * If first fragment to arrive, create a reassembly queue.
293 if ((t
= m_get()) == NULL
) goto dropfrag
;
294 fp
= mtod(t
, struct ipq
*);
296 fp
->ipq_ttl
= IPFRAGTTL
;
297 fp
->ipq_p
= ip
->ip_p
;
298 fp
->ipq_id
= ip
->ip_id
;
299 fp
->ipq_next
= fp
->ipq_prev
= (ipasfragp_32
)fp
;
300 fp
->ipq_src
= ((struct ip
*)ip
)->ip_src
;
301 fp
->ipq_dst
= ((struct ip
*)ip
)->ip_dst
;
302 q
= (struct ipasfrag
*)fp
;
307 * Find a segment which begins after this one does.
309 for (q
= (struct ipasfrag
*)fp
->ipq_next
; q
!= (struct ipasfrag
*)fp
;
310 q
= (struct ipasfrag
*)q
->ipf_next
)
311 if (q
->ip_off
> ip
->ip_off
)
315 * If there is a preceding segment, it may provide some of
316 * our data already. If so, drop the data from the incoming
317 * segment. If it provides all of our data, drop us.
319 if (q
->ipf_prev
!= (ipasfragp_32
)fp
) {
320 i
= ((struct ipasfrag
*)(q
->ipf_prev
))->ip_off
+
321 ((struct ipasfrag
*)(q
->ipf_prev
))->ip_len
- ip
->ip_off
;
332 * While we overlap succeeding segments trim them or,
333 * if they are completely covered, dequeue them.
335 while (q
!= (struct ipasfrag
*)fp
&& ip
->ip_off
+ ip
->ip_len
> q
->ip_off
) {
336 i
= (ip
->ip_off
+ ip
->ip_len
) - q
->ip_off
;
343 q
= (struct ipasfrag
*) q
->ipf_next
;
344 m_freem(dtom((struct ipasfrag
*) q
->ipf_prev
));
345 ip_deq((struct ipasfrag
*) q
->ipf_prev
);
350 * Stick new segment in its place;
351 * check for complete reassembly.
353 ip_enq(ip
, (struct ipasfrag
*) q
->ipf_prev
);
355 for (q
= (struct ipasfrag
*) fp
->ipq_next
; q
!= (struct ipasfrag
*)fp
;
356 q
= (struct ipasfrag
*) q
->ipf_next
) {
357 if (q
->ip_off
!= next
)
361 if (((struct ipasfrag
*)(q
->ipf_prev
))->ipf_mff
& 1)
365 * Reassembly is complete; concatenate fragments.
367 q
= (struct ipasfrag
*) fp
->ipq_next
;
370 q
= (struct ipasfrag
*) q
->ipf_next
;
371 while (q
!= (struct ipasfrag
*)fp
) {
374 q
= (struct ipasfrag
*) q
->ipf_next
;
379 * Create header for new ip packet by
380 * modifying header of first packet;
381 * dequeue and discard fragment reassembly header.
382 * Make header visible.
384 ip
= (struct ipasfrag
*) fp
->ipq_next
;
387 * If the fragments concatenated to an mbuf that's
388 * bigger than the total size of the fragment, then and
389 * m_ext buffer was alloced. But fp->ipq_next points to
390 * the old buffer (in the mbuf), so we must point ip
391 * into the new buffer.
393 if (m
->m_flags
& M_EXT
) {
395 delta
= (char *)ip
- m
->m_dat
;
396 ip
= (struct ipasfrag
*)(m
->m_ext
+ delta
);
399 /* DEBUG_ARG("ip = %lx", (long)ip);
400 * ip=(struct ipasfrag *)m->m_data; */
404 ((struct ip
*)ip
)->ip_src
= fp
->ipq_src
;
405 ((struct ip
*)ip
)->ip_dst
= fp
->ipq_dst
;
407 (void) m_free(dtom(fp
));
409 m
->m_len
+= (ip
->ip_hl
<< 2);
410 m
->m_data
-= (ip
->ip_hl
<< 2);
412 return ((struct ip
*)ip
);
415 STAT(ipstat
.ips_fragdropped
++);
421 * Free a fragment reassembly header and all
422 * associated datagrams.
425 ip_freef(struct ipq
*fp
)
427 register struct ipasfrag
*q
, *p
;
429 for (q
= (struct ipasfrag
*) fp
->ipq_next
; q
!= (struct ipasfrag
*)fp
;
431 p
= (struct ipasfrag
*) q
->ipf_next
;
436 (void) m_free(dtom(fp
));
440 * Put an ip fragment on a reassembly chain.
441 * Like insque, but pointers in middle of structure.
444 ip_enq(register struct ipasfrag
*p
, register struct ipasfrag
*prev
)
446 DEBUG_CALL("ip_enq");
447 DEBUG_ARG("prev = %lx", (long)prev
);
448 p
->ipf_prev
= (ipasfragp_32
) prev
;
449 p
->ipf_next
= prev
->ipf_next
;
450 ((struct ipasfrag
*)(prev
->ipf_next
))->ipf_prev
= (ipasfragp_32
) p
;
451 prev
->ipf_next
= (ipasfragp_32
) p
;
455 * To ip_enq as remque is to insque.
458 ip_deq(register struct ipasfrag
*p
)
460 ((struct ipasfrag
*)(p
->ipf_prev
))->ipf_next
= p
->ipf_next
;
461 ((struct ipasfrag
*)(p
->ipf_next
))->ipf_prev
= p
->ipf_prev
;
465 * IP timer processing;
466 * if a timer expires on a reassembly
472 register struct ipq
*fp
;
474 DEBUG_CALL("ip_slowtimo");
476 fp
= (struct ipq
*) ipq
.next
;
482 fp
= (struct ipq
*) fp
->next
;
483 if (((struct ipq
*)(fp
->prev
))->ipq_ttl
== 0) {
484 STAT(ipstat
.ips_fragtimeout
++);
485 ip_freef((struct ipq
*) fp
->prev
);
491 * Do option processing on a datagram,
492 * possibly discarding it if bad options are encountered,
493 * or forwarding it if source-routed.
494 * Returns 1 if packet has been forwarded/freed,
495 * 0 if the packet should be processed further.
504 register struct ip
*ip
= mtod(m
, struct ip
*);
506 register struct ip_timestamp
*ipt
;
507 register struct in_ifaddr
*ia
;
508 /* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
509 int opt
, optlen
, cnt
, off
, code
, type
, forward
= 0;
510 struct in_addr
*sin
, dst
;
511 typedef u_int32_t n_time
;
515 cp
= (u_char
*)(ip
+ 1);
516 cnt
= (ip
->ip_hl
<< 2) - sizeof (struct ip
);
517 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
518 opt
= cp
[IPOPT_OPTVAL
];
519 if (opt
== IPOPT_EOL
)
521 if (opt
== IPOPT_NOP
)
524 optlen
= cp
[IPOPT_OLEN
];
525 if (optlen
<= 0 || optlen
> cnt
) {
526 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
536 * Source routing with record.
537 * Find interface with current destination address.
538 * If none on this machine then drop if strictly routed,
539 * or do nothing if loosely routed.
540 * Record interface address and bring up next address
541 * component. If strictly routed make sure next
542 * address is on directly accessible net.
546 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
547 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
550 ipaddr
.sin_addr
= ip
->ip_dst
;
551 ia
= (struct in_ifaddr
*)
552 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
554 if (opt
== IPOPT_SSRR
) {
556 code
= ICMP_UNREACH_SRCFAIL
;
560 * Loose routing, and not at next destination
561 * yet; nothing to do except forward.
565 off
--; / * 0 origin
* /
566 if (off
> optlen
- sizeof(struct in_addr
)) {
568 * End of source route. Should be for us.
570 save_rte(cp
, ip
->ip_src
);
574 * locate outgoing interface
576 bcopy((caddr_t
)(cp
+ off
), (caddr_t
)&ipaddr
.sin_addr
,
577 sizeof(ipaddr
.sin_addr
));
578 if (opt
== IPOPT_SSRR
) {
579 #define INA struct in_ifaddr *
580 #define SA struct sockaddr *
581 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0)
582 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
584 ia
= ip_rtaddr(ipaddr
.sin_addr
);
587 code
= ICMP_UNREACH_SRCFAIL
;
590 ip
->ip_dst
= ipaddr
.sin_addr
;
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
);
595 * Let ip_intr's mcast routing check handle mcast pkts
597 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
601 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
602 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
606 * If no space remains, ignore.
609 if (off
> optlen
- sizeof(struct in_addr
))
611 bcopy((caddr_t
)(&ip
->ip_dst
), (caddr_t
)&ipaddr
.sin_addr
,
612 sizeof(ipaddr
.sin_addr
));
614 * locate outgoing interface; if we're the destination,
615 * use the incoming interface (should be same).
617 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0 &&
618 (ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
620 code
= ICMP_UNREACH_HOST
;
623 bcopy((caddr_t
)&(IA_SIN(ia
)->sin_addr
),
624 (caddr_t
)(cp
+ off
), sizeof(struct in_addr
));
625 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
629 code
= cp
- (u_char
*)ip
;
630 ipt
= (struct ip_timestamp
*)cp
;
631 if (ipt
->ipt_len
< 5)
633 if (ipt
->ipt_ptr
> ipt
->ipt_len
- sizeof (int32_t)) {
634 if (++ipt
->ipt_oflw
== 0)
638 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
639 switch (ipt
->ipt_flg
) {
641 case IPOPT_TS_TSONLY
:
644 case IPOPT_TS_TSANDADDR
:
645 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
646 sizeof(struct in_addr
) > ipt
->ipt_len
)
648 ipaddr
.sin_addr
= dst
;
649 ia
= (INA
)ifaof_ i f p
foraddr((SA
)&ipaddr
,
653 bcopy((caddr_t
)&IA_SIN(ia
)->sin_addr
,
654 (caddr_t
)sin
, sizeof(struct in_addr
));
655 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
658 case IPOPT_TS_PRESPEC
:
659 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
660 sizeof(struct in_addr
) > ipt
->ipt_len
)
662 bcopy((caddr_t
)sin
, (caddr_t
)&ipaddr
.sin_addr
,
663 sizeof(struct in_addr
));
664 if (ifa_ifwithaddr((SA
)&ipaddr
) == 0)
666 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
673 bcopy((caddr_t
)&ntime
, (caddr_t
)cp
+ ipt
->ipt_ptr
- 1,
675 ipt
->ipt_ptr
+= sizeof(n_time
);
686 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
689 icmp_error(m
, type
, code
, 0, 0);
691 STAT(ipstat
.ips_badoptions
++);
698 * Strip out IP options, at higher
699 * level protocol in the kernel.
700 * Second argument is buffer to which options
701 * will be moved, and return value is their length.
702 * (XXX) should be deleted; last arg currently ignored.
705 ip_stripoptions(m
, mopt
)
706 register struct mbuf
*m
;
710 struct ip
*ip
= mtod(m
, struct ip
*);
711 register caddr_t opts
;
714 olen
= (ip
->ip_hl
<<2) - sizeof (struct ip
);
715 opts
= (caddr_t
)(ip
+ 1);
716 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
717 memcpy(opts
, opts
+ olen
, (unsigned)i
);
720 ip
->ip_hl
= sizeof(struct ip
) >> 2;