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
;
67 * Ip input routine. Checksum and byte swap header. If fragmented
68 * try to reassemble. Process options. Pass to next level.
71 ip_input(struct mbuf
*m
)
73 register struct ip
*ip
;
76 DEBUG_CALL("ip_input");
77 DEBUG_ARG("m = %lx", (long)m
);
78 DEBUG_ARG("m_len = %d", m
->m_len
);
80 if (m
->m_len
< sizeof (struct ip
)) {
84 ip
= mtod(m
, struct ip
*);
86 if (ip
->ip_v
!= IPVERSION
) {
90 hlen
= ip
->ip_hl
<< 2;
91 if (hlen
<sizeof(struct ip
) || hlen
>m
->m_len
) {/* min header length */
92 goto bad
; /* or packet too short */
95 /* keep ip header intact for ICMP reply
96 * ip->ip_sum = cksum(m, hlen);
104 * Convert fields to host representation.
107 if (ip
->ip_len
< hlen
) {
114 * Check that the amount of data in the buffers
115 * is as at least much as the IP header would have us expect.
116 * Trim mbufs if longer than we expect.
117 * Drop packet if shorter than we expect.
119 if (m
->m_len
< ip
->ip_len
) {
123 if (slirp_restrict
) {
124 if ((ip
->ip_dst
.s_addr
& vnetwork_mask
.s_addr
) ==
125 vnetwork_addr
.s_addr
) {
126 if (ip
->ip_dst
.s_addr
== 0xffffffff && ip
->ip_p
!= IPPROTO_UDP
)
129 struct ex_list
*ex_ptr
;
131 if ((ip
->ip_dst
.s_addr
& ~vnetwork_mask
.s_addr
) ==
132 ~vnetwork_mask
.s_addr
)
135 for (ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
)
136 if (ex_ptr
->ex_addr
.s_addr
== ip
->ip_dst
.s_addr
)
144 /* Should drop packet if mbuf too long? hmmm... */
145 if (m
->m_len
> ip
->ip_len
)
146 m_adj(m
, ip
->ip_len
- m
->m_len
);
148 /* check ip_ttl for a correct ICMP reply */
149 if(ip
->ip_ttl
==0 || ip
->ip_ttl
==1) {
150 icmp_error(m
, ICMP_TIMXCEED
,ICMP_TIMXCEED_INTRANS
, 0,"ttl");
155 * If offset or IP_MF are set, must reassemble.
156 * Otherwise, nothing need be done.
157 * (We could look in the reassembly queue to see
158 * if the packet was previously fragmented,
159 * but it's not worth the time; just let them time out.)
161 * XXX This should fail, don't fragment yet
163 if (ip
->ip_off
&~ IP_DF
) {
164 register struct ipq
*fp
;
167 * Look for queue of fragments
170 for (l
= ipq
.ip_link
.next
; l
!= &ipq
.ip_link
; l
= l
->next
) {
171 fp
= container_of(l
, struct ipq
, ip_link
);
172 if (ip
->ip_id
== fp
->ipq_id
&&
173 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
174 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
175 ip
->ip_p
== fp
->ipq_p
)
182 * Adjust ip_len to not reflect header,
183 * set ip_mff if more fragments are expected,
184 * convert offset of this to bytes.
187 if (ip
->ip_off
& IP_MF
)
195 * If datagram marked as having more fragments
196 * or if this is not the first fragment,
197 * attempt reassembly; if it succeeds, proceed.
199 if (ip
->ip_tos
& 1 || ip
->ip_off
) {
200 ip
= ip_reass(ip
, fp
);
212 * Switch out to protocol's input routine.
216 tcp_input(m
, hlen
, (struct socket
*)NULL
);
233 #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
234 #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
236 * Take incoming datagram fragment and try to
237 * reassemble it into whole datagram. If a chain for
238 * reassembly of this datagram already exists, then it
239 * is given as fp; otherwise have to make a chain.
242 ip_reass(register struct ip
*ip
, register struct ipq
*fp
)
244 register struct mbuf
*m
= dtom(ip
);
245 register struct ipasfrag
*q
;
246 int hlen
= ip
->ip_hl
<< 2;
249 DEBUG_CALL("ip_reass");
250 DEBUG_ARG("ip = %lx", (long)ip
);
251 DEBUG_ARG("fp = %lx", (long)fp
);
252 DEBUG_ARG("m = %lx", (long)m
);
255 * Presence of header sizes in mbufs
256 * would confuse code below.
257 * Fragment m_data is concatenated.
263 * If first fragment to arrive, create a reassembly queue.
267 if ((t
= m_get()) == NULL
) goto dropfrag
;
268 fp
= mtod(t
, struct ipq
*);
269 insque(&fp
->ip_link
, &ipq
.ip_link
);
270 fp
->ipq_ttl
= IPFRAGTTL
;
271 fp
->ipq_p
= ip
->ip_p
;
272 fp
->ipq_id
= ip
->ip_id
;
273 fp
->frag_link
.next
= fp
->frag_link
.prev
= &fp
->frag_link
;
274 fp
->ipq_src
= ip
->ip_src
;
275 fp
->ipq_dst
= ip
->ip_dst
;
276 q
= (struct ipasfrag
*)fp
;
281 * Find a segment which begins after this one does.
283 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
;
285 if (q
->ipf_off
> ip
->ip_off
)
289 * If there is a preceding segment, it may provide some of
290 * our data already. If so, drop the data from the incoming
291 * segment. If it provides all of our data, drop us.
293 if (q
->ipf_prev
!= &fp
->frag_link
) {
294 struct ipasfrag
*pq
= q
->ipf_prev
;
295 i
= pq
->ipf_off
+ pq
->ipf_len
- ip
->ip_off
;
306 * While we overlap succeeding segments trim them or,
307 * if they are completely covered, dequeue them.
309 while (q
!= (struct ipasfrag
*)&fp
->frag_link
&&
310 ip
->ip_off
+ ip
->ip_len
> q
->ipf_off
) {
311 i
= (ip
->ip_off
+ ip
->ip_len
) - q
->ipf_off
;
312 if (i
< q
->ipf_len
) {
319 m_freem(dtom(q
->ipf_prev
));
325 * Stick new segment in its place;
326 * check for complete reassembly.
328 ip_enq(iptofrag(ip
), q
->ipf_prev
);
330 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
;
332 if (q
->ipf_off
!= next
)
336 if (((struct ipasfrag
*)(q
->ipf_prev
))->ipf_tos
& 1)
340 * Reassembly is complete; concatenate fragments.
342 q
= fp
->frag_link
.next
;
345 q
= (struct ipasfrag
*) q
->ipf_next
;
346 while (q
!= (struct ipasfrag
*)&fp
->frag_link
) {
347 struct mbuf
*t
= dtom(q
);
348 q
= (struct ipasfrag
*) q
->ipf_next
;
353 * Create header for new ip packet by
354 * modifying header of first packet;
355 * dequeue and discard fragment reassembly header.
356 * Make header visible.
358 q
= fp
->frag_link
.next
;
361 * If the fragments concatenated to an mbuf that's
362 * bigger than the total size of the fragment, then and
363 * m_ext buffer was alloced. But fp->ipq_next points to
364 * the old buffer (in the mbuf), so we must point ip
365 * into the new buffer.
367 if (m
->m_flags
& M_EXT
) {
368 int delta
= (char *)q
- m
->m_dat
;
369 q
= (struct ipasfrag
*)(m
->m_ext
+ delta
);
375 ip
->ip_src
= fp
->ipq_src
;
376 ip
->ip_dst
= fp
->ipq_dst
;
377 remque(&fp
->ip_link
);
378 (void) m_free(dtom(fp
));
379 m
->m_len
+= (ip
->ip_hl
<< 2);
380 m
->m_data
-= (ip
->ip_hl
<< 2);
390 * Free a fragment reassembly header and all
391 * associated datagrams.
394 ip_freef(struct ipq
*fp
)
396 register struct ipasfrag
*q
, *p
;
398 for (q
= fp
->frag_link
.next
; q
!= (struct ipasfrag
*)&fp
->frag_link
; q
= p
) {
403 remque(&fp
->ip_link
);
404 (void) m_free(dtom(fp
));
408 * Put an ip fragment on a reassembly chain.
409 * Like insque, but pointers in middle of structure.
412 ip_enq(register struct ipasfrag
*p
, register struct ipasfrag
*prev
)
414 DEBUG_CALL("ip_enq");
415 DEBUG_ARG("prev = %lx", (long)prev
);
417 p
->ipf_next
= prev
->ipf_next
;
418 ((struct ipasfrag
*)(prev
->ipf_next
))->ipf_prev
= p
;
423 * To ip_enq as remque is to insque.
426 ip_deq(register struct ipasfrag
*p
)
428 ((struct ipasfrag
*)(p
->ipf_prev
))->ipf_next
= p
->ipf_next
;
429 ((struct ipasfrag
*)(p
->ipf_next
))->ipf_prev
= p
->ipf_prev
;
433 * IP timer processing;
434 * if a timer expires on a reassembly
442 DEBUG_CALL("ip_slowtimo");
444 l
= ipq
.ip_link
.next
;
449 while (l
!= &ipq
.ip_link
) {
450 struct ipq
*fp
= container_of(l
, struct ipq
, ip_link
);
452 if (--fp
->ipq_ttl
== 0) {
459 * Do option processing on a datagram,
460 * possibly discarding it if bad options are encountered,
461 * or forwarding it if source-routed.
462 * Returns 1 if packet has been forwarded/freed,
463 * 0 if the packet should be processed further.
472 register struct ip
*ip
= mtod(m
, struct ip
*);
474 register struct ip_timestamp
*ipt
;
475 register struct in_ifaddr
*ia
;
476 int opt
, optlen
, cnt
, off
, code
, type
, forward
= 0;
477 struct in_addr
*sin
, dst
;
478 typedef u_int32_t n_time
;
482 cp
= (u_char
*)(ip
+ 1);
483 cnt
= (ip
->ip_hl
<< 2) - sizeof (struct ip
);
484 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
485 opt
= cp
[IPOPT_OPTVAL
];
486 if (opt
== IPOPT_EOL
)
488 if (opt
== IPOPT_NOP
)
491 optlen
= cp
[IPOPT_OLEN
];
492 if (optlen
<= 0 || optlen
> cnt
) {
493 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
503 * Source routing with record.
504 * Find interface with current destination address.
505 * If none on this machine then drop if strictly routed,
506 * or do nothing if loosely routed.
507 * Record interface address and bring up next address
508 * component. If strictly routed make sure next
509 * address is on directly accessible net.
513 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
514 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
517 ipaddr
.sin_addr
= ip
->ip_dst
;
518 ia
= (struct in_ifaddr
*)
519 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
521 if (opt
== IPOPT_SSRR
) {
523 code
= ICMP_UNREACH_SRCFAIL
;
527 * Loose routing, and not at next destination
528 * yet; nothing to do except forward.
532 off
--; / * 0 origin
* /
533 if (off
> optlen
- sizeof(struct in_addr
)) {
535 * End of source route. Should be for us.
537 save_rte(cp
, ip
->ip_src
);
541 * locate outgoing interface
543 bcopy((caddr_t
)(cp
+ off
), (caddr_t
)&ipaddr
.sin_addr
,
544 sizeof(ipaddr
.sin_addr
));
545 if (opt
== IPOPT_SSRR
) {
546 #define INA struct in_ifaddr *
547 #define SA struct sockaddr *
548 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0)
549 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
551 ia
= ip_rtaddr(ipaddr
.sin_addr
);
554 code
= ICMP_UNREACH_SRCFAIL
;
557 ip
->ip_dst
= ipaddr
.sin_addr
;
558 bcopy((caddr_t
)&(IA_SIN(ia
)->sin_addr
),
559 (caddr_t
)(cp
+ off
), sizeof(struct in_addr
));
560 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
562 * Let ip_intr's mcast routing check handle mcast pkts
564 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
568 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
569 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
573 * If no space remains, ignore.
576 if (off
> optlen
- sizeof(struct in_addr
))
578 bcopy((caddr_t
)(&ip
->ip_dst
), (caddr_t
)&ipaddr
.sin_addr
,
579 sizeof(ipaddr
.sin_addr
));
581 * locate outgoing interface; if we're the destination,
582 * use the incoming interface (should be same).
584 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0 &&
585 (ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
587 code
= ICMP_UNREACH_HOST
;
590 bcopy((caddr_t
)&(IA_SIN(ia
)->sin_addr
),
591 (caddr_t
)(cp
+ off
), sizeof(struct in_addr
));
592 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
596 code
= cp
- (u_char
*)ip
;
597 ipt
= (struct ip_timestamp
*)cp
;
598 if (ipt
->ipt_len
< 5)
600 if (ipt
->ipt_ptr
> ipt
->ipt_len
- sizeof (int32_t)) {
601 if (++ipt
->ipt_oflw
== 0)
605 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
606 switch (ipt
->ipt_flg
) {
608 case IPOPT_TS_TSONLY
:
611 case IPOPT_TS_TSANDADDR
:
612 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
613 sizeof(struct in_addr
) > ipt
->ipt_len
)
615 ipaddr
.sin_addr
= dst
;
616 ia
= (INA
)ifaof_ i f p
foraddr((SA
)&ipaddr
,
620 bcopy((caddr_t
)&IA_SIN(ia
)->sin_addr
,
621 (caddr_t
)sin
, sizeof(struct in_addr
));
622 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
625 case IPOPT_TS_PRESPEC
:
626 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
627 sizeof(struct in_addr
) > ipt
->ipt_len
)
629 bcopy((caddr_t
)sin
, (caddr_t
)&ipaddr
.sin_addr
,
630 sizeof(struct in_addr
));
631 if (ifa_ifwithaddr((SA
)&ipaddr
) == 0)
633 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
640 bcopy((caddr_t
)&ntime
, (caddr_t
)cp
+ ipt
->ipt_ptr
- 1,
642 ipt
->ipt_ptr
+= sizeof(n_time
);
653 icmp_error(m
, type
, code
, 0, 0);
661 * Strip out IP options, at higher
662 * level protocol in the kernel.
663 * Second argument is buffer to which options
664 * will be moved, and return value is their length.
665 * (XXX) should be deleted; last arg currently ignored.
668 ip_stripoptions(register struct mbuf
*m
, struct mbuf
*mopt
)
671 struct ip
*ip
= mtod(m
, struct ip
*);
672 register caddr_t opts
;
675 olen
= (ip
->ip_hl
<<2) - sizeof (struct ip
);
676 opts
= (caddr_t
)(ip
+ 1);
677 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
678 memcpy(opts
, opts
+ olen
, (unsigned)i
);
681 ip
->ip_hl
= sizeof(struct ip
) >> 2;