2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1982, 1986, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
58 * $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.25 2001/08/29 21:41:37 jesper Exp $
63 #include <sys/param.h>
64 #include <sys/systm.h>
66 #include <sys/malloc.h>
67 #include <sys/domain.h>
68 #include <sys/protosw.h>
69 #include <sys/socket.h>
71 #include <sys/kernel.h>
72 #include <sys/syslog.h>
73 #include <sys/sysctl.h>
75 #include <kern/queue.h>
78 #include <net/if_var.h>
79 #include <net/if_dl.h>
80 #include <net/route.h>
81 #include <net/netisr.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_pcb.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/ip_icmp.h>
90 #include <sys/socketvar.h>
92 #include <netinet/ip_fw.h>
94 /* needed for AUTOCONFIGURING: */
95 #include <netinet/udp.h>
96 #include <netinet/udp_var.h>
97 #include <netinet/bootp.h>
99 #include <sys/kdebug.h>
101 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0)
102 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2)
103 #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8))
107 #include <netinet6/ipsec.h>
108 #include <netkey/key.h>
112 #if defined(NFAITH) && NFAITH > 0
113 #include <net/if_types.h>
117 #include <netinet/ip_dummynet.h>
121 extern int ipsec_bypass
;
125 static int ip_rsvp_on
;
126 struct socket
*ip_rsvpd
;
128 int ipforwarding
= 0;
129 SYSCTL_INT(_net_inet_ip
, IPCTL_FORWARDING
, forwarding
, CTLFLAG_RW
,
130 &ipforwarding
, 0, "Enable IP forwarding between interfaces");
132 static int ipsendredirects
= 1; /* XXX */
133 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
, CTLFLAG_RW
,
134 &ipsendredirects
, 0, "Enable sending IP redirects");
136 int ip_defttl
= IPDEFTTL
;
137 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
,
138 &ip_defttl
, 0, "Maximum TTL on IP packets");
140 static int ip_dosourceroute
= 0;
141 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
, CTLFLAG_RW
,
142 &ip_dosourceroute
, 0, "Enable forwarding source routed IP packets");
144 static int ip_acceptsourceroute
= 0;
145 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,
146 CTLFLAG_RW
, &ip_acceptsourceroute
, 0,
147 "Enable accepting source routed IP packets");
149 static int ip_keepfaith
= 0;
150 SYSCTL_INT(_net_inet_ip
, IPCTL_KEEPFAITH
, keepfaith
, CTLFLAG_RW
,
152 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
154 static int ip_nfragpackets
= 0;
155 static int ip_maxfragpackets
; /* initialized in ip_init() */
156 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragpackets
, CTLFLAG_RW
,
157 &ip_maxfragpackets
, 0,
158 "Maximum number of IPv4 fragment reassembly queue entries");
161 * XXX - Setting ip_checkinterface mostly implements the receive side of
162 * the Strong ES model described in RFC 1122, but since the routing table
163 * and transmit implementation do not implement the Strong ES model,
164 * setting this to 1 results in an odd hybrid.
166 * XXX - ip_checkinterface currently must be disabled if you use ipnat
167 * to translate the destination address to another local interface.
169 * XXX - ip_checkinterface must be disabled if you add IP aliases
170 * to the loopback interface instead of the interface where the
171 * packets for those addresses are received.
173 static int ip_checkinterface
= 0;
174 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_interface
, CTLFLAG_RW
,
175 &ip_checkinterface
, 0, "Verify packet arrives on correct interface");
178 static int ipprintfs
= 0;
181 extern struct domain inetdomain
;
182 extern struct protosw inetsw
[];
183 struct protosw
*ip_protox
[IPPROTO_MAX
];
184 static int ipqmaxlen
= IFQ_MAXLEN
;
185 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
186 struct ifqueue ipintrq
;
187 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQMAXLEN
, intr_queue_maxlen
, CTLFLAG_RW
,
188 &ipintrq
.ifq_maxlen
, 0, "Maximum size of the IP input queue");
189 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQDROPS
, intr_queue_drops
, CTLFLAG_RD
,
190 &ipintrq
.ifq_drops
, 0, "Number of packets dropped from the IP input queue");
192 struct ipstat ipstat
;
193 SYSCTL_STRUCT(_net_inet_ip
, IPCTL_STATS
, stats
, CTLFLAG_RD
,
194 &ipstat
, ipstat
, "IP statistics (struct ipstat, netinet/ip_var.h)");
196 /* Packet reassembly stuff */
197 #define IPREASS_NHASH_LOG2 6
198 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
199 #define IPREASS_HMASK (IPREASS_NHASH - 1)
200 #define IPREASS_HASH(x,y) \
201 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
203 static struct ipq ipq
[IPREASS_NHASH
];
204 static int nipq
= 0; /* total # of reass queues */
206 const int ipintrq_present
= 1;
209 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
,
210 &ip_mtu
, 0, "Default MTU");
214 static int ipstealth
= 0;
215 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, stealth
, CTLFLAG_RW
,
221 ip_fw_chk_t
*ip_fw_chk_ptr
;
222 ip_fw_ctl_t
*ip_fw_ctl_ptr
;
226 ip_dn_ctl_t
*ip_dn_ctl_ptr
;
229 int (*fr_checkp
) __P((struct ip
*, int, struct ifnet
*, int, struct mbuf
**)) = NULL
;
231 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
, CTLFLAG_RW
, 0, "link local");
233 struct ip_linklocal_stat ip_linklocal_stat
;
234 SYSCTL_STRUCT(_net_inet_ip_linklocal
, OID_AUTO
, stat
, CTLFLAG_RD
,
235 &ip_linklocal_stat
, ip_linklocal_stat
,
236 "Number of link local packets with TTL less than 255");
238 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
, CTLFLAG_RW
, 0, "link local input");
240 int ip_linklocal_in_allowbadttl
= 0;
241 SYSCTL_INT(_net_inet_ip_linklocal_in
, OID_AUTO
, allowbadttl
, CTLFLAG_RW
,
242 &ip_linklocal_in_allowbadttl
, 0,
243 "Allow incoming link local packets with TTL less than 255");
247 * We need to save the IP options in case a protocol wants to respond
248 * to an incoming packet over the same route if the packet got here
249 * using IP source routing. This allows connection establishment and
250 * maintenance when the remote end is on a network that is not known
253 static int ip_nhops
= 0;
254 static struct ip_srcrt
{
255 struct in_addr dst
; /* final destination */
256 char nop
; /* one NOP to align */
257 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
258 struct in_addr route
[MAX_IPOPTLEN
/sizeof(struct in_addr
)];
261 struct sockaddr_in
*ip_fw_fwd_addr
;
264 extern struct mbuf
* m_dup(register struct mbuf
*m
, int how
);
267 static void save_rte
__P((u_char
*, struct in_addr
));
268 static int ip_dooptions
__P((struct mbuf
*));
269 static void ip_forward
__P((struct mbuf
*, int));
270 static void ip_freef
__P((struct ipq
*));
273 static struct mbuf
*ip_reass
__P((struct mbuf
*,
274 struct ipq
*, struct ipq
*, u_int32_t
*, u_int16_t
*));
276 static struct mbuf
*ip_reass
__P((struct mbuf
*,
277 struct ipq
*, struct ipq
*, u_int16_t
*, u_int16_t
*));
280 static struct mbuf
*ip_reass
__P((struct mbuf
*, struct ipq
*, struct ipq
*));
282 static struct in_ifaddr
*ip_rtaddr
__P((struct in_addr
));
283 void ipintr
__P((void));
286 extern u_short ip_id
;
290 * IP initialization: fill in IP protocol switch table.
291 * All protocols not implemented in kernel go to raw IP protocol handler.
296 register struct protosw
*pr
;
298 static ip_initialized
= 0;
302 TAILQ_INIT(&in_ifaddrhead
);
303 pr
= pffindproto(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
306 for (i
= 0; i
< IPPROTO_MAX
; i
++)
308 for (pr
= inetdomain
.dom_protosw
; pr
; pr
= pr
->pr_next
)
309 { if(!((unsigned int)pr
->pr_domain
)) continue; /* If uninitialized, skip */
310 if (pr
->pr_domain
->dom_family
== PF_INET
&&
311 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
)
312 ip_protox
[pr
->pr_protocol
] = pr
;
314 for (i
= 0; i
< IPREASS_NHASH
; i
++)
315 ipq
[i
].next
= ipq
[i
].prev
= &ipq
[i
];
317 maxnipq
= nmbclusters
/ 4;
318 ip_maxfragpackets
= nmbclusters
/ 4;
321 ip_id
= time_second
& 0xffff;
323 ipintrq
.ifq_maxlen
= ipqmaxlen
;
328 /* Initialize the PF_INET domain, and add in the pre-defined protos */
332 register struct protosw
*pr
;
333 register struct domain
*dp
;
334 static inetdomain_initted
= 0;
335 extern int in_proto_count
;
337 if (!inetdomain_initted
)
339 kprintf("Initing %d protosw entries\n", in_proto_count
);
342 for (i
=0, pr
= &inetsw
[0]; i
<in_proto_count
; i
++, pr
++)
343 net_add_proto(pr
, dp
);
344 inetdomain_initted
= 1;
348 static struct sockaddr_in ipaddr
= { sizeof(ipaddr
), AF_INET
};
349 static struct route ipforward_rt
;
352 * Ip input routine. Checksum and byte swap header. If fragmented
353 * try to reassemble. Process options. Pass to next level.
356 ip_input(struct mbuf
*m
)
360 struct in_ifaddr
*ia
= NULL
;
361 int i
, hlen
, mff
, checkif
;
363 u_int16_t divert_cookie
; /* firewall cookie */
364 struct in_addr pkt_dst
;
366 u_int32_t divert_info
= 0; /* packet divert/tee info */
368 struct ip_fw_chain
*rule
= NULL
;
371 /* Get and reset firewall cookie */
372 divert_cookie
= ip_divert_cookie
;
373 ip_divert_cookie
= 0;
378 #if IPFIREWALL && DUMMYNET
380 * dummynet packet are prepended a vestigial mbuf with
381 * m_type = MT_DUMMYNET and m_data pointing to the matching
384 if (m
->m_type
== MT_DUMMYNET
) {
385 rule
= (struct ip_fw_chain
*)(m
->m_data
) ;
387 ip
= mtod(m
, struct ip
*);
388 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
395 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
396 panic("ip_input no HDR");
400 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
403 if (m
->m_len
< sizeof (struct ip
) &&
404 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
405 ipstat
.ips_toosmall
++;
408 ip
= mtod(m
, struct ip
*);
410 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
411 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
413 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
414 ipstat
.ips_badvers
++;
418 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
419 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
420 ipstat
.ips_badhlen
++;
423 if (hlen
> m
->m_len
) {
424 if ((m
= m_pullup(m
, hlen
)) == 0) {
425 ipstat
.ips_badhlen
++;
428 ip
= mtod(m
, struct ip
*);
431 /* 127/8 must not appear on wire - RFC1122 */
432 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
433 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
434 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
436 ipstat
.ips_badaddr
++;
442 /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
443 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
444 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
445 ip_linklocal_stat
.iplls_in_total
++;
446 if (ip
->ip_ttl
!= MAXTTL
) {
447 ip_linklocal_stat
.iplls_in_badttl
++;
448 /* Silently drop link local traffic with bad TTL */
449 if (ip_linklocal_in_allowbadttl
!= 0)
453 if (m
->m_pkthdr
.rcvif
->if_hwassist
== 0)
454 m
->m_pkthdr
.csum_flags
= 0;
456 if ((m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) && ip
->ip_p
!= IPPROTO_TCP
)
457 m
->m_pkthdr
.csum_flags
= 0;
459 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
460 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
462 sum
= in_cksum(m
, hlen
);
470 * Convert fields to host representation.
473 if (ip
->ip_len
< hlen
) {
480 * Check that the amount of data in the buffers
481 * is as at least much as the IP header would have us expect.
482 * Trim mbufs if longer than we expect.
483 * Drop packet if shorter than we expect.
485 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
487 ipstat
.ips_tooshort
++;
490 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
491 /* Invalidate hwcksuming */
492 m
->m_pkthdr
.csum_flags
= 0;
493 m
->m_pkthdr
.csum_data
= 0;
495 if (m
->m_len
== m
->m_pkthdr
.len
) {
496 m
->m_len
= ip
->ip_len
;
497 m
->m_pkthdr
.len
= ip
->ip_len
;
499 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
503 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
509 * Right now when no processing on packet has done
510 * and it is still fresh out of network we do our black
512 * - Firewall: deny/allow/divert
513 * - Xlate: translate packet's addr/port (NAT).
514 * - Pipe: pass pkt through dummynet.
515 * - Wrap: fake packet's addr/port <unimpl.>
516 * - Encapsulate: put it in another IP and send out. <unimp.>
519 #if defined(IPFIREWALL) && defined(DUMMYNET)
523 * Check if we want to allow this packet to be processed.
524 * Consider it to be bad if not.
529 if ((*fr_checkp
)(ip
, hlen
, m
->m_pkthdr
.rcvif
, 0, &m1
) || !m1
)
531 ip
= mtod(m
= m1
, struct ip
*);
533 if (fw_enable
&& ip_fw_chk_ptr
) {
534 #if IPFIREWALL_FORWARD
536 * If we've been forwarded from the output side, then
537 * skip the firewall a second time
541 #endif /* IPFIREWALL_FORWARD */
543 * See the comment in ip_output for the return values
544 * produced by the firewall.
546 i
= (*ip_fw_chk_ptr
)(&ip
,
547 hlen
, NULL
, &divert_cookie
, &m
, &rule
, &ip_fw_fwd_addr
);
548 if ( (i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
553 ip
= mtod(m
, struct ip
*); /* just in case m changed */
554 if (i
== 0 && ip_fw_fwd_addr
== NULL
) /* common case */
557 if ((i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
558 /* send packet to the appropriate pipe */
559 dummynet_io(i
&0xffff,DN_TO_IP_IN
,m
,NULL
,NULL
,0, rule
);
564 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
565 /* Divert or tee packet */
570 #if IPFIREWALL_FORWARD
571 if (i
== 0 && ip_fw_fwd_addr
!= NULL
)
575 * if we get here, the packet must be dropped
583 * Process options and, if not destined for us,
584 * ship it on. ip_dooptions returns 1 when an
585 * error was detected (causing an icmp message
586 * to be sent and the original packet to be freed).
588 ip_nhops
= 0; /* for source routed packets */
589 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
)) {
590 #if IPFIREWALL_FORWARD
591 ip_fw_fwd_addr
= NULL
;
596 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
597 * matter if it is destined to another node, or whether it is
598 * a multicast one, RSVP wants it! and prevents it from being forwarded
599 * anywhere else. Also checks if the rsvp daemon is running before
600 * grabbing the packet.
602 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
606 * Check our list of addresses, to see if the packet is for us.
607 * If we don't have any addresses, assume any unicast packet
608 * we receive might be for us (and let the upper layers deal
611 if (TAILQ_EMPTY(&in_ifaddrhead
) &&
612 (m
->m_flags
& (M_MCAST
|M_BCAST
)) == 0)
616 * Cache the destination address of the packet; this may be
617 * changed by use of 'ipfw fwd'.
619 pkt_dst
= ip_fw_fwd_addr
== NULL
?
620 ip
->ip_dst
: ip_fw_fwd_addr
->sin_addr
;
623 * Enable a consistency check between the destination address
624 * and the arrival interface for a unicast packet (the RFC 1122
625 * strong ES model) if IP forwarding is disabled and the packet
626 * is not locally generated and the packet is not subject to
629 * XXX - Checking also should be disabled if the destination
630 * address is ipnat'ed to a different interface.
632 * XXX - Checking is incompatible with IP aliases added
633 * to the loopback interface instead of the interface where
634 * the packets are received.
636 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
637 ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) &&
638 (ip_fw_fwd_addr
== NULL
);
640 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) {
641 #define satosin(sa) ((struct sockaddr_in *)(sa))
643 if (IA_SIN(ia
)->sin_addr
.s_addr
== INADDR_ANY
)
647 * If the address matches, verify that the packet
648 * arrived via the correct interface if checking is
651 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
652 (!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
))
655 * Only accept broadcast packets that arrive via the
656 * matching interface. Reception of forwarded directed
657 * broadcasts would be handled via ip_forward() and
658 * ether_output() with the loopback into the stack for
659 * SIMPLEX interfaces handled by ether_output().
661 if (ia
->ia_ifp
== m
->m_pkthdr
.rcvif
&&
662 ia
->ia_ifp
&& ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) {
663 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
666 if (ia
->ia_netbroadcast
.s_addr
== pkt_dst
.s_addr
)
670 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
671 struct in_multi
*inm
;
674 * If we are acting as a multicast router, all
675 * incoming multicast packets are passed to the
676 * kernel-level multicast forwarding function.
677 * The packet is returned (relatively) intact; if
678 * ip_mforward() returns a non-zero value, the packet
679 * must be discarded, else it may be accepted below.
681 if (ip_mforward(ip
, m
->m_pkthdr
.rcvif
, m
, 0) != 0) {
682 ipstat
.ips_cantforward
++;
688 * The process-level routing demon needs to receive
689 * all multicast IGMP packets, whether or not this
690 * host belongs to their destination groups.
692 if (ip
->ip_p
== IPPROTO_IGMP
)
694 ipstat
.ips_forward
++;
697 * See if we belong to the destination multicast group on the
700 IN_LOOKUP_MULTI(ip
->ip_dst
, m
->m_pkthdr
.rcvif
, inm
);
702 ipstat
.ips_notmember
++;
708 if (ip
->ip_dst
.s_addr
== (u_long
)INADDR_BROADCAST
)
710 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
713 /* Allow DHCP/BootP responses through */
714 if (m
->m_pkthdr
.rcvif
!= NULL
715 && (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_AUTOCONFIGURING
)
716 && hlen
== sizeof(struct ip
)
717 && ip
->ip_p
== IPPROTO_UDP
) {
719 if (m
->m_len
< sizeof(struct udpiphdr
)
720 && (m
= m_pullup(m
, sizeof(struct udpiphdr
))) == 0) {
721 udpstat
.udps_hdrops
++;
724 ui
= mtod(m
, struct udpiphdr
*);
725 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
728 ip
= mtod(m
, struct ip
*); /* in case it changed */
731 #if defined(NFAITH) && 0 < NFAITH
733 * FAITH(Firewall Aided Internet Translator)
735 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
737 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_ICMP
)
745 * Not for us; forward if possible and desirable.
747 if (ipforwarding
== 0) {
748 ipstat
.ips_cantforward
++;
752 #if IPFIREWALL_FORWARD
753 ip_fw_fwd_addr
= NULL
;
759 /* Darwin does not have an if_data in ifaddr */
760 /* Count the packet in the ip address stats */
762 ia
->ia_ifa
.if_ipackets
++;
763 ia
->ia_ifa
.if_ibytes
+= m
->m_pkthdr
.len
;
768 * If offset or IP_MF are set, must reassemble.
769 * Otherwise, nothing need be done.
770 * (We could look in the reassembly queue to see
771 * if the packet was previously fragmented,
772 * but it's not worth the time; just let them time out.)
774 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
777 * Reassembly should be able to treat a mbuf cluster, for later
778 * operation of contiguous protocol headers on the cluster. (KAME)
780 if (m
->m_flags
& M_EXT
) { /* XXX */
781 if ((m
= m_pullup(m
, hlen
)) == 0) {
782 ipstat
.ips_toosmall
++;
783 #if IPFIREWALL_FORWARD
784 ip_fw_fwd_addr
= NULL
;
788 ip
= mtod(m
, struct ip
*);
791 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
793 * Look for queue of fragments
796 for (fp
= ipq
[sum
].next
; fp
!= &ipq
[sum
]; fp
= fp
->next
)
797 if (ip
->ip_id
== fp
->ipq_id
&&
798 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
799 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
800 ip
->ip_p
== fp
->ipq_p
)
805 /* check if there's a place for the new queue */
806 if (nipq
> maxnipq
) {
808 * drop something from the tail of the current queue
809 * before proceeding further
811 if (ipq
[sum
].prev
== &ipq
[sum
]) { /* gak */
812 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
813 if (ipq
[i
].prev
!= &ipq
[i
]) {
814 ip_freef(ipq
[i
].prev
);
819 ip_freef(ipq
[sum
].prev
);
823 * Adjust ip_len to not reflect header,
824 * set ip_mff if more fragments are expected,
825 * convert offset of this to bytes.
828 mff
= (ip
->ip_off
& IP_MF
) != 0;
831 * Make sure that fragments have a data length
832 * that's a non-zero multiple of 8 bytes.
834 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
835 ipstat
.ips_toosmall
++; /* XXX */
838 m
->m_flags
|= M_FRAG
;
843 * If datagram marked as having more fragments
844 * or if this is not the first fragment,
845 * attempt reassembly; if it succeeds, proceed.
847 if (mff
|| ip
->ip_off
) {
848 ipstat
.ips_fragments
++;
849 m
->m_pkthdr
.header
= ip
;
852 fp
, &ipq
[sum
], &divert_info
, &divert_cookie
);
854 m
= ip_reass(m
, fp
, &ipq
[sum
]);
857 #if IPFIREWALL_FORWARD
858 ip_fw_fwd_addr
= NULL
;
862 ipstat
.ips_reassembled
++;
863 ip
= mtod(m
, struct ip
*);
864 /* Get the header length of the reassembled packet */
865 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
867 /* Restore original checksum before diverting packet */
868 if (divert_info
!= 0) {
873 ip
->ip_sum
= in_cksum(m
, hlen
);
887 * Divert or tee packet to the divert protocol if required.
889 * If divert_info is zero then cookie should be too, so we shouldn't
890 * need to clear them here. Assume divert_packet() does so also.
892 if (divert_info
!= 0) {
893 struct mbuf
*clone
= NULL
;
895 /* Clone packet if we're doing a 'tee' */
896 if ((divert_info
& IP_FW_PORT_TEE_FLAG
) != 0)
897 clone
= m_dup(m
, M_DONTWAIT
);
899 /* Restore packet header fields to original values */
904 /* Deliver packet to divert input routine */
905 ip_divert_cookie
= divert_cookie
;
906 divert_packet(m
, 1, divert_info
& 0xffff);
907 ipstat
.ips_delivered
++;
909 /* If 'tee', continue with original packet */
913 ip
= mtod(m
, struct ip
*);
919 * enforce IPsec policy checking if we are seeing last header.
920 * note that we do not visit this with protocols with pcb layer
921 * code - like udp/tcp/raw ip.
923 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
) != 0 &&
924 ipsec4_in_reject(m
, NULL
)) {
925 ipsecstat
.in_polvio
++;
931 * Switch out to protocol's input routine.
933 ipstat
.ips_delivered
++;
935 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
936 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
938 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
939 #if IPFIREWALL_FORWARD
940 ip_fw_fwd_addr
= NULL
; /* tcp needed it */
945 #if IPFIREWALL_FORWARD
946 ip_fw_fwd_addr
= NULL
;
948 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
953 * IP software interrupt routine - to go away sometime soon
961 KERNEL_DEBUG(DBG_FNC_IP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
965 IF_DEQUEUE(&ipintrq
, m
);
968 KERNEL_DEBUG(DBG_FNC_IP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
976 NETISR_SET(NETISR_IP
, ipintr
);
979 * Take incoming datagram fragment and try to reassemble it into
980 * whole datagram. If a chain for reassembly of this datagram already
981 * exists, then it is given as fp; otherwise have to make a chain.
983 * When IPDIVERT enabled, keep additional state with each packet that
984 * tells us if we need to divert or tee the packet we're building.
989 ip_reass(m
, fp
, where
, divinfo
, divcookie
)
991 ip_reass(m
, fp
, where
)
993 register struct mbuf
*m
;
994 register struct ipq
*fp
;
1002 u_int16_t
*divcookie
;
1005 struct ip
*ip
= mtod(m
, struct ip
*);
1006 register struct mbuf
*p
= 0, *q
, *nq
;
1008 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1012 * Presence of header sizes in mbufs
1013 * would confuse code below.
1018 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1019 m
->m_pkthdr
.csum_flags
= 0;
1021 * If first fragment to arrive, create a reassembly queue.
1025 * Enforce upper bound on number of fragmented packets
1026 * for which we attempt reassembly;
1027 * If maxfrag is 0, never accept fragments.
1028 * If maxfrag is -1, accept all fragments without limitation.
1030 if ((ip_maxfragpackets
>= 0) && (ip_nfragpackets
>= ip_maxfragpackets
))
1033 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
1035 fp
= mtod(t
, struct ipq
*);
1036 insque((void*)fp
, (void*)where
);
1038 fp
->ipq_ttl
= IPFRAGTTL
;
1039 fp
->ipq_p
= ip
->ip_p
;
1040 fp
->ipq_id
= ip
->ip_id
;
1041 fp
->ipq_src
= ip
->ip_src
;
1042 fp
->ipq_dst
= ip
->ip_dst
;
1044 m
->m_nextpkt
= NULL
;
1047 fp
->ipq_div_info
= 0;
1051 fp
->ipq_div_cookie
= 0;
1056 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
1059 * Find a segment which begins after this one does.
1061 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
1062 if (GETIP(q
)->ip_off
> ip
->ip_off
)
1066 * If there is a preceding segment, it may provide some of
1067 * our data already. If so, drop the data from the incoming
1068 * segment. If it provides all of our data, drop us, otherwise
1069 * stick new segment in the proper place.
1071 * If some of the data is dropped from the the preceding
1072 * segment, then it's checksum is invalidated.
1075 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
1077 if (i
>= ip
->ip_len
)
1080 m
->m_pkthdr
.csum_flags
= 0;
1084 m
->m_nextpkt
= p
->m_nextpkt
;
1087 m
->m_nextpkt
= fp
->ipq_frags
;
1092 * While we overlap succeeding segments trim them or,
1093 * if they are completely covered, dequeue them.
1095 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
1097 i
= (ip
->ip_off
+ ip
->ip_len
) -
1099 if (i
< GETIP(q
)->ip_len
) {
1100 GETIP(q
)->ip_len
-= i
;
1101 GETIP(q
)->ip_off
+= i
;
1103 q
->m_pkthdr
.csum_flags
= 0;
1115 * Transfer firewall instructions to the fragment structure.
1116 * Any fragment diverting causes the whole packet to divert.
1119 fp
->ipq_div_info
= *divinfo
;
1121 fp
->ipq_divert
= *divinfo
;
1123 fp
->ipq_div_cookie
= *divcookie
;
1129 * Check for complete reassembly.
1132 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1133 if (GETIP(q
)->ip_off
!= next
)
1135 next
+= GETIP(q
)->ip_len
;
1137 /* Make sure the last packet didn't have the IP_MF flag */
1138 if (p
->m_flags
& M_FRAG
)
1142 * Reassembly is complete. Make sure the packet is a sane size.
1146 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1147 ipstat
.ips_toolong
++;
1153 * Concatenate fragments.
1161 for (q
= nq
; q
!= NULL
; q
= nq
) {
1163 q
->m_nextpkt
= NULL
;
1164 if (q
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1165 m
->m_pkthdr
.csum_flags
= 0;
1167 m
->m_pkthdr
.csum_flags
&= q
->m_pkthdr
.csum_flags
;
1168 m
->m_pkthdr
.csum_data
+= q
->m_pkthdr
.csum_data
;
1175 * Extract firewall instructions from the fragment structure.
1178 *divinfo
= fp
->ipq_div_info
;
1180 *divinfo
= fp
->ipq_divert
;
1182 *divcookie
= fp
->ipq_div_cookie
;
1186 * Create header for new ip packet by
1187 * modifying header of first packet;
1188 * dequeue and discard fragment reassembly header.
1189 * Make header visible.
1192 ip
->ip_src
= fp
->ipq_src
;
1193 ip
->ip_dst
= fp
->ipq_dst
;
1196 (void) m_free(dtom(fp
));
1198 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1199 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1200 /* some debugging cruft by sklower, below, will go away soon */
1201 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
1202 register int plen
= 0;
1203 for (t
= m
; t
; t
= t
->m_next
)
1205 m
->m_pkthdr
.len
= plen
;
1214 ipstat
.ips_fragdropped
++;
1222 * Free a fragment reassembly header and all
1223 * associated datagrams.
1229 register struct mbuf
*q
;
1231 while (fp
->ipq_frags
) {
1233 fp
->ipq_frags
= q
->m_nextpkt
;
1237 (void) m_free(dtom(fp
));
1243 * IP timer processing;
1244 * if a timer expires on a reassembly
1245 * queue, discard it.
1250 register struct ipq
*fp
;
1254 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1258 while (fp
!= &ipq
[i
]) {
1261 if (fp
->prev
->ipq_ttl
== 0) {
1262 ipstat
.ips_fragtimeout
++;
1268 * If we are over the maximum number of fragments
1269 * (due to the limit being lowered), drain off
1270 * enough to get down to the new limit.
1272 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1273 if (ip_maxfragpackets
>= 0) {
1274 while ((ip_nfragpackets
> ip_maxfragpackets
) &&
1275 (ipq
[i
].next
!= &ipq
[i
])) {
1276 ipstat
.ips_fragdropped
++;
1277 ip_freef(ipq
[i
].next
);
1286 * Drain off all datagram fragments.
1293 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1294 while (ipq
[i
].next
!= &ipq
[i
]) {
1295 ipstat
.ips_fragdropped
++;
1296 ip_freef(ipq
[i
].next
);
1303 * Do option processing on a datagram,
1304 * possibly discarding it if bad options are encountered,
1305 * or forwarding it if source-routed.
1306 * Returns 1 if packet has been forwarded/freed,
1307 * 0 if the packet should be processed further.
1313 register struct ip
*ip
= mtod(m
, struct ip
*);
1314 register u_char
*cp
;
1315 register struct ip_timestamp
*ipt
;
1316 register struct in_ifaddr
*ia
;
1317 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
1318 struct in_addr
*sin
, dst
;
1322 cp
= (u_char
*)(ip
+ 1);
1323 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1324 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1325 opt
= cp
[IPOPT_OPTVAL
];
1326 if (opt
== IPOPT_EOL
)
1328 if (opt
== IPOPT_NOP
)
1331 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1332 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1335 optlen
= cp
[IPOPT_OLEN
];
1336 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1337 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1347 * Source routing with record.
1348 * Find interface with current destination address.
1349 * If none on this machine then drop if strictly routed,
1350 * or do nothing if loosely routed.
1351 * Record interface address and bring up next address
1352 * component. If strictly routed make sure next
1353 * address is on directly accessible net.
1357 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1358 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1361 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1362 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1365 ipaddr
.sin_addr
= ip
->ip_dst
;
1366 ia
= (struct in_ifaddr
*)
1367 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1369 if (opt
== IPOPT_SSRR
) {
1370 type
= ICMP_UNREACH
;
1371 code
= ICMP_UNREACH_SRCFAIL
;
1374 if (!ip_dosourceroute
)
1375 goto nosourcerouting
;
1377 * Loose routing, and not at next destination
1378 * yet; nothing to do except forward.
1382 off
--; /* 0 origin */
1383 if (off
> optlen
- (int)sizeof(struct in_addr
)) {
1385 * End of source route. Should be for us.
1387 if (!ip_acceptsourceroute
)
1388 goto nosourcerouting
;
1389 save_rte(cp
, ip
->ip_src
);
1393 if (!ip_dosourceroute
) {
1395 char buf
[16]; /* aaa.bbb.ccc.ddd\0 */
1397 * Acting as a router, so generate ICMP
1400 strcpy(buf
, inet_ntoa(ip
->ip_dst
));
1402 "attempted source route from %s to %s\n",
1403 inet_ntoa(ip
->ip_src
), buf
);
1404 type
= ICMP_UNREACH
;
1405 code
= ICMP_UNREACH_SRCFAIL
;
1409 * Not acting as a router, so silently drop.
1411 ipstat
.ips_cantforward
++;
1418 * locate outgoing interface
1420 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1421 sizeof(ipaddr
.sin_addr
));
1423 if (opt
== IPOPT_SSRR
) {
1424 #define INA struct in_ifaddr *
1425 #define SA struct sockaddr *
1426 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0)
1427 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
1429 ia
= ip_rtaddr(ipaddr
.sin_addr
);
1431 type
= ICMP_UNREACH
;
1432 code
= ICMP_UNREACH_SRCFAIL
;
1435 ip
->ip_dst
= ipaddr
.sin_addr
;
1436 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1437 sizeof(struct in_addr
));
1438 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1440 * Let ip_intr's mcast routing check handle mcast pkts
1442 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1446 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1447 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1450 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1451 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1455 * If no space remains, ignore.
1457 off
--; /* 0 origin */
1458 if (off
> optlen
- (int)sizeof(struct in_addr
))
1460 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1461 sizeof(ipaddr
.sin_addr
));
1463 * locate outgoing interface; if we're the destination,
1464 * use the incoming interface (should be same).
1466 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0 &&
1467 (ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
1468 type
= ICMP_UNREACH
;
1469 code
= ICMP_UNREACH_HOST
;
1472 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1473 sizeof(struct in_addr
));
1474 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1478 code
= cp
- (u_char
*)ip
;
1479 ipt
= (struct ip_timestamp
*)cp
;
1480 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
1481 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
1484 if (ipt
->ipt_ptr
< 5) {
1485 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
1489 ipt
->ipt_len
- (int)sizeof(int32_t)) {
1490 if (++ipt
->ipt_oflw
== 0) {
1491 code
= (u_char
*)&ipt
->ipt_ptr
-
1497 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
1498 switch (ipt
->ipt_flg
) {
1500 case IPOPT_TS_TSONLY
:
1503 case IPOPT_TS_TSANDADDR
:
1504 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1505 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1506 code
= (u_char
*)&ipt
->ipt_ptr
-
1510 ipaddr
.sin_addr
= dst
;
1511 ia
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
,
1515 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
1516 sizeof(struct in_addr
));
1517 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1520 case IPOPT_TS_PRESPEC
:
1521 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1522 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1523 code
= (u_char
*)&ipt
->ipt_ptr
-
1527 (void)memcpy(&ipaddr
.sin_addr
, sin
,
1528 sizeof(struct in_addr
));
1529 if (ifa_ifwithaddr((SA
)&ipaddr
) == 0)
1531 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1535 /* XXX can't take &ipt->ipt_flg */
1536 code
= (u_char
*)&ipt
->ipt_ptr
-
1541 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
1543 ipt
->ipt_ptr
+= sizeof(n_time
);
1546 if (forward
&& ipforwarding
) {
1552 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2; /* XXX icmp_error adds in hdr length */
1553 icmp_error(m
, type
, code
, 0, 0);
1554 ipstat
.ips_badoptions
++;
1559 * Given address of next destination (final or next hop),
1560 * return internet address info of interface to be used to get there.
1562 static struct in_ifaddr
*
1566 register struct sockaddr_in
*sin
;
1568 sin
= (struct sockaddr_in
*) &ipforward_rt
.ro_dst
;
1570 if (ipforward_rt
.ro_rt
== 0 || dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
1571 if (ipforward_rt
.ro_rt
) {
1572 rtfree(ipforward_rt
.ro_rt
);
1573 ipforward_rt
.ro_rt
= 0;
1575 sin
->sin_family
= AF_INET
;
1576 sin
->sin_len
= sizeof(*sin
);
1577 sin
->sin_addr
= dst
;
1579 rtalloc_ign(&ipforward_rt
, RTF_PRCLONING
);
1581 if (ipforward_rt
.ro_rt
== 0)
1582 return ((struct in_ifaddr
*)0);
1583 return ((struct in_ifaddr
*) ipforward_rt
.ro_rt
->rt_ifa
);
1587 * Save incoming source route for use in replies,
1588 * to be picked up later by ip_srcroute if the receiver is interested.
1591 save_rte(option
, dst
)
1597 olen
= option
[IPOPT_OLEN
];
1600 printf("save_rte: olen %d\n", olen
);
1602 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
1604 bcopy(option
, ip_srcrt
.srcopt
, olen
);
1605 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
1610 * Retrieve incoming source route for use in replies,
1611 * in the same form used by setsockopt.
1612 * The first hop is placed before the options, will be removed later.
1617 register struct in_addr
*p
, *q
;
1618 register struct mbuf
*m
;
1621 return ((struct mbuf
*)0);
1622 m
= m_get(M_DONTWAIT
, MT_HEADER
);
1624 return ((struct mbuf
*)0);
1626 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1628 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1629 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
1633 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
1637 * First save first hop for return route
1639 p
= &ip_srcrt
.route
[ip_nhops
- 1];
1640 *(mtod(m
, struct in_addr
*)) = *p
--;
1643 printf(" hops %lx", (u_long
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
1647 * Copy option fields and padding (nop) to mbuf.
1649 ip_srcrt
.nop
= IPOPT_NOP
;
1650 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
1651 (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
),
1652 &ip_srcrt
.nop
, OPTSIZ
);
1653 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
1654 sizeof(struct in_addr
) + OPTSIZ
);
1657 * Record return path as an IP source route,
1658 * reversing the path (pointers are now aligned).
1660 while (p
>= ip_srcrt
.route
) {
1663 printf(" %lx", (u_long
)ntohl(q
->s_addr
));
1668 * Last hop goes to final destination.
1673 printf(" %lx\n", (u_long
)ntohl(q
->s_addr
));
1679 * Strip out IP options, at higher
1680 * level protocol in the kernel.
1681 * Second argument is buffer to which options
1682 * will be moved, and return value is their length.
1683 * XXX should be deleted; last arg currently ignored.
1686 ip_stripoptions(m
, mopt
)
1687 register struct mbuf
*m
;
1691 struct ip
*ip
= mtod(m
, struct ip
*);
1692 register caddr_t opts
;
1695 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1696 opts
= (caddr_t
)(ip
+ 1);
1697 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
1698 bcopy(opts
+ olen
, opts
, (unsigned)i
);
1700 if (m
->m_flags
& M_PKTHDR
)
1701 m
->m_pkthdr
.len
-= olen
;
1702 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
1705 u_char inetctlerrmap
[PRC_NCMDS
] = {
1707 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
1708 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
1709 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
1711 ENOPROTOOPT
, ECONNREFUSED
1715 * Forward a packet. If some error occurs return the sender
1716 * an icmp packet. Note we can't always generate a meaningful
1717 * icmp message because icmp doesn't have a large enough repertoire
1718 * of codes and types.
1720 * If not forwarding, just drop the packet. This could be confusing
1721 * if ipforwarding was zero but some routing protocol was advancing
1722 * us as a gateway to somewhere. However, we must let the routing
1723 * protocol deal with that.
1725 * The srcrt parameter indicates whether the packet is being forwarded
1726 * via a source route.
1729 ip_forward(m
, srcrt
)
1733 register struct ip
*ip
= mtod(m
, struct ip
*);
1734 register struct sockaddr_in
*sin
;
1735 register struct rtentry
*rt
;
1736 int error
, type
= 0, code
= 0;
1739 struct ifnet
*destifp
;
1741 struct ifnet dummyifp
;
1747 printf("forward: src %lx dst %lx ttl %x\n",
1748 (u_long
)ip
->ip_src
.s_addr
, (u_long
)ip
->ip_dst
.s_addr
,
1753 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || in_canforward(ip
->ip_dst
) == 0) {
1754 ipstat
.ips_cantforward
++;
1761 if (ip
->ip_ttl
<= IPTTLDEC
) {
1762 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
1770 sin
= (struct sockaddr_in
*)&ipforward_rt
.ro_dst
;
1771 if ((rt
= ipforward_rt
.ro_rt
) == 0 ||
1772 ip
->ip_dst
.s_addr
!= sin
->sin_addr
.s_addr
) {
1773 if (ipforward_rt
.ro_rt
) {
1774 rtfree(ipforward_rt
.ro_rt
);
1775 ipforward_rt
.ro_rt
= 0;
1777 sin
->sin_family
= AF_INET
;
1778 sin
->sin_len
= sizeof(*sin
);
1779 sin
->sin_addr
= ip
->ip_dst
;
1781 rtalloc_ign(&ipforward_rt
, RTF_PRCLONING
);
1782 if (ipforward_rt
.ro_rt
== 0) {
1783 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
1786 rt
= ipforward_rt
.ro_rt
;
1790 * Save the IP header and at most 8 bytes of the payload,
1791 * in case we need to generate an ICMP message to the src.
1793 * We don't use m_copy() because it might return a reference
1794 * to a shared cluster. Both this function and ip_output()
1795 * assume exclusive access to the IP header in `m', so any
1796 * data in a cluster may change before we reach icmp_error().
1798 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
1799 if (mcopy
!= NULL
) {
1800 M_COPY_PKTHDR(mcopy
, m
);
1801 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
1803 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
1809 ip
->ip_ttl
-= IPTTLDEC
;
1815 * If forwarding packet using same interface that it came in on,
1816 * perhaps should send a redirect to sender to shortcut a hop.
1817 * Only send redirect if source is sending directly to us,
1818 * and if packet was not source routed (or has any options).
1819 * Also, don't send redirect if forwarding using a default route
1820 * or a route modified by a redirect.
1822 #define satosin(sa) ((struct sockaddr_in *)(sa))
1823 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
1824 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
1825 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
1826 ipsendredirects
&& !srcrt
) {
1827 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1828 u_long src
= ntohl(ip
->ip_src
.s_addr
);
1831 (src
& RTA(rt
)->ia_subnetmask
) == RTA(rt
)->ia_subnet
) {
1832 if (rt
->rt_flags
& RTF_GATEWAY
)
1833 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
1835 dest
= ip
->ip_dst
.s_addr
;
1836 /* Router requirements says to only send host redirects */
1837 type
= ICMP_REDIRECT
;
1838 code
= ICMP_REDIRECT_HOST
;
1841 printf("redirect (%d) to %lx\n", code
, (u_long
)dest
);
1846 error
= ip_output(m
, (struct mbuf
*)0, &ipforward_rt
,
1849 ipstat
.ips_cantforward
++;
1851 ipstat
.ips_forward
++;
1853 ipstat
.ips_redirectsent
++;
1856 ipflow_create(&ipforward_rt
, mcopy
);
1868 case 0: /* forwarded, but need redirect */
1869 /* type, code set above */
1872 case ENETUNREACH
: /* shouldn't happen, checked above */
1877 type
= ICMP_UNREACH
;
1878 code
= ICMP_UNREACH_HOST
;
1882 type
= ICMP_UNREACH
;
1883 code
= ICMP_UNREACH_NEEDFRAG
;
1885 if (ipforward_rt
.ro_rt
)
1886 destifp
= ipforward_rt
.ro_rt
->rt_ifp
;
1889 * If the packet is routed over IPsec tunnel, tell the
1890 * originator the tunnel MTU.
1891 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1894 if (ipforward_rt
.ro_rt
) {
1895 struct secpolicy
*sp
= NULL
;
1901 destifp
= ipforward_rt
.ro_rt
->rt_ifp
;
1902 ipstat
.ips_cantfrag
++;
1906 sp
= ipsec4_getpolicybyaddr(mcopy
,
1912 destifp
= ipforward_rt
.ro_rt
->rt_ifp
;
1914 /* count IPsec header size */
1915 ipsechdr
= ipsec4_hdrsiz(mcopy
,
1920 * find the correct route for outer IPv4
1921 * header, compute tunnel MTU.
1924 * The "dummyifp" code relies upon the fact
1925 * that icmp_error() touches only ifp->if_mtu.
1930 && sp
->req
->sav
!= NULL
1931 && sp
->req
->sav
->sah
!= NULL
) {
1932 ro
= &sp
->req
->sav
->sah
->sa_route
;
1933 if (ro
->ro_rt
&& ro
->ro_rt
->rt_ifp
) {
1935 ro
->ro_rt
->rt_ifp
->if_mtu
;
1936 dummyifp
.if_mtu
-= ipsechdr
;
1937 destifp
= &dummyifp
;
1945 ipstat
.ips_cantfrag
++;
1949 type
= ICMP_SOURCEQUENCH
;
1953 case EACCES
: /* ipfw denied packet */
1957 icmp_error(mcopy
, type
, code
, dest
, destifp
);
1961 ip_savecontrol(inp
, mp
, ip
, m
)
1962 register struct inpcb
*inp
;
1963 register struct mbuf
**mp
;
1964 register struct ip
*ip
;
1965 register struct mbuf
*m
;
1967 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
1971 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
1972 SCM_TIMESTAMP
, SOL_SOCKET
);
1974 mp
= &(*mp
)->m_next
;
1976 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
1977 *mp
= sbcreatecontrol((caddr_t
) &ip
->ip_dst
,
1978 sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
);
1980 mp
= &(*mp
)->m_next
;
1984 * Moving these out of udp_input() made them even more broken
1985 * than they already were.
1987 /* options were tossed already */
1988 if (inp
->inp_flags
& INP_RECVOPTS
) {
1989 *mp
= sbcreatecontrol((caddr_t
) opts_deleted_above
,
1990 sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
);
1992 mp
= &(*mp
)->m_next
;
1994 /* ip_srcroute doesn't do what we want here, need to fix */
1995 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
1996 *mp
= sbcreatecontrol((caddr_t
) ip_srcroute(),
1997 sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
);
1999 mp
= &(*mp
)->m_next
;
2002 if (inp
->inp_flags
& INP_RECVIF
) {
2005 struct sockaddr_dl sdl
;
2008 struct sockaddr_dl
*sdp
;
2009 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
2011 if (((ifp
= m
->m_pkthdr
.rcvif
))
2012 && ( ifp
->if_index
&& (ifp
->if_index
<= if_index
))) {
2013 sdp
= (struct sockaddr_dl
*)(ifnet_addrs
2014 [ifp
->if_index
- 1]->ifa_addr
);
2016 * Change our mind and don't try copy.
2018 if ((sdp
->sdl_family
!= AF_LINK
)
2019 || (sdp
->sdl_len
> sizeof(sdlbuf
))) {
2022 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
2026 = offsetof(struct sockaddr_dl
, sdl_data
[0]);
2027 sdl2
->sdl_family
= AF_LINK
;
2028 sdl2
->sdl_index
= 0;
2029 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
2031 *mp
= sbcreatecontrol((caddr_t
) sdl2
, sdl2
->sdl_len
,
2032 IP_RECVIF
, IPPROTO_IP
);
2034 mp
= &(*mp
)->m_next
;
2039 ip_rsvp_init(struct socket
*so
)
2041 if (so
->so_type
!= SOCK_RAW
||
2042 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2045 if (ip_rsvpd
!= NULL
)
2050 * This may seem silly, but we need to be sure we don't over-increment
2051 * the RSVP counter, in case something slips up.
2066 * This may seem silly, but we need to be sure we don't over-decrement
2067 * the RSVP counter, in case something slips up.