2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
5 * Copyright (C) 2005 Neil Cafferkey
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)ip_input.c 7.19 (Berkeley) 5/25/91
60 #include <libraries/miami.h>
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/malloc.h>
65 #include <sys/domain.h>
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
68 #include <sys/errno.h>
69 #include <sys/kernel.h>
70 #include <sys/synch.h>
73 #include <net/if_protos.h>
74 #include <net/route.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/in_pcb.h>
81 #include <netinet/in_var.h>
82 #include <netinet/ip_var.h>
83 #include <netinet/ip_icmp.h>
85 #include <netinet/ip_input_protos.h>
86 #include <netinet/ip_output_protos.h>
87 #include <netinet/ip_icmp_protos.h>
88 #include <netinet/in_cksum_protos.h>
89 #include <netinet/in_protos.h>
90 #include <kern/uipc_domain_protos.h>
93 #define IPFORWARDING 0
95 #ifndef IPSENDREDIRECTS
96 #define IPSENDREDIRECTS 1
102 /* These three are now accessed from the configuration module
104 int ipforwarding
= IPFORWARDING
;
105 int ipsendredirects
= IPSENDREDIRECTS
;
106 int ipprintfs
= IPPRINTFS
; /* this has effect only if DIAGNOSTIC */
108 struct ipstat ipstat
= {0}; /* ip statistics */
109 struct ipq ipq
= {0}; /* ip reass. queue */
110 u_short ip_id
= {0}; /* ip packet ctr, for ids */
111 struct ifqueue ipintrq
= {0}; /* ip packet input queue */
113 extern struct domain inetdomain
;
114 extern struct protosw inetsw
[];
115 u_char ip_protox
[IPPROTO_MAX
] = {0};
116 int ipqmaxlen
= IFQ_MAXLEN
;
117 struct in_ifaddr
*in_ifaddr
= NULL
; /* first inet address */
120 * We need to save the IP options in case a protocol wants to respond
121 * to an incoming packet over the same route if the packet got here
122 * using IP source routing. This allows connection establishment and
123 * maintenance when the remote end is on a network that is not known
127 static struct ip_srcrt
{
128 struct in_addr dst
; /* final destination */
129 char nop
; /* one NOP to align */
130 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
131 struct in_addr route
[MAX_IPOPTLEN
/sizeof(struct in_addr
)];
136 u_long
*ip_ifmatrix
= NULL
;
140 * IP initialization: fill in IP protocol switch table.
141 * All protocols not implemented in kernel go to raw IP protocol handler.
146 register struct protosw
*pr
;
149 pr
= pffindproto(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
152 for (i
= 0; i
< IPPROTO_MAX
; i
++)
153 ip_protox
[i
] = pr
- inetsw
;
154 for (pr
= inetdomain
.dom_protosw
;
155 pr
< inetdomain
.dom_protoswNPROTOSW
; pr
++)
156 if (pr
->pr_domain
->dom_family
== PF_INET
&&
157 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
)
158 ip_protox
[pr
->pr_protocol
] = pr
- inetsw
;
159 ipq
.next
= ipq
.prev
= &ipq
;
163 ip_id
= time
.tv_sec
& 0xffff;
165 ipintrq
.ifq_maxlen
= ipqmaxlen
;
168 This does
not work
, since if_index is
not constant
, but actually
169 increases after
this is done
! So change
this before enabling
170 (if_index is defined in net
/if.c
)
172 i
= (if_index
+ 1) * (if_index
+ 1) * sizeof (u_long
);
173 if ((ip_ifmatrix
= (u_long
*) bsd_malloc(i
, M_RTABLE
, M_WAITOK
)) == 0)
174 panic("no memory for ip_ifmatrix");
178 struct ip
*ip_reass();
179 struct sockaddr_in ipaddr
= { sizeof(ipaddr
), AF_INET
};
180 struct route ipforward_rt
= { 0 };
183 * Ip input routine. Checksum and byte swap header. If fragmented
184 * try to reassemble. Process options. Pass to next level.
189 register struct ip
*ip
;
190 register struct mbuf
*m
;
191 register struct ipq
*fp
;
192 register struct in_ifaddr
*ia
;
198 * Get next datagram off input queue and get IP header
202 IF_DEQUEUE(&ipintrq
, m
);
207 if ((m
->m_flags
& M_PKTHDR
) == 0)
208 panic("ipintr no HDR");
211 * If no IP addresses have been set yet but the interfaces
212 * are receiving, can't do anything with incoming packets yet.
214 if (in_ifaddr
== NULL
)
217 if (m
->m_len
< sizeof (struct ip
) &&
218 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
219 ipstat
.ips_toosmall
++;
222 ip
= mtod(m
, struct ip
*);
223 hlen
= ip
->ip_hl
<< 2;
224 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
225 ipstat
.ips_badhlen
++;
228 if (hlen
> m
->m_len
) {
229 if ((m
= m_pullup(m
, hlen
)) == 0) {
230 ipstat
.ips_badhlen
++;
233 ip
= mtod(m
, struct ip
*);
235 if (ip
->ip_sum
= in_cksum(m
, hlen
)) {
241 * Convert fields to host representation.
244 if (ip
->ip_len
< hlen
) {
252 * Check that the amount of data in the buffers
253 * is as at least much as the IP header would have us expect.
254 * Trim mbufs if longer than we expect.
255 * Drop packet if shorter than we expect.
257 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
258 ipstat
.ips_tooshort
++;
261 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
262 if (m
->m_len
== m
->m_pkthdr
.len
) {
263 m
->m_len
= ip
->ip_len
;
264 m
->m_pkthdr
.len
= ip
->ip_len
;
266 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
270 * Run through list of hooks.
272 pfil_run_hooks(m
, m
->m_pkthdr
.rcvif
, MIAMIPFBPT_IP
);
275 * Process options and, if not destined for us,
276 * ship it on. ip_dooptions returns 1 when an
277 * error was detected (causing an icmp message
278 * to be sent and the original packet to be freed).
280 ip_nhops
= 0; /* for source routed packets */
281 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
))
285 * Check our list of addresses, to see if the packet is for us.
287 for (ia
= in_ifaddr
; ia
; ia
= ia
->ia_next
) {
288 #define satosin(sa) ((struct sockaddr_in *)(sa))
290 if (IA_SIN(ia
)->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
)
293 #ifdef DIRECTED_BROADCAST
294 ia
->ia_ifp
== m
->m_pkthdr
.rcvif
&&
296 (ia
->ia_ifp
->if_flags
& IFF_BROADCAST
)) {
299 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
302 if (ip
->ip_dst
.s_addr
== ia
->ia_netbroadcast
.s_addr
)
305 * Look for all-0's host part (old broadcast addr),
306 * either for subnet or net.
308 t
= ntohl(ip
->ip_dst
.s_addr
);
309 if (t
== ia
->ia_subnet
)
315 if (ip
->ip_dst
.s_addr
== (u_long
)INADDR_BROADCAST
)
317 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
321 * Not for us; forward if possible and desirable.
323 if (ipforwarding
== 0) {
324 ipstat
.ips_cantforward
++;
332 * If offset or IP_MF are set, must reassemble.
333 * Otherwise, nothing need be done.
334 * (We could look in the reassembly queue to see
335 * if the packet was previously fragmented,
336 * but it's not worth the time; just let them time out.)
338 if (ip
->ip_off
&~ IP_DF
) {
339 if (m
->m_flags
& M_EXT
) { /* XXX */
340 if ((m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
341 ipstat
.ips_toosmall
++;
344 ip
= mtod(m
, struct ip
*);
347 * Look for queue of fragments
350 for (fp
= ipq
.next
; fp
!= &ipq
; fp
= fp
->next
)
351 if (ip
->ip_id
== fp
->ipq_id
&&
352 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
353 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
354 ip
->ip_p
== fp
->ipq_p
)
360 * Adjust ip_len to not reflect header,
361 * set ip_mff if more fragments are expected,
362 * convert offset of this to bytes.
365 ((struct ipasfrag
*)ip
)->ipf_mff
= 0;
366 if (ip
->ip_off
& IP_MF
)
367 ((struct ipasfrag
*)ip
)->ipf_mff
= 1;
371 * If datagram marked as having more fragments
372 * or if this is not the first fragment,
373 * attempt reassembly; if it succeeds, proceed.
375 if (((struct ipasfrag
*)ip
)->ipf_mff
|| ip
->ip_off
) {
376 ipstat
.ips_fragments
++;
377 ip
= ip_reass((struct ipasfrag
*)ip
, fp
);
381 ipstat
.ips_reassembled
++;
390 * Switch out to protocol's input routine.
392 ipstat
.ips_delivered
++;
393 (*inetsw
[ip_protox
[ip
->ip_p
]].pr_input
)(m
, hlen
);
401 * Take incoming datagram fragment and try to
402 * reassemble it into whole datagram. If a chain for
403 * reassembly of this datagram already exists, then it
404 * is given as fp; otherwise have to make a chain.
408 register struct ipasfrag
*ip
;
409 register struct ipq
*fp
;
411 register struct mbuf
*m
= dtom(ip
);
412 register struct ipasfrag
*q
;
414 int hlen
= ip
->ip_hl
<< 2;
418 * Presence of header sizes in mbufs
419 * would confuse code below.
425 * If first fragment to arrive, create a reassembly queue.
428 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
430 fp
= mtod(t
, struct ipq
*);
432 fp
->ipq_ttl
= IPFRAGTTL
;
433 fp
->ipq_p
= ip
->ip_p
;
434 fp
->ipq_id
= ip
->ip_id
;
435 fp
->ipq_next
= fp
->ipq_prev
= (struct ipasfrag
*)fp
;
436 fp
->ipq_src
= ((struct ip
*)ip
)->ip_src
;
437 fp
->ipq_dst
= ((struct ip
*)ip
)->ip_dst
;
438 q
= (struct ipasfrag
*)fp
;
443 * Find a segment which begins after this one does.
445 for (q
= fp
->ipq_next
; q
!= (struct ipasfrag
*)fp
; q
= q
->ipf_next
)
446 if (q
->ip_off
> ip
->ip_off
)
450 * If there is a preceding segment, it may provide some of
451 * our data already. If so, drop the data from the incoming
452 * segment. If it provides all of our data, drop us.
454 if (q
->ipf_prev
!= (struct ipasfrag
*)fp
) {
455 i
= q
->ipf_prev
->ip_off
+ q
->ipf_prev
->ip_len
- ip
->ip_off
;
466 * While we overlap succeeding segments trim them or,
467 * if they are completely covered, dequeue them.
469 while (q
!= (struct ipasfrag
*)fp
&& ip
->ip_off
+ ip
->ip_len
> q
->ip_off
) {
470 i
= (ip
->ip_off
+ ip
->ip_len
) - q
->ip_off
;
478 m_freem(dtom(q
->ipf_prev
));
484 * Stick new segment in its place;
485 * check for complete reassembly.
487 ip_enq(ip
, q
->ipf_prev
);
489 for (q
= fp
->ipq_next
; q
!= (struct ipasfrag
*)fp
; q
= q
->ipf_next
) {
490 if (q
->ip_off
!= next
)
494 if (q
->ipf_prev
->ipf_mff
)
498 * Reassembly is complete; concatenate fragments.
506 while (q
!= (struct ipasfrag
*)fp
) {
513 * Create header for new ip packet by
514 * modifying header of first packet;
515 * dequeue and discard fragment reassembly header.
516 * Make header visible.
520 ((struct ip
*)ip
)->ip_src
= fp
->ipq_src
;
521 ((struct ip
*)ip
)->ip_dst
= fp
->ipq_dst
;
523 (void) m_free(dtom(fp
));
525 m
->m_len
+= (ip
->ip_hl
<< 2);
526 m
->m_data
-= (ip
->ip_hl
<< 2);
527 /* some debugging cruft by sklower, below, will go away soon */
528 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
529 register int plen
= 0;
530 for (t
= m
; m
; m
= m
->m_next
)
532 t
->m_pkthdr
.len
= plen
;
534 return ((struct ip
*)ip
);
537 ipstat
.ips_fragdropped
++;
543 * Free a fragment reassembly header and all
544 * associated datagrams.
550 register struct ipasfrag
*q
, *p
;
552 for (q
= fp
->ipq_next
; q
!= (struct ipasfrag
*)fp
; q
= p
) {
558 (void) m_free(dtom(fp
));
562 * Put an ip fragment on a reassembly chain.
563 * Like insque, but pointers in middle of structure.
567 register struct ipasfrag
*p
, *prev
;
571 p
->ipf_next
= prev
->ipf_next
;
572 prev
->ipf_next
->ipf_prev
= p
;
577 * To ip_enq as remque is to insque.
581 register struct ipasfrag
*p
;
584 p
->ipf_prev
->ipf_next
= p
->ipf_next
;
585 p
->ipf_next
->ipf_prev
= p
->ipf_prev
;
589 * IP timer processing;
590 * if a timer expires on a reassembly
596 register struct ipq
*fp
;
607 if (fp
->prev
->ipq_ttl
== 0) {
608 ipstat
.ips_fragtimeout
++;
616 * Drain off all datagram fragments.
622 while (ipq
.next
!= &ipq
) {
623 ipstat
.ips_fragdropped
++;
628 extern struct in_ifaddr
*ifptoia();
629 struct in_ifaddr
*ip_rtaddr();
632 * Do option processing on a datagram,
633 * possibly discarding it if bad options are encountered,
634 * or forwarding it if source-routed.
635 * Returns 1 if packet has been forwarded/freed,
636 * 0 if the packet should be processed further.
642 register struct ip
*ip
= mtod(m
, struct ip
*);
644 register struct ip_timestamp
*ipt
;
645 register struct in_ifaddr
*ia
;
646 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
647 struct in_addr
*sin
, dest
;
650 cp
= (u_char
*)(ip
+ 1);
651 cnt
= (ip
->ip_hl
<< 2) - sizeof (struct ip
);
652 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
653 opt
= cp
[IPOPT_OPTVAL
];
654 if (opt
== IPOPT_EOL
)
656 if (opt
== IPOPT_NOP
)
659 optlen
= cp
[IPOPT_OLEN
];
660 if (optlen
<= 0 || optlen
> cnt
) {
661 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
671 * Source routing with record.
672 * Find interface with current destination address.
673 * If none on this machine then drop if strictly routed,
674 * or do nothing if loosely routed.
675 * Record interface address and bring up next address
676 * component. If strictly routed make sure next
677 * address is on directly accessible net.
681 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
682 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
685 ipaddr
.sin_addr
= ip
->ip_dst
;
686 ia
= (struct in_ifaddr
*)
687 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
689 if (opt
== IPOPT_SSRR
) {
691 code
= ICMP_UNREACH_SRCFAIL
;
695 * Loose routing, and not at next destination
696 * yet; nothing to do except forward.
700 off
--; /* 0 origin */
701 if (off
> optlen
- sizeof(struct in_addr
)) {
703 * End of source route. Should be for us.
705 save_rte(cp
, ip
->ip_src
);
709 * locate outgoing interface
711 bcopy((caddr_t
)(cp
+ off
), (caddr_t
)&ipaddr
.sin_addr
,
712 sizeof(ipaddr
.sin_addr
));
713 if (opt
== IPOPT_SSRR
) {
714 #define INA struct in_ifaddr *
715 #define SA struct sockaddr *
716 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0)
717 ia
= in_iaonnetof(in_netof(ipaddr
.sin_addr
));
719 ia
= ip_rtaddr(ipaddr
.sin_addr
);
722 code
= ICMP_UNREACH_SRCFAIL
;
725 ip
->ip_dst
= ipaddr
.sin_addr
;
726 bcopy((caddr_t
)&(IA_SIN(ia
)->sin_addr
),
727 (caddr_t
)(cp
+ off
), sizeof(struct in_addr
));
728 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
733 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
734 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
738 * If no space remains, ignore.
740 off
--; /* 0 origin */
741 if (off
> optlen
- sizeof(struct in_addr
))
743 ipaddr
.sin_addr
= ip
->ip_dst
;
745 * locate outgoing interface; if we're the destination,
746 * use the incoming interface (should be same).
748 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0 &&
749 (ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
751 code
= ICMP_UNREACH_HOST
;
754 bcopy((caddr_t
)&(IA_SIN(ia
)->sin_addr
),
755 (caddr_t
)(cp
+ off
), sizeof(struct in_addr
));
756 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
760 code
= cp
- (u_char
*)ip
;
761 ipt
= (struct ip_timestamp
*)cp
;
762 if (ipt
->ipt_len
< 5)
764 if (ipt
->ipt_ptr
> ipt
->ipt_len
- sizeof (long)) {
765 if (++ipt
->ipt_oflw
== 0)
769 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
770 switch (ipt
->ipt_flg
) {
772 case IPOPT_TS_TSONLY
:
775 case IPOPT_TS_TSANDADDR
:
776 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
777 sizeof(struct in_addr
) > ipt
->ipt_len
)
779 ia
= ifptoia(m
->m_pkthdr
.rcvif
);
780 *sin
= IA_SIN(ia
)->sin_addr
;
781 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
784 case IPOPT_TS_PRESPEC
:
785 if (ipt
->ipt_ptr
+ sizeof(n_time
) +
786 sizeof(struct in_addr
) > ipt
->ipt_len
)
788 ipaddr
.sin_addr
= *sin
;
789 if (ifa_ifwithaddr((SA
)&ipaddr
) == 0)
791 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
798 bcopy((caddr_t
)&ntime
, (caddr_t
)cp
+ ipt
->ipt_ptr
- 1,
800 ipt
->ipt_ptr
+= sizeof(n_time
);
809 icmp_error(m
, type
, code
, dest
); /* dest is not used */
814 * Given address of next destination (final or next hop),
815 * return internet address info of interface to be used to get there.
821 register struct sockaddr_in
*sin
;
823 sin
= (struct sockaddr_in
*) &ipforward_rt
.ro_dst
;
825 if (ipforward_rt
.ro_rt
== 0 || dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
826 if (ipforward_rt
.ro_rt
) {
827 RTFREE(ipforward_rt
.ro_rt
);
828 ipforward_rt
.ro_rt
= 0;
830 sin
->sin_family
= AF_INET
;
831 sin
->sin_len
= sizeof(*sin
);
834 rtalloc(&ipforward_rt
);
836 if (ipforward_rt
.ro_rt
== 0)
837 return ((struct in_ifaddr
*)0);
838 return ((struct in_ifaddr
*) ipforward_rt
.ro_rt
->rt_ifa
);
842 * Save incoming source route for use in replies,
843 * to be picked up later by ip_srcroute if the receiver is interested.
846 save_rte(option
, dst
)
852 olen
= option
[IPOPT_OLEN
];
855 printf("save_rte: olen %ld\n", olen
);
857 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
859 bcopy((caddr_t
)option
, (caddr_t
)ip_srcrt
.srcopt
, olen
);
860 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
865 * Retrieve incoming source route for use in replies,
866 * in the same form used by setsockopt.
867 * The first hop is placed before the options, will be removed later.
872 register struct in_addr
*p
, *q
;
873 register struct mbuf
*m
;
876 return ((struct mbuf
*)0);
877 m
= m_get(M_DONTWAIT
, MT_SOOPTS
);
879 return ((struct mbuf
*)0);
881 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
883 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
884 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
888 printf("ip_srcroute: nhops %ld mlen %ld", ip_nhops
, m
->m_len
);
892 * First save first hop for return route
894 p
= &ip_srcrt
.route
[ip_nhops
- 1];
895 *(mtod(m
, struct in_addr
*)) = *p
--;
898 printf(" hops %lx", ntohl(mtod(m
, struct in_addr
*)->s_addr
));
902 * Copy option fields and padding (nop) to mbuf.
904 ip_srcrt
.nop
= IPOPT_NOP
;
905 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
906 aligned_bcopy_const((caddr_t
)&ip_srcrt
.nop
,
907 mtod(m
, caddr_t
) + sizeof(struct in_addr
), OPTSIZ
);
908 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
909 sizeof(struct in_addr
) + OPTSIZ
);
912 * Record return path as an IP source route,
913 * reversing the path (pointers are now aligned).
915 while (p
>= ip_srcrt
.route
) {
918 printf(" %lx", ntohl(q
->s_addr
));
923 * Last hop goes to final destination.
928 printf(" %lx\n", ntohl(q
->s_addr
));
934 * Strip out IP options, at higher
935 * level protocol in the kernel.
936 * Second argument is buffer to which options
937 * will be moved, and return value is their length.
938 * XXX should be deleted; last arg currently ignored.
941 ip_stripoptions(m
, mopt
)
942 register struct mbuf
*m
;
946 struct ip
*ip
= mtod(m
, struct ip
*);
947 register caddr_t opts
;
950 olen
= (ip
->ip_hl
<<2) - sizeof (struct ip
);
951 opts
= (caddr_t
)(ip
+ 1);
952 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
953 aligned_bcopy(opts
+ olen
, opts
, (unsigned)i
);
955 if (m
->m_flags
& M_PKTHDR
)
956 m
->m_pkthdr
.len
-= olen
;
957 ip
->ip_hl
= sizeof(struct ip
) >> 2;
960 u_char inetctlerrmap
[PRC_NCMDS
] = {
962 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
963 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
964 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
970 * Forward a packet. If some error occurs return the sender
971 * an icmp packet. Note we can't always generate a meaningful
972 * icmp message because icmp doesn't have a large enough repertoire
973 * of codes and types.
975 * If not forwarding, just drop the packet. This could be confusing
976 * if ipforwarding was zero but some routing protocol was advancing
977 * us as a gateway to somewhere. However, we must let the routing
978 * protocol deal with that.
980 * The srcrt parameter indicates whether the packet is being forwarded
981 * via a source route.
988 register struct ip
*ip
= mtod(m
, struct ip
*);
989 register struct sockaddr_in
*sin
;
990 register struct rtentry
*rt
;
991 int error
, type
= 0, code
= 0;
998 printf("forward: src %lx dst %lx ttl %lx\n", ip
->ip_src
.s_addr
,
999 ip
->ip_dst
.s_addr
, ip
->ip_ttl
);
1001 if (m
->m_flags
& M_BCAST
|| in_canforward(ip
->ip_dst
) == 0) {
1002 ipstat
.ips_cantforward
++;
1007 if (ip
->ip_ttl
<= IPTTLDEC
) {
1008 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
, dest
);
1011 ip
->ip_ttl
-= IPTTLDEC
;
1013 sin
= (struct sockaddr_in
*)&ipforward_rt
.ro_dst
;
1014 if ((rt
= ipforward_rt
.ro_rt
) == 0 ||
1015 ip
->ip_dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
1016 if (ipforward_rt
.ro_rt
) {
1017 RTFREE(ipforward_rt
.ro_rt
);
1018 ipforward_rt
.ro_rt
= 0;
1020 sin
->sin_family
= AF_INET
;
1021 sin
->sin_len
= sizeof(*sin
);
1022 sin
->sin_addr
= ip
->ip_dst
;
1024 rtalloc(&ipforward_rt
);
1025 if (ipforward_rt
.ro_rt
== 0) {
1026 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
);
1029 rt
= ipforward_rt
.ro_rt
;
1033 * Save at most 64 bytes of the packet in case
1034 * we need to generate an ICMP message to the src.
1036 mcopy
= m_copy(m
, 0, imin((int)ip
->ip_len
, 64));
1039 ip_ifmatrix
[rt
->rt_ifp
->if_index
+
1040 if_index
* m
->m_pkthdr
.rcvif
->if_index
]++;
1043 * If forwarding packet using same interface that it came in on,
1044 * perhaps should send a redirect to sender to shortcut a hop.
1045 * Only send redirect if source is sending directly to us,
1046 * and if packet was not source routed (or has any options).
1047 * Also, don't send redirect if forwarding using a default route
1048 * or a route modified by a redirect.
1050 #define satosin(sa) ((struct sockaddr_in *)(sa))
1051 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
1052 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
1053 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
1054 ipsendredirects
&& !srcrt
) {
1055 struct in_ifaddr
*ia
;
1056 u_long src
= ntohl(ip
->ip_src
.s_addr
);
1057 u_long dst
= ntohl(ip
->ip_dst
.s_addr
);
1059 if ((ia
= ifptoia(m
->m_pkthdr
.rcvif
)) &&
1060 (src
& ia
->ia_subnetmask
) == ia
->ia_subnet
) {
1061 if (rt
->rt_flags
& RTF_GATEWAY
)
1062 dest
= satosin(rt
->rt_gateway
)->sin_addr
;
1066 * If the destination is reached by a route to host,
1067 * is on a subnet of a local net, or is directly
1068 * on the attached net (!), use host redirect.
1069 * (We may be the correct first hop for other subnets.)
1071 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1072 type
= ICMP_REDIRECT
;
1073 if ((rt
->rt_flags
& RTF_HOST
) ||
1074 (rt
->rt_flags
& RTF_GATEWAY
) == 0)
1075 code
= ICMP_REDIRECT_HOST
;
1076 else if (RTA(rt
)->ia_subnetmask
!= RTA(rt
)->ia_netmask
&&
1077 (dst
& RTA(rt
)->ia_netmask
) == RTA(rt
)->ia_net
)
1078 code
= ICMP_REDIRECT_HOST
;
1080 code
= ICMP_REDIRECT_NET
;
1083 printf("redirect (%ld) to %lx\n", code
, dest
.s_addr
);
1088 error
= ip_output(m
, (struct mbuf
*)0, &ipforward_rt
, IP_FORWARDING
);
1090 ipstat
.ips_cantforward
++;
1092 ipstat
.ips_forward
++;
1094 ipstat
.ips_redirectsent
++;
1105 case 0: /* forwarded, but need redirect */
1106 /* type, code set above */
1109 case ENETUNREACH
: /* shouldn't happen, checked above */
1114 type
= ICMP_UNREACH
;
1115 code
= ICMP_UNREACH_HOST
;
1119 type
= ICMP_UNREACH
;
1120 code
= ICMP_UNREACH_NEEDFRAG
;
1121 ipstat
.ips_cantfrag
++;
1125 type
= ICMP_SOURCEQUENCH
;
1129 icmp_error(mcopy
, type
, code
, dest
);