2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
55 * $ANA: ip_input.c,v 1.5 1996/09/18 14:34:59 wollman Exp $
62 #include <sys/param.h>
63 #include <sys/systm.h>
65 #include <sys/malloc.h>
66 #include <sys/domain.h>
67 #include <sys/protosw.h>
68 #include <sys/socket.h>
70 #include <sys/kernel.h>
71 #include <sys/syslog.h>
72 #include <sys/sysctl.h>
74 #include <kern/queue.h>
77 #include <net/if_var.h>
78 #include <net/if_dl.h>
79 #include <net/route.h>
80 #include <net/netisr.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/in_var.h>
85 #include <netinet/ip.h>
87 #include <netinet/ip6.h>
88 #include <netinet6/ip6_var.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/ip_icmp.h>
93 #include <sys/socketvar.h>
95 #include <sys/kdebug.h>
98 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0)
99 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2)
100 #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8))
104 #include <netinet/ip_fw.h>
108 #include <netinet6/ipsec.h>
109 #include <netkey/key.h>
110 #include <netkey/key_debug.h>
114 #if defined(NFAITH) && NFAITH > 0
115 #include <net/if_types.h>
119 #include <netinet/ip_dummynet.h>
123 static int ip_rsvp_on
;
124 struct socket
*ip_rsvpd
;
126 int ipforwarding
= 0;
127 SYSCTL_INT(_net_inet_ip
, IPCTL_FORWARDING
, forwarding
, CTLFLAG_RW
,
128 &ipforwarding
, 0, "");
130 static int ipsendredirects
= 1; /* XXX */
131 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
, CTLFLAG_RW
,
132 &ipsendredirects
, 0, "");
134 int ip_defttl
= IPDEFTTL
;
135 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
,
138 static int ip_dosourceroute
= 0;
139 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
, CTLFLAG_RW
,
140 &ip_dosourceroute
, 0, "");
142 static int ip_acceptsourceroute
= 0;
143 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,
144 CTLFLAG_RW
, &ip_acceptsourceroute
, 0, "");
146 static int ip_keepfaith
= 0;
147 SYSCTL_INT(_net_inet_ip
, IPCTL_KEEPFAITH
, keepfaith
, CTLFLAG_RW
,
148 &ip_keepfaith
, 0, "");
151 static int ipprintfs
= 0;
154 extern struct domain inetdomain
;
155 extern struct protosw inetsw
[];
156 struct protosw
*ip_protox
[IPPROTO_MAX
];
157 static int ipqmaxlen
= IFQ_MAXLEN
;
158 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
159 struct ifqueue ipintrq
;
160 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQMAXLEN
, intr_queue_maxlen
, CTLFLAG_RD
,
161 &ipintrq
.ifq_maxlen
, 0, "");
162 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQDROPS
, intr_queue_drops
, CTLFLAG_RD
,
163 &ipintrq
.ifq_drops
, 0, "");
165 struct ipstat ipstat
;
166 SYSCTL_STRUCT(_net_inet_ip
, IPCTL_STATS
, stats
, CTLFLAG_RD
,
167 &ipstat
, ipstat
, "");
169 /* Packet reassembly stuff */
170 #define IPREASS_NHASH_LOG2 6
171 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
172 #define IPREASS_HMASK (IPREASS_NHASH - 1)
173 #define IPREASS_HASH(x,y) \
174 ((((x) & 0xF | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
176 static struct ipq ipq
[IPREASS_NHASH
];
177 static int nipq
= 0; /* total # of reass queues */
181 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
,
185 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
187 #define COMPAT_IPFW 1
194 #include <netinet/ip_fw.h>
197 ip_fw_chk_t
*ip_fw_chk_ptr
;
198 ip_fw_ctl_t
*ip_fw_ctl_ptr
;
201 ip_dn_ctl_t
*ip_dn_ctl_ptr
;
204 /* IP Network Address Translation (NAT) hooks */
205 ip_nat_t
*ip_nat_ptr
;
206 ip_nat_ctl_t
*ip_nat_ctl_ptr
;
209 #if defined(IPFILTER_LKM) || defined(IPFILTER)
210 int iplattach
__P((void));
211 int (*fr_checkp
) __P((struct ip
*, int, struct ifnet
*, int, struct mbuf
**)) = NULL
;
216 * We need to save the IP options in case a protocol wants to respond
217 * to an incoming packet over the same route if the packet got here
218 * using IP source routing. This allows connection establishment and
219 * maintenance when the remote end is on a network that is not known
222 static int ip_nhops
= 0;
223 static struct ip_srcrt
{
224 struct in_addr dst
; /* final destination */
225 char nop
; /* one NOP to align */
226 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
227 struct in_addr route
[MAX_IPOPTLEN
/sizeof(struct in_addr
)];
232 * Shared variable between ip_input() and ip_reass() to communicate
233 * about which packets, once assembled from fragments, get diverted,
236 static u_short frag_divert_port
;
239 struct sockaddr_in
*ip_fw_fwd_addr
;
241 static void save_rte
__P((u_char
*, struct in_addr
));
242 static int ip_dooptions
__P((struct mbuf
*));
246 void ip_forward
__P((struct mbuf
*, int));
247 static void ip_freef
__P((struct ipq
*));
249 ip_reass
__P((struct mbuf
*, struct ipq
*, struct ipq
*));
250 static struct in_ifaddr
*
251 ip_rtaddr
__P((struct in_addr
));
252 void ipintr
__P((void));
258 extern int pm_in
__P((struct ifnet
*, struct ip
*, struct mbuf
*));
259 extern struct route
*pm_route
__P((struct mbuf
*));
263 extern int ip6_protocol_tr
;
265 int natpt_in4
__P((struct mbuf
*, struct mbuf
**));
270 * IP initialization: fill in IP protocol switch table.
271 * All protocols not implemented in kernel go to raw IP protocol handler.
276 register struct protosw
*pr
;
278 static ip_initialized
= 0;
282 TAILQ_INIT(&in_ifaddrhead
);
283 pr
= pffindproto(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
286 for (i
= 0; i
< IPPROTO_MAX
; i
++)
288 for (pr
= inetdomain
.dom_protosw
; pr
; pr
= pr
->pr_next
)
289 { if(!((unsigned int)pr
->pr_domain
)) continue; /* If uninitialized, skip */
290 if (pr
->pr_domain
->dom_family
== PF_INET
&&
291 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
)
292 ip_protox
[pr
->pr_protocol
] = pr
;
294 for (i
= 0; i
< IPREASS_NHASH
; i
++)
295 ipq
[i
].next
= ipq
[i
].prev
= &ipq
[i
];
297 maxnipq
= nmbclusters
/4;
299 ip_id
= time_second
& 0xffff;
300 ipintrq
.ifq_maxlen
= ipqmaxlen
;
314 /* Initialize the PF_INET domain, and add in the pre-defined protos */
318 register struct protosw
*pr
;
319 register struct domain
*dp
;
320 static inetdomain_initted
= 0;
321 extern int in_proto_count
;
323 if (!inetdomain_initted
)
324 { kprintf("Initing %d protosw entries\n", in_proto_count
);
327 for (i
=0, pr
= &inetsw
[0]; i
<in_proto_count
; i
++, pr
++)
328 net_add_proto(pr
, dp
);
329 inetdomain_initted
= 1;
333 static struct sockaddr_in ipaddr
= { sizeof(ipaddr
), AF_INET
};
334 static struct route ipforward_rt
;
337 * Ip input routine. Checksum and byte swap header. If fragmented
338 * try to reassemble. Process options. Pass to next level.
341 ip_input(struct mbuf
*m
)
345 struct in_ifaddr
*ia
;
348 #if !IPDIVERT /* dummy variable for the firewall code to play with */
349 u_short ip_divert_cookie
= 0 ;
352 struct ip_fw_chain
*rule
= NULL
;
355 #if IPFIREWALL && DUMMYNET
357 * dummynet packet are prepended a vestigial mbuf with
358 * m_type = MT_DUMMYNET and m_data pointing to the matching
361 if (m
->m_type
== MT_DUMMYNET
) {
362 struct mbuf
*m0
= m
;
363 rule
= (struct ip_fw_chain
*)(m
->m_data
) ;
366 ip
= mtod(m
, struct ip
*);
367 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
374 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
375 panic("ip_input no HDR");
378 * If no IP addresses have been set yet but the interfaces
379 * are receiving, can't do anything with incoming packets yet.
380 * XXX This is broken! We should be able to receive broadcasts
381 * and multicasts even without any local addresses configured.
383 if (TAILQ_EMPTY(&in_ifaddrhead
))
387 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
390 if (m
->m_len
< sizeof (struct ip
) &&
391 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
392 ipstat
.ips_toosmall
++;
395 ip
= mtod(m
, struct ip
*);
397 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
398 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
400 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
401 ipstat
.ips_badvers
++;
405 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
406 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
407 ipstat
.ips_badhlen
++;
410 if (hlen
> m
->m_len
) {
411 if ((m
= m_pullup(m
, hlen
)) == 0) {
412 ipstat
.ips_badhlen
++;
415 ip
= mtod(m
, struct ip
*);
418 sum
= in_cksum(m
, hlen
);
426 * Convert fields to host representation.
429 if (ip
->ip_len
< hlen
) {
437 * Check that the amount of data in the buffers
438 * is as at least much as the IP header would have us expect.
439 * Trim mbufs if longer than we expect.
440 * Drop packet if shorter than we expect.
442 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
444 ipstat
.ips_tooshort
++;
447 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
448 if (m
->m_len
== m
->m_pkthdr
.len
) {
449 m
->m_len
= ip
->ip_len
;
450 m
->m_pkthdr
.len
= ip
->ip_len
;
452 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
456 * Right now when no processing on packet has done
457 * and it is still fresh out of network we do our black
459 * - Firewall: deny/allow/divert
460 * - Xlate: translate packet's addr/port (NAT).
461 * - Pipe: pass pkt through dummynet.
462 * - Wrap: fake packet's addr/port <unimpl.>
463 * - Encapsulate: put it in another IP and send out. <unimp.>
466 #if defined(IPFIREWALL) && defined(DUMMYNET)
469 #if defined(IPFILTER) || defined(IPFILTER_LKM)
471 * Check if we want to allow this packet to be processed.
472 * Consider it to be bad if not.
477 if ((*fr_checkp
)(ip
, hlen
, m
->m_pkthdr
.rcvif
, 0, &m1
) || !m1
)
479 ip
= mtod(m
= m1
, struct ip
*);
484 #if IPFIREWALL_FORWARD
486 * If we've been forwarded from the output side, then
487 * skip the firewall a second time
491 #endif /* IPFIREWALL_FORWARD */
492 i
= (*ip_fw_chk_ptr
)(&ip
, hlen
, NULL
, &ip_divert_cookie
,
493 &m
, &rule
, &ip_fw_fwd_addr
);
495 * see the comment in ip_output for the return values
496 * produced by the firewall.
498 if (!m
) /* packet discarded by firewall */
500 if (i
== 0 && ip_fw_fwd_addr
== NULL
) /* common case */
504 /* send packet to the appropriate pipe */
505 dummynet_io(i
&0xffff,DN_TO_IP_IN
,m
,NULL
,NULL
,0, rule
);
510 if (i
> 0 && i
< 0x10000) {
512 frag_divert_port
= i
& 0xffff ;
516 #if IPFIREWALL_FORWARD
517 if (i
== 0 && ip_fw_fwd_addr
!= NULL
)
521 * if we get here, the packet must be dropped
528 if (ip_nat_ptr
&& !(*ip_nat_ptr
)(&ip
, &m
, m
->m_pkthdr
.rcvif
, IP_NAT_IN
)) {
529 #if IPFIREWALL_FORWARD
530 ip_fw_fwd_addr
= NULL
;
534 #endif /* !COMPAT_IPFW */
538 * Process ip-filter/NAT.
539 * Return TRUE if this packed is discarded.
540 * Return FALSE if this packed is accepted.
543 if (doNatFil
&& pm_in(m
->m_pkthdr
.rcvif
, ip
, m
))
553 struct mbuf
*m1
= NULL
;
555 switch (natpt_in4(m
, &m1
))
557 case IPPROTO_IP
: goto dooptions
;
558 case IPPROTO_IPV4
: ip_forward(m1
, 0); break;
559 case IPPROTO_IPV6
: ip6_forward(m1
, 1); break;
560 case IPPROTO_MAX
: /* discard this packet */
573 * Process options and, if not destined for us,
574 * ship it on. ip_dooptions returns 1 when an
575 * error was detected (causing an icmp message
576 * to be sent and the original packet to be freed).
578 ip_nhops
= 0; /* for source routed packets */
579 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
)) {
580 #if IPFIREWALL_FORWARD
581 ip_fw_fwd_addr
= NULL
;
586 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
587 * matter if it is destined to another node, or whether it is
588 * a multicast one, RSVP wants it! and prevents it from being forwarded
589 * anywhere else. Also checks if the rsvp daemon is running before
590 * grabbing the packet.
592 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
596 * Check our list of addresses, to see if the packet is for us.
598 for (ia
= TAILQ_FIRST(&in_ifaddrhead
); ia
;
599 ia
= TAILQ_NEXT(ia
, ia_link
)) {
600 #define satosin(sa) ((struct sockaddr_in *)(sa))
602 if (IA_SIN(ia
)->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
)
605 if (IA_SIN(ia
)->sin_addr
.s_addr
== INADDR_ANY
)
608 #if IPFIREWALL_FORWARD
610 * If the addr to forward to is one of ours, we pretend to
611 * be the destination for this packet.
613 if (ip_fw_fwd_addr
== NULL
) {
614 if (IA_SIN(ia
)->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
)
616 } else if (IA_SIN(ia
)->sin_addr
.s_addr
==
617 ip_fw_fwd_addr
->sin_addr
.s_addr
)
620 if (IA_SIN(ia
)->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
)
623 if (ia
->ia_ifp
&& ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) {
624 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
627 if (ip
->ip_dst
.s_addr
== ia
->ia_netbroadcast
.s_addr
)
631 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
632 struct in_multi
*inm
;
635 * If we are acting as a multicast router, all
636 * incoming multicast packets are passed to the
637 * kernel-level multicast forwarding function.
638 * The packet is returned (relatively) intact; if
639 * ip_mforward() returns a non-zero value, the packet
640 * must be discarded, else it may be accepted below.
642 * (The IP ident field is put in the same byte order
643 * as expected when ip_mforward() is called from
646 ip
->ip_id
= htons(ip
->ip_id
);
647 if (ip_mforward(ip
, m
->m_pkthdr
.rcvif
, m
, 0) != 0) {
648 ipstat
.ips_cantforward
++;
652 ip
->ip_id
= ntohs(ip
->ip_id
);
655 * The process-level routing demon needs to receive
656 * all multicast IGMP packets, whether or not this
657 * host belongs to their destination groups.
659 if (ip
->ip_p
== IPPROTO_IGMP
)
661 ipstat
.ips_forward
++;
664 * See if we belong to the destination multicast group on the
667 IN_LOOKUP_MULTI(ip
->ip_dst
, m
->m_pkthdr
.rcvif
, inm
);
669 ipstat
.ips_notmember
++;
675 if (ip
->ip_dst
.s_addr
== (u_long
)INADDR_BROADCAST
)
677 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
680 #if defined(NFAITH) && NFAITH > 0
682 * FAITH(Firewall Aided Internet Translator)
684 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
686 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_ICMP
)
694 * Not for us; forward if possible and desirable.
696 if (ipforwarding
== 0) {
697 ipstat
.ips_cantforward
++;
701 #if IPFIREWALL_FORWARD
702 ip_fw_fwd_addr
= NULL
;
709 * If offset or IP_MF are set, must reassemble.
710 * Otherwise, nothing need be done.
711 * (We could look in the reassembly queue to see
712 * if the packet was previously fragmented,
713 * but it's not worth the time; just let them time out.)
715 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
716 if (m
->m_flags
& M_EXT
) { /* XXX */
717 if ((m
= m_pullup(m
, hlen
)) == 0) {
718 ipstat
.ips_toosmall
++;
720 frag_divert_port
= 0;
721 ip_divert_cookie
= 0;
723 #if IPFIREWALL_FORWARD
724 ip_fw_fwd_addr
= NULL
;
728 ip
= mtod(m
, struct ip
*);
730 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
732 * Look for queue of fragments
735 for (fp
= ipq
[sum
].next
; fp
!= &ipq
[sum
]; fp
= fp
->next
)
736 if (ip
->ip_id
== fp
->ipq_id
&&
737 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
738 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
739 ip
->ip_p
== fp
->ipq_p
)
744 /* check if there's a place for the new queue */
745 if (nipq
> maxnipq
) {
747 * drop something from the tail of the current queue
748 * before proceeding further
750 if (ipq
[sum
].prev
== &ipq
[sum
]) { /* gak */
751 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
752 if (ipq
[i
].prev
!= &ipq
[i
]) {
753 ip_freef(ipq
[i
].prev
);
758 ip_freef(ipq
[sum
].prev
);
762 * Adjust ip_len to not reflect header,
763 * set ip_mff if more fragments are expected,
764 * convert offset of this to bytes.
767 mff
= (ip
->ip_off
& IP_MF
) != 0;
770 * Make sure that fragments have a data length
771 * that's a non-zero multiple of 8 bytes.
773 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
774 ipstat
.ips_toosmall
++; /* XXX */
777 m
->m_flags
|= M_FRAG
;
782 * If datagram marked as having more fragments
783 * or if this is not the first fragment,
784 * attempt reassembly; if it succeeds, proceed.
786 if (mff
|| ip
->ip_off
) {
787 ipstat
.ips_fragments
++;
788 m
->m_pkthdr
.header
= ip
;
789 ip
= ip_reass(m
, fp
, &ipq
[sum
]);
791 #if IPFIREWALL_FORWARD
792 ip_fw_fwd_addr
= NULL
;
796 /* Get the length of the reassembled packets header */
797 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
798 ipstat
.ips_reassembled
++;
801 if (frag_divert_port
) {
805 m
.m_data
= (char *) ip
;
811 ip
->ip_sum
= in_cksum(&m
, hlen
);
826 * Divert reassembled packets to the divert protocol if required
827 * If divert port is null then cookie should be too,
828 * so we shouldn't need to clear them here. Assume ip_divert does so.
830 if (frag_divert_port
) {
831 ipstat
.ips_delivered
++;
832 ip_divert_port
= frag_divert_port
;
833 frag_divert_port
= 0;
834 (*ip_protox
[IPPROTO_DIVERT
]->pr_input
)(m
, hlen
);
838 /* Don't let packets divert themselves */
839 if (ip
->ip_p
== IPPROTO_DIVERT
) {
840 ipstat
.ips_noproto
++;
847 * Switch out to protocol's input routine.
849 ipstat
.ips_delivered
++;
851 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
852 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
854 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
855 #if IPFIREWALL_FORWARD
856 ip_fw_fwd_addr
= NULL
; /* tcp needed it */
860 #if IPFIREWALL_FORWARD
861 ip_fw_fwd_addr
= NULL
;
863 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
868 * IP software interrupt routine - to go away sometime soon
876 KERNEL_DEBUG(DBG_FNC_IP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
880 IF_DEQUEUE(&ipintrq
, m
);
883 KERNEL_DEBUG(DBG_FNC_IP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
891 NETISR_SET(NETISR_IP
, ipintr
);
894 * Take incoming datagram fragment and try to
895 * reassemble it into whole datagram. If a chain for
896 * reassembly of this datagram already exists, then it
897 * is given as fp; otherwise have to make a chain.
900 ip_reass(m
, fp
, where
)
901 register struct mbuf
*m
;
902 register struct ipq
*fp
;
905 struct ip
*ip
= mtod(m
, struct ip
*);
906 register struct mbuf
*p
= 0, *q
, *nq
;
908 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
912 * Presence of header sizes in mbufs
913 * would confuse code below.
919 * If first fragment to arrive, create a reassembly queue.
922 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
924 fp
= mtod(t
, struct ipq
*);
925 insque((void *) fp
, (void *) where
);
927 fp
->ipq_ttl
= IPFRAGTTL
;
928 fp
->ipq_p
= ip
->ip_p
;
929 fp
->ipq_id
= ip
->ip_id
;
930 fp
->ipq_src
= ip
->ip_src
;
931 fp
->ipq_dst
= ip
->ip_dst
;
936 fp
->ipq_div_cookie
= 0;
941 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
944 * Find a segment which begins after this one does.
946 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
947 if (GETIP(q
)->ip_off
> ip
->ip_off
)
951 * If there is a preceding segment, it may provide some of
952 * our data already. If so, drop the data from the incoming
953 * segment. If it provides all of our data, drop us, otherwise
954 * stick new segment in the proper place.
957 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
965 m
->m_nextpkt
= p
->m_nextpkt
;
968 m
->m_nextpkt
= fp
->ipq_frags
;
973 * While we overlap succeeding segments trim them or,
974 * if they are completely covered, dequeue them.
976 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
978 i
= (ip
->ip_off
+ ip
->ip_len
) -
980 if (i
< GETIP(q
)->ip_len
) {
981 GETIP(q
)->ip_len
-= i
;
982 GETIP(q
)->ip_off
+= i
;
995 * Any fragment diverting causes the whole packet to divert
997 if (frag_divert_port
) {
998 fp
->ipq_divert
= frag_divert_port
;
999 fp
->ipq_div_cookie
= ip_divert_cookie
;
1001 frag_divert_port
= 0;
1002 ip_divert_cookie
= 0;
1006 * Check for complete reassembly.
1009 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1010 if (GETIP(q
)->ip_off
!= next
)
1012 next
+= GETIP(q
)->ip_len
;
1014 /* Make sure the last packet didn't have the IP_MF flag */
1015 if (p
->m_flags
& M_FRAG
)
1019 * Reassembly is complete. Make sure the packet is a sane size.
1023 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1024 ipstat
.ips_toolong
++;
1030 * Concatenate fragments.
1038 for (q
= nq
; q
!= NULL
; q
= nq
) {
1040 q
->m_nextpkt
= NULL
;
1046 * extract divert port for packet, if any
1048 frag_divert_port
= fp
->ipq_divert
;
1049 ip_divert_cookie
= fp
->ipq_div_cookie
;
1053 * Create header for new ip packet by
1054 * modifying header of first packet;
1055 * dequeue and discard fragment reassembly header.
1056 * Make header visible.
1059 ip
->ip_src
= fp
->ipq_src
;
1060 ip
->ip_dst
= fp
->ipq_dst
;
1061 remque((void *) fp
);
1063 (void) m_free(dtom(fp
));
1064 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1065 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1066 /* some debugging cruft by sklower, below, will go away soon */
1067 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
1068 register int plen
= 0;
1069 for (t
= m
; m
; m
= m
->m_next
)
1071 t
->m_pkthdr
.len
= plen
;
1077 frag_divert_port
= 0;
1078 ip_divert_cookie
= 0;
1080 ipstat
.ips_fragdropped
++;
1088 * Free a fragment reassembly header and all
1089 * associated datagrams.
1095 register struct mbuf
*q
;
1097 while (fp
->ipq_frags
) {
1099 fp
->ipq_frags
= q
->m_nextpkt
;
1102 remque((void *) fp
);
1103 (void) m_free(dtom(fp
));
1108 * IP timer processing;
1109 * if a timer expires on a reassembly
1110 * queue, discard it.
1115 register struct ipq
*fp
;
1119 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1123 while (fp
!= &ipq
[i
]) {
1126 if (fp
->prev
->ipq_ttl
== 0) {
1127 ipstat
.ips_fragtimeout
++;
1137 * Drain off all datagram fragments.
1144 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1145 while (ipq
[i
].next
!= &ipq
[i
]) {
1146 ipstat
.ips_fragdropped
++;
1147 ip_freef(ipq
[i
].next
);
1154 * Do option processing on a datagram,
1155 * possibly discarding it if bad options are encountered,
1156 * or forwarding it if source-routed.
1157 * Returns 1 if packet has been forwarded/freed,
1158 * 0 if the packet should be processed further.
1164 register struct ip
*ip
= mtod(m
, struct ip
*);
1165 register u_char
*cp
;
1166 register struct ip_timestamp
*ipt
;
1167 register struct in_ifaddr
*ia
;
1168 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
1169 struct in_addr
*sin
, dst
;
1173 cp
= (u_char
*)(ip
+ 1);
1174 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1175 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1176 opt
= cp
[IPOPT_OPTVAL
];
1177 if (opt
== IPOPT_EOL
)
1179 if (opt
== IPOPT_NOP
)
1182 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1183 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1186 optlen
= cp
[IPOPT_OLEN
];
1187 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1188 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1198 * Source routing with record.
1199 * Find interface with current destination address.
1200 * If none on this machine then drop if strictly routed,
1201 * or do nothing if loosely routed.
1202 * Record interface address and bring up next address
1203 * component. If strictly routed make sure next
1204 * address is on directly accessible net.
1208 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1209 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1212 ipaddr
.sin_addr
= ip
->ip_dst
;
1213 ia
= (struct in_ifaddr
*)
1214 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1216 if (opt
== IPOPT_SSRR
) {
1217 type
= ICMP_UNREACH
;
1218 code
= ICMP_UNREACH_SRCFAIL
;
1221 if (!ip_dosourceroute
)
1222 goto nosourcerouting
;
1224 * Loose routing, and not at next destination
1225 * yet; nothing to do except forward.
1229 off
--; /* 0 origin */
1230 if (off
> optlen
- sizeof(struct in_addr
)) {
1232 * End of source route. Should be for us.
1234 if (!ip_acceptsourceroute
)
1235 goto nosourcerouting
;
1236 save_rte(cp
, ip
->ip_src
);
1240 if (!ip_dosourceroute
) {
1242 char buf
[16]; /* aaa.bbb.ccc.ddd\0 */
1244 * Acting as a router, so generate ICMP
1247 strcpy(buf
, inet_ntoa(ip
->ip_dst
));
1249 "attempted source route from %s to %s\n",
1250 inet_ntoa(ip
->ip_src
), buf
);
1251 type
= ICMP_UNREACH
;
1252 code
= ICMP_UNREACH_SRCFAIL
;
1256 * Not acting as a router, so silently drop.
1258 ipstat
.ips_cantforward
++;
1265 * locate outgoing interface
1267 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1268 sizeof(ipaddr
.sin_addr
));
1270 if (opt
== IPOPT_SSRR
) {
1271 #define INA struct in_ifaddr *
1272 #define SA struct sockaddr *
1273 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0)
1274 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
1276 ia
= ip_rtaddr(ipaddr
.sin_addr
);
1278 type
= ICMP_UNREACH
;
1279 code
= ICMP_UNREACH_SRCFAIL
;
1282 ip
->ip_dst
= ipaddr
.sin_addr
;
1283 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1284 sizeof(struct in_addr
));
1285 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1287 * Let ip_intr's mcast routing check handle mcast pkts
1289 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1293 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1294 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1297 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1298 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1302 * If no space remains, ignore.
1304 off
--; /* 0 origin */
1305 if (off
> optlen
- sizeof(struct in_addr
))
1307 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1308 sizeof(ipaddr
.sin_addr
));
1310 * locate outgoing interface; if we're the destination,
1311 * use the incoming interface (should be same).
1313 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0 &&
1314 (ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
1315 type
= ICMP_UNREACH
;
1316 code
= ICMP_UNREACH_HOST
;
1319 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1320 sizeof(struct in_addr
));
1321 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1325 code
= cp
- (u_char
*)ip
;
1326 ipt
= (struct ip_timestamp
*)cp
;
1327 if (ipt
->ipt_len
< 5)
1329 if (ipt
->ipt_ptr
> ipt
->ipt_len
- sizeof(int32_t)) {
1330 if (++ipt
->ipt_oflw
== 0)
1334 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
1335 switch (ipt
->ipt_flg
) {
1337 case IPOPT_TS_TSONLY
:
1340 case IPOPT_TS_TSANDADDR
:
1341 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1342 sizeof(struct in_addr
) > ipt
->ipt_len
)
1344 ipaddr
.sin_addr
= dst
;
1345 ia
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
,
1349 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
1350 sizeof(struct in_addr
));
1351 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1354 case IPOPT_TS_PRESPEC
:
1355 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1356 sizeof(struct in_addr
) > ipt
->ipt_len
)
1358 (void)memcpy(&ipaddr
.sin_addr
, sin
,
1359 sizeof(struct in_addr
));
1360 if (ifa_ifwithaddr((SA
)&ipaddr
) == 0)
1362 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1369 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
1371 ipt
->ipt_ptr
+= sizeof(n_time
);
1374 if (forward
&& ipforwarding
) {
1380 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2; /* XXX icmp_error adds in hdr length */
1381 icmp_error(m
, type
, code
, 0, 0);
1382 ipstat
.ips_badoptions
++;
1387 * Given address of next destination (final or next hop),
1388 * return internet address info of interface to be used to get there.
1390 static struct in_ifaddr
*
1394 register struct sockaddr_in
*sin
;
1396 sin
= (struct sockaddr_in
*) &ipforward_rt
.ro_dst
;
1398 if (ipforward_rt
.ro_rt
== 0 || dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
1399 if (ipforward_rt
.ro_rt
) {
1400 RTFREE(ipforward_rt
.ro_rt
);
1401 ipforward_rt
.ro_rt
= 0;
1403 sin
->sin_family
= AF_INET
;
1404 sin
->sin_len
= sizeof(*sin
);
1405 sin
->sin_addr
= dst
;
1407 rtalloc_ign(&ipforward_rt
, RTF_PRCLONING
);
1409 if (ipforward_rt
.ro_rt
== 0)
1410 return ((struct in_ifaddr
*)0);
1411 return ((struct in_ifaddr
*) ipforward_rt
.ro_rt
->rt_ifa
);
1415 * Save incoming source route for use in replies,
1416 * to be picked up later by ip_srcroute if the receiver is interested.
1419 save_rte(option
, dst
)
1425 olen
= option
[IPOPT_OLEN
];
1428 printf("save_rte: olen %d\n", olen
);
1430 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
1432 bcopy(option
, ip_srcrt
.srcopt
, olen
);
1433 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
1438 * Retrieve incoming source route for use in replies,
1439 * in the same form used by setsockopt.
1440 * The first hop is placed before the options, will be removed later.
1445 register struct in_addr
*p
, *q
;
1446 register struct mbuf
*m
;
1449 return ((struct mbuf
*)0);
1450 m
= m_get(M_DONTWAIT
, MT_HEADER
);
1452 return ((struct mbuf
*)0);
1454 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1456 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1457 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
1461 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
1465 * First save first hop for return route
1467 p
= &ip_srcrt
.route
[ip_nhops
- 1];
1468 *(mtod(m
, struct in_addr
*)) = *p
--;
1471 printf(" hops %lx", (u_long
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
1475 * Copy option fields and padding (nop) to mbuf.
1477 ip_srcrt
.nop
= IPOPT_NOP
;
1478 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
1479 (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
),
1480 &ip_srcrt
.nop
, OPTSIZ
);
1481 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
1482 sizeof(struct in_addr
) + OPTSIZ
);
1485 * Record return path as an IP source route,
1486 * reversing the path (pointers are now aligned).
1488 while (p
>= ip_srcrt
.route
) {
1491 printf(" %lx", (u_long
)ntohl(q
->s_addr
));
1496 * Last hop goes to final destination.
1501 printf(" %lx\n", (u_long
)ntohl(q
->s_addr
));
1507 * Strip out IP options, at higher
1508 * level protocol in the kernel.
1509 * Second argument is buffer to which options
1510 * will be moved, and return value is their length.
1511 * XXX should be deleted; last arg currently ignored.
1514 ip_stripoptions(m
, mopt
)
1515 register struct mbuf
*m
;
1519 struct ip
*ip
= mtod(m
, struct ip
*);
1520 register caddr_t opts
;
1523 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1524 opts
= (caddr_t
)(ip
+ 1);
1525 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
1526 bcopy(opts
+ olen
, opts
, (unsigned)i
);
1528 if (m
->m_flags
& M_PKTHDR
)
1529 m
->m_pkthdr
.len
-= olen
;
1530 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
1533 u_char inetctlerrmap
[PRC_NCMDS
] = {
1535 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
1536 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
1537 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
1543 * Forward a packet. If some error occurs return the sender
1544 * an icmp packet. Note we can't always generate a meaningful
1545 * icmp message because icmp doesn't have a large enough repertoire
1546 * of codes and types.
1548 * If not forwarding, just drop the packet. This could be confusing
1549 * if ipforwarding was zero but some routing protocol was advancing
1550 * us as a gateway to somewhere. However, we must let the routing
1551 * protocol deal with that.
1553 * The srcrt parameter indicates whether the packet is being forwarded
1554 * via a source route.
1560 ip_forward(m
, srcrt
)
1564 register struct ip
*ip
= mtod(m
, struct ip
*);
1565 register struct sockaddr_in
*sin
;
1566 register struct rtentry
*rt
;
1567 int error
, type
= 0, code
= 0;
1570 struct ifnet
*destifp
;
1572 struct ifnet dummyifp
;
1578 printf("forward: src %lx dst %lx ttl %x\n",
1579 (u_long
)ip
->ip_src
.s_addr
, (u_long
)ip
->ip_dst
.s_addr
,
1584 if (m
->m_flags
& M_BCAST
|| in_canforward(ip
->ip_dst
) == 0) {
1585 ipstat
.ips_cantforward
++;
1590 if (ip
->ip_ttl
<= IPTTLDEC
) {
1591 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
, dest
, 0);
1594 ip
->ip_ttl
-= IPTTLDEC
;
1599 struct route
*ipfw_rt
;
1601 if ((ipfw_rt
= pm_route(m
)) != NULL
)
1603 mcopy
= m_copy(m
, 0, imin((int)ip
->ip_len
, 64));
1605 ipsec_setsocket(m
, NULL
);
1607 error
= ip_output(m
, (struct mbuf
*)0, ipfw_rt
,
1608 IP_FORWARDING
| IP_PROTOCOLROUTE
, 0);
1615 sin
= (struct sockaddr_in
*)&ipforward_rt
.ro_dst
;
1616 if ((rt
= ipforward_rt
.ro_rt
) == 0 ||
1617 ip
->ip_dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
1618 if (ipforward_rt
.ro_rt
) {
1619 RTFREE(ipforward_rt
.ro_rt
);
1620 ipforward_rt
.ro_rt
= 0;
1622 sin
->sin_family
= AF_INET
;
1623 sin
->sin_len
= sizeof(*sin
);
1624 sin
->sin_addr
= ip
->ip_dst
;
1626 rtalloc_ign(&ipforward_rt
, RTF_PRCLONING
);
1627 if (ipforward_rt
.ro_rt
== 0) {
1628 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
1631 rt
= ipforward_rt
.ro_rt
;
1635 * Save at most 64 bytes of the packet in case
1636 * we need to generate an ICMP message to the src.
1638 mcopy
= m_copy(m
, 0, imin((int)ip
->ip_len
, 64));
1641 * If forwarding packet using same interface that it came in on,
1642 * perhaps should send a redirect to sender to shortcut a hop.
1643 * Only send redirect if source is sending directly to us,
1644 * and if packet was not source routed (or has any options).
1645 * Also, don't send redirect if forwarding using a default route
1646 * or a route modified by a redirect.
1648 #define satosin(sa) ((struct sockaddr_in *)(sa))
1649 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
1650 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
1651 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
1652 ipsendredirects
&& !srcrt
) {
1653 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1654 u_long src
= ntohl(ip
->ip_src
.s_addr
);
1657 (src
& RTA(rt
)->ia_subnetmask
) == RTA(rt
)->ia_subnet
) {
1658 if (rt
->rt_flags
& RTF_GATEWAY
)
1659 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
1661 dest
= ip
->ip_dst
.s_addr
;
1662 /* Router requirements says to only send host redirects */
1663 type
= ICMP_REDIRECT
;
1664 code
= ICMP_REDIRECT_HOST
;
1667 printf("redirect (%d) to %lx\n", code
, (u_long
)dest
);
1673 ipsec_setsocket(m
, NULL
);
1675 error
= ip_output(m
, (struct mbuf
*)0, &ipforward_rt
,
1681 ipstat
.ips_cantforward
++;
1683 ipstat
.ips_forward
++;
1685 ipstat
.ips_redirectsent
++;
1688 ipflow_create(&ipforward_rt
, mcopy
);
1700 case 0: /* forwarded, but need redirect */
1701 /* type, code set above */
1704 case ENETUNREACH
: /* shouldn't happen, checked above */
1709 type
= ICMP_UNREACH
;
1710 code
= ICMP_UNREACH_HOST
;
1714 type
= ICMP_UNREACH
;
1715 code
= ICMP_UNREACH_NEEDFRAG
;
1717 if (ipforward_rt
.ro_rt
)
1718 destifp
= ipforward_rt
.ro_rt
->rt_ifp
;
1721 * If the packet is routed over IPsec tunnel, tell the
1722 * originator the tunnel MTU.
1723 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1726 if (ipforward_rt
.ro_rt
) {
1727 struct secpolicy
*sp
= NULL
;
1732 sp
= ipsec4_getpolicybyaddr(mcopy
,
1738 destifp
= ipforward_rt
.ro_rt
->rt_ifp
;
1740 /* count IPsec header size */
1741 ipsechdr
= ipsec4_hdrsiz(mcopy
,
1746 * find the correct route for outer IPv4
1747 * header, compute tunnel MTU.
1750 * The "dummyifp" code relies upon the fact
1751 * that icmp_error() touches only ifp->if_mtu.
1756 && sp
->req
->sav
!= NULL
1757 && sp
->req
->sav
->sah
!= NULL
) {
1758 ro
= &sp
->req
->sav
->sah
->sa_route
;
1759 if (ro
->ro_rt
&& ro
->ro_rt
->rt_ifp
) {
1761 ro
->ro_rt
->rt_ifp
->if_mtu
;
1762 dummyifp
.if_mtu
-= ipsechdr
;
1763 destifp
= &dummyifp
;
1771 ipstat
.ips_cantfrag
++;
1775 type
= ICMP_SOURCEQUENCH
;
1779 icmp_error(mcopy
, type
, code
, dest
, destifp
);
1783 ip_savecontrol(inp
, mp
, ip
, m
)
1784 register struct inpcb
*inp
;
1785 register struct mbuf
**mp
;
1786 register struct ip
*ip
;
1787 register struct mbuf
*m
;
1789 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
1793 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
1794 SCM_TIMESTAMP
, SOL_SOCKET
);
1796 mp
= &(*mp
)->m_next
;
1798 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
1799 *mp
= sbcreatecontrol((caddr_t
) &ip
->ip_dst
,
1800 sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
);
1802 mp
= &(*mp
)->m_next
;
1806 * Moving these out of udp_input() made them even more broken
1807 * than they already were.
1809 /* options were tossed already */
1810 if (inp
->inp_flags
& INP_RECVOPTS
) {
1811 *mp
= sbcreatecontrol((caddr_t
) opts_deleted_above
,
1812 sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
);
1814 mp
= &(*mp
)->m_next
;
1816 /* ip_srcroute doesn't do what we want here, need to fix */
1817 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
1818 *mp
= sbcreatecontrol((caddr_t
) ip_srcroute(),
1819 sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
);
1821 mp
= &(*mp
)->m_next
;
1824 if (inp
->inp_flags
& INP_RECVIF
) {
1827 struct sockaddr_dl sdl
;
1830 struct sockaddr_dl
*sdp
;
1831 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
1833 if (((ifp
= m
->m_pkthdr
.rcvif
))
1834 && ( ifp
->if_index
&& (ifp
->if_index
<= if_index
))) {
1835 sdp
= (struct sockaddr_dl
*)(ifnet_addrs
1836 [ifp
->if_index
- 1]->ifa_addr
);
1838 * Change our mind and don't try copy.
1840 if ((sdp
->sdl_family
!= AF_LINK
)
1841 || (sdp
->sdl_len
> sizeof(sdlbuf
))) {
1844 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
1848 = offsetof(struct sockaddr_dl
, sdl_data
[0]);
1849 sdl2
->sdl_family
= AF_LINK
;
1850 sdl2
->sdl_index
= 0;
1851 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
1853 *mp
= sbcreatecontrol((caddr_t
) sdl2
, sdl2
->sdl_len
,
1854 IP_RECVIF
, IPPROTO_IP
);
1856 mp
= &(*mp
)->m_next
;
1861 ip_rsvp_init(struct socket
*so
)
1863 if (so
->so_type
!= SOCK_RAW
||
1864 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
1867 if (ip_rsvpd
!= NULL
)
1872 * This may seem silly, but we need to be sure we don't over-increment
1873 * the RSVP counter, in case something slips up.
1888 * This may seem silly, but we need to be sure we don't over-decrement
1889 * the RSVP counter, in case something slips up.