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
;
210 * We need to save the IP options in case a protocol wants to respond
211 * to an incoming packet over the same route if the packet got here
212 * using IP source routing. This allows connection establishment and
213 * maintenance when the remote end is on a network that is not known
216 static int ip_nhops
= 0;
217 static struct ip_srcrt
{
218 struct in_addr dst
; /* final destination */
219 char nop
; /* one NOP to align */
220 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
221 struct in_addr route
[MAX_IPOPTLEN
/sizeof(struct in_addr
)];
226 * Shared variable between ip_input() and ip_reass() to communicate
227 * about which packets, once assembled from fragments, get diverted,
230 static u_short frag_divert_port
;
233 struct sockaddr_in
*ip_fw_fwd_addr
;
235 static void save_rte
__P((u_char
*, struct in_addr
));
236 static int ip_dooptions
__P((struct mbuf
*));
240 void ip_forward
__P((struct mbuf
*, int));
241 static void ip_freef
__P((struct ipq
*));
243 ip_reass
__P((struct mbuf
*, struct ipq
*, struct ipq
*));
244 static struct in_ifaddr
*
245 ip_rtaddr
__P((struct in_addr
));
246 void ipintr
__P((void));
252 extern int pm_in
__P((struct ifnet
*, struct ip
*, struct mbuf
*));
253 extern struct route
*pm_route
__P((struct mbuf
*));
257 extern int ip6_protocol_tr
;
259 int natpt_in4
__P((struct mbuf
*, struct mbuf
**));
264 * IP initialization: fill in IP protocol switch table.
265 * All protocols not implemented in kernel go to raw IP protocol handler.
270 register struct protosw
*pr
;
272 static ip_initialized
= 0;
276 TAILQ_INIT(&in_ifaddrhead
);
277 pr
= pffindproto(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
280 for (i
= 0; i
< IPPROTO_MAX
; i
++)
282 for (pr
= inetdomain
.dom_protosw
; pr
; pr
= pr
->pr_next
)
283 { if(!((unsigned int)pr
->pr_domain
)) continue; /* If uninitialized, skip */
284 if (pr
->pr_domain
->dom_family
== PF_INET
&&
285 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
)
286 ip_protox
[pr
->pr_protocol
] = pr
;
288 for (i
= 0; i
< IPREASS_NHASH
; i
++)
289 ipq
[i
].next
= ipq
[i
].prev
= &ipq
[i
];
291 maxnipq
= nmbclusters
/4;
293 ip_id
= time_second
& 0xffff;
294 ipintrq
.ifq_maxlen
= ipqmaxlen
;
305 /* Initialize the PF_INET domain, and add in the pre-defined protos */
309 register struct protosw
*pr
;
310 register struct domain
*dp
;
311 static inetdomain_initted
= 0;
312 extern int in_proto_count
;
314 if (!inetdomain_initted
)
315 { kprintf("Initing %d protosw entries\n", in_proto_count
);
318 for (i
=0, pr
= &inetsw
[0]; i
<in_proto_count
; i
++, pr
++)
319 net_add_proto(pr
, dp
);
320 inetdomain_initted
= 1;
324 static struct sockaddr_in ipaddr
= { sizeof(ipaddr
), AF_INET
};
325 static struct route ipforward_rt
;
328 * Ip input routine. Checksum and byte swap header. If fragmented
329 * try to reassemble. Process options. Pass to next level.
332 ip_input(struct mbuf
*m
)
336 struct in_ifaddr
*ia
;
339 #if !IPDIVERT /* dummy variable for the firewall code to play with */
340 u_short ip_divert_cookie
= 0 ;
343 struct ip_fw_chain
*rule
= NULL
;
346 #if IPFIREWALL && DUMMYNET
348 * dummynet packet are prepended a vestigial mbuf with
349 * m_type = MT_DUMMYNET and m_data pointing to the matching
352 if (m
->m_type
== MT_DUMMYNET
) {
353 struct mbuf
*m0
= m
;
354 rule
= (struct ip_fw_chain
*)(m
->m_data
) ;
357 ip
= mtod(m
, struct ip
*);
358 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
365 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
366 panic("ip_input no HDR");
369 * If no IP addresses have been set yet but the interfaces
370 * are receiving, can't do anything with incoming packets yet.
371 * XXX This is broken! We should be able to receive broadcasts
372 * and multicasts even without any local addresses configured.
374 if (TAILQ_EMPTY(&in_ifaddrhead
))
378 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
381 if (m
->m_len
< sizeof (struct ip
) &&
382 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
383 ipstat
.ips_toosmall
++;
386 ip
= mtod(m
, struct ip
*);
388 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
389 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
391 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
392 ipstat
.ips_badvers
++;
396 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
397 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
398 ipstat
.ips_badhlen
++;
401 if (hlen
> m
->m_len
) {
402 if ((m
= m_pullup(m
, hlen
)) == 0) {
403 ipstat
.ips_badhlen
++;
406 ip
= mtod(m
, struct ip
*);
409 if (m
->m_pkthdr
.rcvif
->if_hwassist
== 0)
410 m
->m_pkthdr
.csum_flags
= 0;
412 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
413 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
415 sum
= in_cksum(m
, hlen
);
423 * Convert fields to host representation.
426 if (ip
->ip_len
< hlen
) {
434 * Check that the amount of data in the buffers
435 * is as at least much as the IP header would have us expect.
436 * Trim mbufs if longer than we expect.
437 * Drop packet if shorter than we expect.
439 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
441 ipstat
.ips_tooshort
++;
444 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
445 /* Invalidate hwcksuming */
446 m
->m_pkthdr
.csum_flags
= 0;
447 m
->m_pkthdr
.csum_data
= 0;
449 if (m
->m_len
== m
->m_pkthdr
.len
) {
450 m
->m_len
= ip
->ip_len
;
451 m
->m_pkthdr
.len
= ip
->ip_len
;
453 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
457 * Right now when no processing on packet has done
458 * and it is still fresh out of network we do our black
460 * - Firewall: deny/allow/divert
461 * - Xlate: translate packet's addr/port (NAT).
462 * - Pipe: pass pkt through dummynet.
463 * - Wrap: fake packet's addr/port <unimpl.>
464 * - Encapsulate: put it in another IP and send out. <unimp.>
467 #if defined(IPFIREWALL) && defined(DUMMYNET)
472 #if IPFIREWALL_FORWARD
474 * If we've been forwarded from the output side, then
475 * skip the firewall a second time
479 #endif /* IPFIREWALL_FORWARD */
480 i
= (*ip_fw_chk_ptr
)(&ip
, hlen
, NULL
, &ip_divert_cookie
,
481 &m
, &rule
, &ip_fw_fwd_addr
);
483 * see the comment in ip_output for the return values
484 * produced by the firewall.
486 if (!m
) /* packet discarded by firewall */
488 if (i
== 0 && ip_fw_fwd_addr
== NULL
) /* common case */
492 /* send packet to the appropriate pipe */
493 dummynet_io(i
&0xffff,DN_TO_IP_IN
,m
,NULL
,NULL
,0, rule
);
498 if (i
> 0 && i
< 0x10000) {
500 frag_divert_port
= i
& 0xffff ;
504 #if IPFIREWALL_FORWARD
505 if (i
== 0 && ip_fw_fwd_addr
!= NULL
)
509 * if we get here, the packet must be dropped
516 if (ip_nat_ptr
&& !(*ip_nat_ptr
)(&ip
, &m
, m
->m_pkthdr
.rcvif
, IP_NAT_IN
)) {
517 #if IPFIREWALL_FORWARD
518 ip_fw_fwd_addr
= NULL
;
522 #endif /* !COMPAT_IPFW */
526 * Process ip-filter/NAT.
527 * Return TRUE if this packed is discarded.
528 * Return FALSE if this packed is accepted.
531 if (doNatFil
&& pm_in(m
->m_pkthdr
.rcvif
, ip
, m
))
541 struct mbuf
*m1
= NULL
;
543 switch (natpt_in4(m
, &m1
))
545 case IPPROTO_IP
: goto dooptions
;
546 case IPPROTO_IPV4
: ip_forward(m1
, 0); break;
547 case IPPROTO_IPV6
: ip6_forward(m1
, 1); break;
548 case IPPROTO_MAX
: /* discard this packet */
561 * Process options and, if not destined for us,
562 * ship it on. ip_dooptions returns 1 when an
563 * error was detected (causing an icmp message
564 * to be sent and the original packet to be freed).
566 ip_nhops
= 0; /* for source routed packets */
567 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
)) {
568 #if IPFIREWALL_FORWARD
569 ip_fw_fwd_addr
= NULL
;
574 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
575 * matter if it is destined to another node, or whether it is
576 * a multicast one, RSVP wants it! and prevents it from being forwarded
577 * anywhere else. Also checks if the rsvp daemon is running before
578 * grabbing the packet.
580 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
584 * Check our list of addresses, to see if the packet is for us.
586 for (ia
= TAILQ_FIRST(&in_ifaddrhead
); ia
;
587 ia
= TAILQ_NEXT(ia
, ia_link
)) {
588 #define satosin(sa) ((struct sockaddr_in *)(sa))
590 if (IA_SIN(ia
)->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
)
593 if (IA_SIN(ia
)->sin_addr
.s_addr
== INADDR_ANY
)
596 #if IPFIREWALL_FORWARD
598 * If the addr to forward to is one of ours, we pretend to
599 * be the destination for this packet.
601 if (ip_fw_fwd_addr
== NULL
) {
602 if (IA_SIN(ia
)->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
)
604 } else if (IA_SIN(ia
)->sin_addr
.s_addr
==
605 ip_fw_fwd_addr
->sin_addr
.s_addr
)
608 if (IA_SIN(ia
)->sin_addr
.s_addr
== ip
->ip_dst
.s_addr
)
611 if (ia
->ia_ifp
&& ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) {
612 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
615 if (ip
->ip_dst
.s_addr
== ia
->ia_netbroadcast
.s_addr
)
619 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
620 struct in_multi
*inm
;
623 * If we are acting as a multicast router, all
624 * incoming multicast packets are passed to the
625 * kernel-level multicast forwarding function.
626 * The packet is returned (relatively) intact; if
627 * ip_mforward() returns a non-zero value, the packet
628 * must be discarded, else it may be accepted below.
630 * (The IP ident field is put in the same byte order
631 * as expected when ip_mforward() is called from
634 ip
->ip_id
= htons(ip
->ip_id
);
635 if (ip_mforward(ip
, m
->m_pkthdr
.rcvif
, m
, 0) != 0) {
636 ipstat
.ips_cantforward
++;
640 ip
->ip_id
= ntohs(ip
->ip_id
);
643 * The process-level routing demon needs to receive
644 * all multicast IGMP packets, whether or not this
645 * host belongs to their destination groups.
647 if (ip
->ip_p
== IPPROTO_IGMP
)
649 ipstat
.ips_forward
++;
652 * See if we belong to the destination multicast group on the
655 IN_LOOKUP_MULTI(ip
->ip_dst
, m
->m_pkthdr
.rcvif
, inm
);
657 ipstat
.ips_notmember
++;
663 if (ip
->ip_dst
.s_addr
== (u_long
)INADDR_BROADCAST
)
665 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
668 if (m
->m_pkthdr
.rcvif
669 && (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_AUTOCONFIGURING
)
670 && ip
->ip_p
== IPPROTO_UDP
) {
674 #if defined(NFAITH) && NFAITH > 0
676 * FAITH(Firewall Aided Internet Translator)
678 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
680 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_ICMP
)
688 * Not for us; forward if possible and desirable.
690 if (ipforwarding
== 0) {
691 ipstat
.ips_cantforward
++;
695 #if IPFIREWALL_FORWARD
696 ip_fw_fwd_addr
= NULL
;
703 * If offset or IP_MF are set, must reassemble.
704 * Otherwise, nothing need be done.
705 * (We could look in the reassembly queue to see
706 * if the packet was previously fragmented,
707 * but it's not worth the time; just let them time out.)
709 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
710 if (m
->m_flags
& M_EXT
) { /* XXX */
711 if ((m
= m_pullup(m
, hlen
)) == 0) {
712 ipstat
.ips_toosmall
++;
714 frag_divert_port
= 0;
715 ip_divert_cookie
= 0;
717 #if IPFIREWALL_FORWARD
718 ip_fw_fwd_addr
= NULL
;
722 ip
= mtod(m
, struct ip
*);
724 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
726 * Look for queue of fragments
729 for (fp
= ipq
[sum
].next
; fp
!= &ipq
[sum
]; fp
= fp
->next
)
730 if (ip
->ip_id
== fp
->ipq_id
&&
731 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
732 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
733 ip
->ip_p
== fp
->ipq_p
)
738 /* check if there's a place for the new queue */
739 if (nipq
> maxnipq
) {
741 * drop something from the tail of the current queue
742 * before proceeding further
744 if (ipq
[sum
].prev
== &ipq
[sum
]) { /* gak */
745 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
746 if (ipq
[i
].prev
!= &ipq
[i
]) {
747 ip_freef(ipq
[i
].prev
);
752 ip_freef(ipq
[sum
].prev
);
756 * Adjust ip_len to not reflect header,
757 * set ip_mff if more fragments are expected,
758 * convert offset of this to bytes.
761 mff
= (ip
->ip_off
& IP_MF
) != 0;
764 * Make sure that fragments have a data length
765 * that's a non-zero multiple of 8 bytes.
767 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
768 ipstat
.ips_toosmall
++; /* XXX */
771 m
->m_flags
|= M_FRAG
;
776 * If datagram marked as having more fragments
777 * or if this is not the first fragment,
778 * attempt reassembly; if it succeeds, proceed.
780 if (mff
|| ip
->ip_off
) {
781 ipstat
.ips_fragments
++;
782 m
->m_pkthdr
.header
= ip
;
783 ip
= ip_reass(m
, fp
, &ipq
[sum
]);
785 #if IPFIREWALL_FORWARD
786 ip_fw_fwd_addr
= NULL
;
790 /* Get the length of the reassembled packets header */
791 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
792 ipstat
.ips_reassembled
++;
795 if (frag_divert_port
) {
799 m
.m_data
= (char *) ip
;
805 ip
->ip_sum
= in_cksum(&m
, hlen
);
820 * Divert reassembled packets to the divert protocol if required
821 * If divert port is null then cookie should be too,
822 * so we shouldn't need to clear them here. Assume ip_divert does so.
824 if (frag_divert_port
) {
825 ipstat
.ips_delivered
++;
826 ip_divert_port
= frag_divert_port
;
827 frag_divert_port
= 0;
828 (*ip_protox
[IPPROTO_DIVERT
]->pr_input
)(m
, hlen
);
832 /* Don't let packets divert themselves */
833 if (ip
->ip_p
== IPPROTO_DIVERT
) {
834 ipstat
.ips_noproto
++;
841 * Switch out to protocol's input routine.
843 ipstat
.ips_delivered
++;
845 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
846 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
848 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
849 #if IPFIREWALL_FORWARD
850 ip_fw_fwd_addr
= NULL
; /* tcp needed it */
854 #if IPFIREWALL_FORWARD
855 ip_fw_fwd_addr
= NULL
;
857 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
862 * IP software interrupt routine - to go away sometime soon
870 KERNEL_DEBUG(DBG_FNC_IP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
874 IF_DEQUEUE(&ipintrq
, m
);
877 KERNEL_DEBUG(DBG_FNC_IP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
885 NETISR_SET(NETISR_IP
, ipintr
);
888 * Take incoming datagram fragment and try to
889 * reassemble it into whole datagram. If a chain for
890 * reassembly of this datagram already exists, then it
891 * is given as fp; otherwise have to make a chain.
894 ip_reass(m
, fp
, where
)
895 register struct mbuf
*m
;
896 register struct ipq
*fp
;
899 struct ip
*ip
= mtod(m
, struct ip
*);
900 register struct mbuf
*p
= 0, *q
, *nq
;
902 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
906 * Presence of header sizes in mbufs
907 * would confuse code below.
912 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
913 m
->m_pkthdr
.csum_flags
= 0;
915 * If first fragment to arrive, create a reassembly queue.
918 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
920 fp
= mtod(t
, struct ipq
*);
921 insque((void *) fp
, (void *) where
);
923 fp
->ipq_ttl
= IPFRAGTTL
;
924 fp
->ipq_p
= ip
->ip_p
;
925 fp
->ipq_id
= ip
->ip_id
;
926 fp
->ipq_src
= ip
->ip_src
;
927 fp
->ipq_dst
= ip
->ip_dst
;
932 fp
->ipq_div_cookie
= 0;
937 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
940 * Find a segment which begins after this one does.
942 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
943 if (GETIP(q
)->ip_off
> ip
->ip_off
)
947 * If there is a preceding segment, it may provide some of
948 * our data already. If so, drop the data from the incoming
949 * segment. If it provides all of our data, drop us, otherwise
950 * stick new segment in the proper place.
953 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
958 m
->m_pkthdr
.csum_flags
= 0;
962 m
->m_nextpkt
= p
->m_nextpkt
;
965 m
->m_nextpkt
= fp
->ipq_frags
;
970 * While we overlap succeeding segments trim them or,
971 * if they are completely covered, dequeue them.
973 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
975 i
= (ip
->ip_off
+ ip
->ip_len
) -
977 if (i
< GETIP(q
)->ip_len
) {
978 GETIP(q
)->ip_len
-= i
;
979 GETIP(q
)->ip_off
+= i
;
981 q
->m_pkthdr
.csum_flags
= 0;
993 * Any fragment diverting causes the whole packet to divert
995 if (frag_divert_port
) {
996 fp
->ipq_divert
= frag_divert_port
;
997 fp
->ipq_div_cookie
= ip_divert_cookie
;
999 frag_divert_port
= 0;
1000 ip_divert_cookie
= 0;
1004 * Check for complete reassembly.
1007 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1008 if (GETIP(q
)->ip_off
!= next
)
1010 next
+= GETIP(q
)->ip_len
;
1012 /* Make sure the last packet didn't have the IP_MF flag */
1013 if (p
->m_flags
& M_FRAG
)
1017 * Reassembly is complete. Make sure the packet is a sane size.
1021 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1022 ipstat
.ips_toolong
++;
1028 * Concatenate fragments.
1036 for (q
= nq
; q
!= NULL
; q
= nq
) {
1038 q
->m_nextpkt
= NULL
;
1039 if (q
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1040 m
->m_pkthdr
.csum_flags
= 0;
1042 m
->m_pkthdr
.csum_data
+= q
->m_pkthdr
.csum_data
;
1043 m
->m_pkthdr
.csum_flags
&= q
->m_pkthdr
.csum_flags
;
1050 * extract divert port for packet, if any
1052 frag_divert_port
= fp
->ipq_divert
;
1053 ip_divert_cookie
= fp
->ipq_div_cookie
;
1057 * Create header for new ip packet by
1058 * modifying header of first packet;
1059 * dequeue and discard fragment reassembly header.
1060 * Make header visible.
1063 ip
->ip_src
= fp
->ipq_src
;
1064 ip
->ip_dst
= fp
->ipq_dst
;
1065 remque((void *) fp
);
1067 (void) m_free(dtom(fp
));
1068 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1069 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1070 /* some debugging cruft by sklower, below, will go away soon */
1071 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
1072 register int plen
= 0;
1073 for (t
= m
; m
; m
= m
->m_next
)
1075 t
->m_pkthdr
.len
= plen
;
1081 frag_divert_port
= 0;
1082 ip_divert_cookie
= 0;
1084 ipstat
.ips_fragdropped
++;
1092 * Free a fragment reassembly header and all
1093 * associated datagrams.
1099 register struct mbuf
*q
;
1101 while (fp
->ipq_frags
) {
1103 fp
->ipq_frags
= q
->m_nextpkt
;
1106 remque((void *) fp
);
1107 (void) m_free(dtom(fp
));
1112 * IP timer processing;
1113 * if a timer expires on a reassembly
1114 * queue, discard it.
1119 register struct ipq
*fp
;
1123 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1127 while (fp
!= &ipq
[i
]) {
1130 if (fp
->prev
->ipq_ttl
== 0) {
1131 ipstat
.ips_fragtimeout
++;
1141 * Drain off all datagram fragments.
1148 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1149 while (ipq
[i
].next
!= &ipq
[i
]) {
1150 ipstat
.ips_fragdropped
++;
1151 ip_freef(ipq
[i
].next
);
1158 * Do option processing on a datagram,
1159 * possibly discarding it if bad options are encountered,
1160 * or forwarding it if source-routed.
1161 * Returns 1 if packet has been forwarded/freed,
1162 * 0 if the packet should be processed further.
1168 register struct ip
*ip
= mtod(m
, struct ip
*);
1169 register u_char
*cp
;
1170 register struct ip_timestamp
*ipt
;
1171 register struct in_ifaddr
*ia
;
1172 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
1173 struct in_addr
*sin
, dst
;
1177 cp
= (u_char
*)(ip
+ 1);
1178 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1179 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1180 opt
= cp
[IPOPT_OPTVAL
];
1181 if (opt
== IPOPT_EOL
)
1183 if (opt
== IPOPT_NOP
)
1186 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1187 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1190 optlen
= cp
[IPOPT_OLEN
];
1191 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1192 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1202 * Source routing with record.
1203 * Find interface with current destination address.
1204 * If none on this machine then drop if strictly routed,
1205 * or do nothing if loosely routed.
1206 * Record interface address and bring up next address
1207 * component. If strictly routed make sure next
1208 * address is on directly accessible net.
1212 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1213 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1216 ipaddr
.sin_addr
= ip
->ip_dst
;
1217 ia
= (struct in_ifaddr
*)
1218 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1220 if (opt
== IPOPT_SSRR
) {
1221 type
= ICMP_UNREACH
;
1222 code
= ICMP_UNREACH_SRCFAIL
;
1225 if (!ip_dosourceroute
)
1226 goto nosourcerouting
;
1228 * Loose routing, and not at next destination
1229 * yet; nothing to do except forward.
1233 off
--; /* 0 origin */
1234 if (off
> optlen
- sizeof(struct in_addr
)) {
1236 * End of source route. Should be for us.
1238 if (!ip_acceptsourceroute
)
1239 goto nosourcerouting
;
1240 save_rte(cp
, ip
->ip_src
);
1244 if (!ip_dosourceroute
) {
1246 char buf
[16]; /* aaa.bbb.ccc.ddd\0 */
1248 * Acting as a router, so generate ICMP
1251 strcpy(buf
, inet_ntoa(ip
->ip_dst
));
1253 "attempted source route from %s to %s\n",
1254 inet_ntoa(ip
->ip_src
), buf
);
1255 type
= ICMP_UNREACH
;
1256 code
= ICMP_UNREACH_SRCFAIL
;
1260 * Not acting as a router, so silently drop.
1262 ipstat
.ips_cantforward
++;
1269 * locate outgoing interface
1271 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1272 sizeof(ipaddr
.sin_addr
));
1274 if (opt
== IPOPT_SSRR
) {
1275 #define INA struct in_ifaddr *
1276 #define SA struct sockaddr *
1277 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0)
1278 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
1280 ia
= ip_rtaddr(ipaddr
.sin_addr
);
1282 type
= ICMP_UNREACH
;
1283 code
= ICMP_UNREACH_SRCFAIL
;
1286 ip
->ip_dst
= ipaddr
.sin_addr
;
1287 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1288 sizeof(struct in_addr
));
1289 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1291 * Let ip_intr's mcast routing check handle mcast pkts
1293 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1297 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1298 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1301 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1302 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1306 * If no space remains, ignore.
1308 off
--; /* 0 origin */
1309 if (off
> optlen
- sizeof(struct in_addr
))
1311 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1312 sizeof(ipaddr
.sin_addr
));
1314 * locate outgoing interface; if we're the destination,
1315 * use the incoming interface (should be same).
1317 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0 &&
1318 (ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
1319 type
= ICMP_UNREACH
;
1320 code
= ICMP_UNREACH_HOST
;
1323 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1324 sizeof(struct in_addr
));
1325 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1329 code
= cp
- (u_char
*)ip
;
1330 ipt
= (struct ip_timestamp
*)cp
;
1331 if (ipt
->ipt_len
< 5)
1333 if (ipt
->ipt_ptr
> ipt
->ipt_len
- sizeof(int32_t)) {
1334 if (++ipt
->ipt_oflw
== 0)
1338 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
1339 switch (ipt
->ipt_flg
) {
1341 case IPOPT_TS_TSONLY
:
1344 case IPOPT_TS_TSANDADDR
:
1345 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1346 sizeof(struct in_addr
) > ipt
->ipt_len
)
1348 ipaddr
.sin_addr
= dst
;
1349 ia
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
,
1353 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
1354 sizeof(struct in_addr
));
1355 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1358 case IPOPT_TS_PRESPEC
:
1359 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1360 sizeof(struct in_addr
) > ipt
->ipt_len
)
1362 (void)memcpy(&ipaddr
.sin_addr
, sin
,
1363 sizeof(struct in_addr
));
1364 if (ifa_ifwithaddr((SA
)&ipaddr
) == 0)
1366 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1373 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
1375 ipt
->ipt_ptr
+= sizeof(n_time
);
1378 if (forward
&& ipforwarding
) {
1384 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2; /* XXX icmp_error adds in hdr length */
1385 icmp_error(m
, type
, code
, 0, 0);
1386 ipstat
.ips_badoptions
++;
1391 * Given address of next destination (final or next hop),
1392 * return internet address info of interface to be used to get there.
1394 static struct in_ifaddr
*
1398 register struct sockaddr_in
*sin
;
1400 sin
= (struct sockaddr_in
*) &ipforward_rt
.ro_dst
;
1402 if (ipforward_rt
.ro_rt
== 0 || dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
1403 if (ipforward_rt
.ro_rt
) {
1404 RTFREE(ipforward_rt
.ro_rt
);
1405 ipforward_rt
.ro_rt
= 0;
1407 sin
->sin_family
= AF_INET
;
1408 sin
->sin_len
= sizeof(*sin
);
1409 sin
->sin_addr
= dst
;
1411 rtalloc_ign(&ipforward_rt
, RTF_PRCLONING
);
1413 if (ipforward_rt
.ro_rt
== 0)
1414 return ((struct in_ifaddr
*)0);
1415 return ((struct in_ifaddr
*) ipforward_rt
.ro_rt
->rt_ifa
);
1419 * Save incoming source route for use in replies,
1420 * to be picked up later by ip_srcroute if the receiver is interested.
1423 save_rte(option
, dst
)
1429 olen
= option
[IPOPT_OLEN
];
1432 printf("save_rte: olen %d\n", olen
);
1434 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
1436 bcopy(option
, ip_srcrt
.srcopt
, olen
);
1437 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
1442 * Retrieve incoming source route for use in replies,
1443 * in the same form used by setsockopt.
1444 * The first hop is placed before the options, will be removed later.
1449 register struct in_addr
*p
, *q
;
1450 register struct mbuf
*m
;
1453 return ((struct mbuf
*)0);
1454 m
= m_get(M_DONTWAIT
, MT_HEADER
);
1456 return ((struct mbuf
*)0);
1458 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1460 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1461 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
1465 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
1469 * First save first hop for return route
1471 p
= &ip_srcrt
.route
[ip_nhops
- 1];
1472 *(mtod(m
, struct in_addr
*)) = *p
--;
1475 printf(" hops %lx", (u_long
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
1479 * Copy option fields and padding (nop) to mbuf.
1481 ip_srcrt
.nop
= IPOPT_NOP
;
1482 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
1483 (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
),
1484 &ip_srcrt
.nop
, OPTSIZ
);
1485 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
1486 sizeof(struct in_addr
) + OPTSIZ
);
1489 * Record return path as an IP source route,
1490 * reversing the path (pointers are now aligned).
1492 while (p
>= ip_srcrt
.route
) {
1495 printf(" %lx", (u_long
)ntohl(q
->s_addr
));
1500 * Last hop goes to final destination.
1505 printf(" %lx\n", (u_long
)ntohl(q
->s_addr
));
1511 * Strip out IP options, at higher
1512 * level protocol in the kernel.
1513 * Second argument is buffer to which options
1514 * will be moved, and return value is their length.
1515 * XXX should be deleted; last arg currently ignored.
1518 ip_stripoptions(m
, mopt
)
1519 register struct mbuf
*m
;
1523 struct ip
*ip
= mtod(m
, struct ip
*);
1524 register caddr_t opts
;
1527 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1528 opts
= (caddr_t
)(ip
+ 1);
1529 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
1530 bcopy(opts
+ olen
, opts
, (unsigned)i
);
1532 if (m
->m_flags
& M_PKTHDR
)
1533 m
->m_pkthdr
.len
-= olen
;
1534 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
1537 u_char inetctlerrmap
[PRC_NCMDS
] = {
1539 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
1540 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
1541 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
1547 * Forward a packet. If some error occurs return the sender
1548 * an icmp packet. Note we can't always generate a meaningful
1549 * icmp message because icmp doesn't have a large enough repertoire
1550 * of codes and types.
1552 * If not forwarding, just drop the packet. This could be confusing
1553 * if ipforwarding was zero but some routing protocol was advancing
1554 * us as a gateway to somewhere. However, we must let the routing
1555 * protocol deal with that.
1557 * The srcrt parameter indicates whether the packet is being forwarded
1558 * via a source route.
1564 ip_forward(m
, srcrt
)
1568 register struct ip
*ip
= mtod(m
, struct ip
*);
1569 register struct sockaddr_in
*sin
;
1570 register struct rtentry
*rt
;
1571 int error
, type
= 0, code
= 0;
1574 struct ifnet
*destifp
;
1576 struct ifnet dummyifp
;
1582 printf("forward: src %lx dst %lx ttl %x\n",
1583 (u_long
)ip
->ip_src
.s_addr
, (u_long
)ip
->ip_dst
.s_addr
,
1588 if (m
->m_flags
& M_BCAST
|| in_canforward(ip
->ip_dst
) == 0) {
1589 ipstat
.ips_cantforward
++;
1594 if (ip
->ip_ttl
<= IPTTLDEC
) {
1595 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
, dest
, 0);
1598 ip
->ip_ttl
-= IPTTLDEC
;
1603 struct route
*ipfw_rt
;
1605 if ((ipfw_rt
= pm_route(m
)) != NULL
)
1607 mcopy
= m_copy(m
, 0, imin((int)ip
->ip_len
, 64));
1609 ipsec_setsocket(m
, NULL
);
1611 error
= ip_output(m
, (struct mbuf
*)0, ipfw_rt
,
1612 IP_FORWARDING
| IP_PROTOCOLROUTE
, 0);
1619 sin
= (struct sockaddr_in
*)&ipforward_rt
.ro_dst
;
1620 if ((rt
= ipforward_rt
.ro_rt
) == 0 ||
1621 ip
->ip_dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
1622 if (ipforward_rt
.ro_rt
) {
1623 RTFREE(ipforward_rt
.ro_rt
);
1624 ipforward_rt
.ro_rt
= 0;
1626 sin
->sin_family
= AF_INET
;
1627 sin
->sin_len
= sizeof(*sin
);
1628 sin
->sin_addr
= ip
->ip_dst
;
1630 rtalloc_ign(&ipforward_rt
, RTF_PRCLONING
);
1631 if (ipforward_rt
.ro_rt
== 0) {
1632 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
1635 rt
= ipforward_rt
.ro_rt
;
1639 * Save at most 64 bytes of the packet in case
1640 * we need to generate an ICMP message to the src.
1642 mcopy
= m_copy(m
, 0, imin((int)ip
->ip_len
, 64));
1645 * If forwarding packet using same interface that it came in on,
1646 * perhaps should send a redirect to sender to shortcut a hop.
1647 * Only send redirect if source is sending directly to us,
1648 * and if packet was not source routed (or has any options).
1649 * Also, don't send redirect if forwarding using a default route
1650 * or a route modified by a redirect.
1652 #define satosin(sa) ((struct sockaddr_in *)(sa))
1653 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
1654 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
1655 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
1656 ipsendredirects
&& !srcrt
) {
1657 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1658 u_long src
= ntohl(ip
->ip_src
.s_addr
);
1661 (src
& RTA(rt
)->ia_subnetmask
) == RTA(rt
)->ia_subnet
) {
1662 if (rt
->rt_flags
& RTF_GATEWAY
)
1663 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
1665 dest
= ip
->ip_dst
.s_addr
;
1666 /* Router requirements says to only send host redirects */
1667 type
= ICMP_REDIRECT
;
1668 code
= ICMP_REDIRECT_HOST
;
1671 printf("redirect (%d) to %lx\n", code
, (u_long
)dest
);
1677 ipsec_setsocket(m
, NULL
);
1679 error
= ip_output(m
, (struct mbuf
*)0, &ipforward_rt
,
1685 ipstat
.ips_cantforward
++;
1687 ipstat
.ips_forward
++;
1689 ipstat
.ips_redirectsent
++;
1692 ipflow_create(&ipforward_rt
, mcopy
);
1704 case 0: /* forwarded, but need redirect */
1705 /* type, code set above */
1708 case ENETUNREACH
: /* shouldn't happen, checked above */
1713 type
= ICMP_UNREACH
;
1714 code
= ICMP_UNREACH_HOST
;
1718 type
= ICMP_UNREACH
;
1719 code
= ICMP_UNREACH_NEEDFRAG
;
1721 if (ipforward_rt
.ro_rt
)
1722 destifp
= ipforward_rt
.ro_rt
->rt_ifp
;
1725 * If the packet is routed over IPsec tunnel, tell the
1726 * originator the tunnel MTU.
1727 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1730 if (ipforward_rt
.ro_rt
) {
1731 struct secpolicy
*sp
= NULL
;
1736 sp
= ipsec4_getpolicybyaddr(mcopy
,
1742 destifp
= ipforward_rt
.ro_rt
->rt_ifp
;
1744 /* count IPsec header size */
1745 ipsechdr
= ipsec4_hdrsiz(mcopy
,
1750 * find the correct route for outer IPv4
1751 * header, compute tunnel MTU.
1754 * The "dummyifp" code relies upon the fact
1755 * that icmp_error() touches only ifp->if_mtu.
1760 && sp
->req
->sav
!= NULL
1761 && sp
->req
->sav
->sah
!= NULL
) {
1762 ro
= &sp
->req
->sav
->sah
->sa_route
;
1763 if (ro
->ro_rt
&& ro
->ro_rt
->rt_ifp
) {
1765 ro
->ro_rt
->rt_ifp
->if_mtu
;
1766 dummyifp
.if_mtu
-= ipsechdr
;
1767 destifp
= &dummyifp
;
1775 ipstat
.ips_cantfrag
++;
1779 type
= ICMP_SOURCEQUENCH
;
1783 icmp_error(mcopy
, type
, code
, dest
, destifp
);
1787 ip_savecontrol(inp
, mp
, ip
, m
)
1788 register struct inpcb
*inp
;
1789 register struct mbuf
**mp
;
1790 register struct ip
*ip
;
1791 register struct mbuf
*m
;
1793 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
1797 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
1798 SCM_TIMESTAMP
, SOL_SOCKET
);
1800 mp
= &(*mp
)->m_next
;
1802 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
1803 *mp
= sbcreatecontrol((caddr_t
) &ip
->ip_dst
,
1804 sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
);
1806 mp
= &(*mp
)->m_next
;
1810 * Moving these out of udp_input() made them even more broken
1811 * than they already were.
1813 /* options were tossed already */
1814 if (inp
->inp_flags
& INP_RECVOPTS
) {
1815 *mp
= sbcreatecontrol((caddr_t
) opts_deleted_above
,
1816 sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
);
1818 mp
= &(*mp
)->m_next
;
1820 /* ip_srcroute doesn't do what we want here, need to fix */
1821 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
1822 *mp
= sbcreatecontrol((caddr_t
) ip_srcroute(),
1823 sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
);
1825 mp
= &(*mp
)->m_next
;
1828 if (inp
->inp_flags
& INP_RECVIF
) {
1831 struct sockaddr_dl sdl
;
1834 struct sockaddr_dl
*sdp
;
1835 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
1837 if (((ifp
= m
->m_pkthdr
.rcvif
))
1838 && ( ifp
->if_index
&& (ifp
->if_index
<= if_index
))) {
1839 sdp
= (struct sockaddr_dl
*)(ifnet_addrs
1840 [ifp
->if_index
- 1]->ifa_addr
);
1842 * Change our mind and don't try copy.
1844 if ((sdp
->sdl_family
!= AF_LINK
)
1845 || (sdp
->sdl_len
> sizeof(sdlbuf
))) {
1848 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
1852 = offsetof(struct sockaddr_dl
, sdl_data
[0]);
1853 sdl2
->sdl_family
= AF_LINK
;
1854 sdl2
->sdl_index
= 0;
1855 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
1857 *mp
= sbcreatecontrol((caddr_t
) sdl2
, sdl2
->sdl_len
,
1858 IP_RECVIF
, IPPROTO_IP
);
1860 mp
= &(*mp
)->m_next
;
1865 ip_rsvp_init(struct socket
*so
)
1867 if (so
->so_type
!= SOCK_RAW
||
1868 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
1871 if (ip_rsvpd
!= NULL
)
1876 * This may seem silly, but we need to be sure we don't over-increment
1877 * the RSVP counter, in case something slips up.
1892 * This may seem silly, but we need to be sure we don't over-decrement
1893 * the RSVP counter, in case something slips up.